[multiple changes]
[gcc.git] / gcc / ada / sem_ch3.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2010, 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_Formal_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 else
776 -- For access formals, access components, and access discriminants,
777 -- the scope is that of the enclosing declaration,
778
779 Anon_Scope := Scope (Current_Scope);
780 end if;
781
782 Anon_Type :=
783 Create_Itype
784 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
785
786 if All_Present (N)
787 and then Ada_Version >= Ada_2005
788 then
789 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
790 end if;
791
792 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
793 -- the corresponding semantic routine
794
795 if Present (Access_To_Subprogram_Definition (N)) then
796 Access_Subprogram_Declaration
797 (T_Name => Anon_Type,
798 T_Def => Access_To_Subprogram_Definition (N));
799
800 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
801 Set_Ekind
802 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
803 else
804 Set_Ekind
805 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
806 end if;
807
808 Set_Can_Use_Internal_Rep
809 (Anon_Type, not Always_Compatible_Rep_On_Target);
810
811 -- If the anonymous access is associated with a protected operation
812 -- create a reference to it after the enclosing protected definition
813 -- because the itype will be used in the subsequent bodies.
814
815 if Ekind (Current_Scope) = E_Protected_Type then
816 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
817 end if;
818
819 return Anon_Type;
820 end if;
821
822 Find_Type (Subtype_Mark (N));
823 Desig_Type := Entity (Subtype_Mark (N));
824
825 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
826 Set_Etype (Anon_Type, Anon_Type);
827
828 -- Make sure the anonymous access type has size and alignment fields
829 -- set, as required by gigi. This is necessary in the case of the
830 -- Task_Body_Procedure.
831
832 if not Has_Private_Component (Desig_Type) then
833 Layout_Type (Anon_Type);
834 end if;
835
836 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
837 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
838 -- the null value is allowed. In Ada 95 the null value is never allowed.
839
840 if Ada_Version >= Ada_2005 then
841 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
842 else
843 Set_Can_Never_Be_Null (Anon_Type, True);
844 end if;
845
846 -- The anonymous access type is as public as the discriminated type or
847 -- subprogram that defines it. It is imported (for back-end purposes)
848 -- if the designated type is.
849
850 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
851
852 -- Ada 2005 (AI-231): Propagate the access-constant attribute
853
854 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
855
856 -- The context is either a subprogram declaration, object declaration,
857 -- or an access discriminant, in a private or a full type declaration.
858 -- In the case of a subprogram, if the designated type is incomplete,
859 -- the operation will be a primitive operation of the full type, to be
860 -- updated subsequently. If the type is imported through a limited_with
861 -- clause, the subprogram is not a primitive operation of the type
862 -- (which is declared elsewhere in some other scope).
863
864 if Ekind (Desig_Type) = E_Incomplete_Type
865 and then not From_With_Type (Desig_Type)
866 and then Is_Overloadable (Current_Scope)
867 then
868 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
869 Set_Has_Delayed_Freeze (Current_Scope);
870 end if;
871
872 -- Ada 2005: if the designated type is an interface that may contain
873 -- tasks, create a Master entity for the declaration. This must be done
874 -- before expansion of the full declaration, because the declaration may
875 -- include an expression that is an allocator, whose expansion needs the
876 -- proper Master for the created tasks.
877
878 if Nkind (Related_Nod) = N_Object_Declaration
879 and then Expander_Active
880 then
881 if Is_Interface (Desig_Type)
882 and then Is_Limited_Record (Desig_Type)
883 then
884 Build_Class_Wide_Master (Anon_Type);
885
886 -- Similarly, if the type is an anonymous access that designates
887 -- tasks, create a master entity for it in the current context.
888
889 elsif Has_Task (Desig_Type)
890 and then Comes_From_Source (Related_Nod)
891 and then not Restriction_Active (No_Task_Hierarchy)
892 then
893 if not Has_Master_Entity (Current_Scope) then
894 Decl :=
895 Make_Object_Declaration (Loc,
896 Defining_Identifier =>
897 Make_Defining_Identifier (Loc, Name_uMaster),
898 Constant_Present => True,
899 Object_Definition =>
900 New_Reference_To (RTE (RE_Master_Id), Loc),
901 Expression =>
902 Make_Explicit_Dereference (Loc,
903 New_Reference_To (RTE (RE_Current_Master), Loc)));
904
905 Insert_Before (Related_Nod, Decl);
906 Analyze (Decl);
907
908 Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
909 Set_Has_Master_Entity (Current_Scope);
910 else
911 Build_Master_Renaming (Related_Nod, Anon_Type);
912 end if;
913 end if;
914 end if;
915
916 -- For a private component of a protected type, it is imperative that
917 -- the back-end elaborate the type immediately after the protected
918 -- declaration, because this type will be used in the declarations
919 -- created for the component within each protected body, so we must
920 -- create an itype reference for it now.
921
922 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
923 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
924
925 -- Similarly, if the access definition is the return result of a
926 -- function, create an itype reference for it because it will be used
927 -- within the function body. For a regular function that is not a
928 -- compilation unit, insert reference after the declaration. For a
929 -- protected operation, insert it after the enclosing protected type
930 -- declaration. In either case, do not create a reference for a type
931 -- obtained through a limited_with clause, because this would introduce
932 -- semantic dependencies.
933
934 -- Similarly, do not create a reference if the designated type is a
935 -- generic formal, because no use of it will reach the backend.
936
937 elsif Nkind (Related_Nod) = N_Function_Specification
938 and then not From_With_Type (Desig_Type)
939 and then not Is_Generic_Type (Desig_Type)
940 then
941 if Present (Enclosing_Prot_Type) then
942 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
943
944 elsif Is_List_Member (Parent (Related_Nod))
945 and then Nkind (Parent (N)) /= N_Parameter_Specification
946 then
947 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
948 end if;
949
950 -- Finally, create an itype reference for an object declaration of an
951 -- anonymous access type. This is strictly necessary only for deferred
952 -- constants, but in any case will avoid out-of-scope problems in the
953 -- back-end.
954
955 elsif Nkind (Related_Nod) = N_Object_Declaration then
956 Build_Itype_Reference (Anon_Type, Related_Nod);
957 end if;
958
959 return Anon_Type;
960 end Access_Definition;
961
962 -----------------------------------
963 -- Access_Subprogram_Declaration --
964 -----------------------------------
965
966 procedure Access_Subprogram_Declaration
967 (T_Name : Entity_Id;
968 T_Def : Node_Id)
969 is
970
971 procedure Check_For_Premature_Usage (Def : Node_Id);
972 -- Check that type T_Name is not used, directly or recursively, as a
973 -- parameter or a return type in Def. Def is either a subtype, an
974 -- access_definition, or an access_to_subprogram_definition.
975
976 -------------------------------
977 -- Check_For_Premature_Usage --
978 -------------------------------
979
980 procedure Check_For_Premature_Usage (Def : Node_Id) is
981 Param : Node_Id;
982
983 begin
984 -- Check for a subtype mark
985
986 if Nkind (Def) in N_Has_Etype then
987 if Etype (Def) = T_Name then
988 Error_Msg_N
989 ("type& cannot be used before end of its declaration", Def);
990 end if;
991
992 -- If this is not a subtype, then this is an access_definition
993
994 elsif Nkind (Def) = N_Access_Definition then
995 if Present (Access_To_Subprogram_Definition (Def)) then
996 Check_For_Premature_Usage
997 (Access_To_Subprogram_Definition (Def));
998 else
999 Check_For_Premature_Usage (Subtype_Mark (Def));
1000 end if;
1001
1002 -- The only cases left are N_Access_Function_Definition and
1003 -- N_Access_Procedure_Definition.
1004
1005 else
1006 if Present (Parameter_Specifications (Def)) then
1007 Param := First (Parameter_Specifications (Def));
1008 while Present (Param) loop
1009 Check_For_Premature_Usage (Parameter_Type (Param));
1010 Param := Next (Param);
1011 end loop;
1012 end if;
1013
1014 if Nkind (Def) = N_Access_Function_Definition then
1015 Check_For_Premature_Usage (Result_Definition (Def));
1016 end if;
1017 end if;
1018 end Check_For_Premature_Usage;
1019
1020 -- Local variables
1021
1022 Formals : constant List_Id := Parameter_Specifications (T_Def);
1023 Formal : Entity_Id;
1024 D_Ityp : Node_Id;
1025 Desig_Type : constant Entity_Id :=
1026 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1027
1028 -- Start of processing for Access_Subprogram_Declaration
1029
1030 begin
1031 Check_Formal_Restriction ("access type is not allowed", T_Def);
1032
1033 -- Associate the Itype node with the inner full-type declaration or
1034 -- subprogram spec or entry body. This is required to handle nested
1035 -- anonymous declarations. For example:
1036
1037 -- procedure P
1038 -- (X : access procedure
1039 -- (Y : access procedure
1040 -- (Z : access T)))
1041
1042 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1043 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1044 N_Private_Type_Declaration,
1045 N_Private_Extension_Declaration,
1046 N_Procedure_Specification,
1047 N_Function_Specification,
1048 N_Entry_Body)
1049
1050 or else
1051 Nkind_In (D_Ityp, N_Object_Declaration,
1052 N_Object_Renaming_Declaration,
1053 N_Formal_Object_Declaration,
1054 N_Formal_Type_Declaration,
1055 N_Task_Type_Declaration,
1056 N_Protected_Type_Declaration))
1057 loop
1058 D_Ityp := Parent (D_Ityp);
1059 pragma Assert (D_Ityp /= Empty);
1060 end loop;
1061
1062 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1063
1064 if Nkind_In (D_Ityp, N_Procedure_Specification,
1065 N_Function_Specification)
1066 then
1067 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1068
1069 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1070 N_Object_Declaration,
1071 N_Object_Renaming_Declaration,
1072 N_Formal_Type_Declaration)
1073 then
1074 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1075 end if;
1076
1077 if Nkind (T_Def) = N_Access_Function_Definition then
1078 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1079 declare
1080 Acc : constant Node_Id := Result_Definition (T_Def);
1081
1082 begin
1083 if Present (Access_To_Subprogram_Definition (Acc))
1084 and then
1085 Protected_Present (Access_To_Subprogram_Definition (Acc))
1086 then
1087 Set_Etype
1088 (Desig_Type,
1089 Replace_Anonymous_Access_To_Protected_Subprogram
1090 (T_Def));
1091
1092 else
1093 Set_Etype
1094 (Desig_Type,
1095 Access_Definition (T_Def, Result_Definition (T_Def)));
1096 end if;
1097 end;
1098
1099 else
1100 Analyze (Result_Definition (T_Def));
1101
1102 declare
1103 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1104
1105 begin
1106 -- If a null exclusion is imposed on the result type, then
1107 -- create a null-excluding itype (an access subtype) and use
1108 -- it as the function's Etype.
1109
1110 if Is_Access_Type (Typ)
1111 and then Null_Exclusion_In_Return_Present (T_Def)
1112 then
1113 Set_Etype (Desig_Type,
1114 Create_Null_Excluding_Itype
1115 (T => Typ,
1116 Related_Nod => T_Def,
1117 Scope_Id => Current_Scope));
1118
1119 else
1120 if From_With_Type (Typ) then
1121
1122 -- AI05-151: Incomplete types are allowed in all basic
1123 -- declarations, including access to subprograms.
1124
1125 if Ada_Version >= Ada_2012 then
1126 null;
1127
1128 else
1129 Error_Msg_NE
1130 ("illegal use of incomplete type&",
1131 Result_Definition (T_Def), Typ);
1132 end if;
1133
1134 elsif Ekind (Current_Scope) = E_Package
1135 and then In_Private_Part (Current_Scope)
1136 then
1137 if Ekind (Typ) = E_Incomplete_Type then
1138 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1139
1140 elsif Is_Class_Wide_Type (Typ)
1141 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1142 then
1143 Append_Elmt
1144 (Desig_Type, Private_Dependents (Etype (Typ)));
1145 end if;
1146 end if;
1147
1148 Set_Etype (Desig_Type, Typ);
1149 end if;
1150 end;
1151 end if;
1152
1153 if not (Is_Type (Etype (Desig_Type))) then
1154 Error_Msg_N
1155 ("expect type in function specification",
1156 Result_Definition (T_Def));
1157 end if;
1158
1159 else
1160 Set_Etype (Desig_Type, Standard_Void_Type);
1161 end if;
1162
1163 if Present (Formals) then
1164 Push_Scope (Desig_Type);
1165
1166 -- A bit of a kludge here. These kludges will be removed when Itypes
1167 -- have proper parent pointers to their declarations???
1168
1169 -- Kludge 1) Link defining_identifier of formals. Required by
1170 -- First_Formal to provide its functionality.
1171
1172 declare
1173 F : Node_Id;
1174
1175 begin
1176 F := First (Formals);
1177
1178 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1179 -- when it is part of an unconstrained type and subtype expansion
1180 -- is disabled. To avoid back-end problems with shared profiles,
1181 -- use previous subprogram type as the designated type.
1182
1183 if ASIS_Mode
1184 and then Present (Scope (Defining_Identifier (F)))
1185 then
1186 Set_Etype (T_Name, T_Name);
1187 Init_Size_Align (T_Name);
1188 Set_Directly_Designated_Type (T_Name,
1189 Scope (Defining_Identifier (F)));
1190 return;
1191 end if;
1192
1193 while Present (F) loop
1194 if No (Parent (Defining_Identifier (F))) then
1195 Set_Parent (Defining_Identifier (F), F);
1196 end if;
1197
1198 Next (F);
1199 end loop;
1200 end;
1201
1202 Process_Formals (Formals, Parent (T_Def));
1203
1204 -- Kludge 2) End_Scope requires that the parent pointer be set to
1205 -- something reasonable, but Itypes don't have parent pointers. So
1206 -- we set it and then unset it ???
1207
1208 Set_Parent (Desig_Type, T_Name);
1209 End_Scope;
1210 Set_Parent (Desig_Type, Empty);
1211 end if;
1212
1213 -- Check for premature usage of the type being defined
1214
1215 Check_For_Premature_Usage (T_Def);
1216
1217 -- The return type and/or any parameter type may be incomplete. Mark
1218 -- the subprogram_type as depending on the incomplete type, so that
1219 -- it can be updated when the full type declaration is seen. This
1220 -- only applies to incomplete types declared in some enclosing scope,
1221 -- not to limited views from other packages.
1222
1223 if Present (Formals) then
1224 Formal := First_Formal (Desig_Type);
1225 while Present (Formal) loop
1226 if Ekind (Formal) /= E_In_Parameter
1227 and then Nkind (T_Def) = N_Access_Function_Definition
1228 then
1229 Error_Msg_N ("functions can only have IN parameters", Formal);
1230 end if;
1231
1232 if Ekind (Etype (Formal)) = E_Incomplete_Type
1233 and then In_Open_Scopes (Scope (Etype (Formal)))
1234 then
1235 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1236 Set_Has_Delayed_Freeze (Desig_Type);
1237 end if;
1238
1239 Next_Formal (Formal);
1240 end loop;
1241 end if;
1242
1243 -- If the return type is incomplete, this is legal as long as the
1244 -- type is declared in the current scope and will be completed in
1245 -- it (rather than being part of limited view).
1246
1247 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1248 and then not Has_Delayed_Freeze (Desig_Type)
1249 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1250 then
1251 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1252 Set_Has_Delayed_Freeze (Desig_Type);
1253 end if;
1254
1255 Check_Delayed_Subprogram (Desig_Type);
1256
1257 if Protected_Present (T_Def) then
1258 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1259 Set_Convention (Desig_Type, Convention_Protected);
1260 else
1261 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1262 end if;
1263
1264 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1265
1266 Set_Etype (T_Name, T_Name);
1267 Init_Size_Align (T_Name);
1268 Set_Directly_Designated_Type (T_Name, Desig_Type);
1269
1270 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1271
1272 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1273
1274 Check_Restriction (No_Access_Subprograms, T_Def);
1275 end Access_Subprogram_Declaration;
1276
1277 ----------------------------
1278 -- Access_Type_Declaration --
1279 ----------------------------
1280
1281 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1282 S : constant Node_Id := Subtype_Indication (Def);
1283 P : constant Node_Id := Parent (Def);
1284 begin
1285 Check_Formal_Restriction ("access type is not allowed", Def);
1286
1287 -- Check for permissible use of incomplete type
1288
1289 if Nkind (S) /= N_Subtype_Indication then
1290 Analyze (S);
1291
1292 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1293 Set_Directly_Designated_Type (T, Entity (S));
1294 else
1295 Set_Directly_Designated_Type (T,
1296 Process_Subtype (S, P, T, 'P'));
1297 end if;
1298
1299 else
1300 Set_Directly_Designated_Type (T,
1301 Process_Subtype (S, P, T, 'P'));
1302 end if;
1303
1304 if All_Present (Def) or Constant_Present (Def) then
1305 Set_Ekind (T, E_General_Access_Type);
1306 else
1307 Set_Ekind (T, E_Access_Type);
1308 end if;
1309
1310 if Base_Type (Designated_Type (T)) = T then
1311 Error_Msg_N ("access type cannot designate itself", S);
1312
1313 -- In Ada 2005, the type may have a limited view through some unit
1314 -- in its own context, allowing the following circularity that cannot
1315 -- be detected earlier
1316
1317 elsif Is_Class_Wide_Type (Designated_Type (T))
1318 and then Etype (Designated_Type (T)) = T
1319 then
1320 Error_Msg_N
1321 ("access type cannot designate its own classwide type", S);
1322
1323 -- Clean up indication of tagged status to prevent cascaded errors
1324
1325 Set_Is_Tagged_Type (T, False);
1326 end if;
1327
1328 Set_Etype (T, T);
1329
1330 -- If the type has appeared already in a with_type clause, it is
1331 -- frozen and the pointer size is already set. Else, initialize.
1332
1333 if not From_With_Type (T) then
1334 Init_Size_Align (T);
1335 end if;
1336
1337 -- Note that Has_Task is always false, since the access type itself
1338 -- is not a task type. See Einfo for more description on this point.
1339 -- Exactly the same consideration applies to Has_Controlled_Component.
1340
1341 Set_Has_Task (T, False);
1342 Set_Has_Controlled_Component (T, False);
1343
1344 -- Initialize Associated_Final_Chain explicitly to Empty, to avoid
1345 -- problems where an incomplete view of this entity has been previously
1346 -- established by a limited with and an overlaid version of this field
1347 -- (Stored_Constraint) was initialized for the incomplete view.
1348
1349 Set_Associated_Final_Chain (T, Empty);
1350
1351 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1352 -- attributes
1353
1354 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1355 Set_Is_Access_Constant (T, Constant_Present (Def));
1356 end Access_Type_Declaration;
1357
1358 ----------------------------------
1359 -- Add_Interface_Tag_Components --
1360 ----------------------------------
1361
1362 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1363 Loc : constant Source_Ptr := Sloc (N);
1364 L : List_Id;
1365 Last_Tag : Node_Id;
1366
1367 procedure Add_Tag (Iface : Entity_Id);
1368 -- Add tag for one of the progenitor interfaces
1369
1370 -------------
1371 -- Add_Tag --
1372 -------------
1373
1374 procedure Add_Tag (Iface : Entity_Id) is
1375 Decl : Node_Id;
1376 Def : Node_Id;
1377 Tag : Entity_Id;
1378 Offset : Entity_Id;
1379
1380 begin
1381 pragma Assert (Is_Tagged_Type (Iface)
1382 and then Is_Interface (Iface));
1383
1384 -- This is a reasonable place to propagate predicates
1385
1386 if Has_Predicates (Iface) then
1387 Set_Has_Predicates (Typ);
1388 end if;
1389
1390 Def :=
1391 Make_Component_Definition (Loc,
1392 Aliased_Present => True,
1393 Subtype_Indication =>
1394 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1395
1396 Tag := Make_Temporary (Loc, 'V');
1397
1398 Decl :=
1399 Make_Component_Declaration (Loc,
1400 Defining_Identifier => Tag,
1401 Component_Definition => Def);
1402
1403 Analyze_Component_Declaration (Decl);
1404
1405 Set_Analyzed (Decl);
1406 Set_Ekind (Tag, E_Component);
1407 Set_Is_Tag (Tag);
1408 Set_Is_Aliased (Tag);
1409 Set_Related_Type (Tag, Iface);
1410 Init_Component_Location (Tag);
1411
1412 pragma Assert (Is_Frozen (Iface));
1413
1414 Set_DT_Entry_Count (Tag,
1415 DT_Entry_Count (First_Entity (Iface)));
1416
1417 if No (Last_Tag) then
1418 Prepend (Decl, L);
1419 else
1420 Insert_After (Last_Tag, Decl);
1421 end if;
1422
1423 Last_Tag := Decl;
1424
1425 -- If the ancestor has discriminants we need to give special support
1426 -- to store the offset_to_top value of the secondary dispatch tables.
1427 -- For this purpose we add a supplementary component just after the
1428 -- field that contains the tag associated with each secondary DT.
1429
1430 if Typ /= Etype (Typ)
1431 and then Has_Discriminants (Etype (Typ))
1432 then
1433 Def :=
1434 Make_Component_Definition (Loc,
1435 Subtype_Indication =>
1436 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1437
1438 Offset := Make_Temporary (Loc, 'V');
1439
1440 Decl :=
1441 Make_Component_Declaration (Loc,
1442 Defining_Identifier => Offset,
1443 Component_Definition => Def);
1444
1445 Analyze_Component_Declaration (Decl);
1446
1447 Set_Analyzed (Decl);
1448 Set_Ekind (Offset, E_Component);
1449 Set_Is_Aliased (Offset);
1450 Set_Related_Type (Offset, Iface);
1451 Init_Component_Location (Offset);
1452 Insert_After (Last_Tag, Decl);
1453 Last_Tag := Decl;
1454 end if;
1455 end Add_Tag;
1456
1457 -- Local variables
1458
1459 Elmt : Elmt_Id;
1460 Ext : Node_Id;
1461 Comp : Node_Id;
1462
1463 -- Start of processing for Add_Interface_Tag_Components
1464
1465 begin
1466 if not RTE_Available (RE_Interface_Tag) then
1467 Error_Msg
1468 ("(Ada 2005) interface types not supported by this run-time!",
1469 Sloc (N));
1470 return;
1471 end if;
1472
1473 if Ekind (Typ) /= E_Record_Type
1474 or else (Is_Concurrent_Record_Type (Typ)
1475 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1476 or else (not Is_Concurrent_Record_Type (Typ)
1477 and then No (Interfaces (Typ))
1478 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1479 then
1480 return;
1481 end if;
1482
1483 -- Find the current last tag
1484
1485 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1486 Ext := Record_Extension_Part (Type_Definition (N));
1487 else
1488 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1489 Ext := Type_Definition (N);
1490 end if;
1491
1492 Last_Tag := Empty;
1493
1494 if not (Present (Component_List (Ext))) then
1495 Set_Null_Present (Ext, False);
1496 L := New_List;
1497 Set_Component_List (Ext,
1498 Make_Component_List (Loc,
1499 Component_Items => L,
1500 Null_Present => False));
1501 else
1502 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1503 L := Component_Items
1504 (Component_List
1505 (Record_Extension_Part
1506 (Type_Definition (N))));
1507 else
1508 L := Component_Items
1509 (Component_List
1510 (Type_Definition (N)));
1511 end if;
1512
1513 -- Find the last tag component
1514
1515 Comp := First (L);
1516 while Present (Comp) loop
1517 if Nkind (Comp) = N_Component_Declaration
1518 and then Is_Tag (Defining_Identifier (Comp))
1519 then
1520 Last_Tag := Comp;
1521 end if;
1522
1523 Next (Comp);
1524 end loop;
1525 end if;
1526
1527 -- At this point L references the list of components and Last_Tag
1528 -- references the current last tag (if any). Now we add the tag
1529 -- corresponding with all the interfaces that are not implemented
1530 -- by the parent.
1531
1532 if Present (Interfaces (Typ)) then
1533 Elmt := First_Elmt (Interfaces (Typ));
1534 while Present (Elmt) loop
1535 Add_Tag (Node (Elmt));
1536 Next_Elmt (Elmt);
1537 end loop;
1538 end if;
1539 end Add_Interface_Tag_Components;
1540
1541 -------------------------------------
1542 -- Add_Internal_Interface_Entities --
1543 -------------------------------------
1544
1545 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1546 Elmt : Elmt_Id;
1547 Iface : Entity_Id;
1548 Iface_Elmt : Elmt_Id;
1549 Iface_Prim : Entity_Id;
1550 Ifaces_List : Elist_Id;
1551 New_Subp : Entity_Id := Empty;
1552 Prim : Entity_Id;
1553 Restore_Scope : Boolean := False;
1554
1555 begin
1556 pragma Assert (Ada_Version >= Ada_2005
1557 and then Is_Record_Type (Tagged_Type)
1558 and then Is_Tagged_Type (Tagged_Type)
1559 and then Has_Interfaces (Tagged_Type)
1560 and then not Is_Interface (Tagged_Type));
1561
1562 -- Ensure that the internal entities are added to the scope of the type
1563
1564 if Scope (Tagged_Type) /= Current_Scope then
1565 Push_Scope (Scope (Tagged_Type));
1566 Restore_Scope := True;
1567 end if;
1568
1569 Collect_Interfaces (Tagged_Type, Ifaces_List);
1570
1571 Iface_Elmt := First_Elmt (Ifaces_List);
1572 while Present (Iface_Elmt) loop
1573 Iface := Node (Iface_Elmt);
1574
1575 -- Originally we excluded here from this processing interfaces that
1576 -- are parents of Tagged_Type because their primitives are located
1577 -- in the primary dispatch table (and hence no auxiliary internal
1578 -- entities are required to handle secondary dispatch tables in such
1579 -- case). However, these auxiliary entities are also required to
1580 -- handle derivations of interfaces in formals of generics (see
1581 -- Derive_Subprograms).
1582
1583 Elmt := First_Elmt (Primitive_Operations (Iface));
1584 while Present (Elmt) loop
1585 Iface_Prim := Node (Elmt);
1586
1587 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1588 Prim :=
1589 Find_Primitive_Covering_Interface
1590 (Tagged_Type => Tagged_Type,
1591 Iface_Prim => Iface_Prim);
1592
1593 pragma Assert (Present (Prim));
1594
1595 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1596 -- differs from the name of the interface primitive then it is
1597 -- a private primitive inherited from a parent type. In such
1598 -- case, given that Tagged_Type covers the interface, the
1599 -- inherited private primitive becomes visible. For such
1600 -- purpose we add a new entity that renames the inherited
1601 -- private primitive.
1602
1603 if Chars (Prim) /= Chars (Iface_Prim) then
1604 pragma Assert (Has_Suffix (Prim, 'P'));
1605 Derive_Subprogram
1606 (New_Subp => New_Subp,
1607 Parent_Subp => Iface_Prim,
1608 Derived_Type => Tagged_Type,
1609 Parent_Type => Iface);
1610 Set_Alias (New_Subp, Prim);
1611 Set_Is_Abstract_Subprogram
1612 (New_Subp, Is_Abstract_Subprogram (Prim));
1613 end if;
1614
1615 Derive_Subprogram
1616 (New_Subp => New_Subp,
1617 Parent_Subp => Iface_Prim,
1618 Derived_Type => Tagged_Type,
1619 Parent_Type => Iface);
1620
1621 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1622 -- associated with interface types. These entities are
1623 -- only registered in the list of primitives of its
1624 -- corresponding tagged type because they are only used
1625 -- to fill the contents of the secondary dispatch tables.
1626 -- Therefore they are removed from the homonym chains.
1627
1628 Set_Is_Hidden (New_Subp);
1629 Set_Is_Internal (New_Subp);
1630 Set_Alias (New_Subp, Prim);
1631 Set_Is_Abstract_Subprogram
1632 (New_Subp, Is_Abstract_Subprogram (Prim));
1633 Set_Interface_Alias (New_Subp, Iface_Prim);
1634
1635 -- Internal entities associated with interface types are
1636 -- only registered in the list of primitives of the tagged
1637 -- type. They are only used to fill the contents of the
1638 -- secondary dispatch tables. Therefore they are not needed
1639 -- in the homonym chains.
1640
1641 Remove_Homonym (New_Subp);
1642
1643 -- Hidden entities associated with interfaces must have set
1644 -- the Has_Delay_Freeze attribute to ensure that, in case of
1645 -- locally defined tagged types (or compiling with static
1646 -- dispatch tables generation disabled) the corresponding
1647 -- entry of the secondary dispatch table is filled when
1648 -- such an entity is frozen.
1649
1650 Set_Has_Delayed_Freeze (New_Subp);
1651 end if;
1652
1653 Next_Elmt (Elmt);
1654 end loop;
1655
1656 Next_Elmt (Iface_Elmt);
1657 end loop;
1658
1659 if Restore_Scope then
1660 Pop_Scope;
1661 end if;
1662 end Add_Internal_Interface_Entities;
1663
1664 -----------------------------------
1665 -- Analyze_Component_Declaration --
1666 -----------------------------------
1667
1668 procedure Analyze_Component_Declaration (N : Node_Id) is
1669 Id : constant Entity_Id := Defining_Identifier (N);
1670 E : constant Node_Id := Expression (N);
1671 Typ : constant Node_Id :=
1672 Subtype_Indication (Component_Definition (N));
1673 T : Entity_Id;
1674 P : Entity_Id;
1675
1676 function Contains_POC (Constr : Node_Id) return Boolean;
1677 -- Determines whether a constraint uses the discriminant of a record
1678 -- type thus becoming a per-object constraint (POC).
1679
1680 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1681 -- Typ is the type of the current component, check whether this type is
1682 -- a limited type. Used to validate declaration against that of
1683 -- enclosing record.
1684
1685 ------------------
1686 -- Contains_POC --
1687 ------------------
1688
1689 function Contains_POC (Constr : Node_Id) return Boolean is
1690 begin
1691 -- Prevent cascaded errors
1692
1693 if Error_Posted (Constr) then
1694 return False;
1695 end if;
1696
1697 case Nkind (Constr) is
1698 when N_Attribute_Reference =>
1699 return
1700 Attribute_Name (Constr) = Name_Access
1701 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1702
1703 when N_Discriminant_Association =>
1704 return Denotes_Discriminant (Expression (Constr));
1705
1706 when N_Identifier =>
1707 return Denotes_Discriminant (Constr);
1708
1709 when N_Index_Or_Discriminant_Constraint =>
1710 declare
1711 IDC : Node_Id;
1712
1713 begin
1714 IDC := First (Constraints (Constr));
1715 while Present (IDC) loop
1716
1717 -- One per-object constraint is sufficient
1718
1719 if Contains_POC (IDC) then
1720 return True;
1721 end if;
1722
1723 Next (IDC);
1724 end loop;
1725
1726 return False;
1727 end;
1728
1729 when N_Range =>
1730 return Denotes_Discriminant (Low_Bound (Constr))
1731 or else
1732 Denotes_Discriminant (High_Bound (Constr));
1733
1734 when N_Range_Constraint =>
1735 return Denotes_Discriminant (Range_Expression (Constr));
1736
1737 when others =>
1738 return False;
1739
1740 end case;
1741 end Contains_POC;
1742
1743 ----------------------
1744 -- Is_Known_Limited --
1745 ----------------------
1746
1747 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1748 P : constant Entity_Id := Etype (Typ);
1749 R : constant Entity_Id := Root_Type (Typ);
1750
1751 begin
1752 if Is_Limited_Record (Typ) then
1753 return True;
1754
1755 -- If the root type is limited (and not a limited interface)
1756 -- so is the current type
1757
1758 elsif Is_Limited_Record (R)
1759 and then
1760 (not Is_Interface (R)
1761 or else not Is_Limited_Interface (R))
1762 then
1763 return True;
1764
1765 -- Else the type may have a limited interface progenitor, but a
1766 -- limited record parent.
1767
1768 elsif R /= P
1769 and then Is_Limited_Record (P)
1770 then
1771 return True;
1772
1773 else
1774 return False;
1775 end if;
1776 end Is_Known_Limited;
1777
1778 -- Start of processing for Analyze_Component_Declaration
1779
1780 begin
1781 Generate_Definition (Id);
1782 Enter_Name (Id);
1783
1784 if Present (Typ) then
1785 if Nkind (Typ) /= N_Identifier then
1786 Check_Formal_Restriction ("subtype mark required", Typ);
1787 end if;
1788
1789 T := Find_Type_Of_Object
1790 (Subtype_Indication (Component_Definition (N)), N);
1791
1792 -- Ada 2005 (AI-230): Access Definition case
1793
1794 else
1795 pragma Assert (Present
1796 (Access_Definition (Component_Definition (N))));
1797
1798 T := Access_Definition
1799 (Related_Nod => N,
1800 N => Access_Definition (Component_Definition (N)));
1801 Set_Is_Local_Anonymous_Access (T);
1802
1803 -- Ada 2005 (AI-254)
1804
1805 if Present (Access_To_Subprogram_Definition
1806 (Access_Definition (Component_Definition (N))))
1807 and then Protected_Present (Access_To_Subprogram_Definition
1808 (Access_Definition
1809 (Component_Definition (N))))
1810 then
1811 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1812 end if;
1813 end if;
1814
1815 -- If the subtype is a constrained subtype of the enclosing record,
1816 -- (which must have a partial view) the back-end does not properly
1817 -- handle the recursion. Rewrite the component declaration with an
1818 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1819 -- the tree directly because side effects have already been removed from
1820 -- discriminant constraints.
1821
1822 if Ekind (T) = E_Access_Subtype
1823 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1824 and then Comes_From_Source (T)
1825 and then Nkind (Parent (T)) = N_Subtype_Declaration
1826 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1827 then
1828 Rewrite
1829 (Subtype_Indication (Component_Definition (N)),
1830 New_Copy_Tree (Subtype_Indication (Parent (T))));
1831 T := Find_Type_Of_Object
1832 (Subtype_Indication (Component_Definition (N)), N);
1833 end if;
1834
1835 -- If the component declaration includes a default expression, then we
1836 -- check that the component is not of a limited type (RM 3.7(5)),
1837 -- and do the special preanalysis of the expression (see section on
1838 -- "Handling of Default and Per-Object Expressions" in the spec of
1839 -- package Sem).
1840
1841 if Present (E) then
1842 Check_Formal_Restriction ("default expression is not allowed", E);
1843 Preanalyze_Spec_Expression (E, T);
1844 Check_Initialization (T, E);
1845
1846 if Ada_Version >= Ada_2005
1847 and then Ekind (T) = E_Anonymous_Access_Type
1848 and then Etype (E) /= Any_Type
1849 then
1850 -- Check RM 3.9.2(9): "if the expected type for an expression is
1851 -- an anonymous access-to-specific tagged type, then the object
1852 -- designated by the expression shall not be dynamically tagged
1853 -- unless it is a controlling operand in a call on a dispatching
1854 -- operation"
1855
1856 if Is_Tagged_Type (Directly_Designated_Type (T))
1857 and then
1858 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1859 and then
1860 Ekind (Directly_Designated_Type (Etype (E))) =
1861 E_Class_Wide_Type
1862 then
1863 Error_Msg_N
1864 ("access to specific tagged type required (RM 3.9.2(9))", E);
1865 end if;
1866
1867 -- (Ada 2005: AI-230): Accessibility check for anonymous
1868 -- components
1869
1870 if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1871 Error_Msg_N
1872 ("expression has deeper access level than component " &
1873 "(RM 3.10.2 (12.2))", E);
1874 end if;
1875
1876 -- The initialization expression is a reference to an access
1877 -- discriminant. The type of the discriminant is always deeper
1878 -- than any access type.
1879
1880 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1881 and then Is_Entity_Name (E)
1882 and then Ekind (Entity (E)) = E_In_Parameter
1883 and then Present (Discriminal_Link (Entity (E)))
1884 then
1885 Error_Msg_N
1886 ("discriminant has deeper accessibility level than target",
1887 E);
1888 end if;
1889 end if;
1890 end if;
1891
1892 -- The parent type may be a private view with unknown discriminants,
1893 -- and thus unconstrained. Regular components must be constrained.
1894
1895 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1896 if Is_Class_Wide_Type (T) then
1897 Error_Msg_N
1898 ("class-wide subtype with unknown discriminants" &
1899 " in component declaration",
1900 Subtype_Indication (Component_Definition (N)));
1901 else
1902 Error_Msg_N
1903 ("unconstrained subtype in component declaration",
1904 Subtype_Indication (Component_Definition (N)));
1905 end if;
1906
1907 -- Components cannot be abstract, except for the special case of
1908 -- the _Parent field (case of extending an abstract tagged type)
1909
1910 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1911 Error_Msg_N ("type of a component cannot be abstract", N);
1912 end if;
1913
1914 Set_Etype (Id, T);
1915 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1916
1917 -- The component declaration may have a per-object constraint, set
1918 -- the appropriate flag in the defining identifier of the subtype.
1919
1920 if Present (Subtype_Indication (Component_Definition (N))) then
1921 declare
1922 Sindic : constant Node_Id :=
1923 Subtype_Indication (Component_Definition (N));
1924 begin
1925 if Nkind (Sindic) = N_Subtype_Indication
1926 and then Present (Constraint (Sindic))
1927 and then Contains_POC (Constraint (Sindic))
1928 then
1929 Set_Has_Per_Object_Constraint (Id);
1930 end if;
1931 end;
1932 end if;
1933
1934 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1935 -- out some static checks.
1936
1937 if Ada_Version >= Ada_2005
1938 and then Can_Never_Be_Null (T)
1939 then
1940 Null_Exclusion_Static_Checks (N);
1941 end if;
1942
1943 -- If this component is private (or depends on a private type), flag the
1944 -- record type to indicate that some operations are not available.
1945
1946 P := Private_Component (T);
1947
1948 if Present (P) then
1949
1950 -- Check for circular definitions
1951
1952 if P = Any_Type then
1953 Set_Etype (Id, Any_Type);
1954
1955 -- There is a gap in the visibility of operations only if the
1956 -- component type is not defined in the scope of the record type.
1957
1958 elsif Scope (P) = Scope (Current_Scope) then
1959 null;
1960
1961 elsif Is_Limited_Type (P) then
1962 Set_Is_Limited_Composite (Current_Scope);
1963
1964 else
1965 Set_Is_Private_Composite (Current_Scope);
1966 end if;
1967 end if;
1968
1969 if P /= Any_Type
1970 and then Is_Limited_Type (T)
1971 and then Chars (Id) /= Name_uParent
1972 and then Is_Tagged_Type (Current_Scope)
1973 then
1974 if Is_Derived_Type (Current_Scope)
1975 and then not Is_Known_Limited (Current_Scope)
1976 then
1977 Error_Msg_N
1978 ("extension of nonlimited type cannot have limited components",
1979 N);
1980
1981 if Is_Interface (Root_Type (Current_Scope)) then
1982 Error_Msg_N
1983 ("\limitedness is not inherited from limited interface", N);
1984 Error_Msg_N ("\add LIMITED to type indication", N);
1985 end if;
1986
1987 Explain_Limited_Type (T, N);
1988 Set_Etype (Id, Any_Type);
1989 Set_Is_Limited_Composite (Current_Scope, False);
1990
1991 elsif not Is_Derived_Type (Current_Scope)
1992 and then not Is_Limited_Record (Current_Scope)
1993 and then not Is_Concurrent_Type (Current_Scope)
1994 then
1995 Error_Msg_N
1996 ("nonlimited tagged type cannot have limited components", N);
1997 Explain_Limited_Type (T, N);
1998 Set_Etype (Id, Any_Type);
1999 Set_Is_Limited_Composite (Current_Scope, False);
2000 end if;
2001 end if;
2002
2003 Set_Original_Record_Component (Id, Id);
2004
2005 if Has_Aspects (N) then
2006 Analyze_Aspect_Specifications (N, Id);
2007 end if;
2008 end Analyze_Component_Declaration;
2009
2010 --------------------------
2011 -- Analyze_Declarations --
2012 --------------------------
2013
2014 procedure Analyze_Declarations (L : List_Id) is
2015 D : Node_Id;
2016 Freeze_From : Entity_Id := Empty;
2017 Next_Node : Node_Id;
2018
2019 procedure Adjust_D;
2020 -- Adjust D not to include implicit label declarations, since these
2021 -- have strange Sloc values that result in elaboration check problems.
2022 -- (They have the sloc of the label as found in the source, and that
2023 -- is ahead of the current declarative part).
2024
2025 --------------
2026 -- Adjust_D --
2027 --------------
2028
2029 procedure Adjust_D is
2030 begin
2031 while Present (Prev (D))
2032 and then Nkind (D) = N_Implicit_Label_Declaration
2033 loop
2034 Prev (D);
2035 end loop;
2036 end Adjust_D;
2037
2038 -- Start of processing for Analyze_Declarations
2039
2040 begin
2041 D := First (L);
2042 while Present (D) loop
2043
2044 -- Package specification cannot contain a package declaration in
2045 -- SPARK or ALFA.
2046
2047 if Nkind (D) = N_Package_Declaration
2048 and then Nkind (Parent (L)) = N_Package_Specification
2049 then
2050 Check_Formal_Restriction ("package specification cannot contain "
2051 & "a package declaration", D);
2052 end if;
2053
2054 -- Complete analysis of declaration
2055
2056 Analyze (D);
2057 Next_Node := Next (D);
2058
2059 if No (Freeze_From) then
2060 Freeze_From := First_Entity (Current_Scope);
2061 end if;
2062
2063 -- At the end of a declarative part, freeze remaining entities
2064 -- declared in it. The end of the visible declarations of package
2065 -- specification is not the end of a declarative part if private
2066 -- declarations are present. The end of a package declaration is a
2067 -- freezing point only if it a library package. A task definition or
2068 -- protected type definition is not a freeze point either. Finally,
2069 -- we do not freeze entities in generic scopes, because there is no
2070 -- code generated for them and freeze nodes will be generated for
2071 -- the instance.
2072
2073 -- The end of a package instantiation is not a freeze point, but
2074 -- for now we make it one, because the generic body is inserted
2075 -- (currently) immediately after. Generic instantiations will not
2076 -- be a freeze point once delayed freezing of bodies is implemented.
2077 -- (This is needed in any case for early instantiations ???).
2078
2079 if No (Next_Node) then
2080 if Nkind_In (Parent (L), N_Component_List,
2081 N_Task_Definition,
2082 N_Protected_Definition)
2083 then
2084 null;
2085
2086 elsif Nkind (Parent (L)) /= N_Package_Specification then
2087 if Nkind (Parent (L)) = N_Package_Body then
2088 Freeze_From := First_Entity (Current_Scope);
2089 end if;
2090
2091 Adjust_D;
2092 Freeze_All (Freeze_From, D);
2093 Freeze_From := Last_Entity (Current_Scope);
2094
2095 elsif Scope (Current_Scope) /= Standard_Standard
2096 and then not Is_Child_Unit (Current_Scope)
2097 and then No (Generic_Parent (Parent (L)))
2098 then
2099 null;
2100
2101 elsif L /= Visible_Declarations (Parent (L))
2102 or else No (Private_Declarations (Parent (L)))
2103 or else Is_Empty_List (Private_Declarations (Parent (L)))
2104 then
2105 Adjust_D;
2106 Freeze_All (Freeze_From, D);
2107 Freeze_From := Last_Entity (Current_Scope);
2108 end if;
2109
2110 -- If next node is a body then freeze all types before the body.
2111 -- An exception occurs for some expander-generated bodies. If these
2112 -- are generated at places where in general language rules would not
2113 -- allow a freeze point, then we assume that the expander has
2114 -- explicitly checked that all required types are properly frozen,
2115 -- and we do not cause general freezing here. This special circuit
2116 -- is used when the encountered body is marked as having already
2117 -- been analyzed.
2118
2119 -- In all other cases (bodies that come from source, and expander
2120 -- generated bodies that have not been analyzed yet), freeze all
2121 -- types now. Note that in the latter case, the expander must take
2122 -- care to attach the bodies at a proper place in the tree so as to
2123 -- not cause unwanted freezing at that point.
2124
2125 elsif not Analyzed (Next_Node)
2126 and then (Nkind_In (Next_Node, N_Subprogram_Body,
2127 N_Entry_Body,
2128 N_Package_Body,
2129 N_Protected_Body,
2130 N_Task_Body)
2131 or else
2132 Nkind (Next_Node) in N_Body_Stub)
2133 then
2134 Adjust_D;
2135 Freeze_All (Freeze_From, D);
2136 Freeze_From := Last_Entity (Current_Scope);
2137 end if;
2138
2139 D := Next_Node;
2140 end loop;
2141
2142 -- One more thing to do, we need to scan the declarations to check
2143 -- for any precondition/postcondition pragmas (Pre/Post aspects have
2144 -- by this stage been converted into corresponding pragmas). It is
2145 -- at this point that we analyze the expressions in such pragmas,
2146 -- to implement the delayed visibility requirement.
2147
2148 declare
2149 Decl : Node_Id;
2150 Spec : Node_Id;
2151 Sent : Entity_Id;
2152 Prag : Node_Id;
2153
2154 begin
2155 Decl := First (L);
2156 while Present (Decl) loop
2157 if Nkind (Original_Node (Decl)) = N_Subprogram_Declaration then
2158 Spec := Specification (Original_Node (Decl));
2159 Sent := Defining_Unit_Name (Spec);
2160 Prag := Spec_PPC_List (Sent);
2161 while Present (Prag) loop
2162 Analyze_PPC_In_Decl_Part (Prag, Sent);
2163 Prag := Next_Pragma (Prag);
2164 end loop;
2165 end if;
2166
2167 Next (Decl);
2168 end loop;
2169 end;
2170 end Analyze_Declarations;
2171
2172 -----------------------------------
2173 -- Analyze_Full_Type_Declaration --
2174 -----------------------------------
2175
2176 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2177 Def : constant Node_Id := Type_Definition (N);
2178 Def_Id : constant Entity_Id := Defining_Identifier (N);
2179 T : Entity_Id;
2180 Prev : Entity_Id;
2181
2182 Is_Remote : constant Boolean :=
2183 (Is_Remote_Types (Current_Scope)
2184 or else Is_Remote_Call_Interface (Current_Scope))
2185 and then not (In_Private_Part (Current_Scope)
2186 or else In_Package_Body (Current_Scope));
2187
2188 procedure Check_Ops_From_Incomplete_Type;
2189 -- If there is a tagged incomplete partial view of the type, transfer
2190 -- its operations to the full view, and indicate that the type of the
2191 -- controlling parameter (s) is this full view.
2192
2193 ------------------------------------
2194 -- Check_Ops_From_Incomplete_Type --
2195 ------------------------------------
2196
2197 procedure Check_Ops_From_Incomplete_Type is
2198 Elmt : Elmt_Id;
2199 Formal : Entity_Id;
2200 Op : Entity_Id;
2201
2202 begin
2203 if Prev /= T
2204 and then Ekind (Prev) = E_Incomplete_Type
2205 and then Is_Tagged_Type (Prev)
2206 and then Is_Tagged_Type (T)
2207 then
2208 Elmt := First_Elmt (Primitive_Operations (Prev));
2209 while Present (Elmt) loop
2210 Op := Node (Elmt);
2211 Prepend_Elmt (Op, Primitive_Operations (T));
2212
2213 Formal := First_Formal (Op);
2214 while Present (Formal) loop
2215 if Etype (Formal) = Prev then
2216 Set_Etype (Formal, T);
2217 end if;
2218
2219 Next_Formal (Formal);
2220 end loop;
2221
2222 if Etype (Op) = Prev then
2223 Set_Etype (Op, T);
2224 end if;
2225
2226 Next_Elmt (Elmt);
2227 end loop;
2228 end if;
2229 end Check_Ops_From_Incomplete_Type;
2230
2231 -- Start of processing for Analyze_Full_Type_Declaration
2232
2233 begin
2234 Prev := Find_Type_Name (N);
2235
2236 -- The full view, if present, now points to the current type
2237
2238 -- Ada 2005 (AI-50217): If the type was previously decorated when
2239 -- imported through a LIMITED WITH clause, it appears as incomplete
2240 -- but has no full view.
2241
2242 if Ekind (Prev) = E_Incomplete_Type
2243 and then Present (Full_View (Prev))
2244 then
2245 T := Full_View (Prev);
2246 else
2247 T := Prev;
2248 end if;
2249
2250 Set_Is_Pure (T, Is_Pure (Current_Scope));
2251
2252 -- We set the flag Is_First_Subtype here. It is needed to set the
2253 -- corresponding flag for the Implicit class-wide-type created
2254 -- during tagged types processing.
2255
2256 Set_Is_First_Subtype (T, True);
2257
2258 -- Only composite types other than array types are allowed to have
2259 -- discriminants.
2260
2261 case Nkind (Def) is
2262
2263 -- For derived types, the rule will be checked once we've figured
2264 -- out the parent type.
2265
2266 when N_Derived_Type_Definition =>
2267 null;
2268
2269 -- For record types, discriminants are allowed, unless we are in
2270 -- SPARK or ALFA.
2271
2272 when N_Record_Definition =>
2273 if Present (Discriminant_Specifications (N)) then
2274 Check_Formal_Restriction
2275 ("discriminant type is not allowed",
2276 Defining_Identifier
2277 (First (Discriminant_Specifications (N))));
2278 end if;
2279
2280 when others =>
2281 if Present (Discriminant_Specifications (N)) then
2282 Error_Msg_N
2283 ("elementary or array type cannot have discriminants",
2284 Defining_Identifier
2285 (First (Discriminant_Specifications (N))));
2286 end if;
2287 end case;
2288
2289 -- Elaborate the type definition according to kind, and generate
2290 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2291 -- already done (this happens during the reanalysis that follows a call
2292 -- to the high level optimizer).
2293
2294 if not Analyzed (T) then
2295 Set_Analyzed (T);
2296
2297 case Nkind (Def) is
2298
2299 when N_Access_To_Subprogram_Definition =>
2300 Access_Subprogram_Declaration (T, Def);
2301
2302 -- If this is a remote access to subprogram, we must create the
2303 -- equivalent fat pointer type, and related subprograms.
2304
2305 if Is_Remote then
2306 Process_Remote_AST_Declaration (N);
2307 end if;
2308
2309 -- Validate categorization rule against access type declaration
2310 -- usually a violation in Pure unit, Shared_Passive unit.
2311
2312 Validate_Access_Type_Declaration (T, N);
2313
2314 when N_Access_To_Object_Definition =>
2315 Access_Type_Declaration (T, Def);
2316
2317 -- Validate categorization rule against access type declaration
2318 -- usually a violation in Pure unit, Shared_Passive unit.
2319
2320 Validate_Access_Type_Declaration (T, N);
2321
2322 -- If we are in a Remote_Call_Interface package and define a
2323 -- RACW, then calling stubs and specific stream attributes
2324 -- must be added.
2325
2326 if Is_Remote
2327 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2328 then
2329 Add_RACW_Features (Def_Id);
2330 end if;
2331
2332 -- Set no strict aliasing flag if config pragma seen
2333
2334 if Opt.No_Strict_Aliasing then
2335 Set_No_Strict_Aliasing (Base_Type (Def_Id));
2336 end if;
2337
2338 when N_Array_Type_Definition =>
2339 Array_Type_Declaration (T, Def);
2340
2341 when N_Derived_Type_Definition =>
2342 Derived_Type_Declaration (T, N, T /= Def_Id);
2343
2344 when N_Enumeration_Type_Definition =>
2345 Enumeration_Type_Declaration (T, Def);
2346
2347 when N_Floating_Point_Definition =>
2348 Floating_Point_Type_Declaration (T, Def);
2349
2350 when N_Decimal_Fixed_Point_Definition =>
2351 Decimal_Fixed_Point_Type_Declaration (T, Def);
2352
2353 when N_Ordinary_Fixed_Point_Definition =>
2354 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2355
2356 when N_Signed_Integer_Type_Definition =>
2357 Signed_Integer_Type_Declaration (T, Def);
2358
2359 when N_Modular_Type_Definition =>
2360 Modular_Type_Declaration (T, Def);
2361
2362 when N_Record_Definition =>
2363 Record_Type_Declaration (T, N, Prev);
2364
2365 -- If declaration has a parse error, nothing to elaborate.
2366
2367 when N_Error =>
2368 null;
2369
2370 when others =>
2371 raise Program_Error;
2372
2373 end case;
2374 end if;
2375
2376 if Etype (T) = Any_Type then
2377 return;
2378 end if;
2379
2380 -- Controlled type is not allowed in SPARK and ALFA
2381
2382 if Is_Visibly_Controlled (T) then
2383 Check_Formal_Restriction ("controlled type is not allowed", N);
2384 end if;
2385
2386 -- Some common processing for all types
2387
2388 Set_Depends_On_Private (T, Has_Private_Component (T));
2389 Check_Ops_From_Incomplete_Type;
2390
2391 -- Both the declared entity, and its anonymous base type if one
2392 -- was created, need freeze nodes allocated.
2393
2394 declare
2395 B : constant Entity_Id := Base_Type (T);
2396
2397 begin
2398 -- In the case where the base type differs from the first subtype, we
2399 -- pre-allocate a freeze node, and set the proper link to the first
2400 -- subtype. Freeze_Entity will use this preallocated freeze node when
2401 -- it freezes the entity.
2402
2403 -- This does not apply if the base type is a generic type, whose
2404 -- declaration is independent of the current derived definition.
2405
2406 if B /= T and then not Is_Generic_Type (B) then
2407 Ensure_Freeze_Node (B);
2408 Set_First_Subtype_Link (Freeze_Node (B), T);
2409 end if;
2410
2411 -- A type that is imported through a limited_with clause cannot
2412 -- generate any code, and thus need not be frozen. However, an access
2413 -- type with an imported designated type needs a finalization list,
2414 -- which may be referenced in some other package that has non-limited
2415 -- visibility on the designated type. Thus we must create the
2416 -- finalization list at the point the access type is frozen, to
2417 -- prevent unsatisfied references at link time.
2418
2419 if not From_With_Type (T) or else Is_Access_Type (T) then
2420 Set_Has_Delayed_Freeze (T);
2421 end if;
2422 end;
2423
2424 -- Case where T is the full declaration of some private type which has
2425 -- been swapped in Defining_Identifier (N).
2426
2427 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2428 Process_Full_View (N, T, Def_Id);
2429
2430 -- Record the reference. The form of this is a little strange, since
2431 -- the full declaration has been swapped in. So the first parameter
2432 -- here represents the entity to which a reference is made which is
2433 -- the "real" entity, i.e. the one swapped in, and the second
2434 -- parameter provides the reference location.
2435
2436 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2437 -- since we don't want a complaint about the full type being an
2438 -- unwanted reference to the private type
2439
2440 declare
2441 B : constant Boolean := Has_Pragma_Unreferenced (T);
2442 begin
2443 Set_Has_Pragma_Unreferenced (T, False);
2444 Generate_Reference (T, T, 'c');
2445 Set_Has_Pragma_Unreferenced (T, B);
2446 end;
2447
2448 Set_Completion_Referenced (Def_Id);
2449
2450 -- For completion of incomplete type, process incomplete dependents
2451 -- and always mark the full type as referenced (it is the incomplete
2452 -- type that we get for any real reference).
2453
2454 elsif Ekind (Prev) = E_Incomplete_Type then
2455 Process_Incomplete_Dependents (N, T, Prev);
2456 Generate_Reference (Prev, Def_Id, 'c');
2457 Set_Completion_Referenced (Def_Id);
2458
2459 -- If not private type or incomplete type completion, this is a real
2460 -- definition of a new entity, so record it.
2461
2462 else
2463 Generate_Definition (Def_Id);
2464 end if;
2465
2466 if Chars (Scope (Def_Id)) = Name_System
2467 and then Chars (Def_Id) = Name_Address
2468 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2469 then
2470 Set_Is_Descendent_Of_Address (Def_Id);
2471 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2472 Set_Is_Descendent_Of_Address (Prev);
2473 end if;
2474
2475 Set_Optimize_Alignment_Flags (Def_Id);
2476 Check_Eliminated (Def_Id);
2477
2478 if Has_Aspects (N) then
2479 Analyze_Aspect_Specifications (N, Def_Id);
2480 end if;
2481 end Analyze_Full_Type_Declaration;
2482
2483 ----------------------------------
2484 -- Analyze_Incomplete_Type_Decl --
2485 ----------------------------------
2486
2487 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2488 F : constant Boolean := Is_Pure (Current_Scope);
2489 T : Entity_Id;
2490
2491 begin
2492 Check_Formal_Restriction ("incomplete type is not allowed", N);
2493
2494 Generate_Definition (Defining_Identifier (N));
2495
2496 -- Process an incomplete declaration. The identifier must not have been
2497 -- declared already in the scope. However, an incomplete declaration may
2498 -- appear in the private part of a package, for a private type that has
2499 -- already been declared.
2500
2501 -- In this case, the discriminants (if any) must match
2502
2503 T := Find_Type_Name (N);
2504
2505 Set_Ekind (T, E_Incomplete_Type);
2506 Init_Size_Align (T);
2507 Set_Is_First_Subtype (T, True);
2508 Set_Etype (T, T);
2509
2510 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2511 -- incomplete types.
2512
2513 if Tagged_Present (N) then
2514 Set_Is_Tagged_Type (T);
2515 Make_Class_Wide_Type (T);
2516 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2517 end if;
2518
2519 Push_Scope (T);
2520
2521 Set_Stored_Constraint (T, No_Elist);
2522
2523 if Present (Discriminant_Specifications (N)) then
2524 Process_Discriminants (N);
2525 end if;
2526
2527 End_Scope;
2528
2529 -- If the type has discriminants, non-trivial subtypes may be
2530 -- declared before the full view of the type. The full views of those
2531 -- subtypes will be built after the full view of the type.
2532
2533 Set_Private_Dependents (T, New_Elmt_List);
2534 Set_Is_Pure (T, F);
2535 end Analyze_Incomplete_Type_Decl;
2536
2537 -----------------------------------
2538 -- Analyze_Interface_Declaration --
2539 -----------------------------------
2540
2541 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2542 CW : constant Entity_Id := Class_Wide_Type (T);
2543
2544 begin
2545 Set_Is_Tagged_Type (T);
2546
2547 Set_Is_Limited_Record (T, Limited_Present (Def)
2548 or else Task_Present (Def)
2549 or else Protected_Present (Def)
2550 or else Synchronized_Present (Def));
2551
2552 -- Type is abstract if full declaration carries keyword, or if previous
2553 -- partial view did.
2554
2555 Set_Is_Abstract_Type (T);
2556 Set_Is_Interface (T);
2557
2558 -- Type is a limited interface if it includes the keyword limited, task,
2559 -- protected, or synchronized.
2560
2561 Set_Is_Limited_Interface
2562 (T, Limited_Present (Def)
2563 or else Protected_Present (Def)
2564 or else Synchronized_Present (Def)
2565 or else Task_Present (Def));
2566
2567 Set_Interfaces (T, New_Elmt_List);
2568 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2569
2570 -- Complete the decoration of the class-wide entity if it was already
2571 -- built (i.e. during the creation of the limited view)
2572
2573 if Present (CW) then
2574 Set_Is_Interface (CW);
2575 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2576 end if;
2577
2578 -- Check runtime support for synchronized interfaces
2579
2580 if VM_Target = No_VM
2581 and then (Is_Task_Interface (T)
2582 or else Is_Protected_Interface (T)
2583 or else Is_Synchronized_Interface (T))
2584 and then not RTE_Available (RE_Select_Specific_Data)
2585 then
2586 Error_Msg_CRT ("synchronized interfaces", T);
2587 end if;
2588 end Analyze_Interface_Declaration;
2589
2590 -----------------------------
2591 -- Analyze_Itype_Reference --
2592 -----------------------------
2593
2594 -- Nothing to do. This node is placed in the tree only for the benefit of
2595 -- back end processing, and has no effect on the semantic processing.
2596
2597 procedure Analyze_Itype_Reference (N : Node_Id) is
2598 begin
2599 pragma Assert (Is_Itype (Itype (N)));
2600 null;
2601 end Analyze_Itype_Reference;
2602
2603 --------------------------------
2604 -- Analyze_Number_Declaration --
2605 --------------------------------
2606
2607 procedure Analyze_Number_Declaration (N : Node_Id) is
2608 Id : constant Entity_Id := Defining_Identifier (N);
2609 E : constant Node_Id := Expression (N);
2610 T : Entity_Id;
2611 Index : Interp_Index;
2612 It : Interp;
2613
2614 begin
2615 Generate_Definition (Id);
2616 Enter_Name (Id);
2617
2618 -- This is an optimization of a common case of an integer literal
2619
2620 if Nkind (E) = N_Integer_Literal then
2621 Set_Is_Static_Expression (E, True);
2622 Set_Etype (E, Universal_Integer);
2623
2624 Set_Etype (Id, Universal_Integer);
2625 Set_Ekind (Id, E_Named_Integer);
2626 Set_Is_Frozen (Id, True);
2627 return;
2628 end if;
2629
2630 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2631
2632 -- Process expression, replacing error by integer zero, to avoid
2633 -- cascaded errors or aborts further along in the processing
2634
2635 -- Replace Error by integer zero, which seems least likely to
2636 -- cause cascaded errors.
2637
2638 if E = Error then
2639 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2640 Set_Error_Posted (E);
2641 end if;
2642
2643 Analyze (E);
2644
2645 -- Verify that the expression is static and numeric. If
2646 -- the expression is overloaded, we apply the preference
2647 -- rule that favors root numeric types.
2648
2649 if not Is_Overloaded (E) then
2650 T := Etype (E);
2651
2652 else
2653 T := Any_Type;
2654
2655 Get_First_Interp (E, Index, It);
2656 while Present (It.Typ) loop
2657 if (Is_Integer_Type (It.Typ)
2658 or else Is_Real_Type (It.Typ))
2659 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2660 then
2661 if T = Any_Type then
2662 T := It.Typ;
2663
2664 elsif It.Typ = Universal_Real
2665 or else It.Typ = Universal_Integer
2666 then
2667 -- Choose universal interpretation over any other
2668
2669 T := It.Typ;
2670 exit;
2671 end if;
2672 end if;
2673
2674 Get_Next_Interp (Index, It);
2675 end loop;
2676 end if;
2677
2678 if Is_Integer_Type (T) then
2679 Resolve (E, T);
2680 Set_Etype (Id, Universal_Integer);
2681 Set_Ekind (Id, E_Named_Integer);
2682
2683 elsif Is_Real_Type (T) then
2684
2685 -- Because the real value is converted to universal_real, this is a
2686 -- legal context for a universal fixed expression.
2687
2688 if T = Universal_Fixed then
2689 declare
2690 Loc : constant Source_Ptr := Sloc (N);
2691 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2692 Subtype_Mark =>
2693 New_Occurrence_Of (Universal_Real, Loc),
2694 Expression => Relocate_Node (E));
2695
2696 begin
2697 Rewrite (E, Conv);
2698 Analyze (E);
2699 end;
2700
2701 elsif T = Any_Fixed then
2702 Error_Msg_N ("illegal context for mixed mode operation", E);
2703
2704 -- Expression is of the form : universal_fixed * integer. Try to
2705 -- resolve as universal_real.
2706
2707 T := Universal_Real;
2708 Set_Etype (E, T);
2709 end if;
2710
2711 Resolve (E, T);
2712 Set_Etype (Id, Universal_Real);
2713 Set_Ekind (Id, E_Named_Real);
2714
2715 else
2716 Wrong_Type (E, Any_Numeric);
2717 Resolve (E, T);
2718
2719 Set_Etype (Id, T);
2720 Set_Ekind (Id, E_Constant);
2721 Set_Never_Set_In_Source (Id, True);
2722 Set_Is_True_Constant (Id, True);
2723 return;
2724 end if;
2725
2726 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2727 Set_Etype (E, Etype (Id));
2728 end if;
2729
2730 if not Is_OK_Static_Expression (E) then
2731 Flag_Non_Static_Expr
2732 ("non-static expression used in number declaration!", E);
2733 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2734 Set_Etype (E, Any_Type);
2735 end if;
2736 end Analyze_Number_Declaration;
2737
2738 --------------------------------
2739 -- Analyze_Object_Declaration --
2740 --------------------------------
2741
2742 procedure Analyze_Object_Declaration (N : Node_Id) is
2743 Loc : constant Source_Ptr := Sloc (N);
2744 Id : constant Entity_Id := Defining_Identifier (N);
2745 T : Entity_Id;
2746 Act_T : Entity_Id;
2747
2748 E : Node_Id := Expression (N);
2749 -- E is set to Expression (N) throughout this routine. When
2750 -- Expression (N) is modified, E is changed accordingly.
2751
2752 Prev_Entity : Entity_Id := Empty;
2753
2754 function Count_Tasks (T : Entity_Id) return Uint;
2755 -- This function is called when a non-generic library level object of a
2756 -- task type is declared. Its function is to count the static number of
2757 -- tasks declared within the type (it is only called if Has_Tasks is set
2758 -- for T). As a side effect, if an array of tasks with non-static bounds
2759 -- or a variant record type is encountered, Check_Restrictions is called
2760 -- indicating the count is unknown.
2761
2762 -----------------
2763 -- Count_Tasks --
2764 -----------------
2765
2766 function Count_Tasks (T : Entity_Id) return Uint is
2767 C : Entity_Id;
2768 X : Node_Id;
2769 V : Uint;
2770
2771 begin
2772 if Is_Task_Type (T) then
2773 return Uint_1;
2774
2775 elsif Is_Record_Type (T) then
2776 if Has_Discriminants (T) then
2777 Check_Restriction (Max_Tasks, N);
2778 return Uint_0;
2779
2780 else
2781 V := Uint_0;
2782 C := First_Component (T);
2783 while Present (C) loop
2784 V := V + Count_Tasks (Etype (C));
2785 Next_Component (C);
2786 end loop;
2787
2788 return V;
2789 end if;
2790
2791 elsif Is_Array_Type (T) then
2792 X := First_Index (T);
2793 V := Count_Tasks (Component_Type (T));
2794 while Present (X) loop
2795 C := Etype (X);
2796
2797 if not Is_Static_Subtype (C) then
2798 Check_Restriction (Max_Tasks, N);
2799 return Uint_0;
2800 else
2801 V := V * (UI_Max (Uint_0,
2802 Expr_Value (Type_High_Bound (C)) -
2803 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2804 end if;
2805
2806 Next_Index (X);
2807 end loop;
2808
2809 return V;
2810
2811 else
2812 return Uint_0;
2813 end if;
2814 end Count_Tasks;
2815
2816 -- Start of processing for Analyze_Object_Declaration
2817
2818 begin
2819 -- There are three kinds of implicit types generated by an
2820 -- object declaration:
2821
2822 -- 1. Those generated by the original Object Definition
2823
2824 -- 2. Those generated by the Expression
2825
2826 -- 3. Those used to constrained the Object Definition with the
2827 -- expression constraints when it is unconstrained
2828
2829 -- They must be generated in this order to avoid order of elaboration
2830 -- issues. Thus the first step (after entering the name) is to analyze
2831 -- the object definition.
2832
2833 if Constant_Present (N) then
2834 Prev_Entity := Current_Entity_In_Scope (Id);
2835
2836 if Present (Prev_Entity)
2837 and then
2838 -- If the homograph is an implicit subprogram, it is overridden
2839 -- by the current declaration.
2840
2841 ((Is_Overloadable (Prev_Entity)
2842 and then Is_Inherited_Operation (Prev_Entity))
2843
2844 -- The current object is a discriminal generated for an entry
2845 -- family index. Even though the index is a constant, in this
2846 -- particular context there is no true constant redeclaration.
2847 -- Enter_Name will handle the visibility.
2848
2849 or else
2850 (Is_Discriminal (Id)
2851 and then Ekind (Discriminal_Link (Id)) =
2852 E_Entry_Index_Parameter)
2853
2854 -- The current object is the renaming for a generic declared
2855 -- within the instance.
2856
2857 or else
2858 (Ekind (Prev_Entity) = E_Package
2859 and then Nkind (Parent (Prev_Entity)) =
2860 N_Package_Renaming_Declaration
2861 and then not Comes_From_Source (Prev_Entity)
2862 and then Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
2863 then
2864 Prev_Entity := Empty;
2865 end if;
2866 end if;
2867
2868 if Present (Prev_Entity) then
2869 Constant_Redeclaration (Id, N, T);
2870
2871 Generate_Reference (Prev_Entity, Id, 'c');
2872 Set_Completion_Referenced (Id);
2873
2874 if Error_Posted (N) then
2875
2876 -- Type mismatch or illegal redeclaration, Do not analyze
2877 -- expression to avoid cascaded errors.
2878
2879 T := Find_Type_Of_Object (Object_Definition (N), N);
2880 Set_Etype (Id, T);
2881 Set_Ekind (Id, E_Variable);
2882 goto Leave;
2883 end if;
2884
2885 -- In the normal case, enter identifier at the start to catch premature
2886 -- usage in the initialization expression.
2887
2888 else
2889 Generate_Definition (Id);
2890 Enter_Name (Id);
2891
2892 Mark_Coextensions (N, Object_Definition (N));
2893
2894 T := Find_Type_Of_Object (Object_Definition (N), N);
2895
2896 if Nkind (Object_Definition (N)) = N_Access_Definition
2897 and then Present
2898 (Access_To_Subprogram_Definition (Object_Definition (N)))
2899 and then Protected_Present
2900 (Access_To_Subprogram_Definition (Object_Definition (N)))
2901 then
2902 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2903 end if;
2904
2905 if Error_Posted (Id) then
2906 Set_Etype (Id, T);
2907 Set_Ekind (Id, E_Variable);
2908 goto Leave;
2909 end if;
2910 end if;
2911
2912 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2913 -- out some static checks
2914
2915 if Ada_Version >= Ada_2005
2916 and then Can_Never_Be_Null (T)
2917 then
2918 -- In case of aggregates we must also take care of the correct
2919 -- initialization of nested aggregates bug this is done at the
2920 -- point of the analysis of the aggregate (see sem_aggr.adb)
2921
2922 if Present (Expression (N))
2923 and then Nkind (Expression (N)) = N_Aggregate
2924 then
2925 null;
2926
2927 else
2928 declare
2929 Save_Typ : constant Entity_Id := Etype (Id);
2930 begin
2931 Set_Etype (Id, T); -- Temp. decoration for static checks
2932 Null_Exclusion_Static_Checks (N);
2933 Set_Etype (Id, Save_Typ);
2934 end;
2935 end if;
2936 end if;
2937
2938 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2939
2940 -- If deferred constant, make sure context is appropriate. We detect
2941 -- a deferred constant as a constant declaration with no expression.
2942 -- A deferred constant can appear in a package body if its completion
2943 -- is by means of an interface pragma.
2944
2945 if Constant_Present (N)
2946 and then No (E)
2947 then
2948 -- A deferred constant may appear in the declarative part of the
2949 -- following constructs:
2950
2951 -- blocks
2952 -- entry bodies
2953 -- extended return statements
2954 -- package specs
2955 -- package bodies
2956 -- subprogram bodies
2957 -- task bodies
2958
2959 -- When declared inside a package spec, a deferred constant must be
2960 -- completed by a full constant declaration or pragma Import. In all
2961 -- other cases, the only proper completion is pragma Import. Extended
2962 -- return statements are flagged as invalid contexts because they do
2963 -- not have a declarative part and so cannot accommodate the pragma.
2964
2965 if Ekind (Current_Scope) = E_Return_Statement then
2966 Error_Msg_N
2967 ("invalid context for deferred constant declaration (RM 7.4)",
2968 N);
2969 Error_Msg_N
2970 ("\declaration requires an initialization expression",
2971 N);
2972 Set_Constant_Present (N, False);
2973
2974 -- In Ada 83, deferred constant must be of private type
2975
2976 elsif not Is_Private_Type (T) then
2977 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2978 Error_Msg_N
2979 ("(Ada 83) deferred constant must be private type", N);
2980 end if;
2981 end if;
2982
2983 -- If not a deferred constant, then object declaration freezes its type
2984
2985 else
2986 Check_Fully_Declared (T, N);
2987 Freeze_Before (N, T);
2988 end if;
2989
2990 -- If the object was created by a constrained array definition, then
2991 -- set the link in both the anonymous base type and anonymous subtype
2992 -- that are built to represent the array type to point to the object.
2993
2994 if Nkind (Object_Definition (Declaration_Node (Id))) =
2995 N_Constrained_Array_Definition
2996 then
2997 Set_Related_Array_Object (T, Id);
2998 Set_Related_Array_Object (Base_Type (T), Id);
2999 end if;
3000
3001 -- Special checks for protected objects not at library level
3002
3003 if Is_Protected_Type (T)
3004 and then not Is_Library_Level_Entity (Id)
3005 then
3006 Check_Restriction (No_Local_Protected_Objects, Id);
3007
3008 -- Protected objects with interrupt handlers must be at library level
3009
3010 -- Ada 2005: this test is not needed (and the corresponding clause
3011 -- in the RM is removed) because accessibility checks are sufficient
3012 -- to make handlers not at the library level illegal.
3013
3014 if Has_Interrupt_Handler (T)
3015 and then Ada_Version < Ada_2005
3016 then
3017 Error_Msg_N
3018 ("interrupt object can only be declared at library level", Id);
3019 end if;
3020 end if;
3021
3022 -- The actual subtype of the object is the nominal subtype, unless
3023 -- the nominal one is unconstrained and obtained from the expression.
3024
3025 Act_T := T;
3026
3027 -- These checks should be performed before the initialization expression
3028 -- is considered, so that the Object_Definition node is still the same
3029 -- as in source code.
3030
3031 -- In SPARK or ALFA, the nominal subtype shall be given by a subtype
3032 -- mark and shall not be unconstrained. (The only exception to this
3033 -- is the admission of declarations of constants of type String.)
3034
3035 if not Nkind_In (Object_Definition (N), N_Identifier,
3036 N_Expanded_Name)
3037 then
3038 Check_Formal_Restriction
3039 ("subtype mark expected", Object_Definition (N));
3040
3041 elsif Is_Array_Type (T)
3042 and then not Is_Constrained (T)
3043 and then T /= Standard_String
3044 then
3045 Check_Formal_Restriction
3046 ("subtype mark of constrained type expected",
3047 Object_Definition (N));
3048 end if;
3049
3050 -- There are no aliased objects in SPARK or ALFA
3051
3052 if Aliased_Present (N) then
3053 Check_Formal_Restriction ("aliased object is not allowed", N);
3054 end if;
3055
3056 -- Process initialization expression if present and not in error
3057
3058 if Present (E) and then E /= Error then
3059
3060 -- Generate an error in case of CPP class-wide object initialization.
3061 -- Required because otherwise the expansion of the class-wide
3062 -- assignment would try to use 'size to initialize the object
3063 -- (primitive that is not available in CPP tagged types).
3064
3065 if Is_Class_Wide_Type (Act_T)
3066 and then
3067 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3068 or else
3069 (Present (Full_View (Root_Type (Etype (Act_T))))
3070 and then
3071 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3072 then
3073 Error_Msg_N
3074 ("predefined assignment not available for 'C'P'P tagged types",
3075 E);
3076 end if;
3077
3078 Mark_Coextensions (N, E);
3079 Analyze (E);
3080
3081 -- In case of errors detected in the analysis of the expression,
3082 -- decorate it with the expected type to avoid cascaded errors
3083
3084 if No (Etype (E)) then
3085 Set_Etype (E, T);
3086 end if;
3087
3088 -- If an initialization expression is present, then we set the
3089 -- Is_True_Constant flag. It will be reset if this is a variable
3090 -- and it is indeed modified.
3091
3092 Set_Is_True_Constant (Id, True);
3093
3094 -- If we are analyzing a constant declaration, set its completion
3095 -- flag after analyzing and resolving the expression.
3096
3097 if Constant_Present (N) then
3098 Set_Has_Completion (Id);
3099 end if;
3100
3101 -- Set type and resolve (type may be overridden later on)
3102
3103 Set_Etype (Id, T);
3104 Resolve (E, T);
3105
3106 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3107 -- node (which was marked already-analyzed), we need to set the type
3108 -- to something other than Any_Access in order to keep gigi happy.
3109
3110 if Etype (E) = Any_Access then
3111 Set_Etype (E, T);
3112 end if;
3113
3114 -- If the object is an access to variable, the initialization
3115 -- expression cannot be an access to constant.
3116
3117 if Is_Access_Type (T)
3118 and then not Is_Access_Constant (T)
3119 and then Is_Access_Type (Etype (E))
3120 and then Is_Access_Constant (Etype (E))
3121 then
3122 Error_Msg_N
3123 ("access to variable cannot be initialized "
3124 & "with an access-to-constant expression", E);
3125 end if;
3126
3127 if not Assignment_OK (N) then
3128 Check_Initialization (T, E);
3129 end if;
3130
3131 Check_Unset_Reference (E);
3132
3133 -- If this is a variable, then set current value. If this is a
3134 -- declared constant of a scalar type with a static expression,
3135 -- indicate that it is always valid.
3136
3137 if not Constant_Present (N) then
3138 if Compile_Time_Known_Value (E) then
3139 Set_Current_Value (Id, E);
3140 end if;
3141
3142 elsif Is_Scalar_Type (T)
3143 and then Is_OK_Static_Expression (E)
3144 then
3145 Set_Is_Known_Valid (Id);
3146 end if;
3147
3148 -- Deal with setting of null flags
3149
3150 if Is_Access_Type (T) then
3151 if Known_Non_Null (E) then
3152 Set_Is_Known_Non_Null (Id, True);
3153 elsif Known_Null (E)
3154 and then not Can_Never_Be_Null (Id)
3155 then
3156 Set_Is_Known_Null (Id, True);
3157 end if;
3158 end if;
3159
3160 -- Check incorrect use of dynamically tagged expressions.
3161
3162 if Is_Tagged_Type (T) then
3163 Check_Dynamically_Tagged_Expression
3164 (Expr => E,
3165 Typ => T,
3166 Related_Nod => N);
3167 end if;
3168
3169 Apply_Scalar_Range_Check (E, T);
3170 Apply_Static_Length_Check (E, T);
3171
3172 if Nkind (Original_Node (N)) = N_Object_Declaration
3173 and then Comes_From_Source (Original_Node (N))
3174 and then Formal_Verification_Mode -- only call test if needed
3175 and then not Is_SPARK_Initialization_Expr (E)
3176 then
3177 Check_Formal_Restriction
3178 ("initialization expression is not appropriate", E);
3179 end if;
3180 end if;
3181
3182 -- If the No_Streams restriction is set, check that the type of the
3183 -- object is not, and does not contain, any subtype derived from
3184 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3185 -- Has_Stream just for efficiency reasons. There is no point in
3186 -- spending time on a Has_Stream check if the restriction is not set.
3187
3188 if Restriction_Check_Required (No_Streams) then
3189 if Has_Stream (T) then
3190 Check_Restriction (No_Streams, N);
3191 end if;
3192 end if;
3193
3194 -- Deal with predicate check before we start to do major rewriting.
3195 -- it is OK to initialize and then check the initialized value, since
3196 -- the object goes out of scope if we get a predicate failure. Note
3197 -- that we do this in the analyzer and not the expander because the
3198 -- analyzer does some substantial rewriting in some cases.
3199
3200 -- We need a predicate check if the type has predicates, and if either
3201 -- there is an initializing expression, or for default initialization
3202 -- when we have at least one case of an explicit default initial value.
3203
3204 if not Suppress_Assignment_Checks (N)
3205 and then Present (Predicate_Function (T))
3206 and then
3207 (Present (E)
3208 or else
3209 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3210 then
3211 Insert_After (N,
3212 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3213 end if;
3214
3215 -- Case of unconstrained type
3216
3217 if Is_Indefinite_Subtype (T) then
3218
3219 -- Nothing to do in deferred constant case
3220
3221 if Constant_Present (N) and then No (E) then
3222 null;
3223
3224 -- Case of no initialization present
3225
3226 elsif No (E) then
3227 if No_Initialization (N) then
3228 null;
3229
3230 elsif Is_Class_Wide_Type (T) then
3231 Error_Msg_N
3232 ("initialization required in class-wide declaration ", N);
3233
3234 else
3235 Error_Msg_N
3236 ("unconstrained subtype not allowed (need initialization)",
3237 Object_Definition (N));
3238
3239 if Is_Record_Type (T) and then Has_Discriminants (T) then
3240 Error_Msg_N
3241 ("\provide initial value or explicit discriminant values",
3242 Object_Definition (N));
3243
3244 Error_Msg_NE
3245 ("\or give default discriminant values for type&",
3246 Object_Definition (N), T);
3247
3248 elsif Is_Array_Type (T) then
3249 Error_Msg_N
3250 ("\provide initial value or explicit array bounds",
3251 Object_Definition (N));
3252 end if;
3253 end if;
3254
3255 -- Case of initialization present but in error. Set initial
3256 -- expression as absent (but do not make above complaints)
3257
3258 elsif E = Error then
3259 Set_Expression (N, Empty);
3260 E := Empty;
3261
3262 -- Case of initialization present
3263
3264 else
3265 -- Not allowed in Ada 83
3266
3267 if not Constant_Present (N) then
3268 if Ada_Version = Ada_83
3269 and then Comes_From_Source (Object_Definition (N))
3270 then
3271 Error_Msg_N
3272 ("(Ada 83) unconstrained variable not allowed",
3273 Object_Definition (N));
3274 end if;
3275 end if;
3276
3277 -- Now we constrain the variable from the initializing expression
3278
3279 -- If the expression is an aggregate, it has been expanded into
3280 -- individual assignments. Retrieve the actual type from the
3281 -- expanded construct.
3282
3283 if Is_Array_Type (T)
3284 and then No_Initialization (N)
3285 and then Nkind (Original_Node (E)) = N_Aggregate
3286 then
3287 Act_T := Etype (E);
3288
3289 -- In case of class-wide interface object declarations we delay
3290 -- the generation of the equivalent record type declarations until
3291 -- its expansion because there are cases in they are not required.
3292
3293 elsif Is_Interface (T) then
3294 null;
3295
3296 else
3297 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3298 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3299 end if;
3300
3301 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
3302
3303 if Aliased_Present (N) then
3304 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3305 end if;
3306
3307 Freeze_Before (N, Act_T);
3308 Freeze_Before (N, T);
3309 end if;
3310
3311 elsif Is_Array_Type (T)
3312 and then No_Initialization (N)
3313 and then Nkind (Original_Node (E)) = N_Aggregate
3314 then
3315 if not Is_Entity_Name (Object_Definition (N)) then
3316 Act_T := Etype (E);
3317 Check_Compile_Time_Size (Act_T);
3318
3319 if Aliased_Present (N) then
3320 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3321 end if;
3322 end if;
3323
3324 -- When the given object definition and the aggregate are specified
3325 -- independently, and their lengths might differ do a length check.
3326 -- This cannot happen if the aggregate is of the form (others =>...)
3327
3328 if not Is_Constrained (T) then
3329 null;
3330
3331 elsif Nkind (E) = N_Raise_Constraint_Error then
3332
3333 -- Aggregate is statically illegal. Place back in declaration
3334
3335 Set_Expression (N, E);
3336 Set_No_Initialization (N, False);
3337
3338 elsif T = Etype (E) then
3339 null;
3340
3341 elsif Nkind (E) = N_Aggregate
3342 and then Present (Component_Associations (E))
3343 and then Present (Choices (First (Component_Associations (E))))
3344 and then Nkind (First
3345 (Choices (First (Component_Associations (E))))) = N_Others_Choice
3346 then
3347 null;
3348
3349 else
3350 Apply_Length_Check (E, T);
3351 end if;
3352
3353 -- If the type is limited unconstrained with defaulted discriminants and
3354 -- there is no expression, then the object is constrained by the
3355 -- defaults, so it is worthwhile building the corresponding subtype.
3356
3357 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
3358 and then not Is_Constrained (T)
3359 and then Has_Discriminants (T)
3360 then
3361 if No (E) then
3362 Act_T := Build_Default_Subtype (T, N);
3363 else
3364 -- Ada 2005: a limited object may be initialized by means of an
3365 -- aggregate. If the type has default discriminants it has an
3366 -- unconstrained nominal type, Its actual subtype will be obtained
3367 -- from the aggregate, and not from the default discriminants.
3368
3369 Act_T := Etype (E);
3370 end if;
3371
3372 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
3373
3374 elsif Present (Underlying_Type (T))
3375 and then not Is_Constrained (Underlying_Type (T))
3376 and then Has_Discriminants (Underlying_Type (T))
3377 and then Nkind (E) = N_Function_Call
3378 and then Constant_Present (N)
3379 then
3380 -- The back-end has problems with constants of a discriminated type
3381 -- with defaults, if the initial value is a function call. We
3382 -- generate an intermediate temporary for the result of the call.
3383 -- It is unclear why this should make it acceptable to gcc. ???
3384
3385 Remove_Side_Effects (E);
3386 end if;
3387
3388 -- Check No_Wide_Characters restriction
3389
3390 Check_Wide_Character_Restriction (T, Object_Definition (N));
3391
3392 -- Indicate this is not set in source. Certainly true for constants,
3393 -- and true for variables so far (will be reset for a variable if and
3394 -- when we encounter a modification in the source).
3395
3396 Set_Never_Set_In_Source (Id, True);
3397
3398 -- Now establish the proper kind and type of the object
3399
3400 if Constant_Present (N) then
3401 Set_Ekind (Id, E_Constant);
3402 Set_Is_True_Constant (Id, True);
3403
3404 else
3405 Set_Ekind (Id, E_Variable);
3406
3407 -- A variable is set as shared passive if it appears in a shared
3408 -- passive package, and is at the outer level. This is not done
3409 -- for entities generated during expansion, because those are
3410 -- always manipulated locally.
3411
3412 if Is_Shared_Passive (Current_Scope)
3413 and then Is_Library_Level_Entity (Id)
3414 and then Comes_From_Source (Id)
3415 then
3416 Set_Is_Shared_Passive (Id);
3417 Check_Shared_Var (Id, T, N);
3418 end if;
3419
3420 -- Set Has_Initial_Value if initializing expression present. Note
3421 -- that if there is no initializing expression, we leave the state
3422 -- of this flag unchanged (usually it will be False, but notably in
3423 -- the case of exception choice variables, it will already be true).
3424
3425 if Present (E) then
3426 Set_Has_Initial_Value (Id, True);
3427 end if;
3428 end if;
3429
3430 -- Initialize alignment and size and capture alignment setting
3431
3432 Init_Alignment (Id);
3433 Init_Esize (Id);
3434 Set_Optimize_Alignment_Flags (Id);
3435
3436 -- Deal with aliased case
3437
3438 if Aliased_Present (N) then
3439 Set_Is_Aliased (Id);
3440
3441 -- If the object is aliased and the type is unconstrained with
3442 -- defaulted discriminants and there is no expression, then the
3443 -- object is constrained by the defaults, so it is worthwhile
3444 -- building the corresponding subtype.
3445
3446 -- Ada 2005 (AI-363): If the aliased object is discriminated and
3447 -- unconstrained, then only establish an actual subtype if the
3448 -- nominal subtype is indefinite. In definite cases the object is
3449 -- unconstrained in Ada 2005.
3450
3451 if No (E)
3452 and then Is_Record_Type (T)
3453 and then not Is_Constrained (T)
3454 and then Has_Discriminants (T)
3455 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
3456 then
3457 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
3458 end if;
3459 end if;
3460
3461 -- Now we can set the type of the object
3462
3463 Set_Etype (Id, Act_T);
3464
3465 -- Deal with controlled types
3466
3467 if Has_Controlled_Component (Etype (Id))
3468 or else Is_Controlled (Etype (Id))
3469 then
3470 if not Is_Library_Level_Entity (Id) then
3471 Check_Restriction (No_Nested_Finalization, N);
3472 else
3473 Validate_Controlled_Object (Id);
3474 end if;
3475
3476 -- Generate a warning when an initialization causes an obvious ABE
3477 -- violation. If the init expression is a simple aggregate there
3478 -- shouldn't be any initialize/adjust call generated. This will be
3479 -- true as soon as aggregates are built in place when possible.
3480
3481 -- ??? at the moment we do not generate warnings for temporaries
3482 -- created for those aggregates although Program_Error might be
3483 -- generated if compiled with -gnato.
3484
3485 if Is_Controlled (Etype (Id))
3486 and then Comes_From_Source (Id)
3487 then
3488 declare
3489 BT : constant Entity_Id := Base_Type (Etype (Id));
3490
3491 Implicit_Call : Entity_Id;
3492 pragma Warnings (Off, Implicit_Call);
3493 -- ??? what is this for (never referenced!)
3494
3495 function Is_Aggr (N : Node_Id) return Boolean;
3496 -- Check that N is an aggregate
3497
3498 -------------
3499 -- Is_Aggr --
3500 -------------
3501
3502 function Is_Aggr (N : Node_Id) return Boolean is
3503 begin
3504 case Nkind (Original_Node (N)) is
3505 when N_Aggregate | N_Extension_Aggregate =>
3506 return True;
3507
3508 when N_Qualified_Expression |
3509 N_Type_Conversion |
3510 N_Unchecked_Type_Conversion =>
3511 return Is_Aggr (Expression (Original_Node (N)));
3512
3513 when others =>
3514 return False;
3515 end case;
3516 end Is_Aggr;
3517
3518 begin
3519 -- If no underlying type, we already are in an error situation.
3520 -- Do not try to add a warning since we do not have access to
3521 -- prim-op list.
3522
3523 if No (Underlying_Type (BT)) then
3524 Implicit_Call := Empty;
3525
3526 -- A generic type does not have usable primitive operators.
3527 -- Initialization calls are built for instances.
3528
3529 elsif Is_Generic_Type (BT) then
3530 Implicit_Call := Empty;
3531
3532 -- If the init expression is not an aggregate, an adjust call
3533 -- will be generated
3534
3535 elsif Present (E) and then not Is_Aggr (E) then
3536 Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
3537
3538 -- If no init expression and we are not in the deferred
3539 -- constant case, an Initialize call will be generated
3540
3541 elsif No (E) and then not Constant_Present (N) then
3542 Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
3543
3544 else
3545 Implicit_Call := Empty;
3546 end if;
3547 end;
3548 end if;
3549 end if;
3550
3551 if Has_Task (Etype (Id)) then
3552 Check_Restriction (No_Tasking, N);
3553
3554 -- Deal with counting max tasks
3555
3556 -- Nothing to do if inside a generic
3557
3558 if Inside_A_Generic then
3559 null;
3560
3561 -- If library level entity, then count tasks
3562
3563 elsif Is_Library_Level_Entity (Id) then
3564 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
3565
3566 -- If not library level entity, then indicate we don't know max
3567 -- tasks and also check task hierarchy restriction and blocking
3568 -- operation (since starting a task is definitely blocking!)
3569
3570 else
3571 Check_Restriction (Max_Tasks, N);
3572 Check_Restriction (No_Task_Hierarchy, N);
3573 Check_Potentially_Blocking_Operation (N);
3574 end if;
3575
3576 -- A rather specialized test. If we see two tasks being declared
3577 -- of the same type in the same object declaration, and the task
3578 -- has an entry with an address clause, we know that program error
3579 -- will be raised at run time since we can't have two tasks with
3580 -- entries at the same address.
3581
3582 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3583 declare
3584 E : Entity_Id;
3585
3586 begin
3587 E := First_Entity (Etype (Id));
3588 while Present (E) loop
3589 if Ekind (E) = E_Entry
3590 and then Present (Get_Attribute_Definition_Clause
3591 (E, Attribute_Address))
3592 then
3593 Error_Msg_N
3594 ("?more than one task with same entry address", N);
3595 Error_Msg_N
3596 ("\?Program_Error will be raised at run time", N);
3597 Insert_Action (N,
3598 Make_Raise_Program_Error (Loc,
3599 Reason => PE_Duplicated_Entry_Address));
3600 exit;
3601 end if;
3602
3603 Next_Entity (E);
3604 end loop;
3605 end;
3606 end if;
3607 end if;
3608
3609 -- Some simple constant-propagation: if the expression is a constant
3610 -- string initialized with a literal, share the literal. This avoids
3611 -- a run-time copy.
3612
3613 if Present (E)
3614 and then Is_Entity_Name (E)
3615 and then Ekind (Entity (E)) = E_Constant
3616 and then Base_Type (Etype (E)) = Standard_String
3617 then
3618 declare
3619 Val : constant Node_Id := Constant_Value (Entity (E));
3620 begin
3621 if Present (Val)
3622 and then Nkind (Val) = N_String_Literal
3623 then
3624 Rewrite (E, New_Copy (Val));
3625 end if;
3626 end;
3627 end if;
3628
3629 -- Another optimization: if the nominal subtype is unconstrained and
3630 -- the expression is a function call that returns an unconstrained
3631 -- type, rewrite the declaration as a renaming of the result of the
3632 -- call. The exceptions below are cases where the copy is expected,
3633 -- either by the back end (Aliased case) or by the semantics, as for
3634 -- initializing controlled types or copying tags for classwide types.
3635
3636 if Present (E)
3637 and then Nkind (E) = N_Explicit_Dereference
3638 and then Nkind (Original_Node (E)) = N_Function_Call
3639 and then not Is_Library_Level_Entity (Id)
3640 and then not Is_Constrained (Underlying_Type (T))
3641 and then not Is_Aliased (Id)
3642 and then not Is_Class_Wide_Type (T)
3643 and then not Is_Controlled (T)
3644 and then not Has_Controlled_Component (Base_Type (T))
3645 and then Expander_Active
3646 then
3647 Rewrite (N,
3648 Make_Object_Renaming_Declaration (Loc,
3649 Defining_Identifier => Id,
3650 Access_Definition => Empty,
3651 Subtype_Mark => New_Occurrence_Of
3652 (Base_Type (Etype (Id)), Loc),
3653 Name => E));
3654
3655 Set_Renamed_Object (Id, E);
3656
3657 -- Force generation of debugging information for the constant and for
3658 -- the renamed function call.
3659
3660 Set_Debug_Info_Needed (Id);
3661 Set_Debug_Info_Needed (Entity (Prefix (E)));
3662 end if;
3663
3664 if Present (Prev_Entity)
3665 and then Is_Frozen (Prev_Entity)
3666 and then not Error_Posted (Id)
3667 then
3668 Error_Msg_N ("full constant declaration appears too late", N);
3669 end if;
3670
3671 Check_Eliminated (Id);
3672
3673 -- Deal with setting In_Private_Part flag if in private part
3674
3675 if Ekind (Scope (Id)) = E_Package
3676 and then In_Private_Part (Scope (Id))
3677 then
3678 Set_In_Private_Part (Id);
3679 end if;
3680
3681 -- Check for violation of No_Local_Timing_Events
3682
3683 if Restriction_Check_Required (No_Local_Timing_Events)
3684 and then not Is_Library_Level_Entity (Id)
3685 and then Is_RTE (Etype (Id), RE_Timing_Event)
3686 then
3687 Check_Restriction (No_Local_Timing_Events, N);
3688 end if;
3689
3690 <<Leave>>
3691 if Has_Aspects (N) then
3692 Analyze_Aspect_Specifications (N, Id);
3693 end if;
3694 end Analyze_Object_Declaration;
3695
3696 ---------------------------
3697 -- Analyze_Others_Choice --
3698 ---------------------------
3699
3700 -- Nothing to do for the others choice node itself, the semantic analysis
3701 -- of the others choice will occur as part of the processing of the parent
3702
3703 procedure Analyze_Others_Choice (N : Node_Id) is
3704 pragma Warnings (Off, N);
3705 begin
3706 null;
3707 end Analyze_Others_Choice;
3708
3709 -------------------------------------------
3710 -- Analyze_Private_Extension_Declaration --
3711 -------------------------------------------
3712
3713 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3714 T : constant Entity_Id := Defining_Identifier (N);
3715 Indic : constant Node_Id := Subtype_Indication (N);
3716 Parent_Type : Entity_Id;
3717 Parent_Base : Entity_Id;
3718
3719 begin
3720 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3721
3722 if Is_Non_Empty_List (Interface_List (N)) then
3723 declare
3724 Intf : Node_Id;
3725 T : Entity_Id;
3726
3727 begin
3728 Intf := First (Interface_List (N));
3729 while Present (Intf) loop
3730 T := Find_Type_Of_Subtype_Indic (Intf);
3731
3732 Diagnose_Interface (Intf, T);
3733 Next (Intf);
3734 end loop;
3735 end;
3736 end if;
3737
3738 Generate_Definition (T);
3739
3740 -- For other than Ada 2012, just enter the name in the current scope
3741
3742 if Ada_Version < Ada_2012 then
3743 Enter_Name (T);
3744
3745 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
3746 -- case of private type that completes an incomplete type.
3747
3748 else
3749 declare
3750 Prev : Entity_Id;
3751
3752 begin
3753 Prev := Find_Type_Name (N);
3754
3755 pragma Assert (Prev = T
3756 or else (Ekind (Prev) = E_Incomplete_Type
3757 and then Present (Full_View (Prev))
3758 and then Full_View (Prev) = T));
3759 end;
3760 end if;
3761
3762 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3763 Parent_Base := Base_Type (Parent_Type);
3764
3765 if Parent_Type = Any_Type
3766 or else Etype (Parent_Type) = Any_Type
3767 then
3768 Set_Ekind (T, Ekind (Parent_Type));
3769 Set_Etype (T, Any_Type);
3770 goto Leave;
3771
3772 elsif not Is_Tagged_Type (Parent_Type) then
3773 Error_Msg_N
3774 ("parent of type extension must be a tagged type ", Indic);
3775 goto Leave;
3776
3777 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
3778 Error_Msg_N ("premature derivation of incomplete type", Indic);
3779 goto Leave;
3780
3781 elsif Is_Concurrent_Type (Parent_Type) then
3782 Error_Msg_N
3783 ("parent type of a private extension cannot be "
3784 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3785
3786 Set_Etype (T, Any_Type);
3787 Set_Ekind (T, E_Limited_Private_Type);
3788 Set_Private_Dependents (T, New_Elmt_List);
3789 Set_Error_Posted (T);
3790 goto Leave;
3791 end if;
3792
3793 -- Perhaps the parent type should be changed to the class-wide type's
3794 -- specific type in this case to prevent cascading errors ???
3795
3796 if Is_Class_Wide_Type (Parent_Type) then
3797 Error_Msg_N
3798 ("parent of type extension must not be a class-wide type", Indic);
3799 goto Leave;
3800 end if;
3801
3802 if (not Is_Package_Or_Generic_Package (Current_Scope)
3803 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3804 or else In_Private_Part (Current_Scope)
3805
3806 then
3807 Error_Msg_N ("invalid context for private extension", N);
3808 end if;
3809
3810 -- Set common attributes
3811
3812 Set_Is_Pure (T, Is_Pure (Current_Scope));
3813 Set_Scope (T, Current_Scope);
3814 Set_Ekind (T, E_Record_Type_With_Private);
3815 Init_Size_Align (T);
3816
3817 Set_Etype (T, Parent_Base);
3818 Set_Has_Task (T, Has_Task (Parent_Base));
3819
3820 Set_Convention (T, Convention (Parent_Type));
3821 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3822 Set_Is_First_Subtype (T);
3823 Make_Class_Wide_Type (T);
3824
3825 if Unknown_Discriminants_Present (N) then
3826 Set_Discriminant_Constraint (T, No_Elist);
3827 end if;
3828
3829 Build_Derived_Record_Type (N, Parent_Type, T);
3830
3831 -- Propagate inherited invariant information. The new type has
3832 -- invariants, if the parent type has inheritable invariants,
3833 -- and these invariants can in turn be inherited.
3834
3835 if Has_Inheritable_Invariants (Parent_Type) then
3836 Set_Has_Inheritable_Invariants (T);
3837 Set_Has_Invariants (T);
3838 end if;
3839
3840 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3841 -- synchronized formal derived type.
3842
3843 if Ada_Version >= Ada_2005
3844 and then Synchronized_Present (N)
3845 then
3846 Set_Is_Limited_Record (T);
3847
3848 -- Formal derived type case
3849
3850 if Is_Generic_Type (T) then
3851
3852 -- The parent must be a tagged limited type or a synchronized
3853 -- interface.
3854
3855 if (not Is_Tagged_Type (Parent_Type)
3856 or else not Is_Limited_Type (Parent_Type))
3857 and then
3858 (not Is_Interface (Parent_Type)
3859 or else not Is_Synchronized_Interface (Parent_Type))
3860 then
3861 Error_Msg_NE ("parent type of & must be tagged limited " &
3862 "or synchronized", N, T);
3863 end if;
3864
3865 -- The progenitors (if any) must be limited or synchronized
3866 -- interfaces.
3867
3868 if Present (Interfaces (T)) then
3869 declare
3870 Iface : Entity_Id;
3871 Iface_Elmt : Elmt_Id;
3872
3873 begin
3874 Iface_Elmt := First_Elmt (Interfaces (T));
3875 while Present (Iface_Elmt) loop
3876 Iface := Node (Iface_Elmt);
3877
3878 if not Is_Limited_Interface (Iface)
3879 and then not Is_Synchronized_Interface (Iface)
3880 then
3881 Error_Msg_NE ("progenitor & must be limited " &
3882 "or synchronized", N, Iface);
3883 end if;
3884
3885 Next_Elmt (Iface_Elmt);
3886 end loop;
3887 end;
3888 end if;
3889
3890 -- Regular derived extension, the parent must be a limited or
3891 -- synchronized interface.
3892
3893 else
3894 if not Is_Interface (Parent_Type)
3895 or else (not Is_Limited_Interface (Parent_Type)
3896 and then
3897 not Is_Synchronized_Interface (Parent_Type))
3898 then
3899 Error_Msg_NE
3900 ("parent type of & must be limited interface", N, T);
3901 end if;
3902 end if;
3903
3904 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
3905 -- extension with a synchronized parent must be explicitly declared
3906 -- synchronized, because the full view will be a synchronized type.
3907 -- This must be checked before the check for limited types below,
3908 -- to ensure that types declared limited are not allowed to extend
3909 -- synchronized interfaces.
3910
3911 elsif Is_Interface (Parent_Type)
3912 and then Is_Synchronized_Interface (Parent_Type)
3913 and then not Synchronized_Present (N)
3914 then
3915 Error_Msg_NE
3916 ("private extension of& must be explicitly synchronized",
3917 N, Parent_Type);
3918
3919 elsif Limited_Present (N) then
3920 Set_Is_Limited_Record (T);
3921
3922 if not Is_Limited_Type (Parent_Type)
3923 and then
3924 (not Is_Interface (Parent_Type)
3925 or else not Is_Limited_Interface (Parent_Type))
3926 then
3927 Error_Msg_NE ("parent type& of limited extension must be limited",
3928 N, Parent_Type);
3929 end if;
3930 end if;
3931
3932 <<Leave>>
3933 if Has_Aspects (N) then
3934 Analyze_Aspect_Specifications (N, T);
3935 end if;
3936 end Analyze_Private_Extension_Declaration;
3937
3938 ---------------------------------
3939 -- Analyze_Subtype_Declaration --
3940 ---------------------------------
3941
3942 procedure Analyze_Subtype_Declaration
3943 (N : Node_Id;
3944 Skip : Boolean := False)
3945 is
3946 Id : constant Entity_Id := Defining_Identifier (N);
3947 T : Entity_Id;
3948 R_Checks : Check_Result;
3949
3950 begin
3951 Generate_Definition (Id);
3952 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3953 Init_Size_Align (Id);
3954
3955 -- The following guard condition on Enter_Name is to handle cases where
3956 -- the defining identifier has already been entered into the scope but
3957 -- the declaration as a whole needs to be analyzed.
3958
3959 -- This case in particular happens for derived enumeration types. The
3960 -- derived enumeration type is processed as an inserted enumeration type
3961 -- declaration followed by a rewritten subtype declaration. The defining
3962 -- identifier, however, is entered into the name scope very early in the
3963 -- processing of the original type declaration and therefore needs to be
3964 -- avoided here, when the created subtype declaration is analyzed. (See
3965 -- Build_Derived_Types)
3966
3967 -- This also happens when the full view of a private type is derived
3968 -- type with constraints. In this case the entity has been introduced
3969 -- in the private declaration.
3970
3971 if Skip
3972 or else (Present (Etype (Id))
3973 and then (Is_Private_Type (Etype (Id))
3974 or else Is_Task_Type (Etype (Id))
3975 or else Is_Rewrite_Substitution (N)))
3976 then
3977 null;
3978
3979 else
3980 Enter_Name (Id);
3981 end if;
3982
3983 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3984
3985 -- Inherit common attributes
3986
3987 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
3988 Set_Is_Volatile (Id, Is_Volatile (T));
3989 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3990 Set_Is_Atomic (Id, Is_Atomic (T));
3991 Set_Is_Ada_2005_Only (Id, Is_Ada_2005_Only (T));
3992 Set_Is_Ada_2012_Only (Id, Is_Ada_2012_Only (T));
3993 Set_Convention (Id, Convention (T));
3994
3995 -- If ancestor has predicates then so does the subtype, and in addition
3996 -- we must delay the freeze to properly arrange predicate inheritance.
3997
3998 -- The Ancestor_Type test is a big kludge, there seem to be cases in
3999 -- which T = ID, so the above tests and assignments do nothing???
4000
4001 if Has_Predicates (T)
4002 or else (Present (Ancestor_Subtype (T))
4003 and then Has_Predicates (Ancestor_Subtype (T)))
4004 then
4005 Set_Has_Predicates (Id);
4006 Set_Has_Delayed_Freeze (Id);
4007 end if;
4008
4009 -- Subtype of Boolean cannot have a constraint in SPARK or ALFA
4010
4011 if Is_Boolean_Type (T)
4012 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4013 then
4014 Check_Formal_Restriction
4015 ("subtype of Boolean cannot have constraint", N);
4016 end if;
4017
4018 -- String subtype must have a lower bound of 1 in SPARK/ALFA. Note that
4019 -- we do not need to test for the non-static case here, since that was
4020 -- already taken care of in Process_Range_Expr_In_Decl.
4021
4022 if Base_Type (T) = Standard_String
4023 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4024 then
4025 declare
4026 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4027 Drange : Node_Id;
4028 Low : Node_Id;
4029
4030 begin
4031 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
4032 and then List_Length (Constraints (Cstr)) = 1
4033 then
4034 Drange := First (Constraints (Cstr));
4035
4036 if Nkind (Drange) = N_Range then
4037 Low := Low_Bound (Drange);
4038
4039 if Is_OK_Static_Expression (Low)
4040 and then Expr_Value (Low) /= 1
4041 then
4042 Check_Formal_Restriction
4043 ("String subtype must have lower bound of 1", N);
4044 end if;
4045 end if;
4046 end if;
4047 end;
4048 end if;
4049
4050 -- In the case where there is no constraint given in the subtype
4051 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4052 -- semantic attributes must be established here.
4053
4054 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4055 Set_Etype (Id, Base_Type (T));
4056
4057 -- Subtype of unconstrained array without constraint is not allowed
4058 -- in SPARK or ALFA.
4059
4060 if Is_Array_Type (T)
4061 and then not Is_Constrained (T)
4062 then
4063 Check_Formal_Restriction
4064 ("subtype of unconstrained array must have constraint", N);
4065 end if;
4066
4067 case Ekind (T) is
4068 when Array_Kind =>
4069 Set_Ekind (Id, E_Array_Subtype);
4070 Copy_Array_Subtype_Attributes (Id, T);
4071
4072 when Decimal_Fixed_Point_Kind =>
4073 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4074 Set_Digits_Value (Id, Digits_Value (T));
4075 Set_Delta_Value (Id, Delta_Value (T));
4076 Set_Scale_Value (Id, Scale_Value (T));
4077 Set_Small_Value (Id, Small_Value (T));
4078 Set_Scalar_Range (Id, Scalar_Range (T));
4079 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4080 Set_Is_Constrained (Id, Is_Constrained (T));
4081 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4082 Set_RM_Size (Id, RM_Size (T));
4083
4084 when Enumeration_Kind =>
4085 Set_Ekind (Id, E_Enumeration_Subtype);
4086 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4087 Set_Scalar_Range (Id, Scalar_Range (T));
4088 Set_Is_Character_Type (Id, Is_Character_Type (T));
4089 Set_Is_Constrained (Id, Is_Constrained (T));
4090 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4091 Set_RM_Size (Id, RM_Size (T));
4092
4093 when Ordinary_Fixed_Point_Kind =>
4094 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4095 Set_Scalar_Range (Id, Scalar_Range (T));
4096 Set_Small_Value (Id, Small_Value (T));
4097 Set_Delta_Value (Id, Delta_Value (T));
4098 Set_Is_Constrained (Id, Is_Constrained (T));
4099 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4100 Set_RM_Size (Id, RM_Size (T));
4101
4102 when Float_Kind =>
4103 Set_Ekind (Id, E_Floating_Point_Subtype);
4104 Set_Scalar_Range (Id, Scalar_Range (T));
4105 Set_Digits_Value (Id, Digits_Value (T));
4106 Set_Is_Constrained (Id, Is_Constrained (T));
4107
4108 when Signed_Integer_Kind =>
4109 Set_Ekind (Id, E_Signed_Integer_Subtype);
4110 Set_Scalar_Range (Id, Scalar_Range (T));
4111 Set_Is_Constrained (Id, Is_Constrained (T));
4112 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4113 Set_RM_Size (Id, RM_Size (T));
4114
4115 when Modular_Integer_Kind =>
4116 Set_Ekind (Id, E_Modular_Integer_Subtype);
4117 Set_Scalar_Range (Id, Scalar_Range (T));
4118 Set_Is_Constrained (Id, Is_Constrained (T));
4119 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4120 Set_RM_Size (Id, RM_Size (T));
4121
4122 when Class_Wide_Kind =>
4123 Set_Ekind (Id, E_Class_Wide_Subtype);
4124 Set_First_Entity (Id, First_Entity (T));
4125 Set_Last_Entity (Id, Last_Entity (T));
4126 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4127 Set_Cloned_Subtype (Id, T);
4128 Set_Is_Tagged_Type (Id, True);
4129 Set_Has_Unknown_Discriminants
4130 (Id, True);
4131
4132 if Ekind (T) = E_Class_Wide_Subtype then
4133 Set_Equivalent_Type (Id, Equivalent_Type (T));
4134 end if;
4135
4136 when E_Record_Type | E_Record_Subtype =>
4137 Set_Ekind (Id, E_Record_Subtype);
4138
4139 if Ekind (T) = E_Record_Subtype
4140 and then Present (Cloned_Subtype (T))
4141 then
4142 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4143 else
4144 Set_Cloned_Subtype (Id, T);
4145 end if;
4146
4147 Set_First_Entity (Id, First_Entity (T));
4148 Set_Last_Entity (Id, Last_Entity (T));
4149 Set_Has_Discriminants (Id, Has_Discriminants (T));
4150 Set_Is_Constrained (Id, Is_Constrained (T));
4151 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4152 Set_Has_Unknown_Discriminants
4153 (Id, Has_Unknown_Discriminants (T));
4154
4155 if Has_Discriminants (T) then
4156 Set_Discriminant_Constraint
4157 (Id, Discriminant_Constraint (T));
4158 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4159
4160 elsif Has_Unknown_Discriminants (Id) then
4161 Set_Discriminant_Constraint (Id, No_Elist);
4162 end if;
4163
4164 if Is_Tagged_Type (T) then
4165 Set_Is_Tagged_Type (Id);
4166 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4167 Set_Direct_Primitive_Operations
4168 (Id, Direct_Primitive_Operations (T));
4169 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4170
4171 if Is_Interface (T) then
4172 Set_Is_Interface (Id);
4173 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4174 end if;
4175 end if;
4176
4177 when Private_Kind =>
4178 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4179 Set_Has_Discriminants (Id, Has_Discriminants (T));
4180 Set_Is_Constrained (Id, Is_Constrained (T));
4181 Set_First_Entity (Id, First_Entity (T));
4182 Set_Last_Entity (Id, Last_Entity (T));
4183 Set_Private_Dependents (Id, New_Elmt_List);
4184 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4185 Set_Has_Unknown_Discriminants
4186 (Id, Has_Unknown_Discriminants (T));
4187 Set_Known_To_Have_Preelab_Init
4188 (Id, Known_To_Have_Preelab_Init (T));
4189
4190 if Is_Tagged_Type (T) then
4191 Set_Is_Tagged_Type (Id);
4192 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4193 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4194 Set_Direct_Primitive_Operations (Id,
4195 Direct_Primitive_Operations (T));
4196 end if;
4197
4198 -- In general the attributes of the subtype of a private type
4199 -- are the attributes of the partial view of parent. However,
4200 -- the full view may be a discriminated type, and the subtype
4201 -- must share the discriminant constraint to generate correct
4202 -- calls to initialization procedures.
4203
4204 if Has_Discriminants (T) then
4205 Set_Discriminant_Constraint
4206 (Id, Discriminant_Constraint (T));
4207 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4208
4209 elsif Present (Full_View (T))
4210 and then Has_Discriminants (Full_View (T))
4211 then
4212 Set_Discriminant_Constraint
4213 (Id, Discriminant_Constraint (Full_View (T)));
4214 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4215
4216 -- This would seem semantically correct, but apparently
4217 -- confuses the back-end. To be explained and checked with
4218 -- current version ???
4219
4220 -- Set_Has_Discriminants (Id);
4221 end if;
4222
4223 Prepare_Private_Subtype_Completion (Id, N);
4224
4225 when Access_Kind =>
4226 Set_Ekind (Id, E_Access_Subtype);
4227 Set_Is_Constrained (Id, Is_Constrained (T));
4228 Set_Is_Access_Constant
4229 (Id, Is_Access_Constant (T));
4230 Set_Directly_Designated_Type
4231 (Id, Designated_Type (T));
4232 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4233
4234 -- A Pure library_item must not contain the declaration of a
4235 -- named access type, except within a subprogram, generic
4236 -- subprogram, task unit, or protected unit, or if it has
4237 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4238
4239 if Comes_From_Source (Id)
4240 and then In_Pure_Unit
4241 and then not In_Subprogram_Task_Protected_Unit
4242 and then not No_Pool_Assigned (Id)
4243 then
4244 Error_Msg_N
4245 ("named access types not allowed in pure unit", N);
4246 end if;
4247
4248 when Concurrent_Kind =>
4249 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4250 Set_Corresponding_Record_Type (Id,
4251 Corresponding_Record_Type (T));
4252 Set_First_Entity (Id, First_Entity (T));
4253 Set_First_Private_Entity (Id, First_Private_Entity (T));
4254 Set_Has_Discriminants (Id, Has_Discriminants (T));
4255 Set_Is_Constrained (Id, Is_Constrained (T));
4256 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4257 Set_Last_Entity (Id, Last_Entity (T));
4258
4259 if Has_Discriminants (T) then
4260 Set_Discriminant_Constraint (Id,
4261 Discriminant_Constraint (T));
4262 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4263 end if;
4264
4265 when E_Incomplete_Type =>
4266 if Ada_Version >= Ada_2005 then
4267 Set_Ekind (Id, E_Incomplete_Subtype);
4268
4269 -- Ada 2005 (AI-412): Decorate an incomplete subtype
4270 -- of an incomplete type visible through a limited
4271 -- with clause.
4272
4273 if From_With_Type (T)
4274 and then Present (Non_Limited_View (T))
4275 then
4276 Set_From_With_Type (Id);
4277 Set_Non_Limited_View (Id, Non_Limited_View (T));
4278
4279 -- Ada 2005 (AI-412): Add the regular incomplete subtype
4280 -- to the private dependents of the original incomplete
4281 -- type for future transformation.
4282
4283 else
4284 Append_Elmt (Id, Private_Dependents (T));
4285 end if;
4286
4287 -- If the subtype name denotes an incomplete type an error
4288 -- was already reported by Process_Subtype.
4289
4290 else
4291 Set_Etype (Id, Any_Type);
4292 end if;
4293
4294 when others =>
4295 raise Program_Error;
4296 end case;
4297 end if;
4298
4299 if Etype (Id) = Any_Type then
4300 goto Leave;
4301 end if;
4302
4303 -- Some common processing on all types
4304
4305 Set_Size_Info (Id, T);
4306 Set_First_Rep_Item (Id, First_Rep_Item (T));
4307
4308 T := Etype (Id);
4309
4310 Set_Is_Immediately_Visible (Id, True);
4311 Set_Depends_On_Private (Id, Has_Private_Component (T));
4312 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
4313
4314 if Is_Interface (T) then
4315 Set_Is_Interface (Id);
4316 end if;
4317
4318 if Present (Generic_Parent_Type (N))
4319 and then
4320 (Nkind
4321 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
4322 or else Nkind
4323 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
4324 /= N_Formal_Private_Type_Definition)
4325 then
4326 if Is_Tagged_Type (Id) then
4327
4328 -- If this is a generic actual subtype for a synchronized type,
4329 -- the primitive operations are those of the corresponding record
4330 -- for which there is a separate subtype declaration.
4331
4332 if Is_Concurrent_Type (Id) then
4333 null;
4334 elsif Is_Class_Wide_Type (Id) then
4335 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
4336 else
4337 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
4338 end if;
4339
4340 elsif Scope (Etype (Id)) /= Standard_Standard then
4341 Derive_Subprograms (Generic_Parent_Type (N), Id);
4342 end if;
4343 end if;
4344
4345 if Is_Private_Type (T)
4346 and then Present (Full_View (T))
4347 then
4348 Conditional_Delay (Id, Full_View (T));
4349
4350 -- The subtypes of components or subcomponents of protected types
4351 -- do not need freeze nodes, which would otherwise appear in the
4352 -- wrong scope (before the freeze node for the protected type). The
4353 -- proper subtypes are those of the subcomponents of the corresponding
4354 -- record.
4355
4356 elsif Ekind (Scope (Id)) /= E_Protected_Type
4357 and then Present (Scope (Scope (Id))) -- error defense!
4358 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
4359 then
4360 Conditional_Delay (Id, T);
4361 end if;
4362
4363 -- Check that constraint_error is raised for a scalar subtype
4364 -- indication when the lower or upper bound of a non-null range
4365 -- lies outside the range of the type mark.
4366
4367 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4368 if Is_Scalar_Type (Etype (Id))
4369 and then Scalar_Range (Id) /=
4370 Scalar_Range (Etype (Subtype_Mark
4371 (Subtype_Indication (N))))
4372 then
4373 Apply_Range_Check
4374 (Scalar_Range (Id),
4375 Etype (Subtype_Mark (Subtype_Indication (N))));
4376
4377 elsif Is_Array_Type (Etype (Id))
4378 and then Present (First_Index (Id))
4379 then
4380 -- This really should be a subprogram that finds the indications
4381 -- to check???
4382
4383 if ((Nkind (First_Index (Id)) = N_Identifier
4384 and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
4385 or else Nkind (First_Index (Id)) = N_Subtype_Indication)
4386 and then
4387 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
4388 then
4389 declare
4390 Target_Typ : constant Entity_Id :=
4391 Etype
4392 (First_Index (Etype
4393 (Subtype_Mark (Subtype_Indication (N)))));
4394 begin
4395 R_Checks :=
4396 Get_Range_Checks
4397 (Scalar_Range (Etype (First_Index (Id))),
4398 Target_Typ,
4399 Etype (First_Index (Id)),
4400 Defining_Identifier (N));
4401
4402 Insert_Range_Checks
4403 (R_Checks,
4404 N,
4405 Target_Typ,
4406 Sloc (Defining_Identifier (N)));
4407 end;
4408 end if;
4409 end if;
4410 end if;
4411
4412 -- Make sure that generic actual types are properly frozen. The subtype
4413 -- is marked as a generic actual type when the enclosing instance is
4414 -- analyzed, so here we identify the subtype from the tree structure.
4415
4416 if Expander_Active
4417 and then Is_Generic_Actual_Type (Id)
4418 and then In_Instance
4419 and then not Comes_From_Source (N)
4420 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
4421 and then Is_Frozen (T)
4422 then
4423 Freeze_Before (N, Id);
4424 end if;
4425
4426 Set_Optimize_Alignment_Flags (Id);
4427 Check_Eliminated (Id);
4428
4429 <<Leave>>
4430 if Has_Aspects (N) then
4431 Analyze_Aspect_Specifications (N, Id);
4432 end if;
4433 end Analyze_Subtype_Declaration;
4434
4435 --------------------------------
4436 -- Analyze_Subtype_Indication --
4437 --------------------------------
4438
4439 procedure Analyze_Subtype_Indication (N : Node_Id) is
4440 T : constant Entity_Id := Subtype_Mark (N);
4441 R : constant Node_Id := Range_Expression (Constraint (N));
4442
4443 begin
4444 Analyze (T);
4445
4446 if R /= Error then
4447 Analyze (R);
4448 Set_Etype (N, Etype (R));
4449 Resolve (R, Entity (T));
4450 else
4451 Set_Error_Posted (R);
4452 Set_Error_Posted (T);
4453 end if;
4454 end Analyze_Subtype_Indication;
4455
4456 --------------------------
4457 -- Analyze_Variant_Part --
4458 --------------------------
4459
4460 procedure Analyze_Variant_Part (N : Node_Id) is
4461
4462 procedure Non_Static_Choice_Error (Choice : Node_Id);
4463 -- Error routine invoked by the generic instantiation below when the
4464 -- variant part has a non static choice.
4465
4466 procedure Process_Declarations (Variant : Node_Id);
4467 -- Analyzes all the declarations associated with a Variant. Needed by
4468 -- the generic instantiation below.
4469
4470 package Variant_Choices_Processing is new
4471 Generic_Choices_Processing
4472 (Get_Alternatives => Variants,
4473 Get_Choices => Discrete_Choices,
4474 Process_Empty_Choice => No_OP,
4475 Process_Non_Static_Choice => Non_Static_Choice_Error,
4476 Process_Associated_Node => Process_Declarations);
4477 use Variant_Choices_Processing;
4478 -- Instantiation of the generic choice processing package
4479
4480 -----------------------------
4481 -- Non_Static_Choice_Error --
4482 -----------------------------
4483
4484 procedure Non_Static_Choice_Error (Choice : Node_Id) is
4485 begin
4486 Flag_Non_Static_Expr
4487 ("choice given in variant part is not static!", Choice);
4488 end Non_Static_Choice_Error;
4489
4490 --------------------------
4491 -- Process_Declarations --
4492 --------------------------
4493
4494 procedure Process_Declarations (Variant : Node_Id) is
4495 begin
4496 if not Null_Present (Component_List (Variant)) then
4497 Analyze_Declarations (Component_Items (Component_List (Variant)));
4498
4499 if Present (Variant_Part (Component_List (Variant))) then
4500 Analyze (Variant_Part (Component_List (Variant)));
4501 end if;
4502 end if;
4503 end Process_Declarations;
4504
4505 -- Local Variables
4506
4507 Discr_Name : Node_Id;
4508 Discr_Type : Entity_Id;
4509
4510 Dont_Care : Boolean;
4511 Others_Present : Boolean := False;
4512
4513 pragma Warnings (Off, Dont_Care);
4514 pragma Warnings (Off, Others_Present);
4515 -- We don't care about the assigned values of any of these
4516
4517 -- Start of processing for Analyze_Variant_Part
4518
4519 begin
4520 Discr_Name := Name (N);
4521 Analyze (Discr_Name);
4522
4523 -- If Discr_Name bad, get out (prevent cascaded errors)
4524
4525 if Etype (Discr_Name) = Any_Type then
4526 return;
4527 end if;
4528
4529 -- Check invalid discriminant in variant part
4530
4531 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4532 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4533 end if;
4534
4535 Discr_Type := Etype (Entity (Discr_Name));
4536
4537 if not Is_Discrete_Type (Discr_Type) then
4538 Error_Msg_N
4539 ("discriminant in a variant part must be of a discrete type",
4540 Name (N));
4541 return;
4542 end if;
4543
4544 -- Call the instantiated Analyze_Choices which does the rest of the work
4545
4546 Analyze_Choices (N, Discr_Type, Dont_Care, Others_Present);
4547 end Analyze_Variant_Part;
4548
4549 ----------------------------
4550 -- Array_Type_Declaration --
4551 ----------------------------
4552
4553 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4554 Component_Def : constant Node_Id := Component_Definition (Def);
4555 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
4556 Element_Type : Entity_Id;
4557 Implicit_Base : Entity_Id;
4558 Index : Node_Id;
4559 Related_Id : Entity_Id := Empty;
4560 Nb_Index : Nat;
4561 P : constant Node_Id := Parent (Def);
4562 Priv : Entity_Id;
4563
4564 begin
4565 if Nkind (Def) = N_Constrained_Array_Definition then
4566 Index := First (Discrete_Subtype_Definitions (Def));
4567 else
4568 Index := First (Subtype_Marks (Def));
4569 end if;
4570
4571 -- Find proper names for the implicit types which may be public. In case
4572 -- of anonymous arrays we use the name of the first object of that type
4573 -- as prefix.
4574
4575 if No (T) then
4576 Related_Id := Defining_Identifier (P);
4577 else
4578 Related_Id := T;
4579 end if;
4580
4581 Nb_Index := 1;
4582 while Present (Index) loop
4583 if Nkind (Index) /= N_Identifier then
4584 Check_Formal_Restriction ("subtype mark required", Index);
4585 end if;
4586
4587 Analyze (Index);
4588
4589 -- Add a subtype declaration for each index of private array type
4590 -- declaration whose etype is also private. For example:
4591
4592 -- package Pkg is
4593 -- type Index is private;
4594 -- private
4595 -- type Table is array (Index) of ...
4596 -- end;
4597
4598 -- This is currently required by the expander for the internally
4599 -- generated equality subprogram of records with variant parts in
4600 -- which the etype of some component is such private type.
4601
4602 if Ekind (Current_Scope) = E_Package
4603 and then In_Private_Part (Current_Scope)
4604 and then Has_Private_Declaration (Etype (Index))
4605 then
4606 declare
4607 Loc : constant Source_Ptr := Sloc (Def);
4608 New_E : Entity_Id;
4609 Decl : Entity_Id;
4610
4611 begin
4612 New_E := Make_Temporary (Loc, 'T');
4613 Set_Is_Internal (New_E);
4614
4615 Decl :=
4616 Make_Subtype_Declaration (Loc,
4617 Defining_Identifier => New_E,
4618 Subtype_Indication =>
4619 New_Occurrence_Of (Etype (Index), Loc));
4620
4621 Insert_Before (Parent (Def), Decl);
4622 Analyze (Decl);
4623 Set_Etype (Index, New_E);
4624
4625 -- If the index is a range the Entity attribute is not
4626 -- available. Example:
4627
4628 -- package Pkg is
4629 -- type T is private;
4630 -- private
4631 -- type T is new Natural;
4632 -- Table : array (T(1) .. T(10)) of Boolean;
4633 -- end Pkg;
4634
4635 if Nkind (Index) /= N_Range then
4636 Set_Entity (Index, New_E);
4637 end if;
4638 end;
4639 end if;
4640
4641 Make_Index (Index, P, Related_Id, Nb_Index);
4642
4643 -- Check error of subtype with predicate for index type
4644
4645 Bad_Predicated_Subtype_Use
4646 ("subtype& has predicate, not allowed as index subtype",
4647 Index, Etype (Index));
4648
4649 -- Move to next index
4650
4651 Next_Index (Index);
4652 Nb_Index := Nb_Index + 1;
4653 end loop;
4654
4655 -- Process subtype indication if one is present
4656
4657 if Present (Component_Typ) then
4658 if Nkind (Component_Typ) /= N_Identifier then
4659 Check_Formal_Restriction ("subtype mark required", Component_Typ);
4660 end if;
4661
4662 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
4663
4664 -- Ada 2005 (AI-230): Access Definition case
4665
4666 else pragma Assert (Present (Access_Definition (Component_Def)));
4667
4668 -- Indicate that the anonymous access type is created by the
4669 -- array type declaration.
4670
4671 Element_Type := Access_Definition
4672 (Related_Nod => P,
4673 N => Access_Definition (Component_Def));
4674 Set_Is_Local_Anonymous_Access (Element_Type);
4675
4676 -- Propagate the parent. This field is needed if we have to generate
4677 -- the master_id associated with an anonymous access to task type
4678 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4679
4680 Set_Parent (Element_Type, Parent (T));
4681
4682 -- Ada 2005 (AI-230): In case of components that are anonymous access
4683 -- types the level of accessibility depends on the enclosing type
4684 -- declaration
4685
4686 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4687
4688 -- Ada 2005 (AI-254)
4689
4690 declare
4691 CD : constant Node_Id :=
4692 Access_To_Subprogram_Definition
4693 (Access_Definition (Component_Def));
4694 begin
4695 if Present (CD) and then Protected_Present (CD) then
4696 Element_Type :=
4697 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4698 end if;
4699 end;
4700 end if;
4701
4702 -- Constrained array case
4703
4704 if No (T) then
4705 T := Create_Itype (E_Void, P, Related_Id, 'T');
4706 end if;
4707
4708 if Nkind (Def) = N_Constrained_Array_Definition then
4709
4710 -- Establish Implicit_Base as unconstrained base type
4711
4712 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4713
4714 Set_Etype (Implicit_Base, Implicit_Base);
4715 Set_Scope (Implicit_Base, Current_Scope);
4716 Set_Has_Delayed_Freeze (Implicit_Base);
4717
4718 -- The constrained array type is a subtype of the unconstrained one
4719
4720 Set_Ekind (T, E_Array_Subtype);
4721 Init_Size_Align (T);
4722 Set_Etype (T, Implicit_Base);
4723 Set_Scope (T, Current_Scope);
4724 Set_Is_Constrained (T, True);
4725 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4726 Set_Has_Delayed_Freeze (T);
4727
4728 -- Complete setup of implicit base type
4729
4730 Set_First_Index (Implicit_Base, First_Index (T));
4731 Set_Component_Type (Implicit_Base, Element_Type);
4732 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4733 Set_Component_Size (Implicit_Base, Uint_0);
4734 Set_Packed_Array_Type (Implicit_Base, Empty);
4735 Set_Has_Controlled_Component
4736 (Implicit_Base, Has_Controlled_Component
4737 (Element_Type)
4738 or else Is_Controlled
4739 (Element_Type));
4740 Set_Finalize_Storage_Only
4741 (Implicit_Base, Finalize_Storage_Only
4742 (Element_Type));
4743
4744 -- Unconstrained array case
4745
4746 else
4747 Set_Ekind (T, E_Array_Type);
4748 Init_Size_Align (T);
4749 Set_Etype (T, T);
4750 Set_Scope (T, Current_Scope);
4751 Set_Component_Size (T, Uint_0);
4752 Set_Is_Constrained (T, False);
4753 Set_First_Index (T, First (Subtype_Marks (Def)));
4754 Set_Has_Delayed_Freeze (T, True);
4755 Set_Has_Task (T, Has_Task (Element_Type));
4756 Set_Has_Controlled_Component (T, Has_Controlled_Component
4757 (Element_Type)
4758 or else
4759 Is_Controlled (Element_Type));
4760 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4761 (Element_Type));
4762 end if;
4763
4764 -- Common attributes for both cases
4765
4766 Set_Component_Type (Base_Type (T), Element_Type);
4767 Set_Packed_Array_Type (T, Empty);
4768
4769 if Aliased_Present (Component_Definition (Def)) then
4770 Check_Formal_Restriction
4771 ("aliased is not allowed", Component_Definition (Def));
4772 Set_Has_Aliased_Components (Etype (T));
4773 end if;
4774
4775 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4776 -- array type to ensure that objects of this type are initialized.
4777
4778 if Ada_Version >= Ada_2005
4779 and then Can_Never_Be_Null (Element_Type)
4780 then
4781 Set_Can_Never_Be_Null (T);
4782
4783 if Null_Exclusion_Present (Component_Definition (Def))
4784
4785 -- No need to check itypes because in their case this check was
4786 -- done at their point of creation
4787
4788 and then not Is_Itype (Element_Type)
4789 then
4790 Error_Msg_N
4791 ("`NOT NULL` not allowed (null already excluded)",
4792 Subtype_Indication (Component_Definition (Def)));
4793 end if;
4794 end if;
4795
4796 Priv := Private_Component (Element_Type);
4797
4798 if Present (Priv) then
4799
4800 -- Check for circular definitions
4801
4802 if Priv = Any_Type then
4803 Set_Component_Type (Etype (T), Any_Type);
4804
4805 -- There is a gap in the visibility of operations on the composite
4806 -- type only if the component type is defined in a different scope.
4807
4808 elsif Scope (Priv) = Current_Scope then
4809 null;
4810
4811 elsif Is_Limited_Type (Priv) then
4812 Set_Is_Limited_Composite (Etype (T));
4813 Set_Is_Limited_Composite (T);
4814 else
4815 Set_Is_Private_Composite (Etype (T));
4816 Set_Is_Private_Composite (T);
4817 end if;
4818 end if;
4819
4820 -- A syntax error in the declaration itself may lead to an empty index
4821 -- list, in which case do a minimal patch.
4822
4823 if No (First_Index (T)) then
4824 Error_Msg_N ("missing index definition in array type declaration", T);
4825
4826 declare
4827 Indexes : constant List_Id :=
4828 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4829 begin
4830 Set_Discrete_Subtype_Definitions (Def, Indexes);
4831 Set_First_Index (T, First (Indexes));
4832 return;
4833 end;
4834 end if;
4835
4836 -- Create a concatenation operator for the new type. Internal array
4837 -- types created for packed entities do not need such, they are
4838 -- compatible with the user-defined type.
4839
4840 if Number_Dimensions (T) = 1
4841 and then not Is_Packed_Array_Type (T)
4842 then
4843 New_Concatenation_Op (T);
4844 end if;
4845
4846 -- In the case of an unconstrained array the parser has already verified
4847 -- that all the indexes are unconstrained but we still need to make sure
4848 -- that the element type is constrained.
4849
4850 if Is_Indefinite_Subtype (Element_Type) then
4851 Error_Msg_N
4852 ("unconstrained element type in array declaration",
4853 Subtype_Indication (Component_Def));
4854
4855 elsif Is_Abstract_Type (Element_Type) then
4856 Error_Msg_N
4857 ("the type of a component cannot be abstract",
4858 Subtype_Indication (Component_Def));
4859 end if;
4860 end Array_Type_Declaration;
4861
4862 ------------------------------------------------------
4863 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4864 ------------------------------------------------------
4865
4866 function Replace_Anonymous_Access_To_Protected_Subprogram
4867 (N : Node_Id) return Entity_Id
4868 is
4869 Loc : constant Source_Ptr := Sloc (N);
4870
4871 Curr_Scope : constant Scope_Stack_Entry :=
4872 Scope_Stack.Table (Scope_Stack.Last);
4873
4874 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
4875 Acc : Node_Id;
4876 Comp : Node_Id;
4877 Decl : Node_Id;
4878 P : Node_Id;
4879
4880 begin
4881 Set_Is_Internal (Anon);
4882
4883 case Nkind (N) is
4884 when N_Component_Declaration |
4885 N_Unconstrained_Array_Definition |
4886 N_Constrained_Array_Definition =>
4887 Comp := Component_Definition (N);
4888 Acc := Access_Definition (Comp);
4889
4890 when N_Discriminant_Specification =>
4891 Comp := Discriminant_Type (N);
4892 Acc := Comp;
4893
4894 when N_Parameter_Specification =>
4895 Comp := Parameter_Type (N);
4896 Acc := Comp;
4897
4898 when N_Access_Function_Definition =>
4899 Comp := Result_Definition (N);
4900 Acc := Comp;
4901
4902 when N_Object_Declaration =>
4903 Comp := Object_Definition (N);
4904 Acc := Comp;
4905
4906 when N_Function_Specification =>
4907 Comp := Result_Definition (N);
4908 Acc := Comp;
4909
4910 when others =>
4911 raise Program_Error;
4912 end case;
4913
4914 Decl := Make_Full_Type_Declaration (Loc,
4915 Defining_Identifier => Anon,
4916 Type_Definition =>
4917 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
4918
4919 Mark_Rewrite_Insertion (Decl);
4920
4921 -- Insert the new declaration in the nearest enclosing scope. If the
4922 -- node is a body and N is its return type, the declaration belongs in
4923 -- the enclosing scope.
4924
4925 P := Parent (N);
4926
4927 if Nkind (P) = N_Subprogram_Body
4928 and then Nkind (N) = N_Function_Specification
4929 then
4930 P := Parent (P);
4931 end if;
4932
4933 while Present (P) and then not Has_Declarations (P) loop
4934 P := Parent (P);
4935 end loop;
4936
4937 pragma Assert (Present (P));
4938
4939 if Nkind (P) = N_Package_Specification then
4940 Prepend (Decl, Visible_Declarations (P));
4941 else
4942 Prepend (Decl, Declarations (P));
4943 end if;
4944
4945 -- Replace the anonymous type with an occurrence of the new declaration.
4946 -- In all cases the rewritten node does not have the null-exclusion
4947 -- attribute because (if present) it was already inherited by the
4948 -- anonymous entity (Anon). Thus, in case of components we do not
4949 -- inherit this attribute.
4950
4951 if Nkind (N) = N_Parameter_Specification then
4952 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4953 Set_Etype (Defining_Identifier (N), Anon);
4954 Set_Null_Exclusion_Present (N, False);
4955
4956 elsif Nkind (N) = N_Object_Declaration then
4957 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4958 Set_Etype (Defining_Identifier (N), Anon);
4959
4960 elsif Nkind (N) = N_Access_Function_Definition then
4961 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4962
4963 elsif Nkind (N) = N_Function_Specification then
4964 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4965 Set_Etype (Defining_Unit_Name (N), Anon);
4966
4967 else
4968 Rewrite (Comp,
4969 Make_Component_Definition (Loc,
4970 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4971 end if;
4972
4973 Mark_Rewrite_Insertion (Comp);
4974
4975 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
4976 Analyze (Decl);
4977
4978 else
4979 -- Temporarily remove the current scope (record or subprogram) from
4980 -- the stack to add the new declarations to the enclosing scope.
4981
4982 Scope_Stack.Decrement_Last;
4983 Analyze (Decl);
4984 Set_Is_Itype (Anon);
4985 Scope_Stack.Append (Curr_Scope);
4986 end if;
4987
4988 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
4989 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
4990 return Anon;
4991 end Replace_Anonymous_Access_To_Protected_Subprogram;
4992
4993 -------------------------------
4994 -- Build_Derived_Access_Type --
4995 -------------------------------
4996
4997 procedure Build_Derived_Access_Type
4998 (N : Node_Id;
4999 Parent_Type : Entity_Id;
5000 Derived_Type : Entity_Id)
5001 is
5002 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5003
5004 Desig_Type : Entity_Id;
5005 Discr : Entity_Id;
5006 Discr_Con_Elist : Elist_Id;
5007 Discr_Con_El : Elmt_Id;
5008 Subt : Entity_Id;
5009
5010 begin
5011 -- Set the designated type so it is available in case this is an access
5012 -- to a self-referential type, e.g. a standard list type with a next
5013 -- pointer. Will be reset after subtype is built.
5014
5015 Set_Directly_Designated_Type
5016 (Derived_Type, Designated_Type (Parent_Type));
5017
5018 Subt := Process_Subtype (S, N);
5019
5020 if Nkind (S) /= N_Subtype_Indication
5021 and then Subt /= Base_Type (Subt)
5022 then
5023 Set_Ekind (Derived_Type, E_Access_Subtype);
5024 end if;
5025
5026 if Ekind (Derived_Type) = E_Access_Subtype then
5027 declare
5028 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5029 Ibase : constant Entity_Id :=
5030 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5031 Svg_Chars : constant Name_Id := Chars (Ibase);
5032 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5033
5034 begin
5035 Copy_Node (Pbase, Ibase);
5036
5037 Set_Chars (Ibase, Svg_Chars);
5038 Set_Next_Entity (Ibase, Svg_Next_E);
5039 Set_Sloc (Ibase, Sloc (Derived_Type));
5040 Set_Scope (Ibase, Scope (Derived_Type));
5041 Set_Freeze_Node (Ibase, Empty);
5042 Set_Is_Frozen (Ibase, False);
5043 Set_Comes_From_Source (Ibase, False);
5044 Set_Is_First_Subtype (Ibase, False);
5045
5046 Set_Etype (Ibase, Pbase);
5047 Set_Etype (Derived_Type, Ibase);
5048 end;
5049 end if;
5050
5051 Set_Directly_Designated_Type
5052 (Derived_Type, Designated_Type (Subt));
5053
5054 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5055 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5056 Set_Size_Info (Derived_Type, Parent_Type);
5057 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5058 Set_Depends_On_Private (Derived_Type,
5059 Has_Private_Component (Derived_Type));
5060 Conditional_Delay (Derived_Type, Subt);
5061
5062 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5063 -- that it is not redundant.
5064
5065 if Null_Exclusion_Present (Type_Definition (N)) then
5066 Set_Can_Never_Be_Null (Derived_Type);
5067
5068 if Can_Never_Be_Null (Parent_Type)
5069 and then False
5070 then
5071 Error_Msg_NE
5072 ("`NOT NULL` not allowed (& already excludes null)",
5073 N, Parent_Type);
5074 end if;
5075
5076 elsif Can_Never_Be_Null (Parent_Type) then
5077 Set_Can_Never_Be_Null (Derived_Type);
5078 end if;
5079
5080 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5081 -- the root type for this information.
5082
5083 -- Apply range checks to discriminants for derived record case
5084 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
5085
5086 Desig_Type := Designated_Type (Derived_Type);
5087 if Is_Composite_Type (Desig_Type)
5088 and then (not Is_Array_Type (Desig_Type))
5089 and then Has_Discriminants (Desig_Type)
5090 and then Base_Type (Desig_Type) /= Desig_Type
5091 then
5092 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
5093 Discr_Con_El := First_Elmt (Discr_Con_Elist);
5094
5095 Discr := First_Discriminant (Base_Type (Desig_Type));
5096 while Present (Discr_Con_El) loop
5097 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
5098 Next_Elmt (Discr_Con_El);
5099 Next_Discriminant (Discr);
5100 end loop;
5101 end if;
5102 end Build_Derived_Access_Type;
5103
5104 ------------------------------
5105 -- Build_Derived_Array_Type --
5106 ------------------------------
5107
5108 procedure Build_Derived_Array_Type
5109 (N : Node_Id;
5110 Parent_Type : Entity_Id;
5111 Derived_Type : Entity_Id)
5112 is
5113 Loc : constant Source_Ptr := Sloc (N);
5114 Tdef : constant Node_Id := Type_Definition (N);
5115 Indic : constant Node_Id := Subtype_Indication (Tdef);
5116 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5117 Implicit_Base : Entity_Id;
5118 New_Indic : Node_Id;
5119
5120 procedure Make_Implicit_Base;
5121 -- If the parent subtype is constrained, the derived type is a subtype
5122 -- of an implicit base type derived from the parent base.
5123
5124 ------------------------
5125 -- Make_Implicit_Base --
5126 ------------------------
5127
5128 procedure Make_Implicit_Base is
5129 begin
5130 Implicit_Base :=
5131 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5132
5133 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5134 Set_Etype (Implicit_Base, Parent_Base);
5135
5136 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
5137 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
5138
5139 Set_Has_Delayed_Freeze (Implicit_Base, True);
5140 end Make_Implicit_Base;
5141
5142 -- Start of processing for Build_Derived_Array_Type
5143
5144 begin
5145 if not Is_Constrained (Parent_Type) then
5146 if Nkind (Indic) /= N_Subtype_Indication then
5147 Set_Ekind (Derived_Type, E_Array_Type);
5148
5149 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5150 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
5151
5152 Set_Has_Delayed_Freeze (Derived_Type, True);
5153
5154 else
5155 Make_Implicit_Base;
5156 Set_Etype (Derived_Type, Implicit_Base);
5157
5158 New_Indic :=
5159 Make_Subtype_Declaration (Loc,
5160 Defining_Identifier => Derived_Type,
5161 Subtype_Indication =>
5162 Make_Subtype_Indication (Loc,
5163 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
5164 Constraint => Constraint (Indic)));
5165
5166 Rewrite (N, New_Indic);
5167 Analyze (N);
5168 end if;
5169
5170 else
5171 if Nkind (Indic) /= N_Subtype_Indication then
5172 Make_Implicit_Base;
5173
5174 Set_Ekind (Derived_Type, Ekind (Parent_Type));
5175 Set_Etype (Derived_Type, Implicit_Base);
5176 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5177
5178 else
5179 Error_Msg_N ("illegal constraint on constrained type", Indic);
5180 end if;
5181 end if;
5182
5183 -- If parent type is not a derived type itself, and is declared in
5184 -- closed scope (e.g. a subprogram), then we must explicitly introduce
5185 -- the new type's concatenation operator since Derive_Subprograms
5186 -- will not inherit the parent's operator. If the parent type is
5187 -- unconstrained, the operator is of the unconstrained base type.
5188
5189 if Number_Dimensions (Parent_Type) = 1
5190 and then not Is_Limited_Type (Parent_Type)
5191 and then not Is_Derived_Type (Parent_Type)
5192 and then not Is_Package_Or_Generic_Package
5193 (Scope (Base_Type (Parent_Type)))
5194 then
5195 if not Is_Constrained (Parent_Type)
5196 and then Is_Constrained (Derived_Type)
5197 then
5198 New_Concatenation_Op (Implicit_Base);
5199 else
5200 New_Concatenation_Op (Derived_Type);
5201 end if;
5202 end if;
5203 end Build_Derived_Array_Type;
5204
5205 -----------------------------------
5206 -- Build_Derived_Concurrent_Type --
5207 -----------------------------------
5208
5209 procedure Build_Derived_Concurrent_Type
5210 (N : Node_Id;
5211 Parent_Type : Entity_Id;
5212 Derived_Type : Entity_Id)
5213 is
5214 Loc : constant Source_Ptr := Sloc (N);
5215
5216 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
5217 Corr_Decl : Node_Id;
5218 Corr_Decl_Needed : Boolean;
5219 -- If the derived type has fewer discriminants than its parent, the
5220 -- corresponding record is also a derived type, in order to account for
5221 -- the bound discriminants. We create a full type declaration for it in
5222 -- this case.
5223
5224 Constraint_Present : constant Boolean :=
5225 Nkind (Subtype_Indication (Type_Definition (N))) =
5226 N_Subtype_Indication;
5227
5228 D_Constraint : Node_Id;
5229 New_Constraint : Elist_Id;
5230 Old_Disc : Entity_Id;
5231 New_Disc : Entity_Id;
5232 New_N : Node_Id;
5233
5234 begin
5235 Set_Stored_Constraint (Derived_Type, No_Elist);
5236 Corr_Decl_Needed := False;
5237 Old_Disc := Empty;
5238
5239 if Present (Discriminant_Specifications (N))
5240 and then Constraint_Present
5241 then
5242 Old_Disc := First_Discriminant (Parent_Type);
5243 New_Disc := First (Discriminant_Specifications (N));
5244 while Present (New_Disc) and then Present (Old_Disc) loop
5245 Next_Discriminant (Old_Disc);
5246 Next (New_Disc);
5247 end loop;
5248 end if;
5249
5250 if Present (Old_Disc) and then Expander_Active then
5251
5252 -- The new type has fewer discriminants, so we need to create a new
5253 -- corresponding record, which is derived from the corresponding
5254 -- record of the parent, and has a stored constraint that captures
5255 -- the values of the discriminant constraints. The corresponding
5256 -- record is needed only if expander is active and code generation is
5257 -- enabled.
5258
5259 -- The type declaration for the derived corresponding record has the
5260 -- same discriminant part and constraints as the current declaration.
5261 -- Copy the unanalyzed tree to build declaration.
5262
5263 Corr_Decl_Needed := True;
5264 New_N := Copy_Separate_Tree (N);
5265
5266 Corr_Decl :=
5267 Make_Full_Type_Declaration (Loc,
5268 Defining_Identifier => Corr_Record,
5269 Discriminant_Specifications =>
5270 Discriminant_Specifications (New_N),
5271 Type_Definition =>
5272 Make_Derived_Type_Definition (Loc,
5273 Subtype_Indication =>
5274 Make_Subtype_Indication (Loc,
5275 Subtype_Mark =>
5276 New_Occurrence_Of
5277 (Corresponding_Record_Type (Parent_Type), Loc),
5278 Constraint =>
5279 Constraint
5280 (Subtype_Indication (Type_Definition (New_N))))));
5281 end if;
5282
5283 -- Copy Storage_Size and Relative_Deadline variables if task case
5284
5285 if Is_Task_Type (Parent_Type) then
5286 Set_Storage_Size_Variable (Derived_Type,
5287 Storage_Size_Variable (Parent_Type));
5288 Set_Relative_Deadline_Variable (Derived_Type,
5289 Relative_Deadline_Variable (Parent_Type));
5290 end if;
5291
5292 if Present (Discriminant_Specifications (N)) then
5293 Push_Scope (Derived_Type);
5294 Check_Or_Process_Discriminants (N, Derived_Type);
5295
5296 if Constraint_Present then
5297 New_Constraint :=
5298 Expand_To_Stored_Constraint
5299 (Parent_Type,
5300 Build_Discriminant_Constraints
5301 (Parent_Type,
5302 Subtype_Indication (Type_Definition (N)), True));
5303 end if;
5304
5305 End_Scope;
5306
5307 elsif Constraint_Present then
5308
5309 -- Build constrained subtype and derive from it
5310
5311 declare
5312 Loc : constant Source_Ptr := Sloc (N);
5313 Anon : constant Entity_Id :=
5314 Make_Defining_Identifier (Loc,
5315 Chars => New_External_Name (Chars (Derived_Type), 'T'));
5316 Decl : Node_Id;
5317
5318 begin
5319 Decl :=
5320 Make_Subtype_Declaration (Loc,
5321 Defining_Identifier => Anon,
5322 Subtype_Indication =>
5323 Subtype_Indication (Type_Definition (N)));
5324 Insert_Before (N, Decl);
5325 Analyze (Decl);
5326
5327 Rewrite (Subtype_Indication (Type_Definition (N)),
5328 New_Occurrence_Of (Anon, Loc));
5329 Set_Analyzed (Derived_Type, False);
5330 Analyze (N);
5331 return;
5332 end;
5333 end if;
5334
5335 -- By default, operations and private data are inherited from parent.
5336 -- However, in the presence of bound discriminants, a new corresponding
5337 -- record will be created, see below.
5338
5339 Set_Has_Discriminants
5340 (Derived_Type, Has_Discriminants (Parent_Type));
5341 Set_Corresponding_Record_Type
5342 (Derived_Type, Corresponding_Record_Type (Parent_Type));
5343
5344 -- Is_Constrained is set according the parent subtype, but is set to
5345 -- False if the derived type is declared with new discriminants.
5346
5347 Set_Is_Constrained
5348 (Derived_Type,
5349 (Is_Constrained (Parent_Type) or else Constraint_Present)
5350 and then not Present (Discriminant_Specifications (N)));
5351
5352 if Constraint_Present then
5353 if not Has_Discriminants (Parent_Type) then
5354 Error_Msg_N ("untagged parent must have discriminants", N);
5355
5356 elsif Present (Discriminant_Specifications (N)) then
5357
5358 -- Verify that new discriminants are used to constrain old ones
5359
5360 D_Constraint :=
5361 First
5362 (Constraints
5363 (Constraint (Subtype_Indication (Type_Definition (N)))));
5364
5365 Old_Disc := First_Discriminant (Parent_Type);
5366
5367 while Present (D_Constraint) loop
5368 if Nkind (D_Constraint) /= N_Discriminant_Association then
5369
5370 -- Positional constraint. If it is a reference to a new
5371 -- discriminant, it constrains the corresponding old one.
5372
5373 if Nkind (D_Constraint) = N_Identifier then
5374 New_Disc := First_Discriminant (Derived_Type);
5375 while Present (New_Disc) loop
5376 exit when Chars (New_Disc) = Chars (D_Constraint);
5377 Next_Discriminant (New_Disc);
5378 end loop;
5379
5380 if Present (New_Disc) then
5381 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
5382 end if;
5383 end if;
5384
5385 Next_Discriminant (Old_Disc);
5386
5387 -- if this is a named constraint, search by name for the old
5388 -- discriminants constrained by the new one.
5389
5390 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
5391
5392 -- Find new discriminant with that name
5393
5394 New_Disc := First_Discriminant (Derived_Type);
5395 while Present (New_Disc) loop
5396 exit when
5397 Chars (New_Disc) = Chars (Expression (D_Constraint));
5398 Next_Discriminant (New_Disc);
5399 end loop;
5400
5401 if Present (New_Disc) then
5402
5403 -- Verify that new discriminant renames some discriminant
5404 -- of the parent type, and associate the new discriminant
5405 -- with one or more old ones that it renames.
5406
5407 declare
5408 Selector : Node_Id;
5409
5410 begin
5411 Selector := First (Selector_Names (D_Constraint));
5412 while Present (Selector) loop
5413 Old_Disc := First_Discriminant (Parent_Type);
5414 while Present (Old_Disc) loop
5415 exit when Chars (Old_Disc) = Chars (Selector);
5416 Next_Discriminant (Old_Disc);
5417 end loop;
5418
5419 if Present (Old_Disc) then
5420 Set_Corresponding_Discriminant
5421 (New_Disc, Old_Disc);
5422 end if;
5423
5424 Next (Selector);
5425 end loop;
5426 end;
5427 end if;
5428 end if;
5429
5430 Next (D_Constraint);
5431 end loop;
5432
5433 New_Disc := First_Discriminant (Derived_Type);
5434 while Present (New_Disc) loop
5435 if No (Corresponding_Discriminant (New_Disc)) then
5436 Error_Msg_NE
5437 ("new discriminant& must constrain old one", N, New_Disc);
5438
5439 elsif not
5440 Subtypes_Statically_Compatible
5441 (Etype (New_Disc),
5442 Etype (Corresponding_Discriminant (New_Disc)))
5443 then
5444 Error_Msg_NE
5445 ("& not statically compatible with parent discriminant",
5446 N, New_Disc);
5447 end if;
5448
5449 Next_Discriminant (New_Disc);
5450 end loop;
5451 end if;
5452
5453 elsif Present (Discriminant_Specifications (N)) then
5454 Error_Msg_N
5455 ("missing discriminant constraint in untagged derivation", N);
5456 end if;
5457
5458 -- The entity chain of the derived type includes the new discriminants
5459 -- but shares operations with the parent.
5460
5461 if Present (Discriminant_Specifications (N)) then
5462 Old_Disc := First_Discriminant (Parent_Type);
5463 while Present (Old_Disc) loop
5464 if No (Next_Entity (Old_Disc))
5465 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
5466 then
5467 Set_Next_Entity
5468 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
5469 exit;
5470 end if;
5471
5472 Next_Discriminant (Old_Disc);
5473 end loop;
5474
5475 else
5476 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
5477 if Has_Discriminants (Parent_Type) then
5478 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5479 Set_Discriminant_Constraint (
5480 Derived_Type, Discriminant_Constraint (Parent_Type));
5481 end if;
5482 end if;
5483
5484 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
5485
5486 Set_Has_Completion (Derived_Type);
5487
5488 if Corr_Decl_Needed then
5489 Set_Stored_Constraint (Derived_Type, New_Constraint);
5490 Insert_After (N, Corr_Decl);
5491 Analyze (Corr_Decl);
5492 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
5493 end if;
5494 end Build_Derived_Concurrent_Type;
5495
5496 ------------------------------------
5497 -- Build_Derived_Enumeration_Type --
5498 ------------------------------------
5499
5500 procedure Build_Derived_Enumeration_Type
5501 (N : Node_Id;
5502 Parent_Type : Entity_Id;
5503 Derived_Type : Entity_Id)
5504 is
5505 Loc : constant Source_Ptr := Sloc (N);
5506 Def : constant Node_Id := Type_Definition (N);
5507 Indic : constant Node_Id := Subtype_Indication (Def);
5508 Implicit_Base : Entity_Id;
5509 Literal : Entity_Id;
5510 New_Lit : Entity_Id;
5511 Literals_List : List_Id;
5512 Type_Decl : Node_Id;
5513 Hi, Lo : Node_Id;
5514 Rang_Expr : Node_Id;
5515
5516 begin
5517 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
5518 -- not have explicit literals lists we need to process types derived
5519 -- from them specially. This is handled by Derived_Standard_Character.
5520 -- If the parent type is a generic type, there are no literals either,
5521 -- and we construct the same skeletal representation as for the generic
5522 -- parent type.
5523
5524 if Is_Standard_Character_Type (Parent_Type) then
5525 Derived_Standard_Character (N, Parent_Type, Derived_Type);
5526
5527 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
5528 declare
5529 Lo : Node_Id;
5530 Hi : Node_Id;
5531
5532 begin
5533 if Nkind (Indic) /= N_Subtype_Indication then
5534 Lo :=
5535 Make_Attribute_Reference (Loc,
5536 Attribute_Name => Name_First,
5537 Prefix => New_Reference_To (Derived_Type, Loc));
5538 Set_Etype (Lo, Derived_Type);
5539
5540 Hi :=
5541 Make_Attribute_Reference (Loc,
5542 Attribute_Name => Name_Last,
5543 Prefix => New_Reference_To (Derived_Type, Loc));
5544 Set_Etype (Hi, Derived_Type);
5545
5546 Set_Scalar_Range (Derived_Type,
5547 Make_Range (Loc,
5548 Low_Bound => Lo,
5549 High_Bound => Hi));
5550 else
5551
5552 -- Analyze subtype indication and verify compatibility
5553 -- with parent type.
5554
5555 if Base_Type (Process_Subtype (Indic, N)) /=
5556 Base_Type (Parent_Type)
5557 then
5558 Error_Msg_N
5559 ("illegal constraint for formal discrete type", N);
5560 end if;
5561 end if;
5562 end;
5563
5564 else
5565 -- If a constraint is present, analyze the bounds to catch
5566 -- premature usage of the derived literals.
5567
5568 if Nkind (Indic) = N_Subtype_Indication
5569 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5570 then
5571 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
5572 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5573 end if;
5574
5575 -- Introduce an implicit base type for the derived type even if there
5576 -- is no constraint attached to it, since this seems closer to the
5577 -- Ada semantics. Build a full type declaration tree for the derived
5578 -- type using the implicit base type as the defining identifier. The
5579 -- build a subtype declaration tree which applies the constraint (if
5580 -- any) have it replace the derived type declaration.
5581
5582 Literal := First_Literal (Parent_Type);
5583 Literals_List := New_List;
5584 while Present (Literal)
5585 and then Ekind (Literal) = E_Enumeration_Literal
5586 loop
5587 -- Literals of the derived type have the same representation as
5588 -- those of the parent type, but this representation can be
5589 -- overridden by an explicit representation clause. Indicate
5590 -- that there is no explicit representation given yet. These
5591 -- derived literals are implicit operations of the new type,
5592 -- and can be overridden by explicit ones.
5593
5594 if Nkind (Literal) = N_Defining_Character_Literal then
5595 New_Lit :=
5596 Make_Defining_Character_Literal (Loc, Chars (Literal));
5597 else
5598 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5599 end if;
5600
5601 Set_Ekind (New_Lit, E_Enumeration_Literal);
5602 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
5603 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
5604 Set_Enumeration_Rep_Expr (New_Lit, Empty);
5605 Set_Alias (New_Lit, Literal);
5606 Set_Is_Known_Valid (New_Lit, True);
5607
5608 Append (New_Lit, Literals_List);
5609 Next_Literal (Literal);
5610 end loop;
5611
5612 Implicit_Base :=
5613 Make_Defining_Identifier (Sloc (Derived_Type),
5614 Chars => New_External_Name (Chars (Derived_Type), 'B'));
5615
5616 -- Indicate the proper nature of the derived type. This must be done
5617 -- before analysis of the literals, to recognize cases when a literal
5618 -- may be hidden by a previous explicit function definition (cf.
5619 -- c83031a).
5620
5621 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5622 Set_Etype (Derived_Type, Implicit_Base);
5623
5624 Type_Decl :=
5625 Make_Full_Type_Declaration (Loc,
5626 Defining_Identifier => Implicit_Base,
5627 Discriminant_Specifications => No_List,
5628 Type_Definition =>
5629 Make_Enumeration_Type_Definition (Loc, Literals_List));
5630
5631 Mark_Rewrite_Insertion (Type_Decl);
5632 Insert_Before (N, Type_Decl);
5633 Analyze (Type_Decl);
5634
5635 -- After the implicit base is analyzed its Etype needs to be changed
5636 -- to reflect the fact that it is derived from the parent type which
5637 -- was ignored during analysis. We also set the size at this point.
5638
5639 Set_Etype (Implicit_Base, Parent_Type);
5640
5641 Set_Size_Info (Implicit_Base, Parent_Type);
5642 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5643 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5644
5645 -- Copy other flags from parent type
5646
5647 Set_Has_Non_Standard_Rep
5648 (Implicit_Base, Has_Non_Standard_Rep
5649 (Parent_Type));
5650 Set_Has_Pragma_Ordered
5651 (Implicit_Base, Has_Pragma_Ordered
5652 (Parent_Type));
5653 Set_Has_Delayed_Freeze (Implicit_Base);
5654
5655 -- Process the subtype indication including a validation check on the
5656 -- constraint, if any. If a constraint is given, its bounds must be
5657 -- implicitly converted to the new type.
5658
5659 if Nkind (Indic) = N_Subtype_Indication then
5660 declare
5661 R : constant Node_Id :=
5662 Range_Expression (Constraint (Indic));
5663
5664 begin
5665 if Nkind (R) = N_Range then
5666 Hi := Build_Scalar_Bound
5667 (High_Bound (R), Parent_Type, Implicit_Base);
5668 Lo := Build_Scalar_Bound
5669 (Low_Bound (R), Parent_Type, Implicit_Base);
5670
5671 else
5672 -- Constraint is a Range attribute. Replace with explicit
5673 -- mention of the bounds of the prefix, which must be a
5674 -- subtype.
5675
5676 Analyze (Prefix (R));
5677 Hi :=
5678 Convert_To (Implicit_Base,
5679 Make_Attribute_Reference (Loc,
5680 Attribute_Name => Name_Last,
5681 Prefix =>
5682 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5683
5684 Lo :=
5685 Convert_To (Implicit_Base,
5686 Make_Attribute_Reference (Loc,
5687 Attribute_Name => Name_First,
5688 Prefix =>
5689 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5690 end if;
5691 end;
5692
5693 else
5694 Hi :=
5695 Build_Scalar_Bound
5696 (Type_High_Bound (Parent_Type),
5697 Parent_Type, Implicit_Base);
5698 Lo :=
5699 Build_Scalar_Bound
5700 (Type_Low_Bound (Parent_Type),
5701 Parent_Type, Implicit_Base);
5702 end if;
5703
5704 Rang_Expr :=
5705 Make_Range (Loc,
5706 Low_Bound => Lo,
5707 High_Bound => Hi);
5708
5709 -- If we constructed a default range for the case where no range
5710 -- was given, then the expressions in the range must not freeze
5711 -- since they do not correspond to expressions in the source.
5712
5713 if Nkind (Indic) /= N_Subtype_Indication then
5714 Set_Must_Not_Freeze (Lo);
5715 Set_Must_Not_Freeze (Hi);
5716 Set_Must_Not_Freeze (Rang_Expr);
5717 end if;
5718
5719 Rewrite (N,
5720 Make_Subtype_Declaration (Loc,
5721 Defining_Identifier => Derived_Type,
5722 Subtype_Indication =>
5723 Make_Subtype_Indication (Loc,
5724 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5725 Constraint =>
5726 Make_Range_Constraint (Loc,
5727 Range_Expression => Rang_Expr))));
5728
5729 Analyze (N);
5730
5731 -- If pragma Discard_Names applies on the first subtype of the parent
5732 -- type, then it must be applied on this subtype as well.
5733
5734 if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5735 Set_Discard_Names (Derived_Type);
5736 end if;
5737
5738 -- Apply a range check. Since this range expression doesn't have an
5739 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5740 -- this right???
5741
5742 if Nkind (Indic) = N_Subtype_Indication then
5743 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5744 Parent_Type,
5745 Source_Typ => Entity (Subtype_Mark (Indic)));
5746 end if;
5747 end if;
5748 end Build_Derived_Enumeration_Type;
5749
5750 --------------------------------
5751 -- Build_Derived_Numeric_Type --
5752 --------------------------------
5753
5754 procedure Build_Derived_Numeric_Type
5755 (N : Node_Id;
5756 Parent_Type : Entity_Id;
5757 Derived_Type : Entity_Id)
5758 is
5759 Loc : constant Source_Ptr := Sloc (N);
5760 Tdef : constant Node_Id := Type_Definition (N);
5761 Indic : constant Node_Id := Subtype_Indication (Tdef);
5762 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5763 No_Constraint : constant Boolean := Nkind (Indic) /=
5764 N_Subtype_Indication;
5765 Implicit_Base : Entity_Id;
5766
5767 Lo : Node_Id;
5768 Hi : Node_Id;
5769
5770 begin
5771 -- Process the subtype indication including a validation check on
5772 -- the constraint if any.
5773
5774 Discard_Node (Process_Subtype (Indic, N));
5775
5776 -- Introduce an implicit base type for the derived type even if there
5777 -- is no constraint attached to it, since this seems closer to the Ada
5778 -- semantics.
5779
5780 Implicit_Base :=
5781 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5782
5783 Set_Etype (Implicit_Base, Parent_Base);
5784 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5785 Set_Size_Info (Implicit_Base, Parent_Base);
5786 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5787 Set_Parent (Implicit_Base, Parent (Derived_Type));
5788 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
5789
5790 -- Set RM Size for discrete type or decimal fixed-point type
5791 -- Ordinary fixed-point is excluded, why???
5792
5793 if Is_Discrete_Type (Parent_Base)
5794 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5795 then
5796 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5797 end if;
5798
5799 Set_Has_Delayed_Freeze (Implicit_Base);
5800
5801 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5802 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5803
5804 Set_Scalar_Range (Implicit_Base,
5805 Make_Range (Loc,
5806 Low_Bound => Lo,
5807 High_Bound => Hi));
5808
5809 if Has_Infinities (Parent_Base) then
5810 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5811 end if;
5812
5813 -- The Derived_Type, which is the entity of the declaration, is a
5814 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5815 -- absence of an explicit constraint.
5816
5817 Set_Etype (Derived_Type, Implicit_Base);
5818
5819 -- If we did not have a constraint, then the Ekind is set from the
5820 -- parent type (otherwise Process_Subtype has set the bounds)
5821
5822 if No_Constraint then
5823 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5824 end if;
5825
5826 -- If we did not have a range constraint, then set the range from the
5827 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
5828
5829 if No_Constraint
5830 or else not Has_Range_Constraint (Indic)
5831 then
5832 Set_Scalar_Range (Derived_Type,
5833 Make_Range (Loc,
5834 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
5835 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5836 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5837
5838 if Has_Infinities (Parent_Type) then
5839 Set_Includes_Infinities (Scalar_Range (Derived_Type));
5840 end if;
5841
5842 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
5843 end if;
5844
5845 Set_Is_Descendent_Of_Address (Derived_Type,
5846 Is_Descendent_Of_Address (Parent_Type));
5847 Set_Is_Descendent_Of_Address (Implicit_Base,
5848 Is_Descendent_Of_Address (Parent_Type));
5849
5850 -- Set remaining type-specific fields, depending on numeric type
5851
5852 if Is_Modular_Integer_Type (Parent_Type) then
5853 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5854
5855 Set_Non_Binary_Modulus
5856 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5857
5858 Set_Is_Known_Valid
5859 (Implicit_Base, Is_Known_Valid (Parent_Base));
5860
5861 elsif Is_Floating_Point_Type (Parent_Type) then
5862
5863 -- Digits of base type is always copied from the digits value of
5864 -- the parent base type, but the digits of the derived type will
5865 -- already have been set if there was a constraint present.
5866
5867 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5868 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
5869
5870 if No_Constraint then
5871 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5872 end if;
5873
5874 elsif Is_Fixed_Point_Type (Parent_Type) then
5875
5876 -- Small of base type and derived type are always copied from the
5877 -- parent base type, since smalls never change. The delta of the
5878 -- base type is also copied from the parent base type. However the
5879 -- delta of the derived type will have been set already if a
5880 -- constraint was present.
5881
5882 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
5883 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5884 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5885
5886 if No_Constraint then
5887 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
5888 end if;
5889
5890 -- The scale and machine radix in the decimal case are always
5891 -- copied from the parent base type.
5892
5893 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5894 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
5895 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5896
5897 Set_Machine_Radix_10
5898 (Derived_Type, Machine_Radix_10 (Parent_Base));
5899 Set_Machine_Radix_10
5900 (Implicit_Base, Machine_Radix_10 (Parent_Base));
5901
5902 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5903
5904 if No_Constraint then
5905 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5906
5907 else
5908 -- the analysis of the subtype_indication sets the
5909 -- digits value of the derived type.
5910
5911 null;
5912 end if;
5913 end if;
5914 end if;
5915
5916 -- The type of the bounds is that of the parent type, and they
5917 -- must be converted to the derived type.
5918
5919 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5920
5921 -- The implicit_base should be frozen when the derived type is frozen,
5922 -- but note that it is used in the conversions of the bounds. For fixed
5923 -- types we delay the determination of the bounds until the proper
5924 -- freezing point. For other numeric types this is rejected by GCC, for
5925 -- reasons that are currently unclear (???), so we choose to freeze the
5926 -- implicit base now. In the case of integers and floating point types
5927 -- this is harmless because subsequent representation clauses cannot
5928 -- affect anything, but it is still baffling that we cannot use the
5929 -- same mechanism for all derived numeric types.
5930
5931 -- There is a further complication: actually *some* representation
5932 -- clauses can affect the implicit base type. Namely, attribute
5933 -- definition clauses for stream-oriented attributes need to set the
5934 -- corresponding TSS entries on the base type, and this normally cannot
5935 -- be done after the base type is frozen, so the circuitry in
5936 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5937 -- not use Set_TSS in this case.
5938
5939 if Is_Fixed_Point_Type (Parent_Type) then
5940 Conditional_Delay (Implicit_Base, Parent_Type);
5941 else
5942 Freeze_Before (N, Implicit_Base);
5943 end if;
5944 end Build_Derived_Numeric_Type;
5945
5946 --------------------------------
5947 -- Build_Derived_Private_Type --
5948 --------------------------------
5949
5950 procedure Build_Derived_Private_Type
5951 (N : Node_Id;
5952 Parent_Type : Entity_Id;
5953 Derived_Type : Entity_Id;
5954 Is_Completion : Boolean;
5955 Derive_Subps : Boolean := True)
5956 is
5957 Loc : constant Source_Ptr := Sloc (N);
5958 Der_Base : Entity_Id;
5959 Discr : Entity_Id;
5960 Full_Decl : Node_Id := Empty;
5961 Full_Der : Entity_Id;
5962 Full_P : Entity_Id;
5963 Last_Discr : Entity_Id;
5964 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
5965 Swapped : Boolean := False;
5966
5967 procedure Copy_And_Build;
5968 -- Copy derived type declaration, replace parent with its full view,
5969 -- and analyze new declaration.
5970
5971 --------------------
5972 -- Copy_And_Build --
5973 --------------------
5974
5975 procedure Copy_And_Build is
5976 Full_N : Node_Id;
5977
5978 begin
5979 if Ekind (Parent_Type) in Record_Kind
5980 or else
5981 (Ekind (Parent_Type) in Enumeration_Kind
5982 and then not Is_Standard_Character_Type (Parent_Type)
5983 and then not Is_Generic_Type (Root_Type (Parent_Type)))
5984 then
5985 Full_N := New_Copy_Tree (N);
5986 Insert_After (N, Full_N);
5987 Build_Derived_Type (
5988 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5989
5990 else
5991 Build_Derived_Type (
5992 N, Parent_Type, Full_Der, True, Derive_Subps => False);
5993 end if;
5994 end Copy_And_Build;
5995
5996 -- Start of processing for Build_Derived_Private_Type
5997
5998 begin
5999 if Is_Tagged_Type (Parent_Type) then
6000 Full_P := Full_View (Parent_Type);
6001
6002 -- A type extension of a type with unknown discriminants is an
6003 -- indefinite type that the back-end cannot handle directly.
6004 -- We treat it as a private type, and build a completion that is
6005 -- derived from the full view of the parent, and hopefully has
6006 -- known discriminants.
6007
6008 -- If the full view of the parent type has an underlying record view,
6009 -- use it to generate the underlying record view of this derived type
6010 -- (required for chains of derivations with unknown discriminants).
6011
6012 -- Minor optimization: we avoid the generation of useless underlying
6013 -- record view entities if the private type declaration has unknown
6014 -- discriminants but its corresponding full view has no
6015 -- discriminants.
6016
6017 if Has_Unknown_Discriminants (Parent_Type)
6018 and then Present (Full_P)
6019 and then (Has_Discriminants (Full_P)
6020 or else Present (Underlying_Record_View (Full_P)))
6021 and then not In_Open_Scopes (Par_Scope)
6022 and then Expander_Active
6023 then
6024 declare
6025 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
6026 New_Ext : constant Node_Id :=
6027 Copy_Separate_Tree
6028 (Record_Extension_Part (Type_Definition (N)));
6029 Decl : Node_Id;
6030
6031 begin
6032 Build_Derived_Record_Type
6033 (N, Parent_Type, Derived_Type, Derive_Subps);
6034
6035 -- Build anonymous completion, as a derivation from the full
6036 -- view of the parent. This is not a completion in the usual
6037 -- sense, because the current type is not private.
6038
6039 Decl :=
6040 Make_Full_Type_Declaration (Loc,
6041 Defining_Identifier => Full_Der,
6042 Type_Definition =>
6043 Make_Derived_Type_Definition (Loc,
6044 Subtype_Indication =>
6045 New_Copy_Tree
6046 (Subtype_Indication (Type_Definition (N))),
6047 Record_Extension_Part => New_Ext));
6048
6049 -- If the parent type has an underlying record view, use it
6050 -- here to build the new underlying record view.
6051
6052 if Present (Underlying_Record_View (Full_P)) then
6053 pragma Assert
6054 (Nkind (Subtype_Indication (Type_Definition (Decl)))
6055 = N_Identifier);
6056 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
6057 Underlying_Record_View (Full_P));
6058 end if;
6059
6060 Install_Private_Declarations (Par_Scope);
6061 Install_Visible_Declarations (Par_Scope);
6062 Insert_Before (N, Decl);
6063
6064 -- Mark entity as an underlying record view before analysis,
6065 -- to avoid generating the list of its primitive operations
6066 -- (which is not really required for this entity) and thus
6067 -- prevent spurious errors associated with missing overriding
6068 -- of abstract primitives (overridden only for Derived_Type).
6069
6070 Set_Ekind (Full_Der, E_Record_Type);
6071 Set_Is_Underlying_Record_View (Full_Der);
6072
6073 Analyze (Decl);
6074
6075 pragma Assert (Has_Discriminants (Full_Der)
6076 and then not Has_Unknown_Discriminants (Full_Der));
6077
6078 Uninstall_Declarations (Par_Scope);
6079
6080 -- Freeze the underlying record view, to prevent generation of
6081 -- useless dispatching information, which is simply shared with
6082 -- the real derived type.
6083
6084 Set_Is_Frozen (Full_Der);
6085
6086 -- Set up links between real entity and underlying record view
6087
6088 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
6089 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
6090 end;
6091
6092 -- If discriminants are known, build derived record
6093
6094 else
6095 Build_Derived_Record_Type
6096 (N, Parent_Type, Derived_Type, Derive_Subps);
6097 end if;
6098
6099 return;
6100
6101 elsif Has_Discriminants (Parent_Type) then
6102 if Present (Full_View (Parent_Type)) then
6103 if not Is_Completion then
6104
6105 -- Copy declaration for subsequent analysis, to provide a
6106 -- completion for what is a private declaration. Indicate that
6107 -- the full type is internally generated.
6108
6109 Full_Decl := New_Copy_Tree (N);
6110 Full_Der := New_Copy (Derived_Type);
6111 Set_Comes_From_Source (Full_Decl, False);
6112 Set_Comes_From_Source (Full_Der, False);
6113 Set_Parent (Full_Der, Full_Decl);
6114
6115 Insert_After (N, Full_Decl);
6116
6117 else
6118 -- If this is a completion, the full view being built is itself
6119 -- private. We build a subtype of the parent with the same
6120 -- constraints as this full view, to convey to the back end the
6121 -- constrained components and the size of this subtype. If the
6122 -- parent is constrained, its full view can serve as the
6123 -- underlying full view of the derived type.
6124
6125 if No (Discriminant_Specifications (N)) then
6126 if Nkind (Subtype_Indication (Type_Definition (N))) =
6127 N_Subtype_Indication
6128 then
6129 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
6130
6131 elsif Is_Constrained (Full_View (Parent_Type)) then
6132 Set_Underlying_Full_View
6133 (Derived_Type, Full_View (Parent_Type));
6134 end if;
6135
6136 else
6137 -- If there are new discriminants, the parent subtype is
6138 -- constrained by them, but it is not clear how to build
6139 -- the Underlying_Full_View in this case???
6140
6141 null;
6142 end if;
6143 end if;
6144 end if;
6145
6146 -- Build partial view of derived type from partial view of parent
6147
6148 Build_Derived_Record_Type
6149 (N, Parent_Type, Derived_Type, Derive_Subps);
6150
6151 if Present (Full_View (Parent_Type)) and then not Is_Completion then
6152 if not In_Open_Scopes (Par_Scope)
6153 or else not In_Same_Source_Unit (N, Parent_Type)
6154 then
6155 -- Swap partial and full views temporarily
6156
6157 Install_Private_Declarations (Par_Scope);
6158 Install_Visible_Declarations (Par_Scope);
6159 Swapped := True;
6160 end if;
6161
6162 -- Build full view of derived type from full view of parent which
6163 -- is now installed. Subprograms have been derived on the partial
6164 -- view, the completion does not derive them anew.
6165
6166 if not Is_Tagged_Type (Parent_Type) then
6167
6168 -- If the parent is itself derived from another private type,
6169 -- installing the private declarations has not affected its
6170 -- privacy status, so use its own full view explicitly.
6171
6172 if Is_Private_Type (Parent_Type) then
6173 Build_Derived_Record_Type
6174 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
6175 else
6176 Build_Derived_Record_Type
6177 (Full_Decl, Parent_Type, Full_Der, False);
6178 end if;
6179
6180 else
6181 -- If full view of parent is tagged, the completion inherits
6182 -- the proper primitive operations.
6183
6184 Set_Defining_Identifier (Full_Decl, Full_Der);
6185 Build_Derived_Record_Type
6186 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
6187 end if;
6188
6189 -- The full declaration has been introduced into the tree and
6190 -- processed in the step above. It should not be analyzed again
6191 -- (when encountered later in the current list of declarations)
6192 -- to prevent spurious name conflicts. The full entity remains
6193 -- invisible.
6194
6195 Set_Analyzed (Full_Decl);
6196
6197 if Swapped then
6198 Uninstall_Declarations (Par_Scope);
6199
6200 if In_Open_Scopes (Par_Scope) then
6201 Install_Visible_Declarations (Par_Scope);
6202 end if;
6203 end if;
6204
6205 Der_Base := Base_Type (Derived_Type);
6206 Set_Full_View (Derived_Type, Full_Der);
6207 Set_Full_View (Der_Base, Base_Type (Full_Der));
6208
6209 -- Copy the discriminant list from full view to the partial views
6210 -- (base type and its subtype). Gigi requires that the partial and
6211 -- full views have the same discriminants.
6212
6213 -- Note that since the partial view is pointing to discriminants
6214 -- in the full view, their scope will be that of the full view.
6215 -- This might cause some front end problems and need adjustment???
6216
6217 Discr := First_Discriminant (Base_Type (Full_Der));
6218 Set_First_Entity (Der_Base, Discr);
6219
6220 loop
6221 Last_Discr := Discr;
6222 Next_Discriminant (Discr);
6223 exit when No (Discr);
6224 end loop;
6225
6226 Set_Last_Entity (Der_Base, Last_Discr);
6227
6228 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
6229 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
6230 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
6231
6232 else
6233 -- If this is a completion, the derived type stays private and
6234 -- there is no need to create a further full view, except in the
6235 -- unusual case when the derivation is nested within a child unit,
6236 -- see below.
6237
6238 null;
6239 end if;
6240
6241 elsif Present (Full_View (Parent_Type))
6242 and then Has_Discriminants (Full_View (Parent_Type))
6243 then
6244 if Has_Unknown_Discriminants (Parent_Type)
6245 and then Nkind (Subtype_Indication (Type_Definition (N))) =
6246 N_Subtype_Indication
6247 then
6248 Error_Msg_N
6249 ("cannot constrain type with unknown discriminants",
6250 Subtype_Indication (Type_Definition (N)));
6251 return;
6252 end if;
6253
6254 -- If full view of parent is a record type, build full view as a
6255 -- derivation from the parent's full view. Partial view remains
6256 -- private. For code generation and linking, the full view must have
6257 -- the same public status as the partial one. This full view is only
6258 -- needed if the parent type is in an enclosing scope, so that the
6259 -- full view may actually become visible, e.g. in a child unit. This
6260 -- is both more efficient, and avoids order of freezing problems with
6261 -- the added entities.
6262
6263 if not Is_Private_Type (Full_View (Parent_Type))
6264 and then (In_Open_Scopes (Scope (Parent_Type)))
6265 then
6266 Full_Der :=
6267 Make_Defining_Identifier
6268 (Sloc (Derived_Type), Chars (Derived_Type));
6269 Set_Is_Itype (Full_Der);
6270 Set_Has_Private_Declaration (Full_Der);
6271 Set_Has_Private_Declaration (Derived_Type);
6272 Set_Associated_Node_For_Itype (Full_Der, N);
6273 Set_Parent (Full_Der, Parent (Derived_Type));
6274 Set_Full_View (Derived_Type, Full_Der);
6275 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
6276 Full_P := Full_View (Parent_Type);
6277 Exchange_Declarations (Parent_Type);
6278 Copy_And_Build;
6279 Exchange_Declarations (Full_P);
6280
6281 else
6282 Build_Derived_Record_Type
6283 (N, Full_View (Parent_Type), Derived_Type,
6284 Derive_Subps => False);
6285 end if;
6286
6287 -- In any case, the primitive operations are inherited from the
6288 -- parent type, not from the internal full view.
6289
6290 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
6291
6292 if Derive_Subps then
6293 Derive_Subprograms (Parent_Type, Derived_Type);
6294 end if;
6295
6296 else
6297 -- Untagged type, No discriminants on either view
6298
6299 if Nkind (Subtype_Indication (Type_Definition (N))) =
6300 N_Subtype_Indication
6301 then
6302 Error_Msg_N
6303 ("illegal constraint on type without discriminants", N);
6304 end if;
6305
6306 if Present (Discriminant_Specifications (N))
6307 and then Present (Full_View (Parent_Type))
6308 and then not Is_Tagged_Type (Full_View (Parent_Type))
6309 then
6310 Error_Msg_N ("cannot add discriminants to untagged type", N);
6311 end if;
6312
6313 Set_Stored_Constraint (Derived_Type, No_Elist);
6314 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6315 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
6316 Set_Has_Controlled_Component
6317 (Derived_Type, Has_Controlled_Component
6318 (Parent_Type));
6319
6320 -- Direct controlled types do not inherit Finalize_Storage_Only flag
6321
6322 if not Is_Controlled (Parent_Type) then
6323 Set_Finalize_Storage_Only
6324 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
6325 end if;
6326
6327 -- Construct the implicit full view by deriving from full view of the
6328 -- parent type. In order to get proper visibility, we install the
6329 -- parent scope and its declarations.
6330
6331 -- ??? If the parent is untagged private and its completion is
6332 -- tagged, this mechanism will not work because we cannot derive from
6333 -- the tagged full view unless we have an extension.
6334
6335 if Present (Full_View (Parent_Type))
6336 and then not Is_Tagged_Type (Full_View (Parent_Type))
6337 and then not Is_Completion
6338 then
6339 Full_Der :=
6340 Make_Defining_Identifier
6341 (Sloc (Derived_Type), Chars (Derived_Type));
6342 Set_Is_Itype (Full_Der);
6343 Set_Has_Private_Declaration (Full_Der);
6344 Set_Has_Private_Declaration (Derived_Type);
6345 Set_Associated_Node_For_Itype (Full_Der, N);
6346 Set_Parent (Full_Der, Parent (Derived_Type));
6347 Set_Full_View (Derived_Type, Full_Der);
6348
6349 if not In_Open_Scopes (Par_Scope) then
6350 Install_Private_Declarations (Par_Scope);
6351 Install_Visible_Declarations (Par_Scope);
6352 Copy_And_Build;
6353 Uninstall_Declarations (Par_Scope);
6354
6355 -- If parent scope is open and in another unit, and parent has a
6356 -- completion, then the derivation is taking place in the visible
6357 -- part of a child unit. In that case retrieve the full view of
6358 -- the parent momentarily.
6359
6360 elsif not In_Same_Source_Unit (N, Parent_Type) then
6361 Full_P := Full_View (Parent_Type);
6362 Exchange_Declarations (Parent_Type);
6363 Copy_And_Build;
6364 Exchange_Declarations (Full_P);
6365
6366 -- Otherwise it is a local derivation
6367
6368 else
6369 Copy_And_Build;
6370 end if;
6371
6372 Set_Scope (Full_Der, Current_Scope);
6373 Set_Is_First_Subtype (Full_Der,
6374 Is_First_Subtype (Derived_Type));
6375 Set_Has_Size_Clause (Full_Der, False);
6376 Set_Has_Alignment_Clause (Full_Der, False);
6377 Set_Next_Entity (Full_Der, Empty);
6378 Set_Has_Delayed_Freeze (Full_Der);
6379 Set_Is_Frozen (Full_Der, False);
6380 Set_Freeze_Node (Full_Der, Empty);
6381 Set_Depends_On_Private (Full_Der,
6382 Has_Private_Component (Full_Der));
6383 Set_Public_Status (Full_Der);
6384 end if;
6385 end if;
6386
6387 Set_Has_Unknown_Discriminants (Derived_Type,
6388 Has_Unknown_Discriminants (Parent_Type));
6389
6390 if Is_Private_Type (Derived_Type) then
6391 Set_Private_Dependents (Derived_Type, New_Elmt_List);
6392 end if;
6393
6394 if Is_Private_Type (Parent_Type)
6395 and then Base_Type (Parent_Type) = Parent_Type
6396 and then In_Open_Scopes (Scope (Parent_Type))
6397 then
6398 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
6399
6400 if Is_Child_Unit (Scope (Current_Scope))
6401 and then Is_Completion
6402 and then In_Private_Part (Current_Scope)
6403 and then Scope (Parent_Type) /= Current_Scope
6404 then
6405 -- This is the unusual case where a type completed by a private
6406 -- derivation occurs within a package nested in a child unit, and
6407 -- the parent is declared in an ancestor. In this case, the full
6408 -- view of the parent type will become visible in the body of
6409 -- the enclosing child, and only then will the current type be
6410 -- possibly non-private. We build a underlying full view that
6411 -- will be installed when the enclosing child body is compiled.
6412
6413 Full_Der :=
6414 Make_Defining_Identifier
6415 (Sloc (Derived_Type), Chars (Derived_Type));
6416 Set_Is_Itype (Full_Der);
6417 Build_Itype_Reference (Full_Der, N);
6418
6419 -- The full view will be used to swap entities on entry/exit to
6420 -- the body, and must appear in the entity list for the package.
6421
6422 Append_Entity (Full_Der, Scope (Derived_Type));
6423 Set_Has_Private_Declaration (Full_Der);
6424 Set_Has_Private_Declaration (Derived_Type);
6425 Set_Associated_Node_For_Itype (Full_Der, N);
6426 Set_Parent (Full_Der, Parent (Derived_Type));
6427 Full_P := Full_View (Parent_Type);
6428 Exchange_Declarations (Parent_Type);
6429 Copy_And_Build;
6430 Exchange_Declarations (Full_P);
6431 Set_Underlying_Full_View (Derived_Type, Full_Der);
6432 end if;
6433 end if;
6434 end Build_Derived_Private_Type;
6435
6436 -------------------------------
6437 -- Build_Derived_Record_Type --
6438 -------------------------------
6439
6440 -- 1. INTRODUCTION
6441
6442 -- Ideally we would like to use the same model of type derivation for
6443 -- tagged and untagged record types. Unfortunately this is not quite
6444 -- possible because the semantics of representation clauses is different
6445 -- for tagged and untagged records under inheritance. Consider the
6446 -- following:
6447
6448 -- type R (...) is [tagged] record ... end record;
6449 -- type T (...) is new R (...) [with ...];
6450
6451 -- The representation clauses for T can specify a completely different
6452 -- record layout from R's. Hence the same component can be placed in two
6453 -- very different positions in objects of type T and R. If R and T are
6454 -- tagged types, representation clauses for T can only specify the layout
6455 -- of non inherited components, thus components that are common in R and T
6456 -- have the same position in objects of type R and T.
6457
6458 -- This has two implications. The first is that the entire tree for R's
6459 -- declaration needs to be copied for T in the untagged case, so that T
6460 -- can be viewed as a record type of its own with its own representation
6461 -- clauses. The second implication is the way we handle discriminants.
6462 -- Specifically, in the untagged case we need a way to communicate to Gigi
6463 -- what are the real discriminants in the record, while for the semantics
6464 -- we need to consider those introduced by the user to rename the
6465 -- discriminants in the parent type. This is handled by introducing the
6466 -- notion of stored discriminants. See below for more.
6467
6468 -- Fortunately the way regular components are inherited can be handled in
6469 -- the same way in tagged and untagged types.
6470
6471 -- To complicate things a bit more the private view of a private extension
6472 -- cannot be handled in the same way as the full view (for one thing the
6473 -- semantic rules are somewhat different). We will explain what differs
6474 -- below.
6475
6476 -- 2. DISCRIMINANTS UNDER INHERITANCE
6477
6478 -- The semantic rules governing the discriminants of derived types are
6479 -- quite subtle.
6480
6481 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
6482 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
6483
6484 -- If parent type has discriminants, then the discriminants that are
6485 -- declared in the derived type are [3.4 (11)]:
6486
6487 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
6488 -- there is one;
6489
6490 -- o Otherwise, each discriminant of the parent type (implicitly declared
6491 -- in the same order with the same specifications). In this case, the
6492 -- discriminants are said to be "inherited", or if unknown in the parent
6493 -- are also unknown in the derived type.
6494
6495 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
6496
6497 -- o The parent subtype shall be constrained;
6498
6499 -- o If the parent type is not a tagged type, then each discriminant of
6500 -- the derived type shall be used in the constraint defining a parent
6501 -- subtype. [Implementation note: This ensures that the new discriminant
6502 -- can share storage with an existing discriminant.]
6503
6504 -- For the derived type each discriminant of the parent type is either
6505 -- inherited, constrained to equal some new discriminant of the derived
6506 -- type, or constrained to the value of an expression.
6507
6508 -- When inherited or constrained to equal some new discriminant, the
6509 -- parent discriminant and the discriminant of the derived type are said
6510 -- to "correspond".
6511
6512 -- If a discriminant of the parent type is constrained to a specific value
6513 -- in the derived type definition, then the discriminant is said to be
6514 -- "specified" by that derived type definition.
6515
6516 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
6517
6518 -- We have spoken about stored discriminants in point 1 (introduction)
6519 -- above. There are two sort of stored discriminants: implicit and
6520 -- explicit. As long as the derived type inherits the same discriminants as
6521 -- the root record type, stored discriminants are the same as regular
6522 -- discriminants, and are said to be implicit. However, if any discriminant
6523 -- in the root type was renamed in the derived type, then the derived
6524 -- type will contain explicit stored discriminants. Explicit stored
6525 -- discriminants are discriminants in addition to the semantically visible
6526 -- discriminants defined for the derived type. Stored discriminants are
6527 -- used by Gigi to figure out what are the physical discriminants in
6528 -- objects of the derived type (see precise definition in einfo.ads).
6529 -- As an example, consider the following:
6530
6531 -- type R (D1, D2, D3 : Int) is record ... end record;
6532 -- type T1 is new R;
6533 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
6534 -- type T3 is new T2;
6535 -- type T4 (Y : Int) is new T3 (Y, 99);
6536
6537 -- The following table summarizes the discriminants and stored
6538 -- discriminants in R and T1 through T4.
6539
6540 -- Type Discrim Stored Discrim Comment
6541 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
6542 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
6543 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
6544 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
6545 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
6546
6547 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
6548 -- find the corresponding discriminant in the parent type, while
6549 -- Original_Record_Component (abbreviated ORC below), the actual physical
6550 -- component that is renamed. Finally the field Is_Completely_Hidden
6551 -- (abbreviated ICH below) is set for all explicit stored discriminants
6552 -- (see einfo.ads for more info). For the above example this gives:
6553
6554 -- Discrim CD ORC ICH
6555 -- ^^^^^^^ ^^ ^^^ ^^^
6556 -- D1 in R empty itself no
6557 -- D2 in R empty itself no
6558 -- D3 in R empty itself no
6559
6560 -- D1 in T1 D1 in R itself no
6561 -- D2 in T1 D2 in R itself no
6562 -- D3 in T1 D3 in R itself no
6563
6564 -- X1 in T2 D3 in T1 D3 in T2 no
6565 -- X2 in T2 D1 in T1 D1 in T2 no
6566 -- D1 in T2 empty itself yes
6567 -- D2 in T2 empty itself yes
6568 -- D3 in T2 empty itself yes
6569
6570 -- X1 in T3 X1 in T2 D3 in T3 no
6571 -- X2 in T3 X2 in T2 D1 in T3 no
6572 -- D1 in T3 empty itself yes
6573 -- D2 in T3 empty itself yes
6574 -- D3 in T3 empty itself yes
6575
6576 -- Y in T4 X1 in T3 D3 in T3 no
6577 -- D1 in T3 empty itself yes
6578 -- D2 in T3 empty itself yes
6579 -- D3 in T3 empty itself yes
6580
6581 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
6582
6583 -- Type derivation for tagged types is fairly straightforward. If no
6584 -- discriminants are specified by the derived type, these are inherited
6585 -- from the parent. No explicit stored discriminants are ever necessary.
6586 -- The only manipulation that is done to the tree is that of adding a
6587 -- _parent field with parent type and constrained to the same constraint
6588 -- specified for the parent in the derived type definition. For instance:
6589
6590 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
6591 -- type T1 is new R with null record;
6592 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
6593
6594 -- are changed into:
6595
6596 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
6597 -- _parent : R (D1, D2, D3);
6598 -- end record;
6599
6600 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
6601 -- _parent : T1 (X2, 88, X1);
6602 -- end record;
6603
6604 -- The discriminants actually present in R, T1 and T2 as well as their CD,
6605 -- ORC and ICH fields are:
6606
6607 -- Discrim CD ORC ICH
6608 -- ^^^^^^^ ^^ ^^^ ^^^
6609 -- D1 in R empty itself no
6610 -- D2 in R empty itself no
6611 -- D3 in R empty itself no
6612
6613 -- D1 in T1 D1 in R D1 in R no
6614 -- D2 in T1 D2 in R D2 in R no
6615 -- D3 in T1 D3 in R D3 in R no
6616
6617 -- X1 in T2 D3 in T1 D3 in R no
6618 -- X2 in T2 D1 in T1 D1 in R no
6619
6620 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
6621 --
6622 -- Regardless of whether we dealing with a tagged or untagged type
6623 -- we will transform all derived type declarations of the form
6624 --
6625 -- type T is new R (...) [with ...];
6626 -- or
6627 -- subtype S is R (...);
6628 -- type T is new S [with ...];
6629 -- into
6630 -- type BT is new R [with ...];
6631 -- subtype T is BT (...);
6632 --
6633 -- That is, the base derived type is constrained only if it has no
6634 -- discriminants. The reason for doing this is that GNAT's semantic model
6635 -- assumes that a base type with discriminants is unconstrained.
6636 --
6637 -- Note that, strictly speaking, the above transformation is not always
6638 -- correct. Consider for instance the following excerpt from ACVC b34011a:
6639 --
6640 -- procedure B34011A is
6641 -- type REC (D : integer := 0) is record
6642 -- I : Integer;
6643 -- end record;
6644
6645 -- package P is
6646 -- type T6 is new Rec;
6647 -- function F return T6;
6648 -- end P;
6649
6650 -- use P;
6651 -- package Q6 is
6652 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
6653 -- end Q6;
6654 --
6655 -- The definition of Q6.U is illegal. However transforming Q6.U into
6656
6657 -- type BaseU is new T6;
6658 -- subtype U is BaseU (Q6.F.I)
6659
6660 -- turns U into a legal subtype, which is incorrect. To avoid this problem
6661 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
6662 -- the transformation described above.
6663
6664 -- There is another instance where the above transformation is incorrect.
6665 -- Consider:
6666
6667 -- package Pack is
6668 -- type Base (D : Integer) is tagged null record;
6669 -- procedure P (X : Base);
6670
6671 -- type Der is new Base (2) with null record;
6672 -- procedure P (X : Der);
6673 -- end Pack;
6674
6675 -- Then the above transformation turns this into
6676
6677 -- type Der_Base is new Base with null record;
6678 -- -- procedure P (X : Base) is implicitly inherited here
6679 -- -- as procedure P (X : Der_Base).
6680
6681 -- subtype Der is Der_Base (2);
6682 -- procedure P (X : Der);
6683 -- -- The overriding of P (X : Der_Base) is illegal since we
6684 -- -- have a parameter conformance problem.
6685
6686 -- To get around this problem, after having semantically processed Der_Base
6687 -- and the rewritten subtype declaration for Der, we copy Der_Base field
6688 -- Discriminant_Constraint from Der so that when parameter conformance is
6689 -- checked when P is overridden, no semantic errors are flagged.
6690
6691 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
6692
6693 -- Regardless of whether we are dealing with a tagged or untagged type
6694 -- we will transform all derived type declarations of the form
6695
6696 -- type R (D1, .., Dn : ...) is [tagged] record ...;
6697 -- type T is new R [with ...];
6698 -- into
6699 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6700
6701 -- The reason for such transformation is that it allows us to implement a
6702 -- very clean form of component inheritance as explained below.
6703
6704 -- Note that this transformation is not achieved by direct tree rewriting
6705 -- and manipulation, but rather by redoing the semantic actions that the
6706 -- above transformation will entail. This is done directly in routine
6707 -- Inherit_Components.
6708
6709 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
6710
6711 -- In both tagged and untagged derived types, regular non discriminant
6712 -- components are inherited in the derived type from the parent type. In
6713 -- the absence of discriminants component, inheritance is straightforward
6714 -- as components can simply be copied from the parent.
6715
6716 -- If the parent has discriminants, inheriting components constrained with
6717 -- these discriminants requires caution. Consider the following example:
6718
6719 -- type R (D1, D2 : Positive) is [tagged] record
6720 -- S : String (D1 .. D2);
6721 -- end record;
6722
6723 -- type T1 is new R [with null record];
6724 -- type T2 (X : positive) is new R (1, X) [with null record];
6725
6726 -- As explained in 6. above, T1 is rewritten as
6727 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
6728 -- which makes the treatment for T1 and T2 identical.
6729
6730 -- What we want when inheriting S, is that references to D1 and D2 in R are
6731 -- replaced with references to their correct constraints, i.e. D1 and D2 in
6732 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
6733 -- with either discriminant references in the derived type or expressions.
6734 -- This replacement is achieved as follows: before inheriting R's
6735 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6736 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
6737 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6738 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
6739 -- by String (1 .. X).
6740
6741 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6742
6743 -- We explain here the rules governing private type extensions relevant to
6744 -- type derivation. These rules are explained on the following example:
6745
6746 -- type D [(...)] is new A [(...)] with private; <-- partial view
6747 -- type D [(...)] is new P [(...)] with null record; <-- full view
6748
6749 -- Type A is called the ancestor subtype of the private extension.
6750 -- Type P is the parent type of the full view of the private extension. It
6751 -- must be A or a type derived from A.
6752
6753 -- The rules concerning the discriminants of private type extensions are
6754 -- [7.3(10-13)]:
6755
6756 -- o If a private extension inherits known discriminants from the ancestor
6757 -- subtype, then the full view shall also inherit its discriminants from
6758 -- the ancestor subtype and the parent subtype of the full view shall be
6759 -- constrained if and only if the ancestor subtype is constrained.
6760
6761 -- o If a partial view has unknown discriminants, then the full view may
6762 -- define a definite or an indefinite subtype, with or without
6763 -- discriminants.
6764
6765 -- o If a partial view has neither known nor unknown discriminants, then
6766 -- the full view shall define a definite subtype.
6767
6768 -- o If the ancestor subtype of a private extension has constrained
6769 -- discriminants, then the parent subtype of the full view shall impose a
6770 -- statically matching constraint on those discriminants.
6771
6772 -- This means that only the following forms of private extensions are
6773 -- allowed:
6774
6775 -- type D is new A with private; <-- partial view
6776 -- type D is new P with null record; <-- full view
6777
6778 -- If A has no discriminants than P has no discriminants, otherwise P must
6779 -- inherit A's discriminants.
6780
6781 -- type D is new A (...) with private; <-- partial view
6782 -- type D is new P (:::) with null record; <-- full view
6783
6784 -- P must inherit A's discriminants and (...) and (:::) must statically
6785 -- match.
6786
6787 -- subtype A is R (...);
6788 -- type D is new A with private; <-- partial view
6789 -- type D is new P with null record; <-- full view
6790
6791 -- P must have inherited R's discriminants and must be derived from A or
6792 -- any of its subtypes.
6793
6794 -- type D (..) is new A with private; <-- partial view
6795 -- type D (..) is new P [(:::)] with null record; <-- full view
6796
6797 -- No specific constraints on P's discriminants or constraint (:::).
6798 -- Note that A can be unconstrained, but the parent subtype P must either
6799 -- be constrained or (:::) must be present.
6800
6801 -- type D (..) is new A [(...)] with private; <-- partial view
6802 -- type D (..) is new P [(:::)] with null record; <-- full view
6803
6804 -- P's constraints on A's discriminants must statically match those
6805 -- imposed by (...).
6806
6807 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6808
6809 -- The full view of a private extension is handled exactly as described
6810 -- above. The model chose for the private view of a private extension is
6811 -- the same for what concerns discriminants (i.e. they receive the same
6812 -- treatment as in the tagged case). However, the private view of the
6813 -- private extension always inherits the components of the parent base,
6814 -- without replacing any discriminant reference. Strictly speaking this is
6815 -- incorrect. However, Gigi never uses this view to generate code so this
6816 -- is a purely semantic issue. In theory, a set of transformations similar
6817 -- to those given in 5. and 6. above could be applied to private views of
6818 -- private extensions to have the same model of component inheritance as
6819 -- for non private extensions. However, this is not done because it would
6820 -- further complicate private type processing. Semantically speaking, this
6821 -- leaves us in an uncomfortable situation. As an example consider:
6822
6823 -- package Pack is
6824 -- type R (D : integer) is tagged record
6825 -- S : String (1 .. D);
6826 -- end record;
6827 -- procedure P (X : R);
6828 -- type T is new R (1) with private;
6829 -- private
6830 -- type T is new R (1) with null record;
6831 -- end;
6832
6833 -- This is transformed into:
6834
6835 -- package Pack is
6836 -- type R (D : integer) is tagged record
6837 -- S : String (1 .. D);
6838 -- end record;
6839 -- procedure P (X : R);
6840 -- type T is new R (1) with private;
6841 -- private
6842 -- type BaseT is new R with null record;
6843 -- subtype T is BaseT (1);
6844 -- end;
6845
6846 -- (strictly speaking the above is incorrect Ada)
6847
6848 -- From the semantic standpoint the private view of private extension T
6849 -- should be flagged as constrained since one can clearly have
6850 --
6851 -- Obj : T;
6852 --
6853 -- in a unit withing Pack. However, when deriving subprograms for the
6854 -- private view of private extension T, T must be seen as unconstrained
6855 -- since T has discriminants (this is a constraint of the current
6856 -- subprogram derivation model). Thus, when processing the private view of
6857 -- a private extension such as T, we first mark T as unconstrained, we
6858 -- process it, we perform program derivation and just before returning from
6859 -- Build_Derived_Record_Type we mark T as constrained.
6860
6861 -- ??? Are there are other uncomfortable cases that we will have to
6862 -- deal with.
6863
6864 -- 10. RECORD_TYPE_WITH_PRIVATE complications
6865
6866 -- Types that are derived from a visible record type and have a private
6867 -- extension present other peculiarities. They behave mostly like private
6868 -- types, but if they have primitive operations defined, these will not
6869 -- have the proper signatures for further inheritance, because other
6870 -- primitive operations will use the implicit base that we define for
6871 -- private derivations below. This affect subprogram inheritance (see
6872 -- Derive_Subprograms for details). We also derive the implicit base from
6873 -- the base type of the full view, so that the implicit base is a record
6874 -- type and not another private type, This avoids infinite loops.
6875
6876 procedure Build_Derived_Record_Type
6877 (N : Node_Id;
6878 Parent_Type : Entity_Id;
6879 Derived_Type : Entity_Id;
6880 Derive_Subps : Boolean := True)
6881 is
6882 Loc : constant Source_Ptr := Sloc (N);
6883 Parent_Base : Entity_Id;
6884 Type_Def : Node_Id;
6885 Indic : Node_Id;
6886 Discrim : Entity_Id;
6887 Last_Discrim : Entity_Id;
6888 Constrs : Elist_Id;
6889
6890 Discs : Elist_Id := New_Elmt_List;
6891 -- An empty Discs list means that there were no constraints in the
6892 -- subtype indication or that there was an error processing it.
6893
6894 Assoc_List : Elist_Id;
6895 New_Discrs : Elist_Id;
6896 New_Base : Entity_Id;
6897 New_Decl : Node_Id;
6898 New_Indic : Node_Id;
6899
6900 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
6901 Discriminant_Specs : constant Boolean :=
6902 Present (Discriminant_Specifications (N));
6903 Private_Extension : constant Boolean :=
6904 Nkind (N) = N_Private_Extension_Declaration;
6905
6906 Constraint_Present : Boolean;
6907 Inherit_Discrims : Boolean := False;
6908 Save_Etype : Entity_Id;
6909 Save_Discr_Constr : Elist_Id;
6910 Save_Next_Entity : Entity_Id;
6911
6912 begin
6913 if Ekind (Parent_Type) = E_Record_Type_With_Private
6914 and then Present (Full_View (Parent_Type))
6915 and then Has_Discriminants (Parent_Type)
6916 then
6917 Parent_Base := Base_Type (Full_View (Parent_Type));
6918 else
6919 Parent_Base := Base_Type (Parent_Type);
6920 end if;
6921
6922 -- Before we start the previously documented transformations, here is
6923 -- little fix for size and alignment of tagged types. Normally when we
6924 -- derive type D from type P, we copy the size and alignment of P as the
6925 -- default for D, and in the absence of explicit representation clauses
6926 -- for D, the size and alignment are indeed the same as the parent.
6927
6928 -- But this is wrong for tagged types, since fields may be added, and
6929 -- the default size may need to be larger, and the default alignment may
6930 -- need to be larger.
6931
6932 -- We therefore reset the size and alignment fields in the tagged case.
6933 -- Note that the size and alignment will in any case be at least as
6934 -- large as the parent type (since the derived type has a copy of the
6935 -- parent type in the _parent field)
6936
6937 -- The type is also marked as being tagged here, which is needed when
6938 -- processing components with a self-referential anonymous access type
6939 -- in the call to Check_Anonymous_Access_Components below. Note that
6940 -- this flag is also set later on for completeness.
6941
6942 if Is_Tagged then
6943 Set_Is_Tagged_Type (Derived_Type);
6944 Init_Size_Align (Derived_Type);
6945 end if;
6946
6947 -- STEP 0a: figure out what kind of derived type declaration we have
6948
6949 if Private_Extension then
6950 Type_Def := N;
6951 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6952
6953 else
6954 Type_Def := Type_Definition (N);
6955
6956 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
6957 -- Parent_Base can be a private type or private extension. However,
6958 -- for tagged types with an extension the newly added fields are
6959 -- visible and hence the Derived_Type is always an E_Record_Type.
6960 -- (except that the parent may have its own private fields).
6961 -- For untagged types we preserve the Ekind of the Parent_Base.
6962
6963 if Present (Record_Extension_Part (Type_Def)) then
6964 Set_Ekind (Derived_Type, E_Record_Type);
6965
6966 -- Create internal access types for components with anonymous
6967 -- access types.
6968
6969 if Ada_Version >= Ada_2005 then
6970 Check_Anonymous_Access_Components
6971 (N, Derived_Type, Derived_Type,
6972 Component_List (Record_Extension_Part (Type_Def)));
6973 end if;
6974
6975 else
6976 Set_Ekind (Derived_Type, Ekind (Parent_Base));
6977 end if;
6978 end if;
6979
6980 -- Indic can either be an N_Identifier if the subtype indication
6981 -- contains no constraint or an N_Subtype_Indication if the subtype
6982 -- indication has a constraint.
6983
6984 Indic := Subtype_Indication (Type_Def);
6985 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6986
6987 -- Check that the type has visible discriminants. The type may be
6988 -- a private type with unknown discriminants whose full view has
6989 -- discriminants which are invisible.
6990
6991 if Constraint_Present then
6992 if not Has_Discriminants (Parent_Base)
6993 or else
6994 (Has_Unknown_Discriminants (Parent_Base)
6995 and then Is_Private_Type (Parent_Base))
6996 then
6997 Error_Msg_N
6998 ("invalid constraint: type has no discriminant",
6999 Constraint (Indic));
7000
7001 Constraint_Present := False;
7002 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7003
7004 elsif Is_Constrained (Parent_Type) then
7005 Error_Msg_N
7006 ("invalid constraint: parent type is already constrained",
7007 Constraint (Indic));
7008
7009 Constraint_Present := False;
7010 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7011 end if;
7012 end if;
7013
7014 -- STEP 0b: If needed, apply transformation given in point 5. above
7015
7016 if not Private_Extension
7017 and then Has_Discriminants (Parent_Type)
7018 and then not Discriminant_Specs
7019 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7020 then
7021 -- First, we must analyze the constraint (see comment in point 5.)
7022
7023 if Constraint_Present then
7024 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
7025
7026 if Has_Discriminants (Derived_Type)
7027 and then Has_Private_Declaration (Derived_Type)
7028 and then Present (Discriminant_Constraint (Derived_Type))
7029 then
7030 -- Verify that constraints of the full view statically match
7031 -- those given in the partial view.
7032
7033 declare
7034 C1, C2 : Elmt_Id;
7035
7036 begin
7037 C1 := First_Elmt (New_Discrs);
7038 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
7039 while Present (C1) and then Present (C2) loop
7040 if Fully_Conformant_Expressions (Node (C1), Node (C2))
7041 or else
7042 (Is_OK_Static_Expression (Node (C1))
7043 and then
7044 Is_OK_Static_Expression (Node (C2))
7045 and then
7046 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
7047 then
7048 null;
7049
7050 else
7051 Error_Msg_N (
7052 "constraint not conformant to previous declaration",
7053 Node (C1));
7054 end if;
7055
7056 Next_Elmt (C1);
7057 Next_Elmt (C2);
7058 end loop;
7059 end;
7060 end if;
7061 end if;
7062
7063 -- Insert and analyze the declaration for the unconstrained base type
7064
7065 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7066
7067 New_Decl :=
7068 Make_Full_Type_Declaration (Loc,
7069 Defining_Identifier => New_Base,
7070 Type_Definition =>
7071 Make_Derived_Type_Definition (Loc,
7072 Abstract_Present => Abstract_Present (Type_Def),
7073 Limited_Present => Limited_Present (Type_Def),
7074 Subtype_Indication =>
7075 New_Occurrence_Of (Parent_Base, Loc),
7076 Record_Extension_Part =>
7077 Relocate_Node (Record_Extension_Part (Type_Def)),
7078 Interface_List => Interface_List (Type_Def)));
7079
7080 Set_Parent (New_Decl, Parent (N));
7081 Mark_Rewrite_Insertion (New_Decl);
7082 Insert_Before (N, New_Decl);
7083
7084 -- In the extension case, make sure ancestor is frozen appropriately
7085 -- (see also non-discriminated case below).
7086
7087 if Present (Record_Extension_Part (Type_Def))
7088 or else Is_Interface (Parent_Base)
7089 then
7090 Freeze_Before (New_Decl, Parent_Type);
7091 end if;
7092
7093 -- Note that this call passes False for the Derive_Subps parameter
7094 -- because subprogram derivation is deferred until after creating
7095 -- the subtype (see below).
7096
7097 Build_Derived_Type
7098 (New_Decl, Parent_Base, New_Base,
7099 Is_Completion => True, Derive_Subps => False);
7100
7101 -- ??? This needs re-examination to determine whether the
7102 -- above call can simply be replaced by a call to Analyze.
7103
7104 Set_Analyzed (New_Decl);
7105
7106 -- Insert and analyze the declaration for the constrained subtype
7107
7108 if Constraint_Present then
7109 New_Indic :=
7110 Make_Subtype_Indication (Loc,
7111 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7112 Constraint => Relocate_Node (Constraint (Indic)));
7113
7114 else
7115 declare
7116 Constr_List : constant List_Id := New_List;
7117 C : Elmt_Id;
7118 Expr : Node_Id;
7119
7120 begin
7121 C := First_Elmt (Discriminant_Constraint (Parent_Type));
7122 while Present (C) loop
7123 Expr := Node (C);
7124
7125 -- It is safe here to call New_Copy_Tree since
7126 -- Force_Evaluation was called on each constraint in
7127 -- Build_Discriminant_Constraints.
7128
7129 Append (New_Copy_Tree (Expr), To => Constr_List);
7130
7131 Next_Elmt (C);
7132 end loop;
7133
7134 New_Indic :=
7135 Make_Subtype_Indication (Loc,
7136 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7137 Constraint =>
7138 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
7139 end;
7140 end if;
7141
7142 Rewrite (N,
7143 Make_Subtype_Declaration (Loc,
7144 Defining_Identifier => Derived_Type,
7145 Subtype_Indication => New_Indic));
7146
7147 Analyze (N);
7148
7149 -- Derivation of subprograms must be delayed until the full subtype
7150 -- has been established to ensure proper overriding of subprograms
7151 -- inherited by full types. If the derivations occurred as part of
7152 -- the call to Build_Derived_Type above, then the check for type
7153 -- conformance would fail because earlier primitive subprograms
7154 -- could still refer to the full type prior the change to the new
7155 -- subtype and hence would not match the new base type created here.
7156
7157 Derive_Subprograms (Parent_Type, Derived_Type);
7158
7159 -- For tagged types the Discriminant_Constraint of the new base itype
7160 -- is inherited from the first subtype so that no subtype conformance
7161 -- problem arise when the first subtype overrides primitive
7162 -- operations inherited by the implicit base type.
7163
7164 if Is_Tagged then
7165 Set_Discriminant_Constraint
7166 (New_Base, Discriminant_Constraint (Derived_Type));
7167 end if;
7168
7169 return;
7170 end if;
7171
7172 -- If we get here Derived_Type will have no discriminants or it will be
7173 -- a discriminated unconstrained base type.
7174
7175 -- STEP 1a: perform preliminary actions/checks for derived tagged types
7176
7177 if Is_Tagged then
7178
7179 -- The parent type is frozen for non-private extensions (RM 13.14(7))
7180 -- The declaration of a specific descendant of an interface type
7181 -- freezes the interface type (RM 13.14).
7182
7183 if not Private_Extension or else Is_Interface (Parent_Base) then
7184 Freeze_Before (N, Parent_Type);
7185 end if;
7186
7187 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
7188 -- cannot be declared at a deeper level than its parent type is
7189 -- removed. The check on derivation within a generic body is also
7190 -- relaxed, but there's a restriction that a derived tagged type
7191 -- cannot be declared in a generic body if it's derived directly
7192 -- or indirectly from a formal type of that generic.
7193
7194 if Ada_Version >= Ada_2005 then
7195 if Present (Enclosing_Generic_Body (Derived_Type)) then
7196 declare
7197 Ancestor_Type : Entity_Id;
7198
7199 begin
7200 -- Check to see if any ancestor of the derived type is a
7201 -- formal type.
7202
7203 Ancestor_Type := Parent_Type;
7204 while not Is_Generic_Type (Ancestor_Type)
7205 and then Etype (Ancestor_Type) /= Ancestor_Type
7206 loop
7207 Ancestor_Type := Etype (Ancestor_Type);
7208 end loop;
7209
7210 -- If the derived type does have a formal type as an
7211 -- ancestor, then it's an error if the derived type is
7212 -- declared within the body of the generic unit that
7213 -- declares the formal type in its generic formal part. It's
7214 -- sufficient to check whether the ancestor type is declared
7215 -- inside the same generic body as the derived type (such as
7216 -- within a nested generic spec), in which case the
7217 -- derivation is legal. If the formal type is declared
7218 -- outside of that generic body, then it's guaranteed that
7219 -- the derived type is declared within the generic body of
7220 -- the generic unit declaring the formal type.
7221
7222 if Is_Generic_Type (Ancestor_Type)
7223 and then Enclosing_Generic_Body (Ancestor_Type) /=
7224 Enclosing_Generic_Body (Derived_Type)
7225 then
7226 Error_Msg_NE
7227 ("parent type of& must not be descendant of formal type"
7228 & " of an enclosing generic body",
7229 Indic, Derived_Type);
7230 end if;
7231 end;
7232 end if;
7233
7234 elsif Type_Access_Level (Derived_Type) /=
7235 Type_Access_Level (Parent_Type)
7236 and then not Is_Generic_Type (Derived_Type)
7237 then
7238 if Is_Controlled (Parent_Type) then
7239 Error_Msg_N
7240 ("controlled type must be declared at the library level",
7241 Indic);
7242 else
7243 Error_Msg_N
7244 ("type extension at deeper accessibility level than parent",
7245 Indic);
7246 end if;
7247
7248 else
7249 declare
7250 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
7251
7252 begin
7253 if Present (GB)
7254 and then GB /= Enclosing_Generic_Body (Parent_Base)
7255 then
7256 Error_Msg_NE
7257 ("parent type of& must not be outside generic body"
7258 & " (RM 3.9.1(4))",
7259 Indic, Derived_Type);
7260 end if;
7261 end;
7262 end if;
7263 end if;
7264
7265 -- Ada 2005 (AI-251)
7266
7267 if Ada_Version >= Ada_2005 and then Is_Tagged then
7268
7269 -- "The declaration of a specific descendant of an interface type
7270 -- freezes the interface type" (RM 13.14).
7271
7272 declare
7273 Iface : Node_Id;
7274 begin
7275 if Is_Non_Empty_List (Interface_List (Type_Def)) then
7276 Iface := First (Interface_List (Type_Def));
7277 while Present (Iface) loop
7278 Freeze_Before (N, Etype (Iface));
7279 Next (Iface);
7280 end loop;
7281 end if;
7282 end;
7283 end if;
7284
7285 -- STEP 1b : preliminary cleanup of the full view of private types
7286
7287 -- If the type is already marked as having discriminants, then it's the
7288 -- completion of a private type or private extension and we need to
7289 -- retain the discriminants from the partial view if the current
7290 -- declaration has Discriminant_Specifications so that we can verify
7291 -- conformance. However, we must remove any existing components that
7292 -- were inherited from the parent (and attached in Copy_And_Swap)
7293 -- because the full type inherits all appropriate components anyway, and
7294 -- we do not want the partial view's components interfering.
7295
7296 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
7297 Discrim := First_Discriminant (Derived_Type);
7298 loop
7299 Last_Discrim := Discrim;
7300 Next_Discriminant (Discrim);
7301 exit when No (Discrim);
7302 end loop;
7303
7304 Set_Last_Entity (Derived_Type, Last_Discrim);
7305
7306 -- In all other cases wipe out the list of inherited components (even
7307 -- inherited discriminants), it will be properly rebuilt here.
7308
7309 else
7310 Set_First_Entity (Derived_Type, Empty);
7311 Set_Last_Entity (Derived_Type, Empty);
7312 end if;
7313
7314 -- STEP 1c: Initialize some flags for the Derived_Type
7315
7316 -- The following flags must be initialized here so that
7317 -- Process_Discriminants can check that discriminants of tagged types do
7318 -- not have a default initial value and that access discriminants are
7319 -- only specified for limited records. For completeness, these flags are
7320 -- also initialized along with all the other flags below.
7321
7322 -- AI-419: Limitedness is not inherited from an interface parent, so to
7323 -- be limited in that case the type must be explicitly declared as
7324 -- limited. However, task and protected interfaces are always limited.
7325
7326 if Limited_Present (Type_Def) then
7327 Set_Is_Limited_Record (Derived_Type);
7328
7329 elsif Is_Limited_Record (Parent_Type)
7330 or else (Present (Full_View (Parent_Type))
7331 and then Is_Limited_Record (Full_View (Parent_Type)))
7332 then
7333 if not Is_Interface (Parent_Type)
7334 or else Is_Synchronized_Interface (Parent_Type)
7335 or else Is_Protected_Interface (Parent_Type)
7336 or else Is_Task_Interface (Parent_Type)
7337 then
7338 Set_Is_Limited_Record (Derived_Type);
7339 end if;
7340 end if;
7341
7342 -- STEP 2a: process discriminants of derived type if any
7343
7344 Push_Scope (Derived_Type);
7345
7346 if Discriminant_Specs then
7347 Set_Has_Unknown_Discriminants (Derived_Type, False);
7348
7349 -- The following call initializes fields Has_Discriminants and
7350 -- Discriminant_Constraint, unless we are processing the completion
7351 -- of a private type declaration.
7352
7353 Check_Or_Process_Discriminants (N, Derived_Type);
7354
7355 -- For untagged types, the constraint on the Parent_Type must be
7356 -- present and is used to rename the discriminants.
7357
7358 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
7359 Error_Msg_N ("untagged parent must have discriminants", Indic);
7360
7361 elsif not Is_Tagged and then not Constraint_Present then
7362 Error_Msg_N
7363 ("discriminant constraint needed for derived untagged records",
7364 Indic);
7365
7366 -- Otherwise the parent subtype must be constrained unless we have a
7367 -- private extension.
7368
7369 elsif not Constraint_Present
7370 and then not Private_Extension
7371 and then not Is_Constrained (Parent_Type)
7372 then
7373 Error_Msg_N
7374 ("unconstrained type not allowed in this context", Indic);
7375
7376 elsif Constraint_Present then
7377 -- The following call sets the field Corresponding_Discriminant
7378 -- for the discriminants in the Derived_Type.
7379
7380 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
7381
7382 -- For untagged types all new discriminants must rename
7383 -- discriminants in the parent. For private extensions new
7384 -- discriminants cannot rename old ones (implied by [7.3(13)]).
7385
7386 Discrim := First_Discriminant (Derived_Type);
7387 while Present (Discrim) loop
7388 if not Is_Tagged
7389 and then No (Corresponding_Discriminant (Discrim))
7390 then
7391 Error_Msg_N
7392 ("new discriminants must constrain old ones", Discrim);
7393
7394 elsif Private_Extension
7395 and then Present (Corresponding_Discriminant (Discrim))
7396 then
7397 Error_Msg_N
7398 ("only static constraints allowed for parent"
7399 & " discriminants in the partial view", Indic);
7400 exit;
7401 end if;
7402
7403 -- If a new discriminant is used in the constraint, then its
7404 -- subtype must be statically compatible with the parent
7405 -- discriminant's subtype (3.7(15)).
7406
7407 if Present (Corresponding_Discriminant (Discrim))
7408 and then
7409 not Subtypes_Statically_Compatible
7410 (Etype (Discrim),
7411 Etype (Corresponding_Discriminant (Discrim)))
7412 then
7413 Error_Msg_N
7414 ("subtype must be compatible with parent discriminant",
7415 Discrim);
7416 end if;
7417
7418 Next_Discriminant (Discrim);
7419 end loop;
7420
7421 -- Check whether the constraints of the full view statically
7422 -- match those imposed by the parent subtype [7.3(13)].
7423
7424 if Present (Stored_Constraint (Derived_Type)) then
7425 declare
7426 C1, C2 : Elmt_Id;
7427
7428 begin
7429 C1 := First_Elmt (Discs);
7430 C2 := First_Elmt (Stored_Constraint (Derived_Type));
7431 while Present (C1) and then Present (C2) loop
7432 if not
7433 Fully_Conformant_Expressions (Node (C1), Node (C2))
7434 then
7435 Error_Msg_N
7436 ("not conformant with previous declaration",
7437 Node (C1));
7438 end if;
7439
7440 Next_Elmt (C1);
7441 Next_Elmt (C2);
7442 end loop;
7443 end;
7444 end if;
7445 end if;
7446
7447 -- STEP 2b: No new discriminants, inherit discriminants if any
7448
7449 else
7450 if Private_Extension then
7451 Set_Has_Unknown_Discriminants
7452 (Derived_Type,
7453 Has_Unknown_Discriminants (Parent_Type)
7454 or else Unknown_Discriminants_Present (N));
7455
7456 -- The partial view of the parent may have unknown discriminants,
7457 -- but if the full view has discriminants and the parent type is
7458 -- in scope they must be inherited.
7459
7460 elsif Has_Unknown_Discriminants (Parent_Type)
7461 and then
7462 (not Has_Discriminants (Parent_Type)
7463 or else not In_Open_Scopes (Scope (Parent_Type)))
7464 then
7465 Set_Has_Unknown_Discriminants (Derived_Type);
7466 end if;
7467
7468 if not Has_Unknown_Discriminants (Derived_Type)
7469 and then not Has_Unknown_Discriminants (Parent_Base)
7470 and then Has_Discriminants (Parent_Type)
7471 then
7472 Inherit_Discrims := True;
7473 Set_Has_Discriminants
7474 (Derived_Type, True);
7475 Set_Discriminant_Constraint
7476 (Derived_Type, Discriminant_Constraint (Parent_Base));
7477 end if;
7478
7479 -- The following test is true for private types (remember
7480 -- transformation 5. is not applied to those) and in an error
7481 -- situation.
7482
7483 if Constraint_Present then
7484 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
7485 end if;
7486
7487 -- For now mark a new derived type as constrained only if it has no
7488 -- discriminants. At the end of Build_Derived_Record_Type we properly
7489 -- set this flag in the case of private extensions. See comments in
7490 -- point 9. just before body of Build_Derived_Record_Type.
7491
7492 Set_Is_Constrained
7493 (Derived_Type,
7494 not (Inherit_Discrims
7495 or else Has_Unknown_Discriminants (Derived_Type)));
7496 end if;
7497
7498 -- STEP 3: initialize fields of derived type
7499
7500 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
7501 Set_Stored_Constraint (Derived_Type, No_Elist);
7502
7503 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
7504 -- but cannot be interfaces
7505
7506 if not Private_Extension
7507 and then Ekind (Derived_Type) /= E_Private_Type
7508 and then Ekind (Derived_Type) /= E_Limited_Private_Type
7509 then
7510 if Interface_Present (Type_Def) then
7511 Analyze_Interface_Declaration (Derived_Type, Type_Def);
7512 end if;
7513
7514 Set_Interfaces (Derived_Type, No_Elist);
7515 end if;
7516
7517 -- Fields inherited from the Parent_Type
7518
7519 Set_Discard_Names
7520 (Derived_Type, Einfo.Discard_Names (Parent_Type));
7521 Set_Has_Specified_Layout
7522 (Derived_Type, Has_Specified_Layout (Parent_Type));
7523 Set_Is_Limited_Composite
7524 (Derived_Type, Is_Limited_Composite (Parent_Type));
7525 Set_Is_Private_Composite
7526 (Derived_Type, Is_Private_Composite (Parent_Type));
7527
7528 -- Fields inherited from the Parent_Base
7529
7530 Set_Has_Controlled_Component
7531 (Derived_Type, Has_Controlled_Component (Parent_Base));
7532 Set_Has_Non_Standard_Rep
7533 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7534 Set_Has_Primitive_Operations
7535 (Derived_Type, Has_Primitive_Operations (Parent_Base));
7536
7537 -- Fields inherited from the Parent_Base in the non-private case
7538
7539 if Ekind (Derived_Type) = E_Record_Type then
7540 Set_Has_Complex_Representation
7541 (Derived_Type, Has_Complex_Representation (Parent_Base));
7542 end if;
7543
7544 -- Fields inherited from the Parent_Base for record types
7545
7546 if Is_Record_Type (Derived_Type) then
7547
7548 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7549 -- Parent_Base can be a private type or private extension.
7550
7551 if Present (Full_View (Parent_Base)) then
7552 Set_OK_To_Reorder_Components
7553 (Derived_Type,
7554 OK_To_Reorder_Components (Full_View (Parent_Base)));
7555 Set_Reverse_Bit_Order
7556 (Derived_Type, Reverse_Bit_Order (Full_View (Parent_Base)));
7557 else
7558 Set_OK_To_Reorder_Components
7559 (Derived_Type, OK_To_Reorder_Components (Parent_Base));
7560 Set_Reverse_Bit_Order
7561 (Derived_Type, Reverse_Bit_Order (Parent_Base));
7562 end if;
7563 end if;
7564
7565 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7566
7567 if not Is_Controlled (Parent_Type) then
7568 Set_Finalize_Storage_Only
7569 (Derived_Type, Finalize_Storage_Only (Parent_Type));
7570 end if;
7571
7572 -- Set fields for private derived types
7573
7574 if Is_Private_Type (Derived_Type) then
7575 Set_Depends_On_Private (Derived_Type, True);
7576 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7577
7578 -- Inherit fields from non private record types. If this is the
7579 -- completion of a derivation from a private type, the parent itself
7580 -- is private, and the attributes come from its full view, which must
7581 -- be present.
7582
7583 else
7584 if Is_Private_Type (Parent_Base)
7585 and then not Is_Record_Type (Parent_Base)
7586 then
7587 Set_Component_Alignment
7588 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
7589 Set_C_Pass_By_Copy
7590 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
7591 else
7592 Set_Component_Alignment
7593 (Derived_Type, Component_Alignment (Parent_Base));
7594 Set_C_Pass_By_Copy
7595 (Derived_Type, C_Pass_By_Copy (Parent_Base));
7596 end if;
7597 end if;
7598
7599 -- Set fields for tagged types
7600
7601 if Is_Tagged then
7602 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
7603
7604 -- All tagged types defined in Ada.Finalization are controlled
7605
7606 if Chars (Scope (Derived_Type)) = Name_Finalization
7607 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
7608 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
7609 then
7610 Set_Is_Controlled (Derived_Type);
7611 else
7612 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
7613 end if;
7614
7615 -- Minor optimization: there is no need to generate the class-wide
7616 -- entity associated with an underlying record view.
7617
7618 if not Is_Underlying_Record_View (Derived_Type) then
7619 Make_Class_Wide_Type (Derived_Type);
7620 end if;
7621
7622 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
7623
7624 if Has_Discriminants (Derived_Type)
7625 and then Constraint_Present
7626 then
7627 Set_Stored_Constraint
7628 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
7629 end if;
7630
7631 if Ada_Version >= Ada_2005 then
7632 declare
7633 Ifaces_List : Elist_Id;
7634
7635 begin
7636 -- Checks rules 3.9.4 (13/2 and 14/2)
7637
7638 if Comes_From_Source (Derived_Type)
7639 and then not Is_Private_Type (Derived_Type)
7640 and then Is_Interface (Parent_Type)
7641 and then not Is_Interface (Derived_Type)
7642 then
7643 if Is_Task_Interface (Parent_Type) then
7644 Error_Msg_N
7645 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
7646 Derived_Type);
7647
7648 elsif Is_Protected_Interface (Parent_Type) then
7649 Error_Msg_N
7650 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
7651 Derived_Type);
7652 end if;
7653 end if;
7654
7655 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
7656
7657 Check_Interfaces (N, Type_Def);
7658
7659 -- Ada 2005 (AI-251): Collect the list of progenitors that are
7660 -- not already in the parents.
7661
7662 Collect_Interfaces
7663 (T => Derived_Type,
7664 Ifaces_List => Ifaces_List,
7665 Exclude_Parents => True);
7666
7667 Set_Interfaces (Derived_Type, Ifaces_List);
7668
7669 -- If the derived type is the anonymous type created for
7670 -- a declaration whose parent has a constraint, propagate
7671 -- the interface list to the source type. This must be done
7672 -- prior to the completion of the analysis of the source type
7673 -- because the components in the extension may contain current
7674 -- instances whose legality depends on some ancestor.
7675
7676 if Is_Itype (Derived_Type) then
7677 declare
7678 Def : constant Node_Id :=
7679 Associated_Node_For_Itype (Derived_Type);
7680 begin
7681 if Present (Def)
7682 and then Nkind (Def) = N_Full_Type_Declaration
7683 then
7684 Set_Interfaces
7685 (Defining_Identifier (Def), Ifaces_List);
7686 end if;
7687 end;
7688 end if;
7689 end;
7690 end if;
7691
7692 else
7693 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
7694 Set_Has_Non_Standard_Rep
7695 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7696 end if;
7697
7698 -- STEP 4: Inherit components from the parent base and constrain them.
7699 -- Apply the second transformation described in point 6. above.
7700
7701 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
7702 or else not Has_Discriminants (Parent_Type)
7703 or else not Is_Constrained (Parent_Type)
7704 then
7705 Constrs := Discs;
7706 else
7707 Constrs := Discriminant_Constraint (Parent_Type);
7708 end if;
7709
7710 Assoc_List :=
7711 Inherit_Components
7712 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
7713
7714 -- STEP 5a: Copy the parent record declaration for untagged types
7715
7716 if not Is_Tagged then
7717
7718 -- Discriminant_Constraint (Derived_Type) has been properly
7719 -- constructed. Save it and temporarily set it to Empty because we
7720 -- do not want the call to New_Copy_Tree below to mess this list.
7721
7722 if Has_Discriminants (Derived_Type) then
7723 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7724 Set_Discriminant_Constraint (Derived_Type, No_Elist);
7725 else
7726 Save_Discr_Constr := No_Elist;
7727 end if;
7728
7729 -- Save the Etype field of Derived_Type. It is correctly set now,
7730 -- but the call to New_Copy tree may remap it to point to itself,
7731 -- which is not what we want. Ditto for the Next_Entity field.
7732
7733 Save_Etype := Etype (Derived_Type);
7734 Save_Next_Entity := Next_Entity (Derived_Type);
7735
7736 -- Assoc_List maps all stored discriminants in the Parent_Base to
7737 -- stored discriminants in the Derived_Type. It is fundamental that
7738 -- no types or itypes with discriminants other than the stored
7739 -- discriminants appear in the entities declared inside
7740 -- Derived_Type, since the back end cannot deal with it.
7741
7742 New_Decl :=
7743 New_Copy_Tree
7744 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7745
7746 -- Restore the fields saved prior to the New_Copy_Tree call
7747 -- and compute the stored constraint.
7748
7749 Set_Etype (Derived_Type, Save_Etype);
7750 Set_Next_Entity (Derived_Type, Save_Next_Entity);
7751
7752 if Has_Discriminants (Derived_Type) then
7753 Set_Discriminant_Constraint
7754 (Derived_Type, Save_Discr_Constr);
7755 Set_Stored_Constraint
7756 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
7757 Replace_Components (Derived_Type, New_Decl);
7758 end if;
7759
7760 -- Insert the new derived type declaration
7761
7762 Rewrite (N, New_Decl);
7763
7764 -- STEP 5b: Complete the processing for record extensions in generics
7765
7766 -- There is no completion for record extensions declared in the
7767 -- parameter part of a generic, so we need to complete processing for
7768 -- these generic record extensions here. The Record_Type_Definition call
7769 -- will change the Ekind of the components from E_Void to E_Component.
7770
7771 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
7772 Record_Type_Definition (Empty, Derived_Type);
7773
7774 -- STEP 5c: Process the record extension for non private tagged types
7775
7776 elsif not Private_Extension then
7777
7778 -- Add the _parent field in the derived type
7779
7780 Expand_Record_Extension (Derived_Type, Type_Def);
7781
7782 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
7783 -- implemented interfaces if we are in expansion mode
7784
7785 if Expander_Active
7786 and then Has_Interfaces (Derived_Type)
7787 then
7788 Add_Interface_Tag_Components (N, Derived_Type);
7789 end if;
7790
7791 -- Analyze the record extension
7792
7793 Record_Type_Definition
7794 (Record_Extension_Part (Type_Def), Derived_Type);
7795 end if;
7796
7797 End_Scope;
7798
7799 -- Nothing else to do if there is an error in the derivation.
7800 -- An unusual case: the full view may be derived from a type in an
7801 -- instance, when the partial view was used illegally as an actual
7802 -- in that instance, leading to a circular definition.
7803
7804 if Etype (Derived_Type) = Any_Type
7805 or else Etype (Parent_Type) = Derived_Type
7806 then
7807 return;
7808 end if;
7809
7810 -- Set delayed freeze and then derive subprograms, we need to do
7811 -- this in this order so that derived subprograms inherit the
7812 -- derived freeze if necessary.
7813
7814 Set_Has_Delayed_Freeze (Derived_Type);
7815
7816 if Derive_Subps then
7817 Derive_Subprograms (Parent_Type, Derived_Type);
7818 end if;
7819
7820 -- If we have a private extension which defines a constrained derived
7821 -- type mark as constrained here after we have derived subprograms. See
7822 -- comment on point 9. just above the body of Build_Derived_Record_Type.
7823
7824 if Private_Extension and then Inherit_Discrims then
7825 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7826 Set_Is_Constrained (Derived_Type, True);
7827 Set_Discriminant_Constraint (Derived_Type, Discs);
7828
7829 elsif Is_Constrained (Parent_Type) then
7830 Set_Is_Constrained
7831 (Derived_Type, True);
7832 Set_Discriminant_Constraint
7833 (Derived_Type, Discriminant_Constraint (Parent_Type));
7834 end if;
7835 end if;
7836
7837 -- Update the class-wide type, which shares the now-completed entity
7838 -- list with its specific type. In case of underlying record views,
7839 -- we do not generate the corresponding class wide entity.
7840
7841 if Is_Tagged
7842 and then not Is_Underlying_Record_View (Derived_Type)
7843 then
7844 Set_First_Entity
7845 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7846 Set_Last_Entity
7847 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7848 end if;
7849
7850 -- Update the scope of anonymous access types of discriminants and other
7851 -- components, to prevent scope anomalies in gigi, when the derivation
7852 -- appears in a scope nested within that of the parent.
7853
7854 declare
7855 D : Entity_Id;
7856
7857 begin
7858 D := First_Entity (Derived_Type);
7859 while Present (D) loop
7860 if Ekind_In (D, E_Discriminant, E_Component) then
7861 if Is_Itype (Etype (D))
7862 and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7863 then
7864 Set_Scope (Etype (D), Current_Scope);
7865 end if;
7866 end if;
7867
7868 Next_Entity (D);
7869 end loop;
7870 end;
7871 end Build_Derived_Record_Type;
7872
7873 ------------------------
7874 -- Build_Derived_Type --
7875 ------------------------
7876
7877 procedure Build_Derived_Type
7878 (N : Node_Id;
7879 Parent_Type : Entity_Id;
7880 Derived_Type : Entity_Id;
7881 Is_Completion : Boolean;
7882 Derive_Subps : Boolean := True)
7883 is
7884 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7885
7886 begin
7887 -- Set common attributes
7888
7889 Set_Scope (Derived_Type, Current_Scope);
7890
7891 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7892 Set_Etype (Derived_Type, Parent_Base);
7893 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
7894
7895 Set_Size_Info (Derived_Type, Parent_Type);
7896 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
7897 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7898 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
7899
7900 -- If the parent type is a private subtype, the convention on the base
7901 -- type may be set in the private part, and not propagated to the
7902 -- subtype until later, so we obtain the convention from the base type.
7903
7904 Set_Convention (Derived_Type, Convention (Parent_Base));
7905
7906 -- Propagate invariant information. The new type has invariants if
7907 -- they are inherited from the parent type, and these invariants can
7908 -- be further inherited, so both flags are set.
7909
7910 if Has_Inheritable_Invariants (Parent_Type) then
7911 Set_Has_Inheritable_Invariants (Derived_Type);
7912 Set_Has_Invariants (Derived_Type);
7913 end if;
7914
7915 -- We similarly inherit predicates
7916
7917 if Has_Predicates (Parent_Type) then
7918 Set_Has_Predicates (Derived_Type);
7919 end if;
7920
7921 -- The derived type inherits the representation clauses of the parent.
7922 -- However, for a private type that is completed by a derivation, there
7923 -- may be operation attributes that have been specified already (stream
7924 -- attributes and External_Tag) and those must be provided. Finally,
7925 -- if the partial view is a private extension, the representation items
7926 -- of the parent have been inherited already, and should not be chained
7927 -- twice to the derived type.
7928
7929 if Is_Tagged_Type (Parent_Type)
7930 and then Present (First_Rep_Item (Derived_Type))
7931 then
7932 -- The existing items are either operational items or items inherited
7933 -- from a private extension declaration.
7934
7935 declare
7936 Rep : Node_Id;
7937 -- Used to iterate over representation items of the derived type
7938
7939 Last_Rep : Node_Id;
7940 -- Last representation item of the (non-empty) representation
7941 -- item list of the derived type.
7942
7943 Found : Boolean := False;
7944
7945 begin
7946 Rep := First_Rep_Item (Derived_Type);
7947 Last_Rep := Rep;
7948 while Present (Rep) loop
7949 if Rep = First_Rep_Item (Parent_Type) then
7950 Found := True;
7951 exit;
7952
7953 else
7954 Rep := Next_Rep_Item (Rep);
7955
7956 if Present (Rep) then
7957 Last_Rep := Rep;
7958 end if;
7959 end if;
7960 end loop;
7961
7962 -- Here if we either encountered the parent type's first rep
7963 -- item on the derived type's rep item list (in which case
7964 -- Found is True, and we have nothing else to do), or if we
7965 -- reached the last rep item of the derived type, which is
7966 -- Last_Rep, in which case we further chain the parent type's
7967 -- rep items to those of the derived type.
7968
7969 if not Found then
7970 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
7971 end if;
7972 end;
7973
7974 else
7975 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7976 end if;
7977
7978 case Ekind (Parent_Type) is
7979 when Numeric_Kind =>
7980 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7981
7982 when Array_Kind =>
7983 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
7984
7985 when E_Record_Type
7986 | E_Record_Subtype
7987 | Class_Wide_Kind =>
7988 Build_Derived_Record_Type
7989 (N, Parent_Type, Derived_Type, Derive_Subps);
7990 return;
7991
7992 when Enumeration_Kind =>
7993 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7994
7995 when Access_Kind =>
7996 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7997
7998 when Incomplete_Or_Private_Kind =>
7999 Build_Derived_Private_Type
8000 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
8001
8002 -- For discriminated types, the derivation includes deriving
8003 -- primitive operations. For others it is done below.
8004
8005 if Is_Tagged_Type (Parent_Type)
8006 or else Has_Discriminants (Parent_Type)
8007 or else (Present (Full_View (Parent_Type))
8008 and then Has_Discriminants (Full_View (Parent_Type)))
8009 then
8010 return;
8011 end if;
8012
8013 when Concurrent_Kind =>
8014 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
8015
8016 when others =>
8017 raise Program_Error;
8018 end case;
8019
8020 if Etype (Derived_Type) = Any_Type then
8021 return;
8022 end if;
8023
8024 -- Set delayed freeze and then derive subprograms, we need to do this
8025 -- in this order so that derived subprograms inherit the derived freeze
8026 -- if necessary.
8027
8028 Set_Has_Delayed_Freeze (Derived_Type);
8029 if Derive_Subps then
8030 Derive_Subprograms (Parent_Type, Derived_Type);
8031 end if;
8032
8033 Set_Has_Primitive_Operations
8034 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
8035 end Build_Derived_Type;
8036
8037 -----------------------
8038 -- Build_Discriminal --
8039 -----------------------
8040
8041 procedure Build_Discriminal (Discrim : Entity_Id) is
8042 D_Minal : Entity_Id;
8043 CR_Disc : Entity_Id;
8044
8045 begin
8046 -- A discriminal has the same name as the discriminant
8047
8048 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8049
8050 Set_Ekind (D_Minal, E_In_Parameter);
8051 Set_Mechanism (D_Minal, Default_Mechanism);
8052 Set_Etype (D_Minal, Etype (Discrim));
8053 Set_Scope (D_Minal, Current_Scope);
8054
8055 Set_Discriminal (Discrim, D_Minal);
8056 Set_Discriminal_Link (D_Minal, Discrim);
8057
8058 -- For task types, build at once the discriminants of the corresponding
8059 -- record, which are needed if discriminants are used in entry defaults
8060 -- and in family bounds.
8061
8062 if Is_Concurrent_Type (Current_Scope)
8063 or else Is_Limited_Type (Current_Scope)
8064 then
8065 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8066
8067 Set_Ekind (CR_Disc, E_In_Parameter);
8068 Set_Mechanism (CR_Disc, Default_Mechanism);
8069 Set_Etype (CR_Disc, Etype (Discrim));
8070 Set_Scope (CR_Disc, Current_Scope);
8071 Set_Discriminal_Link (CR_Disc, Discrim);
8072 Set_CR_Discriminant (Discrim, CR_Disc);
8073 end if;
8074 end Build_Discriminal;
8075
8076 ------------------------------------
8077 -- Build_Discriminant_Constraints --
8078 ------------------------------------
8079
8080 function Build_Discriminant_Constraints
8081 (T : Entity_Id;
8082 Def : Node_Id;
8083 Derived_Def : Boolean := False) return Elist_Id
8084 is
8085 C : constant Node_Id := Constraint (Def);
8086 Nb_Discr : constant Nat := Number_Discriminants (T);
8087
8088 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
8089 -- Saves the expression corresponding to a given discriminant in T
8090
8091 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
8092 -- Return the Position number within array Discr_Expr of a discriminant
8093 -- D within the discriminant list of the discriminated type T.
8094
8095 ------------------
8096 -- Pos_Of_Discr --
8097 ------------------
8098
8099 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
8100 Disc : Entity_Id;
8101
8102 begin
8103 Disc := First_Discriminant (T);
8104 for J in Discr_Expr'Range loop
8105 if Disc = D then
8106 return J;
8107 end if;
8108
8109 Next_Discriminant (Disc);
8110 end loop;
8111
8112 -- Note: Since this function is called on discriminants that are
8113 -- known to belong to the discriminated type, falling through the
8114 -- loop with no match signals an internal compiler error.
8115
8116 raise Program_Error;
8117 end Pos_Of_Discr;
8118
8119 -- Declarations local to Build_Discriminant_Constraints
8120
8121 Discr : Entity_Id;
8122 E : Entity_Id;
8123 Elist : constant Elist_Id := New_Elmt_List;
8124
8125 Constr : Node_Id;
8126 Expr : Node_Id;
8127 Id : Node_Id;
8128 Position : Nat;
8129 Found : Boolean;
8130
8131 Discrim_Present : Boolean := False;
8132
8133 -- Start of processing for Build_Discriminant_Constraints
8134
8135 begin
8136 -- The following loop will process positional associations only.
8137 -- For a positional association, the (single) discriminant is
8138 -- implicitly specified by position, in textual order (RM 3.7.2).
8139
8140 Discr := First_Discriminant (T);
8141 Constr := First (Constraints (C));
8142 for D in Discr_Expr'Range loop
8143 exit when Nkind (Constr) = N_Discriminant_Association;
8144
8145 if No (Constr) then
8146 Error_Msg_N ("too few discriminants given in constraint", C);
8147 return New_Elmt_List;
8148
8149 elsif Nkind (Constr) = N_Range
8150 or else (Nkind (Constr) = N_Attribute_Reference
8151 and then
8152 Attribute_Name (Constr) = Name_Range)
8153 then
8154 Error_Msg_N
8155 ("a range is not a valid discriminant constraint", Constr);
8156 Discr_Expr (D) := Error;
8157
8158 else
8159 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
8160 Discr_Expr (D) := Constr;
8161 end if;
8162
8163 Next_Discriminant (Discr);
8164 Next (Constr);
8165 end loop;
8166
8167 if No (Discr) and then Present (Constr) then
8168 Error_Msg_N ("too many discriminants given in constraint", Constr);
8169 return New_Elmt_List;
8170 end if;
8171
8172 -- Named associations can be given in any order, but if both positional
8173 -- and named associations are used in the same discriminant constraint,
8174 -- then positional associations must occur first, at their normal
8175 -- position. Hence once a named association is used, the rest of the
8176 -- discriminant constraint must use only named associations.
8177
8178 while Present (Constr) loop
8179
8180 -- Positional association forbidden after a named association
8181
8182 if Nkind (Constr) /= N_Discriminant_Association then
8183 Error_Msg_N ("positional association follows named one", Constr);
8184 return New_Elmt_List;
8185
8186 -- Otherwise it is a named association
8187
8188 else
8189 -- E records the type of the discriminants in the named
8190 -- association. All the discriminants specified in the same name
8191 -- association must have the same type.
8192
8193 E := Empty;
8194
8195 -- Search the list of discriminants in T to see if the simple name
8196 -- given in the constraint matches any of them.
8197
8198 Id := First (Selector_Names (Constr));
8199 while Present (Id) loop
8200 Found := False;
8201
8202 -- If Original_Discriminant is present, we are processing a
8203 -- generic instantiation and this is an instance node. We need
8204 -- to find the name of the corresponding discriminant in the
8205 -- actual record type T and not the name of the discriminant in
8206 -- the generic formal. Example:
8207
8208 -- generic
8209 -- type G (D : int) is private;
8210 -- package P is
8211 -- subtype W is G (D => 1);
8212 -- end package;
8213 -- type Rec (X : int) is record ... end record;
8214 -- package Q is new P (G => Rec);
8215
8216 -- At the point of the instantiation, formal type G is Rec
8217 -- and therefore when reanalyzing "subtype W is G (D => 1);"
8218 -- which really looks like "subtype W is Rec (D => 1);" at
8219 -- the point of instantiation, we want to find the discriminant
8220 -- that corresponds to D in Rec, i.e. X.
8221
8222 if Present (Original_Discriminant (Id)) then
8223 Discr := Find_Corresponding_Discriminant (Id, T);
8224 Found := True;
8225
8226 else
8227 Discr := First_Discriminant (T);
8228 while Present (Discr) loop
8229 if Chars (Discr) = Chars (Id) then
8230 Found := True;
8231 exit;
8232 end if;
8233
8234 Next_Discriminant (Discr);
8235 end loop;
8236
8237 if not Found then
8238 Error_Msg_N ("& does not match any discriminant", Id);
8239 return New_Elmt_List;
8240
8241 -- The following is only useful for the benefit of generic
8242 -- instances but it does not interfere with other
8243 -- processing for the non-generic case so we do it in all
8244 -- cases (for generics this statement is executed when
8245 -- processing the generic definition, see comment at the
8246 -- beginning of this if statement).
8247
8248 else
8249 Set_Original_Discriminant (Id, Discr);
8250 end if;
8251 end if;
8252
8253 Position := Pos_Of_Discr (T, Discr);
8254
8255 if Present (Discr_Expr (Position)) then
8256 Error_Msg_N ("duplicate constraint for discriminant&", Id);
8257
8258 else
8259 -- Each discriminant specified in the same named association
8260 -- must be associated with a separate copy of the
8261 -- corresponding expression.
8262
8263 if Present (Next (Id)) then
8264 Expr := New_Copy_Tree (Expression (Constr));
8265 Set_Parent (Expr, Parent (Expression (Constr)));
8266 else
8267 Expr := Expression (Constr);
8268 end if;
8269
8270 Discr_Expr (Position) := Expr;
8271 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
8272 end if;
8273
8274 -- A discriminant association with more than one discriminant
8275 -- name is only allowed if the named discriminants are all of
8276 -- the same type (RM 3.7.1(8)).
8277
8278 if E = Empty then
8279 E := Base_Type (Etype (Discr));
8280
8281 elsif Base_Type (Etype (Discr)) /= E then
8282 Error_Msg_N
8283 ("all discriminants in an association " &
8284 "must have the same type", Id);
8285 end if;
8286
8287 Next (Id);
8288 end loop;
8289 end if;
8290
8291 Next (Constr);
8292 end loop;
8293
8294 -- A discriminant constraint must provide exactly one value for each
8295 -- discriminant of the type (RM 3.7.1(8)).
8296
8297 for J in Discr_Expr'Range loop
8298 if No (Discr_Expr (J)) then
8299 Error_Msg_N ("too few discriminants given in constraint", C);
8300 return New_Elmt_List;
8301 end if;
8302 end loop;
8303
8304 -- Determine if there are discriminant expressions in the constraint
8305
8306 for J in Discr_Expr'Range loop
8307 if Denotes_Discriminant
8308 (Discr_Expr (J), Check_Concurrent => True)
8309 then
8310 Discrim_Present := True;
8311 end if;
8312 end loop;
8313
8314 -- Build an element list consisting of the expressions given in the
8315 -- discriminant constraint and apply the appropriate checks. The list
8316 -- is constructed after resolving any named discriminant associations
8317 -- and therefore the expressions appear in the textual order of the
8318 -- discriminants.
8319
8320 Discr := First_Discriminant (T);
8321 for J in Discr_Expr'Range loop
8322 if Discr_Expr (J) /= Error then
8323 Append_Elmt (Discr_Expr (J), Elist);
8324
8325 -- If any of the discriminant constraints is given by a
8326 -- discriminant and we are in a derived type declaration we
8327 -- have a discriminant renaming. Establish link between new
8328 -- and old discriminant.
8329
8330 if Denotes_Discriminant (Discr_Expr (J)) then
8331 if Derived_Def then
8332 Set_Corresponding_Discriminant
8333 (Entity (Discr_Expr (J)), Discr);
8334 end if;
8335
8336 -- Force the evaluation of non-discriminant expressions.
8337 -- If we have found a discriminant in the constraint 3.4(26)
8338 -- and 3.8(18) demand that no range checks are performed are
8339 -- after evaluation. If the constraint is for a component
8340 -- definition that has a per-object constraint, expressions are
8341 -- evaluated but not checked either. In all other cases perform
8342 -- a range check.
8343
8344 else
8345 if Discrim_Present then
8346 null;
8347
8348 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
8349 and then
8350 Has_Per_Object_Constraint
8351 (Defining_Identifier (Parent (Parent (Def))))
8352 then
8353 null;
8354
8355 elsif Is_Access_Type (Etype (Discr)) then
8356 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
8357
8358 else
8359 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
8360 end if;
8361
8362 Force_Evaluation (Discr_Expr (J));
8363 end if;
8364
8365 -- Check that the designated type of an access discriminant's
8366 -- expression is not a class-wide type unless the discriminant's
8367 -- designated type is also class-wide.
8368
8369 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
8370 and then not Is_Class_Wide_Type
8371 (Designated_Type (Etype (Discr)))
8372 and then Etype (Discr_Expr (J)) /= Any_Type
8373 and then Is_Class_Wide_Type
8374 (Designated_Type (Etype (Discr_Expr (J))))
8375 then
8376 Wrong_Type (Discr_Expr (J), Etype (Discr));
8377
8378 elsif Is_Access_Type (Etype (Discr))
8379 and then not Is_Access_Constant (Etype (Discr))
8380 and then Is_Access_Type (Etype (Discr_Expr (J)))
8381 and then Is_Access_Constant (Etype (Discr_Expr (J)))
8382 then
8383 Error_Msg_NE
8384 ("constraint for discriminant& must be access to variable",
8385 Def, Discr);
8386 end if;
8387 end if;
8388
8389 Next_Discriminant (Discr);
8390 end loop;
8391
8392 return Elist;
8393 end Build_Discriminant_Constraints;
8394
8395 ---------------------------------
8396 -- Build_Discriminated_Subtype --
8397 ---------------------------------
8398
8399 procedure Build_Discriminated_Subtype
8400 (T : Entity_Id;
8401 Def_Id : Entity_Id;
8402 Elist : Elist_Id;
8403 Related_Nod : Node_Id;
8404 For_Access : Boolean := False)
8405 is
8406 Has_Discrs : constant Boolean := Has_Discriminants (T);
8407 Constrained : constant Boolean :=
8408 (Has_Discrs
8409 and then not Is_Empty_Elmt_List (Elist)
8410 and then not Is_Class_Wide_Type (T))
8411 or else Is_Constrained (T);
8412
8413 begin
8414 if Ekind (T) = E_Record_Type then
8415 if For_Access then
8416 Set_Ekind (Def_Id, E_Private_Subtype);
8417 Set_Is_For_Access_Subtype (Def_Id, True);
8418 else
8419 Set_Ekind (Def_Id, E_Record_Subtype);
8420 end if;
8421
8422 -- Inherit preelaboration flag from base, for types for which it
8423 -- may have been set: records, private types, protected types.
8424
8425 Set_Known_To_Have_Preelab_Init
8426 (Def_Id, Known_To_Have_Preelab_Init (T));
8427
8428 elsif Ekind (T) = E_Task_Type then
8429 Set_Ekind (Def_Id, E_Task_Subtype);
8430
8431 elsif Ekind (T) = E_Protected_Type then
8432 Set_Ekind (Def_Id, E_Protected_Subtype);
8433 Set_Known_To_Have_Preelab_Init
8434 (Def_Id, Known_To_Have_Preelab_Init (T));
8435
8436 elsif Is_Private_Type (T) then
8437 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
8438 Set_Known_To_Have_Preelab_Init
8439 (Def_Id, Known_To_Have_Preelab_Init (T));
8440
8441 elsif Is_Class_Wide_Type (T) then
8442 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
8443
8444 else
8445 -- Incomplete type. Attach subtype to list of dependents, to be
8446 -- completed with full view of parent type, unless is it the
8447 -- designated subtype of a record component within an init_proc.
8448 -- This last case arises for a component of an access type whose
8449 -- designated type is incomplete (e.g. a Taft Amendment type).
8450 -- The designated subtype is within an inner scope, and needs no
8451 -- elaboration, because only the access type is needed in the
8452 -- initialization procedure.
8453
8454 Set_Ekind (Def_Id, Ekind (T));
8455
8456 if For_Access and then Within_Init_Proc then
8457 null;
8458 else
8459 Append_Elmt (Def_Id, Private_Dependents (T));
8460 end if;
8461 end if;
8462
8463 Set_Etype (Def_Id, T);
8464 Init_Size_Align (Def_Id);
8465 Set_Has_Discriminants (Def_Id, Has_Discrs);
8466 Set_Is_Constrained (Def_Id, Constrained);
8467
8468 Set_First_Entity (Def_Id, First_Entity (T));
8469 Set_Last_Entity (Def_Id, Last_Entity (T));
8470
8471 -- If the subtype is the completion of a private declaration, there may
8472 -- have been representation clauses for the partial view, and they must
8473 -- be preserved. Build_Derived_Type chains the inherited clauses with
8474 -- the ones appearing on the extension. If this comes from a subtype
8475 -- declaration, all clauses are inherited.
8476
8477 if No (First_Rep_Item (Def_Id)) then
8478 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8479 end if;
8480
8481 if Is_Tagged_Type (T) then
8482 Set_Is_Tagged_Type (Def_Id);
8483 Make_Class_Wide_Type (Def_Id);
8484 end if;
8485
8486 Set_Stored_Constraint (Def_Id, No_Elist);
8487
8488 if Has_Discrs then
8489 Set_Discriminant_Constraint (Def_Id, Elist);
8490 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
8491 end if;
8492
8493 if Is_Tagged_Type (T) then
8494
8495 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
8496 -- concurrent record type (which has the list of primitive
8497 -- operations).
8498
8499 if Ada_Version >= Ada_2005
8500 and then Is_Concurrent_Type (T)
8501 then
8502 Set_Corresponding_Record_Type (Def_Id,
8503 Corresponding_Record_Type (T));
8504 else
8505 Set_Direct_Primitive_Operations (Def_Id,
8506 Direct_Primitive_Operations (T));
8507 end if;
8508
8509 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
8510 end if;
8511
8512 -- Subtypes introduced by component declarations do not need to be
8513 -- marked as delayed, and do not get freeze nodes, because the semantics
8514 -- verifies that the parents of the subtypes are frozen before the
8515 -- enclosing record is frozen.
8516
8517 if not Is_Type (Scope (Def_Id)) then
8518 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
8519
8520 if Is_Private_Type (T)
8521 and then Present (Full_View (T))
8522 then
8523 Conditional_Delay (Def_Id, Full_View (T));
8524 else
8525 Conditional_Delay (Def_Id, T);
8526 end if;
8527 end if;
8528
8529 if Is_Record_Type (T) then
8530 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
8531
8532 if Has_Discrs
8533 and then not Is_Empty_Elmt_List (Elist)
8534 and then not For_Access
8535 then
8536 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
8537 elsif not For_Access then
8538 Set_Cloned_Subtype (Def_Id, T);
8539 end if;
8540 end if;
8541 end Build_Discriminated_Subtype;
8542
8543 ---------------------------
8544 -- Build_Itype_Reference --
8545 ---------------------------
8546
8547 procedure Build_Itype_Reference
8548 (Ityp : Entity_Id;
8549 Nod : Node_Id)
8550 is
8551 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
8552 begin
8553 Set_Itype (IR, Ityp);
8554 Insert_After (Nod, IR);
8555 end Build_Itype_Reference;
8556
8557 ------------------------
8558 -- Build_Scalar_Bound --
8559 ------------------------
8560
8561 function Build_Scalar_Bound
8562 (Bound : Node_Id;
8563 Par_T : Entity_Id;
8564 Der_T : Entity_Id) return Node_Id
8565 is
8566 New_Bound : Entity_Id;
8567
8568 begin
8569 -- Note: not clear why this is needed, how can the original bound
8570 -- be unanalyzed at this point? and if it is, what business do we
8571 -- have messing around with it? and why is the base type of the
8572 -- parent type the right type for the resolution. It probably is
8573 -- not! It is OK for the new bound we are creating, but not for
8574 -- the old one??? Still if it never happens, no problem!
8575
8576 Analyze_And_Resolve (Bound, Base_Type (Par_T));
8577
8578 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
8579 New_Bound := New_Copy (Bound);
8580 Set_Etype (New_Bound, Der_T);
8581 Set_Analyzed (New_Bound);
8582
8583 elsif Is_Entity_Name (Bound) then
8584 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
8585
8586 -- The following is almost certainly wrong. What business do we have
8587 -- relocating a node (Bound) that is presumably still attached to
8588 -- the tree elsewhere???
8589
8590 else
8591 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
8592 end if;
8593
8594 Set_Etype (New_Bound, Der_T);
8595 return New_Bound;
8596 end Build_Scalar_Bound;
8597
8598 --------------------------------
8599 -- Build_Underlying_Full_View --
8600 --------------------------------
8601
8602 procedure Build_Underlying_Full_View
8603 (N : Node_Id;
8604 Typ : Entity_Id;
8605 Par : Entity_Id)
8606 is
8607 Loc : constant Source_Ptr := Sloc (N);
8608 Subt : constant Entity_Id :=
8609 Make_Defining_Identifier
8610 (Loc, New_External_Name (Chars (Typ), 'S'));
8611
8612 Constr : Node_Id;
8613 Indic : Node_Id;
8614 C : Node_Id;
8615 Id : Node_Id;
8616
8617 procedure Set_Discriminant_Name (Id : Node_Id);
8618 -- If the derived type has discriminants, they may rename discriminants
8619 -- of the parent. When building the full view of the parent, we need to
8620 -- recover the names of the original discriminants if the constraint is
8621 -- given by named associations.
8622
8623 ---------------------------
8624 -- Set_Discriminant_Name --
8625 ---------------------------
8626
8627 procedure Set_Discriminant_Name (Id : Node_Id) is
8628 Disc : Entity_Id;
8629
8630 begin
8631 Set_Original_Discriminant (Id, Empty);
8632
8633 if Has_Discriminants (Typ) then
8634 Disc := First_Discriminant (Typ);
8635 while Present (Disc) loop
8636 if Chars (Disc) = Chars (Id)
8637 and then Present (Corresponding_Discriminant (Disc))
8638 then
8639 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
8640 end if;
8641 Next_Discriminant (Disc);
8642 end loop;
8643 end if;
8644 end Set_Discriminant_Name;
8645
8646 -- Start of processing for Build_Underlying_Full_View
8647
8648 begin
8649 if Nkind (N) = N_Full_Type_Declaration then
8650 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
8651
8652 elsif Nkind (N) = N_Subtype_Declaration then
8653 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
8654
8655 elsif Nkind (N) = N_Component_Declaration then
8656 Constr :=
8657 New_Copy_Tree
8658 (Constraint (Subtype_Indication (Component_Definition (N))));
8659
8660 else
8661 raise Program_Error;
8662 end if;
8663
8664 C := First (Constraints (Constr));
8665 while Present (C) loop
8666 if Nkind (C) = N_Discriminant_Association then
8667 Id := First (Selector_Names (C));
8668 while Present (Id) loop
8669 Set_Discriminant_Name (Id);
8670 Next (Id);
8671 end loop;
8672 end if;
8673
8674 Next (C);
8675 end loop;
8676
8677 Indic :=
8678 Make_Subtype_Declaration (Loc,
8679 Defining_Identifier => Subt,
8680 Subtype_Indication =>
8681 Make_Subtype_Indication (Loc,
8682 Subtype_Mark => New_Reference_To (Par, Loc),
8683 Constraint => New_Copy_Tree (Constr)));
8684
8685 -- If this is a component subtype for an outer itype, it is not
8686 -- a list member, so simply set the parent link for analysis: if
8687 -- the enclosing type does not need to be in a declarative list,
8688 -- neither do the components.
8689
8690 if Is_List_Member (N)
8691 and then Nkind (N) /= N_Component_Declaration
8692 then
8693 Insert_Before (N, Indic);
8694 else
8695 Set_Parent (Indic, Parent (N));
8696 end if;
8697
8698 Analyze (Indic);
8699 Set_Underlying_Full_View (Typ, Full_View (Subt));
8700 end Build_Underlying_Full_View;
8701
8702 -------------------------------
8703 -- Check_Abstract_Overriding --
8704 -------------------------------
8705
8706 procedure Check_Abstract_Overriding (T : Entity_Id) is
8707 Alias_Subp : Entity_Id;
8708 Elmt : Elmt_Id;
8709 Op_List : Elist_Id;
8710 Subp : Entity_Id;
8711 Type_Def : Node_Id;
8712
8713 procedure Check_Pragma_Implemented (Subp : Entity_Id);
8714 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
8715 -- which has pragma Implemented already set. Check whether Subp's entity
8716 -- kind conforms to the implementation kind of the overridden routine.
8717
8718 procedure Check_Pragma_Implemented
8719 (Subp : Entity_Id;
8720 Iface_Subp : Entity_Id);
8721 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
8722 -- Iface_Subp and both entities have pragma Implemented already set on
8723 -- them. Check whether the two implementation kinds are conforming.
8724
8725 procedure Inherit_Pragma_Implemented
8726 (Subp : Entity_Id;
8727 Iface_Subp : Entity_Id);
8728 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
8729 -- subprogram Iface_Subp which has been marked by pragma Implemented.
8730 -- Propagate the implementation kind of Iface_Subp to Subp.
8731
8732 ------------------------------
8733 -- Check_Pragma_Implemented --
8734 ------------------------------
8735
8736 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
8737 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
8738 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
8739 Contr_Typ : Entity_Id;
8740
8741 begin
8742 -- Subp must have an alias since it is a hidden entity used to link
8743 -- an interface subprogram to its overriding counterpart.
8744
8745 pragma Assert (Present (Alias (Subp)));
8746
8747 -- Extract the type of the controlling formal
8748
8749 Contr_Typ := Etype (First_Formal (Alias (Subp)));
8750
8751 if Is_Concurrent_Record_Type (Contr_Typ) then
8752 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
8753 end if;
8754
8755 -- An interface subprogram whose implementation kind is By_Entry must
8756 -- be implemented by an entry.
8757
8758 if Impl_Kind = Name_By_Entry
8759 and then Ekind (Wrapped_Entity (Alias (Subp))) /= E_Entry
8760 then
8761 Error_Msg_Node_2 := Iface_Alias;
8762 Error_Msg_NE
8763 ("type & must implement abstract subprogram & with an entry",
8764 Alias (Subp), Contr_Typ);
8765
8766 elsif Impl_Kind = Name_By_Protected_Procedure then
8767
8768 -- An interface subprogram whose implementation kind is By_
8769 -- Protected_Procedure cannot be implemented by a primitive
8770 -- procedure of a task type.
8771
8772 if Ekind (Contr_Typ) /= E_Protected_Type then
8773 Error_Msg_Node_2 := Contr_Typ;
8774 Error_Msg_NE
8775 ("interface subprogram & cannot be implemented by a " &
8776 "primitive procedure of task type &", Alias (Subp),
8777 Iface_Alias);
8778
8779 -- An interface subprogram whose implementation kind is By_
8780 -- Protected_Procedure must be implemented by a procedure.
8781
8782 elsif Is_Primitive_Wrapper (Alias (Subp))
8783 and then Ekind (Wrapped_Entity (Alias (Subp))) /= E_Procedure
8784 then
8785 Error_Msg_Node_2 := Iface_Alias;
8786 Error_Msg_NE
8787 ("type & must implement abstract subprogram & with a " &
8788 "procedure", Alias (Subp), Contr_Typ);
8789 end if;
8790 end if;
8791 end Check_Pragma_Implemented;
8792
8793 ------------------------------
8794 -- Check_Pragma_Implemented --
8795 ------------------------------
8796
8797 procedure Check_Pragma_Implemented
8798 (Subp : Entity_Id;
8799 Iface_Subp : Entity_Id)
8800 is
8801 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8802 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
8803
8804 begin
8805 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
8806 -- and overriding subprogram are different. In general this is an
8807 -- error except when the implementation kind of the overridden
8808 -- subprograms is By_Any.
8809
8810 if Iface_Kind /= Subp_Kind
8811 and then Iface_Kind /= Name_By_Any
8812 then
8813 if Iface_Kind = Name_By_Entry then
8814 Error_Msg_N
8815 ("incompatible implementation kind, overridden subprogram " &
8816 "is marked By_Entry", Subp);
8817 else
8818 Error_Msg_N
8819 ("incompatible implementation kind, overridden subprogram " &
8820 "is marked By_Protected_Procedure", Subp);
8821 end if;
8822 end if;
8823 end Check_Pragma_Implemented;
8824
8825 --------------------------------
8826 -- Inherit_Pragma_Implemented --
8827 --------------------------------
8828
8829 procedure Inherit_Pragma_Implemented
8830 (Subp : Entity_Id;
8831 Iface_Subp : Entity_Id)
8832 is
8833 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8834 Loc : constant Source_Ptr := Sloc (Subp);
8835 Impl_Prag : Node_Id;
8836
8837 begin
8838 -- Since the implementation kind is stored as a representation item
8839 -- rather than a flag, create a pragma node.
8840
8841 Impl_Prag :=
8842 Make_Pragma (Loc,
8843 Chars => Name_Implemented,
8844 Pragma_Argument_Associations => New_List (
8845 Make_Pragma_Argument_Association (Loc,
8846 Expression =>
8847 New_Reference_To (Subp, Loc)),
8848
8849 Make_Pragma_Argument_Association (Loc,
8850 Expression => Make_Identifier (Loc, Iface_Kind))));
8851
8852 -- The pragma doesn't need to be analyzed because it is internally
8853 -- build. It is safe to directly register it as a rep item since we
8854 -- are only interested in the characters of the implementation kind.
8855
8856 Record_Rep_Item (Subp, Impl_Prag);
8857 end Inherit_Pragma_Implemented;
8858
8859 -- Start of processing for Check_Abstract_Overriding
8860
8861 begin
8862 Op_List := Primitive_Operations (T);
8863
8864 -- Loop to check primitive operations
8865
8866 Elmt := First_Elmt (Op_List);
8867 while Present (Elmt) loop
8868 Subp := Node (Elmt);
8869 Alias_Subp := Alias (Subp);
8870
8871 -- Inherited subprograms are identified by the fact that they do not
8872 -- come from source, and the associated source location is the
8873 -- location of the first subtype of the derived type.
8874
8875 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
8876 -- subprograms that "require overriding".
8877
8878 -- Special exception, do not complain about failure to override the
8879 -- stream routines _Input and _Output, as well as the primitive
8880 -- operations used in dispatching selects since we always provide
8881 -- automatic overridings for these subprograms.
8882
8883 -- Also ignore this rule for convention CIL since .NET libraries
8884 -- do bizarre things with interfaces???
8885
8886 -- The partial view of T may have been a private extension, for
8887 -- which inherited functions dispatching on result are abstract.
8888 -- If the full view is a null extension, there is no need for
8889 -- overriding in Ada2005, but wrappers need to be built for them
8890 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
8891
8892 if Is_Null_Extension (T)
8893 and then Has_Controlling_Result (Subp)
8894 and then Ada_Version >= Ada_2005
8895 and then Present (Alias_Subp)
8896 and then not Comes_From_Source (Subp)
8897 and then not Is_Abstract_Subprogram (Alias_Subp)
8898 and then not Is_Access_Type (Etype (Subp))
8899 then
8900 null;
8901
8902 -- Ada 2005 (AI-251): Internal entities of interfaces need no
8903 -- processing because this check is done with the aliased
8904 -- entity
8905
8906 elsif Present (Interface_Alias (Subp)) then
8907 null;
8908
8909 elsif (Is_Abstract_Subprogram (Subp)
8910 or else Requires_Overriding (Subp)
8911 or else
8912 (Has_Controlling_Result (Subp)
8913 and then Present (Alias_Subp)
8914 and then not Comes_From_Source (Subp)
8915 and then Sloc (Subp) = Sloc (First_Subtype (T))))
8916 and then not Is_TSS (Subp, TSS_Stream_Input)
8917 and then not Is_TSS (Subp, TSS_Stream_Output)
8918 and then not Is_Abstract_Type (T)
8919 and then Convention (T) /= Convention_CIL
8920 and then not Is_Predefined_Interface_Primitive (Subp)
8921
8922 -- Ada 2005 (AI-251): Do not consider hidden entities associated
8923 -- with abstract interface types because the check will be done
8924 -- with the aliased entity (otherwise we generate a duplicated
8925 -- error message).
8926
8927 and then not Present (Interface_Alias (Subp))
8928 then
8929 if Present (Alias_Subp) then
8930
8931 -- Only perform the check for a derived subprogram when the
8932 -- type has an explicit record extension. This avoids incorrect
8933 -- flagging of abstract subprograms for the case of a type
8934 -- without an extension that is derived from a formal type
8935 -- with a tagged actual (can occur within a private part).
8936
8937 -- Ada 2005 (AI-391): In the case of an inherited function with
8938 -- a controlling result of the type, the rule does not apply if
8939 -- the type is a null extension (unless the parent function
8940 -- itself is abstract, in which case the function must still be
8941 -- be overridden). The expander will generate an overriding
8942 -- wrapper function calling the parent subprogram (see
8943 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
8944
8945 Type_Def := Type_Definition (Parent (T));
8946
8947 if Nkind (Type_Def) = N_Derived_Type_Definition
8948 and then Present (Record_Extension_Part (Type_Def))
8949 and then
8950 (Ada_Version < Ada_2005
8951 or else not Is_Null_Extension (T)
8952 or else Ekind (Subp) = E_Procedure
8953 or else not Has_Controlling_Result (Subp)
8954 or else Is_Abstract_Subprogram (Alias_Subp)
8955 or else Requires_Overriding (Subp)
8956 or else Is_Access_Type (Etype (Subp)))
8957 then
8958 -- Avoid reporting error in case of abstract predefined
8959 -- primitive inherited from interface type because the
8960 -- body of internally generated predefined primitives
8961 -- of tagged types are generated later by Freeze_Type
8962
8963 if Is_Interface (Root_Type (T))
8964 and then Is_Abstract_Subprogram (Subp)
8965 and then Is_Predefined_Dispatching_Operation (Subp)
8966 and then not Comes_From_Source (Ultimate_Alias (Subp))
8967 then
8968 null;
8969
8970 else
8971 Error_Msg_NE
8972 ("type must be declared abstract or & overridden",
8973 T, Subp);
8974
8975 -- Traverse the whole chain of aliased subprograms to
8976 -- complete the error notification. This is especially
8977 -- useful for traceability of the chain of entities when
8978 -- the subprogram corresponds with an interface
8979 -- subprogram (which may be defined in another package).
8980
8981 if Present (Alias_Subp) then
8982 declare
8983 E : Entity_Id;
8984
8985 begin
8986 E := Subp;
8987 while Present (Alias (E)) loop
8988 Error_Msg_Sloc := Sloc (E);
8989 Error_Msg_NE
8990 ("\& has been inherited #", T, Subp);
8991 E := Alias (E);
8992 end loop;
8993
8994 Error_Msg_Sloc := Sloc (E);
8995 Error_Msg_NE
8996 ("\& has been inherited from subprogram #",
8997 T, Subp);
8998 end;
8999 end if;
9000 end if;
9001
9002 -- Ada 2005 (AI-345): Protected or task type implementing
9003 -- abstract interfaces.
9004
9005 elsif Is_Concurrent_Record_Type (T)
9006 and then Present (Interfaces (T))
9007 then
9008 -- The controlling formal of Subp must be of mode "out",
9009 -- "in out" or an access-to-variable to be overridden.
9010
9011 -- Error message below needs rewording (remember comma
9012 -- in -gnatj mode) ???
9013
9014 if Ekind (First_Formal (Subp)) = E_In_Parameter
9015 and then Ekind (Subp) /= E_Function
9016 then
9017 if not Is_Predefined_Dispatching_Operation (Subp) then
9018 Error_Msg_NE
9019 ("first formal of & must be of mode `OUT`, " &
9020 "`IN OUT` or access-to-variable", T, Subp);
9021 Error_Msg_N
9022 ("\to be overridden by protected procedure or " &
9023 "entry (RM 9.4(11.9/2))", T);
9024 end if;
9025
9026 -- Some other kind of overriding failure
9027
9028 else
9029 Error_Msg_NE
9030 ("interface subprogram & must be overridden",
9031 T, Subp);
9032
9033 -- Examine primitive operations of synchronized type,
9034 -- to find homonyms that have the wrong profile.
9035
9036 declare
9037 Prim : Entity_Id;
9038
9039 begin
9040 Prim :=
9041 First_Entity (Corresponding_Concurrent_Type (T));
9042 while Present (Prim) loop
9043 if Chars (Prim) = Chars (Subp) then
9044 Error_Msg_NE
9045 ("profile is not type conformant with "
9046 & "prefixed view profile of "
9047 & "inherited operation&", Prim, Subp);
9048 end if;
9049
9050 Next_Entity (Prim);
9051 end loop;
9052 end;
9053 end if;
9054 end if;
9055
9056 else
9057 Error_Msg_Node_2 := T;
9058 Error_Msg_N
9059 ("abstract subprogram& not allowed for type&", Subp);
9060
9061 -- Also post unconditional warning on the type (unconditional
9062 -- so that if there are more than one of these cases, we get
9063 -- them all, and not just the first one).
9064
9065 Error_Msg_Node_2 := Subp;
9066 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
9067 end if;
9068 end if;
9069
9070 -- Ada 2012 (AI05-0030): Perform some checks related to pragma
9071 -- Implemented
9072
9073 -- Subp is an expander-generated procedure which maps an interface
9074 -- alias to a protected wrapper. The interface alias is flagged by
9075 -- pragma Implemented. Ensure that Subp is a procedure when the
9076 -- implementation kind is By_Protected_Procedure or an entry when
9077 -- By_Entry.
9078
9079 if Ada_Version >= Ada_2012
9080 and then Is_Hidden (Subp)
9081 and then Present (Interface_Alias (Subp))
9082 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
9083 then
9084 Check_Pragma_Implemented (Subp);
9085 end if;
9086
9087 -- Subp is an interface primitive which overrides another interface
9088 -- primitive marked with pragma Implemented.
9089
9090 if Ada_Version >= Ada_2012
9091 and then Present (Overridden_Operation (Subp))
9092 and then Has_Rep_Pragma
9093 (Overridden_Operation (Subp), Name_Implemented)
9094 then
9095 -- If the overriding routine is also marked by Implemented, check
9096 -- that the two implementation kinds are conforming.
9097
9098 if Has_Rep_Pragma (Subp, Name_Implemented) then
9099 Check_Pragma_Implemented
9100 (Subp => Subp,
9101 Iface_Subp => Overridden_Operation (Subp));
9102
9103 -- Otherwise the overriding routine inherits the implementation
9104 -- kind from the overridden subprogram.
9105
9106 else
9107 Inherit_Pragma_Implemented
9108 (Subp => Subp,
9109 Iface_Subp => Overridden_Operation (Subp));
9110 end if;
9111 end if;
9112
9113 Next_Elmt (Elmt);
9114 end loop;
9115 end Check_Abstract_Overriding;
9116
9117 ------------------------------------------------
9118 -- Check_Access_Discriminant_Requires_Limited --
9119 ------------------------------------------------
9120
9121 procedure Check_Access_Discriminant_Requires_Limited
9122 (D : Node_Id;
9123 Loc : Node_Id)
9124 is
9125 begin
9126 -- A discriminant_specification for an access discriminant shall appear
9127 -- only in the declaration for a task or protected type, or for a type
9128 -- with the reserved word 'limited' in its definition or in one of its
9129 -- ancestors (RM 3.7(10)).
9130
9131 -- AI-0063: The proper condition is that type must be immutably limited,
9132 -- or else be a partial view.
9133
9134 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
9135 if Is_Immutably_Limited_Type (Current_Scope)
9136 or else
9137 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
9138 and then Limited_Present (Parent (Current_Scope)))
9139 then
9140 null;
9141
9142 else
9143 Error_Msg_N
9144 ("access discriminants allowed only for limited types", Loc);
9145 end if;
9146 end if;
9147 end Check_Access_Discriminant_Requires_Limited;
9148
9149 -----------------------------------
9150 -- Check_Aliased_Component_Types --
9151 -----------------------------------
9152
9153 procedure Check_Aliased_Component_Types (T : Entity_Id) is
9154 C : Entity_Id;
9155
9156 begin
9157 -- ??? Also need to check components of record extensions, but not
9158 -- components of protected types (which are always limited).
9159
9160 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
9161 -- types to be unconstrained. This is safe because it is illegal to
9162 -- create access subtypes to such types with explicit discriminant
9163 -- constraints.
9164
9165 if not Is_Limited_Type (T) then
9166 if Ekind (T) = E_Record_Type then
9167 C := First_Component (T);
9168 while Present (C) loop
9169 if Is_Aliased (C)
9170 and then Has_Discriminants (Etype (C))
9171 and then not Is_Constrained (Etype (C))
9172 and then not In_Instance_Body
9173 and then Ada_Version < Ada_2005
9174 then
9175 Error_Msg_N
9176 ("aliased component must be constrained (RM 3.6(11))",
9177 C);
9178 end if;
9179
9180 Next_Component (C);
9181 end loop;
9182
9183 elsif Ekind (T) = E_Array_Type then
9184 if Has_Aliased_Components (T)
9185 and then Has_Discriminants (Component_Type (T))
9186 and then not Is_Constrained (Component_Type (T))
9187 and then not In_Instance_Body
9188 and then Ada_Version < Ada_2005
9189 then
9190 Error_Msg_N
9191 ("aliased component type must be constrained (RM 3.6(11))",
9192 T);
9193 end if;
9194 end if;
9195 end if;
9196 end Check_Aliased_Component_Types;
9197
9198 ----------------------
9199 -- Check_Completion --
9200 ----------------------
9201
9202 procedure Check_Completion (Body_Id : Node_Id := Empty) is
9203 E : Entity_Id;
9204
9205 procedure Post_Error;
9206 -- Post error message for lack of completion for entity E
9207
9208 ----------------
9209 -- Post_Error --
9210 ----------------
9211
9212 procedure Post_Error is
9213
9214 procedure Missing_Body;
9215 -- Output missing body message
9216
9217 ------------------
9218 -- Missing_Body --
9219 ------------------
9220
9221 procedure Missing_Body is
9222 begin
9223 -- Spec is in same unit, so we can post on spec
9224
9225 if In_Same_Source_Unit (Body_Id, E) then
9226 Error_Msg_N ("missing body for &", E);
9227
9228 -- Spec is in a separate unit, so we have to post on the body
9229
9230 else
9231 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
9232 end if;
9233 end Missing_Body;
9234
9235 -- Start of processing for Post_Error
9236
9237 begin
9238 if not Comes_From_Source (E) then
9239
9240 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
9241 -- It may be an anonymous protected type created for a
9242 -- single variable. Post error on variable, if present.
9243
9244 declare
9245 Var : Entity_Id;
9246
9247 begin
9248 Var := First_Entity (Current_Scope);
9249 while Present (Var) loop
9250 exit when Etype (Var) = E
9251 and then Comes_From_Source (Var);
9252
9253 Next_Entity (Var);
9254 end loop;
9255
9256 if Present (Var) then
9257 E := Var;
9258 end if;
9259 end;
9260 end if;
9261 end if;
9262
9263 -- If a generated entity has no completion, then either previous
9264 -- semantic errors have disabled the expansion phase, or else we had
9265 -- missing subunits, or else we are compiling without expansion,
9266 -- or else something is very wrong.
9267
9268 if not Comes_From_Source (E) then
9269 pragma Assert
9270 (Serious_Errors_Detected > 0
9271 or else Configurable_Run_Time_Violations > 0
9272 or else Subunits_Missing
9273 or else not Expander_Active);
9274 return;
9275
9276 -- Here for source entity
9277
9278 else
9279 -- Here if no body to post the error message, so we post the error
9280 -- on the declaration that has no completion. This is not really
9281 -- the right place to post it, think about this later ???
9282
9283 if No (Body_Id) then
9284 if Is_Type (E) then
9285 Error_Msg_NE
9286 ("missing full declaration for }", Parent (E), E);
9287 else
9288 Error_Msg_NE ("missing body for &", Parent (E), E);
9289 end if;
9290
9291 -- Package body has no completion for a declaration that appears
9292 -- in the corresponding spec. Post error on the body, with a
9293 -- reference to the non-completed declaration.
9294
9295 else
9296 Error_Msg_Sloc := Sloc (E);
9297
9298 if Is_Type (E) then
9299 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
9300
9301 elsif Is_Overloadable (E)
9302 and then Current_Entity_In_Scope (E) /= E
9303 then
9304 -- It may be that the completion is mistyped and appears as
9305 -- a distinct overloading of the entity.
9306
9307 declare
9308 Candidate : constant Entity_Id :=
9309 Current_Entity_In_Scope (E);
9310 Decl : constant Node_Id :=
9311 Unit_Declaration_Node (Candidate);
9312
9313 begin
9314 if Is_Overloadable (Candidate)
9315 and then Ekind (Candidate) = Ekind (E)
9316 and then Nkind (Decl) = N_Subprogram_Body
9317 and then Acts_As_Spec (Decl)
9318 then
9319 Check_Type_Conformant (Candidate, E);
9320
9321 else
9322 Missing_Body;
9323 end if;
9324 end;
9325
9326 else
9327 Missing_Body;
9328 end if;
9329 end if;
9330 end if;
9331 end Post_Error;
9332
9333 -- Start of processing for Check_Completion
9334
9335 begin
9336 E := First_Entity (Current_Scope);
9337 while Present (E) loop
9338 if Is_Intrinsic_Subprogram (E) then
9339 null;
9340
9341 -- The following situation requires special handling: a child unit
9342 -- that appears in the context clause of the body of its parent:
9343
9344 -- procedure Parent.Child (...);
9345
9346 -- with Parent.Child;
9347 -- package body Parent is
9348
9349 -- Here Parent.Child appears as a local entity, but should not be
9350 -- flagged as requiring completion, because it is a compilation
9351 -- unit.
9352
9353 -- Ignore missing completion for a subprogram that does not come from
9354 -- source (including the _Call primitive operation of RAS types,
9355 -- which has to have the flag Comes_From_Source for other purposes):
9356 -- we assume that the expander will provide the missing completion.
9357 -- In case of previous errors, other expansion actions that provide
9358 -- bodies for null procedures with not be invoked, so inhibit message
9359 -- in those cases.
9360 -- Note that E_Operator is not in the list that follows, because
9361 -- this kind is reserved for predefined operators, that are
9362 -- intrinsic and do not need completion.
9363
9364 elsif Ekind (E) = E_Function
9365 or else Ekind (E) = E_Procedure
9366 or else Ekind (E) = E_Generic_Function
9367 or else Ekind (E) = E_Generic_Procedure
9368 then
9369 if Has_Completion (E) then
9370 null;
9371
9372 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
9373 null;
9374
9375 elsif Is_Subprogram (E)
9376 and then (not Comes_From_Source (E)
9377 or else Chars (E) = Name_uCall)
9378 then
9379 null;
9380
9381 elsif
9382 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9383 then
9384 null;
9385
9386 elsif Nkind (Parent (E)) = N_Procedure_Specification
9387 and then Null_Present (Parent (E))
9388 and then Serious_Errors_Detected > 0
9389 then
9390 null;
9391
9392 else
9393 Post_Error;
9394 end if;
9395
9396 elsif Is_Entry (E) then
9397 if not Has_Completion (E) and then
9398 (Ekind (Scope (E)) = E_Protected_Object
9399 or else Ekind (Scope (E)) = E_Protected_Type)
9400 then
9401 Post_Error;
9402 end if;
9403
9404 elsif Is_Package_Or_Generic_Package (E) then
9405 if Unit_Requires_Body (E) then
9406 if not Has_Completion (E)
9407 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
9408 N_Compilation_Unit
9409 then
9410 Post_Error;
9411 end if;
9412
9413 elsif not Is_Child_Unit (E) then
9414 May_Need_Implicit_Body (E);
9415 end if;
9416
9417 elsif Ekind (E) = E_Incomplete_Type
9418 and then No (Underlying_Type (E))
9419 then
9420 Post_Error;
9421
9422 elsif (Ekind (E) = E_Task_Type or else
9423 Ekind (E) = E_Protected_Type)
9424 and then not Has_Completion (E)
9425 then
9426 Post_Error;
9427
9428 -- A single task declared in the current scope is a constant, verify
9429 -- that the body of its anonymous type is in the same scope. If the
9430 -- task is defined elsewhere, this may be a renaming declaration for
9431 -- which no completion is needed.
9432
9433 elsif Ekind (E) = E_Constant
9434 and then Ekind (Etype (E)) = E_Task_Type
9435 and then not Has_Completion (Etype (E))
9436 and then Scope (Etype (E)) = Current_Scope
9437 then
9438 Post_Error;
9439
9440 elsif Ekind (E) = E_Protected_Object
9441 and then not Has_Completion (Etype (E))
9442 then
9443 Post_Error;
9444
9445 elsif Ekind (E) = E_Record_Type then
9446 if Is_Tagged_Type (E) then
9447 Check_Abstract_Overriding (E);
9448 Check_Conventions (E);
9449 end if;
9450
9451 Check_Aliased_Component_Types (E);
9452
9453 elsif Ekind (E) = E_Array_Type then
9454 Check_Aliased_Component_Types (E);
9455
9456 end if;
9457
9458 Next_Entity (E);
9459 end loop;
9460 end Check_Completion;
9461
9462 ----------------------------
9463 -- Check_Delta_Expression --
9464 ----------------------------
9465
9466 procedure Check_Delta_Expression (E : Node_Id) is
9467 begin
9468 if not (Is_Real_Type (Etype (E))) then
9469 Wrong_Type (E, Any_Real);
9470
9471 elsif not Is_OK_Static_Expression (E) then
9472 Flag_Non_Static_Expr
9473 ("non-static expression used for delta value!", E);
9474
9475 elsif not UR_Is_Positive (Expr_Value_R (E)) then
9476 Error_Msg_N ("delta expression must be positive", E);
9477
9478 else
9479 return;
9480 end if;
9481
9482 -- If any of above errors occurred, then replace the incorrect
9483 -- expression by the real 0.1, which should prevent further errors.
9484
9485 Rewrite (E,
9486 Make_Real_Literal (Sloc (E), Ureal_Tenth));
9487 Analyze_And_Resolve (E, Standard_Float);
9488 end Check_Delta_Expression;
9489
9490 -----------------------------
9491 -- Check_Digits_Expression --
9492 -----------------------------
9493
9494 procedure Check_Digits_Expression (E : Node_Id) is
9495 begin
9496 if not (Is_Integer_Type (Etype (E))) then
9497 Wrong_Type (E, Any_Integer);
9498
9499 elsif not Is_OK_Static_Expression (E) then
9500 Flag_Non_Static_Expr
9501 ("non-static expression used for digits value!", E);
9502
9503 elsif Expr_Value (E) <= 0 then
9504 Error_Msg_N ("digits value must be greater than zero", E);
9505
9506 else
9507 return;
9508 end if;
9509
9510 -- If any of above errors occurred, then replace the incorrect
9511 -- expression by the integer 1, which should prevent further errors.
9512
9513 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
9514 Analyze_And_Resolve (E, Standard_Integer);
9515
9516 end Check_Digits_Expression;
9517
9518 --------------------------
9519 -- Check_Initialization --
9520 --------------------------
9521
9522 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
9523 begin
9524 if Is_Limited_Type (T)
9525 and then not In_Instance
9526 and then not In_Inlined_Body
9527 then
9528 if not OK_For_Limited_Init (T, Exp) then
9529
9530 -- In GNAT mode, this is just a warning, to allow it to be evilly
9531 -- turned off. Otherwise it is a real error.
9532
9533 if GNAT_Mode then
9534 Error_Msg_N
9535 ("?cannot initialize entities of limited type!", Exp);
9536
9537 elsif Ada_Version < Ada_2005 then
9538 Error_Msg_N
9539 ("cannot initialize entities of limited type", Exp);
9540 Explain_Limited_Type (T, Exp);
9541
9542 else
9543 -- Specialize error message according to kind of illegal
9544 -- initial expression.
9545
9546 if Nkind (Exp) = N_Type_Conversion
9547 and then Nkind (Expression (Exp)) = N_Function_Call
9548 then
9549 Error_Msg_N
9550 ("illegal context for call"
9551 & " to function with limited result", Exp);
9552
9553 else
9554 Error_Msg_N
9555 ("initialization of limited object requires aggregate "
9556 & "or function call", Exp);
9557 end if;
9558 end if;
9559 end if;
9560 end if;
9561 end Check_Initialization;
9562
9563 ----------------------
9564 -- Check_Interfaces --
9565 ----------------------
9566
9567 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
9568 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
9569
9570 Iface : Node_Id;
9571 Iface_Def : Node_Id;
9572 Iface_Typ : Entity_Id;
9573 Parent_Node : Node_Id;
9574
9575 Is_Task : Boolean := False;
9576 -- Set True if parent type or any progenitor is a task interface
9577
9578 Is_Protected : Boolean := False;
9579 -- Set True if parent type or any progenitor is a protected interface
9580
9581 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
9582 -- Check that a progenitor is compatible with declaration.
9583 -- Error is posted on Error_Node.
9584
9585 ------------------
9586 -- Check_Ifaces --
9587 ------------------
9588
9589 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
9590 Iface_Id : constant Entity_Id :=
9591 Defining_Identifier (Parent (Iface_Def));
9592 Type_Def : Node_Id;
9593
9594 begin
9595 if Nkind (N) = N_Private_Extension_Declaration then
9596 Type_Def := N;
9597 else
9598 Type_Def := Type_Definition (N);
9599 end if;
9600
9601 if Is_Task_Interface (Iface_Id) then
9602 Is_Task := True;
9603
9604 elsif Is_Protected_Interface (Iface_Id) then
9605 Is_Protected := True;
9606 end if;
9607
9608 if Is_Synchronized_Interface (Iface_Id) then
9609
9610 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
9611 -- extension derived from a synchronized interface must explicitly
9612 -- be declared synchronized, because the full view will be a
9613 -- synchronized type.
9614
9615 if Nkind (N) = N_Private_Extension_Declaration then
9616 if not Synchronized_Present (N) then
9617 Error_Msg_NE
9618 ("private extension of& must be explicitly synchronized",
9619 N, Iface_Id);
9620 end if;
9621
9622 -- However, by 3.9.4(16/2), a full type that is a record extension
9623 -- is never allowed to derive from a synchronized interface (note
9624 -- that interfaces must be excluded from this check, because those
9625 -- are represented by derived type definitions in some cases).
9626
9627 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9628 and then not Interface_Present (Type_Definition (N))
9629 then
9630 Error_Msg_N ("record extension cannot derive from synchronized"
9631 & " interface", Error_Node);
9632 end if;
9633 end if;
9634
9635 -- Check that the characteristics of the progenitor are compatible
9636 -- with the explicit qualifier in the declaration.
9637 -- The check only applies to qualifiers that come from source.
9638 -- Limited_Present also appears in the declaration of corresponding
9639 -- records, and the check does not apply to them.
9640
9641 if Limited_Present (Type_Def)
9642 and then not
9643 Is_Concurrent_Record_Type (Defining_Identifier (N))
9644 then
9645 if Is_Limited_Interface (Parent_Type)
9646 and then not Is_Limited_Interface (Iface_Id)
9647 then
9648 Error_Msg_NE
9649 ("progenitor& must be limited interface",
9650 Error_Node, Iface_Id);
9651
9652 elsif
9653 (Task_Present (Iface_Def)
9654 or else Protected_Present (Iface_Def)
9655 or else Synchronized_Present (Iface_Def))
9656 and then Nkind (N) /= N_Private_Extension_Declaration
9657 and then not Error_Posted (N)
9658 then
9659 Error_Msg_NE
9660 ("progenitor& must be limited interface",
9661 Error_Node, Iface_Id);
9662 end if;
9663
9664 -- Protected interfaces can only inherit from limited, synchronized
9665 -- or protected interfaces.
9666
9667 elsif Nkind (N) = N_Full_Type_Declaration
9668 and then Protected_Present (Type_Def)
9669 then
9670 if Limited_Present (Iface_Def)
9671 or else Synchronized_Present (Iface_Def)
9672 or else Protected_Present (Iface_Def)
9673 then
9674 null;
9675
9676 elsif Task_Present (Iface_Def) then
9677 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9678 & " from task interface", Error_Node);
9679
9680 else
9681 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9682 & " from non-limited interface", Error_Node);
9683 end if;
9684
9685 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
9686 -- limited and synchronized.
9687
9688 elsif Synchronized_Present (Type_Def) then
9689 if Limited_Present (Iface_Def)
9690 or else Synchronized_Present (Iface_Def)
9691 then
9692 null;
9693
9694 elsif Protected_Present (Iface_Def)
9695 and then Nkind (N) /= N_Private_Extension_Declaration
9696 then
9697 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9698 & " from protected interface", Error_Node);
9699
9700 elsif Task_Present (Iface_Def)
9701 and then Nkind (N) /= N_Private_Extension_Declaration
9702 then
9703 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9704 & " from task interface", Error_Node);
9705
9706 elsif not Is_Limited_Interface (Iface_Id) then
9707 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9708 & " from non-limited interface", Error_Node);
9709 end if;
9710
9711 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
9712 -- synchronized or task interfaces.
9713
9714 elsif Nkind (N) = N_Full_Type_Declaration
9715 and then Task_Present (Type_Def)
9716 then
9717 if Limited_Present (Iface_Def)
9718 or else Synchronized_Present (Iface_Def)
9719 or else Task_Present (Iface_Def)
9720 then
9721 null;
9722
9723 elsif Protected_Present (Iface_Def) then
9724 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9725 & " protected interface", Error_Node);
9726
9727 else
9728 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9729 & " non-limited interface", Error_Node);
9730 end if;
9731 end if;
9732 end Check_Ifaces;
9733
9734 -- Start of processing for Check_Interfaces
9735
9736 begin
9737 if Is_Interface (Parent_Type) then
9738 if Is_Task_Interface (Parent_Type) then
9739 Is_Task := True;
9740
9741 elsif Is_Protected_Interface (Parent_Type) then
9742 Is_Protected := True;
9743 end if;
9744 end if;
9745
9746 if Nkind (N) = N_Private_Extension_Declaration then
9747
9748 -- Check that progenitors are compatible with declaration
9749
9750 Iface := First (Interface_List (Def));
9751 while Present (Iface) loop
9752 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9753
9754 Parent_Node := Parent (Base_Type (Iface_Typ));
9755 Iface_Def := Type_Definition (Parent_Node);
9756
9757 if not Is_Interface (Iface_Typ) then
9758 Diagnose_Interface (Iface, Iface_Typ);
9759
9760 else
9761 Check_Ifaces (Iface_Def, Iface);
9762 end if;
9763
9764 Next (Iface);
9765 end loop;
9766
9767 if Is_Task and Is_Protected then
9768 Error_Msg_N
9769 ("type cannot derive from task and protected interface", N);
9770 end if;
9771
9772 return;
9773 end if;
9774
9775 -- Full type declaration of derived type.
9776 -- Check compatibility with parent if it is interface type
9777
9778 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9779 and then Is_Interface (Parent_Type)
9780 then
9781 Parent_Node := Parent (Parent_Type);
9782
9783 -- More detailed checks for interface varieties
9784
9785 Check_Ifaces
9786 (Iface_Def => Type_Definition (Parent_Node),
9787 Error_Node => Subtype_Indication (Type_Definition (N)));
9788 end if;
9789
9790 Iface := First (Interface_List (Def));
9791 while Present (Iface) loop
9792 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9793
9794 Parent_Node := Parent (Base_Type (Iface_Typ));
9795 Iface_Def := Type_Definition (Parent_Node);
9796
9797 if not Is_Interface (Iface_Typ) then
9798 Diagnose_Interface (Iface, Iface_Typ);
9799
9800 else
9801 -- "The declaration of a specific descendant of an interface
9802 -- type freezes the interface type" RM 13.14
9803
9804 Freeze_Before (N, Iface_Typ);
9805 Check_Ifaces (Iface_Def, Error_Node => Iface);
9806 end if;
9807
9808 Next (Iface);
9809 end loop;
9810
9811 if Is_Task and Is_Protected then
9812 Error_Msg_N
9813 ("type cannot derive from task and protected interface", N);
9814 end if;
9815 end Check_Interfaces;
9816
9817 ------------------------------------
9818 -- Check_Or_Process_Discriminants --
9819 ------------------------------------
9820
9821 -- If an incomplete or private type declaration was already given for the
9822 -- type, the discriminants may have already been processed if they were
9823 -- present on the incomplete declaration. In this case a full conformance
9824 -- check has been performed in Find_Type_Name, and we then recheck here
9825 -- some properties that can't be checked on the partial view alone.
9826 -- Otherwise we call Process_Discriminants.
9827
9828 procedure Check_Or_Process_Discriminants
9829 (N : Node_Id;
9830 T : Entity_Id;
9831 Prev : Entity_Id := Empty)
9832 is
9833 begin
9834 if Has_Discriminants (T) then
9835
9836 -- Discriminants are already set on T if they were already present
9837 -- on the partial view. Make them visible to component declarations.
9838
9839 declare
9840 D : Entity_Id;
9841 -- Discriminant on T (full view) referencing expr on partial view
9842
9843 Prev_D : Entity_Id;
9844 -- Entity of corresponding discriminant on partial view
9845
9846 New_D : Node_Id;
9847 -- Discriminant specification for full view, expression is the
9848 -- syntactic copy on full view (which has been checked for
9849 -- conformance with partial view), only used here to post error
9850 -- message.
9851
9852 begin
9853 D := First_Discriminant (T);
9854 New_D := First (Discriminant_Specifications (N));
9855 while Present (D) loop
9856 Prev_D := Current_Entity (D);
9857 Set_Current_Entity (D);
9858 Set_Is_Immediately_Visible (D);
9859 Set_Homonym (D, Prev_D);
9860
9861 -- Handle the case where there is an untagged partial view and
9862 -- the full view is tagged: must disallow discriminants with
9863 -- defaults, unless compiling for Ada 2012, which allows a
9864 -- limited tagged type to have defaulted discriminants (see
9865 -- AI05-0214). However, suppress the error here if it was
9866 -- already reported on the default expression of the partial
9867 -- view.
9868
9869 if Is_Tagged_Type (T)
9870 and then Present (Expression (Parent (D)))
9871 and then (not Is_Limited_Type (Current_Scope)
9872 or else Ada_Version < Ada_2012)
9873 and then not Error_Posted (Expression (Parent (D)))
9874 then
9875 if Ada_Version >= Ada_2012 then
9876 Error_Msg_N
9877 ("discriminants of nonlimited tagged type cannot have"
9878 & " defaults",
9879 Expression (New_D));
9880 else
9881 Error_Msg_N
9882 ("discriminants of tagged type cannot have defaults",
9883 Expression (New_D));
9884 end if;
9885 end if;
9886
9887 -- Ada 2005 (AI-230): Access discriminant allowed in
9888 -- non-limited record types.
9889
9890 if Ada_Version < Ada_2005 then
9891
9892 -- This restriction gets applied to the full type here. It
9893 -- has already been applied earlier to the partial view.
9894
9895 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
9896 end if;
9897
9898 Next_Discriminant (D);
9899 Next (New_D);
9900 end loop;
9901 end;
9902
9903 elsif Present (Discriminant_Specifications (N)) then
9904 Process_Discriminants (N, Prev);
9905 end if;
9906 end Check_Or_Process_Discriminants;
9907
9908 ----------------------
9909 -- Check_Real_Bound --
9910 ----------------------
9911
9912 procedure Check_Real_Bound (Bound : Node_Id) is
9913 begin
9914 if not Is_Real_Type (Etype (Bound)) then
9915 Error_Msg_N
9916 ("bound in real type definition must be of real type", Bound);
9917
9918 elsif not Is_OK_Static_Expression (Bound) then
9919 Flag_Non_Static_Expr
9920 ("non-static expression used for real type bound!", Bound);
9921
9922 else
9923 return;
9924 end if;
9925
9926 Rewrite
9927 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
9928 Analyze (Bound);
9929 Resolve (Bound, Standard_Float);
9930 end Check_Real_Bound;
9931
9932 ------------------------------
9933 -- Complete_Private_Subtype --
9934 ------------------------------
9935
9936 procedure Complete_Private_Subtype
9937 (Priv : Entity_Id;
9938 Full : Entity_Id;
9939 Full_Base : Entity_Id;
9940 Related_Nod : Node_Id)
9941 is
9942 Save_Next_Entity : Entity_Id;
9943 Save_Homonym : Entity_Id;
9944
9945 begin
9946 -- Set semantic attributes for (implicit) private subtype completion.
9947 -- If the full type has no discriminants, then it is a copy of the full
9948 -- view of the base. Otherwise, it is a subtype of the base with a
9949 -- possible discriminant constraint. Save and restore the original
9950 -- Next_Entity field of full to ensure that the calls to Copy_Node
9951 -- do not corrupt the entity chain.
9952
9953 -- Note that the type of the full view is the same entity as the type of
9954 -- the partial view. In this fashion, the subtype has access to the
9955 -- correct view of the parent.
9956
9957 Save_Next_Entity := Next_Entity (Full);
9958 Save_Homonym := Homonym (Priv);
9959
9960 case Ekind (Full_Base) is
9961 when E_Record_Type |
9962 E_Record_Subtype |
9963 Class_Wide_Kind |
9964 Private_Kind |
9965 Task_Kind |
9966 Protected_Kind =>
9967 Copy_Node (Priv, Full);
9968
9969 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
9970 Set_First_Entity (Full, First_Entity (Full_Base));
9971 Set_Last_Entity (Full, Last_Entity (Full_Base));
9972
9973 when others =>
9974 Copy_Node (Full_Base, Full);
9975 Set_Chars (Full, Chars (Priv));
9976 Conditional_Delay (Full, Priv);
9977 Set_Sloc (Full, Sloc (Priv));
9978 end case;
9979
9980 Set_Next_Entity (Full, Save_Next_Entity);
9981 Set_Homonym (Full, Save_Homonym);
9982 Set_Associated_Node_For_Itype (Full, Related_Nod);
9983
9984 -- Set common attributes for all subtypes: kind, convention, etc.
9985
9986 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
9987 Set_Convention (Full, Convention (Full_Base));
9988
9989 -- The Etype of the full view is inconsistent. Gigi needs to see the
9990 -- structural full view, which is what the current scheme gives:
9991 -- the Etype of the full view is the etype of the full base. However,
9992 -- if the full base is a derived type, the full view then looks like
9993 -- a subtype of the parent, not a subtype of the full base. If instead
9994 -- we write:
9995
9996 -- Set_Etype (Full, Full_Base);
9997
9998 -- then we get inconsistencies in the front-end (confusion between
9999 -- views). Several outstanding bugs are related to this ???
10000
10001 Set_Is_First_Subtype (Full, False);
10002 Set_Scope (Full, Scope (Priv));
10003 Set_Size_Info (Full, Full_Base);
10004 Set_RM_Size (Full, RM_Size (Full_Base));
10005 Set_Is_Itype (Full);
10006
10007 -- A subtype of a private-type-without-discriminants, whose full-view
10008 -- has discriminants with default expressions, is not constrained!
10009
10010 if not Has_Discriminants (Priv) then
10011 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
10012
10013 if Has_Discriminants (Full_Base) then
10014 Set_Discriminant_Constraint
10015 (Full, Discriminant_Constraint (Full_Base));
10016
10017 -- The partial view may have been indefinite, the full view
10018 -- might not be.
10019
10020 Set_Has_Unknown_Discriminants
10021 (Full, Has_Unknown_Discriminants (Full_Base));
10022 end if;
10023 end if;
10024
10025 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
10026 Set_Depends_On_Private (Full, Has_Private_Component (Full));
10027
10028 -- Freeze the private subtype entity if its parent is delayed, and not
10029 -- already frozen. We skip this processing if the type is an anonymous
10030 -- subtype of a record component, or is the corresponding record of a
10031 -- protected type, since ???
10032
10033 if not Is_Type (Scope (Full)) then
10034 Set_Has_Delayed_Freeze (Full,
10035 Has_Delayed_Freeze (Full_Base)
10036 and then (not Is_Frozen (Full_Base)));
10037 end if;
10038
10039 Set_Freeze_Node (Full, Empty);
10040 Set_Is_Frozen (Full, False);
10041 Set_Full_View (Priv, Full);
10042
10043 if Has_Discriminants (Full) then
10044 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
10045 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
10046
10047 if Has_Unknown_Discriminants (Full) then
10048 Set_Discriminant_Constraint (Full, No_Elist);
10049 end if;
10050 end if;
10051
10052 if Ekind (Full_Base) = E_Record_Type
10053 and then Has_Discriminants (Full_Base)
10054 and then Has_Discriminants (Priv) -- might not, if errors
10055 and then not Has_Unknown_Discriminants (Priv)
10056 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
10057 then
10058 Create_Constrained_Components
10059 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
10060
10061 -- If the full base is itself derived from private, build a congruent
10062 -- subtype of its underlying type, for use by the back end. For a
10063 -- constrained record component, the declaration cannot be placed on
10064 -- the component list, but it must nevertheless be built an analyzed, to
10065 -- supply enough information for Gigi to compute the size of component.
10066
10067 elsif Ekind (Full_Base) in Private_Kind
10068 and then Is_Derived_Type (Full_Base)
10069 and then Has_Discriminants (Full_Base)
10070 and then (Ekind (Current_Scope) /= E_Record_Subtype)
10071 then
10072 if not Is_Itype (Priv)
10073 and then
10074 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
10075 then
10076 Build_Underlying_Full_View
10077 (Parent (Priv), Full, Etype (Full_Base));
10078
10079 elsif Nkind (Related_Nod) = N_Component_Declaration then
10080 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
10081 end if;
10082
10083 elsif Is_Record_Type (Full_Base) then
10084
10085 -- Show Full is simply a renaming of Full_Base
10086
10087 Set_Cloned_Subtype (Full, Full_Base);
10088 end if;
10089
10090 -- It is unsafe to share to bounds of a scalar type, because the Itype
10091 -- is elaborated on demand, and if a bound is non-static then different
10092 -- orders of elaboration in different units will lead to different
10093 -- external symbols.
10094
10095 if Is_Scalar_Type (Full_Base) then
10096 Set_Scalar_Range (Full,
10097 Make_Range (Sloc (Related_Nod),
10098 Low_Bound =>
10099 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
10100 High_Bound =>
10101 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
10102
10103 -- This completion inherits the bounds of the full parent, but if
10104 -- the parent is an unconstrained floating point type, so is the
10105 -- completion.
10106
10107 if Is_Floating_Point_Type (Full_Base) then
10108 Set_Includes_Infinities
10109 (Scalar_Range (Full), Has_Infinities (Full_Base));
10110 end if;
10111 end if;
10112
10113 -- ??? It seems that a lot of fields are missing that should be copied
10114 -- from Full_Base to Full. Here are some that are introduced in a
10115 -- non-disruptive way but a cleanup is necessary.
10116
10117 if Is_Tagged_Type (Full_Base) then
10118 Set_Is_Tagged_Type (Full);
10119 Set_Direct_Primitive_Operations (Full,
10120 Direct_Primitive_Operations (Full_Base));
10121
10122 -- Inherit class_wide type of full_base in case the partial view was
10123 -- not tagged. Otherwise it has already been created when the private
10124 -- subtype was analyzed.
10125
10126 if No (Class_Wide_Type (Full)) then
10127 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
10128 end if;
10129
10130 -- If this is a subtype of a protected or task type, constrain its
10131 -- corresponding record, unless this is a subtype without constraints,
10132 -- i.e. a simple renaming as with an actual subtype in an instance.
10133
10134 elsif Is_Concurrent_Type (Full_Base) then
10135 if Has_Discriminants (Full)
10136 and then Present (Corresponding_Record_Type (Full_Base))
10137 and then
10138 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
10139 then
10140 Set_Corresponding_Record_Type (Full,
10141 Constrain_Corresponding_Record
10142 (Full, Corresponding_Record_Type (Full_Base),
10143 Related_Nod, Full_Base));
10144
10145 else
10146 Set_Corresponding_Record_Type (Full,
10147 Corresponding_Record_Type (Full_Base));
10148 end if;
10149 end if;
10150
10151 -- Link rep item chain, and also setting of Has_Predicates from private
10152 -- subtype to full subtype, since we will need these on the full subtype
10153 -- to create the predicate function. Note that the full subtype may
10154 -- already have rep items, inherited from the full view of the base
10155 -- type, so we must be sure not to overwrite these entries.
10156
10157 declare
10158 Item : Node_Id;
10159 Next_Item : Node_Id;
10160
10161 begin
10162 Item := First_Rep_Item (Full);
10163
10164 -- If no existing rep items on full type, we can just link directly
10165 -- to the list of items on the private type.
10166
10167 if No (Item) then
10168 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
10169
10170 -- Else search to end of items currently linked to the full subtype
10171
10172 else
10173 loop
10174 Next_Item := Next_Rep_Item (Item);
10175 exit when No (Next_Item);
10176 Item := Next_Item;
10177 end loop;
10178
10179 -- And link the private type items at the end of the chain
10180
10181 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
10182 end if;
10183 end;
10184
10185 -- Make sure Has_Predicates is set on full type if it is set on the
10186 -- private type. Note that it may already be set on the full type and
10187 -- if so, we don't want to unset it.
10188
10189 if Has_Predicates (Priv) then
10190 Set_Has_Predicates (Full);
10191 end if;
10192 end Complete_Private_Subtype;
10193
10194 ----------------------------
10195 -- Constant_Redeclaration --
10196 ----------------------------
10197
10198 procedure Constant_Redeclaration
10199 (Id : Entity_Id;
10200 N : Node_Id;
10201 T : out Entity_Id)
10202 is
10203 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
10204 Obj_Def : constant Node_Id := Object_Definition (N);
10205 New_T : Entity_Id;
10206
10207 procedure Check_Possible_Deferred_Completion
10208 (Prev_Id : Entity_Id;
10209 Prev_Obj_Def : Node_Id;
10210 Curr_Obj_Def : Node_Id);
10211 -- Determine whether the two object definitions describe the partial
10212 -- and the full view of a constrained deferred constant. Generate
10213 -- a subtype for the full view and verify that it statically matches
10214 -- the subtype of the partial view.
10215
10216 procedure Check_Recursive_Declaration (Typ : Entity_Id);
10217 -- If deferred constant is an access type initialized with an allocator,
10218 -- check whether there is an illegal recursion in the definition,
10219 -- through a default value of some record subcomponent. This is normally
10220 -- detected when generating init procs, but requires this additional
10221 -- mechanism when expansion is disabled.
10222
10223 ----------------------------------------
10224 -- Check_Possible_Deferred_Completion --
10225 ----------------------------------------
10226
10227 procedure Check_Possible_Deferred_Completion
10228 (Prev_Id : Entity_Id;
10229 Prev_Obj_Def : Node_Id;
10230 Curr_Obj_Def : Node_Id)
10231 is
10232 begin
10233 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
10234 and then Present (Constraint (Prev_Obj_Def))
10235 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
10236 and then Present (Constraint (Curr_Obj_Def))
10237 then
10238 declare
10239 Loc : constant Source_Ptr := Sloc (N);
10240 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
10241 Decl : constant Node_Id :=
10242 Make_Subtype_Declaration (Loc,
10243 Defining_Identifier => Def_Id,
10244 Subtype_Indication =>
10245 Relocate_Node (Curr_Obj_Def));
10246
10247 begin
10248 Insert_Before_And_Analyze (N, Decl);
10249 Set_Etype (Id, Def_Id);
10250
10251 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
10252 Error_Msg_Sloc := Sloc (Prev_Id);
10253 Error_Msg_N ("subtype does not statically match deferred " &
10254 "declaration#", N);
10255 end if;
10256 end;
10257 end if;
10258 end Check_Possible_Deferred_Completion;
10259
10260 ---------------------------------
10261 -- Check_Recursive_Declaration --
10262 ---------------------------------
10263
10264 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
10265 Comp : Entity_Id;
10266
10267 begin
10268 if Is_Record_Type (Typ) then
10269 Comp := First_Component (Typ);
10270 while Present (Comp) loop
10271 if Comes_From_Source (Comp) then
10272 if Present (Expression (Parent (Comp)))
10273 and then Is_Entity_Name (Expression (Parent (Comp)))
10274 and then Entity (Expression (Parent (Comp))) = Prev
10275 then
10276 Error_Msg_Sloc := Sloc (Parent (Comp));
10277 Error_Msg_NE
10278 ("illegal circularity with declaration for&#",
10279 N, Comp);
10280 return;
10281
10282 elsif Is_Record_Type (Etype (Comp)) then
10283 Check_Recursive_Declaration (Etype (Comp));
10284 end if;
10285 end if;
10286
10287 Next_Component (Comp);
10288 end loop;
10289 end if;
10290 end Check_Recursive_Declaration;
10291
10292 -- Start of processing for Constant_Redeclaration
10293
10294 begin
10295 if Nkind (Parent (Prev)) = N_Object_Declaration then
10296 if Nkind (Object_Definition
10297 (Parent (Prev))) = N_Subtype_Indication
10298 then
10299 -- Find type of new declaration. The constraints of the two
10300 -- views must match statically, but there is no point in
10301 -- creating an itype for the full view.
10302
10303 if Nkind (Obj_Def) = N_Subtype_Indication then
10304 Find_Type (Subtype_Mark (Obj_Def));
10305 New_T := Entity (Subtype_Mark (Obj_Def));
10306
10307 else
10308 Find_Type (Obj_Def);
10309 New_T := Entity (Obj_Def);
10310 end if;
10311
10312 T := Etype (Prev);
10313
10314 else
10315 -- The full view may impose a constraint, even if the partial
10316 -- view does not, so construct the subtype.
10317
10318 New_T := Find_Type_Of_Object (Obj_Def, N);
10319 T := New_T;
10320 end if;
10321
10322 else
10323 -- Current declaration is illegal, diagnosed below in Enter_Name
10324
10325 T := Empty;
10326 New_T := Any_Type;
10327 end if;
10328
10329 -- If previous full declaration or a renaming declaration exists, or if
10330 -- a homograph is present, let Enter_Name handle it, either with an
10331 -- error or with the removal of an overridden implicit subprogram.
10332
10333 if Ekind (Prev) /= E_Constant
10334 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
10335 or else Present (Expression (Parent (Prev)))
10336 or else Present (Full_View (Prev))
10337 then
10338 Enter_Name (Id);
10339
10340 -- Verify that types of both declarations match, or else that both types
10341 -- are anonymous access types whose designated subtypes statically match
10342 -- (as allowed in Ada 2005 by AI-385).
10343
10344 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
10345 and then
10346 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
10347 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
10348 or else Is_Access_Constant (Etype (New_T)) /=
10349 Is_Access_Constant (Etype (Prev))
10350 or else Can_Never_Be_Null (Etype (New_T)) /=
10351 Can_Never_Be_Null (Etype (Prev))
10352 or else Null_Exclusion_Present (Parent (Prev)) /=
10353 Null_Exclusion_Present (Parent (Id))
10354 or else not Subtypes_Statically_Match
10355 (Designated_Type (Etype (Prev)),
10356 Designated_Type (Etype (New_T))))
10357 then
10358 Error_Msg_Sloc := Sloc (Prev);
10359 Error_Msg_N ("type does not match declaration#", N);
10360 Set_Full_View (Prev, Id);
10361 Set_Etype (Id, Any_Type);
10362
10363 elsif
10364 Null_Exclusion_Present (Parent (Prev))
10365 and then not Null_Exclusion_Present (N)
10366 then
10367 Error_Msg_Sloc := Sloc (Prev);
10368 Error_Msg_N ("null-exclusion does not match declaration#", N);
10369 Set_Full_View (Prev, Id);
10370 Set_Etype (Id, Any_Type);
10371
10372 -- If so, process the full constant declaration
10373
10374 else
10375 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
10376 -- the deferred declaration is constrained, then the subtype defined
10377 -- by the subtype_indication in the full declaration shall match it
10378 -- statically.
10379
10380 Check_Possible_Deferred_Completion
10381 (Prev_Id => Prev,
10382 Prev_Obj_Def => Object_Definition (Parent (Prev)),
10383 Curr_Obj_Def => Obj_Def);
10384
10385 Set_Full_View (Prev, Id);
10386 Set_Is_Public (Id, Is_Public (Prev));
10387 Set_Is_Internal (Id);
10388 Append_Entity (Id, Current_Scope);
10389
10390 -- Check ALIASED present if present before (RM 7.4(7))
10391
10392 if Is_Aliased (Prev)
10393 and then not Aliased_Present (N)
10394 then
10395 Error_Msg_Sloc := Sloc (Prev);
10396 Error_Msg_N ("ALIASED required (see declaration#)", N);
10397 end if;
10398
10399 -- Check that placement is in private part and that the incomplete
10400 -- declaration appeared in the visible part.
10401
10402 if Ekind (Current_Scope) = E_Package
10403 and then not In_Private_Part (Current_Scope)
10404 then
10405 Error_Msg_Sloc := Sloc (Prev);
10406 Error_Msg_N
10407 ("full constant for declaration#"
10408 & " must be in private part", N);
10409
10410 elsif Ekind (Current_Scope) = E_Package
10411 and then
10412 List_Containing (Parent (Prev)) /=
10413 Visible_Declarations
10414 (Specification (Unit_Declaration_Node (Current_Scope)))
10415 then
10416 Error_Msg_N
10417 ("deferred constant must be declared in visible part",
10418 Parent (Prev));
10419 end if;
10420
10421 if Is_Access_Type (T)
10422 and then Nkind (Expression (N)) = N_Allocator
10423 then
10424 Check_Recursive_Declaration (Designated_Type (T));
10425 end if;
10426 end if;
10427 end Constant_Redeclaration;
10428
10429 ----------------------
10430 -- Constrain_Access --
10431 ----------------------
10432
10433 procedure Constrain_Access
10434 (Def_Id : in out Entity_Id;
10435 S : Node_Id;
10436 Related_Nod : Node_Id)
10437 is
10438 T : constant Entity_Id := Entity (Subtype_Mark (S));
10439 Desig_Type : constant Entity_Id := Designated_Type (T);
10440 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
10441 Constraint_OK : Boolean := True;
10442
10443 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
10444 -- Simple predicate to test for defaulted discriminants
10445 -- Shouldn't this be in sem_util???
10446
10447 ---------------------------------
10448 -- Has_Defaulted_Discriminants --
10449 ---------------------------------
10450
10451 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
10452 begin
10453 return Has_Discriminants (Typ)
10454 and then Present (First_Discriminant (Typ))
10455 and then Present
10456 (Discriminant_Default_Value (First_Discriminant (Typ)));
10457 end Has_Defaulted_Discriminants;
10458
10459 -- Start of processing for Constrain_Access
10460
10461 begin
10462 if Is_Array_Type (Desig_Type) then
10463 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
10464
10465 elsif (Is_Record_Type (Desig_Type)
10466 or else Is_Incomplete_Or_Private_Type (Desig_Type))
10467 and then not Is_Constrained (Desig_Type)
10468 then
10469 -- ??? The following code is a temporary kludge to ignore a
10470 -- discriminant constraint on access type if it is constraining
10471 -- the current record. Avoid creating the implicit subtype of the
10472 -- record we are currently compiling since right now, we cannot
10473 -- handle these. For now, just return the access type itself.
10474
10475 if Desig_Type = Current_Scope
10476 and then No (Def_Id)
10477 then
10478 Set_Ekind (Desig_Subtype, E_Record_Subtype);
10479 Def_Id := Entity (Subtype_Mark (S));
10480
10481 -- This call added to ensure that the constraint is analyzed
10482 -- (needed for a B test). Note that we still return early from
10483 -- this procedure to avoid recursive processing. ???
10484
10485 Constrain_Discriminated_Type
10486 (Desig_Subtype, S, Related_Nod, For_Access => True);
10487 return;
10488 end if;
10489
10490 if (Ekind (T) = E_General_Access_Type
10491 or else Ada_Version >= Ada_2005)
10492 and then Has_Private_Declaration (Desig_Type)
10493 and then In_Open_Scopes (Scope (Desig_Type))
10494 and then Has_Discriminants (Desig_Type)
10495 then
10496 -- Enforce rule that the constraint is illegal if there is
10497 -- an unconstrained view of the designated type. This means
10498 -- that the partial view (either a private type declaration or
10499 -- a derivation from a private type) has no discriminants.
10500 -- (Defect Report 8652/0008, Technical Corrigendum 1, checked
10501 -- by ACATS B371001).
10502
10503 -- Rule updated for Ada 2005: the private type is said to have
10504 -- a constrained partial view, given that objects of the type
10505 -- can be declared. Furthermore, the rule applies to all access
10506 -- types, unlike the rule concerning default discriminants.
10507
10508 declare
10509 Pack : constant Node_Id :=
10510 Unit_Declaration_Node (Scope (Desig_Type));
10511 Decls : List_Id;
10512 Decl : Node_Id;
10513
10514 begin
10515 if Nkind (Pack) = N_Package_Declaration then
10516 Decls := Visible_Declarations (Specification (Pack));
10517 Decl := First (Decls);
10518 while Present (Decl) loop
10519 if (Nkind (Decl) = N_Private_Type_Declaration
10520 and then
10521 Chars (Defining_Identifier (Decl)) =
10522 Chars (Desig_Type))
10523
10524 or else
10525 (Nkind (Decl) = N_Full_Type_Declaration
10526 and then
10527 Chars (Defining_Identifier (Decl)) =
10528 Chars (Desig_Type)
10529 and then Is_Derived_Type (Desig_Type)
10530 and then
10531 Has_Private_Declaration (Etype (Desig_Type)))
10532 then
10533 if No (Discriminant_Specifications (Decl)) then
10534 Error_Msg_N
10535 ("cannot constrain general access type if " &
10536 "designated type has constrained partial view",
10537 S);
10538 end if;
10539
10540 exit;
10541 end if;
10542
10543 Next (Decl);
10544 end loop;
10545 end if;
10546 end;
10547 end if;
10548
10549 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
10550 For_Access => True);
10551
10552 elsif (Is_Task_Type (Desig_Type)
10553 or else Is_Protected_Type (Desig_Type))
10554 and then not Is_Constrained (Desig_Type)
10555 then
10556 Constrain_Concurrent
10557 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
10558
10559 else
10560 Error_Msg_N ("invalid constraint on access type", S);
10561 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
10562 Constraint_OK := False;
10563 end if;
10564
10565 if No (Def_Id) then
10566 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
10567 else
10568 Set_Ekind (Def_Id, E_Access_Subtype);
10569 end if;
10570
10571 if Constraint_OK then
10572 Set_Etype (Def_Id, Base_Type (T));
10573
10574 if Is_Private_Type (Desig_Type) then
10575 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
10576 end if;
10577 else
10578 Set_Etype (Def_Id, Any_Type);
10579 end if;
10580
10581 Set_Size_Info (Def_Id, T);
10582 Set_Is_Constrained (Def_Id, Constraint_OK);
10583 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
10584 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10585 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
10586
10587 Conditional_Delay (Def_Id, T);
10588
10589 -- AI-363 : Subtypes of general access types whose designated types have
10590 -- default discriminants are disallowed. In instances, the rule has to
10591 -- be checked against the actual, of which T is the subtype. In a
10592 -- generic body, the rule is checked assuming that the actual type has
10593 -- defaulted discriminants.
10594
10595 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
10596 if Ekind (Base_Type (T)) = E_General_Access_Type
10597 and then Has_Defaulted_Discriminants (Desig_Type)
10598 then
10599 if Ada_Version < Ada_2005 then
10600 Error_Msg_N
10601 ("access subtype of general access type would not " &
10602 "be allowed in Ada 2005?", S);
10603 else
10604 Error_Msg_N
10605 ("access subtype of general access type not allowed", S);
10606 end if;
10607
10608 Error_Msg_N ("\discriminants have defaults", S);
10609
10610 elsif Is_Access_Type (T)
10611 and then Is_Generic_Type (Desig_Type)
10612 and then Has_Discriminants (Desig_Type)
10613 and then In_Package_Body (Current_Scope)
10614 then
10615 if Ada_Version < Ada_2005 then
10616 Error_Msg_N
10617 ("access subtype would not be allowed in generic body " &
10618 "in Ada 2005?", S);
10619 else
10620 Error_Msg_N
10621 ("access subtype not allowed in generic body", S);
10622 end if;
10623
10624 Error_Msg_N
10625 ("\designated type is a discriminated formal", S);
10626 end if;
10627 end if;
10628 end Constrain_Access;
10629
10630 ---------------------
10631 -- Constrain_Array --
10632 ---------------------
10633
10634 procedure Constrain_Array
10635 (Def_Id : in out Entity_Id;
10636 SI : Node_Id;
10637 Related_Nod : Node_Id;
10638 Related_Id : Entity_Id;
10639 Suffix : Character)
10640 is
10641 C : constant Node_Id := Constraint (SI);
10642 Number_Of_Constraints : Nat := 0;
10643 Index : Node_Id;
10644 S, T : Entity_Id;
10645 Constraint_OK : Boolean := True;
10646
10647 begin
10648 T := Entity (Subtype_Mark (SI));
10649
10650 if Ekind (T) in Access_Kind then
10651 T := Designated_Type (T);
10652 end if;
10653
10654 -- If an index constraint follows a subtype mark in a subtype indication
10655 -- then the type or subtype denoted by the subtype mark must not already
10656 -- impose an index constraint. The subtype mark must denote either an
10657 -- unconstrained array type or an access type whose designated type
10658 -- is such an array type... (RM 3.6.1)
10659
10660 if Is_Constrained (T) then
10661 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
10662 Constraint_OK := False;
10663
10664 else
10665 S := First (Constraints (C));
10666 while Present (S) loop
10667 Number_Of_Constraints := Number_Of_Constraints + 1;
10668 Next (S);
10669 end loop;
10670
10671 -- In either case, the index constraint must provide a discrete
10672 -- range for each index of the array type and the type of each
10673 -- discrete range must be the same as that of the corresponding
10674 -- index. (RM 3.6.1)
10675
10676 if Number_Of_Constraints /= Number_Dimensions (T) then
10677 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
10678 Constraint_OK := False;
10679
10680 else
10681 S := First (Constraints (C));
10682 Index := First_Index (T);
10683 Analyze (Index);
10684
10685 -- Apply constraints to each index type
10686
10687 for J in 1 .. Number_Of_Constraints loop
10688 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
10689 Next (Index);
10690 Next (S);
10691 end loop;
10692
10693 end if;
10694 end if;
10695
10696 if No (Def_Id) then
10697 Def_Id :=
10698 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
10699 Set_Parent (Def_Id, Related_Nod);
10700
10701 else
10702 Set_Ekind (Def_Id, E_Array_Subtype);
10703 end if;
10704
10705 Set_Size_Info (Def_Id, (T));
10706 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10707 Set_Etype (Def_Id, Base_Type (T));
10708
10709 if Constraint_OK then
10710 Set_First_Index (Def_Id, First (Constraints (C)));
10711 else
10712 Set_First_Index (Def_Id, First_Index (T));
10713 end if;
10714
10715 Set_Is_Constrained (Def_Id, True);
10716 Set_Is_Aliased (Def_Id, Is_Aliased (T));
10717 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10718
10719 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
10720 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
10721
10722 -- A subtype does not inherit the packed_array_type of is parent. We
10723 -- need to initialize the attribute because if Def_Id is previously
10724 -- analyzed through a limited_with clause, it will have the attributes
10725 -- of an incomplete type, one of which is an Elist that overlaps the
10726 -- Packed_Array_Type field.
10727
10728 Set_Packed_Array_Type (Def_Id, Empty);
10729
10730 -- Build a freeze node if parent still needs one. Also make sure that
10731 -- the Depends_On_Private status is set because the subtype will need
10732 -- reprocessing at the time the base type does, and also we must set a
10733 -- conditional delay.
10734
10735 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10736 Conditional_Delay (Def_Id, T);
10737 end Constrain_Array;
10738
10739 ------------------------------
10740 -- Constrain_Component_Type --
10741 ------------------------------
10742
10743 function Constrain_Component_Type
10744 (Comp : Entity_Id;
10745 Constrained_Typ : Entity_Id;
10746 Related_Node : Node_Id;
10747 Typ : Entity_Id;
10748 Constraints : Elist_Id) return Entity_Id
10749 is
10750 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
10751 Compon_Type : constant Entity_Id := Etype (Comp);
10752
10753 function Build_Constrained_Array_Type
10754 (Old_Type : Entity_Id) return Entity_Id;
10755 -- If Old_Type is an array type, one of whose indexes is constrained
10756 -- by a discriminant, build an Itype whose constraint replaces the
10757 -- discriminant with its value in the constraint.
10758
10759 function Build_Constrained_Discriminated_Type
10760 (Old_Type : Entity_Id) return Entity_Id;
10761 -- Ditto for record components
10762
10763 function Build_Constrained_Access_Type
10764 (Old_Type : Entity_Id) return Entity_Id;
10765 -- Ditto for access types. Makes use of previous two functions, to
10766 -- constrain designated type.
10767
10768 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
10769 -- T is an array or discriminated type, C is a list of constraints
10770 -- that apply to T. This routine builds the constrained subtype.
10771
10772 function Is_Discriminant (Expr : Node_Id) return Boolean;
10773 -- Returns True if Expr is a discriminant
10774
10775 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
10776 -- Find the value of discriminant Discrim in Constraint
10777
10778 -----------------------------------
10779 -- Build_Constrained_Access_Type --
10780 -----------------------------------
10781
10782 function Build_Constrained_Access_Type
10783 (Old_Type : Entity_Id) return Entity_Id
10784 is
10785 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
10786 Itype : Entity_Id;
10787 Desig_Subtype : Entity_Id;
10788 Scop : Entity_Id;
10789
10790 begin
10791 -- if the original access type was not embedded in the enclosing
10792 -- type definition, there is no need to produce a new access
10793 -- subtype. In fact every access type with an explicit constraint
10794 -- generates an itype whose scope is the enclosing record.
10795
10796 if not Is_Type (Scope (Old_Type)) then
10797 return Old_Type;
10798
10799 elsif Is_Array_Type (Desig_Type) then
10800 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
10801
10802 elsif Has_Discriminants (Desig_Type) then
10803
10804 -- This may be an access type to an enclosing record type for
10805 -- which we are constructing the constrained components. Return
10806 -- the enclosing record subtype. This is not always correct,
10807 -- but avoids infinite recursion. ???
10808
10809 Desig_Subtype := Any_Type;
10810
10811 for J in reverse 0 .. Scope_Stack.Last loop
10812 Scop := Scope_Stack.Table (J).Entity;
10813
10814 if Is_Type (Scop)
10815 and then Base_Type (Scop) = Base_Type (Desig_Type)
10816 then
10817 Desig_Subtype := Scop;
10818 end if;
10819
10820 exit when not Is_Type (Scop);
10821 end loop;
10822
10823 if Desig_Subtype = Any_Type then
10824 Desig_Subtype :=
10825 Build_Constrained_Discriminated_Type (Desig_Type);
10826 end if;
10827
10828 else
10829 return Old_Type;
10830 end if;
10831
10832 if Desig_Subtype /= Desig_Type then
10833
10834 -- The Related_Node better be here or else we won't be able
10835 -- to attach new itypes to a node in the tree.
10836
10837 pragma Assert (Present (Related_Node));
10838
10839 Itype := Create_Itype (E_Access_Subtype, Related_Node);
10840
10841 Set_Etype (Itype, Base_Type (Old_Type));
10842 Set_Size_Info (Itype, (Old_Type));
10843 Set_Directly_Designated_Type (Itype, Desig_Subtype);
10844 Set_Depends_On_Private (Itype, Has_Private_Component
10845 (Old_Type));
10846 Set_Is_Access_Constant (Itype, Is_Access_Constant
10847 (Old_Type));
10848
10849 -- The new itype needs freezing when it depends on a not frozen
10850 -- type and the enclosing subtype needs freezing.
10851
10852 if Has_Delayed_Freeze (Constrained_Typ)
10853 and then not Is_Frozen (Constrained_Typ)
10854 then
10855 Conditional_Delay (Itype, Base_Type (Old_Type));
10856 end if;
10857
10858 return Itype;
10859
10860 else
10861 return Old_Type;
10862 end if;
10863 end Build_Constrained_Access_Type;
10864
10865 ----------------------------------
10866 -- Build_Constrained_Array_Type --
10867 ----------------------------------
10868
10869 function Build_Constrained_Array_Type
10870 (Old_Type : Entity_Id) return Entity_Id
10871 is
10872 Lo_Expr : Node_Id;
10873 Hi_Expr : Node_Id;
10874 Old_Index : Node_Id;
10875 Range_Node : Node_Id;
10876 Constr_List : List_Id;
10877
10878 Need_To_Create_Itype : Boolean := False;
10879
10880 begin
10881 Old_Index := First_Index (Old_Type);
10882 while Present (Old_Index) loop
10883 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10884
10885 if Is_Discriminant (Lo_Expr)
10886 or else Is_Discriminant (Hi_Expr)
10887 then
10888 Need_To_Create_Itype := True;
10889 end if;
10890
10891 Next_Index (Old_Index);
10892 end loop;
10893
10894 if Need_To_Create_Itype then
10895 Constr_List := New_List;
10896
10897 Old_Index := First_Index (Old_Type);
10898 while Present (Old_Index) loop
10899 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10900
10901 if Is_Discriminant (Lo_Expr) then
10902 Lo_Expr := Get_Discr_Value (Lo_Expr);
10903 end if;
10904
10905 if Is_Discriminant (Hi_Expr) then
10906 Hi_Expr := Get_Discr_Value (Hi_Expr);
10907 end if;
10908
10909 Range_Node :=
10910 Make_Range
10911 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
10912
10913 Append (Range_Node, To => Constr_List);
10914
10915 Next_Index (Old_Index);
10916 end loop;
10917
10918 return Build_Subtype (Old_Type, Constr_List);
10919
10920 else
10921 return Old_Type;
10922 end if;
10923 end Build_Constrained_Array_Type;
10924
10925 ------------------------------------------
10926 -- Build_Constrained_Discriminated_Type --
10927 ------------------------------------------
10928
10929 function Build_Constrained_Discriminated_Type
10930 (Old_Type : Entity_Id) return Entity_Id
10931 is
10932 Expr : Node_Id;
10933 Constr_List : List_Id;
10934 Old_Constraint : Elmt_Id;
10935
10936 Need_To_Create_Itype : Boolean := False;
10937
10938 begin
10939 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10940 while Present (Old_Constraint) loop
10941 Expr := Node (Old_Constraint);
10942
10943 if Is_Discriminant (Expr) then
10944 Need_To_Create_Itype := True;
10945 end if;
10946
10947 Next_Elmt (Old_Constraint);
10948 end loop;
10949
10950 if Need_To_Create_Itype then
10951 Constr_List := New_List;
10952
10953 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10954 while Present (Old_Constraint) loop
10955 Expr := Node (Old_Constraint);
10956
10957 if Is_Discriminant (Expr) then
10958 Expr := Get_Discr_Value (Expr);
10959 end if;
10960
10961 Append (New_Copy_Tree (Expr), To => Constr_List);
10962
10963 Next_Elmt (Old_Constraint);
10964 end loop;
10965
10966 return Build_Subtype (Old_Type, Constr_List);
10967
10968 else
10969 return Old_Type;
10970 end if;
10971 end Build_Constrained_Discriminated_Type;
10972
10973 -------------------
10974 -- Build_Subtype --
10975 -------------------
10976
10977 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
10978 Indic : Node_Id;
10979 Subtyp_Decl : Node_Id;
10980 Def_Id : Entity_Id;
10981 Btyp : Entity_Id := Base_Type (T);
10982
10983 begin
10984 -- The Related_Node better be here or else we won't be able to
10985 -- attach new itypes to a node in the tree.
10986
10987 pragma Assert (Present (Related_Node));
10988
10989 -- If the view of the component's type is incomplete or private
10990 -- with unknown discriminants, then the constraint must be applied
10991 -- to the full type.
10992
10993 if Has_Unknown_Discriminants (Btyp)
10994 and then Present (Underlying_Type (Btyp))
10995 then
10996 Btyp := Underlying_Type (Btyp);
10997 end if;
10998
10999 Indic :=
11000 Make_Subtype_Indication (Loc,
11001 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
11002 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
11003
11004 Def_Id := Create_Itype (Ekind (T), Related_Node);
11005
11006 Subtyp_Decl :=
11007 Make_Subtype_Declaration (Loc,
11008 Defining_Identifier => Def_Id,
11009 Subtype_Indication => Indic);
11010
11011 Set_Parent (Subtyp_Decl, Parent (Related_Node));
11012
11013 -- Itypes must be analyzed with checks off (see package Itypes)
11014
11015 Analyze (Subtyp_Decl, Suppress => All_Checks);
11016
11017 return Def_Id;
11018 end Build_Subtype;
11019
11020 ---------------------
11021 -- Get_Discr_Value --
11022 ---------------------
11023
11024 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
11025 D : Entity_Id;
11026 E : Elmt_Id;
11027
11028 begin
11029 -- The discriminant may be declared for the type, in which case we
11030 -- find it by iterating over the list of discriminants. If the
11031 -- discriminant is inherited from a parent type, it appears as the
11032 -- corresponding discriminant of the current type. This will be the
11033 -- case when constraining an inherited component whose constraint is
11034 -- given by a discriminant of the parent.
11035
11036 D := First_Discriminant (Typ);
11037 E := First_Elmt (Constraints);
11038
11039 while Present (D) loop
11040 if D = Entity (Discrim)
11041 or else D = CR_Discriminant (Entity (Discrim))
11042 or else Corresponding_Discriminant (D) = Entity (Discrim)
11043 then
11044 return Node (E);
11045 end if;
11046
11047 Next_Discriminant (D);
11048 Next_Elmt (E);
11049 end loop;
11050
11051 -- The Corresponding_Discriminant mechanism is incomplete, because
11052 -- the correspondence between new and old discriminants is not one
11053 -- to one: one new discriminant can constrain several old ones. In
11054 -- that case, scan sequentially the stored_constraint, the list of
11055 -- discriminants of the parents, and the constraints.
11056 -- Previous code checked for the present of the Stored_Constraint
11057 -- list for the derived type, but did not use it at all. Should it
11058 -- be present when the component is a discriminated task type?
11059
11060 if Is_Derived_Type (Typ)
11061 and then Scope (Entity (Discrim)) = Etype (Typ)
11062 then
11063 D := First_Discriminant (Etype (Typ));
11064 E := First_Elmt (Constraints);
11065 while Present (D) loop
11066 if D = Entity (Discrim) then
11067 return Node (E);
11068 end if;
11069
11070 Next_Discriminant (D);
11071 Next_Elmt (E);
11072 end loop;
11073 end if;
11074
11075 -- Something is wrong if we did not find the value
11076
11077 raise Program_Error;
11078 end Get_Discr_Value;
11079
11080 ---------------------
11081 -- Is_Discriminant --
11082 ---------------------
11083
11084 function Is_Discriminant (Expr : Node_Id) return Boolean is
11085 Discrim_Scope : Entity_Id;
11086
11087 begin
11088 if Denotes_Discriminant (Expr) then
11089 Discrim_Scope := Scope (Entity (Expr));
11090
11091 -- Either we have a reference to one of Typ's discriminants,
11092
11093 pragma Assert (Discrim_Scope = Typ
11094
11095 -- or to the discriminants of the parent type, in the case
11096 -- of a derivation of a tagged type with variants.
11097
11098 or else Discrim_Scope = Etype (Typ)
11099 or else Full_View (Discrim_Scope) = Etype (Typ)
11100
11101 -- or same as above for the case where the discriminants
11102 -- were declared in Typ's private view.
11103
11104 or else (Is_Private_Type (Discrim_Scope)
11105 and then Chars (Discrim_Scope) = Chars (Typ))
11106
11107 -- or else we are deriving from the full view and the
11108 -- discriminant is declared in the private entity.
11109
11110 or else (Is_Private_Type (Typ)
11111 and then Chars (Discrim_Scope) = Chars (Typ))
11112
11113 -- Or we are constrained the corresponding record of a
11114 -- synchronized type that completes a private declaration.
11115
11116 or else (Is_Concurrent_Record_Type (Typ)
11117 and then
11118 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
11119
11120 -- or we have a class-wide type, in which case make sure the
11121 -- discriminant found belongs to the root type.
11122
11123 or else (Is_Class_Wide_Type (Typ)
11124 and then Etype (Typ) = Discrim_Scope));
11125
11126 return True;
11127 end if;
11128
11129 -- In all other cases we have something wrong
11130
11131 return False;
11132 end Is_Discriminant;
11133
11134 -- Start of processing for Constrain_Component_Type
11135
11136 begin
11137 if Nkind (Parent (Comp)) = N_Component_Declaration
11138 and then Comes_From_Source (Parent (Comp))
11139 and then Comes_From_Source
11140 (Subtype_Indication (Component_Definition (Parent (Comp))))
11141 and then
11142 Is_Entity_Name
11143 (Subtype_Indication (Component_Definition (Parent (Comp))))
11144 then
11145 return Compon_Type;
11146
11147 elsif Is_Array_Type (Compon_Type) then
11148 return Build_Constrained_Array_Type (Compon_Type);
11149
11150 elsif Has_Discriminants (Compon_Type) then
11151 return Build_Constrained_Discriminated_Type (Compon_Type);
11152
11153 elsif Is_Access_Type (Compon_Type) then
11154 return Build_Constrained_Access_Type (Compon_Type);
11155
11156 else
11157 return Compon_Type;
11158 end if;
11159 end Constrain_Component_Type;
11160
11161 --------------------------
11162 -- Constrain_Concurrent --
11163 --------------------------
11164
11165 -- For concurrent types, the associated record value type carries the same
11166 -- discriminants, so when we constrain a concurrent type, we must constrain
11167 -- the corresponding record type as well.
11168
11169 procedure Constrain_Concurrent
11170 (Def_Id : in out Entity_Id;
11171 SI : Node_Id;
11172 Related_Nod : Node_Id;
11173 Related_Id : Entity_Id;
11174 Suffix : Character)
11175 is
11176 T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
11177 T_Val : Entity_Id;
11178
11179 begin
11180 if Ekind (T_Ent) in Access_Kind then
11181 T_Ent := Designated_Type (T_Ent);
11182 end if;
11183
11184 T_Val := Corresponding_Record_Type (T_Ent);
11185
11186 if Present (T_Val) then
11187
11188 if No (Def_Id) then
11189 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11190 end if;
11191
11192 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11193
11194 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11195 Set_Corresponding_Record_Type (Def_Id,
11196 Constrain_Corresponding_Record
11197 (Def_Id, T_Val, Related_Nod, Related_Id));
11198
11199 else
11200 -- If there is no associated record, expansion is disabled and this
11201 -- is a generic context. Create a subtype in any case, so that
11202 -- semantic analysis can proceed.
11203
11204 if No (Def_Id) then
11205 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11206 end if;
11207
11208 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11209 end if;
11210 end Constrain_Concurrent;
11211
11212 ------------------------------------
11213 -- Constrain_Corresponding_Record --
11214 ------------------------------------
11215
11216 function Constrain_Corresponding_Record
11217 (Prot_Subt : Entity_Id;
11218 Corr_Rec : Entity_Id;
11219 Related_Nod : Node_Id;
11220 Related_Id : Entity_Id) return Entity_Id
11221 is
11222 T_Sub : constant Entity_Id :=
11223 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
11224
11225 begin
11226 Set_Etype (T_Sub, Corr_Rec);
11227 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
11228 Set_Is_Constrained (T_Sub, True);
11229 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
11230 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
11231
11232 -- As elsewhere, we do not want to create a freeze node for this itype
11233 -- if it is created for a constrained component of an enclosing record
11234 -- because references to outer discriminants will appear out of scope.
11235
11236 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
11237 Conditional_Delay (T_Sub, Corr_Rec);
11238 else
11239 Set_Is_Frozen (T_Sub);
11240 end if;
11241
11242 if Has_Discriminants (Prot_Subt) then -- False only if errors.
11243 Set_Discriminant_Constraint
11244 (T_Sub, Discriminant_Constraint (Prot_Subt));
11245 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
11246 Create_Constrained_Components
11247 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
11248 end if;
11249
11250 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
11251
11252 return T_Sub;
11253 end Constrain_Corresponding_Record;
11254
11255 -----------------------
11256 -- Constrain_Decimal --
11257 -----------------------
11258
11259 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
11260 T : constant Entity_Id := Entity (Subtype_Mark (S));
11261 C : constant Node_Id := Constraint (S);
11262 Loc : constant Source_Ptr := Sloc (C);
11263 Range_Expr : Node_Id;
11264 Digits_Expr : Node_Id;
11265 Digits_Val : Uint;
11266 Bound_Val : Ureal;
11267
11268 begin
11269 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
11270
11271 if Nkind (C) = N_Range_Constraint then
11272 Range_Expr := Range_Expression (C);
11273 Digits_Val := Digits_Value (T);
11274
11275 else
11276 pragma Assert (Nkind (C) = N_Digits_Constraint);
11277
11278 Check_Formal_Restriction ("digits constraint is not allowed", S);
11279
11280 Digits_Expr := Digits_Expression (C);
11281 Analyze_And_Resolve (Digits_Expr, Any_Integer);
11282
11283 Check_Digits_Expression (Digits_Expr);
11284 Digits_Val := Expr_Value (Digits_Expr);
11285
11286 if Digits_Val > Digits_Value (T) then
11287 Error_Msg_N
11288 ("digits expression is incompatible with subtype", C);
11289 Digits_Val := Digits_Value (T);
11290 end if;
11291
11292 if Present (Range_Constraint (C)) then
11293 Range_Expr := Range_Expression (Range_Constraint (C));
11294 else
11295 Range_Expr := Empty;
11296 end if;
11297 end if;
11298
11299 Set_Etype (Def_Id, Base_Type (T));
11300 Set_Size_Info (Def_Id, (T));
11301 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11302 Set_Delta_Value (Def_Id, Delta_Value (T));
11303 Set_Scale_Value (Def_Id, Scale_Value (T));
11304 Set_Small_Value (Def_Id, Small_Value (T));
11305 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
11306 Set_Digits_Value (Def_Id, Digits_Val);
11307
11308 -- Manufacture range from given digits value if no range present
11309
11310 if No (Range_Expr) then
11311 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
11312 Range_Expr :=
11313 Make_Range (Loc,
11314 Low_Bound =>
11315 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
11316 High_Bound =>
11317 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
11318 end if;
11319
11320 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
11321 Set_Discrete_RM_Size (Def_Id);
11322
11323 -- Unconditionally delay the freeze, since we cannot set size
11324 -- information in all cases correctly until the freeze point.
11325
11326 Set_Has_Delayed_Freeze (Def_Id);
11327 end Constrain_Decimal;
11328
11329 ----------------------------------
11330 -- Constrain_Discriminated_Type --
11331 ----------------------------------
11332
11333 procedure Constrain_Discriminated_Type
11334 (Def_Id : Entity_Id;
11335 S : Node_Id;
11336 Related_Nod : Node_Id;
11337 For_Access : Boolean := False)
11338 is
11339 E : constant Entity_Id := Entity (Subtype_Mark (S));
11340 T : Entity_Id;
11341 C : Node_Id;
11342 Elist : Elist_Id := New_Elmt_List;
11343
11344 procedure Fixup_Bad_Constraint;
11345 -- This is called after finding a bad constraint, and after having
11346 -- posted an appropriate error message. The mission is to leave the
11347 -- entity T in as reasonable state as possible!
11348
11349 --------------------------
11350 -- Fixup_Bad_Constraint --
11351 --------------------------
11352
11353 procedure Fixup_Bad_Constraint is
11354 begin
11355 -- Set a reasonable Ekind for the entity. For an incomplete type,
11356 -- we can't do much, but for other types, we can set the proper
11357 -- corresponding subtype kind.
11358
11359 if Ekind (T) = E_Incomplete_Type then
11360 Set_Ekind (Def_Id, Ekind (T));
11361 else
11362 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
11363 end if;
11364
11365 -- Set Etype to the known type, to reduce chances of cascaded errors
11366
11367 Set_Etype (Def_Id, E);
11368 Set_Error_Posted (Def_Id);
11369 end Fixup_Bad_Constraint;
11370
11371 -- Start of processing for Constrain_Discriminated_Type
11372
11373 begin
11374 C := Constraint (S);
11375
11376 -- A discriminant constraint is only allowed in a subtype indication,
11377 -- after a subtype mark. This subtype mark must denote either a type
11378 -- with discriminants, or an access type whose designated type is a
11379 -- type with discriminants. A discriminant constraint specifies the
11380 -- values of these discriminants (RM 3.7.2(5)).
11381
11382 T := Base_Type (Entity (Subtype_Mark (S)));
11383
11384 if Ekind (T) in Access_Kind then
11385 T := Designated_Type (T);
11386 end if;
11387
11388 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
11389 -- Avoid generating an error for access-to-incomplete subtypes.
11390
11391 if Ada_Version >= Ada_2005
11392 and then Ekind (T) = E_Incomplete_Type
11393 and then Nkind (Parent (S)) = N_Subtype_Declaration
11394 and then not Is_Itype (Def_Id)
11395 then
11396 -- A little sanity check, emit an error message if the type
11397 -- has discriminants to begin with. Type T may be a regular
11398 -- incomplete type or imported via a limited with clause.
11399
11400 if Has_Discriminants (T)
11401 or else
11402 (From_With_Type (T)
11403 and then Present (Non_Limited_View (T))
11404 and then Nkind (Parent (Non_Limited_View (T))) =
11405 N_Full_Type_Declaration
11406 and then Present (Discriminant_Specifications
11407 (Parent (Non_Limited_View (T)))))
11408 then
11409 Error_Msg_N
11410 ("(Ada 2005) incomplete subtype may not be constrained", C);
11411 else
11412 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11413 end if;
11414
11415 Fixup_Bad_Constraint;
11416 return;
11417
11418 -- Check that the type has visible discriminants. The type may be
11419 -- a private type with unknown discriminants whose full view has
11420 -- discriminants which are invisible.
11421
11422 elsif not Has_Discriminants (T)
11423 or else
11424 (Has_Unknown_Discriminants (T)
11425 and then Is_Private_Type (T))
11426 then
11427 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11428 Fixup_Bad_Constraint;
11429 return;
11430
11431 elsif Is_Constrained (E)
11432 or else (Ekind (E) = E_Class_Wide_Subtype
11433 and then Present (Discriminant_Constraint (E)))
11434 then
11435 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
11436 Fixup_Bad_Constraint;
11437 return;
11438 end if;
11439
11440 -- T may be an unconstrained subtype (e.g. a generic actual).
11441 -- Constraint applies to the base type.
11442
11443 T := Base_Type (T);
11444
11445 Elist := Build_Discriminant_Constraints (T, S);
11446
11447 -- If the list returned was empty we had an error in building the
11448 -- discriminant constraint. We have also already signalled an error
11449 -- in the incomplete type case
11450
11451 if Is_Empty_Elmt_List (Elist) then
11452 Fixup_Bad_Constraint;
11453 return;
11454 end if;
11455
11456 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
11457 end Constrain_Discriminated_Type;
11458
11459 ---------------------------
11460 -- Constrain_Enumeration --
11461 ---------------------------
11462
11463 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
11464 T : constant Entity_Id := Entity (Subtype_Mark (S));
11465 C : constant Node_Id := Constraint (S);
11466
11467 begin
11468 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11469
11470 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
11471
11472 Set_Etype (Def_Id, Base_Type (T));
11473 Set_Size_Info (Def_Id, (T));
11474 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11475 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11476
11477 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11478
11479 Set_Discrete_RM_Size (Def_Id);
11480 end Constrain_Enumeration;
11481
11482 ----------------------
11483 -- Constrain_Float --
11484 ----------------------
11485
11486 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
11487 T : constant Entity_Id := Entity (Subtype_Mark (S));
11488 C : Node_Id;
11489 D : Node_Id;
11490 Rais : Node_Id;
11491
11492 begin
11493 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
11494
11495 Set_Etype (Def_Id, Base_Type (T));
11496 Set_Size_Info (Def_Id, (T));
11497 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11498
11499 -- Process the constraint
11500
11501 C := Constraint (S);
11502
11503 -- Digits constraint present
11504
11505 if Nkind (C) = N_Digits_Constraint then
11506
11507 Check_Formal_Restriction ("digits constraint is not allowed", S);
11508 Check_Restriction (No_Obsolescent_Features, C);
11509
11510 if Warn_On_Obsolescent_Feature then
11511 Error_Msg_N
11512 ("subtype digits constraint is an " &
11513 "obsolescent feature (RM J.3(8))?", C);
11514 end if;
11515
11516 D := Digits_Expression (C);
11517 Analyze_And_Resolve (D, Any_Integer);
11518 Check_Digits_Expression (D);
11519 Set_Digits_Value (Def_Id, Expr_Value (D));
11520
11521 -- Check that digits value is in range. Obviously we can do this
11522 -- at compile time, but it is strictly a runtime check, and of
11523 -- course there is an ACVC test that checks this!
11524
11525 if Digits_Value (Def_Id) > Digits_Value (T) then
11526 Error_Msg_Uint_1 := Digits_Value (T);
11527 Error_Msg_N ("?digits value is too large, maximum is ^", D);
11528 Rais :=
11529 Make_Raise_Constraint_Error (Sloc (D),
11530 Reason => CE_Range_Check_Failed);
11531 Insert_Action (Declaration_Node (Def_Id), Rais);
11532 end if;
11533
11534 C := Range_Constraint (C);
11535
11536 -- No digits constraint present
11537
11538 else
11539 Set_Digits_Value (Def_Id, Digits_Value (T));
11540 end if;
11541
11542 -- Range constraint present
11543
11544 if Nkind (C) = N_Range_Constraint then
11545 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11546
11547 -- No range constraint present
11548
11549 else
11550 pragma Assert (No (C));
11551 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11552 end if;
11553
11554 Set_Is_Constrained (Def_Id);
11555 end Constrain_Float;
11556
11557 ---------------------
11558 -- Constrain_Index --
11559 ---------------------
11560
11561 procedure Constrain_Index
11562 (Index : Node_Id;
11563 S : Node_Id;
11564 Related_Nod : Node_Id;
11565 Related_Id : Entity_Id;
11566 Suffix : Character;
11567 Suffix_Index : Nat)
11568 is
11569 Def_Id : Entity_Id;
11570 R : Node_Id := Empty;
11571 T : constant Entity_Id := Etype (Index);
11572
11573 begin
11574 if Nkind (S) = N_Range
11575 or else
11576 (Nkind (S) = N_Attribute_Reference
11577 and then Attribute_Name (S) = Name_Range)
11578 then
11579 -- A Range attribute will be transformed into N_Range by Resolve
11580
11581 Analyze (S);
11582 Set_Etype (S, T);
11583 R := S;
11584
11585 Process_Range_Expr_In_Decl (R, T, Empty_List);
11586
11587 if not Error_Posted (S)
11588 and then
11589 (Nkind (S) /= N_Range
11590 or else not Covers (T, (Etype (Low_Bound (S))))
11591 or else not Covers (T, (Etype (High_Bound (S)))))
11592 then
11593 if Base_Type (T) /= Any_Type
11594 and then Etype (Low_Bound (S)) /= Any_Type
11595 and then Etype (High_Bound (S)) /= Any_Type
11596 then
11597 Error_Msg_N ("range expected", S);
11598 end if;
11599 end if;
11600
11601 elsif Nkind (S) = N_Subtype_Indication then
11602
11603 -- The parser has verified that this is a discrete indication
11604
11605 Resolve_Discrete_Subtype_Indication (S, T);
11606 R := Range_Expression (Constraint (S));
11607
11608 -- Capture values of bounds and generate temporaries for them if
11609 -- needed, since checks may cause duplication of the expressions
11610 -- which must not be reevaluated.
11611
11612 if Expander_Active then
11613 Force_Evaluation (Low_Bound (R));
11614 Force_Evaluation (High_Bound (R));
11615 end if;
11616
11617 elsif Nkind (S) = N_Discriminant_Association then
11618
11619 -- Syntactically valid in subtype indication
11620
11621 Error_Msg_N ("invalid index constraint", S);
11622 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11623 return;
11624
11625 -- Subtype_Mark case, no anonymous subtypes to construct
11626
11627 else
11628 Analyze (S);
11629
11630 if Is_Entity_Name (S) then
11631 if not Is_Type (Entity (S)) then
11632 Error_Msg_N ("expect subtype mark for index constraint", S);
11633
11634 elsif Base_Type (Entity (S)) /= Base_Type (T) then
11635 Wrong_Type (S, Base_Type (T));
11636
11637 -- Check error of subtype with predicate in index constraint
11638
11639 else
11640 Bad_Predicated_Subtype_Use
11641 ("subtype& has predicate, not allowed in index constraint",
11642 S, Entity (S));
11643 end if;
11644
11645 return;
11646
11647 else
11648 Error_Msg_N ("invalid index constraint", S);
11649 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11650 return;
11651 end if;
11652 end if;
11653
11654 Def_Id :=
11655 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
11656
11657 Set_Etype (Def_Id, Base_Type (T));
11658
11659 if Is_Modular_Integer_Type (T) then
11660 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11661
11662 elsif Is_Integer_Type (T) then
11663 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11664
11665 else
11666 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11667 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11668 Set_First_Literal (Def_Id, First_Literal (T));
11669 end if;
11670
11671 Set_Size_Info (Def_Id, (T));
11672 Set_RM_Size (Def_Id, RM_Size (T));
11673 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11674
11675 Set_Scalar_Range (Def_Id, R);
11676
11677 Set_Etype (S, Def_Id);
11678 Set_Discrete_RM_Size (Def_Id);
11679 end Constrain_Index;
11680
11681 -----------------------
11682 -- Constrain_Integer --
11683 -----------------------
11684
11685 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
11686 T : constant Entity_Id := Entity (Subtype_Mark (S));
11687 C : constant Node_Id := Constraint (S);
11688
11689 begin
11690 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11691
11692 if Is_Modular_Integer_Type (T) then
11693 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11694 else
11695 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11696 end if;
11697
11698 Set_Etype (Def_Id, Base_Type (T));
11699 Set_Size_Info (Def_Id, (T));
11700 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11701 Set_Discrete_RM_Size (Def_Id);
11702 end Constrain_Integer;
11703
11704 ------------------------------
11705 -- Constrain_Ordinary_Fixed --
11706 ------------------------------
11707
11708 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
11709 T : constant Entity_Id := Entity (Subtype_Mark (S));
11710 C : Node_Id;
11711 D : Node_Id;
11712 Rais : Node_Id;
11713
11714 begin
11715 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
11716 Set_Etype (Def_Id, Base_Type (T));
11717 Set_Size_Info (Def_Id, (T));
11718 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11719 Set_Small_Value (Def_Id, Small_Value (T));
11720
11721 -- Process the constraint
11722
11723 C := Constraint (S);
11724
11725 -- Delta constraint present
11726
11727 if Nkind (C) = N_Delta_Constraint then
11728
11729 Check_Formal_Restriction ("delta constraint is not allowed", S);
11730 Check_Restriction (No_Obsolescent_Features, C);
11731
11732 if Warn_On_Obsolescent_Feature then
11733 Error_Msg_S
11734 ("subtype delta constraint is an " &
11735 "obsolescent feature (RM J.3(7))?");
11736 end if;
11737
11738 D := Delta_Expression (C);
11739 Analyze_And_Resolve (D, Any_Real);
11740 Check_Delta_Expression (D);
11741 Set_Delta_Value (Def_Id, Expr_Value_R (D));
11742
11743 -- Check that delta value is in range. Obviously we can do this
11744 -- at compile time, but it is strictly a runtime check, and of
11745 -- course there is an ACVC test that checks this!
11746
11747 if Delta_Value (Def_Id) < Delta_Value (T) then
11748 Error_Msg_N ("?delta value is too small", D);
11749 Rais :=
11750 Make_Raise_Constraint_Error (Sloc (D),
11751 Reason => CE_Range_Check_Failed);
11752 Insert_Action (Declaration_Node (Def_Id), Rais);
11753 end if;
11754
11755 C := Range_Constraint (C);
11756
11757 -- No delta constraint present
11758
11759 else
11760 Set_Delta_Value (Def_Id, Delta_Value (T));
11761 end if;
11762
11763 -- Range constraint present
11764
11765 if Nkind (C) = N_Range_Constraint then
11766 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11767
11768 -- No range constraint present
11769
11770 else
11771 pragma Assert (No (C));
11772 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11773
11774 end if;
11775
11776 Set_Discrete_RM_Size (Def_Id);
11777
11778 -- Unconditionally delay the freeze, since we cannot set size
11779 -- information in all cases correctly until the freeze point.
11780
11781 Set_Has_Delayed_Freeze (Def_Id);
11782 end Constrain_Ordinary_Fixed;
11783
11784 -----------------------
11785 -- Contain_Interface --
11786 -----------------------
11787
11788 function Contain_Interface
11789 (Iface : Entity_Id;
11790 Ifaces : Elist_Id) return Boolean
11791 is
11792 Iface_Elmt : Elmt_Id;
11793
11794 begin
11795 if Present (Ifaces) then
11796 Iface_Elmt := First_Elmt (Ifaces);
11797 while Present (Iface_Elmt) loop
11798 if Node (Iface_Elmt) = Iface then
11799 return True;
11800 end if;
11801
11802 Next_Elmt (Iface_Elmt);
11803 end loop;
11804 end if;
11805
11806 return False;
11807 end Contain_Interface;
11808
11809 ---------------------------
11810 -- Convert_Scalar_Bounds --
11811 ---------------------------
11812
11813 procedure Convert_Scalar_Bounds
11814 (N : Node_Id;
11815 Parent_Type : Entity_Id;
11816 Derived_Type : Entity_Id;
11817 Loc : Source_Ptr)
11818 is
11819 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
11820
11821 Lo : Node_Id;
11822 Hi : Node_Id;
11823 Rng : Node_Id;
11824
11825 begin
11826 -- Defend against previous errors
11827
11828 if No (Scalar_Range (Derived_Type)) then
11829 return;
11830 end if;
11831
11832 Lo := Build_Scalar_Bound
11833 (Type_Low_Bound (Derived_Type),
11834 Parent_Type, Implicit_Base);
11835
11836 Hi := Build_Scalar_Bound
11837 (Type_High_Bound (Derived_Type),
11838 Parent_Type, Implicit_Base);
11839
11840 Rng :=
11841 Make_Range (Loc,
11842 Low_Bound => Lo,
11843 High_Bound => Hi);
11844
11845 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
11846
11847 Set_Parent (Rng, N);
11848 Set_Scalar_Range (Derived_Type, Rng);
11849
11850 -- Analyze the bounds
11851
11852 Analyze_And_Resolve (Lo, Implicit_Base);
11853 Analyze_And_Resolve (Hi, Implicit_Base);
11854
11855 -- Analyze the range itself, except that we do not analyze it if
11856 -- the bounds are real literals, and we have a fixed-point type.
11857 -- The reason for this is that we delay setting the bounds in this
11858 -- case till we know the final Small and Size values (see circuit
11859 -- in Freeze.Freeze_Fixed_Point_Type for further details).
11860
11861 if Is_Fixed_Point_Type (Parent_Type)
11862 and then Nkind (Lo) = N_Real_Literal
11863 and then Nkind (Hi) = N_Real_Literal
11864 then
11865 return;
11866
11867 -- Here we do the analysis of the range
11868
11869 -- Note: we do this manually, since if we do a normal Analyze and
11870 -- Resolve call, there are problems with the conversions used for
11871 -- the derived type range.
11872
11873 else
11874 Set_Etype (Rng, Implicit_Base);
11875 Set_Analyzed (Rng, True);
11876 end if;
11877 end Convert_Scalar_Bounds;
11878
11879 -------------------
11880 -- Copy_And_Swap --
11881 -------------------
11882
11883 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
11884 begin
11885 -- Initialize new full declaration entity by copying the pertinent
11886 -- fields of the corresponding private declaration entity.
11887
11888 -- We temporarily set Ekind to a value appropriate for a type to
11889 -- avoid assert failures in Einfo from checking for setting type
11890 -- attributes on something that is not a type. Ekind (Priv) is an
11891 -- appropriate choice, since it allowed the attributes to be set
11892 -- in the first place. This Ekind value will be modified later.
11893
11894 Set_Ekind (Full, Ekind (Priv));
11895
11896 -- Also set Etype temporarily to Any_Type, again, in the absence
11897 -- of errors, it will be properly reset, and if there are errors,
11898 -- then we want a value of Any_Type to remain.
11899
11900 Set_Etype (Full, Any_Type);
11901
11902 -- Now start copying attributes
11903
11904 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
11905
11906 if Has_Discriminants (Full) then
11907 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
11908 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
11909 end if;
11910
11911 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11912 Set_Homonym (Full, Homonym (Priv));
11913 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
11914 Set_Is_Public (Full, Is_Public (Priv));
11915 Set_Is_Pure (Full, Is_Pure (Priv));
11916 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
11917 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
11918 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
11919 Set_Has_Pragma_Unreferenced_Objects
11920 (Full, Has_Pragma_Unreferenced_Objects
11921 (Priv));
11922
11923 Conditional_Delay (Full, Priv);
11924
11925 if Is_Tagged_Type (Full) then
11926 Set_Direct_Primitive_Operations (Full,
11927 Direct_Primitive_Operations (Priv));
11928
11929 if Is_Base_Type (Priv) then
11930 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
11931 end if;
11932 end if;
11933
11934 Set_Is_Volatile (Full, Is_Volatile (Priv));
11935 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
11936 Set_Scope (Full, Scope (Priv));
11937 Set_Next_Entity (Full, Next_Entity (Priv));
11938 Set_First_Entity (Full, First_Entity (Priv));
11939 Set_Last_Entity (Full, Last_Entity (Priv));
11940
11941 -- If access types have been recorded for later handling, keep them in
11942 -- the full view so that they get handled when the full view freeze
11943 -- node is expanded.
11944
11945 if Present (Freeze_Node (Priv))
11946 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
11947 then
11948 Ensure_Freeze_Node (Full);
11949 Set_Access_Types_To_Process
11950 (Freeze_Node (Full),
11951 Access_Types_To_Process (Freeze_Node (Priv)));
11952 end if;
11953
11954 -- Swap the two entities. Now Private is the full type entity and Full
11955 -- is the private one. They will be swapped back at the end of the
11956 -- private part. This swapping ensures that the entity that is visible
11957 -- in the private part is the full declaration.
11958
11959 Exchange_Entities (Priv, Full);
11960 Append_Entity (Full, Scope (Full));
11961 end Copy_And_Swap;
11962
11963 -------------------------------------
11964 -- Copy_Array_Base_Type_Attributes --
11965 -------------------------------------
11966
11967 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
11968 begin
11969 Set_Component_Alignment (T1, Component_Alignment (T2));
11970 Set_Component_Type (T1, Component_Type (T2));
11971 Set_Component_Size (T1, Component_Size (T2));
11972 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
11973 Set_Finalize_Storage_Only (T1, Finalize_Storage_Only (T2));
11974 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
11975 Set_Has_Task (T1, Has_Task (T2));
11976 Set_Is_Packed (T1, Is_Packed (T2));
11977 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
11978 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
11979 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
11980 end Copy_Array_Base_Type_Attributes;
11981
11982 -----------------------------------
11983 -- Copy_Array_Subtype_Attributes --
11984 -----------------------------------
11985
11986 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
11987 begin
11988 Set_Size_Info (T1, T2);
11989
11990 Set_First_Index (T1, First_Index (T2));
11991 Set_Is_Aliased (T1, Is_Aliased (T2));
11992 Set_Is_Atomic (T1, Is_Atomic (T2));
11993 Set_Is_Volatile (T1, Is_Volatile (T2));
11994 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
11995 Set_Is_Constrained (T1, Is_Constrained (T2));
11996 Set_Depends_On_Private (T1, Has_Private_Component (T2));
11997 Set_First_Rep_Item (T1, First_Rep_Item (T2));
11998 Set_Convention (T1, Convention (T2));
11999 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
12000 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
12001 Set_Packed_Array_Type (T1, Packed_Array_Type (T2));
12002 end Copy_Array_Subtype_Attributes;
12003
12004 -----------------------------------
12005 -- Create_Constrained_Components --
12006 -----------------------------------
12007
12008 procedure Create_Constrained_Components
12009 (Subt : Entity_Id;
12010 Decl_Node : Node_Id;
12011 Typ : Entity_Id;
12012 Constraints : Elist_Id)
12013 is
12014 Loc : constant Source_Ptr := Sloc (Subt);
12015 Comp_List : constant Elist_Id := New_Elmt_List;
12016 Parent_Type : constant Entity_Id := Etype (Typ);
12017 Assoc_List : constant List_Id := New_List;
12018 Discr_Val : Elmt_Id;
12019 Errors : Boolean;
12020 New_C : Entity_Id;
12021 Old_C : Entity_Id;
12022 Is_Static : Boolean := True;
12023
12024 procedure Collect_Fixed_Components (Typ : Entity_Id);
12025 -- Collect parent type components that do not appear in a variant part
12026
12027 procedure Create_All_Components;
12028 -- Iterate over Comp_List to create the components of the subtype
12029
12030 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
12031 -- Creates a new component from Old_Compon, copying all the fields from
12032 -- it, including its Etype, inserts the new component in the Subt entity
12033 -- chain and returns the new component.
12034
12035 function Is_Variant_Record (T : Entity_Id) return Boolean;
12036 -- If true, and discriminants are static, collect only components from
12037 -- variants selected by discriminant values.
12038
12039 ------------------------------
12040 -- Collect_Fixed_Components --
12041 ------------------------------
12042
12043 procedure Collect_Fixed_Components (Typ : Entity_Id) is
12044 begin
12045 -- Build association list for discriminants, and find components of the
12046 -- variant part selected by the values of the discriminants.
12047
12048 Old_C := First_Discriminant (Typ);
12049 Discr_Val := First_Elmt (Constraints);
12050 while Present (Old_C) loop
12051 Append_To (Assoc_List,
12052 Make_Component_Association (Loc,
12053 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
12054 Expression => New_Copy (Node (Discr_Val))));
12055
12056 Next_Elmt (Discr_Val);
12057 Next_Discriminant (Old_C);
12058 end loop;
12059
12060 -- The tag, and the possible parent and controller components
12061 -- are unconditionally in the subtype.
12062
12063 if Is_Tagged_Type (Typ)
12064 or else Has_Controlled_Component (Typ)
12065 then
12066 Old_C := First_Component (Typ);
12067 while Present (Old_C) loop
12068 if Chars ((Old_C)) = Name_uTag
12069 or else Chars ((Old_C)) = Name_uParent
12070 or else Chars ((Old_C)) = Name_uController
12071 then
12072 Append_Elmt (Old_C, Comp_List);
12073 end if;
12074
12075 Next_Component (Old_C);
12076 end loop;
12077 end if;
12078 end Collect_Fixed_Components;
12079
12080 ---------------------------
12081 -- Create_All_Components --
12082 ---------------------------
12083
12084 procedure Create_All_Components is
12085 Comp : Elmt_Id;
12086
12087 begin
12088 Comp := First_Elmt (Comp_List);
12089 while Present (Comp) loop
12090 Old_C := Node (Comp);
12091 New_C := Create_Component (Old_C);
12092
12093 Set_Etype
12094 (New_C,
12095 Constrain_Component_Type
12096 (Old_C, Subt, Decl_Node, Typ, Constraints));
12097 Set_Is_Public (New_C, Is_Public (Subt));
12098
12099 Next_Elmt (Comp);
12100 end loop;
12101 end Create_All_Components;
12102
12103 ----------------------
12104 -- Create_Component --
12105 ----------------------
12106
12107 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
12108 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
12109
12110 begin
12111 if Ekind (Old_Compon) = E_Discriminant
12112 and then Is_Completely_Hidden (Old_Compon)
12113 then
12114 -- This is a shadow discriminant created for a discriminant of
12115 -- the parent type, which needs to be present in the subtype.
12116 -- Give the shadow discriminant an internal name that cannot
12117 -- conflict with that of visible components.
12118
12119 Set_Chars (New_Compon, New_Internal_Name ('C'));
12120 end if;
12121
12122 -- Set the parent so we have a proper link for freezing etc. This is
12123 -- not a real parent pointer, since of course our parent does not own
12124 -- up to us and reference us, we are an illegitimate child of the
12125 -- original parent!
12126
12127 Set_Parent (New_Compon, Parent (Old_Compon));
12128
12129 -- If the old component's Esize was already determined and is a
12130 -- static value, then the new component simply inherits it. Otherwise
12131 -- the old component's size may require run-time determination, but
12132 -- the new component's size still might be statically determinable
12133 -- (if, for example it has a static constraint). In that case we want
12134 -- Layout_Type to recompute the component's size, so we reset its
12135 -- size and positional fields.
12136
12137 if Frontend_Layout_On_Target
12138 and then not Known_Static_Esize (Old_Compon)
12139 then
12140 Set_Esize (New_Compon, Uint_0);
12141 Init_Normalized_First_Bit (New_Compon);
12142 Init_Normalized_Position (New_Compon);
12143 Init_Normalized_Position_Max (New_Compon);
12144 end if;
12145
12146 -- We do not want this node marked as Comes_From_Source, since
12147 -- otherwise it would get first class status and a separate cross-
12148 -- reference line would be generated. Illegitimate children do not
12149 -- rate such recognition.
12150
12151 Set_Comes_From_Source (New_Compon, False);
12152
12153 -- But it is a real entity, and a birth certificate must be properly
12154 -- registered by entering it into the entity list.
12155
12156 Enter_Name (New_Compon);
12157
12158 return New_Compon;
12159 end Create_Component;
12160
12161 -----------------------
12162 -- Is_Variant_Record --
12163 -----------------------
12164
12165 function Is_Variant_Record (T : Entity_Id) return Boolean is
12166 begin
12167 return Nkind (Parent (T)) = N_Full_Type_Declaration
12168 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
12169 and then Present (Component_List (Type_Definition (Parent (T))))
12170 and then
12171 Present
12172 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
12173 end Is_Variant_Record;
12174
12175 -- Start of processing for Create_Constrained_Components
12176
12177 begin
12178 pragma Assert (Subt /= Base_Type (Subt));
12179 pragma Assert (Typ = Base_Type (Typ));
12180
12181 Set_First_Entity (Subt, Empty);
12182 Set_Last_Entity (Subt, Empty);
12183
12184 -- Check whether constraint is fully static, in which case we can
12185 -- optimize the list of components.
12186
12187 Discr_Val := First_Elmt (Constraints);
12188 while Present (Discr_Val) loop
12189 if not Is_OK_Static_Expression (Node (Discr_Val)) then
12190 Is_Static := False;
12191 exit;
12192 end if;
12193
12194 Next_Elmt (Discr_Val);
12195 end loop;
12196
12197 Set_Has_Static_Discriminants (Subt, Is_Static);
12198
12199 Push_Scope (Subt);
12200
12201 -- Inherit the discriminants of the parent type
12202
12203 Add_Discriminants : declare
12204 Num_Disc : Int;
12205 Num_Gird : Int;
12206
12207 begin
12208 Num_Disc := 0;
12209 Old_C := First_Discriminant (Typ);
12210
12211 while Present (Old_C) loop
12212 Num_Disc := Num_Disc + 1;
12213 New_C := Create_Component (Old_C);
12214 Set_Is_Public (New_C, Is_Public (Subt));
12215 Next_Discriminant (Old_C);
12216 end loop;
12217
12218 -- For an untagged derived subtype, the number of discriminants may
12219 -- be smaller than the number of inherited discriminants, because
12220 -- several of them may be renamed by a single new discriminant or
12221 -- constrained. In this case, add the hidden discriminants back into
12222 -- the subtype, because they need to be present if the optimizer of
12223 -- the GCC 4.x back-end decides to break apart assignments between
12224 -- objects using the parent view into member-wise assignments.
12225
12226 Num_Gird := 0;
12227
12228 if Is_Derived_Type (Typ)
12229 and then not Is_Tagged_Type (Typ)
12230 then
12231 Old_C := First_Stored_Discriminant (Typ);
12232
12233 while Present (Old_C) loop
12234 Num_Gird := Num_Gird + 1;
12235 Next_Stored_Discriminant (Old_C);
12236 end loop;
12237 end if;
12238
12239 if Num_Gird > Num_Disc then
12240
12241 -- Find out multiple uses of new discriminants, and add hidden
12242 -- components for the extra renamed discriminants. We recognize
12243 -- multiple uses through the Corresponding_Discriminant of a
12244 -- new discriminant: if it constrains several old discriminants,
12245 -- this field points to the last one in the parent type. The
12246 -- stored discriminants of the derived type have the same name
12247 -- as those of the parent.
12248
12249 declare
12250 Constr : Elmt_Id;
12251 New_Discr : Entity_Id;
12252 Old_Discr : Entity_Id;
12253
12254 begin
12255 Constr := First_Elmt (Stored_Constraint (Typ));
12256 Old_Discr := First_Stored_Discriminant (Typ);
12257 while Present (Constr) loop
12258 if Is_Entity_Name (Node (Constr))
12259 and then Ekind (Entity (Node (Constr))) = E_Discriminant
12260 then
12261 New_Discr := Entity (Node (Constr));
12262
12263 if Chars (Corresponding_Discriminant (New_Discr)) /=
12264 Chars (Old_Discr)
12265 then
12266 -- The new discriminant has been used to rename a
12267 -- subsequent old discriminant. Introduce a shadow
12268 -- component for the current old discriminant.
12269
12270 New_C := Create_Component (Old_Discr);
12271 Set_Original_Record_Component (New_C, Old_Discr);
12272 end if;
12273
12274 else
12275 -- The constraint has eliminated the old discriminant.
12276 -- Introduce a shadow component.
12277
12278 New_C := Create_Component (Old_Discr);
12279 Set_Original_Record_Component (New_C, Old_Discr);
12280 end if;
12281
12282 Next_Elmt (Constr);
12283 Next_Stored_Discriminant (Old_Discr);
12284 end loop;
12285 end;
12286 end if;
12287 end Add_Discriminants;
12288
12289 if Is_Static
12290 and then Is_Variant_Record (Typ)
12291 then
12292 Collect_Fixed_Components (Typ);
12293
12294 Gather_Components (
12295 Typ,
12296 Component_List (Type_Definition (Parent (Typ))),
12297 Governed_By => Assoc_List,
12298 Into => Comp_List,
12299 Report_Errors => Errors);
12300 pragma Assert (not Errors);
12301
12302 Create_All_Components;
12303
12304 -- If the subtype declaration is created for a tagged type derivation
12305 -- with constraints, we retrieve the record definition of the parent
12306 -- type to select the components of the proper variant.
12307
12308 elsif Is_Static
12309 and then Is_Tagged_Type (Typ)
12310 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
12311 and then
12312 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
12313 and then Is_Variant_Record (Parent_Type)
12314 then
12315 Collect_Fixed_Components (Typ);
12316
12317 Gather_Components (
12318 Typ,
12319 Component_List (Type_Definition (Parent (Parent_Type))),
12320 Governed_By => Assoc_List,
12321 Into => Comp_List,
12322 Report_Errors => Errors);
12323 pragma Assert (not Errors);
12324
12325 -- If the tagged derivation has a type extension, collect all the
12326 -- new components therein.
12327
12328 if Present
12329 (Record_Extension_Part (Type_Definition (Parent (Typ))))
12330 then
12331 Old_C := First_Component (Typ);
12332 while Present (Old_C) loop
12333 if Original_Record_Component (Old_C) = Old_C
12334 and then Chars (Old_C) /= Name_uTag
12335 and then Chars (Old_C) /= Name_uParent
12336 and then Chars (Old_C) /= Name_uController
12337 then
12338 Append_Elmt (Old_C, Comp_List);
12339 end if;
12340
12341 Next_Component (Old_C);
12342 end loop;
12343 end if;
12344
12345 Create_All_Components;
12346
12347 else
12348 -- If discriminants are not static, or if this is a multi-level type
12349 -- extension, we have to include all components of the parent type.
12350
12351 Old_C := First_Component (Typ);
12352 while Present (Old_C) loop
12353 New_C := Create_Component (Old_C);
12354
12355 Set_Etype
12356 (New_C,
12357 Constrain_Component_Type
12358 (Old_C, Subt, Decl_Node, Typ, Constraints));
12359 Set_Is_Public (New_C, Is_Public (Subt));
12360
12361 Next_Component (Old_C);
12362 end loop;
12363 end if;
12364
12365 End_Scope;
12366 end Create_Constrained_Components;
12367
12368 ------------------------------------------
12369 -- Decimal_Fixed_Point_Type_Declaration --
12370 ------------------------------------------
12371
12372 procedure Decimal_Fixed_Point_Type_Declaration
12373 (T : Entity_Id;
12374 Def : Node_Id)
12375 is
12376 Loc : constant Source_Ptr := Sloc (Def);
12377 Digs_Expr : constant Node_Id := Digits_Expression (Def);
12378 Delta_Expr : constant Node_Id := Delta_Expression (Def);
12379 Implicit_Base : Entity_Id;
12380 Digs_Val : Uint;
12381 Delta_Val : Ureal;
12382 Scale_Val : Uint;
12383 Bound_Val : Ureal;
12384
12385 begin
12386 Check_Formal_Restriction
12387 ("decimal fixed point type is not allowed", Def);
12388 Check_Restriction (No_Fixed_Point, Def);
12389
12390 -- Create implicit base type
12391
12392 Implicit_Base :=
12393 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
12394 Set_Etype (Implicit_Base, Implicit_Base);
12395
12396 -- Analyze and process delta expression
12397
12398 Analyze_And_Resolve (Delta_Expr, Universal_Real);
12399
12400 Check_Delta_Expression (Delta_Expr);
12401 Delta_Val := Expr_Value_R (Delta_Expr);
12402
12403 -- Check delta is power of 10, and determine scale value from it
12404
12405 declare
12406 Val : Ureal;
12407
12408 begin
12409 Scale_Val := Uint_0;
12410 Val := Delta_Val;
12411
12412 if Val < Ureal_1 then
12413 while Val < Ureal_1 loop
12414 Val := Val * Ureal_10;
12415 Scale_Val := Scale_Val + 1;
12416 end loop;
12417
12418 if Scale_Val > 18 then
12419 Error_Msg_N ("scale exceeds maximum value of 18", Def);
12420 Scale_Val := UI_From_Int (+18);
12421 end if;
12422
12423 else
12424 while Val > Ureal_1 loop
12425 Val := Val / Ureal_10;
12426 Scale_Val := Scale_Val - 1;
12427 end loop;
12428
12429 if Scale_Val < -18 then
12430 Error_Msg_N ("scale is less than minimum value of -18", Def);
12431 Scale_Val := UI_From_Int (-18);
12432 end if;
12433 end if;
12434
12435 if Val /= Ureal_1 then
12436 Error_Msg_N ("delta expression must be a power of 10", Def);
12437 Delta_Val := Ureal_10 ** (-Scale_Val);
12438 end if;
12439 end;
12440
12441 -- Set delta, scale and small (small = delta for decimal type)
12442
12443 Set_Delta_Value (Implicit_Base, Delta_Val);
12444 Set_Scale_Value (Implicit_Base, Scale_Val);
12445 Set_Small_Value (Implicit_Base, Delta_Val);
12446
12447 -- Analyze and process digits expression
12448
12449 Analyze_And_Resolve (Digs_Expr, Any_Integer);
12450 Check_Digits_Expression (Digs_Expr);
12451 Digs_Val := Expr_Value (Digs_Expr);
12452
12453 if Digs_Val > 18 then
12454 Digs_Val := UI_From_Int (+18);
12455 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
12456 end if;
12457
12458 Set_Digits_Value (Implicit_Base, Digs_Val);
12459 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
12460
12461 -- Set range of base type from digits value for now. This will be
12462 -- expanded to represent the true underlying base range by Freeze.
12463
12464 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
12465
12466 -- Note: We leave size as zero for now, size will be set at freeze
12467 -- time. We have to do this for ordinary fixed-point, because the size
12468 -- depends on the specified small, and we might as well do the same for
12469 -- decimal fixed-point.
12470
12471 pragma Assert (Esize (Implicit_Base) = Uint_0);
12472
12473 -- If there are bounds given in the declaration use them as the
12474 -- bounds of the first named subtype.
12475
12476 if Present (Real_Range_Specification (Def)) then
12477 declare
12478 RRS : constant Node_Id := Real_Range_Specification (Def);
12479 Low : constant Node_Id := Low_Bound (RRS);
12480 High : constant Node_Id := High_Bound (RRS);
12481 Low_Val : Ureal;
12482 High_Val : Ureal;
12483
12484 begin
12485 Analyze_And_Resolve (Low, Any_Real);
12486 Analyze_And_Resolve (High, Any_Real);
12487 Check_Real_Bound (Low);
12488 Check_Real_Bound (High);
12489 Low_Val := Expr_Value_R (Low);
12490 High_Val := Expr_Value_R (High);
12491
12492 if Low_Val < (-Bound_Val) then
12493 Error_Msg_N
12494 ("range low bound too small for digits value", Low);
12495 Low_Val := -Bound_Val;
12496 end if;
12497
12498 if High_Val > Bound_Val then
12499 Error_Msg_N
12500 ("range high bound too large for digits value", High);
12501 High_Val := Bound_Val;
12502 end if;
12503
12504 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
12505 end;
12506
12507 -- If no explicit range, use range that corresponds to given
12508 -- digits value. This will end up as the final range for the
12509 -- first subtype.
12510
12511 else
12512 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
12513 end if;
12514
12515 -- Complete entity for first subtype
12516
12517 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
12518 Set_Etype (T, Implicit_Base);
12519 Set_Size_Info (T, Implicit_Base);
12520 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
12521 Set_Digits_Value (T, Digs_Val);
12522 Set_Delta_Value (T, Delta_Val);
12523 Set_Small_Value (T, Delta_Val);
12524 Set_Scale_Value (T, Scale_Val);
12525 Set_Is_Constrained (T);
12526 end Decimal_Fixed_Point_Type_Declaration;
12527
12528 -----------------------------------
12529 -- Derive_Progenitor_Subprograms --
12530 -----------------------------------
12531
12532 procedure Derive_Progenitor_Subprograms
12533 (Parent_Type : Entity_Id;
12534 Tagged_Type : Entity_Id)
12535 is
12536 E : Entity_Id;
12537 Elmt : Elmt_Id;
12538 Iface : Entity_Id;
12539 Iface_Elmt : Elmt_Id;
12540 Iface_Subp : Entity_Id;
12541 New_Subp : Entity_Id := Empty;
12542 Prim_Elmt : Elmt_Id;
12543 Subp : Entity_Id;
12544 Typ : Entity_Id;
12545
12546 begin
12547 pragma Assert (Ada_Version >= Ada_2005
12548 and then Is_Record_Type (Tagged_Type)
12549 and then Is_Tagged_Type (Tagged_Type)
12550 and then Has_Interfaces (Tagged_Type));
12551
12552 -- Step 1: Transfer to the full-view primitives associated with the
12553 -- partial-view that cover interface primitives. Conceptually this
12554 -- work should be done later by Process_Full_View; done here to
12555 -- simplify its implementation at later stages. It can be safely
12556 -- done here because interfaces must be visible in the partial and
12557 -- private view (RM 7.3(7.3/2)).
12558
12559 -- Small optimization: This work is only required if the parent is
12560 -- abstract. If the tagged type is not abstract, it cannot have
12561 -- abstract primitives (the only entities in the list of primitives of
12562 -- non-abstract tagged types that can reference abstract primitives
12563 -- through its Alias attribute are the internal entities that have
12564 -- attribute Interface_Alias, and these entities are generated later
12565 -- by Add_Internal_Interface_Entities).
12566
12567 if In_Private_Part (Current_Scope)
12568 and then Is_Abstract_Type (Parent_Type)
12569 then
12570 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
12571 while Present (Elmt) loop
12572 Subp := Node (Elmt);
12573
12574 -- At this stage it is not possible to have entities in the list
12575 -- of primitives that have attribute Interface_Alias
12576
12577 pragma Assert (No (Interface_Alias (Subp)));
12578
12579 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
12580
12581 if Is_Interface (Typ) then
12582 E := Find_Primitive_Covering_Interface
12583 (Tagged_Type => Tagged_Type,
12584 Iface_Prim => Subp);
12585
12586 if Present (E)
12587 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
12588 then
12589 Replace_Elmt (Elmt, E);
12590 Remove_Homonym (Subp);
12591 end if;
12592 end if;
12593
12594 Next_Elmt (Elmt);
12595 end loop;
12596 end if;
12597
12598 -- Step 2: Add primitives of progenitors that are not implemented by
12599 -- parents of Tagged_Type
12600
12601 if Present (Interfaces (Base_Type (Tagged_Type))) then
12602 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
12603 while Present (Iface_Elmt) loop
12604 Iface := Node (Iface_Elmt);
12605
12606 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
12607 while Present (Prim_Elmt) loop
12608 Iface_Subp := Node (Prim_Elmt);
12609
12610 -- Exclude derivation of predefined primitives except those
12611 -- that come from source. Required to catch declarations of
12612 -- equality operators of interfaces. For example:
12613
12614 -- type Iface is interface;
12615 -- function "=" (Left, Right : Iface) return Boolean;
12616
12617 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
12618 or else Comes_From_Source (Iface_Subp)
12619 then
12620 E := Find_Primitive_Covering_Interface
12621 (Tagged_Type => Tagged_Type,
12622 Iface_Prim => Iface_Subp);
12623
12624 -- If not found we derive a new primitive leaving its alias
12625 -- attribute referencing the interface primitive
12626
12627 if No (E) then
12628 Derive_Subprogram
12629 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12630
12631 -- Ada 2012 (AI05-0197): If the covering primitive's name
12632 -- differs from the name of the interface primitive then it
12633 -- is a private primitive inherited from a parent type. In
12634 -- such case, given that Tagged_Type covers the interface,
12635 -- the inherited private primitive becomes visible. For such
12636 -- purpose we add a new entity that renames the inherited
12637 -- private primitive.
12638
12639 elsif Chars (E) /= Chars (Iface_Subp) then
12640 pragma Assert (Has_Suffix (E, 'P'));
12641 Derive_Subprogram
12642 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12643 Set_Alias (New_Subp, E);
12644 Set_Is_Abstract_Subprogram (New_Subp,
12645 Is_Abstract_Subprogram (E));
12646
12647 -- Propagate to the full view interface entities associated
12648 -- with the partial view
12649
12650 elsif In_Private_Part (Current_Scope)
12651 and then Present (Alias (E))
12652 and then Alias (E) = Iface_Subp
12653 and then
12654 List_Containing (Parent (E)) /=
12655 Private_Declarations
12656 (Specification
12657 (Unit_Declaration_Node (Current_Scope)))
12658 then
12659 Append_Elmt (E, Primitive_Operations (Tagged_Type));
12660 end if;
12661 end if;
12662
12663 Next_Elmt (Prim_Elmt);
12664 end loop;
12665
12666 Next_Elmt (Iface_Elmt);
12667 end loop;
12668 end if;
12669 end Derive_Progenitor_Subprograms;
12670
12671 -----------------------
12672 -- Derive_Subprogram --
12673 -----------------------
12674
12675 procedure Derive_Subprogram
12676 (New_Subp : in out Entity_Id;
12677 Parent_Subp : Entity_Id;
12678 Derived_Type : Entity_Id;
12679 Parent_Type : Entity_Id;
12680 Actual_Subp : Entity_Id := Empty)
12681 is
12682 Formal : Entity_Id;
12683 -- Formal parameter of parent primitive operation
12684
12685 Formal_Of_Actual : Entity_Id;
12686 -- Formal parameter of actual operation, when the derivation is to
12687 -- create a renaming for a primitive operation of an actual in an
12688 -- instantiation.
12689
12690 New_Formal : Entity_Id;
12691 -- Formal of inherited operation
12692
12693 Visible_Subp : Entity_Id := Parent_Subp;
12694
12695 function Is_Private_Overriding return Boolean;
12696 -- If Subp is a private overriding of a visible operation, the inherited
12697 -- operation derives from the overridden op (even though its body is the
12698 -- overriding one) and the inherited operation is visible now. See
12699 -- sem_disp to see the full details of the handling of the overridden
12700 -- subprogram, which is removed from the list of primitive operations of
12701 -- the type. The overridden subprogram is saved locally in Visible_Subp,
12702 -- and used to diagnose abstract operations that need overriding in the
12703 -- derived type.
12704
12705 procedure Replace_Type (Id, New_Id : Entity_Id);
12706 -- When the type is an anonymous access type, create a new access type
12707 -- designating the derived type.
12708
12709 procedure Set_Derived_Name;
12710 -- This procedure sets the appropriate Chars name for New_Subp. This
12711 -- is normally just a copy of the parent name. An exception arises for
12712 -- type support subprograms, where the name is changed to reflect the
12713 -- name of the derived type, e.g. if type foo is derived from type bar,
12714 -- then a procedure barDA is derived with a name fooDA.
12715
12716 ---------------------------
12717 -- Is_Private_Overriding --
12718 ---------------------------
12719
12720 function Is_Private_Overriding return Boolean is
12721 Prev : Entity_Id;
12722
12723 begin
12724 -- If the parent is not a dispatching operation there is no
12725 -- need to investigate overridings
12726
12727 if not Is_Dispatching_Operation (Parent_Subp) then
12728 return False;
12729 end if;
12730
12731 -- The visible operation that is overridden is a homonym of the
12732 -- parent subprogram. We scan the homonym chain to find the one
12733 -- whose alias is the subprogram we are deriving.
12734
12735 Prev := Current_Entity (Parent_Subp);
12736 while Present (Prev) loop
12737 if Ekind (Prev) = Ekind (Parent_Subp)
12738 and then Alias (Prev) = Parent_Subp
12739 and then Scope (Parent_Subp) = Scope (Prev)
12740 and then not Is_Hidden (Prev)
12741 then
12742 Visible_Subp := Prev;
12743 return True;
12744 end if;
12745
12746 Prev := Homonym (Prev);
12747 end loop;
12748
12749 return False;
12750 end Is_Private_Overriding;
12751
12752 ------------------
12753 -- Replace_Type --
12754 ------------------
12755
12756 procedure Replace_Type (Id, New_Id : Entity_Id) is
12757 Acc_Type : Entity_Id;
12758 Par : constant Node_Id := Parent (Derived_Type);
12759
12760 begin
12761 -- When the type is an anonymous access type, create a new access
12762 -- type designating the derived type. This itype must be elaborated
12763 -- at the point of the derivation, not on subsequent calls that may
12764 -- be out of the proper scope for Gigi, so we insert a reference to
12765 -- it after the derivation.
12766
12767 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
12768 declare
12769 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
12770
12771 begin
12772 if Ekind (Desig_Typ) = E_Record_Type_With_Private
12773 and then Present (Full_View (Desig_Typ))
12774 and then not Is_Private_Type (Parent_Type)
12775 then
12776 Desig_Typ := Full_View (Desig_Typ);
12777 end if;
12778
12779 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
12780
12781 -- Ada 2005 (AI-251): Handle also derivations of abstract
12782 -- interface primitives.
12783
12784 or else (Is_Interface (Desig_Typ)
12785 and then not Is_Class_Wide_Type (Desig_Typ))
12786 then
12787 Acc_Type := New_Copy (Etype (Id));
12788 Set_Etype (Acc_Type, Acc_Type);
12789 Set_Scope (Acc_Type, New_Subp);
12790
12791 -- Compute size of anonymous access type
12792
12793 if Is_Array_Type (Desig_Typ)
12794 and then not Is_Constrained (Desig_Typ)
12795 then
12796 Init_Size (Acc_Type, 2 * System_Address_Size);
12797 else
12798 Init_Size (Acc_Type, System_Address_Size);
12799 end if;
12800
12801 Init_Alignment (Acc_Type);
12802 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
12803
12804 Set_Etype (New_Id, Acc_Type);
12805 Set_Scope (New_Id, New_Subp);
12806
12807 -- Create a reference to it
12808 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
12809
12810 else
12811 Set_Etype (New_Id, Etype (Id));
12812 end if;
12813 end;
12814
12815 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
12816 or else
12817 (Ekind (Etype (Id)) = E_Record_Type_With_Private
12818 and then Present (Full_View (Etype (Id)))
12819 and then
12820 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
12821 then
12822 -- Constraint checks on formals are generated during expansion,
12823 -- based on the signature of the original subprogram. The bounds
12824 -- of the derived type are not relevant, and thus we can use
12825 -- the base type for the formals. However, the return type may be
12826 -- used in a context that requires that the proper static bounds
12827 -- be used (a case statement, for example) and for those cases
12828 -- we must use the derived type (first subtype), not its base.
12829
12830 -- If the derived_type_definition has no constraints, we know that
12831 -- the derived type has the same constraints as the first subtype
12832 -- of the parent, and we can also use it rather than its base,
12833 -- which can lead to more efficient code.
12834
12835 if Etype (Id) = Parent_Type then
12836 if Is_Scalar_Type (Parent_Type)
12837 and then
12838 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
12839 then
12840 Set_Etype (New_Id, Derived_Type);
12841
12842 elsif Nkind (Par) = N_Full_Type_Declaration
12843 and then
12844 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
12845 and then
12846 Is_Entity_Name
12847 (Subtype_Indication (Type_Definition (Par)))
12848 then
12849 Set_Etype (New_Id, Derived_Type);
12850
12851 else
12852 Set_Etype (New_Id, Base_Type (Derived_Type));
12853 end if;
12854
12855 else
12856 Set_Etype (New_Id, Base_Type (Derived_Type));
12857 end if;
12858
12859 else
12860 Set_Etype (New_Id, Etype (Id));
12861 end if;
12862 end Replace_Type;
12863
12864 ----------------------
12865 -- Set_Derived_Name --
12866 ----------------------
12867
12868 procedure Set_Derived_Name is
12869 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
12870 begin
12871 if Nm = TSS_Null then
12872 Set_Chars (New_Subp, Chars (Parent_Subp));
12873 else
12874 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
12875 end if;
12876 end Set_Derived_Name;
12877
12878 -- Start of processing for Derive_Subprogram
12879
12880 begin
12881 New_Subp :=
12882 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
12883 Set_Ekind (New_Subp, Ekind (Parent_Subp));
12884
12885 -- Check whether the inherited subprogram is a private operation that
12886 -- should be inherited but not yet made visible. Such subprograms can
12887 -- become visible at a later point (e.g., the private part of a public
12888 -- child unit) via Declare_Inherited_Private_Subprograms. If the
12889 -- following predicate is true, then this is not such a private
12890 -- operation and the subprogram simply inherits the name of the parent
12891 -- subprogram. Note the special check for the names of controlled
12892 -- operations, which are currently exempted from being inherited with
12893 -- a hidden name because they must be findable for generation of
12894 -- implicit run-time calls.
12895
12896 if not Is_Hidden (Parent_Subp)
12897 or else Is_Internal (Parent_Subp)
12898 or else Is_Private_Overriding
12899 or else Is_Internal_Name (Chars (Parent_Subp))
12900 or else Chars (Parent_Subp) = Name_Initialize
12901 or else Chars (Parent_Subp) = Name_Adjust
12902 or else Chars (Parent_Subp) = Name_Finalize
12903 then
12904 Set_Derived_Name;
12905
12906 -- An inherited dispatching equality will be overridden by an internally
12907 -- generated one, or by an explicit one, so preserve its name and thus
12908 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
12909 -- private operation it may become invisible if the full view has
12910 -- progenitors, and the dispatch table will be malformed.
12911 -- We check that the type is limited to handle the anomalous declaration
12912 -- of Limited_Controlled, which is derived from a non-limited type, and
12913 -- which is handled specially elsewhere as well.
12914
12915 elsif Chars (Parent_Subp) = Name_Op_Eq
12916 and then Is_Dispatching_Operation (Parent_Subp)
12917 and then Etype (Parent_Subp) = Standard_Boolean
12918 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
12919 and then
12920 Etype (First_Formal (Parent_Subp)) =
12921 Etype (Next_Formal (First_Formal (Parent_Subp)))
12922 then
12923 Set_Derived_Name;
12924
12925 -- If parent is hidden, this can be a regular derivation if the
12926 -- parent is immediately visible in a non-instantiating context,
12927 -- or if we are in the private part of an instance. This test
12928 -- should still be refined ???
12929
12930 -- The test for In_Instance_Not_Visible avoids inheriting the derived
12931 -- operation as a non-visible operation in cases where the parent
12932 -- subprogram might not be visible now, but was visible within the
12933 -- original generic, so it would be wrong to make the inherited
12934 -- subprogram non-visible now. (Not clear if this test is fully
12935 -- correct; are there any cases where we should declare the inherited
12936 -- operation as not visible to avoid it being overridden, e.g., when
12937 -- the parent type is a generic actual with private primitives ???)
12938
12939 -- (they should be treated the same as other private inherited
12940 -- subprograms, but it's not clear how to do this cleanly). ???
12941
12942 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
12943 and then Is_Immediately_Visible (Parent_Subp)
12944 and then not In_Instance)
12945 or else In_Instance_Not_Visible
12946 then
12947 Set_Derived_Name;
12948
12949 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
12950 -- overrides an interface primitive because interface primitives
12951 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
12952
12953 elsif Ada_Version >= Ada_2005
12954 and then Is_Dispatching_Operation (Parent_Subp)
12955 and then Covers_Some_Interface (Parent_Subp)
12956 then
12957 Set_Derived_Name;
12958
12959 -- Otherwise, the type is inheriting a private operation, so enter
12960 -- it with a special name so it can't be overridden.
12961
12962 else
12963 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
12964 end if;
12965
12966 Set_Parent (New_Subp, Parent (Derived_Type));
12967
12968 if Present (Actual_Subp) then
12969 Replace_Type (Actual_Subp, New_Subp);
12970 else
12971 Replace_Type (Parent_Subp, New_Subp);
12972 end if;
12973
12974 Conditional_Delay (New_Subp, Parent_Subp);
12975
12976 -- If we are creating a renaming for a primitive operation of an
12977 -- actual of a generic derived type, we must examine the signature
12978 -- of the actual primitive, not that of the generic formal, which for
12979 -- example may be an interface. However the name and initial value
12980 -- of the inherited operation are those of the formal primitive.
12981
12982 Formal := First_Formal (Parent_Subp);
12983
12984 if Present (Actual_Subp) then
12985 Formal_Of_Actual := First_Formal (Actual_Subp);
12986 else
12987 Formal_Of_Actual := Empty;
12988 end if;
12989
12990 while Present (Formal) loop
12991 New_Formal := New_Copy (Formal);
12992
12993 -- Normally we do not go copying parents, but in the case of
12994 -- formals, we need to link up to the declaration (which is the
12995 -- parameter specification), and it is fine to link up to the
12996 -- original formal's parameter specification in this case.
12997
12998 Set_Parent (New_Formal, Parent (Formal));
12999 Append_Entity (New_Formal, New_Subp);
13000
13001 if Present (Formal_Of_Actual) then
13002 Replace_Type (Formal_Of_Actual, New_Formal);
13003 Next_Formal (Formal_Of_Actual);
13004 else
13005 Replace_Type (Formal, New_Formal);
13006 end if;
13007
13008 Next_Formal (Formal);
13009 end loop;
13010
13011 -- If this derivation corresponds to a tagged generic actual, then
13012 -- primitive operations rename those of the actual. Otherwise the
13013 -- primitive operations rename those of the parent type, If the parent
13014 -- renames an intrinsic operator, so does the new subprogram. We except
13015 -- concatenation, which is always properly typed, and does not get
13016 -- expanded as other intrinsic operations.
13017
13018 if No (Actual_Subp) then
13019 if Is_Intrinsic_Subprogram (Parent_Subp) then
13020 Set_Is_Intrinsic_Subprogram (New_Subp);
13021
13022 if Present (Alias (Parent_Subp))
13023 and then Chars (Parent_Subp) /= Name_Op_Concat
13024 then
13025 Set_Alias (New_Subp, Alias (Parent_Subp));
13026 else
13027 Set_Alias (New_Subp, Parent_Subp);
13028 end if;
13029
13030 else
13031 Set_Alias (New_Subp, Parent_Subp);
13032 end if;
13033
13034 else
13035 Set_Alias (New_Subp, Actual_Subp);
13036 end if;
13037
13038 -- Derived subprograms of a tagged type must inherit the convention
13039 -- of the parent subprogram (a requirement of AI-117). Derived
13040 -- subprograms of untagged types simply get convention Ada by default.
13041
13042 if Is_Tagged_Type (Derived_Type) then
13043 Set_Convention (New_Subp, Convention (Parent_Subp));
13044 end if;
13045
13046 -- Predefined controlled operations retain their name even if the parent
13047 -- is hidden (see above), but they are not primitive operations if the
13048 -- ancestor is not visible, for example if the parent is a private
13049 -- extension completed with a controlled extension. Note that a full
13050 -- type that is controlled can break privacy: the flag Is_Controlled is
13051 -- set on both views of the type.
13052
13053 if Is_Controlled (Parent_Type)
13054 and then
13055 (Chars (Parent_Subp) = Name_Initialize
13056 or else Chars (Parent_Subp) = Name_Adjust
13057 or else Chars (Parent_Subp) = Name_Finalize)
13058 and then Is_Hidden (Parent_Subp)
13059 and then not Is_Visibly_Controlled (Parent_Type)
13060 then
13061 Set_Is_Hidden (New_Subp);
13062 end if;
13063
13064 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
13065 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
13066
13067 if Ekind (Parent_Subp) = E_Procedure then
13068 Set_Is_Valued_Procedure
13069 (New_Subp, Is_Valued_Procedure (Parent_Subp));
13070 else
13071 Set_Has_Controlling_Result
13072 (New_Subp, Has_Controlling_Result (Parent_Subp));
13073 end if;
13074
13075 -- No_Return must be inherited properly. If this is overridden in the
13076 -- case of a dispatching operation, then a check is made in Sem_Disp
13077 -- that the overriding operation is also No_Return (no such check is
13078 -- required for the case of non-dispatching operation.
13079
13080 Set_No_Return (New_Subp, No_Return (Parent_Subp));
13081
13082 -- A derived function with a controlling result is abstract. If the
13083 -- Derived_Type is a nonabstract formal generic derived type, then
13084 -- inherited operations are not abstract: the required check is done at
13085 -- instantiation time. If the derivation is for a generic actual, the
13086 -- function is not abstract unless the actual is.
13087
13088 if Is_Generic_Type (Derived_Type)
13089 and then not Is_Abstract_Type (Derived_Type)
13090 then
13091 null;
13092
13093 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
13094 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
13095
13096 elsif Ada_Version >= Ada_2005
13097 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13098 or else (Is_Tagged_Type (Derived_Type)
13099 and then Etype (New_Subp) = Derived_Type
13100 and then not Is_Null_Extension (Derived_Type))
13101 or else (Is_Tagged_Type (Derived_Type)
13102 and then Ekind (Etype (New_Subp)) =
13103 E_Anonymous_Access_Type
13104 and then Designated_Type (Etype (New_Subp)) =
13105 Derived_Type
13106 and then not Is_Null_Extension (Derived_Type)))
13107 and then No (Actual_Subp)
13108 then
13109 if not Is_Tagged_Type (Derived_Type)
13110 or else Is_Abstract_Type (Derived_Type)
13111 or else Is_Abstract_Subprogram (Alias (New_Subp))
13112 then
13113 Set_Is_Abstract_Subprogram (New_Subp);
13114 else
13115 Set_Requires_Overriding (New_Subp);
13116 end if;
13117
13118 elsif Ada_Version < Ada_2005
13119 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13120 or else (Is_Tagged_Type (Derived_Type)
13121 and then Etype (New_Subp) = Derived_Type
13122 and then No (Actual_Subp)))
13123 then
13124 Set_Is_Abstract_Subprogram (New_Subp);
13125
13126 -- AI05-0097 : an inherited operation that dispatches on result is
13127 -- abstract if the derived type is abstract, even if the parent type
13128 -- is concrete and the derived type is a null extension.
13129
13130 elsif Has_Controlling_Result (Alias (New_Subp))
13131 and then Is_Abstract_Type (Etype (New_Subp))
13132 then
13133 Set_Is_Abstract_Subprogram (New_Subp);
13134
13135 -- Finally, if the parent type is abstract we must verify that all
13136 -- inherited operations are either non-abstract or overridden, or that
13137 -- the derived type itself is abstract (this check is performed at the
13138 -- end of a package declaration, in Check_Abstract_Overriding). A
13139 -- private overriding in the parent type will not be visible in the
13140 -- derivation if we are not in an inner package or in a child unit of
13141 -- the parent type, in which case the abstractness of the inherited
13142 -- operation is carried to the new subprogram.
13143
13144 elsif Is_Abstract_Type (Parent_Type)
13145 and then not In_Open_Scopes (Scope (Parent_Type))
13146 and then Is_Private_Overriding
13147 and then Is_Abstract_Subprogram (Visible_Subp)
13148 then
13149 if No (Actual_Subp) then
13150 Set_Alias (New_Subp, Visible_Subp);
13151 Set_Is_Abstract_Subprogram (New_Subp, True);
13152
13153 else
13154 -- If this is a derivation for an instance of a formal derived
13155 -- type, abstractness comes from the primitive operation of the
13156 -- actual, not from the operation inherited from the ancestor.
13157
13158 Set_Is_Abstract_Subprogram
13159 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
13160 end if;
13161 end if;
13162
13163 New_Overloaded_Entity (New_Subp, Derived_Type);
13164
13165 -- Check for case of a derived subprogram for the instantiation of a
13166 -- formal derived tagged type, if so mark the subprogram as dispatching
13167 -- and inherit the dispatching attributes of the parent subprogram. The
13168 -- derived subprogram is effectively renaming of the actual subprogram,
13169 -- so it needs to have the same attributes as the actual.
13170
13171 if Present (Actual_Subp)
13172 and then Is_Dispatching_Operation (Parent_Subp)
13173 then
13174 Set_Is_Dispatching_Operation (New_Subp);
13175
13176 if Present (DTC_Entity (Parent_Subp)) then
13177 Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
13178 Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
13179 end if;
13180 end if;
13181
13182 -- Indicate that a derived subprogram does not require a body and that
13183 -- it does not require processing of default expressions.
13184
13185 Set_Has_Completion (New_Subp);
13186 Set_Default_Expressions_Processed (New_Subp);
13187
13188 if Ekind (New_Subp) = E_Function then
13189 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
13190 end if;
13191 end Derive_Subprogram;
13192
13193 ------------------------
13194 -- Derive_Subprograms --
13195 ------------------------
13196
13197 procedure Derive_Subprograms
13198 (Parent_Type : Entity_Id;
13199 Derived_Type : Entity_Id;
13200 Generic_Actual : Entity_Id := Empty)
13201 is
13202 Op_List : constant Elist_Id :=
13203 Collect_Primitive_Operations (Parent_Type);
13204
13205 function Check_Derived_Type return Boolean;
13206 -- Check that all the entities derived from Parent_Type are found in
13207 -- the list of primitives of Derived_Type exactly in the same order.
13208
13209 procedure Derive_Interface_Subprogram
13210 (New_Subp : in out Entity_Id;
13211 Subp : Entity_Id;
13212 Actual_Subp : Entity_Id);
13213 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
13214 -- (which is an interface primitive). If Generic_Actual is present then
13215 -- Actual_Subp is the actual subprogram corresponding with the generic
13216 -- subprogram Subp.
13217
13218 function Check_Derived_Type return Boolean is
13219 E : Entity_Id;
13220 Elmt : Elmt_Id;
13221 List : Elist_Id;
13222 New_Subp : Entity_Id;
13223 Op_Elmt : Elmt_Id;
13224 Subp : Entity_Id;
13225
13226 begin
13227 -- Traverse list of entities in the current scope searching for
13228 -- an incomplete type whose full-view is derived type
13229
13230 E := First_Entity (Scope (Derived_Type));
13231 while Present (E)
13232 and then E /= Derived_Type
13233 loop
13234 if Ekind (E) = E_Incomplete_Type
13235 and then Present (Full_View (E))
13236 and then Full_View (E) = Derived_Type
13237 then
13238 -- Disable this test if Derived_Type completes an incomplete
13239 -- type because in such case more primitives can be added
13240 -- later to the list of primitives of Derived_Type by routine
13241 -- Process_Incomplete_Dependents
13242
13243 return True;
13244 end if;
13245
13246 E := Next_Entity (E);
13247 end loop;
13248
13249 List := Collect_Primitive_Operations (Derived_Type);
13250 Elmt := First_Elmt (List);
13251
13252 Op_Elmt := First_Elmt (Op_List);
13253 while Present (Op_Elmt) loop
13254 Subp := Node (Op_Elmt);
13255 New_Subp := Node (Elmt);
13256
13257 -- At this early stage Derived_Type has no entities with attribute
13258 -- Interface_Alias. In addition, such primitives are always
13259 -- located at the end of the list of primitives of Parent_Type.
13260 -- Therefore, if found we can safely stop processing pending
13261 -- entities.
13262
13263 exit when Present (Interface_Alias (Subp));
13264
13265 -- Handle hidden entities
13266
13267 if not Is_Predefined_Dispatching_Operation (Subp)
13268 and then Is_Hidden (Subp)
13269 then
13270 if Present (New_Subp)
13271 and then Primitive_Names_Match (Subp, New_Subp)
13272 then
13273 Next_Elmt (Elmt);
13274 end if;
13275
13276 else
13277 if not Present (New_Subp)
13278 or else Ekind (Subp) /= Ekind (New_Subp)
13279 or else not Primitive_Names_Match (Subp, New_Subp)
13280 then
13281 return False;
13282 end if;
13283
13284 Next_Elmt (Elmt);
13285 end if;
13286
13287 Next_Elmt (Op_Elmt);
13288 end loop;
13289
13290 return True;
13291 end Check_Derived_Type;
13292
13293 ---------------------------------
13294 -- Derive_Interface_Subprogram --
13295 ---------------------------------
13296
13297 procedure Derive_Interface_Subprogram
13298 (New_Subp : in out Entity_Id;
13299 Subp : Entity_Id;
13300 Actual_Subp : Entity_Id)
13301 is
13302 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
13303 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
13304
13305 begin
13306 pragma Assert (Is_Interface (Iface_Type));
13307
13308 Derive_Subprogram
13309 (New_Subp => New_Subp,
13310 Parent_Subp => Iface_Subp,
13311 Derived_Type => Derived_Type,
13312 Parent_Type => Iface_Type,
13313 Actual_Subp => Actual_Subp);
13314
13315 -- Given that this new interface entity corresponds with a primitive
13316 -- of the parent that was not overridden we must leave it associated
13317 -- with its parent primitive to ensure that it will share the same
13318 -- dispatch table slot when overridden.
13319
13320 if No (Actual_Subp) then
13321 Set_Alias (New_Subp, Subp);
13322
13323 -- For instantiations this is not needed since the previous call to
13324 -- Derive_Subprogram leaves the entity well decorated.
13325
13326 else
13327 pragma Assert (Alias (New_Subp) = Actual_Subp);
13328 null;
13329 end if;
13330 end Derive_Interface_Subprogram;
13331
13332 -- Local variables
13333
13334 Alias_Subp : Entity_Id;
13335 Act_List : Elist_Id;
13336 Act_Elmt : Elmt_Id := No_Elmt;
13337 Act_Subp : Entity_Id := Empty;
13338 Elmt : Elmt_Id;
13339 Need_Search : Boolean := False;
13340 New_Subp : Entity_Id := Empty;
13341 Parent_Base : Entity_Id;
13342 Subp : Entity_Id;
13343
13344 -- Start of processing for Derive_Subprograms
13345
13346 begin
13347 if Ekind (Parent_Type) = E_Record_Type_With_Private
13348 and then Has_Discriminants (Parent_Type)
13349 and then Present (Full_View (Parent_Type))
13350 then
13351 Parent_Base := Full_View (Parent_Type);
13352 else
13353 Parent_Base := Parent_Type;
13354 end if;
13355
13356 if Present (Generic_Actual) then
13357 Act_List := Collect_Primitive_Operations (Generic_Actual);
13358 Act_Elmt := First_Elmt (Act_List);
13359 end if;
13360
13361 -- Derive primitives inherited from the parent. Note that if the generic
13362 -- actual is present, this is not really a type derivation, it is a
13363 -- completion within an instance.
13364
13365 -- Case 1: Derived_Type does not implement interfaces
13366
13367 if not Is_Tagged_Type (Derived_Type)
13368 or else (not Has_Interfaces (Derived_Type)
13369 and then not (Present (Generic_Actual)
13370 and then
13371 Has_Interfaces (Generic_Actual)))
13372 then
13373 Elmt := First_Elmt (Op_List);
13374 while Present (Elmt) loop
13375 Subp := Node (Elmt);
13376
13377 -- Literals are derived earlier in the process of building the
13378 -- derived type, and are skipped here.
13379
13380 if Ekind (Subp) = E_Enumeration_Literal then
13381 null;
13382
13383 -- The actual is a direct descendant and the common primitive
13384 -- operations appear in the same order.
13385
13386 -- If the generic parent type is present, the derived type is an
13387 -- instance of a formal derived type, and within the instance its
13388 -- operations are those of the actual. We derive from the formal
13389 -- type but make the inherited operations aliases of the
13390 -- corresponding operations of the actual.
13391
13392 else
13393 pragma Assert (No (Node (Act_Elmt))
13394 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
13395 and then
13396 Type_Conformant (Subp, Node (Act_Elmt),
13397 Skip_Controlling_Formals => True)));
13398
13399 Derive_Subprogram
13400 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
13401
13402 if Present (Act_Elmt) then
13403 Next_Elmt (Act_Elmt);
13404 end if;
13405 end if;
13406
13407 Next_Elmt (Elmt);
13408 end loop;
13409
13410 -- Case 2: Derived_Type implements interfaces
13411
13412 else
13413 -- If the parent type has no predefined primitives we remove
13414 -- predefined primitives from the list of primitives of generic
13415 -- actual to simplify the complexity of this algorithm.
13416
13417 if Present (Generic_Actual) then
13418 declare
13419 Has_Predefined_Primitives : Boolean := False;
13420
13421 begin
13422 -- Check if the parent type has predefined primitives
13423
13424 Elmt := First_Elmt (Op_List);
13425 while Present (Elmt) loop
13426 Subp := Node (Elmt);
13427
13428 if Is_Predefined_Dispatching_Operation (Subp)
13429 and then not Comes_From_Source (Ultimate_Alias (Subp))
13430 then
13431 Has_Predefined_Primitives := True;
13432 exit;
13433 end if;
13434
13435 Next_Elmt (Elmt);
13436 end loop;
13437
13438 -- Remove predefined primitives of Generic_Actual. We must use
13439 -- an auxiliary list because in case of tagged types the value
13440 -- returned by Collect_Primitive_Operations is the value stored
13441 -- in its Primitive_Operations attribute (and we don't want to
13442 -- modify its current contents).
13443
13444 if not Has_Predefined_Primitives then
13445 declare
13446 Aux_List : constant Elist_Id := New_Elmt_List;
13447
13448 begin
13449 Elmt := First_Elmt (Act_List);
13450 while Present (Elmt) loop
13451 Subp := Node (Elmt);
13452
13453 if not Is_Predefined_Dispatching_Operation (Subp)
13454 or else Comes_From_Source (Subp)
13455 then
13456 Append_Elmt (Subp, Aux_List);
13457 end if;
13458
13459 Next_Elmt (Elmt);
13460 end loop;
13461
13462 Act_List := Aux_List;
13463 end;
13464 end if;
13465
13466 Act_Elmt := First_Elmt (Act_List);
13467 Act_Subp := Node (Act_Elmt);
13468 end;
13469 end if;
13470
13471 -- Stage 1: If the generic actual is not present we derive the
13472 -- primitives inherited from the parent type. If the generic parent
13473 -- type is present, the derived type is an instance of a formal
13474 -- derived type, and within the instance its operations are those of
13475 -- the actual. We derive from the formal type but make the inherited
13476 -- operations aliases of the corresponding operations of the actual.
13477
13478 Elmt := First_Elmt (Op_List);
13479 while Present (Elmt) loop
13480 Subp := Node (Elmt);
13481 Alias_Subp := Ultimate_Alias (Subp);
13482
13483 -- Do not derive internal entities of the parent that link
13484 -- interface primitives with their covering primitive. These
13485 -- entities will be added to this type when frozen.
13486
13487 if Present (Interface_Alias (Subp)) then
13488 goto Continue;
13489 end if;
13490
13491 -- If the generic actual is present find the corresponding
13492 -- operation in the generic actual. If the parent type is a
13493 -- direct ancestor of the derived type then, even if it is an
13494 -- interface, the operations are inherited from the primary
13495 -- dispatch table and are in the proper order. If we detect here
13496 -- that primitives are not in the same order we traverse the list
13497 -- of primitive operations of the actual to find the one that
13498 -- implements the interface primitive.
13499
13500 if Need_Search
13501 or else
13502 (Present (Generic_Actual)
13503 and then Present (Act_Subp)
13504 and then not
13505 (Primitive_Names_Match (Subp, Act_Subp)
13506 and then
13507 Type_Conformant (Subp, Act_Subp,
13508 Skip_Controlling_Formals => True)))
13509 then
13510 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual));
13511
13512 -- Remember that we need searching for all pending primitives
13513
13514 Need_Search := True;
13515
13516 -- Handle entities associated with interface primitives
13517
13518 if Present (Alias_Subp)
13519 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13520 and then not Is_Predefined_Dispatching_Operation (Subp)
13521 then
13522 -- Search for the primitive in the homonym chain
13523
13524 Act_Subp :=
13525 Find_Primitive_Covering_Interface
13526 (Tagged_Type => Generic_Actual,
13527 Iface_Prim => Alias_Subp);
13528
13529 -- Previous search may not locate primitives covering
13530 -- interfaces defined in generics units or instantiations.
13531 -- (it fails if the covering primitive has formals whose
13532 -- type is also defined in generics or instantiations).
13533 -- In such case we search in the list of primitives of the
13534 -- generic actual for the internal entity that links the
13535 -- interface primitive and the covering primitive.
13536
13537 if No (Act_Subp)
13538 and then Is_Generic_Type (Parent_Type)
13539 then
13540 -- This code has been designed to handle only generic
13541 -- formals that implement interfaces that are defined
13542 -- in a generic unit or instantiation. If this code is
13543 -- needed for other cases we must review it because
13544 -- (given that it relies on Original_Location to locate
13545 -- the primitive of Generic_Actual that covers the
13546 -- interface) it could leave linked through attribute
13547 -- Alias entities of unrelated instantiations).
13548
13549 pragma Assert
13550 (Is_Generic_Unit
13551 (Scope (Find_Dispatching_Type (Alias_Subp)))
13552 or else
13553 Instantiation_Depth
13554 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
13555
13556 declare
13557 Iface_Prim_Loc : constant Source_Ptr :=
13558 Original_Location (Sloc (Alias_Subp));
13559 Elmt : Elmt_Id;
13560 Prim : Entity_Id;
13561 begin
13562 Elmt :=
13563 First_Elmt (Primitive_Operations (Generic_Actual));
13564
13565 Search : while Present (Elmt) loop
13566 Prim := Node (Elmt);
13567
13568 if Present (Interface_Alias (Prim))
13569 and then Original_Location
13570 (Sloc (Interface_Alias (Prim)))
13571 = Iface_Prim_Loc
13572 then
13573 Act_Subp := Alias (Prim);
13574 exit Search;
13575 end if;
13576
13577 Next_Elmt (Elmt);
13578 end loop Search;
13579 end;
13580 end if;
13581
13582 pragma Assert (Present (Act_Subp)
13583 or else Is_Abstract_Type (Generic_Actual)
13584 or else Serious_Errors_Detected > 0);
13585
13586 -- Handle predefined primitives plus the rest of user-defined
13587 -- primitives
13588
13589 else
13590 Act_Elmt := First_Elmt (Act_List);
13591 while Present (Act_Elmt) loop
13592 Act_Subp := Node (Act_Elmt);
13593
13594 exit when Primitive_Names_Match (Subp, Act_Subp)
13595 and then Type_Conformant
13596 (Subp, Act_Subp,
13597 Skip_Controlling_Formals => True)
13598 and then No (Interface_Alias (Act_Subp));
13599
13600 Next_Elmt (Act_Elmt);
13601 end loop;
13602
13603 if No (Act_Elmt) then
13604 Act_Subp := Empty;
13605 end if;
13606 end if;
13607 end if;
13608
13609 -- Case 1: If the parent is a limited interface then it has the
13610 -- predefined primitives of synchronized interfaces. However, the
13611 -- actual type may be a non-limited type and hence it does not
13612 -- have such primitives.
13613
13614 if Present (Generic_Actual)
13615 and then not Present (Act_Subp)
13616 and then Is_Limited_Interface (Parent_Base)
13617 and then Is_Predefined_Interface_Primitive (Subp)
13618 then
13619 null;
13620
13621 -- Case 2: Inherit entities associated with interfaces that were
13622 -- not covered by the parent type. We exclude here null interface
13623 -- primitives because they do not need special management.
13624
13625 -- We also exclude interface operations that are renamings. If the
13626 -- subprogram is an explicit renaming of an interface primitive,
13627 -- it is a regular primitive operation, and the presence of its
13628 -- alias is not relevant: it has to be derived like any other
13629 -- primitive.
13630
13631 elsif Present (Alias (Subp))
13632 and then Nkind (Unit_Declaration_Node (Subp)) /=
13633 N_Subprogram_Renaming_Declaration
13634 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13635 and then not
13636 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
13637 and then Null_Present (Parent (Alias_Subp)))
13638 then
13639 -- If this is an abstract private type then we transfer the
13640 -- derivation of the interface primitive from the partial view
13641 -- to the full view. This is safe because all the interfaces
13642 -- must be visible in the partial view. Done to avoid adding
13643 -- a new interface derivation to the private part of the
13644 -- enclosing package; otherwise this new derivation would be
13645 -- decorated as hidden when the analysis of the enclosing
13646 -- package completes.
13647
13648 if Is_Abstract_Type (Derived_Type)
13649 and then In_Private_Part (Current_Scope)
13650 and then Has_Private_Declaration (Derived_Type)
13651 then
13652 declare
13653 Partial_View : Entity_Id;
13654 Elmt : Elmt_Id;
13655 Ent : Entity_Id;
13656
13657 begin
13658 Partial_View := First_Entity (Current_Scope);
13659 loop
13660 exit when No (Partial_View)
13661 or else (Has_Private_Declaration (Partial_View)
13662 and then
13663 Full_View (Partial_View) = Derived_Type);
13664
13665 Next_Entity (Partial_View);
13666 end loop;
13667
13668 -- If the partial view was not found then the source code
13669 -- has errors and the derivation is not needed.
13670
13671 if Present (Partial_View) then
13672 Elmt :=
13673 First_Elmt (Primitive_Operations (Partial_View));
13674 while Present (Elmt) loop
13675 Ent := Node (Elmt);
13676
13677 if Present (Alias (Ent))
13678 and then Ultimate_Alias (Ent) = Alias (Subp)
13679 then
13680 Append_Elmt
13681 (Ent, Primitive_Operations (Derived_Type));
13682 exit;
13683 end if;
13684
13685 Next_Elmt (Elmt);
13686 end loop;
13687
13688 -- If the interface primitive was not found in the
13689 -- partial view then this interface primitive was
13690 -- overridden. We add a derivation to activate in
13691 -- Derive_Progenitor_Subprograms the machinery to
13692 -- search for it.
13693
13694 if No (Elmt) then
13695 Derive_Interface_Subprogram
13696 (New_Subp => New_Subp,
13697 Subp => Subp,
13698 Actual_Subp => Act_Subp);
13699 end if;
13700 end if;
13701 end;
13702 else
13703 Derive_Interface_Subprogram
13704 (New_Subp => New_Subp,
13705 Subp => Subp,
13706 Actual_Subp => Act_Subp);
13707 end if;
13708
13709 -- Case 3: Common derivation
13710
13711 else
13712 Derive_Subprogram
13713 (New_Subp => New_Subp,
13714 Parent_Subp => Subp,
13715 Derived_Type => Derived_Type,
13716 Parent_Type => Parent_Base,
13717 Actual_Subp => Act_Subp);
13718 end if;
13719
13720 -- No need to update Act_Elm if we must search for the
13721 -- corresponding operation in the generic actual
13722
13723 if not Need_Search
13724 and then Present (Act_Elmt)
13725 then
13726 Next_Elmt (Act_Elmt);
13727 Act_Subp := Node (Act_Elmt);
13728 end if;
13729
13730 <<Continue>>
13731 Next_Elmt (Elmt);
13732 end loop;
13733
13734 -- Inherit additional operations from progenitors. If the derived
13735 -- type is a generic actual, there are not new primitive operations
13736 -- for the type because it has those of the actual, and therefore
13737 -- nothing needs to be done. The renamings generated above are not
13738 -- primitive operations, and their purpose is simply to make the
13739 -- proper operations visible within an instantiation.
13740
13741 if No (Generic_Actual) then
13742 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
13743 end if;
13744 end if;
13745
13746 -- Final check: Direct descendants must have their primitives in the
13747 -- same order. We exclude from this test untagged types and instances
13748 -- of formal derived types. We skip this test if we have already
13749 -- reported serious errors in the sources.
13750
13751 pragma Assert (not Is_Tagged_Type (Derived_Type)
13752 or else Present (Generic_Actual)
13753 or else Serious_Errors_Detected > 0
13754 or else Check_Derived_Type);
13755 end Derive_Subprograms;
13756
13757 --------------------------------
13758 -- Derived_Standard_Character --
13759 --------------------------------
13760
13761 procedure Derived_Standard_Character
13762 (N : Node_Id;
13763 Parent_Type : Entity_Id;
13764 Derived_Type : Entity_Id)
13765 is
13766 Loc : constant Source_Ptr := Sloc (N);
13767 Def : constant Node_Id := Type_Definition (N);
13768 Indic : constant Node_Id := Subtype_Indication (Def);
13769 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
13770 Implicit_Base : constant Entity_Id :=
13771 Create_Itype
13772 (E_Enumeration_Type, N, Derived_Type, 'B');
13773
13774 Lo : Node_Id;
13775 Hi : Node_Id;
13776
13777 begin
13778 Discard_Node (Process_Subtype (Indic, N));
13779
13780 Set_Etype (Implicit_Base, Parent_Base);
13781 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
13782 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
13783
13784 Set_Is_Character_Type (Implicit_Base, True);
13785 Set_Has_Delayed_Freeze (Implicit_Base);
13786
13787 -- The bounds of the implicit base are the bounds of the parent base.
13788 -- Note that their type is the parent base.
13789
13790 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
13791 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
13792
13793 Set_Scalar_Range (Implicit_Base,
13794 Make_Range (Loc,
13795 Low_Bound => Lo,
13796 High_Bound => Hi));
13797
13798 Conditional_Delay (Derived_Type, Parent_Type);
13799
13800 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
13801 Set_Etype (Derived_Type, Implicit_Base);
13802 Set_Size_Info (Derived_Type, Parent_Type);
13803
13804 if Unknown_RM_Size (Derived_Type) then
13805 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
13806 end if;
13807
13808 Set_Is_Character_Type (Derived_Type, True);
13809
13810 if Nkind (Indic) /= N_Subtype_Indication then
13811
13812 -- If no explicit constraint, the bounds are those
13813 -- of the parent type.
13814
13815 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
13816 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
13817 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
13818 end if;
13819
13820 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
13821
13822 -- Because the implicit base is used in the conversion of the bounds, we
13823 -- have to freeze it now. This is similar to what is done for numeric
13824 -- types, and it equally suspicious, but otherwise a non-static bound
13825 -- will have a reference to an unfrozen type, which is rejected by Gigi
13826 -- (???). This requires specific care for definition of stream
13827 -- attributes. For details, see comments at the end of
13828 -- Build_Derived_Numeric_Type.
13829
13830 Freeze_Before (N, Implicit_Base);
13831 end Derived_Standard_Character;
13832
13833 ------------------------------
13834 -- Derived_Type_Declaration --
13835 ------------------------------
13836
13837 procedure Derived_Type_Declaration
13838 (T : Entity_Id;
13839 N : Node_Id;
13840 Is_Completion : Boolean)
13841 is
13842 Parent_Type : Entity_Id;
13843
13844 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
13845 -- Check whether the parent type is a generic formal, or derives
13846 -- directly or indirectly from one.
13847
13848 ------------------------
13849 -- Comes_From_Generic --
13850 ------------------------
13851
13852 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
13853 begin
13854 if Is_Generic_Type (Typ) then
13855 return True;
13856
13857 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
13858 return True;
13859
13860 elsif Is_Private_Type (Typ)
13861 and then Present (Full_View (Typ))
13862 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
13863 then
13864 return True;
13865
13866 elsif Is_Generic_Actual_Type (Typ) then
13867 return True;
13868
13869 else
13870 return False;
13871 end if;
13872 end Comes_From_Generic;
13873
13874 -- Local variables
13875
13876 Def : constant Node_Id := Type_Definition (N);
13877 Iface_Def : Node_Id;
13878 Indic : constant Node_Id := Subtype_Indication (Def);
13879 Extension : constant Node_Id := Record_Extension_Part (Def);
13880 Parent_Node : Node_Id;
13881 Parent_Scope : Entity_Id;
13882 Taggd : Boolean;
13883
13884 -- Start of processing for Derived_Type_Declaration
13885
13886 begin
13887 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
13888
13889 -- Ada 2005 (AI-251): In case of interface derivation check that the
13890 -- parent is also an interface.
13891
13892 if Interface_Present (Def) then
13893 Check_Formal_Restriction ("interface is not allowed", Def);
13894
13895 if not Is_Interface (Parent_Type) then
13896 Diagnose_Interface (Indic, Parent_Type);
13897
13898 else
13899 Parent_Node := Parent (Base_Type (Parent_Type));
13900 Iface_Def := Type_Definition (Parent_Node);
13901
13902 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
13903 -- other limited interfaces.
13904
13905 if Limited_Present (Def) then
13906 if Limited_Present (Iface_Def) then
13907 null;
13908
13909 elsif Protected_Present (Iface_Def) then
13910 Error_Msg_NE
13911 ("descendant of& must be declared"
13912 & " as a protected interface",
13913 N, Parent_Type);
13914
13915 elsif Synchronized_Present (Iface_Def) then
13916 Error_Msg_NE
13917 ("descendant of& must be declared"
13918 & " as a synchronized interface",
13919 N, Parent_Type);
13920
13921 elsif Task_Present (Iface_Def) then
13922 Error_Msg_NE
13923 ("descendant of& must be declared as a task interface",
13924 N, Parent_Type);
13925
13926 else
13927 Error_Msg_N
13928 ("(Ada 2005) limited interface cannot "
13929 & "inherit from non-limited interface", Indic);
13930 end if;
13931
13932 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
13933 -- from non-limited or limited interfaces.
13934
13935 elsif not Protected_Present (Def)
13936 and then not Synchronized_Present (Def)
13937 and then not Task_Present (Def)
13938 then
13939 if Limited_Present (Iface_Def) then
13940 null;
13941
13942 elsif Protected_Present (Iface_Def) then
13943 Error_Msg_NE
13944 ("descendant of& must be declared"
13945 & " as a protected interface",
13946 N, Parent_Type);
13947
13948 elsif Synchronized_Present (Iface_Def) then
13949 Error_Msg_NE
13950 ("descendant of& must be declared"
13951 & " as a synchronized interface",
13952 N, Parent_Type);
13953
13954 elsif Task_Present (Iface_Def) then
13955 Error_Msg_NE
13956 ("descendant of& must be declared as a task interface",
13957 N, Parent_Type);
13958 else
13959 null;
13960 end if;
13961 end if;
13962 end if;
13963 end if;
13964
13965 if Is_Tagged_Type (Parent_Type)
13966 and then Is_Concurrent_Type (Parent_Type)
13967 and then not Is_Interface (Parent_Type)
13968 then
13969 Error_Msg_N
13970 ("parent type of a record extension cannot be "
13971 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
13972 Set_Etype (T, Any_Type);
13973 return;
13974 end if;
13975
13976 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
13977 -- interfaces
13978
13979 if Is_Tagged_Type (Parent_Type)
13980 and then Is_Non_Empty_List (Interface_List (Def))
13981 then
13982 declare
13983 Intf : Node_Id;
13984 T : Entity_Id;
13985
13986 begin
13987 Intf := First (Interface_List (Def));
13988 while Present (Intf) loop
13989 T := Find_Type_Of_Subtype_Indic (Intf);
13990
13991 if not Is_Interface (T) then
13992 Diagnose_Interface (Intf, T);
13993
13994 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
13995 -- a limited type from having a nonlimited progenitor.
13996
13997 elsif (Limited_Present (Def)
13998 or else (not Is_Interface (Parent_Type)
13999 and then Is_Limited_Type (Parent_Type)))
14000 and then not Is_Limited_Interface (T)
14001 then
14002 Error_Msg_NE
14003 ("progenitor interface& of limited type must be limited",
14004 N, T);
14005 end if;
14006
14007 Next (Intf);
14008 end loop;
14009 end;
14010 end if;
14011
14012 if Parent_Type = Any_Type
14013 or else Etype (Parent_Type) = Any_Type
14014 or else (Is_Class_Wide_Type (Parent_Type)
14015 and then Etype (Parent_Type) = T)
14016 then
14017 -- If Parent_Type is undefined or illegal, make new type into a
14018 -- subtype of Any_Type, and set a few attributes to prevent cascaded
14019 -- errors. If this is a self-definition, emit error now.
14020
14021 if T = Parent_Type
14022 or else T = Etype (Parent_Type)
14023 then
14024 Error_Msg_N ("type cannot be used in its own definition", Indic);
14025 end if;
14026
14027 Set_Ekind (T, Ekind (Parent_Type));
14028 Set_Etype (T, Any_Type);
14029 Set_Scalar_Range (T, Scalar_Range (Any_Type));
14030
14031 if Is_Tagged_Type (T)
14032 and then Is_Record_Type (T)
14033 then
14034 Set_Direct_Primitive_Operations (T, New_Elmt_List);
14035 end if;
14036
14037 return;
14038 end if;
14039
14040 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
14041 -- an interface is special because the list of interfaces in the full
14042 -- view can be given in any order. For example:
14043
14044 -- type A is interface;
14045 -- type B is interface and A;
14046 -- type D is new B with private;
14047 -- private
14048 -- type D is new A and B with null record; -- 1 --
14049
14050 -- In this case we perform the following transformation of -1-:
14051
14052 -- type D is new B and A with null record;
14053
14054 -- If the parent of the full-view covers the parent of the partial-view
14055 -- we have two possible cases:
14056
14057 -- 1) They have the same parent
14058 -- 2) The parent of the full-view implements some further interfaces
14059
14060 -- In both cases we do not need to perform the transformation. In the
14061 -- first case the source program is correct and the transformation is
14062 -- not needed; in the second case the source program does not fulfill
14063 -- the no-hidden interfaces rule (AI-396) and the error will be reported
14064 -- later.
14065
14066 -- This transformation not only simplifies the rest of the analysis of
14067 -- this type declaration but also simplifies the correct generation of
14068 -- the object layout to the expander.
14069
14070 if In_Private_Part (Current_Scope)
14071 and then Is_Interface (Parent_Type)
14072 then
14073 declare
14074 Iface : Node_Id;
14075 Partial_View : Entity_Id;
14076 Partial_View_Parent : Entity_Id;
14077 New_Iface : Node_Id;
14078
14079 begin
14080 -- Look for the associated private type declaration
14081
14082 Partial_View := First_Entity (Current_Scope);
14083 loop
14084 exit when No (Partial_View)
14085 or else (Has_Private_Declaration (Partial_View)
14086 and then Full_View (Partial_View) = T);
14087
14088 Next_Entity (Partial_View);
14089 end loop;
14090
14091 -- If the partial view was not found then the source code has
14092 -- errors and the transformation is not needed.
14093
14094 if Present (Partial_View) then
14095 Partial_View_Parent := Etype (Partial_View);
14096
14097 -- If the parent of the full-view covers the parent of the
14098 -- partial-view we have nothing else to do.
14099
14100 if Interface_Present_In_Ancestor
14101 (Parent_Type, Partial_View_Parent)
14102 then
14103 null;
14104
14105 -- Traverse the list of interfaces of the full-view to look
14106 -- for the parent of the partial-view and perform the tree
14107 -- transformation.
14108
14109 else
14110 Iface := First (Interface_List (Def));
14111 while Present (Iface) loop
14112 if Etype (Iface) = Etype (Partial_View) then
14113 Rewrite (Subtype_Indication (Def),
14114 New_Copy (Subtype_Indication
14115 (Parent (Partial_View))));
14116
14117 New_Iface :=
14118 Make_Identifier (Sloc (N), Chars (Parent_Type));
14119 Append (New_Iface, Interface_List (Def));
14120
14121 -- Analyze the transformed code
14122
14123 Derived_Type_Declaration (T, N, Is_Completion);
14124 return;
14125 end if;
14126
14127 Next (Iface);
14128 end loop;
14129 end if;
14130 end if;
14131 end;
14132 end if;
14133
14134 -- Only composite types other than array types are allowed to have
14135 -- discriminants. In SPARK and in ALFA, no types are allowed to have
14136 -- discriminants.
14137
14138 if Present (Discriminant_Specifications (N)) then
14139 if (Is_Elementary_Type (Parent_Type)
14140 or else Is_Array_Type (Parent_Type))
14141 and then not Error_Posted (N)
14142 then
14143 Error_Msg_N
14144 ("elementary or array type cannot have discriminants",
14145 Defining_Identifier (First (Discriminant_Specifications (N))));
14146 Set_Has_Discriminants (T, False);
14147 else
14148 Check_Formal_Restriction ("discriminant type is not allowed", N);
14149 end if;
14150 end if;
14151
14152 -- In Ada 83, a derived type defined in a package specification cannot
14153 -- be used for further derivation until the end of its visible part.
14154 -- Note that derivation in the private part of the package is allowed.
14155
14156 if Ada_Version = Ada_83
14157 and then Is_Derived_Type (Parent_Type)
14158 and then In_Visible_Part (Scope (Parent_Type))
14159 then
14160 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
14161 Error_Msg_N
14162 ("(Ada 83): premature use of type for derivation", Indic);
14163 end if;
14164 end if;
14165
14166 -- Check for early use of incomplete or private type
14167
14168 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
14169 Error_Msg_N ("premature derivation of incomplete type", Indic);
14170 return;
14171
14172 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
14173 and then not Comes_From_Generic (Parent_Type))
14174 or else Has_Private_Component (Parent_Type)
14175 then
14176 -- The ancestor type of a formal type can be incomplete, in which
14177 -- case only the operations of the partial view are available in the
14178 -- generic. Subsequent checks may be required when the full view is
14179 -- analyzed to verify that a derivation from a tagged type has an
14180 -- extension.
14181
14182 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
14183 null;
14184
14185 elsif No (Underlying_Type (Parent_Type))
14186 or else Has_Private_Component (Parent_Type)
14187 then
14188 Error_Msg_N
14189 ("premature derivation of derived or private type", Indic);
14190
14191 -- Flag the type itself as being in error, this prevents some
14192 -- nasty problems with subsequent uses of the malformed type.
14193
14194 Set_Error_Posted (T);
14195
14196 -- Check that within the immediate scope of an untagged partial
14197 -- view it's illegal to derive from the partial view if the
14198 -- full view is tagged. (7.3(7))
14199
14200 -- We verify that the Parent_Type is a partial view by checking
14201 -- that it is not a Full_Type_Declaration (i.e. a private type or
14202 -- private extension declaration), to distinguish a partial view
14203 -- from a derivation from a private type which also appears as
14204 -- E_Private_Type.
14205
14206 elsif Present (Full_View (Parent_Type))
14207 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
14208 and then not Is_Tagged_Type (Parent_Type)
14209 and then Is_Tagged_Type (Full_View (Parent_Type))
14210 then
14211 Parent_Scope := Scope (T);
14212 while Present (Parent_Scope)
14213 and then Parent_Scope /= Standard_Standard
14214 loop
14215 if Parent_Scope = Scope (Parent_Type) then
14216 Error_Msg_N
14217 ("premature derivation from type with tagged full view",
14218 Indic);
14219 end if;
14220
14221 Parent_Scope := Scope (Parent_Scope);
14222 end loop;
14223 end if;
14224 end if;
14225
14226 -- Check that form of derivation is appropriate
14227
14228 Taggd := Is_Tagged_Type (Parent_Type);
14229
14230 -- Perhaps the parent type should be changed to the class-wide type's
14231 -- specific type in this case to prevent cascading errors ???
14232
14233 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
14234 Error_Msg_N ("parent type must not be a class-wide type", Indic);
14235 return;
14236 end if;
14237
14238 if Present (Extension) and then not Taggd then
14239 Error_Msg_N
14240 ("type derived from untagged type cannot have extension", Indic);
14241
14242 elsif No (Extension) and then Taggd then
14243
14244 -- If this declaration is within a private part (or body) of a
14245 -- generic instantiation then the derivation is allowed (the parent
14246 -- type can only appear tagged in this case if it's a generic actual
14247 -- type, since it would otherwise have been rejected in the analysis
14248 -- of the generic template).
14249
14250 if not Is_Generic_Actual_Type (Parent_Type)
14251 or else In_Visible_Part (Scope (Parent_Type))
14252 then
14253 if Is_Class_Wide_Type (Parent_Type) then
14254 Error_Msg_N
14255 ("parent type must not be a class-wide type", Indic);
14256
14257 -- Use specific type to prevent cascaded errors.
14258
14259 Parent_Type := Etype (Parent_Type);
14260
14261 else
14262 Error_Msg_N
14263 ("type derived from tagged type must have extension", Indic);
14264 end if;
14265 end if;
14266 end if;
14267
14268 -- AI-443: Synchronized formal derived types require a private
14269 -- extension. There is no point in checking the ancestor type or
14270 -- the progenitors since the construct is wrong to begin with.
14271
14272 if Ada_Version >= Ada_2005
14273 and then Is_Generic_Type (T)
14274 and then Present (Original_Node (N))
14275 then
14276 declare
14277 Decl : constant Node_Id := Original_Node (N);
14278
14279 begin
14280 if Nkind (Decl) = N_Formal_Type_Declaration
14281 and then Nkind (Formal_Type_Definition (Decl)) =
14282 N_Formal_Derived_Type_Definition
14283 and then Synchronized_Present (Formal_Type_Definition (Decl))
14284 and then No (Extension)
14285
14286 -- Avoid emitting a duplicate error message
14287
14288 and then not Error_Posted (Indic)
14289 then
14290 Error_Msg_N
14291 ("synchronized derived type must have extension", N);
14292 end if;
14293 end;
14294 end if;
14295
14296 if Null_Exclusion_Present (Def)
14297 and then not Is_Access_Type (Parent_Type)
14298 then
14299 Error_Msg_N ("null exclusion can only apply to an access type", N);
14300 end if;
14301
14302 -- Avoid deriving parent primitives of underlying record views
14303
14304 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
14305 Derive_Subps => not Is_Underlying_Record_View (T));
14306
14307 -- AI-419: The parent type of an explicitly limited derived type must
14308 -- be a limited type or a limited interface.
14309
14310 if Limited_Present (Def) then
14311 Set_Is_Limited_Record (T);
14312
14313 if Is_Interface (T) then
14314 Set_Is_Limited_Interface (T);
14315 end if;
14316
14317 if not Is_Limited_Type (Parent_Type)
14318 and then
14319 (not Is_Interface (Parent_Type)
14320 or else not Is_Limited_Interface (Parent_Type))
14321 then
14322 -- AI05-0096: a derivation in the private part of an instance is
14323 -- legal if the generic formal is untagged limited, and the actual
14324 -- is non-limited.
14325
14326 if Is_Generic_Actual_Type (Parent_Type)
14327 and then In_Private_Part (Current_Scope)
14328 and then
14329 not Is_Tagged_Type
14330 (Generic_Parent_Type (Parent (Parent_Type)))
14331 then
14332 null;
14333
14334 else
14335 Error_Msg_NE
14336 ("parent type& of limited type must be limited",
14337 N, Parent_Type);
14338 end if;
14339 end if;
14340 end if;
14341
14342 -- In SPARK or ALFA, there are no derived type definitions other than
14343 -- type extensions of tagged record types.
14344
14345 if No (Extension) then
14346 Check_Formal_Restriction ("derived type is not allowed", N);
14347 end if;
14348 end Derived_Type_Declaration;
14349
14350 ------------------------
14351 -- Diagnose_Interface --
14352 ------------------------
14353
14354 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
14355 begin
14356 if not Is_Interface (E)
14357 and then E /= Any_Type
14358 then
14359 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
14360 end if;
14361 end Diagnose_Interface;
14362
14363 ----------------------------------
14364 -- Enumeration_Type_Declaration --
14365 ----------------------------------
14366
14367 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14368 Ev : Uint;
14369 L : Node_Id;
14370 R_Node : Node_Id;
14371 B_Node : Node_Id;
14372
14373 begin
14374 -- Create identifier node representing lower bound
14375
14376 B_Node := New_Node (N_Identifier, Sloc (Def));
14377 L := First (Literals (Def));
14378 Set_Chars (B_Node, Chars (L));
14379 Set_Entity (B_Node, L);
14380 Set_Etype (B_Node, T);
14381 Set_Is_Static_Expression (B_Node, True);
14382
14383 R_Node := New_Node (N_Range, Sloc (Def));
14384 Set_Low_Bound (R_Node, B_Node);
14385
14386 Set_Ekind (T, E_Enumeration_Type);
14387 Set_First_Literal (T, L);
14388 Set_Etype (T, T);
14389 Set_Is_Constrained (T);
14390
14391 Ev := Uint_0;
14392
14393 -- Loop through literals of enumeration type setting pos and rep values
14394 -- except that if the Ekind is already set, then it means the literal
14395 -- was already constructed (case of a derived type declaration and we
14396 -- should not disturb the Pos and Rep values.
14397
14398 while Present (L) loop
14399 if Ekind (L) /= E_Enumeration_Literal then
14400 Set_Ekind (L, E_Enumeration_Literal);
14401 Set_Enumeration_Pos (L, Ev);
14402 Set_Enumeration_Rep (L, Ev);
14403 Set_Is_Known_Valid (L, True);
14404 end if;
14405
14406 Set_Etype (L, T);
14407 New_Overloaded_Entity (L);
14408 Generate_Definition (L);
14409 Set_Convention (L, Convention_Intrinsic);
14410
14411 -- Case of character literal
14412
14413 if Nkind (L) = N_Defining_Character_Literal then
14414 Set_Is_Character_Type (T, True);
14415
14416 -- Check violation of No_Wide_Characters
14417
14418 if Restriction_Check_Required (No_Wide_Characters) then
14419 Get_Name_String (Chars (L));
14420
14421 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
14422 Check_Restriction (No_Wide_Characters, L);
14423 end if;
14424 end if;
14425 end if;
14426
14427 Ev := Ev + 1;
14428 Next (L);
14429 end loop;
14430
14431 -- Now create a node representing upper bound
14432
14433 B_Node := New_Node (N_Identifier, Sloc (Def));
14434 Set_Chars (B_Node, Chars (Last (Literals (Def))));
14435 Set_Entity (B_Node, Last (Literals (Def)));
14436 Set_Etype (B_Node, T);
14437 Set_Is_Static_Expression (B_Node, True);
14438
14439 Set_High_Bound (R_Node, B_Node);
14440
14441 -- Initialize various fields of the type. Some of this information
14442 -- may be overwritten later through rep.clauses.
14443
14444 Set_Scalar_Range (T, R_Node);
14445 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
14446 Set_Enum_Esize (T);
14447 Set_Enum_Pos_To_Rep (T, Empty);
14448
14449 -- Set Discard_Names if configuration pragma set, or if there is
14450 -- a parameterless pragma in the current declarative region
14451
14452 if Global_Discard_Names
14453 or else Discard_Names (Scope (T))
14454 then
14455 Set_Discard_Names (T);
14456 end if;
14457
14458 -- Process end label if there is one
14459
14460 if Present (Def) then
14461 Process_End_Label (Def, 'e', T);
14462 end if;
14463 end Enumeration_Type_Declaration;
14464
14465 ---------------------------------
14466 -- Expand_To_Stored_Constraint --
14467 ---------------------------------
14468
14469 function Expand_To_Stored_Constraint
14470 (Typ : Entity_Id;
14471 Constraint : Elist_Id) return Elist_Id
14472 is
14473 Explicitly_Discriminated_Type : Entity_Id;
14474 Expansion : Elist_Id;
14475 Discriminant : Entity_Id;
14476
14477 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
14478 -- Find the nearest type that actually specifies discriminants
14479
14480 ---------------------------------
14481 -- Type_With_Explicit_Discrims --
14482 ---------------------------------
14483
14484 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
14485 Typ : constant E := Base_Type (Id);
14486
14487 begin
14488 if Ekind (Typ) in Incomplete_Or_Private_Kind then
14489 if Present (Full_View (Typ)) then
14490 return Type_With_Explicit_Discrims (Full_View (Typ));
14491 end if;
14492
14493 else
14494 if Has_Discriminants (Typ) then
14495 return Typ;
14496 end if;
14497 end if;
14498
14499 if Etype (Typ) = Typ then
14500 return Empty;
14501 elsif Has_Discriminants (Typ) then
14502 return Typ;
14503 else
14504 return Type_With_Explicit_Discrims (Etype (Typ));
14505 end if;
14506
14507 end Type_With_Explicit_Discrims;
14508
14509 -- Start of processing for Expand_To_Stored_Constraint
14510
14511 begin
14512 if No (Constraint)
14513 or else Is_Empty_Elmt_List (Constraint)
14514 then
14515 return No_Elist;
14516 end if;
14517
14518 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
14519
14520 if No (Explicitly_Discriminated_Type) then
14521 return No_Elist;
14522 end if;
14523
14524 Expansion := New_Elmt_List;
14525
14526 Discriminant :=
14527 First_Stored_Discriminant (Explicitly_Discriminated_Type);
14528 while Present (Discriminant) loop
14529 Append_Elmt (
14530 Get_Discriminant_Value (
14531 Discriminant, Explicitly_Discriminated_Type, Constraint),
14532 Expansion);
14533 Next_Stored_Discriminant (Discriminant);
14534 end loop;
14535
14536 return Expansion;
14537 end Expand_To_Stored_Constraint;
14538
14539 ---------------------------
14540 -- Find_Hidden_Interface --
14541 ---------------------------
14542
14543 function Find_Hidden_Interface
14544 (Src : Elist_Id;
14545 Dest : Elist_Id) return Entity_Id
14546 is
14547 Iface : Entity_Id;
14548 Iface_Elmt : Elmt_Id;
14549
14550 begin
14551 if Present (Src) and then Present (Dest) then
14552 Iface_Elmt := First_Elmt (Src);
14553 while Present (Iface_Elmt) loop
14554 Iface := Node (Iface_Elmt);
14555
14556 if Is_Interface (Iface)
14557 and then not Contain_Interface (Iface, Dest)
14558 then
14559 return Iface;
14560 end if;
14561
14562 Next_Elmt (Iface_Elmt);
14563 end loop;
14564 end if;
14565
14566 return Empty;
14567 end Find_Hidden_Interface;
14568
14569 --------------------
14570 -- Find_Type_Name --
14571 --------------------
14572
14573 function Find_Type_Name (N : Node_Id) return Entity_Id is
14574 Id : constant Entity_Id := Defining_Identifier (N);
14575 Prev : Entity_Id;
14576 New_Id : Entity_Id;
14577 Prev_Par : Node_Id;
14578
14579 procedure Tag_Mismatch;
14580 -- Diagnose a tagged partial view whose full view is untagged.
14581 -- We post the message on the full view, with a reference to
14582 -- the previous partial view. The partial view can be private
14583 -- or incomplete, and these are handled in a different manner,
14584 -- so we determine the position of the error message from the
14585 -- respective slocs of both.
14586
14587 ------------------
14588 -- Tag_Mismatch --
14589 ------------------
14590
14591 procedure Tag_Mismatch is
14592 begin
14593 if Sloc (Prev) < Sloc (Id) then
14594 if Ada_Version >= Ada_2012
14595 and then Nkind (N) = N_Private_Type_Declaration
14596 then
14597 Error_Msg_NE
14598 ("declaration of private } must be a tagged type ", Id, Prev);
14599 else
14600 Error_Msg_NE
14601 ("full declaration of } must be a tagged type ", Id, Prev);
14602 end if;
14603 else
14604 if Ada_Version >= Ada_2012
14605 and then Nkind (N) = N_Private_Type_Declaration
14606 then
14607 Error_Msg_NE
14608 ("declaration of private } must be a tagged type ", Prev, Id);
14609 else
14610 Error_Msg_NE
14611 ("full declaration of } must be a tagged type ", Prev, Id);
14612 end if;
14613 end if;
14614 end Tag_Mismatch;
14615
14616 -- Start of processing for Find_Type_Name
14617
14618 begin
14619 -- Find incomplete declaration, if one was given
14620
14621 Prev := Current_Entity_In_Scope (Id);
14622
14623 -- New type declaration
14624
14625 if No (Prev) then
14626 Enter_Name (Id);
14627 return Id;
14628
14629 -- Previous declaration exists
14630
14631 else
14632 Prev_Par := Parent (Prev);
14633
14634 -- Error if not incomplete/private case except if previous
14635 -- declaration is implicit, etc. Enter_Name will emit error if
14636 -- appropriate.
14637
14638 if not Is_Incomplete_Or_Private_Type (Prev) then
14639 Enter_Name (Id);
14640 New_Id := Id;
14641
14642 -- Check invalid completion of private or incomplete type
14643
14644 elsif not Nkind_In (N, N_Full_Type_Declaration,
14645 N_Task_Type_Declaration,
14646 N_Protected_Type_Declaration)
14647 and then
14648 (Ada_Version < Ada_2012
14649 or else not Is_Incomplete_Type (Prev)
14650 or else not Nkind_In (N, N_Private_Type_Declaration,
14651 N_Private_Extension_Declaration))
14652 then
14653 -- Completion must be a full type declarations (RM 7.3(4))
14654
14655 Error_Msg_Sloc := Sloc (Prev);
14656 Error_Msg_NE ("invalid completion of }", Id, Prev);
14657
14658 -- Set scope of Id to avoid cascaded errors. Entity is never
14659 -- examined again, except when saving globals in generics.
14660
14661 Set_Scope (Id, Current_Scope);
14662 New_Id := Id;
14663
14664 -- If this is a repeated incomplete declaration, no further
14665 -- checks are possible.
14666
14667 if Nkind (N) = N_Incomplete_Type_Declaration then
14668 return Prev;
14669 end if;
14670
14671 -- Case of full declaration of incomplete type
14672
14673 elsif Ekind (Prev) = E_Incomplete_Type
14674 and then (Ada_Version < Ada_2012
14675 or else No (Full_View (Prev))
14676 or else not Is_Private_Type (Full_View (Prev)))
14677 then
14678
14679 -- Indicate that the incomplete declaration has a matching full
14680 -- declaration. The defining occurrence of the incomplete
14681 -- declaration remains the visible one, and the procedure
14682 -- Get_Full_View dereferences it whenever the type is used.
14683
14684 if Present (Full_View (Prev)) then
14685 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14686 end if;
14687
14688 Set_Full_View (Prev, Id);
14689 Append_Entity (Id, Current_Scope);
14690 Set_Is_Public (Id, Is_Public (Prev));
14691 Set_Is_Internal (Id);
14692 New_Id := Prev;
14693
14694 -- If the incomplete view is tagged, a class_wide type has been
14695 -- created already. Use it for the private type as well, in order
14696 -- to prevent multiple incompatible class-wide types that may be
14697 -- created for self-referential anonymous access components.
14698
14699 if Is_Tagged_Type (Prev)
14700 and then Present (Class_Wide_Type (Prev))
14701 then
14702 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
14703 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
14704 Set_Etype (Class_Wide_Type (Id), Id);
14705 end if;
14706
14707 -- Case of full declaration of private type
14708
14709 else
14710 -- If the private type was a completion of an incomplete type then
14711 -- update Prev to reference the private type
14712
14713 if Ada_Version >= Ada_2012
14714 and then Ekind (Prev) = E_Incomplete_Type
14715 and then Present (Full_View (Prev))
14716 and then Is_Private_Type (Full_View (Prev))
14717 then
14718 Prev := Full_View (Prev);
14719 Prev_Par := Parent (Prev);
14720 end if;
14721
14722 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
14723 if Etype (Prev) /= Prev then
14724
14725 -- Prev is a private subtype or a derived type, and needs
14726 -- no completion.
14727
14728 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14729 New_Id := Id;
14730
14731 elsif Ekind (Prev) = E_Private_Type
14732 and then Nkind_In (N, N_Task_Type_Declaration,
14733 N_Protected_Type_Declaration)
14734 then
14735 Error_Msg_N
14736 ("completion of nonlimited type cannot be limited", N);
14737
14738 elsif Ekind (Prev) = E_Record_Type_With_Private
14739 and then Nkind_In (N, N_Task_Type_Declaration,
14740 N_Protected_Type_Declaration)
14741 then
14742 if not Is_Limited_Record (Prev) then
14743 Error_Msg_N
14744 ("completion of nonlimited type cannot be limited", N);
14745
14746 elsif No (Interface_List (N)) then
14747 Error_Msg_N
14748 ("completion of tagged private type must be tagged",
14749 N);
14750 end if;
14751
14752 elsif Nkind (N) = N_Full_Type_Declaration
14753 and then
14754 Nkind (Type_Definition (N)) = N_Record_Definition
14755 and then Interface_Present (Type_Definition (N))
14756 then
14757 Error_Msg_N
14758 ("completion of private type cannot be an interface", N);
14759 end if;
14760
14761 -- Ada 2005 (AI-251): Private extension declaration of a task
14762 -- type or a protected type. This case arises when covering
14763 -- interface types.
14764
14765 elsif Nkind_In (N, N_Task_Type_Declaration,
14766 N_Protected_Type_Declaration)
14767 then
14768 null;
14769
14770 elsif Nkind (N) /= N_Full_Type_Declaration
14771 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
14772 then
14773 Error_Msg_N
14774 ("full view of private extension must be an extension", N);
14775
14776 elsif not (Abstract_Present (Parent (Prev)))
14777 and then Abstract_Present (Type_Definition (N))
14778 then
14779 Error_Msg_N
14780 ("full view of non-abstract extension cannot be abstract", N);
14781 end if;
14782
14783 if not In_Private_Part (Current_Scope) then
14784 Error_Msg_N
14785 ("declaration of full view must appear in private part", N);
14786 end if;
14787
14788 Copy_And_Swap (Prev, Id);
14789 Set_Has_Private_Declaration (Prev);
14790 Set_Has_Private_Declaration (Id);
14791
14792 -- If no error, propagate freeze_node from private to full view.
14793 -- It may have been generated for an early operational item.
14794
14795 if Present (Freeze_Node (Id))
14796 and then Serious_Errors_Detected = 0
14797 and then No (Full_View (Id))
14798 then
14799 Set_Freeze_Node (Prev, Freeze_Node (Id));
14800 Set_Freeze_Node (Id, Empty);
14801 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
14802 end if;
14803
14804 Set_Full_View (Id, Prev);
14805 New_Id := Prev;
14806 end if;
14807
14808 -- Verify that full declaration conforms to partial one
14809
14810 if Is_Incomplete_Or_Private_Type (Prev)
14811 and then Present (Discriminant_Specifications (Prev_Par))
14812 then
14813 if Present (Discriminant_Specifications (N)) then
14814 if Ekind (Prev) = E_Incomplete_Type then
14815 Check_Discriminant_Conformance (N, Prev, Prev);
14816 else
14817 Check_Discriminant_Conformance (N, Prev, Id);
14818 end if;
14819
14820 else
14821 Error_Msg_N
14822 ("missing discriminants in full type declaration", N);
14823
14824 -- To avoid cascaded errors on subsequent use, share the
14825 -- discriminants of the partial view.
14826
14827 Set_Discriminant_Specifications (N,
14828 Discriminant_Specifications (Prev_Par));
14829 end if;
14830 end if;
14831
14832 -- A prior untagged partial view can have an associated class-wide
14833 -- type due to use of the class attribute, and in this case the full
14834 -- type must also be tagged. This Ada 95 usage is deprecated in favor
14835 -- of incomplete tagged declarations, but we check for it.
14836
14837 if Is_Type (Prev)
14838 and then (Is_Tagged_Type (Prev)
14839 or else Present (Class_Wide_Type (Prev)))
14840 then
14841 -- Ada 2012 (AI05-0162): A private type may be the completion of
14842 -- an incomplete type
14843
14844 if Ada_Version >= Ada_2012
14845 and then Is_Incomplete_Type (Prev)
14846 and then Nkind_In (N, N_Private_Type_Declaration,
14847 N_Private_Extension_Declaration)
14848 then
14849 -- No need to check private extensions since they are tagged
14850
14851 if Nkind (N) = N_Private_Type_Declaration
14852 and then not Tagged_Present (N)
14853 then
14854 Tag_Mismatch;
14855 end if;
14856
14857 -- The full declaration is either a tagged type (including
14858 -- a synchronized type that implements interfaces) or a
14859 -- type extension, otherwise this is an error.
14860
14861 elsif Nkind_In (N, N_Task_Type_Declaration,
14862 N_Protected_Type_Declaration)
14863 then
14864 if No (Interface_List (N))
14865 and then not Error_Posted (N)
14866 then
14867 Tag_Mismatch;
14868 end if;
14869
14870 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
14871
14872 -- Indicate that the previous declaration (tagged incomplete
14873 -- or private declaration) requires the same on the full one.
14874
14875 if not Tagged_Present (Type_Definition (N)) then
14876 Tag_Mismatch;
14877 Set_Is_Tagged_Type (Id);
14878 end if;
14879
14880 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
14881 if No (Record_Extension_Part (Type_Definition (N))) then
14882 Error_Msg_NE
14883 ("full declaration of } must be a record extension",
14884 Prev, Id);
14885
14886 -- Set some attributes to produce a usable full view
14887
14888 Set_Is_Tagged_Type (Id);
14889 end if;
14890
14891 else
14892 Tag_Mismatch;
14893 end if;
14894 end if;
14895
14896 return New_Id;
14897 end if;
14898 end Find_Type_Name;
14899
14900 -------------------------
14901 -- Find_Type_Of_Object --
14902 -------------------------
14903
14904 function Find_Type_Of_Object
14905 (Obj_Def : Node_Id;
14906 Related_Nod : Node_Id) return Entity_Id
14907 is
14908 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
14909 P : Node_Id := Parent (Obj_Def);
14910 T : Entity_Id;
14911 Nam : Name_Id;
14912
14913 begin
14914 -- If the parent is a component_definition node we climb to the
14915 -- component_declaration node
14916
14917 if Nkind (P) = N_Component_Definition then
14918 P := Parent (P);
14919 end if;
14920
14921 -- Case of an anonymous array subtype
14922
14923 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
14924 N_Unconstrained_Array_Definition)
14925 then
14926 T := Empty;
14927 Array_Type_Declaration (T, Obj_Def);
14928
14929 -- Create an explicit subtype whenever possible
14930
14931 elsif Nkind (P) /= N_Component_Declaration
14932 and then Def_Kind = N_Subtype_Indication
14933 then
14934 -- Base name of subtype on object name, which will be unique in
14935 -- the current scope.
14936
14937 -- If this is a duplicate declaration, return base type, to avoid
14938 -- generating duplicate anonymous types.
14939
14940 if Error_Posted (P) then
14941 Analyze (Subtype_Mark (Obj_Def));
14942 return Entity (Subtype_Mark (Obj_Def));
14943 end if;
14944
14945 Nam :=
14946 New_External_Name
14947 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
14948
14949 T := Make_Defining_Identifier (Sloc (P), Nam);
14950
14951 Insert_Action (Obj_Def,
14952 Make_Subtype_Declaration (Sloc (P),
14953 Defining_Identifier => T,
14954 Subtype_Indication => Relocate_Node (Obj_Def)));
14955
14956 -- This subtype may need freezing, and this will not be done
14957 -- automatically if the object declaration is not in declarative
14958 -- part. Since this is an object declaration, the type cannot always
14959 -- be frozen here. Deferred constants do not freeze their type
14960 -- (which often enough will be private).
14961
14962 if Nkind (P) = N_Object_Declaration
14963 and then Constant_Present (P)
14964 and then No (Expression (P))
14965 then
14966 null;
14967 else
14968 Insert_Actions (Obj_Def, Freeze_Entity (T, P));
14969 end if;
14970
14971 -- Ada 2005 AI-406: the object definition in an object declaration
14972 -- can be an access definition.
14973
14974 elsif Def_Kind = N_Access_Definition then
14975 T := Access_Definition (Related_Nod, Obj_Def);
14976 Set_Is_Local_Anonymous_Access (T);
14977
14978 -- Otherwise, the object definition is just a subtype_mark
14979
14980 else
14981 T := Process_Subtype (Obj_Def, Related_Nod);
14982
14983 -- If expansion is disabled an object definition that is an aggregate
14984 -- will not get expanded and may lead to scoping problems in the back
14985 -- end, if the object is referenced in an inner scope. In that case
14986 -- create an itype reference for the object definition now. This
14987 -- may be redundant in some cases, but harmless.
14988
14989 if Is_Itype (T)
14990 and then Nkind (Related_Nod) = N_Object_Declaration
14991 and then ASIS_Mode
14992 then
14993 Build_Itype_Reference (T, Related_Nod);
14994 end if;
14995 end if;
14996
14997 return T;
14998 end Find_Type_Of_Object;
14999
15000 --------------------------------
15001 -- Find_Type_Of_Subtype_Indic --
15002 --------------------------------
15003
15004 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
15005 Typ : Entity_Id;
15006
15007 begin
15008 -- Case of subtype mark with a constraint
15009
15010 if Nkind (S) = N_Subtype_Indication then
15011 Find_Type (Subtype_Mark (S));
15012 Typ := Entity (Subtype_Mark (S));
15013
15014 if not
15015 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
15016 then
15017 Error_Msg_N
15018 ("incorrect constraint for this kind of type", Constraint (S));
15019 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
15020 end if;
15021
15022 -- Otherwise we have a subtype mark without a constraint
15023
15024 elsif Error_Posted (S) then
15025 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
15026 return Any_Type;
15027
15028 else
15029 Find_Type (S);
15030 Typ := Entity (S);
15031 end if;
15032
15033 -- Check No_Wide_Characters restriction
15034
15035 Check_Wide_Character_Restriction (Typ, S);
15036
15037 return Typ;
15038 end Find_Type_Of_Subtype_Indic;
15039
15040 -------------------------------------
15041 -- Floating_Point_Type_Declaration --
15042 -------------------------------------
15043
15044 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15045 Digs : constant Node_Id := Digits_Expression (Def);
15046 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
15047 Digs_Val : Uint;
15048 Base_Typ : Entity_Id;
15049 Implicit_Base : Entity_Id;
15050 Bound : Node_Id;
15051
15052 function Can_Derive_From (E : Entity_Id) return Boolean;
15053 -- Find if given digits value, and possibly a specified range, allows
15054 -- derivation from specified type
15055
15056 ---------------------
15057 -- Can_Derive_From --
15058 ---------------------
15059
15060 function Can_Derive_From (E : Entity_Id) return Boolean is
15061 Spec : constant Entity_Id := Real_Range_Specification (Def);
15062
15063 begin
15064 if Digs_Val > Digits_Value (E) then
15065 return False;
15066 end if;
15067
15068 if Present (Spec) then
15069 if Expr_Value_R (Type_Low_Bound (E)) >
15070 Expr_Value_R (Low_Bound (Spec))
15071 then
15072 return False;
15073 end if;
15074
15075 if Expr_Value_R (Type_High_Bound (E)) <
15076 Expr_Value_R (High_Bound (Spec))
15077 then
15078 return False;
15079 end if;
15080 end if;
15081
15082 return True;
15083 end Can_Derive_From;
15084
15085 -- Start of processing for Floating_Point_Type_Declaration
15086
15087 begin
15088 Check_Restriction (No_Floating_Point, Def);
15089
15090 -- Create an implicit base type
15091
15092 Implicit_Base :=
15093 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
15094
15095 -- Analyze and verify digits value
15096
15097 Analyze_And_Resolve (Digs, Any_Integer);
15098 Check_Digits_Expression (Digs);
15099 Digs_Val := Expr_Value (Digs);
15100
15101 -- Process possible range spec and find correct type to derive from
15102
15103 Process_Real_Range_Specification (Def);
15104
15105 -- Check that requested number of digits is not too high.
15106
15107 if Digs_Val > Max_Digs_Val then
15108 -- The check for Max_Base_Digits may be somewhat expensive, as it
15109 -- requires reading System, so only do it when necessary.
15110
15111 declare
15112 Max_Base_Digits : constant Uint :=
15113 Expr_Value (Expression (Parent (RTE (RE_Max_Base_Digits))));
15114 begin
15115 if Digs_Val > Max_Base_Digits then
15116 Error_Msg_Uint_1 := Max_Base_Digits;
15117 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
15118
15119 elsif No (Real_Range_Specification (Def)) then
15120 Error_Msg_Uint_1 := Max_Digs_Val;
15121 Error_Msg_N ("types with more than ^ digits need range spec "
15122 & "('R'M 3.5.7(6))", Digs);
15123 end if;
15124 end;
15125 end if;
15126
15127 Base_Typ := First (Predefined_Float_Types);
15128
15129 while Present (Base_Typ) and then not Can_Derive_From (Base_Typ) loop
15130 Next (Base_Typ);
15131 end loop;
15132
15133 -- If we can't derive from any existing type, use Long_Long_Float
15134 -- and give appropriate message explaining the problem.
15135
15136 if No (Base_Typ) then
15137 Base_Typ := Standard_Long_Long_Float;
15138
15139 if Digs_Val > Max_Digs_Val then
15140 -- It might be the case that there is a type with the requested
15141 -- range, just not the combination of digits and range.
15142
15143 Error_Msg_N
15144 ("no predefined type has requested range and precision",
15145 Real_Range_Specification (Def));
15146
15147 else
15148 Error_Msg_N
15149 ("range too large for any predefined type",
15150 Real_Range_Specification (Def));
15151 end if;
15152 end if;
15153
15154 -- If there are bounds given in the declaration use them as the bounds
15155 -- of the type, otherwise use the bounds of the predefined base type
15156 -- that was chosen based on the Digits value.
15157
15158 if Present (Real_Range_Specification (Def)) then
15159 Set_Scalar_Range (T, Real_Range_Specification (Def));
15160 Set_Is_Constrained (T);
15161
15162 -- The bounds of this range must be converted to machine numbers
15163 -- in accordance with RM 4.9(38).
15164
15165 Bound := Type_Low_Bound (T);
15166
15167 if Nkind (Bound) = N_Real_Literal then
15168 Set_Realval
15169 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
15170 Set_Is_Machine_Number (Bound);
15171 end if;
15172
15173 Bound := Type_High_Bound (T);
15174
15175 if Nkind (Bound) = N_Real_Literal then
15176 Set_Realval
15177 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
15178 Set_Is_Machine_Number (Bound);
15179 end if;
15180
15181 else
15182 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
15183 end if;
15184
15185 -- Complete definition of implicit base and declared first subtype
15186
15187 Set_Etype (Implicit_Base, Base_Typ);
15188
15189 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
15190 Set_Size_Info (Implicit_Base, (Base_Typ));
15191 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
15192 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
15193 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
15194 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
15195
15196 Set_Ekind (T, E_Floating_Point_Subtype);
15197 Set_Etype (T, Implicit_Base);
15198
15199 Set_Size_Info (T, (Implicit_Base));
15200 Set_RM_Size (T, RM_Size (Implicit_Base));
15201 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15202 Set_Digits_Value (T, Digs_Val);
15203 end Floating_Point_Type_Declaration;
15204
15205 ----------------------------
15206 -- Get_Discriminant_Value --
15207 ----------------------------
15208
15209 -- This is the situation:
15210
15211 -- There is a non-derived type
15212
15213 -- type T0 (Dx, Dy, Dz...)
15214
15215 -- There are zero or more levels of derivation, with each derivation
15216 -- either purely inheriting the discriminants, or defining its own.
15217
15218 -- type Ti is new Ti-1
15219 -- or
15220 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
15221 -- or
15222 -- subtype Ti is ...
15223
15224 -- The subtype issue is avoided by the use of Original_Record_Component,
15225 -- and the fact that derived subtypes also derive the constraints.
15226
15227 -- This chain leads back from
15228
15229 -- Typ_For_Constraint
15230
15231 -- Typ_For_Constraint has discriminants, and the value for each
15232 -- discriminant is given by its corresponding Elmt of Constraints.
15233
15234 -- Discriminant is some discriminant in this hierarchy
15235
15236 -- We need to return its value
15237
15238 -- We do this by recursively searching each level, and looking for
15239 -- Discriminant. Once we get to the bottom, we start backing up
15240 -- returning the value for it which may in turn be a discriminant
15241 -- further up, so on the backup we continue the substitution.
15242
15243 function Get_Discriminant_Value
15244 (Discriminant : Entity_Id;
15245 Typ_For_Constraint : Entity_Id;
15246 Constraint : Elist_Id) return Node_Id
15247 is
15248 function Search_Derivation_Levels
15249 (Ti : Entity_Id;
15250 Discrim_Values : Elist_Id;
15251 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
15252 -- This is the routine that performs the recursive search of levels
15253 -- as described above.
15254
15255 ------------------------------
15256 -- Search_Derivation_Levels --
15257 ------------------------------
15258
15259 function Search_Derivation_Levels
15260 (Ti : Entity_Id;
15261 Discrim_Values : Elist_Id;
15262 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
15263 is
15264 Assoc : Elmt_Id;
15265 Disc : Entity_Id;
15266 Result : Node_Or_Entity_Id;
15267 Result_Entity : Node_Id;
15268
15269 begin
15270 -- If inappropriate type, return Error, this happens only in
15271 -- cascaded error situations, and we want to avoid a blow up.
15272
15273 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
15274 return Error;
15275 end if;
15276
15277 -- Look deeper if possible. Use Stored_Constraints only for
15278 -- untagged types. For tagged types use the given constraint.
15279 -- This asymmetry needs explanation???
15280
15281 if not Stored_Discrim_Values
15282 and then Present (Stored_Constraint (Ti))
15283 and then not Is_Tagged_Type (Ti)
15284 then
15285 Result :=
15286 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
15287 else
15288 declare
15289 Td : constant Entity_Id := Etype (Ti);
15290
15291 begin
15292 if Td = Ti then
15293 Result := Discriminant;
15294
15295 else
15296 if Present (Stored_Constraint (Ti)) then
15297 Result :=
15298 Search_Derivation_Levels
15299 (Td, Stored_Constraint (Ti), True);
15300 else
15301 Result :=
15302 Search_Derivation_Levels
15303 (Td, Discrim_Values, Stored_Discrim_Values);
15304 end if;
15305 end if;
15306 end;
15307 end if;
15308
15309 -- Extra underlying places to search, if not found above. For
15310 -- concurrent types, the relevant discriminant appears in the
15311 -- corresponding record. For a type derived from a private type
15312 -- without discriminant, the full view inherits the discriminants
15313 -- of the full view of the parent.
15314
15315 if Result = Discriminant then
15316 if Is_Concurrent_Type (Ti)
15317 and then Present (Corresponding_Record_Type (Ti))
15318 then
15319 Result :=
15320 Search_Derivation_Levels (
15321 Corresponding_Record_Type (Ti),
15322 Discrim_Values,
15323 Stored_Discrim_Values);
15324
15325 elsif Is_Private_Type (Ti)
15326 and then not Has_Discriminants (Ti)
15327 and then Present (Full_View (Ti))
15328 and then Etype (Full_View (Ti)) /= Ti
15329 then
15330 Result :=
15331 Search_Derivation_Levels (
15332 Full_View (Ti),
15333 Discrim_Values,
15334 Stored_Discrim_Values);
15335 end if;
15336 end if;
15337
15338 -- If Result is not a (reference to a) discriminant, return it,
15339 -- otherwise set Result_Entity to the discriminant.
15340
15341 if Nkind (Result) = N_Defining_Identifier then
15342 pragma Assert (Result = Discriminant);
15343 Result_Entity := Result;
15344
15345 else
15346 if not Denotes_Discriminant (Result) then
15347 return Result;
15348 end if;
15349
15350 Result_Entity := Entity (Result);
15351 end if;
15352
15353 -- See if this level of derivation actually has discriminants
15354 -- because tagged derivations can add them, hence the lower
15355 -- levels need not have any.
15356
15357 if not Has_Discriminants (Ti) then
15358 return Result;
15359 end if;
15360
15361 -- Scan Ti's discriminants for Result_Entity,
15362 -- and return its corresponding value, if any.
15363
15364 Result_Entity := Original_Record_Component (Result_Entity);
15365
15366 Assoc := First_Elmt (Discrim_Values);
15367
15368 if Stored_Discrim_Values then
15369 Disc := First_Stored_Discriminant (Ti);
15370 else
15371 Disc := First_Discriminant (Ti);
15372 end if;
15373
15374 while Present (Disc) loop
15375 pragma Assert (Present (Assoc));
15376
15377 if Original_Record_Component (Disc) = Result_Entity then
15378 return Node (Assoc);
15379 end if;
15380
15381 Next_Elmt (Assoc);
15382
15383 if Stored_Discrim_Values then
15384 Next_Stored_Discriminant (Disc);
15385 else
15386 Next_Discriminant (Disc);
15387 end if;
15388 end loop;
15389
15390 -- Could not find it
15391 --
15392 return Result;
15393 end Search_Derivation_Levels;
15394
15395 -- Local Variables
15396
15397 Result : Node_Or_Entity_Id;
15398
15399 -- Start of processing for Get_Discriminant_Value
15400
15401 begin
15402 -- ??? This routine is a gigantic mess and will be deleted. For the
15403 -- time being just test for the trivial case before calling recurse.
15404
15405 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
15406 declare
15407 D : Entity_Id;
15408 E : Elmt_Id;
15409
15410 begin
15411 D := First_Discriminant (Typ_For_Constraint);
15412 E := First_Elmt (Constraint);
15413 while Present (D) loop
15414 if Chars (D) = Chars (Discriminant) then
15415 return Node (E);
15416 end if;
15417
15418 Next_Discriminant (D);
15419 Next_Elmt (E);
15420 end loop;
15421 end;
15422 end if;
15423
15424 Result := Search_Derivation_Levels
15425 (Typ_For_Constraint, Constraint, False);
15426
15427 -- ??? hack to disappear when this routine is gone
15428
15429 if Nkind (Result) = N_Defining_Identifier then
15430 declare
15431 D : Entity_Id;
15432 E : Elmt_Id;
15433
15434 begin
15435 D := First_Discriminant (Typ_For_Constraint);
15436 E := First_Elmt (Constraint);
15437 while Present (D) loop
15438 if Corresponding_Discriminant (D) = Discriminant then
15439 return Node (E);
15440 end if;
15441
15442 Next_Discriminant (D);
15443 Next_Elmt (E);
15444 end loop;
15445 end;
15446 end if;
15447
15448 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
15449 return Result;
15450 end Get_Discriminant_Value;
15451
15452 --------------------------
15453 -- Has_Range_Constraint --
15454 --------------------------
15455
15456 function Has_Range_Constraint (N : Node_Id) return Boolean is
15457 C : constant Node_Id := Constraint (N);
15458
15459 begin
15460 if Nkind (C) = N_Range_Constraint then
15461 return True;
15462
15463 elsif Nkind (C) = N_Digits_Constraint then
15464 return
15465 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
15466 or else
15467 Present (Range_Constraint (C));
15468
15469 elsif Nkind (C) = N_Delta_Constraint then
15470 return Present (Range_Constraint (C));
15471
15472 else
15473 return False;
15474 end if;
15475 end Has_Range_Constraint;
15476
15477 ------------------------
15478 -- Inherit_Components --
15479 ------------------------
15480
15481 function Inherit_Components
15482 (N : Node_Id;
15483 Parent_Base : Entity_Id;
15484 Derived_Base : Entity_Id;
15485 Is_Tagged : Boolean;
15486 Inherit_Discr : Boolean;
15487 Discs : Elist_Id) return Elist_Id
15488 is
15489 Assoc_List : constant Elist_Id := New_Elmt_List;
15490
15491 procedure Inherit_Component
15492 (Old_C : Entity_Id;
15493 Plain_Discrim : Boolean := False;
15494 Stored_Discrim : Boolean := False);
15495 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
15496 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
15497 -- True, Old_C is a stored discriminant. If they are both false then
15498 -- Old_C is a regular component.
15499
15500 -----------------------
15501 -- Inherit_Component --
15502 -----------------------
15503
15504 procedure Inherit_Component
15505 (Old_C : Entity_Id;
15506 Plain_Discrim : Boolean := False;
15507 Stored_Discrim : Boolean := False)
15508 is
15509 New_C : constant Entity_Id := New_Copy (Old_C);
15510
15511 Discrim : Entity_Id;
15512 Corr_Discrim : Entity_Id;
15513
15514 begin
15515 pragma Assert (not Is_Tagged or else not Stored_Discrim);
15516
15517 Set_Parent (New_C, Parent (Old_C));
15518
15519 -- Regular discriminants and components must be inserted in the scope
15520 -- of the Derived_Base. Do it here.
15521
15522 if not Stored_Discrim then
15523 Enter_Name (New_C);
15524 end if;
15525
15526 -- For tagged types the Original_Record_Component must point to
15527 -- whatever this field was pointing to in the parent type. This has
15528 -- already been achieved by the call to New_Copy above.
15529
15530 if not Is_Tagged then
15531 Set_Original_Record_Component (New_C, New_C);
15532 end if;
15533
15534 -- If we have inherited a component then see if its Etype contains
15535 -- references to Parent_Base discriminants. In this case, replace
15536 -- these references with the constraints given in Discs. We do not
15537 -- do this for the partial view of private types because this is
15538 -- not needed (only the components of the full view will be used
15539 -- for code generation) and cause problem. We also avoid this
15540 -- transformation in some error situations.
15541
15542 if Ekind (New_C) = E_Component then
15543 if (Is_Private_Type (Derived_Base)
15544 and then not Is_Generic_Type (Derived_Base))
15545 or else (Is_Empty_Elmt_List (Discs)
15546 and then not Expander_Active)
15547 then
15548 Set_Etype (New_C, Etype (Old_C));
15549
15550 else
15551 -- The current component introduces a circularity of the
15552 -- following kind:
15553
15554 -- limited with Pack_2;
15555 -- package Pack_1 is
15556 -- type T_1 is tagged record
15557 -- Comp : access Pack_2.T_2;
15558 -- ...
15559 -- end record;
15560 -- end Pack_1;
15561
15562 -- with Pack_1;
15563 -- package Pack_2 is
15564 -- type T_2 is new Pack_1.T_1 with ...;
15565 -- end Pack_2;
15566
15567 Set_Etype
15568 (New_C,
15569 Constrain_Component_Type
15570 (Old_C, Derived_Base, N, Parent_Base, Discs));
15571 end if;
15572 end if;
15573
15574 -- In derived tagged types it is illegal to reference a non
15575 -- discriminant component in the parent type. To catch this, mark
15576 -- these components with an Ekind of E_Void. This will be reset in
15577 -- Record_Type_Definition after processing the record extension of
15578 -- the derived type.
15579
15580 -- If the declaration is a private extension, there is no further
15581 -- record extension to process, and the components retain their
15582 -- current kind, because they are visible at this point.
15583
15584 if Is_Tagged and then Ekind (New_C) = E_Component
15585 and then Nkind (N) /= N_Private_Extension_Declaration
15586 then
15587 Set_Ekind (New_C, E_Void);
15588 end if;
15589
15590 if Plain_Discrim then
15591 Set_Corresponding_Discriminant (New_C, Old_C);
15592 Build_Discriminal (New_C);
15593
15594 -- If we are explicitly inheriting a stored discriminant it will be
15595 -- completely hidden.
15596
15597 elsif Stored_Discrim then
15598 Set_Corresponding_Discriminant (New_C, Empty);
15599 Set_Discriminal (New_C, Empty);
15600 Set_Is_Completely_Hidden (New_C);
15601
15602 -- Set the Original_Record_Component of each discriminant in the
15603 -- derived base to point to the corresponding stored that we just
15604 -- created.
15605
15606 Discrim := First_Discriminant (Derived_Base);
15607 while Present (Discrim) loop
15608 Corr_Discrim := Corresponding_Discriminant (Discrim);
15609
15610 -- Corr_Discrim could be missing in an error situation
15611
15612 if Present (Corr_Discrim)
15613 and then Original_Record_Component (Corr_Discrim) = Old_C
15614 then
15615 Set_Original_Record_Component (Discrim, New_C);
15616 end if;
15617
15618 Next_Discriminant (Discrim);
15619 end loop;
15620
15621 Append_Entity (New_C, Derived_Base);
15622 end if;
15623
15624 if not Is_Tagged then
15625 Append_Elmt (Old_C, Assoc_List);
15626 Append_Elmt (New_C, Assoc_List);
15627 end if;
15628 end Inherit_Component;
15629
15630 -- Variables local to Inherit_Component
15631
15632 Loc : constant Source_Ptr := Sloc (N);
15633
15634 Parent_Discrim : Entity_Id;
15635 Stored_Discrim : Entity_Id;
15636 D : Entity_Id;
15637 Component : Entity_Id;
15638
15639 -- Start of processing for Inherit_Components
15640
15641 begin
15642 if not Is_Tagged then
15643 Append_Elmt (Parent_Base, Assoc_List);
15644 Append_Elmt (Derived_Base, Assoc_List);
15645 end if;
15646
15647 -- Inherit parent discriminants if needed
15648
15649 if Inherit_Discr then
15650 Parent_Discrim := First_Discriminant (Parent_Base);
15651 while Present (Parent_Discrim) loop
15652 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
15653 Next_Discriminant (Parent_Discrim);
15654 end loop;
15655 end if;
15656
15657 -- Create explicit stored discrims for untagged types when necessary
15658
15659 if not Has_Unknown_Discriminants (Derived_Base)
15660 and then Has_Discriminants (Parent_Base)
15661 and then not Is_Tagged
15662 and then
15663 (not Inherit_Discr
15664 or else First_Discriminant (Parent_Base) /=
15665 First_Stored_Discriminant (Parent_Base))
15666 then
15667 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
15668 while Present (Stored_Discrim) loop
15669 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
15670 Next_Stored_Discriminant (Stored_Discrim);
15671 end loop;
15672 end if;
15673
15674 -- See if we can apply the second transformation for derived types, as
15675 -- explained in point 6. in the comments above Build_Derived_Record_Type
15676 -- This is achieved by appending Derived_Base discriminants into Discs,
15677 -- which has the side effect of returning a non empty Discs list to the
15678 -- caller of Inherit_Components, which is what we want. This must be
15679 -- done for private derived types if there are explicit stored
15680 -- discriminants, to ensure that we can retrieve the values of the
15681 -- constraints provided in the ancestors.
15682
15683 if Inherit_Discr
15684 and then Is_Empty_Elmt_List (Discs)
15685 and then Present (First_Discriminant (Derived_Base))
15686 and then
15687 (not Is_Private_Type (Derived_Base)
15688 or else Is_Completely_Hidden
15689 (First_Stored_Discriminant (Derived_Base))
15690 or else Is_Generic_Type (Derived_Base))
15691 then
15692 D := First_Discriminant (Derived_Base);
15693 while Present (D) loop
15694 Append_Elmt (New_Reference_To (D, Loc), Discs);
15695 Next_Discriminant (D);
15696 end loop;
15697 end if;
15698
15699 -- Finally, inherit non-discriminant components unless they are not
15700 -- visible because defined or inherited from the full view of the
15701 -- parent. Don't inherit the _parent field of the parent type.
15702
15703 Component := First_Entity (Parent_Base);
15704 while Present (Component) loop
15705
15706 -- Ada 2005 (AI-251): Do not inherit components associated with
15707 -- secondary tags of the parent.
15708
15709 if Ekind (Component) = E_Component
15710 and then Present (Related_Type (Component))
15711 then
15712 null;
15713
15714 elsif Ekind (Component) /= E_Component
15715 or else Chars (Component) = Name_uParent
15716 then
15717 null;
15718
15719 -- If the derived type is within the parent type's declarative
15720 -- region, then the components can still be inherited even though
15721 -- they aren't visible at this point. This can occur for cases
15722 -- such as within public child units where the components must
15723 -- become visible upon entering the child unit's private part.
15724
15725 elsif not Is_Visible_Component (Component)
15726 and then not In_Open_Scopes (Scope (Parent_Base))
15727 then
15728 null;
15729
15730 elsif Ekind_In (Derived_Base, E_Private_Type,
15731 E_Limited_Private_Type)
15732 then
15733 null;
15734
15735 else
15736 Inherit_Component (Component);
15737 end if;
15738
15739 Next_Entity (Component);
15740 end loop;
15741
15742 -- For tagged derived types, inherited discriminants cannot be used in
15743 -- component declarations of the record extension part. To achieve this
15744 -- we mark the inherited discriminants as not visible.
15745
15746 if Is_Tagged and then Inherit_Discr then
15747 D := First_Discriminant (Derived_Base);
15748 while Present (D) loop
15749 Set_Is_Immediately_Visible (D, False);
15750 Next_Discriminant (D);
15751 end loop;
15752 end if;
15753
15754 return Assoc_List;
15755 end Inherit_Components;
15756
15757 -----------------------
15758 -- Is_Constant_Bound --
15759 -----------------------
15760
15761 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
15762 begin
15763 if Compile_Time_Known_Value (Exp) then
15764 return True;
15765
15766 elsif Is_Entity_Name (Exp)
15767 and then Present (Entity (Exp))
15768 then
15769 return Is_Constant_Object (Entity (Exp))
15770 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
15771
15772 elsif Nkind (Exp) in N_Binary_Op then
15773 return Is_Constant_Bound (Left_Opnd (Exp))
15774 and then Is_Constant_Bound (Right_Opnd (Exp))
15775 and then Scope (Entity (Exp)) = Standard_Standard;
15776
15777 else
15778 return False;
15779 end if;
15780 end Is_Constant_Bound;
15781
15782 -----------------------
15783 -- Is_Null_Extension --
15784 -----------------------
15785
15786 function Is_Null_Extension (T : Entity_Id) return Boolean is
15787 Type_Decl : constant Node_Id := Parent (Base_Type (T));
15788 Comp_List : Node_Id;
15789 Comp : Node_Id;
15790
15791 begin
15792 if Nkind (Type_Decl) /= N_Full_Type_Declaration
15793 or else not Is_Tagged_Type (T)
15794 or else Nkind (Type_Definition (Type_Decl)) /=
15795 N_Derived_Type_Definition
15796 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
15797 then
15798 return False;
15799 end if;
15800
15801 Comp_List :=
15802 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
15803
15804 if Present (Discriminant_Specifications (Type_Decl)) then
15805 return False;
15806
15807 elsif Present (Comp_List)
15808 and then Is_Non_Empty_List (Component_Items (Comp_List))
15809 then
15810 Comp := First (Component_Items (Comp_List));
15811
15812 -- Only user-defined components are relevant. The component list
15813 -- may also contain a parent component and internal components
15814 -- corresponding to secondary tags, but these do not determine
15815 -- whether this is a null extension.
15816
15817 while Present (Comp) loop
15818 if Comes_From_Source (Comp) then
15819 return False;
15820 end if;
15821
15822 Next (Comp);
15823 end loop;
15824
15825 return True;
15826 else
15827 return True;
15828 end if;
15829 end Is_Null_Extension;
15830
15831 ------------------------------
15832 -- Is_Valid_Constraint_Kind --
15833 ------------------------------
15834
15835 function Is_Valid_Constraint_Kind
15836 (T_Kind : Type_Kind;
15837 Constraint_Kind : Node_Kind) return Boolean
15838 is
15839 begin
15840 case T_Kind is
15841 when Enumeration_Kind |
15842 Integer_Kind =>
15843 return Constraint_Kind = N_Range_Constraint;
15844
15845 when Decimal_Fixed_Point_Kind =>
15846 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
15847 N_Range_Constraint);
15848
15849 when Ordinary_Fixed_Point_Kind =>
15850 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
15851 N_Range_Constraint);
15852
15853 when Float_Kind =>
15854 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
15855 N_Range_Constraint);
15856
15857 when Access_Kind |
15858 Array_Kind |
15859 E_Record_Type |
15860 E_Record_Subtype |
15861 Class_Wide_Kind |
15862 E_Incomplete_Type |
15863 Private_Kind |
15864 Concurrent_Kind =>
15865 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
15866
15867 when others =>
15868 return True; -- Error will be detected later
15869 end case;
15870 end Is_Valid_Constraint_Kind;
15871
15872 --------------------------
15873 -- Is_Visible_Component --
15874 --------------------------
15875
15876 function Is_Visible_Component (C : Entity_Id) return Boolean is
15877 Original_Comp : Entity_Id := Empty;
15878 Original_Scope : Entity_Id;
15879 Type_Scope : Entity_Id;
15880
15881 function Is_Local_Type (Typ : Entity_Id) return Boolean;
15882 -- Check whether parent type of inherited component is declared locally,
15883 -- possibly within a nested package or instance. The current scope is
15884 -- the derived record itself.
15885
15886 -------------------
15887 -- Is_Local_Type --
15888 -------------------
15889
15890 function Is_Local_Type (Typ : Entity_Id) return Boolean is
15891 Scop : Entity_Id;
15892
15893 begin
15894 Scop := Scope (Typ);
15895 while Present (Scop)
15896 and then Scop /= Standard_Standard
15897 loop
15898 if Scop = Scope (Current_Scope) then
15899 return True;
15900 end if;
15901
15902 Scop := Scope (Scop);
15903 end loop;
15904
15905 return False;
15906 end Is_Local_Type;
15907
15908 -- Start of processing for Is_Visible_Component
15909
15910 begin
15911 if Ekind_In (C, E_Component, E_Discriminant) then
15912 Original_Comp := Original_Record_Component (C);
15913 end if;
15914
15915 if No (Original_Comp) then
15916
15917 -- Premature usage, or previous error
15918
15919 return False;
15920
15921 else
15922 Original_Scope := Scope (Original_Comp);
15923 Type_Scope := Scope (Base_Type (Scope (C)));
15924 end if;
15925
15926 -- This test only concerns tagged types
15927
15928 if not Is_Tagged_Type (Original_Scope) then
15929 return True;
15930
15931 -- If it is _Parent or _Tag, there is no visibility issue
15932
15933 elsif not Comes_From_Source (Original_Comp) then
15934 return True;
15935
15936 -- If we are in the body of an instantiation, the component is visible
15937 -- even when the parent type (possibly defined in an enclosing unit or
15938 -- in a parent unit) might not.
15939
15940 elsif In_Instance_Body then
15941 return True;
15942
15943 -- Discriminants are always visible
15944
15945 elsif Ekind (Original_Comp) = E_Discriminant
15946 and then not Has_Unknown_Discriminants (Original_Scope)
15947 then
15948 return True;
15949
15950 -- If the component has been declared in an ancestor which is currently
15951 -- a private type, then it is not visible. The same applies if the
15952 -- component's containing type is not in an open scope and the original
15953 -- component's enclosing type is a visible full view of a private type
15954 -- (which can occur in cases where an attempt is being made to reference
15955 -- a component in a sibling package that is inherited from a visible
15956 -- component of a type in an ancestor package; the component in the
15957 -- sibling package should not be visible even though the component it
15958 -- inherited from is visible). This does not apply however in the case
15959 -- where the scope of the type is a private child unit, or when the
15960 -- parent comes from a local package in which the ancestor is currently
15961 -- visible. The latter suppression of visibility is needed for cases
15962 -- that are tested in B730006.
15963
15964 elsif Is_Private_Type (Original_Scope)
15965 or else
15966 (not Is_Private_Descendant (Type_Scope)
15967 and then not In_Open_Scopes (Type_Scope)
15968 and then Has_Private_Declaration (Original_Scope))
15969 then
15970 -- If the type derives from an entity in a formal package, there
15971 -- are no additional visible components.
15972
15973 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
15974 N_Formal_Package_Declaration
15975 then
15976 return False;
15977
15978 -- if we are not in the private part of the current package, there
15979 -- are no additional visible components.
15980
15981 elsif Ekind (Scope (Current_Scope)) = E_Package
15982 and then not In_Private_Part (Scope (Current_Scope))
15983 then
15984 return False;
15985 else
15986 return
15987 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
15988 and then In_Open_Scopes (Scope (Original_Scope))
15989 and then Is_Local_Type (Type_Scope);
15990 end if;
15991
15992 -- There is another weird way in which a component may be invisible
15993 -- when the private and the full view are not derived from the same
15994 -- ancestor. Here is an example :
15995
15996 -- type A1 is tagged record F1 : integer; end record;
15997 -- type A2 is new A1 with record F2 : integer; end record;
15998 -- type T is new A1 with private;
15999 -- private
16000 -- type T is new A2 with null record;
16001
16002 -- In this case, the full view of T inherits F1 and F2 but the private
16003 -- view inherits only F1
16004
16005 else
16006 declare
16007 Ancestor : Entity_Id := Scope (C);
16008
16009 begin
16010 loop
16011 if Ancestor = Original_Scope then
16012 return True;
16013 elsif Ancestor = Etype (Ancestor) then
16014 return False;
16015 end if;
16016
16017 Ancestor := Etype (Ancestor);
16018 end loop;
16019 end;
16020 end if;
16021 end Is_Visible_Component;
16022
16023 --------------------------
16024 -- Make_Class_Wide_Type --
16025 --------------------------
16026
16027 procedure Make_Class_Wide_Type (T : Entity_Id) is
16028 CW_Type : Entity_Id;
16029 CW_Name : Name_Id;
16030 Next_E : Entity_Id;
16031
16032 begin
16033 -- The class wide type can have been defined by the partial view, in
16034 -- which case everything is already done.
16035
16036 if Present (Class_Wide_Type (T)) then
16037 return;
16038 end if;
16039
16040 CW_Type :=
16041 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
16042
16043 -- Inherit root type characteristics
16044
16045 CW_Name := Chars (CW_Type);
16046 Next_E := Next_Entity (CW_Type);
16047 Copy_Node (T, CW_Type);
16048 Set_Comes_From_Source (CW_Type, False);
16049 Set_Chars (CW_Type, CW_Name);
16050 Set_Parent (CW_Type, Parent (T));
16051 Set_Next_Entity (CW_Type, Next_E);
16052
16053 -- Ensure we have a new freeze node for the class-wide type. The partial
16054 -- view may have freeze action of its own, requiring a proper freeze
16055 -- node, and the same freeze node cannot be shared between the two
16056 -- types.
16057
16058 Set_Has_Delayed_Freeze (CW_Type);
16059 Set_Freeze_Node (CW_Type, Empty);
16060
16061 -- Customize the class-wide type: It has no prim. op., it cannot be
16062 -- abstract and its Etype points back to the specific root type.
16063
16064 Set_Ekind (CW_Type, E_Class_Wide_Type);
16065 Set_Is_Tagged_Type (CW_Type, True);
16066 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
16067 Set_Is_Abstract_Type (CW_Type, False);
16068 Set_Is_Constrained (CW_Type, False);
16069 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
16070
16071 if Ekind (T) = E_Class_Wide_Subtype then
16072 Set_Etype (CW_Type, Etype (Base_Type (T)));
16073 else
16074 Set_Etype (CW_Type, T);
16075 end if;
16076
16077 -- If this is the class_wide type of a constrained subtype, it does
16078 -- not have discriminants.
16079
16080 Set_Has_Discriminants (CW_Type,
16081 Has_Discriminants (T) and then not Is_Constrained (T));
16082
16083 Set_Has_Unknown_Discriminants (CW_Type, True);
16084 Set_Class_Wide_Type (T, CW_Type);
16085 Set_Equivalent_Type (CW_Type, Empty);
16086
16087 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
16088
16089 Set_Class_Wide_Type (CW_Type, CW_Type);
16090 end Make_Class_Wide_Type;
16091
16092 ----------------
16093 -- Make_Index --
16094 ----------------
16095
16096 procedure Make_Index
16097 (I : Node_Id;
16098 Related_Nod : Node_Id;
16099 Related_Id : Entity_Id := Empty;
16100 Suffix_Index : Nat := 1)
16101 is
16102 R : Node_Id;
16103 T : Entity_Id;
16104 Def_Id : Entity_Id := Empty;
16105 Found : Boolean := False;
16106
16107 begin
16108 -- For a discrete range used in a constrained array definition and
16109 -- defined by a range, an implicit conversion to the predefined type
16110 -- INTEGER is assumed if each bound is either a numeric literal, a named
16111 -- number, or an attribute, and the type of both bounds (prior to the
16112 -- implicit conversion) is the type universal_integer. Otherwise, both
16113 -- bounds must be of the same discrete type, other than universal
16114 -- integer; this type must be determinable independently of the
16115 -- context, but using the fact that the type must be discrete and that
16116 -- both bounds must have the same type.
16117
16118 -- Character literals also have a universal type in the absence of
16119 -- of additional context, and are resolved to Standard_Character.
16120
16121 if Nkind (I) = N_Range then
16122
16123 -- The index is given by a range constraint. The bounds are known
16124 -- to be of a consistent type.
16125
16126 if not Is_Overloaded (I) then
16127 T := Etype (I);
16128
16129 -- For universal bounds, choose the specific predefined type
16130
16131 if T = Universal_Integer then
16132 T := Standard_Integer;
16133
16134 elsif T = Any_Character then
16135 Ambiguous_Character (Low_Bound (I));
16136
16137 T := Standard_Character;
16138 end if;
16139
16140 -- The node may be overloaded because some user-defined operators
16141 -- are available, but if a universal interpretation exists it is
16142 -- also the selected one.
16143
16144 elsif Universal_Interpretation (I) = Universal_Integer then
16145 T := Standard_Integer;
16146
16147 else
16148 T := Any_Type;
16149
16150 declare
16151 Ind : Interp_Index;
16152 It : Interp;
16153
16154 begin
16155 Get_First_Interp (I, Ind, It);
16156 while Present (It.Typ) loop
16157 if Is_Discrete_Type (It.Typ) then
16158
16159 if Found
16160 and then not Covers (It.Typ, T)
16161 and then not Covers (T, It.Typ)
16162 then
16163 Error_Msg_N ("ambiguous bounds in discrete range", I);
16164 exit;
16165 else
16166 T := It.Typ;
16167 Found := True;
16168 end if;
16169 end if;
16170
16171 Get_Next_Interp (Ind, It);
16172 end loop;
16173
16174 if T = Any_Type then
16175 Error_Msg_N ("discrete type required for range", I);
16176 Set_Etype (I, Any_Type);
16177 return;
16178
16179 elsif T = Universal_Integer then
16180 T := Standard_Integer;
16181 end if;
16182 end;
16183 end if;
16184
16185 if not Is_Discrete_Type (T) then
16186 Error_Msg_N ("discrete type required for range", I);
16187 Set_Etype (I, Any_Type);
16188 return;
16189 end if;
16190
16191 if Nkind (Low_Bound (I)) = N_Attribute_Reference
16192 and then Attribute_Name (Low_Bound (I)) = Name_First
16193 and then Is_Entity_Name (Prefix (Low_Bound (I)))
16194 and then Is_Type (Entity (Prefix (Low_Bound (I))))
16195 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
16196 then
16197 -- The type of the index will be the type of the prefix, as long
16198 -- as the upper bound is 'Last of the same type.
16199
16200 Def_Id := Entity (Prefix (Low_Bound (I)));
16201
16202 if Nkind (High_Bound (I)) /= N_Attribute_Reference
16203 or else Attribute_Name (High_Bound (I)) /= Name_Last
16204 or else not Is_Entity_Name (Prefix (High_Bound (I)))
16205 or else Entity (Prefix (High_Bound (I))) /= Def_Id
16206 then
16207 Def_Id := Empty;
16208 end if;
16209 end if;
16210
16211 R := I;
16212 Process_Range_Expr_In_Decl (R, T);
16213
16214 elsif Nkind (I) = N_Subtype_Indication then
16215
16216 -- The index is given by a subtype with a range constraint
16217
16218 T := Base_Type (Entity (Subtype_Mark (I)));
16219
16220 if not Is_Discrete_Type (T) then
16221 Error_Msg_N ("discrete type required for range", I);
16222 Set_Etype (I, Any_Type);
16223 return;
16224 end if;
16225
16226 R := Range_Expression (Constraint (I));
16227
16228 Resolve (R, T);
16229 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
16230
16231 elsif Nkind (I) = N_Attribute_Reference then
16232
16233 -- The parser guarantees that the attribute is a RANGE attribute
16234
16235 -- If the node denotes the range of a type mark, that is also the
16236 -- resulting type, and we do no need to create an Itype for it.
16237
16238 if Is_Entity_Name (Prefix (I))
16239 and then Comes_From_Source (I)
16240 and then Is_Type (Entity (Prefix (I)))
16241 and then Is_Discrete_Type (Entity (Prefix (I)))
16242 then
16243 Def_Id := Entity (Prefix (I));
16244 end if;
16245
16246 Analyze_And_Resolve (I);
16247 T := Etype (I);
16248 R := I;
16249
16250 -- If none of the above, must be a subtype. We convert this to a
16251 -- range attribute reference because in the case of declared first
16252 -- named subtypes, the types in the range reference can be different
16253 -- from the type of the entity. A range attribute normalizes the
16254 -- reference and obtains the correct types for the bounds.
16255
16256 -- This transformation is in the nature of an expansion, is only
16257 -- done if expansion is active. In particular, it is not done on
16258 -- formal generic types, because we need to retain the name of the
16259 -- original index for instantiation purposes.
16260
16261 else
16262 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
16263 Error_Msg_N ("invalid subtype mark in discrete range ", I);
16264 Set_Etype (I, Any_Integer);
16265 return;
16266
16267 else
16268 -- The type mark may be that of an incomplete type. It is only
16269 -- now that we can get the full view, previous analysis does
16270 -- not look specifically for a type mark.
16271
16272 Set_Entity (I, Get_Full_View (Entity (I)));
16273 Set_Etype (I, Entity (I));
16274 Def_Id := Entity (I);
16275
16276 if not Is_Discrete_Type (Def_Id) then
16277 Error_Msg_N ("discrete type required for index", I);
16278 Set_Etype (I, Any_Type);
16279 return;
16280 end if;
16281 end if;
16282
16283 if Expander_Active then
16284 Rewrite (I,
16285 Make_Attribute_Reference (Sloc (I),
16286 Attribute_Name => Name_Range,
16287 Prefix => Relocate_Node (I)));
16288
16289 -- The original was a subtype mark that does not freeze. This
16290 -- means that the rewritten version must not freeze either.
16291
16292 Set_Must_Not_Freeze (I);
16293 Set_Must_Not_Freeze (Prefix (I));
16294
16295 -- Is order critical??? if so, document why, if not
16296 -- use Analyze_And_Resolve
16297
16298 Analyze_And_Resolve (I);
16299 T := Etype (I);
16300 R := I;
16301
16302 -- If expander is inactive, type is legal, nothing else to construct
16303
16304 else
16305 return;
16306 end if;
16307 end if;
16308
16309 if not Is_Discrete_Type (T) then
16310 Error_Msg_N ("discrete type required for range", I);
16311 Set_Etype (I, Any_Type);
16312 return;
16313
16314 elsif T = Any_Type then
16315 Set_Etype (I, Any_Type);
16316 return;
16317 end if;
16318
16319 -- We will now create the appropriate Itype to describe the range, but
16320 -- first a check. If we originally had a subtype, then we just label
16321 -- the range with this subtype. Not only is there no need to construct
16322 -- a new subtype, but it is wrong to do so for two reasons:
16323
16324 -- 1. A legality concern, if we have a subtype, it must not freeze,
16325 -- and the Itype would cause freezing incorrectly
16326
16327 -- 2. An efficiency concern, if we created an Itype, it would not be
16328 -- recognized as the same type for the purposes of eliminating
16329 -- checks in some circumstances.
16330
16331 -- We signal this case by setting the subtype entity in Def_Id
16332
16333 if No (Def_Id) then
16334 Def_Id :=
16335 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
16336 Set_Etype (Def_Id, Base_Type (T));
16337
16338 if Is_Signed_Integer_Type (T) then
16339 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
16340
16341 elsif Is_Modular_Integer_Type (T) then
16342 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
16343
16344 else
16345 Set_Ekind (Def_Id, E_Enumeration_Subtype);
16346 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
16347 Set_First_Literal (Def_Id, First_Literal (T));
16348 end if;
16349
16350 Set_Size_Info (Def_Id, (T));
16351 Set_RM_Size (Def_Id, RM_Size (T));
16352 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
16353
16354 Set_Scalar_Range (Def_Id, R);
16355 Conditional_Delay (Def_Id, T);
16356
16357 -- In the subtype indication case, if the immediate parent of the
16358 -- new subtype is non-static, then the subtype we create is non-
16359 -- static, even if its bounds are static.
16360
16361 if Nkind (I) = N_Subtype_Indication
16362 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
16363 then
16364 Set_Is_Non_Static_Subtype (Def_Id);
16365 end if;
16366 end if;
16367
16368 -- Final step is to label the index with this constructed type
16369
16370 Set_Etype (I, Def_Id);
16371 end Make_Index;
16372
16373 ------------------------------
16374 -- Modular_Type_Declaration --
16375 ------------------------------
16376
16377 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16378 Mod_Expr : constant Node_Id := Expression (Def);
16379 M_Val : Uint;
16380
16381 procedure Set_Modular_Size (Bits : Int);
16382 -- Sets RM_Size to Bits, and Esize to normal word size above this
16383
16384 ----------------------
16385 -- Set_Modular_Size --
16386 ----------------------
16387
16388 procedure Set_Modular_Size (Bits : Int) is
16389 begin
16390 Set_RM_Size (T, UI_From_Int (Bits));
16391
16392 if Bits <= 8 then
16393 Init_Esize (T, 8);
16394
16395 elsif Bits <= 16 then
16396 Init_Esize (T, 16);
16397
16398 elsif Bits <= 32 then
16399 Init_Esize (T, 32);
16400
16401 else
16402 Init_Esize (T, System_Max_Binary_Modulus_Power);
16403 end if;
16404
16405 if not Non_Binary_Modulus (T)
16406 and then Esize (T) = RM_Size (T)
16407 then
16408 Set_Is_Known_Valid (T);
16409 end if;
16410 end Set_Modular_Size;
16411
16412 -- Start of processing for Modular_Type_Declaration
16413
16414 begin
16415 Analyze_And_Resolve (Mod_Expr, Any_Integer);
16416 Set_Etype (T, T);
16417 Set_Ekind (T, E_Modular_Integer_Type);
16418 Init_Alignment (T);
16419 Set_Is_Constrained (T);
16420
16421 if not Is_OK_Static_Expression (Mod_Expr) then
16422 Flag_Non_Static_Expr
16423 ("non-static expression used for modular type bound!", Mod_Expr);
16424 M_Val := 2 ** System_Max_Binary_Modulus_Power;
16425 else
16426 M_Val := Expr_Value (Mod_Expr);
16427 end if;
16428
16429 if M_Val < 1 then
16430 Error_Msg_N ("modulus value must be positive", Mod_Expr);
16431 M_Val := 2 ** System_Max_Binary_Modulus_Power;
16432 end if;
16433
16434 Set_Modulus (T, M_Val);
16435
16436 -- Create bounds for the modular type based on the modulus given in
16437 -- the type declaration and then analyze and resolve those bounds.
16438
16439 Set_Scalar_Range (T,
16440 Make_Range (Sloc (Mod_Expr),
16441 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
16442 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
16443
16444 -- Properly analyze the literals for the range. We do this manually
16445 -- because we can't go calling Resolve, since we are resolving these
16446 -- bounds with the type, and this type is certainly not complete yet!
16447
16448 Set_Etype (Low_Bound (Scalar_Range (T)), T);
16449 Set_Etype (High_Bound (Scalar_Range (T)), T);
16450 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
16451 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
16452
16453 -- Loop through powers of two to find number of bits required
16454
16455 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
16456
16457 -- Binary case
16458
16459 if M_Val = 2 ** Bits then
16460 Set_Modular_Size (Bits);
16461 return;
16462
16463 -- Non-binary case
16464
16465 elsif M_Val < 2 ** Bits then
16466 Check_Formal_Restriction ("modulus should be a power of 2", T);
16467 Set_Non_Binary_Modulus (T);
16468
16469 if Bits > System_Max_Nonbinary_Modulus_Power then
16470 Error_Msg_Uint_1 :=
16471 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
16472 Error_Msg_F
16473 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
16474 Set_Modular_Size (System_Max_Binary_Modulus_Power);
16475 return;
16476
16477 else
16478 -- In the non-binary case, set size as per RM 13.3(55)
16479
16480 Set_Modular_Size (Bits);
16481 return;
16482 end if;
16483 end if;
16484
16485 end loop;
16486
16487 -- If we fall through, then the size exceed System.Max_Binary_Modulus
16488 -- so we just signal an error and set the maximum size.
16489
16490 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
16491 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
16492
16493 Set_Modular_Size (System_Max_Binary_Modulus_Power);
16494 Init_Alignment (T);
16495
16496 end Modular_Type_Declaration;
16497
16498 --------------------------
16499 -- New_Concatenation_Op --
16500 --------------------------
16501
16502 procedure New_Concatenation_Op (Typ : Entity_Id) is
16503 Loc : constant Source_Ptr := Sloc (Typ);
16504 Op : Entity_Id;
16505
16506 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
16507 -- Create abbreviated declaration for the formal of a predefined
16508 -- Operator 'Op' of type 'Typ'
16509
16510 --------------------
16511 -- Make_Op_Formal --
16512 --------------------
16513
16514 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
16515 Formal : Entity_Id;
16516 begin
16517 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
16518 Set_Etype (Formal, Typ);
16519 Set_Mechanism (Formal, Default_Mechanism);
16520 return Formal;
16521 end Make_Op_Formal;
16522
16523 -- Start of processing for New_Concatenation_Op
16524
16525 begin
16526 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
16527
16528 Set_Ekind (Op, E_Operator);
16529 Set_Scope (Op, Current_Scope);
16530 Set_Etype (Op, Typ);
16531 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
16532 Set_Is_Immediately_Visible (Op);
16533 Set_Is_Intrinsic_Subprogram (Op);
16534 Set_Has_Completion (Op);
16535 Append_Entity (Op, Current_Scope);
16536
16537 Set_Name_Entity_Id (Name_Op_Concat, Op);
16538
16539 Append_Entity (Make_Op_Formal (Typ, Op), Op);
16540 Append_Entity (Make_Op_Formal (Typ, Op), Op);
16541 end New_Concatenation_Op;
16542
16543 -------------------------
16544 -- OK_For_Limited_Init --
16545 -------------------------
16546
16547 -- ???Check all calls of this, and compare the conditions under which it's
16548 -- called.
16549
16550 function OK_For_Limited_Init
16551 (Typ : Entity_Id;
16552 Exp : Node_Id) return Boolean
16553 is
16554 begin
16555 return Is_CPP_Constructor_Call (Exp)
16556 or else (Ada_Version >= Ada_2005
16557 and then not Debug_Flag_Dot_L
16558 and then OK_For_Limited_Init_In_05 (Typ, Exp));
16559 end OK_For_Limited_Init;
16560
16561 -------------------------------
16562 -- OK_For_Limited_Init_In_05 --
16563 -------------------------------
16564
16565 function OK_For_Limited_Init_In_05
16566 (Typ : Entity_Id;
16567 Exp : Node_Id) return Boolean
16568 is
16569 begin
16570 -- An object of a limited interface type can be initialized with any
16571 -- expression of a nonlimited descendant type.
16572
16573 if Is_Class_Wide_Type (Typ)
16574 and then Is_Limited_Interface (Typ)
16575 and then not Is_Limited_Type (Etype (Exp))
16576 then
16577 return True;
16578 end if;
16579
16580 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
16581 -- case of limited aggregates (including extension aggregates), and
16582 -- function calls. The function call may have been given in prefixed
16583 -- notation, in which case the original node is an indexed component.
16584 -- If the function is parameterless, the original node was an explicit
16585 -- dereference.
16586
16587 case Nkind (Original_Node (Exp)) is
16588 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
16589 return True;
16590
16591 when N_Qualified_Expression =>
16592 return
16593 OK_For_Limited_Init_In_05
16594 (Typ, Expression (Original_Node (Exp)));
16595
16596 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
16597 -- with a function call, the expander has rewritten the call into an
16598 -- N_Type_Conversion node to force displacement of the pointer to
16599 -- reference the component containing the secondary dispatch table.
16600 -- Otherwise a type conversion is not a legal context.
16601 -- A return statement for a build-in-place function returning a
16602 -- synchronized type also introduces an unchecked conversion.
16603
16604 when N_Type_Conversion |
16605 N_Unchecked_Type_Conversion =>
16606 return not Comes_From_Source (Exp)
16607 and then
16608 OK_For_Limited_Init_In_05
16609 (Typ, Expression (Original_Node (Exp)));
16610
16611 when N_Indexed_Component |
16612 N_Selected_Component |
16613 N_Explicit_Dereference =>
16614 return Nkind (Exp) = N_Function_Call;
16615
16616 -- A use of 'Input is a function call, hence allowed. Normally the
16617 -- attribute will be changed to a call, but the attribute by itself
16618 -- can occur with -gnatc.
16619
16620 when N_Attribute_Reference =>
16621 return Attribute_Name (Original_Node (Exp)) = Name_Input;
16622
16623 when others =>
16624 return False;
16625 end case;
16626 end OK_For_Limited_Init_In_05;
16627
16628 -------------------------------------------
16629 -- Ordinary_Fixed_Point_Type_Declaration --
16630 -------------------------------------------
16631
16632 procedure Ordinary_Fixed_Point_Type_Declaration
16633 (T : Entity_Id;
16634 Def : Node_Id)
16635 is
16636 Loc : constant Source_Ptr := Sloc (Def);
16637 Delta_Expr : constant Node_Id := Delta_Expression (Def);
16638 RRS : constant Node_Id := Real_Range_Specification (Def);
16639 Implicit_Base : Entity_Id;
16640 Delta_Val : Ureal;
16641 Small_Val : Ureal;
16642 Low_Val : Ureal;
16643 High_Val : Ureal;
16644
16645 begin
16646 Check_Restriction (No_Fixed_Point, Def);
16647
16648 -- Create implicit base type
16649
16650 Implicit_Base :=
16651 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
16652 Set_Etype (Implicit_Base, Implicit_Base);
16653
16654 -- Analyze and process delta expression
16655
16656 Analyze_And_Resolve (Delta_Expr, Any_Real);
16657
16658 Check_Delta_Expression (Delta_Expr);
16659 Delta_Val := Expr_Value_R (Delta_Expr);
16660
16661 Set_Delta_Value (Implicit_Base, Delta_Val);
16662
16663 -- Compute default small from given delta, which is the largest power
16664 -- of two that does not exceed the given delta value.
16665
16666 declare
16667 Tmp : Ureal;
16668 Scale : Int;
16669
16670 begin
16671 Tmp := Ureal_1;
16672 Scale := 0;
16673
16674 if Delta_Val < Ureal_1 then
16675 while Delta_Val < Tmp loop
16676 Tmp := Tmp / Ureal_2;
16677 Scale := Scale + 1;
16678 end loop;
16679
16680 else
16681 loop
16682 Tmp := Tmp * Ureal_2;
16683 exit when Tmp > Delta_Val;
16684 Scale := Scale - 1;
16685 end loop;
16686 end if;
16687
16688 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
16689 end;
16690
16691 Set_Small_Value (Implicit_Base, Small_Val);
16692
16693 -- If no range was given, set a dummy range
16694
16695 if RRS <= Empty_Or_Error then
16696 Low_Val := -Small_Val;
16697 High_Val := Small_Val;
16698
16699 -- Otherwise analyze and process given range
16700
16701 else
16702 declare
16703 Low : constant Node_Id := Low_Bound (RRS);
16704 High : constant Node_Id := High_Bound (RRS);
16705
16706 begin
16707 Analyze_And_Resolve (Low, Any_Real);
16708 Analyze_And_Resolve (High, Any_Real);
16709 Check_Real_Bound (Low);
16710 Check_Real_Bound (High);
16711
16712 -- Obtain and set the range
16713
16714 Low_Val := Expr_Value_R (Low);
16715 High_Val := Expr_Value_R (High);
16716
16717 if Low_Val > High_Val then
16718 Error_Msg_NE ("?fixed point type& has null range", Def, T);
16719 end if;
16720 end;
16721 end if;
16722
16723 -- The range for both the implicit base and the declared first subtype
16724 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
16725 -- set a temporary range in place. Note that the bounds of the base
16726 -- type will be widened to be symmetrical and to fill the available
16727 -- bits when the type is frozen.
16728
16729 -- We could do this with all discrete types, and probably should, but
16730 -- we absolutely have to do it for fixed-point, since the end-points
16731 -- of the range and the size are determined by the small value, which
16732 -- could be reset before the freeze point.
16733
16734 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
16735 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
16736
16737 -- Complete definition of first subtype
16738
16739 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
16740 Set_Etype (T, Implicit_Base);
16741 Init_Size_Align (T);
16742 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
16743 Set_Small_Value (T, Small_Val);
16744 Set_Delta_Value (T, Delta_Val);
16745 Set_Is_Constrained (T);
16746
16747 end Ordinary_Fixed_Point_Type_Declaration;
16748
16749 ----------------------------------------
16750 -- Prepare_Private_Subtype_Completion --
16751 ----------------------------------------
16752
16753 procedure Prepare_Private_Subtype_Completion
16754 (Id : Entity_Id;
16755 Related_Nod : Node_Id)
16756 is
16757 Id_B : constant Entity_Id := Base_Type (Id);
16758 Full_B : constant Entity_Id := Full_View (Id_B);
16759 Full : Entity_Id;
16760
16761 begin
16762 if Present (Full_B) then
16763
16764 -- The Base_Type is already completed, we can complete the subtype
16765 -- now. We have to create a new entity with the same name, Thus we
16766 -- can't use Create_Itype.
16767
16768 -- This is messy, should be fixed ???
16769
16770 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
16771 Set_Is_Itype (Full);
16772 Set_Associated_Node_For_Itype (Full, Related_Nod);
16773 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
16774 end if;
16775
16776 -- The parent subtype may be private, but the base might not, in some
16777 -- nested instances. In that case, the subtype does not need to be
16778 -- exchanged. It would still be nice to make private subtypes and their
16779 -- bases consistent at all times ???
16780
16781 if Is_Private_Type (Id_B) then
16782 Append_Elmt (Id, Private_Dependents (Id_B));
16783 end if;
16784
16785 end Prepare_Private_Subtype_Completion;
16786
16787 ---------------------------
16788 -- Process_Discriminants --
16789 ---------------------------
16790
16791 procedure Process_Discriminants
16792 (N : Node_Id;
16793 Prev : Entity_Id := Empty)
16794 is
16795 Elist : constant Elist_Id := New_Elmt_List;
16796 Id : Node_Id;
16797 Discr : Node_Id;
16798 Discr_Number : Uint;
16799 Discr_Type : Entity_Id;
16800 Default_Present : Boolean := False;
16801 Default_Not_Present : Boolean := False;
16802
16803 begin
16804 -- A composite type other than an array type can have discriminants.
16805 -- On entry, the current scope is the composite type.
16806
16807 -- The discriminants are initially entered into the scope of the type
16808 -- via Enter_Name with the default Ekind of E_Void to prevent premature
16809 -- use, as explained at the end of this procedure.
16810
16811 Discr := First (Discriminant_Specifications (N));
16812 while Present (Discr) loop
16813 Enter_Name (Defining_Identifier (Discr));
16814
16815 -- For navigation purposes we add a reference to the discriminant
16816 -- in the entity for the type. If the current declaration is a
16817 -- completion, place references on the partial view. Otherwise the
16818 -- type is the current scope.
16819
16820 if Present (Prev) then
16821
16822 -- The references go on the partial view, if present. If the
16823 -- partial view has discriminants, the references have been
16824 -- generated already.
16825
16826 if not Has_Discriminants (Prev) then
16827 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
16828 end if;
16829 else
16830 Generate_Reference
16831 (Current_Scope, Defining_Identifier (Discr), 'd');
16832 end if;
16833
16834 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
16835 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
16836
16837 -- Ada 2005 (AI-254)
16838
16839 if Present (Access_To_Subprogram_Definition
16840 (Discriminant_Type (Discr)))
16841 and then Protected_Present (Access_To_Subprogram_Definition
16842 (Discriminant_Type (Discr)))
16843 then
16844 Discr_Type :=
16845 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
16846 end if;
16847
16848 else
16849 Find_Type (Discriminant_Type (Discr));
16850 Discr_Type := Etype (Discriminant_Type (Discr));
16851
16852 if Error_Posted (Discriminant_Type (Discr)) then
16853 Discr_Type := Any_Type;
16854 end if;
16855 end if;
16856
16857 if Is_Access_Type (Discr_Type) then
16858
16859 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
16860 -- record types
16861
16862 if Ada_Version < Ada_2005 then
16863 Check_Access_Discriminant_Requires_Limited
16864 (Discr, Discriminant_Type (Discr));
16865 end if;
16866
16867 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
16868 Error_Msg_N
16869 ("(Ada 83) access discriminant not allowed", Discr);
16870 end if;
16871
16872 elsif not Is_Discrete_Type (Discr_Type) then
16873 Error_Msg_N ("discriminants must have a discrete or access type",
16874 Discriminant_Type (Discr));
16875 end if;
16876
16877 Set_Etype (Defining_Identifier (Discr), Discr_Type);
16878
16879 -- If a discriminant specification includes the assignment compound
16880 -- delimiter followed by an expression, the expression is the default
16881 -- expression of the discriminant; the default expression must be of
16882 -- the type of the discriminant. (RM 3.7.1) Since this expression is
16883 -- a default expression, we do the special preanalysis, since this
16884 -- expression does not freeze (see "Handling of Default and Per-
16885 -- Object Expressions" in spec of package Sem).
16886
16887 if Present (Expression (Discr)) then
16888 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
16889
16890 if Nkind (N) = N_Formal_Type_Declaration then
16891 Error_Msg_N
16892 ("discriminant defaults not allowed for formal type",
16893 Expression (Discr));
16894
16895 -- Flag an error for a tagged type with defaulted discriminants,
16896 -- excluding limited tagged types when compiling for Ada 2012
16897 -- (see AI05-0214).
16898
16899 elsif Is_Tagged_Type (Current_Scope)
16900 and then (not Is_Limited_Type (Current_Scope)
16901 or else Ada_Version < Ada_2012)
16902 and then Comes_From_Source (N)
16903 then
16904 -- Note: see similar test in Check_Or_Process_Discriminants, to
16905 -- handle the (illegal) case of the completion of an untagged
16906 -- view with discriminants with defaults by a tagged full view.
16907 -- We skip the check if Discr does not come from source, to
16908 -- account for the case of an untagged derived type providing
16909 -- defaults for a renamed discriminant from a private untagged
16910 -- ancestor with a tagged full view (ACATS B460006).
16911
16912 if Ada_Version >= Ada_2012 then
16913 Error_Msg_N
16914 ("discriminants of nonlimited tagged type cannot have"
16915 & " defaults",
16916 Expression (Discr));
16917 else
16918 Error_Msg_N
16919 ("discriminants of tagged type cannot have defaults",
16920 Expression (Discr));
16921 end if;
16922
16923 else
16924 Default_Present := True;
16925 Append_Elmt (Expression (Discr), Elist);
16926
16927 -- Tag the defining identifiers for the discriminants with
16928 -- their corresponding default expressions from the tree.
16929
16930 Set_Discriminant_Default_Value
16931 (Defining_Identifier (Discr), Expression (Discr));
16932 end if;
16933
16934 else
16935 Default_Not_Present := True;
16936 end if;
16937
16938 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
16939 -- Discr_Type but with the null-exclusion attribute
16940
16941 if Ada_Version >= Ada_2005 then
16942
16943 -- Ada 2005 (AI-231): Static checks
16944
16945 if Can_Never_Be_Null (Discr_Type) then
16946 Null_Exclusion_Static_Checks (Discr);
16947
16948 elsif Is_Access_Type (Discr_Type)
16949 and then Null_Exclusion_Present (Discr)
16950
16951 -- No need to check itypes because in their case this check
16952 -- was done at their point of creation
16953
16954 and then not Is_Itype (Discr_Type)
16955 then
16956 if Can_Never_Be_Null (Discr_Type) then
16957 Error_Msg_NE
16958 ("`NOT NULL` not allowed (& already excludes null)",
16959 Discr,
16960 Discr_Type);
16961 end if;
16962
16963 Set_Etype (Defining_Identifier (Discr),
16964 Create_Null_Excluding_Itype
16965 (T => Discr_Type,
16966 Related_Nod => Discr));
16967
16968 -- Check for improper null exclusion if the type is otherwise
16969 -- legal for a discriminant.
16970
16971 elsif Null_Exclusion_Present (Discr)
16972 and then Is_Discrete_Type (Discr_Type)
16973 then
16974 Error_Msg_N
16975 ("null exclusion can only apply to an access type", Discr);
16976 end if;
16977
16978 -- Ada 2005 (AI-402): access discriminants of nonlimited types
16979 -- can't have defaults. Synchronized types, or types that are
16980 -- explicitly limited are fine, but special tests apply to derived
16981 -- types in generics: in a generic body we have to assume the
16982 -- worst, and therefore defaults are not allowed if the parent is
16983 -- a generic formal private type (see ACATS B370001).
16984
16985 if Is_Access_Type (Discr_Type) then
16986 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
16987 or else not Default_Present
16988 or else Is_Limited_Record (Current_Scope)
16989 or else Is_Concurrent_Type (Current_Scope)
16990 or else Is_Concurrent_Record_Type (Current_Scope)
16991 or else Ekind (Current_Scope) = E_Limited_Private_Type
16992 then
16993 if not Is_Derived_Type (Current_Scope)
16994 or else not Is_Generic_Type (Etype (Current_Scope))
16995 or else not In_Package_Body (Scope (Etype (Current_Scope)))
16996 or else Limited_Present
16997 (Type_Definition (Parent (Current_Scope)))
16998 then
16999 null;
17000
17001 else
17002 Error_Msg_N ("access discriminants of nonlimited types",
17003 Expression (Discr));
17004 Error_Msg_N ("\cannot have defaults", Expression (Discr));
17005 end if;
17006
17007 elsif Present (Expression (Discr)) then
17008 Error_Msg_N
17009 ("(Ada 2005) access discriminants of nonlimited types",
17010 Expression (Discr));
17011 Error_Msg_N ("\cannot have defaults", Expression (Discr));
17012 end if;
17013 end if;
17014 end if;
17015
17016 Next (Discr);
17017 end loop;
17018
17019 -- An element list consisting of the default expressions of the
17020 -- discriminants is constructed in the above loop and used to set
17021 -- the Discriminant_Constraint attribute for the type. If an object
17022 -- is declared of this (record or task) type without any explicit
17023 -- discriminant constraint given, this element list will form the
17024 -- actual parameters for the corresponding initialization procedure
17025 -- for the type.
17026
17027 Set_Discriminant_Constraint (Current_Scope, Elist);
17028 Set_Stored_Constraint (Current_Scope, No_Elist);
17029
17030 -- Default expressions must be provided either for all or for none
17031 -- of the discriminants of a discriminant part. (RM 3.7.1)
17032
17033 if Default_Present and then Default_Not_Present then
17034 Error_Msg_N
17035 ("incomplete specification of defaults for discriminants", N);
17036 end if;
17037
17038 -- The use of the name of a discriminant is not allowed in default
17039 -- expressions of a discriminant part if the specification of the
17040 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
17041
17042 -- To detect this, the discriminant names are entered initially with an
17043 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
17044 -- attempt to use a void entity (for example in an expression that is
17045 -- type-checked) produces the error message: premature usage. Now after
17046 -- completing the semantic analysis of the discriminant part, we can set
17047 -- the Ekind of all the discriminants appropriately.
17048
17049 Discr := First (Discriminant_Specifications (N));
17050 Discr_Number := Uint_1;
17051 while Present (Discr) loop
17052 Id := Defining_Identifier (Discr);
17053 Set_Ekind (Id, E_Discriminant);
17054 Init_Component_Location (Id);
17055 Init_Esize (Id);
17056 Set_Discriminant_Number (Id, Discr_Number);
17057
17058 -- Make sure this is always set, even in illegal programs
17059
17060 Set_Corresponding_Discriminant (Id, Empty);
17061
17062 -- Initialize the Original_Record_Component to the entity itself.
17063 -- Inherit_Components will propagate the right value to
17064 -- discriminants in derived record types.
17065
17066 Set_Original_Record_Component (Id, Id);
17067
17068 -- Create the discriminal for the discriminant
17069
17070 Build_Discriminal (Id);
17071
17072 Next (Discr);
17073 Discr_Number := Discr_Number + 1;
17074 end loop;
17075
17076 Set_Has_Discriminants (Current_Scope);
17077 end Process_Discriminants;
17078
17079 -----------------------
17080 -- Process_Full_View --
17081 -----------------------
17082
17083 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
17084 Priv_Parent : Entity_Id;
17085 Full_Parent : Entity_Id;
17086 Full_Indic : Node_Id;
17087
17088 procedure Collect_Implemented_Interfaces
17089 (Typ : Entity_Id;
17090 Ifaces : Elist_Id);
17091 -- Ada 2005: Gather all the interfaces that Typ directly or
17092 -- inherently implements. Duplicate entries are not added to
17093 -- the list Ifaces.
17094
17095 ------------------------------------
17096 -- Collect_Implemented_Interfaces --
17097 ------------------------------------
17098
17099 procedure Collect_Implemented_Interfaces
17100 (Typ : Entity_Id;
17101 Ifaces : Elist_Id)
17102 is
17103 Iface : Entity_Id;
17104 Iface_Elmt : Elmt_Id;
17105
17106 begin
17107 -- Abstract interfaces are only associated with tagged record types
17108
17109 if not Is_Tagged_Type (Typ)
17110 or else not Is_Record_Type (Typ)
17111 then
17112 return;
17113 end if;
17114
17115 -- Recursively climb to the ancestors
17116
17117 if Etype (Typ) /= Typ
17118
17119 -- Protect the frontend against wrong cyclic declarations like:
17120
17121 -- type B is new A with private;
17122 -- type C is new A with private;
17123 -- private
17124 -- type B is new C with null record;
17125 -- type C is new B with null record;
17126
17127 and then Etype (Typ) /= Priv_T
17128 and then Etype (Typ) /= Full_T
17129 then
17130 -- Keep separate the management of private type declarations
17131
17132 if Ekind (Typ) = E_Record_Type_With_Private then
17133
17134 -- Handle the following erroneous case:
17135 -- type Private_Type is tagged private;
17136 -- private
17137 -- type Private_Type is new Type_Implementing_Iface;
17138
17139 if Present (Full_View (Typ))
17140 and then Etype (Typ) /= Full_View (Typ)
17141 then
17142 if Is_Interface (Etype (Typ)) then
17143 Append_Unique_Elmt (Etype (Typ), Ifaces);
17144 end if;
17145
17146 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17147 end if;
17148
17149 -- Non-private types
17150
17151 else
17152 if Is_Interface (Etype (Typ)) then
17153 Append_Unique_Elmt (Etype (Typ), Ifaces);
17154 end if;
17155
17156 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17157 end if;
17158 end if;
17159
17160 -- Handle entities in the list of abstract interfaces
17161
17162 if Present (Interfaces (Typ)) then
17163 Iface_Elmt := First_Elmt (Interfaces (Typ));
17164 while Present (Iface_Elmt) loop
17165 Iface := Node (Iface_Elmt);
17166
17167 pragma Assert (Is_Interface (Iface));
17168
17169 if not Contain_Interface (Iface, Ifaces) then
17170 Append_Elmt (Iface, Ifaces);
17171 Collect_Implemented_Interfaces (Iface, Ifaces);
17172 end if;
17173
17174 Next_Elmt (Iface_Elmt);
17175 end loop;
17176 end if;
17177 end Collect_Implemented_Interfaces;
17178
17179 -- Start of processing for Process_Full_View
17180
17181 begin
17182 -- First some sanity checks that must be done after semantic
17183 -- decoration of the full view and thus cannot be placed with other
17184 -- similar checks in Find_Type_Name
17185
17186 if not Is_Limited_Type (Priv_T)
17187 and then (Is_Limited_Type (Full_T)
17188 or else Is_Limited_Composite (Full_T))
17189 then
17190 Error_Msg_N
17191 ("completion of nonlimited type cannot be limited", Full_T);
17192 Explain_Limited_Type (Full_T, Full_T);
17193
17194 elsif Is_Abstract_Type (Full_T)
17195 and then not Is_Abstract_Type (Priv_T)
17196 then
17197 Error_Msg_N
17198 ("completion of nonabstract type cannot be abstract", Full_T);
17199
17200 elsif Is_Tagged_Type (Priv_T)
17201 and then Is_Limited_Type (Priv_T)
17202 and then not Is_Limited_Type (Full_T)
17203 then
17204 -- If pragma CPP_Class was applied to the private declaration
17205 -- propagate the limitedness to the full-view
17206
17207 if Is_CPP_Class (Priv_T) then
17208 Set_Is_Limited_Record (Full_T);
17209
17210 -- GNAT allow its own definition of Limited_Controlled to disobey
17211 -- this rule in order in ease the implementation. The next test is
17212 -- safe because Root_Controlled is defined in a private system child
17213
17214 elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
17215 Set_Is_Limited_Composite (Full_T);
17216 else
17217 Error_Msg_N
17218 ("completion of limited tagged type must be limited", Full_T);
17219 end if;
17220
17221 elsif Is_Generic_Type (Priv_T) then
17222 Error_Msg_N ("generic type cannot have a completion", Full_T);
17223 end if;
17224
17225 -- Check that ancestor interfaces of private and full views are
17226 -- consistent. We omit this check for synchronized types because
17227 -- they are performed on the corresponding record type when frozen.
17228
17229 if Ada_Version >= Ada_2005
17230 and then Is_Tagged_Type (Priv_T)
17231 and then Is_Tagged_Type (Full_T)
17232 and then not Is_Concurrent_Type (Full_T)
17233 then
17234 declare
17235 Iface : Entity_Id;
17236 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
17237 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
17238
17239 begin
17240 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
17241 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
17242
17243 -- Ada 2005 (AI-251): The partial view shall be a descendant of
17244 -- an interface type if and only if the full type is descendant
17245 -- of the interface type (AARM 7.3 (7.3/2).
17246
17247 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
17248
17249 if Present (Iface) then
17250 Error_Msg_NE
17251 ("interface & not implemented by full type " &
17252 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
17253 end if;
17254
17255 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
17256
17257 if Present (Iface) then
17258 Error_Msg_NE
17259 ("interface & not implemented by partial view " &
17260 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
17261 end if;
17262 end;
17263 end if;
17264
17265 if Is_Tagged_Type (Priv_T)
17266 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17267 and then Is_Derived_Type (Full_T)
17268 then
17269 Priv_Parent := Etype (Priv_T);
17270
17271 -- The full view of a private extension may have been transformed
17272 -- into an unconstrained derived type declaration and a subtype
17273 -- declaration (see build_derived_record_type for details).
17274
17275 if Nkind (N) = N_Subtype_Declaration then
17276 Full_Indic := Subtype_Indication (N);
17277 Full_Parent := Etype (Base_Type (Full_T));
17278 else
17279 Full_Indic := Subtype_Indication (Type_Definition (N));
17280 Full_Parent := Etype (Full_T);
17281 end if;
17282
17283 -- Check that the parent type of the full type is a descendant of
17284 -- the ancestor subtype given in the private extension. If either
17285 -- entity has an Etype equal to Any_Type then we had some previous
17286 -- error situation [7.3(8)].
17287
17288 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
17289 return;
17290
17291 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
17292 -- any order. Therefore we don't have to check that its parent must
17293 -- be a descendant of the parent of the private type declaration.
17294
17295 elsif Is_Interface (Priv_Parent)
17296 and then Is_Interface (Full_Parent)
17297 then
17298 null;
17299
17300 -- Ada 2005 (AI-251): If the parent of the private type declaration
17301 -- is an interface there is no need to check that it is an ancestor
17302 -- of the associated full type declaration. The required tests for
17303 -- this case are performed by Build_Derived_Record_Type.
17304
17305 elsif not Is_Interface (Base_Type (Priv_Parent))
17306 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
17307 then
17308 Error_Msg_N
17309 ("parent of full type must descend from parent"
17310 & " of private extension", Full_Indic);
17311
17312 -- First check a formal restriction, and then proceed with checking
17313 -- Ada rules. Since the formal restriction is not a serious error, we
17314 -- don't prevent further error detection for this check, hence the
17315 -- ELSE.
17316
17317 else
17318
17319 -- In formal mode, when completing a private extension the type
17320 -- named in the private part must be exactly the same as that
17321 -- named in the visible part.
17322
17323 if Priv_Parent /= Full_Parent then
17324 Error_Msg_Name_1 := Chars (Priv_Parent);
17325 Check_Formal_Restriction ("% expected", Full_Indic);
17326 end if;
17327
17328 -- Check the rules of 7.3(10): if the private extension inherits
17329 -- known discriminants, then the full type must also inherit those
17330 -- discriminants from the same (ancestor) type, and the parent
17331 -- subtype of the full type must be constrained if and only if
17332 -- the ancestor subtype of the private extension is constrained.
17333
17334 if No (Discriminant_Specifications (Parent (Priv_T)))
17335 and then not Has_Unknown_Discriminants (Priv_T)
17336 and then Has_Discriminants (Base_Type (Priv_Parent))
17337 then
17338 declare
17339 Priv_Indic : constant Node_Id :=
17340 Subtype_Indication (Parent (Priv_T));
17341
17342 Priv_Constr : constant Boolean :=
17343 Is_Constrained (Priv_Parent)
17344 or else
17345 Nkind (Priv_Indic) = N_Subtype_Indication
17346 or else
17347 Is_Constrained (Entity (Priv_Indic));
17348
17349 Full_Constr : constant Boolean :=
17350 Is_Constrained (Full_Parent)
17351 or else
17352 Nkind (Full_Indic) = N_Subtype_Indication
17353 or else
17354 Is_Constrained (Entity (Full_Indic));
17355
17356 Priv_Discr : Entity_Id;
17357 Full_Discr : Entity_Id;
17358
17359 begin
17360 Priv_Discr := First_Discriminant (Priv_Parent);
17361 Full_Discr := First_Discriminant (Full_Parent);
17362 while Present (Priv_Discr) and then Present (Full_Discr) loop
17363 if Original_Record_Component (Priv_Discr) =
17364 Original_Record_Component (Full_Discr)
17365 or else
17366 Corresponding_Discriminant (Priv_Discr) =
17367 Corresponding_Discriminant (Full_Discr)
17368 then
17369 null;
17370 else
17371 exit;
17372 end if;
17373
17374 Next_Discriminant (Priv_Discr);
17375 Next_Discriminant (Full_Discr);
17376 end loop;
17377
17378 if Present (Priv_Discr) or else Present (Full_Discr) then
17379 Error_Msg_N
17380 ("full view must inherit discriminants of the parent"
17381 & " type used in the private extension", Full_Indic);
17382
17383 elsif Priv_Constr and then not Full_Constr then
17384 Error_Msg_N
17385 ("parent subtype of full type must be constrained",
17386 Full_Indic);
17387
17388 elsif Full_Constr and then not Priv_Constr then
17389 Error_Msg_N
17390 ("parent subtype of full type must be unconstrained",
17391 Full_Indic);
17392 end if;
17393 end;
17394
17395 -- Check the rules of 7.3(12): if a partial view has neither
17396 -- known or unknown discriminants, then the full type
17397 -- declaration shall define a definite subtype.
17398
17399 elsif not Has_Unknown_Discriminants (Priv_T)
17400 and then not Has_Discriminants (Priv_T)
17401 and then not Is_Constrained (Full_T)
17402 then
17403 Error_Msg_N
17404 ("full view must define a constrained type if partial view"
17405 & " has no discriminants", Full_T);
17406 end if;
17407
17408 -- ??????? Do we implement the following properly ?????
17409 -- If the ancestor subtype of a private extension has constrained
17410 -- discriminants, then the parent subtype of the full view shall
17411 -- impose a statically matching constraint on those discriminants
17412 -- [7.3(13)].
17413 end if;
17414
17415 else
17416 -- For untagged types, verify that a type without discriminants
17417 -- is not completed with an unconstrained type.
17418
17419 if not Is_Indefinite_Subtype (Priv_T)
17420 and then Is_Indefinite_Subtype (Full_T)
17421 then
17422 Error_Msg_N ("full view of type must be definite subtype", Full_T);
17423 end if;
17424 end if;
17425
17426 -- AI-419: verify that the use of "limited" is consistent
17427
17428 declare
17429 Orig_Decl : constant Node_Id := Original_Node (N);
17430
17431 begin
17432 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17433 and then not Limited_Present (Parent (Priv_T))
17434 and then not Synchronized_Present (Parent (Priv_T))
17435 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
17436 and then Nkind
17437 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
17438 and then Limited_Present (Type_Definition (Orig_Decl))
17439 then
17440 Error_Msg_N
17441 ("full view of non-limited extension cannot be limited", N);
17442 end if;
17443 end;
17444
17445 -- Ada 2005 (AI-443): A synchronized private extension must be
17446 -- completed by a task or protected type.
17447
17448 if Ada_Version >= Ada_2005
17449 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17450 and then Synchronized_Present (Parent (Priv_T))
17451 and then not Is_Concurrent_Type (Full_T)
17452 then
17453 Error_Msg_N ("full view of synchronized extension must " &
17454 "be synchronized type", N);
17455 end if;
17456
17457 -- Ada 2005 AI-363: if the full view has discriminants with
17458 -- defaults, it is illegal to declare constrained access subtypes
17459 -- whose designated type is the current type. This allows objects
17460 -- of the type that are declared in the heap to be unconstrained.
17461
17462 if not Has_Unknown_Discriminants (Priv_T)
17463 and then not Has_Discriminants (Priv_T)
17464 and then Has_Discriminants (Full_T)
17465 and then
17466 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
17467 then
17468 Set_Has_Constrained_Partial_View (Full_T);
17469 Set_Has_Constrained_Partial_View (Priv_T);
17470 end if;
17471
17472 -- Create a full declaration for all its subtypes recorded in
17473 -- Private_Dependents and swap them similarly to the base type. These
17474 -- are subtypes that have been define before the full declaration of
17475 -- the private type. We also swap the entry in Private_Dependents list
17476 -- so we can properly restore the private view on exit from the scope.
17477
17478 declare
17479 Priv_Elmt : Elmt_Id;
17480 Priv : Entity_Id;
17481 Full : Entity_Id;
17482
17483 begin
17484 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
17485 while Present (Priv_Elmt) loop
17486 Priv := Node (Priv_Elmt);
17487
17488 if Ekind_In (Priv, E_Private_Subtype,
17489 E_Limited_Private_Subtype,
17490 E_Record_Subtype_With_Private)
17491 then
17492 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
17493 Set_Is_Itype (Full);
17494 Set_Parent (Full, Parent (Priv));
17495 Set_Associated_Node_For_Itype (Full, N);
17496
17497 -- Now we need to complete the private subtype, but since the
17498 -- base type has already been swapped, we must also swap the
17499 -- subtypes (and thus, reverse the arguments in the call to
17500 -- Complete_Private_Subtype).
17501
17502 Copy_And_Swap (Priv, Full);
17503 Complete_Private_Subtype (Full, Priv, Full_T, N);
17504 Replace_Elmt (Priv_Elmt, Full);
17505 end if;
17506
17507 Next_Elmt (Priv_Elmt);
17508 end loop;
17509 end;
17510
17511 -- If the private view was tagged, copy the new primitive operations
17512 -- from the private view to the full view.
17513
17514 if Is_Tagged_Type (Full_T) then
17515 declare
17516 Disp_Typ : Entity_Id;
17517 Full_List : Elist_Id;
17518 Prim : Entity_Id;
17519 Prim_Elmt : Elmt_Id;
17520 Priv_List : Elist_Id;
17521
17522 function Contains
17523 (E : Entity_Id;
17524 L : Elist_Id) return Boolean;
17525 -- Determine whether list L contains element E
17526
17527 --------------
17528 -- Contains --
17529 --------------
17530
17531 function Contains
17532 (E : Entity_Id;
17533 L : Elist_Id) return Boolean
17534 is
17535 List_Elmt : Elmt_Id;
17536
17537 begin
17538 List_Elmt := First_Elmt (L);
17539 while Present (List_Elmt) loop
17540 if Node (List_Elmt) = E then
17541 return True;
17542 end if;
17543
17544 Next_Elmt (List_Elmt);
17545 end loop;
17546
17547 return False;
17548 end Contains;
17549
17550 -- Start of processing
17551
17552 begin
17553 if Is_Tagged_Type (Priv_T) then
17554 Priv_List := Primitive_Operations (Priv_T);
17555 Prim_Elmt := First_Elmt (Priv_List);
17556
17557 -- In the case of a concurrent type completing a private tagged
17558 -- type, primitives may have been declared in between the two
17559 -- views. These subprograms need to be wrapped the same way
17560 -- entries and protected procedures are handled because they
17561 -- cannot be directly shared by the two views.
17562
17563 if Is_Concurrent_Type (Full_T) then
17564 declare
17565 Conc_Typ : constant Entity_Id :=
17566 Corresponding_Record_Type (Full_T);
17567 Curr_Nod : Node_Id := Parent (Conc_Typ);
17568 Wrap_Spec : Node_Id;
17569
17570 begin
17571 while Present (Prim_Elmt) loop
17572 Prim := Node (Prim_Elmt);
17573
17574 if Comes_From_Source (Prim)
17575 and then not Is_Abstract_Subprogram (Prim)
17576 then
17577 Wrap_Spec :=
17578 Make_Subprogram_Declaration (Sloc (Prim),
17579 Specification =>
17580 Build_Wrapper_Spec
17581 (Subp_Id => Prim,
17582 Obj_Typ => Conc_Typ,
17583 Formals =>
17584 Parameter_Specifications (
17585 Parent (Prim))));
17586
17587 Insert_After (Curr_Nod, Wrap_Spec);
17588 Curr_Nod := Wrap_Spec;
17589
17590 Analyze (Wrap_Spec);
17591 end if;
17592
17593 Next_Elmt (Prim_Elmt);
17594 end loop;
17595
17596 return;
17597 end;
17598
17599 -- For non-concurrent types, transfer explicit primitives, but
17600 -- omit those inherited from the parent of the private view
17601 -- since they will be re-inherited later on.
17602
17603 else
17604 Full_List := Primitive_Operations (Full_T);
17605
17606 while Present (Prim_Elmt) loop
17607 Prim := Node (Prim_Elmt);
17608
17609 if Comes_From_Source (Prim)
17610 and then not Contains (Prim, Full_List)
17611 then
17612 Append_Elmt (Prim, Full_List);
17613 end if;
17614
17615 Next_Elmt (Prim_Elmt);
17616 end loop;
17617 end if;
17618
17619 -- Untagged private view
17620
17621 else
17622 Full_List := Primitive_Operations (Full_T);
17623
17624 -- In this case the partial view is untagged, so here we locate
17625 -- all of the earlier primitives that need to be treated as
17626 -- dispatching (those that appear between the two views). Note
17627 -- that these additional operations must all be new operations
17628 -- (any earlier operations that override inherited operations
17629 -- of the full view will already have been inserted in the
17630 -- primitives list, marked by Check_Operation_From_Private_View
17631 -- as dispatching. Note that implicit "/=" operators are
17632 -- excluded from being added to the primitives list since they
17633 -- shouldn't be treated as dispatching (tagged "/=" is handled
17634 -- specially).
17635
17636 Prim := Next_Entity (Full_T);
17637 while Present (Prim) and then Prim /= Priv_T loop
17638 if Ekind_In (Prim, E_Procedure, E_Function) then
17639 Disp_Typ := Find_Dispatching_Type (Prim);
17640
17641 if Disp_Typ = Full_T
17642 and then (Chars (Prim) /= Name_Op_Ne
17643 or else Comes_From_Source (Prim))
17644 then
17645 Check_Controlling_Formals (Full_T, Prim);
17646
17647 if not Is_Dispatching_Operation (Prim) then
17648 Append_Elmt (Prim, Full_List);
17649 Set_Is_Dispatching_Operation (Prim, True);
17650 Set_DT_Position (Prim, No_Uint);
17651 end if;
17652
17653 elsif Is_Dispatching_Operation (Prim)
17654 and then Disp_Typ /= Full_T
17655 then
17656
17657 -- Verify that it is not otherwise controlled by a
17658 -- formal or a return value of type T.
17659
17660 Check_Controlling_Formals (Disp_Typ, Prim);
17661 end if;
17662 end if;
17663
17664 Next_Entity (Prim);
17665 end loop;
17666 end if;
17667
17668 -- For the tagged case, the two views can share the same primitive
17669 -- operations list and the same class-wide type. Update attributes
17670 -- of the class-wide type which depend on the full declaration.
17671
17672 if Is_Tagged_Type (Priv_T) then
17673 Set_Direct_Primitive_Operations (Priv_T, Full_List);
17674 Set_Class_Wide_Type
17675 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
17676
17677 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
17678 end if;
17679 end;
17680 end if;
17681
17682 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
17683
17684 if Known_To_Have_Preelab_Init (Priv_T) then
17685
17686 -- Case where there is a pragma Preelaborable_Initialization. We
17687 -- always allow this in predefined units, which is a bit of a kludge,
17688 -- but it means we don't have to struggle to meet the requirements in
17689 -- the RM for having Preelaborable Initialization. Otherwise we
17690 -- require that the type meets the RM rules. But we can't check that
17691 -- yet, because of the rule about overriding Initialize, so we simply
17692 -- set a flag that will be checked at freeze time.
17693
17694 if not In_Predefined_Unit (Full_T) then
17695 Set_Must_Have_Preelab_Init (Full_T);
17696 end if;
17697 end if;
17698
17699 -- If pragma CPP_Class was applied to the private type declaration,
17700 -- propagate it now to the full type declaration.
17701
17702 if Is_CPP_Class (Priv_T) then
17703 Set_Is_CPP_Class (Full_T);
17704 Set_Convention (Full_T, Convention_CPP);
17705 end if;
17706
17707 -- If the private view has user specified stream attributes, then so has
17708 -- the full view.
17709
17710 -- Why the test, how could these flags be already set in Full_T ???
17711
17712 if Has_Specified_Stream_Read (Priv_T) then
17713 Set_Has_Specified_Stream_Read (Full_T);
17714 end if;
17715
17716 if Has_Specified_Stream_Write (Priv_T) then
17717 Set_Has_Specified_Stream_Write (Full_T);
17718 end if;
17719
17720 if Has_Specified_Stream_Input (Priv_T) then
17721 Set_Has_Specified_Stream_Input (Full_T);
17722 end if;
17723
17724 if Has_Specified_Stream_Output (Priv_T) then
17725 Set_Has_Specified_Stream_Output (Full_T);
17726 end if;
17727
17728 -- Propagate invariants to full type
17729
17730 if Has_Invariants (Priv_T) then
17731 Set_Has_Invariants (Full_T);
17732 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
17733 end if;
17734
17735 if Has_Inheritable_Invariants (Priv_T) then
17736 Set_Has_Inheritable_Invariants (Full_T);
17737 end if;
17738
17739 -- Propagate predicates to full type
17740
17741 if Has_Predicates (Priv_T) then
17742 Set_Predicate_Function (Priv_T, Predicate_Function (Full_T));
17743 Set_Has_Predicates (Priv_T);
17744 end if;
17745 end Process_Full_View;
17746
17747 -----------------------------------
17748 -- Process_Incomplete_Dependents --
17749 -----------------------------------
17750
17751 procedure Process_Incomplete_Dependents
17752 (N : Node_Id;
17753 Full_T : Entity_Id;
17754 Inc_T : Entity_Id)
17755 is
17756 Inc_Elmt : Elmt_Id;
17757 Priv_Dep : Entity_Id;
17758 New_Subt : Entity_Id;
17759
17760 Disc_Constraint : Elist_Id;
17761
17762 begin
17763 if No (Private_Dependents (Inc_T)) then
17764 return;
17765 end if;
17766
17767 -- Itypes that may be generated by the completion of an incomplete
17768 -- subtype are not used by the back-end and not attached to the tree.
17769 -- They are created only for constraint-checking purposes.
17770
17771 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
17772 while Present (Inc_Elmt) loop
17773 Priv_Dep := Node (Inc_Elmt);
17774
17775 if Ekind (Priv_Dep) = E_Subprogram_Type then
17776
17777 -- An Access_To_Subprogram type may have a return type or a
17778 -- parameter type that is incomplete. Replace with the full view.
17779
17780 if Etype (Priv_Dep) = Inc_T then
17781 Set_Etype (Priv_Dep, Full_T);
17782 end if;
17783
17784 declare
17785 Formal : Entity_Id;
17786
17787 begin
17788 Formal := First_Formal (Priv_Dep);
17789 while Present (Formal) loop
17790 if Etype (Formal) = Inc_T then
17791 Set_Etype (Formal, Full_T);
17792 end if;
17793
17794 Next_Formal (Formal);
17795 end loop;
17796 end;
17797
17798 elsif Is_Overloadable (Priv_Dep) then
17799
17800 -- A protected operation is never dispatching: only its
17801 -- wrapper operation (which has convention Ada) is.
17802
17803 if Is_Tagged_Type (Full_T)
17804 and then Convention (Priv_Dep) /= Convention_Protected
17805 then
17806
17807 -- Subprogram has an access parameter whose designated type
17808 -- was incomplete. Reexamine declaration now, because it may
17809 -- be a primitive operation of the full type.
17810
17811 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
17812 Set_Is_Dispatching_Operation (Priv_Dep);
17813 Check_Controlling_Formals (Full_T, Priv_Dep);
17814 end if;
17815
17816 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
17817
17818 -- Can happen during processing of a body before the completion
17819 -- of a TA type. Ignore, because spec is also on dependent list.
17820
17821 return;
17822
17823 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
17824 -- corresponding subtype of the full view.
17825
17826 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
17827 Set_Subtype_Indication
17828 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
17829 Set_Etype (Priv_Dep, Full_T);
17830 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
17831 Set_Analyzed (Parent (Priv_Dep), False);
17832
17833 -- Reanalyze the declaration, suppressing the call to
17834 -- Enter_Name to avoid duplicate names.
17835
17836 Analyze_Subtype_Declaration
17837 (N => Parent (Priv_Dep),
17838 Skip => True);
17839
17840 -- Dependent is a subtype
17841
17842 else
17843 -- We build a new subtype indication using the full view of the
17844 -- incomplete parent. The discriminant constraints have been
17845 -- elaborated already at the point of the subtype declaration.
17846
17847 New_Subt := Create_Itype (E_Void, N);
17848
17849 if Has_Discriminants (Full_T) then
17850 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
17851 else
17852 Disc_Constraint := No_Elist;
17853 end if;
17854
17855 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
17856 Set_Full_View (Priv_Dep, New_Subt);
17857 end if;
17858
17859 Next_Elmt (Inc_Elmt);
17860 end loop;
17861 end Process_Incomplete_Dependents;
17862
17863 --------------------------------
17864 -- Process_Range_Expr_In_Decl --
17865 --------------------------------
17866
17867 procedure Process_Range_Expr_In_Decl
17868 (R : Node_Id;
17869 T : Entity_Id;
17870 Check_List : List_Id := Empty_List;
17871 R_Check_Off : Boolean := False)
17872 is
17873 Lo, Hi : Node_Id;
17874 R_Checks : Check_Result;
17875 Insert_Node : Node_Id;
17876 Def_Id : Entity_Id;
17877
17878 begin
17879 Analyze_And_Resolve (R, Base_Type (T));
17880
17881 if Nkind (R) = N_Range then
17882 if not Is_Static_Range (R) then
17883 Check_Formal_Restriction ("range should be static", R);
17884 end if;
17885
17886 Lo := Low_Bound (R);
17887 Hi := High_Bound (R);
17888
17889 -- We need to ensure validity of the bounds here, because if we
17890 -- go ahead and do the expansion, then the expanded code will get
17891 -- analyzed with range checks suppressed and we miss the check.
17892
17893 Validity_Check_Range (R);
17894
17895 -- If there were errors in the declaration, try and patch up some
17896 -- common mistakes in the bounds. The cases handled are literals
17897 -- which are Integer where the expected type is Real and vice versa.
17898 -- These corrections allow the compilation process to proceed further
17899 -- along since some basic assumptions of the format of the bounds
17900 -- are guaranteed.
17901
17902 if Etype (R) = Any_Type then
17903
17904 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
17905 Rewrite (Lo,
17906 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
17907
17908 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
17909 Rewrite (Hi,
17910 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
17911
17912 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
17913 Rewrite (Lo,
17914 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
17915
17916 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
17917 Rewrite (Hi,
17918 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
17919 end if;
17920
17921 Set_Etype (Lo, T);
17922 Set_Etype (Hi, T);
17923 end if;
17924
17925 -- If the bounds of the range have been mistakenly given as string
17926 -- literals (perhaps in place of character literals), then an error
17927 -- has already been reported, but we rewrite the string literal as a
17928 -- bound of the range's type to avoid blowups in later processing
17929 -- that looks at static values.
17930
17931 if Nkind (Lo) = N_String_Literal then
17932 Rewrite (Lo,
17933 Make_Attribute_Reference (Sloc (Lo),
17934 Attribute_Name => Name_First,
17935 Prefix => New_Reference_To (T, Sloc (Lo))));
17936 Analyze_And_Resolve (Lo);
17937 end if;
17938
17939 if Nkind (Hi) = N_String_Literal then
17940 Rewrite (Hi,
17941 Make_Attribute_Reference (Sloc (Hi),
17942 Attribute_Name => Name_First,
17943 Prefix => New_Reference_To (T, Sloc (Hi))));
17944 Analyze_And_Resolve (Hi);
17945 end if;
17946
17947 -- If bounds aren't scalar at this point then exit, avoiding
17948 -- problems with further processing of the range in this procedure.
17949
17950 if not Is_Scalar_Type (Etype (Lo)) then
17951 return;
17952 end if;
17953
17954 -- Resolve (actually Sem_Eval) has checked that the bounds are in
17955 -- then range of the base type. Here we check whether the bounds
17956 -- are in the range of the subtype itself. Note that if the bounds
17957 -- represent the null range the Constraint_Error exception should
17958 -- not be raised.
17959
17960 -- ??? The following code should be cleaned up as follows
17961
17962 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
17963 -- is done in the call to Range_Check (R, T); below
17964
17965 -- 2. The use of R_Check_Off should be investigated and possibly
17966 -- removed, this would clean up things a bit.
17967
17968 if Is_Null_Range (Lo, Hi) then
17969 null;
17970
17971 else
17972 -- Capture values of bounds and generate temporaries for them
17973 -- if needed, before applying checks, since checks may cause
17974 -- duplication of the expression without forcing evaluation.
17975
17976 if Expander_Active then
17977 Force_Evaluation (Lo);
17978 Force_Evaluation (Hi);
17979 end if;
17980
17981 -- We use a flag here instead of suppressing checks on the
17982 -- type because the type we check against isn't necessarily
17983 -- the place where we put the check.
17984
17985 if not R_Check_Off then
17986 R_Checks := Get_Range_Checks (R, T);
17987
17988 -- Look up tree to find an appropriate insertion point. We
17989 -- can't just use insert_actions because later processing
17990 -- depends on the insertion node. Prior to Ada2012 the
17991 -- insertion point could only be a declaration or a loop, but
17992 -- quantified expressions can appear within any context in an
17993 -- expression, and the insertion point can be any statement,
17994 -- pragma, or declaration.
17995
17996 Insert_Node := Parent (R);
17997 while Present (Insert_Node) loop
17998 exit when
17999 Nkind (Insert_Node) in N_Declaration
18000 and then
18001 not Nkind_In
18002 (Insert_Node, N_Component_Declaration,
18003 N_Loop_Parameter_Specification,
18004 N_Function_Specification,
18005 N_Procedure_Specification);
18006
18007 exit when Nkind (Insert_Node) in N_Later_Decl_Item
18008 or else Nkind (Insert_Node) in
18009 N_Statement_Other_Than_Procedure_Call
18010 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
18011 N_Pragma);
18012
18013 Insert_Node := Parent (Insert_Node);
18014 end loop;
18015
18016 -- Why would Type_Decl not be present??? Without this test,
18017 -- short regression tests fail.
18018
18019 if Present (Insert_Node) then
18020
18021 -- Case of loop statement. Verify that the range is part
18022 -- of the subtype indication of the iteration scheme.
18023
18024 if Nkind (Insert_Node) = N_Loop_Statement then
18025 declare
18026 Indic : Node_Id;
18027
18028 begin
18029 Indic := Parent (R);
18030 while Present (Indic)
18031 and then Nkind (Indic) /= N_Subtype_Indication
18032 loop
18033 Indic := Parent (Indic);
18034 end loop;
18035
18036 if Present (Indic) then
18037 Def_Id := Etype (Subtype_Mark (Indic));
18038
18039 Insert_Range_Checks
18040 (R_Checks,
18041 Insert_Node,
18042 Def_Id,
18043 Sloc (Insert_Node),
18044 R,
18045 Do_Before => True);
18046 end if;
18047 end;
18048
18049 -- Insertion before a declaration. If the declaration
18050 -- includes discriminants, the list of applicable checks
18051 -- is given by the caller.
18052
18053 elsif Nkind (Insert_Node) in N_Declaration then
18054 Def_Id := Defining_Identifier (Insert_Node);
18055
18056 if (Ekind (Def_Id) = E_Record_Type
18057 and then Depends_On_Discriminant (R))
18058 or else
18059 (Ekind (Def_Id) = E_Protected_Type
18060 and then Has_Discriminants (Def_Id))
18061 then
18062 Append_Range_Checks
18063 (R_Checks,
18064 Check_List, Def_Id, Sloc (Insert_Node), R);
18065
18066 else
18067 Insert_Range_Checks
18068 (R_Checks,
18069 Insert_Node, Def_Id, Sloc (Insert_Node), R);
18070
18071 end if;
18072
18073 -- Insertion before a statement. Range appears in the
18074 -- context of a quantified expression. Insertion will
18075 -- take place when expression is expanded.
18076
18077 else
18078 null;
18079 end if;
18080 end if;
18081 end if;
18082 end if;
18083
18084 -- Case of other than an explicit N_Range node
18085
18086 elsif Expander_Active then
18087 Get_Index_Bounds (R, Lo, Hi);
18088 Force_Evaluation (Lo);
18089 Force_Evaluation (Hi);
18090 end if;
18091 end Process_Range_Expr_In_Decl;
18092
18093 --------------------------------------
18094 -- Process_Real_Range_Specification --
18095 --------------------------------------
18096
18097 procedure Process_Real_Range_Specification (Def : Node_Id) is
18098 Spec : constant Node_Id := Real_Range_Specification (Def);
18099 Lo : Node_Id;
18100 Hi : Node_Id;
18101 Err : Boolean := False;
18102
18103 procedure Analyze_Bound (N : Node_Id);
18104 -- Analyze and check one bound
18105
18106 -------------------
18107 -- Analyze_Bound --
18108 -------------------
18109
18110 procedure Analyze_Bound (N : Node_Id) is
18111 begin
18112 Analyze_And_Resolve (N, Any_Real);
18113
18114 if not Is_OK_Static_Expression (N) then
18115 Flag_Non_Static_Expr
18116 ("bound in real type definition is not static!", N);
18117 Err := True;
18118 end if;
18119 end Analyze_Bound;
18120
18121 -- Start of processing for Process_Real_Range_Specification
18122
18123 begin
18124 if Present (Spec) then
18125 Lo := Low_Bound (Spec);
18126 Hi := High_Bound (Spec);
18127 Analyze_Bound (Lo);
18128 Analyze_Bound (Hi);
18129
18130 -- If error, clear away junk range specification
18131
18132 if Err then
18133 Set_Real_Range_Specification (Def, Empty);
18134 end if;
18135 end if;
18136 end Process_Real_Range_Specification;
18137
18138 ---------------------
18139 -- Process_Subtype --
18140 ---------------------
18141
18142 function Process_Subtype
18143 (S : Node_Id;
18144 Related_Nod : Node_Id;
18145 Related_Id : Entity_Id := Empty;
18146 Suffix : Character := ' ') return Entity_Id
18147 is
18148 P : Node_Id;
18149 Def_Id : Entity_Id;
18150 Error_Node : Node_Id;
18151 Full_View_Id : Entity_Id;
18152 Subtype_Mark_Id : Entity_Id;
18153
18154 May_Have_Null_Exclusion : Boolean;
18155
18156 procedure Check_Incomplete (T : Entity_Id);
18157 -- Called to verify that an incomplete type is not used prematurely
18158
18159 ----------------------
18160 -- Check_Incomplete --
18161 ----------------------
18162
18163 procedure Check_Incomplete (T : Entity_Id) is
18164 begin
18165 -- Ada 2005 (AI-412): Incomplete subtypes are legal
18166
18167 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
18168 and then
18169 not (Ada_Version >= Ada_2005
18170 and then
18171 (Nkind (Parent (T)) = N_Subtype_Declaration
18172 or else
18173 (Nkind (Parent (T)) = N_Subtype_Indication
18174 and then Nkind (Parent (Parent (T))) =
18175 N_Subtype_Declaration)))
18176 then
18177 Error_Msg_N ("invalid use of type before its full declaration", T);
18178 end if;
18179 end Check_Incomplete;
18180
18181 -- Start of processing for Process_Subtype
18182
18183 begin
18184 -- Case of no constraints present
18185
18186 if Nkind (S) /= N_Subtype_Indication then
18187 Find_Type (S);
18188 Check_Incomplete (S);
18189 P := Parent (S);
18190
18191 -- Ada 2005 (AI-231): Static check
18192
18193 if Ada_Version >= Ada_2005
18194 and then Present (P)
18195 and then Null_Exclusion_Present (P)
18196 and then Nkind (P) /= N_Access_To_Object_Definition
18197 and then not Is_Access_Type (Entity (S))
18198 then
18199 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
18200 end if;
18201
18202 -- The following is ugly, can't we have a range or even a flag???
18203
18204 May_Have_Null_Exclusion :=
18205 Nkind_In (P, N_Access_Definition,
18206 N_Access_Function_Definition,
18207 N_Access_Procedure_Definition,
18208 N_Access_To_Object_Definition,
18209 N_Allocator,
18210 N_Component_Definition)
18211 or else
18212 Nkind_In (P, N_Derived_Type_Definition,
18213 N_Discriminant_Specification,
18214 N_Formal_Object_Declaration,
18215 N_Object_Declaration,
18216 N_Object_Renaming_Declaration,
18217 N_Parameter_Specification,
18218 N_Subtype_Declaration);
18219
18220 -- Create an Itype that is a duplicate of Entity (S) but with the
18221 -- null-exclusion attribute.
18222
18223 if May_Have_Null_Exclusion
18224 and then Is_Access_Type (Entity (S))
18225 and then Null_Exclusion_Present (P)
18226
18227 -- No need to check the case of an access to object definition.
18228 -- It is correct to define double not-null pointers.
18229
18230 -- Example:
18231 -- type Not_Null_Int_Ptr is not null access Integer;
18232 -- type Acc is not null access Not_Null_Int_Ptr;
18233
18234 and then Nkind (P) /= N_Access_To_Object_Definition
18235 then
18236 if Can_Never_Be_Null (Entity (S)) then
18237 case Nkind (Related_Nod) is
18238 when N_Full_Type_Declaration =>
18239 if Nkind (Type_Definition (Related_Nod))
18240 in N_Array_Type_Definition
18241 then
18242 Error_Node :=
18243 Subtype_Indication
18244 (Component_Definition
18245 (Type_Definition (Related_Nod)));
18246 else
18247 Error_Node :=
18248 Subtype_Indication (Type_Definition (Related_Nod));
18249 end if;
18250
18251 when N_Subtype_Declaration =>
18252 Error_Node := Subtype_Indication (Related_Nod);
18253
18254 when N_Object_Declaration =>
18255 Error_Node := Object_Definition (Related_Nod);
18256
18257 when N_Component_Declaration =>
18258 Error_Node :=
18259 Subtype_Indication (Component_Definition (Related_Nod));
18260
18261 when N_Allocator =>
18262 Error_Node := Expression (Related_Nod);
18263
18264 when others =>
18265 pragma Assert (False);
18266 Error_Node := Related_Nod;
18267 end case;
18268
18269 Error_Msg_NE
18270 ("`NOT NULL` not allowed (& already excludes null)",
18271 Error_Node,
18272 Entity (S));
18273 end if;
18274
18275 Set_Etype (S,
18276 Create_Null_Excluding_Itype
18277 (T => Entity (S),
18278 Related_Nod => P));
18279 Set_Entity (S, Etype (S));
18280 end if;
18281
18282 return Entity (S);
18283
18284 -- Case of constraint present, so that we have an N_Subtype_Indication
18285 -- node (this node is created only if constraints are present).
18286
18287 else
18288 Find_Type (Subtype_Mark (S));
18289
18290 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
18291 and then not
18292 (Nkind (Parent (S)) = N_Subtype_Declaration
18293 and then Is_Itype (Defining_Identifier (Parent (S))))
18294 then
18295 Check_Incomplete (Subtype_Mark (S));
18296 end if;
18297
18298 P := Parent (S);
18299 Subtype_Mark_Id := Entity (Subtype_Mark (S));
18300
18301 -- Explicit subtype declaration case
18302
18303 if Nkind (P) = N_Subtype_Declaration then
18304 Def_Id := Defining_Identifier (P);
18305
18306 -- Explicit derived type definition case
18307
18308 elsif Nkind (P) = N_Derived_Type_Definition then
18309 Def_Id := Defining_Identifier (Parent (P));
18310
18311 -- Implicit case, the Def_Id must be created as an implicit type.
18312 -- The one exception arises in the case of concurrent types, array
18313 -- and access types, where other subsidiary implicit types may be
18314 -- created and must appear before the main implicit type. In these
18315 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
18316 -- has not yet been called to create Def_Id.
18317
18318 else
18319 if Is_Array_Type (Subtype_Mark_Id)
18320 or else Is_Concurrent_Type (Subtype_Mark_Id)
18321 or else Is_Access_Type (Subtype_Mark_Id)
18322 then
18323 Def_Id := Empty;
18324
18325 -- For the other cases, we create a new unattached Itype,
18326 -- and set the indication to ensure it gets attached later.
18327
18328 else
18329 Def_Id :=
18330 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18331 end if;
18332 end if;
18333
18334 -- If the kind of constraint is invalid for this kind of type,
18335 -- then give an error, and then pretend no constraint was given.
18336
18337 if not Is_Valid_Constraint_Kind
18338 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
18339 then
18340 Error_Msg_N
18341 ("incorrect constraint for this kind of type", Constraint (S));
18342
18343 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18344
18345 -- Set Ekind of orphan itype, to prevent cascaded errors
18346
18347 if Present (Def_Id) then
18348 Set_Ekind (Def_Id, Ekind (Any_Type));
18349 end if;
18350
18351 -- Make recursive call, having got rid of the bogus constraint
18352
18353 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
18354 end if;
18355
18356 -- Remaining processing depends on type
18357
18358 case Ekind (Subtype_Mark_Id) is
18359 when Access_Kind =>
18360 Constrain_Access (Def_Id, S, Related_Nod);
18361
18362 if Expander_Active
18363 and then Is_Itype (Designated_Type (Def_Id))
18364 and then Nkind (Related_Nod) = N_Subtype_Declaration
18365 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
18366 then
18367 Build_Itype_Reference
18368 (Designated_Type (Def_Id), Related_Nod);
18369 end if;
18370
18371 when Array_Kind =>
18372 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
18373
18374 when Decimal_Fixed_Point_Kind =>
18375 Constrain_Decimal (Def_Id, S);
18376
18377 when Enumeration_Kind =>
18378 Constrain_Enumeration (Def_Id, S);
18379
18380 when Ordinary_Fixed_Point_Kind =>
18381 Constrain_Ordinary_Fixed (Def_Id, S);
18382
18383 when Float_Kind =>
18384 Constrain_Float (Def_Id, S);
18385
18386 when Integer_Kind =>
18387 Constrain_Integer (Def_Id, S);
18388
18389 when E_Record_Type |
18390 E_Record_Subtype |
18391 Class_Wide_Kind |
18392 E_Incomplete_Type =>
18393 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
18394
18395 if Ekind (Def_Id) = E_Incomplete_Type then
18396 Set_Private_Dependents (Def_Id, New_Elmt_List);
18397 end if;
18398
18399 when Private_Kind =>
18400 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
18401 Set_Private_Dependents (Def_Id, New_Elmt_List);
18402
18403 -- In case of an invalid constraint prevent further processing
18404 -- since the type constructed is missing expected fields.
18405
18406 if Etype (Def_Id) = Any_Type then
18407 return Def_Id;
18408 end if;
18409
18410 -- If the full view is that of a task with discriminants,
18411 -- we must constrain both the concurrent type and its
18412 -- corresponding record type. Otherwise we will just propagate
18413 -- the constraint to the full view, if available.
18414
18415 if Present (Full_View (Subtype_Mark_Id))
18416 and then Has_Discriminants (Subtype_Mark_Id)
18417 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
18418 then
18419 Full_View_Id :=
18420 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18421
18422 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
18423 Constrain_Concurrent (Full_View_Id, S,
18424 Related_Nod, Related_Id, Suffix);
18425 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
18426 Set_Full_View (Def_Id, Full_View_Id);
18427
18428 -- Introduce an explicit reference to the private subtype,
18429 -- to prevent scope anomalies in gigi if first use appears
18430 -- in a nested context, e.g. a later function body.
18431 -- Should this be generated in other contexts than a full
18432 -- type declaration?
18433
18434 if Is_Itype (Def_Id)
18435 and then
18436 Nkind (Parent (P)) = N_Full_Type_Declaration
18437 then
18438 Build_Itype_Reference (Def_Id, Parent (P));
18439 end if;
18440
18441 else
18442 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
18443 end if;
18444
18445 when Concurrent_Kind =>
18446 Constrain_Concurrent (Def_Id, S,
18447 Related_Nod, Related_Id, Suffix);
18448
18449 when others =>
18450 Error_Msg_N ("invalid subtype mark in subtype indication", S);
18451 end case;
18452
18453 -- Size and Convention are always inherited from the base type
18454
18455 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
18456 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
18457
18458 return Def_Id;
18459 end if;
18460 end Process_Subtype;
18461
18462 ---------------------------------------
18463 -- Check_Anonymous_Access_Components --
18464 ---------------------------------------
18465
18466 procedure Check_Anonymous_Access_Components
18467 (Typ_Decl : Node_Id;
18468 Typ : Entity_Id;
18469 Prev : Entity_Id;
18470 Comp_List : Node_Id)
18471 is
18472 Loc : constant Source_Ptr := Sloc (Typ_Decl);
18473 Anon_Access : Entity_Id;
18474 Acc_Def : Node_Id;
18475 Comp : Node_Id;
18476 Comp_Def : Node_Id;
18477 Decl : Node_Id;
18478 Type_Def : Node_Id;
18479
18480 procedure Build_Incomplete_Type_Declaration;
18481 -- If the record type contains components that include an access to the
18482 -- current record, then create an incomplete type declaration for the
18483 -- record, to be used as the designated type of the anonymous access.
18484 -- This is done only once, and only if there is no previous partial
18485 -- view of the type.
18486
18487 function Designates_T (Subt : Node_Id) return Boolean;
18488 -- Check whether a node designates the enclosing record type, or 'Class
18489 -- of that type
18490
18491 function Mentions_T (Acc_Def : Node_Id) return Boolean;
18492 -- Check whether an access definition includes a reference to
18493 -- the enclosing record type. The reference can be a subtype mark
18494 -- in the access definition itself, a 'Class attribute reference, or
18495 -- recursively a reference appearing in a parameter specification
18496 -- or result definition of an access_to_subprogram definition.
18497
18498 --------------------------------------
18499 -- Build_Incomplete_Type_Declaration --
18500 --------------------------------------
18501
18502 procedure Build_Incomplete_Type_Declaration is
18503 Decl : Node_Id;
18504 Inc_T : Entity_Id;
18505 H : Entity_Id;
18506
18507 -- Is_Tagged indicates whether the type is tagged. It is tagged if
18508 -- it's "is new ... with record" or else "is tagged record ...".
18509
18510 Is_Tagged : constant Boolean :=
18511 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
18512 and then
18513 Present
18514 (Record_Extension_Part (Type_Definition (Typ_Decl))))
18515 or else
18516 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
18517 and then Tagged_Present (Type_Definition (Typ_Decl)));
18518
18519 begin
18520 -- If there is a previous partial view, no need to create a new one
18521 -- If the partial view, given by Prev, is incomplete, If Prev is
18522 -- a private declaration, full declaration is flagged accordingly.
18523
18524 if Prev /= Typ then
18525 if Is_Tagged then
18526 Make_Class_Wide_Type (Prev);
18527 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
18528 Set_Etype (Class_Wide_Type (Typ), Typ);
18529 end if;
18530
18531 return;
18532
18533 elsif Has_Private_Declaration (Typ) then
18534
18535 -- If we refer to T'Class inside T, and T is the completion of a
18536 -- private type, then we need to make sure the class-wide type
18537 -- exists.
18538
18539 if Is_Tagged then
18540 Make_Class_Wide_Type (Typ);
18541 end if;
18542
18543 return;
18544
18545 -- If there was a previous anonymous access type, the incomplete
18546 -- type declaration will have been created already.
18547
18548 elsif Present (Current_Entity (Typ))
18549 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
18550 and then Full_View (Current_Entity (Typ)) = Typ
18551 then
18552 if Is_Tagged
18553 and then Comes_From_Source (Current_Entity (Typ))
18554 and then not Is_Tagged_Type (Current_Entity (Typ))
18555 then
18556 Make_Class_Wide_Type (Typ);
18557 Error_Msg_N
18558 ("incomplete view of tagged type should be declared tagged?",
18559 Parent (Current_Entity (Typ)));
18560 end if;
18561 return;
18562
18563 else
18564 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
18565 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
18566
18567 -- Type has already been inserted into the current scope. Remove
18568 -- it, and add incomplete declaration for type, so that subsequent
18569 -- anonymous access types can use it. The entity is unchained from
18570 -- the homonym list and from immediate visibility. After analysis,
18571 -- the entity in the incomplete declaration becomes immediately
18572 -- visible in the record declaration that follows.
18573
18574 H := Current_Entity (Typ);
18575
18576 if H = Typ then
18577 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
18578 else
18579 while Present (H)
18580 and then Homonym (H) /= Typ
18581 loop
18582 H := Homonym (Typ);
18583 end loop;
18584
18585 Set_Homonym (H, Homonym (Typ));
18586 end if;
18587
18588 Insert_Before (Typ_Decl, Decl);
18589 Analyze (Decl);
18590 Set_Full_View (Inc_T, Typ);
18591
18592 if Is_Tagged then
18593
18594 -- Create a common class-wide type for both views, and set the
18595 -- Etype of the class-wide type to the full view.
18596
18597 Make_Class_Wide_Type (Inc_T);
18598 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
18599 Set_Etype (Class_Wide_Type (Typ), Typ);
18600 end if;
18601 end if;
18602 end Build_Incomplete_Type_Declaration;
18603
18604 ------------------
18605 -- Designates_T --
18606 ------------------
18607
18608 function Designates_T (Subt : Node_Id) return Boolean is
18609 Type_Id : constant Name_Id := Chars (Typ);
18610
18611 function Names_T (Nam : Node_Id) return Boolean;
18612 -- The record type has not been introduced in the current scope
18613 -- yet, so we must examine the name of the type itself, either
18614 -- an identifier T, or an expanded name of the form P.T, where
18615 -- P denotes the current scope.
18616
18617 -------------
18618 -- Names_T --
18619 -------------
18620
18621 function Names_T (Nam : Node_Id) return Boolean is
18622 begin
18623 if Nkind (Nam) = N_Identifier then
18624 return Chars (Nam) = Type_Id;
18625
18626 elsif Nkind (Nam) = N_Selected_Component then
18627 if Chars (Selector_Name (Nam)) = Type_Id then
18628 if Nkind (Prefix (Nam)) = N_Identifier then
18629 return Chars (Prefix (Nam)) = Chars (Current_Scope);
18630
18631 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
18632 return Chars (Selector_Name (Prefix (Nam))) =
18633 Chars (Current_Scope);
18634 else
18635 return False;
18636 end if;
18637
18638 else
18639 return False;
18640 end if;
18641
18642 else
18643 return False;
18644 end if;
18645 end Names_T;
18646
18647 -- Start of processing for Designates_T
18648
18649 begin
18650 if Nkind (Subt) = N_Identifier then
18651 return Chars (Subt) = Type_Id;
18652
18653 -- Reference can be through an expanded name which has not been
18654 -- analyzed yet, and which designates enclosing scopes.
18655
18656 elsif Nkind (Subt) = N_Selected_Component then
18657 if Names_T (Subt) then
18658 return True;
18659
18660 -- Otherwise it must denote an entity that is already visible.
18661 -- The access definition may name a subtype of the enclosing
18662 -- type, if there is a previous incomplete declaration for it.
18663
18664 else
18665 Find_Selected_Component (Subt);
18666 return
18667 Is_Entity_Name (Subt)
18668 and then Scope (Entity (Subt)) = Current_Scope
18669 and then
18670 (Chars (Base_Type (Entity (Subt))) = Type_Id
18671 or else
18672 (Is_Class_Wide_Type (Entity (Subt))
18673 and then
18674 Chars (Etype (Base_Type (Entity (Subt)))) =
18675 Type_Id));
18676 end if;
18677
18678 -- A reference to the current type may appear as the prefix of
18679 -- a 'Class attribute.
18680
18681 elsif Nkind (Subt) = N_Attribute_Reference
18682 and then Attribute_Name (Subt) = Name_Class
18683 then
18684 return Names_T (Prefix (Subt));
18685
18686 else
18687 return False;
18688 end if;
18689 end Designates_T;
18690
18691 ----------------
18692 -- Mentions_T --
18693 ----------------
18694
18695 function Mentions_T (Acc_Def : Node_Id) return Boolean is
18696 Param_Spec : Node_Id;
18697
18698 Acc_Subprg : constant Node_Id :=
18699 Access_To_Subprogram_Definition (Acc_Def);
18700
18701 begin
18702 if No (Acc_Subprg) then
18703 return Designates_T (Subtype_Mark (Acc_Def));
18704 end if;
18705
18706 -- Component is an access_to_subprogram: examine its formals,
18707 -- and result definition in the case of an access_to_function.
18708
18709 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
18710 while Present (Param_Spec) loop
18711 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
18712 and then Mentions_T (Parameter_Type (Param_Spec))
18713 then
18714 return True;
18715
18716 elsif Designates_T (Parameter_Type (Param_Spec)) then
18717 return True;
18718 end if;
18719
18720 Next (Param_Spec);
18721 end loop;
18722
18723 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
18724 if Nkind (Result_Definition (Acc_Subprg)) =
18725 N_Access_Definition
18726 then
18727 return Mentions_T (Result_Definition (Acc_Subprg));
18728 else
18729 return Designates_T (Result_Definition (Acc_Subprg));
18730 end if;
18731 end if;
18732
18733 return False;
18734 end Mentions_T;
18735
18736 -- Start of processing for Check_Anonymous_Access_Components
18737
18738 begin
18739 if No (Comp_List) then
18740 return;
18741 end if;
18742
18743 Comp := First (Component_Items (Comp_List));
18744 while Present (Comp) loop
18745 if Nkind (Comp) = N_Component_Declaration
18746 and then Present
18747 (Access_Definition (Component_Definition (Comp)))
18748 and then
18749 Mentions_T (Access_Definition (Component_Definition (Comp)))
18750 then
18751 Comp_Def := Component_Definition (Comp);
18752 Acc_Def :=
18753 Access_To_Subprogram_Definition
18754 (Access_Definition (Comp_Def));
18755
18756 Build_Incomplete_Type_Declaration;
18757 Anon_Access := Make_Temporary (Loc, 'S');
18758
18759 -- Create a declaration for the anonymous access type: either
18760 -- an access_to_object or an access_to_subprogram.
18761
18762 if Present (Acc_Def) then
18763 if Nkind (Acc_Def) = N_Access_Function_Definition then
18764 Type_Def :=
18765 Make_Access_Function_Definition (Loc,
18766 Parameter_Specifications =>
18767 Parameter_Specifications (Acc_Def),
18768 Result_Definition => Result_Definition (Acc_Def));
18769 else
18770 Type_Def :=
18771 Make_Access_Procedure_Definition (Loc,
18772 Parameter_Specifications =>
18773 Parameter_Specifications (Acc_Def));
18774 end if;
18775
18776 else
18777 Type_Def :=
18778 Make_Access_To_Object_Definition (Loc,
18779 Subtype_Indication =>
18780 Relocate_Node
18781 (Subtype_Mark
18782 (Access_Definition (Comp_Def))));
18783
18784 Set_Constant_Present
18785 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
18786 Set_All_Present
18787 (Type_Def, All_Present (Access_Definition (Comp_Def)));
18788 end if;
18789
18790 Set_Null_Exclusion_Present
18791 (Type_Def,
18792 Null_Exclusion_Present (Access_Definition (Comp_Def)));
18793
18794 Decl :=
18795 Make_Full_Type_Declaration (Loc,
18796 Defining_Identifier => Anon_Access,
18797 Type_Definition => Type_Def);
18798
18799 Insert_Before (Typ_Decl, Decl);
18800 Analyze (Decl);
18801
18802 -- If an access to object, Preserve entity of designated type,
18803 -- for ASIS use, before rewriting the component definition.
18804
18805 if No (Acc_Def) then
18806 declare
18807 Desig : Entity_Id;
18808
18809 begin
18810 Desig := Entity (Subtype_Indication (Type_Def));
18811
18812 -- If the access definition is to the current record,
18813 -- the visible entity at this point is an incomplete
18814 -- type. Retrieve the full view to simplify ASIS queries
18815
18816 if Ekind (Desig) = E_Incomplete_Type then
18817 Desig := Full_View (Desig);
18818 end if;
18819
18820 Set_Entity
18821 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
18822 end;
18823 end if;
18824
18825 Rewrite (Comp_Def,
18826 Make_Component_Definition (Loc,
18827 Subtype_Indication =>
18828 New_Occurrence_Of (Anon_Access, Loc)));
18829
18830 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
18831 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
18832 else
18833 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
18834 end if;
18835
18836 Set_Is_Local_Anonymous_Access (Anon_Access);
18837 end if;
18838
18839 Next (Comp);
18840 end loop;
18841
18842 if Present (Variant_Part (Comp_List)) then
18843 declare
18844 V : Node_Id;
18845 begin
18846 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
18847 while Present (V) loop
18848 Check_Anonymous_Access_Components
18849 (Typ_Decl, Typ, Prev, Component_List (V));
18850 Next_Non_Pragma (V);
18851 end loop;
18852 end;
18853 end if;
18854 end Check_Anonymous_Access_Components;
18855
18856 --------------------------------
18857 -- Preanalyze_Spec_Expression --
18858 --------------------------------
18859
18860 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18861 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18862 begin
18863 In_Spec_Expression := True;
18864 Preanalyze_And_Resolve (N, T);
18865 In_Spec_Expression := Save_In_Spec_Expression;
18866 end Preanalyze_Spec_Expression;
18867
18868 -----------------------------
18869 -- Record_Type_Declaration --
18870 -----------------------------
18871
18872 procedure Record_Type_Declaration
18873 (T : Entity_Id;
18874 N : Node_Id;
18875 Prev : Entity_Id)
18876 is
18877 Def : constant Node_Id := Type_Definition (N);
18878 Is_Tagged : Boolean;
18879 Tag_Comp : Entity_Id;
18880
18881 begin
18882 -- These flags must be initialized before calling Process_Discriminants
18883 -- because this routine makes use of them.
18884
18885 Set_Ekind (T, E_Record_Type);
18886 Set_Etype (T, T);
18887 Init_Size_Align (T);
18888 Set_Interfaces (T, No_Elist);
18889 Set_Stored_Constraint (T, No_Elist);
18890
18891 -- Normal case
18892
18893 if Ada_Version < Ada_2005
18894 or else not Interface_Present (Def)
18895 then
18896 if Limited_Present (Def) then
18897 Check_Formal_Restriction ("limited is not allowed", N);
18898 end if;
18899
18900 if Abstract_Present (Def) then
18901 Check_Formal_Restriction ("abstract is not allowed", N);
18902 end if;
18903
18904 -- The flag Is_Tagged_Type might have already been set by
18905 -- Find_Type_Name if it detected an error for declaration T. This
18906 -- arises in the case of private tagged types where the full view
18907 -- omits the word tagged.
18908
18909 Is_Tagged :=
18910 Tagged_Present (Def)
18911 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
18912
18913 Set_Is_Tagged_Type (T, Is_Tagged);
18914 Set_Is_Limited_Record (T, Limited_Present (Def));
18915
18916 -- Type is abstract if full declaration carries keyword, or if
18917 -- previous partial view did.
18918
18919 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
18920 or else Abstract_Present (Def));
18921
18922 else
18923 Check_Formal_Restriction ("interface is not allowed", N);
18924
18925 Is_Tagged := True;
18926 Analyze_Interface_Declaration (T, Def);
18927
18928 if Present (Discriminant_Specifications (N)) then
18929 Error_Msg_N
18930 ("interface types cannot have discriminants",
18931 Defining_Identifier
18932 (First (Discriminant_Specifications (N))));
18933 end if;
18934 end if;
18935
18936 -- First pass: if there are self-referential access components,
18937 -- create the required anonymous access type declarations, and if
18938 -- need be an incomplete type declaration for T itself.
18939
18940 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
18941
18942 if Ada_Version >= Ada_2005
18943 and then Present (Interface_List (Def))
18944 then
18945 Check_Interfaces (N, Def);
18946
18947 declare
18948 Ifaces_List : Elist_Id;
18949
18950 begin
18951 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
18952 -- already in the parents.
18953
18954 Collect_Interfaces
18955 (T => T,
18956 Ifaces_List => Ifaces_List,
18957 Exclude_Parents => True);
18958
18959 Set_Interfaces (T, Ifaces_List);
18960 end;
18961 end if;
18962
18963 -- Records constitute a scope for the component declarations within.
18964 -- The scope is created prior to the processing of these declarations.
18965 -- Discriminants are processed first, so that they are visible when
18966 -- processing the other components. The Ekind of the record type itself
18967 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
18968
18969 -- Enter record scope
18970
18971 Push_Scope (T);
18972
18973 -- If an incomplete or private type declaration was already given for
18974 -- the type, then this scope already exists, and the discriminants have
18975 -- been declared within. We must verify that the full declaration
18976 -- matches the incomplete one.
18977
18978 Check_Or_Process_Discriminants (N, T, Prev);
18979
18980 Set_Is_Constrained (T, not Has_Discriminants (T));
18981 Set_Has_Delayed_Freeze (T, True);
18982
18983 -- For tagged types add a manually analyzed component corresponding
18984 -- to the component _tag, the corresponding piece of tree will be
18985 -- expanded as part of the freezing actions if it is not a CPP_Class.
18986
18987 if Is_Tagged then
18988
18989 -- Do not add the tag unless we are in expansion mode
18990
18991 if Expander_Active then
18992 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
18993 Enter_Name (Tag_Comp);
18994
18995 Set_Ekind (Tag_Comp, E_Component);
18996 Set_Is_Tag (Tag_Comp);
18997 Set_Is_Aliased (Tag_Comp);
18998 Set_Etype (Tag_Comp, RTE (RE_Tag));
18999 Set_DT_Entry_Count (Tag_Comp, No_Uint);
19000 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
19001 Init_Component_Location (Tag_Comp);
19002
19003 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
19004 -- implemented interfaces.
19005
19006 if Has_Interfaces (T) then
19007 Add_Interface_Tag_Components (N, T);
19008 end if;
19009 end if;
19010
19011 Make_Class_Wide_Type (T);
19012 Set_Direct_Primitive_Operations (T, New_Elmt_List);
19013 end if;
19014
19015 -- We must suppress range checks when processing record components in
19016 -- the presence of discriminants, since we don't want spurious checks to
19017 -- be generated during their analysis, but Suppress_Range_Checks flags
19018 -- must be reset the after processing the record definition.
19019
19020 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
19021 -- couldn't we just use the normal range check suppression method here.
19022 -- That would seem cleaner ???
19023
19024 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
19025 Set_Kill_Range_Checks (T, True);
19026 Record_Type_Definition (Def, Prev);
19027 Set_Kill_Range_Checks (T, False);
19028 else
19029 Record_Type_Definition (Def, Prev);
19030 end if;
19031
19032 -- Exit from record scope
19033
19034 End_Scope;
19035
19036 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
19037 -- the implemented interfaces and associate them an aliased entity.
19038
19039 if Is_Tagged
19040 and then not Is_Empty_List (Interface_List (Def))
19041 then
19042 Derive_Progenitor_Subprograms (T, T);
19043 end if;
19044 end Record_Type_Declaration;
19045
19046 ----------------------------
19047 -- Record_Type_Definition --
19048 ----------------------------
19049
19050 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
19051 Component : Entity_Id;
19052 Ctrl_Components : Boolean := False;
19053 Final_Storage_Only : Boolean;
19054 T : Entity_Id;
19055
19056 begin
19057 if Ekind (Prev_T) = E_Incomplete_Type then
19058 T := Full_View (Prev_T);
19059 else
19060 T := Prev_T;
19061 end if;
19062
19063 -- In SPARK or ALFA, tagged types and type extensions may only be
19064 -- declared in the specification of library unit packages.
19065
19066 if Present (Def) and then Is_Tagged_Type (T) then
19067 declare
19068 Typ : Node_Id;
19069 Ctxt : Node_Id;
19070
19071 begin
19072 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
19073 Typ := Parent (Def);
19074 else
19075 pragma Assert
19076 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
19077 Typ := Parent (Parent (Def));
19078 end if;
19079
19080 Ctxt := Parent (Typ);
19081
19082 if Nkind (Ctxt) = N_Package_Body
19083 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
19084 then
19085 Check_Formal_Restriction
19086 ("type should be defined in package specification", Typ);
19087
19088 elsif Nkind (Ctxt) /= N_Package_Specification
19089 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
19090 then
19091 Check_Formal_Restriction
19092 ("type should be defined in library unit package", Typ);
19093 end if;
19094 end;
19095 end if;
19096
19097 Final_Storage_Only := not Is_Controlled (T);
19098
19099 -- Ada 2005: check whether an explicit Limited is present in a derived
19100 -- type declaration.
19101
19102 if Nkind (Parent (Def)) = N_Derived_Type_Definition
19103 and then Limited_Present (Parent (Def))
19104 then
19105 Set_Is_Limited_Record (T);
19106 end if;
19107
19108 -- If the component list of a record type is defined by the reserved
19109 -- word null and there is no discriminant part, then the record type has
19110 -- no components and all records of the type are null records (RM 3.7)
19111 -- This procedure is also called to process the extension part of a
19112 -- record extension, in which case the current scope may have inherited
19113 -- components.
19114
19115 if No (Def)
19116 or else No (Component_List (Def))
19117 or else Null_Present (Component_List (Def))
19118 then
19119 if not Is_Tagged_Type (T) then
19120 Check_Formal_Restriction ("non-tagged record cannot be null", Def);
19121 end if;
19122
19123 else
19124 Analyze_Declarations (Component_Items (Component_List (Def)));
19125
19126 if Present (Variant_Part (Component_List (Def))) then
19127 Check_Formal_Restriction ("variant part is not allowed", Def);
19128 Analyze (Variant_Part (Component_List (Def)));
19129 end if;
19130 end if;
19131
19132 -- After completing the semantic analysis of the record definition,
19133 -- record components, both new and inherited, are accessible. Set their
19134 -- kind accordingly. Exclude malformed itypes from illegal declarations,
19135 -- whose Ekind may be void.
19136
19137 Component := First_Entity (Current_Scope);
19138 while Present (Component) loop
19139 if Ekind (Component) = E_Void
19140 and then not Is_Itype (Component)
19141 then
19142 Set_Ekind (Component, E_Component);
19143 Init_Component_Location (Component);
19144 end if;
19145
19146 if Has_Task (Etype (Component)) then
19147 Set_Has_Task (T);
19148 end if;
19149
19150 if Ekind (Component) /= E_Component then
19151 null;
19152
19153 -- Do not set Has_Controlled_Component on a class-wide equivalent
19154 -- type. See Make_CW_Equivalent_Type.
19155
19156 elsif not Is_Class_Wide_Equivalent_Type (T)
19157 and then (Has_Controlled_Component (Etype (Component))
19158 or else (Chars (Component) /= Name_uParent
19159 and then Is_Controlled (Etype (Component))))
19160 then
19161 Set_Has_Controlled_Component (T, True);
19162 Final_Storage_Only :=
19163 Final_Storage_Only
19164 and then Finalize_Storage_Only (Etype (Component));
19165 Ctrl_Components := True;
19166 end if;
19167
19168 Next_Entity (Component);
19169 end loop;
19170
19171 -- A Type is Finalize_Storage_Only only if all its controlled components
19172 -- are also.
19173
19174 if Ctrl_Components then
19175 Set_Finalize_Storage_Only (T, Final_Storage_Only);
19176 end if;
19177
19178 -- Place reference to end record on the proper entity, which may
19179 -- be a partial view.
19180
19181 if Present (Def) then
19182 Process_End_Label (Def, 'e', Prev_T);
19183 end if;
19184 end Record_Type_Definition;
19185
19186 ------------------------
19187 -- Replace_Components --
19188 ------------------------
19189
19190 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
19191 function Process (N : Node_Id) return Traverse_Result;
19192
19193 -------------
19194 -- Process --
19195 -------------
19196
19197 function Process (N : Node_Id) return Traverse_Result is
19198 Comp : Entity_Id;
19199
19200 begin
19201 if Nkind (N) = N_Discriminant_Specification then
19202 Comp := First_Discriminant (Typ);
19203 while Present (Comp) loop
19204 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19205 Set_Defining_Identifier (N, Comp);
19206 exit;
19207 end if;
19208
19209 Next_Discriminant (Comp);
19210 end loop;
19211
19212 elsif Nkind (N) = N_Component_Declaration then
19213 Comp := First_Component (Typ);
19214 while Present (Comp) loop
19215 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19216 Set_Defining_Identifier (N, Comp);
19217 exit;
19218 end if;
19219
19220 Next_Component (Comp);
19221 end loop;
19222 end if;
19223
19224 return OK;
19225 end Process;
19226
19227 procedure Replace is new Traverse_Proc (Process);
19228
19229 -- Start of processing for Replace_Components
19230
19231 begin
19232 Replace (Decl);
19233 end Replace_Components;
19234
19235 -------------------------------
19236 -- Set_Completion_Referenced --
19237 -------------------------------
19238
19239 procedure Set_Completion_Referenced (E : Entity_Id) is
19240 begin
19241 -- If in main unit, mark entity that is a completion as referenced,
19242 -- warnings go on the partial view when needed.
19243
19244 if In_Extended_Main_Source_Unit (E) then
19245 Set_Referenced (E);
19246 end if;
19247 end Set_Completion_Referenced;
19248
19249 ---------------------
19250 -- Set_Fixed_Range --
19251 ---------------------
19252
19253 -- The range for fixed-point types is complicated by the fact that we
19254 -- do not know the exact end points at the time of the declaration. This
19255 -- is true for three reasons:
19256
19257 -- A size clause may affect the fudging of the end-points
19258 -- A small clause may affect the values of the end-points
19259 -- We try to include the end-points if it does not affect the size
19260
19261 -- This means that the actual end-points must be established at the point
19262 -- when the type is frozen. Meanwhile, we first narrow the range as
19263 -- permitted (so that it will fit if necessary in a small specified size),
19264 -- and then build a range subtree with these narrowed bounds.
19265
19266 -- Set_Fixed_Range constructs the range from real literal values, and sets
19267 -- the range as the Scalar_Range of the given fixed-point type entity.
19268
19269 -- The parent of this range is set to point to the entity so that it is
19270 -- properly hooked into the tree (unlike normal Scalar_Range entries for
19271 -- other scalar types, which are just pointers to the range in the
19272 -- original tree, this would otherwise be an orphan).
19273
19274 -- The tree is left unanalyzed. When the type is frozen, the processing
19275 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
19276 -- analyzed, and uses this as an indication that it should complete
19277 -- work on the range (it will know the final small and size values).
19278
19279 procedure Set_Fixed_Range
19280 (E : Entity_Id;
19281 Loc : Source_Ptr;
19282 Lo : Ureal;
19283 Hi : Ureal)
19284 is
19285 S : constant Node_Id :=
19286 Make_Range (Loc,
19287 Low_Bound => Make_Real_Literal (Loc, Lo),
19288 High_Bound => Make_Real_Literal (Loc, Hi));
19289 begin
19290 Set_Scalar_Range (E, S);
19291 Set_Parent (S, E);
19292 end Set_Fixed_Range;
19293
19294 ----------------------------------
19295 -- Set_Scalar_Range_For_Subtype --
19296 ----------------------------------
19297
19298 procedure Set_Scalar_Range_For_Subtype
19299 (Def_Id : Entity_Id;
19300 R : Node_Id;
19301 Subt : Entity_Id)
19302 is
19303 Kind : constant Entity_Kind := Ekind (Def_Id);
19304
19305 begin
19306 -- Defend against previous error
19307
19308 if Nkind (R) = N_Error then
19309 return;
19310 end if;
19311
19312 Set_Scalar_Range (Def_Id, R);
19313
19314 -- We need to link the range into the tree before resolving it so
19315 -- that types that are referenced, including importantly the subtype
19316 -- itself, are properly frozen (Freeze_Expression requires that the
19317 -- expression be properly linked into the tree). Of course if it is
19318 -- already linked in, then we do not disturb the current link.
19319
19320 if No (Parent (R)) then
19321 Set_Parent (R, Def_Id);
19322 end if;
19323
19324 -- Reset the kind of the subtype during analysis of the range, to
19325 -- catch possible premature use in the bounds themselves.
19326
19327 Set_Ekind (Def_Id, E_Void);
19328 Process_Range_Expr_In_Decl (R, Subt);
19329 Set_Ekind (Def_Id, Kind);
19330 end Set_Scalar_Range_For_Subtype;
19331
19332 --------------------------------------------------------
19333 -- Set_Stored_Constraint_From_Discriminant_Constraint --
19334 --------------------------------------------------------
19335
19336 procedure Set_Stored_Constraint_From_Discriminant_Constraint
19337 (E : Entity_Id)
19338 is
19339 begin
19340 -- Make sure set if encountered during Expand_To_Stored_Constraint
19341
19342 Set_Stored_Constraint (E, No_Elist);
19343
19344 -- Give it the right value
19345
19346 if Is_Constrained (E) and then Has_Discriminants (E) then
19347 Set_Stored_Constraint (E,
19348 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
19349 end if;
19350 end Set_Stored_Constraint_From_Discriminant_Constraint;
19351
19352 -------------------------------------
19353 -- Signed_Integer_Type_Declaration --
19354 -------------------------------------
19355
19356 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19357 Implicit_Base : Entity_Id;
19358 Base_Typ : Entity_Id;
19359 Lo_Val : Uint;
19360 Hi_Val : Uint;
19361 Errs : Boolean := False;
19362 Lo : Node_Id;
19363 Hi : Node_Id;
19364
19365 function Can_Derive_From (E : Entity_Id) return Boolean;
19366 -- Determine whether given bounds allow derivation from specified type
19367
19368 procedure Check_Bound (Expr : Node_Id);
19369 -- Check bound to make sure it is integral and static. If not, post
19370 -- appropriate error message and set Errs flag
19371
19372 ---------------------
19373 -- Can_Derive_From --
19374 ---------------------
19375
19376 -- Note we check both bounds against both end values, to deal with
19377 -- strange types like ones with a range of 0 .. -12341234.
19378
19379 function Can_Derive_From (E : Entity_Id) return Boolean is
19380 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
19381 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
19382 begin
19383 return Lo <= Lo_Val and then Lo_Val <= Hi
19384 and then
19385 Lo <= Hi_Val and then Hi_Val <= Hi;
19386 end Can_Derive_From;
19387
19388 -----------------
19389 -- Check_Bound --
19390 -----------------
19391
19392 procedure Check_Bound (Expr : Node_Id) is
19393 begin
19394 -- If a range constraint is used as an integer type definition, each
19395 -- bound of the range must be defined by a static expression of some
19396 -- integer type, but the two bounds need not have the same integer
19397 -- type (Negative bounds are allowed.) (RM 3.5.4)
19398
19399 if not Is_Integer_Type (Etype (Expr)) then
19400 Error_Msg_N
19401 ("integer type definition bounds must be of integer type", Expr);
19402 Errs := True;
19403
19404 elsif not Is_OK_Static_Expression (Expr) then
19405 Flag_Non_Static_Expr
19406 ("non-static expression used for integer type bound!", Expr);
19407 Errs := True;
19408
19409 -- The bounds are folded into literals, and we set their type to be
19410 -- universal, to avoid typing difficulties: we cannot set the type
19411 -- of the literal to the new type, because this would be a forward
19412 -- reference for the back end, and if the original type is user-
19413 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
19414
19415 else
19416 if Is_Entity_Name (Expr) then
19417 Fold_Uint (Expr, Expr_Value (Expr), True);
19418 end if;
19419
19420 Set_Etype (Expr, Universal_Integer);
19421 end if;
19422 end Check_Bound;
19423
19424 -- Start of processing for Signed_Integer_Type_Declaration
19425
19426 begin
19427 -- Create an anonymous base type
19428
19429 Implicit_Base :=
19430 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
19431
19432 -- Analyze and check the bounds, they can be of any integer type
19433
19434 Lo := Low_Bound (Def);
19435 Hi := High_Bound (Def);
19436
19437 -- Arbitrarily use Integer as the type if either bound had an error
19438
19439 if Hi = Error or else Lo = Error then
19440 Base_Typ := Any_Integer;
19441 Set_Error_Posted (T, True);
19442
19443 -- Here both bounds are OK expressions
19444
19445 else
19446 Analyze_And_Resolve (Lo, Any_Integer);
19447 Analyze_And_Resolve (Hi, Any_Integer);
19448
19449 Check_Bound (Lo);
19450 Check_Bound (Hi);
19451
19452 if Errs then
19453 Hi := Type_High_Bound (Standard_Long_Long_Integer);
19454 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
19455 end if;
19456
19457 -- Find type to derive from
19458
19459 Lo_Val := Expr_Value (Lo);
19460 Hi_Val := Expr_Value (Hi);
19461
19462 if Can_Derive_From (Standard_Short_Short_Integer) then
19463 Base_Typ := Base_Type (Standard_Short_Short_Integer);
19464
19465 elsif Can_Derive_From (Standard_Short_Integer) then
19466 Base_Typ := Base_Type (Standard_Short_Integer);
19467
19468 elsif Can_Derive_From (Standard_Integer) then
19469 Base_Typ := Base_Type (Standard_Integer);
19470
19471 elsif Can_Derive_From (Standard_Long_Integer) then
19472 Base_Typ := Base_Type (Standard_Long_Integer);
19473
19474 elsif Can_Derive_From (Standard_Long_Long_Integer) then
19475 Base_Typ := Base_Type (Standard_Long_Long_Integer);
19476
19477 else
19478 Base_Typ := Base_Type (Standard_Long_Long_Integer);
19479 Error_Msg_N ("integer type definition bounds out of range", Def);
19480 Hi := Type_High_Bound (Standard_Long_Long_Integer);
19481 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
19482 end if;
19483 end if;
19484
19485 -- Complete both implicit base and declared first subtype entities
19486
19487 Set_Etype (Implicit_Base, Base_Typ);
19488 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
19489 Set_Size_Info (Implicit_Base, (Base_Typ));
19490 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
19491 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
19492
19493 Set_Ekind (T, E_Signed_Integer_Subtype);
19494 Set_Etype (T, Implicit_Base);
19495
19496 Set_Size_Info (T, (Implicit_Base));
19497 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
19498 Set_Scalar_Range (T, Def);
19499 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
19500 Set_Is_Constrained (T);
19501 end Signed_Integer_Type_Declaration;
19502
19503 end Sem_Ch3;