sem_ch3.adb (Analyze_Declarations): Correct comments
[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-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Eval_Fat; use Eval_Fat;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Tss; use Exp_Tss;
41 with Exp_Util; use Exp_Util;
42 with Fname; use Fname;
43 with Freeze; use Freeze;
44 with Ghost; use Ghost;
45 with Itypes; use Itypes;
46 with Layout; use Layout;
47 with Lib; use Lib;
48 with Lib.Xref; use Lib.Xref;
49 with Namet; use Namet;
50 with Nmake; use Nmake;
51 with Opt; use Opt;
52 with Restrict; use Restrict;
53 with Rident; use Rident;
54 with Rtsfind; use Rtsfind;
55 with Sem; use Sem;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Case; use Sem_Case;
58 with Sem_Cat; use Sem_Cat;
59 with Sem_Ch6; use Sem_Ch6;
60 with Sem_Ch7; use Sem_Ch7;
61 with Sem_Ch8; use Sem_Ch8;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Dim; use Sem_Dim;
64 with Sem_Disp; use Sem_Disp;
65 with Sem_Dist; use Sem_Dist;
66 with Sem_Elim; use Sem_Elim;
67 with Sem_Eval; use Sem_Eval;
68 with Sem_Mech; use Sem_Mech;
69 with Sem_Res; use Sem_Res;
70 with Sem_Smem; use Sem_Smem;
71 with Sem_Type; use Sem_Type;
72 with Sem_Util; use Sem_Util;
73 with Sem_Warn; use Sem_Warn;
74 with Stand; use Stand;
75 with Sinfo; use Sinfo;
76 with Sinput; use Sinput;
77 with Snames; use Snames;
78 with Targparm; use Targparm;
79 with Tbuild; use Tbuild;
80 with Ttypes; use Ttypes;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
83
84 package body Sem_Ch3 is
85
86 -----------------------
87 -- Local Subprograms --
88 -----------------------
89
90 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
91 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
92 -- abstract interface types implemented by a record type or a derived
93 -- record type.
94
95 procedure Build_Derived_Type
96 (N : Node_Id;
97 Parent_Type : Entity_Id;
98 Derived_Type : Entity_Id;
99 Is_Completion : Boolean;
100 Derive_Subps : Boolean := True);
101 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
102 -- the N_Full_Type_Declaration node containing the derived type definition.
103 -- Parent_Type is the entity for the parent type in the derived type
104 -- definition and Derived_Type the actual derived type. Is_Completion must
105 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
106 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
107 -- completion of a private type declaration. If Is_Completion is set to
108 -- True, N is the completion of a private type declaration and Derived_Type
109 -- is different from the defining identifier inside N (i.e. Derived_Type /=
110 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
111 -- subprograms should be derived. The only case where this parameter is
112 -- False is when Build_Derived_Type is recursively called to process an
113 -- implicit derived full type for a type derived from a private type (in
114 -- that case the subprograms must only be derived for the private view of
115 -- the type).
116 --
117 -- ??? These flags need a bit of re-examination and re-documentation:
118 -- ??? are they both necessary (both seem related to the recursion)?
119
120 procedure Build_Derived_Access_Type
121 (N : Node_Id;
122 Parent_Type : Entity_Id;
123 Derived_Type : Entity_Id);
124 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
125 -- create an implicit base if the parent type is constrained or if the
126 -- subtype indication has a constraint.
127
128 procedure Build_Derived_Array_Type
129 (N : Node_Id;
130 Parent_Type : Entity_Id;
131 Derived_Type : Entity_Id);
132 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
133 -- create an implicit base if the parent type is constrained or if the
134 -- subtype indication has a constraint.
135
136 procedure Build_Derived_Concurrent_Type
137 (N : Node_Id;
138 Parent_Type : Entity_Id;
139 Derived_Type : Entity_Id);
140 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
141 -- protected type, inherit entries and protected subprograms, check
142 -- legality of discriminant constraints if any.
143
144 procedure Build_Derived_Enumeration_Type
145 (N : Node_Id;
146 Parent_Type : Entity_Id;
147 Derived_Type : Entity_Id);
148 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
149 -- type, we must create a new list of literals. Types derived from
150 -- Character and [Wide_]Wide_Character are special-cased.
151
152 procedure Build_Derived_Numeric_Type
153 (N : Node_Id;
154 Parent_Type : Entity_Id;
155 Derived_Type : Entity_Id);
156 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
157 -- an anonymous base type, and propagate constraint to subtype if needed.
158
159 procedure Build_Derived_Private_Type
160 (N : Node_Id;
161 Parent_Type : Entity_Id;
162 Derived_Type : Entity_Id;
163 Is_Completion : Boolean;
164 Derive_Subps : Boolean := True);
165 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
166 -- because the parent may or may not have a completion, and the derivation
167 -- may itself be a completion.
168
169 procedure Build_Derived_Record_Type
170 (N : Node_Id;
171 Parent_Type : Entity_Id;
172 Derived_Type : Entity_Id;
173 Derive_Subps : Boolean := True);
174 -- Subsidiary procedure used for tagged and untagged record types
175 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
176 -- All parameters are as in Build_Derived_Type except that N, in
177 -- addition to being an N_Full_Type_Declaration node, can also be an
178 -- N_Private_Extension_Declaration node. See the definition of this routine
179 -- for much more info. Derive_Subps indicates whether subprograms should be
180 -- derived from the parent type. The only case where Derive_Subps is False
181 -- is for an implicit derived full type for a type derived from a private
182 -- type (see Build_Derived_Type).
183
184 procedure Build_Discriminal (Discrim : Entity_Id);
185 -- Create the discriminal corresponding to discriminant Discrim, that is
186 -- the parameter corresponding to Discrim to be used in initialization
187 -- procedures for the type where Discrim is a discriminant. Discriminals
188 -- are not used during semantic analysis, and are not fully defined
189 -- entities until expansion. Thus they are not given a scope until
190 -- initialization procedures are built.
191
192 function Build_Discriminant_Constraints
193 (T : Entity_Id;
194 Def : Node_Id;
195 Derived_Def : Boolean := False) return Elist_Id;
196 -- Validate discriminant constraints and return the list of the constraints
197 -- in order of discriminant declarations, where T is the discriminated
198 -- unconstrained type. Def is the N_Subtype_Indication node where the
199 -- discriminants constraints for T are specified. Derived_Def is True
200 -- when building the discriminant constraints in a derived type definition
201 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
202 -- type and Def is the constraint "(xxx)" on T and this routine sets the
203 -- Corresponding_Discriminant field of the discriminants in the derived
204 -- type D to point to the corresponding discriminants in the parent type T.
205
206 procedure Build_Discriminated_Subtype
207 (T : Entity_Id;
208 Def_Id : Entity_Id;
209 Elist : Elist_Id;
210 Related_Nod : Node_Id;
211 For_Access : Boolean := False);
212 -- Subsidiary procedure to Constrain_Discriminated_Type and to
213 -- Process_Incomplete_Dependents. Given
214 --
215 -- T (a possibly discriminated base type)
216 -- Def_Id (a very partially built subtype for T),
217 --
218 -- the call completes Def_Id to be the appropriate E_*_Subtype.
219 --
220 -- The Elist is the list of discriminant constraints if any (it is set
221 -- to No_Elist if T is not a discriminated type, and to an empty list if
222 -- T has discriminants but there are no discriminant constraints). The
223 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
224 -- The For_Access says whether or not this subtype is really constraining
225 -- an access type. That is its sole purpose is the designated type of an
226 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
227 -- is built to avoid freezing T when the access subtype is frozen.
228
229 function Build_Scalar_Bound
230 (Bound : Node_Id;
231 Par_T : Entity_Id;
232 Der_T : Entity_Id) return Node_Id;
233 -- The bounds of a derived scalar type are conversions of the bounds of
234 -- the parent type. Optimize the representation if the bounds are literals.
235 -- Needs a more complete spec--what are the parameters exactly, and what
236 -- exactly is the returned value, and how is Bound affected???
237
238 procedure Build_Underlying_Full_View
239 (N : Node_Id;
240 Typ : Entity_Id;
241 Par : Entity_Id);
242 -- If the completion of a private type is itself derived from a private
243 -- type, or if the full view of a private subtype is itself private, the
244 -- back-end has no way to compute the actual size of this type. We build
245 -- an internal subtype declaration of the proper parent type to convey
246 -- this information. This extra mechanism is needed because a full
247 -- view cannot itself have a full view (it would get clobbered during
248 -- view exchanges).
249
250 procedure Check_Access_Discriminant_Requires_Limited
251 (D : Node_Id;
252 Loc : Node_Id);
253 -- Check the restriction that the type to which an access discriminant
254 -- belongs must be a concurrent type or a descendant of a type with
255 -- the reserved word 'limited' in its declaration.
256
257 procedure Check_Anonymous_Access_Components
258 (Typ_Decl : Node_Id;
259 Typ : Entity_Id;
260 Prev : Entity_Id;
261 Comp_List : Node_Id);
262 -- Ada 2005 AI-382: an access component in a record definition can refer to
263 -- the enclosing record, in which case it denotes the type itself, and not
264 -- the current instance of the type. We create an anonymous access type for
265 -- the component, and flag it as an access to a component, so accessibility
266 -- checks are properly performed on it. The declaration of the access type
267 -- is placed ahead of that of the record to prevent order-of-elaboration
268 -- circularity issues in Gigi. We create an incomplete type for the record
269 -- declaration, which is the designated type of the anonymous access.
270
271 procedure Check_Delta_Expression (E : Node_Id);
272 -- Check that the expression represented by E is suitable for use as a
273 -- delta expression, i.e. it is of real type and is static.
274
275 procedure Check_Digits_Expression (E : Node_Id);
276 -- Check that the expression represented by E is suitable for use as a
277 -- digits expression, i.e. it is of integer type, positive and static.
278
279 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
280 -- Validate the initialization of an object declaration. T is the required
281 -- type, and Exp is the initialization expression.
282
283 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
284 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
285
286 procedure Check_Or_Process_Discriminants
287 (N : Node_Id;
288 T : Entity_Id;
289 Prev : Entity_Id := Empty);
290 -- If N is the full declaration of the completion T of an incomplete or
291 -- private type, check its discriminants (which are already known to be
292 -- conformant with those of the partial view, see Find_Type_Name),
293 -- otherwise process them. Prev is the entity of the partial declaration,
294 -- if any.
295
296 procedure Check_Real_Bound (Bound : Node_Id);
297 -- Check given bound for being of real type and static. If not, post an
298 -- appropriate message, and rewrite the bound with the real literal zero.
299
300 procedure Constant_Redeclaration
301 (Id : Entity_Id;
302 N : Node_Id;
303 T : out Entity_Id);
304 -- Various checks on legality of full declaration of deferred constant.
305 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
306 -- node. The caller has not yet set any attributes of this entity.
307
308 function Contain_Interface
309 (Iface : Entity_Id;
310 Ifaces : Elist_Id) return Boolean;
311 -- Ada 2005: Determine whether Iface is present in the list Ifaces
312
313 procedure Convert_Scalar_Bounds
314 (N : Node_Id;
315 Parent_Type : Entity_Id;
316 Derived_Type : Entity_Id;
317 Loc : Source_Ptr);
318 -- For derived scalar types, convert the bounds in the type definition to
319 -- the derived type, and complete their analysis. Given a constraint of the
320 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
321 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
322 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
323 -- subtype are conversions of those bounds to the derived_type, so that
324 -- their typing is consistent.
325
326 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
327 -- Copies attributes from array base type T2 to array base type T1. Copies
328 -- only attributes that apply to base types, but not subtypes.
329
330 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
331 -- Copies attributes from array subtype T2 to array subtype T1. Copies
332 -- attributes that apply to both subtypes and base types.
333
334 procedure Create_Constrained_Components
335 (Subt : Entity_Id;
336 Decl_Node : Node_Id;
337 Typ : Entity_Id;
338 Constraints : Elist_Id);
339 -- Build the list of entities for a constrained discriminated record
340 -- subtype. If a component depends on a discriminant, replace its subtype
341 -- using the discriminant values in the discriminant constraint. Subt
342 -- is the defining identifier for the subtype whose list of constrained
343 -- entities we will create. Decl_Node is the type declaration node where
344 -- we will attach all the itypes created. Typ is the base discriminated
345 -- type for the subtype Subt. Constraints is the list of discriminant
346 -- constraints for Typ.
347
348 function Constrain_Component_Type
349 (Comp : Entity_Id;
350 Constrained_Typ : Entity_Id;
351 Related_Node : Node_Id;
352 Typ : Entity_Id;
353 Constraints : Elist_Id) return Entity_Id;
354 -- Given a discriminated base type Typ, a list of discriminant constraints,
355 -- Constraints, for Typ and a component Comp of Typ, create and return the
356 -- type corresponding to Etype (Comp) where all discriminant references
357 -- are replaced with the corresponding constraint. If Etype (Comp) contains
358 -- no discriminant references then it is returned as-is. Constrained_Typ
359 -- is the final constrained subtype to which the constrained component
360 -- belongs. Related_Node is the node where we attach all created itypes.
361
362 procedure Constrain_Access
363 (Def_Id : in out Entity_Id;
364 S : Node_Id;
365 Related_Nod : Node_Id);
366 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
367 -- an anonymous type created for a subtype indication. In that case it is
368 -- created in the procedure and attached to Related_Nod.
369
370 procedure Constrain_Array
371 (Def_Id : in out Entity_Id;
372 SI : Node_Id;
373 Related_Nod : Node_Id;
374 Related_Id : Entity_Id;
375 Suffix : Character);
376 -- Apply a list of index constraints to an unconstrained array type. The
377 -- first parameter is the entity for the resulting subtype. A value of
378 -- Empty for Def_Id indicates that an implicit type must be created, but
379 -- creation is delayed (and must be done by this procedure) because other
380 -- subsidiary implicit types must be created first (which is why Def_Id
381 -- is an in/out parameter). The second parameter is a subtype indication
382 -- node for the constrained array to be created (e.g. something of the
383 -- form string (1 .. 10)). Related_Nod gives the place where this type
384 -- has to be inserted in the tree. The Related_Id and Suffix parameters
385 -- are used to build the associated Implicit type name.
386
387 procedure Constrain_Concurrent
388 (Def_Id : in out Entity_Id;
389 SI : Node_Id;
390 Related_Nod : Node_Id;
391 Related_Id : Entity_Id;
392 Suffix : Character);
393 -- Apply list of discriminant constraints to an unconstrained concurrent
394 -- type.
395 --
396 -- SI is the N_Subtype_Indication node containing the constraint and
397 -- the unconstrained type to constrain.
398 --
399 -- Def_Id is the entity for the resulting constrained subtype. A value
400 -- of Empty for Def_Id indicates that an implicit type must be created,
401 -- but creation is delayed (and must be done by this procedure) because
402 -- other subsidiary implicit types must be created first (which is why
403 -- Def_Id is an in/out parameter).
404 --
405 -- Related_Nod gives the place where this type has to be inserted
406 -- in the tree.
407 --
408 -- The last two arguments are used to create its external name if needed.
409
410 function Constrain_Corresponding_Record
411 (Prot_Subt : Entity_Id;
412 Corr_Rec : Entity_Id;
413 Related_Nod : Node_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 implicit
527 -- 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 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
581 -- Propagate static and dynamic predicate flags from a parent to the
582 -- subtype in a subtype declaration with and without constraints.
583
584 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
585 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
586 -- Determine whether subprogram Subp is a procedure subject to pragma
587 -- Extensions_Visible with value False and has at least one controlling
588 -- parameter of mode OUT.
589
590 function Is_Valid_Constraint_Kind
591 (T_Kind : Type_Kind;
592 Constraint_Kind : Node_Kind) return Boolean;
593 -- Returns True if it is legal to apply the given kind of constraint to the
594 -- given kind of type (index constraint to an array type, for example).
595
596 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
597 -- Create new modular type. Verify that modulus is in bounds
598
599 procedure New_Concatenation_Op (Typ : Entity_Id);
600 -- Create an abbreviated declaration for an operator in order to
601 -- materialize concatenation on array types.
602
603 procedure Ordinary_Fixed_Point_Type_Declaration
604 (T : Entity_Id;
605 Def : Node_Id);
606 -- Create a new ordinary fixed point type, and apply the constraint to
607 -- obtain subtype of it.
608
609 procedure Prepare_Private_Subtype_Completion
610 (Id : Entity_Id;
611 Related_Nod : Node_Id);
612 -- Id is a subtype of some private type. Creates the full declaration
613 -- associated with Id whenever possible, i.e. when the full declaration
614 -- of the base type is already known. Records each subtype into
615 -- Private_Dependents of the base type.
616
617 procedure Process_Incomplete_Dependents
618 (N : Node_Id;
619 Full_T : Entity_Id;
620 Inc_T : Entity_Id);
621 -- Process all entities that depend on an incomplete type. There include
622 -- subtypes, subprogram types that mention the incomplete type in their
623 -- profiles, and subprogram with access parameters that designate the
624 -- incomplete type.
625
626 -- Inc_T is the defining identifier of an incomplete type declaration, its
627 -- Ekind is E_Incomplete_Type.
628 --
629 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
630 --
631 -- Full_T is N's defining identifier.
632 --
633 -- Subtypes of incomplete types with discriminants are completed when the
634 -- parent type is. This is simpler than private subtypes, because they can
635 -- only appear in the same scope, and there is no need to exchange views.
636 -- Similarly, access_to_subprogram types may have a parameter or a return
637 -- type that is an incomplete type, and that must be replaced with the
638 -- full type.
639 --
640 -- If the full type is tagged, subprogram with access parameters that
641 -- designated the incomplete may be primitive operations of the full type,
642 -- and have to be processed accordingly.
643
644 procedure Process_Real_Range_Specification (Def : Node_Id);
645 -- Given the type definition for a real type, this procedure processes and
646 -- checks the real range specification of this type definition if one is
647 -- present. If errors are found, error messages are posted, and the
648 -- Real_Range_Specification of Def is reset to Empty.
649
650 procedure Record_Type_Declaration
651 (T : Entity_Id;
652 N : Node_Id;
653 Prev : Entity_Id);
654 -- Process a record type declaration (for both untagged and tagged
655 -- records). Parameters T and N are exactly like in procedure
656 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
657 -- for this routine. If this is the completion of an incomplete type
658 -- declaration, Prev is the entity of the incomplete declaration, used for
659 -- cross-referencing. Otherwise Prev = T.
660
661 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
662 -- This routine is used to process the actual record type definition (both
663 -- for untagged and tagged records). Def is a record type definition node.
664 -- This procedure analyzes the components in this record type definition.
665 -- Prev_T is the entity for the enclosing record type. It is provided so
666 -- that its Has_Task flag can be set if any of the component have Has_Task
667 -- set. If the declaration is the completion of an incomplete type
668 -- declaration, Prev_T is the original incomplete type, whose full view is
669 -- the record type.
670
671 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
672 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
673 -- build a copy of the declaration tree of the parent, and we create
674 -- independently the list of components for the derived type. Semantic
675 -- information uses the component entities, but record representation
676 -- clauses are validated on the declaration tree. This procedure replaces
677 -- discriminants and components in the declaration with those that have
678 -- been created by Inherit_Components.
679
680 procedure Set_Fixed_Range
681 (E : Entity_Id;
682 Loc : Source_Ptr;
683 Lo : Ureal;
684 Hi : Ureal);
685 -- Build a range node with the given bounds and set it as the Scalar_Range
686 -- of the given fixed-point type entity. Loc is the source location used
687 -- for the constructed range. See body for further details.
688
689 procedure Set_Scalar_Range_For_Subtype
690 (Def_Id : Entity_Id;
691 R : Node_Id;
692 Subt : Entity_Id);
693 -- This routine is used to set the scalar range field for a subtype given
694 -- Def_Id, the entity for the subtype, and R, the range expression for the
695 -- scalar range. Subt provides the parent subtype to be used to analyze,
696 -- resolve, and check the given range.
697
698 procedure Set_Default_SSO (T : Entity_Id);
699 -- T is the entity for an array or record being declared. This procedure
700 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
701 -- to the setting of Opt.Default_SSO.
702
703 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
704 -- Create a new signed integer entity, and apply the constraint to obtain
705 -- the required first named subtype of this type.
706
707 procedure Set_Stored_Constraint_From_Discriminant_Constraint
708 (E : Entity_Id);
709 -- E is some record type. This routine computes E's Stored_Constraint
710 -- from its Discriminant_Constraint.
711
712 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
713 -- Check that an entity in a list of progenitors is an interface,
714 -- emit error otherwise.
715
716 -----------------------
717 -- Access_Definition --
718 -----------------------
719
720 function Access_Definition
721 (Related_Nod : Node_Id;
722 N : Node_Id) return Entity_Id
723 is
724 Anon_Type : Entity_Id;
725 Anon_Scope : Entity_Id;
726 Desig_Type : Entity_Id;
727 Enclosing_Prot_Type : Entity_Id := Empty;
728
729 begin
730 Check_SPARK_05_Restriction ("access type is not allowed", N);
731
732 if Is_Entry (Current_Scope)
733 and then Is_Task_Type (Etype (Scope (Current_Scope)))
734 then
735 Error_Msg_N ("task entries cannot have access parameters", N);
736 return Empty;
737 end if;
738
739 -- Ada 2005: For an object declaration the corresponding anonymous
740 -- type is declared in the current scope.
741
742 -- If the access definition is the return type of another access to
743 -- function, scope is the current one, because it is the one of the
744 -- current type declaration, except for the pathological case below.
745
746 if Nkind_In (Related_Nod, N_Object_Declaration,
747 N_Access_Function_Definition)
748 then
749 Anon_Scope := Current_Scope;
750
751 -- A pathological case: function returning access functions that
752 -- return access functions, etc. Each anonymous access type created
753 -- is in the enclosing scope of the outermost function.
754
755 declare
756 Par : Node_Id;
757
758 begin
759 Par := Related_Nod;
760 while Nkind_In (Par, N_Access_Function_Definition,
761 N_Access_Definition)
762 loop
763 Par := Parent (Par);
764 end loop;
765
766 if Nkind (Par) = N_Function_Specification then
767 Anon_Scope := Scope (Defining_Entity (Par));
768 end if;
769 end;
770
771 -- For the anonymous function result case, retrieve the scope of the
772 -- function specification's associated entity rather than using the
773 -- current scope. The current scope will be the function itself if the
774 -- formal part is currently being analyzed, but will be the parent scope
775 -- in the case of a parameterless function, and we always want to use
776 -- the function's parent scope. Finally, if the function is a child
777 -- unit, we must traverse the tree to retrieve the proper entity.
778
779 elsif Nkind (Related_Nod) = N_Function_Specification
780 and then Nkind (Parent (N)) /= N_Parameter_Specification
781 then
782 -- If the current scope is a protected type, the anonymous access
783 -- is associated with one of the protected operations, and must
784 -- be available in the scope that encloses the protected declaration.
785 -- Otherwise the type is in the scope enclosing the subprogram.
786
787 -- If the function has formals, The return type of a subprogram
788 -- declaration is analyzed in the scope of the subprogram (see
789 -- Process_Formals) and thus the protected type, if present, is
790 -- the scope of the current function scope.
791
792 if Ekind (Current_Scope) = E_Protected_Type then
793 Enclosing_Prot_Type := Current_Scope;
794
795 elsif Ekind (Current_Scope) = E_Function
796 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
797 then
798 Enclosing_Prot_Type := Scope (Current_Scope);
799 end if;
800
801 if Present (Enclosing_Prot_Type) then
802 Anon_Scope := Scope (Enclosing_Prot_Type);
803
804 else
805 Anon_Scope := Scope (Defining_Entity (Related_Nod));
806 end if;
807
808 -- For an access type definition, if the current scope is a child
809 -- unit it is the scope of the type.
810
811 elsif Is_Compilation_Unit (Current_Scope) then
812 Anon_Scope := Current_Scope;
813
814 -- For access formals, access components, and access discriminants, the
815 -- scope is that of the enclosing declaration,
816
817 else
818 Anon_Scope := Scope (Current_Scope);
819 end if;
820
821 Anon_Type :=
822 Create_Itype
823 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
824
825 if All_Present (N)
826 and then Ada_Version >= Ada_2005
827 then
828 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
829 end if;
830
831 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
832 -- the corresponding semantic routine
833
834 if Present (Access_To_Subprogram_Definition (N)) then
835
836 -- Compiler runtime units are compiled in Ada 2005 mode when building
837 -- the runtime library but must also be compilable in Ada 95 mode
838 -- (when bootstrapping the compiler).
839
840 Check_Compiler_Unit ("anonymous access to subprogram", N);
841
842 Access_Subprogram_Declaration
843 (T_Name => Anon_Type,
844 T_Def => Access_To_Subprogram_Definition (N));
845
846 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
847 Set_Ekind
848 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
849 else
850 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
851 end if;
852
853 Set_Can_Use_Internal_Rep
854 (Anon_Type, not Always_Compatible_Rep_On_Target);
855
856 -- If the anonymous access is associated with a protected operation,
857 -- create a reference to it after the enclosing protected definition
858 -- because the itype will be used in the subsequent bodies.
859
860 -- If the anonymous access itself is protected, a full type
861 -- declaratiton will be created for it, so that the equivalent
862 -- record type can be constructed. For further details, see
863 -- Replace_Anonymous_Access_To_Protected-Subprogram.
864
865 if Ekind (Current_Scope) = E_Protected_Type
866 and then not Protected_Present (Access_To_Subprogram_Definition (N))
867 then
868 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
869 end if;
870
871 return Anon_Type;
872 end if;
873
874 Find_Type (Subtype_Mark (N));
875 Desig_Type := Entity (Subtype_Mark (N));
876
877 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
878 Set_Etype (Anon_Type, Anon_Type);
879
880 -- Make sure the anonymous access type has size and alignment fields
881 -- set, as required by gigi. This is necessary in the case of the
882 -- Task_Body_Procedure.
883
884 if not Has_Private_Component (Desig_Type) then
885 Layout_Type (Anon_Type);
886 end if;
887
888 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
889 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
890 -- the null value is allowed. In Ada 95 the null value is never allowed.
891
892 if Ada_Version >= Ada_2005 then
893 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
894 else
895 Set_Can_Never_Be_Null (Anon_Type, True);
896 end if;
897
898 -- The anonymous access type is as public as the discriminated type or
899 -- subprogram that defines it. It is imported (for back-end purposes)
900 -- if the designated type is.
901
902 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
903
904 -- Ada 2005 (AI-231): Propagate the access-constant attribute
905
906 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
907
908 -- The context is either a subprogram declaration, object declaration,
909 -- or an access discriminant, in a private or a full type declaration.
910 -- In the case of a subprogram, if the designated type is incomplete,
911 -- the operation will be a primitive operation of the full type, to be
912 -- updated subsequently. If the type is imported through a limited_with
913 -- clause, the subprogram is not a primitive operation of the type
914 -- (which is declared elsewhere in some other scope).
915
916 if Ekind (Desig_Type) = E_Incomplete_Type
917 and then not From_Limited_With (Desig_Type)
918 and then Is_Overloadable (Current_Scope)
919 then
920 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
921 Set_Has_Delayed_Freeze (Current_Scope);
922 end if;
923
924 -- Ada 2005: If the designated type is an interface that may contain
925 -- tasks, create a Master entity for the declaration. This must be done
926 -- before expansion of the full declaration, because the declaration may
927 -- include an expression that is an allocator, whose expansion needs the
928 -- proper Master for the created tasks.
929
930 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
931 then
932 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
933 then
934 Build_Class_Wide_Master (Anon_Type);
935
936 -- Similarly, if the type is an anonymous access that designates
937 -- tasks, create a master entity for it in the current context.
938
939 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
940 then
941 Build_Master_Entity (Defining_Identifier (Related_Nod));
942 Build_Master_Renaming (Anon_Type);
943 end if;
944 end if;
945
946 -- For a private component of a protected type, it is imperative that
947 -- the back-end elaborate the type immediately after the protected
948 -- declaration, because this type will be used in the declarations
949 -- created for the component within each protected body, so we must
950 -- create an itype reference for it now.
951
952 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
953 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
954
955 -- Similarly, if the access definition is the return result of a
956 -- function, create an itype reference for it because it will be used
957 -- within the function body. For a regular function that is not a
958 -- compilation unit, insert reference after the declaration. For a
959 -- protected operation, insert it after the enclosing protected type
960 -- declaration. In either case, do not create a reference for a type
961 -- obtained through a limited_with clause, because this would introduce
962 -- semantic dependencies.
963
964 -- Similarly, do not create a reference if the designated type is a
965 -- generic formal, because no use of it will reach the backend.
966
967 elsif Nkind (Related_Nod) = N_Function_Specification
968 and then not From_Limited_With (Desig_Type)
969 and then not Is_Generic_Type (Desig_Type)
970 then
971 if Present (Enclosing_Prot_Type) then
972 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
973
974 elsif Is_List_Member (Parent (Related_Nod))
975 and then Nkind (Parent (N)) /= N_Parameter_Specification
976 then
977 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
978 end if;
979
980 -- Finally, create an itype reference for an object declaration of an
981 -- anonymous access type. This is strictly necessary only for deferred
982 -- constants, but in any case will avoid out-of-scope problems in the
983 -- back-end.
984
985 elsif Nkind (Related_Nod) = N_Object_Declaration then
986 Build_Itype_Reference (Anon_Type, Related_Nod);
987 end if;
988
989 return Anon_Type;
990 end Access_Definition;
991
992 -----------------------------------
993 -- Access_Subprogram_Declaration --
994 -----------------------------------
995
996 procedure Access_Subprogram_Declaration
997 (T_Name : Entity_Id;
998 T_Def : Node_Id)
999 is
1000 procedure Check_For_Premature_Usage (Def : Node_Id);
1001 -- Check that type T_Name is not used, directly or recursively, as a
1002 -- parameter or a return type in Def. Def is either a subtype, an
1003 -- access_definition, or an access_to_subprogram_definition.
1004
1005 -------------------------------
1006 -- Check_For_Premature_Usage --
1007 -------------------------------
1008
1009 procedure Check_For_Premature_Usage (Def : Node_Id) is
1010 Param : Node_Id;
1011
1012 begin
1013 -- Check for a subtype mark
1014
1015 if Nkind (Def) in N_Has_Etype then
1016 if Etype (Def) = T_Name then
1017 Error_Msg_N
1018 ("type& cannot be used before end of its declaration", Def);
1019 end if;
1020
1021 -- If this is not a subtype, then this is an access_definition
1022
1023 elsif Nkind (Def) = N_Access_Definition then
1024 if Present (Access_To_Subprogram_Definition (Def)) then
1025 Check_For_Premature_Usage
1026 (Access_To_Subprogram_Definition (Def));
1027 else
1028 Check_For_Premature_Usage (Subtype_Mark (Def));
1029 end if;
1030
1031 -- The only cases left are N_Access_Function_Definition and
1032 -- N_Access_Procedure_Definition.
1033
1034 else
1035 if Present (Parameter_Specifications (Def)) then
1036 Param := First (Parameter_Specifications (Def));
1037 while Present (Param) loop
1038 Check_For_Premature_Usage (Parameter_Type (Param));
1039 Param := Next (Param);
1040 end loop;
1041 end if;
1042
1043 if Nkind (Def) = N_Access_Function_Definition then
1044 Check_For_Premature_Usage (Result_Definition (Def));
1045 end if;
1046 end if;
1047 end Check_For_Premature_Usage;
1048
1049 -- Local variables
1050
1051 Formals : constant List_Id := Parameter_Specifications (T_Def);
1052 Formal : Entity_Id;
1053 D_Ityp : Node_Id;
1054 Desig_Type : constant Entity_Id :=
1055 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1056
1057 -- Start of processing for Access_Subprogram_Declaration
1058
1059 begin
1060 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1061
1062 -- Associate the Itype node with the inner full-type declaration or
1063 -- subprogram spec or entry body. This is required to handle nested
1064 -- anonymous declarations. For example:
1065
1066 -- procedure P
1067 -- (X : access procedure
1068 -- (Y : access procedure
1069 -- (Z : access T)))
1070
1071 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1072 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1073 N_Private_Type_Declaration,
1074 N_Private_Extension_Declaration,
1075 N_Procedure_Specification,
1076 N_Function_Specification,
1077 N_Entry_Body)
1078
1079 or else
1080 Nkind_In (D_Ityp, N_Object_Declaration,
1081 N_Object_Renaming_Declaration,
1082 N_Formal_Object_Declaration,
1083 N_Formal_Type_Declaration,
1084 N_Task_Type_Declaration,
1085 N_Protected_Type_Declaration))
1086 loop
1087 D_Ityp := Parent (D_Ityp);
1088 pragma Assert (D_Ityp /= Empty);
1089 end loop;
1090
1091 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1092
1093 if Nkind_In (D_Ityp, N_Procedure_Specification,
1094 N_Function_Specification)
1095 then
1096 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1097
1098 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1099 N_Object_Declaration,
1100 N_Object_Renaming_Declaration,
1101 N_Formal_Type_Declaration)
1102 then
1103 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1104 end if;
1105
1106 if Nkind (T_Def) = N_Access_Function_Definition then
1107 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1108 declare
1109 Acc : constant Node_Id := Result_Definition (T_Def);
1110
1111 begin
1112 if Present (Access_To_Subprogram_Definition (Acc))
1113 and then
1114 Protected_Present (Access_To_Subprogram_Definition (Acc))
1115 then
1116 Set_Etype
1117 (Desig_Type,
1118 Replace_Anonymous_Access_To_Protected_Subprogram
1119 (T_Def));
1120
1121 else
1122 Set_Etype
1123 (Desig_Type,
1124 Access_Definition (T_Def, Result_Definition (T_Def)));
1125 end if;
1126 end;
1127
1128 else
1129 Analyze (Result_Definition (T_Def));
1130
1131 declare
1132 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1133
1134 begin
1135 -- If a null exclusion is imposed on the result type, then
1136 -- create a null-excluding itype (an access subtype) and use
1137 -- it as the function's Etype.
1138
1139 if Is_Access_Type (Typ)
1140 and then Null_Exclusion_In_Return_Present (T_Def)
1141 then
1142 Set_Etype (Desig_Type,
1143 Create_Null_Excluding_Itype
1144 (T => Typ,
1145 Related_Nod => T_Def,
1146 Scope_Id => Current_Scope));
1147
1148 else
1149 if From_Limited_With (Typ) then
1150
1151 -- AI05-151: Incomplete types are allowed in all basic
1152 -- declarations, including access to subprograms.
1153
1154 if Ada_Version >= Ada_2012 then
1155 null;
1156
1157 else
1158 Error_Msg_NE
1159 ("illegal use of incomplete type&",
1160 Result_Definition (T_Def), Typ);
1161 end if;
1162
1163 elsif Ekind (Current_Scope) = E_Package
1164 and then In_Private_Part (Current_Scope)
1165 then
1166 if Ekind (Typ) = E_Incomplete_Type then
1167 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1168
1169 elsif Is_Class_Wide_Type (Typ)
1170 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1171 then
1172 Append_Elmt
1173 (Desig_Type, Private_Dependents (Etype (Typ)));
1174 end if;
1175 end if;
1176
1177 Set_Etype (Desig_Type, Typ);
1178 end if;
1179 end;
1180 end if;
1181
1182 if not (Is_Type (Etype (Desig_Type))) then
1183 Error_Msg_N
1184 ("expect type in function specification",
1185 Result_Definition (T_Def));
1186 end if;
1187
1188 else
1189 Set_Etype (Desig_Type, Standard_Void_Type);
1190 end if;
1191
1192 if Present (Formals) then
1193 Push_Scope (Desig_Type);
1194
1195 -- Some special tests here. These special tests can be removed
1196 -- if and when Itypes always have proper parent pointers to their
1197 -- declarations???
1198
1199 -- Special test 1) Link defining_identifier of formals. Required by
1200 -- First_Formal to provide its functionality.
1201
1202 declare
1203 F : Node_Id;
1204
1205 begin
1206 F := First (Formals);
1207
1208 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1209 -- when it is part of an unconstrained type and subtype expansion
1210 -- is disabled. To avoid back-end problems with shared profiles,
1211 -- use previous subprogram type as the designated type, and then
1212 -- remove scope added above.
1213
1214 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1215 then
1216 Set_Etype (T_Name, T_Name);
1217 Init_Size_Align (T_Name);
1218 Set_Directly_Designated_Type (T_Name,
1219 Scope (Defining_Identifier (F)));
1220 End_Scope;
1221 return;
1222 end if;
1223
1224 while Present (F) loop
1225 if No (Parent (Defining_Identifier (F))) then
1226 Set_Parent (Defining_Identifier (F), F);
1227 end if;
1228
1229 Next (F);
1230 end loop;
1231 end;
1232
1233 Process_Formals (Formals, Parent (T_Def));
1234
1235 -- Special test 2) End_Scope requires that the parent pointer be set
1236 -- to something reasonable, but Itypes don't have parent pointers. So
1237 -- we set it and then unset it ???
1238
1239 Set_Parent (Desig_Type, T_Name);
1240 End_Scope;
1241 Set_Parent (Desig_Type, Empty);
1242 end if;
1243
1244 -- Check for premature usage of the type being defined
1245
1246 Check_For_Premature_Usage (T_Def);
1247
1248 -- The return type and/or any parameter type may be incomplete. Mark the
1249 -- subprogram_type as depending on the incomplete type, so that it can
1250 -- be updated when the full type declaration is seen. This only applies
1251 -- to incomplete types declared in some enclosing scope, not to limited
1252 -- views from other packages.
1253
1254 -- Prior to Ada 2012, access to functions can only have in_parameters.
1255
1256 if Present (Formals) then
1257 Formal := First_Formal (Desig_Type);
1258 while Present (Formal) loop
1259 if Ekind (Formal) /= E_In_Parameter
1260 and then Nkind (T_Def) = N_Access_Function_Definition
1261 and then Ada_Version < Ada_2012
1262 then
1263 Error_Msg_N ("functions can only have IN parameters", Formal);
1264 end if;
1265
1266 if Ekind (Etype (Formal)) = E_Incomplete_Type
1267 and then In_Open_Scopes (Scope (Etype (Formal)))
1268 then
1269 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1270 Set_Has_Delayed_Freeze (Desig_Type);
1271 end if;
1272
1273 Next_Formal (Formal);
1274 end loop;
1275 end if;
1276
1277 -- Check whether an indirect call without actuals may be possible. This
1278 -- is used when resolving calls whose result is then indexed.
1279
1280 May_Need_Actuals (Desig_Type);
1281
1282 -- If the return type is incomplete, this is legal as long as the type
1283 -- is declared in the current scope and will be completed in it (rather
1284 -- than being part of limited view).
1285
1286 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1287 and then not Has_Delayed_Freeze (Desig_Type)
1288 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1289 then
1290 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1291 Set_Has_Delayed_Freeze (Desig_Type);
1292 end if;
1293
1294 Check_Delayed_Subprogram (Desig_Type);
1295
1296 if Protected_Present (T_Def) then
1297 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1298 Set_Convention (Desig_Type, Convention_Protected);
1299 else
1300 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1301 end if;
1302
1303 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1304
1305 Set_Etype (T_Name, T_Name);
1306 Init_Size_Align (T_Name);
1307 Set_Directly_Designated_Type (T_Name, Desig_Type);
1308
1309 Generate_Reference_To_Formals (T_Name);
1310
1311 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1312
1313 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1314
1315 Check_Restriction (No_Access_Subprograms, T_Def);
1316 end Access_Subprogram_Declaration;
1317
1318 ----------------------------
1319 -- Access_Type_Declaration --
1320 ----------------------------
1321
1322 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1323 P : constant Node_Id := Parent (Def);
1324 S : constant Node_Id := Subtype_Indication (Def);
1325
1326 Full_Desig : Entity_Id;
1327
1328 begin
1329 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1330
1331 -- Check for permissible use of incomplete type
1332
1333 if Nkind (S) /= N_Subtype_Indication then
1334 Analyze (S);
1335
1336 if Present (Entity (S))
1337 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1338 then
1339 Set_Directly_Designated_Type (T, Entity (S));
1340
1341 -- If the designated type is a limited view, we cannot tell if
1342 -- the full view contains tasks, and there is no way to handle
1343 -- that full view in a client. We create a master entity for the
1344 -- scope, which will be used when a client determines that one
1345 -- is needed.
1346
1347 if From_Limited_With (Entity (S))
1348 and then not Is_Class_Wide_Type (Entity (S))
1349 then
1350 Set_Ekind (T, E_Access_Type);
1351 Build_Master_Entity (T);
1352 Build_Master_Renaming (T);
1353 end if;
1354
1355 else
1356 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1357 end if;
1358
1359 -- If the access definition is of the form: ACCESS NOT NULL ..
1360 -- the subtype indication must be of an access type. Create
1361 -- a null-excluding subtype of it.
1362
1363 if Null_Excluding_Subtype (Def) then
1364 if not Is_Access_Type (Entity (S)) then
1365 Error_Msg_N ("null exclusion must apply to access type", Def);
1366
1367 else
1368 declare
1369 Loc : constant Source_Ptr := Sloc (S);
1370 Decl : Node_Id;
1371 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1372
1373 begin
1374 Decl :=
1375 Make_Subtype_Declaration (Loc,
1376 Defining_Identifier => Nam,
1377 Subtype_Indication =>
1378 New_Occurrence_Of (Entity (S), Loc));
1379 Set_Null_Exclusion_Present (Decl);
1380 Insert_Before (Parent (Def), Decl);
1381 Analyze (Decl);
1382 Set_Entity (S, Nam);
1383 end;
1384 end if;
1385 end if;
1386
1387 else
1388 Set_Directly_Designated_Type (T,
1389 Process_Subtype (S, P, T, 'P'));
1390 end if;
1391
1392 if All_Present (Def) or Constant_Present (Def) then
1393 Set_Ekind (T, E_General_Access_Type);
1394 else
1395 Set_Ekind (T, E_Access_Type);
1396 end if;
1397
1398 Full_Desig := Designated_Type (T);
1399
1400 if Base_Type (Full_Desig) = T then
1401 Error_Msg_N ("access type cannot designate itself", S);
1402
1403 -- In Ada 2005, the type may have a limited view through some unit in
1404 -- its own context, allowing the following circularity that cannot be
1405 -- detected earlier.
1406
1407 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1408 then
1409 Error_Msg_N
1410 ("access type cannot designate its own class-wide type", S);
1411
1412 -- Clean up indication of tagged status to prevent cascaded errors
1413
1414 Set_Is_Tagged_Type (T, False);
1415 end if;
1416
1417 Set_Etype (T, T);
1418
1419 -- If the type has appeared already in a with_type clause, it is frozen
1420 -- and the pointer size is already set. Else, initialize.
1421
1422 if not From_Limited_With (T) then
1423 Init_Size_Align (T);
1424 end if;
1425
1426 -- Note that Has_Task is always false, since the access type itself
1427 -- is not a task type. See Einfo for more description on this point.
1428 -- Exactly the same consideration applies to Has_Controlled_Component
1429 -- and to Has_Protected.
1430
1431 Set_Has_Task (T, False);
1432 Set_Has_Protected (T, False);
1433 Set_Has_Timing_Event (T, False);
1434 Set_Has_Controlled_Component (T, False);
1435
1436 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1437 -- problems where an incomplete view of this entity has been previously
1438 -- established by a limited with and an overlaid version of this field
1439 -- (Stored_Constraint) was initialized for the incomplete view.
1440
1441 -- This reset is performed in most cases except where the access type
1442 -- has been created for the purposes of allocating or deallocating a
1443 -- build-in-place object. Such access types have explicitly set pools
1444 -- and finalization masters.
1445
1446 if No (Associated_Storage_Pool (T)) then
1447 Set_Finalization_Master (T, Empty);
1448 end if;
1449
1450 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1451 -- attributes
1452
1453 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1454 Set_Is_Access_Constant (T, Constant_Present (Def));
1455 end Access_Type_Declaration;
1456
1457 ----------------------------------
1458 -- Add_Interface_Tag_Components --
1459 ----------------------------------
1460
1461 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1462 Loc : constant Source_Ptr := Sloc (N);
1463 L : List_Id;
1464 Last_Tag : Node_Id;
1465
1466 procedure Add_Tag (Iface : Entity_Id);
1467 -- Add tag for one of the progenitor interfaces
1468
1469 -------------
1470 -- Add_Tag --
1471 -------------
1472
1473 procedure Add_Tag (Iface : Entity_Id) is
1474 Decl : Node_Id;
1475 Def : Node_Id;
1476 Tag : Entity_Id;
1477 Offset : Entity_Id;
1478
1479 begin
1480 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1481
1482 -- This is a reasonable place to propagate predicates
1483
1484 if Has_Predicates (Iface) then
1485 Set_Has_Predicates (Typ);
1486 end if;
1487
1488 Def :=
1489 Make_Component_Definition (Loc,
1490 Aliased_Present => True,
1491 Subtype_Indication =>
1492 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1493
1494 Tag := Make_Temporary (Loc, 'V');
1495
1496 Decl :=
1497 Make_Component_Declaration (Loc,
1498 Defining_Identifier => Tag,
1499 Component_Definition => Def);
1500
1501 Analyze_Component_Declaration (Decl);
1502
1503 Set_Analyzed (Decl);
1504 Set_Ekind (Tag, E_Component);
1505 Set_Is_Tag (Tag);
1506 Set_Is_Aliased (Tag);
1507 Set_Related_Type (Tag, Iface);
1508 Init_Component_Location (Tag);
1509
1510 pragma Assert (Is_Frozen (Iface));
1511
1512 Set_DT_Entry_Count (Tag,
1513 DT_Entry_Count (First_Entity (Iface)));
1514
1515 if No (Last_Tag) then
1516 Prepend (Decl, L);
1517 else
1518 Insert_After (Last_Tag, Decl);
1519 end if;
1520
1521 Last_Tag := Decl;
1522
1523 -- If the ancestor has discriminants we need to give special support
1524 -- to store the offset_to_top value of the secondary dispatch tables.
1525 -- For this purpose we add a supplementary component just after the
1526 -- field that contains the tag associated with each secondary DT.
1527
1528 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1529 Def :=
1530 Make_Component_Definition (Loc,
1531 Subtype_Indication =>
1532 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1533
1534 Offset := Make_Temporary (Loc, 'V');
1535
1536 Decl :=
1537 Make_Component_Declaration (Loc,
1538 Defining_Identifier => Offset,
1539 Component_Definition => Def);
1540
1541 Analyze_Component_Declaration (Decl);
1542
1543 Set_Analyzed (Decl);
1544 Set_Ekind (Offset, E_Component);
1545 Set_Is_Aliased (Offset);
1546 Set_Related_Type (Offset, Iface);
1547 Init_Component_Location (Offset);
1548 Insert_After (Last_Tag, Decl);
1549 Last_Tag := Decl;
1550 end if;
1551 end Add_Tag;
1552
1553 -- Local variables
1554
1555 Elmt : Elmt_Id;
1556 Ext : Node_Id;
1557 Comp : Node_Id;
1558
1559 -- Start of processing for Add_Interface_Tag_Components
1560
1561 begin
1562 if not RTE_Available (RE_Interface_Tag) then
1563 Error_Msg
1564 ("(Ada 2005) interface types not supported by this run-time!",
1565 Sloc (N));
1566 return;
1567 end if;
1568
1569 if Ekind (Typ) /= E_Record_Type
1570 or else (Is_Concurrent_Record_Type (Typ)
1571 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1572 or else (not Is_Concurrent_Record_Type (Typ)
1573 and then No (Interfaces (Typ))
1574 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1575 then
1576 return;
1577 end if;
1578
1579 -- Find the current last tag
1580
1581 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1582 Ext := Record_Extension_Part (Type_Definition (N));
1583 else
1584 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1585 Ext := Type_Definition (N);
1586 end if;
1587
1588 Last_Tag := Empty;
1589
1590 if not (Present (Component_List (Ext))) then
1591 Set_Null_Present (Ext, False);
1592 L := New_List;
1593 Set_Component_List (Ext,
1594 Make_Component_List (Loc,
1595 Component_Items => L,
1596 Null_Present => False));
1597 else
1598 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1599 L := Component_Items
1600 (Component_List
1601 (Record_Extension_Part
1602 (Type_Definition (N))));
1603 else
1604 L := Component_Items
1605 (Component_List
1606 (Type_Definition (N)));
1607 end if;
1608
1609 -- Find the last tag component
1610
1611 Comp := First (L);
1612 while Present (Comp) loop
1613 if Nkind (Comp) = N_Component_Declaration
1614 and then Is_Tag (Defining_Identifier (Comp))
1615 then
1616 Last_Tag := Comp;
1617 end if;
1618
1619 Next (Comp);
1620 end loop;
1621 end if;
1622
1623 -- At this point L references the list of components and Last_Tag
1624 -- references the current last tag (if any). Now we add the tag
1625 -- corresponding with all the interfaces that are not implemented
1626 -- by the parent.
1627
1628 if Present (Interfaces (Typ)) then
1629 Elmt := First_Elmt (Interfaces (Typ));
1630 while Present (Elmt) loop
1631 Add_Tag (Node (Elmt));
1632 Next_Elmt (Elmt);
1633 end loop;
1634 end if;
1635 end Add_Interface_Tag_Components;
1636
1637 -------------------------------------
1638 -- Add_Internal_Interface_Entities --
1639 -------------------------------------
1640
1641 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1642 Elmt : Elmt_Id;
1643 Iface : Entity_Id;
1644 Iface_Elmt : Elmt_Id;
1645 Iface_Prim : Entity_Id;
1646 Ifaces_List : Elist_Id;
1647 New_Subp : Entity_Id := Empty;
1648 Prim : Entity_Id;
1649 Restore_Scope : Boolean := False;
1650
1651 begin
1652 pragma Assert (Ada_Version >= Ada_2005
1653 and then Is_Record_Type (Tagged_Type)
1654 and then Is_Tagged_Type (Tagged_Type)
1655 and then Has_Interfaces (Tagged_Type)
1656 and then not Is_Interface (Tagged_Type));
1657
1658 -- Ensure that the internal entities are added to the scope of the type
1659
1660 if Scope (Tagged_Type) /= Current_Scope then
1661 Push_Scope (Scope (Tagged_Type));
1662 Restore_Scope := True;
1663 end if;
1664
1665 Collect_Interfaces (Tagged_Type, Ifaces_List);
1666
1667 Iface_Elmt := First_Elmt (Ifaces_List);
1668 while Present (Iface_Elmt) loop
1669 Iface := Node (Iface_Elmt);
1670
1671 -- Originally we excluded here from this processing interfaces that
1672 -- are parents of Tagged_Type because their primitives are located
1673 -- in the primary dispatch table (and hence no auxiliary internal
1674 -- entities are required to handle secondary dispatch tables in such
1675 -- case). However, these auxiliary entities are also required to
1676 -- handle derivations of interfaces in formals of generics (see
1677 -- Derive_Subprograms).
1678
1679 Elmt := First_Elmt (Primitive_Operations (Iface));
1680 while Present (Elmt) loop
1681 Iface_Prim := Node (Elmt);
1682
1683 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1684 Prim :=
1685 Find_Primitive_Covering_Interface
1686 (Tagged_Type => Tagged_Type,
1687 Iface_Prim => Iface_Prim);
1688
1689 if No (Prim) and then Serious_Errors_Detected > 0 then
1690 goto Continue;
1691 end if;
1692
1693 pragma Assert (Present (Prim));
1694
1695 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1696 -- differs from the name of the interface primitive then it is
1697 -- a private primitive inherited from a parent type. In such
1698 -- case, given that Tagged_Type covers the interface, the
1699 -- inherited private primitive becomes visible. For such
1700 -- purpose we add a new entity that renames the inherited
1701 -- private primitive.
1702
1703 if Chars (Prim) /= Chars (Iface_Prim) then
1704 pragma Assert (Has_Suffix (Prim, 'P'));
1705 Derive_Subprogram
1706 (New_Subp => New_Subp,
1707 Parent_Subp => Iface_Prim,
1708 Derived_Type => Tagged_Type,
1709 Parent_Type => Iface);
1710 Set_Alias (New_Subp, Prim);
1711 Set_Is_Abstract_Subprogram
1712 (New_Subp, Is_Abstract_Subprogram (Prim));
1713 end if;
1714
1715 Derive_Subprogram
1716 (New_Subp => New_Subp,
1717 Parent_Subp => Iface_Prim,
1718 Derived_Type => Tagged_Type,
1719 Parent_Type => Iface);
1720
1721 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1722 -- associated with interface types. These entities are
1723 -- only registered in the list of primitives of its
1724 -- corresponding tagged type because they are only used
1725 -- to fill the contents of the secondary dispatch tables.
1726 -- Therefore they are removed from the homonym chains.
1727
1728 Set_Is_Hidden (New_Subp);
1729 Set_Is_Internal (New_Subp);
1730 Set_Alias (New_Subp, Prim);
1731 Set_Is_Abstract_Subprogram
1732 (New_Subp, Is_Abstract_Subprogram (Prim));
1733 Set_Interface_Alias (New_Subp, Iface_Prim);
1734
1735 -- If the returned type is an interface then propagate it to
1736 -- the returned type. Needed by the thunk to generate the code
1737 -- which displaces "this" to reference the corresponding
1738 -- secondary dispatch table in the returned object.
1739
1740 if Is_Interface (Etype (Iface_Prim)) then
1741 Set_Etype (New_Subp, Etype (Iface_Prim));
1742 end if;
1743
1744 -- Internal entities associated with interface types are only
1745 -- registered in the list of primitives of the tagged type.
1746 -- They are only used to fill the contents of the secondary
1747 -- dispatch tables. Therefore they are not needed in the
1748 -- homonym chains.
1749
1750 Remove_Homonym (New_Subp);
1751
1752 -- Hidden entities associated with interfaces must have set
1753 -- the Has_Delay_Freeze attribute to ensure that, in case
1754 -- of locally defined tagged types (or compiling with static
1755 -- dispatch tables generation disabled) the corresponding
1756 -- entry of the secondary dispatch table is filled when such
1757 -- an entity is frozen. This is an expansion activity that must
1758 -- be suppressed for ASIS because it leads to gigi elaboration
1759 -- issues in annotate mode.
1760
1761 if not ASIS_Mode then
1762 Set_Has_Delayed_Freeze (New_Subp);
1763 end if;
1764 end if;
1765
1766 <<Continue>>
1767 Next_Elmt (Elmt);
1768 end loop;
1769
1770 Next_Elmt (Iface_Elmt);
1771 end loop;
1772
1773 if Restore_Scope then
1774 Pop_Scope;
1775 end if;
1776 end Add_Internal_Interface_Entities;
1777
1778 -----------------------------------
1779 -- Analyze_Component_Declaration --
1780 -----------------------------------
1781
1782 procedure Analyze_Component_Declaration (N : Node_Id) is
1783 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1784 Id : constant Entity_Id := Defining_Identifier (N);
1785 E : constant Node_Id := Expression (N);
1786 Typ : constant Node_Id :=
1787 Subtype_Indication (Component_Definition (N));
1788 T : Entity_Id;
1789 P : Entity_Id;
1790
1791 function Contains_POC (Constr : Node_Id) return Boolean;
1792 -- Determines whether a constraint uses the discriminant of a record
1793 -- type thus becoming a per-object constraint (POC).
1794
1795 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1796 -- Typ is the type of the current component, check whether this type is
1797 -- a limited type. Used to validate declaration against that of
1798 -- enclosing record.
1799
1800 ------------------
1801 -- Contains_POC --
1802 ------------------
1803
1804 function Contains_POC (Constr : Node_Id) return Boolean is
1805 begin
1806 -- Prevent cascaded errors
1807
1808 if Error_Posted (Constr) then
1809 return False;
1810 end if;
1811
1812 case Nkind (Constr) is
1813 when N_Attribute_Reference =>
1814 return Attribute_Name (Constr) = Name_Access
1815 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1816
1817 when N_Discriminant_Association =>
1818 return Denotes_Discriminant (Expression (Constr));
1819
1820 when N_Identifier =>
1821 return Denotes_Discriminant (Constr);
1822
1823 when N_Index_Or_Discriminant_Constraint =>
1824 declare
1825 IDC : Node_Id;
1826
1827 begin
1828 IDC := First (Constraints (Constr));
1829 while Present (IDC) loop
1830
1831 -- One per-object constraint is sufficient
1832
1833 if Contains_POC (IDC) then
1834 return True;
1835 end if;
1836
1837 Next (IDC);
1838 end loop;
1839
1840 return False;
1841 end;
1842
1843 when N_Range =>
1844 return Denotes_Discriminant (Low_Bound (Constr))
1845 or else
1846 Denotes_Discriminant (High_Bound (Constr));
1847
1848 when N_Range_Constraint =>
1849 return Denotes_Discriminant (Range_Expression (Constr));
1850
1851 when others =>
1852 return False;
1853 end case;
1854 end Contains_POC;
1855
1856 ----------------------
1857 -- Is_Known_Limited --
1858 ----------------------
1859
1860 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1861 P : constant Entity_Id := Etype (Typ);
1862 R : constant Entity_Id := Root_Type (Typ);
1863
1864 begin
1865 if Is_Limited_Record (Typ) then
1866 return True;
1867
1868 -- If the root type is limited (and not a limited interface)
1869 -- so is the current type
1870
1871 elsif Is_Limited_Record (R)
1872 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1873 then
1874 return True;
1875
1876 -- Else the type may have a limited interface progenitor, but a
1877 -- limited record parent.
1878
1879 elsif R /= P and then Is_Limited_Record (P) then
1880 return True;
1881
1882 else
1883 return False;
1884 end if;
1885 end Is_Known_Limited;
1886
1887 -- Start of processing for Analyze_Component_Declaration
1888
1889 begin
1890 Generate_Definition (Id);
1891 Enter_Name (Id);
1892
1893 if Present (Typ) then
1894 T := Find_Type_Of_Object
1895 (Subtype_Indication (Component_Definition (N)), N);
1896
1897 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1898 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1899 end if;
1900
1901 -- Ada 2005 (AI-230): Access Definition case
1902
1903 else
1904 pragma Assert (Present
1905 (Access_Definition (Component_Definition (N))));
1906
1907 T := Access_Definition
1908 (Related_Nod => N,
1909 N => Access_Definition (Component_Definition (N)));
1910 Set_Is_Local_Anonymous_Access (T);
1911
1912 -- Ada 2005 (AI-254)
1913
1914 if Present (Access_To_Subprogram_Definition
1915 (Access_Definition (Component_Definition (N))))
1916 and then Protected_Present (Access_To_Subprogram_Definition
1917 (Access_Definition
1918 (Component_Definition (N))))
1919 then
1920 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1921 end if;
1922 end if;
1923
1924 -- If the subtype is a constrained subtype of the enclosing record,
1925 -- (which must have a partial view) the back-end does not properly
1926 -- handle the recursion. Rewrite the component declaration with an
1927 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1928 -- the tree directly because side effects have already been removed from
1929 -- discriminant constraints.
1930
1931 if Ekind (T) = E_Access_Subtype
1932 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1933 and then Comes_From_Source (T)
1934 and then Nkind (Parent (T)) = N_Subtype_Declaration
1935 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1936 then
1937 Rewrite
1938 (Subtype_Indication (Component_Definition (N)),
1939 New_Copy_Tree (Subtype_Indication (Parent (T))));
1940 T := Find_Type_Of_Object
1941 (Subtype_Indication (Component_Definition (N)), N);
1942 end if;
1943
1944 -- If the component declaration includes a default expression, then we
1945 -- check that the component is not of a limited type (RM 3.7(5)),
1946 -- and do the special preanalysis of the expression (see section on
1947 -- "Handling of Default and Per-Object Expressions" in the spec of
1948 -- package Sem).
1949
1950 if Present (E) then
1951 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1952 Preanalyze_Default_Expression (E, T);
1953 Check_Initialization (T, E);
1954
1955 if Ada_Version >= Ada_2005
1956 and then Ekind (T) = E_Anonymous_Access_Type
1957 and then Etype (E) /= Any_Type
1958 then
1959 -- Check RM 3.9.2(9): "if the expected type for an expression is
1960 -- an anonymous access-to-specific tagged type, then the object
1961 -- designated by the expression shall not be dynamically tagged
1962 -- unless it is a controlling operand in a call on a dispatching
1963 -- operation"
1964
1965 if Is_Tagged_Type (Directly_Designated_Type (T))
1966 and then
1967 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1968 and then
1969 Ekind (Directly_Designated_Type (Etype (E))) =
1970 E_Class_Wide_Type
1971 then
1972 Error_Msg_N
1973 ("access to specific tagged type required (RM 3.9.2(9))", E);
1974 end if;
1975
1976 -- (Ada 2005: AI-230): Accessibility check for anonymous
1977 -- components
1978
1979 if Type_Access_Level (Etype (E)) >
1980 Deepest_Type_Access_Level (T)
1981 then
1982 Error_Msg_N
1983 ("expression has deeper access level than component " &
1984 "(RM 3.10.2 (12.2))", E);
1985 end if;
1986
1987 -- The initialization expression is a reference to an access
1988 -- discriminant. The type of the discriminant is always deeper
1989 -- than any access type.
1990
1991 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1992 and then Is_Entity_Name (E)
1993 and then Ekind (Entity (E)) = E_In_Parameter
1994 and then Present (Discriminal_Link (Entity (E)))
1995 then
1996 Error_Msg_N
1997 ("discriminant has deeper accessibility level than target",
1998 E);
1999 end if;
2000 end if;
2001 end if;
2002
2003 -- The parent type may be a private view with unknown discriminants,
2004 -- and thus unconstrained. Regular components must be constrained.
2005
2006 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2007 if Is_Class_Wide_Type (T) then
2008 Error_Msg_N
2009 ("class-wide subtype with unknown discriminants" &
2010 " in component declaration",
2011 Subtype_Indication (Component_Definition (N)));
2012 else
2013 Error_Msg_N
2014 ("unconstrained subtype in component declaration",
2015 Subtype_Indication (Component_Definition (N)));
2016 end if;
2017
2018 -- Components cannot be abstract, except for the special case of
2019 -- the _Parent field (case of extending an abstract tagged type)
2020
2021 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2022 Error_Msg_N ("type of a component cannot be abstract", N);
2023 end if;
2024
2025 Set_Etype (Id, T);
2026 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2027
2028 -- The component declaration may have a per-object constraint, set
2029 -- the appropriate flag in the defining identifier of the subtype.
2030
2031 if Present (Subtype_Indication (Component_Definition (N))) then
2032 declare
2033 Sindic : constant Node_Id :=
2034 Subtype_Indication (Component_Definition (N));
2035 begin
2036 if Nkind (Sindic) = N_Subtype_Indication
2037 and then Present (Constraint (Sindic))
2038 and then Contains_POC (Constraint (Sindic))
2039 then
2040 Set_Has_Per_Object_Constraint (Id);
2041 end if;
2042 end;
2043 end if;
2044
2045 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2046 -- out some static checks.
2047
2048 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2049 Null_Exclusion_Static_Checks (N);
2050 end if;
2051
2052 -- If this component is private (or depends on a private type), flag the
2053 -- record type to indicate that some operations are not available.
2054
2055 P := Private_Component (T);
2056
2057 if Present (P) then
2058
2059 -- Check for circular definitions
2060
2061 if P = Any_Type then
2062 Set_Etype (Id, Any_Type);
2063
2064 -- There is a gap in the visibility of operations only if the
2065 -- component type is not defined in the scope of the record type.
2066
2067 elsif Scope (P) = Scope (Current_Scope) then
2068 null;
2069
2070 elsif Is_Limited_Type (P) then
2071 Set_Is_Limited_Composite (Current_Scope);
2072
2073 else
2074 Set_Is_Private_Composite (Current_Scope);
2075 end if;
2076 end if;
2077
2078 if P /= Any_Type
2079 and then Is_Limited_Type (T)
2080 and then Chars (Id) /= Name_uParent
2081 and then Is_Tagged_Type (Current_Scope)
2082 then
2083 if Is_Derived_Type (Current_Scope)
2084 and then not Is_Known_Limited (Current_Scope)
2085 then
2086 Error_Msg_N
2087 ("extension of nonlimited type cannot have limited components",
2088 N);
2089
2090 if Is_Interface (Root_Type (Current_Scope)) then
2091 Error_Msg_N
2092 ("\limitedness is not inherited from limited interface", N);
2093 Error_Msg_N ("\add LIMITED to type indication", N);
2094 end if;
2095
2096 Explain_Limited_Type (T, N);
2097 Set_Etype (Id, Any_Type);
2098 Set_Is_Limited_Composite (Current_Scope, False);
2099
2100 elsif not Is_Derived_Type (Current_Scope)
2101 and then not Is_Limited_Record (Current_Scope)
2102 and then not Is_Concurrent_Type (Current_Scope)
2103 then
2104 Error_Msg_N
2105 ("nonlimited tagged type cannot have limited components", N);
2106 Explain_Limited_Type (T, N);
2107 Set_Etype (Id, Any_Type);
2108 Set_Is_Limited_Composite (Current_Scope, False);
2109 end if;
2110 end if;
2111
2112 -- If the component is an unconstrained task or protected type with
2113 -- discriminants, the component and the enclosing record are limited
2114 -- and the component is constrained by its default values. Compute
2115 -- its actual subtype, else it may be allocated the maximum size by
2116 -- the backend, and possibly overflow.
2117
2118 if Is_Concurrent_Type (T)
2119 and then not Is_Constrained (T)
2120 and then Has_Discriminants (T)
2121 and then not Has_Discriminants (Current_Scope)
2122 then
2123 declare
2124 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2125
2126 begin
2127 Set_Etype (Id, Act_T);
2128
2129 -- Rewrite component definition to use the constrained subtype
2130
2131 Rewrite (Component_Definition (N),
2132 Make_Component_Definition (Loc,
2133 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2134 end;
2135 end if;
2136
2137 Set_Original_Record_Component (Id, Id);
2138
2139 if Has_Aspects (N) then
2140 Analyze_Aspect_Specifications (N, Id);
2141 end if;
2142
2143 Analyze_Dimension (N);
2144 end Analyze_Component_Declaration;
2145
2146 --------------------------
2147 -- Analyze_Declarations --
2148 --------------------------
2149
2150 procedure Analyze_Declarations (L : List_Id) is
2151 Decl : Node_Id;
2152
2153 procedure Adjust_Decl;
2154 -- Adjust Decl not to include implicit label declarations, since these
2155 -- have strange Sloc values that result in elaboration check problems.
2156 -- (They have the sloc of the label as found in the source, and that
2157 -- is ahead of the current declarative part).
2158
2159 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2160 -- Create the subprogram bodies which verify the run-time semantics of
2161 -- the pragmas listed below for each elibigle type found in declarative
2162 -- list Decls. The pragmas are:
2163 --
2164 -- Default_Initial_Condition
2165 -- Invariant
2166 -- Type_Invariant
2167 --
2168 -- Context denotes the owner of the declarative list.
2169
2170 procedure Check_Entry_Contracts;
2171 -- Perform a pre-analysis of the pre- and postconditions of an entry
2172 -- declaration. This must be done before full resolution and creation
2173 -- of the parameter block, etc. to catch illegal uses within the
2174 -- contract expression. Full analysis of the expression is done when
2175 -- the contract is processed.
2176
2177 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2178 -- Determine whether Body_Decl denotes the body of a late controlled
2179 -- primitive (either Initialize, Adjust or Finalize). If this is the
2180 -- case, add a proper spec if the body lacks one. The spec is inserted
2181 -- before Body_Decl and immediately analyzed.
2182
2183 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2184 -- Spec_Id is the entity of a package that may define abstract states,
2185 -- and in the case of a child unit, whose ancestors may define abstract
2186 -- states. If the states have partial visible refinement, remove the
2187 -- partial visibility of each constituent at the end of the package
2188 -- spec and body declarations.
2189
2190 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2191 -- Spec_Id is the entity of a package that may define abstract states.
2192 -- If the states have visible refinement, remove the visibility of each
2193 -- constituent at the end of the package body declaration.
2194
2195 procedure Resolve_Aspects;
2196 -- Utility to resolve the expressions of aspects at the end of a list of
2197 -- declarations.
2198
2199 -----------------
2200 -- Adjust_Decl --
2201 -----------------
2202
2203 procedure Adjust_Decl is
2204 begin
2205 while Present (Prev (Decl))
2206 and then Nkind (Decl) = N_Implicit_Label_Declaration
2207 loop
2208 Prev (Decl);
2209 end loop;
2210 end Adjust_Decl;
2211
2212 ----------------------------
2213 -- Build_Assertion_Bodies --
2214 ----------------------------
2215
2216 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2217 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2218 -- Create the subprogram bodies which verify the run-time semantics
2219 -- of the pragmas listed below for type Typ. The pragmas are:
2220 --
2221 -- Default_Initial_Condition
2222 -- Invariant
2223 -- Type_Invariant
2224
2225 -------------------------------------
2226 -- Build_Assertion_Bodies_For_Type --
2227 -------------------------------------
2228
2229 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2230 begin
2231 -- Preanalyze and resolve the Default_Initial_Condition assertion
2232 -- expression at the end of the declarations to catch any errors.
2233
2234 if Has_DIC (Typ) then
2235 Build_DIC_Procedure_Body (Typ);
2236 end if;
2237
2238 if Nkind (Context) = N_Package_Specification then
2239
2240 -- Preanalyze and resolve the invariants of a private type
2241 -- at the end of the visible declarations to catch potential
2242 -- errors. Inherited class-wide invariants are not included
2243 -- because they have already been resolved.
2244
2245 if Decls = Visible_Declarations (Context)
2246 and then Ekind_In (Typ, E_Limited_Private_Type,
2247 E_Private_Type,
2248 E_Record_Type_With_Private)
2249 and then Has_Own_Invariants (Typ)
2250 then
2251 Build_Invariant_Procedure_Body
2252 (Typ => Typ,
2253 Partial_Invariant => True);
2254
2255 -- Preanalyze and resolve the invariants of a private type's
2256 -- full view at the end of the private declarations to catch
2257 -- potential errors.
2258
2259 elsif Decls = Private_Declarations (Context)
2260 and then not Is_Private_Type (Typ)
2261 and then Has_Private_Declaration (Typ)
2262 and then Has_Invariants (Typ)
2263 then
2264 Build_Invariant_Procedure_Body (Typ);
2265 end if;
2266 end if;
2267 end Build_Assertion_Bodies_For_Type;
2268
2269 -- Local variables
2270
2271 Decl : Node_Id;
2272 Decl_Id : Entity_Id;
2273
2274 -- Start of processing for Build_Assertion_Bodies
2275
2276 begin
2277 Decl := First (Decls);
2278 while Present (Decl) loop
2279 if Is_Declaration (Decl) then
2280 Decl_Id := Defining_Entity (Decl);
2281
2282 if Is_Type (Decl_Id) then
2283 Build_Assertion_Bodies_For_Type (Decl_Id);
2284 end if;
2285 end if;
2286
2287 Next (Decl);
2288 end loop;
2289 end Build_Assertion_Bodies;
2290
2291 ---------------------------
2292 -- Check_Entry_Contracts --
2293 ---------------------------
2294
2295 procedure Check_Entry_Contracts is
2296 ASN : Node_Id;
2297 Ent : Entity_Id;
2298 Exp : Node_Id;
2299
2300 begin
2301 Ent := First_Entity (Current_Scope);
2302 while Present (Ent) loop
2303
2304 -- This only concerns entries with pre/postconditions
2305
2306 if Ekind (Ent) = E_Entry
2307 and then Present (Contract (Ent))
2308 and then Present (Pre_Post_Conditions (Contract (Ent)))
2309 then
2310 ASN := Pre_Post_Conditions (Contract (Ent));
2311 Push_Scope (Ent);
2312 Install_Formals (Ent);
2313
2314 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2315 -- is performed on a copy of the pragma expression, to prevent
2316 -- modifying the original expression.
2317
2318 while Present (ASN) loop
2319 if Nkind (ASN) = N_Pragma then
2320 Exp :=
2321 New_Copy_Tree
2322 (Expression
2323 (First (Pragma_Argument_Associations (ASN))));
2324 Set_Parent (Exp, ASN);
2325
2326 -- ??? why not Preanalyze_Assert_Expression
2327
2328 Preanalyze (Exp);
2329 end if;
2330
2331 ASN := Next_Pragma (ASN);
2332 end loop;
2333
2334 End_Scope;
2335 end if;
2336
2337 Next_Entity (Ent);
2338 end loop;
2339 end Check_Entry_Contracts;
2340
2341 --------------------------------------
2342 -- Handle_Late_Controlled_Primitive --
2343 --------------------------------------
2344
2345 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2346 Body_Spec : constant Node_Id := Specification (Body_Decl);
2347 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2348 Loc : constant Source_Ptr := Sloc (Body_Id);
2349 Params : constant List_Id :=
2350 Parameter_Specifications (Body_Spec);
2351 Spec : Node_Id;
2352 Spec_Id : Entity_Id;
2353 Typ : Node_Id;
2354
2355 begin
2356 -- Consider only procedure bodies whose name matches one of the three
2357 -- controlled primitives.
2358
2359 if Nkind (Body_Spec) /= N_Procedure_Specification
2360 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2361 Name_Finalize,
2362 Name_Initialize)
2363 then
2364 return;
2365
2366 -- A controlled primitive must have exactly one formal which is not
2367 -- an anonymous access type.
2368
2369 elsif List_Length (Params) /= 1 then
2370 return;
2371 end if;
2372
2373 Typ := Parameter_Type (First (Params));
2374
2375 if Nkind (Typ) = N_Access_Definition then
2376 return;
2377 end if;
2378
2379 Find_Type (Typ);
2380
2381 -- The type of the formal must be derived from [Limited_]Controlled
2382
2383 if not Is_Controlled (Entity (Typ)) then
2384 return;
2385 end if;
2386
2387 -- Check whether a specification exists for this body. We do not
2388 -- analyze the spec of the body in full, because it will be analyzed
2389 -- again when the body is properly analyzed, and we cannot create
2390 -- duplicate entries in the formals chain. We look for an explicit
2391 -- specification because the body may be an overriding operation and
2392 -- an inherited spec may be present.
2393
2394 Spec_Id := Current_Entity (Body_Id);
2395
2396 while Present (Spec_Id) loop
2397 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2398 and then Scope (Spec_Id) = Current_Scope
2399 and then Present (First_Formal (Spec_Id))
2400 and then No (Next_Formal (First_Formal (Spec_Id)))
2401 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2402 and then Comes_From_Source (Spec_Id)
2403 then
2404 return;
2405 end if;
2406
2407 Spec_Id := Homonym (Spec_Id);
2408 end loop;
2409
2410 -- At this point the body is known to be a late controlled primitive.
2411 -- Generate a matching spec and insert it before the body. Note the
2412 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2413 -- tree in this case.
2414
2415 Spec := Copy_Separate_Tree (Body_Spec);
2416
2417 -- Ensure that the subprogram declaration does not inherit the null
2418 -- indicator from the body as we now have a proper spec/body pair.
2419
2420 Set_Null_Present (Spec, False);
2421
2422 -- Ensure that the freeze node is inserted after the declaration of
2423 -- the primitive since its expansion will freeze the primitive.
2424
2425 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2426
2427 Insert_Before_And_Analyze (Body_Decl, Decl);
2428 end Handle_Late_Controlled_Primitive;
2429
2430 ----------------------------------------
2431 -- Remove_Partial_Visible_Refinements --
2432 ----------------------------------------
2433
2434 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2435 State_Elmt : Elmt_Id;
2436 begin
2437 if Present (Abstract_States (Spec_Id)) then
2438 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2439 while Present (State_Elmt) loop
2440 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2441 Next_Elmt (State_Elmt);
2442 end loop;
2443 end if;
2444
2445 -- For a child unit, also hide the partial state refinement from
2446 -- ancestor packages.
2447
2448 if Is_Child_Unit (Spec_Id) then
2449 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2450 end if;
2451 end Remove_Partial_Visible_Refinements;
2452
2453 --------------------------------
2454 -- Remove_Visible_Refinements --
2455 --------------------------------
2456
2457 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2458 State_Elmt : Elmt_Id;
2459 begin
2460 if Present (Abstract_States (Spec_Id)) then
2461 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2462 while Present (State_Elmt) loop
2463 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2464 Next_Elmt (State_Elmt);
2465 end loop;
2466 end if;
2467 end Remove_Visible_Refinements;
2468
2469 ---------------------
2470 -- Resolve_Aspects --
2471 ---------------------
2472
2473 procedure Resolve_Aspects is
2474 E : Entity_Id;
2475
2476 begin
2477 E := First_Entity (Current_Scope);
2478 while Present (E) loop
2479 Resolve_Aspect_Expressions (E);
2480 Next_Entity (E);
2481 end loop;
2482 end Resolve_Aspects;
2483
2484 -- Local variables
2485
2486 Context : Node_Id := Empty;
2487 Freeze_From : Entity_Id := Empty;
2488 Next_Decl : Node_Id;
2489
2490 Body_Seen : Boolean := False;
2491 -- Flag set when the first body [stub] is encountered
2492
2493 Ignore_Freezing : Boolean;
2494 -- Flag set when deciding to freeze an expression function in the
2495 -- current scope.
2496
2497 -- Start of processing for Analyze_Declarations
2498
2499 begin
2500 if Restriction_Check_Required (SPARK_05) then
2501 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2502 end if;
2503
2504 Decl := First (L);
2505 while Present (Decl) loop
2506
2507 -- Package spec cannot contain a package declaration in SPARK
2508
2509 if Nkind (Decl) = N_Package_Declaration
2510 and then Nkind (Parent (L)) = N_Package_Specification
2511 then
2512 Check_SPARK_05_Restriction
2513 ("package specification cannot contain a package declaration",
2514 Decl);
2515 end if;
2516
2517 -- Complete analysis of declaration
2518
2519 Analyze (Decl);
2520 Next_Decl := Next (Decl);
2521
2522 if No (Freeze_From) then
2523 Freeze_From := First_Entity (Current_Scope);
2524 end if;
2525
2526 -- At the end of a declarative part, freeze remaining entities
2527 -- declared in it. The end of the visible declarations of package
2528 -- specification is not the end of a declarative part if private
2529 -- declarations are present. The end of a package declaration is a
2530 -- freezing point only if it a library package. A task definition or
2531 -- protected type definition is not a freeze point either. Finally,
2532 -- we do not freeze entities in generic scopes, because there is no
2533 -- code generated for them and freeze nodes will be generated for
2534 -- the instance.
2535
2536 -- The end of a package instantiation is not a freeze point, but
2537 -- for now we make it one, because the generic body is inserted
2538 -- (currently) immediately after. Generic instantiations will not
2539 -- be a freeze point once delayed freezing of bodies is implemented.
2540 -- (This is needed in any case for early instantiations ???).
2541
2542 if No (Next_Decl) then
2543 if Nkind (Parent (L)) = N_Component_List then
2544 null;
2545
2546 elsif Nkind_In (Parent (L), N_Protected_Definition,
2547 N_Task_Definition)
2548 then
2549 Check_Entry_Contracts;
2550
2551 elsif Nkind (Parent (L)) /= N_Package_Specification then
2552 if Nkind (Parent (L)) = N_Package_Body then
2553 Freeze_From := First_Entity (Current_Scope);
2554 end if;
2555
2556 -- There may have been several freezing points previously,
2557 -- for example object declarations or subprogram bodies, but
2558 -- at the end of a declarative part we check freezing from
2559 -- the beginning, even though entities may already be frozen,
2560 -- in order to perform visibility checks on delayed aspects.
2561
2562 Adjust_Decl;
2563 Freeze_All (First_Entity (Current_Scope), Decl);
2564 Freeze_From := Last_Entity (Current_Scope);
2565
2566 -- Current scope is a package specification
2567
2568 elsif Scope (Current_Scope) /= Standard_Standard
2569 and then not Is_Child_Unit (Current_Scope)
2570 and then No (Generic_Parent (Parent (L)))
2571 then
2572 -- This is needed in all cases to catch visibility errors in
2573 -- aspect expressions, but several large user tests are now
2574 -- rejected. Pending notification we restrict this call to
2575 -- ASIS mode.
2576
2577 if False and then ASIS_Mode then -- ????
2578 Resolve_Aspects;
2579 end if;
2580
2581 elsif L /= Visible_Declarations (Parent (L))
2582 or else No (Private_Declarations (Parent (L)))
2583 or else Is_Empty_List (Private_Declarations (Parent (L)))
2584 then
2585 Adjust_Decl;
2586
2587 -- End of a package declaration
2588
2589 -- In compilation mode the expansion of freeze node takes care
2590 -- of resolving expressions of all aspects in the list. In ASIS
2591 -- mode this must be done explicitly.
2592
2593 if ASIS_Mode
2594 and then Scope (Current_Scope) = Standard_Standard
2595 then
2596 Resolve_Aspects;
2597 end if;
2598
2599 -- This is a freeze point because it is the end of a
2600 -- compilation unit.
2601
2602 Freeze_All (First_Entity (Current_Scope), Decl);
2603 Freeze_From := Last_Entity (Current_Scope);
2604
2605 -- At the end of the visible declarations the expressions in
2606 -- aspects of all entities declared so far must be resolved.
2607 -- The entities themselves might be frozen later, and the
2608 -- generated pragmas and attribute definition clauses analyzed
2609 -- in full at that point, but name resolution must take place
2610 -- now.
2611 -- In addition to being the proper semantics, this is mandatory
2612 -- within generic units, because global name capture requires
2613 -- those expressions to be analyzed, given that the generated
2614 -- pragmas do not appear in the original generic tree.
2615
2616 elsif Serious_Errors_Detected = 0 then
2617 Resolve_Aspects;
2618 end if;
2619
2620 -- If next node is a body then freeze all types before the body.
2621 -- An exception occurs for some expander-generated bodies. If these
2622 -- are generated at places where in general language rules would not
2623 -- allow a freeze point, then we assume that the expander has
2624 -- explicitly checked that all required types are properly frozen,
2625 -- and we do not cause general freezing here. This special circuit
2626 -- is used when the encountered body is marked as having already
2627 -- been analyzed.
2628
2629 -- In all other cases (bodies that come from source, and expander
2630 -- generated bodies that have not been analyzed yet), freeze all
2631 -- types now. Note that in the latter case, the expander must take
2632 -- care to attach the bodies at a proper place in the tree so as to
2633 -- not cause unwanted freezing at that point.
2634
2635 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2636
2637 -- Check for an edge case that may cause premature freezing of a
2638 -- private type.
2639
2640 -- If there is an type which depends on a private type from an
2641 -- enclosing package that is in the same scope as a non-completing
2642 -- expression function then we cannot freeze here.
2643
2644 Ignore_Freezing := False;
2645
2646 if Nkind (Next_Decl) = N_Subprogram_Body
2647 and then Was_Expression_Function (Next_Decl)
2648 and then not Is_Compilation_Unit (Current_Scope)
2649 and then not Is_Generic_Instance (Current_Scope)
2650 then
2651 -- Loop through all entities in the current scope to identify
2652 -- an instance of the edge case outlined above and ignore
2653 -- freezing if it is detected.
2654
2655 declare
2656 Curr : Entity_Id := First_Entity (Current_Scope);
2657 begin
2658 loop
2659 if Nkind (Curr) in N_Entity
2660 and then Depends_On_Private (Curr)
2661 then
2662 Ignore_Freezing := True;
2663 exit;
2664 end if;
2665
2666 exit when Last_Entity (Current_Scope) = Curr;
2667 Curr := Next_Entity (Curr);
2668 end loop;
2669 end;
2670 end if;
2671
2672 if not Ignore_Freezing then
2673
2674 -- When a controlled type is frozen, the expander generates
2675 -- stream and controlled-type support routines. If the freeze
2676 -- is caused by the stand-alone body of Initialize, Adjust, or
2677 -- Finalize, the expander will end up using the wrong version
2678 -- of these routines, as the body has not been processed yet.
2679 -- To remedy this, detect a late controlled primitive and
2680 -- create a proper spec for it. This ensures that the primitive
2681 -- will override its inherited counterpart before the freeze
2682 -- takes place.
2683
2684 -- If the declaration we just processed is a body, do not
2685 -- attempt to examine Next_Decl as the late primitive idiom can
2686 -- only apply to the first encountered body.
2687
2688 -- The spec of the late primitive is not generated in ASIS mode
2689 -- to ensure a consistent list of primitives that indicates the
2690 -- true semantic structure of the program (which is not
2691 -- relevant when generating executable code).
2692
2693 -- ??? A cleaner approach may be possible and/or this solution
2694 -- could be extended to general-purpose late primitives, TBD.
2695
2696 if not ASIS_Mode
2697 and then not Body_Seen
2698 and then not Is_Body (Decl)
2699 then
2700 Body_Seen := True;
2701
2702 if Nkind (Next_Decl) = N_Subprogram_Body then
2703 Handle_Late_Controlled_Primitive (Next_Decl);
2704 end if;
2705 end if;
2706
2707 Adjust_Decl;
2708
2709 -- The generated body of an expression function does not
2710 -- freeze, unless it is a completion, in which case only the
2711 -- expression itself freezes. This is handled when the body
2712 -- itself is analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2713
2714 Freeze_All (Freeze_From, Decl);
2715 Freeze_From := Last_Entity (Current_Scope);
2716 end if;
2717 end if;
2718
2719 Decl := Next_Decl;
2720 end loop;
2721
2722 -- Post-freezing actions
2723
2724 if Present (L) then
2725 Context := Parent (L);
2726
2727 -- Analyze the contracts of packages and their bodies
2728
2729 if Nkind (Context) = N_Package_Specification then
2730
2731 -- When a package has private declarations, its contract must be
2732 -- analyzed at the end of the said declarations. This way both the
2733 -- analysis and freeze actions are properly synchronized in case
2734 -- of private type use within the contract.
2735
2736 if L = Private_Declarations (Context) then
2737 Analyze_Package_Contract (Defining_Entity (Context));
2738
2739 -- Otherwise the contract is analyzed at the end of the visible
2740 -- declarations.
2741
2742 elsif L = Visible_Declarations (Context)
2743 and then No (Private_Declarations (Context))
2744 then
2745 Analyze_Package_Contract (Defining_Entity (Context));
2746 end if;
2747
2748 elsif Nkind (Context) = N_Package_Body then
2749 Analyze_Package_Body_Contract (Defining_Entity (Context));
2750 end if;
2751
2752 -- Analyze the contracts of various constructs now due to the delayed
2753 -- visibility needs of their aspects and pragmas.
2754
2755 Analyze_Contracts (L);
2756
2757 if Nkind (Context) = N_Package_Body then
2758
2759 -- Ensure that all abstract states and objects declared in the
2760 -- state space of a package body are utilized as constituents.
2761
2762 Check_Unused_Body_States (Defining_Entity (Context));
2763
2764 -- State refinements are visible up to the end of the package body
2765 -- declarations. Hide the state refinements from visibility to
2766 -- restore the original state conditions.
2767
2768 Remove_Visible_Refinements (Corresponding_Spec (Context));
2769 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2770
2771 elsif Nkind (Context) = N_Package_Declaration then
2772
2773 -- Partial state refinements are visible up to the end of the
2774 -- package spec declarations. Hide the partial state refinements
2775 -- from visibility to restore the original state conditions.
2776
2777 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2778 end if;
2779
2780 -- Verify that all abstract states found in any package declared in
2781 -- the input declarative list have proper refinements. The check is
2782 -- performed only when the context denotes a block, entry, package,
2783 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2784
2785 Check_State_Refinements (Context);
2786
2787 -- Create the subprogram bodies which verify the run-time semantics
2788 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2789 -- types within the current declarative list. This ensures that all
2790 -- assertion expressions are preanalyzed and resolved at the end of
2791 -- the declarative part. Note that the resolution happens even when
2792 -- freezing does not take place.
2793
2794 Build_Assertion_Bodies (L, Context);
2795 end if;
2796 end Analyze_Declarations;
2797
2798 -----------------------------------
2799 -- Analyze_Full_Type_Declaration --
2800 -----------------------------------
2801
2802 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2803 Def : constant Node_Id := Type_Definition (N);
2804 Def_Id : constant Entity_Id := Defining_Identifier (N);
2805 T : Entity_Id;
2806 Prev : Entity_Id;
2807
2808 Is_Remote : constant Boolean :=
2809 (Is_Remote_Types (Current_Scope)
2810 or else Is_Remote_Call_Interface (Current_Scope))
2811 and then not (In_Private_Part (Current_Scope)
2812 or else In_Package_Body (Current_Scope));
2813
2814 procedure Check_Nonoverridable_Aspects;
2815 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2816 -- be overridden, and can only be confirmed on derivation.
2817
2818 procedure Check_Ops_From_Incomplete_Type;
2819 -- If there is a tagged incomplete partial view of the type, traverse
2820 -- the primitives of the incomplete view and change the type of any
2821 -- controlling formals and result to indicate the full view. The
2822 -- primitives will be added to the full type's primitive operations
2823 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2824 -- is called from Process_Incomplete_Dependents).
2825
2826 ----------------------------------
2827 -- Check_Nonoverridable_Aspects --
2828 ----------------------------------
2829
2830 procedure Check_Nonoverridable_Aspects is
2831 function Get_Aspect_Spec
2832 (Specs : List_Id;
2833 Aspect_Name : Name_Id) return Node_Id;
2834 -- Check whether a list of aspect specifications includes an entry
2835 -- for a specific aspect. The list is either that of a partial or
2836 -- a full view.
2837
2838 ---------------------
2839 -- Get_Aspect_Spec --
2840 ---------------------
2841
2842 function Get_Aspect_Spec
2843 (Specs : List_Id;
2844 Aspect_Name : Name_Id) return Node_Id
2845 is
2846 Spec : Node_Id;
2847
2848 begin
2849 Spec := First (Specs);
2850 while Present (Spec) loop
2851 if Chars (Identifier (Spec)) = Aspect_Name then
2852 return Spec;
2853 end if;
2854 Next (Spec);
2855 end loop;
2856
2857 return Empty;
2858 end Get_Aspect_Spec;
2859
2860 -- Local variables
2861
2862 Prev_Aspects : constant List_Id :=
2863 Aspect_Specifications (Parent (Def_Id));
2864 Par_Type : Entity_Id;
2865 Prev_Aspect : Node_Id;
2866
2867 -- Start of processing for Check_Nonoverridable_Aspects
2868
2869 begin
2870 -- Get parent type of derived type. Note that Prev is the entity in
2871 -- the partial declaration, but its contents are now those of full
2872 -- view, while Def_Id reflects the partial view.
2873
2874 if Is_Private_Type (Def_Id) then
2875 Par_Type := Etype (Full_View (Def_Id));
2876 else
2877 Par_Type := Etype (Def_Id);
2878 end if;
2879
2880 -- If there is an inherited Implicit_Dereference, verify that it is
2881 -- made explicit in the partial view.
2882
2883 if Has_Discriminants (Base_Type (Par_Type))
2884 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2885 and then Present (Discriminant_Specifications (Parent (Prev)))
2886 and then Present (Get_Reference_Discriminant (Par_Type))
2887 then
2888 Prev_Aspect :=
2889 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2890
2891 if No (Prev_Aspect)
2892 and then Present
2893 (Discriminant_Specifications
2894 (Original_Node (Parent (Prev))))
2895 then
2896 Error_Msg_N
2897 ("type does not inherit implicit dereference", Prev);
2898
2899 else
2900 -- If one of the views has the aspect specified, verify that it
2901 -- is consistent with that of the parent.
2902
2903 declare
2904 Par_Discr : constant Entity_Id :=
2905 Get_Reference_Discriminant (Par_Type);
2906 Cur_Discr : constant Entity_Id :=
2907 Get_Reference_Discriminant (Prev);
2908
2909 begin
2910 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2911 Error_Msg_N ("aspect incosistent with that of parent", N);
2912 end if;
2913
2914 -- Check that specification in partial view matches the
2915 -- inherited aspect. Compare names directly because aspect
2916 -- expression may not be analyzed.
2917
2918 if Present (Prev_Aspect)
2919 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
2920 and then Chars (Expression (Prev_Aspect)) /=
2921 Chars (Cur_Discr)
2922 then
2923 Error_Msg_N
2924 ("aspect incosistent with that of parent", N);
2925 end if;
2926 end;
2927 end if;
2928 end if;
2929
2930 -- TBD : other nonoverridable aspects.
2931 end Check_Nonoverridable_Aspects;
2932
2933 ------------------------------------
2934 -- Check_Ops_From_Incomplete_Type --
2935 ------------------------------------
2936
2937 procedure Check_Ops_From_Incomplete_Type is
2938 Elmt : Elmt_Id;
2939 Formal : Entity_Id;
2940 Op : Entity_Id;
2941
2942 begin
2943 if Prev /= T
2944 and then Ekind (Prev) = E_Incomplete_Type
2945 and then Is_Tagged_Type (Prev)
2946 and then Is_Tagged_Type (T)
2947 then
2948 Elmt := First_Elmt (Primitive_Operations (Prev));
2949 while Present (Elmt) loop
2950 Op := Node (Elmt);
2951
2952 Formal := First_Formal (Op);
2953 while Present (Formal) loop
2954 if Etype (Formal) = Prev then
2955 Set_Etype (Formal, T);
2956 end if;
2957
2958 Next_Formal (Formal);
2959 end loop;
2960
2961 if Etype (Op) = Prev then
2962 Set_Etype (Op, T);
2963 end if;
2964
2965 Next_Elmt (Elmt);
2966 end loop;
2967 end if;
2968 end Check_Ops_From_Incomplete_Type;
2969
2970 -- Start of processing for Analyze_Full_Type_Declaration
2971
2972 begin
2973 Prev := Find_Type_Name (N);
2974
2975 -- The full view, if present, now points to the current type. If there
2976 -- is an incomplete partial view, set a link to it, to simplify the
2977 -- retrieval of primitive operations of the type.
2978
2979 -- Ada 2005 (AI-50217): If the type was previously decorated when
2980 -- imported through a LIMITED WITH clause, it appears as incomplete
2981 -- but has no full view.
2982
2983 if Ekind (Prev) = E_Incomplete_Type
2984 and then Present (Full_View (Prev))
2985 then
2986 T := Full_View (Prev);
2987 Set_Incomplete_View (N, Parent (Prev));
2988 else
2989 T := Prev;
2990 end if;
2991
2992 Set_Is_Pure (T, Is_Pure (Current_Scope));
2993
2994 -- We set the flag Is_First_Subtype here. It is needed to set the
2995 -- corresponding flag for the Implicit class-wide-type created
2996 -- during tagged types processing.
2997
2998 Set_Is_First_Subtype (T, True);
2999
3000 -- Only composite types other than array types are allowed to have
3001 -- discriminants.
3002
3003 case Nkind (Def) is
3004
3005 -- For derived types, the rule will be checked once we've figured
3006 -- out the parent type.
3007
3008 when N_Derived_Type_Definition =>
3009 null;
3010
3011 -- For record types, discriminants are allowed, unless we are in
3012 -- SPARK.
3013
3014 when N_Record_Definition =>
3015 if Present (Discriminant_Specifications (N)) then
3016 Check_SPARK_05_Restriction
3017 ("discriminant type is not allowed",
3018 Defining_Identifier
3019 (First (Discriminant_Specifications (N))));
3020 end if;
3021
3022 when others =>
3023 if Present (Discriminant_Specifications (N)) then
3024 Error_Msg_N
3025 ("elementary or array type cannot have discriminants",
3026 Defining_Identifier
3027 (First (Discriminant_Specifications (N))));
3028 end if;
3029 end case;
3030
3031 -- Elaborate the type definition according to kind, and generate
3032 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3033 -- already done (this happens during the reanalysis that follows a call
3034 -- to the high level optimizer).
3035
3036 if not Analyzed (T) then
3037 Set_Analyzed (T);
3038
3039 case Nkind (Def) is
3040 when N_Access_To_Subprogram_Definition =>
3041 Access_Subprogram_Declaration (T, Def);
3042
3043 -- If this is a remote access to subprogram, we must create the
3044 -- equivalent fat pointer type, and related subprograms.
3045
3046 if Is_Remote then
3047 Process_Remote_AST_Declaration (N);
3048 end if;
3049
3050 -- Validate categorization rule against access type declaration
3051 -- usually a violation in Pure unit, Shared_Passive unit.
3052
3053 Validate_Access_Type_Declaration (T, N);
3054
3055 when N_Access_To_Object_Definition =>
3056 Access_Type_Declaration (T, Def);
3057
3058 -- Validate categorization rule against access type declaration
3059 -- usually a violation in Pure unit, Shared_Passive unit.
3060
3061 Validate_Access_Type_Declaration (T, N);
3062
3063 -- If we are in a Remote_Call_Interface package and define a
3064 -- RACW, then calling stubs and specific stream attributes
3065 -- must be added.
3066
3067 if Is_Remote
3068 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3069 then
3070 Add_RACW_Features (Def_Id);
3071 end if;
3072
3073 when N_Array_Type_Definition =>
3074 Array_Type_Declaration (T, Def);
3075
3076 when N_Derived_Type_Definition =>
3077 Derived_Type_Declaration (T, N, T /= Def_Id);
3078
3079 when N_Enumeration_Type_Definition =>
3080 Enumeration_Type_Declaration (T, Def);
3081
3082 when N_Floating_Point_Definition =>
3083 Floating_Point_Type_Declaration (T, Def);
3084
3085 when N_Decimal_Fixed_Point_Definition =>
3086 Decimal_Fixed_Point_Type_Declaration (T, Def);
3087
3088 when N_Ordinary_Fixed_Point_Definition =>
3089 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3090
3091 when N_Signed_Integer_Type_Definition =>
3092 Signed_Integer_Type_Declaration (T, Def);
3093
3094 when N_Modular_Type_Definition =>
3095 Modular_Type_Declaration (T, Def);
3096
3097 when N_Record_Definition =>
3098 Record_Type_Declaration (T, N, Prev);
3099
3100 -- If declaration has a parse error, nothing to elaborate.
3101
3102 when N_Error =>
3103 null;
3104
3105 when others =>
3106 raise Program_Error;
3107 end case;
3108 end if;
3109
3110 if Etype (T) = Any_Type then
3111 return;
3112 end if;
3113
3114 -- Controlled type is not allowed in SPARK
3115
3116 if Is_Visibly_Controlled (T) then
3117 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3118 end if;
3119
3120 -- Some common processing for all types
3121
3122 Set_Depends_On_Private (T, Has_Private_Component (T));
3123 Check_Ops_From_Incomplete_Type;
3124
3125 -- Both the declared entity, and its anonymous base type if one was
3126 -- created, need freeze nodes allocated.
3127
3128 declare
3129 B : constant Entity_Id := Base_Type (T);
3130
3131 begin
3132 -- In the case where the base type differs from the first subtype, we
3133 -- pre-allocate a freeze node, and set the proper link to the first
3134 -- subtype. Freeze_Entity will use this preallocated freeze node when
3135 -- it freezes the entity.
3136
3137 -- This does not apply if the base type is a generic type, whose
3138 -- declaration is independent of the current derived definition.
3139
3140 if B /= T and then not Is_Generic_Type (B) then
3141 Ensure_Freeze_Node (B);
3142 Set_First_Subtype_Link (Freeze_Node (B), T);
3143 end if;
3144
3145 -- A type that is imported through a limited_with clause cannot
3146 -- generate any code, and thus need not be frozen. However, an access
3147 -- type with an imported designated type needs a finalization list,
3148 -- which may be referenced in some other package that has non-limited
3149 -- visibility on the designated type. Thus we must create the
3150 -- finalization list at the point the access type is frozen, to
3151 -- prevent unsatisfied references at link time.
3152
3153 if not From_Limited_With (T) or else Is_Access_Type (T) then
3154 Set_Has_Delayed_Freeze (T);
3155 end if;
3156 end;
3157
3158 -- Case where T is the full declaration of some private type which has
3159 -- been swapped in Defining_Identifier (N).
3160
3161 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3162 Process_Full_View (N, T, Def_Id);
3163
3164 -- Record the reference. The form of this is a little strange, since
3165 -- the full declaration has been swapped in. So the first parameter
3166 -- here represents the entity to which a reference is made which is
3167 -- the "real" entity, i.e. the one swapped in, and the second
3168 -- parameter provides the reference location.
3169
3170 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3171 -- since we don't want a complaint about the full type being an
3172 -- unwanted reference to the private type
3173
3174 declare
3175 B : constant Boolean := Has_Pragma_Unreferenced (T);
3176 begin
3177 Set_Has_Pragma_Unreferenced (T, False);
3178 Generate_Reference (T, T, 'c');
3179 Set_Has_Pragma_Unreferenced (T, B);
3180 end;
3181
3182 Set_Completion_Referenced (Def_Id);
3183
3184 -- For completion of incomplete type, process incomplete dependents
3185 -- and always mark the full type as referenced (it is the incomplete
3186 -- type that we get for any real reference).
3187
3188 elsif Ekind (Prev) = E_Incomplete_Type then
3189 Process_Incomplete_Dependents (N, T, Prev);
3190 Generate_Reference (Prev, Def_Id, 'c');
3191 Set_Completion_Referenced (Def_Id);
3192
3193 -- If not private type or incomplete type completion, this is a real
3194 -- definition of a new entity, so record it.
3195
3196 else
3197 Generate_Definition (Def_Id);
3198 end if;
3199
3200 -- Propagate any pending access types whose finalization masters need to
3201 -- be fully initialized from the partial to the full view. Guard against
3202 -- an illegal full view that remains unanalyzed.
3203
3204 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3205 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3206 end if;
3207
3208 if Chars (Scope (Def_Id)) = Name_System
3209 and then Chars (Def_Id) = Name_Address
3210 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
3211 then
3212 Set_Is_Descendant_Of_Address (Def_Id);
3213 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3214 Set_Is_Descendant_Of_Address (Prev);
3215 end if;
3216
3217 Set_Optimize_Alignment_Flags (Def_Id);
3218 Check_Eliminated (Def_Id);
3219
3220 -- If the declaration is a completion and aspects are present, apply
3221 -- them to the entity for the type which is currently the partial
3222 -- view, but which is the one that will be frozen.
3223
3224 if Has_Aspects (N) then
3225
3226 -- In most cases the partial view is a private type, and both views
3227 -- appear in different declarative parts. In the unusual case where
3228 -- the partial view is incomplete, perform the analysis on the
3229 -- full view, to prevent freezing anomalies with the corresponding
3230 -- class-wide type, which otherwise might be frozen before the
3231 -- dispatch table is built.
3232
3233 if Prev /= Def_Id
3234 and then Ekind (Prev) /= E_Incomplete_Type
3235 then
3236 Analyze_Aspect_Specifications (N, Prev);
3237
3238 -- Normal case
3239
3240 else
3241 Analyze_Aspect_Specifications (N, Def_Id);
3242 end if;
3243 end if;
3244
3245 if Is_Derived_Type (Prev)
3246 and then Def_Id /= Prev
3247 then
3248 Check_Nonoverridable_Aspects;
3249 end if;
3250 end Analyze_Full_Type_Declaration;
3251
3252 ----------------------------------
3253 -- Analyze_Incomplete_Type_Decl --
3254 ----------------------------------
3255
3256 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3257 F : constant Boolean := Is_Pure (Current_Scope);
3258 T : Entity_Id;
3259
3260 begin
3261 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3262
3263 Generate_Definition (Defining_Identifier (N));
3264
3265 -- Process an incomplete declaration. The identifier must not have been
3266 -- declared already in the scope. However, an incomplete declaration may
3267 -- appear in the private part of a package, for a private type that has
3268 -- already been declared.
3269
3270 -- In this case, the discriminants (if any) must match
3271
3272 T := Find_Type_Name (N);
3273
3274 Set_Ekind (T, E_Incomplete_Type);
3275 Init_Size_Align (T);
3276 Set_Is_First_Subtype (T, True);
3277 Set_Etype (T, T);
3278
3279 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3280 -- incomplete types.
3281
3282 if Tagged_Present (N) then
3283 Set_Is_Tagged_Type (T, True);
3284 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3285 Make_Class_Wide_Type (T);
3286 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3287 end if;
3288
3289 Set_Stored_Constraint (T, No_Elist);
3290
3291 if Present (Discriminant_Specifications (N)) then
3292 Push_Scope (T);
3293 Process_Discriminants (N);
3294 End_Scope;
3295 end if;
3296
3297 -- If the type has discriminants, nontrivial subtypes may be declared
3298 -- before the full view of the type. The full views of those subtypes
3299 -- will be built after the full view of the type.
3300
3301 Set_Private_Dependents (T, New_Elmt_List);
3302 Set_Is_Pure (T, F);
3303 end Analyze_Incomplete_Type_Decl;
3304
3305 -----------------------------------
3306 -- Analyze_Interface_Declaration --
3307 -----------------------------------
3308
3309 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3310 CW : constant Entity_Id := Class_Wide_Type (T);
3311
3312 begin
3313 Set_Is_Tagged_Type (T);
3314 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3315
3316 Set_Is_Limited_Record (T, Limited_Present (Def)
3317 or else Task_Present (Def)
3318 or else Protected_Present (Def)
3319 or else Synchronized_Present (Def));
3320
3321 -- Type is abstract if full declaration carries keyword, or if previous
3322 -- partial view did.
3323
3324 Set_Is_Abstract_Type (T);
3325 Set_Is_Interface (T);
3326
3327 -- Type is a limited interface if it includes the keyword limited, task,
3328 -- protected, or synchronized.
3329
3330 Set_Is_Limited_Interface
3331 (T, Limited_Present (Def)
3332 or else Protected_Present (Def)
3333 or else Synchronized_Present (Def)
3334 or else Task_Present (Def));
3335
3336 Set_Interfaces (T, New_Elmt_List);
3337 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3338
3339 -- Complete the decoration of the class-wide entity if it was already
3340 -- built (i.e. during the creation of the limited view)
3341
3342 if Present (CW) then
3343 Set_Is_Interface (CW);
3344 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3345 end if;
3346
3347 -- Check runtime support for synchronized interfaces
3348
3349 if (Is_Task_Interface (T)
3350 or else Is_Protected_Interface (T)
3351 or else Is_Synchronized_Interface (T))
3352 and then not RTE_Available (RE_Select_Specific_Data)
3353 then
3354 Error_Msg_CRT ("synchronized interfaces", T);
3355 end if;
3356 end Analyze_Interface_Declaration;
3357
3358 -----------------------------
3359 -- Analyze_Itype_Reference --
3360 -----------------------------
3361
3362 -- Nothing to do. This node is placed in the tree only for the benefit of
3363 -- back end processing, and has no effect on the semantic processing.
3364
3365 procedure Analyze_Itype_Reference (N : Node_Id) is
3366 begin
3367 pragma Assert (Is_Itype (Itype (N)));
3368 null;
3369 end Analyze_Itype_Reference;
3370
3371 --------------------------------
3372 -- Analyze_Number_Declaration --
3373 --------------------------------
3374
3375 procedure Analyze_Number_Declaration (N : Node_Id) is
3376 E : constant Node_Id := Expression (N);
3377 Id : constant Entity_Id := Defining_Identifier (N);
3378 Index : Interp_Index;
3379 It : Interp;
3380 T : Entity_Id;
3381
3382 begin
3383 Generate_Definition (Id);
3384 Enter_Name (Id);
3385
3386 -- This is an optimization of a common case of an integer literal
3387
3388 if Nkind (E) = N_Integer_Literal then
3389 Set_Is_Static_Expression (E, True);
3390 Set_Etype (E, Universal_Integer);
3391
3392 Set_Etype (Id, Universal_Integer);
3393 Set_Ekind (Id, E_Named_Integer);
3394 Set_Is_Frozen (Id, True);
3395 return;
3396 end if;
3397
3398 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3399
3400 -- Process expression, replacing error by integer zero, to avoid
3401 -- cascaded errors or aborts further along in the processing
3402
3403 -- Replace Error by integer zero, which seems least likely to cause
3404 -- cascaded errors.
3405
3406 if E = Error then
3407 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3408 Set_Error_Posted (E);
3409 end if;
3410
3411 Analyze (E);
3412
3413 -- Verify that the expression is static and numeric. If
3414 -- the expression is overloaded, we apply the preference
3415 -- rule that favors root numeric types.
3416
3417 if not Is_Overloaded (E) then
3418 T := Etype (E);
3419 if Has_Dynamic_Predicate_Aspect (T) then
3420 Error_Msg_N
3421 ("subtype has dynamic predicate, "
3422 & "not allowed in number declaration", N);
3423 end if;
3424
3425 else
3426 T := Any_Type;
3427
3428 Get_First_Interp (E, Index, It);
3429 while Present (It.Typ) loop
3430 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3431 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3432 then
3433 if T = Any_Type then
3434 T := It.Typ;
3435
3436 elsif It.Typ = Universal_Real
3437 or else
3438 It.Typ = Universal_Integer
3439 then
3440 -- Choose universal interpretation over any other
3441
3442 T := It.Typ;
3443 exit;
3444 end if;
3445 end if;
3446
3447 Get_Next_Interp (Index, It);
3448 end loop;
3449 end if;
3450
3451 if Is_Integer_Type (T) then
3452 Resolve (E, T);
3453 Set_Etype (Id, Universal_Integer);
3454 Set_Ekind (Id, E_Named_Integer);
3455
3456 elsif Is_Real_Type (T) then
3457
3458 -- Because the real value is converted to universal_real, this is a
3459 -- legal context for a universal fixed expression.
3460
3461 if T = Universal_Fixed then
3462 declare
3463 Loc : constant Source_Ptr := Sloc (N);
3464 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3465 Subtype_Mark =>
3466 New_Occurrence_Of (Universal_Real, Loc),
3467 Expression => Relocate_Node (E));
3468
3469 begin
3470 Rewrite (E, Conv);
3471 Analyze (E);
3472 end;
3473
3474 elsif T = Any_Fixed then
3475 Error_Msg_N ("illegal context for mixed mode operation", E);
3476
3477 -- Expression is of the form : universal_fixed * integer. Try to
3478 -- resolve as universal_real.
3479
3480 T := Universal_Real;
3481 Set_Etype (E, T);
3482 end if;
3483
3484 Resolve (E, T);
3485 Set_Etype (Id, Universal_Real);
3486 Set_Ekind (Id, E_Named_Real);
3487
3488 else
3489 Wrong_Type (E, Any_Numeric);
3490 Resolve (E, T);
3491
3492 Set_Etype (Id, T);
3493 Set_Ekind (Id, E_Constant);
3494 Set_Never_Set_In_Source (Id, True);
3495 Set_Is_True_Constant (Id, True);
3496 return;
3497 end if;
3498
3499 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3500 Set_Etype (E, Etype (Id));
3501 end if;
3502
3503 if not Is_OK_Static_Expression (E) then
3504 Flag_Non_Static_Expr
3505 ("non-static expression used in number declaration!", E);
3506 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3507 Set_Etype (E, Any_Type);
3508 end if;
3509
3510 Analyze_Dimension (N);
3511 end Analyze_Number_Declaration;
3512
3513 --------------------------------
3514 -- Analyze_Object_Declaration --
3515 --------------------------------
3516
3517 -- WARNING: This routine manages Ghost regions. Return statements must be
3518 -- replaced by gotos which jump to the end of the routine and restore the
3519 -- Ghost mode.
3520
3521 procedure Analyze_Object_Declaration (N : Node_Id) is
3522 Loc : constant Source_Ptr := Sloc (N);
3523 Id : constant Entity_Id := Defining_Identifier (N);
3524 Act_T : Entity_Id;
3525 T : Entity_Id;
3526
3527 E : Node_Id := Expression (N);
3528 -- E is set to Expression (N) throughout this routine. When
3529 -- Expression (N) is modified, E is changed accordingly.
3530
3531 Prev_Entity : Entity_Id := Empty;
3532
3533 function Count_Tasks (T : Entity_Id) return Uint;
3534 -- This function is called when a non-generic library level object of a
3535 -- task type is declared. Its function is to count the static number of
3536 -- tasks declared within the type (it is only called if Has_Task is set
3537 -- for T). As a side effect, if an array of tasks with non-static bounds
3538 -- or a variant record type is encountered, Check_Restriction is called
3539 -- indicating the count is unknown.
3540
3541 function Delayed_Aspect_Present return Boolean;
3542 -- If the declaration has an expression that is an aggregate, and it
3543 -- has aspects that require delayed analysis, the resolution of the
3544 -- aggregate must be deferred to the freeze point of the objet. This
3545 -- special processing was created for address clauses, but it must
3546 -- also apply to Alignment. This must be done before the aspect
3547 -- specifications are analyzed because we must handle the aggregate
3548 -- before the analysis of the object declaration is complete.
3549
3550 -- Any other relevant delayed aspects on object declarations ???
3551
3552 -----------------
3553 -- Count_Tasks --
3554 -----------------
3555
3556 function Count_Tasks (T : Entity_Id) return Uint is
3557 C : Entity_Id;
3558 X : Node_Id;
3559 V : Uint;
3560
3561 begin
3562 if Is_Task_Type (T) then
3563 return Uint_1;
3564
3565 elsif Is_Record_Type (T) then
3566 if Has_Discriminants (T) then
3567 Check_Restriction (Max_Tasks, N);
3568 return Uint_0;
3569
3570 else
3571 V := Uint_0;
3572 C := First_Component (T);
3573 while Present (C) loop
3574 V := V + Count_Tasks (Etype (C));
3575 Next_Component (C);
3576 end loop;
3577
3578 return V;
3579 end if;
3580
3581 elsif Is_Array_Type (T) then
3582 X := First_Index (T);
3583 V := Count_Tasks (Component_Type (T));
3584 while Present (X) loop
3585 C := Etype (X);
3586
3587 if not Is_OK_Static_Subtype (C) then
3588 Check_Restriction (Max_Tasks, N);
3589 return Uint_0;
3590 else
3591 V := V * (UI_Max (Uint_0,
3592 Expr_Value (Type_High_Bound (C)) -
3593 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3594 end if;
3595
3596 Next_Index (X);
3597 end loop;
3598
3599 return V;
3600
3601 else
3602 return Uint_0;
3603 end if;
3604 end Count_Tasks;
3605
3606 ----------------------------
3607 -- Delayed_Aspect_Present --
3608 ----------------------------
3609
3610 function Delayed_Aspect_Present return Boolean is
3611 A : Node_Id;
3612 A_Id : Aspect_Id;
3613
3614 begin
3615 if Present (Aspect_Specifications (N)) then
3616 A := First (Aspect_Specifications (N));
3617 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3618 while Present (A) loop
3619 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3620 return True;
3621 end if;
3622
3623 Next (A);
3624 end loop;
3625 end if;
3626
3627 return False;
3628 end Delayed_Aspect_Present;
3629
3630 -- Local variables
3631
3632 Mode : Ghost_Mode_Type;
3633 Mode_Set : Boolean := False;
3634 Related_Id : Entity_Id;
3635
3636 -- Start of processing for Analyze_Object_Declaration
3637
3638 begin
3639 -- There are three kinds of implicit types generated by an
3640 -- object declaration:
3641
3642 -- 1. Those generated by the original Object Definition
3643
3644 -- 2. Those generated by the Expression
3645
3646 -- 3. Those used to constrain the Object Definition with the
3647 -- expression constraints when the definition is unconstrained.
3648
3649 -- They must be generated in this order to avoid order of elaboration
3650 -- issues. Thus the first step (after entering the name) is to analyze
3651 -- the object definition.
3652
3653 if Constant_Present (N) then
3654 Prev_Entity := Current_Entity_In_Scope (Id);
3655
3656 if Present (Prev_Entity)
3657 and then
3658 -- If the homograph is an implicit subprogram, it is overridden
3659 -- by the current declaration.
3660
3661 ((Is_Overloadable (Prev_Entity)
3662 and then Is_Inherited_Operation (Prev_Entity))
3663
3664 -- The current object is a discriminal generated for an entry
3665 -- family index. Even though the index is a constant, in this
3666 -- particular context there is no true constant redeclaration.
3667 -- Enter_Name will handle the visibility.
3668
3669 or else
3670 (Is_Discriminal (Id)
3671 and then Ekind (Discriminal_Link (Id)) =
3672 E_Entry_Index_Parameter)
3673
3674 -- The current object is the renaming for a generic declared
3675 -- within the instance.
3676
3677 or else
3678 (Ekind (Prev_Entity) = E_Package
3679 and then Nkind (Parent (Prev_Entity)) =
3680 N_Package_Renaming_Declaration
3681 and then not Comes_From_Source (Prev_Entity)
3682 and then
3683 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3684
3685 -- The entity may be a homonym of a private component of the
3686 -- enclosing protected object, for which we create a local
3687 -- renaming declaration. The declaration is legal, even if
3688 -- useless when it just captures that component.
3689
3690 or else
3691 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3692 and then Nkind (Parent (Prev_Entity)) =
3693 N_Object_Renaming_Declaration))
3694 then
3695 Prev_Entity := Empty;
3696 end if;
3697 end if;
3698
3699 if Present (Prev_Entity) then
3700
3701 -- The object declaration is Ghost when it completes a deferred Ghost
3702 -- constant.
3703
3704 Mark_And_Set_Ghost_Completion (N, Prev_Entity, Mode);
3705 Mode_Set := True;
3706
3707 Constant_Redeclaration (Id, N, T);
3708
3709 Generate_Reference (Prev_Entity, Id, 'c');
3710 Set_Completion_Referenced (Id);
3711
3712 if Error_Posted (N) then
3713
3714 -- Type mismatch or illegal redeclaration; do not analyze
3715 -- expression to avoid cascaded errors.
3716
3717 T := Find_Type_Of_Object (Object_Definition (N), N);
3718 Set_Etype (Id, T);
3719 Set_Ekind (Id, E_Variable);
3720 goto Leave;
3721 end if;
3722
3723 -- In the normal case, enter identifier at the start to catch premature
3724 -- usage in the initialization expression.
3725
3726 else
3727 Generate_Definition (Id);
3728 Enter_Name (Id);
3729
3730 Mark_Coextensions (N, Object_Definition (N));
3731
3732 T := Find_Type_Of_Object (Object_Definition (N), N);
3733
3734 if Nkind (Object_Definition (N)) = N_Access_Definition
3735 and then Present
3736 (Access_To_Subprogram_Definition (Object_Definition (N)))
3737 and then Protected_Present
3738 (Access_To_Subprogram_Definition (Object_Definition (N)))
3739 then
3740 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3741 end if;
3742
3743 if Error_Posted (Id) then
3744 Set_Etype (Id, T);
3745 Set_Ekind (Id, E_Variable);
3746 goto Leave;
3747 end if;
3748 end if;
3749
3750 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3751 -- out some static checks.
3752
3753 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3754
3755 -- In case of aggregates we must also take care of the correct
3756 -- initialization of nested aggregates bug this is done at the
3757 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
3758
3759 if Present (Expression (N))
3760 and then Nkind (Expression (N)) = N_Aggregate
3761 then
3762 null;
3763
3764 else
3765 declare
3766 Save_Typ : constant Entity_Id := Etype (Id);
3767 begin
3768 Set_Etype (Id, T); -- Temp. decoration for static checks
3769 Null_Exclusion_Static_Checks (N);
3770 Set_Etype (Id, Save_Typ);
3771 end;
3772 end if;
3773 end if;
3774
3775 -- Object is marked pure if it is in a pure scope
3776
3777 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3778
3779 -- If deferred constant, make sure context is appropriate. We detect
3780 -- a deferred constant as a constant declaration with no expression.
3781 -- A deferred constant can appear in a package body if its completion
3782 -- is by means of an interface pragma.
3783
3784 if Constant_Present (N) and then No (E) then
3785
3786 -- A deferred constant may appear in the declarative part of the
3787 -- following constructs:
3788
3789 -- blocks
3790 -- entry bodies
3791 -- extended return statements
3792 -- package specs
3793 -- package bodies
3794 -- subprogram bodies
3795 -- task bodies
3796
3797 -- When declared inside a package spec, a deferred constant must be
3798 -- completed by a full constant declaration or pragma Import. In all
3799 -- other cases, the only proper completion is pragma Import. Extended
3800 -- return statements are flagged as invalid contexts because they do
3801 -- not have a declarative part and so cannot accommodate the pragma.
3802
3803 if Ekind (Current_Scope) = E_Return_Statement then
3804 Error_Msg_N
3805 ("invalid context for deferred constant declaration (RM 7.4)",
3806 N);
3807 Error_Msg_N
3808 ("\declaration requires an initialization expression",
3809 N);
3810 Set_Constant_Present (N, False);
3811
3812 -- In Ada 83, deferred constant must be of private type
3813
3814 elsif not Is_Private_Type (T) then
3815 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3816 Error_Msg_N
3817 ("(Ada 83) deferred constant must be private type", N);
3818 end if;
3819 end if;
3820
3821 -- If not a deferred constant, then the object declaration freezes
3822 -- its type, unless the object is of an anonymous type and has delayed
3823 -- aspects. In that case the type is frozen when the object itself is.
3824
3825 else
3826 Check_Fully_Declared (T, N);
3827
3828 if Has_Delayed_Aspects (Id)
3829 and then Is_Array_Type (T)
3830 and then Is_Itype (T)
3831 then
3832 Set_Has_Delayed_Freeze (T);
3833 else
3834 Freeze_Before (N, T);
3835 end if;
3836 end if;
3837
3838 -- If the object was created by a constrained array definition, then
3839 -- set the link in both the anonymous base type and anonymous subtype
3840 -- that are built to represent the array type to point to the object.
3841
3842 if Nkind (Object_Definition (Declaration_Node (Id))) =
3843 N_Constrained_Array_Definition
3844 then
3845 Set_Related_Array_Object (T, Id);
3846 Set_Related_Array_Object (Base_Type (T), Id);
3847 end if;
3848
3849 -- Special checks for protected objects not at library level
3850
3851 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
3852 Check_Restriction (No_Local_Protected_Objects, Id);
3853
3854 -- Protected objects with interrupt handlers must be at library level
3855
3856 -- Ada 2005: This test is not needed (and the corresponding clause
3857 -- in the RM is removed) because accessibility checks are sufficient
3858 -- to make handlers not at the library level illegal.
3859
3860 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3861 -- applies to the '95 version of the language as well.
3862
3863 if Is_Protected_Type (T)
3864 and then Has_Interrupt_Handler (T)
3865 and then Ada_Version < Ada_95
3866 then
3867 Error_Msg_N
3868 ("interrupt object can only be declared at library level", Id);
3869 end if;
3870 end if;
3871
3872 -- Check for violation of No_Local_Timing_Events
3873
3874 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
3875 Check_Restriction (No_Local_Timing_Events, Id);
3876 end if;
3877
3878 -- The actual subtype of the object is the nominal subtype, unless
3879 -- the nominal one is unconstrained and obtained from the expression.
3880
3881 Act_T := T;
3882
3883 -- These checks should be performed before the initialization expression
3884 -- is considered, so that the Object_Definition node is still the same
3885 -- as in source code.
3886
3887 -- In SPARK, the nominal subtype is always given by a subtype mark
3888 -- and must not be unconstrained. (The only exception to this is the
3889 -- acceptance of declarations of constants of type String.)
3890
3891 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3892 then
3893 Check_SPARK_05_Restriction
3894 ("subtype mark required", Object_Definition (N));
3895
3896 elsif Is_Array_Type (T)
3897 and then not Is_Constrained (T)
3898 and then T /= Standard_String
3899 then
3900 Check_SPARK_05_Restriction
3901 ("subtype mark of constrained type expected",
3902 Object_Definition (N));
3903 end if;
3904
3905 -- There are no aliased objects in SPARK
3906
3907 if Aliased_Present (N) then
3908 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3909 end if;
3910
3911 -- Process initialization expression if present and not in error
3912
3913 if Present (E) and then E /= Error then
3914
3915 -- Generate an error in case of CPP class-wide object initialization.
3916 -- Required because otherwise the expansion of the class-wide
3917 -- assignment would try to use 'size to initialize the object
3918 -- (primitive that is not available in CPP tagged types).
3919
3920 if Is_Class_Wide_Type (Act_T)
3921 and then
3922 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3923 or else
3924 (Present (Full_View (Root_Type (Etype (Act_T))))
3925 and then
3926 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3927 then
3928 Error_Msg_N
3929 ("predefined assignment not available for 'C'P'P tagged types",
3930 E);
3931 end if;
3932
3933 Mark_Coextensions (N, E);
3934 Analyze (E);
3935
3936 -- In case of errors detected in the analysis of the expression,
3937 -- decorate it with the expected type to avoid cascaded errors
3938
3939 if No (Etype (E)) then
3940 Set_Etype (E, T);
3941 end if;
3942
3943 -- If an initialization expression is present, then we set the
3944 -- Is_True_Constant flag. It will be reset if this is a variable
3945 -- and it is indeed modified.
3946
3947 Set_Is_True_Constant (Id, True);
3948
3949 -- If we are analyzing a constant declaration, set its completion
3950 -- flag after analyzing and resolving the expression.
3951
3952 if Constant_Present (N) then
3953 Set_Has_Completion (Id);
3954 end if;
3955
3956 -- Set type and resolve (type may be overridden later on). Note:
3957 -- Ekind (Id) must still be E_Void at this point so that incorrect
3958 -- early usage within E is properly diagnosed.
3959
3960 Set_Etype (Id, T);
3961
3962 -- If the expression is an aggregate we must look ahead to detect
3963 -- the possible presence of an address clause, and defer resolution
3964 -- and expansion of the aggregate to the freeze point of the entity.
3965
3966 -- This is not always legal because the aggregate may contain other
3967 -- references that need freezing, e.g. references to other entities
3968 -- with address clauses. In any case, when compiling with -gnatI the
3969 -- presence of the address clause must be ignored.
3970
3971 if Comes_From_Source (N)
3972 and then Expander_Active
3973 and then Nkind (E) = N_Aggregate
3974 and then
3975 ((Present (Following_Address_Clause (N))
3976 and then not Ignore_Rep_Clauses)
3977 or else Delayed_Aspect_Present)
3978 then
3979 Set_Etype (E, T);
3980
3981 else
3982 Resolve (E, T);
3983 end if;
3984
3985 -- No further action needed if E is a call to an inlined function
3986 -- which returns an unconstrained type and it has been expanded into
3987 -- a procedure call. In that case N has been replaced by an object
3988 -- declaration without initializing expression and it has been
3989 -- analyzed (see Expand_Inlined_Call).
3990
3991 if Back_End_Inlining
3992 and then Expander_Active
3993 and then Nkind (E) = N_Function_Call
3994 and then Nkind (Name (E)) in N_Has_Entity
3995 and then Is_Inlined (Entity (Name (E)))
3996 and then not Is_Constrained (Etype (E))
3997 and then Analyzed (N)
3998 and then No (Expression (N))
3999 then
4000 goto Leave;
4001 end if;
4002
4003 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4004 -- node (which was marked already-analyzed), we need to set the type
4005 -- to something other than Any_Access in order to keep gigi happy.
4006
4007 if Etype (E) = Any_Access then
4008 Set_Etype (E, T);
4009 end if;
4010
4011 -- If the object is an access to variable, the initialization
4012 -- expression cannot be an access to constant.
4013
4014 if Is_Access_Type (T)
4015 and then not Is_Access_Constant (T)
4016 and then Is_Access_Type (Etype (E))
4017 and then Is_Access_Constant (Etype (E))
4018 then
4019 Error_Msg_N
4020 ("access to variable cannot be initialized with an "
4021 & "access-to-constant expression", E);
4022 end if;
4023
4024 if not Assignment_OK (N) then
4025 Check_Initialization (T, E);
4026 end if;
4027
4028 Check_Unset_Reference (E);
4029
4030 -- If this is a variable, then set current value. If this is a
4031 -- declared constant of a scalar type with a static expression,
4032 -- indicate that it is always valid.
4033
4034 if not Constant_Present (N) then
4035 if Compile_Time_Known_Value (E) then
4036 Set_Current_Value (Id, E);
4037 end if;
4038
4039 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4040 Set_Is_Known_Valid (Id);
4041 end if;
4042
4043 -- Deal with setting of null flags
4044
4045 if Is_Access_Type (T) then
4046 if Known_Non_Null (E) then
4047 Set_Is_Known_Non_Null (Id, True);
4048 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4049 Set_Is_Known_Null (Id, True);
4050 end if;
4051 end if;
4052
4053 -- Check incorrect use of dynamically tagged expressions
4054
4055 if Is_Tagged_Type (T) then
4056 Check_Dynamically_Tagged_Expression
4057 (Expr => E,
4058 Typ => T,
4059 Related_Nod => N);
4060 end if;
4061
4062 Apply_Scalar_Range_Check (E, T);
4063 Apply_Static_Length_Check (E, T);
4064
4065 if Nkind (Original_Node (N)) = N_Object_Declaration
4066 and then Comes_From_Source (Original_Node (N))
4067
4068 -- Only call test if needed
4069
4070 and then Restriction_Check_Required (SPARK_05)
4071 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4072 then
4073 Check_SPARK_05_Restriction
4074 ("initialization expression is not appropriate", E);
4075 end if;
4076
4077 -- A formal parameter of a specific tagged type whose related
4078 -- subprogram is subject to pragma Extensions_Visible with value
4079 -- "False" cannot be implicitly converted to a class-wide type by
4080 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4081 -- not consider internally generated expressions.
4082
4083 if Is_Class_Wide_Type (T)
4084 and then Comes_From_Source (E)
4085 and then Is_EVF_Expression (E)
4086 then
4087 Error_Msg_N
4088 ("formal parameter cannot be implicitly converted to "
4089 & "class-wide type when Extensions_Visible is False", E);
4090 end if;
4091 end if;
4092
4093 -- If the No_Streams restriction is set, check that the type of the
4094 -- object is not, and does not contain, any subtype derived from
4095 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4096 -- Has_Stream just for efficiency reasons. There is no point in
4097 -- spending time on a Has_Stream check if the restriction is not set.
4098
4099 if Restriction_Check_Required (No_Streams) then
4100 if Has_Stream (T) then
4101 Check_Restriction (No_Streams, N);
4102 end if;
4103 end if;
4104
4105 -- Deal with predicate check before we start to do major rewriting. It
4106 -- is OK to initialize and then check the initialized value, since the
4107 -- object goes out of scope if we get a predicate failure. Note that we
4108 -- do this in the analyzer and not the expander because the analyzer
4109 -- does some substantial rewriting in some cases.
4110
4111 -- We need a predicate check if the type has predicates that are not
4112 -- ignored, and if either there is an initializing expression, or for
4113 -- default initialization when we have at least one case of an explicit
4114 -- default initial value and then this is not an internal declaration
4115 -- whose initialization comes later (as for an aggregate expansion).
4116
4117 if not Suppress_Assignment_Checks (N)
4118 and then Present (Predicate_Function (T))
4119 and then not Predicates_Ignored (T)
4120 and then not No_Initialization (N)
4121 and then
4122 (Present (E)
4123 or else
4124 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4125 then
4126 -- If the type has a static predicate and the expression is known at
4127 -- compile time, see if the expression satisfies the predicate.
4128
4129 if Present (E) then
4130 Check_Expression_Against_Static_Predicate (E, T);
4131 end if;
4132
4133 -- If the type is a null record and there is no explicit initial
4134 -- expression, no predicate check applies.
4135
4136 if No (E) and then Is_Null_Record_Type (T) then
4137 null;
4138
4139 else
4140 Insert_After (N,
4141 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
4142 end if;
4143 end if;
4144
4145 -- Case of unconstrained type
4146
4147 if not Is_Definite_Subtype (T) then
4148
4149 -- In SPARK, a declaration of unconstrained type is allowed
4150 -- only for constants of type string.
4151
4152 if Is_String_Type (T) and then not Constant_Present (N) then
4153 Check_SPARK_05_Restriction
4154 ("declaration of object of unconstrained type not allowed", N);
4155 end if;
4156
4157 -- Nothing to do in deferred constant case
4158
4159 if Constant_Present (N) and then No (E) then
4160 null;
4161
4162 -- Case of no initialization present
4163
4164 elsif No (E) then
4165 if No_Initialization (N) then
4166 null;
4167
4168 elsif Is_Class_Wide_Type (T) then
4169 Error_Msg_N
4170 ("initialization required in class-wide declaration ", N);
4171
4172 else
4173 Error_Msg_N
4174 ("unconstrained subtype not allowed (need initialization)",
4175 Object_Definition (N));
4176
4177 if Is_Record_Type (T) and then Has_Discriminants (T) then
4178 Error_Msg_N
4179 ("\provide initial value or explicit discriminant values",
4180 Object_Definition (N));
4181
4182 Error_Msg_NE
4183 ("\or give default discriminant values for type&",
4184 Object_Definition (N), T);
4185
4186 elsif Is_Array_Type (T) then
4187 Error_Msg_N
4188 ("\provide initial value or explicit array bounds",
4189 Object_Definition (N));
4190 end if;
4191 end if;
4192
4193 -- Case of initialization present but in error. Set initial
4194 -- expression as absent (but do not make above complaints)
4195
4196 elsif E = Error then
4197 Set_Expression (N, Empty);
4198 E := Empty;
4199
4200 -- Case of initialization present
4201
4202 else
4203 -- Check restrictions in Ada 83
4204
4205 if not Constant_Present (N) then
4206
4207 -- Unconstrained variables not allowed in Ada 83 mode
4208
4209 if Ada_Version = Ada_83
4210 and then Comes_From_Source (Object_Definition (N))
4211 then
4212 Error_Msg_N
4213 ("(Ada 83) unconstrained variable not allowed",
4214 Object_Definition (N));
4215 end if;
4216 end if;
4217
4218 -- Now we constrain the variable from the initializing expression
4219
4220 -- If the expression is an aggregate, it has been expanded into
4221 -- individual assignments. Retrieve the actual type from the
4222 -- expanded construct.
4223
4224 if Is_Array_Type (T)
4225 and then No_Initialization (N)
4226 and then Nkind (Original_Node (E)) = N_Aggregate
4227 then
4228 Act_T := Etype (E);
4229
4230 -- In case of class-wide interface object declarations we delay
4231 -- the generation of the equivalent record type declarations until
4232 -- its expansion because there are cases in they are not required.
4233
4234 elsif Is_Interface (T) then
4235 null;
4236
4237 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4238 -- we should prevent the generation of another Itype with the
4239 -- same name as the one already generated, or we end up with
4240 -- two identical types in GNATprove.
4241
4242 elsif GNATprove_Mode then
4243 null;
4244
4245 -- If the type is an unchecked union, no subtype can be built from
4246 -- the expression. Rewrite declaration as a renaming, which the
4247 -- back-end can handle properly. This is a rather unusual case,
4248 -- because most unchecked_union declarations have default values
4249 -- for discriminants and are thus not indefinite.
4250
4251 elsif Is_Unchecked_Union (T) then
4252 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4253 Set_Ekind (Id, E_Constant);
4254 else
4255 Set_Ekind (Id, E_Variable);
4256 end if;
4257
4258 Rewrite (N,
4259 Make_Object_Renaming_Declaration (Loc,
4260 Defining_Identifier => Id,
4261 Subtype_Mark => New_Occurrence_Of (T, Loc),
4262 Name => E));
4263
4264 Set_Renamed_Object (Id, E);
4265 Freeze_Before (N, T);
4266 Set_Is_Frozen (Id);
4267 goto Leave;
4268
4269 else
4270 -- Ensure that the generated subtype has a unique external name
4271 -- when the related object is public. This guarantees that the
4272 -- subtype and its bounds will not be affected by switches or
4273 -- pragmas that may offset the internal counter due to extra
4274 -- generated code.
4275
4276 if Is_Public (Id) then
4277 Related_Id := Id;
4278 else
4279 Related_Id := Empty;
4280 end if;
4281
4282 Expand_Subtype_From_Expr
4283 (N => N,
4284 Unc_Type => T,
4285 Subtype_Indic => Object_Definition (N),
4286 Exp => E,
4287 Related_Id => Related_Id);
4288
4289 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4290 end if;
4291
4292 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4293
4294 if Aliased_Present (N) then
4295 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4296 end if;
4297
4298 Freeze_Before (N, Act_T);
4299 Freeze_Before (N, T);
4300 end if;
4301
4302 elsif Is_Array_Type (T)
4303 and then No_Initialization (N)
4304 and then (Nkind (Original_Node (E)) = N_Aggregate
4305 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4306 and then Nkind (Original_Node (Expression
4307 (Original_Node (E)))) = N_Aggregate))
4308 then
4309 if not Is_Entity_Name (Object_Definition (N)) then
4310 Act_T := Etype (E);
4311 Check_Compile_Time_Size (Act_T);
4312
4313 if Aliased_Present (N) then
4314 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4315 end if;
4316 end if;
4317
4318 -- When the given object definition and the aggregate are specified
4319 -- independently, and their lengths might differ do a length check.
4320 -- This cannot happen if the aggregate is of the form (others =>...)
4321
4322 if not Is_Constrained (T) then
4323 null;
4324
4325 elsif Nkind (E) = N_Raise_Constraint_Error then
4326
4327 -- Aggregate is statically illegal. Place back in declaration
4328
4329 Set_Expression (N, E);
4330 Set_No_Initialization (N, False);
4331
4332 elsif T = Etype (E) then
4333 null;
4334
4335 elsif Nkind (E) = N_Aggregate
4336 and then Present (Component_Associations (E))
4337 and then Present (Choice_List (First (Component_Associations (E))))
4338 and then
4339 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4340 N_Others_Choice
4341 then
4342 null;
4343
4344 else
4345 Apply_Length_Check (E, T);
4346 end if;
4347
4348 -- If the type is limited unconstrained with defaulted discriminants and
4349 -- there is no expression, then the object is constrained by the
4350 -- defaults, so it is worthwhile building the corresponding subtype.
4351
4352 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4353 and then not Is_Constrained (T)
4354 and then Has_Discriminants (T)
4355 then
4356 if No (E) then
4357 Act_T := Build_Default_Subtype (T, N);
4358 else
4359 -- Ada 2005: A limited object may be initialized by means of an
4360 -- aggregate. If the type has default discriminants it has an
4361 -- unconstrained nominal type, Its actual subtype will be obtained
4362 -- from the aggregate, and not from the default discriminants.
4363
4364 Act_T := Etype (E);
4365 end if;
4366
4367 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4368
4369 elsif Nkind (E) = N_Function_Call
4370 and then Constant_Present (N)
4371 and then Has_Unconstrained_Elements (Etype (E))
4372 then
4373 -- The back-end has problems with constants of a discriminated type
4374 -- with defaults, if the initial value is a function call. We
4375 -- generate an intermediate temporary that will receive a reference
4376 -- to the result of the call. The initialization expression then
4377 -- becomes a dereference of that temporary.
4378
4379 Remove_Side_Effects (E);
4380
4381 -- If this is a constant declaration of an unconstrained type and
4382 -- the initialization is an aggregate, we can use the subtype of the
4383 -- aggregate for the declared entity because it is immutable.
4384
4385 elsif not Is_Constrained (T)
4386 and then Has_Discriminants (T)
4387 and then Constant_Present (N)
4388 and then not Has_Unchecked_Union (T)
4389 and then Nkind (E) = N_Aggregate
4390 then
4391 Act_T := Etype (E);
4392 end if;
4393
4394 -- Check No_Wide_Characters restriction
4395
4396 Check_Wide_Character_Restriction (T, Object_Definition (N));
4397
4398 -- Indicate this is not set in source. Certainly true for constants, and
4399 -- true for variables so far (will be reset for a variable if and when
4400 -- we encounter a modification in the source).
4401
4402 Set_Never_Set_In_Source (Id);
4403
4404 -- Now establish the proper kind and type of the object
4405
4406 if Constant_Present (N) then
4407 Set_Ekind (Id, E_Constant);
4408 Set_Is_True_Constant (Id);
4409
4410 else
4411 Set_Ekind (Id, E_Variable);
4412
4413 -- A variable is set as shared passive if it appears in a shared
4414 -- passive package, and is at the outer level. This is not done for
4415 -- entities generated during expansion, because those are always
4416 -- manipulated locally.
4417
4418 if Is_Shared_Passive (Current_Scope)
4419 and then Is_Library_Level_Entity (Id)
4420 and then Comes_From_Source (Id)
4421 then
4422 Set_Is_Shared_Passive (Id);
4423 Check_Shared_Var (Id, T, N);
4424 end if;
4425
4426 -- Set Has_Initial_Value if initializing expression present. Note
4427 -- that if there is no initializing expression, we leave the state
4428 -- of this flag unchanged (usually it will be False, but notably in
4429 -- the case of exception choice variables, it will already be true).
4430
4431 if Present (E) then
4432 Set_Has_Initial_Value (Id);
4433 end if;
4434 end if;
4435
4436 -- Initialize alignment and size and capture alignment setting
4437
4438 Init_Alignment (Id);
4439 Init_Esize (Id);
4440 Set_Optimize_Alignment_Flags (Id);
4441
4442 -- Deal with aliased case
4443
4444 if Aliased_Present (N) then
4445 Set_Is_Aliased (Id);
4446
4447 -- If the object is aliased and the type is unconstrained with
4448 -- defaulted discriminants and there is no expression, then the
4449 -- object is constrained by the defaults, so it is worthwhile
4450 -- building the corresponding subtype.
4451
4452 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4453 -- unconstrained, then only establish an actual subtype if the
4454 -- nominal subtype is indefinite. In definite cases the object is
4455 -- unconstrained in Ada 2005.
4456
4457 if No (E)
4458 and then Is_Record_Type (T)
4459 and then not Is_Constrained (T)
4460 and then Has_Discriminants (T)
4461 and then (Ada_Version < Ada_2005
4462 or else not Is_Definite_Subtype (T))
4463 then
4464 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4465 end if;
4466 end if;
4467
4468 -- Now we can set the type of the object
4469
4470 Set_Etype (Id, Act_T);
4471
4472 -- Non-constant object is marked to be treated as volatile if type is
4473 -- volatile and we clear the Current_Value setting that may have been
4474 -- set above. Doing so for constants isn't required and might interfere
4475 -- with possible uses of the object as a static expression in contexts
4476 -- incompatible with volatility (e.g. as a case-statement alternative).
4477
4478 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4479 Set_Treat_As_Volatile (Id);
4480 Set_Current_Value (Id, Empty);
4481 end if;
4482
4483 -- Deal with controlled types
4484
4485 if Has_Controlled_Component (Etype (Id))
4486 or else Is_Controlled (Etype (Id))
4487 then
4488 if not Is_Library_Level_Entity (Id) then
4489 Check_Restriction (No_Nested_Finalization, N);
4490 else
4491 Validate_Controlled_Object (Id);
4492 end if;
4493 end if;
4494
4495 if Has_Task (Etype (Id)) then
4496 Check_Restriction (No_Tasking, N);
4497
4498 -- Deal with counting max tasks
4499
4500 -- Nothing to do if inside a generic
4501
4502 if Inside_A_Generic then
4503 null;
4504
4505 -- If library level entity, then count tasks
4506
4507 elsif Is_Library_Level_Entity (Id) then
4508 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4509
4510 -- If not library level entity, then indicate we don't know max
4511 -- tasks and also check task hierarchy restriction and blocking
4512 -- operation (since starting a task is definitely blocking).
4513
4514 else
4515 Check_Restriction (Max_Tasks, N);
4516 Check_Restriction (No_Task_Hierarchy, N);
4517 Check_Potentially_Blocking_Operation (N);
4518 end if;
4519
4520 -- A rather specialized test. If we see two tasks being declared
4521 -- of the same type in the same object declaration, and the task
4522 -- has an entry with an address clause, we know that program error
4523 -- will be raised at run time since we can't have two tasks with
4524 -- entries at the same address.
4525
4526 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4527 declare
4528 E : Entity_Id;
4529
4530 begin
4531 E := First_Entity (Etype (Id));
4532 while Present (E) loop
4533 if Ekind (E) = E_Entry
4534 and then Present (Get_Attribute_Definition_Clause
4535 (E, Attribute_Address))
4536 then
4537 Error_Msg_Warn := SPARK_Mode /= On;
4538 Error_Msg_N
4539 ("more than one task with same entry address<<", N);
4540 Error_Msg_N ("\Program_Error [<<", N);
4541 Insert_Action (N,
4542 Make_Raise_Program_Error (Loc,
4543 Reason => PE_Duplicated_Entry_Address));
4544 exit;
4545 end if;
4546
4547 Next_Entity (E);
4548 end loop;
4549 end;
4550 end if;
4551 end if;
4552
4553 -- Some simple constant-propagation: if the expression is a constant
4554 -- string initialized with a literal, share the literal. This avoids
4555 -- a run-time copy.
4556
4557 if Present (E)
4558 and then Is_Entity_Name (E)
4559 and then Ekind (Entity (E)) = E_Constant
4560 and then Base_Type (Etype (E)) = Standard_String
4561 then
4562 declare
4563 Val : constant Node_Id := Constant_Value (Entity (E));
4564 begin
4565 if Present (Val) and then Nkind (Val) = N_String_Literal then
4566 Rewrite (E, New_Copy (Val));
4567 end if;
4568 end;
4569 end if;
4570
4571 -- Another optimization: if the nominal subtype is unconstrained and
4572 -- the expression is a function call that returns an unconstrained
4573 -- type, rewrite the declaration as a renaming of the result of the
4574 -- call. The exceptions below are cases where the copy is expected,
4575 -- either by the back end (Aliased case) or by the semantics, as for
4576 -- initializing controlled types or copying tags for class-wide types.
4577
4578 if Present (E)
4579 and then Nkind (E) = N_Explicit_Dereference
4580 and then Nkind (Original_Node (E)) = N_Function_Call
4581 and then not Is_Library_Level_Entity (Id)
4582 and then not Is_Constrained (Underlying_Type (T))
4583 and then not Is_Aliased (Id)
4584 and then not Is_Class_Wide_Type (T)
4585 and then not Is_Controlled_Active (T)
4586 and then not Has_Controlled_Component (Base_Type (T))
4587 and then Expander_Active
4588 then
4589 Rewrite (N,
4590 Make_Object_Renaming_Declaration (Loc,
4591 Defining_Identifier => Id,
4592 Access_Definition => Empty,
4593 Subtype_Mark => New_Occurrence_Of
4594 (Base_Type (Etype (Id)), Loc),
4595 Name => E));
4596
4597 Set_Renamed_Object (Id, E);
4598
4599 -- Force generation of debugging information for the constant and for
4600 -- the renamed function call.
4601
4602 Set_Debug_Info_Needed (Id);
4603 Set_Debug_Info_Needed (Entity (Prefix (E)));
4604 end if;
4605
4606 if Present (Prev_Entity)
4607 and then Is_Frozen (Prev_Entity)
4608 and then not Error_Posted (Id)
4609 then
4610 Error_Msg_N ("full constant declaration appears too late", N);
4611 end if;
4612
4613 Check_Eliminated (Id);
4614
4615 -- Deal with setting In_Private_Part flag if in private part
4616
4617 if Ekind (Scope (Id)) = E_Package
4618 and then In_Private_Part (Scope (Id))
4619 then
4620 Set_In_Private_Part (Id);
4621 end if;
4622
4623 <<Leave>>
4624 -- Initialize the refined state of a variable here because this is a
4625 -- common destination for legal and illegal object declarations.
4626
4627 if Ekind (Id) = E_Variable then
4628 Set_Encapsulating_State (Id, Empty);
4629 end if;
4630
4631 if Has_Aspects (N) then
4632 Analyze_Aspect_Specifications (N, Id);
4633 end if;
4634
4635 Analyze_Dimension (N);
4636
4637 -- Verify whether the object declaration introduces an illegal hidden
4638 -- state within a package subject to a null abstract state.
4639
4640 if Ekind (Id) = E_Variable then
4641 Check_No_Hidden_State (Id);
4642 end if;
4643
4644 if Mode_Set then
4645 Restore_Ghost_Mode (Mode);
4646 end if;
4647 end Analyze_Object_Declaration;
4648
4649 ---------------------------
4650 -- Analyze_Others_Choice --
4651 ---------------------------
4652
4653 -- Nothing to do for the others choice node itself, the semantic analysis
4654 -- of the others choice will occur as part of the processing of the parent
4655
4656 procedure Analyze_Others_Choice (N : Node_Id) is
4657 pragma Warnings (Off, N);
4658 begin
4659 null;
4660 end Analyze_Others_Choice;
4661
4662 -------------------------------------------
4663 -- Analyze_Private_Extension_Declaration --
4664 -------------------------------------------
4665
4666 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4667 Indic : constant Node_Id := Subtype_Indication (N);
4668 T : constant Entity_Id := Defining_Identifier (N);
4669 Iface : Entity_Id;
4670 Iface_Elmt : Elmt_Id;
4671 Parent_Base : Entity_Id;
4672 Parent_Type : Entity_Id;
4673
4674 begin
4675 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4676
4677 if Is_Non_Empty_List (Interface_List (N)) then
4678 declare
4679 Intf : Node_Id;
4680 T : Entity_Id;
4681
4682 begin
4683 Intf := First (Interface_List (N));
4684 while Present (Intf) loop
4685 T := Find_Type_Of_Subtype_Indic (Intf);
4686
4687 Diagnose_Interface (Intf, T);
4688 Next (Intf);
4689 end loop;
4690 end;
4691 end if;
4692
4693 Generate_Definition (T);
4694
4695 -- For other than Ada 2012, just enter the name in the current scope
4696
4697 if Ada_Version < Ada_2012 then
4698 Enter_Name (T);
4699
4700 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4701 -- case of private type that completes an incomplete type.
4702
4703 else
4704 declare
4705 Prev : Entity_Id;
4706
4707 begin
4708 Prev := Find_Type_Name (N);
4709
4710 pragma Assert (Prev = T
4711 or else (Ekind (Prev) = E_Incomplete_Type
4712 and then Present (Full_View (Prev))
4713 and then Full_View (Prev) = T));
4714 end;
4715 end if;
4716
4717 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4718 Parent_Base := Base_Type (Parent_Type);
4719
4720 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4721 Set_Ekind (T, Ekind (Parent_Type));
4722 Set_Etype (T, Any_Type);
4723 goto Leave;
4724
4725 elsif not Is_Tagged_Type (Parent_Type) then
4726 Error_Msg_N
4727 ("parent of type extension must be a tagged type ", Indic);
4728 goto Leave;
4729
4730 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4731 Error_Msg_N ("premature derivation of incomplete type", Indic);
4732 goto Leave;
4733
4734 elsif Is_Concurrent_Type (Parent_Type) then
4735 Error_Msg_N
4736 ("parent type of a private extension cannot be a synchronized "
4737 & "tagged type (RM 3.9.1 (3/1))", N);
4738
4739 Set_Etype (T, Any_Type);
4740 Set_Ekind (T, E_Limited_Private_Type);
4741 Set_Private_Dependents (T, New_Elmt_List);
4742 Set_Error_Posted (T);
4743 goto Leave;
4744 end if;
4745
4746 -- Perhaps the parent type should be changed to the class-wide type's
4747 -- specific type in this case to prevent cascading errors ???
4748
4749 if Is_Class_Wide_Type (Parent_Type) then
4750 Error_Msg_N
4751 ("parent of type extension must not be a class-wide type", Indic);
4752 goto Leave;
4753 end if;
4754
4755 if (not Is_Package_Or_Generic_Package (Current_Scope)
4756 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4757 or else In_Private_Part (Current_Scope)
4758 then
4759 Error_Msg_N ("invalid context for private extension", N);
4760 end if;
4761
4762 -- Set common attributes
4763
4764 Set_Is_Pure (T, Is_Pure (Current_Scope));
4765 Set_Scope (T, Current_Scope);
4766 Set_Ekind (T, E_Record_Type_With_Private);
4767 Init_Size_Align (T);
4768 Set_Default_SSO (T);
4769
4770 Set_Etype (T, Parent_Base);
4771 Propagate_Concurrent_Flags (T, Parent_Base);
4772
4773 Set_Convention (T, Convention (Parent_Type));
4774 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4775 Set_Is_First_Subtype (T);
4776 Make_Class_Wide_Type (T);
4777
4778 if Unknown_Discriminants_Present (N) then
4779 Set_Discriminant_Constraint (T, No_Elist);
4780 end if;
4781
4782 Build_Derived_Record_Type (N, Parent_Type, T);
4783
4784 -- A private extension inherits the Default_Initial_Condition pragma
4785 -- coming from any parent type within the derivation chain.
4786
4787 if Has_DIC (Parent_Type) then
4788 Set_Has_Inherited_DIC (T);
4789 end if;
4790
4791 -- A private extension inherits any class-wide invariants coming from a
4792 -- parent type or an interface. Note that the invariant procedure of the
4793 -- parent type should not be inherited because the private extension may
4794 -- define invariants of its own.
4795
4796 if Has_Inherited_Invariants (Parent_Type)
4797 or else Has_Inheritable_Invariants (Parent_Type)
4798 then
4799 Set_Has_Inherited_Invariants (T);
4800
4801 elsif Present (Interfaces (T)) then
4802 Iface_Elmt := First_Elmt (Interfaces (T));
4803 while Present (Iface_Elmt) loop
4804 Iface := Node (Iface_Elmt);
4805
4806 if Has_Inheritable_Invariants (Iface) then
4807 Set_Has_Inherited_Invariants (T);
4808 exit;
4809 end if;
4810
4811 Next_Elmt (Iface_Elmt);
4812 end loop;
4813 end if;
4814
4815 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4816 -- synchronized formal derived type.
4817
4818 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4819 Set_Is_Limited_Record (T);
4820
4821 -- Formal derived type case
4822
4823 if Is_Generic_Type (T) then
4824
4825 -- The parent must be a tagged limited type or a synchronized
4826 -- interface.
4827
4828 if (not Is_Tagged_Type (Parent_Type)
4829 or else not Is_Limited_Type (Parent_Type))
4830 and then
4831 (not Is_Interface (Parent_Type)
4832 or else not Is_Synchronized_Interface (Parent_Type))
4833 then
4834 Error_Msg_NE
4835 ("parent type of & must be tagged limited or synchronized",
4836 N, T);
4837 end if;
4838
4839 -- The progenitors (if any) must be limited or synchronized
4840 -- interfaces.
4841
4842 if Present (Interfaces (T)) then
4843 Iface_Elmt := First_Elmt (Interfaces (T));
4844 while Present (Iface_Elmt) loop
4845 Iface := Node (Iface_Elmt);
4846
4847 if not Is_Limited_Interface (Iface)
4848 and then not Is_Synchronized_Interface (Iface)
4849 then
4850 Error_Msg_NE
4851 ("progenitor & must be limited or synchronized",
4852 N, Iface);
4853 end if;
4854
4855 Next_Elmt (Iface_Elmt);
4856 end loop;
4857 end if;
4858
4859 -- Regular derived extension, the parent must be a limited or
4860 -- synchronized interface.
4861
4862 else
4863 if not Is_Interface (Parent_Type)
4864 or else (not Is_Limited_Interface (Parent_Type)
4865 and then not Is_Synchronized_Interface (Parent_Type))
4866 then
4867 Error_Msg_NE
4868 ("parent type of & must be limited interface", N, T);
4869 end if;
4870 end if;
4871
4872 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4873 -- extension with a synchronized parent must be explicitly declared
4874 -- synchronized, because the full view will be a synchronized type.
4875 -- This must be checked before the check for limited types below,
4876 -- to ensure that types declared limited are not allowed to extend
4877 -- synchronized interfaces.
4878
4879 elsif Is_Interface (Parent_Type)
4880 and then Is_Synchronized_Interface (Parent_Type)
4881 and then not Synchronized_Present (N)
4882 then
4883 Error_Msg_NE
4884 ("private extension of& must be explicitly synchronized",
4885 N, Parent_Type);
4886
4887 elsif Limited_Present (N) then
4888 Set_Is_Limited_Record (T);
4889
4890 if not Is_Limited_Type (Parent_Type)
4891 and then
4892 (not Is_Interface (Parent_Type)
4893 or else not Is_Limited_Interface (Parent_Type))
4894 then
4895 Error_Msg_NE ("parent type& of limited extension must be limited",
4896 N, Parent_Type);
4897 end if;
4898 end if;
4899
4900 <<Leave>>
4901 if Has_Aspects (N) then
4902 Analyze_Aspect_Specifications (N, T);
4903 end if;
4904 end Analyze_Private_Extension_Declaration;
4905
4906 ---------------------------------
4907 -- Analyze_Subtype_Declaration --
4908 ---------------------------------
4909
4910 procedure Analyze_Subtype_Declaration
4911 (N : Node_Id;
4912 Skip : Boolean := False)
4913 is
4914 Id : constant Entity_Id := Defining_Identifier (N);
4915 R_Checks : Check_Result;
4916 T : Entity_Id;
4917
4918 begin
4919 Generate_Definition (Id);
4920 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4921 Init_Size_Align (Id);
4922
4923 -- The following guard condition on Enter_Name is to handle cases where
4924 -- the defining identifier has already been entered into the scope but
4925 -- the declaration as a whole needs to be analyzed.
4926
4927 -- This case in particular happens for derived enumeration types. The
4928 -- derived enumeration type is processed as an inserted enumeration type
4929 -- declaration followed by a rewritten subtype declaration. The defining
4930 -- identifier, however, is entered into the name scope very early in the
4931 -- processing of the original type declaration and therefore needs to be
4932 -- avoided here, when the created subtype declaration is analyzed. (See
4933 -- Build_Derived_Types)
4934
4935 -- This also happens when the full view of a private type is derived
4936 -- type with constraints. In this case the entity has been introduced
4937 -- in the private declaration.
4938
4939 -- Finally this happens in some complex cases when validity checks are
4940 -- enabled, where the same subtype declaration may be analyzed twice.
4941 -- This can happen if the subtype is created by the pre-analysis of
4942 -- an attribute tht gives the range of a loop statement, and the loop
4943 -- itself appears within an if_statement that will be rewritten during
4944 -- expansion.
4945
4946 if Skip
4947 or else (Present (Etype (Id))
4948 and then (Is_Private_Type (Etype (Id))
4949 or else Is_Task_Type (Etype (Id))
4950 or else Is_Rewrite_Substitution (N)))
4951 then
4952 null;
4953
4954 elsif Current_Entity (Id) = Id then
4955 null;
4956
4957 else
4958 Enter_Name (Id);
4959 end if;
4960
4961 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4962
4963 -- Class-wide equivalent types of records with unknown discriminants
4964 -- involve the generation of an itype which serves as the private view
4965 -- of a constrained record subtype. In such cases the base type of the
4966 -- current subtype we are processing is the private itype. Use the full
4967 -- of the private itype when decorating various attributes.
4968
4969 if Is_Itype (T)
4970 and then Is_Private_Type (T)
4971 and then Present (Full_View (T))
4972 then
4973 T := Full_View (T);
4974 end if;
4975
4976 -- Inherit common attributes
4977
4978 Set_Is_Volatile (Id, Is_Volatile (T));
4979 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4980 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4981 Set_Convention (Id, Convention (T));
4982
4983 -- If ancestor has predicates then so does the subtype, and in addition
4984 -- we must delay the freeze to properly arrange predicate inheritance.
4985
4986 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4987 -- in which T = ID, so the above tests and assignments do nothing???
4988
4989 if Has_Predicates (T)
4990 or else (Present (Ancestor_Subtype (T))
4991 and then Has_Predicates (Ancestor_Subtype (T)))
4992 then
4993 Set_Has_Predicates (Id);
4994 Set_Has_Delayed_Freeze (Id);
4995
4996 -- Generated subtypes inherit the predicate function from the parent
4997 -- (no aspects to examine on the generated declaration).
4998
4999 if not Comes_From_Source (N) then
5000 Set_Ekind (Id, Ekind (T));
5001
5002 if Present (Predicate_Function (T)) then
5003 Set_Predicate_Function (Id, Predicate_Function (T));
5004
5005 elsif Present (Ancestor_Subtype (T))
5006 and then Has_Predicates (Ancestor_Subtype (T))
5007 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5008 then
5009 Set_Predicate_Function (Id,
5010 Predicate_Function (Ancestor_Subtype (T)));
5011 end if;
5012 end if;
5013 end if;
5014
5015 -- Subtype of Boolean cannot have a constraint in SPARK
5016
5017 if Is_Boolean_Type (T)
5018 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5019 then
5020 Check_SPARK_05_Restriction
5021 ("subtype of Boolean cannot have constraint", N);
5022 end if;
5023
5024 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5025 declare
5026 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5027 One_Cstr : Node_Id;
5028 Low : Node_Id;
5029 High : Node_Id;
5030
5031 begin
5032 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5033 One_Cstr := First (Constraints (Cstr));
5034 while Present (One_Cstr) loop
5035
5036 -- Index or discriminant constraint in SPARK must be a
5037 -- subtype mark.
5038
5039 if not
5040 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5041 then
5042 Check_SPARK_05_Restriction
5043 ("subtype mark required", One_Cstr);
5044
5045 -- String subtype must have a lower bound of 1 in SPARK.
5046 -- Note that we do not need to test for the non-static case
5047 -- here, since that was already taken care of in
5048 -- Process_Range_Expr_In_Decl.
5049
5050 elsif Base_Type (T) = Standard_String then
5051 Get_Index_Bounds (One_Cstr, Low, High);
5052
5053 if Is_OK_Static_Expression (Low)
5054 and then Expr_Value (Low) /= 1
5055 then
5056 Check_SPARK_05_Restriction
5057 ("String subtype must have lower bound of 1", N);
5058 end if;
5059 end if;
5060
5061 Next (One_Cstr);
5062 end loop;
5063 end if;
5064 end;
5065 end if;
5066
5067 -- In the case where there is no constraint given in the subtype
5068 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5069 -- semantic attributes must be established here.
5070
5071 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5072 Set_Etype (Id, Base_Type (T));
5073
5074 -- Subtype of unconstrained array without constraint is not allowed
5075 -- in SPARK.
5076
5077 if Is_Array_Type (T) and then not Is_Constrained (T) then
5078 Check_SPARK_05_Restriction
5079 ("subtype of unconstrained array must have constraint", N);
5080 end if;
5081
5082 case Ekind (T) is
5083 when Array_Kind =>
5084 Set_Ekind (Id, E_Array_Subtype);
5085 Copy_Array_Subtype_Attributes (Id, T);
5086
5087 when Decimal_Fixed_Point_Kind =>
5088 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5089 Set_Digits_Value (Id, Digits_Value (T));
5090 Set_Delta_Value (Id, Delta_Value (T));
5091 Set_Scale_Value (Id, Scale_Value (T));
5092 Set_Small_Value (Id, Small_Value (T));
5093 Set_Scalar_Range (Id, Scalar_Range (T));
5094 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5095 Set_Is_Constrained (Id, Is_Constrained (T));
5096 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5097 Set_RM_Size (Id, RM_Size (T));
5098
5099 when Enumeration_Kind =>
5100 Set_Ekind (Id, E_Enumeration_Subtype);
5101 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5102 Set_Scalar_Range (Id, Scalar_Range (T));
5103 Set_Is_Character_Type (Id, Is_Character_Type (T));
5104 Set_Is_Constrained (Id, Is_Constrained (T));
5105 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5106 Set_RM_Size (Id, RM_Size (T));
5107 Inherit_Predicate_Flags (Id, T);
5108
5109 when Ordinary_Fixed_Point_Kind =>
5110 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5111 Set_Scalar_Range (Id, Scalar_Range (T));
5112 Set_Small_Value (Id, Small_Value (T));
5113 Set_Delta_Value (Id, Delta_Value (T));
5114 Set_Is_Constrained (Id, Is_Constrained (T));
5115 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5116 Set_RM_Size (Id, RM_Size (T));
5117
5118 when Float_Kind =>
5119 Set_Ekind (Id, E_Floating_Point_Subtype);
5120 Set_Scalar_Range (Id, Scalar_Range (T));
5121 Set_Digits_Value (Id, Digits_Value (T));
5122 Set_Is_Constrained (Id, Is_Constrained (T));
5123
5124 -- If the floating point type has dimensions, these will be
5125 -- inherited subsequently when Analyze_Dimensions is called.
5126
5127 when Signed_Integer_Kind =>
5128 Set_Ekind (Id, E_Signed_Integer_Subtype);
5129 Set_Scalar_Range (Id, Scalar_Range (T));
5130 Set_Is_Constrained (Id, Is_Constrained (T));
5131 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5132 Set_RM_Size (Id, RM_Size (T));
5133 Inherit_Predicate_Flags (Id, T);
5134
5135 when Modular_Integer_Kind =>
5136 Set_Ekind (Id, E_Modular_Integer_Subtype);
5137 Set_Scalar_Range (Id, Scalar_Range (T));
5138 Set_Is_Constrained (Id, Is_Constrained (T));
5139 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5140 Set_RM_Size (Id, RM_Size (T));
5141 Inherit_Predicate_Flags (Id, T);
5142
5143 when Class_Wide_Kind =>
5144 Set_Ekind (Id, E_Class_Wide_Subtype);
5145 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5146 Set_Cloned_Subtype (Id, T);
5147 Set_Is_Tagged_Type (Id, True);
5148 Set_Has_Unknown_Discriminants
5149 (Id, True);
5150 Set_No_Tagged_Streams_Pragma
5151 (Id, No_Tagged_Streams_Pragma (T));
5152
5153 if Ekind (T) = E_Class_Wide_Subtype then
5154 Set_Equivalent_Type (Id, Equivalent_Type (T));
5155 end if;
5156
5157 when E_Record_Subtype
5158 | E_Record_Type
5159 =>
5160 Set_Ekind (Id, E_Record_Subtype);
5161
5162 if Ekind (T) = E_Record_Subtype
5163 and then Present (Cloned_Subtype (T))
5164 then
5165 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5166 else
5167 Set_Cloned_Subtype (Id, T);
5168 end if;
5169
5170 Set_First_Entity (Id, First_Entity (T));
5171 Set_Last_Entity (Id, Last_Entity (T));
5172 Set_Has_Discriminants (Id, Has_Discriminants (T));
5173 Set_Is_Constrained (Id, Is_Constrained (T));
5174 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5175 Set_Has_Implicit_Dereference
5176 (Id, Has_Implicit_Dereference (T));
5177 Set_Has_Unknown_Discriminants
5178 (Id, Has_Unknown_Discriminants (T));
5179
5180 if Has_Discriminants (T) then
5181 Set_Discriminant_Constraint
5182 (Id, Discriminant_Constraint (T));
5183 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5184
5185 elsif Has_Unknown_Discriminants (Id) then
5186 Set_Discriminant_Constraint (Id, No_Elist);
5187 end if;
5188
5189 if Is_Tagged_Type (T) then
5190 Set_Is_Tagged_Type (Id, True);
5191 Set_No_Tagged_Streams_Pragma
5192 (Id, No_Tagged_Streams_Pragma (T));
5193 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5194 Set_Direct_Primitive_Operations
5195 (Id, Direct_Primitive_Operations (T));
5196 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5197
5198 if Is_Interface (T) then
5199 Set_Is_Interface (Id);
5200 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5201 end if;
5202 end if;
5203
5204 when Private_Kind =>
5205 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5206 Set_Has_Discriminants (Id, Has_Discriminants (T));
5207 Set_Is_Constrained (Id, Is_Constrained (T));
5208 Set_First_Entity (Id, First_Entity (T));
5209 Set_Last_Entity (Id, Last_Entity (T));
5210 Set_Private_Dependents (Id, New_Elmt_List);
5211 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5212 Set_Has_Implicit_Dereference
5213 (Id, Has_Implicit_Dereference (T));
5214 Set_Has_Unknown_Discriminants
5215 (Id, Has_Unknown_Discriminants (T));
5216 Set_Known_To_Have_Preelab_Init
5217 (Id, Known_To_Have_Preelab_Init (T));
5218
5219 if Is_Tagged_Type (T) then
5220 Set_Is_Tagged_Type (Id);
5221 Set_No_Tagged_Streams_Pragma (Id,
5222 No_Tagged_Streams_Pragma (T));
5223 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5224 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5225 Set_Direct_Primitive_Operations (Id,
5226 Direct_Primitive_Operations (T));
5227 end if;
5228
5229 -- In general the attributes of the subtype of a private type
5230 -- are the attributes of the partial view of parent. However,
5231 -- the full view may be a discriminated type, and the subtype
5232 -- must share the discriminant constraint to generate correct
5233 -- calls to initialization procedures.
5234
5235 if Has_Discriminants (T) then
5236 Set_Discriminant_Constraint
5237 (Id, Discriminant_Constraint (T));
5238 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5239
5240 elsif Present (Full_View (T))
5241 and then Has_Discriminants (Full_View (T))
5242 then
5243 Set_Discriminant_Constraint
5244 (Id, Discriminant_Constraint (Full_View (T)));
5245 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5246
5247 -- This would seem semantically correct, but apparently
5248 -- generates spurious errors about missing components ???
5249
5250 -- Set_Has_Discriminants (Id);
5251 end if;
5252
5253 Prepare_Private_Subtype_Completion (Id, N);
5254
5255 -- If this is the subtype of a constrained private type with
5256 -- discriminants that has got a full view and we also have
5257 -- built a completion just above, show that the completion
5258 -- is a clone of the full view to the back-end.
5259
5260 if Has_Discriminants (T)
5261 and then not Has_Unknown_Discriminants (T)
5262 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5263 and then Present (Full_View (T))
5264 and then Present (Full_View (Id))
5265 then
5266 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5267 end if;
5268
5269 when Access_Kind =>
5270 Set_Ekind (Id, E_Access_Subtype);
5271 Set_Is_Constrained (Id, Is_Constrained (T));
5272 Set_Is_Access_Constant
5273 (Id, Is_Access_Constant (T));
5274 Set_Directly_Designated_Type
5275 (Id, Designated_Type (T));
5276 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5277
5278 -- A Pure library_item must not contain the declaration of a
5279 -- named access type, except within a subprogram, generic
5280 -- subprogram, task unit, or protected unit, or if it has
5281 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5282
5283 if Comes_From_Source (Id)
5284 and then In_Pure_Unit
5285 and then not In_Subprogram_Task_Protected_Unit
5286 and then not No_Pool_Assigned (Id)
5287 then
5288 Error_Msg_N
5289 ("named access types not allowed in pure unit", N);
5290 end if;
5291
5292 when Concurrent_Kind =>
5293 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5294 Set_Corresponding_Record_Type (Id,
5295 Corresponding_Record_Type (T));
5296 Set_First_Entity (Id, First_Entity (T));
5297 Set_First_Private_Entity (Id, First_Private_Entity (T));
5298 Set_Has_Discriminants (Id, Has_Discriminants (T));
5299 Set_Is_Constrained (Id, Is_Constrained (T));
5300 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5301 Set_Last_Entity (Id, Last_Entity (T));
5302
5303 if Is_Tagged_Type (T) then
5304 Set_No_Tagged_Streams_Pragma
5305 (Id, No_Tagged_Streams_Pragma (T));
5306 end if;
5307
5308 if Has_Discriminants (T) then
5309 Set_Discriminant_Constraint
5310 (Id, Discriminant_Constraint (T));
5311 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5312 end if;
5313
5314 when Incomplete_Kind =>
5315 if Ada_Version >= Ada_2005 then
5316
5317 -- In Ada 2005 an incomplete type can be explicitly tagged:
5318 -- propagate indication. Note that we also have to include
5319 -- subtypes for Ada 2012 extended use of incomplete types.
5320
5321 Set_Ekind (Id, E_Incomplete_Subtype);
5322 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5323 Set_Private_Dependents (Id, New_Elmt_List);
5324
5325 if Is_Tagged_Type (Id) then
5326 Set_No_Tagged_Streams_Pragma
5327 (Id, No_Tagged_Streams_Pragma (T));
5328 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5329 end if;
5330
5331 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5332 -- incomplete type visible through a limited with clause.
5333
5334 if From_Limited_With (T)
5335 and then Present (Non_Limited_View (T))
5336 then
5337 Set_From_Limited_With (Id);
5338 Set_Non_Limited_View (Id, Non_Limited_View (T));
5339
5340 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5341 -- to the private dependents of the original incomplete
5342 -- type for future transformation.
5343
5344 else
5345 Append_Elmt (Id, Private_Dependents (T));
5346 end if;
5347
5348 -- If the subtype name denotes an incomplete type an error
5349 -- was already reported by Process_Subtype.
5350
5351 else
5352 Set_Etype (Id, Any_Type);
5353 end if;
5354
5355 when others =>
5356 raise Program_Error;
5357 end case;
5358 end if;
5359
5360 if Etype (Id) = Any_Type then
5361 goto Leave;
5362 end if;
5363
5364 -- Some common processing on all types
5365
5366 Set_Size_Info (Id, T);
5367 Set_First_Rep_Item (Id, First_Rep_Item (T));
5368
5369 -- If the parent type is a generic actual, so is the subtype. This may
5370 -- happen in a nested instance. Why Comes_From_Source test???
5371
5372 if not Comes_From_Source (N) then
5373 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5374 end if;
5375
5376 -- If this is a subtype declaration for an actual in an instance,
5377 -- inherit static and dynamic predicates if any.
5378
5379 -- If declaration has no aspect specifications, inherit predicate
5380 -- info as well. Unclear how to handle the case of both specified
5381 -- and inherited predicates ??? Other inherited aspects, such as
5382 -- invariants, should be OK, but the combination with later pragmas
5383 -- may also require special merging.
5384
5385 if Has_Predicates (T)
5386 and then Present (Predicate_Function (T))
5387 and then
5388 ((In_Instance and then not Comes_From_Source (N))
5389 or else No (Aspect_Specifications (N)))
5390 then
5391 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5392
5393 if Has_Static_Predicate (T) then
5394 Set_Has_Static_Predicate (Id);
5395 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5396 end if;
5397 end if;
5398
5399 -- Remaining processing depends on characteristics of base type
5400
5401 T := Etype (Id);
5402
5403 Set_Is_Immediately_Visible (Id, True);
5404 Set_Depends_On_Private (Id, Has_Private_Component (T));
5405 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5406
5407 if Is_Interface (T) then
5408 Set_Is_Interface (Id);
5409 end if;
5410
5411 if Present (Generic_Parent_Type (N))
5412 and then
5413 (Nkind (Parent (Generic_Parent_Type (N))) /=
5414 N_Formal_Type_Declaration
5415 or else Nkind (Formal_Type_Definition
5416 (Parent (Generic_Parent_Type (N)))) /=
5417 N_Formal_Private_Type_Definition)
5418 then
5419 if Is_Tagged_Type (Id) then
5420
5421 -- If this is a generic actual subtype for a synchronized type,
5422 -- the primitive operations are those of the corresponding record
5423 -- for which there is a separate subtype declaration.
5424
5425 if Is_Concurrent_Type (Id) then
5426 null;
5427 elsif Is_Class_Wide_Type (Id) then
5428 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5429 else
5430 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5431 end if;
5432
5433 elsif Scope (Etype (Id)) /= Standard_Standard then
5434 Derive_Subprograms (Generic_Parent_Type (N), Id);
5435 end if;
5436 end if;
5437
5438 if Is_Private_Type (T) and then Present (Full_View (T)) then
5439 Conditional_Delay (Id, Full_View (T));
5440
5441 -- The subtypes of components or subcomponents of protected types
5442 -- do not need freeze nodes, which would otherwise appear in the
5443 -- wrong scope (before the freeze node for the protected type). The
5444 -- proper subtypes are those of the subcomponents of the corresponding
5445 -- record.
5446
5447 elsif Ekind (Scope (Id)) /= E_Protected_Type
5448 and then Present (Scope (Scope (Id))) -- error defense
5449 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5450 then
5451 Conditional_Delay (Id, T);
5452 end if;
5453
5454 -- Check that Constraint_Error is raised for a scalar subtype indication
5455 -- when the lower or upper bound of a non-null range lies outside the
5456 -- range of the type mark.
5457
5458 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5459 if Is_Scalar_Type (Etype (Id))
5460 and then Scalar_Range (Id) /=
5461 Scalar_Range
5462 (Etype (Subtype_Mark (Subtype_Indication (N))))
5463 then
5464 Apply_Range_Check
5465 (Scalar_Range (Id),
5466 Etype (Subtype_Mark (Subtype_Indication (N))));
5467
5468 -- In the array case, check compatibility for each index
5469
5470 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5471 then
5472 -- This really should be a subprogram that finds the indications
5473 -- to check???
5474
5475 declare
5476 Subt_Index : Node_Id := First_Index (Id);
5477 Target_Index : Node_Id :=
5478 First_Index (Etype
5479 (Subtype_Mark (Subtype_Indication (N))));
5480 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5481
5482 begin
5483 while Present (Subt_Index) loop
5484 if ((Nkind (Subt_Index) = N_Identifier
5485 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5486 or else Nkind (Subt_Index) = N_Subtype_Indication)
5487 and then
5488 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5489 then
5490 declare
5491 Target_Typ : constant Entity_Id :=
5492 Etype (Target_Index);
5493 begin
5494 R_Checks :=
5495 Get_Range_Checks
5496 (Scalar_Range (Etype (Subt_Index)),
5497 Target_Typ,
5498 Etype (Subt_Index),
5499 Defining_Identifier (N));
5500
5501 -- Reset Has_Dynamic_Range_Check on the subtype to
5502 -- prevent elision of the index check due to a dynamic
5503 -- check generated for a preceding index (needed since
5504 -- Insert_Range_Checks tries to avoid generating
5505 -- redundant checks on a given declaration).
5506
5507 Set_Has_Dynamic_Range_Check (N, False);
5508
5509 Insert_Range_Checks
5510 (R_Checks,
5511 N,
5512 Target_Typ,
5513 Sloc (Defining_Identifier (N)));
5514
5515 -- Record whether this index involved a dynamic check
5516
5517 Has_Dyn_Chk :=
5518 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5519 end;
5520 end if;
5521
5522 Next_Index (Subt_Index);
5523 Next_Index (Target_Index);
5524 end loop;
5525
5526 -- Finally, mark whether the subtype involves dynamic checks
5527
5528 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5529 end;
5530 end if;
5531 end if;
5532
5533 Set_Optimize_Alignment_Flags (Id);
5534 Check_Eliminated (Id);
5535
5536 <<Leave>>
5537 if Has_Aspects (N) then
5538 Analyze_Aspect_Specifications (N, Id);
5539 end if;
5540
5541 Analyze_Dimension (N);
5542
5543 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5544 -- indications on composite types where the constraints are dynamic.
5545 -- Note that object declarations and aggregates generate implicit
5546 -- subtype declarations, which this covers. One special case is that the
5547 -- implicitly generated "=" for discriminated types includes an
5548 -- offending subtype declaration, which is harmless, so we ignore it
5549 -- here.
5550
5551 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5552 declare
5553 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5554 begin
5555 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5556 and then not (Is_Internal (Id)
5557 and then Is_TSS (Scope (Id),
5558 TSS_Composite_Equality))
5559 and then not Within_Init_Proc
5560 and then not All_Composite_Constraints_Static (Cstr)
5561 then
5562 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5563 end if;
5564 end;
5565 end if;
5566 end Analyze_Subtype_Declaration;
5567
5568 --------------------------------
5569 -- Analyze_Subtype_Indication --
5570 --------------------------------
5571
5572 procedure Analyze_Subtype_Indication (N : Node_Id) is
5573 T : constant Entity_Id := Subtype_Mark (N);
5574 R : constant Node_Id := Range_Expression (Constraint (N));
5575
5576 begin
5577 Analyze (T);
5578
5579 if R /= Error then
5580 Analyze (R);
5581 Set_Etype (N, Etype (R));
5582 Resolve (R, Entity (T));
5583 else
5584 Set_Error_Posted (R);
5585 Set_Error_Posted (T);
5586 end if;
5587 end Analyze_Subtype_Indication;
5588
5589 --------------------------
5590 -- Analyze_Variant_Part --
5591 --------------------------
5592
5593 procedure Analyze_Variant_Part (N : Node_Id) is
5594 Discr_Name : Node_Id;
5595 Discr_Type : Entity_Id;
5596
5597 procedure Process_Variant (A : Node_Id);
5598 -- Analyze declarations for a single variant
5599
5600 package Analyze_Variant_Choices is
5601 new Generic_Analyze_Choices (Process_Variant);
5602 use Analyze_Variant_Choices;
5603
5604 ---------------------
5605 -- Process_Variant --
5606 ---------------------
5607
5608 procedure Process_Variant (A : Node_Id) is
5609 CL : constant Node_Id := Component_List (A);
5610 begin
5611 if not Null_Present (CL) then
5612 Analyze_Declarations (Component_Items (CL));
5613
5614 if Present (Variant_Part (CL)) then
5615 Analyze (Variant_Part (CL));
5616 end if;
5617 end if;
5618 end Process_Variant;
5619
5620 -- Start of processing for Analyze_Variant_Part
5621
5622 begin
5623 Discr_Name := Name (N);
5624 Analyze (Discr_Name);
5625
5626 -- If Discr_Name bad, get out (prevent cascaded errors)
5627
5628 if Etype (Discr_Name) = Any_Type then
5629 return;
5630 end if;
5631
5632 -- Check invalid discriminant in variant part
5633
5634 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5635 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5636 end if;
5637
5638 Discr_Type := Etype (Entity (Discr_Name));
5639
5640 if not Is_Discrete_Type (Discr_Type) then
5641 Error_Msg_N
5642 ("discriminant in a variant part must be of a discrete type",
5643 Name (N));
5644 return;
5645 end if;
5646
5647 -- Now analyze the choices, which also analyzes the declarations that
5648 -- are associated with each choice.
5649
5650 Analyze_Choices (Variants (N), Discr_Type);
5651
5652 -- Note: we used to instantiate and call Check_Choices here to check
5653 -- that the choices covered the discriminant, but it's too early to do
5654 -- that because of statically predicated subtypes, whose analysis may
5655 -- be deferred to their freeze point which may be as late as the freeze
5656 -- point of the containing record. So this call is now to be found in
5657 -- Freeze_Record_Declaration.
5658
5659 end Analyze_Variant_Part;
5660
5661 ----------------------------
5662 -- Array_Type_Declaration --
5663 ----------------------------
5664
5665 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5666 Component_Def : constant Node_Id := Component_Definition (Def);
5667 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5668 P : constant Node_Id := Parent (Def);
5669 Element_Type : Entity_Id;
5670 Implicit_Base : Entity_Id;
5671 Index : Node_Id;
5672 Nb_Index : Nat;
5673 Priv : Entity_Id;
5674 Related_Id : Entity_Id := Empty;
5675
5676 begin
5677 if Nkind (Def) = N_Constrained_Array_Definition then
5678 Index := First (Discrete_Subtype_Definitions (Def));
5679 else
5680 Index := First (Subtype_Marks (Def));
5681 end if;
5682
5683 -- Find proper names for the implicit types which may be public. In case
5684 -- of anonymous arrays we use the name of the first object of that type
5685 -- as prefix.
5686
5687 if No (T) then
5688 Related_Id := Defining_Identifier (P);
5689 else
5690 Related_Id := T;
5691 end if;
5692
5693 Nb_Index := 1;
5694 while Present (Index) loop
5695 Analyze (Index);
5696
5697 -- Test for odd case of trying to index a type by the type itself
5698
5699 if Is_Entity_Name (Index) and then Entity (Index) = T then
5700 Error_Msg_N ("type& cannot be indexed by itself", Index);
5701 Set_Entity (Index, Standard_Boolean);
5702 Set_Etype (Index, Standard_Boolean);
5703 end if;
5704
5705 -- Check SPARK restriction requiring a subtype mark
5706
5707 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5708 Check_SPARK_05_Restriction ("subtype mark required", Index);
5709 end if;
5710
5711 -- Add a subtype declaration for each index of private array type
5712 -- declaration whose etype is also private. For example:
5713
5714 -- package Pkg is
5715 -- type Index is private;
5716 -- private
5717 -- type Table is array (Index) of ...
5718 -- end;
5719
5720 -- This is currently required by the expander for the internally
5721 -- generated equality subprogram of records with variant parts in
5722 -- which the etype of some component is such private type.
5723
5724 if Ekind (Current_Scope) = E_Package
5725 and then In_Private_Part (Current_Scope)
5726 and then Has_Private_Declaration (Etype (Index))
5727 then
5728 declare
5729 Loc : constant Source_Ptr := Sloc (Def);
5730 Decl : Entity_Id;
5731 New_E : Entity_Id;
5732
5733 begin
5734 New_E := Make_Temporary (Loc, 'T');
5735 Set_Is_Internal (New_E);
5736
5737 Decl :=
5738 Make_Subtype_Declaration (Loc,
5739 Defining_Identifier => New_E,
5740 Subtype_Indication =>
5741 New_Occurrence_Of (Etype (Index), Loc));
5742
5743 Insert_Before (Parent (Def), Decl);
5744 Analyze (Decl);
5745 Set_Etype (Index, New_E);
5746
5747 -- If the index is a range the Entity attribute is not
5748 -- available. Example:
5749
5750 -- package Pkg is
5751 -- type T is private;
5752 -- private
5753 -- type T is new Natural;
5754 -- Table : array (T(1) .. T(10)) of Boolean;
5755 -- end Pkg;
5756
5757 if Nkind (Index) /= N_Range then
5758 Set_Entity (Index, New_E);
5759 end if;
5760 end;
5761 end if;
5762
5763 Make_Index (Index, P, Related_Id, Nb_Index);
5764
5765 -- Check error of subtype with predicate for index type
5766
5767 Bad_Predicated_Subtype_Use
5768 ("subtype& has predicate, not allowed as index subtype",
5769 Index, Etype (Index));
5770
5771 -- Move to next index
5772
5773 Next_Index (Index);
5774 Nb_Index := Nb_Index + 1;
5775 end loop;
5776
5777 -- Process subtype indication if one is present
5778
5779 if Present (Component_Typ) then
5780 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5781
5782 Set_Etype (Component_Typ, Element_Type);
5783
5784 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5785 Check_SPARK_05_Restriction
5786 ("subtype mark required", Component_Typ);
5787 end if;
5788
5789 -- Ada 2005 (AI-230): Access Definition case
5790
5791 else pragma Assert (Present (Access_Definition (Component_Def)));
5792
5793 -- Indicate that the anonymous access type is created by the
5794 -- array type declaration.
5795
5796 Element_Type := Access_Definition
5797 (Related_Nod => P,
5798 N => Access_Definition (Component_Def));
5799 Set_Is_Local_Anonymous_Access (Element_Type);
5800
5801 -- Propagate the parent. This field is needed if we have to generate
5802 -- the master_id associated with an anonymous access to task type
5803 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5804
5805 Set_Parent (Element_Type, Parent (T));
5806
5807 -- Ada 2005 (AI-230): In case of components that are anonymous access
5808 -- types the level of accessibility depends on the enclosing type
5809 -- declaration
5810
5811 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5812
5813 -- Ada 2005 (AI-254)
5814
5815 declare
5816 CD : constant Node_Id :=
5817 Access_To_Subprogram_Definition
5818 (Access_Definition (Component_Def));
5819 begin
5820 if Present (CD) and then Protected_Present (CD) then
5821 Element_Type :=
5822 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5823 end if;
5824 end;
5825 end if;
5826
5827 -- Constrained array case
5828
5829 if No (T) then
5830 T := Create_Itype (E_Void, P, Related_Id, 'T');
5831 end if;
5832
5833 if Nkind (Def) = N_Constrained_Array_Definition then
5834
5835 -- Establish Implicit_Base as unconstrained base type
5836
5837 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5838
5839 Set_Etype (Implicit_Base, Implicit_Base);
5840 Set_Scope (Implicit_Base, Current_Scope);
5841 Set_Has_Delayed_Freeze (Implicit_Base);
5842 Set_Default_SSO (Implicit_Base);
5843
5844 -- The constrained array type is a subtype of the unconstrained one
5845
5846 Set_Ekind (T, E_Array_Subtype);
5847 Init_Size_Align (T);
5848 Set_Etype (T, Implicit_Base);
5849 Set_Scope (T, Current_Scope);
5850 Set_Is_Constrained (T);
5851 Set_First_Index (T,
5852 First (Discrete_Subtype_Definitions (Def)));
5853 Set_Has_Delayed_Freeze (T);
5854
5855 -- Complete setup of implicit base type
5856
5857 Set_Component_Size (Implicit_Base, Uint_0);
5858 Set_Component_Type (Implicit_Base, Element_Type);
5859 Set_Finalize_Storage_Only
5860 (Implicit_Base,
5861 Finalize_Storage_Only (Element_Type));
5862 Set_First_Index (Implicit_Base, First_Index (T));
5863 Set_Has_Controlled_Component
5864 (Implicit_Base,
5865 Has_Controlled_Component (Element_Type)
5866 or else Is_Controlled_Active (Element_Type));
5867 Set_Packed_Array_Impl_Type
5868 (Implicit_Base, Empty);
5869
5870 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
5871
5872 -- Unconstrained array case
5873
5874 else
5875 Set_Ekind (T, E_Array_Type);
5876 Init_Size_Align (T);
5877 Set_Etype (T, T);
5878 Set_Scope (T, Current_Scope);
5879 Set_Component_Size (T, Uint_0);
5880 Set_Is_Constrained (T, False);
5881 Set_First_Index (T, First (Subtype_Marks (Def)));
5882 Set_Has_Delayed_Freeze (T, True);
5883 Propagate_Concurrent_Flags (T, Element_Type);
5884 Set_Has_Controlled_Component (T, Has_Controlled_Component
5885 (Element_Type)
5886 or else
5887 Is_Controlled_Active (Element_Type));
5888 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5889 (Element_Type));
5890 Set_Default_SSO (T);
5891 end if;
5892
5893 -- Common attributes for both cases
5894
5895 Set_Component_Type (Base_Type (T), Element_Type);
5896 Set_Packed_Array_Impl_Type (T, Empty);
5897
5898 if Aliased_Present (Component_Definition (Def)) then
5899 Check_SPARK_05_Restriction
5900 ("aliased is not allowed", Component_Definition (Def));
5901 Set_Has_Aliased_Components (Etype (T));
5902 end if;
5903
5904 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5905 -- array type to ensure that objects of this type are initialized.
5906
5907 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5908 Set_Can_Never_Be_Null (T);
5909
5910 if Null_Exclusion_Present (Component_Definition (Def))
5911
5912 -- No need to check itypes because in their case this check was
5913 -- done at their point of creation
5914
5915 and then not Is_Itype (Element_Type)
5916 then
5917 Error_Msg_N
5918 ("`NOT NULL` not allowed (null already excluded)",
5919 Subtype_Indication (Component_Definition (Def)));
5920 end if;
5921 end if;
5922
5923 Priv := Private_Component (Element_Type);
5924
5925 if Present (Priv) then
5926
5927 -- Check for circular definitions
5928
5929 if Priv = Any_Type then
5930 Set_Component_Type (Etype (T), Any_Type);
5931
5932 -- There is a gap in the visibility of operations on the composite
5933 -- type only if the component type is defined in a different scope.
5934
5935 elsif Scope (Priv) = Current_Scope then
5936 null;
5937
5938 elsif Is_Limited_Type (Priv) then
5939 Set_Is_Limited_Composite (Etype (T));
5940 Set_Is_Limited_Composite (T);
5941 else
5942 Set_Is_Private_Composite (Etype (T));
5943 Set_Is_Private_Composite (T);
5944 end if;
5945 end if;
5946
5947 -- A syntax error in the declaration itself may lead to an empty index
5948 -- list, in which case do a minimal patch.
5949
5950 if No (First_Index (T)) then
5951 Error_Msg_N ("missing index definition in array type declaration", T);
5952
5953 declare
5954 Indexes : constant List_Id :=
5955 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5956 begin
5957 Set_Discrete_Subtype_Definitions (Def, Indexes);
5958 Set_First_Index (T, First (Indexes));
5959 return;
5960 end;
5961 end if;
5962
5963 -- Create a concatenation operator for the new type. Internal array
5964 -- types created for packed entities do not need such, they are
5965 -- compatible with the user-defined type.
5966
5967 if Number_Dimensions (T) = 1
5968 and then not Is_Packed_Array_Impl_Type (T)
5969 then
5970 New_Concatenation_Op (T);
5971 end if;
5972
5973 -- In the case of an unconstrained array the parser has already verified
5974 -- that all the indexes are unconstrained but we still need to make sure
5975 -- that the element type is constrained.
5976
5977 if not Is_Definite_Subtype (Element_Type) then
5978 Error_Msg_N
5979 ("unconstrained element type in array declaration",
5980 Subtype_Indication (Component_Def));
5981
5982 elsif Is_Abstract_Type (Element_Type) then
5983 Error_Msg_N
5984 ("the type of a component cannot be abstract",
5985 Subtype_Indication (Component_Def));
5986 end if;
5987
5988 -- There may be an invariant declared for the component type, but
5989 -- the construction of the component invariant checking procedure
5990 -- takes place during expansion.
5991 end Array_Type_Declaration;
5992
5993 ------------------------------------------------------
5994 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5995 ------------------------------------------------------
5996
5997 function Replace_Anonymous_Access_To_Protected_Subprogram
5998 (N : Node_Id) return Entity_Id
5999 is
6000 Loc : constant Source_Ptr := Sloc (N);
6001
6002 Curr_Scope : constant Scope_Stack_Entry :=
6003 Scope_Stack.Table (Scope_Stack.Last);
6004
6005 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6006
6007 Acc : Node_Id;
6008 -- Access definition in declaration
6009
6010 Comp : Node_Id;
6011 -- Object definition or formal definition with an access definition
6012
6013 Decl : Node_Id;
6014 -- Declaration of anonymous access to subprogram type
6015
6016 Spec : Node_Id;
6017 -- Original specification in access to subprogram
6018
6019 P : Node_Id;
6020
6021 begin
6022 Set_Is_Internal (Anon);
6023
6024 case Nkind (N) is
6025 when N_Constrained_Array_Definition
6026 | N_Component_Declaration
6027 | N_Unconstrained_Array_Definition
6028 =>
6029 Comp := Component_Definition (N);
6030 Acc := Access_Definition (Comp);
6031
6032 when N_Discriminant_Specification =>
6033 Comp := Discriminant_Type (N);
6034 Acc := Comp;
6035
6036 when N_Parameter_Specification =>
6037 Comp := Parameter_Type (N);
6038 Acc := Comp;
6039
6040 when N_Access_Function_Definition =>
6041 Comp := Result_Definition (N);
6042 Acc := Comp;
6043
6044 when N_Object_Declaration =>
6045 Comp := Object_Definition (N);
6046 Acc := Comp;
6047
6048 when N_Function_Specification =>
6049 Comp := Result_Definition (N);
6050 Acc := Comp;
6051
6052 when others =>
6053 raise Program_Error;
6054 end case;
6055
6056 Spec := Access_To_Subprogram_Definition (Acc);
6057
6058 Decl :=
6059 Make_Full_Type_Declaration (Loc,
6060 Defining_Identifier => Anon,
6061 Type_Definition => Copy_Separate_Tree (Spec));
6062
6063 Mark_Rewrite_Insertion (Decl);
6064
6065 -- In ASIS mode, analyze the profile on the original node, because
6066 -- the separate copy does not provide enough links to recover the
6067 -- original tree. Analysis is limited to type annotations, within
6068 -- a temporary scope that serves as an anonymous subprogram to collect
6069 -- otherwise useless temporaries and itypes.
6070
6071 if ASIS_Mode then
6072 declare
6073 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6074
6075 begin
6076 if Nkind (Spec) = N_Access_Function_Definition then
6077 Set_Ekind (Typ, E_Function);
6078 else
6079 Set_Ekind (Typ, E_Procedure);
6080 end if;
6081
6082 Set_Parent (Typ, N);
6083 Set_Scope (Typ, Current_Scope);
6084 Push_Scope (Typ);
6085
6086 -- Nothing to do if procedure is parameterless
6087
6088 if Present (Parameter_Specifications (Spec)) then
6089 Process_Formals (Parameter_Specifications (Spec), Spec);
6090 end if;
6091
6092 if Nkind (Spec) = N_Access_Function_Definition then
6093 declare
6094 Def : constant Node_Id := Result_Definition (Spec);
6095
6096 begin
6097 -- The result might itself be an anonymous access type, so
6098 -- have to recurse.
6099
6100 if Nkind (Def) = N_Access_Definition then
6101 if Present (Access_To_Subprogram_Definition (Def)) then
6102 Set_Etype
6103 (Def,
6104 Replace_Anonymous_Access_To_Protected_Subprogram
6105 (Spec));
6106 else
6107 Find_Type (Subtype_Mark (Def));
6108 end if;
6109
6110 else
6111 Find_Type (Def);
6112 end if;
6113 end;
6114 end if;
6115
6116 End_Scope;
6117 end;
6118 end if;
6119
6120 -- Insert the new declaration in the nearest enclosing scope. If the
6121 -- parent is a body and N is its return type, the declaration belongs
6122 -- in the enclosing scope. Likewise if N is the type of a parameter.
6123
6124 P := Parent (N);
6125
6126 if Nkind (N) = N_Function_Specification
6127 and then Nkind (P) = N_Subprogram_Body
6128 then
6129 P := Parent (P);
6130 elsif Nkind (N) = N_Parameter_Specification
6131 and then Nkind (P) in N_Subprogram_Specification
6132 and then Nkind (Parent (P)) = N_Subprogram_Body
6133 then
6134 P := Parent (Parent (P));
6135 end if;
6136
6137 while Present (P) and then not Has_Declarations (P) loop
6138 P := Parent (P);
6139 end loop;
6140
6141 pragma Assert (Present (P));
6142
6143 if Nkind (P) = N_Package_Specification then
6144 Prepend (Decl, Visible_Declarations (P));
6145 else
6146 Prepend (Decl, Declarations (P));
6147 end if;
6148
6149 -- Replace the anonymous type with an occurrence of the new declaration.
6150 -- In all cases the rewritten node does not have the null-exclusion
6151 -- attribute because (if present) it was already inherited by the
6152 -- anonymous entity (Anon). Thus, in case of components we do not
6153 -- inherit this attribute.
6154
6155 if Nkind (N) = N_Parameter_Specification then
6156 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6157 Set_Etype (Defining_Identifier (N), Anon);
6158 Set_Null_Exclusion_Present (N, False);
6159
6160 elsif Nkind (N) = N_Object_Declaration then
6161 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6162 Set_Etype (Defining_Identifier (N), Anon);
6163
6164 elsif Nkind (N) = N_Access_Function_Definition then
6165 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6166
6167 elsif Nkind (N) = N_Function_Specification then
6168 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6169 Set_Etype (Defining_Unit_Name (N), Anon);
6170
6171 else
6172 Rewrite (Comp,
6173 Make_Component_Definition (Loc,
6174 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6175 end if;
6176
6177 Mark_Rewrite_Insertion (Comp);
6178
6179 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6180 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6181 and then not Is_Type (Current_Scope))
6182 then
6183
6184 -- Declaration can be analyzed in the current scope.
6185
6186 Analyze (Decl);
6187
6188 else
6189 -- Temporarily remove the current scope (record or subprogram) from
6190 -- the stack to add the new declarations to the enclosing scope.
6191 -- The anonymous entity is an Itype with the proper attributes.
6192
6193 Scope_Stack.Decrement_Last;
6194 Analyze (Decl);
6195 Set_Is_Itype (Anon);
6196 Set_Associated_Node_For_Itype (Anon, N);
6197 Scope_Stack.Append (Curr_Scope);
6198 end if;
6199
6200 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6201 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6202 return Anon;
6203 end Replace_Anonymous_Access_To_Protected_Subprogram;
6204
6205 -------------------------------
6206 -- Build_Derived_Access_Type --
6207 -------------------------------
6208
6209 procedure Build_Derived_Access_Type
6210 (N : Node_Id;
6211 Parent_Type : Entity_Id;
6212 Derived_Type : Entity_Id)
6213 is
6214 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6215
6216 Desig_Type : Entity_Id;
6217 Discr : Entity_Id;
6218 Discr_Con_Elist : Elist_Id;
6219 Discr_Con_El : Elmt_Id;
6220 Subt : Entity_Id;
6221
6222 begin
6223 -- Set the designated type so it is available in case this is an access
6224 -- to a self-referential type, e.g. a standard list type with a next
6225 -- pointer. Will be reset after subtype is built.
6226
6227 Set_Directly_Designated_Type
6228 (Derived_Type, Designated_Type (Parent_Type));
6229
6230 Subt := Process_Subtype (S, N);
6231
6232 if Nkind (S) /= N_Subtype_Indication
6233 and then Subt /= Base_Type (Subt)
6234 then
6235 Set_Ekind (Derived_Type, E_Access_Subtype);
6236 end if;
6237
6238 if Ekind (Derived_Type) = E_Access_Subtype then
6239 declare
6240 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6241 Ibase : constant Entity_Id :=
6242 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6243 Svg_Chars : constant Name_Id := Chars (Ibase);
6244 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6245
6246 begin
6247 Copy_Node (Pbase, Ibase);
6248
6249 -- Restore Itype status after Copy_Node
6250
6251 Set_Is_Itype (Ibase);
6252 Set_Associated_Node_For_Itype (Ibase, N);
6253
6254 Set_Chars (Ibase, Svg_Chars);
6255 Set_Next_Entity (Ibase, Svg_Next_E);
6256 Set_Sloc (Ibase, Sloc (Derived_Type));
6257 Set_Scope (Ibase, Scope (Derived_Type));
6258 Set_Freeze_Node (Ibase, Empty);
6259 Set_Is_Frozen (Ibase, False);
6260 Set_Comes_From_Source (Ibase, False);
6261 Set_Is_First_Subtype (Ibase, False);
6262
6263 Set_Etype (Ibase, Pbase);
6264 Set_Etype (Derived_Type, Ibase);
6265 end;
6266 end if;
6267
6268 Set_Directly_Designated_Type
6269 (Derived_Type, Designated_Type (Subt));
6270
6271 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6272 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6273 Set_Size_Info (Derived_Type, Parent_Type);
6274 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6275 Set_Depends_On_Private (Derived_Type,
6276 Has_Private_Component (Derived_Type));
6277 Conditional_Delay (Derived_Type, Subt);
6278
6279 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6280 -- that it is not redundant.
6281
6282 if Null_Exclusion_Present (Type_Definition (N)) then
6283 Set_Can_Never_Be_Null (Derived_Type);
6284
6285 elsif Can_Never_Be_Null (Parent_Type) then
6286 Set_Can_Never_Be_Null (Derived_Type);
6287 end if;
6288
6289 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6290 -- the root type for this information.
6291
6292 -- Apply range checks to discriminants for derived record case
6293 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6294
6295 Desig_Type := Designated_Type (Derived_Type);
6296
6297 if Is_Composite_Type (Desig_Type)
6298 and then (not Is_Array_Type (Desig_Type))
6299 and then Has_Discriminants (Desig_Type)
6300 and then Base_Type (Desig_Type) /= Desig_Type
6301 then
6302 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6303 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6304
6305 Discr := First_Discriminant (Base_Type (Desig_Type));
6306 while Present (Discr_Con_El) loop
6307 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6308 Next_Elmt (Discr_Con_El);
6309 Next_Discriminant (Discr);
6310 end loop;
6311 end if;
6312 end Build_Derived_Access_Type;
6313
6314 ------------------------------
6315 -- Build_Derived_Array_Type --
6316 ------------------------------
6317
6318 procedure Build_Derived_Array_Type
6319 (N : Node_Id;
6320 Parent_Type : Entity_Id;
6321 Derived_Type : Entity_Id)
6322 is
6323 Loc : constant Source_Ptr := Sloc (N);
6324 Tdef : constant Node_Id := Type_Definition (N);
6325 Indic : constant Node_Id := Subtype_Indication (Tdef);
6326 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6327 Implicit_Base : Entity_Id;
6328 New_Indic : Node_Id;
6329
6330 procedure Make_Implicit_Base;
6331 -- If the parent subtype is constrained, the derived type is a subtype
6332 -- of an implicit base type derived from the parent base.
6333
6334 ------------------------
6335 -- Make_Implicit_Base --
6336 ------------------------
6337
6338 procedure Make_Implicit_Base is
6339 begin
6340 Implicit_Base :=
6341 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6342
6343 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6344 Set_Etype (Implicit_Base, Parent_Base);
6345
6346 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6347 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6348
6349 Set_Has_Delayed_Freeze (Implicit_Base, True);
6350 end Make_Implicit_Base;
6351
6352 -- Start of processing for Build_Derived_Array_Type
6353
6354 begin
6355 if not Is_Constrained (Parent_Type) then
6356 if Nkind (Indic) /= N_Subtype_Indication then
6357 Set_Ekind (Derived_Type, E_Array_Type);
6358
6359 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6360 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6361
6362 Set_Has_Delayed_Freeze (Derived_Type, True);
6363
6364 else
6365 Make_Implicit_Base;
6366 Set_Etype (Derived_Type, Implicit_Base);
6367
6368 New_Indic :=
6369 Make_Subtype_Declaration (Loc,
6370 Defining_Identifier => Derived_Type,
6371 Subtype_Indication =>
6372 Make_Subtype_Indication (Loc,
6373 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6374 Constraint => Constraint (Indic)));
6375
6376 Rewrite (N, New_Indic);
6377 Analyze (N);
6378 end if;
6379
6380 else
6381 if Nkind (Indic) /= N_Subtype_Indication then
6382 Make_Implicit_Base;
6383
6384 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6385 Set_Etype (Derived_Type, Implicit_Base);
6386 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6387
6388 else
6389 Error_Msg_N ("illegal constraint on constrained type", Indic);
6390 end if;
6391 end if;
6392
6393 -- If parent type is not a derived type itself, and is declared in
6394 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6395 -- the new type's concatenation operator since Derive_Subprograms
6396 -- will not inherit the parent's operator. If the parent type is
6397 -- unconstrained, the operator is of the unconstrained base type.
6398
6399 if Number_Dimensions (Parent_Type) = 1
6400 and then not Is_Limited_Type (Parent_Type)
6401 and then not Is_Derived_Type (Parent_Type)
6402 and then not Is_Package_Or_Generic_Package
6403 (Scope (Base_Type (Parent_Type)))
6404 then
6405 if not Is_Constrained (Parent_Type)
6406 and then Is_Constrained (Derived_Type)
6407 then
6408 New_Concatenation_Op (Implicit_Base);
6409 else
6410 New_Concatenation_Op (Derived_Type);
6411 end if;
6412 end if;
6413 end Build_Derived_Array_Type;
6414
6415 -----------------------------------
6416 -- Build_Derived_Concurrent_Type --
6417 -----------------------------------
6418
6419 procedure Build_Derived_Concurrent_Type
6420 (N : Node_Id;
6421 Parent_Type : Entity_Id;
6422 Derived_Type : Entity_Id)
6423 is
6424 Loc : constant Source_Ptr := Sloc (N);
6425
6426 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6427 Corr_Decl : Node_Id;
6428 Corr_Decl_Needed : Boolean;
6429 -- If the derived type has fewer discriminants than its parent, the
6430 -- corresponding record is also a derived type, in order to account for
6431 -- the bound discriminants. We create a full type declaration for it in
6432 -- this case.
6433
6434 Constraint_Present : constant Boolean :=
6435 Nkind (Subtype_Indication (Type_Definition (N))) =
6436 N_Subtype_Indication;
6437
6438 D_Constraint : Node_Id;
6439 New_Constraint : Elist_Id;
6440 Old_Disc : Entity_Id;
6441 New_Disc : Entity_Id;
6442 New_N : Node_Id;
6443
6444 begin
6445 Set_Stored_Constraint (Derived_Type, No_Elist);
6446 Corr_Decl_Needed := False;
6447 Old_Disc := Empty;
6448
6449 if Present (Discriminant_Specifications (N))
6450 and then Constraint_Present
6451 then
6452 Old_Disc := First_Discriminant (Parent_Type);
6453 New_Disc := First (Discriminant_Specifications (N));
6454 while Present (New_Disc) and then Present (Old_Disc) loop
6455 Next_Discriminant (Old_Disc);
6456 Next (New_Disc);
6457 end loop;
6458 end if;
6459
6460 if Present (Old_Disc) and then Expander_Active then
6461
6462 -- The new type has fewer discriminants, so we need to create a new
6463 -- corresponding record, which is derived from the corresponding
6464 -- record of the parent, and has a stored constraint that captures
6465 -- the values of the discriminant constraints. The corresponding
6466 -- record is needed only if expander is active and code generation is
6467 -- enabled.
6468
6469 -- The type declaration for the derived corresponding record has the
6470 -- same discriminant part and constraints as the current declaration.
6471 -- Copy the unanalyzed tree to build declaration.
6472
6473 Corr_Decl_Needed := True;
6474 New_N := Copy_Separate_Tree (N);
6475
6476 Corr_Decl :=
6477 Make_Full_Type_Declaration (Loc,
6478 Defining_Identifier => Corr_Record,
6479 Discriminant_Specifications =>
6480 Discriminant_Specifications (New_N),
6481 Type_Definition =>
6482 Make_Derived_Type_Definition (Loc,
6483 Subtype_Indication =>
6484 Make_Subtype_Indication (Loc,
6485 Subtype_Mark =>
6486 New_Occurrence_Of
6487 (Corresponding_Record_Type (Parent_Type), Loc),
6488 Constraint =>
6489 Constraint
6490 (Subtype_Indication (Type_Definition (New_N))))));
6491 end if;
6492
6493 -- Copy Storage_Size and Relative_Deadline variables if task case
6494
6495 if Is_Task_Type (Parent_Type) then
6496 Set_Storage_Size_Variable (Derived_Type,
6497 Storage_Size_Variable (Parent_Type));
6498 Set_Relative_Deadline_Variable (Derived_Type,
6499 Relative_Deadline_Variable (Parent_Type));
6500 end if;
6501
6502 if Present (Discriminant_Specifications (N)) then
6503 Push_Scope (Derived_Type);
6504 Check_Or_Process_Discriminants (N, Derived_Type);
6505
6506 if Constraint_Present then
6507 New_Constraint :=
6508 Expand_To_Stored_Constraint
6509 (Parent_Type,
6510 Build_Discriminant_Constraints
6511 (Parent_Type,
6512 Subtype_Indication (Type_Definition (N)), True));
6513 end if;
6514
6515 End_Scope;
6516
6517 elsif Constraint_Present then
6518
6519 -- Build constrained subtype, copying the constraint, and derive
6520 -- from it to create a derived constrained type.
6521
6522 declare
6523 Loc : constant Source_Ptr := Sloc (N);
6524 Anon : constant Entity_Id :=
6525 Make_Defining_Identifier (Loc,
6526 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6527 Decl : Node_Id;
6528
6529 begin
6530 Decl :=
6531 Make_Subtype_Declaration (Loc,
6532 Defining_Identifier => Anon,
6533 Subtype_Indication =>
6534 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6535 Insert_Before (N, Decl);
6536 Analyze (Decl);
6537
6538 Rewrite (Subtype_Indication (Type_Definition (N)),
6539 New_Occurrence_Of (Anon, Loc));
6540 Set_Analyzed (Derived_Type, False);
6541 Analyze (N);
6542 return;
6543 end;
6544 end if;
6545
6546 -- By default, operations and private data are inherited from parent.
6547 -- However, in the presence of bound discriminants, a new corresponding
6548 -- record will be created, see below.
6549
6550 Set_Has_Discriminants
6551 (Derived_Type, Has_Discriminants (Parent_Type));
6552 Set_Corresponding_Record_Type
6553 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6554
6555 -- Is_Constrained is set according the parent subtype, but is set to
6556 -- False if the derived type is declared with new discriminants.
6557
6558 Set_Is_Constrained
6559 (Derived_Type,
6560 (Is_Constrained (Parent_Type) or else Constraint_Present)
6561 and then not Present (Discriminant_Specifications (N)));
6562
6563 if Constraint_Present then
6564 if not Has_Discriminants (Parent_Type) then
6565 Error_Msg_N ("untagged parent must have discriminants", N);
6566
6567 elsif Present (Discriminant_Specifications (N)) then
6568
6569 -- Verify that new discriminants are used to constrain old ones
6570
6571 D_Constraint :=
6572 First
6573 (Constraints
6574 (Constraint (Subtype_Indication (Type_Definition (N)))));
6575
6576 Old_Disc := First_Discriminant (Parent_Type);
6577
6578 while Present (D_Constraint) loop
6579 if Nkind (D_Constraint) /= N_Discriminant_Association then
6580
6581 -- Positional constraint. If it is a reference to a new
6582 -- discriminant, it constrains the corresponding old one.
6583
6584 if Nkind (D_Constraint) = N_Identifier then
6585 New_Disc := First_Discriminant (Derived_Type);
6586 while Present (New_Disc) loop
6587 exit when Chars (New_Disc) = Chars (D_Constraint);
6588 Next_Discriminant (New_Disc);
6589 end loop;
6590
6591 if Present (New_Disc) then
6592 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6593 end if;
6594 end if;
6595
6596 Next_Discriminant (Old_Disc);
6597
6598 -- if this is a named constraint, search by name for the old
6599 -- discriminants constrained by the new one.
6600
6601 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6602
6603 -- Find new discriminant with that name
6604
6605 New_Disc := First_Discriminant (Derived_Type);
6606 while Present (New_Disc) loop
6607 exit when
6608 Chars (New_Disc) = Chars (Expression (D_Constraint));
6609 Next_Discriminant (New_Disc);
6610 end loop;
6611
6612 if Present (New_Disc) then
6613
6614 -- Verify that new discriminant renames some discriminant
6615 -- of the parent type, and associate the new discriminant
6616 -- with one or more old ones that it renames.
6617
6618 declare
6619 Selector : Node_Id;
6620
6621 begin
6622 Selector := First (Selector_Names (D_Constraint));
6623 while Present (Selector) loop
6624 Old_Disc := First_Discriminant (Parent_Type);
6625 while Present (Old_Disc) loop
6626 exit when Chars (Old_Disc) = Chars (Selector);
6627 Next_Discriminant (Old_Disc);
6628 end loop;
6629
6630 if Present (Old_Disc) then
6631 Set_Corresponding_Discriminant
6632 (New_Disc, Old_Disc);
6633 end if;
6634
6635 Next (Selector);
6636 end loop;
6637 end;
6638 end if;
6639 end if;
6640
6641 Next (D_Constraint);
6642 end loop;
6643
6644 New_Disc := First_Discriminant (Derived_Type);
6645 while Present (New_Disc) loop
6646 if No (Corresponding_Discriminant (New_Disc)) then
6647 Error_Msg_NE
6648 ("new discriminant& must constrain old one", N, New_Disc);
6649
6650 elsif not
6651 Subtypes_Statically_Compatible
6652 (Etype (New_Disc),
6653 Etype (Corresponding_Discriminant (New_Disc)))
6654 then
6655 Error_Msg_NE
6656 ("& not statically compatible with parent discriminant",
6657 N, New_Disc);
6658 end if;
6659
6660 Next_Discriminant (New_Disc);
6661 end loop;
6662 end if;
6663
6664 elsif Present (Discriminant_Specifications (N)) then
6665 Error_Msg_N
6666 ("missing discriminant constraint in untagged derivation", N);
6667 end if;
6668
6669 -- The entity chain of the derived type includes the new discriminants
6670 -- but shares operations with the parent.
6671
6672 if Present (Discriminant_Specifications (N)) then
6673 Old_Disc := First_Discriminant (Parent_Type);
6674 while Present (Old_Disc) loop
6675 if No (Next_Entity (Old_Disc))
6676 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6677 then
6678 Set_Next_Entity
6679 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6680 exit;
6681 end if;
6682
6683 Next_Discriminant (Old_Disc);
6684 end loop;
6685
6686 else
6687 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6688 if Has_Discriminants (Parent_Type) then
6689 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6690 Set_Discriminant_Constraint (
6691 Derived_Type, Discriminant_Constraint (Parent_Type));
6692 end if;
6693 end if;
6694
6695 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6696
6697 Set_Has_Completion (Derived_Type);
6698
6699 if Corr_Decl_Needed then
6700 Set_Stored_Constraint (Derived_Type, New_Constraint);
6701 Insert_After (N, Corr_Decl);
6702 Analyze (Corr_Decl);
6703 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6704 end if;
6705 end Build_Derived_Concurrent_Type;
6706
6707 ------------------------------------
6708 -- Build_Derived_Enumeration_Type --
6709 ------------------------------------
6710
6711 procedure Build_Derived_Enumeration_Type
6712 (N : Node_Id;
6713 Parent_Type : Entity_Id;
6714 Derived_Type : Entity_Id)
6715 is
6716 Loc : constant Source_Ptr := Sloc (N);
6717 Def : constant Node_Id := Type_Definition (N);
6718 Indic : constant Node_Id := Subtype_Indication (Def);
6719 Implicit_Base : Entity_Id;
6720 Literal : Entity_Id;
6721 New_Lit : Entity_Id;
6722 Literals_List : List_Id;
6723 Type_Decl : Node_Id;
6724 Hi, Lo : Node_Id;
6725 Rang_Expr : Node_Id;
6726
6727 begin
6728 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6729 -- not have explicit literals lists we need to process types derived
6730 -- from them specially. This is handled by Derived_Standard_Character.
6731 -- If the parent type is a generic type, there are no literals either,
6732 -- and we construct the same skeletal representation as for the generic
6733 -- parent type.
6734
6735 if Is_Standard_Character_Type (Parent_Type) then
6736 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6737
6738 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6739 declare
6740 Lo : Node_Id;
6741 Hi : Node_Id;
6742
6743 begin
6744 if Nkind (Indic) /= N_Subtype_Indication then
6745 Lo :=
6746 Make_Attribute_Reference (Loc,
6747 Attribute_Name => Name_First,
6748 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6749 Set_Etype (Lo, Derived_Type);
6750
6751 Hi :=
6752 Make_Attribute_Reference (Loc,
6753 Attribute_Name => Name_Last,
6754 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6755 Set_Etype (Hi, Derived_Type);
6756
6757 Set_Scalar_Range (Derived_Type,
6758 Make_Range (Loc,
6759 Low_Bound => Lo,
6760 High_Bound => Hi));
6761 else
6762
6763 -- Analyze subtype indication and verify compatibility
6764 -- with parent type.
6765
6766 if Base_Type (Process_Subtype (Indic, N)) /=
6767 Base_Type (Parent_Type)
6768 then
6769 Error_Msg_N
6770 ("illegal constraint for formal discrete type", N);
6771 end if;
6772 end if;
6773 end;
6774
6775 else
6776 -- If a constraint is present, analyze the bounds to catch
6777 -- premature usage of the derived literals.
6778
6779 if Nkind (Indic) = N_Subtype_Indication
6780 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6781 then
6782 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6783 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6784 end if;
6785
6786 -- Introduce an implicit base type for the derived type even if there
6787 -- is no constraint attached to it, since this seems closer to the
6788 -- Ada semantics. Build a full type declaration tree for the derived
6789 -- type using the implicit base type as the defining identifier. The
6790 -- build a subtype declaration tree which applies the constraint (if
6791 -- any) have it replace the derived type declaration.
6792
6793 Literal := First_Literal (Parent_Type);
6794 Literals_List := New_List;
6795 while Present (Literal)
6796 and then Ekind (Literal) = E_Enumeration_Literal
6797 loop
6798 -- Literals of the derived type have the same representation as
6799 -- those of the parent type, but this representation can be
6800 -- overridden by an explicit representation clause. Indicate
6801 -- that there is no explicit representation given yet. These
6802 -- derived literals are implicit operations of the new type,
6803 -- and can be overridden by explicit ones.
6804
6805 if Nkind (Literal) = N_Defining_Character_Literal then
6806 New_Lit :=
6807 Make_Defining_Character_Literal (Loc, Chars (Literal));
6808 else
6809 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6810 end if;
6811
6812 Set_Ekind (New_Lit, E_Enumeration_Literal);
6813 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6814 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6815 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6816 Set_Alias (New_Lit, Literal);
6817 Set_Is_Known_Valid (New_Lit, True);
6818
6819 Append (New_Lit, Literals_List);
6820 Next_Literal (Literal);
6821 end loop;
6822
6823 Implicit_Base :=
6824 Make_Defining_Identifier (Sloc (Derived_Type),
6825 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6826
6827 -- Indicate the proper nature of the derived type. This must be done
6828 -- before analysis of the literals, to recognize cases when a literal
6829 -- may be hidden by a previous explicit function definition (cf.
6830 -- c83031a).
6831
6832 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6833 Set_Etype (Derived_Type, Implicit_Base);
6834
6835 Type_Decl :=
6836 Make_Full_Type_Declaration (Loc,
6837 Defining_Identifier => Implicit_Base,
6838 Discriminant_Specifications => No_List,
6839 Type_Definition =>
6840 Make_Enumeration_Type_Definition (Loc, Literals_List));
6841
6842 Mark_Rewrite_Insertion (Type_Decl);
6843 Insert_Before (N, Type_Decl);
6844 Analyze (Type_Decl);
6845
6846 -- The anonymous base now has a full declaration, but this base
6847 -- is not a first subtype.
6848
6849 Set_Is_First_Subtype (Implicit_Base, False);
6850
6851 -- After the implicit base is analyzed its Etype needs to be changed
6852 -- to reflect the fact that it is derived from the parent type which
6853 -- was ignored during analysis. We also set the size at this point.
6854
6855 Set_Etype (Implicit_Base, Parent_Type);
6856
6857 Set_Size_Info (Implicit_Base, Parent_Type);
6858 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6859 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6860
6861 -- Copy other flags from parent type
6862
6863 Set_Has_Non_Standard_Rep
6864 (Implicit_Base, Has_Non_Standard_Rep
6865 (Parent_Type));
6866 Set_Has_Pragma_Ordered
6867 (Implicit_Base, Has_Pragma_Ordered
6868 (Parent_Type));
6869 Set_Has_Delayed_Freeze (Implicit_Base);
6870
6871 -- Process the subtype indication including a validation check on the
6872 -- constraint, if any. If a constraint is given, its bounds must be
6873 -- implicitly converted to the new type.
6874
6875 if Nkind (Indic) = N_Subtype_Indication then
6876 declare
6877 R : constant Node_Id :=
6878 Range_Expression (Constraint (Indic));
6879
6880 begin
6881 if Nkind (R) = N_Range then
6882 Hi := Build_Scalar_Bound
6883 (High_Bound (R), Parent_Type, Implicit_Base);
6884 Lo := Build_Scalar_Bound
6885 (Low_Bound (R), Parent_Type, Implicit_Base);
6886
6887 else
6888 -- Constraint is a Range attribute. Replace with explicit
6889 -- mention of the bounds of the prefix, which must be a
6890 -- subtype.
6891
6892 Analyze (Prefix (R));
6893 Hi :=
6894 Convert_To (Implicit_Base,
6895 Make_Attribute_Reference (Loc,
6896 Attribute_Name => Name_Last,
6897 Prefix =>
6898 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6899
6900 Lo :=
6901 Convert_To (Implicit_Base,
6902 Make_Attribute_Reference (Loc,
6903 Attribute_Name => Name_First,
6904 Prefix =>
6905 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6906 end if;
6907 end;
6908
6909 else
6910 Hi :=
6911 Build_Scalar_Bound
6912 (Type_High_Bound (Parent_Type),
6913 Parent_Type, Implicit_Base);
6914 Lo :=
6915 Build_Scalar_Bound
6916 (Type_Low_Bound (Parent_Type),
6917 Parent_Type, Implicit_Base);
6918 end if;
6919
6920 Rang_Expr :=
6921 Make_Range (Loc,
6922 Low_Bound => Lo,
6923 High_Bound => Hi);
6924
6925 -- If we constructed a default range for the case where no range
6926 -- was given, then the expressions in the range must not freeze
6927 -- since they do not correspond to expressions in the source.
6928 -- However, if the type inherits predicates the expressions will
6929 -- be elaborated earlier and must freeze.
6930
6931 if Nkind (Indic) /= N_Subtype_Indication
6932 and then not Has_Predicates (Derived_Type)
6933 then
6934 Set_Must_Not_Freeze (Lo);
6935 Set_Must_Not_Freeze (Hi);
6936 Set_Must_Not_Freeze (Rang_Expr);
6937 end if;
6938
6939 Rewrite (N,
6940 Make_Subtype_Declaration (Loc,
6941 Defining_Identifier => Derived_Type,
6942 Subtype_Indication =>
6943 Make_Subtype_Indication (Loc,
6944 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6945 Constraint =>
6946 Make_Range_Constraint (Loc,
6947 Range_Expression => Rang_Expr))));
6948
6949 Analyze (N);
6950
6951 -- Propagate the aspects from the original type declaration to the
6952 -- declaration of the implicit base.
6953
6954 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6955
6956 -- Apply a range check. Since this range expression doesn't have an
6957 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6958 -- this right???
6959
6960 if Nkind (Indic) = N_Subtype_Indication then
6961 Apply_Range_Check
6962 (Range_Expression (Constraint (Indic)), Parent_Type,
6963 Source_Typ => Entity (Subtype_Mark (Indic)));
6964 end if;
6965 end if;
6966 end Build_Derived_Enumeration_Type;
6967
6968 --------------------------------
6969 -- Build_Derived_Numeric_Type --
6970 --------------------------------
6971
6972 procedure Build_Derived_Numeric_Type
6973 (N : Node_Id;
6974 Parent_Type : Entity_Id;
6975 Derived_Type : Entity_Id)
6976 is
6977 Loc : constant Source_Ptr := Sloc (N);
6978 Tdef : constant Node_Id := Type_Definition (N);
6979 Indic : constant Node_Id := Subtype_Indication (Tdef);
6980 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6981 No_Constraint : constant Boolean := Nkind (Indic) /=
6982 N_Subtype_Indication;
6983 Implicit_Base : Entity_Id;
6984
6985 Lo : Node_Id;
6986 Hi : Node_Id;
6987
6988 begin
6989 -- Process the subtype indication including a validation check on
6990 -- the constraint if any.
6991
6992 Discard_Node (Process_Subtype (Indic, N));
6993
6994 -- Introduce an implicit base type for the derived type even if there
6995 -- is no constraint attached to it, since this seems closer to the Ada
6996 -- semantics.
6997
6998 Implicit_Base :=
6999 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7000
7001 Set_Etype (Implicit_Base, Parent_Base);
7002 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7003 Set_Size_Info (Implicit_Base, Parent_Base);
7004 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7005 Set_Parent (Implicit_Base, Parent (Derived_Type));
7006 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7007
7008 -- Set RM Size for discrete type or decimal fixed-point type
7009 -- Ordinary fixed-point is excluded, why???
7010
7011 if Is_Discrete_Type (Parent_Base)
7012 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7013 then
7014 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7015 end if;
7016
7017 Set_Has_Delayed_Freeze (Implicit_Base);
7018
7019 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7020 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7021
7022 Set_Scalar_Range (Implicit_Base,
7023 Make_Range (Loc,
7024 Low_Bound => Lo,
7025 High_Bound => Hi));
7026
7027 if Has_Infinities (Parent_Base) then
7028 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7029 end if;
7030
7031 -- The Derived_Type, which is the entity of the declaration, is a
7032 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7033 -- absence of an explicit constraint.
7034
7035 Set_Etype (Derived_Type, Implicit_Base);
7036
7037 -- If we did not have a constraint, then the Ekind is set from the
7038 -- parent type (otherwise Process_Subtype has set the bounds)
7039
7040 if No_Constraint then
7041 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7042 end if;
7043
7044 -- If we did not have a range constraint, then set the range from the
7045 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7046
7047 if No_Constraint or else not Has_Range_Constraint (Indic) then
7048 Set_Scalar_Range (Derived_Type,
7049 Make_Range (Loc,
7050 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7051 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7052 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7053
7054 if Has_Infinities (Parent_Type) then
7055 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7056 end if;
7057
7058 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7059 end if;
7060
7061 Set_Is_Descendant_Of_Address (Derived_Type,
7062 Is_Descendant_Of_Address (Parent_Type));
7063 Set_Is_Descendant_Of_Address (Implicit_Base,
7064 Is_Descendant_Of_Address (Parent_Type));
7065
7066 -- Set remaining type-specific fields, depending on numeric type
7067
7068 if Is_Modular_Integer_Type (Parent_Type) then
7069 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7070
7071 Set_Non_Binary_Modulus
7072 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7073
7074 Set_Is_Known_Valid
7075 (Implicit_Base, Is_Known_Valid (Parent_Base));
7076
7077 elsif Is_Floating_Point_Type (Parent_Type) then
7078
7079 -- Digits of base type is always copied from the digits value of
7080 -- the parent base type, but the digits of the derived type will
7081 -- already have been set if there was a constraint present.
7082
7083 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7084 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7085
7086 if No_Constraint then
7087 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7088 end if;
7089
7090 elsif Is_Fixed_Point_Type (Parent_Type) then
7091
7092 -- Small of base type and derived type are always copied from the
7093 -- parent base type, since smalls never change. The delta of the
7094 -- base type is also copied from the parent base type. However the
7095 -- delta of the derived type will have been set already if a
7096 -- constraint was present.
7097
7098 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7099 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7100 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7101
7102 if No_Constraint then
7103 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7104 end if;
7105
7106 -- The scale and machine radix in the decimal case are always
7107 -- copied from the parent base type.
7108
7109 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7110 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7111 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7112
7113 Set_Machine_Radix_10
7114 (Derived_Type, Machine_Radix_10 (Parent_Base));
7115 Set_Machine_Radix_10
7116 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7117
7118 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7119
7120 if No_Constraint then
7121 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7122
7123 else
7124 -- the analysis of the subtype_indication sets the
7125 -- digits value of the derived type.
7126
7127 null;
7128 end if;
7129 end if;
7130 end if;
7131
7132 if Is_Integer_Type (Parent_Type) then
7133 Set_Has_Shift_Operator
7134 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7135 end if;
7136
7137 -- The type of the bounds is that of the parent type, and they
7138 -- must be converted to the derived type.
7139
7140 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7141
7142 -- The implicit_base should be frozen when the derived type is frozen,
7143 -- but note that it is used in the conversions of the bounds. For fixed
7144 -- types we delay the determination of the bounds until the proper
7145 -- freezing point. For other numeric types this is rejected by GCC, for
7146 -- reasons that are currently unclear (???), so we choose to freeze the
7147 -- implicit base now. In the case of integers and floating point types
7148 -- this is harmless because subsequent representation clauses cannot
7149 -- affect anything, but it is still baffling that we cannot use the
7150 -- same mechanism for all derived numeric types.
7151
7152 -- There is a further complication: actually some representation
7153 -- clauses can affect the implicit base type. For example, attribute
7154 -- definition clauses for stream-oriented attributes need to set the
7155 -- corresponding TSS entries on the base type, and this normally
7156 -- cannot be done after the base type is frozen, so the circuitry in
7157 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7158 -- and not use Set_TSS in this case.
7159
7160 -- There are also consequences for the case of delayed representation
7161 -- aspects for some cases. For example, a Size aspect is delayed and
7162 -- should not be evaluated to the freeze point. This early freezing
7163 -- means that the size attribute evaluation happens too early???
7164
7165 if Is_Fixed_Point_Type (Parent_Type) then
7166 Conditional_Delay (Implicit_Base, Parent_Type);
7167 else
7168 Freeze_Before (N, Implicit_Base);
7169 end if;
7170 end Build_Derived_Numeric_Type;
7171
7172 --------------------------------
7173 -- Build_Derived_Private_Type --
7174 --------------------------------
7175
7176 procedure Build_Derived_Private_Type
7177 (N : Node_Id;
7178 Parent_Type : Entity_Id;
7179 Derived_Type : Entity_Id;
7180 Is_Completion : Boolean;
7181 Derive_Subps : Boolean := True)
7182 is
7183 Loc : constant Source_Ptr := Sloc (N);
7184 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7185 Par_Scope : constant Entity_Id := Scope (Par_Base);
7186 Full_N : constant Node_Id := New_Copy_Tree (N);
7187 Full_Der : Entity_Id := New_Copy (Derived_Type);
7188 Full_P : Entity_Id;
7189
7190 procedure Build_Full_Derivation;
7191 -- Build full derivation, i.e. derive from the full view
7192
7193 procedure Copy_And_Build;
7194 -- Copy derived type declaration, replace parent with its full view,
7195 -- and build derivation
7196
7197 ---------------------------
7198 -- Build_Full_Derivation --
7199 ---------------------------
7200
7201 procedure Build_Full_Derivation is
7202 begin
7203 -- If parent scope is not open, install the declarations
7204
7205 if not In_Open_Scopes (Par_Scope) then
7206 Install_Private_Declarations (Par_Scope);
7207 Install_Visible_Declarations (Par_Scope);
7208 Copy_And_Build;
7209 Uninstall_Declarations (Par_Scope);
7210
7211 -- If parent scope is open and in another unit, and parent has a
7212 -- completion, then the derivation is taking place in the visible
7213 -- part of a child unit. In that case retrieve the full view of
7214 -- the parent momentarily.
7215
7216 elsif not In_Same_Source_Unit (N, Parent_Type) then
7217 Full_P := Full_View (Parent_Type);
7218 Exchange_Declarations (Parent_Type);
7219 Copy_And_Build;
7220 Exchange_Declarations (Full_P);
7221
7222 -- Otherwise it is a local derivation
7223
7224 else
7225 Copy_And_Build;
7226 end if;
7227 end Build_Full_Derivation;
7228
7229 --------------------
7230 -- Copy_And_Build --
7231 --------------------
7232
7233 procedure Copy_And_Build is
7234 Full_Parent : Entity_Id := Parent_Type;
7235
7236 begin
7237 -- If the parent is itself derived from another private type,
7238 -- installing the private declarations has not affected its
7239 -- privacy status, so use its own full view explicitly.
7240
7241 if Is_Private_Type (Full_Parent)
7242 and then Present (Full_View (Full_Parent))
7243 then
7244 Full_Parent := Full_View (Full_Parent);
7245 end if;
7246
7247 -- And its underlying full view if necessary
7248
7249 if Is_Private_Type (Full_Parent)
7250 and then Present (Underlying_Full_View (Full_Parent))
7251 then
7252 Full_Parent := Underlying_Full_View (Full_Parent);
7253 end if;
7254
7255 -- For record, access and most enumeration types, derivation from
7256 -- the full view requires a fully-fledged declaration. In the other
7257 -- cases, just use an itype.
7258
7259 if Ekind (Full_Parent) in Record_Kind
7260 or else Ekind (Full_Parent) in Access_Kind
7261 or else
7262 (Ekind (Full_Parent) in Enumeration_Kind
7263 and then not Is_Standard_Character_Type (Full_Parent)
7264 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7265 then
7266 -- Copy and adjust declaration to provide a completion for what
7267 -- is originally a private declaration. Indicate that full view
7268 -- is internally generated.
7269
7270 Set_Comes_From_Source (Full_N, False);
7271 Set_Comes_From_Source (Full_Der, False);
7272 Set_Parent (Full_Der, Full_N);
7273 Set_Defining_Identifier (Full_N, Full_Der);
7274
7275 -- If there are no constraints, adjust the subtype mark
7276
7277 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7278 N_Subtype_Indication
7279 then
7280 Set_Subtype_Indication
7281 (Type_Definition (Full_N),
7282 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7283 end if;
7284
7285 Insert_After (N, Full_N);
7286
7287 -- Build full view of derived type from full view of parent which
7288 -- is now installed. Subprograms have been derived on the partial
7289 -- view, the completion does not derive them anew.
7290
7291 if Ekind (Full_Parent) in Record_Kind then
7292
7293 -- If parent type is tagged, the completion inherits the proper
7294 -- primitive operations.
7295
7296 if Is_Tagged_Type (Parent_Type) then
7297 Build_Derived_Record_Type
7298 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7299 else
7300 Build_Derived_Record_Type
7301 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7302 end if;
7303
7304 else
7305 Build_Derived_Type
7306 (Full_N, Full_Parent, Full_Der,
7307 Is_Completion => False, Derive_Subps => False);
7308 end if;
7309
7310 -- The full declaration has been introduced into the tree and
7311 -- processed in the step above. It should not be analyzed again
7312 -- (when encountered later in the current list of declarations)
7313 -- to prevent spurious name conflicts. The full entity remains
7314 -- invisible.
7315
7316 Set_Analyzed (Full_N);
7317
7318 else
7319 Full_Der :=
7320 Make_Defining_Identifier (Sloc (Derived_Type),
7321 Chars => Chars (Derived_Type));
7322 Set_Is_Itype (Full_Der);
7323 Set_Associated_Node_For_Itype (Full_Der, N);
7324 Set_Parent (Full_Der, N);
7325 Build_Derived_Type
7326 (N, Full_Parent, Full_Der,
7327 Is_Completion => False, Derive_Subps => False);
7328 end if;
7329
7330 Set_Has_Private_Declaration (Full_Der);
7331 Set_Has_Private_Declaration (Derived_Type);
7332
7333 Set_Scope (Full_Der, Scope (Derived_Type));
7334 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7335 Set_Has_Size_Clause (Full_Der, False);
7336 Set_Has_Alignment_Clause (Full_Der, False);
7337 Set_Has_Delayed_Freeze (Full_Der);
7338 Set_Is_Frozen (Full_Der, False);
7339 Set_Freeze_Node (Full_Der, Empty);
7340 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7341 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7342
7343 -- The convention on the base type may be set in the private part
7344 -- and not propagated to the subtype until later, so we obtain the
7345 -- convention from the base type of the parent.
7346
7347 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7348 end Copy_And_Build;
7349
7350 -- Start of processing for Build_Derived_Private_Type
7351
7352 begin
7353 if Is_Tagged_Type (Parent_Type) then
7354 Full_P := Full_View (Parent_Type);
7355
7356 -- A type extension of a type with unknown discriminants is an
7357 -- indefinite type that the back-end cannot handle directly.
7358 -- We treat it as a private type, and build a completion that is
7359 -- derived from the full view of the parent, and hopefully has
7360 -- known discriminants.
7361
7362 -- If the full view of the parent type has an underlying record view,
7363 -- use it to generate the underlying record view of this derived type
7364 -- (required for chains of derivations with unknown discriminants).
7365
7366 -- Minor optimization: we avoid the generation of useless underlying
7367 -- record view entities if the private type declaration has unknown
7368 -- discriminants but its corresponding full view has no
7369 -- discriminants.
7370
7371 if Has_Unknown_Discriminants (Parent_Type)
7372 and then Present (Full_P)
7373 and then (Has_Discriminants (Full_P)
7374 or else Present (Underlying_Record_View (Full_P)))
7375 and then not In_Open_Scopes (Par_Scope)
7376 and then Expander_Active
7377 then
7378 declare
7379 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7380 New_Ext : constant Node_Id :=
7381 Copy_Separate_Tree
7382 (Record_Extension_Part (Type_Definition (N)));
7383 Decl : Node_Id;
7384
7385 begin
7386 Build_Derived_Record_Type
7387 (N, Parent_Type, Derived_Type, Derive_Subps);
7388
7389 -- Build anonymous completion, as a derivation from the full
7390 -- view of the parent. This is not a completion in the usual
7391 -- sense, because the current type is not private.
7392
7393 Decl :=
7394 Make_Full_Type_Declaration (Loc,
7395 Defining_Identifier => Full_Der,
7396 Type_Definition =>
7397 Make_Derived_Type_Definition (Loc,
7398 Subtype_Indication =>
7399 New_Copy_Tree
7400 (Subtype_Indication (Type_Definition (N))),
7401 Record_Extension_Part => New_Ext));
7402
7403 -- If the parent type has an underlying record view, use it
7404 -- here to build the new underlying record view.
7405
7406 if Present (Underlying_Record_View (Full_P)) then
7407 pragma Assert
7408 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7409 = N_Identifier);
7410 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7411 Underlying_Record_View (Full_P));
7412 end if;
7413
7414 Install_Private_Declarations (Par_Scope);
7415 Install_Visible_Declarations (Par_Scope);
7416 Insert_Before (N, Decl);
7417
7418 -- Mark entity as an underlying record view before analysis,
7419 -- to avoid generating the list of its primitive operations
7420 -- (which is not really required for this entity) and thus
7421 -- prevent spurious errors associated with missing overriding
7422 -- of abstract primitives (overridden only for Derived_Type).
7423
7424 Set_Ekind (Full_Der, E_Record_Type);
7425 Set_Is_Underlying_Record_View (Full_Der);
7426 Set_Default_SSO (Full_Der);
7427
7428 Analyze (Decl);
7429
7430 pragma Assert (Has_Discriminants (Full_Der)
7431 and then not Has_Unknown_Discriminants (Full_Der));
7432
7433 Uninstall_Declarations (Par_Scope);
7434
7435 -- Freeze the underlying record view, to prevent generation of
7436 -- useless dispatching information, which is simply shared with
7437 -- the real derived type.
7438
7439 Set_Is_Frozen (Full_Der);
7440
7441 -- If the derived type has access discriminants, create
7442 -- references to their anonymous types now, to prevent
7443 -- back-end problems when their first use is in generated
7444 -- bodies of primitives.
7445
7446 declare
7447 E : Entity_Id;
7448
7449 begin
7450 E := First_Entity (Full_Der);
7451
7452 while Present (E) loop
7453 if Ekind (E) = E_Discriminant
7454 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7455 then
7456 Build_Itype_Reference (Etype (E), Decl);
7457 end if;
7458
7459 Next_Entity (E);
7460 end loop;
7461 end;
7462
7463 -- Set up links between real entity and underlying record view
7464
7465 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7466 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7467 end;
7468
7469 -- If discriminants are known, build derived record
7470
7471 else
7472 Build_Derived_Record_Type
7473 (N, Parent_Type, Derived_Type, Derive_Subps);
7474 end if;
7475
7476 return;
7477
7478 elsif Has_Discriminants (Parent_Type) then
7479
7480 -- Build partial view of derived type from partial view of parent.
7481 -- This must be done before building the full derivation because the
7482 -- second derivation will modify the discriminants of the first and
7483 -- the discriminants are chained with the rest of the components in
7484 -- the full derivation.
7485
7486 Build_Derived_Record_Type
7487 (N, Parent_Type, Derived_Type, Derive_Subps);
7488
7489 -- Build the full derivation if this is not the anonymous derived
7490 -- base type created by Build_Derived_Record_Type in the constrained
7491 -- case (see point 5. of its head comment) since we build it for the
7492 -- derived subtype. And skip it for protected types altogether, as
7493 -- gigi does not use these types directly.
7494
7495 if Present (Full_View (Parent_Type))
7496 and then not Is_Itype (Derived_Type)
7497 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7498 then
7499 declare
7500 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7501 Discr : Entity_Id;
7502 Last_Discr : Entity_Id;
7503
7504 begin
7505 -- If this is not a completion, construct the implicit full
7506 -- view by deriving from the full view of the parent type.
7507 -- But if this is a completion, the derived private type
7508 -- being built is a full view and the full derivation can
7509 -- only be its underlying full view.
7510
7511 Build_Full_Derivation;
7512
7513 if not Is_Completion then
7514 Set_Full_View (Derived_Type, Full_Der);
7515 else
7516 Set_Underlying_Full_View (Derived_Type, Full_Der);
7517 Set_Is_Underlying_Full_View (Full_Der);
7518 end if;
7519
7520 if not Is_Base_Type (Derived_Type) then
7521 Set_Full_View (Der_Base, Base_Type (Full_Der));
7522 end if;
7523
7524 -- Copy the discriminant list from full view to the partial
7525 -- view (base type and its subtype). Gigi requires that the
7526 -- partial and full views have the same discriminants.
7527
7528 -- Note that since the partial view points to discriminants
7529 -- in the full view, their scope will be that of the full
7530 -- view. This might cause some front end problems and need
7531 -- adjustment???
7532
7533 Discr := First_Discriminant (Base_Type (Full_Der));
7534 Set_First_Entity (Der_Base, Discr);
7535
7536 loop
7537 Last_Discr := Discr;
7538 Next_Discriminant (Discr);
7539 exit when No (Discr);
7540 end loop;
7541
7542 Set_Last_Entity (Der_Base, Last_Discr);
7543 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7544 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7545
7546 Set_Stored_Constraint
7547 (Full_Der, Stored_Constraint (Derived_Type));
7548 end;
7549 end if;
7550
7551 elsif Present (Full_View (Parent_Type))
7552 and then Has_Discriminants (Full_View (Parent_Type))
7553 then
7554 if Has_Unknown_Discriminants (Parent_Type)
7555 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7556 N_Subtype_Indication
7557 then
7558 Error_Msg_N
7559 ("cannot constrain type with unknown discriminants",
7560 Subtype_Indication (Type_Definition (N)));
7561 return;
7562 end if;
7563
7564 -- If this is not a completion, construct the implicit full view by
7565 -- deriving from the full view of the parent type. But if this is a
7566 -- completion, the derived private type being built is a full view
7567 -- and the full derivation can only be its underlying full view.
7568
7569 Build_Full_Derivation;
7570
7571 if not Is_Completion then
7572 Set_Full_View (Derived_Type, Full_Der);
7573 else
7574 Set_Underlying_Full_View (Derived_Type, Full_Der);
7575 Set_Is_Underlying_Full_View (Full_Der);
7576 end if;
7577
7578 -- In any case, the primitive operations are inherited from the
7579 -- parent type, not from the internal full view.
7580
7581 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7582
7583 if Derive_Subps then
7584 Derive_Subprograms (Parent_Type, Derived_Type);
7585 end if;
7586
7587 Set_Stored_Constraint (Derived_Type, No_Elist);
7588 Set_Is_Constrained
7589 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7590
7591 else
7592 -- Untagged type, No discriminants on either view
7593
7594 if Nkind (Subtype_Indication (Type_Definition (N))) =
7595 N_Subtype_Indication
7596 then
7597 Error_Msg_N
7598 ("illegal constraint on type without discriminants", N);
7599 end if;
7600
7601 if Present (Discriminant_Specifications (N))
7602 and then Present (Full_View (Parent_Type))
7603 and then not Is_Tagged_Type (Full_View (Parent_Type))
7604 then
7605 Error_Msg_N ("cannot add discriminants to untagged type", N);
7606 end if;
7607
7608 Set_Stored_Constraint (Derived_Type, No_Elist);
7609 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7610 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7611 Set_Disable_Controlled (Derived_Type, Disable_Controlled
7612 (Parent_Type));
7613 Set_Has_Controlled_Component
7614 (Derived_Type, Has_Controlled_Component
7615 (Parent_Type));
7616
7617 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7618
7619 if not Is_Controlled_Active (Parent_Type) then
7620 Set_Finalize_Storage_Only
7621 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7622 end if;
7623
7624 -- If this is not a completion, construct the implicit full view by
7625 -- deriving from the full view of the parent type.
7626
7627 -- ??? If the parent is untagged private and its completion is
7628 -- tagged, this mechanism will not work because we cannot derive from
7629 -- the tagged full view unless we have an extension.
7630
7631 if Present (Full_View (Parent_Type))
7632 and then not Is_Tagged_Type (Full_View (Parent_Type))
7633 and then not Is_Completion
7634 then
7635 Build_Full_Derivation;
7636 Set_Full_View (Derived_Type, Full_Der);
7637 end if;
7638 end if;
7639
7640 Set_Has_Unknown_Discriminants (Derived_Type,
7641 Has_Unknown_Discriminants (Parent_Type));
7642
7643 if Is_Private_Type (Derived_Type) then
7644 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7645 end if;
7646
7647 -- If the parent base type is in scope, add the derived type to its
7648 -- list of private dependents, because its full view may become
7649 -- visible subsequently (in a nested private part, a body, or in a
7650 -- further child unit).
7651
7652 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7653 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7654
7655 -- Check for unusual case where a type completed by a private
7656 -- derivation occurs within a package nested in a child unit, and
7657 -- the parent is declared in an ancestor.
7658
7659 if Is_Child_Unit (Scope (Current_Scope))
7660 and then Is_Completion
7661 and then In_Private_Part (Current_Scope)
7662 and then Scope (Parent_Type) /= Current_Scope
7663
7664 -- Note that if the parent has a completion in the private part,
7665 -- (which is itself a derivation from some other private type)
7666 -- it is that completion that is visible, there is no full view
7667 -- available, and no special processing is needed.
7668
7669 and then Present (Full_View (Parent_Type))
7670 then
7671 -- In this case, the full view of the parent type will become
7672 -- visible in the body of the enclosing child, and only then will
7673 -- the current type be possibly non-private. Build an underlying
7674 -- full view that will be installed when the enclosing child body
7675 -- is compiled.
7676
7677 if Present (Underlying_Full_View (Derived_Type)) then
7678 Full_Der := Underlying_Full_View (Derived_Type);
7679 else
7680 Build_Full_Derivation;
7681 Set_Underlying_Full_View (Derived_Type, Full_Der);
7682 Set_Is_Underlying_Full_View (Full_Der);
7683 end if;
7684
7685 -- The full view will be used to swap entities on entry/exit to
7686 -- the body, and must appear in the entity list for the package.
7687
7688 Append_Entity (Full_Der, Scope (Derived_Type));
7689 end if;
7690 end if;
7691 end Build_Derived_Private_Type;
7692
7693 -------------------------------
7694 -- Build_Derived_Record_Type --
7695 -------------------------------
7696
7697 -- 1. INTRODUCTION
7698
7699 -- Ideally we would like to use the same model of type derivation for
7700 -- tagged and untagged record types. Unfortunately this is not quite
7701 -- possible because the semantics of representation clauses is different
7702 -- for tagged and untagged records under inheritance. Consider the
7703 -- following:
7704
7705 -- type R (...) is [tagged] record ... end record;
7706 -- type T (...) is new R (...) [with ...];
7707
7708 -- The representation clauses for T can specify a completely different
7709 -- record layout from R's. Hence the same component can be placed in two
7710 -- very different positions in objects of type T and R. If R and T are
7711 -- tagged types, representation clauses for T can only specify the layout
7712 -- of non inherited components, thus components that are common in R and T
7713 -- have the same position in objects of type R and T.
7714
7715 -- This has two implications. The first is that the entire tree for R's
7716 -- declaration needs to be copied for T in the untagged case, so that T
7717 -- can be viewed as a record type of its own with its own representation
7718 -- clauses. The second implication is the way we handle discriminants.
7719 -- Specifically, in the untagged case we need a way to communicate to Gigi
7720 -- what are the real discriminants in the record, while for the semantics
7721 -- we need to consider those introduced by the user to rename the
7722 -- discriminants in the parent type. This is handled by introducing the
7723 -- notion of stored discriminants. See below for more.
7724
7725 -- Fortunately the way regular components are inherited can be handled in
7726 -- the same way in tagged and untagged types.
7727
7728 -- To complicate things a bit more the private view of a private extension
7729 -- cannot be handled in the same way as the full view (for one thing the
7730 -- semantic rules are somewhat different). We will explain what differs
7731 -- below.
7732
7733 -- 2. DISCRIMINANTS UNDER INHERITANCE
7734
7735 -- The semantic rules governing the discriminants of derived types are
7736 -- quite subtle.
7737
7738 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7739 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7740
7741 -- If parent type has discriminants, then the discriminants that are
7742 -- declared in the derived type are [3.4 (11)]:
7743
7744 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7745 -- there is one;
7746
7747 -- o Otherwise, each discriminant of the parent type (implicitly declared
7748 -- in the same order with the same specifications). In this case, the
7749 -- discriminants are said to be "inherited", or if unknown in the parent
7750 -- are also unknown in the derived type.
7751
7752 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7753
7754 -- o The parent subtype must be constrained;
7755
7756 -- o If the parent type is not a tagged type, then each discriminant of
7757 -- the derived type must be used in the constraint defining a parent
7758 -- subtype. [Implementation note: This ensures that the new discriminant
7759 -- can share storage with an existing discriminant.]
7760
7761 -- For the derived type each discriminant of the parent type is either
7762 -- inherited, constrained to equal some new discriminant of the derived
7763 -- type, or constrained to the value of an expression.
7764
7765 -- When inherited or constrained to equal some new discriminant, the
7766 -- parent discriminant and the discriminant of the derived type are said
7767 -- to "correspond".
7768
7769 -- If a discriminant of the parent type is constrained to a specific value
7770 -- in the derived type definition, then the discriminant is said to be
7771 -- "specified" by that derived type definition.
7772
7773 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7774
7775 -- We have spoken about stored discriminants in point 1 (introduction)
7776 -- above. There are two sort of stored discriminants: implicit and
7777 -- explicit. As long as the derived type inherits the same discriminants as
7778 -- the root record type, stored discriminants are the same as regular
7779 -- discriminants, and are said to be implicit. However, if any discriminant
7780 -- in the root type was renamed in the derived type, then the derived
7781 -- type will contain explicit stored discriminants. Explicit stored
7782 -- discriminants are discriminants in addition to the semantically visible
7783 -- discriminants defined for the derived type. Stored discriminants are
7784 -- used by Gigi to figure out what are the physical discriminants in
7785 -- objects of the derived type (see precise definition in einfo.ads).
7786 -- As an example, consider the following:
7787
7788 -- type R (D1, D2, D3 : Int) is record ... end record;
7789 -- type T1 is new R;
7790 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7791 -- type T3 is new T2;
7792 -- type T4 (Y : Int) is new T3 (Y, 99);
7793
7794 -- The following table summarizes the discriminants and stored
7795 -- discriminants in R and T1 through T4.
7796
7797 -- Type Discrim Stored Discrim Comment
7798 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7799 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7800 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7801 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7802 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7803
7804 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7805 -- find the corresponding discriminant in the parent type, while
7806 -- Original_Record_Component (abbreviated ORC below), the actual physical
7807 -- component that is renamed. Finally the field Is_Completely_Hidden
7808 -- (abbreviated ICH below) is set for all explicit stored discriminants
7809 -- (see einfo.ads for more info). For the above example this gives:
7810
7811 -- Discrim CD ORC ICH
7812 -- ^^^^^^^ ^^ ^^^ ^^^
7813 -- D1 in R empty itself no
7814 -- D2 in R empty itself no
7815 -- D3 in R empty itself no
7816
7817 -- D1 in T1 D1 in R itself no
7818 -- D2 in T1 D2 in R itself no
7819 -- D3 in T1 D3 in R itself no
7820
7821 -- X1 in T2 D3 in T1 D3 in T2 no
7822 -- X2 in T2 D1 in T1 D1 in T2 no
7823 -- D1 in T2 empty itself yes
7824 -- D2 in T2 empty itself yes
7825 -- D3 in T2 empty itself yes
7826
7827 -- X1 in T3 X1 in T2 D3 in T3 no
7828 -- X2 in T3 X2 in T2 D1 in T3 no
7829 -- D1 in T3 empty itself yes
7830 -- D2 in T3 empty itself yes
7831 -- D3 in T3 empty itself yes
7832
7833 -- Y in T4 X1 in T3 D3 in T3 no
7834 -- D1 in T3 empty itself yes
7835 -- D2 in T3 empty itself yes
7836 -- D3 in T3 empty itself yes
7837
7838 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7839
7840 -- Type derivation for tagged types is fairly straightforward. If no
7841 -- discriminants are specified by the derived type, these are inherited
7842 -- from the parent. No explicit stored discriminants are ever necessary.
7843 -- The only manipulation that is done to the tree is that of adding a
7844 -- _parent field with parent type and constrained to the same constraint
7845 -- specified for the parent in the derived type definition. For instance:
7846
7847 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7848 -- type T1 is new R with null record;
7849 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7850
7851 -- are changed into:
7852
7853 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7854 -- _parent : R (D1, D2, D3);
7855 -- end record;
7856
7857 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7858 -- _parent : T1 (X2, 88, X1);
7859 -- end record;
7860
7861 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7862 -- ORC and ICH fields are:
7863
7864 -- Discrim CD ORC ICH
7865 -- ^^^^^^^ ^^ ^^^ ^^^
7866 -- D1 in R empty itself no
7867 -- D2 in R empty itself no
7868 -- D3 in R empty itself no
7869
7870 -- D1 in T1 D1 in R D1 in R no
7871 -- D2 in T1 D2 in R D2 in R no
7872 -- D3 in T1 D3 in R D3 in R no
7873
7874 -- X1 in T2 D3 in T1 D3 in R no
7875 -- X2 in T2 D1 in T1 D1 in R no
7876
7877 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7878 --
7879 -- Regardless of whether we dealing with a tagged or untagged type
7880 -- we will transform all derived type declarations of the form
7881 --
7882 -- type T is new R (...) [with ...];
7883 -- or
7884 -- subtype S is R (...);
7885 -- type T is new S [with ...];
7886 -- into
7887 -- type BT is new R [with ...];
7888 -- subtype T is BT (...);
7889 --
7890 -- That is, the base derived type is constrained only if it has no
7891 -- discriminants. The reason for doing this is that GNAT's semantic model
7892 -- assumes that a base type with discriminants is unconstrained.
7893 --
7894 -- Note that, strictly speaking, the above transformation is not always
7895 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7896 --
7897 -- procedure B34011A is
7898 -- type REC (D : integer := 0) is record
7899 -- I : Integer;
7900 -- end record;
7901
7902 -- package P is
7903 -- type T6 is new Rec;
7904 -- function F return T6;
7905 -- end P;
7906
7907 -- use P;
7908 -- package Q6 is
7909 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7910 -- end Q6;
7911 --
7912 -- The definition of Q6.U is illegal. However transforming Q6.U into
7913
7914 -- type BaseU is new T6;
7915 -- subtype U is BaseU (Q6.F.I)
7916
7917 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7918 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7919 -- the transformation described above.
7920
7921 -- There is another instance where the above transformation is incorrect.
7922 -- Consider:
7923
7924 -- package Pack is
7925 -- type Base (D : Integer) is tagged null record;
7926 -- procedure P (X : Base);
7927
7928 -- type Der is new Base (2) with null record;
7929 -- procedure P (X : Der);
7930 -- end Pack;
7931
7932 -- Then the above transformation turns this into
7933
7934 -- type Der_Base is new Base with null record;
7935 -- -- procedure P (X : Base) is implicitly inherited here
7936 -- -- as procedure P (X : Der_Base).
7937
7938 -- subtype Der is Der_Base (2);
7939 -- procedure P (X : Der);
7940 -- -- The overriding of P (X : Der_Base) is illegal since we
7941 -- -- have a parameter conformance problem.
7942
7943 -- To get around this problem, after having semantically processed Der_Base
7944 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7945 -- Discriminant_Constraint from Der so that when parameter conformance is
7946 -- checked when P is overridden, no semantic errors are flagged.
7947
7948 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7949
7950 -- Regardless of whether we are dealing with a tagged or untagged type
7951 -- we will transform all derived type declarations of the form
7952
7953 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7954 -- type T is new R [with ...];
7955 -- into
7956 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7957
7958 -- The reason for such transformation is that it allows us to implement a
7959 -- very clean form of component inheritance as explained below.
7960
7961 -- Note that this transformation is not achieved by direct tree rewriting
7962 -- and manipulation, but rather by redoing the semantic actions that the
7963 -- above transformation will entail. This is done directly in routine
7964 -- Inherit_Components.
7965
7966 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7967
7968 -- In both tagged and untagged derived types, regular non discriminant
7969 -- components are inherited in the derived type from the parent type. In
7970 -- the absence of discriminants component, inheritance is straightforward
7971 -- as components can simply be copied from the parent.
7972
7973 -- If the parent has discriminants, inheriting components constrained with
7974 -- these discriminants requires caution. Consider the following example:
7975
7976 -- type R (D1, D2 : Positive) is [tagged] record
7977 -- S : String (D1 .. D2);
7978 -- end record;
7979
7980 -- type T1 is new R [with null record];
7981 -- type T2 (X : positive) is new R (1, X) [with null record];
7982
7983 -- As explained in 6. above, T1 is rewritten as
7984 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7985 -- which makes the treatment for T1 and T2 identical.
7986
7987 -- What we want when inheriting S, is that references to D1 and D2 in R are
7988 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7989 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7990 -- with either discriminant references in the derived type or expressions.
7991 -- This replacement is achieved as follows: before inheriting R's
7992 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7993 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7994 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7995 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7996 -- by String (1 .. X).
7997
7998 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7999
8000 -- We explain here the rules governing private type extensions relevant to
8001 -- type derivation. These rules are explained on the following example:
8002
8003 -- type D [(...)] is new A [(...)] with private; <-- partial view
8004 -- type D [(...)] is new P [(...)] with null record; <-- full view
8005
8006 -- Type A is called the ancestor subtype of the private extension.
8007 -- Type P is the parent type of the full view of the private extension. It
8008 -- must be A or a type derived from A.
8009
8010 -- The rules concerning the discriminants of private type extensions are
8011 -- [7.3(10-13)]:
8012
8013 -- o If a private extension inherits known discriminants from the ancestor
8014 -- subtype, then the full view must also inherit its discriminants from
8015 -- the ancestor subtype and the parent subtype of the full view must be
8016 -- constrained if and only if the ancestor subtype is constrained.
8017
8018 -- o If a partial view has unknown discriminants, then the full view may
8019 -- define a definite or an indefinite subtype, with or without
8020 -- discriminants.
8021
8022 -- o If a partial view has neither known nor unknown discriminants, then
8023 -- the full view must define a definite subtype.
8024
8025 -- o If the ancestor subtype of a private extension has constrained
8026 -- discriminants, then the parent subtype of the full view must impose a
8027 -- statically matching constraint on those discriminants.
8028
8029 -- This means that only the following forms of private extensions are
8030 -- allowed:
8031
8032 -- type D is new A with private; <-- partial view
8033 -- type D is new P with null record; <-- full view
8034
8035 -- If A has no discriminants than P has no discriminants, otherwise P must
8036 -- inherit A's discriminants.
8037
8038 -- type D is new A (...) with private; <-- partial view
8039 -- type D is new P (:::) with null record; <-- full view
8040
8041 -- P must inherit A's discriminants and (...) and (:::) must statically
8042 -- match.
8043
8044 -- subtype A is R (...);
8045 -- type D is new A with private; <-- partial view
8046 -- type D is new P with null record; <-- full view
8047
8048 -- P must have inherited R's discriminants and must be derived from A or
8049 -- any of its subtypes.
8050
8051 -- type D (..) is new A with private; <-- partial view
8052 -- type D (..) is new P [(:::)] with null record; <-- full view
8053
8054 -- No specific constraints on P's discriminants or constraint (:::).
8055 -- Note that A can be unconstrained, but the parent subtype P must either
8056 -- be constrained or (:::) must be present.
8057
8058 -- type D (..) is new A [(...)] with private; <-- partial view
8059 -- type D (..) is new P [(:::)] with null record; <-- full view
8060
8061 -- P's constraints on A's discriminants must statically match those
8062 -- imposed by (...).
8063
8064 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8065
8066 -- The full view of a private extension is handled exactly as described
8067 -- above. The model chose for the private view of a private extension is
8068 -- the same for what concerns discriminants (i.e. they receive the same
8069 -- treatment as in the tagged case). However, the private view of the
8070 -- private extension always inherits the components of the parent base,
8071 -- without replacing any discriminant reference. Strictly speaking this is
8072 -- incorrect. However, Gigi never uses this view to generate code so this
8073 -- is a purely semantic issue. In theory, a set of transformations similar
8074 -- to those given in 5. and 6. above could be applied to private views of
8075 -- private extensions to have the same model of component inheritance as
8076 -- for non private extensions. However, this is not done because it would
8077 -- further complicate private type processing. Semantically speaking, this
8078 -- leaves us in an uncomfortable situation. As an example consider:
8079
8080 -- package Pack is
8081 -- type R (D : integer) is tagged record
8082 -- S : String (1 .. D);
8083 -- end record;
8084 -- procedure P (X : R);
8085 -- type T is new R (1) with private;
8086 -- private
8087 -- type T is new R (1) with null record;
8088 -- end;
8089
8090 -- This is transformed into:
8091
8092 -- package Pack is
8093 -- type R (D : integer) is tagged record
8094 -- S : String (1 .. D);
8095 -- end record;
8096 -- procedure P (X : R);
8097 -- type T is new R (1) with private;
8098 -- private
8099 -- type BaseT is new R with null record;
8100 -- subtype T is BaseT (1);
8101 -- end;
8102
8103 -- (strictly speaking the above is incorrect Ada)
8104
8105 -- From the semantic standpoint the private view of private extension T
8106 -- should be flagged as constrained since one can clearly have
8107 --
8108 -- Obj : T;
8109 --
8110 -- in a unit withing Pack. However, when deriving subprograms for the
8111 -- private view of private extension T, T must be seen as unconstrained
8112 -- since T has discriminants (this is a constraint of the current
8113 -- subprogram derivation model). Thus, when processing the private view of
8114 -- a private extension such as T, we first mark T as unconstrained, we
8115 -- process it, we perform program derivation and just before returning from
8116 -- Build_Derived_Record_Type we mark T as constrained.
8117
8118 -- ??? Are there are other uncomfortable cases that we will have to
8119 -- deal with.
8120
8121 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8122
8123 -- Types that are derived from a visible record type and have a private
8124 -- extension present other peculiarities. They behave mostly like private
8125 -- types, but if they have primitive operations defined, these will not
8126 -- have the proper signatures for further inheritance, because other
8127 -- primitive operations will use the implicit base that we define for
8128 -- private derivations below. This affect subprogram inheritance (see
8129 -- Derive_Subprograms for details). We also derive the implicit base from
8130 -- the base type of the full view, so that the implicit base is a record
8131 -- type and not another private type, This avoids infinite loops.
8132
8133 procedure Build_Derived_Record_Type
8134 (N : Node_Id;
8135 Parent_Type : Entity_Id;
8136 Derived_Type : Entity_Id;
8137 Derive_Subps : Boolean := True)
8138 is
8139 Discriminant_Specs : constant Boolean :=
8140 Present (Discriminant_Specifications (N));
8141 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8142 Loc : constant Source_Ptr := Sloc (N);
8143 Private_Extension : constant Boolean :=
8144 Nkind (N) = N_Private_Extension_Declaration;
8145 Assoc_List : Elist_Id;
8146 Constraint_Present : Boolean;
8147 Constrs : Elist_Id;
8148 Discrim : Entity_Id;
8149 Indic : Node_Id;
8150 Inherit_Discrims : Boolean := False;
8151 Last_Discrim : Entity_Id;
8152 New_Base : Entity_Id;
8153 New_Decl : Node_Id;
8154 New_Discrs : Elist_Id;
8155 New_Indic : Node_Id;
8156 Parent_Base : Entity_Id;
8157 Save_Etype : Entity_Id;
8158 Save_Discr_Constr : Elist_Id;
8159 Save_Next_Entity : Entity_Id;
8160 Type_Def : Node_Id;
8161
8162 Discs : Elist_Id := New_Elmt_List;
8163 -- An empty Discs list means that there were no constraints in the
8164 -- subtype indication or that there was an error processing it.
8165
8166 begin
8167 if Ekind (Parent_Type) = E_Record_Type_With_Private
8168 and then Present (Full_View (Parent_Type))
8169 and then Has_Discriminants (Parent_Type)
8170 then
8171 Parent_Base := Base_Type (Full_View (Parent_Type));
8172 else
8173 Parent_Base := Base_Type (Parent_Type);
8174 end if;
8175
8176 -- AI05-0115 : if this is a derivation from a private type in some
8177 -- other scope that may lead to invisible components for the derived
8178 -- type, mark it accordingly.
8179
8180 if Is_Private_Type (Parent_Type) then
8181 if Scope (Parent_Type) = Scope (Derived_Type) then
8182 null;
8183
8184 elsif In_Open_Scopes (Scope (Parent_Type))
8185 and then In_Private_Part (Scope (Parent_Type))
8186 then
8187 null;
8188
8189 else
8190 Set_Has_Private_Ancestor (Derived_Type);
8191 end if;
8192
8193 else
8194 Set_Has_Private_Ancestor
8195 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8196 end if;
8197
8198 -- Before we start the previously documented transformations, here is
8199 -- little fix for size and alignment of tagged types. Normally when we
8200 -- derive type D from type P, we copy the size and alignment of P as the
8201 -- default for D, and in the absence of explicit representation clauses
8202 -- for D, the size and alignment are indeed the same as the parent.
8203
8204 -- But this is wrong for tagged types, since fields may be added, and
8205 -- the default size may need to be larger, and the default alignment may
8206 -- need to be larger.
8207
8208 -- We therefore reset the size and alignment fields in the tagged case.
8209 -- Note that the size and alignment will in any case be at least as
8210 -- large as the parent type (since the derived type has a copy of the
8211 -- parent type in the _parent field)
8212
8213 -- The type is also marked as being tagged here, which is needed when
8214 -- processing components with a self-referential anonymous access type
8215 -- in the call to Check_Anonymous_Access_Components below. Note that
8216 -- this flag is also set later on for completeness.
8217
8218 if Is_Tagged then
8219 Set_Is_Tagged_Type (Derived_Type);
8220 Init_Size_Align (Derived_Type);
8221 end if;
8222
8223 -- STEP 0a: figure out what kind of derived type declaration we have
8224
8225 if Private_Extension then
8226 Type_Def := N;
8227 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8228 Set_Default_SSO (Derived_Type);
8229
8230 else
8231 Type_Def := Type_Definition (N);
8232
8233 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8234 -- Parent_Base can be a private type or private extension. However,
8235 -- for tagged types with an extension the newly added fields are
8236 -- visible and hence the Derived_Type is always an E_Record_Type.
8237 -- (except that the parent may have its own private fields).
8238 -- For untagged types we preserve the Ekind of the Parent_Base.
8239
8240 if Present (Record_Extension_Part (Type_Def)) then
8241 Set_Ekind (Derived_Type, E_Record_Type);
8242 Set_Default_SSO (Derived_Type);
8243
8244 -- Create internal access types for components with anonymous
8245 -- access types.
8246
8247 if Ada_Version >= Ada_2005 then
8248 Check_Anonymous_Access_Components
8249 (N, Derived_Type, Derived_Type,
8250 Component_List (Record_Extension_Part (Type_Def)));
8251 end if;
8252
8253 else
8254 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8255 end if;
8256 end if;
8257
8258 -- Indic can either be an N_Identifier if the subtype indication
8259 -- contains no constraint or an N_Subtype_Indication if the subtype
8260 -- indication has a constraint.
8261
8262 Indic := Subtype_Indication (Type_Def);
8263 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8264
8265 -- Check that the type has visible discriminants. The type may be
8266 -- a private type with unknown discriminants whose full view has
8267 -- discriminants which are invisible.
8268
8269 if Constraint_Present then
8270 if not Has_Discriminants (Parent_Base)
8271 or else
8272 (Has_Unknown_Discriminants (Parent_Base)
8273 and then Is_Private_Type (Parent_Base))
8274 then
8275 Error_Msg_N
8276 ("invalid constraint: type has no discriminant",
8277 Constraint (Indic));
8278
8279 Constraint_Present := False;
8280 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8281
8282 elsif Is_Constrained (Parent_Type) then
8283 Error_Msg_N
8284 ("invalid constraint: parent type is already constrained",
8285 Constraint (Indic));
8286
8287 Constraint_Present := False;
8288 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8289 end if;
8290 end if;
8291
8292 -- STEP 0b: If needed, apply transformation given in point 5. above
8293
8294 if not Private_Extension
8295 and then Has_Discriminants (Parent_Type)
8296 and then not Discriminant_Specs
8297 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8298 then
8299 -- First, we must analyze the constraint (see comment in point 5.)
8300 -- The constraint may come from the subtype indication of the full
8301 -- declaration.
8302
8303 if Constraint_Present then
8304 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8305
8306 -- If there is no explicit constraint, there might be one that is
8307 -- inherited from a constrained parent type. In that case verify that
8308 -- it conforms to the constraint in the partial view. In perverse
8309 -- cases the parent subtypes of the partial and full view can have
8310 -- different constraints.
8311
8312 elsif Present (Stored_Constraint (Parent_Type)) then
8313 New_Discrs := Stored_Constraint (Parent_Type);
8314
8315 else
8316 New_Discrs := No_Elist;
8317 end if;
8318
8319 if Has_Discriminants (Derived_Type)
8320 and then Has_Private_Declaration (Derived_Type)
8321 and then Present (Discriminant_Constraint (Derived_Type))
8322 and then Present (New_Discrs)
8323 then
8324 -- Verify that constraints of the full view statically match
8325 -- those given in the partial view.
8326
8327 declare
8328 C1, C2 : Elmt_Id;
8329
8330 begin
8331 C1 := First_Elmt (New_Discrs);
8332 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8333 while Present (C1) and then Present (C2) loop
8334 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8335 or else
8336 (Is_OK_Static_Expression (Node (C1))
8337 and then Is_OK_Static_Expression (Node (C2))
8338 and then
8339 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8340 then
8341 null;
8342
8343 else
8344 if Constraint_Present then
8345 Error_Msg_N
8346 ("constraint not conformant to previous declaration",
8347 Node (C1));
8348 else
8349 Error_Msg_N
8350 ("constraint of full view is incompatible "
8351 & "with partial view", N);
8352 end if;
8353 end if;
8354
8355 Next_Elmt (C1);
8356 Next_Elmt (C2);
8357 end loop;
8358 end;
8359 end if;
8360
8361 -- Insert and analyze the declaration for the unconstrained base type
8362
8363 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8364
8365 New_Decl :=
8366 Make_Full_Type_Declaration (Loc,
8367 Defining_Identifier => New_Base,
8368 Type_Definition =>
8369 Make_Derived_Type_Definition (Loc,
8370 Abstract_Present => Abstract_Present (Type_Def),
8371 Limited_Present => Limited_Present (Type_Def),
8372 Subtype_Indication =>
8373 New_Occurrence_Of (Parent_Base, Loc),
8374 Record_Extension_Part =>
8375 Relocate_Node (Record_Extension_Part (Type_Def)),
8376 Interface_List => Interface_List (Type_Def)));
8377
8378 Set_Parent (New_Decl, Parent (N));
8379 Mark_Rewrite_Insertion (New_Decl);
8380 Insert_Before (N, New_Decl);
8381
8382 -- In the extension case, make sure ancestor is frozen appropriately
8383 -- (see also non-discriminated case below).
8384
8385 if Present (Record_Extension_Part (Type_Def))
8386 or else Is_Interface (Parent_Base)
8387 then
8388 Freeze_Before (New_Decl, Parent_Type);
8389 end if;
8390
8391 -- Note that this call passes False for the Derive_Subps parameter
8392 -- because subprogram derivation is deferred until after creating
8393 -- the subtype (see below).
8394
8395 Build_Derived_Type
8396 (New_Decl, Parent_Base, New_Base,
8397 Is_Completion => False, Derive_Subps => False);
8398
8399 -- ??? This needs re-examination to determine whether the
8400 -- above call can simply be replaced by a call to Analyze.
8401
8402 Set_Analyzed (New_Decl);
8403
8404 -- Insert and analyze the declaration for the constrained subtype
8405
8406 if Constraint_Present then
8407 New_Indic :=
8408 Make_Subtype_Indication (Loc,
8409 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8410 Constraint => Relocate_Node (Constraint (Indic)));
8411
8412 else
8413 declare
8414 Constr_List : constant List_Id := New_List;
8415 C : Elmt_Id;
8416 Expr : Node_Id;
8417
8418 begin
8419 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8420 while Present (C) loop
8421 Expr := Node (C);
8422
8423 -- It is safe here to call New_Copy_Tree since we called
8424 -- Force_Evaluation on each constraint previously
8425 -- in Build_Discriminant_Constraints.
8426
8427 Append (New_Copy_Tree (Expr), To => Constr_List);
8428
8429 Next_Elmt (C);
8430 end loop;
8431
8432 New_Indic :=
8433 Make_Subtype_Indication (Loc,
8434 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8435 Constraint =>
8436 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8437 end;
8438 end if;
8439
8440 Rewrite (N,
8441 Make_Subtype_Declaration (Loc,
8442 Defining_Identifier => Derived_Type,
8443 Subtype_Indication => New_Indic));
8444
8445 Analyze (N);
8446
8447 -- Derivation of subprograms must be delayed until the full subtype
8448 -- has been established, to ensure proper overriding of subprograms
8449 -- inherited by full types. If the derivations occurred as part of
8450 -- the call to Build_Derived_Type above, then the check for type
8451 -- conformance would fail because earlier primitive subprograms
8452 -- could still refer to the full type prior the change to the new
8453 -- subtype and hence would not match the new base type created here.
8454 -- Subprograms are not derived, however, when Derive_Subps is False
8455 -- (since otherwise there could be redundant derivations).
8456
8457 if Derive_Subps then
8458 Derive_Subprograms (Parent_Type, Derived_Type);
8459 end if;
8460
8461 -- For tagged types the Discriminant_Constraint of the new base itype
8462 -- is inherited from the first subtype so that no subtype conformance
8463 -- problem arise when the first subtype overrides primitive
8464 -- operations inherited by the implicit base type.
8465
8466 if Is_Tagged then
8467 Set_Discriminant_Constraint
8468 (New_Base, Discriminant_Constraint (Derived_Type));
8469 end if;
8470
8471 return;
8472 end if;
8473
8474 -- If we get here Derived_Type will have no discriminants or it will be
8475 -- a discriminated unconstrained base type.
8476
8477 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8478
8479 if Is_Tagged then
8480
8481 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8482 -- The declaration of a specific descendant of an interface type
8483 -- freezes the interface type (RM 13.14).
8484
8485 if not Private_Extension or else Is_Interface (Parent_Base) then
8486 Freeze_Before (N, Parent_Type);
8487 end if;
8488
8489 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8490 -- cannot be declared at a deeper level than its parent type is
8491 -- removed. The check on derivation within a generic body is also
8492 -- relaxed, but there's a restriction that a derived tagged type
8493 -- cannot be declared in a generic body if it's derived directly
8494 -- or indirectly from a formal type of that generic.
8495
8496 if Ada_Version >= Ada_2005 then
8497 if Present (Enclosing_Generic_Body (Derived_Type)) then
8498 declare
8499 Ancestor_Type : Entity_Id;
8500
8501 begin
8502 -- Check to see if any ancestor of the derived type is a
8503 -- formal type.
8504
8505 Ancestor_Type := Parent_Type;
8506 while not Is_Generic_Type (Ancestor_Type)
8507 and then Etype (Ancestor_Type) /= Ancestor_Type
8508 loop
8509 Ancestor_Type := Etype (Ancestor_Type);
8510 end loop;
8511
8512 -- If the derived type does have a formal type as an
8513 -- ancestor, then it's an error if the derived type is
8514 -- declared within the body of the generic unit that
8515 -- declares the formal type in its generic formal part. It's
8516 -- sufficient to check whether the ancestor type is declared
8517 -- inside the same generic body as the derived type (such as
8518 -- within a nested generic spec), in which case the
8519 -- derivation is legal. If the formal type is declared
8520 -- outside of that generic body, then it's guaranteed that
8521 -- the derived type is declared within the generic body of
8522 -- the generic unit declaring the formal type.
8523
8524 if Is_Generic_Type (Ancestor_Type)
8525 and then Enclosing_Generic_Body (Ancestor_Type) /=
8526 Enclosing_Generic_Body (Derived_Type)
8527 then
8528 Error_Msg_NE
8529 ("parent type of& must not be descendant of formal type"
8530 & " of an enclosing generic body",
8531 Indic, Derived_Type);
8532 end if;
8533 end;
8534 end if;
8535
8536 elsif Type_Access_Level (Derived_Type) /=
8537 Type_Access_Level (Parent_Type)
8538 and then not Is_Generic_Type (Derived_Type)
8539 then
8540 if Is_Controlled (Parent_Type) then
8541 Error_Msg_N
8542 ("controlled type must be declared at the library level",
8543 Indic);
8544 else
8545 Error_Msg_N
8546 ("type extension at deeper accessibility level than parent",
8547 Indic);
8548 end if;
8549
8550 else
8551 declare
8552 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8553 begin
8554 if Present (GB)
8555 and then GB /= Enclosing_Generic_Body (Parent_Base)
8556 then
8557 Error_Msg_NE
8558 ("parent type of& must not be outside generic body"
8559 & " (RM 3.9.1(4))",
8560 Indic, Derived_Type);
8561 end if;
8562 end;
8563 end if;
8564 end if;
8565
8566 -- Ada 2005 (AI-251)
8567
8568 if Ada_Version >= Ada_2005 and then Is_Tagged then
8569
8570 -- "The declaration of a specific descendant of an interface type
8571 -- freezes the interface type" (RM 13.14).
8572
8573 declare
8574 Iface : Node_Id;
8575 begin
8576 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8577 Iface := First (Interface_List (Type_Def));
8578 while Present (Iface) loop
8579 Freeze_Before (N, Etype (Iface));
8580 Next (Iface);
8581 end loop;
8582 end if;
8583 end;
8584 end if;
8585
8586 -- STEP 1b : preliminary cleanup of the full view of private types
8587
8588 -- If the type is already marked as having discriminants, then it's the
8589 -- completion of a private type or private extension and we need to
8590 -- retain the discriminants from the partial view if the current
8591 -- declaration has Discriminant_Specifications so that we can verify
8592 -- conformance. However, we must remove any existing components that
8593 -- were inherited from the parent (and attached in Copy_And_Swap)
8594 -- because the full type inherits all appropriate components anyway, and
8595 -- we do not want the partial view's components interfering.
8596
8597 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8598 Discrim := First_Discriminant (Derived_Type);
8599 loop
8600 Last_Discrim := Discrim;
8601 Next_Discriminant (Discrim);
8602 exit when No (Discrim);
8603 end loop;
8604
8605 Set_Last_Entity (Derived_Type, Last_Discrim);
8606
8607 -- In all other cases wipe out the list of inherited components (even
8608 -- inherited discriminants), it will be properly rebuilt here.
8609
8610 else
8611 Set_First_Entity (Derived_Type, Empty);
8612 Set_Last_Entity (Derived_Type, Empty);
8613 end if;
8614
8615 -- STEP 1c: Initialize some flags for the Derived_Type
8616
8617 -- The following flags must be initialized here so that
8618 -- Process_Discriminants can check that discriminants of tagged types do
8619 -- not have a default initial value and that access discriminants are
8620 -- only specified for limited records. For completeness, these flags are
8621 -- also initialized along with all the other flags below.
8622
8623 -- AI-419: Limitedness is not inherited from an interface parent, so to
8624 -- be limited in that case the type must be explicitly declared as
8625 -- limited. However, task and protected interfaces are always limited.
8626
8627 if Limited_Present (Type_Def) then
8628 Set_Is_Limited_Record (Derived_Type);
8629
8630 elsif Is_Limited_Record (Parent_Type)
8631 or else (Present (Full_View (Parent_Type))
8632 and then Is_Limited_Record (Full_View (Parent_Type)))
8633 then
8634 if not Is_Interface (Parent_Type)
8635 or else Is_Synchronized_Interface (Parent_Type)
8636 or else Is_Protected_Interface (Parent_Type)
8637 or else Is_Task_Interface (Parent_Type)
8638 then
8639 Set_Is_Limited_Record (Derived_Type);
8640 end if;
8641 end if;
8642
8643 -- STEP 2a: process discriminants of derived type if any
8644
8645 Push_Scope (Derived_Type);
8646
8647 if Discriminant_Specs then
8648 Set_Has_Unknown_Discriminants (Derived_Type, False);
8649
8650 -- The following call initializes fields Has_Discriminants and
8651 -- Discriminant_Constraint, unless we are processing the completion
8652 -- of a private type declaration.
8653
8654 Check_Or_Process_Discriminants (N, Derived_Type);
8655
8656 -- For untagged types, the constraint on the Parent_Type must be
8657 -- present and is used to rename the discriminants.
8658
8659 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8660 Error_Msg_N ("untagged parent must have discriminants", Indic);
8661
8662 elsif not Is_Tagged and then not Constraint_Present then
8663 Error_Msg_N
8664 ("discriminant constraint needed for derived untagged records",
8665 Indic);
8666
8667 -- Otherwise the parent subtype must be constrained unless we have a
8668 -- private extension.
8669
8670 elsif not Constraint_Present
8671 and then not Private_Extension
8672 and then not Is_Constrained (Parent_Type)
8673 then
8674 Error_Msg_N
8675 ("unconstrained type not allowed in this context", Indic);
8676
8677 elsif Constraint_Present then
8678 -- The following call sets the field Corresponding_Discriminant
8679 -- for the discriminants in the Derived_Type.
8680
8681 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8682
8683 -- For untagged types all new discriminants must rename
8684 -- discriminants in the parent. For private extensions new
8685 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8686
8687 Discrim := First_Discriminant (Derived_Type);
8688 while Present (Discrim) loop
8689 if not Is_Tagged
8690 and then No (Corresponding_Discriminant (Discrim))
8691 then
8692 Error_Msg_N
8693 ("new discriminants must constrain old ones", Discrim);
8694
8695 elsif Private_Extension
8696 and then Present (Corresponding_Discriminant (Discrim))
8697 then
8698 Error_Msg_N
8699 ("only static constraints allowed for parent"
8700 & " discriminants in the partial view", Indic);
8701 exit;
8702 end if;
8703
8704 -- If a new discriminant is used in the constraint, then its
8705 -- subtype must be statically compatible with the parent
8706 -- discriminant's subtype (3.7(15)).
8707
8708 -- However, if the record contains an array constrained by
8709 -- the discriminant but with some different bound, the compiler
8710 -- attemps to create a smaller range for the discriminant type.
8711 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8712 -- the discriminant type is a scalar type, the check must use
8713 -- the original discriminant type in the parent declaration.
8714
8715 declare
8716 Corr_Disc : constant Entity_Id :=
8717 Corresponding_Discriminant (Discrim);
8718 Disc_Type : constant Entity_Id := Etype (Discrim);
8719 Corr_Type : Entity_Id;
8720
8721 begin
8722 if Present (Corr_Disc) then
8723 if Is_Scalar_Type (Disc_Type) then
8724 Corr_Type :=
8725 Entity (Discriminant_Type (Parent (Corr_Disc)));
8726 else
8727 Corr_Type := Etype (Corr_Disc);
8728 end if;
8729
8730 if not
8731 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8732 then
8733 Error_Msg_N
8734 ("subtype must be compatible "
8735 & "with parent discriminant",
8736 Discrim);
8737 end if;
8738 end if;
8739 end;
8740
8741 Next_Discriminant (Discrim);
8742 end loop;
8743
8744 -- Check whether the constraints of the full view statically
8745 -- match those imposed by the parent subtype [7.3(13)].
8746
8747 if Present (Stored_Constraint (Derived_Type)) then
8748 declare
8749 C1, C2 : Elmt_Id;
8750
8751 begin
8752 C1 := First_Elmt (Discs);
8753 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8754 while Present (C1) and then Present (C2) loop
8755 if not
8756 Fully_Conformant_Expressions (Node (C1), Node (C2))
8757 then
8758 Error_Msg_N
8759 ("not conformant with previous declaration",
8760 Node (C1));
8761 end if;
8762
8763 Next_Elmt (C1);
8764 Next_Elmt (C2);
8765 end loop;
8766 end;
8767 end if;
8768 end if;
8769
8770 -- STEP 2b: No new discriminants, inherit discriminants if any
8771
8772 else
8773 if Private_Extension then
8774 Set_Has_Unknown_Discriminants
8775 (Derived_Type,
8776 Has_Unknown_Discriminants (Parent_Type)
8777 or else Unknown_Discriminants_Present (N));
8778
8779 -- The partial view of the parent may have unknown discriminants,
8780 -- but if the full view has discriminants and the parent type is
8781 -- in scope they must be inherited.
8782
8783 elsif Has_Unknown_Discriminants (Parent_Type)
8784 and then
8785 (not Has_Discriminants (Parent_Type)
8786 or else not In_Open_Scopes (Scope (Parent_Type)))
8787 then
8788 Set_Has_Unknown_Discriminants (Derived_Type);
8789 end if;
8790
8791 if not Has_Unknown_Discriminants (Derived_Type)
8792 and then not Has_Unknown_Discriminants (Parent_Base)
8793 and then Has_Discriminants (Parent_Type)
8794 then
8795 Inherit_Discrims := True;
8796 Set_Has_Discriminants
8797 (Derived_Type, True);
8798 Set_Discriminant_Constraint
8799 (Derived_Type, Discriminant_Constraint (Parent_Base));
8800 end if;
8801
8802 -- The following test is true for private types (remember
8803 -- transformation 5. is not applied to those) and in an error
8804 -- situation.
8805
8806 if Constraint_Present then
8807 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8808 end if;
8809
8810 -- For now mark a new derived type as constrained only if it has no
8811 -- discriminants. At the end of Build_Derived_Record_Type we properly
8812 -- set this flag in the case of private extensions. See comments in
8813 -- point 9. just before body of Build_Derived_Record_Type.
8814
8815 Set_Is_Constrained
8816 (Derived_Type,
8817 not (Inherit_Discrims
8818 or else Has_Unknown_Discriminants (Derived_Type)));
8819 end if;
8820
8821 -- STEP 3: initialize fields of derived type
8822
8823 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8824 Set_Stored_Constraint (Derived_Type, No_Elist);
8825
8826 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8827 -- but cannot be interfaces
8828
8829 if not Private_Extension
8830 and then Ekind (Derived_Type) /= E_Private_Type
8831 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8832 then
8833 if Interface_Present (Type_Def) then
8834 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8835 end if;
8836
8837 Set_Interfaces (Derived_Type, No_Elist);
8838 end if;
8839
8840 -- Fields inherited from the Parent_Type
8841
8842 Set_Has_Specified_Layout
8843 (Derived_Type, Has_Specified_Layout (Parent_Type));
8844 Set_Is_Limited_Composite
8845 (Derived_Type, Is_Limited_Composite (Parent_Type));
8846 Set_Is_Private_Composite
8847 (Derived_Type, Is_Private_Composite (Parent_Type));
8848
8849 if Is_Tagged_Type (Parent_Type) then
8850 Set_No_Tagged_Streams_Pragma
8851 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8852 end if;
8853
8854 -- Fields inherited from the Parent_Base
8855
8856 Set_Has_Controlled_Component
8857 (Derived_Type, Has_Controlled_Component (Parent_Base));
8858 Set_Has_Non_Standard_Rep
8859 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8860 Set_Has_Primitive_Operations
8861 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8862
8863 -- Fields inherited from the Parent_Base in the non-private case
8864
8865 if Ekind (Derived_Type) = E_Record_Type then
8866 Set_Has_Complex_Representation
8867 (Derived_Type, Has_Complex_Representation (Parent_Base));
8868 end if;
8869
8870 -- Fields inherited from the Parent_Base for record types
8871
8872 if Is_Record_Type (Derived_Type) then
8873 declare
8874 Parent_Full : Entity_Id;
8875
8876 begin
8877 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8878 -- Parent_Base can be a private type or private extension. Go
8879 -- to the full view here to get the E_Record_Type specific flags.
8880
8881 if Present (Full_View (Parent_Base)) then
8882 Parent_Full := Full_View (Parent_Base);
8883 else
8884 Parent_Full := Parent_Base;
8885 end if;
8886
8887 Set_OK_To_Reorder_Components
8888 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8889 end;
8890 end if;
8891
8892 -- Set fields for private derived types
8893
8894 if Is_Private_Type (Derived_Type) then
8895 Set_Depends_On_Private (Derived_Type, True);
8896 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8897
8898 -- Inherit fields from non private record types. If this is the
8899 -- completion of a derivation from a private type, the parent itself
8900 -- is private, and the attributes come from its full view, which must
8901 -- be present.
8902
8903 else
8904 if Is_Private_Type (Parent_Base)
8905 and then not Is_Record_Type (Parent_Base)
8906 then
8907 Set_Component_Alignment
8908 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8909 Set_C_Pass_By_Copy
8910 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8911 else
8912 Set_Component_Alignment
8913 (Derived_Type, Component_Alignment (Parent_Base));
8914 Set_C_Pass_By_Copy
8915 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8916 end if;
8917 end if;
8918
8919 -- Set fields for tagged types
8920
8921 if Is_Tagged then
8922 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8923
8924 -- All tagged types defined in Ada.Finalization are controlled
8925
8926 if Chars (Scope (Derived_Type)) = Name_Finalization
8927 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8928 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8929 then
8930 Set_Is_Controlled (Derived_Type);
8931 else
8932 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8933 end if;
8934
8935 -- Minor optimization: there is no need to generate the class-wide
8936 -- entity associated with an underlying record view.
8937
8938 if not Is_Underlying_Record_View (Derived_Type) then
8939 Make_Class_Wide_Type (Derived_Type);
8940 end if;
8941
8942 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8943
8944 if Has_Discriminants (Derived_Type)
8945 and then Constraint_Present
8946 then
8947 Set_Stored_Constraint
8948 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8949 end if;
8950
8951 if Ada_Version >= Ada_2005 then
8952 declare
8953 Ifaces_List : Elist_Id;
8954
8955 begin
8956 -- Checks rules 3.9.4 (13/2 and 14/2)
8957
8958 if Comes_From_Source (Derived_Type)
8959 and then not Is_Private_Type (Derived_Type)
8960 and then Is_Interface (Parent_Type)
8961 and then not Is_Interface (Derived_Type)
8962 then
8963 if Is_Task_Interface (Parent_Type) then
8964 Error_Msg_N
8965 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8966 Derived_Type);
8967
8968 elsif Is_Protected_Interface (Parent_Type) then
8969 Error_Msg_N
8970 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8971 Derived_Type);
8972 end if;
8973 end if;
8974
8975 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8976
8977 Check_Interfaces (N, Type_Def);
8978
8979 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8980 -- not already in the parents.
8981
8982 Collect_Interfaces
8983 (T => Derived_Type,
8984 Ifaces_List => Ifaces_List,
8985 Exclude_Parents => True);
8986
8987 Set_Interfaces (Derived_Type, Ifaces_List);
8988
8989 -- If the derived type is the anonymous type created for
8990 -- a declaration whose parent has a constraint, propagate
8991 -- the interface list to the source type. This must be done
8992 -- prior to the completion of the analysis of the source type
8993 -- because the components in the extension may contain current
8994 -- instances whose legality depends on some ancestor.
8995
8996 if Is_Itype (Derived_Type) then
8997 declare
8998 Def : constant Node_Id :=
8999 Associated_Node_For_Itype (Derived_Type);
9000 begin
9001 if Present (Def)
9002 and then Nkind (Def) = N_Full_Type_Declaration
9003 then
9004 Set_Interfaces
9005 (Defining_Identifier (Def), Ifaces_List);
9006 end if;
9007 end;
9008 end if;
9009
9010 -- A type extension is automatically Ghost when one of its
9011 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9012 -- also inherited when the parent type is Ghost, but this is
9013 -- done in Build_Derived_Type as the mechanism also handles
9014 -- untagged derivations.
9015
9016 if Implements_Ghost_Interface (Derived_Type) then
9017 Set_Is_Ghost_Entity (Derived_Type);
9018 end if;
9019 end;
9020 end if;
9021
9022 else
9023 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
9024 Set_Has_Non_Standard_Rep
9025 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9026 end if;
9027
9028 -- STEP 4: Inherit components from the parent base and constrain them.
9029 -- Apply the second transformation described in point 6. above.
9030
9031 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9032 or else not Has_Discriminants (Parent_Type)
9033 or else not Is_Constrained (Parent_Type)
9034 then
9035 Constrs := Discs;
9036 else
9037 Constrs := Discriminant_Constraint (Parent_Type);
9038 end if;
9039
9040 Assoc_List :=
9041 Inherit_Components
9042 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9043
9044 -- STEP 5a: Copy the parent record declaration for untagged types
9045
9046 Set_Has_Implicit_Dereference
9047 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9048
9049 if not Is_Tagged then
9050
9051 -- Discriminant_Constraint (Derived_Type) has been properly
9052 -- constructed. Save it and temporarily set it to Empty because we
9053 -- do not want the call to New_Copy_Tree below to mess this list.
9054
9055 if Has_Discriminants (Derived_Type) then
9056 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9057 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9058 else
9059 Save_Discr_Constr := No_Elist;
9060 end if;
9061
9062 -- Save the Etype field of Derived_Type. It is correctly set now,
9063 -- but the call to New_Copy tree may remap it to point to itself,
9064 -- which is not what we want. Ditto for the Next_Entity field.
9065
9066 Save_Etype := Etype (Derived_Type);
9067 Save_Next_Entity := Next_Entity (Derived_Type);
9068
9069 -- Assoc_List maps all stored discriminants in the Parent_Base to
9070 -- stored discriminants in the Derived_Type. It is fundamental that
9071 -- no types or itypes with discriminants other than the stored
9072 -- discriminants appear in the entities declared inside
9073 -- Derived_Type, since the back end cannot deal with it.
9074
9075 New_Decl :=
9076 New_Copy_Tree
9077 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9078
9079 -- Restore the fields saved prior to the New_Copy_Tree call
9080 -- and compute the stored constraint.
9081
9082 Set_Etype (Derived_Type, Save_Etype);
9083 Set_Next_Entity (Derived_Type, Save_Next_Entity);
9084
9085 if Has_Discriminants (Derived_Type) then
9086 Set_Discriminant_Constraint
9087 (Derived_Type, Save_Discr_Constr);
9088 Set_Stored_Constraint
9089 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9090 Replace_Components (Derived_Type, New_Decl);
9091 end if;
9092
9093 -- Insert the new derived type declaration
9094
9095 Rewrite (N, New_Decl);
9096
9097 -- STEP 5b: Complete the processing for record extensions in generics
9098
9099 -- There is no completion for record extensions declared in the
9100 -- parameter part of a generic, so we need to complete processing for
9101 -- these generic record extensions here. The Record_Type_Definition call
9102 -- will change the Ekind of the components from E_Void to E_Component.
9103
9104 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9105 Record_Type_Definition (Empty, Derived_Type);
9106
9107 -- STEP 5c: Process the record extension for non private tagged types
9108
9109 elsif not Private_Extension then
9110 Expand_Record_Extension (Derived_Type, Type_Def);
9111
9112 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9113 -- derived type to propagate some semantic information. This led
9114 -- to other ASIS failures and has been removed.
9115
9116 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9117 -- implemented interfaces if we are in expansion mode
9118
9119 if Expander_Active
9120 and then Has_Interfaces (Derived_Type)
9121 then
9122 Add_Interface_Tag_Components (N, Derived_Type);
9123 end if;
9124
9125 -- Analyze the record extension
9126
9127 Record_Type_Definition
9128 (Record_Extension_Part (Type_Def), Derived_Type);
9129 end if;
9130
9131 End_Scope;
9132
9133 -- Nothing else to do if there is an error in the derivation.
9134 -- An unusual case: the full view may be derived from a type in an
9135 -- instance, when the partial view was used illegally as an actual
9136 -- in that instance, leading to a circular definition.
9137
9138 if Etype (Derived_Type) = Any_Type
9139 or else Etype (Parent_Type) = Derived_Type
9140 then
9141 return;
9142 end if;
9143
9144 -- Set delayed freeze and then derive subprograms, we need to do
9145 -- this in this order so that derived subprograms inherit the
9146 -- derived freeze if necessary.
9147
9148 Set_Has_Delayed_Freeze (Derived_Type);
9149
9150 if Derive_Subps then
9151 Derive_Subprograms (Parent_Type, Derived_Type);
9152 end if;
9153
9154 -- If we have a private extension which defines a constrained derived
9155 -- type mark as constrained here after we have derived subprograms. See
9156 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9157
9158 if Private_Extension and then Inherit_Discrims then
9159 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9160 Set_Is_Constrained (Derived_Type, True);
9161 Set_Discriminant_Constraint (Derived_Type, Discs);
9162
9163 elsif Is_Constrained (Parent_Type) then
9164 Set_Is_Constrained
9165 (Derived_Type, True);
9166 Set_Discriminant_Constraint
9167 (Derived_Type, Discriminant_Constraint (Parent_Type));
9168 end if;
9169 end if;
9170
9171 -- Update the class-wide type, which shares the now-completed entity
9172 -- list with its specific type. In case of underlying record views,
9173 -- we do not generate the corresponding class wide entity.
9174
9175 if Is_Tagged
9176 and then not Is_Underlying_Record_View (Derived_Type)
9177 then
9178 Set_First_Entity
9179 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9180 Set_Last_Entity
9181 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9182 end if;
9183
9184 Check_Function_Writable_Actuals (N);
9185 end Build_Derived_Record_Type;
9186
9187 ------------------------
9188 -- Build_Derived_Type --
9189 ------------------------
9190
9191 procedure Build_Derived_Type
9192 (N : Node_Id;
9193 Parent_Type : Entity_Id;
9194 Derived_Type : Entity_Id;
9195 Is_Completion : Boolean;
9196 Derive_Subps : Boolean := True)
9197 is
9198 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9199
9200 begin
9201 -- Set common attributes
9202
9203 Set_Scope (Derived_Type, Current_Scope);
9204
9205 Set_Etype (Derived_Type, Parent_Base);
9206 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9207 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9208
9209 Set_Size_Info (Derived_Type, Parent_Type);
9210 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9211 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
9212 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9213
9214 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9215 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9216
9217 if Is_Tagged_Type (Derived_Type) then
9218 Set_No_Tagged_Streams_Pragma
9219 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9220 end if;
9221
9222 -- If the parent has primitive routines, set the derived type link
9223
9224 if Has_Primitive_Operations (Parent_Type) then
9225 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9226 end if;
9227
9228 -- If the parent type is a private subtype, the convention on the base
9229 -- type may be set in the private part, and not propagated to the
9230 -- subtype until later, so we obtain the convention from the base type.
9231
9232 Set_Convention (Derived_Type, Convention (Parent_Base));
9233
9234 -- Set SSO default for record or array type
9235
9236 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9237 and then Is_Base_Type (Derived_Type)
9238 then
9239 Set_Default_SSO (Derived_Type);
9240 end if;
9241
9242 -- A derived type inherits the Default_Initial_Condition pragma coming
9243 -- from any parent type within the derivation chain.
9244
9245 if Has_DIC (Parent_Type) then
9246 Set_Has_Inherited_DIC (Derived_Type);
9247 end if;
9248
9249 -- A derived type inherits any class-wide invariants coming from a
9250 -- parent type or an interface. Note that the invariant procedure of
9251 -- the parent type should not be inherited because the derived type may
9252 -- define invariants of its own.
9253
9254 if not Is_Interface (Derived_Type) then
9255 if Has_Inherited_Invariants (Parent_Type)
9256 or else Has_Inheritable_Invariants (Parent_Type)
9257 then
9258 Set_Has_Inherited_Invariants (Derived_Type);
9259
9260 elsif Is_Concurrent_Type (Derived_Type)
9261 or else Is_Tagged_Type (Derived_Type)
9262 then
9263 declare
9264 Iface : Entity_Id;
9265 Ifaces : Elist_Id;
9266 Iface_Elmt : Elmt_Id;
9267
9268 begin
9269 Collect_Interfaces
9270 (T => Derived_Type,
9271 Ifaces_List => Ifaces,
9272 Exclude_Parents => True);
9273
9274 if Present (Ifaces) then
9275 Iface_Elmt := First_Elmt (Ifaces);
9276 while Present (Iface_Elmt) loop
9277 Iface := Node (Iface_Elmt);
9278
9279 if Has_Inheritable_Invariants (Iface) then
9280 Set_Has_Inherited_Invariants (Derived_Type);
9281 exit;
9282 end if;
9283
9284 Next_Elmt (Iface_Elmt);
9285 end loop;
9286 end if;
9287 end;
9288 end if;
9289 end if;
9290
9291 -- We similarly inherit predicates. Note that for scalar derived types
9292 -- the predicate is inherited from the first subtype, and not from its
9293 -- (anonymous) base type.
9294
9295 if Has_Predicates (Parent_Type)
9296 or else Has_Predicates (First_Subtype (Parent_Type))
9297 then
9298 Set_Has_Predicates (Derived_Type);
9299 end if;
9300
9301 -- The derived type inherits the representation clauses of the parent
9302
9303 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9304
9305 -- If the parent type has delayed rep aspects, then mark the derived
9306 -- type as possibly inheriting a delayed rep aspect.
9307
9308 if Has_Delayed_Rep_Aspects (Parent_Type) then
9309 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9310 end if;
9311
9312 -- A derived type becomes Ghost when its parent type is also Ghost
9313 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9314 -- directly inherited because the Ghost policy in effect may differ.
9315
9316 if Is_Ghost_Entity (Parent_Type) then
9317 Set_Is_Ghost_Entity (Derived_Type);
9318 end if;
9319
9320 -- Type dependent processing
9321
9322 case Ekind (Parent_Type) is
9323 when Numeric_Kind =>
9324 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9325
9326 when Array_Kind =>
9327 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9328
9329 when Class_Wide_Kind
9330 | E_Record_Subtype
9331 | E_Record_Type
9332 =>
9333 Build_Derived_Record_Type
9334 (N, Parent_Type, Derived_Type, Derive_Subps);
9335 return;
9336
9337 when Enumeration_Kind =>
9338 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9339
9340 when Access_Kind =>
9341 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9342
9343 when Incomplete_Or_Private_Kind =>
9344 Build_Derived_Private_Type
9345 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9346
9347 -- For discriminated types, the derivation includes deriving
9348 -- primitive operations. For others it is done below.
9349
9350 if Is_Tagged_Type (Parent_Type)
9351 or else Has_Discriminants (Parent_Type)
9352 or else (Present (Full_View (Parent_Type))
9353 and then Has_Discriminants (Full_View (Parent_Type)))
9354 then
9355 return;
9356 end if;
9357
9358 when Concurrent_Kind =>
9359 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9360
9361 when others =>
9362 raise Program_Error;
9363 end case;
9364
9365 -- Nothing more to do if some error occurred
9366
9367 if Etype (Derived_Type) = Any_Type then
9368 return;
9369 end if;
9370
9371 -- Set delayed freeze and then derive subprograms, we need to do this
9372 -- in this order so that derived subprograms inherit the derived freeze
9373 -- if necessary.
9374
9375 Set_Has_Delayed_Freeze (Derived_Type);
9376
9377 if Derive_Subps then
9378 Derive_Subprograms (Parent_Type, Derived_Type);
9379 end if;
9380
9381 Set_Has_Primitive_Operations
9382 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9383 end Build_Derived_Type;
9384
9385 -----------------------
9386 -- Build_Discriminal --
9387 -----------------------
9388
9389 procedure Build_Discriminal (Discrim : Entity_Id) is
9390 D_Minal : Entity_Id;
9391 CR_Disc : Entity_Id;
9392
9393 begin
9394 -- A discriminal has the same name as the discriminant
9395
9396 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9397
9398 Set_Ekind (D_Minal, E_In_Parameter);
9399 Set_Mechanism (D_Minal, Default_Mechanism);
9400 Set_Etype (D_Minal, Etype (Discrim));
9401 Set_Scope (D_Minal, Current_Scope);
9402 Set_Parent (D_Minal, Parent (Discrim));
9403
9404 Set_Discriminal (Discrim, D_Minal);
9405 Set_Discriminal_Link (D_Minal, Discrim);
9406
9407 -- For task types, build at once the discriminants of the corresponding
9408 -- record, which are needed if discriminants are used in entry defaults
9409 -- and in family bounds.
9410
9411 if Is_Concurrent_Type (Current_Scope)
9412 or else
9413 Is_Limited_Type (Current_Scope)
9414 then
9415 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9416
9417 Set_Ekind (CR_Disc, E_In_Parameter);
9418 Set_Mechanism (CR_Disc, Default_Mechanism);
9419 Set_Etype (CR_Disc, Etype (Discrim));
9420 Set_Scope (CR_Disc, Current_Scope);
9421 Set_Discriminal_Link (CR_Disc, Discrim);
9422 Set_CR_Discriminant (Discrim, CR_Disc);
9423 end if;
9424 end Build_Discriminal;
9425
9426 ------------------------------------
9427 -- Build_Discriminant_Constraints --
9428 ------------------------------------
9429
9430 function Build_Discriminant_Constraints
9431 (T : Entity_Id;
9432 Def : Node_Id;
9433 Derived_Def : Boolean := False) return Elist_Id
9434 is
9435 C : constant Node_Id := Constraint (Def);
9436 Nb_Discr : constant Nat := Number_Discriminants (T);
9437
9438 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9439 -- Saves the expression corresponding to a given discriminant in T
9440
9441 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9442 -- Return the Position number within array Discr_Expr of a discriminant
9443 -- D within the discriminant list of the discriminated type T.
9444
9445 procedure Process_Discriminant_Expression
9446 (Expr : Node_Id;
9447 D : Entity_Id);
9448 -- If this is a discriminant constraint on a partial view, do not
9449 -- generate an overflow check on the discriminant expression. The check
9450 -- will be generated when constraining the full view. Otherwise the
9451 -- backend creates duplicate symbols for the temporaries corresponding
9452 -- to the expressions to be checked, causing spurious assembler errors.
9453
9454 ------------------
9455 -- Pos_Of_Discr --
9456 ------------------
9457
9458 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9459 Disc : Entity_Id;
9460
9461 begin
9462 Disc := First_Discriminant (T);
9463 for J in Discr_Expr'Range loop
9464 if Disc = D then
9465 return J;
9466 end if;
9467
9468 Next_Discriminant (Disc);
9469 end loop;
9470
9471 -- Note: Since this function is called on discriminants that are
9472 -- known to belong to the discriminated type, falling through the
9473 -- loop with no match signals an internal compiler error.
9474
9475 raise Program_Error;
9476 end Pos_Of_Discr;
9477
9478 -------------------------------------
9479 -- Process_Discriminant_Expression --
9480 -------------------------------------
9481
9482 procedure Process_Discriminant_Expression
9483 (Expr : Node_Id;
9484 D : Entity_Id)
9485 is
9486 BDT : constant Entity_Id := Base_Type (Etype (D));
9487
9488 begin
9489 -- If this is a discriminant constraint on a partial view, do
9490 -- not generate an overflow on the discriminant expression. The
9491 -- check will be generated when constraining the full view.
9492
9493 if Is_Private_Type (T)
9494 and then Present (Full_View (T))
9495 then
9496 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9497 else
9498 Analyze_And_Resolve (Expr, BDT);
9499 end if;
9500 end Process_Discriminant_Expression;
9501
9502 -- Declarations local to Build_Discriminant_Constraints
9503
9504 Discr : Entity_Id;
9505 E : Entity_Id;
9506 Elist : constant Elist_Id := New_Elmt_List;
9507
9508 Constr : Node_Id;
9509 Expr : Node_Id;
9510 Id : Node_Id;
9511 Position : Nat;
9512 Found : Boolean;
9513
9514 Discrim_Present : Boolean := False;
9515
9516 -- Start of processing for Build_Discriminant_Constraints
9517
9518 begin
9519 -- The following loop will process positional associations only.
9520 -- For a positional association, the (single) discriminant is
9521 -- implicitly specified by position, in textual order (RM 3.7.2).
9522
9523 Discr := First_Discriminant (T);
9524 Constr := First (Constraints (C));
9525 for D in Discr_Expr'Range loop
9526 exit when Nkind (Constr) = N_Discriminant_Association;
9527
9528 if No (Constr) then
9529 Error_Msg_N ("too few discriminants given in constraint", C);
9530 return New_Elmt_List;
9531
9532 elsif Nkind (Constr) = N_Range
9533 or else (Nkind (Constr) = N_Attribute_Reference
9534 and then Attribute_Name (Constr) = Name_Range)
9535 then
9536 Error_Msg_N
9537 ("a range is not a valid discriminant constraint", Constr);
9538 Discr_Expr (D) := Error;
9539
9540 else
9541 Process_Discriminant_Expression (Constr, Discr);
9542 Discr_Expr (D) := Constr;
9543 end if;
9544
9545 Next_Discriminant (Discr);
9546 Next (Constr);
9547 end loop;
9548
9549 if No (Discr) and then Present (Constr) then
9550 Error_Msg_N ("too many discriminants given in constraint", Constr);
9551 return New_Elmt_List;
9552 end if;
9553
9554 -- Named associations can be given in any order, but if both positional
9555 -- and named associations are used in the same discriminant constraint,
9556 -- then positional associations must occur first, at their normal
9557 -- position. Hence once a named association is used, the rest of the
9558 -- discriminant constraint must use only named associations.
9559
9560 while Present (Constr) loop
9561
9562 -- Positional association forbidden after a named association
9563
9564 if Nkind (Constr) /= N_Discriminant_Association then
9565 Error_Msg_N ("positional association follows named one", Constr);
9566 return New_Elmt_List;
9567
9568 -- Otherwise it is a named association
9569
9570 else
9571 -- E records the type of the discriminants in the named
9572 -- association. All the discriminants specified in the same name
9573 -- association must have the same type.
9574
9575 E := Empty;
9576
9577 -- Search the list of discriminants in T to see if the simple name
9578 -- given in the constraint matches any of them.
9579
9580 Id := First (Selector_Names (Constr));
9581 while Present (Id) loop
9582 Found := False;
9583
9584 -- If Original_Discriminant is present, we are processing a
9585 -- generic instantiation and this is an instance node. We need
9586 -- to find the name of the corresponding discriminant in the
9587 -- actual record type T and not the name of the discriminant in
9588 -- the generic formal. Example:
9589
9590 -- generic
9591 -- type G (D : int) is private;
9592 -- package P is
9593 -- subtype W is G (D => 1);
9594 -- end package;
9595 -- type Rec (X : int) is record ... end record;
9596 -- package Q is new P (G => Rec);
9597
9598 -- At the point of the instantiation, formal type G is Rec
9599 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9600 -- which really looks like "subtype W is Rec (D => 1);" at
9601 -- the point of instantiation, we want to find the discriminant
9602 -- that corresponds to D in Rec, i.e. X.
9603
9604 if Present (Original_Discriminant (Id))
9605 and then In_Instance
9606 then
9607 Discr := Find_Corresponding_Discriminant (Id, T);
9608 Found := True;
9609
9610 else
9611 Discr := First_Discriminant (T);
9612 while Present (Discr) loop
9613 if Chars (Discr) = Chars (Id) then
9614 Found := True;
9615 exit;
9616 end if;
9617
9618 Next_Discriminant (Discr);
9619 end loop;
9620
9621 if not Found then
9622 Error_Msg_N ("& does not match any discriminant", Id);
9623 return New_Elmt_List;
9624
9625 -- If the parent type is a generic formal, preserve the
9626 -- name of the discriminant for subsequent instances.
9627 -- see comment at the beginning of this if statement.
9628
9629 elsif Is_Generic_Type (Root_Type (T)) then
9630 Set_Original_Discriminant (Id, Discr);
9631 end if;
9632 end if;
9633
9634 Position := Pos_Of_Discr (T, Discr);
9635
9636 if Present (Discr_Expr (Position)) then
9637 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9638
9639 else
9640 -- Each discriminant specified in the same named association
9641 -- must be associated with a separate copy of the
9642 -- corresponding expression.
9643
9644 if Present (Next (Id)) then
9645 Expr := New_Copy_Tree (Expression (Constr));
9646 Set_Parent (Expr, Parent (Expression (Constr)));
9647 else
9648 Expr := Expression (Constr);
9649 end if;
9650
9651 Discr_Expr (Position) := Expr;
9652 Process_Discriminant_Expression (Expr, Discr);
9653 end if;
9654
9655 -- A discriminant association with more than one discriminant
9656 -- name is only allowed if the named discriminants are all of
9657 -- the same type (RM 3.7.1(8)).
9658
9659 if E = Empty then
9660 E := Base_Type (Etype (Discr));
9661
9662 elsif Base_Type (Etype (Discr)) /= E then
9663 Error_Msg_N
9664 ("all discriminants in an association " &
9665 "must have the same type", Id);
9666 end if;
9667
9668 Next (Id);
9669 end loop;
9670 end if;
9671
9672 Next (Constr);
9673 end loop;
9674
9675 -- A discriminant constraint must provide exactly one value for each
9676 -- discriminant of the type (RM 3.7.1(8)).
9677
9678 for J in Discr_Expr'Range loop
9679 if No (Discr_Expr (J)) then
9680 Error_Msg_N ("too few discriminants given in constraint", C);
9681 return New_Elmt_List;
9682 end if;
9683 end loop;
9684
9685 -- Determine if there are discriminant expressions in the constraint
9686
9687 for J in Discr_Expr'Range loop
9688 if Denotes_Discriminant
9689 (Discr_Expr (J), Check_Concurrent => True)
9690 then
9691 Discrim_Present := True;
9692 end if;
9693 end loop;
9694
9695 -- Build an element list consisting of the expressions given in the
9696 -- discriminant constraint and apply the appropriate checks. The list
9697 -- is constructed after resolving any named discriminant associations
9698 -- and therefore the expressions appear in the textual order of the
9699 -- discriminants.
9700
9701 Discr := First_Discriminant (T);
9702 for J in Discr_Expr'Range loop
9703 if Discr_Expr (J) /= Error then
9704 Append_Elmt (Discr_Expr (J), Elist);
9705
9706 -- If any of the discriminant constraints is given by a
9707 -- discriminant and we are in a derived type declaration we
9708 -- have a discriminant renaming. Establish link between new
9709 -- and old discriminant. The new discriminant has an implicit
9710 -- dereference if the old one does.
9711
9712 if Denotes_Discriminant (Discr_Expr (J)) then
9713 if Derived_Def then
9714 declare
9715 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
9716
9717 begin
9718 Set_Corresponding_Discriminant (New_Discr, Discr);
9719 Set_Has_Implicit_Dereference (New_Discr,
9720 Has_Implicit_Dereference (Discr));
9721 end;
9722 end if;
9723
9724 -- Force the evaluation of non-discriminant expressions.
9725 -- If we have found a discriminant in the constraint 3.4(26)
9726 -- and 3.8(18) demand that no range checks are performed are
9727 -- after evaluation. If the constraint is for a component
9728 -- definition that has a per-object constraint, expressions are
9729 -- evaluated but not checked either. In all other cases perform
9730 -- a range check.
9731
9732 else
9733 if Discrim_Present then
9734 null;
9735
9736 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9737 and then Has_Per_Object_Constraint
9738 (Defining_Identifier (Parent (Parent (Def))))
9739 then
9740 null;
9741
9742 elsif Is_Access_Type (Etype (Discr)) then
9743 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9744
9745 else
9746 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9747 end if;
9748
9749 Force_Evaluation (Discr_Expr (J));
9750 end if;
9751
9752 -- Check that the designated type of an access discriminant's
9753 -- expression is not a class-wide type unless the discriminant's
9754 -- designated type is also class-wide.
9755
9756 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9757 and then not Is_Class_Wide_Type
9758 (Designated_Type (Etype (Discr)))
9759 and then Etype (Discr_Expr (J)) /= Any_Type
9760 and then Is_Class_Wide_Type
9761 (Designated_Type (Etype (Discr_Expr (J))))
9762 then
9763 Wrong_Type (Discr_Expr (J), Etype (Discr));
9764
9765 elsif Is_Access_Type (Etype (Discr))
9766 and then not Is_Access_Constant (Etype (Discr))
9767 and then Is_Access_Type (Etype (Discr_Expr (J)))
9768 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9769 then
9770 Error_Msg_NE
9771 ("constraint for discriminant& must be access to variable",
9772 Def, Discr);
9773 end if;
9774 end if;
9775
9776 Next_Discriminant (Discr);
9777 end loop;
9778
9779 return Elist;
9780 end Build_Discriminant_Constraints;
9781
9782 ---------------------------------
9783 -- Build_Discriminated_Subtype --
9784 ---------------------------------
9785
9786 procedure Build_Discriminated_Subtype
9787 (T : Entity_Id;
9788 Def_Id : Entity_Id;
9789 Elist : Elist_Id;
9790 Related_Nod : Node_Id;
9791 For_Access : Boolean := False)
9792 is
9793 Has_Discrs : constant Boolean := Has_Discriminants (T);
9794 Constrained : constant Boolean :=
9795 (Has_Discrs
9796 and then not Is_Empty_Elmt_List (Elist)
9797 and then not Is_Class_Wide_Type (T))
9798 or else Is_Constrained (T);
9799
9800 begin
9801 if Ekind (T) = E_Record_Type then
9802 if For_Access then
9803 Set_Ekind (Def_Id, E_Private_Subtype);
9804 Set_Is_For_Access_Subtype (Def_Id, True);
9805 else
9806 Set_Ekind (Def_Id, E_Record_Subtype);
9807 end if;
9808
9809 -- Inherit preelaboration flag from base, for types for which it
9810 -- may have been set: records, private types, protected types.
9811
9812 Set_Known_To_Have_Preelab_Init
9813 (Def_Id, Known_To_Have_Preelab_Init (T));
9814
9815 elsif Ekind (T) = E_Task_Type then
9816 Set_Ekind (Def_Id, E_Task_Subtype);
9817
9818 elsif Ekind (T) = E_Protected_Type then
9819 Set_Ekind (Def_Id, E_Protected_Subtype);
9820 Set_Known_To_Have_Preelab_Init
9821 (Def_Id, Known_To_Have_Preelab_Init (T));
9822
9823 elsif Is_Private_Type (T) then
9824 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9825 Set_Known_To_Have_Preelab_Init
9826 (Def_Id, Known_To_Have_Preelab_Init (T));
9827
9828 -- Private subtypes may have private dependents
9829
9830 Set_Private_Dependents (Def_Id, New_Elmt_List);
9831
9832 elsif Is_Class_Wide_Type (T) then
9833 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9834
9835 else
9836 -- Incomplete type. Attach subtype to list of dependents, to be
9837 -- completed with full view of parent type, unless is it the
9838 -- designated subtype of a record component within an init_proc.
9839 -- This last case arises for a component of an access type whose
9840 -- designated type is incomplete (e.g. a Taft Amendment type).
9841 -- The designated subtype is within an inner scope, and needs no
9842 -- elaboration, because only the access type is needed in the
9843 -- initialization procedure.
9844
9845 Set_Ekind (Def_Id, Ekind (T));
9846
9847 if For_Access and then Within_Init_Proc then
9848 null;
9849 else
9850 Append_Elmt (Def_Id, Private_Dependents (T));
9851 end if;
9852 end if;
9853
9854 Set_Etype (Def_Id, T);
9855 Init_Size_Align (Def_Id);
9856 Set_Has_Discriminants (Def_Id, Has_Discrs);
9857 Set_Is_Constrained (Def_Id, Constrained);
9858
9859 Set_First_Entity (Def_Id, First_Entity (T));
9860 Set_Last_Entity (Def_Id, Last_Entity (T));
9861 Set_Has_Implicit_Dereference
9862 (Def_Id, Has_Implicit_Dereference (T));
9863
9864 -- If the subtype is the completion of a private declaration, there may
9865 -- have been representation clauses for the partial view, and they must
9866 -- be preserved. Build_Derived_Type chains the inherited clauses with
9867 -- the ones appearing on the extension. If this comes from a subtype
9868 -- declaration, all clauses are inherited.
9869
9870 if No (First_Rep_Item (Def_Id)) then
9871 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9872 end if;
9873
9874 if Is_Tagged_Type (T) then
9875 Set_Is_Tagged_Type (Def_Id);
9876 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9877 Make_Class_Wide_Type (Def_Id);
9878 end if;
9879
9880 Set_Stored_Constraint (Def_Id, No_Elist);
9881
9882 if Has_Discrs then
9883 Set_Discriminant_Constraint (Def_Id, Elist);
9884 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9885 end if;
9886
9887 if Is_Tagged_Type (T) then
9888
9889 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9890 -- concurrent record type (which has the list of primitive
9891 -- operations).
9892
9893 if Ada_Version >= Ada_2005
9894 and then Is_Concurrent_Type (T)
9895 then
9896 Set_Corresponding_Record_Type (Def_Id,
9897 Corresponding_Record_Type (T));
9898 else
9899 Set_Direct_Primitive_Operations (Def_Id,
9900 Direct_Primitive_Operations (T));
9901 end if;
9902
9903 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9904 end if;
9905
9906 -- Subtypes introduced by component declarations do not need to be
9907 -- marked as delayed, and do not get freeze nodes, because the semantics
9908 -- verifies that the parents of the subtypes are frozen before the
9909 -- enclosing record is frozen.
9910
9911 if not Is_Type (Scope (Def_Id)) then
9912 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9913
9914 if Is_Private_Type (T)
9915 and then Present (Full_View (T))
9916 then
9917 Conditional_Delay (Def_Id, Full_View (T));
9918 else
9919 Conditional_Delay (Def_Id, T);
9920 end if;
9921 end if;
9922
9923 if Is_Record_Type (T) then
9924 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9925
9926 if Has_Discrs
9927 and then not Is_Empty_Elmt_List (Elist)
9928 and then not For_Access
9929 then
9930 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9931 elsif not For_Access then
9932 Set_Cloned_Subtype (Def_Id, T);
9933 end if;
9934 end if;
9935 end Build_Discriminated_Subtype;
9936
9937 ---------------------------
9938 -- Build_Itype_Reference --
9939 ---------------------------
9940
9941 procedure Build_Itype_Reference
9942 (Ityp : Entity_Id;
9943 Nod : Node_Id)
9944 is
9945 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9946 begin
9947
9948 -- Itype references are only created for use by the back-end
9949
9950 if Inside_A_Generic then
9951 return;
9952 else
9953 Set_Itype (IR, Ityp);
9954 Insert_After (Nod, IR);
9955 end if;
9956 end Build_Itype_Reference;
9957
9958 ------------------------
9959 -- Build_Scalar_Bound --
9960 ------------------------
9961
9962 function Build_Scalar_Bound
9963 (Bound : Node_Id;
9964 Par_T : Entity_Id;
9965 Der_T : Entity_Id) return Node_Id
9966 is
9967 New_Bound : Entity_Id;
9968
9969 begin
9970 -- Note: not clear why this is needed, how can the original bound
9971 -- be unanalyzed at this point? and if it is, what business do we
9972 -- have messing around with it? and why is the base type of the
9973 -- parent type the right type for the resolution. It probably is
9974 -- not. It is OK for the new bound we are creating, but not for
9975 -- the old one??? Still if it never happens, no problem.
9976
9977 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9978
9979 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9980 New_Bound := New_Copy (Bound);
9981 Set_Etype (New_Bound, Der_T);
9982 Set_Analyzed (New_Bound);
9983
9984 elsif Is_Entity_Name (Bound) then
9985 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9986
9987 -- The following is almost certainly wrong. What business do we have
9988 -- relocating a node (Bound) that is presumably still attached to
9989 -- the tree elsewhere???
9990
9991 else
9992 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9993 end if;
9994
9995 Set_Etype (New_Bound, Der_T);
9996 return New_Bound;
9997 end Build_Scalar_Bound;
9998
9999 --------------------------------
10000 -- Build_Underlying_Full_View --
10001 --------------------------------
10002
10003 procedure Build_Underlying_Full_View
10004 (N : Node_Id;
10005 Typ : Entity_Id;
10006 Par : Entity_Id)
10007 is
10008 Loc : constant Source_Ptr := Sloc (N);
10009 Subt : constant Entity_Id :=
10010 Make_Defining_Identifier
10011 (Loc, New_External_Name (Chars (Typ), 'S'));
10012
10013 Constr : Node_Id;
10014 Indic : Node_Id;
10015 C : Node_Id;
10016 Id : Node_Id;
10017
10018 procedure Set_Discriminant_Name (Id : Node_Id);
10019 -- If the derived type has discriminants, they may rename discriminants
10020 -- of the parent. When building the full view of the parent, we need to
10021 -- recover the names of the original discriminants if the constraint is
10022 -- given by named associations.
10023
10024 ---------------------------
10025 -- Set_Discriminant_Name --
10026 ---------------------------
10027
10028 procedure Set_Discriminant_Name (Id : Node_Id) is
10029 Disc : Entity_Id;
10030
10031 begin
10032 Set_Original_Discriminant (Id, Empty);
10033
10034 if Has_Discriminants (Typ) then
10035 Disc := First_Discriminant (Typ);
10036 while Present (Disc) loop
10037 if Chars (Disc) = Chars (Id)
10038 and then Present (Corresponding_Discriminant (Disc))
10039 then
10040 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
10041 end if;
10042 Next_Discriminant (Disc);
10043 end loop;
10044 end if;
10045 end Set_Discriminant_Name;
10046
10047 -- Start of processing for Build_Underlying_Full_View
10048
10049 begin
10050 if Nkind (N) = N_Full_Type_Declaration then
10051 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10052
10053 elsif Nkind (N) = N_Subtype_Declaration then
10054 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10055
10056 elsif Nkind (N) = N_Component_Declaration then
10057 Constr :=
10058 New_Copy_Tree
10059 (Constraint (Subtype_Indication (Component_Definition (N))));
10060
10061 else
10062 raise Program_Error;
10063 end if;
10064
10065 C := First (Constraints (Constr));
10066 while Present (C) loop
10067 if Nkind (C) = N_Discriminant_Association then
10068 Id := First (Selector_Names (C));
10069 while Present (Id) loop
10070 Set_Discriminant_Name (Id);
10071 Next (Id);
10072 end loop;
10073 end if;
10074
10075 Next (C);
10076 end loop;
10077
10078 Indic :=
10079 Make_Subtype_Declaration (Loc,
10080 Defining_Identifier => Subt,
10081 Subtype_Indication =>
10082 Make_Subtype_Indication (Loc,
10083 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10084 Constraint => New_Copy_Tree (Constr)));
10085
10086 -- If this is a component subtype for an outer itype, it is not
10087 -- a list member, so simply set the parent link for analysis: if
10088 -- the enclosing type does not need to be in a declarative list,
10089 -- neither do the components.
10090
10091 if Is_List_Member (N)
10092 and then Nkind (N) /= N_Component_Declaration
10093 then
10094 Insert_Before (N, Indic);
10095 else
10096 Set_Parent (Indic, Parent (N));
10097 end if;
10098
10099 Analyze (Indic);
10100 Set_Underlying_Full_View (Typ, Full_View (Subt));
10101 Set_Is_Underlying_Full_View (Full_View (Subt));
10102 end Build_Underlying_Full_View;
10103
10104 -------------------------------
10105 -- Check_Abstract_Overriding --
10106 -------------------------------
10107
10108 procedure Check_Abstract_Overriding (T : Entity_Id) is
10109 Alias_Subp : Entity_Id;
10110 Elmt : Elmt_Id;
10111 Op_List : Elist_Id;
10112 Subp : Entity_Id;
10113 Type_Def : Node_Id;
10114
10115 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10116 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10117 -- which has pragma Implemented already set. Check whether Subp's entity
10118 -- kind conforms to the implementation kind of the overridden routine.
10119
10120 procedure Check_Pragma_Implemented
10121 (Subp : Entity_Id;
10122 Iface_Subp : Entity_Id);
10123 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10124 -- Iface_Subp and both entities have pragma Implemented already set on
10125 -- them. Check whether the two implementation kinds are conforming.
10126
10127 procedure Inherit_Pragma_Implemented
10128 (Subp : Entity_Id;
10129 Iface_Subp : Entity_Id);
10130 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10131 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10132 -- Propagate the implementation kind of Iface_Subp to Subp.
10133
10134 ------------------------------
10135 -- Check_Pragma_Implemented --
10136 ------------------------------
10137
10138 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10139 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10140 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10141 Subp_Alias : constant Entity_Id := Alias (Subp);
10142 Contr_Typ : Entity_Id;
10143 Impl_Subp : Entity_Id;
10144
10145 begin
10146 -- Subp must have an alias since it is a hidden entity used to link
10147 -- an interface subprogram to its overriding counterpart.
10148
10149 pragma Assert (Present (Subp_Alias));
10150
10151 -- Handle aliases to synchronized wrappers
10152
10153 Impl_Subp := Subp_Alias;
10154
10155 if Is_Primitive_Wrapper (Impl_Subp) then
10156 Impl_Subp := Wrapped_Entity (Impl_Subp);
10157 end if;
10158
10159 -- Extract the type of the controlling formal
10160
10161 Contr_Typ := Etype (First_Formal (Subp_Alias));
10162
10163 if Is_Concurrent_Record_Type (Contr_Typ) then
10164 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10165 end if;
10166
10167 -- An interface subprogram whose implementation kind is By_Entry must
10168 -- be implemented by an entry.
10169
10170 if Impl_Kind = Name_By_Entry
10171 and then Ekind (Impl_Subp) /= E_Entry
10172 then
10173 Error_Msg_Node_2 := Iface_Alias;
10174 Error_Msg_NE
10175 ("type & must implement abstract subprogram & with an entry",
10176 Subp_Alias, Contr_Typ);
10177
10178 elsif Impl_Kind = Name_By_Protected_Procedure then
10179
10180 -- An interface subprogram whose implementation kind is By_
10181 -- Protected_Procedure cannot be implemented by a primitive
10182 -- procedure of a task type.
10183
10184 if Ekind (Contr_Typ) /= E_Protected_Type then
10185 Error_Msg_Node_2 := Contr_Typ;
10186 Error_Msg_NE
10187 ("interface subprogram & cannot be implemented by a " &
10188 "primitive procedure of task type &", Subp_Alias,
10189 Iface_Alias);
10190
10191 -- An interface subprogram whose implementation kind is By_
10192 -- Protected_Procedure must be implemented by a procedure.
10193
10194 elsif Ekind (Impl_Subp) /= E_Procedure then
10195 Error_Msg_Node_2 := Iface_Alias;
10196 Error_Msg_NE
10197 ("type & must implement abstract subprogram & with a " &
10198 "procedure", Subp_Alias, Contr_Typ);
10199
10200 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10201 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10202 then
10203 Error_Msg_Name_1 := Impl_Kind;
10204 Error_Msg_N
10205 ("overriding operation& must have synchronization%",
10206 Subp_Alias);
10207 end if;
10208
10209 -- If primitive has Optional synchronization, overriding operation
10210 -- must match if it has an explicit synchronization..
10211
10212 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10213 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10214 then
10215 Error_Msg_Name_1 := Impl_Kind;
10216 Error_Msg_N
10217 ("overriding operation& must have syncrhonization%",
10218 Subp_Alias);
10219 end if;
10220 end Check_Pragma_Implemented;
10221
10222 ------------------------------
10223 -- Check_Pragma_Implemented --
10224 ------------------------------
10225
10226 procedure Check_Pragma_Implemented
10227 (Subp : Entity_Id;
10228 Iface_Subp : Entity_Id)
10229 is
10230 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10231 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10232
10233 begin
10234 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10235 -- and overriding subprogram are different. In general this is an
10236 -- error except when the implementation kind of the overridden
10237 -- subprograms is By_Any or Optional.
10238
10239 if Iface_Kind /= Subp_Kind
10240 and then Iface_Kind /= Name_By_Any
10241 and then Iface_Kind /= Name_Optional
10242 then
10243 if Iface_Kind = Name_By_Entry then
10244 Error_Msg_N
10245 ("incompatible implementation kind, overridden subprogram " &
10246 "is marked By_Entry", Subp);
10247 else
10248 Error_Msg_N
10249 ("incompatible implementation kind, overridden subprogram " &
10250 "is marked By_Protected_Procedure", Subp);
10251 end if;
10252 end if;
10253 end Check_Pragma_Implemented;
10254
10255 --------------------------------
10256 -- Inherit_Pragma_Implemented --
10257 --------------------------------
10258
10259 procedure Inherit_Pragma_Implemented
10260 (Subp : Entity_Id;
10261 Iface_Subp : Entity_Id)
10262 is
10263 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10264 Loc : constant Source_Ptr := Sloc (Subp);
10265 Impl_Prag : Node_Id;
10266
10267 begin
10268 -- Since the implementation kind is stored as a representation item
10269 -- rather than a flag, create a pragma node.
10270
10271 Impl_Prag :=
10272 Make_Pragma (Loc,
10273 Chars => Name_Implemented,
10274 Pragma_Argument_Associations => New_List (
10275 Make_Pragma_Argument_Association (Loc,
10276 Expression => New_Occurrence_Of (Subp, Loc)),
10277
10278 Make_Pragma_Argument_Association (Loc,
10279 Expression => Make_Identifier (Loc, Iface_Kind))));
10280
10281 -- The pragma doesn't need to be analyzed because it is internally
10282 -- built. It is safe to directly register it as a rep item since we
10283 -- are only interested in the characters of the implementation kind.
10284
10285 Record_Rep_Item (Subp, Impl_Prag);
10286 end Inherit_Pragma_Implemented;
10287
10288 -- Start of processing for Check_Abstract_Overriding
10289
10290 begin
10291 Op_List := Primitive_Operations (T);
10292
10293 -- Loop to check primitive operations
10294
10295 Elmt := First_Elmt (Op_List);
10296 while Present (Elmt) loop
10297 Subp := Node (Elmt);
10298 Alias_Subp := Alias (Subp);
10299
10300 -- Inherited subprograms are identified by the fact that they do not
10301 -- come from source, and the associated source location is the
10302 -- location of the first subtype of the derived type.
10303
10304 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10305 -- subprograms that "require overriding".
10306
10307 -- Special exception, do not complain about failure to override the
10308 -- stream routines _Input and _Output, as well as the primitive
10309 -- operations used in dispatching selects since we always provide
10310 -- automatic overridings for these subprograms.
10311
10312 -- The partial view of T may have been a private extension, for
10313 -- which inherited functions dispatching on result are abstract.
10314 -- If the full view is a null extension, there is no need for
10315 -- overriding in Ada 2005, but wrappers need to be built for them
10316 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10317
10318 if Is_Null_Extension (T)
10319 and then Has_Controlling_Result (Subp)
10320 and then Ada_Version >= Ada_2005
10321 and then Present (Alias_Subp)
10322 and then not Comes_From_Source (Subp)
10323 and then not Is_Abstract_Subprogram (Alias_Subp)
10324 and then not Is_Access_Type (Etype (Subp))
10325 then
10326 null;
10327
10328 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10329 -- processing because this check is done with the aliased
10330 -- entity
10331
10332 elsif Present (Interface_Alias (Subp)) then
10333 null;
10334
10335 elsif (Is_Abstract_Subprogram (Subp)
10336 or else Requires_Overriding (Subp)
10337 or else
10338 (Has_Controlling_Result (Subp)
10339 and then Present (Alias_Subp)
10340 and then not Comes_From_Source (Subp)
10341 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10342 and then not Is_TSS (Subp, TSS_Stream_Input)
10343 and then not Is_TSS (Subp, TSS_Stream_Output)
10344 and then not Is_Abstract_Type (T)
10345 and then not Is_Predefined_Interface_Primitive (Subp)
10346
10347 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10348 -- with abstract interface types because the check will be done
10349 -- with the aliased entity (otherwise we generate a duplicated
10350 -- error message).
10351
10352 and then not Present (Interface_Alias (Subp))
10353 then
10354 if Present (Alias_Subp) then
10355
10356 -- Only perform the check for a derived subprogram when the
10357 -- type has an explicit record extension. This avoids incorrect
10358 -- flagging of abstract subprograms for the case of a type
10359 -- without an extension that is derived from a formal type
10360 -- with a tagged actual (can occur within a private part).
10361
10362 -- Ada 2005 (AI-391): In the case of an inherited function with
10363 -- a controlling result of the type, the rule does not apply if
10364 -- the type is a null extension (unless the parent function
10365 -- itself is abstract, in which case the function must still be
10366 -- be overridden). The expander will generate an overriding
10367 -- wrapper function calling the parent subprogram (see
10368 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10369
10370 Type_Def := Type_Definition (Parent (T));
10371
10372 if Nkind (Type_Def) = N_Derived_Type_Definition
10373 and then Present (Record_Extension_Part (Type_Def))
10374 and then
10375 (Ada_Version < Ada_2005
10376 or else not Is_Null_Extension (T)
10377 or else Ekind (Subp) = E_Procedure
10378 or else not Has_Controlling_Result (Subp)
10379 or else Is_Abstract_Subprogram (Alias_Subp)
10380 or else Requires_Overriding (Subp)
10381 or else Is_Access_Type (Etype (Subp)))
10382 then
10383 -- Avoid reporting error in case of abstract predefined
10384 -- primitive inherited from interface type because the
10385 -- body of internally generated predefined primitives
10386 -- of tagged types are generated later by Freeze_Type
10387
10388 if Is_Interface (Root_Type (T))
10389 and then Is_Abstract_Subprogram (Subp)
10390 and then Is_Predefined_Dispatching_Operation (Subp)
10391 and then not Comes_From_Source (Ultimate_Alias (Subp))
10392 then
10393 null;
10394
10395 -- A null extension is not obliged to override an inherited
10396 -- procedure subject to pragma Extensions_Visible with value
10397 -- False and at least one controlling OUT parameter
10398 -- (SPARK RM 6.1.7(6)).
10399
10400 elsif Is_Null_Extension (T)
10401 and then Is_EVF_Procedure (Subp)
10402 then
10403 null;
10404
10405 else
10406 Error_Msg_NE
10407 ("type must be declared abstract or & overridden",
10408 T, Subp);
10409
10410 -- Traverse the whole chain of aliased subprograms to
10411 -- complete the error notification. This is especially
10412 -- useful for traceability of the chain of entities when
10413 -- the subprogram corresponds with an interface
10414 -- subprogram (which may be defined in another package).
10415
10416 if Present (Alias_Subp) then
10417 declare
10418 E : Entity_Id;
10419
10420 begin
10421 E := Subp;
10422 while Present (Alias (E)) loop
10423
10424 -- Avoid reporting redundant errors on entities
10425 -- inherited from interfaces
10426
10427 if Sloc (E) /= Sloc (T) then
10428 Error_Msg_Sloc := Sloc (E);
10429 Error_Msg_NE
10430 ("\& has been inherited #", T, Subp);
10431 end if;
10432
10433 E := Alias (E);
10434 end loop;
10435
10436 Error_Msg_Sloc := Sloc (E);
10437
10438 -- AI05-0068: report if there is an overriding
10439 -- non-abstract subprogram that is invisible.
10440
10441 if Is_Hidden (E)
10442 and then not Is_Abstract_Subprogram (E)
10443 then
10444 Error_Msg_NE
10445 ("\& subprogram# is not visible",
10446 T, Subp);
10447
10448 -- Clarify the case where a non-null extension must
10449 -- override inherited procedure subject to pragma
10450 -- Extensions_Visible with value False and at least
10451 -- one controlling OUT param.
10452
10453 elsif Is_EVF_Procedure (E) then
10454 Error_Msg_NE
10455 ("\& # is subject to Extensions_Visible False",
10456 T, Subp);
10457
10458 else
10459 Error_Msg_NE
10460 ("\& has been inherited from subprogram #",
10461 T, Subp);
10462 end if;
10463 end;
10464 end if;
10465 end if;
10466
10467 -- Ada 2005 (AI-345): Protected or task type implementing
10468 -- abstract interfaces.
10469
10470 elsif Is_Concurrent_Record_Type (T)
10471 and then Present (Interfaces (T))
10472 then
10473 -- There is no need to check here RM 9.4(11.9/3) since we
10474 -- are processing the corresponding record type and the
10475 -- mode of the overriding subprograms was verified by
10476 -- Check_Conformance when the corresponding concurrent
10477 -- type declaration was analyzed.
10478
10479 Error_Msg_NE
10480 ("interface subprogram & must be overridden", T, Subp);
10481
10482 -- Examine primitive operations of synchronized type to find
10483 -- homonyms that have the wrong profile.
10484
10485 declare
10486 Prim : Entity_Id;
10487
10488 begin
10489 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10490 while Present (Prim) loop
10491 if Chars (Prim) = Chars (Subp) then
10492 Error_Msg_NE
10493 ("profile is not type conformant with prefixed "
10494 & "view profile of inherited operation&",
10495 Prim, Subp);
10496 end if;
10497
10498 Next_Entity (Prim);
10499 end loop;
10500 end;
10501 end if;
10502
10503 else
10504 Error_Msg_Node_2 := T;
10505 Error_Msg_N
10506 ("abstract subprogram& not allowed for type&", Subp);
10507
10508 -- Also post unconditional warning on the type (unconditional
10509 -- so that if there are more than one of these cases, we get
10510 -- them all, and not just the first one).
10511
10512 Error_Msg_Node_2 := Subp;
10513 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10514 end if;
10515
10516 -- A subprogram subject to pragma Extensions_Visible with value
10517 -- "True" cannot override a subprogram subject to the same pragma
10518 -- with value "False" (SPARK RM 6.1.7(5)).
10519
10520 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10521 and then Present (Overridden_Operation (Subp))
10522 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10523 Extensions_Visible_False
10524 then
10525 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10526 Error_Msg_N
10527 ("subprogram & with Extensions_Visible True cannot override "
10528 & "subprogram # with Extensions_Visible False", Subp);
10529 end if;
10530
10531 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10532
10533 -- Subp is an expander-generated procedure which maps an interface
10534 -- alias to a protected wrapper. The interface alias is flagged by
10535 -- pragma Implemented. Ensure that Subp is a procedure when the
10536 -- implementation kind is By_Protected_Procedure or an entry when
10537 -- By_Entry.
10538
10539 if Ada_Version >= Ada_2012
10540 and then Is_Hidden (Subp)
10541 and then Present (Interface_Alias (Subp))
10542 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10543 then
10544 Check_Pragma_Implemented (Subp);
10545 end if;
10546
10547 -- Subp is an interface primitive which overrides another interface
10548 -- primitive marked with pragma Implemented.
10549
10550 if Ada_Version >= Ada_2012
10551 and then Present (Overridden_Operation (Subp))
10552 and then Has_Rep_Pragma
10553 (Overridden_Operation (Subp), Name_Implemented)
10554 then
10555 -- If the overriding routine is also marked by Implemented, check
10556 -- that the two implementation kinds are conforming.
10557
10558 if Has_Rep_Pragma (Subp, Name_Implemented) then
10559 Check_Pragma_Implemented
10560 (Subp => Subp,
10561 Iface_Subp => Overridden_Operation (Subp));
10562
10563 -- Otherwise the overriding routine inherits the implementation
10564 -- kind from the overridden subprogram.
10565
10566 else
10567 Inherit_Pragma_Implemented
10568 (Subp => Subp,
10569 Iface_Subp => Overridden_Operation (Subp));
10570 end if;
10571 end if;
10572
10573 -- If the operation is a wrapper for a synchronized primitive, it
10574 -- may be called indirectly through a dispatching select. We assume
10575 -- that it will be referenced elsewhere indirectly, and suppress
10576 -- warnings about an unused entity.
10577
10578 if Is_Primitive_Wrapper (Subp)
10579 and then Present (Wrapped_Entity (Subp))
10580 then
10581 Set_Referenced (Wrapped_Entity (Subp));
10582 end if;
10583
10584 Next_Elmt (Elmt);
10585 end loop;
10586 end Check_Abstract_Overriding;
10587
10588 ------------------------------------------------
10589 -- Check_Access_Discriminant_Requires_Limited --
10590 ------------------------------------------------
10591
10592 procedure Check_Access_Discriminant_Requires_Limited
10593 (D : Node_Id;
10594 Loc : Node_Id)
10595 is
10596 begin
10597 -- A discriminant_specification for an access discriminant shall appear
10598 -- only in the declaration for a task or protected type, or for a type
10599 -- with the reserved word 'limited' in its definition or in one of its
10600 -- ancestors (RM 3.7(10)).
10601
10602 -- AI-0063: The proper condition is that type must be immutably limited,
10603 -- or else be a partial view.
10604
10605 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10606 if Is_Limited_View (Current_Scope)
10607 or else
10608 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10609 and then Limited_Present (Parent (Current_Scope)))
10610 then
10611 null;
10612
10613 else
10614 Error_Msg_N
10615 ("access discriminants allowed only for limited types", Loc);
10616 end if;
10617 end if;
10618 end Check_Access_Discriminant_Requires_Limited;
10619
10620 -----------------------------------
10621 -- Check_Aliased_Component_Types --
10622 -----------------------------------
10623
10624 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10625 C : Entity_Id;
10626
10627 begin
10628 -- ??? Also need to check components of record extensions, but not
10629 -- components of protected types (which are always limited).
10630
10631 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10632 -- types to be unconstrained. This is safe because it is illegal to
10633 -- create access subtypes to such types with explicit discriminant
10634 -- constraints.
10635
10636 if not Is_Limited_Type (T) then
10637 if Ekind (T) = E_Record_Type then
10638 C := First_Component (T);
10639 while Present (C) loop
10640 if Is_Aliased (C)
10641 and then Has_Discriminants (Etype (C))
10642 and then not Is_Constrained (Etype (C))
10643 and then not In_Instance_Body
10644 and then Ada_Version < Ada_2005
10645 then
10646 Error_Msg_N
10647 ("aliased component must be constrained (RM 3.6(11))",
10648 C);
10649 end if;
10650
10651 Next_Component (C);
10652 end loop;
10653
10654 elsif Ekind (T) = E_Array_Type then
10655 if Has_Aliased_Components (T)
10656 and then Has_Discriminants (Component_Type (T))
10657 and then not Is_Constrained (Component_Type (T))
10658 and then not In_Instance_Body
10659 and then Ada_Version < Ada_2005
10660 then
10661 Error_Msg_N
10662 ("aliased component type must be constrained (RM 3.6(11))",
10663 T);
10664 end if;
10665 end if;
10666 end if;
10667 end Check_Aliased_Component_Types;
10668
10669 ---------------------------------------
10670 -- Check_Anonymous_Access_Components --
10671 ---------------------------------------
10672
10673 procedure Check_Anonymous_Access_Components
10674 (Typ_Decl : Node_Id;
10675 Typ : Entity_Id;
10676 Prev : Entity_Id;
10677 Comp_List : Node_Id)
10678 is
10679 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10680 Anon_Access : Entity_Id;
10681 Acc_Def : Node_Id;
10682 Comp : Node_Id;
10683 Comp_Def : Node_Id;
10684 Decl : Node_Id;
10685 Type_Def : Node_Id;
10686
10687 procedure Build_Incomplete_Type_Declaration;
10688 -- If the record type contains components that include an access to the
10689 -- current record, then create an incomplete type declaration for the
10690 -- record, to be used as the designated type of the anonymous access.
10691 -- This is done only once, and only if there is no previous partial
10692 -- view of the type.
10693
10694 function Designates_T (Subt : Node_Id) return Boolean;
10695 -- Check whether a node designates the enclosing record type, or 'Class
10696 -- of that type
10697
10698 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10699 -- Check whether an access definition includes a reference to
10700 -- the enclosing record type. The reference can be a subtype mark
10701 -- in the access definition itself, a 'Class attribute reference, or
10702 -- recursively a reference appearing in a parameter specification
10703 -- or result definition of an access_to_subprogram definition.
10704
10705 --------------------------------------
10706 -- Build_Incomplete_Type_Declaration --
10707 --------------------------------------
10708
10709 procedure Build_Incomplete_Type_Declaration is
10710 Decl : Node_Id;
10711 Inc_T : Entity_Id;
10712 H : Entity_Id;
10713
10714 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10715 -- it's "is new ... with record" or else "is tagged record ...".
10716
10717 Is_Tagged : constant Boolean :=
10718 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10719 and then
10720 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10721 or else
10722 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10723 and then Tagged_Present (Type_Definition (Typ_Decl)));
10724
10725 begin
10726 -- If there is a previous partial view, no need to create a new one
10727 -- If the partial view, given by Prev, is incomplete, If Prev is
10728 -- a private declaration, full declaration is flagged accordingly.
10729
10730 if Prev /= Typ then
10731 if Is_Tagged then
10732 Make_Class_Wide_Type (Prev);
10733 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10734 Set_Etype (Class_Wide_Type (Typ), Typ);
10735 end if;
10736
10737 return;
10738
10739 elsif Has_Private_Declaration (Typ) then
10740
10741 -- If we refer to T'Class inside T, and T is the completion of a
10742 -- private type, then make sure the class-wide type exists.
10743
10744 if Is_Tagged then
10745 Make_Class_Wide_Type (Typ);
10746 end if;
10747
10748 return;
10749
10750 -- If there was a previous anonymous access type, the incomplete
10751 -- type declaration will have been created already.
10752
10753 elsif Present (Current_Entity (Typ))
10754 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10755 and then Full_View (Current_Entity (Typ)) = Typ
10756 then
10757 if Is_Tagged
10758 and then Comes_From_Source (Current_Entity (Typ))
10759 and then not Is_Tagged_Type (Current_Entity (Typ))
10760 then
10761 Make_Class_Wide_Type (Typ);
10762 Error_Msg_N
10763 ("incomplete view of tagged type should be declared tagged??",
10764 Parent (Current_Entity (Typ)));
10765 end if;
10766 return;
10767
10768 else
10769 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10770 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10771
10772 -- Type has already been inserted into the current scope. Remove
10773 -- it, and add incomplete declaration for type, so that subsequent
10774 -- anonymous access types can use it. The entity is unchained from
10775 -- the homonym list and from immediate visibility. After analysis,
10776 -- the entity in the incomplete declaration becomes immediately
10777 -- visible in the record declaration that follows.
10778
10779 H := Current_Entity (Typ);
10780
10781 if H = Typ then
10782 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10783 else
10784 while Present (H)
10785 and then Homonym (H) /= Typ
10786 loop
10787 H := Homonym (Typ);
10788 end loop;
10789
10790 Set_Homonym (H, Homonym (Typ));
10791 end if;
10792
10793 Insert_Before (Typ_Decl, Decl);
10794 Analyze (Decl);
10795 Set_Full_View (Inc_T, Typ);
10796
10797 if Is_Tagged then
10798
10799 -- Create a common class-wide type for both views, and set the
10800 -- Etype of the class-wide type to the full view.
10801
10802 Make_Class_Wide_Type (Inc_T);
10803 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10804 Set_Etype (Class_Wide_Type (Typ), Typ);
10805 end if;
10806 end if;
10807 end Build_Incomplete_Type_Declaration;
10808
10809 ------------------
10810 -- Designates_T --
10811 ------------------
10812
10813 function Designates_T (Subt : Node_Id) return Boolean is
10814 Type_Id : constant Name_Id := Chars (Typ);
10815
10816 function Names_T (Nam : Node_Id) return Boolean;
10817 -- The record type has not been introduced in the current scope
10818 -- yet, so we must examine the name of the type itself, either
10819 -- an identifier T, or an expanded name of the form P.T, where
10820 -- P denotes the current scope.
10821
10822 -------------
10823 -- Names_T --
10824 -------------
10825
10826 function Names_T (Nam : Node_Id) return Boolean is
10827 begin
10828 if Nkind (Nam) = N_Identifier then
10829 return Chars (Nam) = Type_Id;
10830
10831 elsif Nkind (Nam) = N_Selected_Component then
10832 if Chars (Selector_Name (Nam)) = Type_Id then
10833 if Nkind (Prefix (Nam)) = N_Identifier then
10834 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10835
10836 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10837 return Chars (Selector_Name (Prefix (Nam))) =
10838 Chars (Current_Scope);
10839 else
10840 return False;
10841 end if;
10842
10843 else
10844 return False;
10845 end if;
10846
10847 else
10848 return False;
10849 end if;
10850 end Names_T;
10851
10852 -- Start of processing for Designates_T
10853
10854 begin
10855 if Nkind (Subt) = N_Identifier then
10856 return Chars (Subt) = Type_Id;
10857
10858 -- Reference can be through an expanded name which has not been
10859 -- analyzed yet, and which designates enclosing scopes.
10860
10861 elsif Nkind (Subt) = N_Selected_Component then
10862 if Names_T (Subt) then
10863 return True;
10864
10865 -- Otherwise it must denote an entity that is already visible.
10866 -- The access definition may name a subtype of the enclosing
10867 -- type, if there is a previous incomplete declaration for it.
10868
10869 else
10870 Find_Selected_Component (Subt);
10871 return
10872 Is_Entity_Name (Subt)
10873 and then Scope (Entity (Subt)) = Current_Scope
10874 and then
10875 (Chars (Base_Type (Entity (Subt))) = Type_Id
10876 or else
10877 (Is_Class_Wide_Type (Entity (Subt))
10878 and then
10879 Chars (Etype (Base_Type (Entity (Subt)))) =
10880 Type_Id));
10881 end if;
10882
10883 -- A reference to the current type may appear as the prefix of
10884 -- a 'Class attribute.
10885
10886 elsif Nkind (Subt) = N_Attribute_Reference
10887 and then Attribute_Name (Subt) = Name_Class
10888 then
10889 return Names_T (Prefix (Subt));
10890
10891 else
10892 return False;
10893 end if;
10894 end Designates_T;
10895
10896 ----------------
10897 -- Mentions_T --
10898 ----------------
10899
10900 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10901 Param_Spec : Node_Id;
10902
10903 Acc_Subprg : constant Node_Id :=
10904 Access_To_Subprogram_Definition (Acc_Def);
10905
10906 begin
10907 if No (Acc_Subprg) then
10908 return Designates_T (Subtype_Mark (Acc_Def));
10909 end if;
10910
10911 -- Component is an access_to_subprogram: examine its formals,
10912 -- and result definition in the case of an access_to_function.
10913
10914 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10915 while Present (Param_Spec) loop
10916 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10917 and then Mentions_T (Parameter_Type (Param_Spec))
10918 then
10919 return True;
10920
10921 elsif Designates_T (Parameter_Type (Param_Spec)) then
10922 return True;
10923 end if;
10924
10925 Next (Param_Spec);
10926 end loop;
10927
10928 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10929 if Nkind (Result_Definition (Acc_Subprg)) =
10930 N_Access_Definition
10931 then
10932 return Mentions_T (Result_Definition (Acc_Subprg));
10933 else
10934 return Designates_T (Result_Definition (Acc_Subprg));
10935 end if;
10936 end if;
10937
10938 return False;
10939 end Mentions_T;
10940
10941 -- Start of processing for Check_Anonymous_Access_Components
10942
10943 begin
10944 if No (Comp_List) then
10945 return;
10946 end if;
10947
10948 Comp := First (Component_Items (Comp_List));
10949 while Present (Comp) loop
10950 if Nkind (Comp) = N_Component_Declaration
10951 and then Present
10952 (Access_Definition (Component_Definition (Comp)))
10953 and then
10954 Mentions_T (Access_Definition (Component_Definition (Comp)))
10955 then
10956 Comp_Def := Component_Definition (Comp);
10957 Acc_Def :=
10958 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10959
10960 Build_Incomplete_Type_Declaration;
10961 Anon_Access := Make_Temporary (Loc, 'S');
10962
10963 -- Create a declaration for the anonymous access type: either
10964 -- an access_to_object or an access_to_subprogram.
10965
10966 if Present (Acc_Def) then
10967 if Nkind (Acc_Def) = N_Access_Function_Definition then
10968 Type_Def :=
10969 Make_Access_Function_Definition (Loc,
10970 Parameter_Specifications =>
10971 Parameter_Specifications (Acc_Def),
10972 Result_Definition => Result_Definition (Acc_Def));
10973 else
10974 Type_Def :=
10975 Make_Access_Procedure_Definition (Loc,
10976 Parameter_Specifications =>
10977 Parameter_Specifications (Acc_Def));
10978 end if;
10979
10980 else
10981 Type_Def :=
10982 Make_Access_To_Object_Definition (Loc,
10983 Subtype_Indication =>
10984 Relocate_Node
10985 (Subtype_Mark (Access_Definition (Comp_Def))));
10986
10987 Set_Constant_Present
10988 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10989 Set_All_Present
10990 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10991 end if;
10992
10993 Set_Null_Exclusion_Present
10994 (Type_Def,
10995 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10996
10997 Decl :=
10998 Make_Full_Type_Declaration (Loc,
10999 Defining_Identifier => Anon_Access,
11000 Type_Definition => Type_Def);
11001
11002 Insert_Before (Typ_Decl, Decl);
11003 Analyze (Decl);
11004
11005 -- If an access to subprogram, create the extra formals
11006
11007 if Present (Acc_Def) then
11008 Create_Extra_Formals (Designated_Type (Anon_Access));
11009
11010 -- If an access to object, preserve entity of designated type,
11011 -- for ASIS use, before rewriting the component definition.
11012
11013 else
11014 declare
11015 Desig : Entity_Id;
11016
11017 begin
11018 Desig := Entity (Subtype_Indication (Type_Def));
11019
11020 -- If the access definition is to the current record,
11021 -- the visible entity at this point is an incomplete
11022 -- type. Retrieve the full view to simplify ASIS queries
11023
11024 if Ekind (Desig) = E_Incomplete_Type then
11025 Desig := Full_View (Desig);
11026 end if;
11027
11028 Set_Entity
11029 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11030 end;
11031 end if;
11032
11033 Rewrite (Comp_Def,
11034 Make_Component_Definition (Loc,
11035 Subtype_Indication =>
11036 New_Occurrence_Of (Anon_Access, Loc)));
11037
11038 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11039 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11040 else
11041 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11042 end if;
11043
11044 Set_Is_Local_Anonymous_Access (Anon_Access);
11045 end if;
11046
11047 Next (Comp);
11048 end loop;
11049
11050 if Present (Variant_Part (Comp_List)) then
11051 declare
11052 V : Node_Id;
11053 begin
11054 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11055 while Present (V) loop
11056 Check_Anonymous_Access_Components
11057 (Typ_Decl, Typ, Prev, Component_List (V));
11058 Next_Non_Pragma (V);
11059 end loop;
11060 end;
11061 end if;
11062 end Check_Anonymous_Access_Components;
11063
11064 ----------------------
11065 -- Check_Completion --
11066 ----------------------
11067
11068 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11069 E : Entity_Id;
11070
11071 procedure Post_Error;
11072 -- Post error message for lack of completion for entity E
11073
11074 ----------------
11075 -- Post_Error --
11076 ----------------
11077
11078 procedure Post_Error is
11079 procedure Missing_Body;
11080 -- Output missing body message
11081
11082 ------------------
11083 -- Missing_Body --
11084 ------------------
11085
11086 procedure Missing_Body is
11087 begin
11088 -- Spec is in same unit, so we can post on spec
11089
11090 if In_Same_Source_Unit (Body_Id, E) then
11091 Error_Msg_N ("missing body for &", E);
11092
11093 -- Spec is in a separate unit, so we have to post on the body
11094
11095 else
11096 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11097 end if;
11098 end Missing_Body;
11099
11100 -- Start of processing for Post_Error
11101
11102 begin
11103 if not Comes_From_Source (E) then
11104 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11105
11106 -- It may be an anonymous protected type created for a
11107 -- single variable. Post error on variable, if present.
11108
11109 declare
11110 Var : Entity_Id;
11111
11112 begin
11113 Var := First_Entity (Current_Scope);
11114 while Present (Var) loop
11115 exit when Etype (Var) = E
11116 and then Comes_From_Source (Var);
11117
11118 Next_Entity (Var);
11119 end loop;
11120
11121 if Present (Var) then
11122 E := Var;
11123 end if;
11124 end;
11125 end if;
11126 end if;
11127
11128 -- If a generated entity has no completion, then either previous
11129 -- semantic errors have disabled the expansion phase, or else we had
11130 -- missing subunits, or else we are compiling without expansion,
11131 -- or else something is very wrong.
11132
11133 if not Comes_From_Source (E) then
11134 pragma Assert
11135 (Serious_Errors_Detected > 0
11136 or else Configurable_Run_Time_Violations > 0
11137 or else Subunits_Missing
11138 or else not Expander_Active);
11139 return;
11140
11141 -- Here for source entity
11142
11143 else
11144 -- Here if no body to post the error message, so we post the error
11145 -- on the declaration that has no completion. This is not really
11146 -- the right place to post it, think about this later ???
11147
11148 if No (Body_Id) then
11149 if Is_Type (E) then
11150 Error_Msg_NE
11151 ("missing full declaration for }", Parent (E), E);
11152 else
11153 Error_Msg_NE ("missing body for &", Parent (E), E);
11154 end if;
11155
11156 -- Package body has no completion for a declaration that appears
11157 -- in the corresponding spec. Post error on the body, with a
11158 -- reference to the non-completed declaration.
11159
11160 else
11161 Error_Msg_Sloc := Sloc (E);
11162
11163 if Is_Type (E) then
11164 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11165
11166 elsif Is_Overloadable (E)
11167 and then Current_Entity_In_Scope (E) /= E
11168 then
11169 -- It may be that the completion is mistyped and appears as
11170 -- a distinct overloading of the entity.
11171
11172 declare
11173 Candidate : constant Entity_Id :=
11174 Current_Entity_In_Scope (E);
11175 Decl : constant Node_Id :=
11176 Unit_Declaration_Node (Candidate);
11177
11178 begin
11179 if Is_Overloadable (Candidate)
11180 and then Ekind (Candidate) = Ekind (E)
11181 and then Nkind (Decl) = N_Subprogram_Body
11182 and then Acts_As_Spec (Decl)
11183 then
11184 Check_Type_Conformant (Candidate, E);
11185
11186 else
11187 Missing_Body;
11188 end if;
11189 end;
11190
11191 else
11192 Missing_Body;
11193 end if;
11194 end if;
11195 end if;
11196 end Post_Error;
11197
11198 -- Local variables
11199
11200 Pack_Id : constant Entity_Id := Current_Scope;
11201
11202 -- Start of processing for Check_Completion
11203
11204 begin
11205 E := First_Entity (Pack_Id);
11206 while Present (E) loop
11207 if Is_Intrinsic_Subprogram (E) then
11208 null;
11209
11210 -- The following situation requires special handling: a child unit
11211 -- that appears in the context clause of the body of its parent:
11212
11213 -- procedure Parent.Child (...);
11214
11215 -- with Parent.Child;
11216 -- package body Parent is
11217
11218 -- Here Parent.Child appears as a local entity, but should not be
11219 -- flagged as requiring completion, because it is a compilation
11220 -- unit.
11221
11222 -- Ignore missing completion for a subprogram that does not come from
11223 -- source (including the _Call primitive operation of RAS types,
11224 -- which has to have the flag Comes_From_Source for other purposes):
11225 -- we assume that the expander will provide the missing completion.
11226 -- In case of previous errors, other expansion actions that provide
11227 -- bodies for null procedures with not be invoked, so inhibit message
11228 -- in those cases.
11229
11230 -- Note that E_Operator is not in the list that follows, because
11231 -- this kind is reserved for predefined operators, that are
11232 -- intrinsic and do not need completion.
11233
11234 elsif Ekind_In (E, E_Function,
11235 E_Procedure,
11236 E_Generic_Function,
11237 E_Generic_Procedure)
11238 then
11239 if Has_Completion (E) then
11240 null;
11241
11242 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11243 null;
11244
11245 elsif Is_Subprogram (E)
11246 and then (not Comes_From_Source (E)
11247 or else Chars (E) = Name_uCall)
11248 then
11249 null;
11250
11251 elsif
11252 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11253 then
11254 null;
11255
11256 elsif Nkind (Parent (E)) = N_Procedure_Specification
11257 and then Null_Present (Parent (E))
11258 and then Serious_Errors_Detected > 0
11259 then
11260 null;
11261
11262 else
11263 Post_Error;
11264 end if;
11265
11266 elsif Is_Entry (E) then
11267 if not Has_Completion (E) and then
11268 (Ekind (Scope (E)) = E_Protected_Object
11269 or else Ekind (Scope (E)) = E_Protected_Type)
11270 then
11271 Post_Error;
11272 end if;
11273
11274 elsif Is_Package_Or_Generic_Package (E) then
11275 if Unit_Requires_Body (E) then
11276 if not Has_Completion (E)
11277 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11278 N_Compilation_Unit
11279 then
11280 Post_Error;
11281 end if;
11282
11283 elsif not Is_Child_Unit (E) then
11284 May_Need_Implicit_Body (E);
11285 end if;
11286
11287 -- A formal incomplete type (Ada 2012) does not require a completion;
11288 -- other incomplete type declarations do.
11289
11290 elsif Ekind (E) = E_Incomplete_Type
11291 and then No (Underlying_Type (E))
11292 and then not Is_Generic_Type (E)
11293 then
11294 Post_Error;
11295
11296 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11297 and then not Has_Completion (E)
11298 then
11299 Post_Error;
11300
11301 -- A single task declared in the current scope is a constant, verify
11302 -- that the body of its anonymous type is in the same scope. If the
11303 -- task is defined elsewhere, this may be a renaming declaration for
11304 -- which no completion is needed.
11305
11306 elsif Ekind (E) = E_Constant
11307 and then Ekind (Etype (E)) = E_Task_Type
11308 and then not Has_Completion (Etype (E))
11309 and then Scope (Etype (E)) = Current_Scope
11310 then
11311 Post_Error;
11312
11313 elsif Ekind (E) = E_Protected_Object
11314 and then not Has_Completion (Etype (E))
11315 then
11316 Post_Error;
11317
11318 elsif Ekind (E) = E_Record_Type then
11319 if Is_Tagged_Type (E) then
11320 Check_Abstract_Overriding (E);
11321 Check_Conventions (E);
11322 end if;
11323
11324 Check_Aliased_Component_Types (E);
11325
11326 elsif Ekind (E) = E_Array_Type then
11327 Check_Aliased_Component_Types (E);
11328
11329 end if;
11330
11331 Next_Entity (E);
11332 end loop;
11333 end Check_Completion;
11334
11335 ------------------------------------
11336 -- Check_CPP_Type_Has_No_Defaults --
11337 ------------------------------------
11338
11339 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11340 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11341 Clist : Node_Id;
11342 Comp : Node_Id;
11343
11344 begin
11345 -- Obtain the component list
11346
11347 if Nkind (Tdef) = N_Record_Definition then
11348 Clist := Component_List (Tdef);
11349 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11350 Clist := Component_List (Record_Extension_Part (Tdef));
11351 end if;
11352
11353 -- Check all components to ensure no default expressions
11354
11355 if Present (Clist) then
11356 Comp := First (Component_Items (Clist));
11357 while Present (Comp) loop
11358 if Present (Expression (Comp)) then
11359 Error_Msg_N
11360 ("component of imported 'C'P'P type cannot have "
11361 & "default expression", Expression (Comp));
11362 end if;
11363
11364 Next (Comp);
11365 end loop;
11366 end if;
11367 end Check_CPP_Type_Has_No_Defaults;
11368
11369 ----------------------------
11370 -- Check_Delta_Expression --
11371 ----------------------------
11372
11373 procedure Check_Delta_Expression (E : Node_Id) is
11374 begin
11375 if not (Is_Real_Type (Etype (E))) then
11376 Wrong_Type (E, Any_Real);
11377
11378 elsif not Is_OK_Static_Expression (E) then
11379 Flag_Non_Static_Expr
11380 ("non-static expression used for delta value!", E);
11381
11382 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11383 Error_Msg_N ("delta expression must be positive", E);
11384
11385 else
11386 return;
11387 end if;
11388
11389 -- If any of above errors occurred, then replace the incorrect
11390 -- expression by the real 0.1, which should prevent further errors.
11391
11392 Rewrite (E,
11393 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11394 Analyze_And_Resolve (E, Standard_Float);
11395 end Check_Delta_Expression;
11396
11397 -----------------------------
11398 -- Check_Digits_Expression --
11399 -----------------------------
11400
11401 procedure Check_Digits_Expression (E : Node_Id) is
11402 begin
11403 if not (Is_Integer_Type (Etype (E))) then
11404 Wrong_Type (E, Any_Integer);
11405
11406 elsif not Is_OK_Static_Expression (E) then
11407 Flag_Non_Static_Expr
11408 ("non-static expression used for digits value!", E);
11409
11410 elsif Expr_Value (E) <= 0 then
11411 Error_Msg_N ("digits value must be greater than zero", E);
11412
11413 else
11414 return;
11415 end if;
11416
11417 -- If any of above errors occurred, then replace the incorrect
11418 -- expression by the integer 1, which should prevent further errors.
11419
11420 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11421 Analyze_And_Resolve (E, Standard_Integer);
11422
11423 end Check_Digits_Expression;
11424
11425 --------------------------
11426 -- Check_Initialization --
11427 --------------------------
11428
11429 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11430 begin
11431 -- Special processing for limited types
11432
11433 if Is_Limited_Type (T)
11434 and then not In_Instance
11435 and then not In_Inlined_Body
11436 then
11437 if not OK_For_Limited_Init (T, Exp) then
11438
11439 -- In GNAT mode, this is just a warning, to allow it to be evilly
11440 -- turned off. Otherwise it is a real error.
11441
11442 if GNAT_Mode then
11443 Error_Msg_N
11444 ("??cannot initialize entities of limited type!", Exp);
11445
11446 elsif Ada_Version < Ada_2005 then
11447
11448 -- The side effect removal machinery may generate illegal Ada
11449 -- code to avoid the usage of access types and 'reference in
11450 -- SPARK mode. Since this is legal code with respect to theorem
11451 -- proving, do not emit the error.
11452
11453 if GNATprove_Mode
11454 and then Nkind (Exp) = N_Function_Call
11455 and then Nkind (Parent (Exp)) = N_Object_Declaration
11456 and then not Comes_From_Source
11457 (Defining_Identifier (Parent (Exp)))
11458 then
11459 null;
11460
11461 else
11462 Error_Msg_N
11463 ("cannot initialize entities of limited type", Exp);
11464 Explain_Limited_Type (T, Exp);
11465 end if;
11466
11467 else
11468 -- Specialize error message according to kind of illegal
11469 -- initial expression.
11470
11471 if Nkind (Exp) = N_Type_Conversion
11472 and then Nkind (Expression (Exp)) = N_Function_Call
11473 then
11474 Error_Msg_N
11475 ("illegal context for call"
11476 & " to function with limited result", Exp);
11477
11478 else
11479 Error_Msg_N
11480 ("initialization of limited object requires aggregate "
11481 & "or function call", Exp);
11482 end if;
11483 end if;
11484 end if;
11485 end if;
11486
11487 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11488 -- set unless we can be sure that no range check is required.
11489
11490 if (GNATprove_Mode or not Expander_Active)
11491 and then Is_Scalar_Type (T)
11492 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11493 then
11494 Set_Do_Range_Check (Exp);
11495 end if;
11496 end Check_Initialization;
11497
11498 ----------------------
11499 -- Check_Interfaces --
11500 ----------------------
11501
11502 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11503 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11504
11505 Iface : Node_Id;
11506 Iface_Def : Node_Id;
11507 Iface_Typ : Entity_Id;
11508 Parent_Node : Node_Id;
11509
11510 Is_Task : Boolean := False;
11511 -- Set True if parent type or any progenitor is a task interface
11512
11513 Is_Protected : Boolean := False;
11514 -- Set True if parent type or any progenitor is a protected interface
11515
11516 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11517 -- Check that a progenitor is compatible with declaration. If an error
11518 -- message is output, it is posted on Error_Node.
11519
11520 ------------------
11521 -- Check_Ifaces --
11522 ------------------
11523
11524 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11525 Iface_Id : constant Entity_Id :=
11526 Defining_Identifier (Parent (Iface_Def));
11527 Type_Def : Node_Id;
11528
11529 begin
11530 if Nkind (N) = N_Private_Extension_Declaration then
11531 Type_Def := N;
11532 else
11533 Type_Def := Type_Definition (N);
11534 end if;
11535
11536 if Is_Task_Interface (Iface_Id) then
11537 Is_Task := True;
11538
11539 elsif Is_Protected_Interface (Iface_Id) then
11540 Is_Protected := True;
11541 end if;
11542
11543 if Is_Synchronized_Interface (Iface_Id) then
11544
11545 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11546 -- extension derived from a synchronized interface must explicitly
11547 -- be declared synchronized, because the full view will be a
11548 -- synchronized type.
11549
11550 if Nkind (N) = N_Private_Extension_Declaration then
11551 if not Synchronized_Present (N) then
11552 Error_Msg_NE
11553 ("private extension of& must be explicitly synchronized",
11554 N, Iface_Id);
11555 end if;
11556
11557 -- However, by 3.9.4(16/2), a full type that is a record extension
11558 -- is never allowed to derive from a synchronized interface (note
11559 -- that interfaces must be excluded from this check, because those
11560 -- are represented by derived type definitions in some cases).
11561
11562 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11563 and then not Interface_Present (Type_Definition (N))
11564 then
11565 Error_Msg_N ("record extension cannot derive from synchronized "
11566 & "interface", Error_Node);
11567 end if;
11568 end if;
11569
11570 -- Check that the characteristics of the progenitor are compatible
11571 -- with the explicit qualifier in the declaration.
11572 -- The check only applies to qualifiers that come from source.
11573 -- Limited_Present also appears in the declaration of corresponding
11574 -- records, and the check does not apply to them.
11575
11576 if Limited_Present (Type_Def)
11577 and then not
11578 Is_Concurrent_Record_Type (Defining_Identifier (N))
11579 then
11580 if Is_Limited_Interface (Parent_Type)
11581 and then not Is_Limited_Interface (Iface_Id)
11582 then
11583 Error_Msg_NE
11584 ("progenitor & must be limited interface",
11585 Error_Node, Iface_Id);
11586
11587 elsif
11588 (Task_Present (Iface_Def)
11589 or else Protected_Present (Iface_Def)
11590 or else Synchronized_Present (Iface_Def))
11591 and then Nkind (N) /= N_Private_Extension_Declaration
11592 and then not Error_Posted (N)
11593 then
11594 Error_Msg_NE
11595 ("progenitor & must be limited interface",
11596 Error_Node, Iface_Id);
11597 end if;
11598
11599 -- Protected interfaces can only inherit from limited, synchronized
11600 -- or protected interfaces.
11601
11602 elsif Nkind (N) = N_Full_Type_Declaration
11603 and then Protected_Present (Type_Def)
11604 then
11605 if Limited_Present (Iface_Def)
11606 or else Synchronized_Present (Iface_Def)
11607 or else Protected_Present (Iface_Def)
11608 then
11609 null;
11610
11611 elsif Task_Present (Iface_Def) then
11612 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11613 & "from task interface", Error_Node);
11614
11615 else
11616 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11617 & "from non-limited interface", Error_Node);
11618 end if;
11619
11620 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11621 -- limited and synchronized.
11622
11623 elsif Synchronized_Present (Type_Def) then
11624 if Limited_Present (Iface_Def)
11625 or else Synchronized_Present (Iface_Def)
11626 then
11627 null;
11628
11629 elsif Protected_Present (Iface_Def)
11630 and then Nkind (N) /= N_Private_Extension_Declaration
11631 then
11632 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11633 & "from protected interface", Error_Node);
11634
11635 elsif Task_Present (Iface_Def)
11636 and then Nkind (N) /= N_Private_Extension_Declaration
11637 then
11638 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11639 & "from task interface", Error_Node);
11640
11641 elsif not Is_Limited_Interface (Iface_Id) then
11642 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11643 & "from non-limited interface", Error_Node);
11644 end if;
11645
11646 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11647 -- synchronized or task interfaces.
11648
11649 elsif Nkind (N) = N_Full_Type_Declaration
11650 and then Task_Present (Type_Def)
11651 then
11652 if Limited_Present (Iface_Def)
11653 or else Synchronized_Present (Iface_Def)
11654 or else Task_Present (Iface_Def)
11655 then
11656 null;
11657
11658 elsif Protected_Present (Iface_Def) then
11659 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11660 & "protected interface", Error_Node);
11661
11662 else
11663 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11664 & "non-limited interface", Error_Node);
11665 end if;
11666 end if;
11667 end Check_Ifaces;
11668
11669 -- Start of processing for Check_Interfaces
11670
11671 begin
11672 if Is_Interface (Parent_Type) then
11673 if Is_Task_Interface (Parent_Type) then
11674 Is_Task := True;
11675
11676 elsif Is_Protected_Interface (Parent_Type) then
11677 Is_Protected := True;
11678 end if;
11679 end if;
11680
11681 if Nkind (N) = N_Private_Extension_Declaration then
11682
11683 -- Check that progenitors are compatible with declaration
11684
11685 Iface := First (Interface_List (Def));
11686 while Present (Iface) loop
11687 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11688
11689 Parent_Node := Parent (Base_Type (Iface_Typ));
11690 Iface_Def := Type_Definition (Parent_Node);
11691
11692 if not Is_Interface (Iface_Typ) then
11693 Diagnose_Interface (Iface, Iface_Typ);
11694 else
11695 Check_Ifaces (Iface_Def, Iface);
11696 end if;
11697
11698 Next (Iface);
11699 end loop;
11700
11701 if Is_Task and Is_Protected then
11702 Error_Msg_N
11703 ("type cannot derive from task and protected interface", N);
11704 end if;
11705
11706 return;
11707 end if;
11708
11709 -- Full type declaration of derived type.
11710 -- Check compatibility with parent if it is interface type
11711
11712 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11713 and then Is_Interface (Parent_Type)
11714 then
11715 Parent_Node := Parent (Parent_Type);
11716
11717 -- More detailed checks for interface varieties
11718
11719 Check_Ifaces
11720 (Iface_Def => Type_Definition (Parent_Node),
11721 Error_Node => Subtype_Indication (Type_Definition (N)));
11722 end if;
11723
11724 Iface := First (Interface_List (Def));
11725 while Present (Iface) loop
11726 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11727
11728 Parent_Node := Parent (Base_Type (Iface_Typ));
11729 Iface_Def := Type_Definition (Parent_Node);
11730
11731 if not Is_Interface (Iface_Typ) then
11732 Diagnose_Interface (Iface, Iface_Typ);
11733
11734 else
11735 -- "The declaration of a specific descendant of an interface
11736 -- type freezes the interface type" RM 13.14
11737
11738 Freeze_Before (N, Iface_Typ);
11739 Check_Ifaces (Iface_Def, Error_Node => Iface);
11740 end if;
11741
11742 Next (Iface);
11743 end loop;
11744
11745 if Is_Task and Is_Protected then
11746 Error_Msg_N
11747 ("type cannot derive from task and protected interface", N);
11748 end if;
11749 end Check_Interfaces;
11750
11751 ------------------------------------
11752 -- Check_Or_Process_Discriminants --
11753 ------------------------------------
11754
11755 -- If an incomplete or private type declaration was already given for the
11756 -- type, the discriminants may have already been processed if they were
11757 -- present on the incomplete declaration. In this case a full conformance
11758 -- check has been performed in Find_Type_Name, and we then recheck here
11759 -- some properties that can't be checked on the partial view alone.
11760 -- Otherwise we call Process_Discriminants.
11761
11762 procedure Check_Or_Process_Discriminants
11763 (N : Node_Id;
11764 T : Entity_Id;
11765 Prev : Entity_Id := Empty)
11766 is
11767 begin
11768 if Has_Discriminants (T) then
11769
11770 -- Discriminants are already set on T if they were already present
11771 -- on the partial view. Make them visible to component declarations.
11772
11773 declare
11774 D : Entity_Id;
11775 -- Discriminant on T (full view) referencing expr on partial view
11776
11777 Prev_D : Entity_Id;
11778 -- Entity of corresponding discriminant on partial view
11779
11780 New_D : Node_Id;
11781 -- Discriminant specification for full view, expression is
11782 -- the syntactic copy on full view (which has been checked for
11783 -- conformance with partial view), only used here to post error
11784 -- message.
11785
11786 begin
11787 D := First_Discriminant (T);
11788 New_D := First (Discriminant_Specifications (N));
11789 while Present (D) loop
11790 Prev_D := Current_Entity (D);
11791 Set_Current_Entity (D);
11792 Set_Is_Immediately_Visible (D);
11793 Set_Homonym (D, Prev_D);
11794
11795 -- Handle the case where there is an untagged partial view and
11796 -- the full view is tagged: must disallow discriminants with
11797 -- defaults, unless compiling for Ada 2012, which allows a
11798 -- limited tagged type to have defaulted discriminants (see
11799 -- AI05-0214). However, suppress error here if it was already
11800 -- reported on the default expression of the partial view.
11801
11802 if Is_Tagged_Type (T)
11803 and then Present (Expression (Parent (D)))
11804 and then (not Is_Limited_Type (Current_Scope)
11805 or else Ada_Version < Ada_2012)
11806 and then not Error_Posted (Expression (Parent (D)))
11807 then
11808 if Ada_Version >= Ada_2012 then
11809 Error_Msg_N
11810 ("discriminants of nonlimited tagged type cannot have "
11811 & "defaults",
11812 Expression (New_D));
11813 else
11814 Error_Msg_N
11815 ("discriminants of tagged type cannot have defaults",
11816 Expression (New_D));
11817 end if;
11818 end if;
11819
11820 -- Ada 2005 (AI-230): Access discriminant allowed in
11821 -- non-limited record types.
11822
11823 if Ada_Version < Ada_2005 then
11824
11825 -- This restriction gets applied to the full type here. It
11826 -- has already been applied earlier to the partial view.
11827
11828 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11829 end if;
11830
11831 Next_Discriminant (D);
11832 Next (New_D);
11833 end loop;
11834 end;
11835
11836 elsif Present (Discriminant_Specifications (N)) then
11837 Process_Discriminants (N, Prev);
11838 end if;
11839 end Check_Or_Process_Discriminants;
11840
11841 ----------------------
11842 -- Check_Real_Bound --
11843 ----------------------
11844
11845 procedure Check_Real_Bound (Bound : Node_Id) is
11846 begin
11847 if not Is_Real_Type (Etype (Bound)) then
11848 Error_Msg_N
11849 ("bound in real type definition must be of real type", Bound);
11850
11851 elsif not Is_OK_Static_Expression (Bound) then
11852 Flag_Non_Static_Expr
11853 ("non-static expression used for real type bound!", Bound);
11854
11855 else
11856 return;
11857 end if;
11858
11859 Rewrite
11860 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11861 Analyze (Bound);
11862 Resolve (Bound, Standard_Float);
11863 end Check_Real_Bound;
11864
11865 ------------------------------
11866 -- Complete_Private_Subtype --
11867 ------------------------------
11868
11869 procedure Complete_Private_Subtype
11870 (Priv : Entity_Id;
11871 Full : Entity_Id;
11872 Full_Base : Entity_Id;
11873 Related_Nod : Node_Id)
11874 is
11875 Save_Next_Entity : Entity_Id;
11876 Save_Homonym : Entity_Id;
11877
11878 begin
11879 -- Set semantic attributes for (implicit) private subtype completion.
11880 -- If the full type has no discriminants, then it is a copy of the
11881 -- full view of the base. Otherwise, it is a subtype of the base with
11882 -- a possible discriminant constraint. Save and restore the original
11883 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11884 -- not corrupt the entity chain.
11885
11886 -- Note that the type of the full view is the same entity as the type
11887 -- of the partial view. In this fashion, the subtype has access to the
11888 -- correct view of the parent.
11889
11890 Save_Next_Entity := Next_Entity (Full);
11891 Save_Homonym := Homonym (Priv);
11892
11893 case Ekind (Full_Base) is
11894 when Class_Wide_Kind
11895 | Private_Kind
11896 | Protected_Kind
11897 | Task_Kind
11898 | E_Record_Subtype
11899 | E_Record_Type
11900 =>
11901 Copy_Node (Priv, Full);
11902
11903 Set_Has_Discriminants
11904 (Full, Has_Discriminants (Full_Base));
11905 Set_Has_Unknown_Discriminants
11906 (Full, Has_Unknown_Discriminants (Full_Base));
11907 Set_First_Entity (Full, First_Entity (Full_Base));
11908 Set_Last_Entity (Full, Last_Entity (Full_Base));
11909
11910 -- If the underlying base type is constrained, we know that the
11911 -- full view of the subtype is constrained as well (the converse
11912 -- is not necessarily true).
11913
11914 if Is_Constrained (Full_Base) then
11915 Set_Is_Constrained (Full);
11916 end if;
11917
11918 when others =>
11919 Copy_Node (Full_Base, Full);
11920
11921 Set_Chars (Full, Chars (Priv));
11922 Conditional_Delay (Full, Priv);
11923 Set_Sloc (Full, Sloc (Priv));
11924 end case;
11925
11926 Set_Next_Entity (Full, Save_Next_Entity);
11927 Set_Homonym (Full, Save_Homonym);
11928 Set_Associated_Node_For_Itype (Full, Related_Nod);
11929
11930 -- Set common attributes for all subtypes: kind, convention, etc.
11931
11932 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11933 Set_Convention (Full, Convention (Full_Base));
11934
11935 -- The Etype of the full view is inconsistent. Gigi needs to see the
11936 -- structural full view, which is what the current scheme gives: the
11937 -- Etype of the full view is the etype of the full base. However, if the
11938 -- full base is a derived type, the full view then looks like a subtype
11939 -- of the parent, not a subtype of the full base. If instead we write:
11940
11941 -- Set_Etype (Full, Full_Base);
11942
11943 -- then we get inconsistencies in the front-end (confusion between
11944 -- views). Several outstanding bugs are related to this ???
11945
11946 Set_Is_First_Subtype (Full, False);
11947 Set_Scope (Full, Scope (Priv));
11948 Set_Size_Info (Full, Full_Base);
11949 Set_RM_Size (Full, RM_Size (Full_Base));
11950 Set_Is_Itype (Full);
11951
11952 -- A subtype of a private-type-without-discriminants, whose full-view
11953 -- has discriminants with default expressions, is not constrained.
11954
11955 if not Has_Discriminants (Priv) then
11956 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11957
11958 if Has_Discriminants (Full_Base) then
11959 Set_Discriminant_Constraint
11960 (Full, Discriminant_Constraint (Full_Base));
11961
11962 -- The partial view may have been indefinite, the full view
11963 -- might not be.
11964
11965 Set_Has_Unknown_Discriminants
11966 (Full, Has_Unknown_Discriminants (Full_Base));
11967 end if;
11968 end if;
11969
11970 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11971 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11972
11973 -- Freeze the private subtype entity if its parent is delayed, and not
11974 -- already frozen. We skip this processing if the type is an anonymous
11975 -- subtype of a record component, or is the corresponding record of a
11976 -- protected type, since these are processed when the enclosing type
11977 -- is frozen. If the parent type is declared in a nested package then
11978 -- the freezing of the private and full views also happens later.
11979
11980 if not Is_Type (Scope (Full)) then
11981 if Is_Itype (Priv)
11982 and then In_Same_Source_Unit (Full, Full_Base)
11983 and then Scope (Full_Base) /= Scope (Full)
11984 then
11985 Set_Has_Delayed_Freeze (Full);
11986 Set_Has_Delayed_Freeze (Priv);
11987
11988 else
11989 Set_Has_Delayed_Freeze (Full,
11990 Has_Delayed_Freeze (Full_Base)
11991 and then not Is_Frozen (Full_Base));
11992 end if;
11993 end if;
11994
11995 Set_Freeze_Node (Full, Empty);
11996 Set_Is_Frozen (Full, False);
11997 Set_Full_View (Priv, Full);
11998
11999 if Has_Discriminants (Full) then
12000 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12001 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12002
12003 if Has_Unknown_Discriminants (Full) then
12004 Set_Discriminant_Constraint (Full, No_Elist);
12005 end if;
12006 end if;
12007
12008 if Ekind (Full_Base) = E_Record_Type
12009 and then Has_Discriminants (Full_Base)
12010 and then Has_Discriminants (Priv) -- might not, if errors
12011 and then not Has_Unknown_Discriminants (Priv)
12012 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12013 then
12014 Create_Constrained_Components
12015 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12016
12017 -- If the full base is itself derived from private, build a congruent
12018 -- subtype of its underlying type, for use by the back end. For a
12019 -- constrained record component, the declaration cannot be placed on
12020 -- the component list, but it must nevertheless be built an analyzed, to
12021 -- supply enough information for Gigi to compute the size of component.
12022
12023 elsif Ekind (Full_Base) in Private_Kind
12024 and then Is_Derived_Type (Full_Base)
12025 and then Has_Discriminants (Full_Base)
12026 and then (Ekind (Current_Scope) /= E_Record_Subtype)
12027 then
12028 if not Is_Itype (Priv)
12029 and then
12030 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
12031 then
12032 Build_Underlying_Full_View
12033 (Parent (Priv), Full, Etype (Full_Base));
12034
12035 elsif Nkind (Related_Nod) = N_Component_Declaration then
12036 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
12037 end if;
12038
12039 elsif Is_Record_Type (Full_Base) then
12040
12041 -- Show Full is simply a renaming of Full_Base
12042
12043 Set_Cloned_Subtype (Full, Full_Base);
12044 end if;
12045
12046 -- It is unsafe to share the bounds of a scalar type, because the Itype
12047 -- is elaborated on demand, and if a bound is non-static then different
12048 -- orders of elaboration in different units will lead to different
12049 -- external symbols.
12050
12051 if Is_Scalar_Type (Full_Base) then
12052 Set_Scalar_Range (Full,
12053 Make_Range (Sloc (Related_Nod),
12054 Low_Bound =>
12055 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12056 High_Bound =>
12057 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12058
12059 -- This completion inherits the bounds of the full parent, but if
12060 -- the parent is an unconstrained floating point type, so is the
12061 -- completion.
12062
12063 if Is_Floating_Point_Type (Full_Base) then
12064 Set_Includes_Infinities
12065 (Scalar_Range (Full), Has_Infinities (Full_Base));
12066 end if;
12067 end if;
12068
12069 -- ??? It seems that a lot of fields are missing that should be copied
12070 -- from Full_Base to Full. Here are some that are introduced in a
12071 -- non-disruptive way but a cleanup is necessary.
12072
12073 if Is_Tagged_Type (Full_Base) then
12074 Set_Is_Tagged_Type (Full);
12075 Set_Direct_Primitive_Operations
12076 (Full, Direct_Primitive_Operations (Full_Base));
12077 Set_No_Tagged_Streams_Pragma
12078 (Full, No_Tagged_Streams_Pragma (Full_Base));
12079
12080 -- Inherit class_wide type of full_base in case the partial view was
12081 -- not tagged. Otherwise it has already been created when the private
12082 -- subtype was analyzed.
12083
12084 if No (Class_Wide_Type (Full)) then
12085 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12086 end if;
12087
12088 -- If this is a subtype of a protected or task type, constrain its
12089 -- corresponding record, unless this is a subtype without constraints,
12090 -- i.e. a simple renaming as with an actual subtype in an instance.
12091
12092 elsif Is_Concurrent_Type (Full_Base) then
12093 if Has_Discriminants (Full)
12094 and then Present (Corresponding_Record_Type (Full_Base))
12095 and then
12096 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12097 then
12098 Set_Corresponding_Record_Type (Full,
12099 Constrain_Corresponding_Record
12100 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12101
12102 else
12103 Set_Corresponding_Record_Type (Full,
12104 Corresponding_Record_Type (Full_Base));
12105 end if;
12106 end if;
12107
12108 -- Link rep item chain, and also setting of Has_Predicates from private
12109 -- subtype to full subtype, since we will need these on the full subtype
12110 -- to create the predicate function. Note that the full subtype may
12111 -- already have rep items, inherited from the full view of the base
12112 -- type, so we must be sure not to overwrite these entries.
12113
12114 declare
12115 Append : Boolean;
12116 Item : Node_Id;
12117 Next_Item : Node_Id;
12118 Priv_Item : Node_Id;
12119
12120 begin
12121 Item := First_Rep_Item (Full);
12122 Priv_Item := First_Rep_Item (Priv);
12123
12124 -- If no existing rep items on full type, we can just link directly
12125 -- to the list of items on the private type, if any exist.. Same if
12126 -- the rep items are only those inherited from the base
12127
12128 if (No (Item)
12129 or else Nkind (Item) /= N_Aspect_Specification
12130 or else Entity (Item) = Full_Base)
12131 and then Present (First_Rep_Item (Priv))
12132 then
12133 Set_First_Rep_Item (Full, Priv_Item);
12134
12135 -- Otherwise, search to the end of items currently linked to the full
12136 -- subtype and append the private items to the end. However, if Priv
12137 -- and Full already have the same list of rep items, then the append
12138 -- is not done, as that would create a circularity.
12139 --
12140 -- The partial view may have a predicate and the rep item lists of
12141 -- both views agree when inherited from the same ancestor. In that
12142 -- case, simply propagate the list from one view to the other.
12143 -- A more complex analysis needed here ???
12144
12145 elsif Present (Priv_Item)
12146 and then Item = Next_Rep_Item (Priv_Item)
12147 then
12148 Set_First_Rep_Item (Full, Priv_Item);
12149
12150 elsif Item /= Priv_Item then
12151 Append := True;
12152 loop
12153 Next_Item := Next_Rep_Item (Item);
12154 exit when No (Next_Item);
12155 Item := Next_Item;
12156
12157 -- If the private view has aspect specifications, the full view
12158 -- inherits them. Since these aspects may already have been
12159 -- attached to the full view during derivation, do not append
12160 -- them if already present.
12161
12162 if Item = First_Rep_Item (Priv) then
12163 Append := False;
12164 exit;
12165 end if;
12166 end loop;
12167
12168 -- And link the private type items at the end of the chain
12169
12170 if Append then
12171 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12172 end if;
12173 end if;
12174 end;
12175
12176 -- Make sure Has_Predicates is set on full type if it is set on the
12177 -- private type. Note that it may already be set on the full type and
12178 -- if so, we don't want to unset it. Similarly, propagate information
12179 -- about delayed aspects, because the corresponding pragmas must be
12180 -- analyzed when one of the views is frozen. This last step is needed
12181 -- in particular when the full type is a scalar type for which an
12182 -- anonymous base type is constructed.
12183
12184 -- The predicate functions are generated either at the freeze point
12185 -- of the type or at the end of the visible part, and we must avoid
12186 -- generating them twice.
12187
12188 if Has_Predicates (Priv) then
12189 Set_Has_Predicates (Full);
12190
12191 if Present (Predicate_Function (Priv))
12192 and then No (Predicate_Function (Full))
12193 then
12194 Set_Predicate_Function (Full, Predicate_Function (Priv));
12195 end if;
12196 end if;
12197
12198 if Has_Delayed_Aspects (Priv) then
12199 Set_Has_Delayed_Aspects (Full);
12200 end if;
12201 end Complete_Private_Subtype;
12202
12203 ----------------------------
12204 -- Constant_Redeclaration --
12205 ----------------------------
12206
12207 procedure Constant_Redeclaration
12208 (Id : Entity_Id;
12209 N : Node_Id;
12210 T : out Entity_Id)
12211 is
12212 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12213 Obj_Def : constant Node_Id := Object_Definition (N);
12214 New_T : Entity_Id;
12215
12216 procedure Check_Possible_Deferred_Completion
12217 (Prev_Id : Entity_Id;
12218 Prev_Obj_Def : Node_Id;
12219 Curr_Obj_Def : Node_Id);
12220 -- Determine whether the two object definitions describe the partial
12221 -- and the full view of a constrained deferred constant. Generate
12222 -- a subtype for the full view and verify that it statically matches
12223 -- the subtype of the partial view.
12224
12225 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12226 -- If deferred constant is an access type initialized with an allocator,
12227 -- check whether there is an illegal recursion in the definition,
12228 -- through a default value of some record subcomponent. This is normally
12229 -- detected when generating init procs, but requires this additional
12230 -- mechanism when expansion is disabled.
12231
12232 ----------------------------------------
12233 -- Check_Possible_Deferred_Completion --
12234 ----------------------------------------
12235
12236 procedure Check_Possible_Deferred_Completion
12237 (Prev_Id : Entity_Id;
12238 Prev_Obj_Def : Node_Id;
12239 Curr_Obj_Def : Node_Id)
12240 is
12241 begin
12242 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12243 and then Present (Constraint (Prev_Obj_Def))
12244 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12245 and then Present (Constraint (Curr_Obj_Def))
12246 then
12247 declare
12248 Loc : constant Source_Ptr := Sloc (N);
12249 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12250 Decl : constant Node_Id :=
12251 Make_Subtype_Declaration (Loc,
12252 Defining_Identifier => Def_Id,
12253 Subtype_Indication =>
12254 Relocate_Node (Curr_Obj_Def));
12255
12256 begin
12257 Insert_Before_And_Analyze (N, Decl);
12258 Set_Etype (Id, Def_Id);
12259
12260 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12261 Error_Msg_Sloc := Sloc (Prev_Id);
12262 Error_Msg_N ("subtype does not statically match deferred "
12263 & "declaration #", N);
12264 end if;
12265 end;
12266 end if;
12267 end Check_Possible_Deferred_Completion;
12268
12269 ---------------------------------
12270 -- Check_Recursive_Declaration --
12271 ---------------------------------
12272
12273 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12274 Comp : Entity_Id;
12275
12276 begin
12277 if Is_Record_Type (Typ) then
12278 Comp := First_Component (Typ);
12279 while Present (Comp) loop
12280 if Comes_From_Source (Comp) then
12281 if Present (Expression (Parent (Comp)))
12282 and then Is_Entity_Name (Expression (Parent (Comp)))
12283 and then Entity (Expression (Parent (Comp))) = Prev
12284 then
12285 Error_Msg_Sloc := Sloc (Parent (Comp));
12286 Error_Msg_NE
12287 ("illegal circularity with declaration for & #",
12288 N, Comp);
12289 return;
12290
12291 elsif Is_Record_Type (Etype (Comp)) then
12292 Check_Recursive_Declaration (Etype (Comp));
12293 end if;
12294 end if;
12295
12296 Next_Component (Comp);
12297 end loop;
12298 end if;
12299 end Check_Recursive_Declaration;
12300
12301 -- Start of processing for Constant_Redeclaration
12302
12303 begin
12304 if Nkind (Parent (Prev)) = N_Object_Declaration then
12305 if Nkind (Object_Definition
12306 (Parent (Prev))) = N_Subtype_Indication
12307 then
12308 -- Find type of new declaration. The constraints of the two
12309 -- views must match statically, but there is no point in
12310 -- creating an itype for the full view.
12311
12312 if Nkind (Obj_Def) = N_Subtype_Indication then
12313 Find_Type (Subtype_Mark (Obj_Def));
12314 New_T := Entity (Subtype_Mark (Obj_Def));
12315
12316 else
12317 Find_Type (Obj_Def);
12318 New_T := Entity (Obj_Def);
12319 end if;
12320
12321 T := Etype (Prev);
12322
12323 else
12324 -- The full view may impose a constraint, even if the partial
12325 -- view does not, so construct the subtype.
12326
12327 New_T := Find_Type_Of_Object (Obj_Def, N);
12328 T := New_T;
12329 end if;
12330
12331 else
12332 -- Current declaration is illegal, diagnosed below in Enter_Name
12333
12334 T := Empty;
12335 New_T := Any_Type;
12336 end if;
12337
12338 -- If previous full declaration or a renaming declaration exists, or if
12339 -- a homograph is present, let Enter_Name handle it, either with an
12340 -- error or with the removal of an overridden implicit subprogram.
12341 -- The previous one is a full declaration if it has an expression
12342 -- (which in the case of an aggregate is indicated by the Init flag).
12343
12344 if Ekind (Prev) /= E_Constant
12345 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12346 or else Present (Expression (Parent (Prev)))
12347 or else Has_Init_Expression (Parent (Prev))
12348 or else Present (Full_View (Prev))
12349 then
12350 Enter_Name (Id);
12351
12352 -- Verify that types of both declarations match, or else that both types
12353 -- are anonymous access types whose designated subtypes statically match
12354 -- (as allowed in Ada 2005 by AI-385).
12355
12356 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12357 and then
12358 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12359 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12360 or else Is_Access_Constant (Etype (New_T)) /=
12361 Is_Access_Constant (Etype (Prev))
12362 or else Can_Never_Be_Null (Etype (New_T)) /=
12363 Can_Never_Be_Null (Etype (Prev))
12364 or else Null_Exclusion_Present (Parent (Prev)) /=
12365 Null_Exclusion_Present (Parent (Id))
12366 or else not Subtypes_Statically_Match
12367 (Designated_Type (Etype (Prev)),
12368 Designated_Type (Etype (New_T))))
12369 then
12370 Error_Msg_Sloc := Sloc (Prev);
12371 Error_Msg_N ("type does not match declaration#", N);
12372 Set_Full_View (Prev, Id);
12373 Set_Etype (Id, Any_Type);
12374
12375 -- A deferred constant whose type is an anonymous array is always
12376 -- illegal (unless imported). A detailed error message might be
12377 -- helpful for Ada beginners.
12378
12379 if Nkind (Object_Definition (Parent (Prev)))
12380 = N_Constrained_Array_Definition
12381 and then Nkind (Object_Definition (N))
12382 = N_Constrained_Array_Definition
12383 then
12384 Error_Msg_N ("\each anonymous array is a distinct type", N);
12385 Error_Msg_N ("a deferred constant must have a named type",
12386 Object_Definition (Parent (Prev)));
12387 end if;
12388
12389 elsif
12390 Null_Exclusion_Present (Parent (Prev))
12391 and then not Null_Exclusion_Present (N)
12392 then
12393 Error_Msg_Sloc := Sloc (Prev);
12394 Error_Msg_N ("null-exclusion does not match declaration#", N);
12395 Set_Full_View (Prev, Id);
12396 Set_Etype (Id, Any_Type);
12397
12398 -- If so, process the full constant declaration
12399
12400 else
12401 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12402 -- the deferred declaration is constrained, then the subtype defined
12403 -- by the subtype_indication in the full declaration shall match it
12404 -- statically.
12405
12406 Check_Possible_Deferred_Completion
12407 (Prev_Id => Prev,
12408 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12409 Curr_Obj_Def => Obj_Def);
12410
12411 Set_Full_View (Prev, Id);
12412 Set_Is_Public (Id, Is_Public (Prev));
12413 Set_Is_Internal (Id);
12414 Append_Entity (Id, Current_Scope);
12415
12416 -- Check ALIASED present if present before (RM 7.4(7))
12417
12418 if Is_Aliased (Prev)
12419 and then not Aliased_Present (N)
12420 then
12421 Error_Msg_Sloc := Sloc (Prev);
12422 Error_Msg_N ("ALIASED required (see declaration #)", N);
12423 end if;
12424
12425 -- Check that placement is in private part and that the incomplete
12426 -- declaration appeared in the visible part.
12427
12428 if Ekind (Current_Scope) = E_Package
12429 and then not In_Private_Part (Current_Scope)
12430 then
12431 Error_Msg_Sloc := Sloc (Prev);
12432 Error_Msg_N
12433 ("full constant for declaration # must be in private part", N);
12434
12435 elsif Ekind (Current_Scope) = E_Package
12436 and then
12437 List_Containing (Parent (Prev)) /=
12438 Visible_Declarations (Package_Specification (Current_Scope))
12439 then
12440 Error_Msg_N
12441 ("deferred constant must be declared in visible part",
12442 Parent (Prev));
12443 end if;
12444
12445 if Is_Access_Type (T)
12446 and then Nkind (Expression (N)) = N_Allocator
12447 then
12448 Check_Recursive_Declaration (Designated_Type (T));
12449 end if;
12450
12451 -- A deferred constant is a visible entity. If type has invariants,
12452 -- verify that the initial value satisfies them.
12453
12454 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12455 Insert_After (N,
12456 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12457 end if;
12458 end if;
12459 end Constant_Redeclaration;
12460
12461 ----------------------
12462 -- Constrain_Access --
12463 ----------------------
12464
12465 procedure Constrain_Access
12466 (Def_Id : in out Entity_Id;
12467 S : Node_Id;
12468 Related_Nod : Node_Id)
12469 is
12470 T : constant Entity_Id := Entity (Subtype_Mark (S));
12471 Desig_Type : constant Entity_Id := Designated_Type (T);
12472 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12473 Constraint_OK : Boolean := True;
12474
12475 begin
12476 if Is_Array_Type (Desig_Type) then
12477 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12478
12479 elsif (Is_Record_Type (Desig_Type)
12480 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12481 and then not Is_Constrained (Desig_Type)
12482 then
12483 -- ??? The following code is a temporary bypass to ignore a
12484 -- discriminant constraint on access type if it is constraining
12485 -- the current record. Avoid creating the implicit subtype of the
12486 -- record we are currently compiling since right now, we cannot
12487 -- handle these. For now, just return the access type itself.
12488
12489 if Desig_Type = Current_Scope
12490 and then No (Def_Id)
12491 then
12492 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12493 Def_Id := Entity (Subtype_Mark (S));
12494
12495 -- This call added to ensure that the constraint is analyzed
12496 -- (needed for a B test). Note that we still return early from
12497 -- this procedure to avoid recursive processing. ???
12498
12499 Constrain_Discriminated_Type
12500 (Desig_Subtype, S, Related_Nod, For_Access => True);
12501 return;
12502 end if;
12503
12504 -- Enforce rule that the constraint is illegal if there is an
12505 -- unconstrained view of the designated type. This means that the
12506 -- partial view (either a private type declaration or a derivation
12507 -- from a private type) has no discriminants. (Defect Report
12508 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12509
12510 -- Rule updated for Ada 2005: The private type is said to have
12511 -- a constrained partial view, given that objects of the type
12512 -- can be declared. Furthermore, the rule applies to all access
12513 -- types, unlike the rule concerning default discriminants (see
12514 -- RM 3.7.1(7/3))
12515
12516 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12517 and then Has_Private_Declaration (Desig_Type)
12518 and then In_Open_Scopes (Scope (Desig_Type))
12519 and then Has_Discriminants (Desig_Type)
12520 then
12521 declare
12522 Pack : constant Node_Id :=
12523 Unit_Declaration_Node (Scope (Desig_Type));
12524 Decls : List_Id;
12525 Decl : Node_Id;
12526
12527 begin
12528 if Nkind (Pack) = N_Package_Declaration then
12529 Decls := Visible_Declarations (Specification (Pack));
12530 Decl := First (Decls);
12531 while Present (Decl) loop
12532 if (Nkind (Decl) = N_Private_Type_Declaration
12533 and then Chars (Defining_Identifier (Decl)) =
12534 Chars (Desig_Type))
12535
12536 or else
12537 (Nkind (Decl) = N_Full_Type_Declaration
12538 and then
12539 Chars (Defining_Identifier (Decl)) =
12540 Chars (Desig_Type)
12541 and then Is_Derived_Type (Desig_Type)
12542 and then
12543 Has_Private_Declaration (Etype (Desig_Type)))
12544 then
12545 if No (Discriminant_Specifications (Decl)) then
12546 Error_Msg_N
12547 ("cannot constrain access type if designated "
12548 & "type has constrained partial view", S);
12549 end if;
12550
12551 exit;
12552 end if;
12553
12554 Next (Decl);
12555 end loop;
12556 end if;
12557 end;
12558 end if;
12559
12560 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12561 For_Access => True);
12562
12563 elsif Is_Concurrent_Type (Desig_Type)
12564 and then not Is_Constrained (Desig_Type)
12565 then
12566 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12567
12568 else
12569 Error_Msg_N ("invalid constraint on access type", S);
12570
12571 -- We simply ignore an invalid constraint
12572
12573 Desig_Subtype := Desig_Type;
12574 Constraint_OK := False;
12575 end if;
12576
12577 if No (Def_Id) then
12578 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12579 else
12580 Set_Ekind (Def_Id, E_Access_Subtype);
12581 end if;
12582
12583 if Constraint_OK then
12584 Set_Etype (Def_Id, Base_Type (T));
12585
12586 if Is_Private_Type (Desig_Type) then
12587 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12588 end if;
12589 else
12590 Set_Etype (Def_Id, Any_Type);
12591 end if;
12592
12593 Set_Size_Info (Def_Id, T);
12594 Set_Is_Constrained (Def_Id, Constraint_OK);
12595 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12596 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12597 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12598
12599 Conditional_Delay (Def_Id, T);
12600
12601 -- AI-363 : Subtypes of general access types whose designated types have
12602 -- default discriminants are disallowed. In instances, the rule has to
12603 -- be checked against the actual, of which T is the subtype. In a
12604 -- generic body, the rule is checked assuming that the actual type has
12605 -- defaulted discriminants.
12606
12607 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12608 if Ekind (Base_Type (T)) = E_General_Access_Type
12609 and then Has_Defaulted_Discriminants (Desig_Type)
12610 then
12611 if Ada_Version < Ada_2005 then
12612 Error_Msg_N
12613 ("access subtype of general access type would not " &
12614 "be allowed in Ada 2005?y?", S);
12615 else
12616 Error_Msg_N
12617 ("access subtype of general access type not allowed", S);
12618 end if;
12619
12620 Error_Msg_N ("\discriminants have defaults", S);
12621
12622 elsif Is_Access_Type (T)
12623 and then Is_Generic_Type (Desig_Type)
12624 and then Has_Discriminants (Desig_Type)
12625 and then In_Package_Body (Current_Scope)
12626 then
12627 if Ada_Version < Ada_2005 then
12628 Error_Msg_N
12629 ("access subtype would not be allowed in generic body "
12630 & "in Ada 2005?y?", S);
12631 else
12632 Error_Msg_N
12633 ("access subtype not allowed in generic body", S);
12634 end if;
12635
12636 Error_Msg_N
12637 ("\designated type is a discriminated formal", S);
12638 end if;
12639 end if;
12640 end Constrain_Access;
12641
12642 ---------------------
12643 -- Constrain_Array --
12644 ---------------------
12645
12646 procedure Constrain_Array
12647 (Def_Id : in out Entity_Id;
12648 SI : Node_Id;
12649 Related_Nod : Node_Id;
12650 Related_Id : Entity_Id;
12651 Suffix : Character)
12652 is
12653 C : constant Node_Id := Constraint (SI);
12654 Number_Of_Constraints : Nat := 0;
12655 Index : Node_Id;
12656 S, T : Entity_Id;
12657 Constraint_OK : Boolean := True;
12658
12659 begin
12660 T := Entity (Subtype_Mark (SI));
12661
12662 if Is_Access_Type (T) then
12663 T := Designated_Type (T);
12664 end if;
12665
12666 -- If an index constraint follows a subtype mark in a subtype indication
12667 -- then the type or subtype denoted by the subtype mark must not already
12668 -- impose an index constraint. The subtype mark must denote either an
12669 -- unconstrained array type or an access type whose designated type
12670 -- is such an array type... (RM 3.6.1)
12671
12672 if Is_Constrained (T) then
12673 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12674 Constraint_OK := False;
12675
12676 else
12677 S := First (Constraints (C));
12678 while Present (S) loop
12679 Number_Of_Constraints := Number_Of_Constraints + 1;
12680 Next (S);
12681 end loop;
12682
12683 -- In either case, the index constraint must provide a discrete
12684 -- range for each index of the array type and the type of each
12685 -- discrete range must be the same as that of the corresponding
12686 -- index. (RM 3.6.1)
12687
12688 if Number_Of_Constraints /= Number_Dimensions (T) then
12689 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12690 Constraint_OK := False;
12691
12692 else
12693 S := First (Constraints (C));
12694 Index := First_Index (T);
12695 Analyze (Index);
12696
12697 -- Apply constraints to each index type
12698
12699 for J in 1 .. Number_Of_Constraints loop
12700 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12701 Next (Index);
12702 Next (S);
12703 end loop;
12704
12705 end if;
12706 end if;
12707
12708 if No (Def_Id) then
12709 Def_Id :=
12710 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12711 Set_Parent (Def_Id, Related_Nod);
12712
12713 else
12714 Set_Ekind (Def_Id, E_Array_Subtype);
12715 end if;
12716
12717 Set_Size_Info (Def_Id, (T));
12718 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12719 Set_Etype (Def_Id, Base_Type (T));
12720
12721 if Constraint_OK then
12722 Set_First_Index (Def_Id, First (Constraints (C)));
12723 else
12724 Set_First_Index (Def_Id, First_Index (T));
12725 end if;
12726
12727 Set_Is_Constrained (Def_Id, True);
12728 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12729 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12730
12731 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12732 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12733
12734 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12735 -- We need to initialize the attribute because if Def_Id is previously
12736 -- analyzed through a limited_with clause, it will have the attributes
12737 -- of an incomplete type, one of which is an Elist that overlaps the
12738 -- Packed_Array_Impl_Type field.
12739
12740 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12741
12742 -- Build a freeze node if parent still needs one. Also make sure that
12743 -- the Depends_On_Private status is set because the subtype will need
12744 -- reprocessing at the time the base type does, and also we must set a
12745 -- conditional delay.
12746
12747 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12748 Conditional_Delay (Def_Id, T);
12749 end Constrain_Array;
12750
12751 ------------------------------
12752 -- Constrain_Component_Type --
12753 ------------------------------
12754
12755 function Constrain_Component_Type
12756 (Comp : Entity_Id;
12757 Constrained_Typ : Entity_Id;
12758 Related_Node : Node_Id;
12759 Typ : Entity_Id;
12760 Constraints : Elist_Id) return Entity_Id
12761 is
12762 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12763 Compon_Type : constant Entity_Id := Etype (Comp);
12764
12765 function Build_Constrained_Array_Type
12766 (Old_Type : Entity_Id) return Entity_Id;
12767 -- If Old_Type is an array type, one of whose indexes is constrained
12768 -- by a discriminant, build an Itype whose constraint replaces the
12769 -- discriminant with its value in the constraint.
12770
12771 function Build_Constrained_Discriminated_Type
12772 (Old_Type : Entity_Id) return Entity_Id;
12773 -- Ditto for record components
12774
12775 function Build_Constrained_Access_Type
12776 (Old_Type : Entity_Id) return Entity_Id;
12777 -- Ditto for access types. Makes use of previous two functions, to
12778 -- constrain designated type.
12779
12780 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12781 -- T is an array or discriminated type, C is a list of constraints
12782 -- that apply to T. This routine builds the constrained subtype.
12783
12784 function Is_Discriminant (Expr : Node_Id) return Boolean;
12785 -- Returns True if Expr is a discriminant
12786
12787 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12788 -- Find the value of discriminant Discrim in Constraint
12789
12790 -----------------------------------
12791 -- Build_Constrained_Access_Type --
12792 -----------------------------------
12793
12794 function Build_Constrained_Access_Type
12795 (Old_Type : Entity_Id) return Entity_Id
12796 is
12797 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12798 Itype : Entity_Id;
12799 Desig_Subtype : Entity_Id;
12800 Scop : Entity_Id;
12801
12802 begin
12803 -- if the original access type was not embedded in the enclosing
12804 -- type definition, there is no need to produce a new access
12805 -- subtype. In fact every access type with an explicit constraint
12806 -- generates an itype whose scope is the enclosing record.
12807
12808 if not Is_Type (Scope (Old_Type)) then
12809 return Old_Type;
12810
12811 elsif Is_Array_Type (Desig_Type) then
12812 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12813
12814 elsif Has_Discriminants (Desig_Type) then
12815
12816 -- This may be an access type to an enclosing record type for
12817 -- which we are constructing the constrained components. Return
12818 -- the enclosing record subtype. This is not always correct,
12819 -- but avoids infinite recursion. ???
12820
12821 Desig_Subtype := Any_Type;
12822
12823 for J in reverse 0 .. Scope_Stack.Last loop
12824 Scop := Scope_Stack.Table (J).Entity;
12825
12826 if Is_Type (Scop)
12827 and then Base_Type (Scop) = Base_Type (Desig_Type)
12828 then
12829 Desig_Subtype := Scop;
12830 end if;
12831
12832 exit when not Is_Type (Scop);
12833 end loop;
12834
12835 if Desig_Subtype = Any_Type then
12836 Desig_Subtype :=
12837 Build_Constrained_Discriminated_Type (Desig_Type);
12838 end if;
12839
12840 else
12841 return Old_Type;
12842 end if;
12843
12844 if Desig_Subtype /= Desig_Type then
12845
12846 -- The Related_Node better be here or else we won't be able
12847 -- to attach new itypes to a node in the tree.
12848
12849 pragma Assert (Present (Related_Node));
12850
12851 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12852
12853 Set_Etype (Itype, Base_Type (Old_Type));
12854 Set_Size_Info (Itype, (Old_Type));
12855 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12856 Set_Depends_On_Private (Itype, Has_Private_Component
12857 (Old_Type));
12858 Set_Is_Access_Constant (Itype, Is_Access_Constant
12859 (Old_Type));
12860
12861 -- The new itype needs freezing when it depends on a not frozen
12862 -- type and the enclosing subtype needs freezing.
12863
12864 if Has_Delayed_Freeze (Constrained_Typ)
12865 and then not Is_Frozen (Constrained_Typ)
12866 then
12867 Conditional_Delay (Itype, Base_Type (Old_Type));
12868 end if;
12869
12870 return Itype;
12871
12872 else
12873 return Old_Type;
12874 end if;
12875 end Build_Constrained_Access_Type;
12876
12877 ----------------------------------
12878 -- Build_Constrained_Array_Type --
12879 ----------------------------------
12880
12881 function Build_Constrained_Array_Type
12882 (Old_Type : Entity_Id) return Entity_Id
12883 is
12884 Lo_Expr : Node_Id;
12885 Hi_Expr : Node_Id;
12886 Old_Index : Node_Id;
12887 Range_Node : Node_Id;
12888 Constr_List : List_Id;
12889
12890 Need_To_Create_Itype : Boolean := False;
12891
12892 begin
12893 Old_Index := First_Index (Old_Type);
12894 while Present (Old_Index) loop
12895 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12896
12897 if Is_Discriminant (Lo_Expr)
12898 or else
12899 Is_Discriminant (Hi_Expr)
12900 then
12901 Need_To_Create_Itype := True;
12902 end if;
12903
12904 Next_Index (Old_Index);
12905 end loop;
12906
12907 if Need_To_Create_Itype then
12908 Constr_List := New_List;
12909
12910 Old_Index := First_Index (Old_Type);
12911 while Present (Old_Index) loop
12912 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12913
12914 if Is_Discriminant (Lo_Expr) then
12915 Lo_Expr := Get_Discr_Value (Lo_Expr);
12916 end if;
12917
12918 if Is_Discriminant (Hi_Expr) then
12919 Hi_Expr := Get_Discr_Value (Hi_Expr);
12920 end if;
12921
12922 Range_Node :=
12923 Make_Range
12924 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12925
12926 Append (Range_Node, To => Constr_List);
12927
12928 Next_Index (Old_Index);
12929 end loop;
12930
12931 return Build_Subtype (Old_Type, Constr_List);
12932
12933 else
12934 return Old_Type;
12935 end if;
12936 end Build_Constrained_Array_Type;
12937
12938 ------------------------------------------
12939 -- Build_Constrained_Discriminated_Type --
12940 ------------------------------------------
12941
12942 function Build_Constrained_Discriminated_Type
12943 (Old_Type : Entity_Id) return Entity_Id
12944 is
12945 Expr : Node_Id;
12946 Constr_List : List_Id;
12947 Old_Constraint : Elmt_Id;
12948
12949 Need_To_Create_Itype : Boolean := False;
12950
12951 begin
12952 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12953 while Present (Old_Constraint) loop
12954 Expr := Node (Old_Constraint);
12955
12956 if Is_Discriminant (Expr) then
12957 Need_To_Create_Itype := True;
12958 end if;
12959
12960 Next_Elmt (Old_Constraint);
12961 end loop;
12962
12963 if Need_To_Create_Itype then
12964 Constr_List := New_List;
12965
12966 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12967 while Present (Old_Constraint) loop
12968 Expr := Node (Old_Constraint);
12969
12970 if Is_Discriminant (Expr) then
12971 Expr := Get_Discr_Value (Expr);
12972 end if;
12973
12974 Append (New_Copy_Tree (Expr), To => Constr_List);
12975
12976 Next_Elmt (Old_Constraint);
12977 end loop;
12978
12979 return Build_Subtype (Old_Type, Constr_List);
12980
12981 else
12982 return Old_Type;
12983 end if;
12984 end Build_Constrained_Discriminated_Type;
12985
12986 -------------------
12987 -- Build_Subtype --
12988 -------------------
12989
12990 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12991 Indic : Node_Id;
12992 Subtyp_Decl : Node_Id;
12993 Def_Id : Entity_Id;
12994 Btyp : Entity_Id := Base_Type (T);
12995
12996 begin
12997 -- The Related_Node better be here or else we won't be able to
12998 -- attach new itypes to a node in the tree.
12999
13000 pragma Assert (Present (Related_Node));
13001
13002 -- If the view of the component's type is incomplete or private
13003 -- with unknown discriminants, then the constraint must be applied
13004 -- to the full type.
13005
13006 if Has_Unknown_Discriminants (Btyp)
13007 and then Present (Underlying_Type (Btyp))
13008 then
13009 Btyp := Underlying_Type (Btyp);
13010 end if;
13011
13012 Indic :=
13013 Make_Subtype_Indication (Loc,
13014 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13015 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13016
13017 Def_Id := Create_Itype (Ekind (T), Related_Node);
13018
13019 Subtyp_Decl :=
13020 Make_Subtype_Declaration (Loc,
13021 Defining_Identifier => Def_Id,
13022 Subtype_Indication => Indic);
13023
13024 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13025
13026 -- Itypes must be analyzed with checks off (see package Itypes)
13027
13028 Analyze (Subtyp_Decl, Suppress => All_Checks);
13029
13030 return Def_Id;
13031 end Build_Subtype;
13032
13033 ---------------------
13034 -- Get_Discr_Value --
13035 ---------------------
13036
13037 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13038 D : Entity_Id;
13039 E : Elmt_Id;
13040
13041 begin
13042 -- The discriminant may be declared for the type, in which case we
13043 -- find it by iterating over the list of discriminants. If the
13044 -- discriminant is inherited from a parent type, it appears as the
13045 -- corresponding discriminant of the current type. This will be the
13046 -- case when constraining an inherited component whose constraint is
13047 -- given by a discriminant of the parent.
13048
13049 D := First_Discriminant (Typ);
13050 E := First_Elmt (Constraints);
13051
13052 while Present (D) loop
13053 if D = Entity (Discrim)
13054 or else D = CR_Discriminant (Entity (Discrim))
13055 or else Corresponding_Discriminant (D) = Entity (Discrim)
13056 then
13057 return Node (E);
13058 end if;
13059
13060 Next_Discriminant (D);
13061 Next_Elmt (E);
13062 end loop;
13063
13064 -- The Corresponding_Discriminant mechanism is incomplete, because
13065 -- the correspondence between new and old discriminants is not one
13066 -- to one: one new discriminant can constrain several old ones. In
13067 -- that case, scan sequentially the stored_constraint, the list of
13068 -- discriminants of the parents, and the constraints.
13069
13070 -- Previous code checked for the present of the Stored_Constraint
13071 -- list for the derived type, but did not use it at all. Should it
13072 -- be present when the component is a discriminated task type?
13073
13074 if Is_Derived_Type (Typ)
13075 and then Scope (Entity (Discrim)) = Etype (Typ)
13076 then
13077 D := First_Discriminant (Etype (Typ));
13078 E := First_Elmt (Constraints);
13079 while Present (D) loop
13080 if D = Entity (Discrim) then
13081 return Node (E);
13082 end if;
13083
13084 Next_Discriminant (D);
13085 Next_Elmt (E);
13086 end loop;
13087 end if;
13088
13089 -- Something is wrong if we did not find the value
13090
13091 raise Program_Error;
13092 end Get_Discr_Value;
13093
13094 ---------------------
13095 -- Is_Discriminant --
13096 ---------------------
13097
13098 function Is_Discriminant (Expr : Node_Id) return Boolean is
13099 Discrim_Scope : Entity_Id;
13100
13101 begin
13102 if Denotes_Discriminant (Expr) then
13103 Discrim_Scope := Scope (Entity (Expr));
13104
13105 -- Either we have a reference to one of Typ's discriminants,
13106
13107 pragma Assert (Discrim_Scope = Typ
13108
13109 -- or to the discriminants of the parent type, in the case
13110 -- of a derivation of a tagged type with variants.
13111
13112 or else Discrim_Scope = Etype (Typ)
13113 or else Full_View (Discrim_Scope) = Etype (Typ)
13114
13115 -- or same as above for the case where the discriminants
13116 -- were declared in Typ's private view.
13117
13118 or else (Is_Private_Type (Discrim_Scope)
13119 and then Chars (Discrim_Scope) = Chars (Typ))
13120
13121 -- or else we are deriving from the full view and the
13122 -- discriminant is declared in the private entity.
13123
13124 or else (Is_Private_Type (Typ)
13125 and then Chars (Discrim_Scope) = Chars (Typ))
13126
13127 -- Or we are constrained the corresponding record of a
13128 -- synchronized type that completes a private declaration.
13129
13130 or else (Is_Concurrent_Record_Type (Typ)
13131 and then
13132 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13133
13134 -- or we have a class-wide type, in which case make sure the
13135 -- discriminant found belongs to the root type.
13136
13137 or else (Is_Class_Wide_Type (Typ)
13138 and then Etype (Typ) = Discrim_Scope));
13139
13140 return True;
13141 end if;
13142
13143 -- In all other cases we have something wrong
13144
13145 return False;
13146 end Is_Discriminant;
13147
13148 -- Start of processing for Constrain_Component_Type
13149
13150 begin
13151 if Nkind (Parent (Comp)) = N_Component_Declaration
13152 and then Comes_From_Source (Parent (Comp))
13153 and then Comes_From_Source
13154 (Subtype_Indication (Component_Definition (Parent (Comp))))
13155 and then
13156 Is_Entity_Name
13157 (Subtype_Indication (Component_Definition (Parent (Comp))))
13158 then
13159 return Compon_Type;
13160
13161 elsif Is_Array_Type (Compon_Type) then
13162 return Build_Constrained_Array_Type (Compon_Type);
13163
13164 elsif Has_Discriminants (Compon_Type) then
13165 return Build_Constrained_Discriminated_Type (Compon_Type);
13166
13167 elsif Is_Access_Type (Compon_Type) then
13168 return Build_Constrained_Access_Type (Compon_Type);
13169
13170 else
13171 return Compon_Type;
13172 end if;
13173 end Constrain_Component_Type;
13174
13175 --------------------------
13176 -- Constrain_Concurrent --
13177 --------------------------
13178
13179 -- For concurrent types, the associated record value type carries the same
13180 -- discriminants, so when we constrain a concurrent type, we must constrain
13181 -- the corresponding record type as well.
13182
13183 procedure Constrain_Concurrent
13184 (Def_Id : in out Entity_Id;
13185 SI : Node_Id;
13186 Related_Nod : Node_Id;
13187 Related_Id : Entity_Id;
13188 Suffix : Character)
13189 is
13190 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13191 -- case of a private subtype (needed when only doing semantic analysis).
13192
13193 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13194 T_Val : Entity_Id;
13195
13196 begin
13197 if Is_Access_Type (T_Ent) then
13198 T_Ent := Designated_Type (T_Ent);
13199 end if;
13200
13201 T_Val := Corresponding_Record_Type (T_Ent);
13202
13203 if Present (T_Val) then
13204
13205 if No (Def_Id) then
13206 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13207
13208 -- Elaborate itype now, as it may be used in a subsequent
13209 -- synchronized operation in another scope.
13210
13211 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13212 Build_Itype_Reference (Def_Id, Related_Nod);
13213 end if;
13214 end if;
13215
13216 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13217
13218 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13219 Set_Corresponding_Record_Type (Def_Id,
13220 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13221
13222 else
13223 -- If there is no associated record, expansion is disabled and this
13224 -- is a generic context. Create a subtype in any case, so that
13225 -- semantic analysis can proceed.
13226
13227 if No (Def_Id) then
13228 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13229 end if;
13230
13231 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13232 end if;
13233 end Constrain_Concurrent;
13234
13235 ------------------------------------
13236 -- Constrain_Corresponding_Record --
13237 ------------------------------------
13238
13239 function Constrain_Corresponding_Record
13240 (Prot_Subt : Entity_Id;
13241 Corr_Rec : Entity_Id;
13242 Related_Nod : Node_Id) return Entity_Id
13243 is
13244 T_Sub : constant Entity_Id :=
13245 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13246
13247 begin
13248 Set_Etype (T_Sub, Corr_Rec);
13249 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13250 Set_Is_Constrained (T_Sub, True);
13251 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13252 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13253
13254 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13255 Set_Discriminant_Constraint
13256 (T_Sub, Discriminant_Constraint (Prot_Subt));
13257 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13258 Create_Constrained_Components
13259 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13260 end if;
13261
13262 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13263
13264 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13265 Conditional_Delay (T_Sub, Corr_Rec);
13266
13267 else
13268 -- This is a component subtype: it will be frozen in the context of
13269 -- the enclosing record's init_proc, so that discriminant references
13270 -- are resolved to discriminals. (Note: we used to skip freezing
13271 -- altogether in that case, which caused errors downstream for
13272 -- components of a bit packed array type).
13273
13274 Set_Has_Delayed_Freeze (T_Sub);
13275 end if;
13276
13277 return T_Sub;
13278 end Constrain_Corresponding_Record;
13279
13280 -----------------------
13281 -- Constrain_Decimal --
13282 -----------------------
13283
13284 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13285 T : constant Entity_Id := Entity (Subtype_Mark (S));
13286 C : constant Node_Id := Constraint (S);
13287 Loc : constant Source_Ptr := Sloc (C);
13288 Range_Expr : Node_Id;
13289 Digits_Expr : Node_Id;
13290 Digits_Val : Uint;
13291 Bound_Val : Ureal;
13292
13293 begin
13294 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13295
13296 if Nkind (C) = N_Range_Constraint then
13297 Range_Expr := Range_Expression (C);
13298 Digits_Val := Digits_Value (T);
13299
13300 else
13301 pragma Assert (Nkind (C) = N_Digits_Constraint);
13302
13303 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13304
13305 Digits_Expr := Digits_Expression (C);
13306 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13307
13308 Check_Digits_Expression (Digits_Expr);
13309 Digits_Val := Expr_Value (Digits_Expr);
13310
13311 if Digits_Val > Digits_Value (T) then
13312 Error_Msg_N
13313 ("digits expression is incompatible with subtype", C);
13314 Digits_Val := Digits_Value (T);
13315 end if;
13316
13317 if Present (Range_Constraint (C)) then
13318 Range_Expr := Range_Expression (Range_Constraint (C));
13319 else
13320 Range_Expr := Empty;
13321 end if;
13322 end if;
13323
13324 Set_Etype (Def_Id, Base_Type (T));
13325 Set_Size_Info (Def_Id, (T));
13326 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13327 Set_Delta_Value (Def_Id, Delta_Value (T));
13328 Set_Scale_Value (Def_Id, Scale_Value (T));
13329 Set_Small_Value (Def_Id, Small_Value (T));
13330 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13331 Set_Digits_Value (Def_Id, Digits_Val);
13332
13333 -- Manufacture range from given digits value if no range present
13334
13335 if No (Range_Expr) then
13336 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13337 Range_Expr :=
13338 Make_Range (Loc,
13339 Low_Bound =>
13340 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13341 High_Bound =>
13342 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13343 end if;
13344
13345 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13346 Set_Discrete_RM_Size (Def_Id);
13347
13348 -- Unconditionally delay the freeze, since we cannot set size
13349 -- information in all cases correctly until the freeze point.
13350
13351 Set_Has_Delayed_Freeze (Def_Id);
13352 end Constrain_Decimal;
13353
13354 ----------------------------------
13355 -- Constrain_Discriminated_Type --
13356 ----------------------------------
13357
13358 procedure Constrain_Discriminated_Type
13359 (Def_Id : Entity_Id;
13360 S : Node_Id;
13361 Related_Nod : Node_Id;
13362 For_Access : Boolean := False)
13363 is
13364 E : Entity_Id := Entity (Subtype_Mark (S));
13365 T : Entity_Id;
13366
13367 procedure Fixup_Bad_Constraint;
13368 -- Called after finding a bad constraint, and after having posted an
13369 -- appropriate error message. The goal is to leave type Def_Id in as
13370 -- reasonable state as possible.
13371
13372 --------------------------
13373 -- Fixup_Bad_Constraint --
13374 --------------------------
13375
13376 procedure Fixup_Bad_Constraint is
13377 begin
13378 -- Set a reasonable Ekind for the entity. For an incomplete type,
13379 -- we can't do much, but for other types, we can set the proper
13380 -- corresponding subtype kind.
13381
13382 if Ekind (T) = E_Incomplete_Type then
13383 Set_Ekind (Def_Id, Ekind (T));
13384 else
13385 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13386 end if;
13387
13388 -- Set Etype to the known type, to reduce chances of cascaded errors
13389
13390 Set_Etype (Def_Id, E);
13391 Set_Error_Posted (Def_Id);
13392 end Fixup_Bad_Constraint;
13393
13394 -- Local variables
13395
13396 C : Node_Id;
13397 Constr : Elist_Id := New_Elmt_List;
13398
13399 -- Start of processing for Constrain_Discriminated_Type
13400
13401 begin
13402 C := Constraint (S);
13403
13404 -- A discriminant constraint is only allowed in a subtype indication,
13405 -- after a subtype mark. This subtype mark must denote either a type
13406 -- with discriminants, or an access type whose designated type is a
13407 -- type with discriminants. A discriminant constraint specifies the
13408 -- values of these discriminants (RM 3.7.2(5)).
13409
13410 T := Base_Type (Entity (Subtype_Mark (S)));
13411
13412 if Is_Access_Type (T) then
13413 T := Designated_Type (T);
13414 end if;
13415
13416 -- In an instance it may be necessary to retrieve the full view of a
13417 -- type with unknown discriminants, or a full view with defaulted
13418 -- discriminants. In other contexts the constraint is illegal.
13419
13420 if In_Instance
13421 and then Is_Private_Type (T)
13422 and then Present (Full_View (T))
13423 and then
13424 (Has_Unknown_Discriminants (T)
13425 or else
13426 (not Has_Discriminants (T)
13427 and then Has_Discriminants (Full_View (T))
13428 and then Present (Discriminant_Default_Value
13429 (First_Discriminant (Full_View (T))))))
13430 then
13431 T := Full_View (T);
13432 E := Full_View (E);
13433 end if;
13434
13435 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13436 -- generating an error for access-to-incomplete subtypes.
13437
13438 if Ada_Version >= Ada_2005
13439 and then Ekind (T) = E_Incomplete_Type
13440 and then Nkind (Parent (S)) = N_Subtype_Declaration
13441 and then not Is_Itype (Def_Id)
13442 then
13443 -- A little sanity check: emit an error message if the type has
13444 -- discriminants to begin with. Type T may be a regular incomplete
13445 -- type or imported via a limited with clause.
13446
13447 if Has_Discriminants (T)
13448 or else (From_Limited_With (T)
13449 and then Present (Non_Limited_View (T))
13450 and then Nkind (Parent (Non_Limited_View (T))) =
13451 N_Full_Type_Declaration
13452 and then Present (Discriminant_Specifications
13453 (Parent (Non_Limited_View (T)))))
13454 then
13455 Error_Msg_N
13456 ("(Ada 2005) incomplete subtype may not be constrained", C);
13457 else
13458 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13459 end if;
13460
13461 Fixup_Bad_Constraint;
13462 return;
13463
13464 -- Check that the type has visible discriminants. The type may be
13465 -- a private type with unknown discriminants whose full view has
13466 -- discriminants which are invisible.
13467
13468 elsif not Has_Discriminants (T)
13469 or else
13470 (Has_Unknown_Discriminants (T)
13471 and then Is_Private_Type (T))
13472 then
13473 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13474 Fixup_Bad_Constraint;
13475 return;
13476
13477 elsif Is_Constrained (E)
13478 or else (Ekind (E) = E_Class_Wide_Subtype
13479 and then Present (Discriminant_Constraint (E)))
13480 then
13481 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13482 Fixup_Bad_Constraint;
13483 return;
13484 end if;
13485
13486 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13487 -- applies to the base type.
13488
13489 T := Base_Type (T);
13490
13491 Constr := Build_Discriminant_Constraints (T, S);
13492
13493 -- If the list returned was empty we had an error in building the
13494 -- discriminant constraint. We have also already signalled an error
13495 -- in the incomplete type case
13496
13497 if Is_Empty_Elmt_List (Constr) then
13498 Fixup_Bad_Constraint;
13499 return;
13500 end if;
13501
13502 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13503 end Constrain_Discriminated_Type;
13504
13505 ---------------------------
13506 -- Constrain_Enumeration --
13507 ---------------------------
13508
13509 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13510 T : constant Entity_Id := Entity (Subtype_Mark (S));
13511 C : constant Node_Id := Constraint (S);
13512
13513 begin
13514 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13515
13516 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13517
13518 Set_Etype (Def_Id, Base_Type (T));
13519 Set_Size_Info (Def_Id, (T));
13520 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13521 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13522
13523 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13524
13525 Set_Discrete_RM_Size (Def_Id);
13526 end Constrain_Enumeration;
13527
13528 ----------------------
13529 -- Constrain_Float --
13530 ----------------------
13531
13532 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13533 T : constant Entity_Id := Entity (Subtype_Mark (S));
13534 C : Node_Id;
13535 D : Node_Id;
13536 Rais : Node_Id;
13537
13538 begin
13539 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13540
13541 Set_Etype (Def_Id, Base_Type (T));
13542 Set_Size_Info (Def_Id, (T));
13543 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13544
13545 -- Process the constraint
13546
13547 C := Constraint (S);
13548
13549 -- Digits constraint present
13550
13551 if Nkind (C) = N_Digits_Constraint then
13552
13553 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13554 Check_Restriction (No_Obsolescent_Features, C);
13555
13556 if Warn_On_Obsolescent_Feature then
13557 Error_Msg_N
13558 ("subtype digits constraint is an " &
13559 "obsolescent feature (RM J.3(8))?j?", C);
13560 end if;
13561
13562 D := Digits_Expression (C);
13563 Analyze_And_Resolve (D, Any_Integer);
13564 Check_Digits_Expression (D);
13565 Set_Digits_Value (Def_Id, Expr_Value (D));
13566
13567 -- Check that digits value is in range. Obviously we can do this
13568 -- at compile time, but it is strictly a runtime check, and of
13569 -- course there is an ACVC test that checks this.
13570
13571 if Digits_Value (Def_Id) > Digits_Value (T) then
13572 Error_Msg_Uint_1 := Digits_Value (T);
13573 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13574 Rais :=
13575 Make_Raise_Constraint_Error (Sloc (D),
13576 Reason => CE_Range_Check_Failed);
13577 Insert_Action (Declaration_Node (Def_Id), Rais);
13578 end if;
13579
13580 C := Range_Constraint (C);
13581
13582 -- No digits constraint present
13583
13584 else
13585 Set_Digits_Value (Def_Id, Digits_Value (T));
13586 end if;
13587
13588 -- Range constraint present
13589
13590 if Nkind (C) = N_Range_Constraint then
13591 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13592
13593 -- No range constraint present
13594
13595 else
13596 pragma Assert (No (C));
13597 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13598 end if;
13599
13600 Set_Is_Constrained (Def_Id);
13601 end Constrain_Float;
13602
13603 ---------------------
13604 -- Constrain_Index --
13605 ---------------------
13606
13607 procedure Constrain_Index
13608 (Index : Node_Id;
13609 S : Node_Id;
13610 Related_Nod : Node_Id;
13611 Related_Id : Entity_Id;
13612 Suffix : Character;
13613 Suffix_Index : Nat)
13614 is
13615 Def_Id : Entity_Id;
13616 R : Node_Id := Empty;
13617 T : constant Entity_Id := Etype (Index);
13618
13619 begin
13620 Def_Id :=
13621 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13622 Set_Etype (Def_Id, Base_Type (T));
13623
13624 if Nkind (S) = N_Range
13625 or else
13626 (Nkind (S) = N_Attribute_Reference
13627 and then Attribute_Name (S) = Name_Range)
13628 then
13629 -- A Range attribute will be transformed into N_Range by Resolve
13630
13631 Analyze (S);
13632 Set_Etype (S, T);
13633 R := S;
13634
13635 Process_Range_Expr_In_Decl (R, T);
13636
13637 if not Error_Posted (S)
13638 and then
13639 (Nkind (S) /= N_Range
13640 or else not Covers (T, (Etype (Low_Bound (S))))
13641 or else not Covers (T, (Etype (High_Bound (S)))))
13642 then
13643 if Base_Type (T) /= Any_Type
13644 and then Etype (Low_Bound (S)) /= Any_Type
13645 and then Etype (High_Bound (S)) /= Any_Type
13646 then
13647 Error_Msg_N ("range expected", S);
13648 end if;
13649 end if;
13650
13651 elsif Nkind (S) = N_Subtype_Indication then
13652
13653 -- The parser has verified that this is a discrete indication
13654
13655 Resolve_Discrete_Subtype_Indication (S, T);
13656 Bad_Predicated_Subtype_Use
13657 ("subtype& has predicate, not allowed in index constraint",
13658 S, Entity (Subtype_Mark (S)));
13659
13660 R := Range_Expression (Constraint (S));
13661
13662 -- Capture values of bounds and generate temporaries for them if
13663 -- needed, since checks may cause duplication of the expressions
13664 -- which must not be reevaluated.
13665
13666 -- The forced evaluation removes side effects from expressions, which
13667 -- should occur also in GNATprove mode. Otherwise, we end up with
13668 -- unexpected insertions of actions at places where this is not
13669 -- supposed to occur, e.g. on default parameters of a call.
13670
13671 if Expander_Active or GNATprove_Mode then
13672 Force_Evaluation
13673 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13674 Force_Evaluation
13675 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13676 end if;
13677
13678 elsif Nkind (S) = N_Discriminant_Association then
13679
13680 -- Syntactically valid in subtype indication
13681
13682 Error_Msg_N ("invalid index constraint", S);
13683 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13684 return;
13685
13686 -- Subtype_Mark case, no anonymous subtypes to construct
13687
13688 else
13689 Analyze (S);
13690
13691 if Is_Entity_Name (S) then
13692 if not Is_Type (Entity (S)) then
13693 Error_Msg_N ("expect subtype mark for index constraint", S);
13694
13695 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13696 Wrong_Type (S, Base_Type (T));
13697
13698 -- Check error of subtype with predicate in index constraint
13699
13700 else
13701 Bad_Predicated_Subtype_Use
13702 ("subtype& has predicate, not allowed in index constraint",
13703 S, Entity (S));
13704 end if;
13705
13706 return;
13707
13708 else
13709 Error_Msg_N ("invalid index constraint", S);
13710 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13711 return;
13712 end if;
13713 end if;
13714
13715 -- Complete construction of the Itype
13716
13717 if Is_Modular_Integer_Type (T) then
13718 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13719
13720 elsif Is_Integer_Type (T) then
13721 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13722
13723 else
13724 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13725 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13726 Set_First_Literal (Def_Id, First_Literal (T));
13727 end if;
13728
13729 Set_Size_Info (Def_Id, (T));
13730 Set_RM_Size (Def_Id, RM_Size (T));
13731 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13732
13733 Set_Scalar_Range (Def_Id, R);
13734
13735 Set_Etype (S, Def_Id);
13736 Set_Discrete_RM_Size (Def_Id);
13737 end Constrain_Index;
13738
13739 -----------------------
13740 -- Constrain_Integer --
13741 -----------------------
13742
13743 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13744 T : constant Entity_Id := Entity (Subtype_Mark (S));
13745 C : constant Node_Id := Constraint (S);
13746
13747 begin
13748 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13749
13750 if Is_Modular_Integer_Type (T) then
13751 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13752 else
13753 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13754 end if;
13755
13756 Set_Etype (Def_Id, Base_Type (T));
13757 Set_Size_Info (Def_Id, (T));
13758 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13759 Set_Discrete_RM_Size (Def_Id);
13760 end Constrain_Integer;
13761
13762 ------------------------------
13763 -- Constrain_Ordinary_Fixed --
13764 ------------------------------
13765
13766 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13767 T : constant Entity_Id := Entity (Subtype_Mark (S));
13768 C : Node_Id;
13769 D : Node_Id;
13770 Rais : Node_Id;
13771
13772 begin
13773 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13774 Set_Etype (Def_Id, Base_Type (T));
13775 Set_Size_Info (Def_Id, (T));
13776 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13777 Set_Small_Value (Def_Id, Small_Value (T));
13778
13779 -- Process the constraint
13780
13781 C := Constraint (S);
13782
13783 -- Delta constraint present
13784
13785 if Nkind (C) = N_Delta_Constraint then
13786
13787 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13788 Check_Restriction (No_Obsolescent_Features, C);
13789
13790 if Warn_On_Obsolescent_Feature then
13791 Error_Msg_S
13792 ("subtype delta constraint is an " &
13793 "obsolescent feature (RM J.3(7))?j?");
13794 end if;
13795
13796 D := Delta_Expression (C);
13797 Analyze_And_Resolve (D, Any_Real);
13798 Check_Delta_Expression (D);
13799 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13800
13801 -- Check that delta value is in range. Obviously we can do this
13802 -- at compile time, but it is strictly a runtime check, and of
13803 -- course there is an ACVC test that checks this.
13804
13805 if Delta_Value (Def_Id) < Delta_Value (T) then
13806 Error_Msg_N ("??delta value is too small", D);
13807 Rais :=
13808 Make_Raise_Constraint_Error (Sloc (D),
13809 Reason => CE_Range_Check_Failed);
13810 Insert_Action (Declaration_Node (Def_Id), Rais);
13811 end if;
13812
13813 C := Range_Constraint (C);
13814
13815 -- No delta constraint present
13816
13817 else
13818 Set_Delta_Value (Def_Id, Delta_Value (T));
13819 end if;
13820
13821 -- Range constraint present
13822
13823 if Nkind (C) = N_Range_Constraint then
13824 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13825
13826 -- No range constraint present
13827
13828 else
13829 pragma Assert (No (C));
13830 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13831 end if;
13832
13833 Set_Discrete_RM_Size (Def_Id);
13834
13835 -- Unconditionally delay the freeze, since we cannot set size
13836 -- information in all cases correctly until the freeze point.
13837
13838 Set_Has_Delayed_Freeze (Def_Id);
13839 end Constrain_Ordinary_Fixed;
13840
13841 -----------------------
13842 -- Contain_Interface --
13843 -----------------------
13844
13845 function Contain_Interface
13846 (Iface : Entity_Id;
13847 Ifaces : Elist_Id) return Boolean
13848 is
13849 Iface_Elmt : Elmt_Id;
13850
13851 begin
13852 if Present (Ifaces) then
13853 Iface_Elmt := First_Elmt (Ifaces);
13854 while Present (Iface_Elmt) loop
13855 if Node (Iface_Elmt) = Iface then
13856 return True;
13857 end if;
13858
13859 Next_Elmt (Iface_Elmt);
13860 end loop;
13861 end if;
13862
13863 return False;
13864 end Contain_Interface;
13865
13866 ---------------------------
13867 -- Convert_Scalar_Bounds --
13868 ---------------------------
13869
13870 procedure Convert_Scalar_Bounds
13871 (N : Node_Id;
13872 Parent_Type : Entity_Id;
13873 Derived_Type : Entity_Id;
13874 Loc : Source_Ptr)
13875 is
13876 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13877
13878 Lo : Node_Id;
13879 Hi : Node_Id;
13880 Rng : Node_Id;
13881
13882 begin
13883 -- Defend against previous errors
13884
13885 if No (Scalar_Range (Derived_Type)) then
13886 Check_Error_Detected;
13887 return;
13888 end if;
13889
13890 Lo := Build_Scalar_Bound
13891 (Type_Low_Bound (Derived_Type),
13892 Parent_Type, Implicit_Base);
13893
13894 Hi := Build_Scalar_Bound
13895 (Type_High_Bound (Derived_Type),
13896 Parent_Type, Implicit_Base);
13897
13898 Rng :=
13899 Make_Range (Loc,
13900 Low_Bound => Lo,
13901 High_Bound => Hi);
13902
13903 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13904
13905 Set_Parent (Rng, N);
13906 Set_Scalar_Range (Derived_Type, Rng);
13907
13908 -- Analyze the bounds
13909
13910 Analyze_And_Resolve (Lo, Implicit_Base);
13911 Analyze_And_Resolve (Hi, Implicit_Base);
13912
13913 -- Analyze the range itself, except that we do not analyze it if
13914 -- the bounds are real literals, and we have a fixed-point type.
13915 -- The reason for this is that we delay setting the bounds in this
13916 -- case till we know the final Small and Size values (see circuit
13917 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13918
13919 if Is_Fixed_Point_Type (Parent_Type)
13920 and then Nkind (Lo) = N_Real_Literal
13921 and then Nkind (Hi) = N_Real_Literal
13922 then
13923 return;
13924
13925 -- Here we do the analysis of the range
13926
13927 -- Note: we do this manually, since if we do a normal Analyze and
13928 -- Resolve call, there are problems with the conversions used for
13929 -- the derived type range.
13930
13931 else
13932 Set_Etype (Rng, Implicit_Base);
13933 Set_Analyzed (Rng, True);
13934 end if;
13935 end Convert_Scalar_Bounds;
13936
13937 -------------------
13938 -- Copy_And_Swap --
13939 -------------------
13940
13941 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13942 begin
13943 -- Initialize new full declaration entity by copying the pertinent
13944 -- fields of the corresponding private declaration entity.
13945
13946 -- We temporarily set Ekind to a value appropriate for a type to
13947 -- avoid assert failures in Einfo from checking for setting type
13948 -- attributes on something that is not a type. Ekind (Priv) is an
13949 -- appropriate choice, since it allowed the attributes to be set
13950 -- in the first place. This Ekind value will be modified later.
13951
13952 Set_Ekind (Full, Ekind (Priv));
13953
13954 -- Also set Etype temporarily to Any_Type, again, in the absence
13955 -- of errors, it will be properly reset, and if there are errors,
13956 -- then we want a value of Any_Type to remain.
13957
13958 Set_Etype (Full, Any_Type);
13959
13960 -- Now start copying attributes
13961
13962 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13963
13964 if Has_Discriminants (Full) then
13965 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13966 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13967 end if;
13968
13969 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13970 Set_Homonym (Full, Homonym (Priv));
13971 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13972 Set_Is_Public (Full, Is_Public (Priv));
13973 Set_Is_Pure (Full, Is_Pure (Priv));
13974 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13975 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13976 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13977 Set_Has_Pragma_Unreferenced_Objects
13978 (Full, Has_Pragma_Unreferenced_Objects
13979 (Priv));
13980
13981 Conditional_Delay (Full, Priv);
13982
13983 if Is_Tagged_Type (Full) then
13984 Set_Direct_Primitive_Operations
13985 (Full, Direct_Primitive_Operations (Priv));
13986 Set_No_Tagged_Streams_Pragma
13987 (Full, No_Tagged_Streams_Pragma (Priv));
13988
13989 if Is_Base_Type (Priv) then
13990 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13991 end if;
13992 end if;
13993
13994 Set_Is_Volatile (Full, Is_Volatile (Priv));
13995 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13996 Set_Scope (Full, Scope (Priv));
13997 Set_Next_Entity (Full, Next_Entity (Priv));
13998 Set_First_Entity (Full, First_Entity (Priv));
13999 Set_Last_Entity (Full, Last_Entity (Priv));
14000
14001 -- If access types have been recorded for later handling, keep them in
14002 -- the full view so that they get handled when the full view freeze
14003 -- node is expanded.
14004
14005 if Present (Freeze_Node (Priv))
14006 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14007 then
14008 Ensure_Freeze_Node (Full);
14009 Set_Access_Types_To_Process
14010 (Freeze_Node (Full),
14011 Access_Types_To_Process (Freeze_Node (Priv)));
14012 end if;
14013
14014 -- Swap the two entities. Now Private is the full type entity and Full
14015 -- is the private one. They will be swapped back at the end of the
14016 -- private part. This swapping ensures that the entity that is visible
14017 -- in the private part is the full declaration.
14018
14019 Exchange_Entities (Priv, Full);
14020 Append_Entity (Full, Scope (Full));
14021 end Copy_And_Swap;
14022
14023 -------------------------------------
14024 -- Copy_Array_Base_Type_Attributes --
14025 -------------------------------------
14026
14027 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14028 begin
14029 Set_Component_Alignment (T1, Component_Alignment (T2));
14030 Set_Component_Type (T1, Component_Type (T2));
14031 Set_Component_Size (T1, Component_Size (T2));
14032 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14033 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14034 Propagate_Concurrent_Flags (T1, T2);
14035 Set_Is_Packed (T1, Is_Packed (T2));
14036 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14037 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14038 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14039 end Copy_Array_Base_Type_Attributes;
14040
14041 -----------------------------------
14042 -- Copy_Array_Subtype_Attributes --
14043 -----------------------------------
14044
14045 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14046 begin
14047 Set_Size_Info (T1, T2);
14048
14049 Set_First_Index (T1, First_Index (T2));
14050 Set_Is_Aliased (T1, Is_Aliased (T2));
14051 Set_Is_Volatile (T1, Is_Volatile (T2));
14052 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14053 Set_Is_Constrained (T1, Is_Constrained (T2));
14054 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14055 Inherit_Rep_Item_Chain (T1, T2);
14056 Set_Convention (T1, Convention (T2));
14057 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14058 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14059 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14060 end Copy_Array_Subtype_Attributes;
14061
14062 -----------------------------------
14063 -- Create_Constrained_Components --
14064 -----------------------------------
14065
14066 procedure Create_Constrained_Components
14067 (Subt : Entity_Id;
14068 Decl_Node : Node_Id;
14069 Typ : Entity_Id;
14070 Constraints : Elist_Id)
14071 is
14072 Loc : constant Source_Ptr := Sloc (Subt);
14073 Comp_List : constant Elist_Id := New_Elmt_List;
14074 Parent_Type : constant Entity_Id := Etype (Typ);
14075 Assoc_List : constant List_Id := New_List;
14076 Discr_Val : Elmt_Id;
14077 Errors : Boolean;
14078 New_C : Entity_Id;
14079 Old_C : Entity_Id;
14080 Is_Static : Boolean := True;
14081
14082 procedure Collect_Fixed_Components (Typ : Entity_Id);
14083 -- Collect parent type components that do not appear in a variant part
14084
14085 procedure Create_All_Components;
14086 -- Iterate over Comp_List to create the components of the subtype
14087
14088 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14089 -- Creates a new component from Old_Compon, copying all the fields from
14090 -- it, including its Etype, inserts the new component in the Subt entity
14091 -- chain and returns the new component.
14092
14093 function Is_Variant_Record (T : Entity_Id) return Boolean;
14094 -- If true, and discriminants are static, collect only components from
14095 -- variants selected by discriminant values.
14096
14097 ------------------------------
14098 -- Collect_Fixed_Components --
14099 ------------------------------
14100
14101 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14102 begin
14103 -- Build association list for discriminants, and find components of the
14104 -- variant part selected by the values of the discriminants.
14105
14106 Old_C := First_Discriminant (Typ);
14107 Discr_Val := First_Elmt (Constraints);
14108 while Present (Old_C) loop
14109 Append_To (Assoc_List,
14110 Make_Component_Association (Loc,
14111 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14112 Expression => New_Copy (Node (Discr_Val))));
14113
14114 Next_Elmt (Discr_Val);
14115 Next_Discriminant (Old_C);
14116 end loop;
14117
14118 -- The tag and the possible parent component are unconditionally in
14119 -- the subtype.
14120
14121 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14122 Old_C := First_Component (Typ);
14123 while Present (Old_C) loop
14124 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14125 Append_Elmt (Old_C, Comp_List);
14126 end if;
14127
14128 Next_Component (Old_C);
14129 end loop;
14130 end if;
14131 end Collect_Fixed_Components;
14132
14133 ---------------------------
14134 -- Create_All_Components --
14135 ---------------------------
14136
14137 procedure Create_All_Components is
14138 Comp : Elmt_Id;
14139
14140 begin
14141 Comp := First_Elmt (Comp_List);
14142 while Present (Comp) loop
14143 Old_C := Node (Comp);
14144 New_C := Create_Component (Old_C);
14145
14146 Set_Etype
14147 (New_C,
14148 Constrain_Component_Type
14149 (Old_C, Subt, Decl_Node, Typ, Constraints));
14150 Set_Is_Public (New_C, Is_Public (Subt));
14151
14152 Next_Elmt (Comp);
14153 end loop;
14154 end Create_All_Components;
14155
14156 ----------------------
14157 -- Create_Component --
14158 ----------------------
14159
14160 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14161 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14162
14163 begin
14164 if Ekind (Old_Compon) = E_Discriminant
14165 and then Is_Completely_Hidden (Old_Compon)
14166 then
14167 -- This is a shadow discriminant created for a discriminant of
14168 -- the parent type, which needs to be present in the subtype.
14169 -- Give the shadow discriminant an internal name that cannot
14170 -- conflict with that of visible components.
14171
14172 Set_Chars (New_Compon, New_Internal_Name ('C'));
14173 end if;
14174
14175 -- Set the parent so we have a proper link for freezing etc. This is
14176 -- not a real parent pointer, since of course our parent does not own
14177 -- up to us and reference us, we are an illegitimate child of the
14178 -- original parent.
14179
14180 Set_Parent (New_Compon, Parent (Old_Compon));
14181
14182 -- If the old component's Esize was already determined and is a
14183 -- static value, then the new component simply inherits it. Otherwise
14184 -- the old component's size may require run-time determination, but
14185 -- the new component's size still might be statically determinable
14186 -- (if, for example it has a static constraint). In that case we want
14187 -- Layout_Type to recompute the component's size, so we reset its
14188 -- size and positional fields.
14189
14190 if Frontend_Layout_On_Target
14191 and then not Known_Static_Esize (Old_Compon)
14192 then
14193 Set_Esize (New_Compon, Uint_0);
14194 Init_Normalized_First_Bit (New_Compon);
14195 Init_Normalized_Position (New_Compon);
14196 Init_Normalized_Position_Max (New_Compon);
14197 end if;
14198
14199 -- We do not want this node marked as Comes_From_Source, since
14200 -- otherwise it would get first class status and a separate cross-
14201 -- reference line would be generated. Illegitimate children do not
14202 -- rate such recognition.
14203
14204 Set_Comes_From_Source (New_Compon, False);
14205
14206 -- But it is a real entity, and a birth certificate must be properly
14207 -- registered by entering it into the entity list.
14208
14209 Enter_Name (New_Compon);
14210
14211 return New_Compon;
14212 end Create_Component;
14213
14214 -----------------------
14215 -- Is_Variant_Record --
14216 -----------------------
14217
14218 function Is_Variant_Record (T : Entity_Id) return Boolean is
14219 begin
14220 return Nkind (Parent (T)) = N_Full_Type_Declaration
14221 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14222 and then Present (Component_List (Type_Definition (Parent (T))))
14223 and then
14224 Present
14225 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14226 end Is_Variant_Record;
14227
14228 -- Start of processing for Create_Constrained_Components
14229
14230 begin
14231 pragma Assert (Subt /= Base_Type (Subt));
14232 pragma Assert (Typ = Base_Type (Typ));
14233
14234 Set_First_Entity (Subt, Empty);
14235 Set_Last_Entity (Subt, Empty);
14236
14237 -- Check whether constraint is fully static, in which case we can
14238 -- optimize the list of components.
14239
14240 Discr_Val := First_Elmt (Constraints);
14241 while Present (Discr_Val) loop
14242 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14243 Is_Static := False;
14244 exit;
14245 end if;
14246
14247 Next_Elmt (Discr_Val);
14248 end loop;
14249
14250 Set_Has_Static_Discriminants (Subt, Is_Static);
14251
14252 Push_Scope (Subt);
14253
14254 -- Inherit the discriminants of the parent type
14255
14256 Add_Discriminants : declare
14257 Num_Disc : Nat;
14258 Num_Gird : Nat;
14259
14260 begin
14261 Num_Disc := 0;
14262 Old_C := First_Discriminant (Typ);
14263
14264 while Present (Old_C) loop
14265 Num_Disc := Num_Disc + 1;
14266 New_C := Create_Component (Old_C);
14267 Set_Is_Public (New_C, Is_Public (Subt));
14268 Next_Discriminant (Old_C);
14269 end loop;
14270
14271 -- For an untagged derived subtype, the number of discriminants may
14272 -- be smaller than the number of inherited discriminants, because
14273 -- several of them may be renamed by a single new discriminant or
14274 -- constrained. In this case, add the hidden discriminants back into
14275 -- the subtype, because they need to be present if the optimizer of
14276 -- the GCC 4.x back-end decides to break apart assignments between
14277 -- objects using the parent view into member-wise assignments.
14278
14279 Num_Gird := 0;
14280
14281 if Is_Derived_Type (Typ)
14282 and then not Is_Tagged_Type (Typ)
14283 then
14284 Old_C := First_Stored_Discriminant (Typ);
14285
14286 while Present (Old_C) loop
14287 Num_Gird := Num_Gird + 1;
14288 Next_Stored_Discriminant (Old_C);
14289 end loop;
14290 end if;
14291
14292 if Num_Gird > Num_Disc then
14293
14294 -- Find out multiple uses of new discriminants, and add hidden
14295 -- components for the extra renamed discriminants. We recognize
14296 -- multiple uses through the Corresponding_Discriminant of a
14297 -- new discriminant: if it constrains several old discriminants,
14298 -- this field points to the last one in the parent type. The
14299 -- stored discriminants of the derived type have the same name
14300 -- as those of the parent.
14301
14302 declare
14303 Constr : Elmt_Id;
14304 New_Discr : Entity_Id;
14305 Old_Discr : Entity_Id;
14306
14307 begin
14308 Constr := First_Elmt (Stored_Constraint (Typ));
14309 Old_Discr := First_Stored_Discriminant (Typ);
14310 while Present (Constr) loop
14311 if Is_Entity_Name (Node (Constr))
14312 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14313 then
14314 New_Discr := Entity (Node (Constr));
14315
14316 if Chars (Corresponding_Discriminant (New_Discr)) /=
14317 Chars (Old_Discr)
14318 then
14319 -- The new discriminant has been used to rename a
14320 -- subsequent old discriminant. Introduce a shadow
14321 -- component for the current old discriminant.
14322
14323 New_C := Create_Component (Old_Discr);
14324 Set_Original_Record_Component (New_C, Old_Discr);
14325 end if;
14326
14327 else
14328 -- The constraint has eliminated the old discriminant.
14329 -- Introduce a shadow component.
14330
14331 New_C := Create_Component (Old_Discr);
14332 Set_Original_Record_Component (New_C, Old_Discr);
14333 end if;
14334
14335 Next_Elmt (Constr);
14336 Next_Stored_Discriminant (Old_Discr);
14337 end loop;
14338 end;
14339 end if;
14340 end Add_Discriminants;
14341
14342 if Is_Static
14343 and then Is_Variant_Record (Typ)
14344 then
14345 Collect_Fixed_Components (Typ);
14346
14347 Gather_Components (
14348 Typ,
14349 Component_List (Type_Definition (Parent (Typ))),
14350 Governed_By => Assoc_List,
14351 Into => Comp_List,
14352 Report_Errors => Errors);
14353 pragma Assert (not Errors
14354 or else Serious_Errors_Detected > 0);
14355
14356 Create_All_Components;
14357
14358 -- If the subtype declaration is created for a tagged type derivation
14359 -- with constraints, we retrieve the record definition of the parent
14360 -- type to select the components of the proper variant.
14361
14362 elsif Is_Static
14363 and then Is_Tagged_Type (Typ)
14364 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14365 and then
14366 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14367 and then Is_Variant_Record (Parent_Type)
14368 then
14369 Collect_Fixed_Components (Typ);
14370
14371 Gather_Components
14372 (Typ,
14373 Component_List (Type_Definition (Parent (Parent_Type))),
14374 Governed_By => Assoc_List,
14375 Into => Comp_List,
14376 Report_Errors => Errors);
14377
14378 -- Note: previously there was a check at this point that no errors
14379 -- were detected. As a consequence of AI05-220 there may be an error
14380 -- if an inherited discriminant that controls a variant has a non-
14381 -- static constraint.
14382
14383 -- If the tagged derivation has a type extension, collect all the
14384 -- new components therein.
14385
14386 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14387 then
14388 Old_C := First_Component (Typ);
14389 while Present (Old_C) loop
14390 if Original_Record_Component (Old_C) = Old_C
14391 and then Chars (Old_C) /= Name_uTag
14392 and then Chars (Old_C) /= Name_uParent
14393 then
14394 Append_Elmt (Old_C, Comp_List);
14395 end if;
14396
14397 Next_Component (Old_C);
14398 end loop;
14399 end if;
14400
14401 Create_All_Components;
14402
14403 else
14404 -- If discriminants are not static, or if this is a multi-level type
14405 -- extension, we have to include all components of the parent type.
14406
14407 Old_C := First_Component (Typ);
14408 while Present (Old_C) loop
14409 New_C := Create_Component (Old_C);
14410
14411 Set_Etype
14412 (New_C,
14413 Constrain_Component_Type
14414 (Old_C, Subt, Decl_Node, Typ, Constraints));
14415 Set_Is_Public (New_C, Is_Public (Subt));
14416
14417 Next_Component (Old_C);
14418 end loop;
14419 end if;
14420
14421 End_Scope;
14422 end Create_Constrained_Components;
14423
14424 ------------------------------------------
14425 -- Decimal_Fixed_Point_Type_Declaration --
14426 ------------------------------------------
14427
14428 procedure Decimal_Fixed_Point_Type_Declaration
14429 (T : Entity_Id;
14430 Def : Node_Id)
14431 is
14432 Loc : constant Source_Ptr := Sloc (Def);
14433 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14434 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14435 Implicit_Base : Entity_Id;
14436 Digs_Val : Uint;
14437 Delta_Val : Ureal;
14438 Scale_Val : Uint;
14439 Bound_Val : Ureal;
14440
14441 begin
14442 Check_SPARK_05_Restriction
14443 ("decimal fixed point type is not allowed", Def);
14444 Check_Restriction (No_Fixed_Point, Def);
14445
14446 -- Create implicit base type
14447
14448 Implicit_Base :=
14449 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14450 Set_Etype (Implicit_Base, Implicit_Base);
14451
14452 -- Analyze and process delta expression
14453
14454 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14455
14456 Check_Delta_Expression (Delta_Expr);
14457 Delta_Val := Expr_Value_R (Delta_Expr);
14458
14459 -- Check delta is power of 10, and determine scale value from it
14460
14461 declare
14462 Val : Ureal;
14463
14464 begin
14465 Scale_Val := Uint_0;
14466 Val := Delta_Val;
14467
14468 if Val < Ureal_1 then
14469 while Val < Ureal_1 loop
14470 Val := Val * Ureal_10;
14471 Scale_Val := Scale_Val + 1;
14472 end loop;
14473
14474 if Scale_Val > 18 then
14475 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14476 Scale_Val := UI_From_Int (+18);
14477 end if;
14478
14479 else
14480 while Val > Ureal_1 loop
14481 Val := Val / Ureal_10;
14482 Scale_Val := Scale_Val - 1;
14483 end loop;
14484
14485 if Scale_Val < -18 then
14486 Error_Msg_N ("scale is less than minimum value of -18", Def);
14487 Scale_Val := UI_From_Int (-18);
14488 end if;
14489 end if;
14490
14491 if Val /= Ureal_1 then
14492 Error_Msg_N ("delta expression must be a power of 10", Def);
14493 Delta_Val := Ureal_10 ** (-Scale_Val);
14494 end if;
14495 end;
14496
14497 -- Set delta, scale and small (small = delta for decimal type)
14498
14499 Set_Delta_Value (Implicit_Base, Delta_Val);
14500 Set_Scale_Value (Implicit_Base, Scale_Val);
14501 Set_Small_Value (Implicit_Base, Delta_Val);
14502
14503 -- Analyze and process digits expression
14504
14505 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14506 Check_Digits_Expression (Digs_Expr);
14507 Digs_Val := Expr_Value (Digs_Expr);
14508
14509 if Digs_Val > 18 then
14510 Digs_Val := UI_From_Int (+18);
14511 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14512 end if;
14513
14514 Set_Digits_Value (Implicit_Base, Digs_Val);
14515 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14516
14517 -- Set range of base type from digits value for now. This will be
14518 -- expanded to represent the true underlying base range by Freeze.
14519
14520 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14521
14522 -- Note: We leave size as zero for now, size will be set at freeze
14523 -- time. We have to do this for ordinary fixed-point, because the size
14524 -- depends on the specified small, and we might as well do the same for
14525 -- decimal fixed-point.
14526
14527 pragma Assert (Esize (Implicit_Base) = Uint_0);
14528
14529 -- If there are bounds given in the declaration use them as the
14530 -- bounds of the first named subtype.
14531
14532 if Present (Real_Range_Specification (Def)) then
14533 declare
14534 RRS : constant Node_Id := Real_Range_Specification (Def);
14535 Low : constant Node_Id := Low_Bound (RRS);
14536 High : constant Node_Id := High_Bound (RRS);
14537 Low_Val : Ureal;
14538 High_Val : Ureal;
14539
14540 begin
14541 Analyze_And_Resolve (Low, Any_Real);
14542 Analyze_And_Resolve (High, Any_Real);
14543 Check_Real_Bound (Low);
14544 Check_Real_Bound (High);
14545 Low_Val := Expr_Value_R (Low);
14546 High_Val := Expr_Value_R (High);
14547
14548 if Low_Val < (-Bound_Val) then
14549 Error_Msg_N
14550 ("range low bound too small for digits value", Low);
14551 Low_Val := -Bound_Val;
14552 end if;
14553
14554 if High_Val > Bound_Val then
14555 Error_Msg_N
14556 ("range high bound too large for digits value", High);
14557 High_Val := Bound_Val;
14558 end if;
14559
14560 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14561 end;
14562
14563 -- If no explicit range, use range that corresponds to given
14564 -- digits value. This will end up as the final range for the
14565 -- first subtype.
14566
14567 else
14568 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14569 end if;
14570
14571 -- Complete entity for first subtype. The inheritance of the rep item
14572 -- chain ensures that SPARK-related pragmas are not clobbered when the
14573 -- decimal fixed point type acts as a full view of a private type.
14574
14575 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14576 Set_Etype (T, Implicit_Base);
14577 Set_Size_Info (T, Implicit_Base);
14578 Inherit_Rep_Item_Chain (T, Implicit_Base);
14579 Set_Digits_Value (T, Digs_Val);
14580 Set_Delta_Value (T, Delta_Val);
14581 Set_Small_Value (T, Delta_Val);
14582 Set_Scale_Value (T, Scale_Val);
14583 Set_Is_Constrained (T);
14584 end Decimal_Fixed_Point_Type_Declaration;
14585
14586 -----------------------------------
14587 -- Derive_Progenitor_Subprograms --
14588 -----------------------------------
14589
14590 procedure Derive_Progenitor_Subprograms
14591 (Parent_Type : Entity_Id;
14592 Tagged_Type : Entity_Id)
14593 is
14594 E : Entity_Id;
14595 Elmt : Elmt_Id;
14596 Iface : Entity_Id;
14597 Iface_Elmt : Elmt_Id;
14598 Iface_Subp : Entity_Id;
14599 New_Subp : Entity_Id := Empty;
14600 Prim_Elmt : Elmt_Id;
14601 Subp : Entity_Id;
14602 Typ : Entity_Id;
14603
14604 begin
14605 pragma Assert (Ada_Version >= Ada_2005
14606 and then Is_Record_Type (Tagged_Type)
14607 and then Is_Tagged_Type (Tagged_Type)
14608 and then Has_Interfaces (Tagged_Type));
14609
14610 -- Step 1: Transfer to the full-view primitives associated with the
14611 -- partial-view that cover interface primitives. Conceptually this
14612 -- work should be done later by Process_Full_View; done here to
14613 -- simplify its implementation at later stages. It can be safely
14614 -- done here because interfaces must be visible in the partial and
14615 -- private view (RM 7.3(7.3/2)).
14616
14617 -- Small optimization: This work is only required if the parent may
14618 -- have entities whose Alias attribute reference an interface primitive.
14619 -- Such a situation may occur if the parent is an abstract type and the
14620 -- primitive has not been yet overridden or if the parent is a generic
14621 -- formal type covering interfaces.
14622
14623 -- If the tagged type is not abstract, it cannot have abstract
14624 -- primitives (the only entities in the list of primitives of
14625 -- non-abstract tagged types that can reference abstract primitives
14626 -- through its Alias attribute are the internal entities that have
14627 -- attribute Interface_Alias, and these entities are generated later
14628 -- by Add_Internal_Interface_Entities).
14629
14630 if In_Private_Part (Current_Scope)
14631 and then (Is_Abstract_Type (Parent_Type)
14632 or else
14633 Is_Generic_Type (Parent_Type))
14634 then
14635 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14636 while Present (Elmt) loop
14637 Subp := Node (Elmt);
14638
14639 -- At this stage it is not possible to have entities in the list
14640 -- of primitives that have attribute Interface_Alias.
14641
14642 pragma Assert (No (Interface_Alias (Subp)));
14643
14644 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14645
14646 if Is_Interface (Typ) then
14647 E := Find_Primitive_Covering_Interface
14648 (Tagged_Type => Tagged_Type,
14649 Iface_Prim => Subp);
14650
14651 if Present (E)
14652 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14653 then
14654 Replace_Elmt (Elmt, E);
14655 Remove_Homonym (Subp);
14656 end if;
14657 end if;
14658
14659 Next_Elmt (Elmt);
14660 end loop;
14661 end if;
14662
14663 -- Step 2: Add primitives of progenitors that are not implemented by
14664 -- parents of Tagged_Type.
14665
14666 if Present (Interfaces (Base_Type (Tagged_Type))) then
14667 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14668 while Present (Iface_Elmt) loop
14669 Iface := Node (Iface_Elmt);
14670
14671 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14672 while Present (Prim_Elmt) loop
14673 Iface_Subp := Node (Prim_Elmt);
14674
14675 -- Exclude derivation of predefined primitives except those
14676 -- that come from source, or are inherited from one that comes
14677 -- from source. Required to catch declarations of equality
14678 -- operators of interfaces. For example:
14679
14680 -- type Iface is interface;
14681 -- function "=" (Left, Right : Iface) return Boolean;
14682
14683 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14684 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14685 then
14686 E := Find_Primitive_Covering_Interface
14687 (Tagged_Type => Tagged_Type,
14688 Iface_Prim => Iface_Subp);
14689
14690 -- If not found we derive a new primitive leaving its alias
14691 -- attribute referencing the interface primitive.
14692
14693 if No (E) then
14694 Derive_Subprogram
14695 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14696
14697 -- Ada 2012 (AI05-0197): If the covering primitive's name
14698 -- differs from the name of the interface primitive then it
14699 -- is a private primitive inherited from a parent type. In
14700 -- such case, given that Tagged_Type covers the interface,
14701 -- the inherited private primitive becomes visible. For such
14702 -- purpose we add a new entity that renames the inherited
14703 -- private primitive.
14704
14705 elsif Chars (E) /= Chars (Iface_Subp) then
14706 pragma Assert (Has_Suffix (E, 'P'));
14707 Derive_Subprogram
14708 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14709 Set_Alias (New_Subp, E);
14710 Set_Is_Abstract_Subprogram (New_Subp,
14711 Is_Abstract_Subprogram (E));
14712
14713 -- Propagate to the full view interface entities associated
14714 -- with the partial view.
14715
14716 elsif In_Private_Part (Current_Scope)
14717 and then Present (Alias (E))
14718 and then Alias (E) = Iface_Subp
14719 and then
14720 List_Containing (Parent (E)) /=
14721 Private_Declarations
14722 (Specification
14723 (Unit_Declaration_Node (Current_Scope)))
14724 then
14725 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14726 end if;
14727 end if;
14728
14729 Next_Elmt (Prim_Elmt);
14730 end loop;
14731
14732 Next_Elmt (Iface_Elmt);
14733 end loop;
14734 end if;
14735 end Derive_Progenitor_Subprograms;
14736
14737 -----------------------
14738 -- Derive_Subprogram --
14739 -----------------------
14740
14741 procedure Derive_Subprogram
14742 (New_Subp : out Entity_Id;
14743 Parent_Subp : Entity_Id;
14744 Derived_Type : Entity_Id;
14745 Parent_Type : Entity_Id;
14746 Actual_Subp : Entity_Id := Empty)
14747 is
14748 Formal : Entity_Id;
14749 -- Formal parameter of parent primitive operation
14750
14751 Formal_Of_Actual : Entity_Id;
14752 -- Formal parameter of actual operation, when the derivation is to
14753 -- create a renaming for a primitive operation of an actual in an
14754 -- instantiation.
14755
14756 New_Formal : Entity_Id;
14757 -- Formal of inherited operation
14758
14759 Visible_Subp : Entity_Id := Parent_Subp;
14760
14761 function Is_Private_Overriding return Boolean;
14762 -- If Subp is a private overriding of a visible operation, the inherited
14763 -- operation derives from the overridden op (even though its body is the
14764 -- overriding one) and the inherited operation is visible now. See
14765 -- sem_disp to see the full details of the handling of the overridden
14766 -- subprogram, which is removed from the list of primitive operations of
14767 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14768 -- and used to diagnose abstract operations that need overriding in the
14769 -- derived type.
14770
14771 procedure Replace_Type (Id, New_Id : Entity_Id);
14772 -- When the type is an anonymous access type, create a new access type
14773 -- designating the derived type.
14774
14775 procedure Set_Derived_Name;
14776 -- This procedure sets the appropriate Chars name for New_Subp. This
14777 -- is normally just a copy of the parent name. An exception arises for
14778 -- type support subprograms, where the name is changed to reflect the
14779 -- name of the derived type, e.g. if type foo is derived from type bar,
14780 -- then a procedure barDA is derived with a name fooDA.
14781
14782 ---------------------------
14783 -- Is_Private_Overriding --
14784 ---------------------------
14785
14786 function Is_Private_Overriding return Boolean is
14787 Prev : Entity_Id;
14788
14789 begin
14790 -- If the parent is not a dispatching operation there is no
14791 -- need to investigate overridings
14792
14793 if not Is_Dispatching_Operation (Parent_Subp) then
14794 return False;
14795 end if;
14796
14797 -- The visible operation that is overridden is a homonym of the
14798 -- parent subprogram. We scan the homonym chain to find the one
14799 -- whose alias is the subprogram we are deriving.
14800
14801 Prev := Current_Entity (Parent_Subp);
14802 while Present (Prev) loop
14803 if Ekind (Prev) = Ekind (Parent_Subp)
14804 and then Alias (Prev) = Parent_Subp
14805 and then Scope (Parent_Subp) = Scope (Prev)
14806 and then not Is_Hidden (Prev)
14807 then
14808 Visible_Subp := Prev;
14809 return True;
14810 end if;
14811
14812 Prev := Homonym (Prev);
14813 end loop;
14814
14815 return False;
14816 end Is_Private_Overriding;
14817
14818 ------------------
14819 -- Replace_Type --
14820 ------------------
14821
14822 procedure Replace_Type (Id, New_Id : Entity_Id) is
14823 Id_Type : constant Entity_Id := Etype (Id);
14824 Acc_Type : Entity_Id;
14825 Par : constant Node_Id := Parent (Derived_Type);
14826
14827 begin
14828 -- When the type is an anonymous access type, create a new access
14829 -- type designating the derived type. This itype must be elaborated
14830 -- at the point of the derivation, not on subsequent calls that may
14831 -- be out of the proper scope for Gigi, so we insert a reference to
14832 -- it after the derivation.
14833
14834 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14835 declare
14836 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14837
14838 begin
14839 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14840 and then Present (Full_View (Desig_Typ))
14841 and then not Is_Private_Type (Parent_Type)
14842 then
14843 Desig_Typ := Full_View (Desig_Typ);
14844 end if;
14845
14846 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14847
14848 -- Ada 2005 (AI-251): Handle also derivations of abstract
14849 -- interface primitives.
14850
14851 or else (Is_Interface (Desig_Typ)
14852 and then not Is_Class_Wide_Type (Desig_Typ))
14853 then
14854 Acc_Type := New_Copy (Id_Type);
14855 Set_Etype (Acc_Type, Acc_Type);
14856 Set_Scope (Acc_Type, New_Subp);
14857
14858 -- Set size of anonymous access type. If we have an access
14859 -- to an unconstrained array, this is a fat pointer, so it
14860 -- is sizes at twice addtress size.
14861
14862 if Is_Array_Type (Desig_Typ)
14863 and then not Is_Constrained (Desig_Typ)
14864 then
14865 Init_Size (Acc_Type, 2 * System_Address_Size);
14866
14867 -- Other cases use a thin pointer
14868
14869 else
14870 Init_Size (Acc_Type, System_Address_Size);
14871 end if;
14872
14873 -- Set remaining characterstics of anonymous access type
14874
14875 Init_Alignment (Acc_Type);
14876 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14877
14878 Set_Etype (New_Id, Acc_Type);
14879 Set_Scope (New_Id, New_Subp);
14880
14881 -- Create a reference to it
14882
14883 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14884
14885 else
14886 Set_Etype (New_Id, Id_Type);
14887 end if;
14888 end;
14889
14890 -- In Ada2012, a formal may have an incomplete type but the type
14891 -- derivation that inherits the primitive follows the full view.
14892
14893 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14894 or else
14895 (Ekind (Id_Type) = E_Record_Type_With_Private
14896 and then Present (Full_View (Id_Type))
14897 and then
14898 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14899 or else
14900 (Ada_Version >= Ada_2012
14901 and then Ekind (Id_Type) = E_Incomplete_Type
14902 and then Full_View (Id_Type) = Parent_Type)
14903 then
14904 -- Constraint checks on formals are generated during expansion,
14905 -- based on the signature of the original subprogram. The bounds
14906 -- of the derived type are not relevant, and thus we can use
14907 -- the base type for the formals. However, the return type may be
14908 -- used in a context that requires that the proper static bounds
14909 -- be used (a case statement, for example) and for those cases
14910 -- we must use the derived type (first subtype), not its base.
14911
14912 -- If the derived_type_definition has no constraints, we know that
14913 -- the derived type has the same constraints as the first subtype
14914 -- of the parent, and we can also use it rather than its base,
14915 -- which can lead to more efficient code.
14916
14917 if Etype (Id) = Parent_Type then
14918 if Is_Scalar_Type (Parent_Type)
14919 and then
14920 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14921 then
14922 Set_Etype (New_Id, Derived_Type);
14923
14924 elsif Nkind (Par) = N_Full_Type_Declaration
14925 and then
14926 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14927 and then
14928 Is_Entity_Name
14929 (Subtype_Indication (Type_Definition (Par)))
14930 then
14931 Set_Etype (New_Id, Derived_Type);
14932
14933 else
14934 Set_Etype (New_Id, Base_Type (Derived_Type));
14935 end if;
14936
14937 else
14938 Set_Etype (New_Id, Base_Type (Derived_Type));
14939 end if;
14940
14941 else
14942 Set_Etype (New_Id, Etype (Id));
14943 end if;
14944 end Replace_Type;
14945
14946 ----------------------
14947 -- Set_Derived_Name --
14948 ----------------------
14949
14950 procedure Set_Derived_Name is
14951 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14952 begin
14953 if Nm = TSS_Null then
14954 Set_Chars (New_Subp, Chars (Parent_Subp));
14955 else
14956 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14957 end if;
14958 end Set_Derived_Name;
14959
14960 -- Start of processing for Derive_Subprogram
14961
14962 begin
14963 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14964 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14965
14966 -- Check whether the inherited subprogram is a private operation that
14967 -- should be inherited but not yet made visible. Such subprograms can
14968 -- become visible at a later point (e.g., the private part of a public
14969 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14970 -- following predicate is true, then this is not such a private
14971 -- operation and the subprogram simply inherits the name of the parent
14972 -- subprogram. Note the special check for the names of controlled
14973 -- operations, which are currently exempted from being inherited with
14974 -- a hidden name because they must be findable for generation of
14975 -- implicit run-time calls.
14976
14977 if not Is_Hidden (Parent_Subp)
14978 or else Is_Internal (Parent_Subp)
14979 or else Is_Private_Overriding
14980 or else Is_Internal_Name (Chars (Parent_Subp))
14981 or else (Is_Controlled (Parent_Type)
14982 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
14983 Name_Finalize,
14984 Name_Initialize))
14985 then
14986 Set_Derived_Name;
14987
14988 -- An inherited dispatching equality will be overridden by an internally
14989 -- generated one, or by an explicit one, so preserve its name and thus
14990 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14991 -- private operation it may become invisible if the full view has
14992 -- progenitors, and the dispatch table will be malformed.
14993 -- We check that the type is limited to handle the anomalous declaration
14994 -- of Limited_Controlled, which is derived from a non-limited type, and
14995 -- which is handled specially elsewhere as well.
14996
14997 elsif Chars (Parent_Subp) = Name_Op_Eq
14998 and then Is_Dispatching_Operation (Parent_Subp)
14999 and then Etype (Parent_Subp) = Standard_Boolean
15000 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15001 and then
15002 Etype (First_Formal (Parent_Subp)) =
15003 Etype (Next_Formal (First_Formal (Parent_Subp)))
15004 then
15005 Set_Derived_Name;
15006
15007 -- If parent is hidden, this can be a regular derivation if the
15008 -- parent is immediately visible in a non-instantiating context,
15009 -- or if we are in the private part of an instance. This test
15010 -- should still be refined ???
15011
15012 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15013 -- operation as a non-visible operation in cases where the parent
15014 -- subprogram might not be visible now, but was visible within the
15015 -- original generic, so it would be wrong to make the inherited
15016 -- subprogram non-visible now. (Not clear if this test is fully
15017 -- correct; are there any cases where we should declare the inherited
15018 -- operation as not visible to avoid it being overridden, e.g., when
15019 -- the parent type is a generic actual with private primitives ???)
15020
15021 -- (they should be treated the same as other private inherited
15022 -- subprograms, but it's not clear how to do this cleanly). ???
15023
15024 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15025 and then Is_Immediately_Visible (Parent_Subp)
15026 and then not In_Instance)
15027 or else In_Instance_Not_Visible
15028 then
15029 Set_Derived_Name;
15030
15031 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15032 -- overrides an interface primitive because interface primitives
15033 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15034
15035 elsif Ada_Version >= Ada_2005
15036 and then Is_Dispatching_Operation (Parent_Subp)
15037 and then Covers_Some_Interface (Parent_Subp)
15038 then
15039 Set_Derived_Name;
15040
15041 -- Otherwise, the type is inheriting a private operation, so enter it
15042 -- with a special name so it can't be overridden.
15043
15044 else
15045 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15046 end if;
15047
15048 Set_Parent (New_Subp, Parent (Derived_Type));
15049
15050 if Present (Actual_Subp) then
15051 Replace_Type (Actual_Subp, New_Subp);
15052 else
15053 Replace_Type (Parent_Subp, New_Subp);
15054 end if;
15055
15056 Conditional_Delay (New_Subp, Parent_Subp);
15057
15058 -- If we are creating a renaming for a primitive operation of an
15059 -- actual of a generic derived type, we must examine the signature
15060 -- of the actual primitive, not that of the generic formal, which for
15061 -- example may be an interface. However the name and initial value
15062 -- of the inherited operation are those of the formal primitive.
15063
15064 Formal := First_Formal (Parent_Subp);
15065
15066 if Present (Actual_Subp) then
15067 Formal_Of_Actual := First_Formal (Actual_Subp);
15068 else
15069 Formal_Of_Actual := Empty;
15070 end if;
15071
15072 while Present (Formal) loop
15073 New_Formal := New_Copy (Formal);
15074
15075 -- Normally we do not go copying parents, but in the case of
15076 -- formals, we need to link up to the declaration (which is the
15077 -- parameter specification), and it is fine to link up to the
15078 -- original formal's parameter specification in this case.
15079
15080 Set_Parent (New_Formal, Parent (Formal));
15081 Append_Entity (New_Formal, New_Subp);
15082
15083 if Present (Formal_Of_Actual) then
15084 Replace_Type (Formal_Of_Actual, New_Formal);
15085 Next_Formal (Formal_Of_Actual);
15086 else
15087 Replace_Type (Formal, New_Formal);
15088 end if;
15089
15090 Next_Formal (Formal);
15091 end loop;
15092
15093 -- If this derivation corresponds to a tagged generic actual, then
15094 -- primitive operations rename those of the actual. Otherwise the
15095 -- primitive operations rename those of the parent type, If the parent
15096 -- renames an intrinsic operator, so does the new subprogram. We except
15097 -- concatenation, which is always properly typed, and does not get
15098 -- expanded as other intrinsic operations.
15099
15100 if No (Actual_Subp) then
15101 if Is_Intrinsic_Subprogram (Parent_Subp) then
15102 Set_Is_Intrinsic_Subprogram (New_Subp);
15103
15104 if Present (Alias (Parent_Subp))
15105 and then Chars (Parent_Subp) /= Name_Op_Concat
15106 then
15107 Set_Alias (New_Subp, Alias (Parent_Subp));
15108 else
15109 Set_Alias (New_Subp, Parent_Subp);
15110 end if;
15111
15112 else
15113 Set_Alias (New_Subp, Parent_Subp);
15114 end if;
15115
15116 else
15117 Set_Alias (New_Subp, Actual_Subp);
15118 end if;
15119
15120 -- Derived subprograms of a tagged type must inherit the convention
15121 -- of the parent subprogram (a requirement of AI-117). Derived
15122 -- subprograms of untagged types simply get convention Ada by default.
15123
15124 -- If the derived type is a tagged generic formal type with unknown
15125 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15126
15127 -- However, if the type is derived from a generic formal, the further
15128 -- inherited subprogram has the convention of the non-generic ancestor.
15129 -- Otherwise there would be no way to override the operation.
15130 -- (This is subject to forthcoming ARG discussions).
15131
15132 if Is_Tagged_Type (Derived_Type) then
15133 if Is_Generic_Type (Derived_Type)
15134 and then Has_Unknown_Discriminants (Derived_Type)
15135 then
15136 Set_Convention (New_Subp, Convention_Intrinsic);
15137
15138 else
15139 if Is_Generic_Type (Parent_Type)
15140 and then Has_Unknown_Discriminants (Parent_Type)
15141 then
15142 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15143 else
15144 Set_Convention (New_Subp, Convention (Parent_Subp));
15145 end if;
15146 end if;
15147 end if;
15148
15149 -- Predefined controlled operations retain their name even if the parent
15150 -- is hidden (see above), but they are not primitive operations if the
15151 -- ancestor is not visible, for example if the parent is a private
15152 -- extension completed with a controlled extension. Note that a full
15153 -- type that is controlled can break privacy: the flag Is_Controlled is
15154 -- set on both views of the type.
15155
15156 if Is_Controlled (Parent_Type)
15157 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15158 Name_Adjust,
15159 Name_Finalize)
15160 and then Is_Hidden (Parent_Subp)
15161 and then not Is_Visibly_Controlled (Parent_Type)
15162 then
15163 Set_Is_Hidden (New_Subp);
15164 end if;
15165
15166 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15167 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15168
15169 if Ekind (Parent_Subp) = E_Procedure then
15170 Set_Is_Valued_Procedure
15171 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15172 else
15173 Set_Has_Controlling_Result
15174 (New_Subp, Has_Controlling_Result (Parent_Subp));
15175 end if;
15176
15177 -- No_Return must be inherited properly. If this is overridden in the
15178 -- case of a dispatching operation, then a check is made in Sem_Disp
15179 -- that the overriding operation is also No_Return (no such check is
15180 -- required for the case of non-dispatching operation.
15181
15182 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15183
15184 -- A derived function with a controlling result is abstract. If the
15185 -- Derived_Type is a nonabstract formal generic derived type, then
15186 -- inherited operations are not abstract: the required check is done at
15187 -- instantiation time. If the derivation is for a generic actual, the
15188 -- function is not abstract unless the actual is.
15189
15190 if Is_Generic_Type (Derived_Type)
15191 and then not Is_Abstract_Type (Derived_Type)
15192 then
15193 null;
15194
15195 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15196 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15197
15198 -- A subprogram subject to pragma Extensions_Visible with value False
15199 -- requires overriding if the subprogram has at least one controlling
15200 -- OUT parameter (SPARK RM 6.1.7(6)).
15201
15202 elsif Ada_Version >= Ada_2005
15203 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15204 or else (Is_Tagged_Type (Derived_Type)
15205 and then Etype (New_Subp) = Derived_Type
15206 and then not Is_Null_Extension (Derived_Type))
15207 or else (Is_Tagged_Type (Derived_Type)
15208 and then Ekind (Etype (New_Subp)) =
15209 E_Anonymous_Access_Type
15210 and then Designated_Type (Etype (New_Subp)) =
15211 Derived_Type
15212 and then not Is_Null_Extension (Derived_Type))
15213 or else (Comes_From_Source (Alias (New_Subp))
15214 and then Is_EVF_Procedure (Alias (New_Subp))))
15215 and then No (Actual_Subp)
15216 then
15217 if not Is_Tagged_Type (Derived_Type)
15218 or else Is_Abstract_Type (Derived_Type)
15219 or else Is_Abstract_Subprogram (Alias (New_Subp))
15220 then
15221 Set_Is_Abstract_Subprogram (New_Subp);
15222 else
15223 Set_Requires_Overriding (New_Subp);
15224 end if;
15225
15226 elsif Ada_Version < Ada_2005
15227 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15228 or else (Is_Tagged_Type (Derived_Type)
15229 and then Etype (New_Subp) = Derived_Type
15230 and then No (Actual_Subp)))
15231 then
15232 Set_Is_Abstract_Subprogram (New_Subp);
15233
15234 -- AI05-0097 : an inherited operation that dispatches on result is
15235 -- abstract if the derived type is abstract, even if the parent type
15236 -- is concrete and the derived type is a null extension.
15237
15238 elsif Has_Controlling_Result (Alias (New_Subp))
15239 and then Is_Abstract_Type (Etype (New_Subp))
15240 then
15241 Set_Is_Abstract_Subprogram (New_Subp);
15242
15243 -- Finally, if the parent type is abstract we must verify that all
15244 -- inherited operations are either non-abstract or overridden, or that
15245 -- the derived type itself is abstract (this check is performed at the
15246 -- end of a package declaration, in Check_Abstract_Overriding). A
15247 -- private overriding in the parent type will not be visible in the
15248 -- derivation if we are not in an inner package or in a child unit of
15249 -- the parent type, in which case the abstractness of the inherited
15250 -- operation is carried to the new subprogram.
15251
15252 elsif Is_Abstract_Type (Parent_Type)
15253 and then not In_Open_Scopes (Scope (Parent_Type))
15254 and then Is_Private_Overriding
15255 and then Is_Abstract_Subprogram (Visible_Subp)
15256 then
15257 if No (Actual_Subp) then
15258 Set_Alias (New_Subp, Visible_Subp);
15259 Set_Is_Abstract_Subprogram (New_Subp, True);
15260
15261 else
15262 -- If this is a derivation for an instance of a formal derived
15263 -- type, abstractness comes from the primitive operation of the
15264 -- actual, not from the operation inherited from the ancestor.
15265
15266 Set_Is_Abstract_Subprogram
15267 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15268 end if;
15269 end if;
15270
15271 New_Overloaded_Entity (New_Subp, Derived_Type);
15272
15273 -- Check for case of a derived subprogram for the instantiation of a
15274 -- formal derived tagged type, if so mark the subprogram as dispatching
15275 -- and inherit the dispatching attributes of the actual subprogram. The
15276 -- derived subprogram is effectively renaming of the actual subprogram,
15277 -- so it needs to have the same attributes as the actual.
15278
15279 if Present (Actual_Subp)
15280 and then Is_Dispatching_Operation (Actual_Subp)
15281 then
15282 Set_Is_Dispatching_Operation (New_Subp);
15283
15284 if Present (DTC_Entity (Actual_Subp)) then
15285 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15286 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15287 end if;
15288 end if;
15289
15290 -- Indicate that a derived subprogram does not require a body and that
15291 -- it does not require processing of default expressions.
15292
15293 Set_Has_Completion (New_Subp);
15294 Set_Default_Expressions_Processed (New_Subp);
15295
15296 if Ekind (New_Subp) = E_Function then
15297 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15298 end if;
15299 end Derive_Subprogram;
15300
15301 ------------------------
15302 -- Derive_Subprograms --
15303 ------------------------
15304
15305 procedure Derive_Subprograms
15306 (Parent_Type : Entity_Id;
15307 Derived_Type : Entity_Id;
15308 Generic_Actual : Entity_Id := Empty)
15309 is
15310 Op_List : constant Elist_Id :=
15311 Collect_Primitive_Operations (Parent_Type);
15312
15313 function Check_Derived_Type return Boolean;
15314 -- Check that all the entities derived from Parent_Type are found in
15315 -- the list of primitives of Derived_Type exactly in the same order.
15316
15317 procedure Derive_Interface_Subprogram
15318 (New_Subp : out Entity_Id;
15319 Subp : Entity_Id;
15320 Actual_Subp : Entity_Id);
15321 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15322 -- (which is an interface primitive). If Generic_Actual is present then
15323 -- Actual_Subp is the actual subprogram corresponding with the generic
15324 -- subprogram Subp.
15325
15326 ------------------------
15327 -- Check_Derived_Type --
15328 ------------------------
15329
15330 function Check_Derived_Type return Boolean is
15331 E : Entity_Id;
15332 Elmt : Elmt_Id;
15333 List : Elist_Id;
15334 New_Subp : Entity_Id;
15335 Op_Elmt : Elmt_Id;
15336 Subp : Entity_Id;
15337
15338 begin
15339 -- Traverse list of entities in the current scope searching for
15340 -- an incomplete type whose full-view is derived type.
15341
15342 E := First_Entity (Scope (Derived_Type));
15343 while Present (E) and then E /= Derived_Type loop
15344 if Ekind (E) = E_Incomplete_Type
15345 and then Present (Full_View (E))
15346 and then Full_View (E) = Derived_Type
15347 then
15348 -- Disable this test if Derived_Type completes an incomplete
15349 -- type because in such case more primitives can be added
15350 -- later to the list of primitives of Derived_Type by routine
15351 -- Process_Incomplete_Dependents
15352
15353 return True;
15354 end if;
15355
15356 E := Next_Entity (E);
15357 end loop;
15358
15359 List := Collect_Primitive_Operations (Derived_Type);
15360 Elmt := First_Elmt (List);
15361
15362 Op_Elmt := First_Elmt (Op_List);
15363 while Present (Op_Elmt) loop
15364 Subp := Node (Op_Elmt);
15365 New_Subp := Node (Elmt);
15366
15367 -- At this early stage Derived_Type has no entities with attribute
15368 -- Interface_Alias. In addition, such primitives are always
15369 -- located at the end of the list of primitives of Parent_Type.
15370 -- Therefore, if found we can safely stop processing pending
15371 -- entities.
15372
15373 exit when Present (Interface_Alias (Subp));
15374
15375 -- Handle hidden entities
15376
15377 if not Is_Predefined_Dispatching_Operation (Subp)
15378 and then Is_Hidden (Subp)
15379 then
15380 if Present (New_Subp)
15381 and then Primitive_Names_Match (Subp, New_Subp)
15382 then
15383 Next_Elmt (Elmt);
15384 end if;
15385
15386 else
15387 if not Present (New_Subp)
15388 or else Ekind (Subp) /= Ekind (New_Subp)
15389 or else not Primitive_Names_Match (Subp, New_Subp)
15390 then
15391 return False;
15392 end if;
15393
15394 Next_Elmt (Elmt);
15395 end if;
15396
15397 Next_Elmt (Op_Elmt);
15398 end loop;
15399
15400 return True;
15401 end Check_Derived_Type;
15402
15403 ---------------------------------
15404 -- Derive_Interface_Subprogram --
15405 ---------------------------------
15406
15407 procedure Derive_Interface_Subprogram
15408 (New_Subp : out Entity_Id;
15409 Subp : Entity_Id;
15410 Actual_Subp : Entity_Id)
15411 is
15412 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15413 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15414
15415 begin
15416 pragma Assert (Is_Interface (Iface_Type));
15417
15418 Derive_Subprogram
15419 (New_Subp => New_Subp,
15420 Parent_Subp => Iface_Subp,
15421 Derived_Type => Derived_Type,
15422 Parent_Type => Iface_Type,
15423 Actual_Subp => Actual_Subp);
15424
15425 -- Given that this new interface entity corresponds with a primitive
15426 -- of the parent that was not overridden we must leave it associated
15427 -- with its parent primitive to ensure that it will share the same
15428 -- dispatch table slot when overridden. We must set the Alias to Subp
15429 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15430 -- (in case we inherited Subp from Iface_Type via a nonabstract
15431 -- generic formal type).
15432
15433 if No (Actual_Subp) then
15434 Set_Alias (New_Subp, Subp);
15435
15436 declare
15437 T : Entity_Id := Find_Dispatching_Type (Subp);
15438 begin
15439 while Etype (T) /= T loop
15440 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15441 Set_Is_Abstract_Subprogram (New_Subp, False);
15442 exit;
15443 end if;
15444
15445 T := Etype (T);
15446 end loop;
15447 end;
15448
15449 -- For instantiations this is not needed since the previous call to
15450 -- Derive_Subprogram leaves the entity well decorated.
15451
15452 else
15453 pragma Assert (Alias (New_Subp) = Actual_Subp);
15454 null;
15455 end if;
15456 end Derive_Interface_Subprogram;
15457
15458 -- Local variables
15459
15460 Alias_Subp : Entity_Id;
15461 Act_List : Elist_Id;
15462 Act_Elmt : Elmt_Id;
15463 Act_Subp : Entity_Id := Empty;
15464 Elmt : Elmt_Id;
15465 Need_Search : Boolean := False;
15466 New_Subp : Entity_Id := Empty;
15467 Parent_Base : Entity_Id;
15468 Subp : Entity_Id;
15469
15470 -- Start of processing for Derive_Subprograms
15471
15472 begin
15473 if Ekind (Parent_Type) = E_Record_Type_With_Private
15474 and then Has_Discriminants (Parent_Type)
15475 and then Present (Full_View (Parent_Type))
15476 then
15477 Parent_Base := Full_View (Parent_Type);
15478 else
15479 Parent_Base := Parent_Type;
15480 end if;
15481
15482 if Present (Generic_Actual) then
15483 Act_List := Collect_Primitive_Operations (Generic_Actual);
15484 Act_Elmt := First_Elmt (Act_List);
15485 else
15486 Act_List := No_Elist;
15487 Act_Elmt := No_Elmt;
15488 end if;
15489
15490 -- Derive primitives inherited from the parent. Note that if the generic
15491 -- actual is present, this is not really a type derivation, it is a
15492 -- completion within an instance.
15493
15494 -- Case 1: Derived_Type does not implement interfaces
15495
15496 if not Is_Tagged_Type (Derived_Type)
15497 or else (not Has_Interfaces (Derived_Type)
15498 and then not (Present (Generic_Actual)
15499 and then Has_Interfaces (Generic_Actual)))
15500 then
15501 Elmt := First_Elmt (Op_List);
15502 while Present (Elmt) loop
15503 Subp := Node (Elmt);
15504
15505 -- Literals are derived earlier in the process of building the
15506 -- derived type, and are skipped here.
15507
15508 if Ekind (Subp) = E_Enumeration_Literal then
15509 null;
15510
15511 -- The actual is a direct descendant and the common primitive
15512 -- operations appear in the same order.
15513
15514 -- If the generic parent type is present, the derived type is an
15515 -- instance of a formal derived type, and within the instance its
15516 -- operations are those of the actual. We derive from the formal
15517 -- type but make the inherited operations aliases of the
15518 -- corresponding operations of the actual.
15519
15520 else
15521 pragma Assert (No (Node (Act_Elmt))
15522 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15523 and then
15524 Type_Conformant
15525 (Subp, Node (Act_Elmt),
15526 Skip_Controlling_Formals => True)));
15527
15528 Derive_Subprogram
15529 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15530
15531 if Present (Act_Elmt) then
15532 Next_Elmt (Act_Elmt);
15533 end if;
15534 end if;
15535
15536 Next_Elmt (Elmt);
15537 end loop;
15538
15539 -- Case 2: Derived_Type implements interfaces
15540
15541 else
15542 -- If the parent type has no predefined primitives we remove
15543 -- predefined primitives from the list of primitives of generic
15544 -- actual to simplify the complexity of this algorithm.
15545
15546 if Present (Generic_Actual) then
15547 declare
15548 Has_Predefined_Primitives : Boolean := False;
15549
15550 begin
15551 -- Check if the parent type has predefined primitives
15552
15553 Elmt := First_Elmt (Op_List);
15554 while Present (Elmt) loop
15555 Subp := Node (Elmt);
15556
15557 if Is_Predefined_Dispatching_Operation (Subp)
15558 and then not Comes_From_Source (Ultimate_Alias (Subp))
15559 then
15560 Has_Predefined_Primitives := True;
15561 exit;
15562 end if;
15563
15564 Next_Elmt (Elmt);
15565 end loop;
15566
15567 -- Remove predefined primitives of Generic_Actual. We must use
15568 -- an auxiliary list because in case of tagged types the value
15569 -- returned by Collect_Primitive_Operations is the value stored
15570 -- in its Primitive_Operations attribute (and we don't want to
15571 -- modify its current contents).
15572
15573 if not Has_Predefined_Primitives then
15574 declare
15575 Aux_List : constant Elist_Id := New_Elmt_List;
15576
15577 begin
15578 Elmt := First_Elmt (Act_List);
15579 while Present (Elmt) loop
15580 Subp := Node (Elmt);
15581
15582 if not Is_Predefined_Dispatching_Operation (Subp)
15583 or else Comes_From_Source (Subp)
15584 then
15585 Append_Elmt (Subp, Aux_List);
15586 end if;
15587
15588 Next_Elmt (Elmt);
15589 end loop;
15590
15591 Act_List := Aux_List;
15592 end;
15593 end if;
15594
15595 Act_Elmt := First_Elmt (Act_List);
15596 Act_Subp := Node (Act_Elmt);
15597 end;
15598 end if;
15599
15600 -- Stage 1: If the generic actual is not present we derive the
15601 -- primitives inherited from the parent type. If the generic parent
15602 -- type is present, the derived type is an instance of a formal
15603 -- derived type, and within the instance its operations are those of
15604 -- the actual. We derive from the formal type but make the inherited
15605 -- operations aliases of the corresponding operations of the actual.
15606
15607 Elmt := First_Elmt (Op_List);
15608 while Present (Elmt) loop
15609 Subp := Node (Elmt);
15610 Alias_Subp := Ultimate_Alias (Subp);
15611
15612 -- Do not derive internal entities of the parent that link
15613 -- interface primitives with their covering primitive. These
15614 -- entities will be added to this type when frozen.
15615
15616 if Present (Interface_Alias (Subp)) then
15617 goto Continue;
15618 end if;
15619
15620 -- If the generic actual is present find the corresponding
15621 -- operation in the generic actual. If the parent type is a
15622 -- direct ancestor of the derived type then, even if it is an
15623 -- interface, the operations are inherited from the primary
15624 -- dispatch table and are in the proper order. If we detect here
15625 -- that primitives are not in the same order we traverse the list
15626 -- of primitive operations of the actual to find the one that
15627 -- implements the interface primitive.
15628
15629 if Need_Search
15630 or else
15631 (Present (Generic_Actual)
15632 and then Present (Act_Subp)
15633 and then not
15634 (Primitive_Names_Match (Subp, Act_Subp)
15635 and then
15636 Type_Conformant (Subp, Act_Subp,
15637 Skip_Controlling_Formals => True)))
15638 then
15639 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15640 Use_Full_View => True));
15641
15642 -- Remember that we need searching for all pending primitives
15643
15644 Need_Search := True;
15645
15646 -- Handle entities associated with interface primitives
15647
15648 if Present (Alias_Subp)
15649 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15650 and then not Is_Predefined_Dispatching_Operation (Subp)
15651 then
15652 -- Search for the primitive in the homonym chain
15653
15654 Act_Subp :=
15655 Find_Primitive_Covering_Interface
15656 (Tagged_Type => Generic_Actual,
15657 Iface_Prim => Alias_Subp);
15658
15659 -- Previous search may not locate primitives covering
15660 -- interfaces defined in generics units or instantiations.
15661 -- (it fails if the covering primitive has formals whose
15662 -- type is also defined in generics or instantiations).
15663 -- In such case we search in the list of primitives of the
15664 -- generic actual for the internal entity that links the
15665 -- interface primitive and the covering primitive.
15666
15667 if No (Act_Subp)
15668 and then Is_Generic_Type (Parent_Type)
15669 then
15670 -- This code has been designed to handle only generic
15671 -- formals that implement interfaces that are defined
15672 -- in a generic unit or instantiation. If this code is
15673 -- needed for other cases we must review it because
15674 -- (given that it relies on Original_Location to locate
15675 -- the primitive of Generic_Actual that covers the
15676 -- interface) it could leave linked through attribute
15677 -- Alias entities of unrelated instantiations).
15678
15679 pragma Assert
15680 (Is_Generic_Unit
15681 (Scope (Find_Dispatching_Type (Alias_Subp)))
15682 or else
15683 Instantiation_Depth
15684 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15685
15686 declare
15687 Iface_Prim_Loc : constant Source_Ptr :=
15688 Original_Location (Sloc (Alias_Subp));
15689
15690 Elmt : Elmt_Id;
15691 Prim : Entity_Id;
15692
15693 begin
15694 Elmt :=
15695 First_Elmt (Primitive_Operations (Generic_Actual));
15696
15697 Search : while Present (Elmt) loop
15698 Prim := Node (Elmt);
15699
15700 if Present (Interface_Alias (Prim))
15701 and then Original_Location
15702 (Sloc (Interface_Alias (Prim))) =
15703 Iface_Prim_Loc
15704 then
15705 Act_Subp := Alias (Prim);
15706 exit Search;
15707 end if;
15708
15709 Next_Elmt (Elmt);
15710 end loop Search;
15711 end;
15712 end if;
15713
15714 pragma Assert (Present (Act_Subp)
15715 or else Is_Abstract_Type (Generic_Actual)
15716 or else Serious_Errors_Detected > 0);
15717
15718 -- Handle predefined primitives plus the rest of user-defined
15719 -- primitives
15720
15721 else
15722 Act_Elmt := First_Elmt (Act_List);
15723 while Present (Act_Elmt) loop
15724 Act_Subp := Node (Act_Elmt);
15725
15726 exit when Primitive_Names_Match (Subp, Act_Subp)
15727 and then Type_Conformant
15728 (Subp, Act_Subp,
15729 Skip_Controlling_Formals => True)
15730 and then No (Interface_Alias (Act_Subp));
15731
15732 Next_Elmt (Act_Elmt);
15733 end loop;
15734
15735 if No (Act_Elmt) then
15736 Act_Subp := Empty;
15737 end if;
15738 end if;
15739 end if;
15740
15741 -- Case 1: If the parent is a limited interface then it has the
15742 -- predefined primitives of synchronized interfaces. However, the
15743 -- actual type may be a non-limited type and hence it does not
15744 -- have such primitives.
15745
15746 if Present (Generic_Actual)
15747 and then not Present (Act_Subp)
15748 and then Is_Limited_Interface (Parent_Base)
15749 and then Is_Predefined_Interface_Primitive (Subp)
15750 then
15751 null;
15752
15753 -- Case 2: Inherit entities associated with interfaces that were
15754 -- not covered by the parent type. We exclude here null interface
15755 -- primitives because they do not need special management.
15756
15757 -- We also exclude interface operations that are renamings. If the
15758 -- subprogram is an explicit renaming of an interface primitive,
15759 -- it is a regular primitive operation, and the presence of its
15760 -- alias is not relevant: it has to be derived like any other
15761 -- primitive.
15762
15763 elsif Present (Alias (Subp))
15764 and then Nkind (Unit_Declaration_Node (Subp)) /=
15765 N_Subprogram_Renaming_Declaration
15766 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15767 and then not
15768 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15769 and then Null_Present (Parent (Alias_Subp)))
15770 then
15771 -- If this is an abstract private type then we transfer the
15772 -- derivation of the interface primitive from the partial view
15773 -- to the full view. This is safe because all the interfaces
15774 -- must be visible in the partial view. Done to avoid adding
15775 -- a new interface derivation to the private part of the
15776 -- enclosing package; otherwise this new derivation would be
15777 -- decorated as hidden when the analysis of the enclosing
15778 -- package completes.
15779
15780 if Is_Abstract_Type (Derived_Type)
15781 and then In_Private_Part (Current_Scope)
15782 and then Has_Private_Declaration (Derived_Type)
15783 then
15784 declare
15785 Partial_View : Entity_Id;
15786 Elmt : Elmt_Id;
15787 Ent : Entity_Id;
15788
15789 begin
15790 Partial_View := First_Entity (Current_Scope);
15791 loop
15792 exit when No (Partial_View)
15793 or else (Has_Private_Declaration (Partial_View)
15794 and then
15795 Full_View (Partial_View) = Derived_Type);
15796
15797 Next_Entity (Partial_View);
15798 end loop;
15799
15800 -- If the partial view was not found then the source code
15801 -- has errors and the derivation is not needed.
15802
15803 if Present (Partial_View) then
15804 Elmt :=
15805 First_Elmt (Primitive_Operations (Partial_View));
15806 while Present (Elmt) loop
15807 Ent := Node (Elmt);
15808
15809 if Present (Alias (Ent))
15810 and then Ultimate_Alias (Ent) = Alias (Subp)
15811 then
15812 Append_Elmt
15813 (Ent, Primitive_Operations (Derived_Type));
15814 exit;
15815 end if;
15816
15817 Next_Elmt (Elmt);
15818 end loop;
15819
15820 -- If the interface primitive was not found in the
15821 -- partial view then this interface primitive was
15822 -- overridden. We add a derivation to activate in
15823 -- Derive_Progenitor_Subprograms the machinery to
15824 -- search for it.
15825
15826 if No (Elmt) then
15827 Derive_Interface_Subprogram
15828 (New_Subp => New_Subp,
15829 Subp => Subp,
15830 Actual_Subp => Act_Subp);
15831 end if;
15832 end if;
15833 end;
15834 else
15835 Derive_Interface_Subprogram
15836 (New_Subp => New_Subp,
15837 Subp => Subp,
15838 Actual_Subp => Act_Subp);
15839 end if;
15840
15841 -- Case 3: Common derivation
15842
15843 else
15844 Derive_Subprogram
15845 (New_Subp => New_Subp,
15846 Parent_Subp => Subp,
15847 Derived_Type => Derived_Type,
15848 Parent_Type => Parent_Base,
15849 Actual_Subp => Act_Subp);
15850 end if;
15851
15852 -- No need to update Act_Elm if we must search for the
15853 -- corresponding operation in the generic actual
15854
15855 if not Need_Search
15856 and then Present (Act_Elmt)
15857 then
15858 Next_Elmt (Act_Elmt);
15859 Act_Subp := Node (Act_Elmt);
15860 end if;
15861
15862 <<Continue>>
15863 Next_Elmt (Elmt);
15864 end loop;
15865
15866 -- Inherit additional operations from progenitors. If the derived
15867 -- type is a generic actual, there are not new primitive operations
15868 -- for the type because it has those of the actual, and therefore
15869 -- nothing needs to be done. The renamings generated above are not
15870 -- primitive operations, and their purpose is simply to make the
15871 -- proper operations visible within an instantiation.
15872
15873 if No (Generic_Actual) then
15874 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15875 end if;
15876 end if;
15877
15878 -- Final check: Direct descendants must have their primitives in the
15879 -- same order. We exclude from this test untagged types and instances
15880 -- of formal derived types. We skip this test if we have already
15881 -- reported serious errors in the sources.
15882
15883 pragma Assert (not Is_Tagged_Type (Derived_Type)
15884 or else Present (Generic_Actual)
15885 or else Serious_Errors_Detected > 0
15886 or else Check_Derived_Type);
15887 end Derive_Subprograms;
15888
15889 --------------------------------
15890 -- Derived_Standard_Character --
15891 --------------------------------
15892
15893 procedure Derived_Standard_Character
15894 (N : Node_Id;
15895 Parent_Type : Entity_Id;
15896 Derived_Type : Entity_Id)
15897 is
15898 Loc : constant Source_Ptr := Sloc (N);
15899 Def : constant Node_Id := Type_Definition (N);
15900 Indic : constant Node_Id := Subtype_Indication (Def);
15901 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15902 Implicit_Base : constant Entity_Id :=
15903 Create_Itype
15904 (E_Enumeration_Type, N, Derived_Type, 'B');
15905
15906 Lo : Node_Id;
15907 Hi : Node_Id;
15908
15909 begin
15910 Discard_Node (Process_Subtype (Indic, N));
15911
15912 Set_Etype (Implicit_Base, Parent_Base);
15913 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15914 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15915
15916 Set_Is_Character_Type (Implicit_Base, True);
15917 Set_Has_Delayed_Freeze (Implicit_Base);
15918
15919 -- The bounds of the implicit base are the bounds of the parent base.
15920 -- Note that their type is the parent base.
15921
15922 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15923 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15924
15925 Set_Scalar_Range (Implicit_Base,
15926 Make_Range (Loc,
15927 Low_Bound => Lo,
15928 High_Bound => Hi));
15929
15930 Conditional_Delay (Derived_Type, Parent_Type);
15931
15932 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15933 Set_Etype (Derived_Type, Implicit_Base);
15934 Set_Size_Info (Derived_Type, Parent_Type);
15935
15936 if Unknown_RM_Size (Derived_Type) then
15937 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15938 end if;
15939
15940 Set_Is_Character_Type (Derived_Type, True);
15941
15942 if Nkind (Indic) /= N_Subtype_Indication then
15943
15944 -- If no explicit constraint, the bounds are those
15945 -- of the parent type.
15946
15947 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15948 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15949 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15950 end if;
15951
15952 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15953
15954 -- Because the implicit base is used in the conversion of the bounds, we
15955 -- have to freeze it now. This is similar to what is done for numeric
15956 -- types, and it equally suspicious, but otherwise a non-static bound
15957 -- will have a reference to an unfrozen type, which is rejected by Gigi
15958 -- (???). This requires specific care for definition of stream
15959 -- attributes. For details, see comments at the end of
15960 -- Build_Derived_Numeric_Type.
15961
15962 Freeze_Before (N, Implicit_Base);
15963 end Derived_Standard_Character;
15964
15965 ------------------------------
15966 -- Derived_Type_Declaration --
15967 ------------------------------
15968
15969 procedure Derived_Type_Declaration
15970 (T : Entity_Id;
15971 N : Node_Id;
15972 Is_Completion : Boolean)
15973 is
15974 Parent_Type : Entity_Id;
15975
15976 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15977 -- Check whether the parent type is a generic formal, or derives
15978 -- directly or indirectly from one.
15979
15980 ------------------------
15981 -- Comes_From_Generic --
15982 ------------------------
15983
15984 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15985 begin
15986 if Is_Generic_Type (Typ) then
15987 return True;
15988
15989 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15990 return True;
15991
15992 elsif Is_Private_Type (Typ)
15993 and then Present (Full_View (Typ))
15994 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15995 then
15996 return True;
15997
15998 elsif Is_Generic_Actual_Type (Typ) then
15999 return True;
16000
16001 else
16002 return False;
16003 end if;
16004 end Comes_From_Generic;
16005
16006 -- Local variables
16007
16008 Def : constant Node_Id := Type_Definition (N);
16009 Iface_Def : Node_Id;
16010 Indic : constant Node_Id := Subtype_Indication (Def);
16011 Extension : constant Node_Id := Record_Extension_Part (Def);
16012 Parent_Node : Node_Id;
16013 Taggd : Boolean;
16014
16015 -- Start of processing for Derived_Type_Declaration
16016
16017 begin
16018 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16019
16020 -- Ada 2005 (AI-251): In case of interface derivation check that the
16021 -- parent is also an interface.
16022
16023 if Interface_Present (Def) then
16024 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16025
16026 if not Is_Interface (Parent_Type) then
16027 Diagnose_Interface (Indic, Parent_Type);
16028
16029 else
16030 Parent_Node := Parent (Base_Type (Parent_Type));
16031 Iface_Def := Type_Definition (Parent_Node);
16032
16033 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16034 -- other limited interfaces.
16035
16036 if Limited_Present (Def) then
16037 if Limited_Present (Iface_Def) then
16038 null;
16039
16040 elsif Protected_Present (Iface_Def) then
16041 Error_Msg_NE
16042 ("descendant of & must be declared as a protected "
16043 & "interface", N, Parent_Type);
16044
16045 elsif Synchronized_Present (Iface_Def) then
16046 Error_Msg_NE
16047 ("descendant of & must be declared as a synchronized "
16048 & "interface", N, Parent_Type);
16049
16050 elsif Task_Present (Iface_Def) then
16051 Error_Msg_NE
16052 ("descendant of & must be declared as a task interface",
16053 N, Parent_Type);
16054
16055 else
16056 Error_Msg_N
16057 ("(Ada 2005) limited interface cannot inherit from "
16058 & "non-limited interface", Indic);
16059 end if;
16060
16061 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16062 -- from non-limited or limited interfaces.
16063
16064 elsif not Protected_Present (Def)
16065 and then not Synchronized_Present (Def)
16066 and then not Task_Present (Def)
16067 then
16068 if Limited_Present (Iface_Def) then
16069 null;
16070
16071 elsif Protected_Present (Iface_Def) then
16072 Error_Msg_NE
16073 ("descendant of & must be declared as a protected "
16074 & "interface", N, Parent_Type);
16075
16076 elsif Synchronized_Present (Iface_Def) then
16077 Error_Msg_NE
16078 ("descendant of & must be declared as a synchronized "
16079 & "interface", N, Parent_Type);
16080
16081 elsif Task_Present (Iface_Def) then
16082 Error_Msg_NE
16083 ("descendant of & must be declared as a task interface",
16084 N, Parent_Type);
16085 else
16086 null;
16087 end if;
16088 end if;
16089 end if;
16090 end if;
16091
16092 if Is_Tagged_Type (Parent_Type)
16093 and then Is_Concurrent_Type (Parent_Type)
16094 and then not Is_Interface (Parent_Type)
16095 then
16096 Error_Msg_N
16097 ("parent type of a record extension cannot be a synchronized "
16098 & "tagged type (RM 3.9.1 (3/1))", N);
16099 Set_Etype (T, Any_Type);
16100 return;
16101 end if;
16102
16103 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16104 -- interfaces
16105
16106 if Is_Tagged_Type (Parent_Type)
16107 and then Is_Non_Empty_List (Interface_List (Def))
16108 then
16109 declare
16110 Intf : Node_Id;
16111 T : Entity_Id;
16112
16113 begin
16114 Intf := First (Interface_List (Def));
16115 while Present (Intf) loop
16116 T := Find_Type_Of_Subtype_Indic (Intf);
16117
16118 if not Is_Interface (T) then
16119 Diagnose_Interface (Intf, T);
16120
16121 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16122 -- a limited type from having a nonlimited progenitor.
16123
16124 elsif (Limited_Present (Def)
16125 or else (not Is_Interface (Parent_Type)
16126 and then Is_Limited_Type (Parent_Type)))
16127 and then not Is_Limited_Interface (T)
16128 then
16129 Error_Msg_NE
16130 ("progenitor interface& of limited type must be limited",
16131 N, T);
16132 end if;
16133
16134 Next (Intf);
16135 end loop;
16136 end;
16137 end if;
16138
16139 if Parent_Type = Any_Type
16140 or else Etype (Parent_Type) = Any_Type
16141 or else (Is_Class_Wide_Type (Parent_Type)
16142 and then Etype (Parent_Type) = T)
16143 then
16144 -- If Parent_Type is undefined or illegal, make new type into a
16145 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16146 -- errors. If this is a self-definition, emit error now.
16147
16148 if T = Parent_Type or else T = Etype (Parent_Type) then
16149 Error_Msg_N ("type cannot be used in its own definition", Indic);
16150 end if;
16151
16152 Set_Ekind (T, Ekind (Parent_Type));
16153 Set_Etype (T, Any_Type);
16154 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16155
16156 if Is_Tagged_Type (T)
16157 and then Is_Record_Type (T)
16158 then
16159 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16160 end if;
16161
16162 return;
16163 end if;
16164
16165 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16166 -- an interface is special because the list of interfaces in the full
16167 -- view can be given in any order. For example:
16168
16169 -- type A is interface;
16170 -- type B is interface and A;
16171 -- type D is new B with private;
16172 -- private
16173 -- type D is new A and B with null record; -- 1 --
16174
16175 -- In this case we perform the following transformation of -1-:
16176
16177 -- type D is new B and A with null record;
16178
16179 -- If the parent of the full-view covers the parent of the partial-view
16180 -- we have two possible cases:
16181
16182 -- 1) They have the same parent
16183 -- 2) The parent of the full-view implements some further interfaces
16184
16185 -- In both cases we do not need to perform the transformation. In the
16186 -- first case the source program is correct and the transformation is
16187 -- not needed; in the second case the source program does not fulfill
16188 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16189 -- later.
16190
16191 -- This transformation not only simplifies the rest of the analysis of
16192 -- this type declaration but also simplifies the correct generation of
16193 -- the object layout to the expander.
16194
16195 if In_Private_Part (Current_Scope)
16196 and then Is_Interface (Parent_Type)
16197 then
16198 declare
16199 Iface : Node_Id;
16200 Partial_View : Entity_Id;
16201 Partial_View_Parent : Entity_Id;
16202 New_Iface : Node_Id;
16203
16204 begin
16205 -- Look for the associated private type declaration
16206
16207 Partial_View := First_Entity (Current_Scope);
16208 loop
16209 exit when No (Partial_View)
16210 or else (Has_Private_Declaration (Partial_View)
16211 and then Full_View (Partial_View) = T);
16212
16213 Next_Entity (Partial_View);
16214 end loop;
16215
16216 -- If the partial view was not found then the source code has
16217 -- errors and the transformation is not needed.
16218
16219 if Present (Partial_View) then
16220 Partial_View_Parent := Etype (Partial_View);
16221
16222 -- If the parent of the full-view covers the parent of the
16223 -- partial-view we have nothing else to do.
16224
16225 if Interface_Present_In_Ancestor
16226 (Parent_Type, Partial_View_Parent)
16227 then
16228 null;
16229
16230 -- Traverse the list of interfaces of the full-view to look
16231 -- for the parent of the partial-view and perform the tree
16232 -- transformation.
16233
16234 else
16235 Iface := First (Interface_List (Def));
16236 while Present (Iface) loop
16237 if Etype (Iface) = Etype (Partial_View) then
16238 Rewrite (Subtype_Indication (Def),
16239 New_Copy (Subtype_Indication
16240 (Parent (Partial_View))));
16241
16242 New_Iface :=
16243 Make_Identifier (Sloc (N), Chars (Parent_Type));
16244 Append (New_Iface, Interface_List (Def));
16245
16246 -- Analyze the transformed code
16247
16248 Derived_Type_Declaration (T, N, Is_Completion);
16249 return;
16250 end if;
16251
16252 Next (Iface);
16253 end loop;
16254 end if;
16255 end if;
16256 end;
16257 end if;
16258
16259 -- Only composite types other than array types are allowed to have
16260 -- discriminants.
16261
16262 if Present (Discriminant_Specifications (N)) then
16263 if (Is_Elementary_Type (Parent_Type)
16264 or else
16265 Is_Array_Type (Parent_Type))
16266 and then not Error_Posted (N)
16267 then
16268 Error_Msg_N
16269 ("elementary or array type cannot have discriminants",
16270 Defining_Identifier (First (Discriminant_Specifications (N))));
16271 Set_Has_Discriminants (T, False);
16272
16273 -- The type is allowed to have discriminants
16274
16275 else
16276 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16277 end if;
16278 end if;
16279
16280 -- In Ada 83, a derived type defined in a package specification cannot
16281 -- be used for further derivation until the end of its visible part.
16282 -- Note that derivation in the private part of the package is allowed.
16283
16284 if Ada_Version = Ada_83
16285 and then Is_Derived_Type (Parent_Type)
16286 and then In_Visible_Part (Scope (Parent_Type))
16287 then
16288 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16289 Error_Msg_N
16290 ("(Ada 83): premature use of type for derivation", Indic);
16291 end if;
16292 end if;
16293
16294 -- Check for early use of incomplete or private type
16295
16296 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16297 Error_Msg_N ("premature derivation of incomplete type", Indic);
16298 return;
16299
16300 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16301 and then not Comes_From_Generic (Parent_Type))
16302 or else Has_Private_Component (Parent_Type)
16303 then
16304 -- The ancestor type of a formal type can be incomplete, in which
16305 -- case only the operations of the partial view are available in the
16306 -- generic. Subsequent checks may be required when the full view is
16307 -- analyzed to verify that a derivation from a tagged type has an
16308 -- extension.
16309
16310 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16311 null;
16312
16313 elsif No (Underlying_Type (Parent_Type))
16314 or else Has_Private_Component (Parent_Type)
16315 then
16316 Error_Msg_N
16317 ("premature derivation of derived or private type", Indic);
16318
16319 -- Flag the type itself as being in error, this prevents some
16320 -- nasty problems with subsequent uses of the malformed type.
16321
16322 Set_Error_Posted (T);
16323
16324 -- Check that within the immediate scope of an untagged partial
16325 -- view it's illegal to derive from the partial view if the
16326 -- full view is tagged. (7.3(7))
16327
16328 -- We verify that the Parent_Type is a partial view by checking
16329 -- that it is not a Full_Type_Declaration (i.e. a private type or
16330 -- private extension declaration), to distinguish a partial view
16331 -- from a derivation from a private type which also appears as
16332 -- E_Private_Type. If the parent base type is not declared in an
16333 -- enclosing scope there is no need to check.
16334
16335 elsif Present (Full_View (Parent_Type))
16336 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16337 and then not Is_Tagged_Type (Parent_Type)
16338 and then Is_Tagged_Type (Full_View (Parent_Type))
16339 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16340 then
16341 Error_Msg_N
16342 ("premature derivation from type with tagged full view",
16343 Indic);
16344 end if;
16345 end if;
16346
16347 -- Check that form of derivation is appropriate
16348
16349 Taggd := Is_Tagged_Type (Parent_Type);
16350
16351 -- Set the parent type to the class-wide type's specific type in this
16352 -- case to prevent cascading errors
16353
16354 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16355 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16356 Set_Etype (T, Etype (Parent_Type));
16357 return;
16358 end if;
16359
16360 if Present (Extension) and then not Taggd then
16361 Error_Msg_N
16362 ("type derived from untagged type cannot have extension", Indic);
16363
16364 elsif No (Extension) and then Taggd then
16365
16366 -- If this declaration is within a private part (or body) of a
16367 -- generic instantiation then the derivation is allowed (the parent
16368 -- type can only appear tagged in this case if it's a generic actual
16369 -- type, since it would otherwise have been rejected in the analysis
16370 -- of the generic template).
16371
16372 if not Is_Generic_Actual_Type (Parent_Type)
16373 or else In_Visible_Part (Scope (Parent_Type))
16374 then
16375 if Is_Class_Wide_Type (Parent_Type) then
16376 Error_Msg_N
16377 ("parent type must not be a class-wide type", Indic);
16378
16379 -- Use specific type to prevent cascaded errors.
16380
16381 Parent_Type := Etype (Parent_Type);
16382
16383 else
16384 Error_Msg_N
16385 ("type derived from tagged type must have extension", Indic);
16386 end if;
16387 end if;
16388 end if;
16389
16390 -- AI-443: Synchronized formal derived types require a private
16391 -- extension. There is no point in checking the ancestor type or
16392 -- the progenitors since the construct is wrong to begin with.
16393
16394 if Ada_Version >= Ada_2005
16395 and then Is_Generic_Type (T)
16396 and then Present (Original_Node (N))
16397 then
16398 declare
16399 Decl : constant Node_Id := Original_Node (N);
16400
16401 begin
16402 if Nkind (Decl) = N_Formal_Type_Declaration
16403 and then Nkind (Formal_Type_Definition (Decl)) =
16404 N_Formal_Derived_Type_Definition
16405 and then Synchronized_Present (Formal_Type_Definition (Decl))
16406 and then No (Extension)
16407
16408 -- Avoid emitting a duplicate error message
16409
16410 and then not Error_Posted (Indic)
16411 then
16412 Error_Msg_N
16413 ("synchronized derived type must have extension", N);
16414 end if;
16415 end;
16416 end if;
16417
16418 if Null_Exclusion_Present (Def)
16419 and then not Is_Access_Type (Parent_Type)
16420 then
16421 Error_Msg_N ("null exclusion can only apply to an access type", N);
16422 end if;
16423
16424 -- Avoid deriving parent primitives of underlying record views
16425
16426 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16427 Derive_Subps => not Is_Underlying_Record_View (T));
16428
16429 -- AI-419: The parent type of an explicitly limited derived type must
16430 -- be a limited type or a limited interface.
16431
16432 if Limited_Present (Def) then
16433 Set_Is_Limited_Record (T);
16434
16435 if Is_Interface (T) then
16436 Set_Is_Limited_Interface (T);
16437 end if;
16438
16439 if not Is_Limited_Type (Parent_Type)
16440 and then
16441 (not Is_Interface (Parent_Type)
16442 or else not Is_Limited_Interface (Parent_Type))
16443 then
16444 -- AI05-0096: a derivation in the private part of an instance is
16445 -- legal if the generic formal is untagged limited, and the actual
16446 -- is non-limited.
16447
16448 if Is_Generic_Actual_Type (Parent_Type)
16449 and then In_Private_Part (Current_Scope)
16450 and then
16451 not Is_Tagged_Type
16452 (Generic_Parent_Type (Parent (Parent_Type)))
16453 then
16454 null;
16455
16456 else
16457 Error_Msg_NE
16458 ("parent type& of limited type must be limited",
16459 N, Parent_Type);
16460 end if;
16461 end if;
16462 end if;
16463
16464 -- In SPARK, there are no derived type definitions other than type
16465 -- extensions of tagged record types.
16466
16467 if No (Extension) then
16468 Check_SPARK_05_Restriction
16469 ("derived type is not allowed", Original_Node (N));
16470 end if;
16471 end Derived_Type_Declaration;
16472
16473 ------------------------
16474 -- Diagnose_Interface --
16475 ------------------------
16476
16477 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16478 begin
16479 if not Is_Interface (E) and then E /= Any_Type then
16480 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16481 end if;
16482 end Diagnose_Interface;
16483
16484 ----------------------------------
16485 -- Enumeration_Type_Declaration --
16486 ----------------------------------
16487
16488 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16489 Ev : Uint;
16490 L : Node_Id;
16491 R_Node : Node_Id;
16492 B_Node : Node_Id;
16493
16494 begin
16495 -- Create identifier node representing lower bound
16496
16497 B_Node := New_Node (N_Identifier, Sloc (Def));
16498 L := First (Literals (Def));
16499 Set_Chars (B_Node, Chars (L));
16500 Set_Entity (B_Node, L);
16501 Set_Etype (B_Node, T);
16502 Set_Is_Static_Expression (B_Node, True);
16503
16504 R_Node := New_Node (N_Range, Sloc (Def));
16505 Set_Low_Bound (R_Node, B_Node);
16506
16507 Set_Ekind (T, E_Enumeration_Type);
16508 Set_First_Literal (T, L);
16509 Set_Etype (T, T);
16510 Set_Is_Constrained (T);
16511
16512 Ev := Uint_0;
16513
16514 -- Loop through literals of enumeration type setting pos and rep values
16515 -- except that if the Ekind is already set, then it means the literal
16516 -- was already constructed (case of a derived type declaration and we
16517 -- should not disturb the Pos and Rep values.
16518
16519 while Present (L) loop
16520 if Ekind (L) /= E_Enumeration_Literal then
16521 Set_Ekind (L, E_Enumeration_Literal);
16522 Set_Enumeration_Pos (L, Ev);
16523 Set_Enumeration_Rep (L, Ev);
16524 Set_Is_Known_Valid (L, True);
16525 end if;
16526
16527 Set_Etype (L, T);
16528 New_Overloaded_Entity (L);
16529 Generate_Definition (L);
16530 Set_Convention (L, Convention_Intrinsic);
16531
16532 -- Case of character literal
16533
16534 if Nkind (L) = N_Defining_Character_Literal then
16535 Set_Is_Character_Type (T, True);
16536
16537 -- Check violation of No_Wide_Characters
16538
16539 if Restriction_Check_Required (No_Wide_Characters) then
16540 Get_Name_String (Chars (L));
16541
16542 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16543 Check_Restriction (No_Wide_Characters, L);
16544 end if;
16545 end if;
16546 end if;
16547
16548 Ev := Ev + 1;
16549 Next (L);
16550 end loop;
16551
16552 -- Now create a node representing upper bound
16553
16554 B_Node := New_Node (N_Identifier, Sloc (Def));
16555 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16556 Set_Entity (B_Node, Last (Literals (Def)));
16557 Set_Etype (B_Node, T);
16558 Set_Is_Static_Expression (B_Node, True);
16559
16560 Set_High_Bound (R_Node, B_Node);
16561
16562 -- Initialize various fields of the type. Some of this information
16563 -- may be overwritten later through rep.clauses.
16564
16565 Set_Scalar_Range (T, R_Node);
16566 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16567 Set_Enum_Esize (T);
16568 Set_Enum_Pos_To_Rep (T, Empty);
16569
16570 -- Set Discard_Names if configuration pragma set, or if there is
16571 -- a parameterless pragma in the current declarative region
16572
16573 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16574 Set_Discard_Names (T);
16575 end if;
16576
16577 -- Process end label if there is one
16578
16579 if Present (Def) then
16580 Process_End_Label (Def, 'e', T);
16581 end if;
16582 end Enumeration_Type_Declaration;
16583
16584 ---------------------------------
16585 -- Expand_To_Stored_Constraint --
16586 ---------------------------------
16587
16588 function Expand_To_Stored_Constraint
16589 (Typ : Entity_Id;
16590 Constraint : Elist_Id) return Elist_Id
16591 is
16592 Explicitly_Discriminated_Type : Entity_Id;
16593 Expansion : Elist_Id;
16594 Discriminant : Entity_Id;
16595
16596 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16597 -- Find the nearest type that actually specifies discriminants
16598
16599 ---------------------------------
16600 -- Type_With_Explicit_Discrims --
16601 ---------------------------------
16602
16603 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16604 Typ : constant E := Base_Type (Id);
16605
16606 begin
16607 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16608 if Present (Full_View (Typ)) then
16609 return Type_With_Explicit_Discrims (Full_View (Typ));
16610 end if;
16611
16612 else
16613 if Has_Discriminants (Typ) then
16614 return Typ;
16615 end if;
16616 end if;
16617
16618 if Etype (Typ) = Typ then
16619 return Empty;
16620 elsif Has_Discriminants (Typ) then
16621 return Typ;
16622 else
16623 return Type_With_Explicit_Discrims (Etype (Typ));
16624 end if;
16625
16626 end Type_With_Explicit_Discrims;
16627
16628 -- Start of processing for Expand_To_Stored_Constraint
16629
16630 begin
16631 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16632 return No_Elist;
16633 end if;
16634
16635 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16636
16637 if No (Explicitly_Discriminated_Type) then
16638 return No_Elist;
16639 end if;
16640
16641 Expansion := New_Elmt_List;
16642
16643 Discriminant :=
16644 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16645 while Present (Discriminant) loop
16646 Append_Elmt
16647 (Get_Discriminant_Value
16648 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16649 To => Expansion);
16650 Next_Stored_Discriminant (Discriminant);
16651 end loop;
16652
16653 return Expansion;
16654 end Expand_To_Stored_Constraint;
16655
16656 ---------------------------
16657 -- Find_Hidden_Interface --
16658 ---------------------------
16659
16660 function Find_Hidden_Interface
16661 (Src : Elist_Id;
16662 Dest : Elist_Id) return Entity_Id
16663 is
16664 Iface : Entity_Id;
16665 Iface_Elmt : Elmt_Id;
16666
16667 begin
16668 if Present (Src) and then Present (Dest) then
16669 Iface_Elmt := First_Elmt (Src);
16670 while Present (Iface_Elmt) loop
16671 Iface := Node (Iface_Elmt);
16672
16673 if Is_Interface (Iface)
16674 and then not Contain_Interface (Iface, Dest)
16675 then
16676 return Iface;
16677 end if;
16678
16679 Next_Elmt (Iface_Elmt);
16680 end loop;
16681 end if;
16682
16683 return Empty;
16684 end Find_Hidden_Interface;
16685
16686 --------------------
16687 -- Find_Type_Name --
16688 --------------------
16689
16690 function Find_Type_Name (N : Node_Id) return Entity_Id is
16691 Id : constant Entity_Id := Defining_Identifier (N);
16692 New_Id : Entity_Id;
16693 Prev : Entity_Id;
16694 Prev_Par : Node_Id;
16695
16696 procedure Check_Duplicate_Aspects;
16697 -- Check that aspects specified in a completion have not been specified
16698 -- already in the partial view.
16699
16700 procedure Tag_Mismatch;
16701 -- Diagnose a tagged partial view whose full view is untagged. We post
16702 -- the message on the full view, with a reference to the previous
16703 -- partial view. The partial view can be private or incomplete, and
16704 -- these are handled in a different manner, so we determine the position
16705 -- of the error message from the respective slocs of both.
16706
16707 -----------------------------
16708 -- Check_Duplicate_Aspects --
16709 -----------------------------
16710
16711 procedure Check_Duplicate_Aspects is
16712 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
16713 -- Return the corresponding aspect of the partial view which matches
16714 -- the aspect id of Asp. Return Empty is no such aspect exists.
16715
16716 -----------------------------
16717 -- Get_Partial_View_Aspect --
16718 -----------------------------
16719
16720 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
16721 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
16722 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
16723 Prev_Asp : Node_Id;
16724
16725 begin
16726 if Present (Prev_Asps) then
16727 Prev_Asp := First (Prev_Asps);
16728 while Present (Prev_Asp) loop
16729 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
16730 return Prev_Asp;
16731 end if;
16732
16733 Next (Prev_Asp);
16734 end loop;
16735 end if;
16736
16737 return Empty;
16738 end Get_Partial_View_Aspect;
16739
16740 -- Local variables
16741
16742 Full_Asps : constant List_Id := Aspect_Specifications (N);
16743 Full_Asp : Node_Id;
16744 Part_Asp : Node_Id;
16745
16746 -- Start of processing for Check_Duplicate_Aspects
16747
16748 begin
16749 if Present (Full_Asps) then
16750 Full_Asp := First (Full_Asps);
16751 while Present (Full_Asp) loop
16752 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
16753
16754 -- An aspect and its class-wide counterpart are two distinct
16755 -- aspects and may apply to both views of an entity.
16756
16757 if Present (Part_Asp)
16758 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
16759 then
16760 Error_Msg_N
16761 ("aspect already specified in private declaration",
16762 Full_Asp);
16763
16764 Remove (Full_Asp);
16765 return;
16766 end if;
16767
16768 if Has_Discriminants (Prev)
16769 and then not Has_Unknown_Discriminants (Prev)
16770 and then Get_Aspect_Id (Full_Asp) =
16771 Aspect_Implicit_Dereference
16772 then
16773 Error_Msg_N
16774 ("cannot specify aspect if partial view has known "
16775 & "discriminants", Full_Asp);
16776 end if;
16777
16778 Next (Full_Asp);
16779 end loop;
16780 end if;
16781 end Check_Duplicate_Aspects;
16782
16783 ------------------
16784 -- Tag_Mismatch --
16785 ------------------
16786
16787 procedure Tag_Mismatch is
16788 begin
16789 if Sloc (Prev) < Sloc (Id) then
16790 if Ada_Version >= Ada_2012
16791 and then Nkind (N) = N_Private_Type_Declaration
16792 then
16793 Error_Msg_NE
16794 ("declaration of private } must be a tagged type ", Id, Prev);
16795 else
16796 Error_Msg_NE
16797 ("full declaration of } must be a tagged type ", Id, Prev);
16798 end if;
16799
16800 else
16801 if Ada_Version >= Ada_2012
16802 and then Nkind (N) = N_Private_Type_Declaration
16803 then
16804 Error_Msg_NE
16805 ("declaration of private } must be a tagged type ", Prev, Id);
16806 else
16807 Error_Msg_NE
16808 ("full declaration of } must be a tagged type ", Prev, Id);
16809 end if;
16810 end if;
16811 end Tag_Mismatch;
16812
16813 -- Start of processing for Find_Type_Name
16814
16815 begin
16816 -- Find incomplete declaration, if one was given
16817
16818 Prev := Current_Entity_In_Scope (Id);
16819
16820 -- New type declaration
16821
16822 if No (Prev) then
16823 Enter_Name (Id);
16824 return Id;
16825
16826 -- Previous declaration exists
16827
16828 else
16829 Prev_Par := Parent (Prev);
16830
16831 -- Error if not incomplete/private case except if previous
16832 -- declaration is implicit, etc. Enter_Name will emit error if
16833 -- appropriate.
16834
16835 if not Is_Incomplete_Or_Private_Type (Prev) then
16836 Enter_Name (Id);
16837 New_Id := Id;
16838
16839 -- Check invalid completion of private or incomplete type
16840
16841 elsif not Nkind_In (N, N_Full_Type_Declaration,
16842 N_Task_Type_Declaration,
16843 N_Protected_Type_Declaration)
16844 and then
16845 (Ada_Version < Ada_2012
16846 or else not Is_Incomplete_Type (Prev)
16847 or else not Nkind_In (N, N_Private_Type_Declaration,
16848 N_Private_Extension_Declaration))
16849 then
16850 -- Completion must be a full type declarations (RM 7.3(4))
16851
16852 Error_Msg_Sloc := Sloc (Prev);
16853 Error_Msg_NE ("invalid completion of }", Id, Prev);
16854
16855 -- Set scope of Id to avoid cascaded errors. Entity is never
16856 -- examined again, except when saving globals in generics.
16857
16858 Set_Scope (Id, Current_Scope);
16859 New_Id := Id;
16860
16861 -- If this is a repeated incomplete declaration, no further
16862 -- checks are possible.
16863
16864 if Nkind (N) = N_Incomplete_Type_Declaration then
16865 return Prev;
16866 end if;
16867
16868 -- Case of full declaration of incomplete type
16869
16870 elsif Ekind (Prev) = E_Incomplete_Type
16871 and then (Ada_Version < Ada_2012
16872 or else No (Full_View (Prev))
16873 or else not Is_Private_Type (Full_View (Prev)))
16874 then
16875 -- Indicate that the incomplete declaration has a matching full
16876 -- declaration. The defining occurrence of the incomplete
16877 -- declaration remains the visible one, and the procedure
16878 -- Get_Full_View dereferences it whenever the type is used.
16879
16880 if Present (Full_View (Prev)) then
16881 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16882 end if;
16883
16884 Set_Full_View (Prev, Id);
16885 Append_Entity (Id, Current_Scope);
16886 Set_Is_Public (Id, Is_Public (Prev));
16887 Set_Is_Internal (Id);
16888 New_Id := Prev;
16889
16890 -- If the incomplete view is tagged, a class_wide type has been
16891 -- created already. Use it for the private type as well, in order
16892 -- to prevent multiple incompatible class-wide types that may be
16893 -- created for self-referential anonymous access components.
16894
16895 if Is_Tagged_Type (Prev)
16896 and then Present (Class_Wide_Type (Prev))
16897 then
16898 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16899 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16900
16901 -- Type of the class-wide type is the current Id. Previously
16902 -- this was not done for private declarations because of order-
16903 -- of-elaboration issues in the back end, but gigi now handles
16904 -- this properly.
16905
16906 Set_Etype (Class_Wide_Type (Id), Id);
16907 end if;
16908
16909 -- Case of full declaration of private type
16910
16911 else
16912 -- If the private type was a completion of an incomplete type then
16913 -- update Prev to reference the private type
16914
16915 if Ada_Version >= Ada_2012
16916 and then Ekind (Prev) = E_Incomplete_Type
16917 and then Present (Full_View (Prev))
16918 and then Is_Private_Type (Full_View (Prev))
16919 then
16920 Prev := Full_View (Prev);
16921 Prev_Par := Parent (Prev);
16922 end if;
16923
16924 if Nkind (N) = N_Full_Type_Declaration
16925 and then Nkind_In
16926 (Type_Definition (N), N_Record_Definition,
16927 N_Derived_Type_Definition)
16928 and then Interface_Present (Type_Definition (N))
16929 then
16930 Error_Msg_N
16931 ("completion of private type cannot be an interface", N);
16932 end if;
16933
16934 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16935 if Etype (Prev) /= Prev then
16936
16937 -- Prev is a private subtype or a derived type, and needs
16938 -- no completion.
16939
16940 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16941 New_Id := Id;
16942
16943 elsif Ekind (Prev) = E_Private_Type
16944 and then Nkind_In (N, N_Task_Type_Declaration,
16945 N_Protected_Type_Declaration)
16946 then
16947 Error_Msg_N
16948 ("completion of nonlimited type cannot be limited", N);
16949
16950 elsif Ekind (Prev) = E_Record_Type_With_Private
16951 and then Nkind_In (N, N_Task_Type_Declaration,
16952 N_Protected_Type_Declaration)
16953 then
16954 if not Is_Limited_Record (Prev) then
16955 Error_Msg_N
16956 ("completion of nonlimited type cannot be limited", N);
16957
16958 elsif No (Interface_List (N)) then
16959 Error_Msg_N
16960 ("completion of tagged private type must be tagged",
16961 N);
16962 end if;
16963 end if;
16964
16965 -- Ada 2005 (AI-251): Private extension declaration of a task
16966 -- type or a protected type. This case arises when covering
16967 -- interface types.
16968
16969 elsif Nkind_In (N, N_Task_Type_Declaration,
16970 N_Protected_Type_Declaration)
16971 then
16972 null;
16973
16974 elsif Nkind (N) /= N_Full_Type_Declaration
16975 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16976 then
16977 Error_Msg_N
16978 ("full view of private extension must be an extension", N);
16979
16980 elsif not (Abstract_Present (Parent (Prev)))
16981 and then Abstract_Present (Type_Definition (N))
16982 then
16983 Error_Msg_N
16984 ("full view of non-abstract extension cannot be abstract", N);
16985 end if;
16986
16987 if not In_Private_Part (Current_Scope) then
16988 Error_Msg_N
16989 ("declaration of full view must appear in private part", N);
16990 end if;
16991
16992 if Ada_Version >= Ada_2012 then
16993 Check_Duplicate_Aspects;
16994 end if;
16995
16996 Copy_And_Swap (Prev, Id);
16997 Set_Has_Private_Declaration (Prev);
16998 Set_Has_Private_Declaration (Id);
16999
17000 -- AI12-0133: Indicate whether we have a partial view with
17001 -- unknown discriminants, in which case initialization of objects
17002 -- of the type do not receive an invariant check.
17003
17004 Set_Partial_View_Has_Unknown_Discr
17005 (Prev, Has_Unknown_Discriminants (Id));
17006
17007 -- Preserve aspect and iterator flags that may have been set on
17008 -- the partial view.
17009
17010 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17011 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17012
17013 -- If no error, propagate freeze_node from private to full view.
17014 -- It may have been generated for an early operational item.
17015
17016 if Present (Freeze_Node (Id))
17017 and then Serious_Errors_Detected = 0
17018 and then No (Full_View (Id))
17019 then
17020 Set_Freeze_Node (Prev, Freeze_Node (Id));
17021 Set_Freeze_Node (Id, Empty);
17022 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17023 end if;
17024
17025 Set_Full_View (Id, Prev);
17026 New_Id := Prev;
17027 end if;
17028
17029 -- Verify that full declaration conforms to partial one
17030
17031 if Is_Incomplete_Or_Private_Type (Prev)
17032 and then Present (Discriminant_Specifications (Prev_Par))
17033 then
17034 if Present (Discriminant_Specifications (N)) then
17035 if Ekind (Prev) = E_Incomplete_Type then
17036 Check_Discriminant_Conformance (N, Prev, Prev);
17037 else
17038 Check_Discriminant_Conformance (N, Prev, Id);
17039 end if;
17040
17041 else
17042 Error_Msg_N
17043 ("missing discriminants in full type declaration", N);
17044
17045 -- To avoid cascaded errors on subsequent use, share the
17046 -- discriminants of the partial view.
17047
17048 Set_Discriminant_Specifications (N,
17049 Discriminant_Specifications (Prev_Par));
17050 end if;
17051 end if;
17052
17053 -- A prior untagged partial view can have an associated class-wide
17054 -- type due to use of the class attribute, and in this case the full
17055 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17056 -- of incomplete tagged declarations, but we check for it.
17057
17058 if Is_Type (Prev)
17059 and then (Is_Tagged_Type (Prev)
17060 or else Present (Class_Wide_Type (Prev)))
17061 then
17062 -- Ada 2012 (AI05-0162): A private type may be the completion of
17063 -- an incomplete type.
17064
17065 if Ada_Version >= Ada_2012
17066 and then Is_Incomplete_Type (Prev)
17067 and then Nkind_In (N, N_Private_Type_Declaration,
17068 N_Private_Extension_Declaration)
17069 then
17070 -- No need to check private extensions since they are tagged
17071
17072 if Nkind (N) = N_Private_Type_Declaration
17073 and then not Tagged_Present (N)
17074 then
17075 Tag_Mismatch;
17076 end if;
17077
17078 -- The full declaration is either a tagged type (including
17079 -- a synchronized type that implements interfaces) or a
17080 -- type extension, otherwise this is an error.
17081
17082 elsif Nkind_In (N, N_Task_Type_Declaration,
17083 N_Protected_Type_Declaration)
17084 then
17085 if No (Interface_List (N)) and then not Error_Posted (N) then
17086 Tag_Mismatch;
17087 end if;
17088
17089 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17090
17091 -- Indicate that the previous declaration (tagged incomplete
17092 -- or private declaration) requires the same on the full one.
17093
17094 if not Tagged_Present (Type_Definition (N)) then
17095 Tag_Mismatch;
17096 Set_Is_Tagged_Type (Id);
17097 end if;
17098
17099 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17100 if No (Record_Extension_Part (Type_Definition (N))) then
17101 Error_Msg_NE
17102 ("full declaration of } must be a record extension",
17103 Prev, Id);
17104
17105 -- Set some attributes to produce a usable full view
17106
17107 Set_Is_Tagged_Type (Id);
17108 end if;
17109
17110 else
17111 Tag_Mismatch;
17112 end if;
17113 end if;
17114
17115 if Present (Prev)
17116 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17117 and then Present (Premature_Use (Parent (Prev)))
17118 then
17119 Error_Msg_Sloc := Sloc (N);
17120 Error_Msg_N
17121 ("\full declaration #", Premature_Use (Parent (Prev)));
17122 end if;
17123
17124 return New_Id;
17125 end if;
17126 end Find_Type_Name;
17127
17128 -------------------------
17129 -- Find_Type_Of_Object --
17130 -------------------------
17131
17132 function Find_Type_Of_Object
17133 (Obj_Def : Node_Id;
17134 Related_Nod : Node_Id) return Entity_Id
17135 is
17136 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17137 P : Node_Id := Parent (Obj_Def);
17138 T : Entity_Id;
17139 Nam : Name_Id;
17140
17141 begin
17142 -- If the parent is a component_definition node we climb to the
17143 -- component_declaration node
17144
17145 if Nkind (P) = N_Component_Definition then
17146 P := Parent (P);
17147 end if;
17148
17149 -- Case of an anonymous array subtype
17150
17151 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17152 N_Unconstrained_Array_Definition)
17153 then
17154 T := Empty;
17155 Array_Type_Declaration (T, Obj_Def);
17156
17157 -- Create an explicit subtype whenever possible
17158
17159 elsif Nkind (P) /= N_Component_Declaration
17160 and then Def_Kind = N_Subtype_Indication
17161 then
17162 -- Base name of subtype on object name, which will be unique in
17163 -- the current scope.
17164
17165 -- If this is a duplicate declaration, return base type, to avoid
17166 -- generating duplicate anonymous types.
17167
17168 if Error_Posted (P) then
17169 Analyze (Subtype_Mark (Obj_Def));
17170 return Entity (Subtype_Mark (Obj_Def));
17171 end if;
17172
17173 Nam :=
17174 New_External_Name
17175 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17176
17177 T := Make_Defining_Identifier (Sloc (P), Nam);
17178
17179 Insert_Action (Obj_Def,
17180 Make_Subtype_Declaration (Sloc (P),
17181 Defining_Identifier => T,
17182 Subtype_Indication => Relocate_Node (Obj_Def)));
17183
17184 -- This subtype may need freezing, and this will not be done
17185 -- automatically if the object declaration is not in declarative
17186 -- part. Since this is an object declaration, the type cannot always
17187 -- be frozen here. Deferred constants do not freeze their type
17188 -- (which often enough will be private).
17189
17190 if Nkind (P) = N_Object_Declaration
17191 and then Constant_Present (P)
17192 and then No (Expression (P))
17193 then
17194 null;
17195
17196 -- Here we freeze the base type of object type to catch premature use
17197 -- of discriminated private type without a full view.
17198
17199 else
17200 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17201 end if;
17202
17203 -- Ada 2005 AI-406: the object definition in an object declaration
17204 -- can be an access definition.
17205
17206 elsif Def_Kind = N_Access_Definition then
17207 T := Access_Definition (Related_Nod, Obj_Def);
17208
17209 Set_Is_Local_Anonymous_Access
17210 (T,
17211 V => (Ada_Version < Ada_2012)
17212 or else (Nkind (P) /= N_Object_Declaration)
17213 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17214
17215 -- Otherwise, the object definition is just a subtype_mark
17216
17217 else
17218 T := Process_Subtype (Obj_Def, Related_Nod);
17219
17220 -- If expansion is disabled an object definition that is an aggregate
17221 -- will not get expanded and may lead to scoping problems in the back
17222 -- end, if the object is referenced in an inner scope. In that case
17223 -- create an itype reference for the object definition now. This
17224 -- may be redundant in some cases, but harmless.
17225
17226 if Is_Itype (T)
17227 and then Nkind (Related_Nod) = N_Object_Declaration
17228 and then ASIS_Mode
17229 then
17230 Build_Itype_Reference (T, Related_Nod);
17231 end if;
17232 end if;
17233
17234 return T;
17235 end Find_Type_Of_Object;
17236
17237 --------------------------------
17238 -- Find_Type_Of_Subtype_Indic --
17239 --------------------------------
17240
17241 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17242 Typ : Entity_Id;
17243
17244 begin
17245 -- Case of subtype mark with a constraint
17246
17247 if Nkind (S) = N_Subtype_Indication then
17248 Find_Type (Subtype_Mark (S));
17249 Typ := Entity (Subtype_Mark (S));
17250
17251 if not
17252 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17253 then
17254 Error_Msg_N
17255 ("incorrect constraint for this kind of type", Constraint (S));
17256 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17257 end if;
17258
17259 -- Otherwise we have a subtype mark without a constraint
17260
17261 elsif Error_Posted (S) then
17262 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17263 return Any_Type;
17264
17265 else
17266 Find_Type (S);
17267 Typ := Entity (S);
17268 end if;
17269
17270 -- Check No_Wide_Characters restriction
17271
17272 Check_Wide_Character_Restriction (Typ, S);
17273
17274 return Typ;
17275 end Find_Type_Of_Subtype_Indic;
17276
17277 -------------------------------------
17278 -- Floating_Point_Type_Declaration --
17279 -------------------------------------
17280
17281 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17282 Digs : constant Node_Id := Digits_Expression (Def);
17283 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17284 Digs_Val : Uint;
17285 Base_Typ : Entity_Id;
17286 Implicit_Base : Entity_Id;
17287 Bound : Node_Id;
17288
17289 function Can_Derive_From (E : Entity_Id) return Boolean;
17290 -- Find if given digits value, and possibly a specified range, allows
17291 -- derivation from specified type
17292
17293 function Find_Base_Type return Entity_Id;
17294 -- Find a predefined base type that Def can derive from, or generate
17295 -- an error and substitute Long_Long_Float if none exists.
17296
17297 ---------------------
17298 -- Can_Derive_From --
17299 ---------------------
17300
17301 function Can_Derive_From (E : Entity_Id) return Boolean is
17302 Spec : constant Entity_Id := Real_Range_Specification (Def);
17303
17304 begin
17305 -- Check specified "digits" constraint
17306
17307 if Digs_Val > Digits_Value (E) then
17308 return False;
17309 end if;
17310
17311 -- Check for matching range, if specified
17312
17313 if Present (Spec) then
17314 if Expr_Value_R (Type_Low_Bound (E)) >
17315 Expr_Value_R (Low_Bound (Spec))
17316 then
17317 return False;
17318 end if;
17319
17320 if Expr_Value_R (Type_High_Bound (E)) <
17321 Expr_Value_R (High_Bound (Spec))
17322 then
17323 return False;
17324 end if;
17325 end if;
17326
17327 return True;
17328 end Can_Derive_From;
17329
17330 --------------------
17331 -- Find_Base_Type --
17332 --------------------
17333
17334 function Find_Base_Type return Entity_Id is
17335 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17336
17337 begin
17338 -- Iterate over the predefined types in order, returning the first
17339 -- one that Def can derive from.
17340
17341 while Present (Choice) loop
17342 if Can_Derive_From (Node (Choice)) then
17343 return Node (Choice);
17344 end if;
17345
17346 Next_Elmt (Choice);
17347 end loop;
17348
17349 -- If we can't derive from any existing type, use Long_Long_Float
17350 -- and give appropriate message explaining the problem.
17351
17352 if Digs_Val > Max_Digs_Val then
17353 -- It might be the case that there is a type with the requested
17354 -- range, just not the combination of digits and range.
17355
17356 Error_Msg_N
17357 ("no predefined type has requested range and precision",
17358 Real_Range_Specification (Def));
17359
17360 else
17361 Error_Msg_N
17362 ("range too large for any predefined type",
17363 Real_Range_Specification (Def));
17364 end if;
17365
17366 return Standard_Long_Long_Float;
17367 end Find_Base_Type;
17368
17369 -- Start of processing for Floating_Point_Type_Declaration
17370
17371 begin
17372 Check_Restriction (No_Floating_Point, Def);
17373
17374 -- Create an implicit base type
17375
17376 Implicit_Base :=
17377 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17378
17379 -- Analyze and verify digits value
17380
17381 Analyze_And_Resolve (Digs, Any_Integer);
17382 Check_Digits_Expression (Digs);
17383 Digs_Val := Expr_Value (Digs);
17384
17385 -- Process possible range spec and find correct type to derive from
17386
17387 Process_Real_Range_Specification (Def);
17388
17389 -- Check that requested number of digits is not too high.
17390
17391 if Digs_Val > Max_Digs_Val then
17392
17393 -- The check for Max_Base_Digits may be somewhat expensive, as it
17394 -- requires reading System, so only do it when necessary.
17395
17396 declare
17397 Max_Base_Digits : constant Uint :=
17398 Expr_Value
17399 (Expression
17400 (Parent (RTE (RE_Max_Base_Digits))));
17401
17402 begin
17403 if Digs_Val > Max_Base_Digits then
17404 Error_Msg_Uint_1 := Max_Base_Digits;
17405 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17406
17407 elsif No (Real_Range_Specification (Def)) then
17408 Error_Msg_Uint_1 := Max_Digs_Val;
17409 Error_Msg_N ("types with more than ^ digits need range spec "
17410 & "(RM 3.5.7(6))", Digs);
17411 end if;
17412 end;
17413 end if;
17414
17415 -- Find a suitable type to derive from or complain and use a substitute
17416
17417 Base_Typ := Find_Base_Type;
17418
17419 -- If there are bounds given in the declaration use them as the bounds
17420 -- of the type, otherwise use the bounds of the predefined base type
17421 -- that was chosen based on the Digits value.
17422
17423 if Present (Real_Range_Specification (Def)) then
17424 Set_Scalar_Range (T, Real_Range_Specification (Def));
17425 Set_Is_Constrained (T);
17426
17427 -- The bounds of this range must be converted to machine numbers
17428 -- in accordance with RM 4.9(38).
17429
17430 Bound := Type_Low_Bound (T);
17431
17432 if Nkind (Bound) = N_Real_Literal then
17433 Set_Realval
17434 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17435 Set_Is_Machine_Number (Bound);
17436 end if;
17437
17438 Bound := Type_High_Bound (T);
17439
17440 if Nkind (Bound) = N_Real_Literal then
17441 Set_Realval
17442 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17443 Set_Is_Machine_Number (Bound);
17444 end if;
17445
17446 else
17447 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17448 end if;
17449
17450 -- Complete definition of implicit base and declared first subtype. The
17451 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17452 -- are not clobbered when the floating point type acts as a full view of
17453 -- a private type.
17454
17455 Set_Etype (Implicit_Base, Base_Typ);
17456 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17457 Set_Size_Info (Implicit_Base, Base_Typ);
17458 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17459 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17460 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17461 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17462
17463 Set_Ekind (T, E_Floating_Point_Subtype);
17464 Set_Etype (T, Implicit_Base);
17465 Set_Size_Info (T, Implicit_Base);
17466 Set_RM_Size (T, RM_Size (Implicit_Base));
17467 Inherit_Rep_Item_Chain (T, Implicit_Base);
17468 Set_Digits_Value (T, Digs_Val);
17469 end Floating_Point_Type_Declaration;
17470
17471 ----------------------------
17472 -- Get_Discriminant_Value --
17473 ----------------------------
17474
17475 -- This is the situation:
17476
17477 -- There is a non-derived type
17478
17479 -- type T0 (Dx, Dy, Dz...)
17480
17481 -- There are zero or more levels of derivation, with each derivation
17482 -- either purely inheriting the discriminants, or defining its own.
17483
17484 -- type Ti is new Ti-1
17485 -- or
17486 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17487 -- or
17488 -- subtype Ti is ...
17489
17490 -- The subtype issue is avoided by the use of Original_Record_Component,
17491 -- and the fact that derived subtypes also derive the constraints.
17492
17493 -- This chain leads back from
17494
17495 -- Typ_For_Constraint
17496
17497 -- Typ_For_Constraint has discriminants, and the value for each
17498 -- discriminant is given by its corresponding Elmt of Constraints.
17499
17500 -- Discriminant is some discriminant in this hierarchy
17501
17502 -- We need to return its value
17503
17504 -- We do this by recursively searching each level, and looking for
17505 -- Discriminant. Once we get to the bottom, we start backing up
17506 -- returning the value for it which may in turn be a discriminant
17507 -- further up, so on the backup we continue the substitution.
17508
17509 function Get_Discriminant_Value
17510 (Discriminant : Entity_Id;
17511 Typ_For_Constraint : Entity_Id;
17512 Constraint : Elist_Id) return Node_Id
17513 is
17514 function Root_Corresponding_Discriminant
17515 (Discr : Entity_Id) return Entity_Id;
17516 -- Given a discriminant, traverse the chain of inherited discriminants
17517 -- and return the topmost discriminant.
17518
17519 function Search_Derivation_Levels
17520 (Ti : Entity_Id;
17521 Discrim_Values : Elist_Id;
17522 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17523 -- This is the routine that performs the recursive search of levels
17524 -- as described above.
17525
17526 -------------------------------------
17527 -- Root_Corresponding_Discriminant --
17528 -------------------------------------
17529
17530 function Root_Corresponding_Discriminant
17531 (Discr : Entity_Id) return Entity_Id
17532 is
17533 D : Entity_Id;
17534
17535 begin
17536 D := Discr;
17537 while Present (Corresponding_Discriminant (D)) loop
17538 D := Corresponding_Discriminant (D);
17539 end loop;
17540
17541 return D;
17542 end Root_Corresponding_Discriminant;
17543
17544 ------------------------------
17545 -- Search_Derivation_Levels --
17546 ------------------------------
17547
17548 function Search_Derivation_Levels
17549 (Ti : Entity_Id;
17550 Discrim_Values : Elist_Id;
17551 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17552 is
17553 Assoc : Elmt_Id;
17554 Disc : Entity_Id;
17555 Result : Node_Or_Entity_Id;
17556 Result_Entity : Node_Id;
17557
17558 begin
17559 -- If inappropriate type, return Error, this happens only in
17560 -- cascaded error situations, and we want to avoid a blow up.
17561
17562 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17563 return Error;
17564 end if;
17565
17566 -- Look deeper if possible. Use Stored_Constraints only for
17567 -- untagged types. For tagged types use the given constraint.
17568 -- This asymmetry needs explanation???
17569
17570 if not Stored_Discrim_Values
17571 and then Present (Stored_Constraint (Ti))
17572 and then not Is_Tagged_Type (Ti)
17573 then
17574 Result :=
17575 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17576 else
17577 declare
17578 Td : constant Entity_Id := Etype (Ti);
17579
17580 begin
17581 if Td = Ti then
17582 Result := Discriminant;
17583
17584 else
17585 if Present (Stored_Constraint (Ti)) then
17586 Result :=
17587 Search_Derivation_Levels
17588 (Td, Stored_Constraint (Ti), True);
17589 else
17590 Result :=
17591 Search_Derivation_Levels
17592 (Td, Discrim_Values, Stored_Discrim_Values);
17593 end if;
17594 end if;
17595 end;
17596 end if;
17597
17598 -- Extra underlying places to search, if not found above. For
17599 -- concurrent types, the relevant discriminant appears in the
17600 -- corresponding record. For a type derived from a private type
17601 -- without discriminant, the full view inherits the discriminants
17602 -- of the full view of the parent.
17603
17604 if Result = Discriminant then
17605 if Is_Concurrent_Type (Ti)
17606 and then Present (Corresponding_Record_Type (Ti))
17607 then
17608 Result :=
17609 Search_Derivation_Levels (
17610 Corresponding_Record_Type (Ti),
17611 Discrim_Values,
17612 Stored_Discrim_Values);
17613
17614 elsif Is_Private_Type (Ti)
17615 and then not Has_Discriminants (Ti)
17616 and then Present (Full_View (Ti))
17617 and then Etype (Full_View (Ti)) /= Ti
17618 then
17619 Result :=
17620 Search_Derivation_Levels (
17621 Full_View (Ti),
17622 Discrim_Values,
17623 Stored_Discrim_Values);
17624 end if;
17625 end if;
17626
17627 -- If Result is not a (reference to a) discriminant, return it,
17628 -- otherwise set Result_Entity to the discriminant.
17629
17630 if Nkind (Result) = N_Defining_Identifier then
17631 pragma Assert (Result = Discriminant);
17632 Result_Entity := Result;
17633
17634 else
17635 if not Denotes_Discriminant (Result) then
17636 return Result;
17637 end if;
17638
17639 Result_Entity := Entity (Result);
17640 end if;
17641
17642 -- See if this level of derivation actually has discriminants because
17643 -- tagged derivations can add them, hence the lower levels need not
17644 -- have any.
17645
17646 if not Has_Discriminants (Ti) then
17647 return Result;
17648 end if;
17649
17650 -- Scan Ti's discriminants for Result_Entity, and return its
17651 -- corresponding value, if any.
17652
17653 Result_Entity := Original_Record_Component (Result_Entity);
17654
17655 Assoc := First_Elmt (Discrim_Values);
17656
17657 if Stored_Discrim_Values then
17658 Disc := First_Stored_Discriminant (Ti);
17659 else
17660 Disc := First_Discriminant (Ti);
17661 end if;
17662
17663 while Present (Disc) loop
17664 pragma Assert (Present (Assoc));
17665
17666 if Original_Record_Component (Disc) = Result_Entity then
17667 return Node (Assoc);
17668 end if;
17669
17670 Next_Elmt (Assoc);
17671
17672 if Stored_Discrim_Values then
17673 Next_Stored_Discriminant (Disc);
17674 else
17675 Next_Discriminant (Disc);
17676 end if;
17677 end loop;
17678
17679 -- Could not find it
17680
17681 return Result;
17682 end Search_Derivation_Levels;
17683
17684 -- Local Variables
17685
17686 Result : Node_Or_Entity_Id;
17687
17688 -- Start of processing for Get_Discriminant_Value
17689
17690 begin
17691 -- ??? This routine is a gigantic mess and will be deleted. For the
17692 -- time being just test for the trivial case before calling recurse.
17693
17694 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17695 declare
17696 D : Entity_Id;
17697 E : Elmt_Id;
17698
17699 begin
17700 D := First_Discriminant (Typ_For_Constraint);
17701 E := First_Elmt (Constraint);
17702 while Present (D) loop
17703 if Chars (D) = Chars (Discriminant) then
17704 return Node (E);
17705 end if;
17706
17707 Next_Discriminant (D);
17708 Next_Elmt (E);
17709 end loop;
17710 end;
17711 end if;
17712
17713 Result := Search_Derivation_Levels
17714 (Typ_For_Constraint, Constraint, False);
17715
17716 -- ??? hack to disappear when this routine is gone
17717
17718 if Nkind (Result) = N_Defining_Identifier then
17719 declare
17720 D : Entity_Id;
17721 E : Elmt_Id;
17722
17723 begin
17724 D := First_Discriminant (Typ_For_Constraint);
17725 E := First_Elmt (Constraint);
17726 while Present (D) loop
17727 if Root_Corresponding_Discriminant (D) = Discriminant then
17728 return Node (E);
17729 end if;
17730
17731 Next_Discriminant (D);
17732 Next_Elmt (E);
17733 end loop;
17734 end;
17735 end if;
17736
17737 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17738 return Result;
17739 end Get_Discriminant_Value;
17740
17741 --------------------------
17742 -- Has_Range_Constraint --
17743 --------------------------
17744
17745 function Has_Range_Constraint (N : Node_Id) return Boolean is
17746 C : constant Node_Id := Constraint (N);
17747
17748 begin
17749 if Nkind (C) = N_Range_Constraint then
17750 return True;
17751
17752 elsif Nkind (C) = N_Digits_Constraint then
17753 return
17754 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17755 or else Present (Range_Constraint (C));
17756
17757 elsif Nkind (C) = N_Delta_Constraint then
17758 return Present (Range_Constraint (C));
17759
17760 else
17761 return False;
17762 end if;
17763 end Has_Range_Constraint;
17764
17765 ------------------------
17766 -- Inherit_Components --
17767 ------------------------
17768
17769 function Inherit_Components
17770 (N : Node_Id;
17771 Parent_Base : Entity_Id;
17772 Derived_Base : Entity_Id;
17773 Is_Tagged : Boolean;
17774 Inherit_Discr : Boolean;
17775 Discs : Elist_Id) return Elist_Id
17776 is
17777 Assoc_List : constant Elist_Id := New_Elmt_List;
17778
17779 procedure Inherit_Component
17780 (Old_C : Entity_Id;
17781 Plain_Discrim : Boolean := False;
17782 Stored_Discrim : Boolean := False);
17783 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17784 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17785 -- True, Old_C is a stored discriminant. If they are both false then
17786 -- Old_C is a regular component.
17787
17788 -----------------------
17789 -- Inherit_Component --
17790 -----------------------
17791
17792 procedure Inherit_Component
17793 (Old_C : Entity_Id;
17794 Plain_Discrim : Boolean := False;
17795 Stored_Discrim : Boolean := False)
17796 is
17797 procedure Set_Anonymous_Type (Id : Entity_Id);
17798 -- Id denotes the entity of an access discriminant or anonymous
17799 -- access component. Set the type of Id to either the same type of
17800 -- Old_C or create a new one depending on whether the parent and
17801 -- the child types are in the same scope.
17802
17803 ------------------------
17804 -- Set_Anonymous_Type --
17805 ------------------------
17806
17807 procedure Set_Anonymous_Type (Id : Entity_Id) is
17808 Old_Typ : constant Entity_Id := Etype (Old_C);
17809
17810 begin
17811 if Scope (Parent_Base) = Scope (Derived_Base) then
17812 Set_Etype (Id, Old_Typ);
17813
17814 -- The parent and the derived type are in two different scopes.
17815 -- Reuse the type of the original discriminant / component by
17816 -- copying it in order to preserve all attributes.
17817
17818 else
17819 declare
17820 Typ : constant Entity_Id := New_Copy (Old_Typ);
17821
17822 begin
17823 Set_Etype (Id, Typ);
17824
17825 -- Since we do not generate component declarations for
17826 -- inherited components, associate the itype with the
17827 -- derived type.
17828
17829 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17830 Set_Scope (Typ, Derived_Base);
17831 end;
17832 end if;
17833 end Set_Anonymous_Type;
17834
17835 -- Local variables and constants
17836
17837 New_C : constant Entity_Id := New_Copy (Old_C);
17838
17839 Corr_Discrim : Entity_Id;
17840 Discrim : Entity_Id;
17841
17842 -- Start of processing for Inherit_Component
17843
17844 begin
17845 pragma Assert (not Is_Tagged or not Stored_Discrim);
17846
17847 Set_Parent (New_C, Parent (Old_C));
17848
17849 -- Regular discriminants and components must be inserted in the scope
17850 -- of the Derived_Base. Do it here.
17851
17852 if not Stored_Discrim then
17853 Enter_Name (New_C);
17854 end if;
17855
17856 -- For tagged types the Original_Record_Component must point to
17857 -- whatever this field was pointing to in the parent type. This has
17858 -- already been achieved by the call to New_Copy above.
17859
17860 if not Is_Tagged then
17861 Set_Original_Record_Component (New_C, New_C);
17862 end if;
17863
17864 -- Set the proper type of an access discriminant
17865
17866 if Ekind (New_C) = E_Discriminant
17867 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17868 then
17869 Set_Anonymous_Type (New_C);
17870 end if;
17871
17872 -- If we have inherited a component then see if its Etype contains
17873 -- references to Parent_Base discriminants. In this case, replace
17874 -- these references with the constraints given in Discs. We do not
17875 -- do this for the partial view of private types because this is
17876 -- not needed (only the components of the full view will be used
17877 -- for code generation) and cause problem. We also avoid this
17878 -- transformation in some error situations.
17879
17880 if Ekind (New_C) = E_Component then
17881
17882 -- Set the proper type of an anonymous access component
17883
17884 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17885 Set_Anonymous_Type (New_C);
17886
17887 elsif (Is_Private_Type (Derived_Base)
17888 and then not Is_Generic_Type (Derived_Base))
17889 or else (Is_Empty_Elmt_List (Discs)
17890 and then not Expander_Active)
17891 then
17892 Set_Etype (New_C, Etype (Old_C));
17893
17894 else
17895 -- The current component introduces a circularity of the
17896 -- following kind:
17897
17898 -- limited with Pack_2;
17899 -- package Pack_1 is
17900 -- type T_1 is tagged record
17901 -- Comp : access Pack_2.T_2;
17902 -- ...
17903 -- end record;
17904 -- end Pack_1;
17905
17906 -- with Pack_1;
17907 -- package Pack_2 is
17908 -- type T_2 is new Pack_1.T_1 with ...;
17909 -- end Pack_2;
17910
17911 Set_Etype
17912 (New_C,
17913 Constrain_Component_Type
17914 (Old_C, Derived_Base, N, Parent_Base, Discs));
17915 end if;
17916 end if;
17917
17918 -- In derived tagged types it is illegal to reference a non
17919 -- discriminant component in the parent type. To catch this, mark
17920 -- these components with an Ekind of E_Void. This will be reset in
17921 -- Record_Type_Definition after processing the record extension of
17922 -- the derived type.
17923
17924 -- If the declaration is a private extension, there is no further
17925 -- record extension to process, and the components retain their
17926 -- current kind, because they are visible at this point.
17927
17928 if Is_Tagged and then Ekind (New_C) = E_Component
17929 and then Nkind (N) /= N_Private_Extension_Declaration
17930 then
17931 Set_Ekind (New_C, E_Void);
17932 end if;
17933
17934 if Plain_Discrim then
17935 Set_Corresponding_Discriminant (New_C, Old_C);
17936 Build_Discriminal (New_C);
17937
17938 -- If we are explicitly inheriting a stored discriminant it will be
17939 -- completely hidden.
17940
17941 elsif Stored_Discrim then
17942 Set_Corresponding_Discriminant (New_C, Empty);
17943 Set_Discriminal (New_C, Empty);
17944 Set_Is_Completely_Hidden (New_C);
17945
17946 -- Set the Original_Record_Component of each discriminant in the
17947 -- derived base to point to the corresponding stored that we just
17948 -- created.
17949
17950 Discrim := First_Discriminant (Derived_Base);
17951 while Present (Discrim) loop
17952 Corr_Discrim := Corresponding_Discriminant (Discrim);
17953
17954 -- Corr_Discrim could be missing in an error situation
17955
17956 if Present (Corr_Discrim)
17957 and then Original_Record_Component (Corr_Discrim) = Old_C
17958 then
17959 Set_Original_Record_Component (Discrim, New_C);
17960 end if;
17961
17962 Next_Discriminant (Discrim);
17963 end loop;
17964
17965 Append_Entity (New_C, Derived_Base);
17966 end if;
17967
17968 if not Is_Tagged then
17969 Append_Elmt (Old_C, Assoc_List);
17970 Append_Elmt (New_C, Assoc_List);
17971 end if;
17972 end Inherit_Component;
17973
17974 -- Variables local to Inherit_Component
17975
17976 Loc : constant Source_Ptr := Sloc (N);
17977
17978 Parent_Discrim : Entity_Id;
17979 Stored_Discrim : Entity_Id;
17980 D : Entity_Id;
17981 Component : Entity_Id;
17982
17983 -- Start of processing for Inherit_Components
17984
17985 begin
17986 if not Is_Tagged then
17987 Append_Elmt (Parent_Base, Assoc_List);
17988 Append_Elmt (Derived_Base, Assoc_List);
17989 end if;
17990
17991 -- Inherit parent discriminants if needed
17992
17993 if Inherit_Discr then
17994 Parent_Discrim := First_Discriminant (Parent_Base);
17995 while Present (Parent_Discrim) loop
17996 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17997 Next_Discriminant (Parent_Discrim);
17998 end loop;
17999 end if;
18000
18001 -- Create explicit stored discrims for untagged types when necessary
18002
18003 if not Has_Unknown_Discriminants (Derived_Base)
18004 and then Has_Discriminants (Parent_Base)
18005 and then not Is_Tagged
18006 and then
18007 (not Inherit_Discr
18008 or else First_Discriminant (Parent_Base) /=
18009 First_Stored_Discriminant (Parent_Base))
18010 then
18011 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18012 while Present (Stored_Discrim) loop
18013 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18014 Next_Stored_Discriminant (Stored_Discrim);
18015 end loop;
18016 end if;
18017
18018 -- See if we can apply the second transformation for derived types, as
18019 -- explained in point 6. in the comments above Build_Derived_Record_Type
18020 -- This is achieved by appending Derived_Base discriminants into Discs,
18021 -- which has the side effect of returning a non empty Discs list to the
18022 -- caller of Inherit_Components, which is what we want. This must be
18023 -- done for private derived types if there are explicit stored
18024 -- discriminants, to ensure that we can retrieve the values of the
18025 -- constraints provided in the ancestors.
18026
18027 if Inherit_Discr
18028 and then Is_Empty_Elmt_List (Discs)
18029 and then Present (First_Discriminant (Derived_Base))
18030 and then
18031 (not Is_Private_Type (Derived_Base)
18032 or else Is_Completely_Hidden
18033 (First_Stored_Discriminant (Derived_Base))
18034 or else Is_Generic_Type (Derived_Base))
18035 then
18036 D := First_Discriminant (Derived_Base);
18037 while Present (D) loop
18038 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18039 Next_Discriminant (D);
18040 end loop;
18041 end if;
18042
18043 -- Finally, inherit non-discriminant components unless they are not
18044 -- visible because defined or inherited from the full view of the
18045 -- parent. Don't inherit the _parent field of the parent type.
18046
18047 Component := First_Entity (Parent_Base);
18048 while Present (Component) loop
18049
18050 -- Ada 2005 (AI-251): Do not inherit components associated with
18051 -- secondary tags of the parent.
18052
18053 if Ekind (Component) = E_Component
18054 and then Present (Related_Type (Component))
18055 then
18056 null;
18057
18058 elsif Ekind (Component) /= E_Component
18059 or else Chars (Component) = Name_uParent
18060 then
18061 null;
18062
18063 -- If the derived type is within the parent type's declarative
18064 -- region, then the components can still be inherited even though
18065 -- they aren't visible at this point. This can occur for cases
18066 -- such as within public child units where the components must
18067 -- become visible upon entering the child unit's private part.
18068
18069 elsif not Is_Visible_Component (Component)
18070 and then not In_Open_Scopes (Scope (Parent_Base))
18071 then
18072 null;
18073
18074 elsif Ekind_In (Derived_Base, E_Private_Type,
18075 E_Limited_Private_Type)
18076 then
18077 null;
18078
18079 else
18080 Inherit_Component (Component);
18081 end if;
18082
18083 Next_Entity (Component);
18084 end loop;
18085
18086 -- For tagged derived types, inherited discriminants cannot be used in
18087 -- component declarations of the record extension part. To achieve this
18088 -- we mark the inherited discriminants as not visible.
18089
18090 if Is_Tagged and then Inherit_Discr then
18091 D := First_Discriminant (Derived_Base);
18092 while Present (D) loop
18093 Set_Is_Immediately_Visible (D, False);
18094 Next_Discriminant (D);
18095 end loop;
18096 end if;
18097
18098 return Assoc_List;
18099 end Inherit_Components;
18100
18101 -----------------------------
18102 -- Inherit_Predicate_Flags --
18103 -----------------------------
18104
18105 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18106 begin
18107 Set_Has_Predicates (Subt, Has_Predicates (Par));
18108 Set_Has_Static_Predicate_Aspect
18109 (Subt, Has_Static_Predicate_Aspect (Par));
18110 Set_Has_Dynamic_Predicate_Aspect
18111 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18112 end Inherit_Predicate_Flags;
18113
18114 ----------------------
18115 -- Is_EVF_Procedure --
18116 ----------------------
18117
18118 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18119 Formal : Entity_Id;
18120
18121 begin
18122 -- Examine the formals of an Extensions_Visible False procedure looking
18123 -- for a controlling OUT parameter.
18124
18125 if Ekind (Subp) = E_Procedure
18126 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18127 then
18128 Formal := First_Formal (Subp);
18129 while Present (Formal) loop
18130 if Ekind (Formal) = E_Out_Parameter
18131 and then Is_Controlling_Formal (Formal)
18132 then
18133 return True;
18134 end if;
18135
18136 Next_Formal (Formal);
18137 end loop;
18138 end if;
18139
18140 return False;
18141 end Is_EVF_Procedure;
18142
18143 -----------------------
18144 -- Is_Null_Extension --
18145 -----------------------
18146
18147 function Is_Null_Extension (T : Entity_Id) return Boolean is
18148 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18149 Comp_List : Node_Id;
18150 Comp : Node_Id;
18151
18152 begin
18153 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18154 or else not Is_Tagged_Type (T)
18155 or else Nkind (Type_Definition (Type_Decl)) /=
18156 N_Derived_Type_Definition
18157 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18158 then
18159 return False;
18160 end if;
18161
18162 Comp_List :=
18163 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18164
18165 if Present (Discriminant_Specifications (Type_Decl)) then
18166 return False;
18167
18168 elsif Present (Comp_List)
18169 and then Is_Non_Empty_List (Component_Items (Comp_List))
18170 then
18171 Comp := First (Component_Items (Comp_List));
18172
18173 -- Only user-defined components are relevant. The component list
18174 -- may also contain a parent component and internal components
18175 -- corresponding to secondary tags, but these do not determine
18176 -- whether this is a null extension.
18177
18178 while Present (Comp) loop
18179 if Comes_From_Source (Comp) then
18180 return False;
18181 end if;
18182
18183 Next (Comp);
18184 end loop;
18185
18186 return True;
18187
18188 else
18189 return True;
18190 end if;
18191 end Is_Null_Extension;
18192
18193 ------------------------------
18194 -- Is_Valid_Constraint_Kind --
18195 ------------------------------
18196
18197 function Is_Valid_Constraint_Kind
18198 (T_Kind : Type_Kind;
18199 Constraint_Kind : Node_Kind) return Boolean
18200 is
18201 begin
18202 case T_Kind is
18203 when Enumeration_Kind
18204 | Integer_Kind
18205 =>
18206 return Constraint_Kind = N_Range_Constraint;
18207
18208 when Decimal_Fixed_Point_Kind =>
18209 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18210 N_Range_Constraint);
18211
18212 when Ordinary_Fixed_Point_Kind =>
18213 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18214 N_Range_Constraint);
18215
18216 when Float_Kind =>
18217 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18218 N_Range_Constraint);
18219
18220 when Access_Kind
18221 | Array_Kind
18222 | Class_Wide_Kind
18223 | Concurrent_Kind
18224 | Private_Kind
18225 | E_Incomplete_Type
18226 | E_Record_Subtype
18227 | E_Record_Type
18228 =>
18229 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18230
18231 when others =>
18232 return True; -- Error will be detected later
18233 end case;
18234 end Is_Valid_Constraint_Kind;
18235
18236 --------------------------
18237 -- Is_Visible_Component --
18238 --------------------------
18239
18240 function Is_Visible_Component
18241 (C : Entity_Id;
18242 N : Node_Id := Empty) return Boolean
18243 is
18244 Original_Comp : Entity_Id := Empty;
18245 Original_Type : Entity_Id;
18246 Type_Scope : Entity_Id;
18247
18248 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18249 -- Check whether parent type of inherited component is declared locally,
18250 -- possibly within a nested package or instance. The current scope is
18251 -- the derived record itself.
18252
18253 -------------------
18254 -- Is_Local_Type --
18255 -------------------
18256
18257 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18258 Scop : Entity_Id;
18259
18260 begin
18261 Scop := Scope (Typ);
18262 while Present (Scop)
18263 and then Scop /= Standard_Standard
18264 loop
18265 if Scop = Scope (Current_Scope) then
18266 return True;
18267 end if;
18268
18269 Scop := Scope (Scop);
18270 end loop;
18271
18272 return False;
18273 end Is_Local_Type;
18274
18275 -- Start of processing for Is_Visible_Component
18276
18277 begin
18278 if Ekind_In (C, E_Component, E_Discriminant) then
18279 Original_Comp := Original_Record_Component (C);
18280 end if;
18281
18282 if No (Original_Comp) then
18283
18284 -- Premature usage, or previous error
18285
18286 return False;
18287
18288 else
18289 Original_Type := Scope (Original_Comp);
18290 Type_Scope := Scope (Base_Type (Scope (C)));
18291 end if;
18292
18293 -- This test only concerns tagged types
18294
18295 if not Is_Tagged_Type (Original_Type) then
18296 return True;
18297
18298 -- If it is _Parent or _Tag, there is no visibility issue
18299
18300 elsif not Comes_From_Source (Original_Comp) then
18301 return True;
18302
18303 -- Discriminants are visible unless the (private) type has unknown
18304 -- discriminants. If the discriminant reference is inserted for a
18305 -- discriminant check on a full view it is also visible.
18306
18307 elsif Ekind (Original_Comp) = E_Discriminant
18308 and then
18309 (not Has_Unknown_Discriminants (Original_Type)
18310 or else (Present (N)
18311 and then Nkind (N) = N_Selected_Component
18312 and then Nkind (Prefix (N)) = N_Type_Conversion
18313 and then not Comes_From_Source (Prefix (N))))
18314 then
18315 return True;
18316
18317 -- In the body of an instantiation, check the visibility of a component
18318 -- in case it has a homograph that is a primitive operation of a private
18319 -- type which was not visible in the generic unit.
18320
18321 -- Should Is_Prefixed_Call be propagated from template to instance???
18322
18323 elsif In_Instance_Body then
18324 if not Is_Tagged_Type (Original_Type)
18325 or else not Is_Private_Type (Original_Type)
18326 then
18327 return True;
18328
18329 else
18330 declare
18331 Subp_Elmt : Elmt_Id;
18332
18333 begin
18334 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18335 while Present (Subp_Elmt) loop
18336
18337 -- The component is hidden by a primitive operation
18338
18339 if Chars (Node (Subp_Elmt)) = Chars (C) then
18340 return False;
18341 end if;
18342
18343 Next_Elmt (Subp_Elmt);
18344 end loop;
18345
18346 return True;
18347 end;
18348 end if;
18349
18350 -- If the component has been declared in an ancestor which is currently
18351 -- a private type, then it is not visible. The same applies if the
18352 -- component's containing type is not in an open scope and the original
18353 -- component's enclosing type is a visible full view of a private type
18354 -- (which can occur in cases where an attempt is being made to reference
18355 -- a component in a sibling package that is inherited from a visible
18356 -- component of a type in an ancestor package; the component in the
18357 -- sibling package should not be visible even though the component it
18358 -- inherited from is visible). This does not apply however in the case
18359 -- where the scope of the type is a private child unit, or when the
18360 -- parent comes from a local package in which the ancestor is currently
18361 -- visible. The latter suppression of visibility is needed for cases
18362 -- that are tested in B730006.
18363
18364 elsif Is_Private_Type (Original_Type)
18365 or else
18366 (not Is_Private_Descendant (Type_Scope)
18367 and then not In_Open_Scopes (Type_Scope)
18368 and then Has_Private_Declaration (Original_Type))
18369 then
18370 -- If the type derives from an entity in a formal package, there
18371 -- are no additional visible components.
18372
18373 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18374 N_Formal_Package_Declaration
18375 then
18376 return False;
18377
18378 -- if we are not in the private part of the current package, there
18379 -- are no additional visible components.
18380
18381 elsif Ekind (Scope (Current_Scope)) = E_Package
18382 and then not In_Private_Part (Scope (Current_Scope))
18383 then
18384 return False;
18385 else
18386 return
18387 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18388 and then In_Open_Scopes (Scope (Original_Type))
18389 and then Is_Local_Type (Type_Scope);
18390 end if;
18391
18392 -- There is another weird way in which a component may be invisible when
18393 -- the private and the full view are not derived from the same ancestor.
18394 -- Here is an example :
18395
18396 -- type A1 is tagged record F1 : integer; end record;
18397 -- type A2 is new A1 with record F2 : integer; end record;
18398 -- type T is new A1 with private;
18399 -- private
18400 -- type T is new A2 with null record;
18401
18402 -- In this case, the full view of T inherits F1 and F2 but the private
18403 -- view inherits only F1
18404
18405 else
18406 declare
18407 Ancestor : Entity_Id := Scope (C);
18408
18409 begin
18410 loop
18411 if Ancestor = Original_Type then
18412 return True;
18413
18414 -- The ancestor may have a partial view of the original type,
18415 -- but if the full view is in scope, as in a child body, the
18416 -- component is visible.
18417
18418 elsif In_Private_Part (Scope (Original_Type))
18419 and then Full_View (Ancestor) = Original_Type
18420 then
18421 return True;
18422
18423 elsif Ancestor = Etype (Ancestor) then
18424
18425 -- No further ancestors to examine
18426
18427 return False;
18428 end if;
18429
18430 Ancestor := Etype (Ancestor);
18431 end loop;
18432 end;
18433 end if;
18434 end Is_Visible_Component;
18435
18436 --------------------------
18437 -- Make_Class_Wide_Type --
18438 --------------------------
18439
18440 procedure Make_Class_Wide_Type (T : Entity_Id) is
18441 CW_Type : Entity_Id;
18442 CW_Name : Name_Id;
18443 Next_E : Entity_Id;
18444
18445 begin
18446 if Present (Class_Wide_Type (T)) then
18447
18448 -- The class-wide type is a partially decorated entity created for a
18449 -- unanalyzed tagged type referenced through a limited with clause.
18450 -- When the tagged type is analyzed, its class-wide type needs to be
18451 -- redecorated. Note that we reuse the entity created by Decorate_
18452 -- Tagged_Type in order to preserve all links.
18453
18454 if Materialize_Entity (Class_Wide_Type (T)) then
18455 CW_Type := Class_Wide_Type (T);
18456 Set_Materialize_Entity (CW_Type, False);
18457
18458 -- The class wide type can have been defined by the partial view, in
18459 -- which case everything is already done.
18460
18461 else
18462 return;
18463 end if;
18464
18465 -- Default case, we need to create a new class-wide type
18466
18467 else
18468 CW_Type :=
18469 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18470 end if;
18471
18472 -- Inherit root type characteristics
18473
18474 CW_Name := Chars (CW_Type);
18475 Next_E := Next_Entity (CW_Type);
18476 Copy_Node (T, CW_Type);
18477 Set_Comes_From_Source (CW_Type, False);
18478 Set_Chars (CW_Type, CW_Name);
18479 Set_Parent (CW_Type, Parent (T));
18480 Set_Next_Entity (CW_Type, Next_E);
18481
18482 -- Ensure we have a new freeze node for the class-wide type. The partial
18483 -- view may have freeze action of its own, requiring a proper freeze
18484 -- node, and the same freeze node cannot be shared between the two
18485 -- types.
18486
18487 Set_Has_Delayed_Freeze (CW_Type);
18488 Set_Freeze_Node (CW_Type, Empty);
18489
18490 -- Customize the class-wide type: It has no prim. op., it cannot be
18491 -- abstract, its Etype points back to the specific root type, and it
18492 -- cannot have any invariants.
18493
18494 Set_Ekind (CW_Type, E_Class_Wide_Type);
18495 Set_Is_Tagged_Type (CW_Type, True);
18496 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18497 Set_Is_Abstract_Type (CW_Type, False);
18498 Set_Is_Constrained (CW_Type, False);
18499 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18500 Set_Default_SSO (CW_Type);
18501 Set_Has_Inheritable_Invariants (CW_Type, False);
18502 Set_Has_Inherited_Invariants (CW_Type, False);
18503 Set_Has_Own_Invariants (CW_Type, False);
18504
18505 if Ekind (T) = E_Class_Wide_Subtype then
18506 Set_Etype (CW_Type, Etype (Base_Type (T)));
18507 else
18508 Set_Etype (CW_Type, T);
18509 end if;
18510
18511 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18512
18513 -- If this is the class_wide type of a constrained subtype, it does
18514 -- not have discriminants.
18515
18516 Set_Has_Discriminants (CW_Type,
18517 Has_Discriminants (T) and then not Is_Constrained (T));
18518
18519 Set_Has_Unknown_Discriminants (CW_Type, True);
18520 Set_Class_Wide_Type (T, CW_Type);
18521 Set_Equivalent_Type (CW_Type, Empty);
18522
18523 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18524
18525 Set_Class_Wide_Type (CW_Type, CW_Type);
18526 end Make_Class_Wide_Type;
18527
18528 ----------------
18529 -- Make_Index --
18530 ----------------
18531
18532 procedure Make_Index
18533 (N : Node_Id;
18534 Related_Nod : Node_Id;
18535 Related_Id : Entity_Id := Empty;
18536 Suffix_Index : Nat := 1;
18537 In_Iter_Schm : Boolean := False)
18538 is
18539 R : Node_Id;
18540 T : Entity_Id;
18541 Def_Id : Entity_Id := Empty;
18542 Found : Boolean := False;
18543
18544 begin
18545 -- For a discrete range used in a constrained array definition and
18546 -- defined by a range, an implicit conversion to the predefined type
18547 -- INTEGER is assumed if each bound is either a numeric literal, a named
18548 -- number, or an attribute, and the type of both bounds (prior to the
18549 -- implicit conversion) is the type universal_integer. Otherwise, both
18550 -- bounds must be of the same discrete type, other than universal
18551 -- integer; this type must be determinable independently of the
18552 -- context, but using the fact that the type must be discrete and that
18553 -- both bounds must have the same type.
18554
18555 -- Character literals also have a universal type in the absence of
18556 -- of additional context, and are resolved to Standard_Character.
18557
18558 if Nkind (N) = N_Range then
18559
18560 -- The index is given by a range constraint. The bounds are known
18561 -- to be of a consistent type.
18562
18563 if not Is_Overloaded (N) then
18564 T := Etype (N);
18565
18566 -- For universal bounds, choose the specific predefined type
18567
18568 if T = Universal_Integer then
18569 T := Standard_Integer;
18570
18571 elsif T = Any_Character then
18572 Ambiguous_Character (Low_Bound (N));
18573
18574 T := Standard_Character;
18575 end if;
18576
18577 -- The node may be overloaded because some user-defined operators
18578 -- are available, but if a universal interpretation exists it is
18579 -- also the selected one.
18580
18581 elsif Universal_Interpretation (N) = Universal_Integer then
18582 T := Standard_Integer;
18583
18584 else
18585 T := Any_Type;
18586
18587 declare
18588 Ind : Interp_Index;
18589 It : Interp;
18590
18591 begin
18592 Get_First_Interp (N, Ind, It);
18593 while Present (It.Typ) loop
18594 if Is_Discrete_Type (It.Typ) then
18595
18596 if Found
18597 and then not Covers (It.Typ, T)
18598 and then not Covers (T, It.Typ)
18599 then
18600 Error_Msg_N ("ambiguous bounds in discrete range", N);
18601 exit;
18602 else
18603 T := It.Typ;
18604 Found := True;
18605 end if;
18606 end if;
18607
18608 Get_Next_Interp (Ind, It);
18609 end loop;
18610
18611 if T = Any_Type then
18612 Error_Msg_N ("discrete type required for range", N);
18613 Set_Etype (N, Any_Type);
18614 return;
18615
18616 elsif T = Universal_Integer then
18617 T := Standard_Integer;
18618 end if;
18619 end;
18620 end if;
18621
18622 if not Is_Discrete_Type (T) then
18623 Error_Msg_N ("discrete type required for range", N);
18624 Set_Etype (N, Any_Type);
18625 return;
18626 end if;
18627
18628 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18629 and then Attribute_Name (Low_Bound (N)) = Name_First
18630 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18631 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18632 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18633 then
18634 -- The type of the index will be the type of the prefix, as long
18635 -- as the upper bound is 'Last of the same type.
18636
18637 Def_Id := Entity (Prefix (Low_Bound (N)));
18638
18639 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18640 or else Attribute_Name (High_Bound (N)) /= Name_Last
18641 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18642 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18643 then
18644 Def_Id := Empty;
18645 end if;
18646 end if;
18647
18648 R := N;
18649 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18650
18651 elsif Nkind (N) = N_Subtype_Indication then
18652
18653 -- The index is given by a subtype with a range constraint
18654
18655 T := Base_Type (Entity (Subtype_Mark (N)));
18656
18657 if not Is_Discrete_Type (T) then
18658 Error_Msg_N ("discrete type required for range", N);
18659 Set_Etype (N, Any_Type);
18660 return;
18661 end if;
18662
18663 R := Range_Expression (Constraint (N));
18664
18665 Resolve (R, T);
18666 Process_Range_Expr_In_Decl
18667 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18668
18669 elsif Nkind (N) = N_Attribute_Reference then
18670
18671 -- Catch beginner's error (use of attribute other than 'Range)
18672
18673 if Attribute_Name (N) /= Name_Range then
18674 Error_Msg_N ("expect attribute ''Range", N);
18675 Set_Etype (N, Any_Type);
18676 return;
18677 end if;
18678
18679 -- If the node denotes the range of a type mark, that is also the
18680 -- resulting type, and we do not need to create an Itype for it.
18681
18682 if Is_Entity_Name (Prefix (N))
18683 and then Comes_From_Source (N)
18684 and then Is_Type (Entity (Prefix (N)))
18685 and then Is_Discrete_Type (Entity (Prefix (N)))
18686 then
18687 Def_Id := Entity (Prefix (N));
18688 end if;
18689
18690 Analyze_And_Resolve (N);
18691 T := Etype (N);
18692 R := N;
18693
18694 -- If none of the above, must be a subtype. We convert this to a
18695 -- range attribute reference because in the case of declared first
18696 -- named subtypes, the types in the range reference can be different
18697 -- from the type of the entity. A range attribute normalizes the
18698 -- reference and obtains the correct types for the bounds.
18699
18700 -- This transformation is in the nature of an expansion, is only
18701 -- done if expansion is active. In particular, it is not done on
18702 -- formal generic types, because we need to retain the name of the
18703 -- original index for instantiation purposes.
18704
18705 else
18706 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18707 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18708 Set_Etype (N, Any_Integer);
18709 return;
18710
18711 else
18712 -- The type mark may be that of an incomplete type. It is only
18713 -- now that we can get the full view, previous analysis does
18714 -- not look specifically for a type mark.
18715
18716 Set_Entity (N, Get_Full_View (Entity (N)));
18717 Set_Etype (N, Entity (N));
18718 Def_Id := Entity (N);
18719
18720 if not Is_Discrete_Type (Def_Id) then
18721 Error_Msg_N ("discrete type required for index", N);
18722 Set_Etype (N, Any_Type);
18723 return;
18724 end if;
18725 end if;
18726
18727 if Expander_Active then
18728 Rewrite (N,
18729 Make_Attribute_Reference (Sloc (N),
18730 Attribute_Name => Name_Range,
18731 Prefix => Relocate_Node (N)));
18732
18733 -- The original was a subtype mark that does not freeze. This
18734 -- means that the rewritten version must not freeze either.
18735
18736 Set_Must_Not_Freeze (N);
18737 Set_Must_Not_Freeze (Prefix (N));
18738 Analyze_And_Resolve (N);
18739 T := Etype (N);
18740 R := N;
18741
18742 -- If expander is inactive, type is legal, nothing else to construct
18743
18744 else
18745 return;
18746 end if;
18747 end if;
18748
18749 if not Is_Discrete_Type (T) then
18750 Error_Msg_N ("discrete type required for range", N);
18751 Set_Etype (N, Any_Type);
18752 return;
18753
18754 elsif T = Any_Type then
18755 Set_Etype (N, Any_Type);
18756 return;
18757 end if;
18758
18759 -- We will now create the appropriate Itype to describe the range, but
18760 -- first a check. If we originally had a subtype, then we just label
18761 -- the range with this subtype. Not only is there no need to construct
18762 -- a new subtype, but it is wrong to do so for two reasons:
18763
18764 -- 1. A legality concern, if we have a subtype, it must not freeze,
18765 -- and the Itype would cause freezing incorrectly
18766
18767 -- 2. An efficiency concern, if we created an Itype, it would not be
18768 -- recognized as the same type for the purposes of eliminating
18769 -- checks in some circumstances.
18770
18771 -- We signal this case by setting the subtype entity in Def_Id
18772
18773 if No (Def_Id) then
18774 Def_Id :=
18775 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18776 Set_Etype (Def_Id, Base_Type (T));
18777
18778 if Is_Signed_Integer_Type (T) then
18779 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18780
18781 elsif Is_Modular_Integer_Type (T) then
18782 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18783
18784 else
18785 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18786 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18787 Set_First_Literal (Def_Id, First_Literal (T));
18788 end if;
18789
18790 Set_Size_Info (Def_Id, (T));
18791 Set_RM_Size (Def_Id, RM_Size (T));
18792 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18793
18794 Set_Scalar_Range (Def_Id, R);
18795 Conditional_Delay (Def_Id, T);
18796
18797 if Nkind (N) = N_Subtype_Indication then
18798 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18799 end if;
18800
18801 -- In the subtype indication case, if the immediate parent of the
18802 -- new subtype is non-static, then the subtype we create is non-
18803 -- static, even if its bounds are static.
18804
18805 if Nkind (N) = N_Subtype_Indication
18806 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18807 then
18808 Set_Is_Non_Static_Subtype (Def_Id);
18809 end if;
18810 end if;
18811
18812 -- Final step is to label the index with this constructed type
18813
18814 Set_Etype (N, Def_Id);
18815 end Make_Index;
18816
18817 ------------------------------
18818 -- Modular_Type_Declaration --
18819 ------------------------------
18820
18821 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18822 Mod_Expr : constant Node_Id := Expression (Def);
18823 M_Val : Uint;
18824
18825 procedure Set_Modular_Size (Bits : Int);
18826 -- Sets RM_Size to Bits, and Esize to normal word size above this
18827
18828 ----------------------
18829 -- Set_Modular_Size --
18830 ----------------------
18831
18832 procedure Set_Modular_Size (Bits : Int) is
18833 begin
18834 Set_RM_Size (T, UI_From_Int (Bits));
18835
18836 if Bits <= 8 then
18837 Init_Esize (T, 8);
18838
18839 elsif Bits <= 16 then
18840 Init_Esize (T, 16);
18841
18842 elsif Bits <= 32 then
18843 Init_Esize (T, 32);
18844
18845 else
18846 Init_Esize (T, System_Max_Binary_Modulus_Power);
18847 end if;
18848
18849 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18850 Set_Is_Known_Valid (T);
18851 end if;
18852 end Set_Modular_Size;
18853
18854 -- Start of processing for Modular_Type_Declaration
18855
18856 begin
18857 -- If the mod expression is (exactly) 2 * literal, where literal is
18858 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18859
18860 if Warn_On_Suspicious_Modulus_Value
18861 and then Nkind (Mod_Expr) = N_Op_Multiply
18862 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18863 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18864 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18865 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18866 then
18867 Error_Msg_N
18868 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18869 end if;
18870
18871 -- Proceed with analysis of mod expression
18872
18873 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18874 Set_Etype (T, T);
18875 Set_Ekind (T, E_Modular_Integer_Type);
18876 Init_Alignment (T);
18877 Set_Is_Constrained (T);
18878
18879 if not Is_OK_Static_Expression (Mod_Expr) then
18880 Flag_Non_Static_Expr
18881 ("non-static expression used for modular type bound!", Mod_Expr);
18882 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18883 else
18884 M_Val := Expr_Value (Mod_Expr);
18885 end if;
18886
18887 if M_Val < 1 then
18888 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18889 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18890 end if;
18891
18892 if M_Val > 2 ** Standard_Long_Integer_Size then
18893 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18894 end if;
18895
18896 Set_Modulus (T, M_Val);
18897
18898 -- Create bounds for the modular type based on the modulus given in
18899 -- the type declaration and then analyze and resolve those bounds.
18900
18901 Set_Scalar_Range (T,
18902 Make_Range (Sloc (Mod_Expr),
18903 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18904 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18905
18906 -- Properly analyze the literals for the range. We do this manually
18907 -- because we can't go calling Resolve, since we are resolving these
18908 -- bounds with the type, and this type is certainly not complete yet.
18909
18910 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18911 Set_Etype (High_Bound (Scalar_Range (T)), T);
18912 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18913 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18914
18915 -- Loop through powers of two to find number of bits required
18916
18917 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18918
18919 -- Binary case
18920
18921 if M_Val = 2 ** Bits then
18922 Set_Modular_Size (Bits);
18923 return;
18924
18925 -- Nonbinary case
18926
18927 elsif M_Val < 2 ** Bits then
18928 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18929 Set_Non_Binary_Modulus (T);
18930
18931 if Bits > System_Max_Nonbinary_Modulus_Power then
18932 Error_Msg_Uint_1 :=
18933 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18934 Error_Msg_F
18935 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18936 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18937 return;
18938
18939 else
18940 -- In the nonbinary case, set size as per RM 13.3(55)
18941
18942 Set_Modular_Size (Bits);
18943 return;
18944 end if;
18945 end if;
18946
18947 end loop;
18948
18949 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18950 -- so we just signal an error and set the maximum size.
18951
18952 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18953 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18954
18955 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18956 Init_Alignment (T);
18957
18958 end Modular_Type_Declaration;
18959
18960 --------------------------
18961 -- New_Concatenation_Op --
18962 --------------------------
18963
18964 procedure New_Concatenation_Op (Typ : Entity_Id) is
18965 Loc : constant Source_Ptr := Sloc (Typ);
18966 Op : Entity_Id;
18967
18968 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18969 -- Create abbreviated declaration for the formal of a predefined
18970 -- Operator 'Op' of type 'Typ'
18971
18972 --------------------
18973 -- Make_Op_Formal --
18974 --------------------
18975
18976 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18977 Formal : Entity_Id;
18978 begin
18979 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18980 Set_Etype (Formal, Typ);
18981 Set_Mechanism (Formal, Default_Mechanism);
18982 return Formal;
18983 end Make_Op_Formal;
18984
18985 -- Start of processing for New_Concatenation_Op
18986
18987 begin
18988 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18989
18990 Set_Ekind (Op, E_Operator);
18991 Set_Scope (Op, Current_Scope);
18992 Set_Etype (Op, Typ);
18993 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18994 Set_Is_Immediately_Visible (Op);
18995 Set_Is_Intrinsic_Subprogram (Op);
18996 Set_Has_Completion (Op);
18997 Append_Entity (Op, Current_Scope);
18998
18999 Set_Name_Entity_Id (Name_Op_Concat, Op);
19000
19001 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19002 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19003 end New_Concatenation_Op;
19004
19005 -------------------------
19006 -- OK_For_Limited_Init --
19007 -------------------------
19008
19009 -- ???Check all calls of this, and compare the conditions under which it's
19010 -- called.
19011
19012 function OK_For_Limited_Init
19013 (Typ : Entity_Id;
19014 Exp : Node_Id) return Boolean
19015 is
19016 begin
19017 return Is_CPP_Constructor_Call (Exp)
19018 or else (Ada_Version >= Ada_2005
19019 and then not Debug_Flag_Dot_L
19020 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19021 end OK_For_Limited_Init;
19022
19023 -------------------------------
19024 -- OK_For_Limited_Init_In_05 --
19025 -------------------------------
19026
19027 function OK_For_Limited_Init_In_05
19028 (Typ : Entity_Id;
19029 Exp : Node_Id) return Boolean
19030 is
19031 begin
19032 -- An object of a limited interface type can be initialized with any
19033 -- expression of a nonlimited descendant type. However this does not
19034 -- apply if this is a view conversion of some other expression. This
19035 -- is checked below.
19036
19037 if Is_Class_Wide_Type (Typ)
19038 and then Is_Limited_Interface (Typ)
19039 and then not Is_Limited_Type (Etype (Exp))
19040 and then Nkind (Exp) /= N_Type_Conversion
19041 then
19042 return True;
19043 end if;
19044
19045 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19046 -- case of limited aggregates (including extension aggregates), and
19047 -- function calls. The function call may have been given in prefixed
19048 -- notation, in which case the original node is an indexed component.
19049 -- If the function is parameterless, the original node was an explicit
19050 -- dereference. The function may also be parameterless, in which case
19051 -- the source node is just an identifier.
19052
19053 -- A branch of a conditional expression may have been removed if the
19054 -- condition is statically known. This happens during expansion, and
19055 -- thus will not happen if previous errors were encountered. The check
19056 -- will have been performed on the chosen branch, which replaces the
19057 -- original conditional expression.
19058
19059 if No (Exp) then
19060 return True;
19061 end if;
19062
19063 case Nkind (Original_Node (Exp)) is
19064 when N_Aggregate
19065 | N_Extension_Aggregate
19066 | N_Function_Call
19067 | N_Op
19068 =>
19069 return True;
19070
19071 when N_Identifier =>
19072 return Present (Entity (Original_Node (Exp)))
19073 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19074
19075 when N_Qualified_Expression =>
19076 return
19077 OK_For_Limited_Init_In_05
19078 (Typ, Expression (Original_Node (Exp)));
19079
19080 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19081 -- with a function call, the expander has rewritten the call into an
19082 -- N_Type_Conversion node to force displacement of the pointer to
19083 -- reference the component containing the secondary dispatch table.
19084 -- Otherwise a type conversion is not a legal context.
19085 -- A return statement for a build-in-place function returning a
19086 -- synchronized type also introduces an unchecked conversion.
19087
19088 when N_Type_Conversion
19089 | N_Unchecked_Type_Conversion
19090 =>
19091 return not Comes_From_Source (Exp)
19092 and then
19093 OK_For_Limited_Init_In_05
19094 (Typ, Expression (Original_Node (Exp)));
19095
19096 when N_Explicit_Dereference
19097 | N_Indexed_Component
19098 | N_Selected_Component
19099 =>
19100 return Nkind (Exp) = N_Function_Call;
19101
19102 -- A use of 'Input is a function call, hence allowed. Normally the
19103 -- attribute will be changed to a call, but the attribute by itself
19104 -- can occur with -gnatc.
19105
19106 when N_Attribute_Reference =>
19107 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19108
19109 -- For a case expression, all dependent expressions must be legal
19110
19111 when N_Case_Expression =>
19112 declare
19113 Alt : Node_Id;
19114
19115 begin
19116 Alt := First (Alternatives (Original_Node (Exp)));
19117 while Present (Alt) loop
19118 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19119 return False;
19120 end if;
19121
19122 Next (Alt);
19123 end loop;
19124
19125 return True;
19126 end;
19127
19128 -- For an if expression, all dependent expressions must be legal
19129
19130 when N_If_Expression =>
19131 declare
19132 Then_Expr : constant Node_Id :=
19133 Next (First (Expressions (Original_Node (Exp))));
19134 Else_Expr : constant Node_Id := Next (Then_Expr);
19135 begin
19136 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19137 and then
19138 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19139 end;
19140
19141 when others =>
19142 return False;
19143 end case;
19144 end OK_For_Limited_Init_In_05;
19145
19146 -------------------------------------------
19147 -- Ordinary_Fixed_Point_Type_Declaration --
19148 -------------------------------------------
19149
19150 procedure Ordinary_Fixed_Point_Type_Declaration
19151 (T : Entity_Id;
19152 Def : Node_Id)
19153 is
19154 Loc : constant Source_Ptr := Sloc (Def);
19155 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19156 RRS : constant Node_Id := Real_Range_Specification (Def);
19157 Implicit_Base : Entity_Id;
19158 Delta_Val : Ureal;
19159 Small_Val : Ureal;
19160 Low_Val : Ureal;
19161 High_Val : Ureal;
19162
19163 begin
19164 Check_Restriction (No_Fixed_Point, Def);
19165
19166 -- Create implicit base type
19167
19168 Implicit_Base :=
19169 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19170 Set_Etype (Implicit_Base, Implicit_Base);
19171
19172 -- Analyze and process delta expression
19173
19174 Analyze_And_Resolve (Delta_Expr, Any_Real);
19175
19176 Check_Delta_Expression (Delta_Expr);
19177 Delta_Val := Expr_Value_R (Delta_Expr);
19178
19179 Set_Delta_Value (Implicit_Base, Delta_Val);
19180
19181 -- Compute default small from given delta, which is the largest power
19182 -- of two that does not exceed the given delta value.
19183
19184 declare
19185 Tmp : Ureal;
19186 Scale : Int;
19187
19188 begin
19189 Tmp := Ureal_1;
19190 Scale := 0;
19191
19192 if Delta_Val < Ureal_1 then
19193 while Delta_Val < Tmp loop
19194 Tmp := Tmp / Ureal_2;
19195 Scale := Scale + 1;
19196 end loop;
19197
19198 else
19199 loop
19200 Tmp := Tmp * Ureal_2;
19201 exit when Tmp > Delta_Val;
19202 Scale := Scale - 1;
19203 end loop;
19204 end if;
19205
19206 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19207 end;
19208
19209 Set_Small_Value (Implicit_Base, Small_Val);
19210
19211 -- If no range was given, set a dummy range
19212
19213 if RRS <= Empty_Or_Error then
19214 Low_Val := -Small_Val;
19215 High_Val := Small_Val;
19216
19217 -- Otherwise analyze and process given range
19218
19219 else
19220 declare
19221 Low : constant Node_Id := Low_Bound (RRS);
19222 High : constant Node_Id := High_Bound (RRS);
19223
19224 begin
19225 Analyze_And_Resolve (Low, Any_Real);
19226 Analyze_And_Resolve (High, Any_Real);
19227 Check_Real_Bound (Low);
19228 Check_Real_Bound (High);
19229
19230 -- Obtain and set the range
19231
19232 Low_Val := Expr_Value_R (Low);
19233 High_Val := Expr_Value_R (High);
19234
19235 if Low_Val > High_Val then
19236 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19237 end if;
19238 end;
19239 end if;
19240
19241 -- The range for both the implicit base and the declared first subtype
19242 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19243 -- set a temporary range in place. Note that the bounds of the base
19244 -- type will be widened to be symmetrical and to fill the available
19245 -- bits when the type is frozen.
19246
19247 -- We could do this with all discrete types, and probably should, but
19248 -- we absolutely have to do it for fixed-point, since the end-points
19249 -- of the range and the size are determined by the small value, which
19250 -- could be reset before the freeze point.
19251
19252 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19253 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19254
19255 -- Complete definition of first subtype. The inheritance of the rep item
19256 -- chain ensures that SPARK-related pragmas are not clobbered when the
19257 -- ordinary fixed point type acts as a full view of a private type.
19258
19259 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19260 Set_Etype (T, Implicit_Base);
19261 Init_Size_Align (T);
19262 Inherit_Rep_Item_Chain (T, Implicit_Base);
19263 Set_Small_Value (T, Small_Val);
19264 Set_Delta_Value (T, Delta_Val);
19265 Set_Is_Constrained (T);
19266 end Ordinary_Fixed_Point_Type_Declaration;
19267
19268 ----------------------------------
19269 -- Preanalyze_Assert_Expression --
19270 ----------------------------------
19271
19272 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19273 begin
19274 In_Assertion_Expr := In_Assertion_Expr + 1;
19275 Preanalyze_Spec_Expression (N, T);
19276 In_Assertion_Expr := In_Assertion_Expr - 1;
19277 end Preanalyze_Assert_Expression;
19278
19279 -----------------------------------
19280 -- Preanalyze_Default_Expression --
19281 -----------------------------------
19282
19283 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19284 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19285 begin
19286 In_Default_Expr := True;
19287 Preanalyze_Spec_Expression (N, T);
19288 In_Default_Expr := Save_In_Default_Expr;
19289 end Preanalyze_Default_Expression;
19290
19291 --------------------------------
19292 -- Preanalyze_Spec_Expression --
19293 --------------------------------
19294
19295 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19296 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19297 begin
19298 In_Spec_Expression := True;
19299 Preanalyze_And_Resolve (N, T);
19300 In_Spec_Expression := Save_In_Spec_Expression;
19301 end Preanalyze_Spec_Expression;
19302
19303 ----------------------------------------
19304 -- Prepare_Private_Subtype_Completion --
19305 ----------------------------------------
19306
19307 procedure Prepare_Private_Subtype_Completion
19308 (Id : Entity_Id;
19309 Related_Nod : Node_Id)
19310 is
19311 Id_B : constant Entity_Id := Base_Type (Id);
19312 Full_B : Entity_Id := Full_View (Id_B);
19313 Full : Entity_Id;
19314
19315 begin
19316 if Present (Full_B) then
19317
19318 -- Get to the underlying full view if necessary
19319
19320 if Is_Private_Type (Full_B)
19321 and then Present (Underlying_Full_View (Full_B))
19322 then
19323 Full_B := Underlying_Full_View (Full_B);
19324 end if;
19325
19326 -- The Base_Type is already completed, we can complete the subtype
19327 -- now. We have to create a new entity with the same name, Thus we
19328 -- can't use Create_Itype.
19329
19330 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19331 Set_Is_Itype (Full);
19332 Set_Associated_Node_For_Itype (Full, Related_Nod);
19333 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19334 end if;
19335
19336 -- The parent subtype may be private, but the base might not, in some
19337 -- nested instances. In that case, the subtype does not need to be
19338 -- exchanged. It would still be nice to make private subtypes and their
19339 -- bases consistent at all times ???
19340
19341 if Is_Private_Type (Id_B) then
19342 Append_Elmt (Id, Private_Dependents (Id_B));
19343 end if;
19344 end Prepare_Private_Subtype_Completion;
19345
19346 ---------------------------
19347 -- Process_Discriminants --
19348 ---------------------------
19349
19350 procedure Process_Discriminants
19351 (N : Node_Id;
19352 Prev : Entity_Id := Empty)
19353 is
19354 Elist : constant Elist_Id := New_Elmt_List;
19355 Id : Node_Id;
19356 Discr : Node_Id;
19357 Discr_Number : Uint;
19358 Discr_Type : Entity_Id;
19359 Default_Present : Boolean := False;
19360 Default_Not_Present : Boolean := False;
19361
19362 begin
19363 -- A composite type other than an array type can have discriminants.
19364 -- On entry, the current scope is the composite type.
19365
19366 -- The discriminants are initially entered into the scope of the type
19367 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19368 -- use, as explained at the end of this procedure.
19369
19370 Discr := First (Discriminant_Specifications (N));
19371 while Present (Discr) loop
19372 Enter_Name (Defining_Identifier (Discr));
19373
19374 -- For navigation purposes we add a reference to the discriminant
19375 -- in the entity for the type. If the current declaration is a
19376 -- completion, place references on the partial view. Otherwise the
19377 -- type is the current scope.
19378
19379 if Present (Prev) then
19380
19381 -- The references go on the partial view, if present. If the
19382 -- partial view has discriminants, the references have been
19383 -- generated already.
19384
19385 if not Has_Discriminants (Prev) then
19386 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19387 end if;
19388 else
19389 Generate_Reference
19390 (Current_Scope, Defining_Identifier (Discr), 'd');
19391 end if;
19392
19393 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19394 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19395
19396 -- Ada 2005 (AI-254)
19397
19398 if Present (Access_To_Subprogram_Definition
19399 (Discriminant_Type (Discr)))
19400 and then Protected_Present (Access_To_Subprogram_Definition
19401 (Discriminant_Type (Discr)))
19402 then
19403 Discr_Type :=
19404 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19405 end if;
19406
19407 else
19408 Find_Type (Discriminant_Type (Discr));
19409 Discr_Type := Etype (Discriminant_Type (Discr));
19410
19411 if Error_Posted (Discriminant_Type (Discr)) then
19412 Discr_Type := Any_Type;
19413 end if;
19414 end if;
19415
19416 -- Handling of discriminants that are access types
19417
19418 if Is_Access_Type (Discr_Type) then
19419
19420 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19421 -- limited record types
19422
19423 if Ada_Version < Ada_2005 then
19424 Check_Access_Discriminant_Requires_Limited
19425 (Discr, Discriminant_Type (Discr));
19426 end if;
19427
19428 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19429 Error_Msg_N
19430 ("(Ada 83) access discriminant not allowed", Discr);
19431 end if;
19432
19433 -- If not access type, must be a discrete type
19434
19435 elsif not Is_Discrete_Type (Discr_Type) then
19436 Error_Msg_N
19437 ("discriminants must have a discrete or access type",
19438 Discriminant_Type (Discr));
19439 end if;
19440
19441 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19442
19443 -- If a discriminant specification includes the assignment compound
19444 -- delimiter followed by an expression, the expression is the default
19445 -- expression of the discriminant; the default expression must be of
19446 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19447 -- a default expression, we do the special preanalysis, since this
19448 -- expression does not freeze (see section "Handling of Default and
19449 -- Per-Object Expressions" in spec of package Sem).
19450
19451 if Present (Expression (Discr)) then
19452 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19453
19454 -- Legaity checks
19455
19456 if Nkind (N) = N_Formal_Type_Declaration then
19457 Error_Msg_N
19458 ("discriminant defaults not allowed for formal type",
19459 Expression (Discr));
19460
19461 -- Flag an error for a tagged type with defaulted discriminants,
19462 -- excluding limited tagged types when compiling for Ada 2012
19463 -- (see AI05-0214).
19464
19465 elsif Is_Tagged_Type (Current_Scope)
19466 and then (not Is_Limited_Type (Current_Scope)
19467 or else Ada_Version < Ada_2012)
19468 and then Comes_From_Source (N)
19469 then
19470 -- Note: see similar test in Check_Or_Process_Discriminants, to
19471 -- handle the (illegal) case of the completion of an untagged
19472 -- view with discriminants with defaults by a tagged full view.
19473 -- We skip the check if Discr does not come from source, to
19474 -- account for the case of an untagged derived type providing
19475 -- defaults for a renamed discriminant from a private untagged
19476 -- ancestor with a tagged full view (ACATS B460006).
19477
19478 if Ada_Version >= Ada_2012 then
19479 Error_Msg_N
19480 ("discriminants of nonlimited tagged type cannot have"
19481 & " defaults",
19482 Expression (Discr));
19483 else
19484 Error_Msg_N
19485 ("discriminants of tagged type cannot have defaults",
19486 Expression (Discr));
19487 end if;
19488
19489 else
19490 Default_Present := True;
19491 Append_Elmt (Expression (Discr), Elist);
19492
19493 -- Tag the defining identifiers for the discriminants with
19494 -- their corresponding default expressions from the tree.
19495
19496 Set_Discriminant_Default_Value
19497 (Defining_Identifier (Discr), Expression (Discr));
19498 end if;
19499
19500 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19501 -- gets set unless we can be sure that no range check is required.
19502
19503 if (GNATprove_Mode or not Expander_Active)
19504 and then not
19505 Is_In_Range
19506 (Expression (Discr), Discr_Type, Assume_Valid => True)
19507 then
19508 Set_Do_Range_Check (Expression (Discr));
19509 end if;
19510
19511 -- No default discriminant value given
19512
19513 else
19514 Default_Not_Present := True;
19515 end if;
19516
19517 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19518 -- Discr_Type but with the null-exclusion attribute
19519
19520 if Ada_Version >= Ada_2005 then
19521
19522 -- Ada 2005 (AI-231): Static checks
19523
19524 if Can_Never_Be_Null (Discr_Type) then
19525 Null_Exclusion_Static_Checks (Discr);
19526
19527 elsif Is_Access_Type (Discr_Type)
19528 and then Null_Exclusion_Present (Discr)
19529
19530 -- No need to check itypes because in their case this check
19531 -- was done at their point of creation
19532
19533 and then not Is_Itype (Discr_Type)
19534 then
19535 if Can_Never_Be_Null (Discr_Type) then
19536 Error_Msg_NE
19537 ("`NOT NULL` not allowed (& already excludes null)",
19538 Discr,
19539 Discr_Type);
19540 end if;
19541
19542 Set_Etype (Defining_Identifier (Discr),
19543 Create_Null_Excluding_Itype
19544 (T => Discr_Type,
19545 Related_Nod => Discr));
19546
19547 -- Check for improper null exclusion if the type is otherwise
19548 -- legal for a discriminant.
19549
19550 elsif Null_Exclusion_Present (Discr)
19551 and then Is_Discrete_Type (Discr_Type)
19552 then
19553 Error_Msg_N
19554 ("null exclusion can only apply to an access type", Discr);
19555 end if;
19556
19557 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19558 -- can't have defaults. Synchronized types, or types that are
19559 -- explicitly limited are fine, but special tests apply to derived
19560 -- types in generics: in a generic body we have to assume the
19561 -- worst, and therefore defaults are not allowed if the parent is
19562 -- a generic formal private type (see ACATS B370001).
19563
19564 if Is_Access_Type (Discr_Type) and then Default_Present then
19565 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19566 or else Is_Limited_Record (Current_Scope)
19567 or else Is_Concurrent_Type (Current_Scope)
19568 or else Is_Concurrent_Record_Type (Current_Scope)
19569 or else Ekind (Current_Scope) = E_Limited_Private_Type
19570 then
19571 if not Is_Derived_Type (Current_Scope)
19572 or else not Is_Generic_Type (Etype (Current_Scope))
19573 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19574 or else Limited_Present
19575 (Type_Definition (Parent (Current_Scope)))
19576 then
19577 null;
19578
19579 else
19580 Error_Msg_N
19581 ("access discriminants of nonlimited types cannot "
19582 & "have defaults", Expression (Discr));
19583 end if;
19584
19585 elsif Present (Expression (Discr)) then
19586 Error_Msg_N
19587 ("(Ada 2005) access discriminants of nonlimited types "
19588 & "cannot have defaults", Expression (Discr));
19589 end if;
19590 end if;
19591 end if;
19592
19593 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
19594 -- This check is relevant only when SPARK_Mode is on as it is not a
19595 -- standard Ada legality rule.
19596
19597 if SPARK_Mode = On
19598 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19599 then
19600 Error_Msg_N ("discriminant cannot be volatile", Discr);
19601 end if;
19602
19603 Next (Discr);
19604 end loop;
19605
19606 -- An element list consisting of the default expressions of the
19607 -- discriminants is constructed in the above loop and used to set
19608 -- the Discriminant_Constraint attribute for the type. If an object
19609 -- is declared of this (record or task) type without any explicit
19610 -- discriminant constraint given, this element list will form the
19611 -- actual parameters for the corresponding initialization procedure
19612 -- for the type.
19613
19614 Set_Discriminant_Constraint (Current_Scope, Elist);
19615 Set_Stored_Constraint (Current_Scope, No_Elist);
19616
19617 -- Default expressions must be provided either for all or for none
19618 -- of the discriminants of a discriminant part. (RM 3.7.1)
19619
19620 if Default_Present and then Default_Not_Present then
19621 Error_Msg_N
19622 ("incomplete specification of defaults for discriminants", N);
19623 end if;
19624
19625 -- The use of the name of a discriminant is not allowed in default
19626 -- expressions of a discriminant part if the specification of the
19627 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19628
19629 -- To detect this, the discriminant names are entered initially with an
19630 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19631 -- attempt to use a void entity (for example in an expression that is
19632 -- type-checked) produces the error message: premature usage. Now after
19633 -- completing the semantic analysis of the discriminant part, we can set
19634 -- the Ekind of all the discriminants appropriately.
19635
19636 Discr := First (Discriminant_Specifications (N));
19637 Discr_Number := Uint_1;
19638 while Present (Discr) loop
19639 Id := Defining_Identifier (Discr);
19640 Set_Ekind (Id, E_Discriminant);
19641 Init_Component_Location (Id);
19642 Init_Esize (Id);
19643 Set_Discriminant_Number (Id, Discr_Number);
19644
19645 -- Make sure this is always set, even in illegal programs
19646
19647 Set_Corresponding_Discriminant (Id, Empty);
19648
19649 -- Initialize the Original_Record_Component to the entity itself.
19650 -- Inherit_Components will propagate the right value to
19651 -- discriminants in derived record types.
19652
19653 Set_Original_Record_Component (Id, Id);
19654
19655 -- Create the discriminal for the discriminant
19656
19657 Build_Discriminal (Id);
19658
19659 Next (Discr);
19660 Discr_Number := Discr_Number + 1;
19661 end loop;
19662
19663 Set_Has_Discriminants (Current_Scope);
19664 end Process_Discriminants;
19665
19666 -----------------------
19667 -- Process_Full_View --
19668 -----------------------
19669
19670 -- WARNING: This routine manages Ghost regions. Return statements must be
19671 -- replaced by gotos which jump to the end of the routine and restore the
19672 -- Ghost mode.
19673
19674 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19675 procedure Collect_Implemented_Interfaces
19676 (Typ : Entity_Id;
19677 Ifaces : Elist_Id);
19678 -- Ada 2005: Gather all the interfaces that Typ directly or
19679 -- inherently implements. Duplicate entries are not added to
19680 -- the list Ifaces.
19681
19682 ------------------------------------
19683 -- Collect_Implemented_Interfaces --
19684 ------------------------------------
19685
19686 procedure Collect_Implemented_Interfaces
19687 (Typ : Entity_Id;
19688 Ifaces : Elist_Id)
19689 is
19690 Iface : Entity_Id;
19691 Iface_Elmt : Elmt_Id;
19692
19693 begin
19694 -- Abstract interfaces are only associated with tagged record types
19695
19696 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19697 return;
19698 end if;
19699
19700 -- Recursively climb to the ancestors
19701
19702 if Etype (Typ) /= Typ
19703
19704 -- Protect the frontend against wrong cyclic declarations like:
19705
19706 -- type B is new A with private;
19707 -- type C is new A with private;
19708 -- private
19709 -- type B is new C with null record;
19710 -- type C is new B with null record;
19711
19712 and then Etype (Typ) /= Priv_T
19713 and then Etype (Typ) /= Full_T
19714 then
19715 -- Keep separate the management of private type declarations
19716
19717 if Ekind (Typ) = E_Record_Type_With_Private then
19718
19719 -- Handle the following illegal usage:
19720 -- type Private_Type is tagged private;
19721 -- private
19722 -- type Private_Type is new Type_Implementing_Iface;
19723
19724 if Present (Full_View (Typ))
19725 and then Etype (Typ) /= Full_View (Typ)
19726 then
19727 if Is_Interface (Etype (Typ)) then
19728 Append_Unique_Elmt (Etype (Typ), Ifaces);
19729 end if;
19730
19731 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19732 end if;
19733
19734 -- Non-private types
19735
19736 else
19737 if Is_Interface (Etype (Typ)) then
19738 Append_Unique_Elmt (Etype (Typ), Ifaces);
19739 end if;
19740
19741 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19742 end if;
19743 end if;
19744
19745 -- Handle entities in the list of abstract interfaces
19746
19747 if Present (Interfaces (Typ)) then
19748 Iface_Elmt := First_Elmt (Interfaces (Typ));
19749 while Present (Iface_Elmt) loop
19750 Iface := Node (Iface_Elmt);
19751
19752 pragma Assert (Is_Interface (Iface));
19753
19754 if not Contain_Interface (Iface, Ifaces) then
19755 Append_Elmt (Iface, Ifaces);
19756 Collect_Implemented_Interfaces (Iface, Ifaces);
19757 end if;
19758
19759 Next_Elmt (Iface_Elmt);
19760 end loop;
19761 end if;
19762 end Collect_Implemented_Interfaces;
19763
19764 -- Local variables
19765
19766 Full_Indic : Node_Id;
19767 Full_Parent : Entity_Id;
19768 Mode : Ghost_Mode_Type;
19769 Priv_Parent : Entity_Id;
19770
19771 -- Start of processing for Process_Full_View
19772
19773 begin
19774 Mark_And_Set_Ghost_Completion (N, Priv_T, Mode);
19775
19776 -- First some sanity checks that must be done after semantic
19777 -- decoration of the full view and thus cannot be placed with other
19778 -- similar checks in Find_Type_Name
19779
19780 if not Is_Limited_Type (Priv_T)
19781 and then (Is_Limited_Type (Full_T)
19782 or else Is_Limited_Composite (Full_T))
19783 then
19784 if In_Instance then
19785 null;
19786 else
19787 Error_Msg_N
19788 ("completion of nonlimited type cannot be limited", Full_T);
19789 Explain_Limited_Type (Full_T, Full_T);
19790 end if;
19791
19792 elsif Is_Abstract_Type (Full_T)
19793 and then not Is_Abstract_Type (Priv_T)
19794 then
19795 Error_Msg_N
19796 ("completion of nonabstract type cannot be abstract", Full_T);
19797
19798 elsif Is_Tagged_Type (Priv_T)
19799 and then Is_Limited_Type (Priv_T)
19800 and then not Is_Limited_Type (Full_T)
19801 then
19802 -- If pragma CPP_Class was applied to the private declaration
19803 -- propagate the limitedness to the full-view
19804
19805 if Is_CPP_Class (Priv_T) then
19806 Set_Is_Limited_Record (Full_T);
19807
19808 -- GNAT allow its own definition of Limited_Controlled to disobey
19809 -- this rule in order in ease the implementation. This test is safe
19810 -- because Root_Controlled is defined in a child of System that
19811 -- normal programs are not supposed to use.
19812
19813 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19814 Set_Is_Limited_Composite (Full_T);
19815 else
19816 Error_Msg_N
19817 ("completion of limited tagged type must be limited", Full_T);
19818 end if;
19819
19820 elsif Is_Generic_Type (Priv_T) then
19821 Error_Msg_N ("generic type cannot have a completion", Full_T);
19822 end if;
19823
19824 -- Check that ancestor interfaces of private and full views are
19825 -- consistent. We omit this check for synchronized types because
19826 -- they are performed on the corresponding record type when frozen.
19827
19828 if Ada_Version >= Ada_2005
19829 and then Is_Tagged_Type (Priv_T)
19830 and then Is_Tagged_Type (Full_T)
19831 and then not Is_Concurrent_Type (Full_T)
19832 then
19833 declare
19834 Iface : Entity_Id;
19835 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19836 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19837
19838 begin
19839 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19840 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19841
19842 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19843 -- an interface type if and only if the full type is descendant
19844 -- of the interface type (AARM 7.3 (7.3/2)).
19845
19846 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19847
19848 if Present (Iface) then
19849 Error_Msg_NE
19850 ("interface in partial view& not implemented by full type "
19851 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19852 end if;
19853
19854 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19855
19856 if Present (Iface) then
19857 Error_Msg_NE
19858 ("interface & not implemented by partial view "
19859 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19860 end if;
19861 end;
19862 end if;
19863
19864 if Is_Tagged_Type (Priv_T)
19865 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19866 and then Is_Derived_Type (Full_T)
19867 then
19868 Priv_Parent := Etype (Priv_T);
19869
19870 -- The full view of a private extension may have been transformed
19871 -- into an unconstrained derived type declaration and a subtype
19872 -- declaration (see build_derived_record_type for details).
19873
19874 if Nkind (N) = N_Subtype_Declaration then
19875 Full_Indic := Subtype_Indication (N);
19876 Full_Parent := Etype (Base_Type (Full_T));
19877 else
19878 Full_Indic := Subtype_Indication (Type_Definition (N));
19879 Full_Parent := Etype (Full_T);
19880 end if;
19881
19882 -- Check that the parent type of the full type is a descendant of
19883 -- the ancestor subtype given in the private extension. If either
19884 -- entity has an Etype equal to Any_Type then we had some previous
19885 -- error situation [7.3(8)].
19886
19887 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19888 goto Leave;
19889
19890 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19891 -- any order. Therefore we don't have to check that its parent must
19892 -- be a descendant of the parent of the private type declaration.
19893
19894 elsif Is_Interface (Priv_Parent)
19895 and then Is_Interface (Full_Parent)
19896 then
19897 null;
19898
19899 -- Ada 2005 (AI-251): If the parent of the private type declaration
19900 -- is an interface there is no need to check that it is an ancestor
19901 -- of the associated full type declaration. The required tests for
19902 -- this case are performed by Build_Derived_Record_Type.
19903
19904 elsif not Is_Interface (Base_Type (Priv_Parent))
19905 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19906 then
19907 Error_Msg_N
19908 ("parent of full type must descend from parent of private "
19909 & "extension", Full_Indic);
19910
19911 -- First check a formal restriction, and then proceed with checking
19912 -- Ada rules. Since the formal restriction is not a serious error, we
19913 -- don't prevent further error detection for this check, hence the
19914 -- ELSE.
19915
19916 else
19917 -- In formal mode, when completing a private extension the type
19918 -- named in the private part must be exactly the same as that
19919 -- named in the visible part.
19920
19921 if Priv_Parent /= Full_Parent then
19922 Error_Msg_Name_1 := Chars (Priv_Parent);
19923 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19924 end if;
19925
19926 -- Check the rules of 7.3(10): if the private extension inherits
19927 -- known discriminants, then the full type must also inherit those
19928 -- discriminants from the same (ancestor) type, and the parent
19929 -- subtype of the full type must be constrained if and only if
19930 -- the ancestor subtype of the private extension is constrained.
19931
19932 if No (Discriminant_Specifications (Parent (Priv_T)))
19933 and then not Has_Unknown_Discriminants (Priv_T)
19934 and then Has_Discriminants (Base_Type (Priv_Parent))
19935 then
19936 declare
19937 Priv_Indic : constant Node_Id :=
19938 Subtype_Indication (Parent (Priv_T));
19939
19940 Priv_Constr : constant Boolean :=
19941 Is_Constrained (Priv_Parent)
19942 or else
19943 Nkind (Priv_Indic) = N_Subtype_Indication
19944 or else
19945 Is_Constrained (Entity (Priv_Indic));
19946
19947 Full_Constr : constant Boolean :=
19948 Is_Constrained (Full_Parent)
19949 or else
19950 Nkind (Full_Indic) = N_Subtype_Indication
19951 or else
19952 Is_Constrained (Entity (Full_Indic));
19953
19954 Priv_Discr : Entity_Id;
19955 Full_Discr : Entity_Id;
19956
19957 begin
19958 Priv_Discr := First_Discriminant (Priv_Parent);
19959 Full_Discr := First_Discriminant (Full_Parent);
19960 while Present (Priv_Discr) and then Present (Full_Discr) loop
19961 if Original_Record_Component (Priv_Discr) =
19962 Original_Record_Component (Full_Discr)
19963 or else
19964 Corresponding_Discriminant (Priv_Discr) =
19965 Corresponding_Discriminant (Full_Discr)
19966 then
19967 null;
19968 else
19969 exit;
19970 end if;
19971
19972 Next_Discriminant (Priv_Discr);
19973 Next_Discriminant (Full_Discr);
19974 end loop;
19975
19976 if Present (Priv_Discr) or else Present (Full_Discr) then
19977 Error_Msg_N
19978 ("full view must inherit discriminants of the parent "
19979 & "type used in the private extension", Full_Indic);
19980
19981 elsif Priv_Constr and then not Full_Constr then
19982 Error_Msg_N
19983 ("parent subtype of full type must be constrained",
19984 Full_Indic);
19985
19986 elsif Full_Constr and then not Priv_Constr then
19987 Error_Msg_N
19988 ("parent subtype of full type must be unconstrained",
19989 Full_Indic);
19990 end if;
19991 end;
19992
19993 -- Check the rules of 7.3(12): if a partial view has neither
19994 -- known or unknown discriminants, then the full type
19995 -- declaration shall define a definite subtype.
19996
19997 elsif not Has_Unknown_Discriminants (Priv_T)
19998 and then not Has_Discriminants (Priv_T)
19999 and then not Is_Constrained (Full_T)
20000 then
20001 Error_Msg_N
20002 ("full view must define a constrained type if partial view "
20003 & "has no discriminants", Full_T);
20004 end if;
20005
20006 -- ??????? Do we implement the following properly ?????
20007 -- If the ancestor subtype of a private extension has constrained
20008 -- discriminants, then the parent subtype of the full view shall
20009 -- impose a statically matching constraint on those discriminants
20010 -- [7.3(13)].
20011 end if;
20012
20013 else
20014 -- For untagged types, verify that a type without discriminants is
20015 -- not completed with an unconstrained type. A separate error message
20016 -- is produced if the full type has defaulted discriminants.
20017
20018 if Is_Definite_Subtype (Priv_T)
20019 and then not Is_Definite_Subtype (Full_T)
20020 then
20021 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20022 Error_Msg_NE
20023 ("full view of& not compatible with declaration#",
20024 Full_T, Priv_T);
20025
20026 if not Is_Tagged_Type (Full_T) then
20027 Error_Msg_N
20028 ("\one is constrained, the other unconstrained", Full_T);
20029 end if;
20030 end if;
20031 end if;
20032
20033 -- AI-419: verify that the use of "limited" is consistent
20034
20035 declare
20036 Orig_Decl : constant Node_Id := Original_Node (N);
20037
20038 begin
20039 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20040 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20041 and then Nkind
20042 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20043 then
20044 if not Limited_Present (Parent (Priv_T))
20045 and then not Synchronized_Present (Parent (Priv_T))
20046 and then Limited_Present (Type_Definition (Orig_Decl))
20047 then
20048 Error_Msg_N
20049 ("full view of non-limited extension cannot be limited", N);
20050
20051 -- Conversely, if the partial view carries the limited keyword,
20052 -- the full view must as well, even if it may be redundant.
20053
20054 elsif Limited_Present (Parent (Priv_T))
20055 and then not Limited_Present (Type_Definition (Orig_Decl))
20056 then
20057 Error_Msg_N
20058 ("full view of limited extension must be explicitly limited",
20059 N);
20060 end if;
20061 end if;
20062 end;
20063
20064 -- Ada 2005 (AI-443): A synchronized private extension must be
20065 -- completed by a task or protected type.
20066
20067 if Ada_Version >= Ada_2005
20068 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20069 and then Synchronized_Present (Parent (Priv_T))
20070 and then not Is_Concurrent_Type (Full_T)
20071 then
20072 Error_Msg_N ("full view of synchronized extension must " &
20073 "be synchronized type", N);
20074 end if;
20075
20076 -- Ada 2005 AI-363: if the full view has discriminants with
20077 -- defaults, it is illegal to declare constrained access subtypes
20078 -- whose designated type is the current type. This allows objects
20079 -- of the type that are declared in the heap to be unconstrained.
20080
20081 if not Has_Unknown_Discriminants (Priv_T)
20082 and then not Has_Discriminants (Priv_T)
20083 and then Has_Discriminants (Full_T)
20084 and then
20085 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20086 then
20087 Set_Has_Constrained_Partial_View (Full_T);
20088 Set_Has_Constrained_Partial_View (Priv_T);
20089 end if;
20090
20091 -- Create a full declaration for all its subtypes recorded in
20092 -- Private_Dependents and swap them similarly to the base type. These
20093 -- are subtypes that have been define before the full declaration of
20094 -- the private type. We also swap the entry in Private_Dependents list
20095 -- so we can properly restore the private view on exit from the scope.
20096
20097 declare
20098 Priv_Elmt : Elmt_Id;
20099 Priv_Scop : Entity_Id;
20100 Priv : Entity_Id;
20101 Full : Entity_Id;
20102
20103 begin
20104 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20105 while Present (Priv_Elmt) loop
20106 Priv := Node (Priv_Elmt);
20107 Priv_Scop := Scope (Priv);
20108
20109 if Ekind_In (Priv, E_Private_Subtype,
20110 E_Limited_Private_Subtype,
20111 E_Record_Subtype_With_Private)
20112 then
20113 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20114 Set_Is_Itype (Full);
20115 Set_Parent (Full, Parent (Priv));
20116 Set_Associated_Node_For_Itype (Full, N);
20117
20118 -- Now we need to complete the private subtype, but since the
20119 -- base type has already been swapped, we must also swap the
20120 -- subtypes (and thus, reverse the arguments in the call to
20121 -- Complete_Private_Subtype). Also note that we may need to
20122 -- re-establish the scope of the private subtype.
20123
20124 Copy_And_Swap (Priv, Full);
20125
20126 if not In_Open_Scopes (Priv_Scop) then
20127 Push_Scope (Priv_Scop);
20128
20129 else
20130 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20131
20132 Priv_Scop := Empty;
20133 end if;
20134
20135 Complete_Private_Subtype (Full, Priv, Full_T, N);
20136
20137 if Present (Priv_Scop) then
20138 Pop_Scope;
20139 end if;
20140
20141 Replace_Elmt (Priv_Elmt, Full);
20142 end if;
20143
20144 Next_Elmt (Priv_Elmt);
20145 end loop;
20146 end;
20147
20148 -- If the private view was tagged, copy the new primitive operations
20149 -- from the private view to the full view.
20150
20151 if Is_Tagged_Type (Full_T) then
20152 declare
20153 Disp_Typ : Entity_Id;
20154 Full_List : Elist_Id;
20155 Prim : Entity_Id;
20156 Prim_Elmt : Elmt_Id;
20157 Priv_List : Elist_Id;
20158
20159 function Contains
20160 (E : Entity_Id;
20161 L : Elist_Id) return Boolean;
20162 -- Determine whether list L contains element E
20163
20164 --------------
20165 -- Contains --
20166 --------------
20167
20168 function Contains
20169 (E : Entity_Id;
20170 L : Elist_Id) return Boolean
20171 is
20172 List_Elmt : Elmt_Id;
20173
20174 begin
20175 List_Elmt := First_Elmt (L);
20176 while Present (List_Elmt) loop
20177 if Node (List_Elmt) = E then
20178 return True;
20179 end if;
20180
20181 Next_Elmt (List_Elmt);
20182 end loop;
20183
20184 return False;
20185 end Contains;
20186
20187 -- Start of processing
20188
20189 begin
20190 if Is_Tagged_Type (Priv_T) then
20191 Priv_List := Primitive_Operations (Priv_T);
20192 Prim_Elmt := First_Elmt (Priv_List);
20193
20194 -- In the case of a concurrent type completing a private tagged
20195 -- type, primitives may have been declared in between the two
20196 -- views. These subprograms need to be wrapped the same way
20197 -- entries and protected procedures are handled because they
20198 -- cannot be directly shared by the two views.
20199
20200 if Is_Concurrent_Type (Full_T) then
20201 declare
20202 Conc_Typ : constant Entity_Id :=
20203 Corresponding_Record_Type (Full_T);
20204 Curr_Nod : Node_Id := Parent (Conc_Typ);
20205 Wrap_Spec : Node_Id;
20206
20207 begin
20208 while Present (Prim_Elmt) loop
20209 Prim := Node (Prim_Elmt);
20210
20211 if Comes_From_Source (Prim)
20212 and then not Is_Abstract_Subprogram (Prim)
20213 then
20214 Wrap_Spec :=
20215 Make_Subprogram_Declaration (Sloc (Prim),
20216 Specification =>
20217 Build_Wrapper_Spec
20218 (Subp_Id => Prim,
20219 Obj_Typ => Conc_Typ,
20220 Formals =>
20221 Parameter_Specifications
20222 (Parent (Prim))));
20223
20224 Insert_After (Curr_Nod, Wrap_Spec);
20225 Curr_Nod := Wrap_Spec;
20226
20227 Analyze (Wrap_Spec);
20228
20229 -- Remove the wrapper from visibility to avoid
20230 -- spurious conflict with the wrapped entity.
20231
20232 Set_Is_Immediately_Visible
20233 (Defining_Entity (Specification (Wrap_Spec)),
20234 False);
20235 end if;
20236
20237 Next_Elmt (Prim_Elmt);
20238 end loop;
20239
20240 goto Leave;
20241 end;
20242
20243 -- For non-concurrent types, transfer explicit primitives, but
20244 -- omit those inherited from the parent of the private view
20245 -- since they will be re-inherited later on.
20246
20247 else
20248 Full_List := Primitive_Operations (Full_T);
20249
20250 while Present (Prim_Elmt) loop
20251 Prim := Node (Prim_Elmt);
20252
20253 if Comes_From_Source (Prim)
20254 and then not Contains (Prim, Full_List)
20255 then
20256 Append_Elmt (Prim, Full_List);
20257 end if;
20258
20259 Next_Elmt (Prim_Elmt);
20260 end loop;
20261 end if;
20262
20263 -- Untagged private view
20264
20265 else
20266 Full_List := Primitive_Operations (Full_T);
20267
20268 -- In this case the partial view is untagged, so here we locate
20269 -- all of the earlier primitives that need to be treated as
20270 -- dispatching (those that appear between the two views). Note
20271 -- that these additional operations must all be new operations
20272 -- (any earlier operations that override inherited operations
20273 -- of the full view will already have been inserted in the
20274 -- primitives list, marked by Check_Operation_From_Private_View
20275 -- as dispatching. Note that implicit "/=" operators are
20276 -- excluded from being added to the primitives list since they
20277 -- shouldn't be treated as dispatching (tagged "/=" is handled
20278 -- specially).
20279
20280 Prim := Next_Entity (Full_T);
20281 while Present (Prim) and then Prim /= Priv_T loop
20282 if Ekind_In (Prim, E_Procedure, E_Function) then
20283 Disp_Typ := Find_Dispatching_Type (Prim);
20284
20285 if Disp_Typ = Full_T
20286 and then (Chars (Prim) /= Name_Op_Ne
20287 or else Comes_From_Source (Prim))
20288 then
20289 Check_Controlling_Formals (Full_T, Prim);
20290
20291 if not Is_Dispatching_Operation (Prim) then
20292 Append_Elmt (Prim, Full_List);
20293 Set_Is_Dispatching_Operation (Prim, True);
20294 Set_DT_Position_Value (Prim, No_Uint);
20295 end if;
20296
20297 elsif Is_Dispatching_Operation (Prim)
20298 and then Disp_Typ /= Full_T
20299 then
20300
20301 -- Verify that it is not otherwise controlled by a
20302 -- formal or a return value of type T.
20303
20304 Check_Controlling_Formals (Disp_Typ, Prim);
20305 end if;
20306 end if;
20307
20308 Next_Entity (Prim);
20309 end loop;
20310 end if;
20311
20312 -- For the tagged case, the two views can share the same primitive
20313 -- operations list and the same class-wide type. Update attributes
20314 -- of the class-wide type which depend on the full declaration.
20315
20316 if Is_Tagged_Type (Priv_T) then
20317 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20318 Set_Class_Wide_Type
20319 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20320
20321 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20322 end if;
20323 end;
20324 end if;
20325
20326 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20327
20328 if Known_To_Have_Preelab_Init (Priv_T) then
20329
20330 -- Case where there is a pragma Preelaborable_Initialization. We
20331 -- always allow this in predefined units, which is cheating a bit,
20332 -- but it means we don't have to struggle to meet the requirements in
20333 -- the RM for having Preelaborable Initialization. Otherwise we
20334 -- require that the type meets the RM rules. But we can't check that
20335 -- yet, because of the rule about overriding Initialize, so we simply
20336 -- set a flag that will be checked at freeze time.
20337
20338 if not In_Predefined_Unit (Full_T) then
20339 Set_Must_Have_Preelab_Init (Full_T);
20340 end if;
20341 end if;
20342
20343 -- If pragma CPP_Class was applied to the private type declaration,
20344 -- propagate it now to the full type declaration.
20345
20346 if Is_CPP_Class (Priv_T) then
20347 Set_Is_CPP_Class (Full_T);
20348 Set_Convention (Full_T, Convention_CPP);
20349
20350 -- Check that components of imported CPP types do not have default
20351 -- expressions.
20352
20353 Check_CPP_Type_Has_No_Defaults (Full_T);
20354 end if;
20355
20356 -- If the private view has user specified stream attributes, then so has
20357 -- the full view.
20358
20359 -- Why the test, how could these flags be already set in Full_T ???
20360
20361 if Has_Specified_Stream_Read (Priv_T) then
20362 Set_Has_Specified_Stream_Read (Full_T);
20363 end if;
20364
20365 if Has_Specified_Stream_Write (Priv_T) then
20366 Set_Has_Specified_Stream_Write (Full_T);
20367 end if;
20368
20369 if Has_Specified_Stream_Input (Priv_T) then
20370 Set_Has_Specified_Stream_Input (Full_T);
20371 end if;
20372
20373 if Has_Specified_Stream_Output (Priv_T) then
20374 Set_Has_Specified_Stream_Output (Full_T);
20375 end if;
20376
20377 -- Propagate Default_Initial_Condition-related attributes from the
20378 -- partial view to the full view and its base type.
20379
20380 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20381 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20382
20383 -- Propagate invariant-related attributes from the partial view to the
20384 -- full view and its base type.
20385
20386 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20387 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20388
20389 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20390 -- in the full view without advertising the inheritance in the partial
20391 -- view. This can only occur when the partial view has no parent type
20392 -- and the full view has an interface as a parent. Any other scenarios
20393 -- are illegal because implemented interfaces must match between the
20394 -- two views.
20395
20396 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20397 declare
20398 Full_Par : constant Entity_Id := Etype (Full_T);
20399 Priv_Par : constant Entity_Id := Etype (Priv_T);
20400
20401 begin
20402 if not Is_Interface (Priv_Par)
20403 and then Is_Interface (Full_Par)
20404 and then Has_Inheritable_Invariants (Full_Par)
20405 then
20406 Error_Msg_N
20407 ("hidden inheritance of class-wide type invariants not "
20408 & "allowed", N);
20409 end if;
20410 end;
20411 end if;
20412
20413 -- Propagate predicates to full type, and predicate function if already
20414 -- defined. It is not clear that this can actually happen? the partial
20415 -- view cannot be frozen yet, and the predicate function has not been
20416 -- built. Still it is a cheap check and seems safer to make it.
20417
20418 if Has_Predicates (Priv_T) then
20419 Set_Has_Predicates (Full_T);
20420
20421 if Present (Predicate_Function (Priv_T)) then
20422 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20423 end if;
20424 end if;
20425
20426 <<Leave>>
20427 Restore_Ghost_Mode (Mode);
20428 end Process_Full_View;
20429
20430 -----------------------------------
20431 -- Process_Incomplete_Dependents --
20432 -----------------------------------
20433
20434 procedure Process_Incomplete_Dependents
20435 (N : Node_Id;
20436 Full_T : Entity_Id;
20437 Inc_T : Entity_Id)
20438 is
20439 Inc_Elmt : Elmt_Id;
20440 Priv_Dep : Entity_Id;
20441 New_Subt : Entity_Id;
20442
20443 Disc_Constraint : Elist_Id;
20444
20445 begin
20446 if No (Private_Dependents (Inc_T)) then
20447 return;
20448 end if;
20449
20450 -- Itypes that may be generated by the completion of an incomplete
20451 -- subtype are not used by the back-end and not attached to the tree.
20452 -- They are created only for constraint-checking purposes.
20453
20454 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20455 while Present (Inc_Elmt) loop
20456 Priv_Dep := Node (Inc_Elmt);
20457
20458 if Ekind (Priv_Dep) = E_Subprogram_Type then
20459
20460 -- An Access_To_Subprogram type may have a return type or a
20461 -- parameter type that is incomplete. Replace with the full view.
20462
20463 if Etype (Priv_Dep) = Inc_T then
20464 Set_Etype (Priv_Dep, Full_T);
20465 end if;
20466
20467 declare
20468 Formal : Entity_Id;
20469
20470 begin
20471 Formal := First_Formal (Priv_Dep);
20472 while Present (Formal) loop
20473 if Etype (Formal) = Inc_T then
20474 Set_Etype (Formal, Full_T);
20475 end if;
20476
20477 Next_Formal (Formal);
20478 end loop;
20479 end;
20480
20481 elsif Is_Overloadable (Priv_Dep) then
20482
20483 -- If a subprogram in the incomplete dependents list is primitive
20484 -- for a tagged full type then mark it as a dispatching operation,
20485 -- check whether it overrides an inherited subprogram, and check
20486 -- restrictions on its controlling formals. Note that a protected
20487 -- operation is never dispatching: only its wrapper operation
20488 -- (which has convention Ada) is.
20489
20490 if Is_Tagged_Type (Full_T)
20491 and then Is_Primitive (Priv_Dep)
20492 and then Convention (Priv_Dep) /= Convention_Protected
20493 then
20494 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20495 Set_Is_Dispatching_Operation (Priv_Dep);
20496 Check_Controlling_Formals (Full_T, Priv_Dep);
20497 end if;
20498
20499 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20500
20501 -- Can happen during processing of a body before the completion
20502 -- of a TA type. Ignore, because spec is also on dependent list.
20503
20504 return;
20505
20506 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20507 -- corresponding subtype of the full view.
20508
20509 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20510 Set_Subtype_Indication
20511 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20512 Set_Etype (Priv_Dep, Full_T);
20513 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20514 Set_Analyzed (Parent (Priv_Dep), False);
20515
20516 -- Reanalyze the declaration, suppressing the call to
20517 -- Enter_Name to avoid duplicate names.
20518
20519 Analyze_Subtype_Declaration
20520 (N => Parent (Priv_Dep),
20521 Skip => True);
20522
20523 -- Dependent is a subtype
20524
20525 else
20526 -- We build a new subtype indication using the full view of the
20527 -- incomplete parent. The discriminant constraints have been
20528 -- elaborated already at the point of the subtype declaration.
20529
20530 New_Subt := Create_Itype (E_Void, N);
20531
20532 if Has_Discriminants (Full_T) then
20533 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20534 else
20535 Disc_Constraint := No_Elist;
20536 end if;
20537
20538 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20539 Set_Full_View (Priv_Dep, New_Subt);
20540 end if;
20541
20542 Next_Elmt (Inc_Elmt);
20543 end loop;
20544 end Process_Incomplete_Dependents;
20545
20546 --------------------------------
20547 -- Process_Range_Expr_In_Decl --
20548 --------------------------------
20549
20550 procedure Process_Range_Expr_In_Decl
20551 (R : Node_Id;
20552 T : Entity_Id;
20553 Subtyp : Entity_Id := Empty;
20554 Check_List : List_Id := Empty_List;
20555 R_Check_Off : Boolean := False;
20556 In_Iter_Schm : Boolean := False)
20557 is
20558 Lo, Hi : Node_Id;
20559 R_Checks : Check_Result;
20560 Insert_Node : Node_Id;
20561 Def_Id : Entity_Id;
20562
20563 begin
20564 Analyze_And_Resolve (R, Base_Type (T));
20565
20566 if Nkind (R) = N_Range then
20567
20568 -- In SPARK, all ranges should be static, with the exception of the
20569 -- discrete type definition of a loop parameter specification.
20570
20571 if not In_Iter_Schm
20572 and then not Is_OK_Static_Range (R)
20573 then
20574 Check_SPARK_05_Restriction ("range should be static", R);
20575 end if;
20576
20577 Lo := Low_Bound (R);
20578 Hi := High_Bound (R);
20579
20580 -- Validity checks on the range of a quantified expression are
20581 -- delayed until the construct is transformed into a loop.
20582
20583 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20584 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20585 then
20586 null;
20587
20588 -- We need to ensure validity of the bounds here, because if we
20589 -- go ahead and do the expansion, then the expanded code will get
20590 -- analyzed with range checks suppressed and we miss the check.
20591
20592 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20593 -- the temporaries generated by routine Remove_Side_Effects by means
20594 -- of validity checks must use the same names. When a range appears
20595 -- in the parent of a generic, the range is processed with checks
20596 -- disabled as part of the generic context and with checks enabled
20597 -- for code generation purposes. This leads to link issues as the
20598 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20599 -- template sees the temporaries generated by Remove_Side_Effects.
20600
20601 else
20602 Validity_Check_Range (R, Subtyp);
20603 end if;
20604
20605 -- If there were errors in the declaration, try and patch up some
20606 -- common mistakes in the bounds. The cases handled are literals
20607 -- which are Integer where the expected type is Real and vice versa.
20608 -- These corrections allow the compilation process to proceed further
20609 -- along since some basic assumptions of the format of the bounds
20610 -- are guaranteed.
20611
20612 if Etype (R) = Any_Type then
20613 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20614 Rewrite (Lo,
20615 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20616
20617 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20618 Rewrite (Hi,
20619 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20620
20621 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20622 Rewrite (Lo,
20623 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20624
20625 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20626 Rewrite (Hi,
20627 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20628 end if;
20629
20630 Set_Etype (Lo, T);
20631 Set_Etype (Hi, T);
20632 end if;
20633
20634 -- If the bounds of the range have been mistakenly given as string
20635 -- literals (perhaps in place of character literals), then an error
20636 -- has already been reported, but we rewrite the string literal as a
20637 -- bound of the range's type to avoid blowups in later processing
20638 -- that looks at static values.
20639
20640 if Nkind (Lo) = N_String_Literal then
20641 Rewrite (Lo,
20642 Make_Attribute_Reference (Sloc (Lo),
20643 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20644 Attribute_Name => Name_First));
20645 Analyze_And_Resolve (Lo);
20646 end if;
20647
20648 if Nkind (Hi) = N_String_Literal then
20649 Rewrite (Hi,
20650 Make_Attribute_Reference (Sloc (Hi),
20651 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20652 Attribute_Name => Name_First));
20653 Analyze_And_Resolve (Hi);
20654 end if;
20655
20656 -- If bounds aren't scalar at this point then exit, avoiding
20657 -- problems with further processing of the range in this procedure.
20658
20659 if not Is_Scalar_Type (Etype (Lo)) then
20660 return;
20661 end if;
20662
20663 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20664 -- then range of the base type. Here we check whether the bounds
20665 -- are in the range of the subtype itself. Note that if the bounds
20666 -- represent the null range the Constraint_Error exception should
20667 -- not be raised.
20668
20669 -- ??? The following code should be cleaned up as follows
20670
20671 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20672 -- is done in the call to Range_Check (R, T); below
20673
20674 -- 2. The use of R_Check_Off should be investigated and possibly
20675 -- removed, this would clean up things a bit.
20676
20677 if Is_Null_Range (Lo, Hi) then
20678 null;
20679
20680 else
20681 -- Capture values of bounds and generate temporaries for them
20682 -- if needed, before applying checks, since checks may cause
20683 -- duplication of the expression without forcing evaluation.
20684
20685 -- The forced evaluation removes side effects from expressions,
20686 -- which should occur also in GNATprove mode. Otherwise, we end up
20687 -- with unexpected insertions of actions at places where this is
20688 -- not supposed to occur, e.g. on default parameters of a call.
20689
20690 if Expander_Active or GNATprove_Mode then
20691
20692 -- Call Force_Evaluation to create declarations as needed to
20693 -- deal with side effects, and also create typ_FIRST/LAST
20694 -- entities for bounds if we have a subtype name.
20695
20696 -- Note: we do this transformation even if expansion is not
20697 -- active if we are in GNATprove_Mode since the transformation
20698 -- is in general required to ensure that the resulting tree has
20699 -- proper Ada semantics.
20700
20701 Force_Evaluation
20702 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20703 Force_Evaluation
20704 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20705 end if;
20706
20707 -- We use a flag here instead of suppressing checks on the type
20708 -- because the type we check against isn't necessarily the place
20709 -- where we put the check.
20710
20711 if not R_Check_Off then
20712 R_Checks := Get_Range_Checks (R, T);
20713
20714 -- Look up tree to find an appropriate insertion point. We
20715 -- can't just use insert_actions because later processing
20716 -- depends on the insertion node. Prior to Ada 2012 the
20717 -- insertion point could only be a declaration or a loop, but
20718 -- quantified expressions can appear within any context in an
20719 -- expression, and the insertion point can be any statement,
20720 -- pragma, or declaration.
20721
20722 Insert_Node := Parent (R);
20723 while Present (Insert_Node) loop
20724 exit when
20725 Nkind (Insert_Node) in N_Declaration
20726 and then
20727 not Nkind_In
20728 (Insert_Node, N_Component_Declaration,
20729 N_Loop_Parameter_Specification,
20730 N_Function_Specification,
20731 N_Procedure_Specification);
20732
20733 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20734 or else Nkind (Insert_Node) in
20735 N_Statement_Other_Than_Procedure_Call
20736 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20737 N_Pragma);
20738
20739 Insert_Node := Parent (Insert_Node);
20740 end loop;
20741
20742 -- Why would Type_Decl not be present??? Without this test,
20743 -- short regression tests fail.
20744
20745 if Present (Insert_Node) then
20746
20747 -- Case of loop statement. Verify that the range is part
20748 -- of the subtype indication of the iteration scheme.
20749
20750 if Nkind (Insert_Node) = N_Loop_Statement then
20751 declare
20752 Indic : Node_Id;
20753
20754 begin
20755 Indic := Parent (R);
20756 while Present (Indic)
20757 and then Nkind (Indic) /= N_Subtype_Indication
20758 loop
20759 Indic := Parent (Indic);
20760 end loop;
20761
20762 if Present (Indic) then
20763 Def_Id := Etype (Subtype_Mark (Indic));
20764
20765 Insert_Range_Checks
20766 (R_Checks,
20767 Insert_Node,
20768 Def_Id,
20769 Sloc (Insert_Node),
20770 R,
20771 Do_Before => True);
20772 end if;
20773 end;
20774
20775 -- Insertion before a declaration. If the declaration
20776 -- includes discriminants, the list of applicable checks
20777 -- is given by the caller.
20778
20779 elsif Nkind (Insert_Node) in N_Declaration then
20780 Def_Id := Defining_Identifier (Insert_Node);
20781
20782 if (Ekind (Def_Id) = E_Record_Type
20783 and then Depends_On_Discriminant (R))
20784 or else
20785 (Ekind (Def_Id) = E_Protected_Type
20786 and then Has_Discriminants (Def_Id))
20787 then
20788 Append_Range_Checks
20789 (R_Checks,
20790 Check_List, Def_Id, Sloc (Insert_Node), R);
20791
20792 else
20793 Insert_Range_Checks
20794 (R_Checks,
20795 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20796
20797 end if;
20798
20799 -- Insertion before a statement. Range appears in the
20800 -- context of a quantified expression. Insertion will
20801 -- take place when expression is expanded.
20802
20803 else
20804 null;
20805 end if;
20806 end if;
20807 end if;
20808 end if;
20809
20810 -- Case of other than an explicit N_Range node
20811
20812 -- The forced evaluation removes side effects from expressions, which
20813 -- should occur also in GNATprove mode. Otherwise, we end up with
20814 -- unexpected insertions of actions at places where this is not
20815 -- supposed to occur, e.g. on default parameters of a call.
20816
20817 elsif Expander_Active or GNATprove_Mode then
20818 Get_Index_Bounds (R, Lo, Hi);
20819 Force_Evaluation (Lo);
20820 Force_Evaluation (Hi);
20821 end if;
20822 end Process_Range_Expr_In_Decl;
20823
20824 --------------------------------------
20825 -- Process_Real_Range_Specification --
20826 --------------------------------------
20827
20828 procedure Process_Real_Range_Specification (Def : Node_Id) is
20829 Spec : constant Node_Id := Real_Range_Specification (Def);
20830 Lo : Node_Id;
20831 Hi : Node_Id;
20832 Err : Boolean := False;
20833
20834 procedure Analyze_Bound (N : Node_Id);
20835 -- Analyze and check one bound
20836
20837 -------------------
20838 -- Analyze_Bound --
20839 -------------------
20840
20841 procedure Analyze_Bound (N : Node_Id) is
20842 begin
20843 Analyze_And_Resolve (N, Any_Real);
20844
20845 if not Is_OK_Static_Expression (N) then
20846 Flag_Non_Static_Expr
20847 ("bound in real type definition is not static!", N);
20848 Err := True;
20849 end if;
20850 end Analyze_Bound;
20851
20852 -- Start of processing for Process_Real_Range_Specification
20853
20854 begin
20855 if Present (Spec) then
20856 Lo := Low_Bound (Spec);
20857 Hi := High_Bound (Spec);
20858 Analyze_Bound (Lo);
20859 Analyze_Bound (Hi);
20860
20861 -- If error, clear away junk range specification
20862
20863 if Err then
20864 Set_Real_Range_Specification (Def, Empty);
20865 end if;
20866 end if;
20867 end Process_Real_Range_Specification;
20868
20869 ---------------------
20870 -- Process_Subtype --
20871 ---------------------
20872
20873 function Process_Subtype
20874 (S : Node_Id;
20875 Related_Nod : Node_Id;
20876 Related_Id : Entity_Id := Empty;
20877 Suffix : Character := ' ') return Entity_Id
20878 is
20879 P : Node_Id;
20880 Def_Id : Entity_Id;
20881 Error_Node : Node_Id;
20882 Full_View_Id : Entity_Id;
20883 Subtype_Mark_Id : Entity_Id;
20884
20885 May_Have_Null_Exclusion : Boolean;
20886
20887 procedure Check_Incomplete (T : Node_Id);
20888 -- Called to verify that an incomplete type is not used prematurely
20889
20890 ----------------------
20891 -- Check_Incomplete --
20892 ----------------------
20893
20894 procedure Check_Incomplete (T : Node_Id) is
20895 begin
20896 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20897
20898 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20899 and then
20900 not (Ada_Version >= Ada_2005
20901 and then
20902 (Nkind (Parent (T)) = N_Subtype_Declaration
20903 or else (Nkind (Parent (T)) = N_Subtype_Indication
20904 and then Nkind (Parent (Parent (T))) =
20905 N_Subtype_Declaration)))
20906 then
20907 Error_Msg_N ("invalid use of type before its full declaration", T);
20908 end if;
20909 end Check_Incomplete;
20910
20911 -- Start of processing for Process_Subtype
20912
20913 begin
20914 -- Case of no constraints present
20915
20916 if Nkind (S) /= N_Subtype_Indication then
20917 Find_Type (S);
20918 Check_Incomplete (S);
20919 P := Parent (S);
20920
20921 -- Ada 2005 (AI-231): Static check
20922
20923 if Ada_Version >= Ada_2005
20924 and then Present (P)
20925 and then Null_Exclusion_Present (P)
20926 and then Nkind (P) /= N_Access_To_Object_Definition
20927 and then not Is_Access_Type (Entity (S))
20928 then
20929 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20930 end if;
20931
20932 -- The following is ugly, can't we have a range or even a flag???
20933
20934 May_Have_Null_Exclusion :=
20935 Nkind_In (P, N_Access_Definition,
20936 N_Access_Function_Definition,
20937 N_Access_Procedure_Definition,
20938 N_Access_To_Object_Definition,
20939 N_Allocator,
20940 N_Component_Definition)
20941 or else
20942 Nkind_In (P, N_Derived_Type_Definition,
20943 N_Discriminant_Specification,
20944 N_Formal_Object_Declaration,
20945 N_Object_Declaration,
20946 N_Object_Renaming_Declaration,
20947 N_Parameter_Specification,
20948 N_Subtype_Declaration);
20949
20950 -- Create an Itype that is a duplicate of Entity (S) but with the
20951 -- null-exclusion attribute.
20952
20953 if May_Have_Null_Exclusion
20954 and then Is_Access_Type (Entity (S))
20955 and then Null_Exclusion_Present (P)
20956
20957 -- No need to check the case of an access to object definition.
20958 -- It is correct to define double not-null pointers.
20959
20960 -- Example:
20961 -- type Not_Null_Int_Ptr is not null access Integer;
20962 -- type Acc is not null access Not_Null_Int_Ptr;
20963
20964 and then Nkind (P) /= N_Access_To_Object_Definition
20965 then
20966 if Can_Never_Be_Null (Entity (S)) then
20967 case Nkind (Related_Nod) is
20968 when N_Full_Type_Declaration =>
20969 if Nkind (Type_Definition (Related_Nod))
20970 in N_Array_Type_Definition
20971 then
20972 Error_Node :=
20973 Subtype_Indication
20974 (Component_Definition
20975 (Type_Definition (Related_Nod)));
20976 else
20977 Error_Node :=
20978 Subtype_Indication (Type_Definition (Related_Nod));
20979 end if;
20980
20981 when N_Subtype_Declaration =>
20982 Error_Node := Subtype_Indication (Related_Nod);
20983
20984 when N_Object_Declaration =>
20985 Error_Node := Object_Definition (Related_Nod);
20986
20987 when N_Component_Declaration =>
20988 Error_Node :=
20989 Subtype_Indication (Component_Definition (Related_Nod));
20990
20991 when N_Allocator =>
20992 Error_Node := Expression (Related_Nod);
20993
20994 when others =>
20995 pragma Assert (False);
20996 Error_Node := Related_Nod;
20997 end case;
20998
20999 Error_Msg_NE
21000 ("`NOT NULL` not allowed (& already excludes null)",
21001 Error_Node,
21002 Entity (S));
21003 end if;
21004
21005 Set_Etype (S,
21006 Create_Null_Excluding_Itype
21007 (T => Entity (S),
21008 Related_Nod => P));
21009 Set_Entity (S, Etype (S));
21010 end if;
21011
21012 return Entity (S);
21013
21014 -- Case of constraint present, so that we have an N_Subtype_Indication
21015 -- node (this node is created only if constraints are present).
21016
21017 else
21018 Find_Type (Subtype_Mark (S));
21019
21020 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21021 and then not
21022 (Nkind (Parent (S)) = N_Subtype_Declaration
21023 and then Is_Itype (Defining_Identifier (Parent (S))))
21024 then
21025 Check_Incomplete (Subtype_Mark (S));
21026 end if;
21027
21028 P := Parent (S);
21029 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21030
21031 -- Explicit subtype declaration case
21032
21033 if Nkind (P) = N_Subtype_Declaration then
21034 Def_Id := Defining_Identifier (P);
21035
21036 -- Explicit derived type definition case
21037
21038 elsif Nkind (P) = N_Derived_Type_Definition then
21039 Def_Id := Defining_Identifier (Parent (P));
21040
21041 -- Implicit case, the Def_Id must be created as an implicit type.
21042 -- The one exception arises in the case of concurrent types, array
21043 -- and access types, where other subsidiary implicit types may be
21044 -- created and must appear before the main implicit type. In these
21045 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21046 -- has not yet been called to create Def_Id.
21047
21048 else
21049 if Is_Array_Type (Subtype_Mark_Id)
21050 or else Is_Concurrent_Type (Subtype_Mark_Id)
21051 or else Is_Access_Type (Subtype_Mark_Id)
21052 then
21053 Def_Id := Empty;
21054
21055 -- For the other cases, we create a new unattached Itype,
21056 -- and set the indication to ensure it gets attached later.
21057
21058 else
21059 Def_Id :=
21060 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21061 end if;
21062 end if;
21063
21064 -- If the kind of constraint is invalid for this kind of type,
21065 -- then give an error, and then pretend no constraint was given.
21066
21067 if not Is_Valid_Constraint_Kind
21068 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21069 then
21070 Error_Msg_N
21071 ("incorrect constraint for this kind of type", Constraint (S));
21072
21073 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21074
21075 -- Set Ekind of orphan itype, to prevent cascaded errors
21076
21077 if Present (Def_Id) then
21078 Set_Ekind (Def_Id, Ekind (Any_Type));
21079 end if;
21080
21081 -- Make recursive call, having got rid of the bogus constraint
21082
21083 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21084 end if;
21085
21086 -- Remaining processing depends on type. Select on Base_Type kind to
21087 -- ensure getting to the concrete type kind in the case of a private
21088 -- subtype (needed when only doing semantic analysis).
21089
21090 case Ekind (Base_Type (Subtype_Mark_Id)) is
21091 when Access_Kind =>
21092
21093 -- If this is a constraint on a class-wide type, discard it.
21094 -- There is currently no way to express a partial discriminant
21095 -- constraint on a type with unknown discriminants. This is
21096 -- a pathology that the ACATS wisely decides not to test.
21097
21098 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21099 if Comes_From_Source (S) then
21100 Error_Msg_N
21101 ("constraint on class-wide type ignored??",
21102 Constraint (S));
21103 end if;
21104
21105 if Nkind (P) = N_Subtype_Declaration then
21106 Set_Subtype_Indication (P,
21107 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21108 end if;
21109
21110 return Subtype_Mark_Id;
21111 end if;
21112
21113 Constrain_Access (Def_Id, S, Related_Nod);
21114
21115 if Expander_Active
21116 and then Is_Itype (Designated_Type (Def_Id))
21117 and then Nkind (Related_Nod) = N_Subtype_Declaration
21118 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21119 then
21120 Build_Itype_Reference
21121 (Designated_Type (Def_Id), Related_Nod);
21122 end if;
21123
21124 when Array_Kind =>
21125 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21126
21127 when Decimal_Fixed_Point_Kind =>
21128 Constrain_Decimal (Def_Id, S);
21129
21130 when Enumeration_Kind =>
21131 Constrain_Enumeration (Def_Id, S);
21132 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21133
21134 when Ordinary_Fixed_Point_Kind =>
21135 Constrain_Ordinary_Fixed (Def_Id, S);
21136
21137 when Float_Kind =>
21138 Constrain_Float (Def_Id, S);
21139
21140 when Integer_Kind =>
21141 Constrain_Integer (Def_Id, S);
21142 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21143
21144 when Class_Wide_Kind
21145 | E_Incomplete_Type
21146 | E_Record_Subtype
21147 | E_Record_Type
21148 =>
21149 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21150
21151 if Ekind (Def_Id) = E_Incomplete_Type then
21152 Set_Private_Dependents (Def_Id, New_Elmt_List);
21153 end if;
21154
21155 when Private_Kind =>
21156 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21157
21158 -- The base type may be private but Def_Id may be a full view
21159 -- in an instance.
21160
21161 if Is_Private_Type (Def_Id) then
21162 Set_Private_Dependents (Def_Id, New_Elmt_List);
21163 end if;
21164
21165 -- In case of an invalid constraint prevent further processing
21166 -- since the type constructed is missing expected fields.
21167
21168 if Etype (Def_Id) = Any_Type then
21169 return Def_Id;
21170 end if;
21171
21172 -- If the full view is that of a task with discriminants,
21173 -- we must constrain both the concurrent type and its
21174 -- corresponding record type. Otherwise we will just propagate
21175 -- the constraint to the full view, if available.
21176
21177 if Present (Full_View (Subtype_Mark_Id))
21178 and then Has_Discriminants (Subtype_Mark_Id)
21179 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21180 then
21181 Full_View_Id :=
21182 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21183
21184 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21185 Constrain_Concurrent (Full_View_Id, S,
21186 Related_Nod, Related_Id, Suffix);
21187 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21188 Set_Full_View (Def_Id, Full_View_Id);
21189
21190 -- Introduce an explicit reference to the private subtype,
21191 -- to prevent scope anomalies in gigi if first use appears
21192 -- in a nested context, e.g. a later function body.
21193 -- Should this be generated in other contexts than a full
21194 -- type declaration?
21195
21196 if Is_Itype (Def_Id)
21197 and then
21198 Nkind (Parent (P)) = N_Full_Type_Declaration
21199 then
21200 Build_Itype_Reference (Def_Id, Parent (P));
21201 end if;
21202
21203 else
21204 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21205 end if;
21206
21207 when Concurrent_Kind =>
21208 Constrain_Concurrent (Def_Id, S,
21209 Related_Nod, Related_Id, Suffix);
21210
21211 when others =>
21212 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21213 end case;
21214
21215 -- Size and Convention are always inherited from the base type
21216
21217 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21218 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21219
21220 return Def_Id;
21221 end if;
21222 end Process_Subtype;
21223
21224 -----------------------------
21225 -- Record_Type_Declaration --
21226 -----------------------------
21227
21228 procedure Record_Type_Declaration
21229 (T : Entity_Id;
21230 N : Node_Id;
21231 Prev : Entity_Id)
21232 is
21233 Def : constant Node_Id := Type_Definition (N);
21234 Is_Tagged : Boolean;
21235 Tag_Comp : Entity_Id;
21236
21237 begin
21238 -- These flags must be initialized before calling Process_Discriminants
21239 -- because this routine makes use of them.
21240
21241 Set_Ekind (T, E_Record_Type);
21242 Set_Etype (T, T);
21243 Init_Size_Align (T);
21244 Set_Interfaces (T, No_Elist);
21245 Set_Stored_Constraint (T, No_Elist);
21246 Set_Default_SSO (T);
21247
21248 -- Normal case
21249
21250 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21251 if Limited_Present (Def) then
21252 Check_SPARK_05_Restriction ("limited is not allowed", N);
21253 end if;
21254
21255 if Abstract_Present (Def) then
21256 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21257 end if;
21258
21259 -- The flag Is_Tagged_Type might have already been set by
21260 -- Find_Type_Name if it detected an error for declaration T. This
21261 -- arises in the case of private tagged types where the full view
21262 -- omits the word tagged.
21263
21264 Is_Tagged :=
21265 Tagged_Present (Def)
21266 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21267
21268 Set_Is_Limited_Record (T, Limited_Present (Def));
21269
21270 if Is_Tagged then
21271 Set_Is_Tagged_Type (T, True);
21272 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21273 end if;
21274
21275 -- Type is abstract if full declaration carries keyword, or if
21276 -- previous partial view did.
21277
21278 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21279 or else Abstract_Present (Def));
21280
21281 else
21282 Check_SPARK_05_Restriction ("interface is not allowed", N);
21283
21284 Is_Tagged := True;
21285 Analyze_Interface_Declaration (T, Def);
21286
21287 if Present (Discriminant_Specifications (N)) then
21288 Error_Msg_N
21289 ("interface types cannot have discriminants",
21290 Defining_Identifier
21291 (First (Discriminant_Specifications (N))));
21292 end if;
21293 end if;
21294
21295 -- First pass: if there are self-referential access components,
21296 -- create the required anonymous access type declarations, and if
21297 -- need be an incomplete type declaration for T itself.
21298
21299 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21300
21301 if Ada_Version >= Ada_2005
21302 and then Present (Interface_List (Def))
21303 then
21304 Check_Interfaces (N, Def);
21305
21306 declare
21307 Ifaces_List : Elist_Id;
21308
21309 begin
21310 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21311 -- already in the parents.
21312
21313 Collect_Interfaces
21314 (T => T,
21315 Ifaces_List => Ifaces_List,
21316 Exclude_Parents => True);
21317
21318 Set_Interfaces (T, Ifaces_List);
21319 end;
21320 end if;
21321
21322 -- Records constitute a scope for the component declarations within.
21323 -- The scope is created prior to the processing of these declarations.
21324 -- Discriminants are processed first, so that they are visible when
21325 -- processing the other components. The Ekind of the record type itself
21326 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21327
21328 -- Enter record scope
21329
21330 Push_Scope (T);
21331
21332 -- If an incomplete or private type declaration was already given for
21333 -- the type, then this scope already exists, and the discriminants have
21334 -- been declared within. We must verify that the full declaration
21335 -- matches the incomplete one.
21336
21337 Check_Or_Process_Discriminants (N, T, Prev);
21338
21339 Set_Is_Constrained (T, not Has_Discriminants (T));
21340 Set_Has_Delayed_Freeze (T, True);
21341
21342 -- For tagged types add a manually analyzed component corresponding
21343 -- to the component _tag, the corresponding piece of tree will be
21344 -- expanded as part of the freezing actions if it is not a CPP_Class.
21345
21346 if Is_Tagged then
21347
21348 -- Do not add the tag unless we are in expansion mode
21349
21350 if Expander_Active then
21351 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21352 Enter_Name (Tag_Comp);
21353
21354 Set_Ekind (Tag_Comp, E_Component);
21355 Set_Is_Tag (Tag_Comp);
21356 Set_Is_Aliased (Tag_Comp);
21357 Set_Etype (Tag_Comp, RTE (RE_Tag));
21358 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21359 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21360 Init_Component_Location (Tag_Comp);
21361
21362 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21363 -- implemented interfaces.
21364
21365 if Has_Interfaces (T) then
21366 Add_Interface_Tag_Components (N, T);
21367 end if;
21368 end if;
21369
21370 Make_Class_Wide_Type (T);
21371 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21372 end if;
21373
21374 -- We must suppress range checks when processing record components in
21375 -- the presence of discriminants, since we don't want spurious checks to
21376 -- be generated during their analysis, but Suppress_Range_Checks flags
21377 -- must be reset the after processing the record definition.
21378
21379 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21380 -- couldn't we just use the normal range check suppression method here.
21381 -- That would seem cleaner ???
21382
21383 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21384 Set_Kill_Range_Checks (T, True);
21385 Record_Type_Definition (Def, Prev);
21386 Set_Kill_Range_Checks (T, False);
21387 else
21388 Record_Type_Definition (Def, Prev);
21389 end if;
21390
21391 -- Exit from record scope
21392
21393 End_Scope;
21394
21395 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21396 -- the implemented interfaces and associate them an aliased entity.
21397
21398 if Is_Tagged
21399 and then not Is_Empty_List (Interface_List (Def))
21400 then
21401 Derive_Progenitor_Subprograms (T, T);
21402 end if;
21403
21404 Check_Function_Writable_Actuals (N);
21405 end Record_Type_Declaration;
21406
21407 ----------------------------
21408 -- Record_Type_Definition --
21409 ----------------------------
21410
21411 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21412 Component : Entity_Id;
21413 Ctrl_Components : Boolean := False;
21414 Final_Storage_Only : Boolean;
21415 T : Entity_Id;
21416
21417 begin
21418 if Ekind (Prev_T) = E_Incomplete_Type then
21419 T := Full_View (Prev_T);
21420 else
21421 T := Prev_T;
21422 end if;
21423
21424 -- In SPARK, tagged types and type extensions may only be declared in
21425 -- the specification of library unit packages.
21426
21427 if Present (Def) and then Is_Tagged_Type (T) then
21428 declare
21429 Typ : Node_Id;
21430 Ctxt : Node_Id;
21431
21432 begin
21433 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21434 Typ := Parent (Def);
21435 else
21436 pragma Assert
21437 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21438 Typ := Parent (Parent (Def));
21439 end if;
21440
21441 Ctxt := Parent (Typ);
21442
21443 if Nkind (Ctxt) = N_Package_Body
21444 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21445 then
21446 Check_SPARK_05_Restriction
21447 ("type should be defined in package specification", Typ);
21448
21449 elsif Nkind (Ctxt) /= N_Package_Specification
21450 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21451 then
21452 Check_SPARK_05_Restriction
21453 ("type should be defined in library unit package", Typ);
21454 end if;
21455 end;
21456 end if;
21457
21458 Final_Storage_Only := not Is_Controlled_Active (T);
21459
21460 -- Ada 2005: Check whether an explicit Limited is present in a derived
21461 -- type declaration.
21462
21463 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21464 and then Limited_Present (Parent (Def))
21465 then
21466 Set_Is_Limited_Record (T);
21467 end if;
21468
21469 -- If the component list of a record type is defined by the reserved
21470 -- word null and there is no discriminant part, then the record type has
21471 -- no components and all records of the type are null records (RM 3.7)
21472 -- This procedure is also called to process the extension part of a
21473 -- record extension, in which case the current scope may have inherited
21474 -- components.
21475
21476 if No (Def)
21477 or else No (Component_List (Def))
21478 or else Null_Present (Component_List (Def))
21479 then
21480 if not Is_Tagged_Type (T) then
21481 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21482 end if;
21483
21484 else
21485 Analyze_Declarations (Component_Items (Component_List (Def)));
21486
21487 if Present (Variant_Part (Component_List (Def))) then
21488 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21489 Analyze (Variant_Part (Component_List (Def)));
21490 end if;
21491 end if;
21492
21493 -- After completing the semantic analysis of the record definition,
21494 -- record components, both new and inherited, are accessible. Set their
21495 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21496 -- whose Ekind may be void.
21497
21498 Component := First_Entity (Current_Scope);
21499 while Present (Component) loop
21500 if Ekind (Component) = E_Void
21501 and then not Is_Itype (Component)
21502 then
21503 Set_Ekind (Component, E_Component);
21504 Init_Component_Location (Component);
21505 end if;
21506
21507 Propagate_Concurrent_Flags (T, Etype (Component));
21508
21509 if Ekind (Component) /= E_Component then
21510 null;
21511
21512 -- Do not set Has_Controlled_Component on a class-wide equivalent
21513 -- type. See Make_CW_Equivalent_Type.
21514
21515 elsif not Is_Class_Wide_Equivalent_Type (T)
21516 and then (Has_Controlled_Component (Etype (Component))
21517 or else (Chars (Component) /= Name_uParent
21518 and then Is_Controlled_Active
21519 (Etype (Component))))
21520 then
21521 Set_Has_Controlled_Component (T, True);
21522 Final_Storage_Only :=
21523 Final_Storage_Only
21524 and then Finalize_Storage_Only (Etype (Component));
21525 Ctrl_Components := True;
21526 end if;
21527
21528 Next_Entity (Component);
21529 end loop;
21530
21531 -- A Type is Finalize_Storage_Only only if all its controlled components
21532 -- are also.
21533
21534 if Ctrl_Components then
21535 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21536 end if;
21537
21538 -- Place reference to end record on the proper entity, which may
21539 -- be a partial view.
21540
21541 if Present (Def) then
21542 Process_End_Label (Def, 'e', Prev_T);
21543 end if;
21544 end Record_Type_Definition;
21545
21546 ------------------------
21547 -- Replace_Components --
21548 ------------------------
21549
21550 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21551 function Process (N : Node_Id) return Traverse_Result;
21552
21553 -------------
21554 -- Process --
21555 -------------
21556
21557 function Process (N : Node_Id) return Traverse_Result is
21558 Comp : Entity_Id;
21559
21560 begin
21561 if Nkind (N) = N_Discriminant_Specification then
21562 Comp := First_Discriminant (Typ);
21563 while Present (Comp) loop
21564 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21565 Set_Defining_Identifier (N, Comp);
21566 exit;
21567 end if;
21568
21569 Next_Discriminant (Comp);
21570 end loop;
21571
21572 elsif Nkind (N) = N_Component_Declaration then
21573 Comp := First_Component (Typ);
21574 while Present (Comp) loop
21575 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21576 Set_Defining_Identifier (N, Comp);
21577 exit;
21578 end if;
21579
21580 Next_Component (Comp);
21581 end loop;
21582 end if;
21583
21584 return OK;
21585 end Process;
21586
21587 procedure Replace is new Traverse_Proc (Process);
21588
21589 -- Start of processing for Replace_Components
21590
21591 begin
21592 Replace (Decl);
21593 end Replace_Components;
21594
21595 -------------------------------
21596 -- Set_Completion_Referenced --
21597 -------------------------------
21598
21599 procedure Set_Completion_Referenced (E : Entity_Id) is
21600 begin
21601 -- If in main unit, mark entity that is a completion as referenced,
21602 -- warnings go on the partial view when needed.
21603
21604 if In_Extended_Main_Source_Unit (E) then
21605 Set_Referenced (E);
21606 end if;
21607 end Set_Completion_Referenced;
21608
21609 ---------------------
21610 -- Set_Default_SSO --
21611 ---------------------
21612
21613 procedure Set_Default_SSO (T : Entity_Id) is
21614 begin
21615 case Opt.Default_SSO is
21616 when ' ' =>
21617 null;
21618 when 'L' =>
21619 Set_SSO_Set_Low_By_Default (T, True);
21620 when 'H' =>
21621 Set_SSO_Set_High_By_Default (T, True);
21622 when others =>
21623 raise Program_Error;
21624 end case;
21625 end Set_Default_SSO;
21626
21627 ---------------------
21628 -- Set_Fixed_Range --
21629 ---------------------
21630
21631 -- The range for fixed-point types is complicated by the fact that we
21632 -- do not know the exact end points at the time of the declaration. This
21633 -- is true for three reasons:
21634
21635 -- A size clause may affect the fudging of the end-points.
21636 -- A small clause may affect the values of the end-points.
21637 -- We try to include the end-points if it does not affect the size.
21638
21639 -- This means that the actual end-points must be established at the
21640 -- point when the type is frozen. Meanwhile, we first narrow the range
21641 -- as permitted (so that it will fit if necessary in a small specified
21642 -- size), and then build a range subtree with these narrowed bounds.
21643 -- Set_Fixed_Range constructs the range from real literal values, and
21644 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21645
21646 -- The parent of this range is set to point to the entity so that it is
21647 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21648 -- other scalar types, which are just pointers to the range in the
21649 -- original tree, this would otherwise be an orphan).
21650
21651 -- The tree is left unanalyzed. When the type is frozen, the processing
21652 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21653 -- analyzed, and uses this as an indication that it should complete
21654 -- work on the range (it will know the final small and size values).
21655
21656 procedure Set_Fixed_Range
21657 (E : Entity_Id;
21658 Loc : Source_Ptr;
21659 Lo : Ureal;
21660 Hi : Ureal)
21661 is
21662 S : constant Node_Id :=
21663 Make_Range (Loc,
21664 Low_Bound => Make_Real_Literal (Loc, Lo),
21665 High_Bound => Make_Real_Literal (Loc, Hi));
21666 begin
21667 Set_Scalar_Range (E, S);
21668 Set_Parent (S, E);
21669
21670 -- Before the freeze point, the bounds of a fixed point are universal
21671 -- and carry the corresponding type.
21672
21673 Set_Etype (Low_Bound (S), Universal_Real);
21674 Set_Etype (High_Bound (S), Universal_Real);
21675 end Set_Fixed_Range;
21676
21677 ----------------------------------
21678 -- Set_Scalar_Range_For_Subtype --
21679 ----------------------------------
21680
21681 procedure Set_Scalar_Range_For_Subtype
21682 (Def_Id : Entity_Id;
21683 R : Node_Id;
21684 Subt : Entity_Id)
21685 is
21686 Kind : constant Entity_Kind := Ekind (Def_Id);
21687
21688 begin
21689 -- Defend against previous error
21690
21691 if Nkind (R) = N_Error then
21692 return;
21693 end if;
21694
21695 Set_Scalar_Range (Def_Id, R);
21696
21697 -- We need to link the range into the tree before resolving it so
21698 -- that types that are referenced, including importantly the subtype
21699 -- itself, are properly frozen (Freeze_Expression requires that the
21700 -- expression be properly linked into the tree). Of course if it is
21701 -- already linked in, then we do not disturb the current link.
21702
21703 if No (Parent (R)) then
21704 Set_Parent (R, Def_Id);
21705 end if;
21706
21707 -- Reset the kind of the subtype during analysis of the range, to
21708 -- catch possible premature use in the bounds themselves.
21709
21710 Set_Ekind (Def_Id, E_Void);
21711 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21712 Set_Ekind (Def_Id, Kind);
21713 end Set_Scalar_Range_For_Subtype;
21714
21715 --------------------------------------------------------
21716 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21717 --------------------------------------------------------
21718
21719 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21720 (E : Entity_Id)
21721 is
21722 begin
21723 -- Make sure set if encountered during Expand_To_Stored_Constraint
21724
21725 Set_Stored_Constraint (E, No_Elist);
21726
21727 -- Give it the right value
21728
21729 if Is_Constrained (E) and then Has_Discriminants (E) then
21730 Set_Stored_Constraint (E,
21731 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21732 end if;
21733 end Set_Stored_Constraint_From_Discriminant_Constraint;
21734
21735 -------------------------------------
21736 -- Signed_Integer_Type_Declaration --
21737 -------------------------------------
21738
21739 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21740 Implicit_Base : Entity_Id;
21741 Base_Typ : Entity_Id;
21742 Lo_Val : Uint;
21743 Hi_Val : Uint;
21744 Errs : Boolean := False;
21745 Lo : Node_Id;
21746 Hi : Node_Id;
21747
21748 function Can_Derive_From (E : Entity_Id) return Boolean;
21749 -- Determine whether given bounds allow derivation from specified type
21750
21751 procedure Check_Bound (Expr : Node_Id);
21752 -- Check bound to make sure it is integral and static. If not, post
21753 -- appropriate error message and set Errs flag
21754
21755 ---------------------
21756 -- Can_Derive_From --
21757 ---------------------
21758
21759 -- Note we check both bounds against both end values, to deal with
21760 -- strange types like ones with a range of 0 .. -12341234.
21761
21762 function Can_Derive_From (E : Entity_Id) return Boolean is
21763 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21764 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21765 begin
21766 return Lo <= Lo_Val and then Lo_Val <= Hi
21767 and then
21768 Lo <= Hi_Val and then Hi_Val <= Hi;
21769 end Can_Derive_From;
21770
21771 -----------------
21772 -- Check_Bound --
21773 -----------------
21774
21775 procedure Check_Bound (Expr : Node_Id) is
21776 begin
21777 -- If a range constraint is used as an integer type definition, each
21778 -- bound of the range must be defined by a static expression of some
21779 -- integer type, but the two bounds need not have the same integer
21780 -- type (Negative bounds are allowed.) (RM 3.5.4)
21781
21782 if not Is_Integer_Type (Etype (Expr)) then
21783 Error_Msg_N
21784 ("integer type definition bounds must be of integer type", Expr);
21785 Errs := True;
21786
21787 elsif not Is_OK_Static_Expression (Expr) then
21788 Flag_Non_Static_Expr
21789 ("non-static expression used for integer type bound!", Expr);
21790 Errs := True;
21791
21792 -- The bounds are folded into literals, and we set their type to be
21793 -- universal, to avoid typing difficulties: we cannot set the type
21794 -- of the literal to the new type, because this would be a forward
21795 -- reference for the back end, and if the original type is user-
21796 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21797
21798 else
21799 if Is_Entity_Name (Expr) then
21800 Fold_Uint (Expr, Expr_Value (Expr), True);
21801 end if;
21802
21803 Set_Etype (Expr, Universal_Integer);
21804 end if;
21805 end Check_Bound;
21806
21807 -- Start of processing for Signed_Integer_Type_Declaration
21808
21809 begin
21810 -- Create an anonymous base type
21811
21812 Implicit_Base :=
21813 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21814
21815 -- Analyze and check the bounds, they can be of any integer type
21816
21817 Lo := Low_Bound (Def);
21818 Hi := High_Bound (Def);
21819
21820 -- Arbitrarily use Integer as the type if either bound had an error
21821
21822 if Hi = Error or else Lo = Error then
21823 Base_Typ := Any_Integer;
21824 Set_Error_Posted (T, True);
21825
21826 -- Here both bounds are OK expressions
21827
21828 else
21829 Analyze_And_Resolve (Lo, Any_Integer);
21830 Analyze_And_Resolve (Hi, Any_Integer);
21831
21832 Check_Bound (Lo);
21833 Check_Bound (Hi);
21834
21835 if Errs then
21836 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21837 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21838 end if;
21839
21840 -- Find type to derive from
21841
21842 Lo_Val := Expr_Value (Lo);
21843 Hi_Val := Expr_Value (Hi);
21844
21845 if Can_Derive_From (Standard_Short_Short_Integer) then
21846 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21847
21848 elsif Can_Derive_From (Standard_Short_Integer) then
21849 Base_Typ := Base_Type (Standard_Short_Integer);
21850
21851 elsif Can_Derive_From (Standard_Integer) then
21852 Base_Typ := Base_Type (Standard_Integer);
21853
21854 elsif Can_Derive_From (Standard_Long_Integer) then
21855 Base_Typ := Base_Type (Standard_Long_Integer);
21856
21857 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21858 Check_Restriction (No_Long_Long_Integers, Def);
21859 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21860
21861 else
21862 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21863 Error_Msg_N ("integer type definition bounds out of range", Def);
21864 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21865 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21866 end if;
21867 end if;
21868
21869 -- Complete both implicit base and declared first subtype entities. The
21870 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21871 -- are not clobbered when the signed integer type acts as a full view of
21872 -- a private type.
21873
21874 Set_Etype (Implicit_Base, Base_Typ);
21875 Set_Size_Info (Implicit_Base, Base_Typ);
21876 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21877 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21878 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21879
21880 Set_Ekind (T, E_Signed_Integer_Subtype);
21881 Set_Etype (T, Implicit_Base);
21882 Set_Size_Info (T, Implicit_Base);
21883 Inherit_Rep_Item_Chain (T, Implicit_Base);
21884 Set_Scalar_Range (T, Def);
21885 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21886 Set_Is_Constrained (T);
21887 end Signed_Integer_Type_Declaration;
21888
21889 end Sem_Ch3;