]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/sem_ch3.adb
exp_ch9.adb (Build_Corresponding_Record): Propagate type invariants to the correspond...
[gcc.git] / gcc / ada / sem_ch3.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch10; use Sem_Ch10;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Prag; use Sem_Prag;
69 with Sem_Res; use Sem_Res;
70 with Sem_Smem; use Sem_Smem;
71 with Sem_Type; use Sem_Type;
72 with Sem_Util; use Sem_Util;
73 with Sem_Warn; use Sem_Warn;
74 with Stand; use Stand;
75 with Sinfo; use Sinfo;
76 with Sinput; use Sinput;
77 with Snames; use Snames;
78 with Targparm; use Targparm;
79 with Tbuild; use Tbuild;
80 with Ttypes; use Ttypes;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
83
84 package body Sem_Ch3 is
85
86 -----------------------
87 -- Local Subprograms --
88 -----------------------
89
90 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
91 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
92 -- abstract interface types implemented by a record type or a derived
93 -- record type.
94
95 procedure Analyze_Object_Contract (Obj_Id : Entity_Id);
96 -- Analyze all delayed pragmas chained on the contract of object Obj_Id as
97 -- if they appeared at the end of the declarative region. The pragmas to be
98 -- considered are:
99 -- Async_Readers
100 -- Async_Writers
101 -- Effective_Reads
102 -- Effective_Writes
103 -- Part_Of
104
105 procedure Build_Derived_Type
106 (N : Node_Id;
107 Parent_Type : Entity_Id;
108 Derived_Type : Entity_Id;
109 Is_Completion : Boolean;
110 Derive_Subps : Boolean := True);
111 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
112 -- the N_Full_Type_Declaration node containing the derived type definition.
113 -- Parent_Type is the entity for the parent type in the derived type
114 -- definition and Derived_Type the actual derived type. Is_Completion must
115 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
116 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
117 -- completion of a private type declaration. If Is_Completion is set to
118 -- True, N is the completion of a private type declaration and Derived_Type
119 -- is different from the defining identifier inside N (i.e. Derived_Type /=
120 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
121 -- subprograms should be derived. The only case where this parameter is
122 -- False is when Build_Derived_Type is recursively called to process an
123 -- implicit derived full type for a type derived from a private type (in
124 -- that case the subprograms must only be derived for the private view of
125 -- the type).
126 --
127 -- ??? These flags need a bit of re-examination and re-documentation:
128 -- ??? are they both necessary (both seem related to the recursion)?
129
130 procedure Build_Derived_Access_Type
131 (N : Node_Id;
132 Parent_Type : Entity_Id;
133 Derived_Type : Entity_Id);
134 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
135 -- create an implicit base if the parent type is constrained or if the
136 -- subtype indication has a constraint.
137
138 procedure Build_Derived_Array_Type
139 (N : Node_Id;
140 Parent_Type : Entity_Id;
141 Derived_Type : Entity_Id);
142 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
143 -- create an implicit base if the parent type is constrained or if the
144 -- subtype indication has a constraint.
145
146 procedure Build_Derived_Concurrent_Type
147 (N : Node_Id;
148 Parent_Type : Entity_Id;
149 Derived_Type : Entity_Id);
150 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
151 -- protected type, inherit entries and protected subprograms, check
152 -- legality of discriminant constraints if any.
153
154 procedure Build_Derived_Enumeration_Type
155 (N : Node_Id;
156 Parent_Type : Entity_Id;
157 Derived_Type : Entity_Id);
158 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
159 -- type, we must create a new list of literals. Types derived from
160 -- Character and [Wide_]Wide_Character are special-cased.
161
162 procedure Build_Derived_Numeric_Type
163 (N : Node_Id;
164 Parent_Type : Entity_Id;
165 Derived_Type : Entity_Id);
166 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
167 -- an anonymous base type, and propagate constraint to subtype if needed.
168
169 procedure Build_Derived_Private_Type
170 (N : Node_Id;
171 Parent_Type : Entity_Id;
172 Derived_Type : Entity_Id;
173 Is_Completion : Boolean;
174 Derive_Subps : Boolean := True);
175 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
176 -- because the parent may or may not have a completion, and the derivation
177 -- may itself be a completion.
178
179 procedure Build_Derived_Record_Type
180 (N : Node_Id;
181 Parent_Type : Entity_Id;
182 Derived_Type : Entity_Id;
183 Derive_Subps : Boolean := True);
184 -- Subsidiary procedure used for tagged and untagged record types
185 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
186 -- All parameters are as in Build_Derived_Type except that N, in
187 -- addition to being an N_Full_Type_Declaration node, can also be an
188 -- N_Private_Extension_Declaration node. See the definition of this routine
189 -- for much more info. Derive_Subps indicates whether subprograms should be
190 -- derived from the parent type. The only case where Derive_Subps is False
191 -- is for an implicit derived full type for a type derived from a private
192 -- type (see Build_Derived_Type).
193
194 procedure Build_Discriminal (Discrim : Entity_Id);
195 -- Create the discriminal corresponding to discriminant Discrim, that is
196 -- the parameter corresponding to Discrim to be used in initialization
197 -- procedures for the type where Discrim is a discriminant. Discriminals
198 -- are not used during semantic analysis, and are not fully defined
199 -- entities until expansion. Thus they are not given a scope until
200 -- initialization procedures are built.
201
202 function Build_Discriminant_Constraints
203 (T : Entity_Id;
204 Def : Node_Id;
205 Derived_Def : Boolean := False) return Elist_Id;
206 -- Validate discriminant constraints and return the list of the constraints
207 -- in order of discriminant declarations, where T is the discriminated
208 -- unconstrained type. Def is the N_Subtype_Indication node where the
209 -- discriminants constraints for T are specified. Derived_Def is True
210 -- when building the discriminant constraints in a derived type definition
211 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
212 -- type and Def is the constraint "(xxx)" on T and this routine sets the
213 -- Corresponding_Discriminant field of the discriminants in the derived
214 -- type D to point to the corresponding discriminants in the parent type T.
215
216 procedure Build_Discriminated_Subtype
217 (T : Entity_Id;
218 Def_Id : Entity_Id;
219 Elist : Elist_Id;
220 Related_Nod : Node_Id;
221 For_Access : Boolean := False);
222 -- Subsidiary procedure to Constrain_Discriminated_Type and to
223 -- Process_Incomplete_Dependents. Given
224 --
225 -- T (a possibly discriminated base type)
226 -- Def_Id (a very partially built subtype for T),
227 --
228 -- the call completes Def_Id to be the appropriate E_*_Subtype.
229 --
230 -- The Elist is the list of discriminant constraints if any (it is set
231 -- to No_Elist if T is not a discriminated type, and to an empty list if
232 -- T has discriminants but there are no discriminant constraints). The
233 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
234 -- The For_Access says whether or not this subtype is really constraining
235 -- an access type. That is its sole purpose is the designated type of an
236 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
237 -- is built to avoid freezing T when the access subtype is frozen.
238
239 function Build_Scalar_Bound
240 (Bound : Node_Id;
241 Par_T : Entity_Id;
242 Der_T : Entity_Id) return Node_Id;
243 -- The bounds of a derived scalar type are conversions of the bounds of
244 -- the parent type. Optimize the representation if the bounds are literals.
245 -- Needs a more complete spec--what are the parameters exactly, and what
246 -- exactly is the returned value, and how is Bound affected???
247
248 procedure Build_Underlying_Full_View
249 (N : Node_Id;
250 Typ : Entity_Id;
251 Par : Entity_Id);
252 -- If the completion of a private type is itself derived from a private
253 -- type, or if the full view of a private subtype is itself private, the
254 -- back-end has no way to compute the actual size of this type. We build
255 -- an internal subtype declaration of the proper parent type to convey
256 -- this information. This extra mechanism is needed because a full
257 -- view cannot itself have a full view (it would get clobbered during
258 -- view exchanges).
259
260 procedure Check_Access_Discriminant_Requires_Limited
261 (D : Node_Id;
262 Loc : Node_Id);
263 -- Check the restriction that the type to which an access discriminant
264 -- belongs must be a concurrent type or a descendant of a type with
265 -- the reserved word 'limited' in its declaration.
266
267 procedure Check_Anonymous_Access_Components
268 (Typ_Decl : Node_Id;
269 Typ : Entity_Id;
270 Prev : Entity_Id;
271 Comp_List : Node_Id);
272 -- Ada 2005 AI-382: an access component in a record definition can refer to
273 -- the enclosing record, in which case it denotes the type itself, and not
274 -- the current instance of the type. We create an anonymous access type for
275 -- the component, and flag it as an access to a component, so accessibility
276 -- checks are properly performed on it. The declaration of the access type
277 -- is placed ahead of that of the record to prevent order-of-elaboration
278 -- circularity issues in Gigi. We create an incomplete type for the record
279 -- declaration, which is the designated type of the anonymous access.
280
281 procedure Check_Delta_Expression (E : Node_Id);
282 -- Check that the expression represented by E is suitable for use as a
283 -- delta expression, i.e. it is of real type and is static.
284
285 procedure Check_Digits_Expression (E : Node_Id);
286 -- Check that the expression represented by E is suitable for use as a
287 -- digits expression, i.e. it is of integer type, positive and static.
288
289 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
290 -- Validate the initialization of an object declaration. T is the required
291 -- type, and Exp is the initialization expression.
292
293 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
294 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
295
296 procedure Check_Or_Process_Discriminants
297 (N : Node_Id;
298 T : Entity_Id;
299 Prev : Entity_Id := Empty);
300 -- If N is the full declaration of the completion T of an incomplete or
301 -- private type, check its discriminants (which are already known to be
302 -- conformant with those of the partial view, see Find_Type_Name),
303 -- otherwise process them. Prev is the entity of the partial declaration,
304 -- if any.
305
306 procedure Check_Real_Bound (Bound : Node_Id);
307 -- Check given bound for being of real type and static. If not, post an
308 -- appropriate message, and rewrite the bound with the real literal zero.
309
310 procedure Constant_Redeclaration
311 (Id : Entity_Id;
312 N : Node_Id;
313 T : out Entity_Id);
314 -- Various checks on legality of full declaration of deferred constant.
315 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
316 -- node. The caller has not yet set any attributes of this entity.
317
318 function Contain_Interface
319 (Iface : Entity_Id;
320 Ifaces : Elist_Id) return Boolean;
321 -- Ada 2005: Determine whether Iface is present in the list Ifaces
322
323 procedure Convert_Scalar_Bounds
324 (N : Node_Id;
325 Parent_Type : Entity_Id;
326 Derived_Type : Entity_Id;
327 Loc : Source_Ptr);
328 -- For derived scalar types, convert the bounds in the type definition to
329 -- the derived type, and complete their analysis. Given a constraint of the
330 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
331 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
332 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
333 -- subtype are conversions of those bounds to the derived_type, so that
334 -- their typing is consistent.
335
336 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
337 -- Copies attributes from array base type T2 to array base type T1. Copies
338 -- only attributes that apply to base types, but not subtypes.
339
340 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
341 -- Copies attributes from array subtype T2 to array subtype T1. Copies
342 -- attributes that apply to both subtypes and base types.
343
344 procedure Create_Constrained_Components
345 (Subt : Entity_Id;
346 Decl_Node : Node_Id;
347 Typ : Entity_Id;
348 Constraints : Elist_Id);
349 -- Build the list of entities for a constrained discriminated record
350 -- subtype. If a component depends on a discriminant, replace its subtype
351 -- using the discriminant values in the discriminant constraint. Subt
352 -- is the defining identifier for the subtype whose list of constrained
353 -- entities we will create. Decl_Node is the type declaration node where
354 -- we will attach all the itypes created. Typ is the base discriminated
355 -- type for the subtype Subt. Constraints is the list of discriminant
356 -- constraints for Typ.
357
358 function Constrain_Component_Type
359 (Comp : Entity_Id;
360 Constrained_Typ : Entity_Id;
361 Related_Node : Node_Id;
362 Typ : Entity_Id;
363 Constraints : Elist_Id) return Entity_Id;
364 -- Given a discriminated base type Typ, a list of discriminant constraints,
365 -- Constraints, for Typ and a component Comp of Typ, create and return the
366 -- type corresponding to Etype (Comp) where all discriminant references
367 -- are replaced with the corresponding constraint. If Etype (Comp) contains
368 -- no discriminant references then it is returned as-is. Constrained_Typ
369 -- is the final constrained subtype to which the constrained component
370 -- belongs. Related_Node is the node where we attach all created itypes.
371
372 procedure Constrain_Access
373 (Def_Id : in out Entity_Id;
374 S : Node_Id;
375 Related_Nod : Node_Id);
376 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
377 -- an anonymous type created for a subtype indication. In that case it is
378 -- created in the procedure and attached to Related_Nod.
379
380 procedure Constrain_Array
381 (Def_Id : in out Entity_Id;
382 SI : Node_Id;
383 Related_Nod : Node_Id;
384 Related_Id : Entity_Id;
385 Suffix : Character);
386 -- Apply a list of index constraints to an unconstrained array type. The
387 -- first parameter is the entity for the resulting subtype. A value of
388 -- Empty for Def_Id indicates that an implicit type must be created, but
389 -- creation is delayed (and must be done by this procedure) because other
390 -- subsidiary implicit types must be created first (which is why Def_Id
391 -- is an in/out parameter). The second parameter is a subtype indication
392 -- node for the constrained array to be created (e.g. something of the
393 -- form string (1 .. 10)). Related_Nod gives the place where this type
394 -- has to be inserted in the tree. The Related_Id and Suffix parameters
395 -- are used to build the associated Implicit type name.
396
397 procedure Constrain_Concurrent
398 (Def_Id : in out Entity_Id;
399 SI : Node_Id;
400 Related_Nod : Node_Id;
401 Related_Id : Entity_Id;
402 Suffix : Character);
403 -- Apply list of discriminant constraints to an unconstrained concurrent
404 -- type.
405 --
406 -- SI is the N_Subtype_Indication node containing the constraint and
407 -- the unconstrained type to constrain.
408 --
409 -- Def_Id is the entity for the resulting constrained subtype. A value
410 -- of Empty for Def_Id indicates that an implicit type must be created,
411 -- but creation is delayed (and must be done by this procedure) because
412 -- other subsidiary implicit types must be created first (which is why
413 -- Def_Id is an in/out parameter).
414 --
415 -- Related_Nod gives the place where this type has to be inserted
416 -- in the tree.
417 --
418 -- The last two arguments are used to create its external name if needed.
419
420 function Constrain_Corresponding_Record
421 (Prot_Subt : Entity_Id;
422 Corr_Rec : Entity_Id;
423 Related_Nod : Node_Id) return Entity_Id;
424 -- When constraining a protected type or task type with discriminants,
425 -- constrain the corresponding record with the same discriminant values.
426
427 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
428 -- Constrain a decimal fixed point type with a digits constraint and/or a
429 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
430
431 procedure Constrain_Discriminated_Type
432 (Def_Id : Entity_Id;
433 S : Node_Id;
434 Related_Nod : Node_Id;
435 For_Access : Boolean := False);
436 -- Process discriminant constraints of composite type. Verify that values
437 -- have been provided for all discriminants, that the original type is
438 -- unconstrained, and that the types of the supplied expressions match
439 -- the discriminant types. The first three parameters are like in routine
440 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
441 -- of For_Access.
442
443 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
444 -- Constrain an enumeration type with a range constraint. This is identical
445 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
446
447 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
448 -- Constrain a floating point type with either a digits constraint
449 -- and/or a range constraint, building a E_Floating_Point_Subtype.
450
451 procedure Constrain_Index
452 (Index : Node_Id;
453 S : Node_Id;
454 Related_Nod : Node_Id;
455 Related_Id : Entity_Id;
456 Suffix : Character;
457 Suffix_Index : Nat);
458 -- Process an index constraint S in a constrained array declaration. The
459 -- constraint can be a subtype name, or a range with or without an explicit
460 -- subtype mark. The index is the corresponding index of the unconstrained
461 -- array. The Related_Id and Suffix parameters are used to build the
462 -- associated Implicit type name.
463
464 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
465 -- Build subtype of a signed or modular integer type
466
467 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
468 -- Constrain an ordinary fixed point type with a range constraint, and
469 -- build an E_Ordinary_Fixed_Point_Subtype entity.
470
471 procedure Copy_And_Swap (Priv, Full : Entity_Id);
472 -- Copy the Priv entity into the entity of its full declaration then swap
473 -- the two entities in such a manner that the former private type is now
474 -- seen as a full type.
475
476 procedure Decimal_Fixed_Point_Type_Declaration
477 (T : Entity_Id;
478 Def : Node_Id);
479 -- Create a new decimal fixed point type, and apply the constraint to
480 -- obtain a subtype of this new type.
481
482 procedure Complete_Private_Subtype
483 (Priv : Entity_Id;
484 Full : Entity_Id;
485 Full_Base : Entity_Id;
486 Related_Nod : Node_Id);
487 -- Complete the implicit full view of a private subtype by setting the
488 -- appropriate semantic fields. If the full view of the parent is a record
489 -- type, build constrained components of subtype.
490
491 procedure Derive_Progenitor_Subprograms
492 (Parent_Type : Entity_Id;
493 Tagged_Type : Entity_Id);
494 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
495 -- operations of progenitors of Tagged_Type, and replace the subsidiary
496 -- subtypes with Tagged_Type, to build the specs of the inherited interface
497 -- primitives. The derived primitives are aliased to those of the
498 -- interface. This routine takes care also of transferring to the full view
499 -- subprograms associated with the partial view of Tagged_Type that cover
500 -- interface primitives.
501
502 procedure Derived_Standard_Character
503 (N : Node_Id;
504 Parent_Type : Entity_Id;
505 Derived_Type : Entity_Id);
506 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
507 -- derivations from types Standard.Character and Standard.Wide_Character.
508
509 procedure Derived_Type_Declaration
510 (T : Entity_Id;
511 N : Node_Id;
512 Is_Completion : Boolean);
513 -- Process a derived type declaration. Build_Derived_Type is invoked
514 -- to process the actual derived type definition. Parameters N and
515 -- Is_Completion have the same meaning as in Build_Derived_Type.
516 -- T is the N_Defining_Identifier for the entity defined in the
517 -- N_Full_Type_Declaration node N, that is T is the derived type.
518
519 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
520 -- Insert each literal in symbol table, as an overloadable identifier. Each
521 -- enumeration type is mapped into a sequence of integers, and each literal
522 -- is defined as a constant with integer value. If any of the literals are
523 -- character literals, the type is a character type, which means that
524 -- strings are legal aggregates for arrays of components of the type.
525
526 function Expand_To_Stored_Constraint
527 (Typ : Entity_Id;
528 Constraint : Elist_Id) return Elist_Id;
529 -- Given a constraint (i.e. a list of expressions) on the discriminants of
530 -- Typ, expand it into a constraint on the stored discriminants and return
531 -- the new list of expressions constraining the stored discriminants.
532
533 function Find_Type_Of_Object
534 (Obj_Def : Node_Id;
535 Related_Nod : Node_Id) return Entity_Id;
536 -- Get type entity for object referenced by Obj_Def, attaching the implicit
537 -- types generated to Related_Nod.
538
539 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
540 -- Create a new float and apply the constraint to obtain subtype of it
541
542 function Has_Range_Constraint (N : Node_Id) return Boolean;
543 -- Given an N_Subtype_Indication node N, return True if a range constraint
544 -- is present, either directly, or as part of a digits or delta constraint.
545 -- In addition, a digits constraint in the decimal case returns True, since
546 -- it establishes a default range if no explicit range is present.
547
548 function Inherit_Components
549 (N : Node_Id;
550 Parent_Base : Entity_Id;
551 Derived_Base : Entity_Id;
552 Is_Tagged : Boolean;
553 Inherit_Discr : Boolean;
554 Discs : Elist_Id) return Elist_Id;
555 -- Called from Build_Derived_Record_Type to inherit the components of
556 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
557 -- For more information on derived types and component inheritance please
558 -- consult the comment above the body of Build_Derived_Record_Type.
559 --
560 -- N is the original derived type declaration
561 --
562 -- Is_Tagged is set if we are dealing with tagged types
563 --
564 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
565 -- Parent_Base, otherwise no discriminants are inherited.
566 --
567 -- Discs gives the list of constraints that apply to Parent_Base in the
568 -- derived type declaration. If Discs is set to No_Elist, then we have
569 -- the following situation:
570 --
571 -- type Parent (D1..Dn : ..) is [tagged] record ...;
572 -- type Derived is new Parent [with ...];
573 --
574 -- which gets treated as
575 --
576 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
577 --
578 -- For untagged types the returned value is an association list. The list
579 -- starts from the association (Parent_Base => Derived_Base), and then it
580 -- contains a sequence of the associations of the form
581 --
582 -- (Old_Component => New_Component),
583 --
584 -- where Old_Component is the Entity_Id of a component in Parent_Base and
585 -- New_Component is the Entity_Id of the corresponding component in
586 -- Derived_Base. For untagged records, this association list is needed when
587 -- copying the record declaration for the derived base. In the tagged case
588 -- the value returned is irrelevant.
589
590 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
591 -- Propagate static and dynamic predicate flags from a parent to the
592 -- subtype in a subtype declaration with and without constraints.
593
594 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
595 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
596 -- Determine whether subprogram Subp is a procedure subject to pragma
597 -- Extensions_Visible with value False and has at least one controlling
598 -- parameter of mode OUT.
599
600 function Is_Valid_Constraint_Kind
601 (T_Kind : Type_Kind;
602 Constraint_Kind : Node_Kind) return Boolean;
603 -- Returns True if it is legal to apply the given kind of constraint to the
604 -- given kind of type (index constraint to an array type, for example).
605
606 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
607 -- Create new modular type. Verify that modulus is in bounds
608
609 procedure New_Concatenation_Op (Typ : Entity_Id);
610 -- Create an abbreviated declaration for an operator in order to
611 -- materialize concatenation on array types.
612
613 procedure Ordinary_Fixed_Point_Type_Declaration
614 (T : Entity_Id;
615 Def : Node_Id);
616 -- Create a new ordinary fixed point type, and apply the constraint to
617 -- obtain subtype of it.
618
619 procedure Prepare_Private_Subtype_Completion
620 (Id : Entity_Id;
621 Related_Nod : Node_Id);
622 -- Id is a subtype of some private type. Creates the full declaration
623 -- associated with Id whenever possible, i.e. when the full declaration
624 -- of the base type is already known. Records each subtype into
625 -- Private_Dependents of the base type.
626
627 procedure Process_Incomplete_Dependents
628 (N : Node_Id;
629 Full_T : Entity_Id;
630 Inc_T : Entity_Id);
631 -- Process all entities that depend on an incomplete type. There include
632 -- subtypes, subprogram types that mention the incomplete type in their
633 -- profiles, and subprogram with access parameters that designate the
634 -- incomplete type.
635
636 -- Inc_T is the defining identifier of an incomplete type declaration, its
637 -- Ekind is E_Incomplete_Type.
638 --
639 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
640 --
641 -- Full_T is N's defining identifier.
642 --
643 -- Subtypes of incomplete types with discriminants are completed when the
644 -- parent type is. This is simpler than private subtypes, because they can
645 -- only appear in the same scope, and there is no need to exchange views.
646 -- Similarly, access_to_subprogram types may have a parameter or a return
647 -- type that is an incomplete type, and that must be replaced with the
648 -- full type.
649 --
650 -- If the full type is tagged, subprogram with access parameters that
651 -- designated the incomplete may be primitive operations of the full type,
652 -- and have to be processed accordingly.
653
654 procedure Process_Real_Range_Specification (Def : Node_Id);
655 -- Given the type definition for a real type, this procedure processes and
656 -- checks the real range specification of this type definition if one is
657 -- present. If errors are found, error messages are posted, and the
658 -- Real_Range_Specification of Def is reset to Empty.
659
660 procedure Propagate_Default_Init_Cond_Attributes
661 (From_Typ : Entity_Id;
662 To_Typ : Entity_Id;
663 Parent_To_Derivation : Boolean := False;
664 Private_To_Full_View : Boolean := False);
665 -- Subsidiary to routines Build_Derived_Type and Process_Full_View. Inherit
666 -- all attributes related to pragma Default_Initial_Condition from From_Typ
667 -- to To_Typ. Flag Parent_To_Derivation should be set when the context is
668 -- the creation of a derived type. Flag Private_To_Full_View should be set
669 -- when processing both views of a private type.
670
671 procedure Record_Type_Declaration
672 (T : Entity_Id;
673 N : Node_Id;
674 Prev : Entity_Id);
675 -- Process a record type declaration (for both untagged and tagged
676 -- records). Parameters T and N are exactly like in procedure
677 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
678 -- for this routine. If this is the completion of an incomplete type
679 -- declaration, Prev is the entity of the incomplete declaration, used for
680 -- cross-referencing. Otherwise Prev = T.
681
682 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
683 -- This routine is used to process the actual record type definition (both
684 -- for untagged and tagged records). Def is a record type definition node.
685 -- This procedure analyzes the components in this record type definition.
686 -- Prev_T is the entity for the enclosing record type. It is provided so
687 -- that its Has_Task flag can be set if any of the component have Has_Task
688 -- set. If the declaration is the completion of an incomplete type
689 -- declaration, Prev_T is the original incomplete type, whose full view is
690 -- the record type.
691
692 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
693 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
694 -- build a copy of the declaration tree of the parent, and we create
695 -- independently the list of components for the derived type. Semantic
696 -- information uses the component entities, but record representation
697 -- clauses are validated on the declaration tree. This procedure replaces
698 -- discriminants and components in the declaration with those that have
699 -- been created by Inherit_Components.
700
701 procedure Set_Fixed_Range
702 (E : Entity_Id;
703 Loc : Source_Ptr;
704 Lo : Ureal;
705 Hi : Ureal);
706 -- Build a range node with the given bounds and set it as the Scalar_Range
707 -- of the given fixed-point type entity. Loc is the source location used
708 -- for the constructed range. See body for further details.
709
710 procedure Set_Scalar_Range_For_Subtype
711 (Def_Id : Entity_Id;
712 R : Node_Id;
713 Subt : Entity_Id);
714 -- This routine is used to set the scalar range field for a subtype given
715 -- Def_Id, the entity for the subtype, and R, the range expression for the
716 -- scalar range. Subt provides the parent subtype to be used to analyze,
717 -- resolve, and check the given range.
718
719 procedure Set_Default_SSO (T : Entity_Id);
720 -- T is the entity for an array or record being declared. This procedure
721 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
722 -- to the setting of Opt.Default_SSO.
723
724 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
725 -- Create a new signed integer entity, and apply the constraint to obtain
726 -- the required first named subtype of this type.
727
728 procedure Set_Stored_Constraint_From_Discriminant_Constraint
729 (E : Entity_Id);
730 -- E is some record type. This routine computes E's Stored_Constraint
731 -- from its Discriminant_Constraint.
732
733 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
734 -- Check that an entity in a list of progenitors is an interface,
735 -- emit error otherwise.
736
737 -----------------------
738 -- Access_Definition --
739 -----------------------
740
741 function Access_Definition
742 (Related_Nod : Node_Id;
743 N : Node_Id) return Entity_Id
744 is
745 Anon_Type : Entity_Id;
746 Anon_Scope : Entity_Id;
747 Desig_Type : Entity_Id;
748 Enclosing_Prot_Type : Entity_Id := Empty;
749
750 begin
751 Check_SPARK_05_Restriction ("access type is not allowed", N);
752
753 if Is_Entry (Current_Scope)
754 and then Is_Task_Type (Etype (Scope (Current_Scope)))
755 then
756 Error_Msg_N ("task entries cannot have access parameters", N);
757 return Empty;
758 end if;
759
760 -- Ada 2005: For an object declaration the corresponding anonymous
761 -- type is declared in the current scope.
762
763 -- If the access definition is the return type of another access to
764 -- function, scope is the current one, because it is the one of the
765 -- current type declaration, except for the pathological case below.
766
767 if Nkind_In (Related_Nod, N_Object_Declaration,
768 N_Access_Function_Definition)
769 then
770 Anon_Scope := Current_Scope;
771
772 -- A pathological case: function returning access functions that
773 -- return access functions, etc. Each anonymous access type created
774 -- is in the enclosing scope of the outermost function.
775
776 declare
777 Par : Node_Id;
778
779 begin
780 Par := Related_Nod;
781 while Nkind_In (Par, N_Access_Function_Definition,
782 N_Access_Definition)
783 loop
784 Par := Parent (Par);
785 end loop;
786
787 if Nkind (Par) = N_Function_Specification then
788 Anon_Scope := Scope (Defining_Entity (Par));
789 end if;
790 end;
791
792 -- For the anonymous function result case, retrieve the scope of the
793 -- function specification's associated entity rather than using the
794 -- current scope. The current scope will be the function itself if the
795 -- formal part is currently being analyzed, but will be the parent scope
796 -- in the case of a parameterless function, and we always want to use
797 -- the function's parent scope. Finally, if the function is a child
798 -- unit, we must traverse the tree to retrieve the proper entity.
799
800 elsif Nkind (Related_Nod) = N_Function_Specification
801 and then Nkind (Parent (N)) /= N_Parameter_Specification
802 then
803 -- If the current scope is a protected type, the anonymous access
804 -- is associated with one of the protected operations, and must
805 -- be available in the scope that encloses the protected declaration.
806 -- Otherwise the type is in the scope enclosing the subprogram.
807
808 -- If the function has formals, The return type of a subprogram
809 -- declaration is analyzed in the scope of the subprogram (see
810 -- Process_Formals) and thus the protected type, if present, is
811 -- the scope of the current function scope.
812
813 if Ekind (Current_Scope) = E_Protected_Type then
814 Enclosing_Prot_Type := Current_Scope;
815
816 elsif Ekind (Current_Scope) = E_Function
817 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
818 then
819 Enclosing_Prot_Type := Scope (Current_Scope);
820 end if;
821
822 if Present (Enclosing_Prot_Type) then
823 Anon_Scope := Scope (Enclosing_Prot_Type);
824
825 else
826 Anon_Scope := Scope (Defining_Entity (Related_Nod));
827 end if;
828
829 -- For an access type definition, if the current scope is a child
830 -- unit it is the scope of the type.
831
832 elsif Is_Compilation_Unit (Current_Scope) then
833 Anon_Scope := Current_Scope;
834
835 -- For access formals, access components, and access discriminants, the
836 -- scope is that of the enclosing declaration,
837
838 else
839 Anon_Scope := Scope (Current_Scope);
840 end if;
841
842 Anon_Type :=
843 Create_Itype
844 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
845
846 if All_Present (N)
847 and then Ada_Version >= Ada_2005
848 then
849 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
850 end if;
851
852 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
853 -- the corresponding semantic routine
854
855 if Present (Access_To_Subprogram_Definition (N)) then
856
857 -- Compiler runtime units are compiled in Ada 2005 mode when building
858 -- the runtime library but must also be compilable in Ada 95 mode
859 -- (when bootstrapping the compiler).
860
861 Check_Compiler_Unit ("anonymous access to subprogram", N);
862
863 Access_Subprogram_Declaration
864 (T_Name => Anon_Type,
865 T_Def => Access_To_Subprogram_Definition (N));
866
867 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
868 Set_Ekind
869 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
870 else
871 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
872 end if;
873
874 Set_Can_Use_Internal_Rep
875 (Anon_Type, not Always_Compatible_Rep_On_Target);
876
877 -- If the anonymous access is associated with a protected operation,
878 -- create a reference to it after the enclosing protected definition
879 -- because the itype will be used in the subsequent bodies.
880
881 -- If the anonymous access itself is protected, a full type
882 -- declaratiton will be created for it, so that the equivalent
883 -- record type can be constructed. For further details, see
884 -- Replace_Anonymous_Access_To_Protected-Subprogram.
885
886 if Ekind (Current_Scope) = E_Protected_Type
887 and then not Protected_Present (Access_To_Subprogram_Definition (N))
888 then
889 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
890 end if;
891
892 return Anon_Type;
893 end if;
894
895 Find_Type (Subtype_Mark (N));
896 Desig_Type := Entity (Subtype_Mark (N));
897
898 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
899 Set_Etype (Anon_Type, Anon_Type);
900
901 -- Make sure the anonymous access type has size and alignment fields
902 -- set, as required by gigi. This is necessary in the case of the
903 -- Task_Body_Procedure.
904
905 if not Has_Private_Component (Desig_Type) then
906 Layout_Type (Anon_Type);
907 end if;
908
909 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
910 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
911 -- the null value is allowed. In Ada 95 the null value is never allowed.
912
913 if Ada_Version >= Ada_2005 then
914 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
915 else
916 Set_Can_Never_Be_Null (Anon_Type, True);
917 end if;
918
919 -- The anonymous access type is as public as the discriminated type or
920 -- subprogram that defines it. It is imported (for back-end purposes)
921 -- if the designated type is.
922
923 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
924
925 -- Ada 2005 (AI-231): Propagate the access-constant attribute
926
927 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
928
929 -- The context is either a subprogram declaration, object declaration,
930 -- or an access discriminant, in a private or a full type declaration.
931 -- In the case of a subprogram, if the designated type is incomplete,
932 -- the operation will be a primitive operation of the full type, to be
933 -- updated subsequently. If the type is imported through a limited_with
934 -- clause, the subprogram is not a primitive operation of the type
935 -- (which is declared elsewhere in some other scope).
936
937 if Ekind (Desig_Type) = E_Incomplete_Type
938 and then not From_Limited_With (Desig_Type)
939 and then Is_Overloadable (Current_Scope)
940 then
941 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
942 Set_Has_Delayed_Freeze (Current_Scope);
943 end if;
944
945 -- Ada 2005: If the designated type is an interface that may contain
946 -- tasks, create a Master entity for the declaration. This must be done
947 -- before expansion of the full declaration, because the declaration may
948 -- include an expression that is an allocator, whose expansion needs the
949 -- proper Master for the created tasks.
950
951 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
952 then
953 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
954 then
955 Build_Class_Wide_Master (Anon_Type);
956
957 -- Similarly, if the type is an anonymous access that designates
958 -- tasks, create a master entity for it in the current context.
959
960 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
961 then
962 Build_Master_Entity (Defining_Identifier (Related_Nod));
963 Build_Master_Renaming (Anon_Type);
964 end if;
965 end if;
966
967 -- For a private component of a protected type, it is imperative that
968 -- the back-end elaborate the type immediately after the protected
969 -- declaration, because this type will be used in the declarations
970 -- created for the component within each protected body, so we must
971 -- create an itype reference for it now.
972
973 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
974 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
975
976 -- Similarly, if the access definition is the return result of a
977 -- function, create an itype reference for it because it will be used
978 -- within the function body. For a regular function that is not a
979 -- compilation unit, insert reference after the declaration. For a
980 -- protected operation, insert it after the enclosing protected type
981 -- declaration. In either case, do not create a reference for a type
982 -- obtained through a limited_with clause, because this would introduce
983 -- semantic dependencies.
984
985 -- Similarly, do not create a reference if the designated type is a
986 -- generic formal, because no use of it will reach the backend.
987
988 elsif Nkind (Related_Nod) = N_Function_Specification
989 and then not From_Limited_With (Desig_Type)
990 and then not Is_Generic_Type (Desig_Type)
991 then
992 if Present (Enclosing_Prot_Type) then
993 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
994
995 elsif Is_List_Member (Parent (Related_Nod))
996 and then Nkind (Parent (N)) /= N_Parameter_Specification
997 then
998 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
999 end if;
1000
1001 -- Finally, create an itype reference for an object declaration of an
1002 -- anonymous access type. This is strictly necessary only for deferred
1003 -- constants, but in any case will avoid out-of-scope problems in the
1004 -- back-end.
1005
1006 elsif Nkind (Related_Nod) = N_Object_Declaration then
1007 Build_Itype_Reference (Anon_Type, Related_Nod);
1008 end if;
1009
1010 return Anon_Type;
1011 end Access_Definition;
1012
1013 -----------------------------------
1014 -- Access_Subprogram_Declaration --
1015 -----------------------------------
1016
1017 procedure Access_Subprogram_Declaration
1018 (T_Name : Entity_Id;
1019 T_Def : Node_Id)
1020 is
1021 procedure Check_For_Premature_Usage (Def : Node_Id);
1022 -- Check that type T_Name is not used, directly or recursively, as a
1023 -- parameter or a return type in Def. Def is either a subtype, an
1024 -- access_definition, or an access_to_subprogram_definition.
1025
1026 -------------------------------
1027 -- Check_For_Premature_Usage --
1028 -------------------------------
1029
1030 procedure Check_For_Premature_Usage (Def : Node_Id) is
1031 Param : Node_Id;
1032
1033 begin
1034 -- Check for a subtype mark
1035
1036 if Nkind (Def) in N_Has_Etype then
1037 if Etype (Def) = T_Name then
1038 Error_Msg_N
1039 ("type& cannot be used before end of its declaration", Def);
1040 end if;
1041
1042 -- If this is not a subtype, then this is an access_definition
1043
1044 elsif Nkind (Def) = N_Access_Definition then
1045 if Present (Access_To_Subprogram_Definition (Def)) then
1046 Check_For_Premature_Usage
1047 (Access_To_Subprogram_Definition (Def));
1048 else
1049 Check_For_Premature_Usage (Subtype_Mark (Def));
1050 end if;
1051
1052 -- The only cases left are N_Access_Function_Definition and
1053 -- N_Access_Procedure_Definition.
1054
1055 else
1056 if Present (Parameter_Specifications (Def)) then
1057 Param := First (Parameter_Specifications (Def));
1058 while Present (Param) loop
1059 Check_For_Premature_Usage (Parameter_Type (Param));
1060 Param := Next (Param);
1061 end loop;
1062 end if;
1063
1064 if Nkind (Def) = N_Access_Function_Definition then
1065 Check_For_Premature_Usage (Result_Definition (Def));
1066 end if;
1067 end if;
1068 end Check_For_Premature_Usage;
1069
1070 -- Local variables
1071
1072 Formals : constant List_Id := Parameter_Specifications (T_Def);
1073 Formal : Entity_Id;
1074 D_Ityp : Node_Id;
1075 Desig_Type : constant Entity_Id :=
1076 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1077
1078 -- Start of processing for Access_Subprogram_Declaration
1079
1080 begin
1081 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1082
1083 -- Associate the Itype node with the inner full-type declaration or
1084 -- subprogram spec or entry body. This is required to handle nested
1085 -- anonymous declarations. For example:
1086
1087 -- procedure P
1088 -- (X : access procedure
1089 -- (Y : access procedure
1090 -- (Z : access T)))
1091
1092 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1093 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1094 N_Private_Type_Declaration,
1095 N_Private_Extension_Declaration,
1096 N_Procedure_Specification,
1097 N_Function_Specification,
1098 N_Entry_Body)
1099
1100 or else
1101 Nkind_In (D_Ityp, N_Object_Declaration,
1102 N_Object_Renaming_Declaration,
1103 N_Formal_Object_Declaration,
1104 N_Formal_Type_Declaration,
1105 N_Task_Type_Declaration,
1106 N_Protected_Type_Declaration))
1107 loop
1108 D_Ityp := Parent (D_Ityp);
1109 pragma Assert (D_Ityp /= Empty);
1110 end loop;
1111
1112 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1113
1114 if Nkind_In (D_Ityp, N_Procedure_Specification,
1115 N_Function_Specification)
1116 then
1117 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1118
1119 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1120 N_Object_Declaration,
1121 N_Object_Renaming_Declaration,
1122 N_Formal_Type_Declaration)
1123 then
1124 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1125 end if;
1126
1127 if Nkind (T_Def) = N_Access_Function_Definition then
1128 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1129 declare
1130 Acc : constant Node_Id := Result_Definition (T_Def);
1131
1132 begin
1133 if Present (Access_To_Subprogram_Definition (Acc))
1134 and then
1135 Protected_Present (Access_To_Subprogram_Definition (Acc))
1136 then
1137 Set_Etype
1138 (Desig_Type,
1139 Replace_Anonymous_Access_To_Protected_Subprogram
1140 (T_Def));
1141
1142 else
1143 Set_Etype
1144 (Desig_Type,
1145 Access_Definition (T_Def, Result_Definition (T_Def)));
1146 end if;
1147 end;
1148
1149 else
1150 Analyze (Result_Definition (T_Def));
1151
1152 declare
1153 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1154
1155 begin
1156 -- If a null exclusion is imposed on the result type, then
1157 -- create a null-excluding itype (an access subtype) and use
1158 -- it as the function's Etype.
1159
1160 if Is_Access_Type (Typ)
1161 and then Null_Exclusion_In_Return_Present (T_Def)
1162 then
1163 Set_Etype (Desig_Type,
1164 Create_Null_Excluding_Itype
1165 (T => Typ,
1166 Related_Nod => T_Def,
1167 Scope_Id => Current_Scope));
1168
1169 else
1170 if From_Limited_With (Typ) then
1171
1172 -- AI05-151: Incomplete types are allowed in all basic
1173 -- declarations, including access to subprograms.
1174
1175 if Ada_Version >= Ada_2012 then
1176 null;
1177
1178 else
1179 Error_Msg_NE
1180 ("illegal use of incomplete type&",
1181 Result_Definition (T_Def), Typ);
1182 end if;
1183
1184 elsif Ekind (Current_Scope) = E_Package
1185 and then In_Private_Part (Current_Scope)
1186 then
1187 if Ekind (Typ) = E_Incomplete_Type then
1188 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1189
1190 elsif Is_Class_Wide_Type (Typ)
1191 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1192 then
1193 Append_Elmt
1194 (Desig_Type, Private_Dependents (Etype (Typ)));
1195 end if;
1196 end if;
1197
1198 Set_Etype (Desig_Type, Typ);
1199 end if;
1200 end;
1201 end if;
1202
1203 if not (Is_Type (Etype (Desig_Type))) then
1204 Error_Msg_N
1205 ("expect type in function specification",
1206 Result_Definition (T_Def));
1207 end if;
1208
1209 else
1210 Set_Etype (Desig_Type, Standard_Void_Type);
1211 end if;
1212
1213 if Present (Formals) then
1214 Push_Scope (Desig_Type);
1215
1216 -- Some special tests here. These special tests can be removed
1217 -- if and when Itypes always have proper parent pointers to their
1218 -- declarations???
1219
1220 -- Special test 1) Link defining_identifier of formals. Required by
1221 -- First_Formal to provide its functionality.
1222
1223 declare
1224 F : Node_Id;
1225
1226 begin
1227 F := First (Formals);
1228
1229 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1230 -- when it is part of an unconstrained type and subtype expansion
1231 -- is disabled. To avoid back-end problems with shared profiles,
1232 -- use previous subprogram type as the designated type, and then
1233 -- remove scope added above.
1234
1235 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1236 then
1237 Set_Etype (T_Name, T_Name);
1238 Init_Size_Align (T_Name);
1239 Set_Directly_Designated_Type (T_Name,
1240 Scope (Defining_Identifier (F)));
1241 End_Scope;
1242 return;
1243 end if;
1244
1245 while Present (F) loop
1246 if No (Parent (Defining_Identifier (F))) then
1247 Set_Parent (Defining_Identifier (F), F);
1248 end if;
1249
1250 Next (F);
1251 end loop;
1252 end;
1253
1254 Process_Formals (Formals, Parent (T_Def));
1255
1256 -- Special test 2) End_Scope requires that the parent pointer be set
1257 -- to something reasonable, but Itypes don't have parent pointers. So
1258 -- we set it and then unset it ???
1259
1260 Set_Parent (Desig_Type, T_Name);
1261 End_Scope;
1262 Set_Parent (Desig_Type, Empty);
1263 end if;
1264
1265 -- Check for premature usage of the type being defined
1266
1267 Check_For_Premature_Usage (T_Def);
1268
1269 -- The return type and/or any parameter type may be incomplete. Mark the
1270 -- subprogram_type as depending on the incomplete type, so that it can
1271 -- be updated when the full type declaration is seen. This only applies
1272 -- to incomplete types declared in some enclosing scope, not to limited
1273 -- views from other packages.
1274
1275 -- Prior to Ada 2012, access to functions can only have in_parameters.
1276
1277 if Present (Formals) then
1278 Formal := First_Formal (Desig_Type);
1279 while Present (Formal) loop
1280 if Ekind (Formal) /= E_In_Parameter
1281 and then Nkind (T_Def) = N_Access_Function_Definition
1282 and then Ada_Version < Ada_2012
1283 then
1284 Error_Msg_N ("functions can only have IN parameters", Formal);
1285 end if;
1286
1287 if Ekind (Etype (Formal)) = E_Incomplete_Type
1288 and then In_Open_Scopes (Scope (Etype (Formal)))
1289 then
1290 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1291 Set_Has_Delayed_Freeze (Desig_Type);
1292 end if;
1293
1294 Next_Formal (Formal);
1295 end loop;
1296 end if;
1297
1298 -- Check whether an indirect call without actuals may be possible. This
1299 -- is used when resolving calls whose result is then indexed.
1300
1301 May_Need_Actuals (Desig_Type);
1302
1303 -- If the return type is incomplete, this is legal as long as the type
1304 -- is declared in the current scope and will be completed in it (rather
1305 -- than being part of limited view).
1306
1307 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1308 and then not Has_Delayed_Freeze (Desig_Type)
1309 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1310 then
1311 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1312 Set_Has_Delayed_Freeze (Desig_Type);
1313 end if;
1314
1315 Check_Delayed_Subprogram (Desig_Type);
1316
1317 if Protected_Present (T_Def) then
1318 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1319 Set_Convention (Desig_Type, Convention_Protected);
1320 else
1321 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1322 end if;
1323
1324 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1325
1326 Set_Etype (T_Name, T_Name);
1327 Init_Size_Align (T_Name);
1328 Set_Directly_Designated_Type (T_Name, Desig_Type);
1329
1330 Generate_Reference_To_Formals (T_Name);
1331
1332 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1333
1334 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1335
1336 Check_Restriction (No_Access_Subprograms, T_Def);
1337 end Access_Subprogram_Declaration;
1338
1339 ----------------------------
1340 -- Access_Type_Declaration --
1341 ----------------------------
1342
1343 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1344 P : constant Node_Id := Parent (Def);
1345 S : constant Node_Id := Subtype_Indication (Def);
1346
1347 Full_Desig : Entity_Id;
1348
1349 begin
1350 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1351
1352 -- Check for permissible use of incomplete type
1353
1354 if Nkind (S) /= N_Subtype_Indication then
1355 Analyze (S);
1356
1357 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1358 Set_Directly_Designated_Type (T, Entity (S));
1359
1360 -- If the designated type is a limited view, we cannot tell if
1361 -- the full view contains tasks, and there is no way to handle
1362 -- that full view in a client. We create a master entity for the
1363 -- scope, which will be used when a client determines that one
1364 -- is needed.
1365
1366 if From_Limited_With (Entity (S))
1367 and then not Is_Class_Wide_Type (Entity (S))
1368 then
1369 Set_Ekind (T, E_Access_Type);
1370 Build_Master_Entity (T);
1371 Build_Master_Renaming (T);
1372 end if;
1373
1374 else
1375 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1376 end if;
1377
1378 -- If the access definition is of the form: ACCESS NOT NULL ..
1379 -- the subtype indication must be of an access type. Create
1380 -- a null-excluding subtype of it.
1381
1382 if Null_Excluding_Subtype (Def) then
1383 if not Is_Access_Type (Entity (S)) then
1384 Error_Msg_N ("null exclusion must apply to access type", Def);
1385
1386 else
1387 declare
1388 Loc : constant Source_Ptr := Sloc (S);
1389 Decl : Node_Id;
1390 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1391
1392 begin
1393 Decl :=
1394 Make_Subtype_Declaration (Loc,
1395 Defining_Identifier => Nam,
1396 Subtype_Indication =>
1397 New_Occurrence_Of (Entity (S), Loc));
1398 Set_Null_Exclusion_Present (Decl);
1399 Insert_Before (Parent (Def), Decl);
1400 Analyze (Decl);
1401 Set_Entity (S, Nam);
1402 end;
1403 end if;
1404 end if;
1405
1406 else
1407 Set_Directly_Designated_Type (T,
1408 Process_Subtype (S, P, T, 'P'));
1409 end if;
1410
1411 if All_Present (Def) or Constant_Present (Def) then
1412 Set_Ekind (T, E_General_Access_Type);
1413 else
1414 Set_Ekind (T, E_Access_Type);
1415 end if;
1416
1417 Full_Desig := Designated_Type (T);
1418
1419 if Base_Type (Full_Desig) = T then
1420 Error_Msg_N ("access type cannot designate itself", S);
1421
1422 -- In Ada 2005, the type may have a limited view through some unit in
1423 -- its own context, allowing the following circularity that cannot be
1424 -- detected earlier.
1425
1426 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1427 then
1428 Error_Msg_N
1429 ("access type cannot designate its own classwide type", S);
1430
1431 -- Clean up indication of tagged status to prevent cascaded errors
1432
1433 Set_Is_Tagged_Type (T, False);
1434 end if;
1435
1436 Set_Etype (T, T);
1437
1438 -- If the type has appeared already in a with_type clause, it is frozen
1439 -- and the pointer size is already set. Else, initialize.
1440
1441 if not From_Limited_With (T) then
1442 Init_Size_Align (T);
1443 end if;
1444
1445 -- Note that Has_Task is always false, since the access type itself
1446 -- is not a task type. See Einfo for more description on this point.
1447 -- Exactly the same consideration applies to Has_Controlled_Component
1448 -- and to Has_Protected.
1449
1450 Set_Has_Task (T, False);
1451 Set_Has_Controlled_Component (T, False);
1452 Set_Has_Protected (T, False);
1453
1454 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1455 -- problems where an incomplete view of this entity has been previously
1456 -- established by a limited with and an overlaid version of this field
1457 -- (Stored_Constraint) was initialized for the incomplete view.
1458
1459 -- This reset is performed in most cases except where the access type
1460 -- has been created for the purposes of allocating or deallocating a
1461 -- build-in-place object. Such access types have explicitly set pools
1462 -- and finalization masters.
1463
1464 if No (Associated_Storage_Pool (T)) then
1465 Set_Finalization_Master (T, Empty);
1466 end if;
1467
1468 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1469 -- attributes
1470
1471 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1472 Set_Is_Access_Constant (T, Constant_Present (Def));
1473 end Access_Type_Declaration;
1474
1475 ----------------------------------
1476 -- Add_Interface_Tag_Components --
1477 ----------------------------------
1478
1479 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1480 Loc : constant Source_Ptr := Sloc (N);
1481 L : List_Id;
1482 Last_Tag : Node_Id;
1483
1484 procedure Add_Tag (Iface : Entity_Id);
1485 -- Add tag for one of the progenitor interfaces
1486
1487 -------------
1488 -- Add_Tag --
1489 -------------
1490
1491 procedure Add_Tag (Iface : Entity_Id) is
1492 Decl : Node_Id;
1493 Def : Node_Id;
1494 Tag : Entity_Id;
1495 Offset : Entity_Id;
1496
1497 begin
1498 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1499
1500 -- This is a reasonable place to propagate predicates
1501
1502 if Has_Predicates (Iface) then
1503 Set_Has_Predicates (Typ);
1504 end if;
1505
1506 Def :=
1507 Make_Component_Definition (Loc,
1508 Aliased_Present => True,
1509 Subtype_Indication =>
1510 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1511
1512 Tag := Make_Temporary (Loc, 'V');
1513
1514 Decl :=
1515 Make_Component_Declaration (Loc,
1516 Defining_Identifier => Tag,
1517 Component_Definition => Def);
1518
1519 Analyze_Component_Declaration (Decl);
1520
1521 Set_Analyzed (Decl);
1522 Set_Ekind (Tag, E_Component);
1523 Set_Is_Tag (Tag);
1524 Set_Is_Aliased (Tag);
1525 Set_Related_Type (Tag, Iface);
1526 Init_Component_Location (Tag);
1527
1528 pragma Assert (Is_Frozen (Iface));
1529
1530 Set_DT_Entry_Count (Tag,
1531 DT_Entry_Count (First_Entity (Iface)));
1532
1533 if No (Last_Tag) then
1534 Prepend (Decl, L);
1535 else
1536 Insert_After (Last_Tag, Decl);
1537 end if;
1538
1539 Last_Tag := Decl;
1540
1541 -- If the ancestor has discriminants we need to give special support
1542 -- to store the offset_to_top value of the secondary dispatch tables.
1543 -- For this purpose we add a supplementary component just after the
1544 -- field that contains the tag associated with each secondary DT.
1545
1546 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1547 Def :=
1548 Make_Component_Definition (Loc,
1549 Subtype_Indication =>
1550 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1551
1552 Offset := Make_Temporary (Loc, 'V');
1553
1554 Decl :=
1555 Make_Component_Declaration (Loc,
1556 Defining_Identifier => Offset,
1557 Component_Definition => Def);
1558
1559 Analyze_Component_Declaration (Decl);
1560
1561 Set_Analyzed (Decl);
1562 Set_Ekind (Offset, E_Component);
1563 Set_Is_Aliased (Offset);
1564 Set_Related_Type (Offset, Iface);
1565 Init_Component_Location (Offset);
1566 Insert_After (Last_Tag, Decl);
1567 Last_Tag := Decl;
1568 end if;
1569 end Add_Tag;
1570
1571 -- Local variables
1572
1573 Elmt : Elmt_Id;
1574 Ext : Node_Id;
1575 Comp : Node_Id;
1576
1577 -- Start of processing for Add_Interface_Tag_Components
1578
1579 begin
1580 if not RTE_Available (RE_Interface_Tag) then
1581 Error_Msg
1582 ("(Ada 2005) interface types not supported by this run-time!",
1583 Sloc (N));
1584 return;
1585 end if;
1586
1587 if Ekind (Typ) /= E_Record_Type
1588 or else (Is_Concurrent_Record_Type (Typ)
1589 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1590 or else (not Is_Concurrent_Record_Type (Typ)
1591 and then No (Interfaces (Typ))
1592 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1593 then
1594 return;
1595 end if;
1596
1597 -- Find the current last tag
1598
1599 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1600 Ext := Record_Extension_Part (Type_Definition (N));
1601 else
1602 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1603 Ext := Type_Definition (N);
1604 end if;
1605
1606 Last_Tag := Empty;
1607
1608 if not (Present (Component_List (Ext))) then
1609 Set_Null_Present (Ext, False);
1610 L := New_List;
1611 Set_Component_List (Ext,
1612 Make_Component_List (Loc,
1613 Component_Items => L,
1614 Null_Present => False));
1615 else
1616 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1617 L := Component_Items
1618 (Component_List
1619 (Record_Extension_Part
1620 (Type_Definition (N))));
1621 else
1622 L := Component_Items
1623 (Component_List
1624 (Type_Definition (N)));
1625 end if;
1626
1627 -- Find the last tag component
1628
1629 Comp := First (L);
1630 while Present (Comp) loop
1631 if Nkind (Comp) = N_Component_Declaration
1632 and then Is_Tag (Defining_Identifier (Comp))
1633 then
1634 Last_Tag := Comp;
1635 end if;
1636
1637 Next (Comp);
1638 end loop;
1639 end if;
1640
1641 -- At this point L references the list of components and Last_Tag
1642 -- references the current last tag (if any). Now we add the tag
1643 -- corresponding with all the interfaces that are not implemented
1644 -- by the parent.
1645
1646 if Present (Interfaces (Typ)) then
1647 Elmt := First_Elmt (Interfaces (Typ));
1648 while Present (Elmt) loop
1649 Add_Tag (Node (Elmt));
1650 Next_Elmt (Elmt);
1651 end loop;
1652 end if;
1653 end Add_Interface_Tag_Components;
1654
1655 -------------------------------------
1656 -- Add_Internal_Interface_Entities --
1657 -------------------------------------
1658
1659 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1660 Elmt : Elmt_Id;
1661 Iface : Entity_Id;
1662 Iface_Elmt : Elmt_Id;
1663 Iface_Prim : Entity_Id;
1664 Ifaces_List : Elist_Id;
1665 New_Subp : Entity_Id := Empty;
1666 Prim : Entity_Id;
1667 Restore_Scope : Boolean := False;
1668
1669 begin
1670 pragma Assert (Ada_Version >= Ada_2005
1671 and then Is_Record_Type (Tagged_Type)
1672 and then Is_Tagged_Type (Tagged_Type)
1673 and then Has_Interfaces (Tagged_Type)
1674 and then not Is_Interface (Tagged_Type));
1675
1676 -- Ensure that the internal entities are added to the scope of the type
1677
1678 if Scope (Tagged_Type) /= Current_Scope then
1679 Push_Scope (Scope (Tagged_Type));
1680 Restore_Scope := True;
1681 end if;
1682
1683 Collect_Interfaces (Tagged_Type, Ifaces_List);
1684
1685 Iface_Elmt := First_Elmt (Ifaces_List);
1686 while Present (Iface_Elmt) loop
1687 Iface := Node (Iface_Elmt);
1688
1689 -- Originally we excluded here from this processing interfaces that
1690 -- are parents of Tagged_Type because their primitives are located
1691 -- in the primary dispatch table (and hence no auxiliary internal
1692 -- entities are required to handle secondary dispatch tables in such
1693 -- case). However, these auxiliary entities are also required to
1694 -- handle derivations of interfaces in formals of generics (see
1695 -- Derive_Subprograms).
1696
1697 Elmt := First_Elmt (Primitive_Operations (Iface));
1698 while Present (Elmt) loop
1699 Iface_Prim := Node (Elmt);
1700
1701 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1702 Prim :=
1703 Find_Primitive_Covering_Interface
1704 (Tagged_Type => Tagged_Type,
1705 Iface_Prim => Iface_Prim);
1706
1707 if No (Prim) and then Serious_Errors_Detected > 0 then
1708 goto Continue;
1709 end if;
1710
1711 pragma Assert (Present (Prim));
1712
1713 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1714 -- differs from the name of the interface primitive then it is
1715 -- a private primitive inherited from a parent type. In such
1716 -- case, given that Tagged_Type covers the interface, the
1717 -- inherited private primitive becomes visible. For such
1718 -- purpose we add a new entity that renames the inherited
1719 -- private primitive.
1720
1721 if Chars (Prim) /= Chars (Iface_Prim) then
1722 pragma Assert (Has_Suffix (Prim, 'P'));
1723 Derive_Subprogram
1724 (New_Subp => New_Subp,
1725 Parent_Subp => Iface_Prim,
1726 Derived_Type => Tagged_Type,
1727 Parent_Type => Iface);
1728 Set_Alias (New_Subp, Prim);
1729 Set_Is_Abstract_Subprogram
1730 (New_Subp, Is_Abstract_Subprogram (Prim));
1731 end if;
1732
1733 Derive_Subprogram
1734 (New_Subp => New_Subp,
1735 Parent_Subp => Iface_Prim,
1736 Derived_Type => Tagged_Type,
1737 Parent_Type => Iface);
1738
1739 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1740 -- associated with interface types. These entities are
1741 -- only registered in the list of primitives of its
1742 -- corresponding tagged type because they are only used
1743 -- to fill the contents of the secondary dispatch tables.
1744 -- Therefore they are removed from the homonym chains.
1745
1746 Set_Is_Hidden (New_Subp);
1747 Set_Is_Internal (New_Subp);
1748 Set_Alias (New_Subp, Prim);
1749 Set_Is_Abstract_Subprogram
1750 (New_Subp, Is_Abstract_Subprogram (Prim));
1751 Set_Interface_Alias (New_Subp, Iface_Prim);
1752
1753 -- If the returned type is an interface then propagate it to
1754 -- the returned type. Needed by the thunk to generate the code
1755 -- which displaces "this" to reference the corresponding
1756 -- secondary dispatch table in the returned object.
1757
1758 if Is_Interface (Etype (Iface_Prim)) then
1759 Set_Etype (New_Subp, Etype (Iface_Prim));
1760 end if;
1761
1762 -- Internal entities associated with interface types are
1763 -- only registered in the list of primitives of the tagged
1764 -- type. They are only used to fill the contents of the
1765 -- secondary dispatch tables. Therefore they are not needed
1766 -- in the homonym chains.
1767
1768 Remove_Homonym (New_Subp);
1769
1770 -- Hidden entities associated with interfaces must have set
1771 -- the Has_Delay_Freeze attribute to ensure that, in case of
1772 -- locally defined tagged types (or compiling with static
1773 -- dispatch tables generation disabled) the corresponding
1774 -- entry of the secondary dispatch table is filled when
1775 -- such an entity is frozen.
1776
1777 Set_Has_Delayed_Freeze (New_Subp);
1778 end if;
1779
1780 <<Continue>>
1781 Next_Elmt (Elmt);
1782 end loop;
1783
1784 Next_Elmt (Iface_Elmt);
1785 end loop;
1786
1787 if Restore_Scope then
1788 Pop_Scope;
1789 end if;
1790 end Add_Internal_Interface_Entities;
1791
1792 -----------------------------------
1793 -- Analyze_Component_Declaration --
1794 -----------------------------------
1795
1796 procedure Analyze_Component_Declaration (N : Node_Id) is
1797 Id : constant Entity_Id := Defining_Identifier (N);
1798 E : constant Node_Id := Expression (N);
1799 Typ : constant Node_Id :=
1800 Subtype_Indication (Component_Definition (N));
1801 T : Entity_Id;
1802 P : Entity_Id;
1803
1804 function Contains_POC (Constr : Node_Id) return Boolean;
1805 -- Determines whether a constraint uses the discriminant of a record
1806 -- type thus becoming a per-object constraint (POC).
1807
1808 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1809 -- Typ is the type of the current component, check whether this type is
1810 -- a limited type. Used to validate declaration against that of
1811 -- enclosing record.
1812
1813 ------------------
1814 -- Contains_POC --
1815 ------------------
1816
1817 function Contains_POC (Constr : Node_Id) return Boolean is
1818 begin
1819 -- Prevent cascaded errors
1820
1821 if Error_Posted (Constr) then
1822 return False;
1823 end if;
1824
1825 case Nkind (Constr) is
1826 when N_Attribute_Reference =>
1827 return Attribute_Name (Constr) = Name_Access
1828 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1829
1830 when N_Discriminant_Association =>
1831 return Denotes_Discriminant (Expression (Constr));
1832
1833 when N_Identifier =>
1834 return Denotes_Discriminant (Constr);
1835
1836 when N_Index_Or_Discriminant_Constraint =>
1837 declare
1838 IDC : Node_Id;
1839
1840 begin
1841 IDC := First (Constraints (Constr));
1842 while Present (IDC) loop
1843
1844 -- One per-object constraint is sufficient
1845
1846 if Contains_POC (IDC) then
1847 return True;
1848 end if;
1849
1850 Next (IDC);
1851 end loop;
1852
1853 return False;
1854 end;
1855
1856 when N_Range =>
1857 return Denotes_Discriminant (Low_Bound (Constr))
1858 or else
1859 Denotes_Discriminant (High_Bound (Constr));
1860
1861 when N_Range_Constraint =>
1862 return Denotes_Discriminant (Range_Expression (Constr));
1863
1864 when others =>
1865 return False;
1866
1867 end case;
1868 end Contains_POC;
1869
1870 ----------------------
1871 -- Is_Known_Limited --
1872 ----------------------
1873
1874 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1875 P : constant Entity_Id := Etype (Typ);
1876 R : constant Entity_Id := Root_Type (Typ);
1877
1878 begin
1879 if Is_Limited_Record (Typ) then
1880 return True;
1881
1882 -- If the root type is limited (and not a limited interface)
1883 -- so is the current type
1884
1885 elsif Is_Limited_Record (R)
1886 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1887 then
1888 return True;
1889
1890 -- Else the type may have a limited interface progenitor, but a
1891 -- limited record parent.
1892
1893 elsif R /= P and then Is_Limited_Record (P) then
1894 return True;
1895
1896 else
1897 return False;
1898 end if;
1899 end Is_Known_Limited;
1900
1901 -- Start of processing for Analyze_Component_Declaration
1902
1903 begin
1904 Generate_Definition (Id);
1905 Enter_Name (Id);
1906
1907 if Present (Typ) then
1908 T := Find_Type_Of_Object
1909 (Subtype_Indication (Component_Definition (N)), N);
1910
1911 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1912 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1913 end if;
1914
1915 -- Ada 2005 (AI-230): Access Definition case
1916
1917 else
1918 pragma Assert (Present
1919 (Access_Definition (Component_Definition (N))));
1920
1921 T := Access_Definition
1922 (Related_Nod => N,
1923 N => Access_Definition (Component_Definition (N)));
1924 Set_Is_Local_Anonymous_Access (T);
1925
1926 -- Ada 2005 (AI-254)
1927
1928 if Present (Access_To_Subprogram_Definition
1929 (Access_Definition (Component_Definition (N))))
1930 and then Protected_Present (Access_To_Subprogram_Definition
1931 (Access_Definition
1932 (Component_Definition (N))))
1933 then
1934 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1935 end if;
1936 end if;
1937
1938 -- If the subtype is a constrained subtype of the enclosing record,
1939 -- (which must have a partial view) the back-end does not properly
1940 -- handle the recursion. Rewrite the component declaration with an
1941 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1942 -- the tree directly because side effects have already been removed from
1943 -- discriminant constraints.
1944
1945 if Ekind (T) = E_Access_Subtype
1946 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1947 and then Comes_From_Source (T)
1948 and then Nkind (Parent (T)) = N_Subtype_Declaration
1949 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1950 then
1951 Rewrite
1952 (Subtype_Indication (Component_Definition (N)),
1953 New_Copy_Tree (Subtype_Indication (Parent (T))));
1954 T := Find_Type_Of_Object
1955 (Subtype_Indication (Component_Definition (N)), N);
1956 end if;
1957
1958 -- If the component declaration includes a default expression, then we
1959 -- check that the component is not of a limited type (RM 3.7(5)),
1960 -- and do the special preanalysis of the expression (see section on
1961 -- "Handling of Default and Per-Object Expressions" in the spec of
1962 -- package Sem).
1963
1964 if Present (E) then
1965 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1966 Preanalyze_Default_Expression (E, T);
1967 Check_Initialization (T, E);
1968
1969 if Ada_Version >= Ada_2005
1970 and then Ekind (T) = E_Anonymous_Access_Type
1971 and then Etype (E) /= Any_Type
1972 then
1973 -- Check RM 3.9.2(9): "if the expected type for an expression is
1974 -- an anonymous access-to-specific tagged type, then the object
1975 -- designated by the expression shall not be dynamically tagged
1976 -- unless it is a controlling operand in a call on a dispatching
1977 -- operation"
1978
1979 if Is_Tagged_Type (Directly_Designated_Type (T))
1980 and then
1981 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1982 and then
1983 Ekind (Directly_Designated_Type (Etype (E))) =
1984 E_Class_Wide_Type
1985 then
1986 Error_Msg_N
1987 ("access to specific tagged type required (RM 3.9.2(9))", E);
1988 end if;
1989
1990 -- (Ada 2005: AI-230): Accessibility check for anonymous
1991 -- components
1992
1993 if Type_Access_Level (Etype (E)) >
1994 Deepest_Type_Access_Level (T)
1995 then
1996 Error_Msg_N
1997 ("expression has deeper access level than component " &
1998 "(RM 3.10.2 (12.2))", E);
1999 end if;
2000
2001 -- The initialization expression is a reference to an access
2002 -- discriminant. The type of the discriminant is always deeper
2003 -- than any access type.
2004
2005 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2006 and then Is_Entity_Name (E)
2007 and then Ekind (Entity (E)) = E_In_Parameter
2008 and then Present (Discriminal_Link (Entity (E)))
2009 then
2010 Error_Msg_N
2011 ("discriminant has deeper accessibility level than target",
2012 E);
2013 end if;
2014 end if;
2015 end if;
2016
2017 -- The parent type may be a private view with unknown discriminants,
2018 -- and thus unconstrained. Regular components must be constrained.
2019
2020 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
2021 if Is_Class_Wide_Type (T) then
2022 Error_Msg_N
2023 ("class-wide subtype with unknown discriminants" &
2024 " in component declaration",
2025 Subtype_Indication (Component_Definition (N)));
2026 else
2027 Error_Msg_N
2028 ("unconstrained subtype in component declaration",
2029 Subtype_Indication (Component_Definition (N)));
2030 end if;
2031
2032 -- Components cannot be abstract, except for the special case of
2033 -- the _Parent field (case of extending an abstract tagged type)
2034
2035 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2036 Error_Msg_N ("type of a component cannot be abstract", N);
2037 end if;
2038
2039 Set_Etype (Id, T);
2040 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2041
2042 -- The component declaration may have a per-object constraint, set
2043 -- the appropriate flag in the defining identifier of the subtype.
2044
2045 if Present (Subtype_Indication (Component_Definition (N))) then
2046 declare
2047 Sindic : constant Node_Id :=
2048 Subtype_Indication (Component_Definition (N));
2049 begin
2050 if Nkind (Sindic) = N_Subtype_Indication
2051 and then Present (Constraint (Sindic))
2052 and then Contains_POC (Constraint (Sindic))
2053 then
2054 Set_Has_Per_Object_Constraint (Id);
2055 end if;
2056 end;
2057 end if;
2058
2059 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2060 -- out some static checks.
2061
2062 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2063 Null_Exclusion_Static_Checks (N);
2064 end if;
2065
2066 -- If this component is private (or depends on a private type), flag the
2067 -- record type to indicate that some operations are not available.
2068
2069 P := Private_Component (T);
2070
2071 if Present (P) then
2072
2073 -- Check for circular definitions
2074
2075 if P = Any_Type then
2076 Set_Etype (Id, Any_Type);
2077
2078 -- There is a gap in the visibility of operations only if the
2079 -- component type is not defined in the scope of the record type.
2080
2081 elsif Scope (P) = Scope (Current_Scope) then
2082 null;
2083
2084 elsif Is_Limited_Type (P) then
2085 Set_Is_Limited_Composite (Current_Scope);
2086
2087 else
2088 Set_Is_Private_Composite (Current_Scope);
2089 end if;
2090 end if;
2091
2092 if P /= Any_Type
2093 and then Is_Limited_Type (T)
2094 and then Chars (Id) /= Name_uParent
2095 and then Is_Tagged_Type (Current_Scope)
2096 then
2097 if Is_Derived_Type (Current_Scope)
2098 and then not Is_Known_Limited (Current_Scope)
2099 then
2100 Error_Msg_N
2101 ("extension of nonlimited type cannot have limited components",
2102 N);
2103
2104 if Is_Interface (Root_Type (Current_Scope)) then
2105 Error_Msg_N
2106 ("\limitedness is not inherited from limited interface", N);
2107 Error_Msg_N ("\add LIMITED to type indication", N);
2108 end if;
2109
2110 Explain_Limited_Type (T, N);
2111 Set_Etype (Id, Any_Type);
2112 Set_Is_Limited_Composite (Current_Scope, False);
2113
2114 elsif not Is_Derived_Type (Current_Scope)
2115 and then not Is_Limited_Record (Current_Scope)
2116 and then not Is_Concurrent_Type (Current_Scope)
2117 then
2118 Error_Msg_N
2119 ("nonlimited tagged type cannot have limited components", N);
2120 Explain_Limited_Type (T, N);
2121 Set_Etype (Id, Any_Type);
2122 Set_Is_Limited_Composite (Current_Scope, False);
2123 end if;
2124 end if;
2125
2126 Set_Original_Record_Component (Id, Id);
2127
2128 if Has_Aspects (N) then
2129 Analyze_Aspect_Specifications (N, Id);
2130 end if;
2131
2132 Analyze_Dimension (N);
2133 end Analyze_Component_Declaration;
2134
2135 --------------------------
2136 -- Analyze_Declarations --
2137 --------------------------
2138
2139 procedure Analyze_Declarations (L : List_Id) is
2140 Decl : Node_Id;
2141
2142 procedure Adjust_Decl;
2143 -- Adjust Decl not to include implicit label declarations, since these
2144 -- have strange Sloc values that result in elaboration check problems.
2145 -- (They have the sloc of the label as found in the source, and that
2146 -- is ahead of the current declarative part).
2147
2148 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2149 -- Determine whether Body_Decl denotes the body of a late controlled
2150 -- primitive (either Initialize, Adjust or Finalize). If this is the
2151 -- case, add a proper spec if the body lacks one. The spec is inserted
2152 -- before Body_Decl and immedately analyzed.
2153
2154 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2155 -- Spec_Id is the entity of a package that may define abstract states.
2156 -- If the states have visible refinement, remove the visibility of each
2157 -- constituent at the end of the package body declarations.
2158
2159 -----------------
2160 -- Adjust_Decl --
2161 -----------------
2162
2163 procedure Adjust_Decl is
2164 begin
2165 while Present (Prev (Decl))
2166 and then Nkind (Decl) = N_Implicit_Label_Declaration
2167 loop
2168 Prev (Decl);
2169 end loop;
2170 end Adjust_Decl;
2171
2172 --------------------------------------
2173 -- Handle_Late_Controlled_Primitive --
2174 --------------------------------------
2175
2176 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2177 Body_Spec : constant Node_Id := Specification (Body_Decl);
2178 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2179 Loc : constant Source_Ptr := Sloc (Body_Id);
2180 Params : constant List_Id :=
2181 Parameter_Specifications (Body_Spec);
2182 Spec : Node_Id;
2183 Spec_Id : Entity_Id;
2184 Typ : Node_Id;
2185
2186 begin
2187 -- Consider only procedure bodies whose name matches one of the three
2188 -- controlled primitives.
2189
2190 if Nkind (Body_Spec) /= N_Procedure_Specification
2191 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2192 Name_Finalize,
2193 Name_Initialize)
2194 then
2195 return;
2196
2197 -- A controlled primitive must have exactly one formal which is not
2198 -- an anonymous access type.
2199
2200 elsif List_Length (Params) /= 1 then
2201 return;
2202 end if;
2203
2204 Typ := Parameter_Type (First (Params));
2205
2206 if Nkind (Typ) = N_Access_Definition then
2207 return;
2208 end if;
2209
2210 Find_Type (Typ);
2211
2212 -- The type of the formal must be derived from [Limited_]Controlled
2213
2214 if not Is_Controlled (Entity (Typ)) then
2215 return;
2216 end if;
2217
2218 -- Check whether a specification exists for this body. We do not
2219 -- analyze the spec of the body in full, because it will be analyzed
2220 -- again when the body is properly analyzed, and we cannot create
2221 -- duplicate entries in the formals chain. We look for an explicit
2222 -- specification because the body may be an overriding operation and
2223 -- an inherited spec may be present.
2224
2225 Spec_Id := Current_Entity (Body_Id);
2226
2227 while Present (Spec_Id) loop
2228 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2229 and then Scope (Spec_Id) = Current_Scope
2230 and then Present (First_Formal (Spec_Id))
2231 and then No (Next_Formal (First_Formal (Spec_Id)))
2232 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2233 and then Comes_From_Source (Spec_Id)
2234 then
2235 return;
2236 end if;
2237
2238 Spec_Id := Homonym (Spec_Id);
2239 end loop;
2240
2241 -- At this point the body is known to be a late controlled primitive.
2242 -- Generate a matching spec and insert it before the body. Note the
2243 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2244 -- tree in this case.
2245
2246 Spec := Copy_Separate_Tree (Body_Spec);
2247
2248 -- Ensure that the subprogram declaration does not inherit the null
2249 -- indicator from the body as we now have a proper spec/body pair.
2250
2251 Set_Null_Present (Spec, False);
2252
2253 Insert_Before_And_Analyze (Body_Decl,
2254 Make_Subprogram_Declaration (Loc, Specification => Spec));
2255 end Handle_Late_Controlled_Primitive;
2256
2257 --------------------------------
2258 -- Remove_Visible_Refinements --
2259 --------------------------------
2260
2261 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2262 State_Elmt : Elmt_Id;
2263 begin
2264 if Present (Abstract_States (Spec_Id)) then
2265 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2266 while Present (State_Elmt) loop
2267 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2268 Next_Elmt (State_Elmt);
2269 end loop;
2270 end if;
2271 end Remove_Visible_Refinements;
2272
2273 -- Local variables
2274
2275 Context : Node_Id;
2276 Freeze_From : Entity_Id := Empty;
2277 Next_Decl : Node_Id;
2278 Spec_Id : Entity_Id;
2279
2280 Body_Seen : Boolean := False;
2281 -- Flag set when the first body [stub] is encountered
2282
2283 In_Package_Body : Boolean := False;
2284 -- Flag set when the current declaration list belongs to a package body
2285
2286 -- Start of processing for Analyze_Declarations
2287
2288 begin
2289 if Restriction_Check_Required (SPARK_05) then
2290 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2291 end if;
2292
2293 Decl := First (L);
2294 while Present (Decl) loop
2295
2296 -- Package spec cannot contain a package declaration in SPARK
2297
2298 if Nkind (Decl) = N_Package_Declaration
2299 and then Nkind (Parent (L)) = N_Package_Specification
2300 then
2301 Check_SPARK_05_Restriction
2302 ("package specification cannot contain a package declaration",
2303 Decl);
2304 end if;
2305
2306 -- Complete analysis of declaration
2307
2308 Analyze (Decl);
2309 Next_Decl := Next (Decl);
2310
2311 if No (Freeze_From) then
2312 Freeze_From := First_Entity (Current_Scope);
2313 end if;
2314
2315 -- At the end of a declarative part, freeze remaining entities
2316 -- declared in it. The end of the visible declarations of package
2317 -- specification is not the end of a declarative part if private
2318 -- declarations are present. The end of a package declaration is a
2319 -- freezing point only if it a library package. A task definition or
2320 -- protected type definition is not a freeze point either. Finally,
2321 -- we do not freeze entities in generic scopes, because there is no
2322 -- code generated for them and freeze nodes will be generated for
2323 -- the instance.
2324
2325 -- The end of a package instantiation is not a freeze point, but
2326 -- for now we make it one, because the generic body is inserted
2327 -- (currently) immediately after. Generic instantiations will not
2328 -- be a freeze point once delayed freezing of bodies is implemented.
2329 -- (This is needed in any case for early instantiations ???).
2330
2331 if No (Next_Decl) then
2332 if Nkind_In (Parent (L), N_Component_List,
2333 N_Task_Definition,
2334 N_Protected_Definition)
2335 then
2336 null;
2337
2338 elsif Nkind (Parent (L)) /= N_Package_Specification then
2339 if Nkind (Parent (L)) = N_Package_Body then
2340 Freeze_From := First_Entity (Current_Scope);
2341 end if;
2342
2343 -- There may have been several freezing points previously,
2344 -- for example object declarations or subprogram bodies, but
2345 -- at the end of a declarative part we check freezing from
2346 -- the beginning, even though entities may already be frozen,
2347 -- in order to perform visibility checks on delayed aspects.
2348
2349 Adjust_Decl;
2350 Freeze_All (First_Entity (Current_Scope), Decl);
2351 Freeze_From := Last_Entity (Current_Scope);
2352
2353 elsif Scope (Current_Scope) /= Standard_Standard
2354 and then not Is_Child_Unit (Current_Scope)
2355 and then No (Generic_Parent (Parent (L)))
2356 then
2357 null;
2358
2359 elsif L /= Visible_Declarations (Parent (L))
2360 or else No (Private_Declarations (Parent (L)))
2361 or else Is_Empty_List (Private_Declarations (Parent (L)))
2362 then
2363 Adjust_Decl;
2364 Freeze_All (First_Entity (Current_Scope), Decl);
2365 Freeze_From := Last_Entity (Current_Scope);
2366 end if;
2367
2368 -- If next node is a body then freeze all types before the body.
2369 -- An exception occurs for some expander-generated bodies. If these
2370 -- are generated at places where in general language rules would not
2371 -- allow a freeze point, then we assume that the expander has
2372 -- explicitly checked that all required types are properly frozen,
2373 -- and we do not cause general freezing here. This special circuit
2374 -- is used when the encountered body is marked as having already
2375 -- been analyzed.
2376
2377 -- In all other cases (bodies that come from source, and expander
2378 -- generated bodies that have not been analyzed yet), freeze all
2379 -- types now. Note that in the latter case, the expander must take
2380 -- care to attach the bodies at a proper place in the tree so as to
2381 -- not cause unwanted freezing at that point.
2382
2383 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2384
2385 -- When a controlled type is frozen, the expander generates stream
2386 -- and controlled type support routines. If the freeze is caused
2387 -- by the stand alone body of Initialize, Adjust and Finalize, the
2388 -- expander will end up using the wrong version of these routines
2389 -- as the body has not been processed yet. To remedy this, detect
2390 -- a late controlled primitive and create a proper spec for it.
2391 -- This ensures that the primitive will override its inherited
2392 -- counterpart before the freeze takes place.
2393
2394 -- If the declaration we just processed is a body, do not attempt
2395 -- to examine Next_Decl as the late primitive idiom can only apply
2396 -- to the first encountered body.
2397
2398 -- The spec of the late primitive is not generated in ASIS mode to
2399 -- ensure a consistent list of primitives that indicates the true
2400 -- semantic structure of the program (which is not relevant when
2401 -- generating executable code.
2402
2403 -- ??? a cleaner approach may be possible and/or this solution
2404 -- could be extended to general-purpose late primitives, TBD.
2405
2406 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2407 then
2408 Body_Seen := True;
2409
2410 if Nkind (Next_Decl) = N_Subprogram_Body then
2411 Handle_Late_Controlled_Primitive (Next_Decl);
2412 end if;
2413 end if;
2414
2415 Adjust_Decl;
2416 Freeze_All (Freeze_From, Decl);
2417 Freeze_From := Last_Entity (Current_Scope);
2418 end if;
2419
2420 Decl := Next_Decl;
2421 end loop;
2422
2423 -- Analyze the contracts of packages and their bodies
2424
2425 if Present (L) then
2426 Context := Parent (L);
2427
2428 if Nkind (Context) = N_Package_Specification then
2429
2430 -- When a package has private declarations, its contract must be
2431 -- analyzed at the end of the said declarations. This way both the
2432 -- analysis and freeze actions are properly synchronized in case
2433 -- of private type use within the contract.
2434
2435 if L = Private_Declarations (Context) then
2436 Analyze_Package_Contract (Defining_Entity (Context));
2437
2438 -- Build the bodies of the default initial condition procedures
2439 -- for all types subject to pragma Default_Initial_Condition.
2440 -- From a purely Ada stand point, this is a freezing activity,
2441 -- however freezing is not available under GNATprove_Mode. To
2442 -- accomodate both scenarios, the bodies are build at the end
2443 -- of private declaration analysis.
2444
2445 Build_Default_Init_Cond_Procedure_Bodies (L);
2446
2447 -- Otherwise the contract is analyzed at the end of the visible
2448 -- declarations.
2449
2450 elsif L = Visible_Declarations (Context)
2451 and then No (Private_Declarations (Context))
2452 then
2453 Analyze_Package_Contract (Defining_Entity (Context));
2454 end if;
2455
2456 elsif Nkind (Context) = N_Package_Body then
2457 In_Package_Body := True;
2458 Spec_Id := Corresponding_Spec (Context);
2459
2460 Analyze_Package_Body_Contract (Defining_Entity (Context));
2461 end if;
2462 end if;
2463
2464 -- Analyze the contracts of subprogram declarations, subprogram bodies
2465 -- and variables now due to the delayed visibility requirements of their
2466 -- aspects.
2467
2468 Decl := First (L);
2469 while Present (Decl) loop
2470 if Nkind (Decl) = N_Object_Declaration then
2471 Analyze_Object_Contract (Defining_Entity (Decl));
2472
2473 elsif Nkind_In (Decl, N_Abstract_Subprogram_Declaration,
2474 N_Generic_Subprogram_Declaration,
2475 N_Subprogram_Declaration)
2476 then
2477 Analyze_Subprogram_Contract (Defining_Entity (Decl));
2478
2479 elsif Nkind (Decl) = N_Subprogram_Body then
2480 Analyze_Subprogram_Body_Contract (Defining_Entity (Decl));
2481
2482 elsif Nkind (Decl) = N_Subprogram_Body_Stub then
2483 Analyze_Subprogram_Body_Stub_Contract (Defining_Entity (Decl));
2484 end if;
2485
2486 Next (Decl);
2487 end loop;
2488
2489 -- State refinements are visible upto the end the of the package body
2490 -- declarations. Hide the refinements from visibility to restore the
2491 -- original state conditions.
2492
2493 if In_Package_Body then
2494 Remove_Visible_Refinements (Spec_Id);
2495 end if;
2496 end Analyze_Declarations;
2497
2498 -----------------------------------
2499 -- Analyze_Full_Type_Declaration --
2500 -----------------------------------
2501
2502 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2503 Def : constant Node_Id := Type_Definition (N);
2504 Def_Id : constant Entity_Id := Defining_Identifier (N);
2505 T : Entity_Id;
2506 Prev : Entity_Id;
2507
2508 Is_Remote : constant Boolean :=
2509 (Is_Remote_Types (Current_Scope)
2510 or else Is_Remote_Call_Interface (Current_Scope))
2511 and then not (In_Private_Part (Current_Scope)
2512 or else In_Package_Body (Current_Scope));
2513
2514 procedure Check_Ops_From_Incomplete_Type;
2515 -- If there is a tagged incomplete partial view of the type, traverse
2516 -- the primitives of the incomplete view and change the type of any
2517 -- controlling formals and result to indicate the full view. The
2518 -- primitives will be added to the full type's primitive operations
2519 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2520 -- is called from Process_Incomplete_Dependents).
2521
2522 ------------------------------------
2523 -- Check_Ops_From_Incomplete_Type --
2524 ------------------------------------
2525
2526 procedure Check_Ops_From_Incomplete_Type is
2527 Elmt : Elmt_Id;
2528 Formal : Entity_Id;
2529 Op : Entity_Id;
2530
2531 begin
2532 if Prev /= T
2533 and then Ekind (Prev) = E_Incomplete_Type
2534 and then Is_Tagged_Type (Prev)
2535 and then Is_Tagged_Type (T)
2536 then
2537 Elmt := First_Elmt (Primitive_Operations (Prev));
2538 while Present (Elmt) loop
2539 Op := Node (Elmt);
2540
2541 Formal := First_Formal (Op);
2542 while Present (Formal) loop
2543 if Etype (Formal) = Prev then
2544 Set_Etype (Formal, T);
2545 end if;
2546
2547 Next_Formal (Formal);
2548 end loop;
2549
2550 if Etype (Op) = Prev then
2551 Set_Etype (Op, T);
2552 end if;
2553
2554 Next_Elmt (Elmt);
2555 end loop;
2556 end if;
2557 end Check_Ops_From_Incomplete_Type;
2558
2559 -- Start of processing for Analyze_Full_Type_Declaration
2560
2561 begin
2562 Prev := Find_Type_Name (N);
2563
2564 -- The type declaration may be subject to pragma Ghost with policy
2565 -- Ignore. Set the mode now to ensure that any nodes generated during
2566 -- analysis and expansion are properly flagged as ignored Ghost.
2567
2568 Set_Ghost_Mode (N, Prev);
2569
2570 -- The full view, if present, now points to the current type. If there
2571 -- is an incomplete partial view, set a link to it, to simplify the
2572 -- retrieval of primitive operations of the type.
2573
2574 -- Ada 2005 (AI-50217): If the type was previously decorated when
2575 -- imported through a LIMITED WITH clause, it appears as incomplete
2576 -- but has no full view.
2577
2578 if Ekind (Prev) = E_Incomplete_Type
2579 and then Present (Full_View (Prev))
2580 then
2581 T := Full_View (Prev);
2582 Set_Incomplete_View (N, Parent (Prev));
2583 else
2584 T := Prev;
2585 end if;
2586
2587 Set_Is_Pure (T, Is_Pure (Current_Scope));
2588
2589 -- We set the flag Is_First_Subtype here. It is needed to set the
2590 -- corresponding flag for the Implicit class-wide-type created
2591 -- during tagged types processing.
2592
2593 Set_Is_First_Subtype (T, True);
2594
2595 -- Only composite types other than array types are allowed to have
2596 -- discriminants.
2597
2598 case Nkind (Def) is
2599
2600 -- For derived types, the rule will be checked once we've figured
2601 -- out the parent type.
2602
2603 when N_Derived_Type_Definition =>
2604 null;
2605
2606 -- For record types, discriminants are allowed, unless we are in
2607 -- SPARK.
2608
2609 when N_Record_Definition =>
2610 if Present (Discriminant_Specifications (N)) then
2611 Check_SPARK_05_Restriction
2612 ("discriminant type is not allowed",
2613 Defining_Identifier
2614 (First (Discriminant_Specifications (N))));
2615 end if;
2616
2617 when others =>
2618 if Present (Discriminant_Specifications (N)) then
2619 Error_Msg_N
2620 ("elementary or array type cannot have discriminants",
2621 Defining_Identifier
2622 (First (Discriminant_Specifications (N))));
2623 end if;
2624 end case;
2625
2626 -- Elaborate the type definition according to kind, and generate
2627 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2628 -- already done (this happens during the reanalysis that follows a call
2629 -- to the high level optimizer).
2630
2631 if not Analyzed (T) then
2632 Set_Analyzed (T);
2633
2634 case Nkind (Def) is
2635 when N_Access_To_Subprogram_Definition =>
2636 Access_Subprogram_Declaration (T, Def);
2637
2638 -- If this is a remote access to subprogram, we must create the
2639 -- equivalent fat pointer type, and related subprograms.
2640
2641 if Is_Remote then
2642 Process_Remote_AST_Declaration (N);
2643 end if;
2644
2645 -- Validate categorization rule against access type declaration
2646 -- usually a violation in Pure unit, Shared_Passive unit.
2647
2648 Validate_Access_Type_Declaration (T, N);
2649
2650 when N_Access_To_Object_Definition =>
2651 Access_Type_Declaration (T, Def);
2652
2653 -- Validate categorization rule against access type declaration
2654 -- usually a violation in Pure unit, Shared_Passive unit.
2655
2656 Validate_Access_Type_Declaration (T, N);
2657
2658 -- If we are in a Remote_Call_Interface package and define a
2659 -- RACW, then calling stubs and specific stream attributes
2660 -- must be added.
2661
2662 if Is_Remote
2663 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2664 then
2665 Add_RACW_Features (Def_Id);
2666 end if;
2667
2668 when N_Array_Type_Definition =>
2669 Array_Type_Declaration (T, Def);
2670
2671 when N_Derived_Type_Definition =>
2672 Derived_Type_Declaration (T, N, T /= Def_Id);
2673
2674 when N_Enumeration_Type_Definition =>
2675 Enumeration_Type_Declaration (T, Def);
2676
2677 when N_Floating_Point_Definition =>
2678 Floating_Point_Type_Declaration (T, Def);
2679
2680 when N_Decimal_Fixed_Point_Definition =>
2681 Decimal_Fixed_Point_Type_Declaration (T, Def);
2682
2683 when N_Ordinary_Fixed_Point_Definition =>
2684 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2685
2686 when N_Signed_Integer_Type_Definition =>
2687 Signed_Integer_Type_Declaration (T, Def);
2688
2689 when N_Modular_Type_Definition =>
2690 Modular_Type_Declaration (T, Def);
2691
2692 when N_Record_Definition =>
2693 Record_Type_Declaration (T, N, Prev);
2694
2695 -- If declaration has a parse error, nothing to elaborate.
2696
2697 when N_Error =>
2698 null;
2699
2700 when others =>
2701 raise Program_Error;
2702
2703 end case;
2704 end if;
2705
2706 if Etype (T) = Any_Type then
2707 return;
2708 end if;
2709
2710 -- Controlled type is not allowed in SPARK
2711
2712 if Is_Visibly_Controlled (T) then
2713 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
2714 end if;
2715
2716 -- A type declared within a Ghost region is automatically Ghost
2717 -- (SPARK RM 6.9(2)).
2718
2719 if Comes_From_Source (T) and then Ghost_Mode > None then
2720 Set_Is_Ghost_Entity (T);
2721 end if;
2722
2723 -- Some common processing for all types
2724
2725 Set_Depends_On_Private (T, Has_Private_Component (T));
2726 Check_Ops_From_Incomplete_Type;
2727
2728 -- Both the declared entity, and its anonymous base type if one was
2729 -- created, need freeze nodes allocated.
2730
2731 declare
2732 B : constant Entity_Id := Base_Type (T);
2733
2734 begin
2735 -- In the case where the base type differs from the first subtype, we
2736 -- pre-allocate a freeze node, and set the proper link to the first
2737 -- subtype. Freeze_Entity will use this preallocated freeze node when
2738 -- it freezes the entity.
2739
2740 -- This does not apply if the base type is a generic type, whose
2741 -- declaration is independent of the current derived definition.
2742
2743 if B /= T and then not Is_Generic_Type (B) then
2744 Ensure_Freeze_Node (B);
2745 Set_First_Subtype_Link (Freeze_Node (B), T);
2746 end if;
2747
2748 -- A type that is imported through a limited_with clause cannot
2749 -- generate any code, and thus need not be frozen. However, an access
2750 -- type with an imported designated type needs a finalization list,
2751 -- which may be referenced in some other package that has non-limited
2752 -- visibility on the designated type. Thus we must create the
2753 -- finalization list at the point the access type is frozen, to
2754 -- prevent unsatisfied references at link time.
2755
2756 if not From_Limited_With (T) or else Is_Access_Type (T) then
2757 Set_Has_Delayed_Freeze (T);
2758 end if;
2759 end;
2760
2761 -- Case where T is the full declaration of some private type which has
2762 -- been swapped in Defining_Identifier (N).
2763
2764 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2765 Process_Full_View (N, T, Def_Id);
2766
2767 -- Record the reference. The form of this is a little strange, since
2768 -- the full declaration has been swapped in. So the first parameter
2769 -- here represents the entity to which a reference is made which is
2770 -- the "real" entity, i.e. the one swapped in, and the second
2771 -- parameter provides the reference location.
2772
2773 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2774 -- since we don't want a complaint about the full type being an
2775 -- unwanted reference to the private type
2776
2777 declare
2778 B : constant Boolean := Has_Pragma_Unreferenced (T);
2779 begin
2780 Set_Has_Pragma_Unreferenced (T, False);
2781 Generate_Reference (T, T, 'c');
2782 Set_Has_Pragma_Unreferenced (T, B);
2783 end;
2784
2785 Set_Completion_Referenced (Def_Id);
2786
2787 -- For completion of incomplete type, process incomplete dependents
2788 -- and always mark the full type as referenced (it is the incomplete
2789 -- type that we get for any real reference).
2790
2791 elsif Ekind (Prev) = E_Incomplete_Type then
2792 Process_Incomplete_Dependents (N, T, Prev);
2793 Generate_Reference (Prev, Def_Id, 'c');
2794 Set_Completion_Referenced (Def_Id);
2795
2796 -- If not private type or incomplete type completion, this is a real
2797 -- definition of a new entity, so record it.
2798
2799 else
2800 Generate_Definition (Def_Id);
2801 end if;
2802
2803 -- Propagate any pending access types whose finalization masters need to
2804 -- be fully initialized from the partial to the full view. Guard against
2805 -- an illegal full view that remains unanalyzed.
2806
2807 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
2808 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
2809 end if;
2810
2811 if Chars (Scope (Def_Id)) = Name_System
2812 and then Chars (Def_Id) = Name_Address
2813 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2814 then
2815 Set_Is_Descendent_Of_Address (Def_Id);
2816 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2817 Set_Is_Descendent_Of_Address (Prev);
2818 end if;
2819
2820 Set_Optimize_Alignment_Flags (Def_Id);
2821 Check_Eliminated (Def_Id);
2822
2823 -- If the declaration is a completion and aspects are present, apply
2824 -- them to the entity for the type which is currently the partial
2825 -- view, but which is the one that will be frozen.
2826
2827 if Has_Aspects (N) then
2828
2829 -- In most cases the partial view is a private type, and both views
2830 -- appear in different declarative parts. In the unusual case where
2831 -- the partial view is incomplete, perform the analysis on the
2832 -- full view, to prevent freezing anomalies with the corresponding
2833 -- class-wide type, which otherwise might be frozen before the
2834 -- dispatch table is built.
2835
2836 if Prev /= Def_Id
2837 and then Ekind (Prev) /= E_Incomplete_Type
2838 then
2839 Analyze_Aspect_Specifications (N, Prev);
2840
2841 -- Normal case
2842
2843 else
2844 Analyze_Aspect_Specifications (N, Def_Id);
2845 end if;
2846 end if;
2847 end Analyze_Full_Type_Declaration;
2848
2849 ----------------------------------
2850 -- Analyze_Incomplete_Type_Decl --
2851 ----------------------------------
2852
2853 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2854 F : constant Boolean := Is_Pure (Current_Scope);
2855 T : Entity_Id;
2856
2857 begin
2858 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
2859
2860 Generate_Definition (Defining_Identifier (N));
2861
2862 -- Process an incomplete declaration. The identifier must not have been
2863 -- declared already in the scope. However, an incomplete declaration may
2864 -- appear in the private part of a package, for a private type that has
2865 -- already been declared.
2866
2867 -- In this case, the discriminants (if any) must match
2868
2869 T := Find_Type_Name (N);
2870
2871 Set_Ekind (T, E_Incomplete_Type);
2872 Init_Size_Align (T);
2873 Set_Is_First_Subtype (T, True);
2874 Set_Etype (T, T);
2875
2876 -- An incomplete type declared within a Ghost region is automatically
2877 -- Ghost (SPARK RM 6.9(2)).
2878
2879 if Ghost_Mode > None then
2880 Set_Is_Ghost_Entity (T);
2881 end if;
2882
2883 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2884 -- incomplete types.
2885
2886 if Tagged_Present (N) then
2887 Set_Is_Tagged_Type (T, True);
2888 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2889 Make_Class_Wide_Type (T);
2890 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2891 end if;
2892
2893 Push_Scope (T);
2894
2895 Set_Stored_Constraint (T, No_Elist);
2896
2897 if Present (Discriminant_Specifications (N)) then
2898 Process_Discriminants (N);
2899 end if;
2900
2901 End_Scope;
2902
2903 -- If the type has discriminants, non-trivial subtypes may be
2904 -- declared before the full view of the type. The full views of those
2905 -- subtypes will be built after the full view of the type.
2906
2907 Set_Private_Dependents (T, New_Elmt_List);
2908 Set_Is_Pure (T, F);
2909 end Analyze_Incomplete_Type_Decl;
2910
2911 -----------------------------------
2912 -- Analyze_Interface_Declaration --
2913 -----------------------------------
2914
2915 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2916 CW : constant Entity_Id := Class_Wide_Type (T);
2917
2918 begin
2919 Set_Is_Tagged_Type (T);
2920 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2921
2922 Set_Is_Limited_Record (T, Limited_Present (Def)
2923 or else Task_Present (Def)
2924 or else Protected_Present (Def)
2925 or else Synchronized_Present (Def));
2926
2927 -- Type is abstract if full declaration carries keyword, or if previous
2928 -- partial view did.
2929
2930 Set_Is_Abstract_Type (T);
2931 Set_Is_Interface (T);
2932
2933 -- Type is a limited interface if it includes the keyword limited, task,
2934 -- protected, or synchronized.
2935
2936 Set_Is_Limited_Interface
2937 (T, Limited_Present (Def)
2938 or else Protected_Present (Def)
2939 or else Synchronized_Present (Def)
2940 or else Task_Present (Def));
2941
2942 Set_Interfaces (T, New_Elmt_List);
2943 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2944
2945 -- Complete the decoration of the class-wide entity if it was already
2946 -- built (i.e. during the creation of the limited view)
2947
2948 if Present (CW) then
2949 Set_Is_Interface (CW);
2950 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2951 end if;
2952
2953 -- Check runtime support for synchronized interfaces
2954
2955 if VM_Target = No_VM
2956 and then (Is_Task_Interface (T)
2957 or else Is_Protected_Interface (T)
2958 or else Is_Synchronized_Interface (T))
2959 and then not RTE_Available (RE_Select_Specific_Data)
2960 then
2961 Error_Msg_CRT ("synchronized interfaces", T);
2962 end if;
2963 end Analyze_Interface_Declaration;
2964
2965 -----------------------------
2966 -- Analyze_Itype_Reference --
2967 -----------------------------
2968
2969 -- Nothing to do. This node is placed in the tree only for the benefit of
2970 -- back end processing, and has no effect on the semantic processing.
2971
2972 procedure Analyze_Itype_Reference (N : Node_Id) is
2973 begin
2974 pragma Assert (Is_Itype (Itype (N)));
2975 null;
2976 end Analyze_Itype_Reference;
2977
2978 --------------------------------
2979 -- Analyze_Number_Declaration --
2980 --------------------------------
2981
2982 procedure Analyze_Number_Declaration (N : Node_Id) is
2983 Id : constant Entity_Id := Defining_Identifier (N);
2984 E : constant Node_Id := Expression (N);
2985 T : Entity_Id;
2986 Index : Interp_Index;
2987 It : Interp;
2988
2989 begin
2990 -- The number declaration may be subject to pragma Ghost with policy
2991 -- Ignore. Set the mode now to ensure that any nodes generated during
2992 -- analysis and expansion are properly flagged as ignored Ghost.
2993
2994 Set_Ghost_Mode (N);
2995
2996 Generate_Definition (Id);
2997 Enter_Name (Id);
2998
2999 -- A number declared within a Ghost region is automatically Ghost
3000 -- (SPARK RM 6.9(2)).
3001
3002 if Ghost_Mode > None then
3003 Set_Is_Ghost_Entity (Id);
3004 end if;
3005
3006 -- This is an optimization of a common case of an integer literal
3007
3008 if Nkind (E) = N_Integer_Literal then
3009 Set_Is_Static_Expression (E, True);
3010 Set_Etype (E, Universal_Integer);
3011
3012 Set_Etype (Id, Universal_Integer);
3013 Set_Ekind (Id, E_Named_Integer);
3014 Set_Is_Frozen (Id, True);
3015 return;
3016 end if;
3017
3018 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3019
3020 -- Process expression, replacing error by integer zero, to avoid
3021 -- cascaded errors or aborts further along in the processing
3022
3023 -- Replace Error by integer zero, which seems least likely to cause
3024 -- cascaded errors.
3025
3026 if E = Error then
3027 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3028 Set_Error_Posted (E);
3029 end if;
3030
3031 Analyze (E);
3032
3033 -- Verify that the expression is static and numeric. If
3034 -- the expression is overloaded, we apply the preference
3035 -- rule that favors root numeric types.
3036
3037 if not Is_Overloaded (E) then
3038 T := Etype (E);
3039 if Has_Dynamic_Predicate_Aspect (T) then
3040 Error_Msg_N
3041 ("subtype has dynamic predicate, "
3042 & "not allowed in number declaration", N);
3043 end if;
3044
3045 else
3046 T := Any_Type;
3047
3048 Get_First_Interp (E, Index, It);
3049 while Present (It.Typ) loop
3050 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3051 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3052 then
3053 if T = Any_Type then
3054 T := It.Typ;
3055
3056 elsif It.Typ = Universal_Real
3057 or else
3058 It.Typ = Universal_Integer
3059 then
3060 -- Choose universal interpretation over any other
3061
3062 T := It.Typ;
3063 exit;
3064 end if;
3065 end if;
3066
3067 Get_Next_Interp (Index, It);
3068 end loop;
3069 end if;
3070
3071 if Is_Integer_Type (T) then
3072 Resolve (E, T);
3073 Set_Etype (Id, Universal_Integer);
3074 Set_Ekind (Id, E_Named_Integer);
3075
3076 elsif Is_Real_Type (T) then
3077
3078 -- Because the real value is converted to universal_real, this is a
3079 -- legal context for a universal fixed expression.
3080
3081 if T = Universal_Fixed then
3082 declare
3083 Loc : constant Source_Ptr := Sloc (N);
3084 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3085 Subtype_Mark =>
3086 New_Occurrence_Of (Universal_Real, Loc),
3087 Expression => Relocate_Node (E));
3088
3089 begin
3090 Rewrite (E, Conv);
3091 Analyze (E);
3092 end;
3093
3094 elsif T = Any_Fixed then
3095 Error_Msg_N ("illegal context for mixed mode operation", E);
3096
3097 -- Expression is of the form : universal_fixed * integer. Try to
3098 -- resolve as universal_real.
3099
3100 T := Universal_Real;
3101 Set_Etype (E, T);
3102 end if;
3103
3104 Resolve (E, T);
3105 Set_Etype (Id, Universal_Real);
3106 Set_Ekind (Id, E_Named_Real);
3107
3108 else
3109 Wrong_Type (E, Any_Numeric);
3110 Resolve (E, T);
3111
3112 Set_Etype (Id, T);
3113 Set_Ekind (Id, E_Constant);
3114 Set_Never_Set_In_Source (Id, True);
3115 Set_Is_True_Constant (Id, True);
3116 return;
3117 end if;
3118
3119 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3120 Set_Etype (E, Etype (Id));
3121 end if;
3122
3123 if not Is_OK_Static_Expression (E) then
3124 Flag_Non_Static_Expr
3125 ("non-static expression used in number declaration!", E);
3126 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3127 Set_Etype (E, Any_Type);
3128 end if;
3129 end Analyze_Number_Declaration;
3130
3131 -----------------------------
3132 -- Analyze_Object_Contract --
3133 -----------------------------
3134
3135 procedure Analyze_Object_Contract (Obj_Id : Entity_Id) is
3136 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
3137 AR_Val : Boolean := False;
3138 AW_Val : Boolean := False;
3139 ER_Val : Boolean := False;
3140 EW_Val : Boolean := False;
3141 Prag : Node_Id;
3142 Seen : Boolean := False;
3143
3144 begin
3145 -- The loop parameter in an element iterator over a formal container
3146 -- is declared with an object declaration but no contracts apply.
3147
3148 if Ekind (Obj_Id) = E_Loop_Parameter then
3149 return;
3150 end if;
3151
3152 if Ekind (Obj_Id) = E_Constant then
3153
3154 -- A constant cannot be effectively volatile. This check is only
3155 -- relevant with SPARK_Mode on as it is not a standard Ada legality
3156 -- rule. Do not flag internally-generated constants that map generic
3157 -- formals to actuals in instantiations (SPARK RM 7.1.3(6)).
3158
3159 if SPARK_Mode = On
3160 and then Is_Effectively_Volatile (Obj_Id)
3161 and then No (Corresponding_Generic_Association (Parent (Obj_Id)))
3162 then
3163 Error_Msg_N ("constant cannot be volatile", Obj_Id);
3164 end if;
3165
3166 else pragma Assert (Ekind (Obj_Id) = E_Variable);
3167
3168 -- The following checks are only relevant when SPARK_Mode is on as
3169 -- they are not standard Ada legality rules. Internally generated
3170 -- temporaries are ignored.
3171
3172 if SPARK_Mode = On and then Comes_From_Source (Obj_Id) then
3173 if Is_Effectively_Volatile (Obj_Id) then
3174
3175 -- The declaration of an effectively volatile object must
3176 -- appear at the library level (SPARK RM 7.1.3(7), C.6(6)).
3177
3178 if not Is_Library_Level_Entity (Obj_Id) then
3179 Error_Msg_N
3180 ("volatile variable & must be declared at library level",
3181 Obj_Id);
3182
3183 -- An object of a discriminated type cannot be effectively
3184 -- volatile (SPARK RM C.6(4)).
3185
3186 elsif Has_Discriminants (Obj_Typ) then
3187 Error_Msg_N
3188 ("discriminated object & cannot be volatile", Obj_Id);
3189
3190 -- An object of a tagged type cannot be effectively volatile
3191 -- (SPARK RM C.6(5)).
3192
3193 elsif Is_Tagged_Type (Obj_Typ) then
3194 Error_Msg_N ("tagged object & cannot be volatile", Obj_Id);
3195 end if;
3196
3197 -- The object is not effectively volatile
3198
3199 else
3200 -- A non-effectively volatile object cannot have effectively
3201 -- volatile components (SPARK RM 7.1.3(7)).
3202
3203 if not Is_Effectively_Volatile (Obj_Id)
3204 and then Has_Volatile_Component (Obj_Typ)
3205 then
3206 Error_Msg_N
3207 ("non-volatile object & cannot have volatile components",
3208 Obj_Id);
3209 end if;
3210 end if;
3211 end if;
3212
3213 if Is_Ghost_Entity (Obj_Id) then
3214
3215 -- A Ghost object cannot be effectively volatile (SPARK RM 6.9(8))
3216
3217 if Is_Effectively_Volatile (Obj_Id) then
3218 Error_Msg_N ("ghost variable & cannot be volatile", Obj_Id);
3219
3220 -- A Ghost object cannot be imported or exported (SPARK RM 6.9(8))
3221
3222 elsif Is_Imported (Obj_Id) then
3223 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3224
3225 elsif Is_Exported (Obj_Id) then
3226 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3227 end if;
3228 end if;
3229
3230 -- Analyze all external properties
3231
3232 Prag := Get_Pragma (Obj_Id, Pragma_Async_Readers);
3233
3234 if Present (Prag) then
3235 Analyze_External_Property_In_Decl_Part (Prag, AR_Val);
3236 Seen := True;
3237 end if;
3238
3239 Prag := Get_Pragma (Obj_Id, Pragma_Async_Writers);
3240
3241 if Present (Prag) then
3242 Analyze_External_Property_In_Decl_Part (Prag, AW_Val);
3243 Seen := True;
3244 end if;
3245
3246 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Reads);
3247
3248 if Present (Prag) then
3249 Analyze_External_Property_In_Decl_Part (Prag, ER_Val);
3250 Seen := True;
3251 end if;
3252
3253 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Writes);
3254
3255 if Present (Prag) then
3256 Analyze_External_Property_In_Decl_Part (Prag, EW_Val);
3257 Seen := True;
3258 end if;
3259
3260 -- Verify the mutual interaction of the various external properties
3261
3262 if Seen then
3263 Check_External_Properties (Obj_Id, AR_Val, AW_Val, ER_Val, EW_Val);
3264 end if;
3265
3266 -- Check whether the lack of indicator Part_Of agrees with the
3267 -- placement of the variable with respect to the state space.
3268
3269 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
3270
3271 if No (Prag) then
3272 Check_Missing_Part_Of (Obj_Id);
3273 end if;
3274 end if;
3275
3276 -- A ghost object cannot be imported or exported (SPARK RM 6.9(8))
3277
3278 if Is_Ghost_Entity (Obj_Id) then
3279 if Is_Exported (Obj_Id) then
3280 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3281
3282 elsif Is_Imported (Obj_Id) then
3283 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3284 end if;
3285 end if;
3286 end Analyze_Object_Contract;
3287
3288 --------------------------------
3289 -- Analyze_Object_Declaration --
3290 --------------------------------
3291
3292 procedure Analyze_Object_Declaration (N : Node_Id) is
3293 Loc : constant Source_Ptr := Sloc (N);
3294 Id : constant Entity_Id := Defining_Identifier (N);
3295 T : Entity_Id;
3296 Act_T : Entity_Id;
3297
3298 E : Node_Id := Expression (N);
3299 -- E is set to Expression (N) throughout this routine. When
3300 -- Expression (N) is modified, E is changed accordingly.
3301
3302 Prev_Entity : Entity_Id := Empty;
3303
3304 function Count_Tasks (T : Entity_Id) return Uint;
3305 -- This function is called when a non-generic library level object of a
3306 -- task type is declared. Its function is to count the static number of
3307 -- tasks declared within the type (it is only called if Has_Tasks is set
3308 -- for T). As a side effect, if an array of tasks with non-static bounds
3309 -- or a variant record type is encountered, Check_Restrictions is called
3310 -- indicating the count is unknown.
3311
3312 -----------------
3313 -- Count_Tasks --
3314 -----------------
3315
3316 function Count_Tasks (T : Entity_Id) return Uint is
3317 C : Entity_Id;
3318 X : Node_Id;
3319 V : Uint;
3320
3321 begin
3322 if Is_Task_Type (T) then
3323 return Uint_1;
3324
3325 elsif Is_Record_Type (T) then
3326 if Has_Discriminants (T) then
3327 Check_Restriction (Max_Tasks, N);
3328 return Uint_0;
3329
3330 else
3331 V := Uint_0;
3332 C := First_Component (T);
3333 while Present (C) loop
3334 V := V + Count_Tasks (Etype (C));
3335 Next_Component (C);
3336 end loop;
3337
3338 return V;
3339 end if;
3340
3341 elsif Is_Array_Type (T) then
3342 X := First_Index (T);
3343 V := Count_Tasks (Component_Type (T));
3344 while Present (X) loop
3345 C := Etype (X);
3346
3347 if not Is_OK_Static_Subtype (C) then
3348 Check_Restriction (Max_Tasks, N);
3349 return Uint_0;
3350 else
3351 V := V * (UI_Max (Uint_0,
3352 Expr_Value (Type_High_Bound (C)) -
3353 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3354 end if;
3355
3356 Next_Index (X);
3357 end loop;
3358
3359 return V;
3360
3361 else
3362 return Uint_0;
3363 end if;
3364 end Count_Tasks;
3365
3366 -- Start of processing for Analyze_Object_Declaration
3367
3368 begin
3369 -- There are three kinds of implicit types generated by an
3370 -- object declaration:
3371
3372 -- 1. Those generated by the original Object Definition
3373
3374 -- 2. Those generated by the Expression
3375
3376 -- 3. Those used to constrain the Object Definition with the
3377 -- expression constraints when the definition is unconstrained.
3378
3379 -- They must be generated in this order to avoid order of elaboration
3380 -- issues. Thus the first step (after entering the name) is to analyze
3381 -- the object definition.
3382
3383 if Constant_Present (N) then
3384 Prev_Entity := Current_Entity_In_Scope (Id);
3385
3386 if Present (Prev_Entity)
3387 and then
3388 -- If the homograph is an implicit subprogram, it is overridden
3389 -- by the current declaration.
3390
3391 ((Is_Overloadable (Prev_Entity)
3392 and then Is_Inherited_Operation (Prev_Entity))
3393
3394 -- The current object is a discriminal generated for an entry
3395 -- family index. Even though the index is a constant, in this
3396 -- particular context there is no true constant redeclaration.
3397 -- Enter_Name will handle the visibility.
3398
3399 or else
3400 (Is_Discriminal (Id)
3401 and then Ekind (Discriminal_Link (Id)) =
3402 E_Entry_Index_Parameter)
3403
3404 -- The current object is the renaming for a generic declared
3405 -- within the instance.
3406
3407 or else
3408 (Ekind (Prev_Entity) = E_Package
3409 and then Nkind (Parent (Prev_Entity)) =
3410 N_Package_Renaming_Declaration
3411 and then not Comes_From_Source (Prev_Entity)
3412 and then
3413 Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
3414 then
3415 Prev_Entity := Empty;
3416 end if;
3417 end if;
3418
3419 -- The object declaration may be subject to pragma Ghost with policy
3420 -- Ignore. Set the mode now to ensure that any nodes generated during
3421 -- analysis and expansion are properly flagged as ignored Ghost.
3422
3423 Set_Ghost_Mode (N, Prev_Entity);
3424
3425 if Present (Prev_Entity) then
3426 Constant_Redeclaration (Id, N, T);
3427
3428 Generate_Reference (Prev_Entity, Id, 'c');
3429 Set_Completion_Referenced (Id);
3430
3431 if Error_Posted (N) then
3432
3433 -- Type mismatch or illegal redeclaration, Do not analyze
3434 -- expression to avoid cascaded errors.
3435
3436 T := Find_Type_Of_Object (Object_Definition (N), N);
3437 Set_Etype (Id, T);
3438 Set_Ekind (Id, E_Variable);
3439 goto Leave;
3440 end if;
3441
3442 -- In the normal case, enter identifier at the start to catch premature
3443 -- usage in the initialization expression.
3444
3445 else
3446 Generate_Definition (Id);
3447 Enter_Name (Id);
3448
3449 Mark_Coextensions (N, Object_Definition (N));
3450
3451 T := Find_Type_Of_Object (Object_Definition (N), N);
3452
3453 if Nkind (Object_Definition (N)) = N_Access_Definition
3454 and then Present
3455 (Access_To_Subprogram_Definition (Object_Definition (N)))
3456 and then Protected_Present
3457 (Access_To_Subprogram_Definition (Object_Definition (N)))
3458 then
3459 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3460 end if;
3461
3462 if Error_Posted (Id) then
3463 Set_Etype (Id, T);
3464 Set_Ekind (Id, E_Variable);
3465 goto Leave;
3466 end if;
3467 end if;
3468
3469 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3470 -- out some static checks
3471
3472 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3473
3474 -- In case of aggregates we must also take care of the correct
3475 -- initialization of nested aggregates bug this is done at the
3476 -- point of the analysis of the aggregate (see sem_aggr.adb).
3477
3478 if Present (Expression (N))
3479 and then Nkind (Expression (N)) = N_Aggregate
3480 then
3481 null;
3482
3483 else
3484 declare
3485 Save_Typ : constant Entity_Id := Etype (Id);
3486 begin
3487 Set_Etype (Id, T); -- Temp. decoration for static checks
3488 Null_Exclusion_Static_Checks (N);
3489 Set_Etype (Id, Save_Typ);
3490 end;
3491 end if;
3492 end if;
3493
3494 -- Object is marked pure if it is in a pure scope
3495
3496 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3497
3498 -- If deferred constant, make sure context is appropriate. We detect
3499 -- a deferred constant as a constant declaration with no expression.
3500 -- A deferred constant can appear in a package body if its completion
3501 -- is by means of an interface pragma.
3502
3503 if Constant_Present (N) and then No (E) then
3504
3505 -- A deferred constant may appear in the declarative part of the
3506 -- following constructs:
3507
3508 -- blocks
3509 -- entry bodies
3510 -- extended return statements
3511 -- package specs
3512 -- package bodies
3513 -- subprogram bodies
3514 -- task bodies
3515
3516 -- When declared inside a package spec, a deferred constant must be
3517 -- completed by a full constant declaration or pragma Import. In all
3518 -- other cases, the only proper completion is pragma Import. Extended
3519 -- return statements are flagged as invalid contexts because they do
3520 -- not have a declarative part and so cannot accommodate the pragma.
3521
3522 if Ekind (Current_Scope) = E_Return_Statement then
3523 Error_Msg_N
3524 ("invalid context for deferred constant declaration (RM 7.4)",
3525 N);
3526 Error_Msg_N
3527 ("\declaration requires an initialization expression",
3528 N);
3529 Set_Constant_Present (N, False);
3530
3531 -- In Ada 83, deferred constant must be of private type
3532
3533 elsif not Is_Private_Type (T) then
3534 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3535 Error_Msg_N
3536 ("(Ada 83) deferred constant must be private type", N);
3537 end if;
3538 end if;
3539
3540 -- If not a deferred constant, then the object declaration freezes
3541 -- its type, unless the object is of an anonymous type and has delayed
3542 -- aspects. In that case the type is frozen when the object itself is.
3543
3544 else
3545 Check_Fully_Declared (T, N);
3546
3547 if Has_Delayed_Aspects (Id)
3548 and then Is_Array_Type (T)
3549 and then Is_Itype (T)
3550 then
3551 Set_Has_Delayed_Freeze (T);
3552 else
3553 Freeze_Before (N, T);
3554 end if;
3555 end if;
3556
3557 -- If the object was created by a constrained array definition, then
3558 -- set the link in both the anonymous base type and anonymous subtype
3559 -- that are built to represent the array type to point to the object.
3560
3561 if Nkind (Object_Definition (Declaration_Node (Id))) =
3562 N_Constrained_Array_Definition
3563 then
3564 Set_Related_Array_Object (T, Id);
3565 Set_Related_Array_Object (Base_Type (T), Id);
3566 end if;
3567
3568 -- Special checks for protected objects not at library level
3569
3570 if Is_Protected_Type (T)
3571 and then not Is_Library_Level_Entity (Id)
3572 then
3573 Check_Restriction (No_Local_Protected_Objects, Id);
3574
3575 -- Protected objects with interrupt handlers must be at library level
3576
3577 -- Ada 2005: This test is not needed (and the corresponding clause
3578 -- in the RM is removed) because accessibility checks are sufficient
3579 -- to make handlers not at the library level illegal.
3580
3581 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3582 -- applies to the '95 version of the language as well.
3583
3584 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3585 Error_Msg_N
3586 ("interrupt object can only be declared at library level", Id);
3587 end if;
3588 end if;
3589
3590 -- The actual subtype of the object is the nominal subtype, unless
3591 -- the nominal one is unconstrained and obtained from the expression.
3592
3593 Act_T := T;
3594
3595 -- These checks should be performed before the initialization expression
3596 -- is considered, so that the Object_Definition node is still the same
3597 -- as in source code.
3598
3599 -- In SPARK, the nominal subtype is always given by a subtype mark
3600 -- and must not be unconstrained. (The only exception to this is the
3601 -- acceptance of declarations of constants of type String.)
3602
3603 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3604 then
3605 Check_SPARK_05_Restriction
3606 ("subtype mark required", Object_Definition (N));
3607
3608 elsif Is_Array_Type (T)
3609 and then not Is_Constrained (T)
3610 and then T /= Standard_String
3611 then
3612 Check_SPARK_05_Restriction
3613 ("subtype mark of constrained type expected",
3614 Object_Definition (N));
3615 end if;
3616
3617 -- There are no aliased objects in SPARK
3618
3619 if Aliased_Present (N) then
3620 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3621 end if;
3622
3623 -- Process initialization expression if present and not in error
3624
3625 if Present (E) and then E /= Error then
3626
3627 -- Generate an error in case of CPP class-wide object initialization.
3628 -- Required because otherwise the expansion of the class-wide
3629 -- assignment would try to use 'size to initialize the object
3630 -- (primitive that is not available in CPP tagged types).
3631
3632 if Is_Class_Wide_Type (Act_T)
3633 and then
3634 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3635 or else
3636 (Present (Full_View (Root_Type (Etype (Act_T))))
3637 and then
3638 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3639 then
3640 Error_Msg_N
3641 ("predefined assignment not available for 'C'P'P tagged types",
3642 E);
3643 end if;
3644
3645 Mark_Coextensions (N, E);
3646 Analyze (E);
3647
3648 -- In case of errors detected in the analysis of the expression,
3649 -- decorate it with the expected type to avoid cascaded errors
3650
3651 if No (Etype (E)) then
3652 Set_Etype (E, T);
3653 end if;
3654
3655 -- If an initialization expression is present, then we set the
3656 -- Is_True_Constant flag. It will be reset if this is a variable
3657 -- and it is indeed modified.
3658
3659 Set_Is_True_Constant (Id, True);
3660
3661 -- If we are analyzing a constant declaration, set its completion
3662 -- flag after analyzing and resolving the expression.
3663
3664 if Constant_Present (N) then
3665 Set_Has_Completion (Id);
3666 end if;
3667
3668 -- Set type and resolve (type may be overridden later on). Note:
3669 -- Ekind (Id) must still be E_Void at this point so that incorrect
3670 -- early usage within E is properly diagnosed.
3671
3672 Set_Etype (Id, T);
3673
3674 -- If the expression is an aggregate we must look ahead to detect
3675 -- the possible presence of an address clause, and defer resolution
3676 -- and expansion of the aggregate to the freeze point of the entity.
3677
3678 if Comes_From_Source (N)
3679 and then Expander_Active
3680 and then Nkind (E) = N_Aggregate
3681 and then Present (Following_Address_Clause (N))
3682 then
3683 Set_Etype (E, T);
3684
3685 else
3686 Resolve (E, T);
3687 end if;
3688
3689 -- No further action needed if E is a call to an inlined function
3690 -- which returns an unconstrained type and it has been expanded into
3691 -- a procedure call. In that case N has been replaced by an object
3692 -- declaration without initializing expression and it has been
3693 -- analyzed (see Expand_Inlined_Call).
3694
3695 if Back_End_Inlining
3696 and then Expander_Active
3697 and then Nkind (E) = N_Function_Call
3698 and then Nkind (Name (E)) in N_Has_Entity
3699 and then Is_Inlined (Entity (Name (E)))
3700 and then not Is_Constrained (Etype (E))
3701 and then Analyzed (N)
3702 and then No (Expression (N))
3703 then
3704 return;
3705 end if;
3706
3707 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3708 -- node (which was marked already-analyzed), we need to set the type
3709 -- to something other than Any_Access in order to keep gigi happy.
3710
3711 if Etype (E) = Any_Access then
3712 Set_Etype (E, T);
3713 end if;
3714
3715 -- If the object is an access to variable, the initialization
3716 -- expression cannot be an access to constant.
3717
3718 if Is_Access_Type (T)
3719 and then not Is_Access_Constant (T)
3720 and then Is_Access_Type (Etype (E))
3721 and then Is_Access_Constant (Etype (E))
3722 then
3723 Error_Msg_N
3724 ("access to variable cannot be initialized with an "
3725 & "access-to-constant expression", E);
3726 end if;
3727
3728 if not Assignment_OK (N) then
3729 Check_Initialization (T, E);
3730 end if;
3731
3732 Check_Unset_Reference (E);
3733
3734 -- If this is a variable, then set current value. If this is a
3735 -- declared constant of a scalar type with a static expression,
3736 -- indicate that it is always valid.
3737
3738 if not Constant_Present (N) then
3739 if Compile_Time_Known_Value (E) then
3740 Set_Current_Value (Id, E);
3741 end if;
3742
3743 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3744 Set_Is_Known_Valid (Id);
3745 end if;
3746
3747 -- Deal with setting of null flags
3748
3749 if Is_Access_Type (T) then
3750 if Known_Non_Null (E) then
3751 Set_Is_Known_Non_Null (Id, True);
3752 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3753 Set_Is_Known_Null (Id, True);
3754 end if;
3755 end if;
3756
3757 -- Check incorrect use of dynamically tagged expressions
3758
3759 if Is_Tagged_Type (T) then
3760 Check_Dynamically_Tagged_Expression
3761 (Expr => E,
3762 Typ => T,
3763 Related_Nod => N);
3764 end if;
3765
3766 Apply_Scalar_Range_Check (E, T);
3767 Apply_Static_Length_Check (E, T);
3768
3769 if Nkind (Original_Node (N)) = N_Object_Declaration
3770 and then Comes_From_Source (Original_Node (N))
3771
3772 -- Only call test if needed
3773
3774 and then Restriction_Check_Required (SPARK_05)
3775 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3776 then
3777 Check_SPARK_05_Restriction
3778 ("initialization expression is not appropriate", E);
3779 end if;
3780
3781 -- A formal parameter of a specific tagged type whose related
3782 -- subprogram is subject to pragma Extensions_Visible with value
3783 -- "False" cannot be implicitly converted to a class-wide type by
3784 -- means of an initialization expression (SPARK RM 6.1.7(3)).
3785
3786 if Is_Class_Wide_Type (T) and then Is_EVF_Expression (E) then
3787 Error_Msg_N
3788 ("formal parameter with Extensions_Visible False cannot be "
3789 & "implicitly converted to class-wide type", E);
3790 end if;
3791 end if;
3792
3793 -- If the No_Streams restriction is set, check that the type of the
3794 -- object is not, and does not contain, any subtype derived from
3795 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3796 -- Has_Stream just for efficiency reasons. There is no point in
3797 -- spending time on a Has_Stream check if the restriction is not set.
3798
3799 if Restriction_Check_Required (No_Streams) then
3800 if Has_Stream (T) then
3801 Check_Restriction (No_Streams, N);
3802 end if;
3803 end if;
3804
3805 -- Deal with predicate check before we start to do major rewriting. It
3806 -- is OK to initialize and then check the initialized value, since the
3807 -- object goes out of scope if we get a predicate failure. Note that we
3808 -- do this in the analyzer and not the expander because the analyzer
3809 -- does some substantial rewriting in some cases.
3810
3811 -- We need a predicate check if the type has predicates, and if either
3812 -- there is an initializing expression, or for default initialization
3813 -- when we have at least one case of an explicit default initial value
3814 -- and then this is not an internal declaration whose initialization
3815 -- comes later (as for an aggregate expansion).
3816
3817 if not Suppress_Assignment_Checks (N)
3818 and then Present (Predicate_Function (T))
3819 and then not No_Initialization (N)
3820 and then
3821 (Present (E)
3822 or else
3823 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3824 then
3825 -- If the type has a static predicate and the expression is known at
3826 -- compile time, see if the expression satisfies the predicate.
3827
3828 if Present (E) then
3829 Check_Expression_Against_Static_Predicate (E, T);
3830 end if;
3831
3832 Insert_After (N,
3833 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3834 end if;
3835
3836 -- Case of unconstrained type
3837
3838 if Is_Indefinite_Subtype (T) then
3839
3840 -- In SPARK, a declaration of unconstrained type is allowed
3841 -- only for constants of type string.
3842
3843 if Is_String_Type (T) and then not Constant_Present (N) then
3844 Check_SPARK_05_Restriction
3845 ("declaration of object of unconstrained type not allowed", N);
3846 end if;
3847
3848 -- Nothing to do in deferred constant case
3849
3850 if Constant_Present (N) and then No (E) then
3851 null;
3852
3853 -- Case of no initialization present
3854
3855 elsif No (E) then
3856 if No_Initialization (N) then
3857 null;
3858
3859 elsif Is_Class_Wide_Type (T) then
3860 Error_Msg_N
3861 ("initialization required in class-wide declaration ", N);
3862
3863 else
3864 Error_Msg_N
3865 ("unconstrained subtype not allowed (need initialization)",
3866 Object_Definition (N));
3867
3868 if Is_Record_Type (T) and then Has_Discriminants (T) then
3869 Error_Msg_N
3870 ("\provide initial value or explicit discriminant values",
3871 Object_Definition (N));
3872
3873 Error_Msg_NE
3874 ("\or give default discriminant values for type&",
3875 Object_Definition (N), T);
3876
3877 elsif Is_Array_Type (T) then
3878 Error_Msg_N
3879 ("\provide initial value or explicit array bounds",
3880 Object_Definition (N));
3881 end if;
3882 end if;
3883
3884 -- Case of initialization present but in error. Set initial
3885 -- expression as absent (but do not make above complaints)
3886
3887 elsif E = Error then
3888 Set_Expression (N, Empty);
3889 E := Empty;
3890
3891 -- Case of initialization present
3892
3893 else
3894 -- Check restrictions in Ada 83
3895
3896 if not Constant_Present (N) then
3897
3898 -- Unconstrained variables not allowed in Ada 83 mode
3899
3900 if Ada_Version = Ada_83
3901 and then Comes_From_Source (Object_Definition (N))
3902 then
3903 Error_Msg_N
3904 ("(Ada 83) unconstrained variable not allowed",
3905 Object_Definition (N));
3906 end if;
3907 end if;
3908
3909 -- Now we constrain the variable from the initializing expression
3910
3911 -- If the expression is an aggregate, it has been expanded into
3912 -- individual assignments. Retrieve the actual type from the
3913 -- expanded construct.
3914
3915 if Is_Array_Type (T)
3916 and then No_Initialization (N)
3917 and then Nkind (Original_Node (E)) = N_Aggregate
3918 then
3919 Act_T := Etype (E);
3920
3921 -- In case of class-wide interface object declarations we delay
3922 -- the generation of the equivalent record type declarations until
3923 -- its expansion because there are cases in they are not required.
3924
3925 elsif Is_Interface (T) then
3926 null;
3927
3928 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
3929 -- we should prevent the generation of another Itype with the
3930 -- same name as the one already generated, or we end up with
3931 -- two identical types in GNATprove.
3932
3933 elsif GNATprove_Mode then
3934 null;
3935
3936 -- If the type is an unchecked union, no subtype can be built from
3937 -- the expression. Rewrite declaration as a renaming, which the
3938 -- back-end can handle properly. This is a rather unusual case,
3939 -- because most unchecked_union declarations have default values
3940 -- for discriminants and are thus not indefinite.
3941
3942 elsif Is_Unchecked_Union (T) then
3943 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
3944 Set_Ekind (Id, E_Constant);
3945 else
3946 Set_Ekind (Id, E_Variable);
3947 end if;
3948
3949 -- An object declared within a Ghost region is automatically
3950 -- Ghost (SPARK RM 6.9(2)).
3951
3952 if Comes_From_Source (Id) and then Ghost_Mode > None then
3953 Set_Is_Ghost_Entity (Id);
3954
3955 -- The Ghost policy in effect at the point of declaration
3956 -- and at the point of completion must match
3957 -- (SPARK RM 6.9(15)).
3958
3959 if Present (Prev_Entity)
3960 and then Is_Ghost_Entity (Prev_Entity)
3961 then
3962 Check_Ghost_Completion (Prev_Entity, Id);
3963 end if;
3964 end if;
3965
3966 Rewrite (N,
3967 Make_Object_Renaming_Declaration (Loc,
3968 Defining_Identifier => Id,
3969 Subtype_Mark => New_Occurrence_Of (T, Loc),
3970 Name => E));
3971
3972 Set_Renamed_Object (Id, E);
3973 Freeze_Before (N, T);
3974 Set_Is_Frozen (Id);
3975 return;
3976
3977 else
3978 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3979 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3980 end if;
3981
3982 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
3983
3984 if Aliased_Present (N) then
3985 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3986 end if;
3987
3988 Freeze_Before (N, Act_T);
3989 Freeze_Before (N, T);
3990 end if;
3991
3992 elsif Is_Array_Type (T)
3993 and then No_Initialization (N)
3994 and then Nkind (Original_Node (E)) = N_Aggregate
3995 then
3996 if not Is_Entity_Name (Object_Definition (N)) then
3997 Act_T := Etype (E);
3998 Check_Compile_Time_Size (Act_T);
3999
4000 if Aliased_Present (N) then
4001 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4002 end if;
4003 end if;
4004
4005 -- When the given object definition and the aggregate are specified
4006 -- independently, and their lengths might differ do a length check.
4007 -- This cannot happen if the aggregate is of the form (others =>...)
4008
4009 if not Is_Constrained (T) then
4010 null;
4011
4012 elsif Nkind (E) = N_Raise_Constraint_Error then
4013
4014 -- Aggregate is statically illegal. Place back in declaration
4015
4016 Set_Expression (N, E);
4017 Set_No_Initialization (N, False);
4018
4019 elsif T = Etype (E) then
4020 null;
4021
4022 elsif Nkind (E) = N_Aggregate
4023 and then Present (Component_Associations (E))
4024 and then Present (Choices (First (Component_Associations (E))))
4025 and then Nkind (First
4026 (Choices (First (Component_Associations (E))))) = N_Others_Choice
4027 then
4028 null;
4029
4030 else
4031 Apply_Length_Check (E, T);
4032 end if;
4033
4034 -- If the type is limited unconstrained with defaulted discriminants and
4035 -- there is no expression, then the object is constrained by the
4036 -- defaults, so it is worthwhile building the corresponding subtype.
4037
4038 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4039 and then not Is_Constrained (T)
4040 and then Has_Discriminants (T)
4041 then
4042 if No (E) then
4043 Act_T := Build_Default_Subtype (T, N);
4044 else
4045 -- Ada 2005: A limited object may be initialized by means of an
4046 -- aggregate. If the type has default discriminants it has an
4047 -- unconstrained nominal type, Its actual subtype will be obtained
4048 -- from the aggregate, and not from the default discriminants.
4049
4050 Act_T := Etype (E);
4051 end if;
4052
4053 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4054
4055 elsif Nkind (E) = N_Function_Call
4056 and then Constant_Present (N)
4057 and then Has_Unconstrained_Elements (Etype (E))
4058 then
4059 -- The back-end has problems with constants of a discriminated type
4060 -- with defaults, if the initial value is a function call. We
4061 -- generate an intermediate temporary that will receive a reference
4062 -- to the result of the call. The initialization expression then
4063 -- becomes a dereference of that temporary.
4064
4065 Remove_Side_Effects (E);
4066
4067 -- If this is a constant declaration of an unconstrained type and
4068 -- the initialization is an aggregate, we can use the subtype of the
4069 -- aggregate for the declared entity because it is immutable.
4070
4071 elsif not Is_Constrained (T)
4072 and then Has_Discriminants (T)
4073 and then Constant_Present (N)
4074 and then not Has_Unchecked_Union (T)
4075 and then Nkind (E) = N_Aggregate
4076 then
4077 Act_T := Etype (E);
4078 end if;
4079
4080 -- Check No_Wide_Characters restriction
4081
4082 Check_Wide_Character_Restriction (T, Object_Definition (N));
4083
4084 -- Indicate this is not set in source. Certainly true for constants, and
4085 -- true for variables so far (will be reset for a variable if and when
4086 -- we encounter a modification in the source).
4087
4088 Set_Never_Set_In_Source (Id);
4089
4090 -- Now establish the proper kind and type of the object
4091
4092 if Constant_Present (N) then
4093 Set_Ekind (Id, E_Constant);
4094 Set_Is_True_Constant (Id);
4095
4096 else
4097 Set_Ekind (Id, E_Variable);
4098
4099 -- A variable is set as shared passive if it appears in a shared
4100 -- passive package, and is at the outer level. This is not done for
4101 -- entities generated during expansion, because those are always
4102 -- manipulated locally.
4103
4104 if Is_Shared_Passive (Current_Scope)
4105 and then Is_Library_Level_Entity (Id)
4106 and then Comes_From_Source (Id)
4107 then
4108 Set_Is_Shared_Passive (Id);
4109 Check_Shared_Var (Id, T, N);
4110 end if;
4111
4112 -- Set Has_Initial_Value if initializing expression present. Note
4113 -- that if there is no initializing expression, we leave the state
4114 -- of this flag unchanged (usually it will be False, but notably in
4115 -- the case of exception choice variables, it will already be true).
4116
4117 if Present (E) then
4118 Set_Has_Initial_Value (Id);
4119 end if;
4120 end if;
4121
4122 -- Initialize alignment and size and capture alignment setting
4123
4124 Init_Alignment (Id);
4125 Init_Esize (Id);
4126 Set_Optimize_Alignment_Flags (Id);
4127
4128 -- An object declared within a Ghost region is automatically Ghost
4129 -- (SPARK RM 6.9(2)).
4130
4131 if Comes_From_Source (Id)
4132 and then (Ghost_Mode > None
4133 or else (Present (Prev_Entity)
4134 and then Is_Ghost_Entity (Prev_Entity)))
4135 then
4136 Set_Is_Ghost_Entity (Id);
4137
4138 -- The Ghost policy in effect at the point of declaration and at the
4139 -- point of completion must match (SPARK RM 6.9(16)).
4140
4141 if Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity) then
4142 Check_Ghost_Completion (Prev_Entity, Id);
4143 end if;
4144 end if;
4145
4146 -- Deal with aliased case
4147
4148 if Aliased_Present (N) then
4149 Set_Is_Aliased (Id);
4150
4151 -- If the object is aliased and the type is unconstrained with
4152 -- defaulted discriminants and there is no expression, then the
4153 -- object is constrained by the defaults, so it is worthwhile
4154 -- building the corresponding subtype.
4155
4156 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4157 -- unconstrained, then only establish an actual subtype if the
4158 -- nominal subtype is indefinite. In definite cases the object is
4159 -- unconstrained in Ada 2005.
4160
4161 if No (E)
4162 and then Is_Record_Type (T)
4163 and then not Is_Constrained (T)
4164 and then Has_Discriminants (T)
4165 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
4166 then
4167 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4168 end if;
4169 end if;
4170
4171 -- Now we can set the type of the object
4172
4173 Set_Etype (Id, Act_T);
4174
4175 -- Non-constant object is marked to be treated as volatile if type is
4176 -- volatile and we clear the Current_Value setting that may have been
4177 -- set above. Doing so for constants isn't required and might interfere
4178 -- with possible uses of the object as a static expression in contexts
4179 -- incompatible with volatility (e.g. as a case-statement alternative).
4180
4181 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4182 Set_Treat_As_Volatile (Id);
4183 Set_Current_Value (Id, Empty);
4184 end if;
4185
4186 -- Deal with controlled types
4187
4188 if Has_Controlled_Component (Etype (Id))
4189 or else Is_Controlled (Etype (Id))
4190 then
4191 if not Is_Library_Level_Entity (Id) then
4192 Check_Restriction (No_Nested_Finalization, N);
4193 else
4194 Validate_Controlled_Object (Id);
4195 end if;
4196 end if;
4197
4198 if Has_Task (Etype (Id)) then
4199 Check_Restriction (No_Tasking, N);
4200
4201 -- Deal with counting max tasks
4202
4203 -- Nothing to do if inside a generic
4204
4205 if Inside_A_Generic then
4206 null;
4207
4208 -- If library level entity, then count tasks
4209
4210 elsif Is_Library_Level_Entity (Id) then
4211 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4212
4213 -- If not library level entity, then indicate we don't know max
4214 -- tasks and also check task hierarchy restriction and blocking
4215 -- operation (since starting a task is definitely blocking).
4216
4217 else
4218 Check_Restriction (Max_Tasks, N);
4219 Check_Restriction (No_Task_Hierarchy, N);
4220 Check_Potentially_Blocking_Operation (N);
4221 end if;
4222
4223 -- A rather specialized test. If we see two tasks being declared
4224 -- of the same type in the same object declaration, and the task
4225 -- has an entry with an address clause, we know that program error
4226 -- will be raised at run time since we can't have two tasks with
4227 -- entries at the same address.
4228
4229 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4230 declare
4231 E : Entity_Id;
4232
4233 begin
4234 E := First_Entity (Etype (Id));
4235 while Present (E) loop
4236 if Ekind (E) = E_Entry
4237 and then Present (Get_Attribute_Definition_Clause
4238 (E, Attribute_Address))
4239 then
4240 Error_Msg_Warn := SPARK_Mode /= On;
4241 Error_Msg_N
4242 ("more than one task with same entry address<<", N);
4243 Error_Msg_N ("\Program_Error [<<", N);
4244 Insert_Action (N,
4245 Make_Raise_Program_Error (Loc,
4246 Reason => PE_Duplicated_Entry_Address));
4247 exit;
4248 end if;
4249
4250 Next_Entity (E);
4251 end loop;
4252 end;
4253 end if;
4254 end if;
4255
4256 -- Some simple constant-propagation: if the expression is a constant
4257 -- string initialized with a literal, share the literal. This avoids
4258 -- a run-time copy.
4259
4260 if Present (E)
4261 and then Is_Entity_Name (E)
4262 and then Ekind (Entity (E)) = E_Constant
4263 and then Base_Type (Etype (E)) = Standard_String
4264 then
4265 declare
4266 Val : constant Node_Id := Constant_Value (Entity (E));
4267 begin
4268 if Present (Val) and then Nkind (Val) = N_String_Literal then
4269 Rewrite (E, New_Copy (Val));
4270 end if;
4271 end;
4272 end if;
4273
4274 -- Another optimization: if the nominal subtype is unconstrained and
4275 -- the expression is a function call that returns an unconstrained
4276 -- type, rewrite the declaration as a renaming of the result of the
4277 -- call. The exceptions below are cases where the copy is expected,
4278 -- either by the back end (Aliased case) or by the semantics, as for
4279 -- initializing controlled types or copying tags for classwide types.
4280
4281 if Present (E)
4282 and then Nkind (E) = N_Explicit_Dereference
4283 and then Nkind (Original_Node (E)) = N_Function_Call
4284 and then not Is_Library_Level_Entity (Id)
4285 and then not Is_Constrained (Underlying_Type (T))
4286 and then not Is_Aliased (Id)
4287 and then not Is_Class_Wide_Type (T)
4288 and then not Is_Controlled (T)
4289 and then not Has_Controlled_Component (Base_Type (T))
4290 and then Expander_Active
4291 then
4292 Rewrite (N,
4293 Make_Object_Renaming_Declaration (Loc,
4294 Defining_Identifier => Id,
4295 Access_Definition => Empty,
4296 Subtype_Mark => New_Occurrence_Of
4297 (Base_Type (Etype (Id)), Loc),
4298 Name => E));
4299
4300 Set_Renamed_Object (Id, E);
4301
4302 -- Force generation of debugging information for the constant and for
4303 -- the renamed function call.
4304
4305 Set_Debug_Info_Needed (Id);
4306 Set_Debug_Info_Needed (Entity (Prefix (E)));
4307 end if;
4308
4309 if Present (Prev_Entity)
4310 and then Is_Frozen (Prev_Entity)
4311 and then not Error_Posted (Id)
4312 then
4313 Error_Msg_N ("full constant declaration appears too late", N);
4314 end if;
4315
4316 Check_Eliminated (Id);
4317
4318 -- Deal with setting In_Private_Part flag if in private part
4319
4320 if Ekind (Scope (Id)) = E_Package and then In_Private_Part (Scope (Id))
4321 then
4322 Set_In_Private_Part (Id);
4323 end if;
4324
4325 -- Check for violation of No_Local_Timing_Events
4326
4327 if Restriction_Check_Required (No_Local_Timing_Events)
4328 and then not Is_Library_Level_Entity (Id)
4329 and then Is_RTE (Etype (Id), RE_Timing_Event)
4330 then
4331 Check_Restriction (No_Local_Timing_Events, N);
4332 end if;
4333
4334 <<Leave>>
4335 -- Initialize the refined state of a variable here because this is a
4336 -- common destination for legal and illegal object declarations.
4337
4338 if Ekind (Id) = E_Variable then
4339 Set_Encapsulating_State (Id, Empty);
4340 end if;
4341
4342 if Has_Aspects (N) then
4343 Analyze_Aspect_Specifications (N, Id);
4344 end if;
4345
4346 Analyze_Dimension (N);
4347
4348 -- Verify whether the object declaration introduces an illegal hidden
4349 -- state within a package subject to a null abstract state.
4350
4351 if Ekind (Id) = E_Variable then
4352 Check_No_Hidden_State (Id);
4353 end if;
4354 end Analyze_Object_Declaration;
4355
4356 ---------------------------
4357 -- Analyze_Others_Choice --
4358 ---------------------------
4359
4360 -- Nothing to do for the others choice node itself, the semantic analysis
4361 -- of the others choice will occur as part of the processing of the parent
4362
4363 procedure Analyze_Others_Choice (N : Node_Id) is
4364 pragma Warnings (Off, N);
4365 begin
4366 null;
4367 end Analyze_Others_Choice;
4368
4369 -------------------------------------------
4370 -- Analyze_Private_Extension_Declaration --
4371 -------------------------------------------
4372
4373 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4374 T : constant Entity_Id := Defining_Identifier (N);
4375 Indic : constant Node_Id := Subtype_Indication (N);
4376 Parent_Type : Entity_Id;
4377 Parent_Base : Entity_Id;
4378
4379 begin
4380 -- The private extension declaration may be subject to pragma Ghost with
4381 -- policy Ignore. Set the mode now to ensure that any nodes generated
4382 -- during analysis and expansion are properly flagged as ignored Ghost.
4383
4384 Set_Ghost_Mode (N);
4385
4386 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4387
4388 if Is_Non_Empty_List (Interface_List (N)) then
4389 declare
4390 Intf : Node_Id;
4391 T : Entity_Id;
4392
4393 begin
4394 Intf := First (Interface_List (N));
4395 while Present (Intf) loop
4396 T := Find_Type_Of_Subtype_Indic (Intf);
4397
4398 Diagnose_Interface (Intf, T);
4399 Next (Intf);
4400 end loop;
4401 end;
4402 end if;
4403
4404 Generate_Definition (T);
4405
4406 -- For other than Ada 2012, just enter the name in the current scope
4407
4408 if Ada_Version < Ada_2012 then
4409 Enter_Name (T);
4410
4411 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4412 -- case of private type that completes an incomplete type.
4413
4414 else
4415 declare
4416 Prev : Entity_Id;
4417
4418 begin
4419 Prev := Find_Type_Name (N);
4420
4421 pragma Assert (Prev = T
4422 or else (Ekind (Prev) = E_Incomplete_Type
4423 and then Present (Full_View (Prev))
4424 and then Full_View (Prev) = T));
4425 end;
4426 end if;
4427
4428 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4429 Parent_Base := Base_Type (Parent_Type);
4430
4431 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4432 Set_Ekind (T, Ekind (Parent_Type));
4433 Set_Etype (T, Any_Type);
4434 goto Leave;
4435
4436 elsif not Is_Tagged_Type (Parent_Type) then
4437 Error_Msg_N
4438 ("parent of type extension must be a tagged type ", Indic);
4439 goto Leave;
4440
4441 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4442 Error_Msg_N ("premature derivation of incomplete type", Indic);
4443 goto Leave;
4444
4445 elsif Is_Concurrent_Type (Parent_Type) then
4446 Error_Msg_N
4447 ("parent type of a private extension cannot be "
4448 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
4449
4450 Set_Etype (T, Any_Type);
4451 Set_Ekind (T, E_Limited_Private_Type);
4452 Set_Private_Dependents (T, New_Elmt_List);
4453 Set_Error_Posted (T);
4454 goto Leave;
4455 end if;
4456
4457 -- Perhaps the parent type should be changed to the class-wide type's
4458 -- specific type in this case to prevent cascading errors ???
4459
4460 if Is_Class_Wide_Type (Parent_Type) then
4461 Error_Msg_N
4462 ("parent of type extension must not be a class-wide type", Indic);
4463 goto Leave;
4464 end if;
4465
4466 if (not Is_Package_Or_Generic_Package (Current_Scope)
4467 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4468 or else In_Private_Part (Current_Scope)
4469
4470 then
4471 Error_Msg_N ("invalid context for private extension", N);
4472 end if;
4473
4474 -- Set common attributes
4475
4476 Set_Is_Pure (T, Is_Pure (Current_Scope));
4477 Set_Scope (T, Current_Scope);
4478 Set_Ekind (T, E_Record_Type_With_Private);
4479 Init_Size_Align (T);
4480 Set_Default_SSO (T);
4481
4482 Set_Etype (T, Parent_Base);
4483 Set_Has_Task (T, Has_Task (Parent_Base));
4484 Set_Has_Protected (T, Has_Task (Parent_Base));
4485
4486 Set_Convention (T, Convention (Parent_Type));
4487 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4488 Set_Is_First_Subtype (T);
4489 Make_Class_Wide_Type (T);
4490
4491 if Unknown_Discriminants_Present (N) then
4492 Set_Discriminant_Constraint (T, No_Elist);
4493 end if;
4494
4495 Build_Derived_Record_Type (N, Parent_Type, T);
4496
4497 -- Propagate inherited invariant information. The new type has
4498 -- invariants, if the parent type has inheritable invariants,
4499 -- and these invariants can in turn be inherited.
4500
4501 if Has_Inheritable_Invariants (Parent_Type) then
4502 Set_Has_Inheritable_Invariants (T);
4503 Set_Has_Invariants (T);
4504 end if;
4505
4506 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4507 -- synchronized formal derived type.
4508
4509 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4510 Set_Is_Limited_Record (T);
4511
4512 -- Formal derived type case
4513
4514 if Is_Generic_Type (T) then
4515
4516 -- The parent must be a tagged limited type or a synchronized
4517 -- interface.
4518
4519 if (not Is_Tagged_Type (Parent_Type)
4520 or else not Is_Limited_Type (Parent_Type))
4521 and then
4522 (not Is_Interface (Parent_Type)
4523 or else not Is_Synchronized_Interface (Parent_Type))
4524 then
4525 Error_Msg_NE ("parent type of & must be tagged limited " &
4526 "or synchronized", N, T);
4527 end if;
4528
4529 -- The progenitors (if any) must be limited or synchronized
4530 -- interfaces.
4531
4532 if Present (Interfaces (T)) then
4533 declare
4534 Iface : Entity_Id;
4535 Iface_Elmt : Elmt_Id;
4536
4537 begin
4538 Iface_Elmt := First_Elmt (Interfaces (T));
4539 while Present (Iface_Elmt) loop
4540 Iface := Node (Iface_Elmt);
4541
4542 if not Is_Limited_Interface (Iface)
4543 and then not Is_Synchronized_Interface (Iface)
4544 then
4545 Error_Msg_NE ("progenitor & must be limited " &
4546 "or synchronized", N, Iface);
4547 end if;
4548
4549 Next_Elmt (Iface_Elmt);
4550 end loop;
4551 end;
4552 end if;
4553
4554 -- Regular derived extension, the parent must be a limited or
4555 -- synchronized interface.
4556
4557 else
4558 if not Is_Interface (Parent_Type)
4559 or else (not Is_Limited_Interface (Parent_Type)
4560 and then not Is_Synchronized_Interface (Parent_Type))
4561 then
4562 Error_Msg_NE
4563 ("parent type of & must be limited interface", N, T);
4564 end if;
4565 end if;
4566
4567 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4568 -- extension with a synchronized parent must be explicitly declared
4569 -- synchronized, because the full view will be a synchronized type.
4570 -- This must be checked before the check for limited types below,
4571 -- to ensure that types declared limited are not allowed to extend
4572 -- synchronized interfaces.
4573
4574 elsif Is_Interface (Parent_Type)
4575 and then Is_Synchronized_Interface (Parent_Type)
4576 and then not Synchronized_Present (N)
4577 then
4578 Error_Msg_NE
4579 ("private extension of& must be explicitly synchronized",
4580 N, Parent_Type);
4581
4582 elsif Limited_Present (N) then
4583 Set_Is_Limited_Record (T);
4584
4585 if not Is_Limited_Type (Parent_Type)
4586 and then
4587 (not Is_Interface (Parent_Type)
4588 or else not Is_Limited_Interface (Parent_Type))
4589 then
4590 Error_Msg_NE ("parent type& of limited extension must be limited",
4591 N, Parent_Type);
4592 end if;
4593 end if;
4594
4595 <<Leave>>
4596 if Has_Aspects (N) then
4597 Analyze_Aspect_Specifications (N, T);
4598 end if;
4599 end Analyze_Private_Extension_Declaration;
4600
4601 ---------------------------------
4602 -- Analyze_Subtype_Declaration --
4603 ---------------------------------
4604
4605 procedure Analyze_Subtype_Declaration
4606 (N : Node_Id;
4607 Skip : Boolean := False)
4608 is
4609 Id : constant Entity_Id := Defining_Identifier (N);
4610 T : Entity_Id;
4611 R_Checks : Check_Result;
4612
4613 begin
4614 -- The subtype declaration may be subject to pragma Ghost with policy
4615 -- Ignore. Set the mode now to ensure that any nodes generated during
4616 -- analysis and expansion are properly flagged as ignored Ghost.
4617
4618 Set_Ghost_Mode (N);
4619
4620 Generate_Definition (Id);
4621 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4622 Init_Size_Align (Id);
4623
4624 -- The following guard condition on Enter_Name is to handle cases where
4625 -- the defining identifier has already been entered into the scope but
4626 -- the declaration as a whole needs to be analyzed.
4627
4628 -- This case in particular happens for derived enumeration types. The
4629 -- derived enumeration type is processed as an inserted enumeration type
4630 -- declaration followed by a rewritten subtype declaration. The defining
4631 -- identifier, however, is entered into the name scope very early in the
4632 -- processing of the original type declaration and therefore needs to be
4633 -- avoided here, when the created subtype declaration is analyzed. (See
4634 -- Build_Derived_Types)
4635
4636 -- This also happens when the full view of a private type is derived
4637 -- type with constraints. In this case the entity has been introduced
4638 -- in the private declaration.
4639
4640 -- Finally this happens in some complex cases when validity checks are
4641 -- enabled, where the same subtype declaration may be analyzed twice.
4642 -- This can happen if the subtype is created by the pre-analysis of
4643 -- an attribute tht gives the range of a loop statement, and the loop
4644 -- itself appears within an if_statement that will be rewritten during
4645 -- expansion.
4646
4647 if Skip
4648 or else (Present (Etype (Id))
4649 and then (Is_Private_Type (Etype (Id))
4650 or else Is_Task_Type (Etype (Id))
4651 or else Is_Rewrite_Substitution (N)))
4652 then
4653 null;
4654
4655 elsif Current_Entity (Id) = Id then
4656 null;
4657
4658 else
4659 Enter_Name (Id);
4660 end if;
4661
4662 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4663
4664 -- Class-wide equivalent types of records with unknown discriminants
4665 -- involve the generation of an itype which serves as the private view
4666 -- of a constrained record subtype. In such cases the base type of the
4667 -- current subtype we are processing is the private itype. Use the full
4668 -- of the private itype when decorating various attributes.
4669
4670 if Is_Itype (T)
4671 and then Is_Private_Type (T)
4672 and then Present (Full_View (T))
4673 then
4674 T := Full_View (T);
4675 end if;
4676
4677 -- Inherit common attributes
4678
4679 Set_Is_Volatile (Id, Is_Volatile (T));
4680 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4681 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4682 Set_Convention (Id, Convention (T));
4683
4684 -- If ancestor has predicates then so does the subtype, and in addition
4685 -- we must delay the freeze to properly arrange predicate inheritance.
4686
4687 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4688 -- in which T = ID, so the above tests and assignments do nothing???
4689
4690 if Has_Predicates (T)
4691 or else (Present (Ancestor_Subtype (T))
4692 and then Has_Predicates (Ancestor_Subtype (T)))
4693 then
4694 Set_Has_Predicates (Id);
4695 Set_Has_Delayed_Freeze (Id);
4696 end if;
4697
4698 -- Subtype of Boolean cannot have a constraint in SPARK
4699
4700 if Is_Boolean_Type (T)
4701 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4702 then
4703 Check_SPARK_05_Restriction
4704 ("subtype of Boolean cannot have constraint", N);
4705 end if;
4706
4707 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4708 declare
4709 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4710 One_Cstr : Node_Id;
4711 Low : Node_Id;
4712 High : Node_Id;
4713
4714 begin
4715 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4716 One_Cstr := First (Constraints (Cstr));
4717 while Present (One_Cstr) loop
4718
4719 -- Index or discriminant constraint in SPARK must be a
4720 -- subtype mark.
4721
4722 if not
4723 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4724 then
4725 Check_SPARK_05_Restriction
4726 ("subtype mark required", One_Cstr);
4727
4728 -- String subtype must have a lower bound of 1 in SPARK.
4729 -- Note that we do not need to test for the non-static case
4730 -- here, since that was already taken care of in
4731 -- Process_Range_Expr_In_Decl.
4732
4733 elsif Base_Type (T) = Standard_String then
4734 Get_Index_Bounds (One_Cstr, Low, High);
4735
4736 if Is_OK_Static_Expression (Low)
4737 and then Expr_Value (Low) /= 1
4738 then
4739 Check_SPARK_05_Restriction
4740 ("String subtype must have lower bound of 1", N);
4741 end if;
4742 end if;
4743
4744 Next (One_Cstr);
4745 end loop;
4746 end if;
4747 end;
4748 end if;
4749
4750 -- In the case where there is no constraint given in the subtype
4751 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4752 -- semantic attributes must be established here.
4753
4754 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4755 Set_Etype (Id, Base_Type (T));
4756
4757 -- Subtype of unconstrained array without constraint is not allowed
4758 -- in SPARK.
4759
4760 if Is_Array_Type (T) and then not Is_Constrained (T) then
4761 Check_SPARK_05_Restriction
4762 ("subtype of unconstrained array must have constraint", N);
4763 end if;
4764
4765 case Ekind (T) is
4766 when Array_Kind =>
4767 Set_Ekind (Id, E_Array_Subtype);
4768 Copy_Array_Subtype_Attributes (Id, T);
4769
4770 when Decimal_Fixed_Point_Kind =>
4771 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4772 Set_Digits_Value (Id, Digits_Value (T));
4773 Set_Delta_Value (Id, Delta_Value (T));
4774 Set_Scale_Value (Id, Scale_Value (T));
4775 Set_Small_Value (Id, Small_Value (T));
4776 Set_Scalar_Range (Id, Scalar_Range (T));
4777 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4778 Set_Is_Constrained (Id, Is_Constrained (T));
4779 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4780 Set_RM_Size (Id, RM_Size (T));
4781
4782 when Enumeration_Kind =>
4783 Set_Ekind (Id, E_Enumeration_Subtype);
4784 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4785 Set_Scalar_Range (Id, Scalar_Range (T));
4786 Set_Is_Character_Type (Id, Is_Character_Type (T));
4787 Set_Is_Constrained (Id, Is_Constrained (T));
4788 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4789 Set_RM_Size (Id, RM_Size (T));
4790 Inherit_Predicate_Flags (Id, T);
4791
4792 when Ordinary_Fixed_Point_Kind =>
4793 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4794 Set_Scalar_Range (Id, Scalar_Range (T));
4795 Set_Small_Value (Id, Small_Value (T));
4796 Set_Delta_Value (Id, Delta_Value (T));
4797 Set_Is_Constrained (Id, Is_Constrained (T));
4798 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4799 Set_RM_Size (Id, RM_Size (T));
4800
4801 when Float_Kind =>
4802 Set_Ekind (Id, E_Floating_Point_Subtype);
4803 Set_Scalar_Range (Id, Scalar_Range (T));
4804 Set_Digits_Value (Id, Digits_Value (T));
4805 Set_Is_Constrained (Id, Is_Constrained (T));
4806
4807 when Signed_Integer_Kind =>
4808 Set_Ekind (Id, E_Signed_Integer_Subtype);
4809 Set_Scalar_Range (Id, Scalar_Range (T));
4810 Set_Is_Constrained (Id, Is_Constrained (T));
4811 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4812 Set_RM_Size (Id, RM_Size (T));
4813 Inherit_Predicate_Flags (Id, T);
4814
4815 when Modular_Integer_Kind =>
4816 Set_Ekind (Id, E_Modular_Integer_Subtype);
4817 Set_Scalar_Range (Id, Scalar_Range (T));
4818 Set_Is_Constrained (Id, Is_Constrained (T));
4819 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4820 Set_RM_Size (Id, RM_Size (T));
4821 Inherit_Predicate_Flags (Id, T);
4822
4823 when Class_Wide_Kind =>
4824 Set_Ekind (Id, E_Class_Wide_Subtype);
4825 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4826 Set_Cloned_Subtype (Id, T);
4827 Set_Is_Tagged_Type (Id, True);
4828 Set_Has_Unknown_Discriminants
4829 (Id, True);
4830 Set_No_Tagged_Streams_Pragma
4831 (Id, No_Tagged_Streams_Pragma (T));
4832
4833 if Ekind (T) = E_Class_Wide_Subtype then
4834 Set_Equivalent_Type (Id, Equivalent_Type (T));
4835 end if;
4836
4837 when E_Record_Type | E_Record_Subtype =>
4838 Set_Ekind (Id, E_Record_Subtype);
4839
4840 if Ekind (T) = E_Record_Subtype
4841 and then Present (Cloned_Subtype (T))
4842 then
4843 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4844 else
4845 Set_Cloned_Subtype (Id, T);
4846 end if;
4847
4848 Set_First_Entity (Id, First_Entity (T));
4849 Set_Last_Entity (Id, Last_Entity (T));
4850 Set_Has_Discriminants (Id, Has_Discriminants (T));
4851 Set_Is_Constrained (Id, Is_Constrained (T));
4852 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4853 Set_Has_Implicit_Dereference
4854 (Id, Has_Implicit_Dereference (T));
4855 Set_Has_Unknown_Discriminants
4856 (Id, Has_Unknown_Discriminants (T));
4857
4858 if Has_Discriminants (T) then
4859 Set_Discriminant_Constraint
4860 (Id, Discriminant_Constraint (T));
4861 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4862
4863 elsif Has_Unknown_Discriminants (Id) then
4864 Set_Discriminant_Constraint (Id, No_Elist);
4865 end if;
4866
4867 if Is_Tagged_Type (T) then
4868 Set_Is_Tagged_Type (Id, True);
4869 Set_No_Tagged_Streams_Pragma
4870 (Id, No_Tagged_Streams_Pragma (T));
4871 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4872 Set_Direct_Primitive_Operations
4873 (Id, Direct_Primitive_Operations (T));
4874 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4875
4876 if Is_Interface (T) then
4877 Set_Is_Interface (Id);
4878 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4879 end if;
4880 end if;
4881
4882 when Private_Kind =>
4883 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4884 Set_Has_Discriminants (Id, Has_Discriminants (T));
4885 Set_Is_Constrained (Id, Is_Constrained (T));
4886 Set_First_Entity (Id, First_Entity (T));
4887 Set_Last_Entity (Id, Last_Entity (T));
4888 Set_Private_Dependents (Id, New_Elmt_List);
4889 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4890 Set_Has_Implicit_Dereference
4891 (Id, Has_Implicit_Dereference (T));
4892 Set_Has_Unknown_Discriminants
4893 (Id, Has_Unknown_Discriminants (T));
4894 Set_Known_To_Have_Preelab_Init
4895 (Id, Known_To_Have_Preelab_Init (T));
4896
4897 if Is_Tagged_Type (T) then
4898 Set_Is_Tagged_Type (Id);
4899 Set_No_Tagged_Streams_Pragma (Id,
4900 No_Tagged_Streams_Pragma (T));
4901 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4902 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4903 Set_Direct_Primitive_Operations (Id,
4904 Direct_Primitive_Operations (T));
4905 end if;
4906
4907 -- In general the attributes of the subtype of a private type
4908 -- are the attributes of the partial view of parent. However,
4909 -- the full view may be a discriminated type, and the subtype
4910 -- must share the discriminant constraint to generate correct
4911 -- calls to initialization procedures.
4912
4913 if Has_Discriminants (T) then
4914 Set_Discriminant_Constraint
4915 (Id, Discriminant_Constraint (T));
4916 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4917
4918 elsif Present (Full_View (T))
4919 and then Has_Discriminants (Full_View (T))
4920 then
4921 Set_Discriminant_Constraint
4922 (Id, Discriminant_Constraint (Full_View (T)));
4923 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4924
4925 -- This would seem semantically correct, but apparently
4926 -- generates spurious errors about missing components ???
4927
4928 -- Set_Has_Discriminants (Id);
4929 end if;
4930
4931 Prepare_Private_Subtype_Completion (Id, N);
4932
4933 -- If this is the subtype of a constrained private type with
4934 -- discriminants that has got a full view and we also have
4935 -- built a completion just above, show that the completion
4936 -- is a clone of the full view to the back-end.
4937
4938 if Has_Discriminants (T)
4939 and then not Has_Unknown_Discriminants (T)
4940 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
4941 and then Present (Full_View (T))
4942 and then Present (Full_View (Id))
4943 then
4944 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
4945 end if;
4946
4947 when Access_Kind =>
4948 Set_Ekind (Id, E_Access_Subtype);
4949 Set_Is_Constrained (Id, Is_Constrained (T));
4950 Set_Is_Access_Constant
4951 (Id, Is_Access_Constant (T));
4952 Set_Directly_Designated_Type
4953 (Id, Designated_Type (T));
4954 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4955
4956 -- A Pure library_item must not contain the declaration of a
4957 -- named access type, except within a subprogram, generic
4958 -- subprogram, task unit, or protected unit, or if it has
4959 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4960
4961 if Comes_From_Source (Id)
4962 and then In_Pure_Unit
4963 and then not In_Subprogram_Task_Protected_Unit
4964 and then not No_Pool_Assigned (Id)
4965 then
4966 Error_Msg_N
4967 ("named access types not allowed in pure unit", N);
4968 end if;
4969
4970 when Concurrent_Kind =>
4971 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4972 Set_Corresponding_Record_Type (Id,
4973 Corresponding_Record_Type (T));
4974 Set_First_Entity (Id, First_Entity (T));
4975 Set_First_Private_Entity (Id, First_Private_Entity (T));
4976 Set_Has_Discriminants (Id, Has_Discriminants (T));
4977 Set_Is_Constrained (Id, Is_Constrained (T));
4978 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4979 Set_Last_Entity (Id, Last_Entity (T));
4980
4981 if Is_Tagged_Type (T) then
4982 Set_No_Tagged_Streams_Pragma
4983 (Id, No_Tagged_Streams_Pragma (T));
4984 end if;
4985
4986 if Has_Discriminants (T) then
4987 Set_Discriminant_Constraint
4988 (Id, Discriminant_Constraint (T));
4989 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4990 end if;
4991
4992 when Incomplete_Kind =>
4993 if Ada_Version >= Ada_2005 then
4994
4995 -- In Ada 2005 an incomplete type can be explicitly tagged:
4996 -- propagate indication. Note that we also have to include
4997 -- subtypes for Ada 2012 extended use of incomplete types.
4998
4999 Set_Ekind (Id, E_Incomplete_Subtype);
5000 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5001 Set_Private_Dependents (Id, New_Elmt_List);
5002
5003 if Is_Tagged_Type (Id) then
5004 Set_No_Tagged_Streams_Pragma
5005 (Id, No_Tagged_Streams_Pragma (T));
5006 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5007 end if;
5008
5009 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5010 -- incomplete type visible through a limited with clause.
5011
5012 if From_Limited_With (T)
5013 and then Present (Non_Limited_View (T))
5014 then
5015 Set_From_Limited_With (Id);
5016 Set_Non_Limited_View (Id, Non_Limited_View (T));
5017
5018 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5019 -- to the private dependents of the original incomplete
5020 -- type for future transformation.
5021
5022 else
5023 Append_Elmt (Id, Private_Dependents (T));
5024 end if;
5025
5026 -- If the subtype name denotes an incomplete type an error
5027 -- was already reported by Process_Subtype.
5028
5029 else
5030 Set_Etype (Id, Any_Type);
5031 end if;
5032
5033 when others =>
5034 raise Program_Error;
5035 end case;
5036 end if;
5037
5038 if Etype (Id) = Any_Type then
5039 goto Leave;
5040 end if;
5041
5042 -- Some common processing on all types
5043
5044 Set_Size_Info (Id, T);
5045 Set_First_Rep_Item (Id, First_Rep_Item (T));
5046
5047 -- If the parent type is a generic actual, so is the subtype. This may
5048 -- happen in a nested instance. Why Comes_From_Source test???
5049
5050 if not Comes_From_Source (N) then
5051 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5052 end if;
5053
5054 T := Etype (Id);
5055
5056 Set_Is_Immediately_Visible (Id, True);
5057 Set_Depends_On_Private (Id, Has_Private_Component (T));
5058 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
5059
5060 if Is_Interface (T) then
5061 Set_Is_Interface (Id);
5062 end if;
5063
5064 if Present (Generic_Parent_Type (N))
5065 and then
5066 (Nkind (Parent (Generic_Parent_Type (N))) /=
5067 N_Formal_Type_Declaration
5068 or else Nkind (Formal_Type_Definition
5069 (Parent (Generic_Parent_Type (N)))) /=
5070 N_Formal_Private_Type_Definition)
5071 then
5072 if Is_Tagged_Type (Id) then
5073
5074 -- If this is a generic actual subtype for a synchronized type,
5075 -- the primitive operations are those of the corresponding record
5076 -- for which there is a separate subtype declaration.
5077
5078 if Is_Concurrent_Type (Id) then
5079 null;
5080 elsif Is_Class_Wide_Type (Id) then
5081 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5082 else
5083 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5084 end if;
5085
5086 elsif Scope (Etype (Id)) /= Standard_Standard then
5087 Derive_Subprograms (Generic_Parent_Type (N), Id);
5088 end if;
5089 end if;
5090
5091 if Is_Private_Type (T) and then Present (Full_View (T)) then
5092 Conditional_Delay (Id, Full_View (T));
5093
5094 -- The subtypes of components or subcomponents of protected types
5095 -- do not need freeze nodes, which would otherwise appear in the
5096 -- wrong scope (before the freeze node for the protected type). The
5097 -- proper subtypes are those of the subcomponents of the corresponding
5098 -- record.
5099
5100 elsif Ekind (Scope (Id)) /= E_Protected_Type
5101 and then Present (Scope (Scope (Id))) -- error defense
5102 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5103 then
5104 Conditional_Delay (Id, T);
5105 end if;
5106
5107 -- Check that Constraint_Error is raised for a scalar subtype indication
5108 -- when the lower or upper bound of a non-null range lies outside the
5109 -- range of the type mark.
5110
5111 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5112 if Is_Scalar_Type (Etype (Id))
5113 and then Scalar_Range (Id) /=
5114 Scalar_Range (Etype (Subtype_Mark
5115 (Subtype_Indication (N))))
5116 then
5117 Apply_Range_Check
5118 (Scalar_Range (Id),
5119 Etype (Subtype_Mark (Subtype_Indication (N))));
5120
5121 -- In the array case, check compatibility for each index
5122
5123 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5124 then
5125 -- This really should be a subprogram that finds the indications
5126 -- to check???
5127
5128 declare
5129 Subt_Index : Node_Id := First_Index (Id);
5130 Target_Index : Node_Id :=
5131 First_Index (Etype
5132 (Subtype_Mark (Subtype_Indication (N))));
5133 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5134
5135 begin
5136 while Present (Subt_Index) loop
5137 if ((Nkind (Subt_Index) = N_Identifier
5138 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5139 or else Nkind (Subt_Index) = N_Subtype_Indication)
5140 and then
5141 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5142 then
5143 declare
5144 Target_Typ : constant Entity_Id :=
5145 Etype (Target_Index);
5146 begin
5147 R_Checks :=
5148 Get_Range_Checks
5149 (Scalar_Range (Etype (Subt_Index)),
5150 Target_Typ,
5151 Etype (Subt_Index),
5152 Defining_Identifier (N));
5153
5154 -- Reset Has_Dynamic_Range_Check on the subtype to
5155 -- prevent elision of the index check due to a dynamic
5156 -- check generated for a preceding index (needed since
5157 -- Insert_Range_Checks tries to avoid generating
5158 -- redundant checks on a given declaration).
5159
5160 Set_Has_Dynamic_Range_Check (N, False);
5161
5162 Insert_Range_Checks
5163 (R_Checks,
5164 N,
5165 Target_Typ,
5166 Sloc (Defining_Identifier (N)));
5167
5168 -- Record whether this index involved a dynamic check
5169
5170 Has_Dyn_Chk :=
5171 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5172 end;
5173 end if;
5174
5175 Next_Index (Subt_Index);
5176 Next_Index (Target_Index);
5177 end loop;
5178
5179 -- Finally, mark whether the subtype involves dynamic checks
5180
5181 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5182 end;
5183 end if;
5184 end if;
5185
5186 -- A type invariant applies to any subtype in its scope, in particular
5187 -- to a generic actual.
5188
5189 if Has_Invariants (T) and then In_Open_Scopes (Scope (T)) then
5190 Set_Has_Invariants (Id);
5191 Set_Invariant_Procedure (Id, Invariant_Procedure (T));
5192 end if;
5193
5194 -- Make sure that generic actual types are properly frozen. The subtype
5195 -- is marked as a generic actual type when the enclosing instance is
5196 -- analyzed, so here we identify the subtype from the tree structure.
5197
5198 if Expander_Active
5199 and then Is_Generic_Actual_Type (Id)
5200 and then In_Instance
5201 and then not Comes_From_Source (N)
5202 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
5203 and then Is_Frozen (T)
5204 then
5205 Freeze_Before (N, Id);
5206 end if;
5207
5208 Set_Optimize_Alignment_Flags (Id);
5209 Check_Eliminated (Id);
5210
5211 <<Leave>>
5212 if Has_Aspects (N) then
5213 Analyze_Aspect_Specifications (N, Id);
5214 end if;
5215
5216 Analyze_Dimension (N);
5217 end Analyze_Subtype_Declaration;
5218
5219 --------------------------------
5220 -- Analyze_Subtype_Indication --
5221 --------------------------------
5222
5223 procedure Analyze_Subtype_Indication (N : Node_Id) is
5224 T : constant Entity_Id := Subtype_Mark (N);
5225 R : constant Node_Id := Range_Expression (Constraint (N));
5226
5227 begin
5228 Analyze (T);
5229
5230 if R /= Error then
5231 Analyze (R);
5232 Set_Etype (N, Etype (R));
5233 Resolve (R, Entity (T));
5234 else
5235 Set_Error_Posted (R);
5236 Set_Error_Posted (T);
5237 end if;
5238 end Analyze_Subtype_Indication;
5239
5240 --------------------------
5241 -- Analyze_Variant_Part --
5242 --------------------------
5243
5244 procedure Analyze_Variant_Part (N : Node_Id) is
5245 Discr_Name : Node_Id;
5246 Discr_Type : Entity_Id;
5247
5248 procedure Process_Variant (A : Node_Id);
5249 -- Analyze declarations for a single variant
5250
5251 package Analyze_Variant_Choices is
5252 new Generic_Analyze_Choices (Process_Variant);
5253 use Analyze_Variant_Choices;
5254
5255 ---------------------
5256 -- Process_Variant --
5257 ---------------------
5258
5259 procedure Process_Variant (A : Node_Id) is
5260 CL : constant Node_Id := Component_List (A);
5261 begin
5262 if not Null_Present (CL) then
5263 Analyze_Declarations (Component_Items (CL));
5264
5265 if Present (Variant_Part (CL)) then
5266 Analyze (Variant_Part (CL));
5267 end if;
5268 end if;
5269 end Process_Variant;
5270
5271 -- Start of processing for Analyze_Variant_Part
5272
5273 begin
5274 Discr_Name := Name (N);
5275 Analyze (Discr_Name);
5276
5277 -- If Discr_Name bad, get out (prevent cascaded errors)
5278
5279 if Etype (Discr_Name) = Any_Type then
5280 return;
5281 end if;
5282
5283 -- Check invalid discriminant in variant part
5284
5285 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5286 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5287 end if;
5288
5289 Discr_Type := Etype (Entity (Discr_Name));
5290
5291 if not Is_Discrete_Type (Discr_Type) then
5292 Error_Msg_N
5293 ("discriminant in a variant part must be of a discrete type",
5294 Name (N));
5295 return;
5296 end if;
5297
5298 -- Now analyze the choices, which also analyzes the declarations that
5299 -- are associated with each choice.
5300
5301 Analyze_Choices (Variants (N), Discr_Type);
5302
5303 -- Note: we used to instantiate and call Check_Choices here to check
5304 -- that the choices covered the discriminant, but it's too early to do
5305 -- that because of statically predicated subtypes, whose analysis may
5306 -- be deferred to their freeze point which may be as late as the freeze
5307 -- point of the containing record. So this call is now to be found in
5308 -- Freeze_Record_Declaration.
5309
5310 end Analyze_Variant_Part;
5311
5312 ----------------------------
5313 -- Array_Type_Declaration --
5314 ----------------------------
5315
5316 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5317 Component_Def : constant Node_Id := Component_Definition (Def);
5318 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5319 Element_Type : Entity_Id;
5320 Implicit_Base : Entity_Id;
5321 Index : Node_Id;
5322 Related_Id : Entity_Id := Empty;
5323 Nb_Index : Nat;
5324 P : constant Node_Id := Parent (Def);
5325 Priv : Entity_Id;
5326
5327 begin
5328 if Nkind (Def) = N_Constrained_Array_Definition then
5329 Index := First (Discrete_Subtype_Definitions (Def));
5330 else
5331 Index := First (Subtype_Marks (Def));
5332 end if;
5333
5334 -- Find proper names for the implicit types which may be public. In case
5335 -- of anonymous arrays we use the name of the first object of that type
5336 -- as prefix.
5337
5338 if No (T) then
5339 Related_Id := Defining_Identifier (P);
5340 else
5341 Related_Id := T;
5342 end if;
5343
5344 Nb_Index := 1;
5345 while Present (Index) loop
5346 Analyze (Index);
5347
5348 -- Test for odd case of trying to index a type by the type itself
5349
5350 if Is_Entity_Name (Index) and then Entity (Index) = T then
5351 Error_Msg_N ("type& cannot be indexed by itself", Index);
5352 Set_Entity (Index, Standard_Boolean);
5353 Set_Etype (Index, Standard_Boolean);
5354 end if;
5355
5356 -- Check SPARK restriction requiring a subtype mark
5357
5358 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5359 Check_SPARK_05_Restriction ("subtype mark required", Index);
5360 end if;
5361
5362 -- Add a subtype declaration for each index of private array type
5363 -- declaration whose etype is also private. For example:
5364
5365 -- package Pkg is
5366 -- type Index is private;
5367 -- private
5368 -- type Table is array (Index) of ...
5369 -- end;
5370
5371 -- This is currently required by the expander for the internally
5372 -- generated equality subprogram of records with variant parts in
5373 -- which the etype of some component is such private type.
5374
5375 if Ekind (Current_Scope) = E_Package
5376 and then In_Private_Part (Current_Scope)
5377 and then Has_Private_Declaration (Etype (Index))
5378 then
5379 declare
5380 Loc : constant Source_Ptr := Sloc (Def);
5381 New_E : Entity_Id;
5382 Decl : Entity_Id;
5383
5384 begin
5385 New_E := Make_Temporary (Loc, 'T');
5386 Set_Is_Internal (New_E);
5387
5388 Decl :=
5389 Make_Subtype_Declaration (Loc,
5390 Defining_Identifier => New_E,
5391 Subtype_Indication =>
5392 New_Occurrence_Of (Etype (Index), Loc));
5393
5394 Insert_Before (Parent (Def), Decl);
5395 Analyze (Decl);
5396 Set_Etype (Index, New_E);
5397
5398 -- If the index is a range the Entity attribute is not
5399 -- available. Example:
5400
5401 -- package Pkg is
5402 -- type T is private;
5403 -- private
5404 -- type T is new Natural;
5405 -- Table : array (T(1) .. T(10)) of Boolean;
5406 -- end Pkg;
5407
5408 if Nkind (Index) /= N_Range then
5409 Set_Entity (Index, New_E);
5410 end if;
5411 end;
5412 end if;
5413
5414 Make_Index (Index, P, Related_Id, Nb_Index);
5415
5416 -- Check error of subtype with predicate for index type
5417
5418 Bad_Predicated_Subtype_Use
5419 ("subtype& has predicate, not allowed as index subtype",
5420 Index, Etype (Index));
5421
5422 -- Move to next index
5423
5424 Next_Index (Index);
5425 Nb_Index := Nb_Index + 1;
5426 end loop;
5427
5428 -- Process subtype indication if one is present
5429
5430 if Present (Component_Typ) then
5431 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5432
5433 Set_Etype (Component_Typ, Element_Type);
5434
5435 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5436 Check_SPARK_05_Restriction
5437 ("subtype mark required", Component_Typ);
5438 end if;
5439
5440 -- Ada 2005 (AI-230): Access Definition case
5441
5442 else pragma Assert (Present (Access_Definition (Component_Def)));
5443
5444 -- Indicate that the anonymous access type is created by the
5445 -- array type declaration.
5446
5447 Element_Type := Access_Definition
5448 (Related_Nod => P,
5449 N => Access_Definition (Component_Def));
5450 Set_Is_Local_Anonymous_Access (Element_Type);
5451
5452 -- Propagate the parent. This field is needed if we have to generate
5453 -- the master_id associated with an anonymous access to task type
5454 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5455
5456 Set_Parent (Element_Type, Parent (T));
5457
5458 -- Ada 2005 (AI-230): In case of components that are anonymous access
5459 -- types the level of accessibility depends on the enclosing type
5460 -- declaration
5461
5462 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5463
5464 -- Ada 2005 (AI-254)
5465
5466 declare
5467 CD : constant Node_Id :=
5468 Access_To_Subprogram_Definition
5469 (Access_Definition (Component_Def));
5470 begin
5471 if Present (CD) and then Protected_Present (CD) then
5472 Element_Type :=
5473 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5474 end if;
5475 end;
5476 end if;
5477
5478 -- Constrained array case
5479
5480 if No (T) then
5481 T := Create_Itype (E_Void, P, Related_Id, 'T');
5482 end if;
5483
5484 if Nkind (Def) = N_Constrained_Array_Definition then
5485
5486 -- Establish Implicit_Base as unconstrained base type
5487
5488 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5489
5490 Set_Etype (Implicit_Base, Implicit_Base);
5491 Set_Scope (Implicit_Base, Current_Scope);
5492 Set_Has_Delayed_Freeze (Implicit_Base);
5493 Set_Default_SSO (Implicit_Base);
5494
5495 -- The constrained array type is a subtype of the unconstrained one
5496
5497 Set_Ekind (T, E_Array_Subtype);
5498 Init_Size_Align (T);
5499 Set_Etype (T, Implicit_Base);
5500 Set_Scope (T, Current_Scope);
5501 Set_Is_Constrained (T);
5502 Set_First_Index (T,
5503 First (Discrete_Subtype_Definitions (Def)));
5504 Set_Has_Delayed_Freeze (T);
5505
5506 -- Complete setup of implicit base type
5507
5508 Set_First_Index (Implicit_Base, First_Index (T));
5509 Set_Component_Type (Implicit_Base, Element_Type);
5510 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
5511 Set_Has_Protected (Implicit_Base, Has_Protected (Element_Type));
5512 Set_Component_Size (Implicit_Base, Uint_0);
5513 Set_Packed_Array_Impl_Type (Implicit_Base, Empty);
5514 Set_Has_Controlled_Component (Implicit_Base,
5515 Has_Controlled_Component (Element_Type)
5516 or else Is_Controlled (Element_Type));
5517 Set_Finalize_Storage_Only (Implicit_Base,
5518 Finalize_Storage_Only (Element_Type));
5519
5520 -- Inherit the "ghostness" from the constrained array type
5521
5522 if Is_Ghost_Entity (T) or else Ghost_Mode > None then
5523 Set_Is_Ghost_Entity (Implicit_Base);
5524 end if;
5525
5526 -- Unconstrained array case
5527
5528 else
5529 Set_Ekind (T, E_Array_Type);
5530 Init_Size_Align (T);
5531 Set_Etype (T, T);
5532 Set_Scope (T, Current_Scope);
5533 Set_Component_Size (T, Uint_0);
5534 Set_Is_Constrained (T, False);
5535 Set_First_Index (T, First (Subtype_Marks (Def)));
5536 Set_Has_Delayed_Freeze (T, True);
5537 Set_Has_Task (T, Has_Task (Element_Type));
5538 Set_Has_Protected (T, Has_Protected (Element_Type));
5539 Set_Has_Controlled_Component (T, Has_Controlled_Component
5540 (Element_Type)
5541 or else
5542 Is_Controlled (Element_Type));
5543 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5544 (Element_Type));
5545 Set_Default_SSO (T);
5546 end if;
5547
5548 -- Common attributes for both cases
5549
5550 Set_Component_Type (Base_Type (T), Element_Type);
5551 Set_Packed_Array_Impl_Type (T, Empty);
5552
5553 if Aliased_Present (Component_Definition (Def)) then
5554 Check_SPARK_05_Restriction
5555 ("aliased is not allowed", Component_Definition (Def));
5556 Set_Has_Aliased_Components (Etype (T));
5557 end if;
5558
5559 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5560 -- array type to ensure that objects of this type are initialized.
5561
5562 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5563 Set_Can_Never_Be_Null (T);
5564
5565 if Null_Exclusion_Present (Component_Definition (Def))
5566
5567 -- No need to check itypes because in their case this check was
5568 -- done at their point of creation
5569
5570 and then not Is_Itype (Element_Type)
5571 then
5572 Error_Msg_N
5573 ("`NOT NULL` not allowed (null already excluded)",
5574 Subtype_Indication (Component_Definition (Def)));
5575 end if;
5576 end if;
5577
5578 Priv := Private_Component (Element_Type);
5579
5580 if Present (Priv) then
5581
5582 -- Check for circular definitions
5583
5584 if Priv = Any_Type then
5585 Set_Component_Type (Etype (T), Any_Type);
5586
5587 -- There is a gap in the visibility of operations on the composite
5588 -- type only if the component type is defined in a different scope.
5589
5590 elsif Scope (Priv) = Current_Scope then
5591 null;
5592
5593 elsif Is_Limited_Type (Priv) then
5594 Set_Is_Limited_Composite (Etype (T));
5595 Set_Is_Limited_Composite (T);
5596 else
5597 Set_Is_Private_Composite (Etype (T));
5598 Set_Is_Private_Composite (T);
5599 end if;
5600 end if;
5601
5602 -- A syntax error in the declaration itself may lead to an empty index
5603 -- list, in which case do a minimal patch.
5604
5605 if No (First_Index (T)) then
5606 Error_Msg_N ("missing index definition in array type declaration", T);
5607
5608 declare
5609 Indexes : constant List_Id :=
5610 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5611 begin
5612 Set_Discrete_Subtype_Definitions (Def, Indexes);
5613 Set_First_Index (T, First (Indexes));
5614 return;
5615 end;
5616 end if;
5617
5618 -- Create a concatenation operator for the new type. Internal array
5619 -- types created for packed entities do not need such, they are
5620 -- compatible with the user-defined type.
5621
5622 if Number_Dimensions (T) = 1
5623 and then not Is_Packed_Array_Impl_Type (T)
5624 then
5625 New_Concatenation_Op (T);
5626 end if;
5627
5628 -- In the case of an unconstrained array the parser has already verified
5629 -- that all the indexes are unconstrained but we still need to make sure
5630 -- that the element type is constrained.
5631
5632 if Is_Indefinite_Subtype (Element_Type) then
5633 Error_Msg_N
5634 ("unconstrained element type in array declaration",
5635 Subtype_Indication (Component_Def));
5636
5637 elsif Is_Abstract_Type (Element_Type) then
5638 Error_Msg_N
5639 ("the type of a component cannot be abstract",
5640 Subtype_Indication (Component_Def));
5641 end if;
5642
5643 -- There may be an invariant declared for the component type, but
5644 -- the construction of the component invariant checking procedure
5645 -- takes place during expansion.
5646 end Array_Type_Declaration;
5647
5648 ------------------------------------------------------
5649 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5650 ------------------------------------------------------
5651
5652 function Replace_Anonymous_Access_To_Protected_Subprogram
5653 (N : Node_Id) return Entity_Id
5654 is
5655 Loc : constant Source_Ptr := Sloc (N);
5656
5657 Curr_Scope : constant Scope_Stack_Entry :=
5658 Scope_Stack.Table (Scope_Stack.Last);
5659
5660 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5661
5662 Acc : Node_Id;
5663 -- Access definition in declaration
5664
5665 Comp : Node_Id;
5666 -- Object definition or formal definition with an access definition
5667
5668 Decl : Node_Id;
5669 -- Declaration of anonymous access to subprogram type
5670
5671 Spec : Node_Id;
5672 -- Original specification in access to subprogram
5673
5674 P : Node_Id;
5675
5676 begin
5677 Set_Is_Internal (Anon);
5678
5679 case Nkind (N) is
5680 when N_Component_Declaration |
5681 N_Unconstrained_Array_Definition |
5682 N_Constrained_Array_Definition =>
5683 Comp := Component_Definition (N);
5684 Acc := Access_Definition (Comp);
5685
5686 when N_Discriminant_Specification =>
5687 Comp := Discriminant_Type (N);
5688 Acc := Comp;
5689
5690 when N_Parameter_Specification =>
5691 Comp := Parameter_Type (N);
5692 Acc := Comp;
5693
5694 when N_Access_Function_Definition =>
5695 Comp := Result_Definition (N);
5696 Acc := Comp;
5697
5698 when N_Object_Declaration =>
5699 Comp := Object_Definition (N);
5700 Acc := Comp;
5701
5702 when N_Function_Specification =>
5703 Comp := Result_Definition (N);
5704 Acc := Comp;
5705
5706 when others =>
5707 raise Program_Error;
5708 end case;
5709
5710 Spec := Access_To_Subprogram_Definition (Acc);
5711
5712 Decl :=
5713 Make_Full_Type_Declaration (Loc,
5714 Defining_Identifier => Anon,
5715 Type_Definition => Copy_Separate_Tree (Spec));
5716
5717 Mark_Rewrite_Insertion (Decl);
5718
5719 -- In ASIS mode, analyze the profile on the original node, because
5720 -- the separate copy does not provide enough links to recover the
5721 -- original tree. Analysis is limited to type annotations, within
5722 -- a temporary scope that serves as an anonymous subprogram to collect
5723 -- otherwise useless temporaries and itypes.
5724
5725 if ASIS_Mode then
5726 declare
5727 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5728
5729 begin
5730 if Nkind (Spec) = N_Access_Function_Definition then
5731 Set_Ekind (Typ, E_Function);
5732 else
5733 Set_Ekind (Typ, E_Procedure);
5734 end if;
5735
5736 Set_Parent (Typ, N);
5737 Set_Scope (Typ, Current_Scope);
5738 Push_Scope (Typ);
5739
5740 Process_Formals (Parameter_Specifications (Spec), Spec);
5741
5742 if Nkind (Spec) = N_Access_Function_Definition then
5743 declare
5744 Def : constant Node_Id := Result_Definition (Spec);
5745
5746 begin
5747 -- The result might itself be an anonymous access type, so
5748 -- have to recurse.
5749
5750 if Nkind (Def) = N_Access_Definition then
5751 if Present (Access_To_Subprogram_Definition (Def)) then
5752 Set_Etype
5753 (Def,
5754 Replace_Anonymous_Access_To_Protected_Subprogram
5755 (Spec));
5756 else
5757 Find_Type (Subtype_Mark (Def));
5758 end if;
5759
5760 else
5761 Find_Type (Def);
5762 end if;
5763 end;
5764 end if;
5765
5766 End_Scope;
5767 end;
5768 end if;
5769
5770 -- Insert the new declaration in the nearest enclosing scope. If the
5771 -- node is a body and N is its return type, the declaration belongs in
5772 -- the enclosing scope.
5773
5774 P := Parent (N);
5775
5776 if Nkind (P) = N_Subprogram_Body
5777 and then Nkind (N) = N_Function_Specification
5778 then
5779 P := Parent (P);
5780 end if;
5781
5782 while Present (P) and then not Has_Declarations (P) loop
5783 P := Parent (P);
5784 end loop;
5785
5786 pragma Assert (Present (P));
5787
5788 if Nkind (P) = N_Package_Specification then
5789 Prepend (Decl, Visible_Declarations (P));
5790 else
5791 Prepend (Decl, Declarations (P));
5792 end if;
5793
5794 -- Replace the anonymous type with an occurrence of the new declaration.
5795 -- In all cases the rewritten node does not have the null-exclusion
5796 -- attribute because (if present) it was already inherited by the
5797 -- anonymous entity (Anon). Thus, in case of components we do not
5798 -- inherit this attribute.
5799
5800 if Nkind (N) = N_Parameter_Specification then
5801 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5802 Set_Etype (Defining_Identifier (N), Anon);
5803 Set_Null_Exclusion_Present (N, False);
5804
5805 elsif Nkind (N) = N_Object_Declaration then
5806 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5807 Set_Etype (Defining_Identifier (N), Anon);
5808
5809 elsif Nkind (N) = N_Access_Function_Definition then
5810 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5811
5812 elsif Nkind (N) = N_Function_Specification then
5813 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5814 Set_Etype (Defining_Unit_Name (N), Anon);
5815
5816 else
5817 Rewrite (Comp,
5818 Make_Component_Definition (Loc,
5819 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5820 end if;
5821
5822 Mark_Rewrite_Insertion (Comp);
5823
5824 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5825 Analyze (Decl);
5826
5827 else
5828 -- Temporarily remove the current scope (record or subprogram) from
5829 -- the stack to add the new declarations to the enclosing scope.
5830
5831 Scope_Stack.Decrement_Last;
5832 Analyze (Decl);
5833 Set_Is_Itype (Anon);
5834 Scope_Stack.Append (Curr_Scope);
5835 end if;
5836
5837 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5838 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5839 return Anon;
5840 end Replace_Anonymous_Access_To_Protected_Subprogram;
5841
5842 -------------------------------
5843 -- Build_Derived_Access_Type --
5844 -------------------------------
5845
5846 procedure Build_Derived_Access_Type
5847 (N : Node_Id;
5848 Parent_Type : Entity_Id;
5849 Derived_Type : Entity_Id)
5850 is
5851 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5852
5853 Desig_Type : Entity_Id;
5854 Discr : Entity_Id;
5855 Discr_Con_Elist : Elist_Id;
5856 Discr_Con_El : Elmt_Id;
5857 Subt : Entity_Id;
5858
5859 begin
5860 -- Set the designated type so it is available in case this is an access
5861 -- to a self-referential type, e.g. a standard list type with a next
5862 -- pointer. Will be reset after subtype is built.
5863
5864 Set_Directly_Designated_Type
5865 (Derived_Type, Designated_Type (Parent_Type));
5866
5867 Subt := Process_Subtype (S, N);
5868
5869 if Nkind (S) /= N_Subtype_Indication
5870 and then Subt /= Base_Type (Subt)
5871 then
5872 Set_Ekind (Derived_Type, E_Access_Subtype);
5873 end if;
5874
5875 if Ekind (Derived_Type) = E_Access_Subtype then
5876 declare
5877 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5878 Ibase : constant Entity_Id :=
5879 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5880 Svg_Chars : constant Name_Id := Chars (Ibase);
5881 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5882
5883 begin
5884 Copy_Node (Pbase, Ibase);
5885
5886 Set_Chars (Ibase, Svg_Chars);
5887 Set_Next_Entity (Ibase, Svg_Next_E);
5888 Set_Sloc (Ibase, Sloc (Derived_Type));
5889 Set_Scope (Ibase, Scope (Derived_Type));
5890 Set_Freeze_Node (Ibase, Empty);
5891 Set_Is_Frozen (Ibase, False);
5892 Set_Comes_From_Source (Ibase, False);
5893 Set_Is_First_Subtype (Ibase, False);
5894
5895 Set_Etype (Ibase, Pbase);
5896 Set_Etype (Derived_Type, Ibase);
5897 end;
5898 end if;
5899
5900 Set_Directly_Designated_Type
5901 (Derived_Type, Designated_Type (Subt));
5902
5903 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5904 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5905 Set_Size_Info (Derived_Type, Parent_Type);
5906 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5907 Set_Depends_On_Private (Derived_Type,
5908 Has_Private_Component (Derived_Type));
5909 Conditional_Delay (Derived_Type, Subt);
5910
5911 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5912 -- that it is not redundant.
5913
5914 if Null_Exclusion_Present (Type_Definition (N)) then
5915 Set_Can_Never_Be_Null (Derived_Type);
5916
5917 -- What is with the "AND THEN FALSE" here ???
5918
5919 if Can_Never_Be_Null (Parent_Type)
5920 and then False
5921 then
5922 Error_Msg_NE
5923 ("`NOT NULL` not allowed (& already excludes null)",
5924 N, Parent_Type);
5925 end if;
5926
5927 elsif Can_Never_Be_Null (Parent_Type) then
5928 Set_Can_Never_Be_Null (Derived_Type);
5929 end if;
5930
5931 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5932 -- the root type for this information.
5933
5934 -- Apply range checks to discriminants for derived record case
5935 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
5936
5937 Desig_Type := Designated_Type (Derived_Type);
5938 if Is_Composite_Type (Desig_Type)
5939 and then (not Is_Array_Type (Desig_Type))
5940 and then Has_Discriminants (Desig_Type)
5941 and then Base_Type (Desig_Type) /= Desig_Type
5942 then
5943 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
5944 Discr_Con_El := First_Elmt (Discr_Con_Elist);
5945
5946 Discr := First_Discriminant (Base_Type (Desig_Type));
5947 while Present (Discr_Con_El) loop
5948 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
5949 Next_Elmt (Discr_Con_El);
5950 Next_Discriminant (Discr);
5951 end loop;
5952 end if;
5953 end Build_Derived_Access_Type;
5954
5955 ------------------------------
5956 -- Build_Derived_Array_Type --
5957 ------------------------------
5958
5959 procedure Build_Derived_Array_Type
5960 (N : Node_Id;
5961 Parent_Type : Entity_Id;
5962 Derived_Type : Entity_Id)
5963 is
5964 Loc : constant Source_Ptr := Sloc (N);
5965 Tdef : constant Node_Id := Type_Definition (N);
5966 Indic : constant Node_Id := Subtype_Indication (Tdef);
5967 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5968 Implicit_Base : Entity_Id;
5969 New_Indic : Node_Id;
5970
5971 procedure Make_Implicit_Base;
5972 -- If the parent subtype is constrained, the derived type is a subtype
5973 -- of an implicit base type derived from the parent base.
5974
5975 ------------------------
5976 -- Make_Implicit_Base --
5977 ------------------------
5978
5979 procedure Make_Implicit_Base is
5980 begin
5981 Implicit_Base :=
5982 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5983
5984 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5985 Set_Etype (Implicit_Base, Parent_Base);
5986
5987 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
5988 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
5989
5990 Set_Has_Delayed_Freeze (Implicit_Base, True);
5991
5992 -- Inherit the "ghostness" from the parent base type
5993
5994 if Is_Ghost_Entity (Parent_Base) or else Ghost_Mode > None then
5995 Set_Is_Ghost_Entity (Implicit_Base);
5996 end if;
5997 end Make_Implicit_Base;
5998
5999 -- Start of processing for Build_Derived_Array_Type
6000
6001 begin
6002 if not Is_Constrained (Parent_Type) then
6003 if Nkind (Indic) /= N_Subtype_Indication then
6004 Set_Ekind (Derived_Type, E_Array_Type);
6005
6006 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6007 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6008
6009 Set_Has_Delayed_Freeze (Derived_Type, True);
6010
6011 else
6012 Make_Implicit_Base;
6013 Set_Etype (Derived_Type, Implicit_Base);
6014
6015 New_Indic :=
6016 Make_Subtype_Declaration (Loc,
6017 Defining_Identifier => Derived_Type,
6018 Subtype_Indication =>
6019 Make_Subtype_Indication (Loc,
6020 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6021 Constraint => Constraint (Indic)));
6022
6023 Rewrite (N, New_Indic);
6024 Analyze (N);
6025 end if;
6026
6027 else
6028 if Nkind (Indic) /= N_Subtype_Indication then
6029 Make_Implicit_Base;
6030
6031 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6032 Set_Etype (Derived_Type, Implicit_Base);
6033 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6034
6035 else
6036 Error_Msg_N ("illegal constraint on constrained type", Indic);
6037 end if;
6038 end if;
6039
6040 -- If parent type is not a derived type itself, and is declared in
6041 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6042 -- the new type's concatenation operator since Derive_Subprograms
6043 -- will not inherit the parent's operator. If the parent type is
6044 -- unconstrained, the operator is of the unconstrained base type.
6045
6046 if Number_Dimensions (Parent_Type) = 1
6047 and then not Is_Limited_Type (Parent_Type)
6048 and then not Is_Derived_Type (Parent_Type)
6049 and then not Is_Package_Or_Generic_Package
6050 (Scope (Base_Type (Parent_Type)))
6051 then
6052 if not Is_Constrained (Parent_Type)
6053 and then Is_Constrained (Derived_Type)
6054 then
6055 New_Concatenation_Op (Implicit_Base);
6056 else
6057 New_Concatenation_Op (Derived_Type);
6058 end if;
6059 end if;
6060 end Build_Derived_Array_Type;
6061
6062 -----------------------------------
6063 -- Build_Derived_Concurrent_Type --
6064 -----------------------------------
6065
6066 procedure Build_Derived_Concurrent_Type
6067 (N : Node_Id;
6068 Parent_Type : Entity_Id;
6069 Derived_Type : Entity_Id)
6070 is
6071 Loc : constant Source_Ptr := Sloc (N);
6072
6073 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6074 Corr_Decl : Node_Id;
6075 Corr_Decl_Needed : Boolean;
6076 -- If the derived type has fewer discriminants than its parent, the
6077 -- corresponding record is also a derived type, in order to account for
6078 -- the bound discriminants. We create a full type declaration for it in
6079 -- this case.
6080
6081 Constraint_Present : constant Boolean :=
6082 Nkind (Subtype_Indication (Type_Definition (N))) =
6083 N_Subtype_Indication;
6084
6085 D_Constraint : Node_Id;
6086 New_Constraint : Elist_Id;
6087 Old_Disc : Entity_Id;
6088 New_Disc : Entity_Id;
6089 New_N : Node_Id;
6090
6091 begin
6092 Set_Stored_Constraint (Derived_Type, No_Elist);
6093 Corr_Decl_Needed := False;
6094 Old_Disc := Empty;
6095
6096 if Present (Discriminant_Specifications (N))
6097 and then Constraint_Present
6098 then
6099 Old_Disc := First_Discriminant (Parent_Type);
6100 New_Disc := First (Discriminant_Specifications (N));
6101 while Present (New_Disc) and then Present (Old_Disc) loop
6102 Next_Discriminant (Old_Disc);
6103 Next (New_Disc);
6104 end loop;
6105 end if;
6106
6107 if Present (Old_Disc) and then Expander_Active then
6108
6109 -- The new type has fewer discriminants, so we need to create a new
6110 -- corresponding record, which is derived from the corresponding
6111 -- record of the parent, and has a stored constraint that captures
6112 -- the values of the discriminant constraints. The corresponding
6113 -- record is needed only if expander is active and code generation is
6114 -- enabled.
6115
6116 -- The type declaration for the derived corresponding record has the
6117 -- same discriminant part and constraints as the current declaration.
6118 -- Copy the unanalyzed tree to build declaration.
6119
6120 Corr_Decl_Needed := True;
6121 New_N := Copy_Separate_Tree (N);
6122
6123 Corr_Decl :=
6124 Make_Full_Type_Declaration (Loc,
6125 Defining_Identifier => Corr_Record,
6126 Discriminant_Specifications =>
6127 Discriminant_Specifications (New_N),
6128 Type_Definition =>
6129 Make_Derived_Type_Definition (Loc,
6130 Subtype_Indication =>
6131 Make_Subtype_Indication (Loc,
6132 Subtype_Mark =>
6133 New_Occurrence_Of
6134 (Corresponding_Record_Type (Parent_Type), Loc),
6135 Constraint =>
6136 Constraint
6137 (Subtype_Indication (Type_Definition (New_N))))));
6138 end if;
6139
6140 -- Copy Storage_Size and Relative_Deadline variables if task case
6141
6142 if Is_Task_Type (Parent_Type) then
6143 Set_Storage_Size_Variable (Derived_Type,
6144 Storage_Size_Variable (Parent_Type));
6145 Set_Relative_Deadline_Variable (Derived_Type,
6146 Relative_Deadline_Variable (Parent_Type));
6147 end if;
6148
6149 if Present (Discriminant_Specifications (N)) then
6150 Push_Scope (Derived_Type);
6151 Check_Or_Process_Discriminants (N, Derived_Type);
6152
6153 if Constraint_Present then
6154 New_Constraint :=
6155 Expand_To_Stored_Constraint
6156 (Parent_Type,
6157 Build_Discriminant_Constraints
6158 (Parent_Type,
6159 Subtype_Indication (Type_Definition (N)), True));
6160 end if;
6161
6162 End_Scope;
6163
6164 elsif Constraint_Present then
6165
6166 -- Build constrained subtype, copying the constraint, and derive
6167 -- from it to create a derived constrained type.
6168
6169 declare
6170 Loc : constant Source_Ptr := Sloc (N);
6171 Anon : constant Entity_Id :=
6172 Make_Defining_Identifier (Loc,
6173 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6174 Decl : Node_Id;
6175
6176 begin
6177 Decl :=
6178 Make_Subtype_Declaration (Loc,
6179 Defining_Identifier => Anon,
6180 Subtype_Indication =>
6181 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6182 Insert_Before (N, Decl);
6183 Analyze (Decl);
6184
6185 Rewrite (Subtype_Indication (Type_Definition (N)),
6186 New_Occurrence_Of (Anon, Loc));
6187 Set_Analyzed (Derived_Type, False);
6188 Analyze (N);
6189 return;
6190 end;
6191 end if;
6192
6193 -- By default, operations and private data are inherited from parent.
6194 -- However, in the presence of bound discriminants, a new corresponding
6195 -- record will be created, see below.
6196
6197 Set_Has_Discriminants
6198 (Derived_Type, Has_Discriminants (Parent_Type));
6199 Set_Corresponding_Record_Type
6200 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6201
6202 -- Is_Constrained is set according the parent subtype, but is set to
6203 -- False if the derived type is declared with new discriminants.
6204
6205 Set_Is_Constrained
6206 (Derived_Type,
6207 (Is_Constrained (Parent_Type) or else Constraint_Present)
6208 and then not Present (Discriminant_Specifications (N)));
6209
6210 if Constraint_Present then
6211 if not Has_Discriminants (Parent_Type) then
6212 Error_Msg_N ("untagged parent must have discriminants", N);
6213
6214 elsif Present (Discriminant_Specifications (N)) then
6215
6216 -- Verify that new discriminants are used to constrain old ones
6217
6218 D_Constraint :=
6219 First
6220 (Constraints
6221 (Constraint (Subtype_Indication (Type_Definition (N)))));
6222
6223 Old_Disc := First_Discriminant (Parent_Type);
6224
6225 while Present (D_Constraint) loop
6226 if Nkind (D_Constraint) /= N_Discriminant_Association then
6227
6228 -- Positional constraint. If it is a reference to a new
6229 -- discriminant, it constrains the corresponding old one.
6230
6231 if Nkind (D_Constraint) = N_Identifier then
6232 New_Disc := First_Discriminant (Derived_Type);
6233 while Present (New_Disc) loop
6234 exit when Chars (New_Disc) = Chars (D_Constraint);
6235 Next_Discriminant (New_Disc);
6236 end loop;
6237
6238 if Present (New_Disc) then
6239 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6240 end if;
6241 end if;
6242
6243 Next_Discriminant (Old_Disc);
6244
6245 -- if this is a named constraint, search by name for the old
6246 -- discriminants constrained by the new one.
6247
6248 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6249
6250 -- Find new discriminant with that name
6251
6252 New_Disc := First_Discriminant (Derived_Type);
6253 while Present (New_Disc) loop
6254 exit when
6255 Chars (New_Disc) = Chars (Expression (D_Constraint));
6256 Next_Discriminant (New_Disc);
6257 end loop;
6258
6259 if Present (New_Disc) then
6260
6261 -- Verify that new discriminant renames some discriminant
6262 -- of the parent type, and associate the new discriminant
6263 -- with one or more old ones that it renames.
6264
6265 declare
6266 Selector : Node_Id;
6267
6268 begin
6269 Selector := First (Selector_Names (D_Constraint));
6270 while Present (Selector) loop
6271 Old_Disc := First_Discriminant (Parent_Type);
6272 while Present (Old_Disc) loop
6273 exit when Chars (Old_Disc) = Chars (Selector);
6274 Next_Discriminant (Old_Disc);
6275 end loop;
6276
6277 if Present (Old_Disc) then
6278 Set_Corresponding_Discriminant
6279 (New_Disc, Old_Disc);
6280 end if;
6281
6282 Next (Selector);
6283 end loop;
6284 end;
6285 end if;
6286 end if;
6287
6288 Next (D_Constraint);
6289 end loop;
6290
6291 New_Disc := First_Discriminant (Derived_Type);
6292 while Present (New_Disc) loop
6293 if No (Corresponding_Discriminant (New_Disc)) then
6294 Error_Msg_NE
6295 ("new discriminant& must constrain old one", N, New_Disc);
6296
6297 elsif not
6298 Subtypes_Statically_Compatible
6299 (Etype (New_Disc),
6300 Etype (Corresponding_Discriminant (New_Disc)))
6301 then
6302 Error_Msg_NE
6303 ("& not statically compatible with parent discriminant",
6304 N, New_Disc);
6305 end if;
6306
6307 Next_Discriminant (New_Disc);
6308 end loop;
6309 end if;
6310
6311 elsif Present (Discriminant_Specifications (N)) then
6312 Error_Msg_N
6313 ("missing discriminant constraint in untagged derivation", N);
6314 end if;
6315
6316 -- The entity chain of the derived type includes the new discriminants
6317 -- but shares operations with the parent.
6318
6319 if Present (Discriminant_Specifications (N)) then
6320 Old_Disc := First_Discriminant (Parent_Type);
6321 while Present (Old_Disc) loop
6322 if No (Next_Entity (Old_Disc))
6323 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6324 then
6325 Set_Next_Entity
6326 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6327 exit;
6328 end if;
6329
6330 Next_Discriminant (Old_Disc);
6331 end loop;
6332
6333 else
6334 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6335 if Has_Discriminants (Parent_Type) then
6336 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6337 Set_Discriminant_Constraint (
6338 Derived_Type, Discriminant_Constraint (Parent_Type));
6339 end if;
6340 end if;
6341
6342 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6343
6344 Set_Has_Completion (Derived_Type);
6345
6346 if Corr_Decl_Needed then
6347 Set_Stored_Constraint (Derived_Type, New_Constraint);
6348 Insert_After (N, Corr_Decl);
6349 Analyze (Corr_Decl);
6350 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6351 end if;
6352 end Build_Derived_Concurrent_Type;
6353
6354 ------------------------------------
6355 -- Build_Derived_Enumeration_Type --
6356 ------------------------------------
6357
6358 procedure Build_Derived_Enumeration_Type
6359 (N : Node_Id;
6360 Parent_Type : Entity_Id;
6361 Derived_Type : Entity_Id)
6362 is
6363 Loc : constant Source_Ptr := Sloc (N);
6364 Def : constant Node_Id := Type_Definition (N);
6365 Indic : constant Node_Id := Subtype_Indication (Def);
6366 Implicit_Base : Entity_Id;
6367 Literal : Entity_Id;
6368 New_Lit : Entity_Id;
6369 Literals_List : List_Id;
6370 Type_Decl : Node_Id;
6371 Hi, Lo : Node_Id;
6372 Rang_Expr : Node_Id;
6373
6374 begin
6375 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6376 -- not have explicit literals lists we need to process types derived
6377 -- from them specially. This is handled by Derived_Standard_Character.
6378 -- If the parent type is a generic type, there are no literals either,
6379 -- and we construct the same skeletal representation as for the generic
6380 -- parent type.
6381
6382 if Is_Standard_Character_Type (Parent_Type) then
6383 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6384
6385 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6386 declare
6387 Lo : Node_Id;
6388 Hi : Node_Id;
6389
6390 begin
6391 if Nkind (Indic) /= N_Subtype_Indication then
6392 Lo :=
6393 Make_Attribute_Reference (Loc,
6394 Attribute_Name => Name_First,
6395 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6396 Set_Etype (Lo, Derived_Type);
6397
6398 Hi :=
6399 Make_Attribute_Reference (Loc,
6400 Attribute_Name => Name_Last,
6401 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6402 Set_Etype (Hi, Derived_Type);
6403
6404 Set_Scalar_Range (Derived_Type,
6405 Make_Range (Loc,
6406 Low_Bound => Lo,
6407 High_Bound => Hi));
6408 else
6409
6410 -- Analyze subtype indication and verify compatibility
6411 -- with parent type.
6412
6413 if Base_Type (Process_Subtype (Indic, N)) /=
6414 Base_Type (Parent_Type)
6415 then
6416 Error_Msg_N
6417 ("illegal constraint for formal discrete type", N);
6418 end if;
6419 end if;
6420 end;
6421
6422 else
6423 -- If a constraint is present, analyze the bounds to catch
6424 -- premature usage of the derived literals.
6425
6426 if Nkind (Indic) = N_Subtype_Indication
6427 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6428 then
6429 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6430 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6431 end if;
6432
6433 -- Introduce an implicit base type for the derived type even if there
6434 -- is no constraint attached to it, since this seems closer to the
6435 -- Ada semantics. Build a full type declaration tree for the derived
6436 -- type using the implicit base type as the defining identifier. The
6437 -- build a subtype declaration tree which applies the constraint (if
6438 -- any) have it replace the derived type declaration.
6439
6440 Literal := First_Literal (Parent_Type);
6441 Literals_List := New_List;
6442 while Present (Literal)
6443 and then Ekind (Literal) = E_Enumeration_Literal
6444 loop
6445 -- Literals of the derived type have the same representation as
6446 -- those of the parent type, but this representation can be
6447 -- overridden by an explicit representation clause. Indicate
6448 -- that there is no explicit representation given yet. These
6449 -- derived literals are implicit operations of the new type,
6450 -- and can be overridden by explicit ones.
6451
6452 if Nkind (Literal) = N_Defining_Character_Literal then
6453 New_Lit :=
6454 Make_Defining_Character_Literal (Loc, Chars (Literal));
6455 else
6456 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6457 end if;
6458
6459 Set_Ekind (New_Lit, E_Enumeration_Literal);
6460 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6461 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6462 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6463 Set_Alias (New_Lit, Literal);
6464 Set_Is_Known_Valid (New_Lit, True);
6465
6466 Append (New_Lit, Literals_List);
6467 Next_Literal (Literal);
6468 end loop;
6469
6470 Implicit_Base :=
6471 Make_Defining_Identifier (Sloc (Derived_Type),
6472 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6473
6474 -- Indicate the proper nature of the derived type. This must be done
6475 -- before analysis of the literals, to recognize cases when a literal
6476 -- may be hidden by a previous explicit function definition (cf.
6477 -- c83031a).
6478
6479 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6480 Set_Etype (Derived_Type, Implicit_Base);
6481
6482 Type_Decl :=
6483 Make_Full_Type_Declaration (Loc,
6484 Defining_Identifier => Implicit_Base,
6485 Discriminant_Specifications => No_List,
6486 Type_Definition =>
6487 Make_Enumeration_Type_Definition (Loc, Literals_List));
6488
6489 Mark_Rewrite_Insertion (Type_Decl);
6490 Insert_Before (N, Type_Decl);
6491 Analyze (Type_Decl);
6492
6493 -- After the implicit base is analyzed its Etype needs to be changed
6494 -- to reflect the fact that it is derived from the parent type which
6495 -- was ignored during analysis. We also set the size at this point.
6496
6497 Set_Etype (Implicit_Base, Parent_Type);
6498
6499 Set_Size_Info (Implicit_Base, Parent_Type);
6500 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6501 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6502
6503 -- Copy other flags from parent type
6504
6505 Set_Has_Non_Standard_Rep
6506 (Implicit_Base, Has_Non_Standard_Rep
6507 (Parent_Type));
6508 Set_Has_Pragma_Ordered
6509 (Implicit_Base, Has_Pragma_Ordered
6510 (Parent_Type));
6511 Set_Has_Delayed_Freeze (Implicit_Base);
6512
6513 -- Process the subtype indication including a validation check on the
6514 -- constraint, if any. If a constraint is given, its bounds must be
6515 -- implicitly converted to the new type.
6516
6517 if Nkind (Indic) = N_Subtype_Indication then
6518 declare
6519 R : constant Node_Id :=
6520 Range_Expression (Constraint (Indic));
6521
6522 begin
6523 if Nkind (R) = N_Range then
6524 Hi := Build_Scalar_Bound
6525 (High_Bound (R), Parent_Type, Implicit_Base);
6526 Lo := Build_Scalar_Bound
6527 (Low_Bound (R), Parent_Type, Implicit_Base);
6528
6529 else
6530 -- Constraint is a Range attribute. Replace with explicit
6531 -- mention of the bounds of the prefix, which must be a
6532 -- subtype.
6533
6534 Analyze (Prefix (R));
6535 Hi :=
6536 Convert_To (Implicit_Base,
6537 Make_Attribute_Reference (Loc,
6538 Attribute_Name => Name_Last,
6539 Prefix =>
6540 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6541
6542 Lo :=
6543 Convert_To (Implicit_Base,
6544 Make_Attribute_Reference (Loc,
6545 Attribute_Name => Name_First,
6546 Prefix =>
6547 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6548 end if;
6549 end;
6550
6551 else
6552 Hi :=
6553 Build_Scalar_Bound
6554 (Type_High_Bound (Parent_Type),
6555 Parent_Type, Implicit_Base);
6556 Lo :=
6557 Build_Scalar_Bound
6558 (Type_Low_Bound (Parent_Type),
6559 Parent_Type, Implicit_Base);
6560 end if;
6561
6562 Rang_Expr :=
6563 Make_Range (Loc,
6564 Low_Bound => Lo,
6565 High_Bound => Hi);
6566
6567 -- If we constructed a default range for the case where no range
6568 -- was given, then the expressions in the range must not freeze
6569 -- since they do not correspond to expressions in the source.
6570
6571 if Nkind (Indic) /= N_Subtype_Indication then
6572 Set_Must_Not_Freeze (Lo);
6573 Set_Must_Not_Freeze (Hi);
6574 Set_Must_Not_Freeze (Rang_Expr);
6575 end if;
6576
6577 Rewrite (N,
6578 Make_Subtype_Declaration (Loc,
6579 Defining_Identifier => Derived_Type,
6580 Subtype_Indication =>
6581 Make_Subtype_Indication (Loc,
6582 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6583 Constraint =>
6584 Make_Range_Constraint (Loc,
6585 Range_Expression => Rang_Expr))));
6586
6587 Analyze (N);
6588
6589 -- Propagate the aspects from the original type declaration to the
6590 -- declaration of the implicit base.
6591
6592 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6593
6594 -- Apply a range check. Since this range expression doesn't have an
6595 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6596 -- this right???
6597
6598 if Nkind (Indic) = N_Subtype_Indication then
6599 Apply_Range_Check
6600 (Range_Expression (Constraint (Indic)), Parent_Type,
6601 Source_Typ => Entity (Subtype_Mark (Indic)));
6602 end if;
6603 end if;
6604 end Build_Derived_Enumeration_Type;
6605
6606 --------------------------------
6607 -- Build_Derived_Numeric_Type --
6608 --------------------------------
6609
6610 procedure Build_Derived_Numeric_Type
6611 (N : Node_Id;
6612 Parent_Type : Entity_Id;
6613 Derived_Type : Entity_Id)
6614 is
6615 Loc : constant Source_Ptr := Sloc (N);
6616 Tdef : constant Node_Id := Type_Definition (N);
6617 Indic : constant Node_Id := Subtype_Indication (Tdef);
6618 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6619 No_Constraint : constant Boolean := Nkind (Indic) /=
6620 N_Subtype_Indication;
6621 Implicit_Base : Entity_Id;
6622
6623 Lo : Node_Id;
6624 Hi : Node_Id;
6625
6626 begin
6627 -- Process the subtype indication including a validation check on
6628 -- the constraint if any.
6629
6630 Discard_Node (Process_Subtype (Indic, N));
6631
6632 -- Introduce an implicit base type for the derived type even if there
6633 -- is no constraint attached to it, since this seems closer to the Ada
6634 -- semantics.
6635
6636 Implicit_Base :=
6637 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6638
6639 Set_Etype (Implicit_Base, Parent_Base);
6640 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6641 Set_Size_Info (Implicit_Base, Parent_Base);
6642 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6643 Set_Parent (Implicit_Base, Parent (Derived_Type));
6644 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6645
6646 -- Set RM Size for discrete type or decimal fixed-point type
6647 -- Ordinary fixed-point is excluded, why???
6648
6649 if Is_Discrete_Type (Parent_Base)
6650 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6651 then
6652 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6653 end if;
6654
6655 Set_Has_Delayed_Freeze (Implicit_Base);
6656
6657 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6658 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6659
6660 Set_Scalar_Range (Implicit_Base,
6661 Make_Range (Loc,
6662 Low_Bound => Lo,
6663 High_Bound => Hi));
6664
6665 if Has_Infinities (Parent_Base) then
6666 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6667 end if;
6668
6669 -- The Derived_Type, which is the entity of the declaration, is a
6670 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6671 -- absence of an explicit constraint.
6672
6673 Set_Etype (Derived_Type, Implicit_Base);
6674
6675 -- If we did not have a constraint, then the Ekind is set from the
6676 -- parent type (otherwise Process_Subtype has set the bounds)
6677
6678 if No_Constraint then
6679 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6680 end if;
6681
6682 -- If we did not have a range constraint, then set the range from the
6683 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6684
6685 if No_Constraint or else not Has_Range_Constraint (Indic) then
6686 Set_Scalar_Range (Derived_Type,
6687 Make_Range (Loc,
6688 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6689 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6690 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6691
6692 if Has_Infinities (Parent_Type) then
6693 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6694 end if;
6695
6696 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6697 end if;
6698
6699 Set_Is_Descendent_Of_Address (Derived_Type,
6700 Is_Descendent_Of_Address (Parent_Type));
6701 Set_Is_Descendent_Of_Address (Implicit_Base,
6702 Is_Descendent_Of_Address (Parent_Type));
6703
6704 -- Set remaining type-specific fields, depending on numeric type
6705
6706 if Is_Modular_Integer_Type (Parent_Type) then
6707 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6708
6709 Set_Non_Binary_Modulus
6710 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6711
6712 Set_Is_Known_Valid
6713 (Implicit_Base, Is_Known_Valid (Parent_Base));
6714
6715 elsif Is_Floating_Point_Type (Parent_Type) then
6716
6717 -- Digits of base type is always copied from the digits value of
6718 -- the parent base type, but the digits of the derived type will
6719 -- already have been set if there was a constraint present.
6720
6721 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6722 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6723
6724 if No_Constraint then
6725 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6726 end if;
6727
6728 elsif Is_Fixed_Point_Type (Parent_Type) then
6729
6730 -- Small of base type and derived type are always copied from the
6731 -- parent base type, since smalls never change. The delta of the
6732 -- base type is also copied from the parent base type. However the
6733 -- delta of the derived type will have been set already if a
6734 -- constraint was present.
6735
6736 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6737 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6738 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6739
6740 if No_Constraint then
6741 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6742 end if;
6743
6744 -- The scale and machine radix in the decimal case are always
6745 -- copied from the parent base type.
6746
6747 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6748 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6749 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6750
6751 Set_Machine_Radix_10
6752 (Derived_Type, Machine_Radix_10 (Parent_Base));
6753 Set_Machine_Radix_10
6754 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6755
6756 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6757
6758 if No_Constraint then
6759 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6760
6761 else
6762 -- the analysis of the subtype_indication sets the
6763 -- digits value of the derived type.
6764
6765 null;
6766 end if;
6767 end if;
6768 end if;
6769
6770 if Is_Integer_Type (Parent_Type) then
6771 Set_Has_Shift_Operator
6772 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6773 end if;
6774
6775 -- The type of the bounds is that of the parent type, and they
6776 -- must be converted to the derived type.
6777
6778 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6779
6780 -- The implicit_base should be frozen when the derived type is frozen,
6781 -- but note that it is used in the conversions of the bounds. For fixed
6782 -- types we delay the determination of the bounds until the proper
6783 -- freezing point. For other numeric types this is rejected by GCC, for
6784 -- reasons that are currently unclear (???), so we choose to freeze the
6785 -- implicit base now. In the case of integers and floating point types
6786 -- this is harmless because subsequent representation clauses cannot
6787 -- affect anything, but it is still baffling that we cannot use the
6788 -- same mechanism for all derived numeric types.
6789
6790 -- There is a further complication: actually some representation
6791 -- clauses can affect the implicit base type. For example, attribute
6792 -- definition clauses for stream-oriented attributes need to set the
6793 -- corresponding TSS entries on the base type, and this normally
6794 -- cannot be done after the base type is frozen, so the circuitry in
6795 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6796 -- and not use Set_TSS in this case.
6797
6798 -- There are also consequences for the case of delayed representation
6799 -- aspects for some cases. For example, a Size aspect is delayed and
6800 -- should not be evaluated to the freeze point. This early freezing
6801 -- means that the size attribute evaluation happens too early???
6802
6803 if Is_Fixed_Point_Type (Parent_Type) then
6804 Conditional_Delay (Implicit_Base, Parent_Type);
6805 else
6806 Freeze_Before (N, Implicit_Base);
6807 end if;
6808 end Build_Derived_Numeric_Type;
6809
6810 --------------------------------
6811 -- Build_Derived_Private_Type --
6812 --------------------------------
6813
6814 procedure Build_Derived_Private_Type
6815 (N : Node_Id;
6816 Parent_Type : Entity_Id;
6817 Derived_Type : Entity_Id;
6818 Is_Completion : Boolean;
6819 Derive_Subps : Boolean := True)
6820 is
6821 Loc : constant Source_Ptr := Sloc (N);
6822 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
6823 Par_Scope : constant Entity_Id := Scope (Par_Base);
6824 Full_N : constant Node_Id := New_Copy_Tree (N);
6825 Full_Der : Entity_Id := New_Copy (Derived_Type);
6826 Full_P : Entity_Id;
6827
6828 procedure Build_Full_Derivation;
6829 -- Build full derivation, i.e. derive from the full view
6830
6831 procedure Copy_And_Build;
6832 -- Copy derived type declaration, replace parent with its full view,
6833 -- and build derivation
6834
6835 ---------------------------
6836 -- Build_Full_Derivation --
6837 ---------------------------
6838
6839 procedure Build_Full_Derivation is
6840 begin
6841 -- If parent scope is not open, install the declarations
6842
6843 if not In_Open_Scopes (Par_Scope) then
6844 Install_Private_Declarations (Par_Scope);
6845 Install_Visible_Declarations (Par_Scope);
6846 Copy_And_Build;
6847 Uninstall_Declarations (Par_Scope);
6848
6849 -- If parent scope is open and in another unit, and parent has a
6850 -- completion, then the derivation is taking place in the visible
6851 -- part of a child unit. In that case retrieve the full view of
6852 -- the parent momentarily.
6853
6854 elsif not In_Same_Source_Unit (N, Parent_Type) then
6855 Full_P := Full_View (Parent_Type);
6856 Exchange_Declarations (Parent_Type);
6857 Copy_And_Build;
6858 Exchange_Declarations (Full_P);
6859
6860 -- Otherwise it is a local derivation
6861
6862 else
6863 Copy_And_Build;
6864 end if;
6865 end Build_Full_Derivation;
6866
6867 --------------------
6868 -- Copy_And_Build --
6869 --------------------
6870
6871 procedure Copy_And_Build is
6872 Full_Parent : Entity_Id := Parent_Type;
6873
6874 begin
6875 -- If the parent is itself derived from another private type,
6876 -- installing the private declarations has not affected its
6877 -- privacy status, so use its own full view explicitly.
6878
6879 if Is_Private_Type (Full_Parent)
6880 and then Present (Full_View (Full_Parent))
6881 then
6882 Full_Parent := Full_View (Full_Parent);
6883 end if;
6884
6885 -- And its underlying full view if necessary
6886
6887 if Is_Private_Type (Full_Parent)
6888 and then Present (Underlying_Full_View (Full_Parent))
6889 then
6890 Full_Parent := Underlying_Full_View (Full_Parent);
6891 end if;
6892
6893 -- For record, access and most enumeration types, derivation from
6894 -- the full view requires a fully-fledged declaration. In the other
6895 -- cases, just use an itype.
6896
6897 if Ekind (Full_Parent) in Record_Kind
6898 or else Ekind (Full_Parent) in Access_Kind
6899 or else
6900 (Ekind (Full_Parent) in Enumeration_Kind
6901 and then not Is_Standard_Character_Type (Full_Parent)
6902 and then not Is_Generic_Type (Root_Type (Full_Parent)))
6903 then
6904 -- Copy and adjust declaration to provide a completion for what
6905 -- is originally a private declaration. Indicate that full view
6906 -- is internally generated.
6907
6908 Set_Comes_From_Source (Full_N, False);
6909 Set_Comes_From_Source (Full_Der, False);
6910 Set_Parent (Full_Der, Full_N);
6911 Set_Defining_Identifier (Full_N, Full_Der);
6912
6913 -- If there are no constraints, adjust the subtype mark
6914
6915 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
6916 N_Subtype_Indication
6917 then
6918 Set_Subtype_Indication
6919 (Type_Definition (Full_N),
6920 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
6921 end if;
6922
6923 Insert_After (N, Full_N);
6924
6925 -- Build full view of derived type from full view of parent which
6926 -- is now installed. Subprograms have been derived on the partial
6927 -- view, the completion does not derive them anew.
6928
6929 if Ekind (Full_Parent) in Record_Kind then
6930
6931 -- If parent type is tagged, the completion inherits the proper
6932 -- primitive operations.
6933
6934 if Is_Tagged_Type (Parent_Type) then
6935 Build_Derived_Record_Type
6936 (Full_N, Full_Parent, Full_Der, Derive_Subps);
6937 else
6938 Build_Derived_Record_Type
6939 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
6940 end if;
6941
6942 else
6943 Build_Derived_Type
6944 (Full_N, Full_Parent, Full_Der,
6945 Is_Completion => False, Derive_Subps => False);
6946 end if;
6947
6948 -- The full declaration has been introduced into the tree and
6949 -- processed in the step above. It should not be analyzed again
6950 -- (when encountered later in the current list of declarations)
6951 -- to prevent spurious name conflicts. The full entity remains
6952 -- invisible.
6953
6954 Set_Analyzed (Full_N);
6955
6956 else
6957 Full_Der :=
6958 Make_Defining_Identifier (Sloc (Derived_Type),
6959 Chars => Chars (Derived_Type));
6960 Set_Is_Itype (Full_Der);
6961 Set_Associated_Node_For_Itype (Full_Der, N);
6962 Set_Parent (Full_Der, N);
6963 Build_Derived_Type
6964 (N, Full_Parent, Full_Der,
6965 Is_Completion => False, Derive_Subps => False);
6966 end if;
6967
6968 Set_Has_Private_Declaration (Full_Der);
6969 Set_Has_Private_Declaration (Derived_Type);
6970
6971 Set_Scope (Full_Der, Scope (Derived_Type));
6972 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
6973 Set_Has_Size_Clause (Full_Der, False);
6974 Set_Has_Alignment_Clause (Full_Der, False);
6975 Set_Has_Delayed_Freeze (Full_Der);
6976 Set_Is_Frozen (Full_Der, False);
6977 Set_Freeze_Node (Full_Der, Empty);
6978 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
6979 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
6980
6981 -- The convention on the base type may be set in the private part
6982 -- and not propagated to the subtype until later, so we obtain the
6983 -- convention from the base type of the parent.
6984
6985 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
6986 end Copy_And_Build;
6987
6988 -- Start of processing for Build_Derived_Private_Type
6989
6990 begin
6991 if Is_Tagged_Type (Parent_Type) then
6992 Full_P := Full_View (Parent_Type);
6993
6994 -- A type extension of a type with unknown discriminants is an
6995 -- indefinite type that the back-end cannot handle directly.
6996 -- We treat it as a private type, and build a completion that is
6997 -- derived from the full view of the parent, and hopefully has
6998 -- known discriminants.
6999
7000 -- If the full view of the parent type has an underlying record view,
7001 -- use it to generate the underlying record view of this derived type
7002 -- (required for chains of derivations with unknown discriminants).
7003
7004 -- Minor optimization: we avoid the generation of useless underlying
7005 -- record view entities if the private type declaration has unknown
7006 -- discriminants but its corresponding full view has no
7007 -- discriminants.
7008
7009 if Has_Unknown_Discriminants (Parent_Type)
7010 and then Present (Full_P)
7011 and then (Has_Discriminants (Full_P)
7012 or else Present (Underlying_Record_View (Full_P)))
7013 and then not In_Open_Scopes (Par_Scope)
7014 and then Expander_Active
7015 then
7016 declare
7017 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7018 New_Ext : constant Node_Id :=
7019 Copy_Separate_Tree
7020 (Record_Extension_Part (Type_Definition (N)));
7021 Decl : Node_Id;
7022
7023 begin
7024 Build_Derived_Record_Type
7025 (N, Parent_Type, Derived_Type, Derive_Subps);
7026
7027 -- Build anonymous completion, as a derivation from the full
7028 -- view of the parent. This is not a completion in the usual
7029 -- sense, because the current type is not private.
7030
7031 Decl :=
7032 Make_Full_Type_Declaration (Loc,
7033 Defining_Identifier => Full_Der,
7034 Type_Definition =>
7035 Make_Derived_Type_Definition (Loc,
7036 Subtype_Indication =>
7037 New_Copy_Tree
7038 (Subtype_Indication (Type_Definition (N))),
7039 Record_Extension_Part => New_Ext));
7040
7041 -- If the parent type has an underlying record view, use it
7042 -- here to build the new underlying record view.
7043
7044 if Present (Underlying_Record_View (Full_P)) then
7045 pragma Assert
7046 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7047 = N_Identifier);
7048 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7049 Underlying_Record_View (Full_P));
7050 end if;
7051
7052 Install_Private_Declarations (Par_Scope);
7053 Install_Visible_Declarations (Par_Scope);
7054 Insert_Before (N, Decl);
7055
7056 -- Mark entity as an underlying record view before analysis,
7057 -- to avoid generating the list of its primitive operations
7058 -- (which is not really required for this entity) and thus
7059 -- prevent spurious errors associated with missing overriding
7060 -- of abstract primitives (overridden only for Derived_Type).
7061
7062 Set_Ekind (Full_Der, E_Record_Type);
7063 Set_Is_Underlying_Record_View (Full_Der);
7064 Set_Default_SSO (Full_Der);
7065
7066 Analyze (Decl);
7067
7068 pragma Assert (Has_Discriminants (Full_Der)
7069 and then not Has_Unknown_Discriminants (Full_Der));
7070
7071 Uninstall_Declarations (Par_Scope);
7072
7073 -- Freeze the underlying record view, to prevent generation of
7074 -- useless dispatching information, which is simply shared with
7075 -- the real derived type.
7076
7077 Set_Is_Frozen (Full_Der);
7078
7079 -- If the derived type has access discriminants, create
7080 -- references to their anonymous types now, to prevent
7081 -- back-end problems when their first use is in generated
7082 -- bodies of primitives.
7083
7084 declare
7085 E : Entity_Id;
7086
7087 begin
7088 E := First_Entity (Full_Der);
7089
7090 while Present (E) loop
7091 if Ekind (E) = E_Discriminant
7092 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7093 then
7094 Build_Itype_Reference (Etype (E), Decl);
7095 end if;
7096
7097 Next_Entity (E);
7098 end loop;
7099 end;
7100
7101 -- Set up links between real entity and underlying record view
7102
7103 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7104 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7105 end;
7106
7107 -- If discriminants are known, build derived record
7108
7109 else
7110 Build_Derived_Record_Type
7111 (N, Parent_Type, Derived_Type, Derive_Subps);
7112 end if;
7113
7114 return;
7115
7116 elsif Has_Discriminants (Parent_Type) then
7117
7118 -- Build partial view of derived type from partial view of parent.
7119 -- This must be done before building the full derivation because the
7120 -- second derivation will modify the discriminants of the first and
7121 -- the discriminants are chained with the rest of the components in
7122 -- the full derivation.
7123
7124 Build_Derived_Record_Type
7125 (N, Parent_Type, Derived_Type, Derive_Subps);
7126
7127 -- Build the full derivation if this is not the anonymous derived
7128 -- base type created by Build_Derived_Record_Type in the constrained
7129 -- case (see point 5. of its head comment) since we build it for the
7130 -- derived subtype. And skip it for protected types altogether, as
7131 -- gigi does not use these types directly.
7132
7133 if Present (Full_View (Parent_Type))
7134 and then not Is_Itype (Derived_Type)
7135 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7136 then
7137 declare
7138 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7139 Discr : Entity_Id;
7140 Last_Discr : Entity_Id;
7141
7142 begin
7143 -- If this is not a completion, construct the implicit full
7144 -- view by deriving from the full view of the parent type.
7145 -- But if this is a completion, the derived private type
7146 -- being built is a full view and the full derivation can
7147 -- only be its underlying full view.
7148
7149 Build_Full_Derivation;
7150
7151 if not Is_Completion then
7152 Set_Full_View (Derived_Type, Full_Der);
7153 else
7154 Set_Underlying_Full_View (Derived_Type, Full_Der);
7155 end if;
7156
7157 if not Is_Base_Type (Derived_Type) then
7158 Set_Full_View (Der_Base, Base_Type (Full_Der));
7159 end if;
7160
7161 -- Copy the discriminant list from full view to the partial
7162 -- view (base type and its subtype). Gigi requires that the
7163 -- partial and full views have the same discriminants.
7164
7165 -- Note that since the partial view points to discriminants
7166 -- in the full view, their scope will be that of the full
7167 -- view. This might cause some front end problems and need
7168 -- adjustment???
7169
7170 Discr := First_Discriminant (Base_Type (Full_Der));
7171 Set_First_Entity (Der_Base, Discr);
7172
7173 loop
7174 Last_Discr := Discr;
7175 Next_Discriminant (Discr);
7176 exit when No (Discr);
7177 end loop;
7178
7179 Set_Last_Entity (Der_Base, Last_Discr);
7180 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7181 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7182
7183 Set_Stored_Constraint
7184 (Full_Der, Stored_Constraint (Derived_Type));
7185 end;
7186 end if;
7187
7188 elsif Present (Full_View (Parent_Type))
7189 and then Has_Discriminants (Full_View (Parent_Type))
7190 then
7191 if Has_Unknown_Discriminants (Parent_Type)
7192 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7193 N_Subtype_Indication
7194 then
7195 Error_Msg_N
7196 ("cannot constrain type with unknown discriminants",
7197 Subtype_Indication (Type_Definition (N)));
7198 return;
7199 end if;
7200
7201 -- If this is not a completion, construct the implicit full view by
7202 -- deriving from the full view of the parent type. But if this is a
7203 -- completion, the derived private type being built is a full view
7204 -- and the full derivation can only be its underlying full view.
7205
7206 Build_Full_Derivation;
7207
7208 if not Is_Completion then
7209 Set_Full_View (Derived_Type, Full_Der);
7210 else
7211 Set_Underlying_Full_View (Derived_Type, Full_Der);
7212 end if;
7213
7214 -- In any case, the primitive operations are inherited from the
7215 -- parent type, not from the internal full view.
7216
7217 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7218
7219 if Derive_Subps then
7220 Derive_Subprograms (Parent_Type, Derived_Type);
7221 end if;
7222
7223 Set_Stored_Constraint (Derived_Type, No_Elist);
7224 Set_Is_Constrained
7225 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7226
7227 else
7228 -- Untagged type, No discriminants on either view
7229
7230 if Nkind (Subtype_Indication (Type_Definition (N))) =
7231 N_Subtype_Indication
7232 then
7233 Error_Msg_N
7234 ("illegal constraint on type without discriminants", N);
7235 end if;
7236
7237 if Present (Discriminant_Specifications (N))
7238 and then Present (Full_View (Parent_Type))
7239 and then not Is_Tagged_Type (Full_View (Parent_Type))
7240 then
7241 Error_Msg_N ("cannot add discriminants to untagged type", N);
7242 end if;
7243
7244 Set_Stored_Constraint (Derived_Type, No_Elist);
7245 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7246 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7247 Set_Has_Controlled_Component
7248 (Derived_Type, Has_Controlled_Component
7249 (Parent_Type));
7250
7251 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7252
7253 if not Is_Controlled (Parent_Type) then
7254 Set_Finalize_Storage_Only
7255 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7256 end if;
7257
7258 -- If this is not a completion, construct the implicit full view by
7259 -- deriving from the full view of the parent type.
7260
7261 -- ??? If the parent is untagged private and its completion is
7262 -- tagged, this mechanism will not work because we cannot derive from
7263 -- the tagged full view unless we have an extension.
7264
7265 if Present (Full_View (Parent_Type))
7266 and then not Is_Tagged_Type (Full_View (Parent_Type))
7267 and then not Is_Completion
7268 then
7269 Build_Full_Derivation;
7270 Set_Full_View (Derived_Type, Full_Der);
7271 end if;
7272 end if;
7273
7274 Set_Has_Unknown_Discriminants (Derived_Type,
7275 Has_Unknown_Discriminants (Parent_Type));
7276
7277 if Is_Private_Type (Derived_Type) then
7278 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7279 end if;
7280
7281 -- If the parent base type is in scope, add the derived type to its
7282 -- list of private dependents, because its full view may become
7283 -- visible subsequently (in a nested private part, a body, or in a
7284 -- further child unit).
7285
7286 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7287 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7288
7289 -- Check for unusual case where a type completed by a private
7290 -- derivation occurs within a package nested in a child unit, and
7291 -- the parent is declared in an ancestor.
7292
7293 if Is_Child_Unit (Scope (Current_Scope))
7294 and then Is_Completion
7295 and then In_Private_Part (Current_Scope)
7296 and then Scope (Parent_Type) /= Current_Scope
7297
7298 -- Note that if the parent has a completion in the private part,
7299 -- (which is itself a derivation from some other private type)
7300 -- it is that completion that is visible, there is no full view
7301 -- available, and no special processing is needed.
7302
7303 and then Present (Full_View (Parent_Type))
7304 then
7305 -- In this case, the full view of the parent type will become
7306 -- visible in the body of the enclosing child, and only then will
7307 -- the current type be possibly non-private. Build an underlying
7308 -- full view that will be installed when the enclosing child body
7309 -- is compiled.
7310
7311 if Present (Underlying_Full_View (Derived_Type)) then
7312 Full_Der := Underlying_Full_View (Derived_Type);
7313 else
7314 Build_Full_Derivation;
7315 Set_Underlying_Full_View (Derived_Type, Full_Der);
7316 end if;
7317
7318 -- The full view will be used to swap entities on entry/exit to
7319 -- the body, and must appear in the entity list for the package.
7320
7321 Append_Entity (Full_Der, Scope (Derived_Type));
7322 end if;
7323 end if;
7324 end Build_Derived_Private_Type;
7325
7326 -------------------------------
7327 -- Build_Derived_Record_Type --
7328 -------------------------------
7329
7330 -- 1. INTRODUCTION
7331
7332 -- Ideally we would like to use the same model of type derivation for
7333 -- tagged and untagged record types. Unfortunately this is not quite
7334 -- possible because the semantics of representation clauses is different
7335 -- for tagged and untagged records under inheritance. Consider the
7336 -- following:
7337
7338 -- type R (...) is [tagged] record ... end record;
7339 -- type T (...) is new R (...) [with ...];
7340
7341 -- The representation clauses for T can specify a completely different
7342 -- record layout from R's. Hence the same component can be placed in two
7343 -- very different positions in objects of type T and R. If R and T are
7344 -- tagged types, representation clauses for T can only specify the layout
7345 -- of non inherited components, thus components that are common in R and T
7346 -- have the same position in objects of type R and T.
7347
7348 -- This has two implications. The first is that the entire tree for R's
7349 -- declaration needs to be copied for T in the untagged case, so that T
7350 -- can be viewed as a record type of its own with its own representation
7351 -- clauses. The second implication is the way we handle discriminants.
7352 -- Specifically, in the untagged case we need a way to communicate to Gigi
7353 -- what are the real discriminants in the record, while for the semantics
7354 -- we need to consider those introduced by the user to rename the
7355 -- discriminants in the parent type. This is handled by introducing the
7356 -- notion of stored discriminants. See below for more.
7357
7358 -- Fortunately the way regular components are inherited can be handled in
7359 -- the same way in tagged and untagged types.
7360
7361 -- To complicate things a bit more the private view of a private extension
7362 -- cannot be handled in the same way as the full view (for one thing the
7363 -- semantic rules are somewhat different). We will explain what differs
7364 -- below.
7365
7366 -- 2. DISCRIMINANTS UNDER INHERITANCE
7367
7368 -- The semantic rules governing the discriminants of derived types are
7369 -- quite subtle.
7370
7371 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7372 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7373
7374 -- If parent type has discriminants, then the discriminants that are
7375 -- declared in the derived type are [3.4 (11)]:
7376
7377 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7378 -- there is one;
7379
7380 -- o Otherwise, each discriminant of the parent type (implicitly declared
7381 -- in the same order with the same specifications). In this case, the
7382 -- discriminants are said to be "inherited", or if unknown in the parent
7383 -- are also unknown in the derived type.
7384
7385 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7386
7387 -- o The parent subtype must be constrained;
7388
7389 -- o If the parent type is not a tagged type, then each discriminant of
7390 -- the derived type must be used in the constraint defining a parent
7391 -- subtype. [Implementation note: This ensures that the new discriminant
7392 -- can share storage with an existing discriminant.]
7393
7394 -- For the derived type each discriminant of the parent type is either
7395 -- inherited, constrained to equal some new discriminant of the derived
7396 -- type, or constrained to the value of an expression.
7397
7398 -- When inherited or constrained to equal some new discriminant, the
7399 -- parent discriminant and the discriminant of the derived type are said
7400 -- to "correspond".
7401
7402 -- If a discriminant of the parent type is constrained to a specific value
7403 -- in the derived type definition, then the discriminant is said to be
7404 -- "specified" by that derived type definition.
7405
7406 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7407
7408 -- We have spoken about stored discriminants in point 1 (introduction)
7409 -- above. There are two sort of stored discriminants: implicit and
7410 -- explicit. As long as the derived type inherits the same discriminants as
7411 -- the root record type, stored discriminants are the same as regular
7412 -- discriminants, and are said to be implicit. However, if any discriminant
7413 -- in the root type was renamed in the derived type, then the derived
7414 -- type will contain explicit stored discriminants. Explicit stored
7415 -- discriminants are discriminants in addition to the semantically visible
7416 -- discriminants defined for the derived type. Stored discriminants are
7417 -- used by Gigi to figure out what are the physical discriminants in
7418 -- objects of the derived type (see precise definition in einfo.ads).
7419 -- As an example, consider the following:
7420
7421 -- type R (D1, D2, D3 : Int) is record ... end record;
7422 -- type T1 is new R;
7423 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7424 -- type T3 is new T2;
7425 -- type T4 (Y : Int) is new T3 (Y, 99);
7426
7427 -- The following table summarizes the discriminants and stored
7428 -- discriminants in R and T1 through T4.
7429
7430 -- Type Discrim Stored Discrim Comment
7431 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7432 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7433 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7434 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7435 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7436
7437 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7438 -- find the corresponding discriminant in the parent type, while
7439 -- Original_Record_Component (abbreviated ORC below), the actual physical
7440 -- component that is renamed. Finally the field Is_Completely_Hidden
7441 -- (abbreviated ICH below) is set for all explicit stored discriminants
7442 -- (see einfo.ads for more info). For the above example this gives:
7443
7444 -- Discrim CD ORC ICH
7445 -- ^^^^^^^ ^^ ^^^ ^^^
7446 -- D1 in R empty itself no
7447 -- D2 in R empty itself no
7448 -- D3 in R empty itself no
7449
7450 -- D1 in T1 D1 in R itself no
7451 -- D2 in T1 D2 in R itself no
7452 -- D3 in T1 D3 in R itself no
7453
7454 -- X1 in T2 D3 in T1 D3 in T2 no
7455 -- X2 in T2 D1 in T1 D1 in T2 no
7456 -- D1 in T2 empty itself yes
7457 -- D2 in T2 empty itself yes
7458 -- D3 in T2 empty itself yes
7459
7460 -- X1 in T3 X1 in T2 D3 in T3 no
7461 -- X2 in T3 X2 in T2 D1 in T3 no
7462 -- D1 in T3 empty itself yes
7463 -- D2 in T3 empty itself yes
7464 -- D3 in T3 empty itself yes
7465
7466 -- Y in T4 X1 in T3 D3 in T3 no
7467 -- D1 in T3 empty itself yes
7468 -- D2 in T3 empty itself yes
7469 -- D3 in T3 empty itself yes
7470
7471 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7472
7473 -- Type derivation for tagged types is fairly straightforward. If no
7474 -- discriminants are specified by the derived type, these are inherited
7475 -- from the parent. No explicit stored discriminants are ever necessary.
7476 -- The only manipulation that is done to the tree is that of adding a
7477 -- _parent field with parent type and constrained to the same constraint
7478 -- specified for the parent in the derived type definition. For instance:
7479
7480 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7481 -- type T1 is new R with null record;
7482 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7483
7484 -- are changed into:
7485
7486 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7487 -- _parent : R (D1, D2, D3);
7488 -- end record;
7489
7490 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7491 -- _parent : T1 (X2, 88, X1);
7492 -- end record;
7493
7494 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7495 -- ORC and ICH fields are:
7496
7497 -- Discrim CD ORC ICH
7498 -- ^^^^^^^ ^^ ^^^ ^^^
7499 -- D1 in R empty itself no
7500 -- D2 in R empty itself no
7501 -- D3 in R empty itself no
7502
7503 -- D1 in T1 D1 in R D1 in R no
7504 -- D2 in T1 D2 in R D2 in R no
7505 -- D3 in T1 D3 in R D3 in R no
7506
7507 -- X1 in T2 D3 in T1 D3 in R no
7508 -- X2 in T2 D1 in T1 D1 in R no
7509
7510 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7511 --
7512 -- Regardless of whether we dealing with a tagged or untagged type
7513 -- we will transform all derived type declarations of the form
7514 --
7515 -- type T is new R (...) [with ...];
7516 -- or
7517 -- subtype S is R (...);
7518 -- type T is new S [with ...];
7519 -- into
7520 -- type BT is new R [with ...];
7521 -- subtype T is BT (...);
7522 --
7523 -- That is, the base derived type is constrained only if it has no
7524 -- discriminants. The reason for doing this is that GNAT's semantic model
7525 -- assumes that a base type with discriminants is unconstrained.
7526 --
7527 -- Note that, strictly speaking, the above transformation is not always
7528 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7529 --
7530 -- procedure B34011A is
7531 -- type REC (D : integer := 0) is record
7532 -- I : Integer;
7533 -- end record;
7534
7535 -- package P is
7536 -- type T6 is new Rec;
7537 -- function F return T6;
7538 -- end P;
7539
7540 -- use P;
7541 -- package Q6 is
7542 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7543 -- end Q6;
7544 --
7545 -- The definition of Q6.U is illegal. However transforming Q6.U into
7546
7547 -- type BaseU is new T6;
7548 -- subtype U is BaseU (Q6.F.I)
7549
7550 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7551 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7552 -- the transformation described above.
7553
7554 -- There is another instance where the above transformation is incorrect.
7555 -- Consider:
7556
7557 -- package Pack is
7558 -- type Base (D : Integer) is tagged null record;
7559 -- procedure P (X : Base);
7560
7561 -- type Der is new Base (2) with null record;
7562 -- procedure P (X : Der);
7563 -- end Pack;
7564
7565 -- Then the above transformation turns this into
7566
7567 -- type Der_Base is new Base with null record;
7568 -- -- procedure P (X : Base) is implicitly inherited here
7569 -- -- as procedure P (X : Der_Base).
7570
7571 -- subtype Der is Der_Base (2);
7572 -- procedure P (X : Der);
7573 -- -- The overriding of P (X : Der_Base) is illegal since we
7574 -- -- have a parameter conformance problem.
7575
7576 -- To get around this problem, after having semantically processed Der_Base
7577 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7578 -- Discriminant_Constraint from Der so that when parameter conformance is
7579 -- checked when P is overridden, no semantic errors are flagged.
7580
7581 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7582
7583 -- Regardless of whether we are dealing with a tagged or untagged type
7584 -- we will transform all derived type declarations of the form
7585
7586 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7587 -- type T is new R [with ...];
7588 -- into
7589 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7590
7591 -- The reason for such transformation is that it allows us to implement a
7592 -- very clean form of component inheritance as explained below.
7593
7594 -- Note that this transformation is not achieved by direct tree rewriting
7595 -- and manipulation, but rather by redoing the semantic actions that the
7596 -- above transformation will entail. This is done directly in routine
7597 -- Inherit_Components.
7598
7599 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7600
7601 -- In both tagged and untagged derived types, regular non discriminant
7602 -- components are inherited in the derived type from the parent type. In
7603 -- the absence of discriminants component, inheritance is straightforward
7604 -- as components can simply be copied from the parent.
7605
7606 -- If the parent has discriminants, inheriting components constrained with
7607 -- these discriminants requires caution. Consider the following example:
7608
7609 -- type R (D1, D2 : Positive) is [tagged] record
7610 -- S : String (D1 .. D2);
7611 -- end record;
7612
7613 -- type T1 is new R [with null record];
7614 -- type T2 (X : positive) is new R (1, X) [with null record];
7615
7616 -- As explained in 6. above, T1 is rewritten as
7617 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7618 -- which makes the treatment for T1 and T2 identical.
7619
7620 -- What we want when inheriting S, is that references to D1 and D2 in R are
7621 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7622 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7623 -- with either discriminant references in the derived type or expressions.
7624 -- This replacement is achieved as follows: before inheriting R's
7625 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7626 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7627 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7628 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7629 -- by String (1 .. X).
7630
7631 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7632
7633 -- We explain here the rules governing private type extensions relevant to
7634 -- type derivation. These rules are explained on the following example:
7635
7636 -- type D [(...)] is new A [(...)] with private; <-- partial view
7637 -- type D [(...)] is new P [(...)] with null record; <-- full view
7638
7639 -- Type A is called the ancestor subtype of the private extension.
7640 -- Type P is the parent type of the full view of the private extension. It
7641 -- must be A or a type derived from A.
7642
7643 -- The rules concerning the discriminants of private type extensions are
7644 -- [7.3(10-13)]:
7645
7646 -- o If a private extension inherits known discriminants from the ancestor
7647 -- subtype, then the full view must also inherit its discriminants from
7648 -- the ancestor subtype and the parent subtype of the full view must be
7649 -- constrained if and only if the ancestor subtype is constrained.
7650
7651 -- o If a partial view has unknown discriminants, then the full view may
7652 -- define a definite or an indefinite subtype, with or without
7653 -- discriminants.
7654
7655 -- o If a partial view has neither known nor unknown discriminants, then
7656 -- the full view must define a definite subtype.
7657
7658 -- o If the ancestor subtype of a private extension has constrained
7659 -- discriminants, then the parent subtype of the full view must impose a
7660 -- statically matching constraint on those discriminants.
7661
7662 -- This means that only the following forms of private extensions are
7663 -- allowed:
7664
7665 -- type D is new A with private; <-- partial view
7666 -- type D is new P with null record; <-- full view
7667
7668 -- If A has no discriminants than P has no discriminants, otherwise P must
7669 -- inherit A's discriminants.
7670
7671 -- type D is new A (...) with private; <-- partial view
7672 -- type D is new P (:::) with null record; <-- full view
7673
7674 -- P must inherit A's discriminants and (...) and (:::) must statically
7675 -- match.
7676
7677 -- subtype A is R (...);
7678 -- type D is new A with private; <-- partial view
7679 -- type D is new P with null record; <-- full view
7680
7681 -- P must have inherited R's discriminants and must be derived from A or
7682 -- any of its subtypes.
7683
7684 -- type D (..) is new A with private; <-- partial view
7685 -- type D (..) is new P [(:::)] with null record; <-- full view
7686
7687 -- No specific constraints on P's discriminants or constraint (:::).
7688 -- Note that A can be unconstrained, but the parent subtype P must either
7689 -- be constrained or (:::) must be present.
7690
7691 -- type D (..) is new A [(...)] with private; <-- partial view
7692 -- type D (..) is new P [(:::)] with null record; <-- full view
7693
7694 -- P's constraints on A's discriminants must statically match those
7695 -- imposed by (...).
7696
7697 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7698
7699 -- The full view of a private extension is handled exactly as described
7700 -- above. The model chose for the private view of a private extension is
7701 -- the same for what concerns discriminants (i.e. they receive the same
7702 -- treatment as in the tagged case). However, the private view of the
7703 -- private extension always inherits the components of the parent base,
7704 -- without replacing any discriminant reference. Strictly speaking this is
7705 -- incorrect. However, Gigi never uses this view to generate code so this
7706 -- is a purely semantic issue. In theory, a set of transformations similar
7707 -- to those given in 5. and 6. above could be applied to private views of
7708 -- private extensions to have the same model of component inheritance as
7709 -- for non private extensions. However, this is not done because it would
7710 -- further complicate private type processing. Semantically speaking, this
7711 -- leaves us in an uncomfortable situation. As an example consider:
7712
7713 -- package Pack is
7714 -- type R (D : integer) is tagged record
7715 -- S : String (1 .. D);
7716 -- end record;
7717 -- procedure P (X : R);
7718 -- type T is new R (1) with private;
7719 -- private
7720 -- type T is new R (1) with null record;
7721 -- end;
7722
7723 -- This is transformed into:
7724
7725 -- package Pack is
7726 -- type R (D : integer) is tagged record
7727 -- S : String (1 .. D);
7728 -- end record;
7729 -- procedure P (X : R);
7730 -- type T is new R (1) with private;
7731 -- private
7732 -- type BaseT is new R with null record;
7733 -- subtype T is BaseT (1);
7734 -- end;
7735
7736 -- (strictly speaking the above is incorrect Ada)
7737
7738 -- From the semantic standpoint the private view of private extension T
7739 -- should be flagged as constrained since one can clearly have
7740 --
7741 -- Obj : T;
7742 --
7743 -- in a unit withing Pack. However, when deriving subprograms for the
7744 -- private view of private extension T, T must be seen as unconstrained
7745 -- since T has discriminants (this is a constraint of the current
7746 -- subprogram derivation model). Thus, when processing the private view of
7747 -- a private extension such as T, we first mark T as unconstrained, we
7748 -- process it, we perform program derivation and just before returning from
7749 -- Build_Derived_Record_Type we mark T as constrained.
7750
7751 -- ??? Are there are other uncomfortable cases that we will have to
7752 -- deal with.
7753
7754 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7755
7756 -- Types that are derived from a visible record type and have a private
7757 -- extension present other peculiarities. They behave mostly like private
7758 -- types, but if they have primitive operations defined, these will not
7759 -- have the proper signatures for further inheritance, because other
7760 -- primitive operations will use the implicit base that we define for
7761 -- private derivations below. This affect subprogram inheritance (see
7762 -- Derive_Subprograms for details). We also derive the implicit base from
7763 -- the base type of the full view, so that the implicit base is a record
7764 -- type and not another private type, This avoids infinite loops.
7765
7766 procedure Build_Derived_Record_Type
7767 (N : Node_Id;
7768 Parent_Type : Entity_Id;
7769 Derived_Type : Entity_Id;
7770 Derive_Subps : Boolean := True)
7771 is
7772 Discriminant_Specs : constant Boolean :=
7773 Present (Discriminant_Specifications (N));
7774 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7775 Loc : constant Source_Ptr := Sloc (N);
7776 Private_Extension : constant Boolean :=
7777 Nkind (N) = N_Private_Extension_Declaration;
7778 Assoc_List : Elist_Id;
7779 Constraint_Present : Boolean;
7780 Constrs : Elist_Id;
7781 Discrim : Entity_Id;
7782 Indic : Node_Id;
7783 Inherit_Discrims : Boolean := False;
7784 Last_Discrim : Entity_Id;
7785 New_Base : Entity_Id;
7786 New_Decl : Node_Id;
7787 New_Discrs : Elist_Id;
7788 New_Indic : Node_Id;
7789 Parent_Base : Entity_Id;
7790 Save_Etype : Entity_Id;
7791 Save_Discr_Constr : Elist_Id;
7792 Save_Next_Entity : Entity_Id;
7793 Type_Def : Node_Id;
7794
7795 Discs : Elist_Id := New_Elmt_List;
7796 -- An empty Discs list means that there were no constraints in the
7797 -- subtype indication or that there was an error processing it.
7798
7799 begin
7800 if Ekind (Parent_Type) = E_Record_Type_With_Private
7801 and then Present (Full_View (Parent_Type))
7802 and then Has_Discriminants (Parent_Type)
7803 then
7804 Parent_Base := Base_Type (Full_View (Parent_Type));
7805 else
7806 Parent_Base := Base_Type (Parent_Type);
7807 end if;
7808
7809 -- AI05-0115 : if this is a derivation from a private type in some
7810 -- other scope that may lead to invisible components for the derived
7811 -- type, mark it accordingly.
7812
7813 if Is_Private_Type (Parent_Type) then
7814 if Scope (Parent_Type) = Scope (Derived_Type) then
7815 null;
7816
7817 elsif In_Open_Scopes (Scope (Parent_Type))
7818 and then In_Private_Part (Scope (Parent_Type))
7819 then
7820 null;
7821
7822 else
7823 Set_Has_Private_Ancestor (Derived_Type);
7824 end if;
7825
7826 else
7827 Set_Has_Private_Ancestor
7828 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7829 end if;
7830
7831 -- Before we start the previously documented transformations, here is
7832 -- little fix for size and alignment of tagged types. Normally when we
7833 -- derive type D from type P, we copy the size and alignment of P as the
7834 -- default for D, and in the absence of explicit representation clauses
7835 -- for D, the size and alignment are indeed the same as the parent.
7836
7837 -- But this is wrong for tagged types, since fields may be added, and
7838 -- the default size may need to be larger, and the default alignment may
7839 -- need to be larger.
7840
7841 -- We therefore reset the size and alignment fields in the tagged case.
7842 -- Note that the size and alignment will in any case be at least as
7843 -- large as the parent type (since the derived type has a copy of the
7844 -- parent type in the _parent field)
7845
7846 -- The type is also marked as being tagged here, which is needed when
7847 -- processing components with a self-referential anonymous access type
7848 -- in the call to Check_Anonymous_Access_Components below. Note that
7849 -- this flag is also set later on for completeness.
7850
7851 if Is_Tagged then
7852 Set_Is_Tagged_Type (Derived_Type);
7853 Init_Size_Align (Derived_Type);
7854 end if;
7855
7856 -- STEP 0a: figure out what kind of derived type declaration we have
7857
7858 if Private_Extension then
7859 Type_Def := N;
7860 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7861 Set_Default_SSO (Derived_Type);
7862
7863 else
7864 Type_Def := Type_Definition (N);
7865
7866 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7867 -- Parent_Base can be a private type or private extension. However,
7868 -- for tagged types with an extension the newly added fields are
7869 -- visible and hence the Derived_Type is always an E_Record_Type.
7870 -- (except that the parent may have its own private fields).
7871 -- For untagged types we preserve the Ekind of the Parent_Base.
7872
7873 if Present (Record_Extension_Part (Type_Def)) then
7874 Set_Ekind (Derived_Type, E_Record_Type);
7875 Set_Default_SSO (Derived_Type);
7876
7877 -- Create internal access types for components with anonymous
7878 -- access types.
7879
7880 if Ada_Version >= Ada_2005 then
7881 Check_Anonymous_Access_Components
7882 (N, Derived_Type, Derived_Type,
7883 Component_List (Record_Extension_Part (Type_Def)));
7884 end if;
7885
7886 else
7887 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7888 end if;
7889 end if;
7890
7891 -- Indic can either be an N_Identifier if the subtype indication
7892 -- contains no constraint or an N_Subtype_Indication if the subtype
7893 -- indication has a constraint.
7894
7895 Indic := Subtype_Indication (Type_Def);
7896 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
7897
7898 -- Check that the type has visible discriminants. The type may be
7899 -- a private type with unknown discriminants whose full view has
7900 -- discriminants which are invisible.
7901
7902 if Constraint_Present then
7903 if not Has_Discriminants (Parent_Base)
7904 or else
7905 (Has_Unknown_Discriminants (Parent_Base)
7906 and then Is_Private_Type (Parent_Base))
7907 then
7908 Error_Msg_N
7909 ("invalid constraint: type has no discriminant",
7910 Constraint (Indic));
7911
7912 Constraint_Present := False;
7913 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7914
7915 elsif Is_Constrained (Parent_Type) then
7916 Error_Msg_N
7917 ("invalid constraint: parent type is already constrained",
7918 Constraint (Indic));
7919
7920 Constraint_Present := False;
7921 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7922 end if;
7923 end if;
7924
7925 -- STEP 0b: If needed, apply transformation given in point 5. above
7926
7927 if not Private_Extension
7928 and then Has_Discriminants (Parent_Type)
7929 and then not Discriminant_Specs
7930 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7931 then
7932 -- First, we must analyze the constraint (see comment in point 5.)
7933 -- The constraint may come from the subtype indication of the full
7934 -- declaration.
7935
7936 if Constraint_Present then
7937 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
7938
7939 -- If there is no explicit constraint, there might be one that is
7940 -- inherited from a constrained parent type. In that case verify that
7941 -- it conforms to the constraint in the partial view. In perverse
7942 -- cases the parent subtypes of the partial and full view can have
7943 -- different constraints.
7944
7945 elsif Present (Stored_Constraint (Parent_Type)) then
7946 New_Discrs := Stored_Constraint (Parent_Type);
7947
7948 else
7949 New_Discrs := No_Elist;
7950 end if;
7951
7952 if Has_Discriminants (Derived_Type)
7953 and then Has_Private_Declaration (Derived_Type)
7954 and then Present (Discriminant_Constraint (Derived_Type))
7955 and then Present (New_Discrs)
7956 then
7957 -- Verify that constraints of the full view statically match
7958 -- those given in the partial view.
7959
7960 declare
7961 C1, C2 : Elmt_Id;
7962
7963 begin
7964 C1 := First_Elmt (New_Discrs);
7965 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
7966 while Present (C1) and then Present (C2) loop
7967 if Fully_Conformant_Expressions (Node (C1), Node (C2))
7968 or else
7969 (Is_OK_Static_Expression (Node (C1))
7970 and then Is_OK_Static_Expression (Node (C2))
7971 and then
7972 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
7973 then
7974 null;
7975
7976 else
7977 if Constraint_Present then
7978 Error_Msg_N
7979 ("constraint not conformant to previous declaration",
7980 Node (C1));
7981 else
7982 Error_Msg_N
7983 ("constraint of full view is incompatible "
7984 & "with partial view", N);
7985 end if;
7986 end if;
7987
7988 Next_Elmt (C1);
7989 Next_Elmt (C2);
7990 end loop;
7991 end;
7992 end if;
7993
7994 -- Insert and analyze the declaration for the unconstrained base type
7995
7996 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7997
7998 New_Decl :=
7999 Make_Full_Type_Declaration (Loc,
8000 Defining_Identifier => New_Base,
8001 Type_Definition =>
8002 Make_Derived_Type_Definition (Loc,
8003 Abstract_Present => Abstract_Present (Type_Def),
8004 Limited_Present => Limited_Present (Type_Def),
8005 Subtype_Indication =>
8006 New_Occurrence_Of (Parent_Base, Loc),
8007 Record_Extension_Part =>
8008 Relocate_Node (Record_Extension_Part (Type_Def)),
8009 Interface_List => Interface_List (Type_Def)));
8010
8011 Set_Parent (New_Decl, Parent (N));
8012 Mark_Rewrite_Insertion (New_Decl);
8013 Insert_Before (N, New_Decl);
8014
8015 -- In the extension case, make sure ancestor is frozen appropriately
8016 -- (see also non-discriminated case below).
8017
8018 if Present (Record_Extension_Part (Type_Def))
8019 or else Is_Interface (Parent_Base)
8020 then
8021 Freeze_Before (New_Decl, Parent_Type);
8022 end if;
8023
8024 -- Note that this call passes False for the Derive_Subps parameter
8025 -- because subprogram derivation is deferred until after creating
8026 -- the subtype (see below).
8027
8028 Build_Derived_Type
8029 (New_Decl, Parent_Base, New_Base,
8030 Is_Completion => False, Derive_Subps => False);
8031
8032 -- ??? This needs re-examination to determine whether the
8033 -- above call can simply be replaced by a call to Analyze.
8034
8035 Set_Analyzed (New_Decl);
8036
8037 -- Insert and analyze the declaration for the constrained subtype
8038
8039 if Constraint_Present then
8040 New_Indic :=
8041 Make_Subtype_Indication (Loc,
8042 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8043 Constraint => Relocate_Node (Constraint (Indic)));
8044
8045 else
8046 declare
8047 Constr_List : constant List_Id := New_List;
8048 C : Elmt_Id;
8049 Expr : Node_Id;
8050
8051 begin
8052 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8053 while Present (C) loop
8054 Expr := Node (C);
8055
8056 -- It is safe here to call New_Copy_Tree since
8057 -- Force_Evaluation was called on each constraint in
8058 -- Build_Discriminant_Constraints.
8059
8060 Append (New_Copy_Tree (Expr), To => Constr_List);
8061
8062 Next_Elmt (C);
8063 end loop;
8064
8065 New_Indic :=
8066 Make_Subtype_Indication (Loc,
8067 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8068 Constraint =>
8069 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8070 end;
8071 end if;
8072
8073 Rewrite (N,
8074 Make_Subtype_Declaration (Loc,
8075 Defining_Identifier => Derived_Type,
8076 Subtype_Indication => New_Indic));
8077
8078 Analyze (N);
8079
8080 -- Derivation of subprograms must be delayed until the full subtype
8081 -- has been established, to ensure proper overriding of subprograms
8082 -- inherited by full types. If the derivations occurred as part of
8083 -- the call to Build_Derived_Type above, then the check for type
8084 -- conformance would fail because earlier primitive subprograms
8085 -- could still refer to the full type prior the change to the new
8086 -- subtype and hence would not match the new base type created here.
8087 -- Subprograms are not derived, however, when Derive_Subps is False
8088 -- (since otherwise there could be redundant derivations).
8089
8090 if Derive_Subps then
8091 Derive_Subprograms (Parent_Type, Derived_Type);
8092 end if;
8093
8094 -- For tagged types the Discriminant_Constraint of the new base itype
8095 -- is inherited from the first subtype so that no subtype conformance
8096 -- problem arise when the first subtype overrides primitive
8097 -- operations inherited by the implicit base type.
8098
8099 if Is_Tagged then
8100 Set_Discriminant_Constraint
8101 (New_Base, Discriminant_Constraint (Derived_Type));
8102 end if;
8103
8104 return;
8105 end if;
8106
8107 -- If we get here Derived_Type will have no discriminants or it will be
8108 -- a discriminated unconstrained base type.
8109
8110 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8111
8112 if Is_Tagged then
8113
8114 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8115 -- The declaration of a specific descendant of an interface type
8116 -- freezes the interface type (RM 13.14).
8117
8118 if not Private_Extension or else Is_Interface (Parent_Base) then
8119 Freeze_Before (N, Parent_Type);
8120 end if;
8121
8122 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8123 -- cannot be declared at a deeper level than its parent type is
8124 -- removed. The check on derivation within a generic body is also
8125 -- relaxed, but there's a restriction that a derived tagged type
8126 -- cannot be declared in a generic body if it's derived directly
8127 -- or indirectly from a formal type of that generic.
8128
8129 if Ada_Version >= Ada_2005 then
8130 if Present (Enclosing_Generic_Body (Derived_Type)) then
8131 declare
8132 Ancestor_Type : Entity_Id;
8133
8134 begin
8135 -- Check to see if any ancestor of the derived type is a
8136 -- formal type.
8137
8138 Ancestor_Type := Parent_Type;
8139 while not Is_Generic_Type (Ancestor_Type)
8140 and then Etype (Ancestor_Type) /= Ancestor_Type
8141 loop
8142 Ancestor_Type := Etype (Ancestor_Type);
8143 end loop;
8144
8145 -- If the derived type does have a formal type as an
8146 -- ancestor, then it's an error if the derived type is
8147 -- declared within the body of the generic unit that
8148 -- declares the formal type in its generic formal part. It's
8149 -- sufficient to check whether the ancestor type is declared
8150 -- inside the same generic body as the derived type (such as
8151 -- within a nested generic spec), in which case the
8152 -- derivation is legal. If the formal type is declared
8153 -- outside of that generic body, then it's guaranteed that
8154 -- the derived type is declared within the generic body of
8155 -- the generic unit declaring the formal type.
8156
8157 if Is_Generic_Type (Ancestor_Type)
8158 and then Enclosing_Generic_Body (Ancestor_Type) /=
8159 Enclosing_Generic_Body (Derived_Type)
8160 then
8161 Error_Msg_NE
8162 ("parent type of& must not be descendant of formal type"
8163 & " of an enclosing generic body",
8164 Indic, Derived_Type);
8165 end if;
8166 end;
8167 end if;
8168
8169 elsif Type_Access_Level (Derived_Type) /=
8170 Type_Access_Level (Parent_Type)
8171 and then not Is_Generic_Type (Derived_Type)
8172 then
8173 if Is_Controlled (Parent_Type) then
8174 Error_Msg_N
8175 ("controlled type must be declared at the library level",
8176 Indic);
8177 else
8178 Error_Msg_N
8179 ("type extension at deeper accessibility level than parent",
8180 Indic);
8181 end if;
8182
8183 else
8184 declare
8185 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8186 begin
8187 if Present (GB)
8188 and then GB /= Enclosing_Generic_Body (Parent_Base)
8189 then
8190 Error_Msg_NE
8191 ("parent type of& must not be outside generic body"
8192 & " (RM 3.9.1(4))",
8193 Indic, Derived_Type);
8194 end if;
8195 end;
8196 end if;
8197 end if;
8198
8199 -- Ada 2005 (AI-251)
8200
8201 if Ada_Version >= Ada_2005 and then Is_Tagged then
8202
8203 -- "The declaration of a specific descendant of an interface type
8204 -- freezes the interface type" (RM 13.14).
8205
8206 declare
8207 Iface : Node_Id;
8208 begin
8209 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8210 Iface := First (Interface_List (Type_Def));
8211 while Present (Iface) loop
8212 Freeze_Before (N, Etype (Iface));
8213 Next (Iface);
8214 end loop;
8215 end if;
8216 end;
8217 end if;
8218
8219 -- STEP 1b : preliminary cleanup of the full view of private types
8220
8221 -- If the type is already marked as having discriminants, then it's the
8222 -- completion of a private type or private extension and we need to
8223 -- retain the discriminants from the partial view if the current
8224 -- declaration has Discriminant_Specifications so that we can verify
8225 -- conformance. However, we must remove any existing components that
8226 -- were inherited from the parent (and attached in Copy_And_Swap)
8227 -- because the full type inherits all appropriate components anyway, and
8228 -- we do not want the partial view's components interfering.
8229
8230 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8231 Discrim := First_Discriminant (Derived_Type);
8232 loop
8233 Last_Discrim := Discrim;
8234 Next_Discriminant (Discrim);
8235 exit when No (Discrim);
8236 end loop;
8237
8238 Set_Last_Entity (Derived_Type, Last_Discrim);
8239
8240 -- In all other cases wipe out the list of inherited components (even
8241 -- inherited discriminants), it will be properly rebuilt here.
8242
8243 else
8244 Set_First_Entity (Derived_Type, Empty);
8245 Set_Last_Entity (Derived_Type, Empty);
8246 end if;
8247
8248 -- STEP 1c: Initialize some flags for the Derived_Type
8249
8250 -- The following flags must be initialized here so that
8251 -- Process_Discriminants can check that discriminants of tagged types do
8252 -- not have a default initial value and that access discriminants are
8253 -- only specified for limited records. For completeness, these flags are
8254 -- also initialized along with all the other flags below.
8255
8256 -- AI-419: Limitedness is not inherited from an interface parent, so to
8257 -- be limited in that case the type must be explicitly declared as
8258 -- limited. However, task and protected interfaces are always limited.
8259
8260 if Limited_Present (Type_Def) then
8261 Set_Is_Limited_Record (Derived_Type);
8262
8263 elsif Is_Limited_Record (Parent_Type)
8264 or else (Present (Full_View (Parent_Type))
8265 and then Is_Limited_Record (Full_View (Parent_Type)))
8266 then
8267 if not Is_Interface (Parent_Type)
8268 or else Is_Synchronized_Interface (Parent_Type)
8269 or else Is_Protected_Interface (Parent_Type)
8270 or else Is_Task_Interface (Parent_Type)
8271 then
8272 Set_Is_Limited_Record (Derived_Type);
8273 end if;
8274 end if;
8275
8276 -- STEP 2a: process discriminants of derived type if any
8277
8278 Push_Scope (Derived_Type);
8279
8280 if Discriminant_Specs then
8281 Set_Has_Unknown_Discriminants (Derived_Type, False);
8282
8283 -- The following call initializes fields Has_Discriminants and
8284 -- Discriminant_Constraint, unless we are processing the completion
8285 -- of a private type declaration.
8286
8287 Check_Or_Process_Discriminants (N, Derived_Type);
8288
8289 -- For untagged types, the constraint on the Parent_Type must be
8290 -- present and is used to rename the discriminants.
8291
8292 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8293 Error_Msg_N ("untagged parent must have discriminants", Indic);
8294
8295 elsif not Is_Tagged and then not Constraint_Present then
8296 Error_Msg_N
8297 ("discriminant constraint needed for derived untagged records",
8298 Indic);
8299
8300 -- Otherwise the parent subtype must be constrained unless we have a
8301 -- private extension.
8302
8303 elsif not Constraint_Present
8304 and then not Private_Extension
8305 and then not Is_Constrained (Parent_Type)
8306 then
8307 Error_Msg_N
8308 ("unconstrained type not allowed in this context", Indic);
8309
8310 elsif Constraint_Present then
8311 -- The following call sets the field Corresponding_Discriminant
8312 -- for the discriminants in the Derived_Type.
8313
8314 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8315
8316 -- For untagged types all new discriminants must rename
8317 -- discriminants in the parent. For private extensions new
8318 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8319
8320 Discrim := First_Discriminant (Derived_Type);
8321 while Present (Discrim) loop
8322 if not Is_Tagged
8323 and then No (Corresponding_Discriminant (Discrim))
8324 then
8325 Error_Msg_N
8326 ("new discriminants must constrain old ones", Discrim);
8327
8328 elsif Private_Extension
8329 and then Present (Corresponding_Discriminant (Discrim))
8330 then
8331 Error_Msg_N
8332 ("only static constraints allowed for parent"
8333 & " discriminants in the partial view", Indic);
8334 exit;
8335 end if;
8336
8337 -- If a new discriminant is used in the constraint, then its
8338 -- subtype must be statically compatible with the parent
8339 -- discriminant's subtype (3.7(15)).
8340
8341 -- However, if the record contains an array constrained by
8342 -- the discriminant but with some different bound, the compiler
8343 -- attemps to create a smaller range for the discriminant type.
8344 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8345 -- the discriminant type is a scalar type, the check must use
8346 -- the original discriminant type in the parent declaration.
8347
8348 declare
8349 Corr_Disc : constant Entity_Id :=
8350 Corresponding_Discriminant (Discrim);
8351 Disc_Type : constant Entity_Id := Etype (Discrim);
8352 Corr_Type : Entity_Id;
8353
8354 begin
8355 if Present (Corr_Disc) then
8356 if Is_Scalar_Type (Disc_Type) then
8357 Corr_Type :=
8358 Entity (Discriminant_Type (Parent (Corr_Disc)));
8359 else
8360 Corr_Type := Etype (Corr_Disc);
8361 end if;
8362
8363 if not
8364 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8365 then
8366 Error_Msg_N
8367 ("subtype must be compatible "
8368 & "with parent discriminant",
8369 Discrim);
8370 end if;
8371 end if;
8372 end;
8373
8374 Next_Discriminant (Discrim);
8375 end loop;
8376
8377 -- Check whether the constraints of the full view statically
8378 -- match those imposed by the parent subtype [7.3(13)].
8379
8380 if Present (Stored_Constraint (Derived_Type)) then
8381 declare
8382 C1, C2 : Elmt_Id;
8383
8384 begin
8385 C1 := First_Elmt (Discs);
8386 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8387 while Present (C1) and then Present (C2) loop
8388 if not
8389 Fully_Conformant_Expressions (Node (C1), Node (C2))
8390 then
8391 Error_Msg_N
8392 ("not conformant with previous declaration",
8393 Node (C1));
8394 end if;
8395
8396 Next_Elmt (C1);
8397 Next_Elmt (C2);
8398 end loop;
8399 end;
8400 end if;
8401 end if;
8402
8403 -- STEP 2b: No new discriminants, inherit discriminants if any
8404
8405 else
8406 if Private_Extension then
8407 Set_Has_Unknown_Discriminants
8408 (Derived_Type,
8409 Has_Unknown_Discriminants (Parent_Type)
8410 or else Unknown_Discriminants_Present (N));
8411
8412 -- The partial view of the parent may have unknown discriminants,
8413 -- but if the full view has discriminants and the parent type is
8414 -- in scope they must be inherited.
8415
8416 elsif Has_Unknown_Discriminants (Parent_Type)
8417 and then
8418 (not Has_Discriminants (Parent_Type)
8419 or else not In_Open_Scopes (Scope (Parent_Type)))
8420 then
8421 Set_Has_Unknown_Discriminants (Derived_Type);
8422 end if;
8423
8424 if not Has_Unknown_Discriminants (Derived_Type)
8425 and then not Has_Unknown_Discriminants (Parent_Base)
8426 and then Has_Discriminants (Parent_Type)
8427 then
8428 Inherit_Discrims := True;
8429 Set_Has_Discriminants
8430 (Derived_Type, True);
8431 Set_Discriminant_Constraint
8432 (Derived_Type, Discriminant_Constraint (Parent_Base));
8433 end if;
8434
8435 -- The following test is true for private types (remember
8436 -- transformation 5. is not applied to those) and in an error
8437 -- situation.
8438
8439 if Constraint_Present then
8440 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8441 end if;
8442
8443 -- For now mark a new derived type as constrained only if it has no
8444 -- discriminants. At the end of Build_Derived_Record_Type we properly
8445 -- set this flag in the case of private extensions. See comments in
8446 -- point 9. just before body of Build_Derived_Record_Type.
8447
8448 Set_Is_Constrained
8449 (Derived_Type,
8450 not (Inherit_Discrims
8451 or else Has_Unknown_Discriminants (Derived_Type)));
8452 end if;
8453
8454 -- STEP 3: initialize fields of derived type
8455
8456 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8457 Set_Stored_Constraint (Derived_Type, No_Elist);
8458
8459 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8460 -- but cannot be interfaces
8461
8462 if not Private_Extension
8463 and then Ekind (Derived_Type) /= E_Private_Type
8464 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8465 then
8466 if Interface_Present (Type_Def) then
8467 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8468 end if;
8469
8470 Set_Interfaces (Derived_Type, No_Elist);
8471 end if;
8472
8473 -- Fields inherited from the Parent_Type
8474
8475 Set_Has_Specified_Layout
8476 (Derived_Type, Has_Specified_Layout (Parent_Type));
8477 Set_Is_Limited_Composite
8478 (Derived_Type, Is_Limited_Composite (Parent_Type));
8479 Set_Is_Private_Composite
8480 (Derived_Type, Is_Private_Composite (Parent_Type));
8481
8482 if Is_Tagged_Type (Parent_Type) then
8483 Set_No_Tagged_Streams_Pragma
8484 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8485 end if;
8486
8487 -- Fields inherited from the Parent_Base
8488
8489 Set_Has_Controlled_Component
8490 (Derived_Type, Has_Controlled_Component (Parent_Base));
8491 Set_Has_Non_Standard_Rep
8492 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8493 Set_Has_Primitive_Operations
8494 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8495
8496 -- Fields inherited from the Parent_Base in the non-private case
8497
8498 if Ekind (Derived_Type) = E_Record_Type then
8499 Set_Has_Complex_Representation
8500 (Derived_Type, Has_Complex_Representation (Parent_Base));
8501 end if;
8502
8503 -- Fields inherited from the Parent_Base for record types
8504
8505 if Is_Record_Type (Derived_Type) then
8506 declare
8507 Parent_Full : Entity_Id;
8508
8509 begin
8510 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8511 -- Parent_Base can be a private type or private extension. Go
8512 -- to the full view here to get the E_Record_Type specific flags.
8513
8514 if Present (Full_View (Parent_Base)) then
8515 Parent_Full := Full_View (Parent_Base);
8516 else
8517 Parent_Full := Parent_Base;
8518 end if;
8519
8520 Set_OK_To_Reorder_Components
8521 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8522 end;
8523 end if;
8524
8525 -- Set fields for private derived types
8526
8527 if Is_Private_Type (Derived_Type) then
8528 Set_Depends_On_Private (Derived_Type, True);
8529 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8530
8531 -- Inherit fields from non private record types. If this is the
8532 -- completion of a derivation from a private type, the parent itself
8533 -- is private, and the attributes come from its full view, which must
8534 -- be present.
8535
8536 else
8537 if Is_Private_Type (Parent_Base)
8538 and then not Is_Record_Type (Parent_Base)
8539 then
8540 Set_Component_Alignment
8541 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8542 Set_C_Pass_By_Copy
8543 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8544 else
8545 Set_Component_Alignment
8546 (Derived_Type, Component_Alignment (Parent_Base));
8547 Set_C_Pass_By_Copy
8548 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8549 end if;
8550 end if;
8551
8552 -- Set fields for tagged types
8553
8554 if Is_Tagged then
8555 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8556
8557 -- All tagged types defined in Ada.Finalization are controlled
8558
8559 if Chars (Scope (Derived_Type)) = Name_Finalization
8560 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8561 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8562 then
8563 Set_Is_Controlled (Derived_Type);
8564 else
8565 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8566 end if;
8567
8568 -- Minor optimization: there is no need to generate the class-wide
8569 -- entity associated with an underlying record view.
8570
8571 if not Is_Underlying_Record_View (Derived_Type) then
8572 Make_Class_Wide_Type (Derived_Type);
8573 end if;
8574
8575 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8576
8577 if Has_Discriminants (Derived_Type)
8578 and then Constraint_Present
8579 then
8580 Set_Stored_Constraint
8581 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8582 end if;
8583
8584 if Ada_Version >= Ada_2005 then
8585 declare
8586 Ifaces_List : Elist_Id;
8587
8588 begin
8589 -- Checks rules 3.9.4 (13/2 and 14/2)
8590
8591 if Comes_From_Source (Derived_Type)
8592 and then not Is_Private_Type (Derived_Type)
8593 and then Is_Interface (Parent_Type)
8594 and then not Is_Interface (Derived_Type)
8595 then
8596 if Is_Task_Interface (Parent_Type) then
8597 Error_Msg_N
8598 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8599 Derived_Type);
8600
8601 elsif Is_Protected_Interface (Parent_Type) then
8602 Error_Msg_N
8603 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8604 Derived_Type);
8605 end if;
8606 end if;
8607
8608 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8609
8610 Check_Interfaces (N, Type_Def);
8611
8612 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8613 -- not already in the parents.
8614
8615 Collect_Interfaces
8616 (T => Derived_Type,
8617 Ifaces_List => Ifaces_List,
8618 Exclude_Parents => True);
8619
8620 Set_Interfaces (Derived_Type, Ifaces_List);
8621
8622 -- If the derived type is the anonymous type created for
8623 -- a declaration whose parent has a constraint, propagate
8624 -- the interface list to the source type. This must be done
8625 -- prior to the completion of the analysis of the source type
8626 -- because the components in the extension may contain current
8627 -- instances whose legality depends on some ancestor.
8628
8629 if Is_Itype (Derived_Type) then
8630 declare
8631 Def : constant Node_Id :=
8632 Associated_Node_For_Itype (Derived_Type);
8633 begin
8634 if Present (Def)
8635 and then Nkind (Def) = N_Full_Type_Declaration
8636 then
8637 Set_Interfaces
8638 (Defining_Identifier (Def), Ifaces_List);
8639 end if;
8640 end;
8641 end if;
8642
8643 -- Propagate inherited invariant information of parents
8644 -- and progenitors
8645
8646 if Ada_Version >= Ada_2012
8647 and then not Is_Interface (Derived_Type)
8648 then
8649 if Has_Inheritable_Invariants (Parent_Type) then
8650 Set_Has_Invariants (Derived_Type);
8651 Set_Has_Inheritable_Invariants (Derived_Type);
8652
8653 elsif not Is_Empty_Elmt_List (Ifaces_List) then
8654 declare
8655 AI : Elmt_Id;
8656
8657 begin
8658 AI := First_Elmt (Ifaces_List);
8659 while Present (AI) loop
8660 if Has_Inheritable_Invariants (Node (AI)) then
8661 Set_Has_Invariants (Derived_Type);
8662 Set_Has_Inheritable_Invariants (Derived_Type);
8663
8664 exit;
8665 end if;
8666
8667 Next_Elmt (AI);
8668 end loop;
8669 end;
8670 end if;
8671 end if;
8672
8673 -- A type extension is automatically Ghost when one of its
8674 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8675 -- also inherited when the parent type is Ghost, but this is
8676 -- done in Build_Derived_Type as the mechanism also handles
8677 -- untagged derivations.
8678
8679 if Implements_Ghost_Interface (Derived_Type) then
8680 Set_Is_Ghost_Entity (Derived_Type);
8681 end if;
8682 end;
8683 end if;
8684
8685 else
8686 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8687 Set_Has_Non_Standard_Rep
8688 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8689 end if;
8690
8691 -- STEP 4: Inherit components from the parent base and constrain them.
8692 -- Apply the second transformation described in point 6. above.
8693
8694 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8695 or else not Has_Discriminants (Parent_Type)
8696 or else not Is_Constrained (Parent_Type)
8697 then
8698 Constrs := Discs;
8699 else
8700 Constrs := Discriminant_Constraint (Parent_Type);
8701 end if;
8702
8703 Assoc_List :=
8704 Inherit_Components
8705 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8706
8707 -- STEP 5a: Copy the parent record declaration for untagged types
8708
8709 if not Is_Tagged then
8710
8711 -- Discriminant_Constraint (Derived_Type) has been properly
8712 -- constructed. Save it and temporarily set it to Empty because we
8713 -- do not want the call to New_Copy_Tree below to mess this list.
8714
8715 if Has_Discriminants (Derived_Type) then
8716 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8717 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8718 else
8719 Save_Discr_Constr := No_Elist;
8720 end if;
8721
8722 -- Save the Etype field of Derived_Type. It is correctly set now,
8723 -- but the call to New_Copy tree may remap it to point to itself,
8724 -- which is not what we want. Ditto for the Next_Entity field.
8725
8726 Save_Etype := Etype (Derived_Type);
8727 Save_Next_Entity := Next_Entity (Derived_Type);
8728
8729 -- Assoc_List maps all stored discriminants in the Parent_Base to
8730 -- stored discriminants in the Derived_Type. It is fundamental that
8731 -- no types or itypes with discriminants other than the stored
8732 -- discriminants appear in the entities declared inside
8733 -- Derived_Type, since the back end cannot deal with it.
8734
8735 New_Decl :=
8736 New_Copy_Tree
8737 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8738
8739 -- Restore the fields saved prior to the New_Copy_Tree call
8740 -- and compute the stored constraint.
8741
8742 Set_Etype (Derived_Type, Save_Etype);
8743 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8744
8745 if Has_Discriminants (Derived_Type) then
8746 Set_Discriminant_Constraint
8747 (Derived_Type, Save_Discr_Constr);
8748 Set_Stored_Constraint
8749 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8750 Replace_Components (Derived_Type, New_Decl);
8751 Set_Has_Implicit_Dereference
8752 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8753 end if;
8754
8755 -- Insert the new derived type declaration
8756
8757 Rewrite (N, New_Decl);
8758
8759 -- STEP 5b: Complete the processing for record extensions in generics
8760
8761 -- There is no completion for record extensions declared in the
8762 -- parameter part of a generic, so we need to complete processing for
8763 -- these generic record extensions here. The Record_Type_Definition call
8764 -- will change the Ekind of the components from E_Void to E_Component.
8765
8766 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8767 Record_Type_Definition (Empty, Derived_Type);
8768
8769 -- STEP 5c: Process the record extension for non private tagged types
8770
8771 elsif not Private_Extension then
8772 Expand_Record_Extension (Derived_Type, Type_Def);
8773
8774 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8775 -- derived type to propagate some semantic information. This led
8776 -- to other ASIS failures and has been removed.
8777
8778 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8779 -- implemented interfaces if we are in expansion mode
8780
8781 if Expander_Active
8782 and then Has_Interfaces (Derived_Type)
8783 then
8784 Add_Interface_Tag_Components (N, Derived_Type);
8785 end if;
8786
8787 -- Analyze the record extension
8788
8789 Record_Type_Definition
8790 (Record_Extension_Part (Type_Def), Derived_Type);
8791 end if;
8792
8793 End_Scope;
8794
8795 -- Nothing else to do if there is an error in the derivation.
8796 -- An unusual case: the full view may be derived from a type in an
8797 -- instance, when the partial view was used illegally as an actual
8798 -- in that instance, leading to a circular definition.
8799
8800 if Etype (Derived_Type) = Any_Type
8801 or else Etype (Parent_Type) = Derived_Type
8802 then
8803 return;
8804 end if;
8805
8806 -- Set delayed freeze and then derive subprograms, we need to do
8807 -- this in this order so that derived subprograms inherit the
8808 -- derived freeze if necessary.
8809
8810 Set_Has_Delayed_Freeze (Derived_Type);
8811
8812 if Derive_Subps then
8813 Derive_Subprograms (Parent_Type, Derived_Type);
8814 end if;
8815
8816 -- If we have a private extension which defines a constrained derived
8817 -- type mark as constrained here after we have derived subprograms. See
8818 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8819
8820 if Private_Extension and then Inherit_Discrims then
8821 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8822 Set_Is_Constrained (Derived_Type, True);
8823 Set_Discriminant_Constraint (Derived_Type, Discs);
8824
8825 elsif Is_Constrained (Parent_Type) then
8826 Set_Is_Constrained
8827 (Derived_Type, True);
8828 Set_Discriminant_Constraint
8829 (Derived_Type, Discriminant_Constraint (Parent_Type));
8830 end if;
8831 end if;
8832
8833 -- Update the class-wide type, which shares the now-completed entity
8834 -- list with its specific type. In case of underlying record views,
8835 -- we do not generate the corresponding class wide entity.
8836
8837 if Is_Tagged
8838 and then not Is_Underlying_Record_View (Derived_Type)
8839 then
8840 Set_First_Entity
8841 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8842 Set_Last_Entity
8843 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8844 end if;
8845
8846 Check_Function_Writable_Actuals (N);
8847 end Build_Derived_Record_Type;
8848
8849 ------------------------
8850 -- Build_Derived_Type --
8851 ------------------------
8852
8853 procedure Build_Derived_Type
8854 (N : Node_Id;
8855 Parent_Type : Entity_Id;
8856 Derived_Type : Entity_Id;
8857 Is_Completion : Boolean;
8858 Derive_Subps : Boolean := True)
8859 is
8860 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8861
8862 begin
8863 -- Set common attributes
8864
8865 Set_Scope (Derived_Type, Current_Scope);
8866
8867 Set_Etype (Derived_Type, Parent_Base);
8868 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8869 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8870 Set_Has_Protected (Derived_Type, Has_Protected (Parent_Base));
8871
8872 Set_Size_Info (Derived_Type, Parent_Type);
8873 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8874 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8875 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8876 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
8877
8878 if Is_Tagged_Type (Derived_Type) then
8879 Set_No_Tagged_Streams_Pragma
8880 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8881 end if;
8882
8883 -- If the parent has primitive routines, set the derived type link
8884
8885 if Has_Primitive_Operations (Parent_Type) then
8886 Set_Derived_Type_Link (Parent_Base, Derived_Type);
8887 end if;
8888
8889 -- If the parent type is a private subtype, the convention on the base
8890 -- type may be set in the private part, and not propagated to the
8891 -- subtype until later, so we obtain the convention from the base type.
8892
8893 Set_Convention (Derived_Type, Convention (Parent_Base));
8894
8895 -- Set SSO default for record or array type
8896
8897 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
8898 and then Is_Base_Type (Derived_Type)
8899 then
8900 Set_Default_SSO (Derived_Type);
8901 end if;
8902
8903 -- Propagate invariant information. The new type has invariants if
8904 -- they are inherited from the parent type, and these invariants can
8905 -- be further inherited, so both flags are set.
8906
8907 -- We similarly inherit predicates
8908
8909 if Has_Predicates (Parent_Type) then
8910 Set_Has_Predicates (Derived_Type);
8911 end if;
8912
8913 -- The derived type inherits the representation clauses of the parent
8914
8915 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
8916
8917 -- Propagate the attributes related to pragma Default_Initial_Condition
8918 -- from the parent type to the private extension. A derived type always
8919 -- inherits the default initial condition flag from the parent type. If
8920 -- the derived type carries its own Default_Initial_Condition pragma,
8921 -- the flag is later reset in Analyze_Pragma. Note that both flags are
8922 -- mutually exclusive.
8923
8924 Propagate_Default_Init_Cond_Attributes
8925 (From_Typ => Parent_Type,
8926 To_Typ => Derived_Type,
8927 Parent_To_Derivation => True);
8928
8929 -- If the parent type has delayed rep aspects, then mark the derived
8930 -- type as possibly inheriting a delayed rep aspect.
8931
8932 if Has_Delayed_Rep_Aspects (Parent_Type) then
8933 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
8934 end if;
8935
8936 -- Propagate the attributes related to pragma Ghost from the parent type
8937 -- to the derived type or type extension (SPARK RM 6.9(9)).
8938
8939 if Is_Ghost_Entity (Parent_Type) then
8940 Set_Is_Ghost_Entity (Derived_Type);
8941 end if;
8942
8943 -- Type dependent processing
8944
8945 case Ekind (Parent_Type) is
8946 when Numeric_Kind =>
8947 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
8948
8949 when Array_Kind =>
8950 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
8951
8952 when E_Record_Type
8953 | E_Record_Subtype
8954 | Class_Wide_Kind =>
8955 Build_Derived_Record_Type
8956 (N, Parent_Type, Derived_Type, Derive_Subps);
8957 return;
8958
8959 when Enumeration_Kind =>
8960 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
8961
8962 when Access_Kind =>
8963 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
8964
8965 when Incomplete_Or_Private_Kind =>
8966 Build_Derived_Private_Type
8967 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
8968
8969 -- For discriminated types, the derivation includes deriving
8970 -- primitive operations. For others it is done below.
8971
8972 if Is_Tagged_Type (Parent_Type)
8973 or else Has_Discriminants (Parent_Type)
8974 or else (Present (Full_View (Parent_Type))
8975 and then Has_Discriminants (Full_View (Parent_Type)))
8976 then
8977 return;
8978 end if;
8979
8980 when Concurrent_Kind =>
8981 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
8982
8983 when others =>
8984 raise Program_Error;
8985 end case;
8986
8987 -- Nothing more to do if some error occurred
8988
8989 if Etype (Derived_Type) = Any_Type then
8990 return;
8991 end if;
8992
8993 -- Set delayed freeze and then derive subprograms, we need to do this
8994 -- in this order so that derived subprograms inherit the derived freeze
8995 -- if necessary.
8996
8997 Set_Has_Delayed_Freeze (Derived_Type);
8998
8999 if Derive_Subps then
9000 Derive_Subprograms (Parent_Type, Derived_Type);
9001 end if;
9002
9003 Set_Has_Primitive_Operations
9004 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9005 end Build_Derived_Type;
9006
9007 -----------------------
9008 -- Build_Discriminal --
9009 -----------------------
9010
9011 procedure Build_Discriminal (Discrim : Entity_Id) is
9012 D_Minal : Entity_Id;
9013 CR_Disc : Entity_Id;
9014
9015 begin
9016 -- A discriminal has the same name as the discriminant
9017
9018 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9019
9020 Set_Ekind (D_Minal, E_In_Parameter);
9021 Set_Mechanism (D_Minal, Default_Mechanism);
9022 Set_Etype (D_Minal, Etype (Discrim));
9023 Set_Scope (D_Minal, Current_Scope);
9024
9025 Set_Discriminal (Discrim, D_Minal);
9026 Set_Discriminal_Link (D_Minal, Discrim);
9027
9028 -- For task types, build at once the discriminants of the corresponding
9029 -- record, which are needed if discriminants are used in entry defaults
9030 -- and in family bounds.
9031
9032 if Is_Concurrent_Type (Current_Scope)
9033 or else
9034 Is_Limited_Type (Current_Scope)
9035 then
9036 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9037
9038 Set_Ekind (CR_Disc, E_In_Parameter);
9039 Set_Mechanism (CR_Disc, Default_Mechanism);
9040 Set_Etype (CR_Disc, Etype (Discrim));
9041 Set_Scope (CR_Disc, Current_Scope);
9042 Set_Discriminal_Link (CR_Disc, Discrim);
9043 Set_CR_Discriminant (Discrim, CR_Disc);
9044 end if;
9045 end Build_Discriminal;
9046
9047 ------------------------------------
9048 -- Build_Discriminant_Constraints --
9049 ------------------------------------
9050
9051 function Build_Discriminant_Constraints
9052 (T : Entity_Id;
9053 Def : Node_Id;
9054 Derived_Def : Boolean := False) return Elist_Id
9055 is
9056 C : constant Node_Id := Constraint (Def);
9057 Nb_Discr : constant Nat := Number_Discriminants (T);
9058
9059 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9060 -- Saves the expression corresponding to a given discriminant in T
9061
9062 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9063 -- Return the Position number within array Discr_Expr of a discriminant
9064 -- D within the discriminant list of the discriminated type T.
9065
9066 procedure Process_Discriminant_Expression
9067 (Expr : Node_Id;
9068 D : Entity_Id);
9069 -- If this is a discriminant constraint on a partial view, do not
9070 -- generate an overflow check on the discriminant expression. The check
9071 -- will be generated when constraining the full view. Otherwise the
9072 -- backend creates duplicate symbols for the temporaries corresponding
9073 -- to the expressions to be checked, causing spurious assembler errors.
9074
9075 ------------------
9076 -- Pos_Of_Discr --
9077 ------------------
9078
9079 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9080 Disc : Entity_Id;
9081
9082 begin
9083 Disc := First_Discriminant (T);
9084 for J in Discr_Expr'Range loop
9085 if Disc = D then
9086 return J;
9087 end if;
9088
9089 Next_Discriminant (Disc);
9090 end loop;
9091
9092 -- Note: Since this function is called on discriminants that are
9093 -- known to belong to the discriminated type, falling through the
9094 -- loop with no match signals an internal compiler error.
9095
9096 raise Program_Error;
9097 end Pos_Of_Discr;
9098
9099 -------------------------------------
9100 -- Process_Discriminant_Expression --
9101 -------------------------------------
9102
9103 procedure Process_Discriminant_Expression
9104 (Expr : Node_Id;
9105 D : Entity_Id)
9106 is
9107 BDT : constant Entity_Id := Base_Type (Etype (D));
9108
9109 begin
9110 -- If this is a discriminant constraint on a partial view, do
9111 -- not generate an overflow on the discriminant expression. The
9112 -- check will be generated when constraining the full view.
9113
9114 if Is_Private_Type (T)
9115 and then Present (Full_View (T))
9116 then
9117 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9118 else
9119 Analyze_And_Resolve (Expr, BDT);
9120 end if;
9121 end Process_Discriminant_Expression;
9122
9123 -- Declarations local to Build_Discriminant_Constraints
9124
9125 Discr : Entity_Id;
9126 E : Entity_Id;
9127 Elist : constant Elist_Id := New_Elmt_List;
9128
9129 Constr : Node_Id;
9130 Expr : Node_Id;
9131 Id : Node_Id;
9132 Position : Nat;
9133 Found : Boolean;
9134
9135 Discrim_Present : Boolean := False;
9136
9137 -- Start of processing for Build_Discriminant_Constraints
9138
9139 begin
9140 -- The following loop will process positional associations only.
9141 -- For a positional association, the (single) discriminant is
9142 -- implicitly specified by position, in textual order (RM 3.7.2).
9143
9144 Discr := First_Discriminant (T);
9145 Constr := First (Constraints (C));
9146 for D in Discr_Expr'Range loop
9147 exit when Nkind (Constr) = N_Discriminant_Association;
9148
9149 if No (Constr) then
9150 Error_Msg_N ("too few discriminants given in constraint", C);
9151 return New_Elmt_List;
9152
9153 elsif Nkind (Constr) = N_Range
9154 or else (Nkind (Constr) = N_Attribute_Reference
9155 and then Attribute_Name (Constr) = Name_Range)
9156 then
9157 Error_Msg_N
9158 ("a range is not a valid discriminant constraint", Constr);
9159 Discr_Expr (D) := Error;
9160
9161 else
9162 Process_Discriminant_Expression (Constr, Discr);
9163 Discr_Expr (D) := Constr;
9164 end if;
9165
9166 Next_Discriminant (Discr);
9167 Next (Constr);
9168 end loop;
9169
9170 if No (Discr) and then Present (Constr) then
9171 Error_Msg_N ("too many discriminants given in constraint", Constr);
9172 return New_Elmt_List;
9173 end if;
9174
9175 -- Named associations can be given in any order, but if both positional
9176 -- and named associations are used in the same discriminant constraint,
9177 -- then positional associations must occur first, at their normal
9178 -- position. Hence once a named association is used, the rest of the
9179 -- discriminant constraint must use only named associations.
9180
9181 while Present (Constr) loop
9182
9183 -- Positional association forbidden after a named association
9184
9185 if Nkind (Constr) /= N_Discriminant_Association then
9186 Error_Msg_N ("positional association follows named one", Constr);
9187 return New_Elmt_List;
9188
9189 -- Otherwise it is a named association
9190
9191 else
9192 -- E records the type of the discriminants in the named
9193 -- association. All the discriminants specified in the same name
9194 -- association must have the same type.
9195
9196 E := Empty;
9197
9198 -- Search the list of discriminants in T to see if the simple name
9199 -- given in the constraint matches any of them.
9200
9201 Id := First (Selector_Names (Constr));
9202 while Present (Id) loop
9203 Found := False;
9204
9205 -- If Original_Discriminant is present, we are processing a
9206 -- generic instantiation and this is an instance node. We need
9207 -- to find the name of the corresponding discriminant in the
9208 -- actual record type T and not the name of the discriminant in
9209 -- the generic formal. Example:
9210
9211 -- generic
9212 -- type G (D : int) is private;
9213 -- package P is
9214 -- subtype W is G (D => 1);
9215 -- end package;
9216 -- type Rec (X : int) is record ... end record;
9217 -- package Q is new P (G => Rec);
9218
9219 -- At the point of the instantiation, formal type G is Rec
9220 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9221 -- which really looks like "subtype W is Rec (D => 1);" at
9222 -- the point of instantiation, we want to find the discriminant
9223 -- that corresponds to D in Rec, i.e. X.
9224
9225 if Present (Original_Discriminant (Id))
9226 and then In_Instance
9227 then
9228 Discr := Find_Corresponding_Discriminant (Id, T);
9229 Found := True;
9230
9231 else
9232 Discr := First_Discriminant (T);
9233 while Present (Discr) loop
9234 if Chars (Discr) = Chars (Id) then
9235 Found := True;
9236 exit;
9237 end if;
9238
9239 Next_Discriminant (Discr);
9240 end loop;
9241
9242 if not Found then
9243 Error_Msg_N ("& does not match any discriminant", Id);
9244 return New_Elmt_List;
9245
9246 -- If the parent type is a generic formal, preserve the
9247 -- name of the discriminant for subsequent instances.
9248 -- see comment at the beginning of this if statement.
9249
9250 elsif Is_Generic_Type (Root_Type (T)) then
9251 Set_Original_Discriminant (Id, Discr);
9252 end if;
9253 end if;
9254
9255 Position := Pos_Of_Discr (T, Discr);
9256
9257 if Present (Discr_Expr (Position)) then
9258 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9259
9260 else
9261 -- Each discriminant specified in the same named association
9262 -- must be associated with a separate copy of the
9263 -- corresponding expression.
9264
9265 if Present (Next (Id)) then
9266 Expr := New_Copy_Tree (Expression (Constr));
9267 Set_Parent (Expr, Parent (Expression (Constr)));
9268 else
9269 Expr := Expression (Constr);
9270 end if;
9271
9272 Discr_Expr (Position) := Expr;
9273 Process_Discriminant_Expression (Expr, Discr);
9274 end if;
9275
9276 -- A discriminant association with more than one discriminant
9277 -- name is only allowed if the named discriminants are all of
9278 -- the same type (RM 3.7.1(8)).
9279
9280 if E = Empty then
9281 E := Base_Type (Etype (Discr));
9282
9283 elsif Base_Type (Etype (Discr)) /= E then
9284 Error_Msg_N
9285 ("all discriminants in an association " &
9286 "must have the same type", Id);
9287 end if;
9288
9289 Next (Id);
9290 end loop;
9291 end if;
9292
9293 Next (Constr);
9294 end loop;
9295
9296 -- A discriminant constraint must provide exactly one value for each
9297 -- discriminant of the type (RM 3.7.1(8)).
9298
9299 for J in Discr_Expr'Range loop
9300 if No (Discr_Expr (J)) then
9301 Error_Msg_N ("too few discriminants given in constraint", C);
9302 return New_Elmt_List;
9303 end if;
9304 end loop;
9305
9306 -- Determine if there are discriminant expressions in the constraint
9307
9308 for J in Discr_Expr'Range loop
9309 if Denotes_Discriminant
9310 (Discr_Expr (J), Check_Concurrent => True)
9311 then
9312 Discrim_Present := True;
9313 end if;
9314 end loop;
9315
9316 -- Build an element list consisting of the expressions given in the
9317 -- discriminant constraint and apply the appropriate checks. The list
9318 -- is constructed after resolving any named discriminant associations
9319 -- and therefore the expressions appear in the textual order of the
9320 -- discriminants.
9321
9322 Discr := First_Discriminant (T);
9323 for J in Discr_Expr'Range loop
9324 if Discr_Expr (J) /= Error then
9325 Append_Elmt (Discr_Expr (J), Elist);
9326
9327 -- If any of the discriminant constraints is given by a
9328 -- discriminant and we are in a derived type declaration we
9329 -- have a discriminant renaming. Establish link between new
9330 -- and old discriminant.
9331
9332 if Denotes_Discriminant (Discr_Expr (J)) then
9333 if Derived_Def then
9334 Set_Corresponding_Discriminant
9335 (Entity (Discr_Expr (J)), Discr);
9336 end if;
9337
9338 -- Force the evaluation of non-discriminant expressions.
9339 -- If we have found a discriminant in the constraint 3.4(26)
9340 -- and 3.8(18) demand that no range checks are performed are
9341 -- after evaluation. If the constraint is for a component
9342 -- definition that has a per-object constraint, expressions are
9343 -- evaluated but not checked either. In all other cases perform
9344 -- a range check.
9345
9346 else
9347 if Discrim_Present then
9348 null;
9349
9350 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9351 and then
9352 Has_Per_Object_Constraint
9353 (Defining_Identifier (Parent (Parent (Def))))
9354 then
9355 null;
9356
9357 elsif Is_Access_Type (Etype (Discr)) then
9358 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9359
9360 else
9361 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9362 end if;
9363
9364 Force_Evaluation (Discr_Expr (J));
9365 end if;
9366
9367 -- Check that the designated type of an access discriminant's
9368 -- expression is not a class-wide type unless the discriminant's
9369 -- designated type is also class-wide.
9370
9371 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9372 and then not Is_Class_Wide_Type
9373 (Designated_Type (Etype (Discr)))
9374 and then Etype (Discr_Expr (J)) /= Any_Type
9375 and then Is_Class_Wide_Type
9376 (Designated_Type (Etype (Discr_Expr (J))))
9377 then
9378 Wrong_Type (Discr_Expr (J), Etype (Discr));
9379
9380 elsif Is_Access_Type (Etype (Discr))
9381 and then not Is_Access_Constant (Etype (Discr))
9382 and then Is_Access_Type (Etype (Discr_Expr (J)))
9383 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9384 then
9385 Error_Msg_NE
9386 ("constraint for discriminant& must be access to variable",
9387 Def, Discr);
9388 end if;
9389 end if;
9390
9391 Next_Discriminant (Discr);
9392 end loop;
9393
9394 return Elist;
9395 end Build_Discriminant_Constraints;
9396
9397 ---------------------------------
9398 -- Build_Discriminated_Subtype --
9399 ---------------------------------
9400
9401 procedure Build_Discriminated_Subtype
9402 (T : Entity_Id;
9403 Def_Id : Entity_Id;
9404 Elist : Elist_Id;
9405 Related_Nod : Node_Id;
9406 For_Access : Boolean := False)
9407 is
9408 Has_Discrs : constant Boolean := Has_Discriminants (T);
9409 Constrained : constant Boolean :=
9410 (Has_Discrs
9411 and then not Is_Empty_Elmt_List (Elist)
9412 and then not Is_Class_Wide_Type (T))
9413 or else Is_Constrained (T);
9414
9415 begin
9416 if Ekind (T) = E_Record_Type then
9417 if For_Access then
9418 Set_Ekind (Def_Id, E_Private_Subtype);
9419 Set_Is_For_Access_Subtype (Def_Id, True);
9420 else
9421 Set_Ekind (Def_Id, E_Record_Subtype);
9422 end if;
9423
9424 -- Inherit preelaboration flag from base, for types for which it
9425 -- may have been set: records, private types, protected types.
9426
9427 Set_Known_To_Have_Preelab_Init
9428 (Def_Id, Known_To_Have_Preelab_Init (T));
9429
9430 elsif Ekind (T) = E_Task_Type then
9431 Set_Ekind (Def_Id, E_Task_Subtype);
9432
9433 elsif Ekind (T) = E_Protected_Type then
9434 Set_Ekind (Def_Id, E_Protected_Subtype);
9435 Set_Known_To_Have_Preelab_Init
9436 (Def_Id, Known_To_Have_Preelab_Init (T));
9437
9438 elsif Is_Private_Type (T) then
9439 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9440 Set_Known_To_Have_Preelab_Init
9441 (Def_Id, Known_To_Have_Preelab_Init (T));
9442
9443 -- Private subtypes may have private dependents
9444
9445 Set_Private_Dependents (Def_Id, New_Elmt_List);
9446
9447 elsif Is_Class_Wide_Type (T) then
9448 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9449
9450 else
9451 -- Incomplete type. Attach subtype to list of dependents, to be
9452 -- completed with full view of parent type, unless is it the
9453 -- designated subtype of a record component within an init_proc.
9454 -- This last case arises for a component of an access type whose
9455 -- designated type is incomplete (e.g. a Taft Amendment type).
9456 -- The designated subtype is within an inner scope, and needs no
9457 -- elaboration, because only the access type is needed in the
9458 -- initialization procedure.
9459
9460 Set_Ekind (Def_Id, Ekind (T));
9461
9462 if For_Access and then Within_Init_Proc then
9463 null;
9464 else
9465 Append_Elmt (Def_Id, Private_Dependents (T));
9466 end if;
9467 end if;
9468
9469 Set_Etype (Def_Id, T);
9470 Init_Size_Align (Def_Id);
9471 Set_Has_Discriminants (Def_Id, Has_Discrs);
9472 Set_Is_Constrained (Def_Id, Constrained);
9473
9474 Set_First_Entity (Def_Id, First_Entity (T));
9475 Set_Last_Entity (Def_Id, Last_Entity (T));
9476 Set_Has_Implicit_Dereference
9477 (Def_Id, Has_Implicit_Dereference (T));
9478
9479 -- If the subtype is the completion of a private declaration, there may
9480 -- have been representation clauses for the partial view, and they must
9481 -- be preserved. Build_Derived_Type chains the inherited clauses with
9482 -- the ones appearing on the extension. If this comes from a subtype
9483 -- declaration, all clauses are inherited.
9484
9485 if No (First_Rep_Item (Def_Id)) then
9486 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9487 end if;
9488
9489 if Is_Tagged_Type (T) then
9490 Set_Is_Tagged_Type (Def_Id);
9491 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9492 Make_Class_Wide_Type (Def_Id);
9493 end if;
9494
9495 Set_Stored_Constraint (Def_Id, No_Elist);
9496
9497 if Has_Discrs then
9498 Set_Discriminant_Constraint (Def_Id, Elist);
9499 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9500 end if;
9501
9502 if Is_Tagged_Type (T) then
9503
9504 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9505 -- concurrent record type (which has the list of primitive
9506 -- operations).
9507
9508 if Ada_Version >= Ada_2005
9509 and then Is_Concurrent_Type (T)
9510 then
9511 Set_Corresponding_Record_Type (Def_Id,
9512 Corresponding_Record_Type (T));
9513 else
9514 Set_Direct_Primitive_Operations (Def_Id,
9515 Direct_Primitive_Operations (T));
9516 end if;
9517
9518 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9519 end if;
9520
9521 -- Subtypes introduced by component declarations do not need to be
9522 -- marked as delayed, and do not get freeze nodes, because the semantics
9523 -- verifies that the parents of the subtypes are frozen before the
9524 -- enclosing record is frozen.
9525
9526 if not Is_Type (Scope (Def_Id)) then
9527 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9528
9529 if Is_Private_Type (T)
9530 and then Present (Full_View (T))
9531 then
9532 Conditional_Delay (Def_Id, Full_View (T));
9533 else
9534 Conditional_Delay (Def_Id, T);
9535 end if;
9536 end if;
9537
9538 if Is_Record_Type (T) then
9539 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9540
9541 if Has_Discrs
9542 and then not Is_Empty_Elmt_List (Elist)
9543 and then not For_Access
9544 then
9545 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9546 elsif not For_Access then
9547 Set_Cloned_Subtype (Def_Id, T);
9548 end if;
9549 end if;
9550 end Build_Discriminated_Subtype;
9551
9552 ---------------------------
9553 -- Build_Itype_Reference --
9554 ---------------------------
9555
9556 procedure Build_Itype_Reference
9557 (Ityp : Entity_Id;
9558 Nod : Node_Id)
9559 is
9560 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9561 begin
9562
9563 -- Itype references are only created for use by the back-end
9564
9565 if Inside_A_Generic then
9566 return;
9567 else
9568 Set_Itype (IR, Ityp);
9569 Insert_After (Nod, IR);
9570 end if;
9571 end Build_Itype_Reference;
9572
9573 ------------------------
9574 -- Build_Scalar_Bound --
9575 ------------------------
9576
9577 function Build_Scalar_Bound
9578 (Bound : Node_Id;
9579 Par_T : Entity_Id;
9580 Der_T : Entity_Id) return Node_Id
9581 is
9582 New_Bound : Entity_Id;
9583
9584 begin
9585 -- Note: not clear why this is needed, how can the original bound
9586 -- be unanalyzed at this point? and if it is, what business do we
9587 -- have messing around with it? and why is the base type of the
9588 -- parent type the right type for the resolution. It probably is
9589 -- not. It is OK for the new bound we are creating, but not for
9590 -- the old one??? Still if it never happens, no problem.
9591
9592 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9593
9594 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9595 New_Bound := New_Copy (Bound);
9596 Set_Etype (New_Bound, Der_T);
9597 Set_Analyzed (New_Bound);
9598
9599 elsif Is_Entity_Name (Bound) then
9600 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9601
9602 -- The following is almost certainly wrong. What business do we have
9603 -- relocating a node (Bound) that is presumably still attached to
9604 -- the tree elsewhere???
9605
9606 else
9607 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9608 end if;
9609
9610 Set_Etype (New_Bound, Der_T);
9611 return New_Bound;
9612 end Build_Scalar_Bound;
9613
9614 --------------------------------
9615 -- Build_Underlying_Full_View --
9616 --------------------------------
9617
9618 procedure Build_Underlying_Full_View
9619 (N : Node_Id;
9620 Typ : Entity_Id;
9621 Par : Entity_Id)
9622 is
9623 Loc : constant Source_Ptr := Sloc (N);
9624 Subt : constant Entity_Id :=
9625 Make_Defining_Identifier
9626 (Loc, New_External_Name (Chars (Typ), 'S'));
9627
9628 Constr : Node_Id;
9629 Indic : Node_Id;
9630 C : Node_Id;
9631 Id : Node_Id;
9632
9633 procedure Set_Discriminant_Name (Id : Node_Id);
9634 -- If the derived type has discriminants, they may rename discriminants
9635 -- of the parent. When building the full view of the parent, we need to
9636 -- recover the names of the original discriminants if the constraint is
9637 -- given by named associations.
9638
9639 ---------------------------
9640 -- Set_Discriminant_Name --
9641 ---------------------------
9642
9643 procedure Set_Discriminant_Name (Id : Node_Id) is
9644 Disc : Entity_Id;
9645
9646 begin
9647 Set_Original_Discriminant (Id, Empty);
9648
9649 if Has_Discriminants (Typ) then
9650 Disc := First_Discriminant (Typ);
9651 while Present (Disc) loop
9652 if Chars (Disc) = Chars (Id)
9653 and then Present (Corresponding_Discriminant (Disc))
9654 then
9655 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9656 end if;
9657 Next_Discriminant (Disc);
9658 end loop;
9659 end if;
9660 end Set_Discriminant_Name;
9661
9662 -- Start of processing for Build_Underlying_Full_View
9663
9664 begin
9665 if Nkind (N) = N_Full_Type_Declaration then
9666 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9667
9668 elsif Nkind (N) = N_Subtype_Declaration then
9669 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9670
9671 elsif Nkind (N) = N_Component_Declaration then
9672 Constr :=
9673 New_Copy_Tree
9674 (Constraint (Subtype_Indication (Component_Definition (N))));
9675
9676 else
9677 raise Program_Error;
9678 end if;
9679
9680 C := First (Constraints (Constr));
9681 while Present (C) loop
9682 if Nkind (C) = N_Discriminant_Association then
9683 Id := First (Selector_Names (C));
9684 while Present (Id) loop
9685 Set_Discriminant_Name (Id);
9686 Next (Id);
9687 end loop;
9688 end if;
9689
9690 Next (C);
9691 end loop;
9692
9693 Indic :=
9694 Make_Subtype_Declaration (Loc,
9695 Defining_Identifier => Subt,
9696 Subtype_Indication =>
9697 Make_Subtype_Indication (Loc,
9698 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9699 Constraint => New_Copy_Tree (Constr)));
9700
9701 -- If this is a component subtype for an outer itype, it is not
9702 -- a list member, so simply set the parent link for analysis: if
9703 -- the enclosing type does not need to be in a declarative list,
9704 -- neither do the components.
9705
9706 if Is_List_Member (N)
9707 and then Nkind (N) /= N_Component_Declaration
9708 then
9709 Insert_Before (N, Indic);
9710 else
9711 Set_Parent (Indic, Parent (N));
9712 end if;
9713
9714 Analyze (Indic);
9715 Set_Underlying_Full_View (Typ, Full_View (Subt));
9716 end Build_Underlying_Full_View;
9717
9718 -------------------------------
9719 -- Check_Abstract_Overriding --
9720 -------------------------------
9721
9722 procedure Check_Abstract_Overriding (T : Entity_Id) is
9723 Alias_Subp : Entity_Id;
9724 Elmt : Elmt_Id;
9725 Op_List : Elist_Id;
9726 Subp : Entity_Id;
9727 Type_Def : Node_Id;
9728
9729 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9730 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9731 -- which has pragma Implemented already set. Check whether Subp's entity
9732 -- kind conforms to the implementation kind of the overridden routine.
9733
9734 procedure Check_Pragma_Implemented
9735 (Subp : Entity_Id;
9736 Iface_Subp : Entity_Id);
9737 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9738 -- Iface_Subp and both entities have pragma Implemented already set on
9739 -- them. Check whether the two implementation kinds are conforming.
9740
9741 procedure Inherit_Pragma_Implemented
9742 (Subp : Entity_Id;
9743 Iface_Subp : Entity_Id);
9744 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9745 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9746 -- Propagate the implementation kind of Iface_Subp to Subp.
9747
9748 ------------------------------
9749 -- Check_Pragma_Implemented --
9750 ------------------------------
9751
9752 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9753 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9754 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9755 Subp_Alias : constant Entity_Id := Alias (Subp);
9756 Contr_Typ : Entity_Id;
9757 Impl_Subp : Entity_Id;
9758
9759 begin
9760 -- Subp must have an alias since it is a hidden entity used to link
9761 -- an interface subprogram to its overriding counterpart.
9762
9763 pragma Assert (Present (Subp_Alias));
9764
9765 -- Handle aliases to synchronized wrappers
9766
9767 Impl_Subp := Subp_Alias;
9768
9769 if Is_Primitive_Wrapper (Impl_Subp) then
9770 Impl_Subp := Wrapped_Entity (Impl_Subp);
9771 end if;
9772
9773 -- Extract the type of the controlling formal
9774
9775 Contr_Typ := Etype (First_Formal (Subp_Alias));
9776
9777 if Is_Concurrent_Record_Type (Contr_Typ) then
9778 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9779 end if;
9780
9781 -- An interface subprogram whose implementation kind is By_Entry must
9782 -- be implemented by an entry.
9783
9784 if Impl_Kind = Name_By_Entry
9785 and then Ekind (Impl_Subp) /= E_Entry
9786 then
9787 Error_Msg_Node_2 := Iface_Alias;
9788 Error_Msg_NE
9789 ("type & must implement abstract subprogram & with an entry",
9790 Subp_Alias, Contr_Typ);
9791
9792 elsif Impl_Kind = Name_By_Protected_Procedure then
9793
9794 -- An interface subprogram whose implementation kind is By_
9795 -- Protected_Procedure cannot be implemented by a primitive
9796 -- procedure of a task type.
9797
9798 if Ekind (Contr_Typ) /= E_Protected_Type then
9799 Error_Msg_Node_2 := Contr_Typ;
9800 Error_Msg_NE
9801 ("interface subprogram & cannot be implemented by a " &
9802 "primitive procedure of task type &", Subp_Alias,
9803 Iface_Alias);
9804
9805 -- An interface subprogram whose implementation kind is By_
9806 -- Protected_Procedure must be implemented by a procedure.
9807
9808 elsif Ekind (Impl_Subp) /= E_Procedure then
9809 Error_Msg_Node_2 := Iface_Alias;
9810 Error_Msg_NE
9811 ("type & must implement abstract subprogram & with a " &
9812 "procedure", Subp_Alias, Contr_Typ);
9813
9814 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9815 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9816 then
9817 Error_Msg_Name_1 := Impl_Kind;
9818 Error_Msg_N
9819 ("overriding operation& must have synchronization%",
9820 Subp_Alias);
9821 end if;
9822
9823 -- If primitive has Optional synchronization, overriding operation
9824 -- must match if it has an explicit synchronization..
9825
9826 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9827 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9828 then
9829 Error_Msg_Name_1 := Impl_Kind;
9830 Error_Msg_N
9831 ("overriding operation& must have syncrhonization%",
9832 Subp_Alias);
9833 end if;
9834 end Check_Pragma_Implemented;
9835
9836 ------------------------------
9837 -- Check_Pragma_Implemented --
9838 ------------------------------
9839
9840 procedure Check_Pragma_Implemented
9841 (Subp : Entity_Id;
9842 Iface_Subp : Entity_Id)
9843 is
9844 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9845 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9846
9847 begin
9848 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9849 -- and overriding subprogram are different. In general this is an
9850 -- error except when the implementation kind of the overridden
9851 -- subprograms is By_Any or Optional.
9852
9853 if Iface_Kind /= Subp_Kind
9854 and then Iface_Kind /= Name_By_Any
9855 and then Iface_Kind /= Name_Optional
9856 then
9857 if Iface_Kind = Name_By_Entry then
9858 Error_Msg_N
9859 ("incompatible implementation kind, overridden subprogram " &
9860 "is marked By_Entry", Subp);
9861 else
9862 Error_Msg_N
9863 ("incompatible implementation kind, overridden subprogram " &
9864 "is marked By_Protected_Procedure", Subp);
9865 end if;
9866 end if;
9867 end Check_Pragma_Implemented;
9868
9869 --------------------------------
9870 -- Inherit_Pragma_Implemented --
9871 --------------------------------
9872
9873 procedure Inherit_Pragma_Implemented
9874 (Subp : Entity_Id;
9875 Iface_Subp : Entity_Id)
9876 is
9877 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9878 Loc : constant Source_Ptr := Sloc (Subp);
9879 Impl_Prag : Node_Id;
9880
9881 begin
9882 -- Since the implementation kind is stored as a representation item
9883 -- rather than a flag, create a pragma node.
9884
9885 Impl_Prag :=
9886 Make_Pragma (Loc,
9887 Chars => Name_Implemented,
9888 Pragma_Argument_Associations => New_List (
9889 Make_Pragma_Argument_Association (Loc,
9890 Expression => New_Occurrence_Of (Subp, Loc)),
9891
9892 Make_Pragma_Argument_Association (Loc,
9893 Expression => Make_Identifier (Loc, Iface_Kind))));
9894
9895 -- The pragma doesn't need to be analyzed because it is internally
9896 -- built. It is safe to directly register it as a rep item since we
9897 -- are only interested in the characters of the implementation kind.
9898
9899 Record_Rep_Item (Subp, Impl_Prag);
9900 end Inherit_Pragma_Implemented;
9901
9902 -- Start of processing for Check_Abstract_Overriding
9903
9904 begin
9905 Op_List := Primitive_Operations (T);
9906
9907 -- Loop to check primitive operations
9908
9909 Elmt := First_Elmt (Op_List);
9910 while Present (Elmt) loop
9911 Subp := Node (Elmt);
9912 Alias_Subp := Alias (Subp);
9913
9914 -- Inherited subprograms are identified by the fact that they do not
9915 -- come from source, and the associated source location is the
9916 -- location of the first subtype of the derived type.
9917
9918 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
9919 -- subprograms that "require overriding".
9920
9921 -- Special exception, do not complain about failure to override the
9922 -- stream routines _Input and _Output, as well as the primitive
9923 -- operations used in dispatching selects since we always provide
9924 -- automatic overridings for these subprograms.
9925
9926 -- Also ignore this rule for convention CIL since .NET libraries
9927 -- do bizarre things with interfaces???
9928
9929 -- The partial view of T may have been a private extension, for
9930 -- which inherited functions dispatching on result are abstract.
9931 -- If the full view is a null extension, there is no need for
9932 -- overriding in Ada 2005, but wrappers need to be built for them
9933 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
9934
9935 if Is_Null_Extension (T)
9936 and then Has_Controlling_Result (Subp)
9937 and then Ada_Version >= Ada_2005
9938 and then Present (Alias_Subp)
9939 and then not Comes_From_Source (Subp)
9940 and then not Is_Abstract_Subprogram (Alias_Subp)
9941 and then not Is_Access_Type (Etype (Subp))
9942 then
9943 null;
9944
9945 -- Ada 2005 (AI-251): Internal entities of interfaces need no
9946 -- processing because this check is done with the aliased
9947 -- entity
9948
9949 elsif Present (Interface_Alias (Subp)) then
9950 null;
9951
9952 elsif (Is_Abstract_Subprogram (Subp)
9953 or else Requires_Overriding (Subp)
9954 or else
9955 (Has_Controlling_Result (Subp)
9956 and then Present (Alias_Subp)
9957 and then not Comes_From_Source (Subp)
9958 and then Sloc (Subp) = Sloc (First_Subtype (T))))
9959 and then not Is_TSS (Subp, TSS_Stream_Input)
9960 and then not Is_TSS (Subp, TSS_Stream_Output)
9961 and then not Is_Abstract_Type (T)
9962 and then Convention (T) /= Convention_CIL
9963 and then not Is_Predefined_Interface_Primitive (Subp)
9964
9965 -- Ada 2005 (AI-251): Do not consider hidden entities associated
9966 -- with abstract interface types because the check will be done
9967 -- with the aliased entity (otherwise we generate a duplicated
9968 -- error message).
9969
9970 and then not Present (Interface_Alias (Subp))
9971 then
9972 if Present (Alias_Subp) then
9973
9974 -- Only perform the check for a derived subprogram when the
9975 -- type has an explicit record extension. This avoids incorrect
9976 -- flagging of abstract subprograms for the case of a type
9977 -- without an extension that is derived from a formal type
9978 -- with a tagged actual (can occur within a private part).
9979
9980 -- Ada 2005 (AI-391): In the case of an inherited function with
9981 -- a controlling result of the type, the rule does not apply if
9982 -- the type is a null extension (unless the parent function
9983 -- itself is abstract, in which case the function must still be
9984 -- be overridden). The expander will generate an overriding
9985 -- wrapper function calling the parent subprogram (see
9986 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
9987
9988 Type_Def := Type_Definition (Parent (T));
9989
9990 if Nkind (Type_Def) = N_Derived_Type_Definition
9991 and then Present (Record_Extension_Part (Type_Def))
9992 and then
9993 (Ada_Version < Ada_2005
9994 or else not Is_Null_Extension (T)
9995 or else Ekind (Subp) = E_Procedure
9996 or else not Has_Controlling_Result (Subp)
9997 or else Is_Abstract_Subprogram (Alias_Subp)
9998 or else Requires_Overriding (Subp)
9999 or else Is_Access_Type (Etype (Subp)))
10000 then
10001 -- Avoid reporting error in case of abstract predefined
10002 -- primitive inherited from interface type because the
10003 -- body of internally generated predefined primitives
10004 -- of tagged types are generated later by Freeze_Type
10005
10006 if Is_Interface (Root_Type (T))
10007 and then Is_Abstract_Subprogram (Subp)
10008 and then Is_Predefined_Dispatching_Operation (Subp)
10009 and then not Comes_From_Source (Ultimate_Alias (Subp))
10010 then
10011 null;
10012
10013 -- A null extension is not obliged to override an inherited
10014 -- procedure subject to pragma Extensions_Visible with value
10015 -- False and at least one controlling OUT parameter
10016 -- (SPARK RM 6.1.7(6)).
10017
10018 elsif Is_Null_Extension (T)
10019 and then Is_EVF_Procedure (Subp)
10020 then
10021 null;
10022
10023 else
10024 Error_Msg_NE
10025 ("type must be declared abstract or & overridden",
10026 T, Subp);
10027
10028 -- Traverse the whole chain of aliased subprograms to
10029 -- complete the error notification. This is especially
10030 -- useful for traceability of the chain of entities when
10031 -- the subprogram corresponds with an interface
10032 -- subprogram (which may be defined in another package).
10033
10034 if Present (Alias_Subp) then
10035 declare
10036 E : Entity_Id;
10037
10038 begin
10039 E := Subp;
10040 while Present (Alias (E)) loop
10041
10042 -- Avoid reporting redundant errors on entities
10043 -- inherited from interfaces
10044
10045 if Sloc (E) /= Sloc (T) then
10046 Error_Msg_Sloc := Sloc (E);
10047 Error_Msg_NE
10048 ("\& has been inherited #", T, Subp);
10049 end if;
10050
10051 E := Alias (E);
10052 end loop;
10053
10054 Error_Msg_Sloc := Sloc (E);
10055
10056 -- AI05-0068: report if there is an overriding
10057 -- non-abstract subprogram that is invisible.
10058
10059 if Is_Hidden (E)
10060 and then not Is_Abstract_Subprogram (E)
10061 then
10062 Error_Msg_NE
10063 ("\& subprogram# is not visible",
10064 T, Subp);
10065
10066 -- Clarify the case where a non-null extension must
10067 -- override inherited procedure subject to pragma
10068 -- Extensions_Visible with value False and at least
10069 -- one controlling OUT param.
10070
10071 elsif Is_EVF_Procedure (E) then
10072 Error_Msg_NE
10073 ("\& # is subject to Extensions_Visible False",
10074 T, Subp);
10075
10076 else
10077 Error_Msg_NE
10078 ("\& has been inherited from subprogram #",
10079 T, Subp);
10080 end if;
10081 end;
10082 end if;
10083 end if;
10084
10085 -- Ada 2005 (AI-345): Protected or task type implementing
10086 -- abstract interfaces.
10087
10088 elsif Is_Concurrent_Record_Type (T)
10089 and then Present (Interfaces (T))
10090 then
10091 -- There is no need to check here RM 9.4(11.9/3) since we
10092 -- are processing the corresponding record type and the
10093 -- mode of the overriding subprograms was verified by
10094 -- Check_Conformance when the corresponding concurrent
10095 -- type declaration was analyzed.
10096
10097 Error_Msg_NE
10098 ("interface subprogram & must be overridden", T, Subp);
10099
10100 -- Examine primitive operations of synchronized type to find
10101 -- homonyms that have the wrong profile.
10102
10103 declare
10104 Prim : Entity_Id;
10105
10106 begin
10107 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10108 while Present (Prim) loop
10109 if Chars (Prim) = Chars (Subp) then
10110 Error_Msg_NE
10111 ("profile is not type conformant with prefixed "
10112 & "view profile of inherited operation&",
10113 Prim, Subp);
10114 end if;
10115
10116 Next_Entity (Prim);
10117 end loop;
10118 end;
10119 end if;
10120
10121 else
10122 Error_Msg_Node_2 := T;
10123 Error_Msg_N
10124 ("abstract subprogram& not allowed for type&", Subp);
10125
10126 -- Also post unconditional warning on the type (unconditional
10127 -- so that if there are more than one of these cases, we get
10128 -- them all, and not just the first one).
10129
10130 Error_Msg_Node_2 := Subp;
10131 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10132 end if;
10133
10134 -- A subprogram subject to pragma Extensions_Visible with value
10135 -- "True" cannot override a subprogram subject to the same pragma
10136 -- with value "False" (SPARK RM 6.1.7(5)).
10137
10138 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10139 and then Present (Overridden_Operation (Subp))
10140 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10141 Extensions_Visible_False
10142 then
10143 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10144 Error_Msg_N
10145 ("subprogram & with Extensions_Visible True cannot override "
10146 & "subprogram # with Extensions_Visible False", Subp);
10147 end if;
10148
10149 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10150
10151 -- Subp is an expander-generated procedure which maps an interface
10152 -- alias to a protected wrapper. The interface alias is flagged by
10153 -- pragma Implemented. Ensure that Subp is a procedure when the
10154 -- implementation kind is By_Protected_Procedure or an entry when
10155 -- By_Entry.
10156
10157 if Ada_Version >= Ada_2012
10158 and then Is_Hidden (Subp)
10159 and then Present (Interface_Alias (Subp))
10160 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10161 then
10162 Check_Pragma_Implemented (Subp);
10163 end if;
10164
10165 -- Subp is an interface primitive which overrides another interface
10166 -- primitive marked with pragma Implemented.
10167
10168 if Ada_Version >= Ada_2012
10169 and then Present (Overridden_Operation (Subp))
10170 and then Has_Rep_Pragma
10171 (Overridden_Operation (Subp), Name_Implemented)
10172 then
10173 -- If the overriding routine is also marked by Implemented, check
10174 -- that the two implementation kinds are conforming.
10175
10176 if Has_Rep_Pragma (Subp, Name_Implemented) then
10177 Check_Pragma_Implemented
10178 (Subp => Subp,
10179 Iface_Subp => Overridden_Operation (Subp));
10180
10181 -- Otherwise the overriding routine inherits the implementation
10182 -- kind from the overridden subprogram.
10183
10184 else
10185 Inherit_Pragma_Implemented
10186 (Subp => Subp,
10187 Iface_Subp => Overridden_Operation (Subp));
10188 end if;
10189 end if;
10190
10191 -- If the operation is a wrapper for a synchronized primitive, it
10192 -- may be called indirectly through a dispatching select. We assume
10193 -- that it will be referenced elsewhere indirectly, and suppress
10194 -- warnings about an unused entity.
10195
10196 if Is_Primitive_Wrapper (Subp)
10197 and then Present (Wrapped_Entity (Subp))
10198 then
10199 Set_Referenced (Wrapped_Entity (Subp));
10200 end if;
10201
10202 Next_Elmt (Elmt);
10203 end loop;
10204 end Check_Abstract_Overriding;
10205
10206 ------------------------------------------------
10207 -- Check_Access_Discriminant_Requires_Limited --
10208 ------------------------------------------------
10209
10210 procedure Check_Access_Discriminant_Requires_Limited
10211 (D : Node_Id;
10212 Loc : Node_Id)
10213 is
10214 begin
10215 -- A discriminant_specification for an access discriminant shall appear
10216 -- only in the declaration for a task or protected type, or for a type
10217 -- with the reserved word 'limited' in its definition or in one of its
10218 -- ancestors (RM 3.7(10)).
10219
10220 -- AI-0063: The proper condition is that type must be immutably limited,
10221 -- or else be a partial view.
10222
10223 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10224 if Is_Limited_View (Current_Scope)
10225 or else
10226 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10227 and then Limited_Present (Parent (Current_Scope)))
10228 then
10229 null;
10230
10231 else
10232 Error_Msg_N
10233 ("access discriminants allowed only for limited types", Loc);
10234 end if;
10235 end if;
10236 end Check_Access_Discriminant_Requires_Limited;
10237
10238 -----------------------------------
10239 -- Check_Aliased_Component_Types --
10240 -----------------------------------
10241
10242 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10243 C : Entity_Id;
10244
10245 begin
10246 -- ??? Also need to check components of record extensions, but not
10247 -- components of protected types (which are always limited).
10248
10249 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10250 -- types to be unconstrained. This is safe because it is illegal to
10251 -- create access subtypes to such types with explicit discriminant
10252 -- constraints.
10253
10254 if not Is_Limited_Type (T) then
10255 if Ekind (T) = E_Record_Type then
10256 C := First_Component (T);
10257 while Present (C) loop
10258 if Is_Aliased (C)
10259 and then Has_Discriminants (Etype (C))
10260 and then not Is_Constrained (Etype (C))
10261 and then not In_Instance_Body
10262 and then Ada_Version < Ada_2005
10263 then
10264 Error_Msg_N
10265 ("aliased component must be constrained (RM 3.6(11))",
10266 C);
10267 end if;
10268
10269 Next_Component (C);
10270 end loop;
10271
10272 elsif Ekind (T) = E_Array_Type then
10273 if Has_Aliased_Components (T)
10274 and then Has_Discriminants (Component_Type (T))
10275 and then not Is_Constrained (Component_Type (T))
10276 and then not In_Instance_Body
10277 and then Ada_Version < Ada_2005
10278 then
10279 Error_Msg_N
10280 ("aliased component type must be constrained (RM 3.6(11))",
10281 T);
10282 end if;
10283 end if;
10284 end if;
10285 end Check_Aliased_Component_Types;
10286
10287 ---------------------------------------
10288 -- Check_Anonymous_Access_Components --
10289 ---------------------------------------
10290
10291 procedure Check_Anonymous_Access_Components
10292 (Typ_Decl : Node_Id;
10293 Typ : Entity_Id;
10294 Prev : Entity_Id;
10295 Comp_List : Node_Id)
10296 is
10297 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10298 Anon_Access : Entity_Id;
10299 Acc_Def : Node_Id;
10300 Comp : Node_Id;
10301 Comp_Def : Node_Id;
10302 Decl : Node_Id;
10303 Type_Def : Node_Id;
10304
10305 procedure Build_Incomplete_Type_Declaration;
10306 -- If the record type contains components that include an access to the
10307 -- current record, then create an incomplete type declaration for the
10308 -- record, to be used as the designated type of the anonymous access.
10309 -- This is done only once, and only if there is no previous partial
10310 -- view of the type.
10311
10312 function Designates_T (Subt : Node_Id) return Boolean;
10313 -- Check whether a node designates the enclosing record type, or 'Class
10314 -- of that type
10315
10316 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10317 -- Check whether an access definition includes a reference to
10318 -- the enclosing record type. The reference can be a subtype mark
10319 -- in the access definition itself, a 'Class attribute reference, or
10320 -- recursively a reference appearing in a parameter specification
10321 -- or result definition of an access_to_subprogram definition.
10322
10323 --------------------------------------
10324 -- Build_Incomplete_Type_Declaration --
10325 --------------------------------------
10326
10327 procedure Build_Incomplete_Type_Declaration is
10328 Decl : Node_Id;
10329 Inc_T : Entity_Id;
10330 H : Entity_Id;
10331
10332 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10333 -- it's "is new ... with record" or else "is tagged record ...".
10334
10335 Is_Tagged : constant Boolean :=
10336 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10337 and then
10338 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10339 or else
10340 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10341 and then Tagged_Present (Type_Definition (Typ_Decl)));
10342
10343 begin
10344 -- If there is a previous partial view, no need to create a new one
10345 -- If the partial view, given by Prev, is incomplete, If Prev is
10346 -- a private declaration, full declaration is flagged accordingly.
10347
10348 if Prev /= Typ then
10349 if Is_Tagged then
10350 Make_Class_Wide_Type (Prev);
10351 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10352 Set_Etype (Class_Wide_Type (Typ), Typ);
10353 end if;
10354
10355 return;
10356
10357 elsif Has_Private_Declaration (Typ) then
10358
10359 -- If we refer to T'Class inside T, and T is the completion of a
10360 -- private type, then make sure the class-wide type exists.
10361
10362 if Is_Tagged then
10363 Make_Class_Wide_Type (Typ);
10364 end if;
10365
10366 return;
10367
10368 -- If there was a previous anonymous access type, the incomplete
10369 -- type declaration will have been created already.
10370
10371 elsif Present (Current_Entity (Typ))
10372 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10373 and then Full_View (Current_Entity (Typ)) = Typ
10374 then
10375 if Is_Tagged
10376 and then Comes_From_Source (Current_Entity (Typ))
10377 and then not Is_Tagged_Type (Current_Entity (Typ))
10378 then
10379 Make_Class_Wide_Type (Typ);
10380 Error_Msg_N
10381 ("incomplete view of tagged type should be declared tagged??",
10382 Parent (Current_Entity (Typ)));
10383 end if;
10384 return;
10385
10386 else
10387 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10388 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10389
10390 -- Type has already been inserted into the current scope. Remove
10391 -- it, and add incomplete declaration for type, so that subsequent
10392 -- anonymous access types can use it. The entity is unchained from
10393 -- the homonym list and from immediate visibility. After analysis,
10394 -- the entity in the incomplete declaration becomes immediately
10395 -- visible in the record declaration that follows.
10396
10397 H := Current_Entity (Typ);
10398
10399 if H = Typ then
10400 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10401 else
10402 while Present (H)
10403 and then Homonym (H) /= Typ
10404 loop
10405 H := Homonym (Typ);
10406 end loop;
10407
10408 Set_Homonym (H, Homonym (Typ));
10409 end if;
10410
10411 Insert_Before (Typ_Decl, Decl);
10412 Analyze (Decl);
10413 Set_Full_View (Inc_T, Typ);
10414
10415 if Is_Tagged then
10416
10417 -- Create a common class-wide type for both views, and set the
10418 -- Etype of the class-wide type to the full view.
10419
10420 Make_Class_Wide_Type (Inc_T);
10421 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10422 Set_Etype (Class_Wide_Type (Typ), Typ);
10423 end if;
10424 end if;
10425 end Build_Incomplete_Type_Declaration;
10426
10427 ------------------
10428 -- Designates_T --
10429 ------------------
10430
10431 function Designates_T (Subt : Node_Id) return Boolean is
10432 Type_Id : constant Name_Id := Chars (Typ);
10433
10434 function Names_T (Nam : Node_Id) return Boolean;
10435 -- The record type has not been introduced in the current scope
10436 -- yet, so we must examine the name of the type itself, either
10437 -- an identifier T, or an expanded name of the form P.T, where
10438 -- P denotes the current scope.
10439
10440 -------------
10441 -- Names_T --
10442 -------------
10443
10444 function Names_T (Nam : Node_Id) return Boolean is
10445 begin
10446 if Nkind (Nam) = N_Identifier then
10447 return Chars (Nam) = Type_Id;
10448
10449 elsif Nkind (Nam) = N_Selected_Component then
10450 if Chars (Selector_Name (Nam)) = Type_Id then
10451 if Nkind (Prefix (Nam)) = N_Identifier then
10452 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10453
10454 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10455 return Chars (Selector_Name (Prefix (Nam))) =
10456 Chars (Current_Scope);
10457 else
10458 return False;
10459 end if;
10460
10461 else
10462 return False;
10463 end if;
10464
10465 else
10466 return False;
10467 end if;
10468 end Names_T;
10469
10470 -- Start of processing for Designates_T
10471
10472 begin
10473 if Nkind (Subt) = N_Identifier then
10474 return Chars (Subt) = Type_Id;
10475
10476 -- Reference can be through an expanded name which has not been
10477 -- analyzed yet, and which designates enclosing scopes.
10478
10479 elsif Nkind (Subt) = N_Selected_Component then
10480 if Names_T (Subt) then
10481 return True;
10482
10483 -- Otherwise it must denote an entity that is already visible.
10484 -- The access definition may name a subtype of the enclosing
10485 -- type, if there is a previous incomplete declaration for it.
10486
10487 else
10488 Find_Selected_Component (Subt);
10489 return
10490 Is_Entity_Name (Subt)
10491 and then Scope (Entity (Subt)) = Current_Scope
10492 and then
10493 (Chars (Base_Type (Entity (Subt))) = Type_Id
10494 or else
10495 (Is_Class_Wide_Type (Entity (Subt))
10496 and then
10497 Chars (Etype (Base_Type (Entity (Subt)))) =
10498 Type_Id));
10499 end if;
10500
10501 -- A reference to the current type may appear as the prefix of
10502 -- a 'Class attribute.
10503
10504 elsif Nkind (Subt) = N_Attribute_Reference
10505 and then Attribute_Name (Subt) = Name_Class
10506 then
10507 return Names_T (Prefix (Subt));
10508
10509 else
10510 return False;
10511 end if;
10512 end Designates_T;
10513
10514 ----------------
10515 -- Mentions_T --
10516 ----------------
10517
10518 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10519 Param_Spec : Node_Id;
10520
10521 Acc_Subprg : constant Node_Id :=
10522 Access_To_Subprogram_Definition (Acc_Def);
10523
10524 begin
10525 if No (Acc_Subprg) then
10526 return Designates_T (Subtype_Mark (Acc_Def));
10527 end if;
10528
10529 -- Component is an access_to_subprogram: examine its formals,
10530 -- and result definition in the case of an access_to_function.
10531
10532 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10533 while Present (Param_Spec) loop
10534 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10535 and then Mentions_T (Parameter_Type (Param_Spec))
10536 then
10537 return True;
10538
10539 elsif Designates_T (Parameter_Type (Param_Spec)) then
10540 return True;
10541 end if;
10542
10543 Next (Param_Spec);
10544 end loop;
10545
10546 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10547 if Nkind (Result_Definition (Acc_Subprg)) =
10548 N_Access_Definition
10549 then
10550 return Mentions_T (Result_Definition (Acc_Subprg));
10551 else
10552 return Designates_T (Result_Definition (Acc_Subprg));
10553 end if;
10554 end if;
10555
10556 return False;
10557 end Mentions_T;
10558
10559 -- Start of processing for Check_Anonymous_Access_Components
10560
10561 begin
10562 if No (Comp_List) then
10563 return;
10564 end if;
10565
10566 Comp := First (Component_Items (Comp_List));
10567 while Present (Comp) loop
10568 if Nkind (Comp) = N_Component_Declaration
10569 and then Present
10570 (Access_Definition (Component_Definition (Comp)))
10571 and then
10572 Mentions_T (Access_Definition (Component_Definition (Comp)))
10573 then
10574 Comp_Def := Component_Definition (Comp);
10575 Acc_Def :=
10576 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10577
10578 Build_Incomplete_Type_Declaration;
10579 Anon_Access := Make_Temporary (Loc, 'S');
10580
10581 -- Create a declaration for the anonymous access type: either
10582 -- an access_to_object or an access_to_subprogram.
10583
10584 if Present (Acc_Def) then
10585 if Nkind (Acc_Def) = N_Access_Function_Definition then
10586 Type_Def :=
10587 Make_Access_Function_Definition (Loc,
10588 Parameter_Specifications =>
10589 Parameter_Specifications (Acc_Def),
10590 Result_Definition => Result_Definition (Acc_Def));
10591 else
10592 Type_Def :=
10593 Make_Access_Procedure_Definition (Loc,
10594 Parameter_Specifications =>
10595 Parameter_Specifications (Acc_Def));
10596 end if;
10597
10598 else
10599 Type_Def :=
10600 Make_Access_To_Object_Definition (Loc,
10601 Subtype_Indication =>
10602 Relocate_Node
10603 (Subtype_Mark (Access_Definition (Comp_Def))));
10604
10605 Set_Constant_Present
10606 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10607 Set_All_Present
10608 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10609 end if;
10610
10611 Set_Null_Exclusion_Present
10612 (Type_Def,
10613 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10614
10615 Decl :=
10616 Make_Full_Type_Declaration (Loc,
10617 Defining_Identifier => Anon_Access,
10618 Type_Definition => Type_Def);
10619
10620 Insert_Before (Typ_Decl, Decl);
10621 Analyze (Decl);
10622
10623 -- If an access to subprogram, create the extra formals
10624
10625 if Present (Acc_Def) then
10626 Create_Extra_Formals (Designated_Type (Anon_Access));
10627
10628 -- If an access to object, preserve entity of designated type,
10629 -- for ASIS use, before rewriting the component definition.
10630
10631 else
10632 declare
10633 Desig : Entity_Id;
10634
10635 begin
10636 Desig := Entity (Subtype_Indication (Type_Def));
10637
10638 -- If the access definition is to the current record,
10639 -- the visible entity at this point is an incomplete
10640 -- type. Retrieve the full view to simplify ASIS queries
10641
10642 if Ekind (Desig) = E_Incomplete_Type then
10643 Desig := Full_View (Desig);
10644 end if;
10645
10646 Set_Entity
10647 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10648 end;
10649 end if;
10650
10651 Rewrite (Comp_Def,
10652 Make_Component_Definition (Loc,
10653 Subtype_Indication =>
10654 New_Occurrence_Of (Anon_Access, Loc)));
10655
10656 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10657 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10658 else
10659 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10660 end if;
10661
10662 Set_Is_Local_Anonymous_Access (Anon_Access);
10663 end if;
10664
10665 Next (Comp);
10666 end loop;
10667
10668 if Present (Variant_Part (Comp_List)) then
10669 declare
10670 V : Node_Id;
10671 begin
10672 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10673 while Present (V) loop
10674 Check_Anonymous_Access_Components
10675 (Typ_Decl, Typ, Prev, Component_List (V));
10676 Next_Non_Pragma (V);
10677 end loop;
10678 end;
10679 end if;
10680 end Check_Anonymous_Access_Components;
10681
10682 ----------------------
10683 -- Check_Completion --
10684 ----------------------
10685
10686 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10687 E : Entity_Id;
10688
10689 procedure Post_Error;
10690 -- Post error message for lack of completion for entity E
10691
10692 ----------------
10693 -- Post_Error --
10694 ----------------
10695
10696 procedure Post_Error is
10697
10698 procedure Missing_Body;
10699 -- Output missing body message
10700
10701 ------------------
10702 -- Missing_Body --
10703 ------------------
10704
10705 procedure Missing_Body is
10706 begin
10707 -- Spec is in same unit, so we can post on spec
10708
10709 if In_Same_Source_Unit (Body_Id, E) then
10710 Error_Msg_N ("missing body for &", E);
10711
10712 -- Spec is in a separate unit, so we have to post on the body
10713
10714 else
10715 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10716 end if;
10717 end Missing_Body;
10718
10719 -- Start of processing for Post_Error
10720
10721 begin
10722 if not Comes_From_Source (E) then
10723
10724 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10725
10726 -- It may be an anonymous protected type created for a
10727 -- single variable. Post error on variable, if present.
10728
10729 declare
10730 Var : Entity_Id;
10731
10732 begin
10733 Var := First_Entity (Current_Scope);
10734 while Present (Var) loop
10735 exit when Etype (Var) = E
10736 and then Comes_From_Source (Var);
10737
10738 Next_Entity (Var);
10739 end loop;
10740
10741 if Present (Var) then
10742 E := Var;
10743 end if;
10744 end;
10745 end if;
10746 end if;
10747
10748 -- If a generated entity has no completion, then either previous
10749 -- semantic errors have disabled the expansion phase, or else we had
10750 -- missing subunits, or else we are compiling without expansion,
10751 -- or else something is very wrong.
10752
10753 if not Comes_From_Source (E) then
10754 pragma Assert
10755 (Serious_Errors_Detected > 0
10756 or else Configurable_Run_Time_Violations > 0
10757 or else Subunits_Missing
10758 or else not Expander_Active);
10759 return;
10760
10761 -- Here for source entity
10762
10763 else
10764 -- Here if no body to post the error message, so we post the error
10765 -- on the declaration that has no completion. This is not really
10766 -- the right place to post it, think about this later ???
10767
10768 if No (Body_Id) then
10769 if Is_Type (E) then
10770 Error_Msg_NE
10771 ("missing full declaration for }", Parent (E), E);
10772 else
10773 Error_Msg_NE ("missing body for &", Parent (E), E);
10774 end if;
10775
10776 -- Package body has no completion for a declaration that appears
10777 -- in the corresponding spec. Post error on the body, with a
10778 -- reference to the non-completed declaration.
10779
10780 else
10781 Error_Msg_Sloc := Sloc (E);
10782
10783 if Is_Type (E) then
10784 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10785
10786 elsif Is_Overloadable (E)
10787 and then Current_Entity_In_Scope (E) /= E
10788 then
10789 -- It may be that the completion is mistyped and appears as
10790 -- a distinct overloading of the entity.
10791
10792 declare
10793 Candidate : constant Entity_Id :=
10794 Current_Entity_In_Scope (E);
10795 Decl : constant Node_Id :=
10796 Unit_Declaration_Node (Candidate);
10797
10798 begin
10799 if Is_Overloadable (Candidate)
10800 and then Ekind (Candidate) = Ekind (E)
10801 and then Nkind (Decl) = N_Subprogram_Body
10802 and then Acts_As_Spec (Decl)
10803 then
10804 Check_Type_Conformant (Candidate, E);
10805
10806 else
10807 Missing_Body;
10808 end if;
10809 end;
10810
10811 else
10812 Missing_Body;
10813 end if;
10814 end if;
10815 end if;
10816 end Post_Error;
10817
10818 -- Local variables
10819
10820 Pack_Id : constant Entity_Id := Current_Scope;
10821
10822 -- Start of processing for Check_Completion
10823
10824 begin
10825 E := First_Entity (Pack_Id);
10826 while Present (E) loop
10827 if Is_Intrinsic_Subprogram (E) then
10828 null;
10829
10830 -- A Ghost entity declared in a non-Ghost package does not force the
10831 -- need for a body (SPARK RM 6.9(11)).
10832
10833 elsif not Is_Ghost_Entity (Pack_Id) and then Is_Ghost_Entity (E) then
10834 null;
10835
10836 -- The following situation requires special handling: a child unit
10837 -- that appears in the context clause of the body of its parent:
10838
10839 -- procedure Parent.Child (...);
10840
10841 -- with Parent.Child;
10842 -- package body Parent is
10843
10844 -- Here Parent.Child appears as a local entity, but should not be
10845 -- flagged as requiring completion, because it is a compilation
10846 -- unit.
10847
10848 -- Ignore missing completion for a subprogram that does not come from
10849 -- source (including the _Call primitive operation of RAS types,
10850 -- which has to have the flag Comes_From_Source for other purposes):
10851 -- we assume that the expander will provide the missing completion.
10852 -- In case of previous errors, other expansion actions that provide
10853 -- bodies for null procedures with not be invoked, so inhibit message
10854 -- in those cases.
10855
10856 -- Note that E_Operator is not in the list that follows, because
10857 -- this kind is reserved for predefined operators, that are
10858 -- intrinsic and do not need completion.
10859
10860 elsif Ekind_In (E, E_Function,
10861 E_Procedure,
10862 E_Generic_Function,
10863 E_Generic_Procedure)
10864 then
10865 if Has_Completion (E) then
10866 null;
10867
10868 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
10869 null;
10870
10871 elsif Is_Subprogram (E)
10872 and then (not Comes_From_Source (E)
10873 or else Chars (E) = Name_uCall)
10874 then
10875 null;
10876
10877 elsif
10878 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
10879 then
10880 null;
10881
10882 elsif Nkind (Parent (E)) = N_Procedure_Specification
10883 and then Null_Present (Parent (E))
10884 and then Serious_Errors_Detected > 0
10885 then
10886 null;
10887
10888 else
10889 Post_Error;
10890 end if;
10891
10892 elsif Is_Entry (E) then
10893 if not Has_Completion (E) and then
10894 (Ekind (Scope (E)) = E_Protected_Object
10895 or else Ekind (Scope (E)) = E_Protected_Type)
10896 then
10897 Post_Error;
10898 end if;
10899
10900 elsif Is_Package_Or_Generic_Package (E) then
10901 if Unit_Requires_Body (E) then
10902 if not Has_Completion (E)
10903 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
10904 N_Compilation_Unit
10905 then
10906 Post_Error;
10907 end if;
10908
10909 elsif not Is_Child_Unit (E) then
10910 May_Need_Implicit_Body (E);
10911 end if;
10912
10913 -- A formal incomplete type (Ada 2012) does not require a completion;
10914 -- other incomplete type declarations do.
10915
10916 elsif Ekind (E) = E_Incomplete_Type
10917 and then No (Underlying_Type (E))
10918 and then not Is_Generic_Type (E)
10919 then
10920 Post_Error;
10921
10922 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
10923 and then not Has_Completion (E)
10924 then
10925 Post_Error;
10926
10927 -- A single task declared in the current scope is a constant, verify
10928 -- that the body of its anonymous type is in the same scope. If the
10929 -- task is defined elsewhere, this may be a renaming declaration for
10930 -- which no completion is needed.
10931
10932 elsif Ekind (E) = E_Constant
10933 and then Ekind (Etype (E)) = E_Task_Type
10934 and then not Has_Completion (Etype (E))
10935 and then Scope (Etype (E)) = Current_Scope
10936 then
10937 Post_Error;
10938
10939 elsif Ekind (E) = E_Protected_Object
10940 and then not Has_Completion (Etype (E))
10941 then
10942 Post_Error;
10943
10944 elsif Ekind (E) = E_Record_Type then
10945 if Is_Tagged_Type (E) then
10946 Check_Abstract_Overriding (E);
10947 Check_Conventions (E);
10948 end if;
10949
10950 Check_Aliased_Component_Types (E);
10951
10952 elsif Ekind (E) = E_Array_Type then
10953 Check_Aliased_Component_Types (E);
10954
10955 end if;
10956
10957 Next_Entity (E);
10958 end loop;
10959 end Check_Completion;
10960
10961 ------------------------------------
10962 -- Check_CPP_Type_Has_No_Defaults --
10963 ------------------------------------
10964
10965 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
10966 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
10967 Clist : Node_Id;
10968 Comp : Node_Id;
10969
10970 begin
10971 -- Obtain the component list
10972
10973 if Nkind (Tdef) = N_Record_Definition then
10974 Clist := Component_List (Tdef);
10975 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
10976 Clist := Component_List (Record_Extension_Part (Tdef));
10977 end if;
10978
10979 -- Check all components to ensure no default expressions
10980
10981 if Present (Clist) then
10982 Comp := First (Component_Items (Clist));
10983 while Present (Comp) loop
10984 if Present (Expression (Comp)) then
10985 Error_Msg_N
10986 ("component of imported 'C'P'P type cannot have "
10987 & "default expression", Expression (Comp));
10988 end if;
10989
10990 Next (Comp);
10991 end loop;
10992 end if;
10993 end Check_CPP_Type_Has_No_Defaults;
10994
10995 ----------------------------
10996 -- Check_Delta_Expression --
10997 ----------------------------
10998
10999 procedure Check_Delta_Expression (E : Node_Id) is
11000 begin
11001 if not (Is_Real_Type (Etype (E))) then
11002 Wrong_Type (E, Any_Real);
11003
11004 elsif not Is_OK_Static_Expression (E) then
11005 Flag_Non_Static_Expr
11006 ("non-static expression used for delta value!", E);
11007
11008 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11009 Error_Msg_N ("delta expression must be positive", E);
11010
11011 else
11012 return;
11013 end if;
11014
11015 -- If any of above errors occurred, then replace the incorrect
11016 -- expression by the real 0.1, which should prevent further errors.
11017
11018 Rewrite (E,
11019 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11020 Analyze_And_Resolve (E, Standard_Float);
11021 end Check_Delta_Expression;
11022
11023 -----------------------------
11024 -- Check_Digits_Expression --
11025 -----------------------------
11026
11027 procedure Check_Digits_Expression (E : Node_Id) is
11028 begin
11029 if not (Is_Integer_Type (Etype (E))) then
11030 Wrong_Type (E, Any_Integer);
11031
11032 elsif not Is_OK_Static_Expression (E) then
11033 Flag_Non_Static_Expr
11034 ("non-static expression used for digits value!", E);
11035
11036 elsif Expr_Value (E) <= 0 then
11037 Error_Msg_N ("digits value must be greater than zero", E);
11038
11039 else
11040 return;
11041 end if;
11042
11043 -- If any of above errors occurred, then replace the incorrect
11044 -- expression by the integer 1, which should prevent further errors.
11045
11046 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11047 Analyze_And_Resolve (E, Standard_Integer);
11048
11049 end Check_Digits_Expression;
11050
11051 --------------------------
11052 -- Check_Initialization --
11053 --------------------------
11054
11055 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11056 begin
11057 -- Special processing for limited types
11058
11059 if Is_Limited_Type (T)
11060 and then not In_Instance
11061 and then not In_Inlined_Body
11062 then
11063 if not OK_For_Limited_Init (T, Exp) then
11064
11065 -- In GNAT mode, this is just a warning, to allow it to be evilly
11066 -- turned off. Otherwise it is a real error.
11067
11068 if GNAT_Mode then
11069 Error_Msg_N
11070 ("??cannot initialize entities of limited type!", Exp);
11071
11072 elsif Ada_Version < Ada_2005 then
11073
11074 -- The side effect removal machinery may generate illegal Ada
11075 -- code to avoid the usage of access types and 'reference in
11076 -- SPARK mode. Since this is legal code with respect to theorem
11077 -- proving, do not emit the error.
11078
11079 if GNATprove_Mode
11080 and then Nkind (Exp) = N_Function_Call
11081 and then Nkind (Parent (Exp)) = N_Object_Declaration
11082 and then not Comes_From_Source
11083 (Defining_Identifier (Parent (Exp)))
11084 then
11085 null;
11086
11087 else
11088 Error_Msg_N
11089 ("cannot initialize entities of limited type", Exp);
11090 Explain_Limited_Type (T, Exp);
11091 end if;
11092
11093 else
11094 -- Specialize error message according to kind of illegal
11095 -- initial expression.
11096
11097 if Nkind (Exp) = N_Type_Conversion
11098 and then Nkind (Expression (Exp)) = N_Function_Call
11099 then
11100 Error_Msg_N
11101 ("illegal context for call"
11102 & " to function with limited result", Exp);
11103
11104 else
11105 Error_Msg_N
11106 ("initialization of limited object requires aggregate "
11107 & "or function call", Exp);
11108 end if;
11109 end if;
11110 end if;
11111 end if;
11112
11113 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11114 -- set unless we can be sure that no range check is required.
11115
11116 if (GNATprove_Mode or not Expander_Active)
11117 and then Is_Scalar_Type (T)
11118 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11119 then
11120 Set_Do_Range_Check (Exp);
11121 end if;
11122 end Check_Initialization;
11123
11124 ----------------------
11125 -- Check_Interfaces --
11126 ----------------------
11127
11128 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11129 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11130
11131 Iface : Node_Id;
11132 Iface_Def : Node_Id;
11133 Iface_Typ : Entity_Id;
11134 Parent_Node : Node_Id;
11135
11136 Is_Task : Boolean := False;
11137 -- Set True if parent type or any progenitor is a task interface
11138
11139 Is_Protected : Boolean := False;
11140 -- Set True if parent type or any progenitor is a protected interface
11141
11142 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11143 -- Check that a progenitor is compatible with declaration. If an error
11144 -- message is output, it is posted on Error_Node.
11145
11146 ------------------
11147 -- Check_Ifaces --
11148 ------------------
11149
11150 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11151 Iface_Id : constant Entity_Id :=
11152 Defining_Identifier (Parent (Iface_Def));
11153 Type_Def : Node_Id;
11154
11155 begin
11156 if Nkind (N) = N_Private_Extension_Declaration then
11157 Type_Def := N;
11158 else
11159 Type_Def := Type_Definition (N);
11160 end if;
11161
11162 if Is_Task_Interface (Iface_Id) then
11163 Is_Task := True;
11164
11165 elsif Is_Protected_Interface (Iface_Id) then
11166 Is_Protected := True;
11167 end if;
11168
11169 if Is_Synchronized_Interface (Iface_Id) then
11170
11171 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11172 -- extension derived from a synchronized interface must explicitly
11173 -- be declared synchronized, because the full view will be a
11174 -- synchronized type.
11175
11176 if Nkind (N) = N_Private_Extension_Declaration then
11177 if not Synchronized_Present (N) then
11178 Error_Msg_NE
11179 ("private extension of& must be explicitly synchronized",
11180 N, Iface_Id);
11181 end if;
11182
11183 -- However, by 3.9.4(16/2), a full type that is a record extension
11184 -- is never allowed to derive from a synchronized interface (note
11185 -- that interfaces must be excluded from this check, because those
11186 -- are represented by derived type definitions in some cases).
11187
11188 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11189 and then not Interface_Present (Type_Definition (N))
11190 then
11191 Error_Msg_N ("record extension cannot derive from synchronized "
11192 & "interface", Error_Node);
11193 end if;
11194 end if;
11195
11196 -- Check that the characteristics of the progenitor are compatible
11197 -- with the explicit qualifier in the declaration.
11198 -- The check only applies to qualifiers that come from source.
11199 -- Limited_Present also appears in the declaration of corresponding
11200 -- records, and the check does not apply to them.
11201
11202 if Limited_Present (Type_Def)
11203 and then not
11204 Is_Concurrent_Record_Type (Defining_Identifier (N))
11205 then
11206 if Is_Limited_Interface (Parent_Type)
11207 and then not Is_Limited_Interface (Iface_Id)
11208 then
11209 Error_Msg_NE
11210 ("progenitor & must be limited interface",
11211 Error_Node, Iface_Id);
11212
11213 elsif
11214 (Task_Present (Iface_Def)
11215 or else Protected_Present (Iface_Def)
11216 or else Synchronized_Present (Iface_Def))
11217 and then Nkind (N) /= N_Private_Extension_Declaration
11218 and then not Error_Posted (N)
11219 then
11220 Error_Msg_NE
11221 ("progenitor & must be limited interface",
11222 Error_Node, Iface_Id);
11223 end if;
11224
11225 -- Protected interfaces can only inherit from limited, synchronized
11226 -- or protected interfaces.
11227
11228 elsif Nkind (N) = N_Full_Type_Declaration
11229 and then Protected_Present (Type_Def)
11230 then
11231 if Limited_Present (Iface_Def)
11232 or else Synchronized_Present (Iface_Def)
11233 or else Protected_Present (Iface_Def)
11234 then
11235 null;
11236
11237 elsif Task_Present (Iface_Def) then
11238 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11239 & "from task interface", Error_Node);
11240
11241 else
11242 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11243 & "from non-limited interface", Error_Node);
11244 end if;
11245
11246 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11247 -- limited and synchronized.
11248
11249 elsif Synchronized_Present (Type_Def) then
11250 if Limited_Present (Iface_Def)
11251 or else Synchronized_Present (Iface_Def)
11252 then
11253 null;
11254
11255 elsif Protected_Present (Iface_Def)
11256 and then Nkind (N) /= N_Private_Extension_Declaration
11257 then
11258 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11259 & "from protected interface", Error_Node);
11260
11261 elsif Task_Present (Iface_Def)
11262 and then Nkind (N) /= N_Private_Extension_Declaration
11263 then
11264 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11265 & "from task interface", Error_Node);
11266
11267 elsif not Is_Limited_Interface (Iface_Id) then
11268 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11269 & "from non-limited interface", Error_Node);
11270 end if;
11271
11272 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11273 -- synchronized or task interfaces.
11274
11275 elsif Nkind (N) = N_Full_Type_Declaration
11276 and then Task_Present (Type_Def)
11277 then
11278 if Limited_Present (Iface_Def)
11279 or else Synchronized_Present (Iface_Def)
11280 or else Task_Present (Iface_Def)
11281 then
11282 null;
11283
11284 elsif Protected_Present (Iface_Def) then
11285 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11286 & "protected interface", Error_Node);
11287
11288 else
11289 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11290 & "non-limited interface", Error_Node);
11291 end if;
11292 end if;
11293 end Check_Ifaces;
11294
11295 -- Start of processing for Check_Interfaces
11296
11297 begin
11298 if Is_Interface (Parent_Type) then
11299 if Is_Task_Interface (Parent_Type) then
11300 Is_Task := True;
11301
11302 elsif Is_Protected_Interface (Parent_Type) then
11303 Is_Protected := True;
11304 end if;
11305 end if;
11306
11307 if Nkind (N) = N_Private_Extension_Declaration then
11308
11309 -- Check that progenitors are compatible with declaration
11310
11311 Iface := First (Interface_List (Def));
11312 while Present (Iface) loop
11313 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11314
11315 Parent_Node := Parent (Base_Type (Iface_Typ));
11316 Iface_Def := Type_Definition (Parent_Node);
11317
11318 if not Is_Interface (Iface_Typ) then
11319 Diagnose_Interface (Iface, Iface_Typ);
11320 else
11321 Check_Ifaces (Iface_Def, Iface);
11322 end if;
11323
11324 Next (Iface);
11325 end loop;
11326
11327 if Is_Task and Is_Protected then
11328 Error_Msg_N
11329 ("type cannot derive from task and protected interface", N);
11330 end if;
11331
11332 return;
11333 end if;
11334
11335 -- Full type declaration of derived type.
11336 -- Check compatibility with parent if it is interface type
11337
11338 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11339 and then Is_Interface (Parent_Type)
11340 then
11341 Parent_Node := Parent (Parent_Type);
11342
11343 -- More detailed checks for interface varieties
11344
11345 Check_Ifaces
11346 (Iface_Def => Type_Definition (Parent_Node),
11347 Error_Node => Subtype_Indication (Type_Definition (N)));
11348 end if;
11349
11350 Iface := First (Interface_List (Def));
11351 while Present (Iface) loop
11352 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11353
11354 Parent_Node := Parent (Base_Type (Iface_Typ));
11355 Iface_Def := Type_Definition (Parent_Node);
11356
11357 if not Is_Interface (Iface_Typ) then
11358 Diagnose_Interface (Iface, Iface_Typ);
11359
11360 else
11361 -- "The declaration of a specific descendant of an interface
11362 -- type freezes the interface type" RM 13.14
11363
11364 Freeze_Before (N, Iface_Typ);
11365 Check_Ifaces (Iface_Def, Error_Node => Iface);
11366 end if;
11367
11368 Next (Iface);
11369 end loop;
11370
11371 if Is_Task and Is_Protected then
11372 Error_Msg_N
11373 ("type cannot derive from task and protected interface", N);
11374 end if;
11375 end Check_Interfaces;
11376
11377 ------------------------------------
11378 -- Check_Or_Process_Discriminants --
11379 ------------------------------------
11380
11381 -- If an incomplete or private type declaration was already given for the
11382 -- type, the discriminants may have already been processed if they were
11383 -- present on the incomplete declaration. In this case a full conformance
11384 -- check has been performed in Find_Type_Name, and we then recheck here
11385 -- some properties that can't be checked on the partial view alone.
11386 -- Otherwise we call Process_Discriminants.
11387
11388 procedure Check_Or_Process_Discriminants
11389 (N : Node_Id;
11390 T : Entity_Id;
11391 Prev : Entity_Id := Empty)
11392 is
11393 begin
11394 if Has_Discriminants (T) then
11395
11396 -- Discriminants are already set on T if they were already present
11397 -- on the partial view. Make them visible to component declarations.
11398
11399 declare
11400 D : Entity_Id;
11401 -- Discriminant on T (full view) referencing expr on partial view
11402
11403 Prev_D : Entity_Id;
11404 -- Entity of corresponding discriminant on partial view
11405
11406 New_D : Node_Id;
11407 -- Discriminant specification for full view, expression is
11408 -- the syntactic copy on full view (which has been checked for
11409 -- conformance with partial view), only used here to post error
11410 -- message.
11411
11412 begin
11413 D := First_Discriminant (T);
11414 New_D := First (Discriminant_Specifications (N));
11415 while Present (D) loop
11416 Prev_D := Current_Entity (D);
11417 Set_Current_Entity (D);
11418 Set_Is_Immediately_Visible (D);
11419 Set_Homonym (D, Prev_D);
11420
11421 -- Handle the case where there is an untagged partial view and
11422 -- the full view is tagged: must disallow discriminants with
11423 -- defaults, unless compiling for Ada 2012, which allows a
11424 -- limited tagged type to have defaulted discriminants (see
11425 -- AI05-0214). However, suppress error here if it was already
11426 -- reported on the default expression of the partial view.
11427
11428 if Is_Tagged_Type (T)
11429 and then Present (Expression (Parent (D)))
11430 and then (not Is_Limited_Type (Current_Scope)
11431 or else Ada_Version < Ada_2012)
11432 and then not Error_Posted (Expression (Parent (D)))
11433 then
11434 if Ada_Version >= Ada_2012 then
11435 Error_Msg_N
11436 ("discriminants of nonlimited tagged type cannot have "
11437 & "defaults",
11438 Expression (New_D));
11439 else
11440 Error_Msg_N
11441 ("discriminants of tagged type cannot have defaults",
11442 Expression (New_D));
11443 end if;
11444 end if;
11445
11446 -- Ada 2005 (AI-230): Access discriminant allowed in
11447 -- non-limited record types.
11448
11449 if Ada_Version < Ada_2005 then
11450
11451 -- This restriction gets applied to the full type here. It
11452 -- has already been applied earlier to the partial view.
11453
11454 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11455 end if;
11456
11457 Next_Discriminant (D);
11458 Next (New_D);
11459 end loop;
11460 end;
11461
11462 elsif Present (Discriminant_Specifications (N)) then
11463 Process_Discriminants (N, Prev);
11464 end if;
11465 end Check_Or_Process_Discriminants;
11466
11467 ----------------------
11468 -- Check_Real_Bound --
11469 ----------------------
11470
11471 procedure Check_Real_Bound (Bound : Node_Id) is
11472 begin
11473 if not Is_Real_Type (Etype (Bound)) then
11474 Error_Msg_N
11475 ("bound in real type definition must be of real type", Bound);
11476
11477 elsif not Is_OK_Static_Expression (Bound) then
11478 Flag_Non_Static_Expr
11479 ("non-static expression used for real type bound!", Bound);
11480
11481 else
11482 return;
11483 end if;
11484
11485 Rewrite
11486 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11487 Analyze (Bound);
11488 Resolve (Bound, Standard_Float);
11489 end Check_Real_Bound;
11490
11491 ------------------------------
11492 -- Complete_Private_Subtype --
11493 ------------------------------
11494
11495 procedure Complete_Private_Subtype
11496 (Priv : Entity_Id;
11497 Full : Entity_Id;
11498 Full_Base : Entity_Id;
11499 Related_Nod : Node_Id)
11500 is
11501 Save_Next_Entity : Entity_Id;
11502 Save_Homonym : Entity_Id;
11503
11504 begin
11505 -- Set semantic attributes for (implicit) private subtype completion.
11506 -- If the full type has no discriminants, then it is a copy of the
11507 -- full view of the base. Otherwise, it is a subtype of the base with
11508 -- a possible discriminant constraint. Save and restore the original
11509 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11510 -- not corrupt the entity chain.
11511
11512 -- Note that the type of the full view is the same entity as the type
11513 -- of the partial view. In this fashion, the subtype has access to the
11514 -- correct view of the parent.
11515
11516 Save_Next_Entity := Next_Entity (Full);
11517 Save_Homonym := Homonym (Priv);
11518
11519 case Ekind (Full_Base) is
11520 when E_Record_Type |
11521 E_Record_Subtype |
11522 Class_Wide_Kind |
11523 Private_Kind |
11524 Task_Kind |
11525 Protected_Kind =>
11526 Copy_Node (Priv, Full);
11527
11528 Set_Has_Discriminants
11529 (Full, Has_Discriminants (Full_Base));
11530 Set_Has_Unknown_Discriminants
11531 (Full, Has_Unknown_Discriminants (Full_Base));
11532 Set_First_Entity (Full, First_Entity (Full_Base));
11533 Set_Last_Entity (Full, Last_Entity (Full_Base));
11534
11535 -- If the underlying base type is constrained, we know that the
11536 -- full view of the subtype is constrained as well (the converse
11537 -- is not necessarily true).
11538
11539 if Is_Constrained (Full_Base) then
11540 Set_Is_Constrained (Full);
11541 end if;
11542
11543 when others =>
11544 Copy_Node (Full_Base, Full);
11545
11546 Set_Chars (Full, Chars (Priv));
11547 Conditional_Delay (Full, Priv);
11548 Set_Sloc (Full, Sloc (Priv));
11549 end case;
11550
11551 Set_Next_Entity (Full, Save_Next_Entity);
11552 Set_Homonym (Full, Save_Homonym);
11553 Set_Associated_Node_For_Itype (Full, Related_Nod);
11554
11555 -- Set common attributes for all subtypes: kind, convention, etc.
11556
11557 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11558 Set_Convention (Full, Convention (Full_Base));
11559
11560 -- The Etype of the full view is inconsistent. Gigi needs to see the
11561 -- structural full view, which is what the current scheme gives: the
11562 -- Etype of the full view is the etype of the full base. However, if the
11563 -- full base is a derived type, the full view then looks like a subtype
11564 -- of the parent, not a subtype of the full base. If instead we write:
11565
11566 -- Set_Etype (Full, Full_Base);
11567
11568 -- then we get inconsistencies in the front-end (confusion between
11569 -- views). Several outstanding bugs are related to this ???
11570
11571 Set_Is_First_Subtype (Full, False);
11572 Set_Scope (Full, Scope (Priv));
11573 Set_Size_Info (Full, Full_Base);
11574 Set_RM_Size (Full, RM_Size (Full_Base));
11575 Set_Is_Itype (Full);
11576
11577 -- A subtype of a private-type-without-discriminants, whose full-view
11578 -- has discriminants with default expressions, is not constrained.
11579
11580 if not Has_Discriminants (Priv) then
11581 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11582
11583 if Has_Discriminants (Full_Base) then
11584 Set_Discriminant_Constraint
11585 (Full, Discriminant_Constraint (Full_Base));
11586
11587 -- The partial view may have been indefinite, the full view
11588 -- might not be.
11589
11590 Set_Has_Unknown_Discriminants
11591 (Full, Has_Unknown_Discriminants (Full_Base));
11592 end if;
11593 end if;
11594
11595 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11596 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11597
11598 -- Freeze the private subtype entity if its parent is delayed, and not
11599 -- already frozen. We skip this processing if the type is an anonymous
11600 -- subtype of a record component, or is the corresponding record of a
11601 -- protected type, since ???
11602
11603 if not Is_Type (Scope (Full)) then
11604 Set_Has_Delayed_Freeze (Full,
11605 Has_Delayed_Freeze (Full_Base)
11606 and then (not Is_Frozen (Full_Base)));
11607 end if;
11608
11609 Set_Freeze_Node (Full, Empty);
11610 Set_Is_Frozen (Full, False);
11611 Set_Full_View (Priv, Full);
11612
11613 if Has_Discriminants (Full) then
11614 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11615 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11616
11617 if Has_Unknown_Discriminants (Full) then
11618 Set_Discriminant_Constraint (Full, No_Elist);
11619 end if;
11620 end if;
11621
11622 if Ekind (Full_Base) = E_Record_Type
11623 and then Has_Discriminants (Full_Base)
11624 and then Has_Discriminants (Priv) -- might not, if errors
11625 and then not Has_Unknown_Discriminants (Priv)
11626 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11627 then
11628 Create_Constrained_Components
11629 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11630
11631 -- If the full base is itself derived from private, build a congruent
11632 -- subtype of its underlying type, for use by the back end. For a
11633 -- constrained record component, the declaration cannot be placed on
11634 -- the component list, but it must nevertheless be built an analyzed, to
11635 -- supply enough information for Gigi to compute the size of component.
11636
11637 elsif Ekind (Full_Base) in Private_Kind
11638 and then Is_Derived_Type (Full_Base)
11639 and then Has_Discriminants (Full_Base)
11640 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11641 then
11642 if not Is_Itype (Priv)
11643 and then
11644 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11645 then
11646 Build_Underlying_Full_View
11647 (Parent (Priv), Full, Etype (Full_Base));
11648
11649 elsif Nkind (Related_Nod) = N_Component_Declaration then
11650 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11651 end if;
11652
11653 elsif Is_Record_Type (Full_Base) then
11654
11655 -- Show Full is simply a renaming of Full_Base
11656
11657 Set_Cloned_Subtype (Full, Full_Base);
11658 end if;
11659
11660 -- It is unsafe to share the bounds of a scalar type, because the Itype
11661 -- is elaborated on demand, and if a bound is non-static then different
11662 -- orders of elaboration in different units will lead to different
11663 -- external symbols.
11664
11665 if Is_Scalar_Type (Full_Base) then
11666 Set_Scalar_Range (Full,
11667 Make_Range (Sloc (Related_Nod),
11668 Low_Bound =>
11669 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11670 High_Bound =>
11671 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11672
11673 -- This completion inherits the bounds of the full parent, but if
11674 -- the parent is an unconstrained floating point type, so is the
11675 -- completion.
11676
11677 if Is_Floating_Point_Type (Full_Base) then
11678 Set_Includes_Infinities
11679 (Scalar_Range (Full), Has_Infinities (Full_Base));
11680 end if;
11681 end if;
11682
11683 -- ??? It seems that a lot of fields are missing that should be copied
11684 -- from Full_Base to Full. Here are some that are introduced in a
11685 -- non-disruptive way but a cleanup is necessary.
11686
11687 if Is_Tagged_Type (Full_Base) then
11688 Set_Is_Tagged_Type (Full);
11689 Set_Direct_Primitive_Operations
11690 (Full, Direct_Primitive_Operations (Full_Base));
11691 Set_No_Tagged_Streams_Pragma
11692 (Full, No_Tagged_Streams_Pragma (Full_Base));
11693
11694 -- Inherit class_wide type of full_base in case the partial view was
11695 -- not tagged. Otherwise it has already been created when the private
11696 -- subtype was analyzed.
11697
11698 if No (Class_Wide_Type (Full)) then
11699 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11700 end if;
11701
11702 -- If this is a subtype of a protected or task type, constrain its
11703 -- corresponding record, unless this is a subtype without constraints,
11704 -- i.e. a simple renaming as with an actual subtype in an instance.
11705
11706 elsif Is_Concurrent_Type (Full_Base) then
11707 if Has_Discriminants (Full)
11708 and then Present (Corresponding_Record_Type (Full_Base))
11709 and then
11710 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11711 then
11712 Set_Corresponding_Record_Type (Full,
11713 Constrain_Corresponding_Record
11714 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11715
11716 else
11717 Set_Corresponding_Record_Type (Full,
11718 Corresponding_Record_Type (Full_Base));
11719 end if;
11720 end if;
11721
11722 -- Link rep item chain, and also setting of Has_Predicates from private
11723 -- subtype to full subtype, since we will need these on the full subtype
11724 -- to create the predicate function. Note that the full subtype may
11725 -- already have rep items, inherited from the full view of the base
11726 -- type, so we must be sure not to overwrite these entries.
11727
11728 declare
11729 Append : Boolean;
11730 Item : Node_Id;
11731 Next_Item : Node_Id;
11732
11733 begin
11734 Item := First_Rep_Item (Full);
11735
11736 -- If no existing rep items on full type, we can just link directly
11737 -- to the list of items on the private type, if any exist.. Same if
11738 -- the rep items are only those inherited from the base
11739
11740 if (No (Item)
11741 or else Nkind (Item) /= N_Aspect_Specification
11742 or else Entity (Item) = Full_Base)
11743 and then Present (First_Rep_Item (Priv))
11744 then
11745 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11746
11747 -- Otherwise, search to the end of items currently linked to the full
11748 -- subtype and append the private items to the end. However, if Priv
11749 -- and Full already have the same list of rep items, then the append
11750 -- is not done, as that would create a circularity.
11751
11752 elsif Item /= First_Rep_Item (Priv) then
11753 Append := True;
11754 loop
11755 Next_Item := Next_Rep_Item (Item);
11756 exit when No (Next_Item);
11757 Item := Next_Item;
11758
11759 -- If the private view has aspect specifications, the full view
11760 -- inherits them. Since these aspects may already have been
11761 -- attached to the full view during derivation, do not append
11762 -- them if already present.
11763
11764 if Item = First_Rep_Item (Priv) then
11765 Append := False;
11766 exit;
11767 end if;
11768 end loop;
11769
11770 -- And link the private type items at the end of the chain
11771
11772 if Append then
11773 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11774 end if;
11775 end if;
11776 end;
11777
11778 -- Make sure Has_Predicates is set on full type if it is set on the
11779 -- private type. Note that it may already be set on the full type and
11780 -- if so, we don't want to unset it.
11781
11782 if Has_Predicates (Priv) then
11783 Set_Has_Predicates (Full);
11784 end if;
11785 end Complete_Private_Subtype;
11786
11787 ----------------------------
11788 -- Constant_Redeclaration --
11789 ----------------------------
11790
11791 procedure Constant_Redeclaration
11792 (Id : Entity_Id;
11793 N : Node_Id;
11794 T : out Entity_Id)
11795 is
11796 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
11797 Obj_Def : constant Node_Id := Object_Definition (N);
11798 New_T : Entity_Id;
11799
11800 procedure Check_Possible_Deferred_Completion
11801 (Prev_Id : Entity_Id;
11802 Prev_Obj_Def : Node_Id;
11803 Curr_Obj_Def : Node_Id);
11804 -- Determine whether the two object definitions describe the partial
11805 -- and the full view of a constrained deferred constant. Generate
11806 -- a subtype for the full view and verify that it statically matches
11807 -- the subtype of the partial view.
11808
11809 procedure Check_Recursive_Declaration (Typ : Entity_Id);
11810 -- If deferred constant is an access type initialized with an allocator,
11811 -- check whether there is an illegal recursion in the definition,
11812 -- through a default value of some record subcomponent. This is normally
11813 -- detected when generating init procs, but requires this additional
11814 -- mechanism when expansion is disabled.
11815
11816 ----------------------------------------
11817 -- Check_Possible_Deferred_Completion --
11818 ----------------------------------------
11819
11820 procedure Check_Possible_Deferred_Completion
11821 (Prev_Id : Entity_Id;
11822 Prev_Obj_Def : Node_Id;
11823 Curr_Obj_Def : Node_Id)
11824 is
11825 begin
11826 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
11827 and then Present (Constraint (Prev_Obj_Def))
11828 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
11829 and then Present (Constraint (Curr_Obj_Def))
11830 then
11831 declare
11832 Loc : constant Source_Ptr := Sloc (N);
11833 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
11834 Decl : constant Node_Id :=
11835 Make_Subtype_Declaration (Loc,
11836 Defining_Identifier => Def_Id,
11837 Subtype_Indication =>
11838 Relocate_Node (Curr_Obj_Def));
11839
11840 begin
11841 Insert_Before_And_Analyze (N, Decl);
11842 Set_Etype (Id, Def_Id);
11843
11844 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
11845 Error_Msg_Sloc := Sloc (Prev_Id);
11846 Error_Msg_N ("subtype does not statically match deferred "
11847 & "declaration #", N);
11848 end if;
11849 end;
11850 end if;
11851 end Check_Possible_Deferred_Completion;
11852
11853 ---------------------------------
11854 -- Check_Recursive_Declaration --
11855 ---------------------------------
11856
11857 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
11858 Comp : Entity_Id;
11859
11860 begin
11861 if Is_Record_Type (Typ) then
11862 Comp := First_Component (Typ);
11863 while Present (Comp) loop
11864 if Comes_From_Source (Comp) then
11865 if Present (Expression (Parent (Comp)))
11866 and then Is_Entity_Name (Expression (Parent (Comp)))
11867 and then Entity (Expression (Parent (Comp))) = Prev
11868 then
11869 Error_Msg_Sloc := Sloc (Parent (Comp));
11870 Error_Msg_NE
11871 ("illegal circularity with declaration for & #",
11872 N, Comp);
11873 return;
11874
11875 elsif Is_Record_Type (Etype (Comp)) then
11876 Check_Recursive_Declaration (Etype (Comp));
11877 end if;
11878 end if;
11879
11880 Next_Component (Comp);
11881 end loop;
11882 end if;
11883 end Check_Recursive_Declaration;
11884
11885 -- Start of processing for Constant_Redeclaration
11886
11887 begin
11888 if Nkind (Parent (Prev)) = N_Object_Declaration then
11889 if Nkind (Object_Definition
11890 (Parent (Prev))) = N_Subtype_Indication
11891 then
11892 -- Find type of new declaration. The constraints of the two
11893 -- views must match statically, but there is no point in
11894 -- creating an itype for the full view.
11895
11896 if Nkind (Obj_Def) = N_Subtype_Indication then
11897 Find_Type (Subtype_Mark (Obj_Def));
11898 New_T := Entity (Subtype_Mark (Obj_Def));
11899
11900 else
11901 Find_Type (Obj_Def);
11902 New_T := Entity (Obj_Def);
11903 end if;
11904
11905 T := Etype (Prev);
11906
11907 else
11908 -- The full view may impose a constraint, even if the partial
11909 -- view does not, so construct the subtype.
11910
11911 New_T := Find_Type_Of_Object (Obj_Def, N);
11912 T := New_T;
11913 end if;
11914
11915 else
11916 -- Current declaration is illegal, diagnosed below in Enter_Name
11917
11918 T := Empty;
11919 New_T := Any_Type;
11920 end if;
11921
11922 -- If previous full declaration or a renaming declaration exists, or if
11923 -- a homograph is present, let Enter_Name handle it, either with an
11924 -- error or with the removal of an overridden implicit subprogram.
11925 -- The previous one is a full declaration if it has an expression
11926 -- (which in the case of an aggregate is indicated by the Init flag).
11927
11928 if Ekind (Prev) /= E_Constant
11929 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
11930 or else Present (Expression (Parent (Prev)))
11931 or else Has_Init_Expression (Parent (Prev))
11932 or else Present (Full_View (Prev))
11933 then
11934 Enter_Name (Id);
11935
11936 -- Verify that types of both declarations match, or else that both types
11937 -- are anonymous access types whose designated subtypes statically match
11938 -- (as allowed in Ada 2005 by AI-385).
11939
11940 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
11941 and then
11942 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
11943 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
11944 or else Is_Access_Constant (Etype (New_T)) /=
11945 Is_Access_Constant (Etype (Prev))
11946 or else Can_Never_Be_Null (Etype (New_T)) /=
11947 Can_Never_Be_Null (Etype (Prev))
11948 or else Null_Exclusion_Present (Parent (Prev)) /=
11949 Null_Exclusion_Present (Parent (Id))
11950 or else not Subtypes_Statically_Match
11951 (Designated_Type (Etype (Prev)),
11952 Designated_Type (Etype (New_T))))
11953 then
11954 Error_Msg_Sloc := Sloc (Prev);
11955 Error_Msg_N ("type does not match declaration#", N);
11956 Set_Full_View (Prev, Id);
11957 Set_Etype (Id, Any_Type);
11958
11959 -- A deferred constant whose type is an anonymous array is always
11960 -- illegal (unless imported). A detailed error message might be
11961 -- helpful for Ada beginners.
11962
11963 if Nkind (Object_Definition (Parent (Prev)))
11964 = N_Constrained_Array_Definition
11965 and then Nkind (Object_Definition (N))
11966 = N_Constrained_Array_Definition
11967 then
11968 Error_Msg_N ("\each anonymous array is a distinct type", N);
11969 Error_Msg_N ("a deferred constant must have a named type",
11970 Object_Definition (Parent (Prev)));
11971 end if;
11972
11973 elsif
11974 Null_Exclusion_Present (Parent (Prev))
11975 and then not Null_Exclusion_Present (N)
11976 then
11977 Error_Msg_Sloc := Sloc (Prev);
11978 Error_Msg_N ("null-exclusion does not match declaration#", N);
11979 Set_Full_View (Prev, Id);
11980 Set_Etype (Id, Any_Type);
11981
11982 -- If so, process the full constant declaration
11983
11984 else
11985 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
11986 -- the deferred declaration is constrained, then the subtype defined
11987 -- by the subtype_indication in the full declaration shall match it
11988 -- statically.
11989
11990 Check_Possible_Deferred_Completion
11991 (Prev_Id => Prev,
11992 Prev_Obj_Def => Object_Definition (Parent (Prev)),
11993 Curr_Obj_Def => Obj_Def);
11994
11995 Set_Full_View (Prev, Id);
11996 Set_Is_Public (Id, Is_Public (Prev));
11997 Set_Is_Internal (Id);
11998 Append_Entity (Id, Current_Scope);
11999
12000 -- Check ALIASED present if present before (RM 7.4(7))
12001
12002 if Is_Aliased (Prev)
12003 and then not Aliased_Present (N)
12004 then
12005 Error_Msg_Sloc := Sloc (Prev);
12006 Error_Msg_N ("ALIASED required (see declaration #)", N);
12007 end if;
12008
12009 -- Check that placement is in private part and that the incomplete
12010 -- declaration appeared in the visible part.
12011
12012 if Ekind (Current_Scope) = E_Package
12013 and then not In_Private_Part (Current_Scope)
12014 then
12015 Error_Msg_Sloc := Sloc (Prev);
12016 Error_Msg_N
12017 ("full constant for declaration # must be in private part", N);
12018
12019 elsif Ekind (Current_Scope) = E_Package
12020 and then
12021 List_Containing (Parent (Prev)) /=
12022 Visible_Declarations (Package_Specification (Current_Scope))
12023 then
12024 Error_Msg_N
12025 ("deferred constant must be declared in visible part",
12026 Parent (Prev));
12027 end if;
12028
12029 if Is_Access_Type (T)
12030 and then Nkind (Expression (N)) = N_Allocator
12031 then
12032 Check_Recursive_Declaration (Designated_Type (T));
12033 end if;
12034
12035 -- A deferred constant is a visible entity. If type has invariants,
12036 -- verify that the initial value satisfies them.
12037
12038 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12039 Insert_After (N,
12040 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12041 end if;
12042 end if;
12043 end Constant_Redeclaration;
12044
12045 ----------------------
12046 -- Constrain_Access --
12047 ----------------------
12048
12049 procedure Constrain_Access
12050 (Def_Id : in out Entity_Id;
12051 S : Node_Id;
12052 Related_Nod : Node_Id)
12053 is
12054 T : constant Entity_Id := Entity (Subtype_Mark (S));
12055 Desig_Type : constant Entity_Id := Designated_Type (T);
12056 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12057 Constraint_OK : Boolean := True;
12058
12059 begin
12060 if Is_Array_Type (Desig_Type) then
12061 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12062
12063 elsif (Is_Record_Type (Desig_Type)
12064 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12065 and then not Is_Constrained (Desig_Type)
12066 then
12067 -- ??? The following code is a temporary bypass to ignore a
12068 -- discriminant constraint on access type if it is constraining
12069 -- the current record. Avoid creating the implicit subtype of the
12070 -- record we are currently compiling since right now, we cannot
12071 -- handle these. For now, just return the access type itself.
12072
12073 if Desig_Type = Current_Scope
12074 and then No (Def_Id)
12075 then
12076 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12077 Def_Id := Entity (Subtype_Mark (S));
12078
12079 -- This call added to ensure that the constraint is analyzed
12080 -- (needed for a B test). Note that we still return early from
12081 -- this procedure to avoid recursive processing. ???
12082
12083 Constrain_Discriminated_Type
12084 (Desig_Subtype, S, Related_Nod, For_Access => True);
12085 return;
12086 end if;
12087
12088 -- Enforce rule that the constraint is illegal if there is an
12089 -- unconstrained view of the designated type. This means that the
12090 -- partial view (either a private type declaration or a derivation
12091 -- from a private type) has no discriminants. (Defect Report
12092 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12093
12094 -- Rule updated for Ada 2005: The private type is said to have
12095 -- a constrained partial view, given that objects of the type
12096 -- can be declared. Furthermore, the rule applies to all access
12097 -- types, unlike the rule concerning default discriminants (see
12098 -- RM 3.7.1(7/3))
12099
12100 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12101 and then Has_Private_Declaration (Desig_Type)
12102 and then In_Open_Scopes (Scope (Desig_Type))
12103 and then Has_Discriminants (Desig_Type)
12104 then
12105 declare
12106 Pack : constant Node_Id :=
12107 Unit_Declaration_Node (Scope (Desig_Type));
12108 Decls : List_Id;
12109 Decl : Node_Id;
12110
12111 begin
12112 if Nkind (Pack) = N_Package_Declaration then
12113 Decls := Visible_Declarations (Specification (Pack));
12114 Decl := First (Decls);
12115 while Present (Decl) loop
12116 if (Nkind (Decl) = N_Private_Type_Declaration
12117 and then Chars (Defining_Identifier (Decl)) =
12118 Chars (Desig_Type))
12119
12120 or else
12121 (Nkind (Decl) = N_Full_Type_Declaration
12122 and then
12123 Chars (Defining_Identifier (Decl)) =
12124 Chars (Desig_Type)
12125 and then Is_Derived_Type (Desig_Type)
12126 and then
12127 Has_Private_Declaration (Etype (Desig_Type)))
12128 then
12129 if No (Discriminant_Specifications (Decl)) then
12130 Error_Msg_N
12131 ("cannot constrain access type if designated "
12132 & "type has constrained partial view", S);
12133 end if;
12134
12135 exit;
12136 end if;
12137
12138 Next (Decl);
12139 end loop;
12140 end if;
12141 end;
12142 end if;
12143
12144 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12145 For_Access => True);
12146
12147 elsif Is_Concurrent_Type (Desig_Type)
12148 and then not Is_Constrained (Desig_Type)
12149 then
12150 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12151
12152 else
12153 Error_Msg_N ("invalid constraint on access type", S);
12154
12155 -- We simply ignore an invalid constraint
12156
12157 Desig_Subtype := Desig_Type;
12158 Constraint_OK := False;
12159 end if;
12160
12161 if No (Def_Id) then
12162 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12163 else
12164 Set_Ekind (Def_Id, E_Access_Subtype);
12165 end if;
12166
12167 if Constraint_OK then
12168 Set_Etype (Def_Id, Base_Type (T));
12169
12170 if Is_Private_Type (Desig_Type) then
12171 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12172 end if;
12173 else
12174 Set_Etype (Def_Id, Any_Type);
12175 end if;
12176
12177 Set_Size_Info (Def_Id, T);
12178 Set_Is_Constrained (Def_Id, Constraint_OK);
12179 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12180 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12181 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12182
12183 Conditional_Delay (Def_Id, T);
12184
12185 -- AI-363 : Subtypes of general access types whose designated types have
12186 -- default discriminants are disallowed. In instances, the rule has to
12187 -- be checked against the actual, of which T is the subtype. In a
12188 -- generic body, the rule is checked assuming that the actual type has
12189 -- defaulted discriminants.
12190
12191 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12192 if Ekind (Base_Type (T)) = E_General_Access_Type
12193 and then Has_Defaulted_Discriminants (Desig_Type)
12194 then
12195 if Ada_Version < Ada_2005 then
12196 Error_Msg_N
12197 ("access subtype of general access type would not " &
12198 "be allowed in Ada 2005?y?", S);
12199 else
12200 Error_Msg_N
12201 ("access subtype of general access type not allowed", S);
12202 end if;
12203
12204 Error_Msg_N ("\discriminants have defaults", S);
12205
12206 elsif Is_Access_Type (T)
12207 and then Is_Generic_Type (Desig_Type)
12208 and then Has_Discriminants (Desig_Type)
12209 and then In_Package_Body (Current_Scope)
12210 then
12211 if Ada_Version < Ada_2005 then
12212 Error_Msg_N
12213 ("access subtype would not be allowed in generic body "
12214 & "in Ada 2005?y?", S);
12215 else
12216 Error_Msg_N
12217 ("access subtype not allowed in generic body", S);
12218 end if;
12219
12220 Error_Msg_N
12221 ("\designated type is a discriminated formal", S);
12222 end if;
12223 end if;
12224 end Constrain_Access;
12225
12226 ---------------------
12227 -- Constrain_Array --
12228 ---------------------
12229
12230 procedure Constrain_Array
12231 (Def_Id : in out Entity_Id;
12232 SI : Node_Id;
12233 Related_Nod : Node_Id;
12234 Related_Id : Entity_Id;
12235 Suffix : Character)
12236 is
12237 C : constant Node_Id := Constraint (SI);
12238 Number_Of_Constraints : Nat := 0;
12239 Index : Node_Id;
12240 S, T : Entity_Id;
12241 Constraint_OK : Boolean := True;
12242
12243 begin
12244 T := Entity (Subtype_Mark (SI));
12245
12246 if Is_Access_Type (T) then
12247 T := Designated_Type (T);
12248 end if;
12249
12250 -- If an index constraint follows a subtype mark in a subtype indication
12251 -- then the type or subtype denoted by the subtype mark must not already
12252 -- impose an index constraint. The subtype mark must denote either an
12253 -- unconstrained array type or an access type whose designated type
12254 -- is such an array type... (RM 3.6.1)
12255
12256 if Is_Constrained (T) then
12257 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12258 Constraint_OK := False;
12259
12260 else
12261 S := First (Constraints (C));
12262 while Present (S) loop
12263 Number_Of_Constraints := Number_Of_Constraints + 1;
12264 Next (S);
12265 end loop;
12266
12267 -- In either case, the index constraint must provide a discrete
12268 -- range for each index of the array type and the type of each
12269 -- discrete range must be the same as that of the corresponding
12270 -- index. (RM 3.6.1)
12271
12272 if Number_Of_Constraints /= Number_Dimensions (T) then
12273 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12274 Constraint_OK := False;
12275
12276 else
12277 S := First (Constraints (C));
12278 Index := First_Index (T);
12279 Analyze (Index);
12280
12281 -- Apply constraints to each index type
12282
12283 for J in 1 .. Number_Of_Constraints loop
12284 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12285 Next (Index);
12286 Next (S);
12287 end loop;
12288
12289 end if;
12290 end if;
12291
12292 if No (Def_Id) then
12293 Def_Id :=
12294 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12295 Set_Parent (Def_Id, Related_Nod);
12296
12297 else
12298 Set_Ekind (Def_Id, E_Array_Subtype);
12299 end if;
12300
12301 Set_Size_Info (Def_Id, (T));
12302 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12303 Set_Etype (Def_Id, Base_Type (T));
12304
12305 if Constraint_OK then
12306 Set_First_Index (Def_Id, First (Constraints (C)));
12307 else
12308 Set_First_Index (Def_Id, First_Index (T));
12309 end if;
12310
12311 Set_Is_Constrained (Def_Id, True);
12312 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12313 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12314
12315 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12316 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12317
12318 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12319 -- We need to initialize the attribute because if Def_Id is previously
12320 -- analyzed through a limited_with clause, it will have the attributes
12321 -- of an incomplete type, one of which is an Elist that overlaps the
12322 -- Packed_Array_Impl_Type field.
12323
12324 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12325
12326 -- Build a freeze node if parent still needs one. Also make sure that
12327 -- the Depends_On_Private status is set because the subtype will need
12328 -- reprocessing at the time the base type does, and also we must set a
12329 -- conditional delay.
12330
12331 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12332 Conditional_Delay (Def_Id, T);
12333 end Constrain_Array;
12334
12335 ------------------------------
12336 -- Constrain_Component_Type --
12337 ------------------------------
12338
12339 function Constrain_Component_Type
12340 (Comp : Entity_Id;
12341 Constrained_Typ : Entity_Id;
12342 Related_Node : Node_Id;
12343 Typ : Entity_Id;
12344 Constraints : Elist_Id) return Entity_Id
12345 is
12346 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12347 Compon_Type : constant Entity_Id := Etype (Comp);
12348
12349 function Build_Constrained_Array_Type
12350 (Old_Type : Entity_Id) return Entity_Id;
12351 -- If Old_Type is an array type, one of whose indexes is constrained
12352 -- by a discriminant, build an Itype whose constraint replaces the
12353 -- discriminant with its value in the constraint.
12354
12355 function Build_Constrained_Discriminated_Type
12356 (Old_Type : Entity_Id) return Entity_Id;
12357 -- Ditto for record components
12358
12359 function Build_Constrained_Access_Type
12360 (Old_Type : Entity_Id) return Entity_Id;
12361 -- Ditto for access types. Makes use of previous two functions, to
12362 -- constrain designated type.
12363
12364 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12365 -- T is an array or discriminated type, C is a list of constraints
12366 -- that apply to T. This routine builds the constrained subtype.
12367
12368 function Is_Discriminant (Expr : Node_Id) return Boolean;
12369 -- Returns True if Expr is a discriminant
12370
12371 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12372 -- Find the value of discriminant Discrim in Constraint
12373
12374 -----------------------------------
12375 -- Build_Constrained_Access_Type --
12376 -----------------------------------
12377
12378 function Build_Constrained_Access_Type
12379 (Old_Type : Entity_Id) return Entity_Id
12380 is
12381 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12382 Itype : Entity_Id;
12383 Desig_Subtype : Entity_Id;
12384 Scop : Entity_Id;
12385
12386 begin
12387 -- if the original access type was not embedded in the enclosing
12388 -- type definition, there is no need to produce a new access
12389 -- subtype. In fact every access type with an explicit constraint
12390 -- generates an itype whose scope is the enclosing record.
12391
12392 if not Is_Type (Scope (Old_Type)) then
12393 return Old_Type;
12394
12395 elsif Is_Array_Type (Desig_Type) then
12396 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12397
12398 elsif Has_Discriminants (Desig_Type) then
12399
12400 -- This may be an access type to an enclosing record type for
12401 -- which we are constructing the constrained components. Return
12402 -- the enclosing record subtype. This is not always correct,
12403 -- but avoids infinite recursion. ???
12404
12405 Desig_Subtype := Any_Type;
12406
12407 for J in reverse 0 .. Scope_Stack.Last loop
12408 Scop := Scope_Stack.Table (J).Entity;
12409
12410 if Is_Type (Scop)
12411 and then Base_Type (Scop) = Base_Type (Desig_Type)
12412 then
12413 Desig_Subtype := Scop;
12414 end if;
12415
12416 exit when not Is_Type (Scop);
12417 end loop;
12418
12419 if Desig_Subtype = Any_Type then
12420 Desig_Subtype :=
12421 Build_Constrained_Discriminated_Type (Desig_Type);
12422 end if;
12423
12424 else
12425 return Old_Type;
12426 end if;
12427
12428 if Desig_Subtype /= Desig_Type then
12429
12430 -- The Related_Node better be here or else we won't be able
12431 -- to attach new itypes to a node in the tree.
12432
12433 pragma Assert (Present (Related_Node));
12434
12435 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12436
12437 Set_Etype (Itype, Base_Type (Old_Type));
12438 Set_Size_Info (Itype, (Old_Type));
12439 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12440 Set_Depends_On_Private (Itype, Has_Private_Component
12441 (Old_Type));
12442 Set_Is_Access_Constant (Itype, Is_Access_Constant
12443 (Old_Type));
12444
12445 -- The new itype needs freezing when it depends on a not frozen
12446 -- type and the enclosing subtype needs freezing.
12447
12448 if Has_Delayed_Freeze (Constrained_Typ)
12449 and then not Is_Frozen (Constrained_Typ)
12450 then
12451 Conditional_Delay (Itype, Base_Type (Old_Type));
12452 end if;
12453
12454 return Itype;
12455
12456 else
12457 return Old_Type;
12458 end if;
12459 end Build_Constrained_Access_Type;
12460
12461 ----------------------------------
12462 -- Build_Constrained_Array_Type --
12463 ----------------------------------
12464
12465 function Build_Constrained_Array_Type
12466 (Old_Type : Entity_Id) return Entity_Id
12467 is
12468 Lo_Expr : Node_Id;
12469 Hi_Expr : Node_Id;
12470 Old_Index : Node_Id;
12471 Range_Node : Node_Id;
12472 Constr_List : List_Id;
12473
12474 Need_To_Create_Itype : Boolean := False;
12475
12476 begin
12477 Old_Index := First_Index (Old_Type);
12478 while Present (Old_Index) loop
12479 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12480
12481 if Is_Discriminant (Lo_Expr)
12482 or else
12483 Is_Discriminant (Hi_Expr)
12484 then
12485 Need_To_Create_Itype := True;
12486 end if;
12487
12488 Next_Index (Old_Index);
12489 end loop;
12490
12491 if Need_To_Create_Itype then
12492 Constr_List := New_List;
12493
12494 Old_Index := First_Index (Old_Type);
12495 while Present (Old_Index) loop
12496 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12497
12498 if Is_Discriminant (Lo_Expr) then
12499 Lo_Expr := Get_Discr_Value (Lo_Expr);
12500 end if;
12501
12502 if Is_Discriminant (Hi_Expr) then
12503 Hi_Expr := Get_Discr_Value (Hi_Expr);
12504 end if;
12505
12506 Range_Node :=
12507 Make_Range
12508 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12509
12510 Append (Range_Node, To => Constr_List);
12511
12512 Next_Index (Old_Index);
12513 end loop;
12514
12515 return Build_Subtype (Old_Type, Constr_List);
12516
12517 else
12518 return Old_Type;
12519 end if;
12520 end Build_Constrained_Array_Type;
12521
12522 ------------------------------------------
12523 -- Build_Constrained_Discriminated_Type --
12524 ------------------------------------------
12525
12526 function Build_Constrained_Discriminated_Type
12527 (Old_Type : Entity_Id) return Entity_Id
12528 is
12529 Expr : Node_Id;
12530 Constr_List : List_Id;
12531 Old_Constraint : Elmt_Id;
12532
12533 Need_To_Create_Itype : Boolean := False;
12534
12535 begin
12536 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12537 while Present (Old_Constraint) loop
12538 Expr := Node (Old_Constraint);
12539
12540 if Is_Discriminant (Expr) then
12541 Need_To_Create_Itype := True;
12542 end if;
12543
12544 Next_Elmt (Old_Constraint);
12545 end loop;
12546
12547 if Need_To_Create_Itype then
12548 Constr_List := New_List;
12549
12550 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12551 while Present (Old_Constraint) loop
12552 Expr := Node (Old_Constraint);
12553
12554 if Is_Discriminant (Expr) then
12555 Expr := Get_Discr_Value (Expr);
12556 end if;
12557
12558 Append (New_Copy_Tree (Expr), To => Constr_List);
12559
12560 Next_Elmt (Old_Constraint);
12561 end loop;
12562
12563 return Build_Subtype (Old_Type, Constr_List);
12564
12565 else
12566 return Old_Type;
12567 end if;
12568 end Build_Constrained_Discriminated_Type;
12569
12570 -------------------
12571 -- Build_Subtype --
12572 -------------------
12573
12574 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12575 Indic : Node_Id;
12576 Subtyp_Decl : Node_Id;
12577 Def_Id : Entity_Id;
12578 Btyp : Entity_Id := Base_Type (T);
12579
12580 begin
12581 -- The Related_Node better be here or else we won't be able to
12582 -- attach new itypes to a node in the tree.
12583
12584 pragma Assert (Present (Related_Node));
12585
12586 -- If the view of the component's type is incomplete or private
12587 -- with unknown discriminants, then the constraint must be applied
12588 -- to the full type.
12589
12590 if Has_Unknown_Discriminants (Btyp)
12591 and then Present (Underlying_Type (Btyp))
12592 then
12593 Btyp := Underlying_Type (Btyp);
12594 end if;
12595
12596 Indic :=
12597 Make_Subtype_Indication (Loc,
12598 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12599 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12600
12601 Def_Id := Create_Itype (Ekind (T), Related_Node);
12602
12603 Subtyp_Decl :=
12604 Make_Subtype_Declaration (Loc,
12605 Defining_Identifier => Def_Id,
12606 Subtype_Indication => Indic);
12607
12608 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12609
12610 -- Itypes must be analyzed with checks off (see package Itypes)
12611
12612 Analyze (Subtyp_Decl, Suppress => All_Checks);
12613
12614 return Def_Id;
12615 end Build_Subtype;
12616
12617 ---------------------
12618 -- Get_Discr_Value --
12619 ---------------------
12620
12621 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12622 D : Entity_Id;
12623 E : Elmt_Id;
12624
12625 begin
12626 -- The discriminant may be declared for the type, in which case we
12627 -- find it by iterating over the list of discriminants. If the
12628 -- discriminant is inherited from a parent type, it appears as the
12629 -- corresponding discriminant of the current type. This will be the
12630 -- case when constraining an inherited component whose constraint is
12631 -- given by a discriminant of the parent.
12632
12633 D := First_Discriminant (Typ);
12634 E := First_Elmt (Constraints);
12635
12636 while Present (D) loop
12637 if D = Entity (Discrim)
12638 or else D = CR_Discriminant (Entity (Discrim))
12639 or else Corresponding_Discriminant (D) = Entity (Discrim)
12640 then
12641 return Node (E);
12642 end if;
12643
12644 Next_Discriminant (D);
12645 Next_Elmt (E);
12646 end loop;
12647
12648 -- The Corresponding_Discriminant mechanism is incomplete, because
12649 -- the correspondence between new and old discriminants is not one
12650 -- to one: one new discriminant can constrain several old ones. In
12651 -- that case, scan sequentially the stored_constraint, the list of
12652 -- discriminants of the parents, and the constraints.
12653
12654 -- Previous code checked for the present of the Stored_Constraint
12655 -- list for the derived type, but did not use it at all. Should it
12656 -- be present when the component is a discriminated task type?
12657
12658 if Is_Derived_Type (Typ)
12659 and then Scope (Entity (Discrim)) = Etype (Typ)
12660 then
12661 D := First_Discriminant (Etype (Typ));
12662 E := First_Elmt (Constraints);
12663 while Present (D) loop
12664 if D = Entity (Discrim) then
12665 return Node (E);
12666 end if;
12667
12668 Next_Discriminant (D);
12669 Next_Elmt (E);
12670 end loop;
12671 end if;
12672
12673 -- Something is wrong if we did not find the value
12674
12675 raise Program_Error;
12676 end Get_Discr_Value;
12677
12678 ---------------------
12679 -- Is_Discriminant --
12680 ---------------------
12681
12682 function Is_Discriminant (Expr : Node_Id) return Boolean is
12683 Discrim_Scope : Entity_Id;
12684
12685 begin
12686 if Denotes_Discriminant (Expr) then
12687 Discrim_Scope := Scope (Entity (Expr));
12688
12689 -- Either we have a reference to one of Typ's discriminants,
12690
12691 pragma Assert (Discrim_Scope = Typ
12692
12693 -- or to the discriminants of the parent type, in the case
12694 -- of a derivation of a tagged type with variants.
12695
12696 or else Discrim_Scope = Etype (Typ)
12697 or else Full_View (Discrim_Scope) = Etype (Typ)
12698
12699 -- or same as above for the case where the discriminants
12700 -- were declared in Typ's private view.
12701
12702 or else (Is_Private_Type (Discrim_Scope)
12703 and then Chars (Discrim_Scope) = Chars (Typ))
12704
12705 -- or else we are deriving from the full view and the
12706 -- discriminant is declared in the private entity.
12707
12708 or else (Is_Private_Type (Typ)
12709 and then Chars (Discrim_Scope) = Chars (Typ))
12710
12711 -- Or we are constrained the corresponding record of a
12712 -- synchronized type that completes a private declaration.
12713
12714 or else (Is_Concurrent_Record_Type (Typ)
12715 and then
12716 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12717
12718 -- or we have a class-wide type, in which case make sure the
12719 -- discriminant found belongs to the root type.
12720
12721 or else (Is_Class_Wide_Type (Typ)
12722 and then Etype (Typ) = Discrim_Scope));
12723
12724 return True;
12725 end if;
12726
12727 -- In all other cases we have something wrong
12728
12729 return False;
12730 end Is_Discriminant;
12731
12732 -- Start of processing for Constrain_Component_Type
12733
12734 begin
12735 if Nkind (Parent (Comp)) = N_Component_Declaration
12736 and then Comes_From_Source (Parent (Comp))
12737 and then Comes_From_Source
12738 (Subtype_Indication (Component_Definition (Parent (Comp))))
12739 and then
12740 Is_Entity_Name
12741 (Subtype_Indication (Component_Definition (Parent (Comp))))
12742 then
12743 return Compon_Type;
12744
12745 elsif Is_Array_Type (Compon_Type) then
12746 return Build_Constrained_Array_Type (Compon_Type);
12747
12748 elsif Has_Discriminants (Compon_Type) then
12749 return Build_Constrained_Discriminated_Type (Compon_Type);
12750
12751 elsif Is_Access_Type (Compon_Type) then
12752 return Build_Constrained_Access_Type (Compon_Type);
12753
12754 else
12755 return Compon_Type;
12756 end if;
12757 end Constrain_Component_Type;
12758
12759 --------------------------
12760 -- Constrain_Concurrent --
12761 --------------------------
12762
12763 -- For concurrent types, the associated record value type carries the same
12764 -- discriminants, so when we constrain a concurrent type, we must constrain
12765 -- the corresponding record type as well.
12766
12767 procedure Constrain_Concurrent
12768 (Def_Id : in out Entity_Id;
12769 SI : Node_Id;
12770 Related_Nod : Node_Id;
12771 Related_Id : Entity_Id;
12772 Suffix : Character)
12773 is
12774 -- Retrieve Base_Type to ensure getting to the concurrent type in the
12775 -- case of a private subtype (needed when only doing semantic analysis).
12776
12777 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
12778 T_Val : Entity_Id;
12779
12780 begin
12781 if Is_Access_Type (T_Ent) then
12782 T_Ent := Designated_Type (T_Ent);
12783 end if;
12784
12785 T_Val := Corresponding_Record_Type (T_Ent);
12786
12787 if Present (T_Val) then
12788
12789 if No (Def_Id) then
12790 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12791 end if;
12792
12793 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12794
12795 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12796 Set_Corresponding_Record_Type (Def_Id,
12797 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
12798
12799 else
12800 -- If there is no associated record, expansion is disabled and this
12801 -- is a generic context. Create a subtype in any case, so that
12802 -- semantic analysis can proceed.
12803
12804 if No (Def_Id) then
12805 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12806 end if;
12807
12808 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12809 end if;
12810 end Constrain_Concurrent;
12811
12812 ------------------------------------
12813 -- Constrain_Corresponding_Record --
12814 ------------------------------------
12815
12816 function Constrain_Corresponding_Record
12817 (Prot_Subt : Entity_Id;
12818 Corr_Rec : Entity_Id;
12819 Related_Nod : Node_Id) return Entity_Id
12820 is
12821 T_Sub : constant Entity_Id :=
12822 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
12823
12824 begin
12825 Set_Etype (T_Sub, Corr_Rec);
12826 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
12827 Set_Is_Constrained (T_Sub, True);
12828 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
12829 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
12830
12831 if Has_Discriminants (Prot_Subt) then -- False only if errors.
12832 Set_Discriminant_Constraint
12833 (T_Sub, Discriminant_Constraint (Prot_Subt));
12834 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
12835 Create_Constrained_Components
12836 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
12837 end if;
12838
12839 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
12840
12841 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
12842 Conditional_Delay (T_Sub, Corr_Rec);
12843
12844 else
12845 -- This is a component subtype: it will be frozen in the context of
12846 -- the enclosing record's init_proc, so that discriminant references
12847 -- are resolved to discriminals. (Note: we used to skip freezing
12848 -- altogether in that case, which caused errors downstream for
12849 -- components of a bit packed array type).
12850
12851 Set_Has_Delayed_Freeze (T_Sub);
12852 end if;
12853
12854 return T_Sub;
12855 end Constrain_Corresponding_Record;
12856
12857 -----------------------
12858 -- Constrain_Decimal --
12859 -----------------------
12860
12861 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
12862 T : constant Entity_Id := Entity (Subtype_Mark (S));
12863 C : constant Node_Id := Constraint (S);
12864 Loc : constant Source_Ptr := Sloc (C);
12865 Range_Expr : Node_Id;
12866 Digits_Expr : Node_Id;
12867 Digits_Val : Uint;
12868 Bound_Val : Ureal;
12869
12870 begin
12871 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
12872
12873 if Nkind (C) = N_Range_Constraint then
12874 Range_Expr := Range_Expression (C);
12875 Digits_Val := Digits_Value (T);
12876
12877 else
12878 pragma Assert (Nkind (C) = N_Digits_Constraint);
12879
12880 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
12881
12882 Digits_Expr := Digits_Expression (C);
12883 Analyze_And_Resolve (Digits_Expr, Any_Integer);
12884
12885 Check_Digits_Expression (Digits_Expr);
12886 Digits_Val := Expr_Value (Digits_Expr);
12887
12888 if Digits_Val > Digits_Value (T) then
12889 Error_Msg_N
12890 ("digits expression is incompatible with subtype", C);
12891 Digits_Val := Digits_Value (T);
12892 end if;
12893
12894 if Present (Range_Constraint (C)) then
12895 Range_Expr := Range_Expression (Range_Constraint (C));
12896 else
12897 Range_Expr := Empty;
12898 end if;
12899 end if;
12900
12901 Set_Etype (Def_Id, Base_Type (T));
12902 Set_Size_Info (Def_Id, (T));
12903 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12904 Set_Delta_Value (Def_Id, Delta_Value (T));
12905 Set_Scale_Value (Def_Id, Scale_Value (T));
12906 Set_Small_Value (Def_Id, Small_Value (T));
12907 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
12908 Set_Digits_Value (Def_Id, Digits_Val);
12909
12910 -- Manufacture range from given digits value if no range present
12911
12912 if No (Range_Expr) then
12913 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
12914 Range_Expr :=
12915 Make_Range (Loc,
12916 Low_Bound =>
12917 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
12918 High_Bound =>
12919 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
12920 end if;
12921
12922 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
12923 Set_Discrete_RM_Size (Def_Id);
12924
12925 -- Unconditionally delay the freeze, since we cannot set size
12926 -- information in all cases correctly until the freeze point.
12927
12928 Set_Has_Delayed_Freeze (Def_Id);
12929 end Constrain_Decimal;
12930
12931 ----------------------------------
12932 -- Constrain_Discriminated_Type --
12933 ----------------------------------
12934
12935 procedure Constrain_Discriminated_Type
12936 (Def_Id : Entity_Id;
12937 S : Node_Id;
12938 Related_Nod : Node_Id;
12939 For_Access : Boolean := False)
12940 is
12941 E : constant Entity_Id := Entity (Subtype_Mark (S));
12942 T : Entity_Id;
12943 C : Node_Id;
12944 Elist : Elist_Id := New_Elmt_List;
12945
12946 procedure Fixup_Bad_Constraint;
12947 -- This is called after finding a bad constraint, and after having
12948 -- posted an appropriate error message. The mission is to leave the
12949 -- entity T in as reasonable state as possible.
12950
12951 --------------------------
12952 -- Fixup_Bad_Constraint --
12953 --------------------------
12954
12955 procedure Fixup_Bad_Constraint is
12956 begin
12957 -- Set a reasonable Ekind for the entity. For an incomplete type,
12958 -- we can't do much, but for other types, we can set the proper
12959 -- corresponding subtype kind.
12960
12961 if Ekind (T) = E_Incomplete_Type then
12962 Set_Ekind (Def_Id, Ekind (T));
12963 else
12964 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
12965 end if;
12966
12967 -- Set Etype to the known type, to reduce chances of cascaded errors
12968
12969 Set_Etype (Def_Id, E);
12970 Set_Error_Posted (Def_Id);
12971 end Fixup_Bad_Constraint;
12972
12973 -- Start of processing for Constrain_Discriminated_Type
12974
12975 begin
12976 C := Constraint (S);
12977
12978 -- A discriminant constraint is only allowed in a subtype indication,
12979 -- after a subtype mark. This subtype mark must denote either a type
12980 -- with discriminants, or an access type whose designated type is a
12981 -- type with discriminants. A discriminant constraint specifies the
12982 -- values of these discriminants (RM 3.7.2(5)).
12983
12984 T := Base_Type (Entity (Subtype_Mark (S)));
12985
12986 if Is_Access_Type (T) then
12987 T := Designated_Type (T);
12988 end if;
12989
12990 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
12991 -- Avoid generating an error for access-to-incomplete subtypes.
12992
12993 if Ada_Version >= Ada_2005
12994 and then Ekind (T) = E_Incomplete_Type
12995 and then Nkind (Parent (S)) = N_Subtype_Declaration
12996 and then not Is_Itype (Def_Id)
12997 then
12998 -- A little sanity check, emit an error message if the type
12999 -- has discriminants to begin with. Type T may be a regular
13000 -- incomplete type or imported via a limited with clause.
13001
13002 if Has_Discriminants (T)
13003 or else (From_Limited_With (T)
13004 and then Present (Non_Limited_View (T))
13005 and then Nkind (Parent (Non_Limited_View (T))) =
13006 N_Full_Type_Declaration
13007 and then Present (Discriminant_Specifications
13008 (Parent (Non_Limited_View (T)))))
13009 then
13010 Error_Msg_N
13011 ("(Ada 2005) incomplete subtype may not be constrained", C);
13012 else
13013 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13014 end if;
13015
13016 Fixup_Bad_Constraint;
13017 return;
13018
13019 -- Check that the type has visible discriminants. The type may be
13020 -- a private type with unknown discriminants whose full view has
13021 -- discriminants which are invisible.
13022
13023 elsif not Has_Discriminants (T)
13024 or else
13025 (Has_Unknown_Discriminants (T)
13026 and then Is_Private_Type (T))
13027 then
13028 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13029 Fixup_Bad_Constraint;
13030 return;
13031
13032 elsif Is_Constrained (E)
13033 or else (Ekind (E) = E_Class_Wide_Subtype
13034 and then Present (Discriminant_Constraint (E)))
13035 then
13036 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13037 Fixup_Bad_Constraint;
13038 return;
13039 end if;
13040
13041 -- T may be an unconstrained subtype (e.g. a generic actual).
13042 -- Constraint applies to the base type.
13043
13044 T := Base_Type (T);
13045
13046 Elist := Build_Discriminant_Constraints (T, S);
13047
13048 -- If the list returned was empty we had an error in building the
13049 -- discriminant constraint. We have also already signalled an error
13050 -- in the incomplete type case
13051
13052 if Is_Empty_Elmt_List (Elist) then
13053 Fixup_Bad_Constraint;
13054 return;
13055 end if;
13056
13057 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
13058 end Constrain_Discriminated_Type;
13059
13060 ---------------------------
13061 -- Constrain_Enumeration --
13062 ---------------------------
13063
13064 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13065 T : constant Entity_Id := Entity (Subtype_Mark (S));
13066 C : constant Node_Id := Constraint (S);
13067
13068 begin
13069 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13070
13071 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13072
13073 Set_Etype (Def_Id, Base_Type (T));
13074 Set_Size_Info (Def_Id, (T));
13075 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13076 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13077
13078 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13079
13080 Set_Discrete_RM_Size (Def_Id);
13081 end Constrain_Enumeration;
13082
13083 ----------------------
13084 -- Constrain_Float --
13085 ----------------------
13086
13087 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13088 T : constant Entity_Id := Entity (Subtype_Mark (S));
13089 C : Node_Id;
13090 D : Node_Id;
13091 Rais : Node_Id;
13092
13093 begin
13094 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13095
13096 Set_Etype (Def_Id, Base_Type (T));
13097 Set_Size_Info (Def_Id, (T));
13098 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13099
13100 -- Process the constraint
13101
13102 C := Constraint (S);
13103
13104 -- Digits constraint present
13105
13106 if Nkind (C) = N_Digits_Constraint then
13107
13108 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13109 Check_Restriction (No_Obsolescent_Features, C);
13110
13111 if Warn_On_Obsolescent_Feature then
13112 Error_Msg_N
13113 ("subtype digits constraint is an " &
13114 "obsolescent feature (RM J.3(8))?j?", C);
13115 end if;
13116
13117 D := Digits_Expression (C);
13118 Analyze_And_Resolve (D, Any_Integer);
13119 Check_Digits_Expression (D);
13120 Set_Digits_Value (Def_Id, Expr_Value (D));
13121
13122 -- Check that digits value is in range. Obviously we can do this
13123 -- at compile time, but it is strictly a runtime check, and of
13124 -- course there is an ACVC test that checks this.
13125
13126 if Digits_Value (Def_Id) > Digits_Value (T) then
13127 Error_Msg_Uint_1 := Digits_Value (T);
13128 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13129 Rais :=
13130 Make_Raise_Constraint_Error (Sloc (D),
13131 Reason => CE_Range_Check_Failed);
13132 Insert_Action (Declaration_Node (Def_Id), Rais);
13133 end if;
13134
13135 C := Range_Constraint (C);
13136
13137 -- No digits constraint present
13138
13139 else
13140 Set_Digits_Value (Def_Id, Digits_Value (T));
13141 end if;
13142
13143 -- Range constraint present
13144
13145 if Nkind (C) = N_Range_Constraint then
13146 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13147
13148 -- No range constraint present
13149
13150 else
13151 pragma Assert (No (C));
13152 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13153 end if;
13154
13155 Set_Is_Constrained (Def_Id);
13156 end Constrain_Float;
13157
13158 ---------------------
13159 -- Constrain_Index --
13160 ---------------------
13161
13162 procedure Constrain_Index
13163 (Index : Node_Id;
13164 S : Node_Id;
13165 Related_Nod : Node_Id;
13166 Related_Id : Entity_Id;
13167 Suffix : Character;
13168 Suffix_Index : Nat)
13169 is
13170 Def_Id : Entity_Id;
13171 R : Node_Id := Empty;
13172 T : constant Entity_Id := Etype (Index);
13173
13174 begin
13175 if Nkind (S) = N_Range
13176 or else
13177 (Nkind (S) = N_Attribute_Reference
13178 and then Attribute_Name (S) = Name_Range)
13179 then
13180 -- A Range attribute will be transformed into N_Range by Resolve
13181
13182 Analyze (S);
13183 Set_Etype (S, T);
13184 R := S;
13185
13186 Process_Range_Expr_In_Decl (R, T);
13187
13188 if not Error_Posted (S)
13189 and then
13190 (Nkind (S) /= N_Range
13191 or else not Covers (T, (Etype (Low_Bound (S))))
13192 or else not Covers (T, (Etype (High_Bound (S)))))
13193 then
13194 if Base_Type (T) /= Any_Type
13195 and then Etype (Low_Bound (S)) /= Any_Type
13196 and then Etype (High_Bound (S)) /= Any_Type
13197 then
13198 Error_Msg_N ("range expected", S);
13199 end if;
13200 end if;
13201
13202 elsif Nkind (S) = N_Subtype_Indication then
13203
13204 -- The parser has verified that this is a discrete indication
13205
13206 Resolve_Discrete_Subtype_Indication (S, T);
13207 Bad_Predicated_Subtype_Use
13208 ("subtype& has predicate, not allowed in index constraint",
13209 S, Entity (Subtype_Mark (S)));
13210
13211 R := Range_Expression (Constraint (S));
13212
13213 -- Capture values of bounds and generate temporaries for them if
13214 -- needed, since checks may cause duplication of the expressions
13215 -- which must not be reevaluated.
13216
13217 -- The forced evaluation removes side effects from expressions, which
13218 -- should occur also in GNATprove mode. Otherwise, we end up with
13219 -- unexpected insertions of actions at places where this is not
13220 -- supposed to occur, e.g. on default parameters of a call.
13221
13222 if Expander_Active or GNATprove_Mode then
13223 Force_Evaluation (Low_Bound (R));
13224 Force_Evaluation (High_Bound (R));
13225 end if;
13226
13227 elsif Nkind (S) = N_Discriminant_Association then
13228
13229 -- Syntactically valid in subtype indication
13230
13231 Error_Msg_N ("invalid index constraint", S);
13232 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13233 return;
13234
13235 -- Subtype_Mark case, no anonymous subtypes to construct
13236
13237 else
13238 Analyze (S);
13239
13240 if Is_Entity_Name (S) then
13241 if not Is_Type (Entity (S)) then
13242 Error_Msg_N ("expect subtype mark for index constraint", S);
13243
13244 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13245 Wrong_Type (S, Base_Type (T));
13246
13247 -- Check error of subtype with predicate in index constraint
13248
13249 else
13250 Bad_Predicated_Subtype_Use
13251 ("subtype& has predicate, not allowed in index constraint",
13252 S, Entity (S));
13253 end if;
13254
13255 return;
13256
13257 else
13258 Error_Msg_N ("invalid index constraint", S);
13259 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13260 return;
13261 end if;
13262 end if;
13263
13264 Def_Id :=
13265 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13266
13267 Set_Etype (Def_Id, Base_Type (T));
13268
13269 if Is_Modular_Integer_Type (T) then
13270 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13271
13272 elsif Is_Integer_Type (T) then
13273 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13274
13275 else
13276 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13277 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13278 Set_First_Literal (Def_Id, First_Literal (T));
13279 end if;
13280
13281 Set_Size_Info (Def_Id, (T));
13282 Set_RM_Size (Def_Id, RM_Size (T));
13283 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13284
13285 Set_Scalar_Range (Def_Id, R);
13286
13287 Set_Etype (S, Def_Id);
13288 Set_Discrete_RM_Size (Def_Id);
13289 end Constrain_Index;
13290
13291 -----------------------
13292 -- Constrain_Integer --
13293 -----------------------
13294
13295 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13296 T : constant Entity_Id := Entity (Subtype_Mark (S));
13297 C : constant Node_Id := Constraint (S);
13298
13299 begin
13300 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13301
13302 if Is_Modular_Integer_Type (T) then
13303 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13304 else
13305 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13306 end if;
13307
13308 Set_Etype (Def_Id, Base_Type (T));
13309 Set_Size_Info (Def_Id, (T));
13310 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13311 Set_Discrete_RM_Size (Def_Id);
13312 end Constrain_Integer;
13313
13314 ------------------------------
13315 -- Constrain_Ordinary_Fixed --
13316 ------------------------------
13317
13318 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13319 T : constant Entity_Id := Entity (Subtype_Mark (S));
13320 C : Node_Id;
13321 D : Node_Id;
13322 Rais : Node_Id;
13323
13324 begin
13325 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13326 Set_Etype (Def_Id, Base_Type (T));
13327 Set_Size_Info (Def_Id, (T));
13328 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13329 Set_Small_Value (Def_Id, Small_Value (T));
13330
13331 -- Process the constraint
13332
13333 C := Constraint (S);
13334
13335 -- Delta constraint present
13336
13337 if Nkind (C) = N_Delta_Constraint then
13338
13339 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13340 Check_Restriction (No_Obsolescent_Features, C);
13341
13342 if Warn_On_Obsolescent_Feature then
13343 Error_Msg_S
13344 ("subtype delta constraint is an " &
13345 "obsolescent feature (RM J.3(7))?j?");
13346 end if;
13347
13348 D := Delta_Expression (C);
13349 Analyze_And_Resolve (D, Any_Real);
13350 Check_Delta_Expression (D);
13351 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13352
13353 -- Check that delta value is in range. Obviously we can do this
13354 -- at compile time, but it is strictly a runtime check, and of
13355 -- course there is an ACVC test that checks this.
13356
13357 if Delta_Value (Def_Id) < Delta_Value (T) then
13358 Error_Msg_N ("??delta value is too small", D);
13359 Rais :=
13360 Make_Raise_Constraint_Error (Sloc (D),
13361 Reason => CE_Range_Check_Failed);
13362 Insert_Action (Declaration_Node (Def_Id), Rais);
13363 end if;
13364
13365 C := Range_Constraint (C);
13366
13367 -- No delta constraint present
13368
13369 else
13370 Set_Delta_Value (Def_Id, Delta_Value (T));
13371 end if;
13372
13373 -- Range constraint present
13374
13375 if Nkind (C) = N_Range_Constraint then
13376 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13377
13378 -- No range constraint present
13379
13380 else
13381 pragma Assert (No (C));
13382 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13383
13384 end if;
13385
13386 Set_Discrete_RM_Size (Def_Id);
13387
13388 -- Unconditionally delay the freeze, since we cannot set size
13389 -- information in all cases correctly until the freeze point.
13390
13391 Set_Has_Delayed_Freeze (Def_Id);
13392 end Constrain_Ordinary_Fixed;
13393
13394 -----------------------
13395 -- Contain_Interface --
13396 -----------------------
13397
13398 function Contain_Interface
13399 (Iface : Entity_Id;
13400 Ifaces : Elist_Id) return Boolean
13401 is
13402 Iface_Elmt : Elmt_Id;
13403
13404 begin
13405 if Present (Ifaces) then
13406 Iface_Elmt := First_Elmt (Ifaces);
13407 while Present (Iface_Elmt) loop
13408 if Node (Iface_Elmt) = Iface then
13409 return True;
13410 end if;
13411
13412 Next_Elmt (Iface_Elmt);
13413 end loop;
13414 end if;
13415
13416 return False;
13417 end Contain_Interface;
13418
13419 ---------------------------
13420 -- Convert_Scalar_Bounds --
13421 ---------------------------
13422
13423 procedure Convert_Scalar_Bounds
13424 (N : Node_Id;
13425 Parent_Type : Entity_Id;
13426 Derived_Type : Entity_Id;
13427 Loc : Source_Ptr)
13428 is
13429 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13430
13431 Lo : Node_Id;
13432 Hi : Node_Id;
13433 Rng : Node_Id;
13434
13435 begin
13436 -- Defend against previous errors
13437
13438 if No (Scalar_Range (Derived_Type)) then
13439 Check_Error_Detected;
13440 return;
13441 end if;
13442
13443 Lo := Build_Scalar_Bound
13444 (Type_Low_Bound (Derived_Type),
13445 Parent_Type, Implicit_Base);
13446
13447 Hi := Build_Scalar_Bound
13448 (Type_High_Bound (Derived_Type),
13449 Parent_Type, Implicit_Base);
13450
13451 Rng :=
13452 Make_Range (Loc,
13453 Low_Bound => Lo,
13454 High_Bound => Hi);
13455
13456 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13457
13458 Set_Parent (Rng, N);
13459 Set_Scalar_Range (Derived_Type, Rng);
13460
13461 -- Analyze the bounds
13462
13463 Analyze_And_Resolve (Lo, Implicit_Base);
13464 Analyze_And_Resolve (Hi, Implicit_Base);
13465
13466 -- Analyze the range itself, except that we do not analyze it if
13467 -- the bounds are real literals, and we have a fixed-point type.
13468 -- The reason for this is that we delay setting the bounds in this
13469 -- case till we know the final Small and Size values (see circuit
13470 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13471
13472 if Is_Fixed_Point_Type (Parent_Type)
13473 and then Nkind (Lo) = N_Real_Literal
13474 and then Nkind (Hi) = N_Real_Literal
13475 then
13476 return;
13477
13478 -- Here we do the analysis of the range
13479
13480 -- Note: we do this manually, since if we do a normal Analyze and
13481 -- Resolve call, there are problems with the conversions used for
13482 -- the derived type range.
13483
13484 else
13485 Set_Etype (Rng, Implicit_Base);
13486 Set_Analyzed (Rng, True);
13487 end if;
13488 end Convert_Scalar_Bounds;
13489
13490 -------------------
13491 -- Copy_And_Swap --
13492 -------------------
13493
13494 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13495 begin
13496 -- Initialize new full declaration entity by copying the pertinent
13497 -- fields of the corresponding private declaration entity.
13498
13499 -- We temporarily set Ekind to a value appropriate for a type to
13500 -- avoid assert failures in Einfo from checking for setting type
13501 -- attributes on something that is not a type. Ekind (Priv) is an
13502 -- appropriate choice, since it allowed the attributes to be set
13503 -- in the first place. This Ekind value will be modified later.
13504
13505 Set_Ekind (Full, Ekind (Priv));
13506
13507 -- Also set Etype temporarily to Any_Type, again, in the absence
13508 -- of errors, it will be properly reset, and if there are errors,
13509 -- then we want a value of Any_Type to remain.
13510
13511 Set_Etype (Full, Any_Type);
13512
13513 -- Now start copying attributes
13514
13515 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13516
13517 if Has_Discriminants (Full) then
13518 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13519 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13520 end if;
13521
13522 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13523 Set_Homonym (Full, Homonym (Priv));
13524 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13525 Set_Is_Public (Full, Is_Public (Priv));
13526 Set_Is_Pure (Full, Is_Pure (Priv));
13527 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13528 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13529 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13530 Set_Has_Pragma_Unreferenced_Objects
13531 (Full, Has_Pragma_Unreferenced_Objects
13532 (Priv));
13533
13534 Conditional_Delay (Full, Priv);
13535
13536 if Is_Tagged_Type (Full) then
13537 Set_Direct_Primitive_Operations
13538 (Full, Direct_Primitive_Operations (Priv));
13539 Set_No_Tagged_Streams_Pragma
13540 (Full, No_Tagged_Streams_Pragma (Priv));
13541
13542 if Is_Base_Type (Priv) then
13543 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13544 end if;
13545 end if;
13546
13547 Set_Is_Volatile (Full, Is_Volatile (Priv));
13548 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13549 Set_Scope (Full, Scope (Priv));
13550 Set_Next_Entity (Full, Next_Entity (Priv));
13551 Set_First_Entity (Full, First_Entity (Priv));
13552 Set_Last_Entity (Full, Last_Entity (Priv));
13553
13554 -- If access types have been recorded for later handling, keep them in
13555 -- the full view so that they get handled when the full view freeze
13556 -- node is expanded.
13557
13558 if Present (Freeze_Node (Priv))
13559 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13560 then
13561 Ensure_Freeze_Node (Full);
13562 Set_Access_Types_To_Process
13563 (Freeze_Node (Full),
13564 Access_Types_To_Process (Freeze_Node (Priv)));
13565 end if;
13566
13567 -- Swap the two entities. Now Private is the full type entity and Full
13568 -- is the private one. They will be swapped back at the end of the
13569 -- private part. This swapping ensures that the entity that is visible
13570 -- in the private part is the full declaration.
13571
13572 Exchange_Entities (Priv, Full);
13573 Append_Entity (Full, Scope (Full));
13574 end Copy_And_Swap;
13575
13576 -------------------------------------
13577 -- Copy_Array_Base_Type_Attributes --
13578 -------------------------------------
13579
13580 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13581 begin
13582 Set_Component_Alignment (T1, Component_Alignment (T2));
13583 Set_Component_Type (T1, Component_Type (T2));
13584 Set_Component_Size (T1, Component_Size (T2));
13585 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13586 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13587 Set_Has_Protected (T1, Has_Protected (T2));
13588 Set_Has_Task (T1, Has_Task (T2));
13589 Set_Is_Packed (T1, Is_Packed (T2));
13590 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13591 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13592 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13593 end Copy_Array_Base_Type_Attributes;
13594
13595 -----------------------------------
13596 -- Copy_Array_Subtype_Attributes --
13597 -----------------------------------
13598
13599 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13600 begin
13601 Set_Size_Info (T1, T2);
13602
13603 Set_First_Index (T1, First_Index (T2));
13604 Set_Is_Aliased (T1, Is_Aliased (T2));
13605 Set_Is_Volatile (T1, Is_Volatile (T2));
13606 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13607 Set_Is_Constrained (T1, Is_Constrained (T2));
13608 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13609 Inherit_Rep_Item_Chain (T1, T2);
13610 Set_Convention (T1, Convention (T2));
13611 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13612 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13613 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13614 end Copy_Array_Subtype_Attributes;
13615
13616 -----------------------------------
13617 -- Create_Constrained_Components --
13618 -----------------------------------
13619
13620 procedure Create_Constrained_Components
13621 (Subt : Entity_Id;
13622 Decl_Node : Node_Id;
13623 Typ : Entity_Id;
13624 Constraints : Elist_Id)
13625 is
13626 Loc : constant Source_Ptr := Sloc (Subt);
13627 Comp_List : constant Elist_Id := New_Elmt_List;
13628 Parent_Type : constant Entity_Id := Etype (Typ);
13629 Assoc_List : constant List_Id := New_List;
13630 Discr_Val : Elmt_Id;
13631 Errors : Boolean;
13632 New_C : Entity_Id;
13633 Old_C : Entity_Id;
13634 Is_Static : Boolean := True;
13635
13636 procedure Collect_Fixed_Components (Typ : Entity_Id);
13637 -- Collect parent type components that do not appear in a variant part
13638
13639 procedure Create_All_Components;
13640 -- Iterate over Comp_List to create the components of the subtype
13641
13642 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13643 -- Creates a new component from Old_Compon, copying all the fields from
13644 -- it, including its Etype, inserts the new component in the Subt entity
13645 -- chain and returns the new component.
13646
13647 function Is_Variant_Record (T : Entity_Id) return Boolean;
13648 -- If true, and discriminants are static, collect only components from
13649 -- variants selected by discriminant values.
13650
13651 ------------------------------
13652 -- Collect_Fixed_Components --
13653 ------------------------------
13654
13655 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13656 begin
13657 -- Build association list for discriminants, and find components of the
13658 -- variant part selected by the values of the discriminants.
13659
13660 Old_C := First_Discriminant (Typ);
13661 Discr_Val := First_Elmt (Constraints);
13662 while Present (Old_C) loop
13663 Append_To (Assoc_List,
13664 Make_Component_Association (Loc,
13665 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13666 Expression => New_Copy (Node (Discr_Val))));
13667
13668 Next_Elmt (Discr_Val);
13669 Next_Discriminant (Old_C);
13670 end loop;
13671
13672 -- The tag and the possible parent component are unconditionally in
13673 -- the subtype.
13674
13675 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13676 Old_C := First_Component (Typ);
13677 while Present (Old_C) loop
13678 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13679 Append_Elmt (Old_C, Comp_List);
13680 end if;
13681
13682 Next_Component (Old_C);
13683 end loop;
13684 end if;
13685 end Collect_Fixed_Components;
13686
13687 ---------------------------
13688 -- Create_All_Components --
13689 ---------------------------
13690
13691 procedure Create_All_Components is
13692 Comp : Elmt_Id;
13693
13694 begin
13695 Comp := First_Elmt (Comp_List);
13696 while Present (Comp) loop
13697 Old_C := Node (Comp);
13698 New_C := Create_Component (Old_C);
13699
13700 Set_Etype
13701 (New_C,
13702 Constrain_Component_Type
13703 (Old_C, Subt, Decl_Node, Typ, Constraints));
13704 Set_Is_Public (New_C, Is_Public (Subt));
13705
13706 Next_Elmt (Comp);
13707 end loop;
13708 end Create_All_Components;
13709
13710 ----------------------
13711 -- Create_Component --
13712 ----------------------
13713
13714 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13715 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13716
13717 begin
13718 if Ekind (Old_Compon) = E_Discriminant
13719 and then Is_Completely_Hidden (Old_Compon)
13720 then
13721 -- This is a shadow discriminant created for a discriminant of
13722 -- the parent type, which needs to be present in the subtype.
13723 -- Give the shadow discriminant an internal name that cannot
13724 -- conflict with that of visible components.
13725
13726 Set_Chars (New_Compon, New_Internal_Name ('C'));
13727 end if;
13728
13729 -- Set the parent so we have a proper link for freezing etc. This is
13730 -- not a real parent pointer, since of course our parent does not own
13731 -- up to us and reference us, we are an illegitimate child of the
13732 -- original parent.
13733
13734 Set_Parent (New_Compon, Parent (Old_Compon));
13735
13736 -- If the old component's Esize was already determined and is a
13737 -- static value, then the new component simply inherits it. Otherwise
13738 -- the old component's size may require run-time determination, but
13739 -- the new component's size still might be statically determinable
13740 -- (if, for example it has a static constraint). In that case we want
13741 -- Layout_Type to recompute the component's size, so we reset its
13742 -- size and positional fields.
13743
13744 if Frontend_Layout_On_Target
13745 and then not Known_Static_Esize (Old_Compon)
13746 then
13747 Set_Esize (New_Compon, Uint_0);
13748 Init_Normalized_First_Bit (New_Compon);
13749 Init_Normalized_Position (New_Compon);
13750 Init_Normalized_Position_Max (New_Compon);
13751 end if;
13752
13753 -- We do not want this node marked as Comes_From_Source, since
13754 -- otherwise it would get first class status and a separate cross-
13755 -- reference line would be generated. Illegitimate children do not
13756 -- rate such recognition.
13757
13758 Set_Comes_From_Source (New_Compon, False);
13759
13760 -- But it is a real entity, and a birth certificate must be properly
13761 -- registered by entering it into the entity list.
13762
13763 Enter_Name (New_Compon);
13764
13765 return New_Compon;
13766 end Create_Component;
13767
13768 -----------------------
13769 -- Is_Variant_Record --
13770 -----------------------
13771
13772 function Is_Variant_Record (T : Entity_Id) return Boolean is
13773 begin
13774 return Nkind (Parent (T)) = N_Full_Type_Declaration
13775 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
13776 and then Present (Component_List (Type_Definition (Parent (T))))
13777 and then
13778 Present
13779 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
13780 end Is_Variant_Record;
13781
13782 -- Start of processing for Create_Constrained_Components
13783
13784 begin
13785 pragma Assert (Subt /= Base_Type (Subt));
13786 pragma Assert (Typ = Base_Type (Typ));
13787
13788 Set_First_Entity (Subt, Empty);
13789 Set_Last_Entity (Subt, Empty);
13790
13791 -- Check whether constraint is fully static, in which case we can
13792 -- optimize the list of components.
13793
13794 Discr_Val := First_Elmt (Constraints);
13795 while Present (Discr_Val) loop
13796 if not Is_OK_Static_Expression (Node (Discr_Val)) then
13797 Is_Static := False;
13798 exit;
13799 end if;
13800
13801 Next_Elmt (Discr_Val);
13802 end loop;
13803
13804 Set_Has_Static_Discriminants (Subt, Is_Static);
13805
13806 Push_Scope (Subt);
13807
13808 -- Inherit the discriminants of the parent type
13809
13810 Add_Discriminants : declare
13811 Num_Disc : Int;
13812 Num_Gird : Int;
13813
13814 begin
13815 Num_Disc := 0;
13816 Old_C := First_Discriminant (Typ);
13817
13818 while Present (Old_C) loop
13819 Num_Disc := Num_Disc + 1;
13820 New_C := Create_Component (Old_C);
13821 Set_Is_Public (New_C, Is_Public (Subt));
13822 Next_Discriminant (Old_C);
13823 end loop;
13824
13825 -- For an untagged derived subtype, the number of discriminants may
13826 -- be smaller than the number of inherited discriminants, because
13827 -- several of them may be renamed by a single new discriminant or
13828 -- constrained. In this case, add the hidden discriminants back into
13829 -- the subtype, because they need to be present if the optimizer of
13830 -- the GCC 4.x back-end decides to break apart assignments between
13831 -- objects using the parent view into member-wise assignments.
13832
13833 Num_Gird := 0;
13834
13835 if Is_Derived_Type (Typ)
13836 and then not Is_Tagged_Type (Typ)
13837 then
13838 Old_C := First_Stored_Discriminant (Typ);
13839
13840 while Present (Old_C) loop
13841 Num_Gird := Num_Gird + 1;
13842 Next_Stored_Discriminant (Old_C);
13843 end loop;
13844 end if;
13845
13846 if Num_Gird > Num_Disc then
13847
13848 -- Find out multiple uses of new discriminants, and add hidden
13849 -- components for the extra renamed discriminants. We recognize
13850 -- multiple uses through the Corresponding_Discriminant of a
13851 -- new discriminant: if it constrains several old discriminants,
13852 -- this field points to the last one in the parent type. The
13853 -- stored discriminants of the derived type have the same name
13854 -- as those of the parent.
13855
13856 declare
13857 Constr : Elmt_Id;
13858 New_Discr : Entity_Id;
13859 Old_Discr : Entity_Id;
13860
13861 begin
13862 Constr := First_Elmt (Stored_Constraint (Typ));
13863 Old_Discr := First_Stored_Discriminant (Typ);
13864 while Present (Constr) loop
13865 if Is_Entity_Name (Node (Constr))
13866 and then Ekind (Entity (Node (Constr))) = E_Discriminant
13867 then
13868 New_Discr := Entity (Node (Constr));
13869
13870 if Chars (Corresponding_Discriminant (New_Discr)) /=
13871 Chars (Old_Discr)
13872 then
13873 -- The new discriminant has been used to rename a
13874 -- subsequent old discriminant. Introduce a shadow
13875 -- component for the current old discriminant.
13876
13877 New_C := Create_Component (Old_Discr);
13878 Set_Original_Record_Component (New_C, Old_Discr);
13879 end if;
13880
13881 else
13882 -- The constraint has eliminated the old discriminant.
13883 -- Introduce a shadow component.
13884
13885 New_C := Create_Component (Old_Discr);
13886 Set_Original_Record_Component (New_C, Old_Discr);
13887 end if;
13888
13889 Next_Elmt (Constr);
13890 Next_Stored_Discriminant (Old_Discr);
13891 end loop;
13892 end;
13893 end if;
13894 end Add_Discriminants;
13895
13896 if Is_Static
13897 and then Is_Variant_Record (Typ)
13898 then
13899 Collect_Fixed_Components (Typ);
13900
13901 Gather_Components (
13902 Typ,
13903 Component_List (Type_Definition (Parent (Typ))),
13904 Governed_By => Assoc_List,
13905 Into => Comp_List,
13906 Report_Errors => Errors);
13907 pragma Assert (not Errors);
13908
13909 Create_All_Components;
13910
13911 -- If the subtype declaration is created for a tagged type derivation
13912 -- with constraints, we retrieve the record definition of the parent
13913 -- type to select the components of the proper variant.
13914
13915 elsif Is_Static
13916 and then Is_Tagged_Type (Typ)
13917 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
13918 and then
13919 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
13920 and then Is_Variant_Record (Parent_Type)
13921 then
13922 Collect_Fixed_Components (Typ);
13923
13924 Gather_Components
13925 (Typ,
13926 Component_List (Type_Definition (Parent (Parent_Type))),
13927 Governed_By => Assoc_List,
13928 Into => Comp_List,
13929 Report_Errors => Errors);
13930
13931 -- Note: previously there was a check at this point that no errors
13932 -- were detected. As a consequence of AI05-220 there may be an error
13933 -- if an inherited discriminant that controls a variant has a non-
13934 -- static constraint.
13935
13936 -- If the tagged derivation has a type extension, collect all the
13937 -- new components therein.
13938
13939 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
13940 then
13941 Old_C := First_Component (Typ);
13942 while Present (Old_C) loop
13943 if Original_Record_Component (Old_C) = Old_C
13944 and then Chars (Old_C) /= Name_uTag
13945 and then Chars (Old_C) /= Name_uParent
13946 then
13947 Append_Elmt (Old_C, Comp_List);
13948 end if;
13949
13950 Next_Component (Old_C);
13951 end loop;
13952 end if;
13953
13954 Create_All_Components;
13955
13956 else
13957 -- If discriminants are not static, or if this is a multi-level type
13958 -- extension, we have to include all components of the parent type.
13959
13960 Old_C := First_Component (Typ);
13961 while Present (Old_C) loop
13962 New_C := Create_Component (Old_C);
13963
13964 Set_Etype
13965 (New_C,
13966 Constrain_Component_Type
13967 (Old_C, Subt, Decl_Node, Typ, Constraints));
13968 Set_Is_Public (New_C, Is_Public (Subt));
13969
13970 Next_Component (Old_C);
13971 end loop;
13972 end if;
13973
13974 End_Scope;
13975 end Create_Constrained_Components;
13976
13977 ------------------------------------------
13978 -- Decimal_Fixed_Point_Type_Declaration --
13979 ------------------------------------------
13980
13981 procedure Decimal_Fixed_Point_Type_Declaration
13982 (T : Entity_Id;
13983 Def : Node_Id)
13984 is
13985 Loc : constant Source_Ptr := Sloc (Def);
13986 Digs_Expr : constant Node_Id := Digits_Expression (Def);
13987 Delta_Expr : constant Node_Id := Delta_Expression (Def);
13988 Implicit_Base : Entity_Id;
13989 Digs_Val : Uint;
13990 Delta_Val : Ureal;
13991 Scale_Val : Uint;
13992 Bound_Val : Ureal;
13993
13994 begin
13995 Check_SPARK_05_Restriction
13996 ("decimal fixed point type is not allowed", Def);
13997 Check_Restriction (No_Fixed_Point, Def);
13998
13999 -- Create implicit base type
14000
14001 Implicit_Base :=
14002 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14003 Set_Etype (Implicit_Base, Implicit_Base);
14004
14005 -- Analyze and process delta expression
14006
14007 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14008
14009 Check_Delta_Expression (Delta_Expr);
14010 Delta_Val := Expr_Value_R (Delta_Expr);
14011
14012 -- Check delta is power of 10, and determine scale value from it
14013
14014 declare
14015 Val : Ureal;
14016
14017 begin
14018 Scale_Val := Uint_0;
14019 Val := Delta_Val;
14020
14021 if Val < Ureal_1 then
14022 while Val < Ureal_1 loop
14023 Val := Val * Ureal_10;
14024 Scale_Val := Scale_Val + 1;
14025 end loop;
14026
14027 if Scale_Val > 18 then
14028 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14029 Scale_Val := UI_From_Int (+18);
14030 end if;
14031
14032 else
14033 while Val > Ureal_1 loop
14034 Val := Val / Ureal_10;
14035 Scale_Val := Scale_Val - 1;
14036 end loop;
14037
14038 if Scale_Val < -18 then
14039 Error_Msg_N ("scale is less than minimum value of -18", Def);
14040 Scale_Val := UI_From_Int (-18);
14041 end if;
14042 end if;
14043
14044 if Val /= Ureal_1 then
14045 Error_Msg_N ("delta expression must be a power of 10", Def);
14046 Delta_Val := Ureal_10 ** (-Scale_Val);
14047 end if;
14048 end;
14049
14050 -- Set delta, scale and small (small = delta for decimal type)
14051
14052 Set_Delta_Value (Implicit_Base, Delta_Val);
14053 Set_Scale_Value (Implicit_Base, Scale_Val);
14054 Set_Small_Value (Implicit_Base, Delta_Val);
14055
14056 -- Analyze and process digits expression
14057
14058 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14059 Check_Digits_Expression (Digs_Expr);
14060 Digs_Val := Expr_Value (Digs_Expr);
14061
14062 if Digs_Val > 18 then
14063 Digs_Val := UI_From_Int (+18);
14064 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14065 end if;
14066
14067 Set_Digits_Value (Implicit_Base, Digs_Val);
14068 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14069
14070 -- Set range of base type from digits value for now. This will be
14071 -- expanded to represent the true underlying base range by Freeze.
14072
14073 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14074
14075 -- Note: We leave size as zero for now, size will be set at freeze
14076 -- time. We have to do this for ordinary fixed-point, because the size
14077 -- depends on the specified small, and we might as well do the same for
14078 -- decimal fixed-point.
14079
14080 pragma Assert (Esize (Implicit_Base) = Uint_0);
14081
14082 -- If there are bounds given in the declaration use them as the
14083 -- bounds of the first named subtype.
14084
14085 if Present (Real_Range_Specification (Def)) then
14086 declare
14087 RRS : constant Node_Id := Real_Range_Specification (Def);
14088 Low : constant Node_Id := Low_Bound (RRS);
14089 High : constant Node_Id := High_Bound (RRS);
14090 Low_Val : Ureal;
14091 High_Val : Ureal;
14092
14093 begin
14094 Analyze_And_Resolve (Low, Any_Real);
14095 Analyze_And_Resolve (High, Any_Real);
14096 Check_Real_Bound (Low);
14097 Check_Real_Bound (High);
14098 Low_Val := Expr_Value_R (Low);
14099 High_Val := Expr_Value_R (High);
14100
14101 if Low_Val < (-Bound_Val) then
14102 Error_Msg_N
14103 ("range low bound too small for digits value", Low);
14104 Low_Val := -Bound_Val;
14105 end if;
14106
14107 if High_Val > Bound_Val then
14108 Error_Msg_N
14109 ("range high bound too large for digits value", High);
14110 High_Val := Bound_Val;
14111 end if;
14112
14113 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14114 end;
14115
14116 -- If no explicit range, use range that corresponds to given
14117 -- digits value. This will end up as the final range for the
14118 -- first subtype.
14119
14120 else
14121 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14122 end if;
14123
14124 -- Complete entity for first subtype. The inheritance of the rep item
14125 -- chain ensures that SPARK-related pragmas are not clobbered when the
14126 -- decimal fixed point type acts as a full view of a private type.
14127
14128 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14129 Set_Etype (T, Implicit_Base);
14130 Set_Size_Info (T, Implicit_Base);
14131 Inherit_Rep_Item_Chain (T, Implicit_Base);
14132 Set_Digits_Value (T, Digs_Val);
14133 Set_Delta_Value (T, Delta_Val);
14134 Set_Small_Value (T, Delta_Val);
14135 Set_Scale_Value (T, Scale_Val);
14136 Set_Is_Constrained (T);
14137 end Decimal_Fixed_Point_Type_Declaration;
14138
14139 -----------------------------------
14140 -- Derive_Progenitor_Subprograms --
14141 -----------------------------------
14142
14143 procedure Derive_Progenitor_Subprograms
14144 (Parent_Type : Entity_Id;
14145 Tagged_Type : Entity_Id)
14146 is
14147 E : Entity_Id;
14148 Elmt : Elmt_Id;
14149 Iface : Entity_Id;
14150 Iface_Elmt : Elmt_Id;
14151 Iface_Subp : Entity_Id;
14152 New_Subp : Entity_Id := Empty;
14153 Prim_Elmt : Elmt_Id;
14154 Subp : Entity_Id;
14155 Typ : Entity_Id;
14156
14157 begin
14158 pragma Assert (Ada_Version >= Ada_2005
14159 and then Is_Record_Type (Tagged_Type)
14160 and then Is_Tagged_Type (Tagged_Type)
14161 and then Has_Interfaces (Tagged_Type));
14162
14163 -- Step 1: Transfer to the full-view primitives associated with the
14164 -- partial-view that cover interface primitives. Conceptually this
14165 -- work should be done later by Process_Full_View; done here to
14166 -- simplify its implementation at later stages. It can be safely
14167 -- done here because interfaces must be visible in the partial and
14168 -- private view (RM 7.3(7.3/2)).
14169
14170 -- Small optimization: This work is only required if the parent may
14171 -- have entities whose Alias attribute reference an interface primitive.
14172 -- Such a situation may occur if the parent is an abstract type and the
14173 -- primitive has not been yet overridden or if the parent is a generic
14174 -- formal type covering interfaces.
14175
14176 -- If the tagged type is not abstract, it cannot have abstract
14177 -- primitives (the only entities in the list of primitives of
14178 -- non-abstract tagged types that can reference abstract primitives
14179 -- through its Alias attribute are the internal entities that have
14180 -- attribute Interface_Alias, and these entities are generated later
14181 -- by Add_Internal_Interface_Entities).
14182
14183 if In_Private_Part (Current_Scope)
14184 and then (Is_Abstract_Type (Parent_Type)
14185 or else
14186 Is_Generic_Type (Parent_Type))
14187 then
14188 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14189 while Present (Elmt) loop
14190 Subp := Node (Elmt);
14191
14192 -- At this stage it is not possible to have entities in the list
14193 -- of primitives that have attribute Interface_Alias.
14194
14195 pragma Assert (No (Interface_Alias (Subp)));
14196
14197 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14198
14199 if Is_Interface (Typ) then
14200 E := Find_Primitive_Covering_Interface
14201 (Tagged_Type => Tagged_Type,
14202 Iface_Prim => Subp);
14203
14204 if Present (E)
14205 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14206 then
14207 Replace_Elmt (Elmt, E);
14208 Remove_Homonym (Subp);
14209 end if;
14210 end if;
14211
14212 Next_Elmt (Elmt);
14213 end loop;
14214 end if;
14215
14216 -- Step 2: Add primitives of progenitors that are not implemented by
14217 -- parents of Tagged_Type.
14218
14219 if Present (Interfaces (Base_Type (Tagged_Type))) then
14220 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14221 while Present (Iface_Elmt) loop
14222 Iface := Node (Iface_Elmt);
14223
14224 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14225 while Present (Prim_Elmt) loop
14226 Iface_Subp := Node (Prim_Elmt);
14227
14228 -- Exclude derivation of predefined primitives except those
14229 -- that come from source, or are inherited from one that comes
14230 -- from source. Required to catch declarations of equality
14231 -- operators of interfaces. For example:
14232
14233 -- type Iface is interface;
14234 -- function "=" (Left, Right : Iface) return Boolean;
14235
14236 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14237 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14238 then
14239 E := Find_Primitive_Covering_Interface
14240 (Tagged_Type => Tagged_Type,
14241 Iface_Prim => Iface_Subp);
14242
14243 -- If not found we derive a new primitive leaving its alias
14244 -- attribute referencing the interface primitive.
14245
14246 if No (E) then
14247 Derive_Subprogram
14248 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14249
14250 -- Ada 2012 (AI05-0197): If the covering primitive's name
14251 -- differs from the name of the interface primitive then it
14252 -- is a private primitive inherited from a parent type. In
14253 -- such case, given that Tagged_Type covers the interface,
14254 -- the inherited private primitive becomes visible. For such
14255 -- purpose we add a new entity that renames the inherited
14256 -- private primitive.
14257
14258 elsif Chars (E) /= Chars (Iface_Subp) then
14259 pragma Assert (Has_Suffix (E, 'P'));
14260 Derive_Subprogram
14261 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14262 Set_Alias (New_Subp, E);
14263 Set_Is_Abstract_Subprogram (New_Subp,
14264 Is_Abstract_Subprogram (E));
14265
14266 -- Propagate to the full view interface entities associated
14267 -- with the partial view.
14268
14269 elsif In_Private_Part (Current_Scope)
14270 and then Present (Alias (E))
14271 and then Alias (E) = Iface_Subp
14272 and then
14273 List_Containing (Parent (E)) /=
14274 Private_Declarations
14275 (Specification
14276 (Unit_Declaration_Node (Current_Scope)))
14277 then
14278 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14279 end if;
14280 end if;
14281
14282 Next_Elmt (Prim_Elmt);
14283 end loop;
14284
14285 Next_Elmt (Iface_Elmt);
14286 end loop;
14287 end if;
14288 end Derive_Progenitor_Subprograms;
14289
14290 -----------------------
14291 -- Derive_Subprogram --
14292 -----------------------
14293
14294 procedure Derive_Subprogram
14295 (New_Subp : in out Entity_Id;
14296 Parent_Subp : Entity_Id;
14297 Derived_Type : Entity_Id;
14298 Parent_Type : Entity_Id;
14299 Actual_Subp : Entity_Id := Empty)
14300 is
14301 Formal : Entity_Id;
14302 -- Formal parameter of parent primitive operation
14303
14304 Formal_Of_Actual : Entity_Id;
14305 -- Formal parameter of actual operation, when the derivation is to
14306 -- create a renaming for a primitive operation of an actual in an
14307 -- instantiation.
14308
14309 New_Formal : Entity_Id;
14310 -- Formal of inherited operation
14311
14312 Visible_Subp : Entity_Id := Parent_Subp;
14313
14314 function Is_Private_Overriding return Boolean;
14315 -- If Subp is a private overriding of a visible operation, the inherited
14316 -- operation derives from the overridden op (even though its body is the
14317 -- overriding one) and the inherited operation is visible now. See
14318 -- sem_disp to see the full details of the handling of the overridden
14319 -- subprogram, which is removed from the list of primitive operations of
14320 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14321 -- and used to diagnose abstract operations that need overriding in the
14322 -- derived type.
14323
14324 procedure Replace_Type (Id, New_Id : Entity_Id);
14325 -- When the type is an anonymous access type, create a new access type
14326 -- designating the derived type.
14327
14328 procedure Set_Derived_Name;
14329 -- This procedure sets the appropriate Chars name for New_Subp. This
14330 -- is normally just a copy of the parent name. An exception arises for
14331 -- type support subprograms, where the name is changed to reflect the
14332 -- name of the derived type, e.g. if type foo is derived from type bar,
14333 -- then a procedure barDA is derived with a name fooDA.
14334
14335 ---------------------------
14336 -- Is_Private_Overriding --
14337 ---------------------------
14338
14339 function Is_Private_Overriding return Boolean is
14340 Prev : Entity_Id;
14341
14342 begin
14343 -- If the parent is not a dispatching operation there is no
14344 -- need to investigate overridings
14345
14346 if not Is_Dispatching_Operation (Parent_Subp) then
14347 return False;
14348 end if;
14349
14350 -- The visible operation that is overridden is a homonym of the
14351 -- parent subprogram. We scan the homonym chain to find the one
14352 -- whose alias is the subprogram we are deriving.
14353
14354 Prev := Current_Entity (Parent_Subp);
14355 while Present (Prev) loop
14356 if Ekind (Prev) = Ekind (Parent_Subp)
14357 and then Alias (Prev) = Parent_Subp
14358 and then Scope (Parent_Subp) = Scope (Prev)
14359 and then not Is_Hidden (Prev)
14360 then
14361 Visible_Subp := Prev;
14362 return True;
14363 end if;
14364
14365 Prev := Homonym (Prev);
14366 end loop;
14367
14368 return False;
14369 end Is_Private_Overriding;
14370
14371 ------------------
14372 -- Replace_Type --
14373 ------------------
14374
14375 procedure Replace_Type (Id, New_Id : Entity_Id) is
14376 Id_Type : constant Entity_Id := Etype (Id);
14377 Acc_Type : Entity_Id;
14378 Par : constant Node_Id := Parent (Derived_Type);
14379
14380 begin
14381 -- When the type is an anonymous access type, create a new access
14382 -- type designating the derived type. This itype must be elaborated
14383 -- at the point of the derivation, not on subsequent calls that may
14384 -- be out of the proper scope for Gigi, so we insert a reference to
14385 -- it after the derivation.
14386
14387 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14388 declare
14389 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14390
14391 begin
14392 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14393 and then Present (Full_View (Desig_Typ))
14394 and then not Is_Private_Type (Parent_Type)
14395 then
14396 Desig_Typ := Full_View (Desig_Typ);
14397 end if;
14398
14399 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14400
14401 -- Ada 2005 (AI-251): Handle also derivations of abstract
14402 -- interface primitives.
14403
14404 or else (Is_Interface (Desig_Typ)
14405 and then not Is_Class_Wide_Type (Desig_Typ))
14406 then
14407 Acc_Type := New_Copy (Id_Type);
14408 Set_Etype (Acc_Type, Acc_Type);
14409 Set_Scope (Acc_Type, New_Subp);
14410
14411 -- Set size of anonymous access type. If we have an access
14412 -- to an unconstrained array, this is a fat pointer, so it
14413 -- is sizes at twice addtress size.
14414
14415 if Is_Array_Type (Desig_Typ)
14416 and then not Is_Constrained (Desig_Typ)
14417 then
14418 Init_Size (Acc_Type, 2 * System_Address_Size);
14419
14420 -- Other cases use a thin pointer
14421
14422 else
14423 Init_Size (Acc_Type, System_Address_Size);
14424 end if;
14425
14426 -- Set remaining characterstics of anonymous access type
14427
14428 Init_Alignment (Acc_Type);
14429 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14430
14431 Set_Etype (New_Id, Acc_Type);
14432 Set_Scope (New_Id, New_Subp);
14433
14434 -- Create a reference to it
14435
14436 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14437
14438 else
14439 Set_Etype (New_Id, Id_Type);
14440 end if;
14441 end;
14442
14443 -- In Ada2012, a formal may have an incomplete type but the type
14444 -- derivation that inherits the primitive follows the full view.
14445
14446 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14447 or else
14448 (Ekind (Id_Type) = E_Record_Type_With_Private
14449 and then Present (Full_View (Id_Type))
14450 and then
14451 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14452 or else
14453 (Ada_Version >= Ada_2012
14454 and then Ekind (Id_Type) = E_Incomplete_Type
14455 and then Full_View (Id_Type) = Parent_Type)
14456 then
14457 -- Constraint checks on formals are generated during expansion,
14458 -- based on the signature of the original subprogram. The bounds
14459 -- of the derived type are not relevant, and thus we can use
14460 -- the base type for the formals. However, the return type may be
14461 -- used in a context that requires that the proper static bounds
14462 -- be used (a case statement, for example) and for those cases
14463 -- we must use the derived type (first subtype), not its base.
14464
14465 -- If the derived_type_definition has no constraints, we know that
14466 -- the derived type has the same constraints as the first subtype
14467 -- of the parent, and we can also use it rather than its base,
14468 -- which can lead to more efficient code.
14469
14470 if Etype (Id) = Parent_Type then
14471 if Is_Scalar_Type (Parent_Type)
14472 and then
14473 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14474 then
14475 Set_Etype (New_Id, Derived_Type);
14476
14477 elsif Nkind (Par) = N_Full_Type_Declaration
14478 and then
14479 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14480 and then
14481 Is_Entity_Name
14482 (Subtype_Indication (Type_Definition (Par)))
14483 then
14484 Set_Etype (New_Id, Derived_Type);
14485
14486 else
14487 Set_Etype (New_Id, Base_Type (Derived_Type));
14488 end if;
14489
14490 else
14491 Set_Etype (New_Id, Base_Type (Derived_Type));
14492 end if;
14493
14494 else
14495 Set_Etype (New_Id, Etype (Id));
14496 end if;
14497 end Replace_Type;
14498
14499 ----------------------
14500 -- Set_Derived_Name --
14501 ----------------------
14502
14503 procedure Set_Derived_Name is
14504 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14505 begin
14506 if Nm = TSS_Null then
14507 Set_Chars (New_Subp, Chars (Parent_Subp));
14508 else
14509 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14510 end if;
14511 end Set_Derived_Name;
14512
14513 -- Start of processing for Derive_Subprogram
14514
14515 begin
14516 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14517 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14518
14519 -- Check whether the inherited subprogram is a private operation that
14520 -- should be inherited but not yet made visible. Such subprograms can
14521 -- become visible at a later point (e.g., the private part of a public
14522 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14523 -- following predicate is true, then this is not such a private
14524 -- operation and the subprogram simply inherits the name of the parent
14525 -- subprogram. Note the special check for the names of controlled
14526 -- operations, which are currently exempted from being inherited with
14527 -- a hidden name because they must be findable for generation of
14528 -- implicit run-time calls.
14529
14530 if not Is_Hidden (Parent_Subp)
14531 or else Is_Internal (Parent_Subp)
14532 or else Is_Private_Overriding
14533 or else Is_Internal_Name (Chars (Parent_Subp))
14534 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
14535 Name_Adjust,
14536 Name_Finalize)
14537 then
14538 Set_Derived_Name;
14539
14540 -- An inherited dispatching equality will be overridden by an internally
14541 -- generated one, or by an explicit one, so preserve its name and thus
14542 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14543 -- private operation it may become invisible if the full view has
14544 -- progenitors, and the dispatch table will be malformed.
14545 -- We check that the type is limited to handle the anomalous declaration
14546 -- of Limited_Controlled, which is derived from a non-limited type, and
14547 -- which is handled specially elsewhere as well.
14548
14549 elsif Chars (Parent_Subp) = Name_Op_Eq
14550 and then Is_Dispatching_Operation (Parent_Subp)
14551 and then Etype (Parent_Subp) = Standard_Boolean
14552 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14553 and then
14554 Etype (First_Formal (Parent_Subp)) =
14555 Etype (Next_Formal (First_Formal (Parent_Subp)))
14556 then
14557 Set_Derived_Name;
14558
14559 -- If parent is hidden, this can be a regular derivation if the
14560 -- parent is immediately visible in a non-instantiating context,
14561 -- or if we are in the private part of an instance. This test
14562 -- should still be refined ???
14563
14564 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14565 -- operation as a non-visible operation in cases where the parent
14566 -- subprogram might not be visible now, but was visible within the
14567 -- original generic, so it would be wrong to make the inherited
14568 -- subprogram non-visible now. (Not clear if this test is fully
14569 -- correct; are there any cases where we should declare the inherited
14570 -- operation as not visible to avoid it being overridden, e.g., when
14571 -- the parent type is a generic actual with private primitives ???)
14572
14573 -- (they should be treated the same as other private inherited
14574 -- subprograms, but it's not clear how to do this cleanly). ???
14575
14576 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14577 and then Is_Immediately_Visible (Parent_Subp)
14578 and then not In_Instance)
14579 or else In_Instance_Not_Visible
14580 then
14581 Set_Derived_Name;
14582
14583 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14584 -- overrides an interface primitive because interface primitives
14585 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14586
14587 elsif Ada_Version >= Ada_2005
14588 and then Is_Dispatching_Operation (Parent_Subp)
14589 and then Covers_Some_Interface (Parent_Subp)
14590 then
14591 Set_Derived_Name;
14592
14593 -- Otherwise, the type is inheriting a private operation, so enter
14594 -- it with a special name so it can't be overridden.
14595
14596 else
14597 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14598 end if;
14599
14600 Set_Parent (New_Subp, Parent (Derived_Type));
14601
14602 if Present (Actual_Subp) then
14603 Replace_Type (Actual_Subp, New_Subp);
14604 else
14605 Replace_Type (Parent_Subp, New_Subp);
14606 end if;
14607
14608 Conditional_Delay (New_Subp, Parent_Subp);
14609
14610 -- If we are creating a renaming for a primitive operation of an
14611 -- actual of a generic derived type, we must examine the signature
14612 -- of the actual primitive, not that of the generic formal, which for
14613 -- example may be an interface. However the name and initial value
14614 -- of the inherited operation are those of the formal primitive.
14615
14616 Formal := First_Formal (Parent_Subp);
14617
14618 if Present (Actual_Subp) then
14619 Formal_Of_Actual := First_Formal (Actual_Subp);
14620 else
14621 Formal_Of_Actual := Empty;
14622 end if;
14623
14624 while Present (Formal) loop
14625 New_Formal := New_Copy (Formal);
14626
14627 -- Normally we do not go copying parents, but in the case of
14628 -- formals, we need to link up to the declaration (which is the
14629 -- parameter specification), and it is fine to link up to the
14630 -- original formal's parameter specification in this case.
14631
14632 Set_Parent (New_Formal, Parent (Formal));
14633 Append_Entity (New_Formal, New_Subp);
14634
14635 if Present (Formal_Of_Actual) then
14636 Replace_Type (Formal_Of_Actual, New_Formal);
14637 Next_Formal (Formal_Of_Actual);
14638 else
14639 Replace_Type (Formal, New_Formal);
14640 end if;
14641
14642 Next_Formal (Formal);
14643 end loop;
14644
14645 -- If this derivation corresponds to a tagged generic actual, then
14646 -- primitive operations rename those of the actual. Otherwise the
14647 -- primitive operations rename those of the parent type, If the parent
14648 -- renames an intrinsic operator, so does the new subprogram. We except
14649 -- concatenation, which is always properly typed, and does not get
14650 -- expanded as other intrinsic operations.
14651
14652 if No (Actual_Subp) then
14653 if Is_Intrinsic_Subprogram (Parent_Subp) then
14654 Set_Is_Intrinsic_Subprogram (New_Subp);
14655
14656 if Present (Alias (Parent_Subp))
14657 and then Chars (Parent_Subp) /= Name_Op_Concat
14658 then
14659 Set_Alias (New_Subp, Alias (Parent_Subp));
14660 else
14661 Set_Alias (New_Subp, Parent_Subp);
14662 end if;
14663
14664 else
14665 Set_Alias (New_Subp, Parent_Subp);
14666 end if;
14667
14668 else
14669 Set_Alias (New_Subp, Actual_Subp);
14670 end if;
14671
14672 -- Inherit the "ghostness" from the parent subprogram
14673
14674 if Is_Ghost_Entity (Alias (New_Subp)) then
14675 Set_Is_Ghost_Entity (New_Subp);
14676 end if;
14677
14678 -- Derived subprograms of a tagged type must inherit the convention
14679 -- of the parent subprogram (a requirement of AI-117). Derived
14680 -- subprograms of untagged types simply get convention Ada by default.
14681
14682 -- If the derived type is a tagged generic formal type with unknown
14683 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14684
14685 -- However, if the type is derived from a generic formal, the further
14686 -- inherited subprogram has the convention of the non-generic ancestor.
14687 -- Otherwise there would be no way to override the operation.
14688 -- (This is subject to forthcoming ARG discussions).
14689
14690 if Is_Tagged_Type (Derived_Type) then
14691 if Is_Generic_Type (Derived_Type)
14692 and then Has_Unknown_Discriminants (Derived_Type)
14693 then
14694 Set_Convention (New_Subp, Convention_Intrinsic);
14695
14696 else
14697 if Is_Generic_Type (Parent_Type)
14698 and then Has_Unknown_Discriminants (Parent_Type)
14699 then
14700 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14701 else
14702 Set_Convention (New_Subp, Convention (Parent_Subp));
14703 end if;
14704 end if;
14705 end if;
14706
14707 -- Predefined controlled operations retain their name even if the parent
14708 -- is hidden (see above), but they are not primitive operations if the
14709 -- ancestor is not visible, for example if the parent is a private
14710 -- extension completed with a controlled extension. Note that a full
14711 -- type that is controlled can break privacy: the flag Is_Controlled is
14712 -- set on both views of the type.
14713
14714 if Is_Controlled (Parent_Type)
14715 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14716 Name_Adjust,
14717 Name_Finalize)
14718 and then Is_Hidden (Parent_Subp)
14719 and then not Is_Visibly_Controlled (Parent_Type)
14720 then
14721 Set_Is_Hidden (New_Subp);
14722 end if;
14723
14724 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14725 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14726
14727 if Ekind (Parent_Subp) = E_Procedure then
14728 Set_Is_Valued_Procedure
14729 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14730 else
14731 Set_Has_Controlling_Result
14732 (New_Subp, Has_Controlling_Result (Parent_Subp));
14733 end if;
14734
14735 -- No_Return must be inherited properly. If this is overridden in the
14736 -- case of a dispatching operation, then a check is made in Sem_Disp
14737 -- that the overriding operation is also No_Return (no such check is
14738 -- required for the case of non-dispatching operation.
14739
14740 Set_No_Return (New_Subp, No_Return (Parent_Subp));
14741
14742 -- A derived function with a controlling result is abstract. If the
14743 -- Derived_Type is a nonabstract formal generic derived type, then
14744 -- inherited operations are not abstract: the required check is done at
14745 -- instantiation time. If the derivation is for a generic actual, the
14746 -- function is not abstract unless the actual is.
14747
14748 if Is_Generic_Type (Derived_Type)
14749 and then not Is_Abstract_Type (Derived_Type)
14750 then
14751 null;
14752
14753 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
14754 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
14755
14756 -- A subprogram subject to pragma Extensions_Visible with value False
14757 -- requires overriding if the subprogram has at least one controlling
14758 -- OUT parameter (SPARK RM 6.1.7(6)).
14759
14760 elsif Ada_Version >= Ada_2005
14761 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14762 or else (Is_Tagged_Type (Derived_Type)
14763 and then Etype (New_Subp) = Derived_Type
14764 and then not Is_Null_Extension (Derived_Type))
14765 or else (Is_Tagged_Type (Derived_Type)
14766 and then Ekind (Etype (New_Subp)) =
14767 E_Anonymous_Access_Type
14768 and then Designated_Type (Etype (New_Subp)) =
14769 Derived_Type
14770 and then not Is_Null_Extension (Derived_Type))
14771 or else (Comes_From_Source (Alias (New_Subp))
14772 and then Is_EVF_Procedure (Alias (New_Subp))))
14773 and then No (Actual_Subp)
14774 then
14775 if not Is_Tagged_Type (Derived_Type)
14776 or else Is_Abstract_Type (Derived_Type)
14777 or else Is_Abstract_Subprogram (Alias (New_Subp))
14778 then
14779 Set_Is_Abstract_Subprogram (New_Subp);
14780 else
14781 Set_Requires_Overriding (New_Subp);
14782 end if;
14783
14784 elsif Ada_Version < Ada_2005
14785 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14786 or else (Is_Tagged_Type (Derived_Type)
14787 and then Etype (New_Subp) = Derived_Type
14788 and then No (Actual_Subp)))
14789 then
14790 Set_Is_Abstract_Subprogram (New_Subp);
14791
14792 -- AI05-0097 : an inherited operation that dispatches on result is
14793 -- abstract if the derived type is abstract, even if the parent type
14794 -- is concrete and the derived type is a null extension.
14795
14796 elsif Has_Controlling_Result (Alias (New_Subp))
14797 and then Is_Abstract_Type (Etype (New_Subp))
14798 then
14799 Set_Is_Abstract_Subprogram (New_Subp);
14800
14801 -- Finally, if the parent type is abstract we must verify that all
14802 -- inherited operations are either non-abstract or overridden, or that
14803 -- the derived type itself is abstract (this check is performed at the
14804 -- end of a package declaration, in Check_Abstract_Overriding). A
14805 -- private overriding in the parent type will not be visible in the
14806 -- derivation if we are not in an inner package or in a child unit of
14807 -- the parent type, in which case the abstractness of the inherited
14808 -- operation is carried to the new subprogram.
14809
14810 elsif Is_Abstract_Type (Parent_Type)
14811 and then not In_Open_Scopes (Scope (Parent_Type))
14812 and then Is_Private_Overriding
14813 and then Is_Abstract_Subprogram (Visible_Subp)
14814 then
14815 if No (Actual_Subp) then
14816 Set_Alias (New_Subp, Visible_Subp);
14817 Set_Is_Abstract_Subprogram (New_Subp, True);
14818
14819 else
14820 -- If this is a derivation for an instance of a formal derived
14821 -- type, abstractness comes from the primitive operation of the
14822 -- actual, not from the operation inherited from the ancestor.
14823
14824 Set_Is_Abstract_Subprogram
14825 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
14826 end if;
14827 end if;
14828
14829 New_Overloaded_Entity (New_Subp, Derived_Type);
14830
14831 -- Check for case of a derived subprogram for the instantiation of a
14832 -- formal derived tagged type, if so mark the subprogram as dispatching
14833 -- and inherit the dispatching attributes of the actual subprogram. The
14834 -- derived subprogram is effectively renaming of the actual subprogram,
14835 -- so it needs to have the same attributes as the actual.
14836
14837 if Present (Actual_Subp)
14838 and then Is_Dispatching_Operation (Actual_Subp)
14839 then
14840 Set_Is_Dispatching_Operation (New_Subp);
14841
14842 if Present (DTC_Entity (Actual_Subp)) then
14843 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
14844 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
14845 end if;
14846 end if;
14847
14848 -- Indicate that a derived subprogram does not require a body and that
14849 -- it does not require processing of default expressions.
14850
14851 Set_Has_Completion (New_Subp);
14852 Set_Default_Expressions_Processed (New_Subp);
14853
14854 if Ekind (New_Subp) = E_Function then
14855 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
14856 end if;
14857 end Derive_Subprogram;
14858
14859 ------------------------
14860 -- Derive_Subprograms --
14861 ------------------------
14862
14863 procedure Derive_Subprograms
14864 (Parent_Type : Entity_Id;
14865 Derived_Type : Entity_Id;
14866 Generic_Actual : Entity_Id := Empty)
14867 is
14868 Op_List : constant Elist_Id :=
14869 Collect_Primitive_Operations (Parent_Type);
14870
14871 function Check_Derived_Type return Boolean;
14872 -- Check that all the entities derived from Parent_Type are found in
14873 -- the list of primitives of Derived_Type exactly in the same order.
14874
14875 procedure Derive_Interface_Subprogram
14876 (New_Subp : in out Entity_Id;
14877 Subp : Entity_Id;
14878 Actual_Subp : Entity_Id);
14879 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
14880 -- (which is an interface primitive). If Generic_Actual is present then
14881 -- Actual_Subp is the actual subprogram corresponding with the generic
14882 -- subprogram Subp.
14883
14884 function Check_Derived_Type return Boolean is
14885 E : Entity_Id;
14886 Elmt : Elmt_Id;
14887 List : Elist_Id;
14888 New_Subp : Entity_Id;
14889 Op_Elmt : Elmt_Id;
14890 Subp : Entity_Id;
14891
14892 begin
14893 -- Traverse list of entities in the current scope searching for
14894 -- an incomplete type whose full-view is derived type
14895
14896 E := First_Entity (Scope (Derived_Type));
14897 while Present (E) and then E /= Derived_Type loop
14898 if Ekind (E) = E_Incomplete_Type
14899 and then Present (Full_View (E))
14900 and then Full_View (E) = Derived_Type
14901 then
14902 -- Disable this test if Derived_Type completes an incomplete
14903 -- type because in such case more primitives can be added
14904 -- later to the list of primitives of Derived_Type by routine
14905 -- Process_Incomplete_Dependents
14906
14907 return True;
14908 end if;
14909
14910 E := Next_Entity (E);
14911 end loop;
14912
14913 List := Collect_Primitive_Operations (Derived_Type);
14914 Elmt := First_Elmt (List);
14915
14916 Op_Elmt := First_Elmt (Op_List);
14917 while Present (Op_Elmt) loop
14918 Subp := Node (Op_Elmt);
14919 New_Subp := Node (Elmt);
14920
14921 -- At this early stage Derived_Type has no entities with attribute
14922 -- Interface_Alias. In addition, such primitives are always
14923 -- located at the end of the list of primitives of Parent_Type.
14924 -- Therefore, if found we can safely stop processing pending
14925 -- entities.
14926
14927 exit when Present (Interface_Alias (Subp));
14928
14929 -- Handle hidden entities
14930
14931 if not Is_Predefined_Dispatching_Operation (Subp)
14932 and then Is_Hidden (Subp)
14933 then
14934 if Present (New_Subp)
14935 and then Primitive_Names_Match (Subp, New_Subp)
14936 then
14937 Next_Elmt (Elmt);
14938 end if;
14939
14940 else
14941 if not Present (New_Subp)
14942 or else Ekind (Subp) /= Ekind (New_Subp)
14943 or else not Primitive_Names_Match (Subp, New_Subp)
14944 then
14945 return False;
14946 end if;
14947
14948 Next_Elmt (Elmt);
14949 end if;
14950
14951 Next_Elmt (Op_Elmt);
14952 end loop;
14953
14954 return True;
14955 end Check_Derived_Type;
14956
14957 ---------------------------------
14958 -- Derive_Interface_Subprogram --
14959 ---------------------------------
14960
14961 procedure Derive_Interface_Subprogram
14962 (New_Subp : in out Entity_Id;
14963 Subp : Entity_Id;
14964 Actual_Subp : Entity_Id)
14965 is
14966 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
14967 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
14968
14969 begin
14970 pragma Assert (Is_Interface (Iface_Type));
14971
14972 Derive_Subprogram
14973 (New_Subp => New_Subp,
14974 Parent_Subp => Iface_Subp,
14975 Derived_Type => Derived_Type,
14976 Parent_Type => Iface_Type,
14977 Actual_Subp => Actual_Subp);
14978
14979 -- Given that this new interface entity corresponds with a primitive
14980 -- of the parent that was not overridden we must leave it associated
14981 -- with its parent primitive to ensure that it will share the same
14982 -- dispatch table slot when overridden.
14983
14984 if No (Actual_Subp) then
14985 Set_Alias (New_Subp, Subp);
14986
14987 -- For instantiations this is not needed since the previous call to
14988 -- Derive_Subprogram leaves the entity well decorated.
14989
14990 else
14991 pragma Assert (Alias (New_Subp) = Actual_Subp);
14992 null;
14993 end if;
14994 end Derive_Interface_Subprogram;
14995
14996 -- Local variables
14997
14998 Alias_Subp : Entity_Id;
14999 Act_List : Elist_Id;
15000 Act_Elmt : Elmt_Id;
15001 Act_Subp : Entity_Id := Empty;
15002 Elmt : Elmt_Id;
15003 Need_Search : Boolean := False;
15004 New_Subp : Entity_Id := Empty;
15005 Parent_Base : Entity_Id;
15006 Subp : Entity_Id;
15007
15008 -- Start of processing for Derive_Subprograms
15009
15010 begin
15011 if Ekind (Parent_Type) = E_Record_Type_With_Private
15012 and then Has_Discriminants (Parent_Type)
15013 and then Present (Full_View (Parent_Type))
15014 then
15015 Parent_Base := Full_View (Parent_Type);
15016 else
15017 Parent_Base := Parent_Type;
15018 end if;
15019
15020 if Present (Generic_Actual) then
15021 Act_List := Collect_Primitive_Operations (Generic_Actual);
15022 Act_Elmt := First_Elmt (Act_List);
15023 else
15024 Act_List := No_Elist;
15025 Act_Elmt := No_Elmt;
15026 end if;
15027
15028 -- Derive primitives inherited from the parent. Note that if the generic
15029 -- actual is present, this is not really a type derivation, it is a
15030 -- completion within an instance.
15031
15032 -- Case 1: Derived_Type does not implement interfaces
15033
15034 if not Is_Tagged_Type (Derived_Type)
15035 or else (not Has_Interfaces (Derived_Type)
15036 and then not (Present (Generic_Actual)
15037 and then Has_Interfaces (Generic_Actual)))
15038 then
15039 Elmt := First_Elmt (Op_List);
15040 while Present (Elmt) loop
15041 Subp := Node (Elmt);
15042
15043 -- Literals are derived earlier in the process of building the
15044 -- derived type, and are skipped here.
15045
15046 if Ekind (Subp) = E_Enumeration_Literal then
15047 null;
15048
15049 -- The actual is a direct descendant and the common primitive
15050 -- operations appear in the same order.
15051
15052 -- If the generic parent type is present, the derived type is an
15053 -- instance of a formal derived type, and within the instance its
15054 -- operations are those of the actual. We derive from the formal
15055 -- type but make the inherited operations aliases of the
15056 -- corresponding operations of the actual.
15057
15058 else
15059 pragma Assert (No (Node (Act_Elmt))
15060 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15061 and then
15062 Type_Conformant
15063 (Subp, Node (Act_Elmt),
15064 Skip_Controlling_Formals => True)));
15065
15066 Derive_Subprogram
15067 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15068
15069 if Present (Act_Elmt) then
15070 Next_Elmt (Act_Elmt);
15071 end if;
15072 end if;
15073
15074 Next_Elmt (Elmt);
15075 end loop;
15076
15077 -- Case 2: Derived_Type implements interfaces
15078
15079 else
15080 -- If the parent type has no predefined primitives we remove
15081 -- predefined primitives from the list of primitives of generic
15082 -- actual to simplify the complexity of this algorithm.
15083
15084 if Present (Generic_Actual) then
15085 declare
15086 Has_Predefined_Primitives : Boolean := False;
15087
15088 begin
15089 -- Check if the parent type has predefined primitives
15090
15091 Elmt := First_Elmt (Op_List);
15092 while Present (Elmt) loop
15093 Subp := Node (Elmt);
15094
15095 if Is_Predefined_Dispatching_Operation (Subp)
15096 and then not Comes_From_Source (Ultimate_Alias (Subp))
15097 then
15098 Has_Predefined_Primitives := True;
15099 exit;
15100 end if;
15101
15102 Next_Elmt (Elmt);
15103 end loop;
15104
15105 -- Remove predefined primitives of Generic_Actual. We must use
15106 -- an auxiliary list because in case of tagged types the value
15107 -- returned by Collect_Primitive_Operations is the value stored
15108 -- in its Primitive_Operations attribute (and we don't want to
15109 -- modify its current contents).
15110
15111 if not Has_Predefined_Primitives then
15112 declare
15113 Aux_List : constant Elist_Id := New_Elmt_List;
15114
15115 begin
15116 Elmt := First_Elmt (Act_List);
15117 while Present (Elmt) loop
15118 Subp := Node (Elmt);
15119
15120 if not Is_Predefined_Dispatching_Operation (Subp)
15121 or else Comes_From_Source (Subp)
15122 then
15123 Append_Elmt (Subp, Aux_List);
15124 end if;
15125
15126 Next_Elmt (Elmt);
15127 end loop;
15128
15129 Act_List := Aux_List;
15130 end;
15131 end if;
15132
15133 Act_Elmt := First_Elmt (Act_List);
15134 Act_Subp := Node (Act_Elmt);
15135 end;
15136 end if;
15137
15138 -- Stage 1: If the generic actual is not present we derive the
15139 -- primitives inherited from the parent type. If the generic parent
15140 -- type is present, the derived type is an instance of a formal
15141 -- derived type, and within the instance its operations are those of
15142 -- the actual. We derive from the formal type but make the inherited
15143 -- operations aliases of the corresponding operations of the actual.
15144
15145 Elmt := First_Elmt (Op_List);
15146 while Present (Elmt) loop
15147 Subp := Node (Elmt);
15148 Alias_Subp := Ultimate_Alias (Subp);
15149
15150 -- Do not derive internal entities of the parent that link
15151 -- interface primitives with their covering primitive. These
15152 -- entities will be added to this type when frozen.
15153
15154 if Present (Interface_Alias (Subp)) then
15155 goto Continue;
15156 end if;
15157
15158 -- If the generic actual is present find the corresponding
15159 -- operation in the generic actual. If the parent type is a
15160 -- direct ancestor of the derived type then, even if it is an
15161 -- interface, the operations are inherited from the primary
15162 -- dispatch table and are in the proper order. If we detect here
15163 -- that primitives are not in the same order we traverse the list
15164 -- of primitive operations of the actual to find the one that
15165 -- implements the interface primitive.
15166
15167 if Need_Search
15168 or else
15169 (Present (Generic_Actual)
15170 and then Present (Act_Subp)
15171 and then not
15172 (Primitive_Names_Match (Subp, Act_Subp)
15173 and then
15174 Type_Conformant (Subp, Act_Subp,
15175 Skip_Controlling_Formals => True)))
15176 then
15177 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15178 Use_Full_View => True));
15179
15180 -- Remember that we need searching for all pending primitives
15181
15182 Need_Search := True;
15183
15184 -- Handle entities associated with interface primitives
15185
15186 if Present (Alias_Subp)
15187 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15188 and then not Is_Predefined_Dispatching_Operation (Subp)
15189 then
15190 -- Search for the primitive in the homonym chain
15191
15192 Act_Subp :=
15193 Find_Primitive_Covering_Interface
15194 (Tagged_Type => Generic_Actual,
15195 Iface_Prim => Alias_Subp);
15196
15197 -- Previous search may not locate primitives covering
15198 -- interfaces defined in generics units or instantiations.
15199 -- (it fails if the covering primitive has formals whose
15200 -- type is also defined in generics or instantiations).
15201 -- In such case we search in the list of primitives of the
15202 -- generic actual for the internal entity that links the
15203 -- interface primitive and the covering primitive.
15204
15205 if No (Act_Subp)
15206 and then Is_Generic_Type (Parent_Type)
15207 then
15208 -- This code has been designed to handle only generic
15209 -- formals that implement interfaces that are defined
15210 -- in a generic unit or instantiation. If this code is
15211 -- needed for other cases we must review it because
15212 -- (given that it relies on Original_Location to locate
15213 -- the primitive of Generic_Actual that covers the
15214 -- interface) it could leave linked through attribute
15215 -- Alias entities of unrelated instantiations).
15216
15217 pragma Assert
15218 (Is_Generic_Unit
15219 (Scope (Find_Dispatching_Type (Alias_Subp)))
15220 or else
15221 Instantiation_Depth
15222 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15223
15224 declare
15225 Iface_Prim_Loc : constant Source_Ptr :=
15226 Original_Location (Sloc (Alias_Subp));
15227
15228 Elmt : Elmt_Id;
15229 Prim : Entity_Id;
15230
15231 begin
15232 Elmt :=
15233 First_Elmt (Primitive_Operations (Generic_Actual));
15234
15235 Search : while Present (Elmt) loop
15236 Prim := Node (Elmt);
15237
15238 if Present (Interface_Alias (Prim))
15239 and then Original_Location
15240 (Sloc (Interface_Alias (Prim))) =
15241 Iface_Prim_Loc
15242 then
15243 Act_Subp := Alias (Prim);
15244 exit Search;
15245 end if;
15246
15247 Next_Elmt (Elmt);
15248 end loop Search;
15249 end;
15250 end if;
15251
15252 pragma Assert (Present (Act_Subp)
15253 or else Is_Abstract_Type (Generic_Actual)
15254 or else Serious_Errors_Detected > 0);
15255
15256 -- Handle predefined primitives plus the rest of user-defined
15257 -- primitives
15258
15259 else
15260 Act_Elmt := First_Elmt (Act_List);
15261 while Present (Act_Elmt) loop
15262 Act_Subp := Node (Act_Elmt);
15263
15264 exit when Primitive_Names_Match (Subp, Act_Subp)
15265 and then Type_Conformant
15266 (Subp, Act_Subp,
15267 Skip_Controlling_Formals => True)
15268 and then No (Interface_Alias (Act_Subp));
15269
15270 Next_Elmt (Act_Elmt);
15271 end loop;
15272
15273 if No (Act_Elmt) then
15274 Act_Subp := Empty;
15275 end if;
15276 end if;
15277 end if;
15278
15279 -- Case 1: If the parent is a limited interface then it has the
15280 -- predefined primitives of synchronized interfaces. However, the
15281 -- actual type may be a non-limited type and hence it does not
15282 -- have such primitives.
15283
15284 if Present (Generic_Actual)
15285 and then not Present (Act_Subp)
15286 and then Is_Limited_Interface (Parent_Base)
15287 and then Is_Predefined_Interface_Primitive (Subp)
15288 then
15289 null;
15290
15291 -- Case 2: Inherit entities associated with interfaces that were
15292 -- not covered by the parent type. We exclude here null interface
15293 -- primitives because they do not need special management.
15294
15295 -- We also exclude interface operations that are renamings. If the
15296 -- subprogram is an explicit renaming of an interface primitive,
15297 -- it is a regular primitive operation, and the presence of its
15298 -- alias is not relevant: it has to be derived like any other
15299 -- primitive.
15300
15301 elsif Present (Alias (Subp))
15302 and then Nkind (Unit_Declaration_Node (Subp)) /=
15303 N_Subprogram_Renaming_Declaration
15304 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15305 and then not
15306 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15307 and then Null_Present (Parent (Alias_Subp)))
15308 then
15309 -- If this is an abstract private type then we transfer the
15310 -- derivation of the interface primitive from the partial view
15311 -- to the full view. This is safe because all the interfaces
15312 -- must be visible in the partial view. Done to avoid adding
15313 -- a new interface derivation to the private part of the
15314 -- enclosing package; otherwise this new derivation would be
15315 -- decorated as hidden when the analysis of the enclosing
15316 -- package completes.
15317
15318 if Is_Abstract_Type (Derived_Type)
15319 and then In_Private_Part (Current_Scope)
15320 and then Has_Private_Declaration (Derived_Type)
15321 then
15322 declare
15323 Partial_View : Entity_Id;
15324 Elmt : Elmt_Id;
15325 Ent : Entity_Id;
15326
15327 begin
15328 Partial_View := First_Entity (Current_Scope);
15329 loop
15330 exit when No (Partial_View)
15331 or else (Has_Private_Declaration (Partial_View)
15332 and then
15333 Full_View (Partial_View) = Derived_Type);
15334
15335 Next_Entity (Partial_View);
15336 end loop;
15337
15338 -- If the partial view was not found then the source code
15339 -- has errors and the derivation is not needed.
15340
15341 if Present (Partial_View) then
15342 Elmt :=
15343 First_Elmt (Primitive_Operations (Partial_View));
15344 while Present (Elmt) loop
15345 Ent := Node (Elmt);
15346
15347 if Present (Alias (Ent))
15348 and then Ultimate_Alias (Ent) = Alias (Subp)
15349 then
15350 Append_Elmt
15351 (Ent, Primitive_Operations (Derived_Type));
15352 exit;
15353 end if;
15354
15355 Next_Elmt (Elmt);
15356 end loop;
15357
15358 -- If the interface primitive was not found in the
15359 -- partial view then this interface primitive was
15360 -- overridden. We add a derivation to activate in
15361 -- Derive_Progenitor_Subprograms the machinery to
15362 -- search for it.
15363
15364 if No (Elmt) then
15365 Derive_Interface_Subprogram
15366 (New_Subp => New_Subp,
15367 Subp => Subp,
15368 Actual_Subp => Act_Subp);
15369 end if;
15370 end if;
15371 end;
15372 else
15373 Derive_Interface_Subprogram
15374 (New_Subp => New_Subp,
15375 Subp => Subp,
15376 Actual_Subp => Act_Subp);
15377 end if;
15378
15379 -- Case 3: Common derivation
15380
15381 else
15382 Derive_Subprogram
15383 (New_Subp => New_Subp,
15384 Parent_Subp => Subp,
15385 Derived_Type => Derived_Type,
15386 Parent_Type => Parent_Base,
15387 Actual_Subp => Act_Subp);
15388 end if;
15389
15390 -- No need to update Act_Elm if we must search for the
15391 -- corresponding operation in the generic actual
15392
15393 if not Need_Search
15394 and then Present (Act_Elmt)
15395 then
15396 Next_Elmt (Act_Elmt);
15397 Act_Subp := Node (Act_Elmt);
15398 end if;
15399
15400 <<Continue>>
15401 Next_Elmt (Elmt);
15402 end loop;
15403
15404 -- Inherit additional operations from progenitors. If the derived
15405 -- type is a generic actual, there are not new primitive operations
15406 -- for the type because it has those of the actual, and therefore
15407 -- nothing needs to be done. The renamings generated above are not
15408 -- primitive operations, and their purpose is simply to make the
15409 -- proper operations visible within an instantiation.
15410
15411 if No (Generic_Actual) then
15412 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15413 end if;
15414 end if;
15415
15416 -- Final check: Direct descendants must have their primitives in the
15417 -- same order. We exclude from this test untagged types and instances
15418 -- of formal derived types. We skip this test if we have already
15419 -- reported serious errors in the sources.
15420
15421 pragma Assert (not Is_Tagged_Type (Derived_Type)
15422 or else Present (Generic_Actual)
15423 or else Serious_Errors_Detected > 0
15424 or else Check_Derived_Type);
15425 end Derive_Subprograms;
15426
15427 --------------------------------
15428 -- Derived_Standard_Character --
15429 --------------------------------
15430
15431 procedure Derived_Standard_Character
15432 (N : Node_Id;
15433 Parent_Type : Entity_Id;
15434 Derived_Type : Entity_Id)
15435 is
15436 Loc : constant Source_Ptr := Sloc (N);
15437 Def : constant Node_Id := Type_Definition (N);
15438 Indic : constant Node_Id := Subtype_Indication (Def);
15439 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15440 Implicit_Base : constant Entity_Id :=
15441 Create_Itype
15442 (E_Enumeration_Type, N, Derived_Type, 'B');
15443
15444 Lo : Node_Id;
15445 Hi : Node_Id;
15446
15447 begin
15448 Discard_Node (Process_Subtype (Indic, N));
15449
15450 Set_Etype (Implicit_Base, Parent_Base);
15451 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15452 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15453
15454 Set_Is_Character_Type (Implicit_Base, True);
15455 Set_Has_Delayed_Freeze (Implicit_Base);
15456
15457 -- The bounds of the implicit base are the bounds of the parent base.
15458 -- Note that their type is the parent base.
15459
15460 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15461 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15462
15463 Set_Scalar_Range (Implicit_Base,
15464 Make_Range (Loc,
15465 Low_Bound => Lo,
15466 High_Bound => Hi));
15467
15468 Conditional_Delay (Derived_Type, Parent_Type);
15469
15470 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15471 Set_Etype (Derived_Type, Implicit_Base);
15472 Set_Size_Info (Derived_Type, Parent_Type);
15473
15474 if Unknown_RM_Size (Derived_Type) then
15475 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15476 end if;
15477
15478 Set_Is_Character_Type (Derived_Type, True);
15479
15480 if Nkind (Indic) /= N_Subtype_Indication then
15481
15482 -- If no explicit constraint, the bounds are those
15483 -- of the parent type.
15484
15485 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15486 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15487 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15488 end if;
15489
15490 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15491
15492 -- Because the implicit base is used in the conversion of the bounds, we
15493 -- have to freeze it now. This is similar to what is done for numeric
15494 -- types, and it equally suspicious, but otherwise a non-static bound
15495 -- will have a reference to an unfrozen type, which is rejected by Gigi
15496 -- (???). This requires specific care for definition of stream
15497 -- attributes. For details, see comments at the end of
15498 -- Build_Derived_Numeric_Type.
15499
15500 Freeze_Before (N, Implicit_Base);
15501 end Derived_Standard_Character;
15502
15503 ------------------------------
15504 -- Derived_Type_Declaration --
15505 ------------------------------
15506
15507 procedure Derived_Type_Declaration
15508 (T : Entity_Id;
15509 N : Node_Id;
15510 Is_Completion : Boolean)
15511 is
15512 Parent_Type : Entity_Id;
15513
15514 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15515 -- Check whether the parent type is a generic formal, or derives
15516 -- directly or indirectly from one.
15517
15518 ------------------------
15519 -- Comes_From_Generic --
15520 ------------------------
15521
15522 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15523 begin
15524 if Is_Generic_Type (Typ) then
15525 return True;
15526
15527 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15528 return True;
15529
15530 elsif Is_Private_Type (Typ)
15531 and then Present (Full_View (Typ))
15532 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15533 then
15534 return True;
15535
15536 elsif Is_Generic_Actual_Type (Typ) then
15537 return True;
15538
15539 else
15540 return False;
15541 end if;
15542 end Comes_From_Generic;
15543
15544 -- Local variables
15545
15546 Def : constant Node_Id := Type_Definition (N);
15547 Iface_Def : Node_Id;
15548 Indic : constant Node_Id := Subtype_Indication (Def);
15549 Extension : constant Node_Id := Record_Extension_Part (Def);
15550 Parent_Node : Node_Id;
15551 Taggd : Boolean;
15552
15553 -- Start of processing for Derived_Type_Declaration
15554
15555 begin
15556 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15557
15558 -- Ada 2005 (AI-251): In case of interface derivation check that the
15559 -- parent is also an interface.
15560
15561 if Interface_Present (Def) then
15562 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15563
15564 if not Is_Interface (Parent_Type) then
15565 Diagnose_Interface (Indic, Parent_Type);
15566
15567 else
15568 Parent_Node := Parent (Base_Type (Parent_Type));
15569 Iface_Def := Type_Definition (Parent_Node);
15570
15571 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15572 -- other limited interfaces.
15573
15574 if Limited_Present (Def) then
15575 if Limited_Present (Iface_Def) then
15576 null;
15577
15578 elsif Protected_Present (Iface_Def) then
15579 Error_Msg_NE
15580 ("descendant of& must be declared"
15581 & " as a protected interface",
15582 N, Parent_Type);
15583
15584 elsif Synchronized_Present (Iface_Def) then
15585 Error_Msg_NE
15586 ("descendant of& must be declared"
15587 & " as a synchronized interface",
15588 N, Parent_Type);
15589
15590 elsif Task_Present (Iface_Def) then
15591 Error_Msg_NE
15592 ("descendant of& must be declared as a task interface",
15593 N, Parent_Type);
15594
15595 else
15596 Error_Msg_N
15597 ("(Ada 2005) limited interface cannot "
15598 & "inherit from non-limited interface", Indic);
15599 end if;
15600
15601 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15602 -- from non-limited or limited interfaces.
15603
15604 elsif not Protected_Present (Def)
15605 and then not Synchronized_Present (Def)
15606 and then not Task_Present (Def)
15607 then
15608 if Limited_Present (Iface_Def) then
15609 null;
15610
15611 elsif Protected_Present (Iface_Def) then
15612 Error_Msg_NE
15613 ("descendant of& must be declared"
15614 & " as a protected interface",
15615 N, Parent_Type);
15616
15617 elsif Synchronized_Present (Iface_Def) then
15618 Error_Msg_NE
15619 ("descendant of& must be declared"
15620 & " as a synchronized interface",
15621 N, Parent_Type);
15622
15623 elsif Task_Present (Iface_Def) then
15624 Error_Msg_NE
15625 ("descendant of& must be declared as a task interface",
15626 N, Parent_Type);
15627 else
15628 null;
15629 end if;
15630 end if;
15631 end if;
15632 end if;
15633
15634 if Is_Tagged_Type (Parent_Type)
15635 and then Is_Concurrent_Type (Parent_Type)
15636 and then not Is_Interface (Parent_Type)
15637 then
15638 Error_Msg_N
15639 ("parent type of a record extension cannot be "
15640 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
15641 Set_Etype (T, Any_Type);
15642 return;
15643 end if;
15644
15645 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15646 -- interfaces
15647
15648 if Is_Tagged_Type (Parent_Type)
15649 and then Is_Non_Empty_List (Interface_List (Def))
15650 then
15651 declare
15652 Intf : Node_Id;
15653 T : Entity_Id;
15654
15655 begin
15656 Intf := First (Interface_List (Def));
15657 while Present (Intf) loop
15658 T := Find_Type_Of_Subtype_Indic (Intf);
15659
15660 if not Is_Interface (T) then
15661 Diagnose_Interface (Intf, T);
15662
15663 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15664 -- a limited type from having a nonlimited progenitor.
15665
15666 elsif (Limited_Present (Def)
15667 or else (not Is_Interface (Parent_Type)
15668 and then Is_Limited_Type (Parent_Type)))
15669 and then not Is_Limited_Interface (T)
15670 then
15671 Error_Msg_NE
15672 ("progenitor interface& of limited type must be limited",
15673 N, T);
15674 end if;
15675
15676 Next (Intf);
15677 end loop;
15678 end;
15679 end if;
15680
15681 if Parent_Type = Any_Type
15682 or else Etype (Parent_Type) = Any_Type
15683 or else (Is_Class_Wide_Type (Parent_Type)
15684 and then Etype (Parent_Type) = T)
15685 then
15686 -- If Parent_Type is undefined or illegal, make new type into a
15687 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15688 -- errors. If this is a self-definition, emit error now.
15689
15690 if T = Parent_Type or else T = Etype (Parent_Type) then
15691 Error_Msg_N ("type cannot be used in its own definition", Indic);
15692 end if;
15693
15694 Set_Ekind (T, Ekind (Parent_Type));
15695 Set_Etype (T, Any_Type);
15696 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15697
15698 if Is_Tagged_Type (T)
15699 and then Is_Record_Type (T)
15700 then
15701 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15702 end if;
15703
15704 return;
15705 end if;
15706
15707 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15708 -- an interface is special because the list of interfaces in the full
15709 -- view can be given in any order. For example:
15710
15711 -- type A is interface;
15712 -- type B is interface and A;
15713 -- type D is new B with private;
15714 -- private
15715 -- type D is new A and B with null record; -- 1 --
15716
15717 -- In this case we perform the following transformation of -1-:
15718
15719 -- type D is new B and A with null record;
15720
15721 -- If the parent of the full-view covers the parent of the partial-view
15722 -- we have two possible cases:
15723
15724 -- 1) They have the same parent
15725 -- 2) The parent of the full-view implements some further interfaces
15726
15727 -- In both cases we do not need to perform the transformation. In the
15728 -- first case the source program is correct and the transformation is
15729 -- not needed; in the second case the source program does not fulfill
15730 -- the no-hidden interfaces rule (AI-396) and the error will be reported
15731 -- later.
15732
15733 -- This transformation not only simplifies the rest of the analysis of
15734 -- this type declaration but also simplifies the correct generation of
15735 -- the object layout to the expander.
15736
15737 if In_Private_Part (Current_Scope)
15738 and then Is_Interface (Parent_Type)
15739 then
15740 declare
15741 Iface : Node_Id;
15742 Partial_View : Entity_Id;
15743 Partial_View_Parent : Entity_Id;
15744 New_Iface : Node_Id;
15745
15746 begin
15747 -- Look for the associated private type declaration
15748
15749 Partial_View := First_Entity (Current_Scope);
15750 loop
15751 exit when No (Partial_View)
15752 or else (Has_Private_Declaration (Partial_View)
15753 and then Full_View (Partial_View) = T);
15754
15755 Next_Entity (Partial_View);
15756 end loop;
15757
15758 -- If the partial view was not found then the source code has
15759 -- errors and the transformation is not needed.
15760
15761 if Present (Partial_View) then
15762 Partial_View_Parent := Etype (Partial_View);
15763
15764 -- If the parent of the full-view covers the parent of the
15765 -- partial-view we have nothing else to do.
15766
15767 if Interface_Present_In_Ancestor
15768 (Parent_Type, Partial_View_Parent)
15769 then
15770 null;
15771
15772 -- Traverse the list of interfaces of the full-view to look
15773 -- for the parent of the partial-view and perform the tree
15774 -- transformation.
15775
15776 else
15777 Iface := First (Interface_List (Def));
15778 while Present (Iface) loop
15779 if Etype (Iface) = Etype (Partial_View) then
15780 Rewrite (Subtype_Indication (Def),
15781 New_Copy (Subtype_Indication
15782 (Parent (Partial_View))));
15783
15784 New_Iface :=
15785 Make_Identifier (Sloc (N), Chars (Parent_Type));
15786 Append (New_Iface, Interface_List (Def));
15787
15788 -- Analyze the transformed code
15789
15790 Derived_Type_Declaration (T, N, Is_Completion);
15791 return;
15792 end if;
15793
15794 Next (Iface);
15795 end loop;
15796 end if;
15797 end if;
15798 end;
15799 end if;
15800
15801 -- Only composite types other than array types are allowed to have
15802 -- discriminants.
15803
15804 if Present (Discriminant_Specifications (N)) then
15805 if (Is_Elementary_Type (Parent_Type)
15806 or else
15807 Is_Array_Type (Parent_Type))
15808 and then not Error_Posted (N)
15809 then
15810 Error_Msg_N
15811 ("elementary or array type cannot have discriminants",
15812 Defining_Identifier (First (Discriminant_Specifications (N))));
15813 Set_Has_Discriminants (T, False);
15814
15815 -- The type is allowed to have discriminants
15816
15817 else
15818 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
15819 end if;
15820 end if;
15821
15822 -- In Ada 83, a derived type defined in a package specification cannot
15823 -- be used for further derivation until the end of its visible part.
15824 -- Note that derivation in the private part of the package is allowed.
15825
15826 if Ada_Version = Ada_83
15827 and then Is_Derived_Type (Parent_Type)
15828 and then In_Visible_Part (Scope (Parent_Type))
15829 then
15830 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
15831 Error_Msg_N
15832 ("(Ada 83): premature use of type for derivation", Indic);
15833 end if;
15834 end if;
15835
15836 -- Check for early use of incomplete or private type
15837
15838 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
15839 Error_Msg_N ("premature derivation of incomplete type", Indic);
15840 return;
15841
15842 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
15843 and then not Comes_From_Generic (Parent_Type))
15844 or else Has_Private_Component (Parent_Type)
15845 then
15846 -- The ancestor type of a formal type can be incomplete, in which
15847 -- case only the operations of the partial view are available in the
15848 -- generic. Subsequent checks may be required when the full view is
15849 -- analyzed to verify that a derivation from a tagged type has an
15850 -- extension.
15851
15852 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
15853 null;
15854
15855 elsif No (Underlying_Type (Parent_Type))
15856 or else Has_Private_Component (Parent_Type)
15857 then
15858 Error_Msg_N
15859 ("premature derivation of derived or private type", Indic);
15860
15861 -- Flag the type itself as being in error, this prevents some
15862 -- nasty problems with subsequent uses of the malformed type.
15863
15864 Set_Error_Posted (T);
15865
15866 -- Check that within the immediate scope of an untagged partial
15867 -- view it's illegal to derive from the partial view if the
15868 -- full view is tagged. (7.3(7))
15869
15870 -- We verify that the Parent_Type is a partial view by checking
15871 -- that it is not a Full_Type_Declaration (i.e. a private type or
15872 -- private extension declaration), to distinguish a partial view
15873 -- from a derivation from a private type which also appears as
15874 -- E_Private_Type. If the parent base type is not declared in an
15875 -- enclosing scope there is no need to check.
15876
15877 elsif Present (Full_View (Parent_Type))
15878 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
15879 and then not Is_Tagged_Type (Parent_Type)
15880 and then Is_Tagged_Type (Full_View (Parent_Type))
15881 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15882 then
15883 Error_Msg_N
15884 ("premature derivation from type with tagged full view",
15885 Indic);
15886 end if;
15887 end if;
15888
15889 -- Check that form of derivation is appropriate
15890
15891 Taggd := Is_Tagged_Type (Parent_Type);
15892
15893 -- Set the parent type to the class-wide type's specific type in this
15894 -- case to prevent cascading errors
15895
15896 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
15897 Error_Msg_N ("parent type must not be a class-wide type", Indic);
15898 Set_Etype (T, Etype (Parent_Type));
15899 return;
15900 end if;
15901
15902 if Present (Extension) and then not Taggd then
15903 Error_Msg_N
15904 ("type derived from untagged type cannot have extension", Indic);
15905
15906 elsif No (Extension) and then Taggd then
15907
15908 -- If this declaration is within a private part (or body) of a
15909 -- generic instantiation then the derivation is allowed (the parent
15910 -- type can only appear tagged in this case if it's a generic actual
15911 -- type, since it would otherwise have been rejected in the analysis
15912 -- of the generic template).
15913
15914 if not Is_Generic_Actual_Type (Parent_Type)
15915 or else In_Visible_Part (Scope (Parent_Type))
15916 then
15917 if Is_Class_Wide_Type (Parent_Type) then
15918 Error_Msg_N
15919 ("parent type must not be a class-wide type", Indic);
15920
15921 -- Use specific type to prevent cascaded errors.
15922
15923 Parent_Type := Etype (Parent_Type);
15924
15925 else
15926 Error_Msg_N
15927 ("type derived from tagged type must have extension", Indic);
15928 end if;
15929 end if;
15930 end if;
15931
15932 -- AI-443: Synchronized formal derived types require a private
15933 -- extension. There is no point in checking the ancestor type or
15934 -- the progenitors since the construct is wrong to begin with.
15935
15936 if Ada_Version >= Ada_2005
15937 and then Is_Generic_Type (T)
15938 and then Present (Original_Node (N))
15939 then
15940 declare
15941 Decl : constant Node_Id := Original_Node (N);
15942
15943 begin
15944 if Nkind (Decl) = N_Formal_Type_Declaration
15945 and then Nkind (Formal_Type_Definition (Decl)) =
15946 N_Formal_Derived_Type_Definition
15947 and then Synchronized_Present (Formal_Type_Definition (Decl))
15948 and then No (Extension)
15949
15950 -- Avoid emitting a duplicate error message
15951
15952 and then not Error_Posted (Indic)
15953 then
15954 Error_Msg_N
15955 ("synchronized derived type must have extension", N);
15956 end if;
15957 end;
15958 end if;
15959
15960 if Null_Exclusion_Present (Def)
15961 and then not Is_Access_Type (Parent_Type)
15962 then
15963 Error_Msg_N ("null exclusion can only apply to an access type", N);
15964 end if;
15965
15966 -- Avoid deriving parent primitives of underlying record views
15967
15968 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
15969 Derive_Subps => not Is_Underlying_Record_View (T));
15970
15971 -- AI-419: The parent type of an explicitly limited derived type must
15972 -- be a limited type or a limited interface.
15973
15974 if Limited_Present (Def) then
15975 Set_Is_Limited_Record (T);
15976
15977 if Is_Interface (T) then
15978 Set_Is_Limited_Interface (T);
15979 end if;
15980
15981 if not Is_Limited_Type (Parent_Type)
15982 and then
15983 (not Is_Interface (Parent_Type)
15984 or else not Is_Limited_Interface (Parent_Type))
15985 then
15986 -- AI05-0096: a derivation in the private part of an instance is
15987 -- legal if the generic formal is untagged limited, and the actual
15988 -- is non-limited.
15989
15990 if Is_Generic_Actual_Type (Parent_Type)
15991 and then In_Private_Part (Current_Scope)
15992 and then
15993 not Is_Tagged_Type
15994 (Generic_Parent_Type (Parent (Parent_Type)))
15995 then
15996 null;
15997
15998 else
15999 Error_Msg_NE
16000 ("parent type& of limited type must be limited",
16001 N, Parent_Type);
16002 end if;
16003 end if;
16004 end if;
16005
16006 -- In SPARK, there are no derived type definitions other than type
16007 -- extensions of tagged record types.
16008
16009 if No (Extension) then
16010 Check_SPARK_05_Restriction
16011 ("derived type is not allowed", Original_Node (N));
16012 end if;
16013 end Derived_Type_Declaration;
16014
16015 ------------------------
16016 -- Diagnose_Interface --
16017 ------------------------
16018
16019 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16020 begin
16021 if not Is_Interface (E) and then E /= Any_Type then
16022 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16023 end if;
16024 end Diagnose_Interface;
16025
16026 ----------------------------------
16027 -- Enumeration_Type_Declaration --
16028 ----------------------------------
16029
16030 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16031 Ev : Uint;
16032 L : Node_Id;
16033 R_Node : Node_Id;
16034 B_Node : Node_Id;
16035
16036 begin
16037 -- Create identifier node representing lower bound
16038
16039 B_Node := New_Node (N_Identifier, Sloc (Def));
16040 L := First (Literals (Def));
16041 Set_Chars (B_Node, Chars (L));
16042 Set_Entity (B_Node, L);
16043 Set_Etype (B_Node, T);
16044 Set_Is_Static_Expression (B_Node, True);
16045
16046 R_Node := New_Node (N_Range, Sloc (Def));
16047 Set_Low_Bound (R_Node, B_Node);
16048
16049 Set_Ekind (T, E_Enumeration_Type);
16050 Set_First_Literal (T, L);
16051 Set_Etype (T, T);
16052 Set_Is_Constrained (T);
16053
16054 Ev := Uint_0;
16055
16056 -- Loop through literals of enumeration type setting pos and rep values
16057 -- except that if the Ekind is already set, then it means the literal
16058 -- was already constructed (case of a derived type declaration and we
16059 -- should not disturb the Pos and Rep values.
16060
16061 while Present (L) loop
16062 if Ekind (L) /= E_Enumeration_Literal then
16063 Set_Ekind (L, E_Enumeration_Literal);
16064 Set_Enumeration_Pos (L, Ev);
16065 Set_Enumeration_Rep (L, Ev);
16066 Set_Is_Known_Valid (L, True);
16067 end if;
16068
16069 Set_Etype (L, T);
16070 New_Overloaded_Entity (L);
16071 Generate_Definition (L);
16072 Set_Convention (L, Convention_Intrinsic);
16073
16074 -- Case of character literal
16075
16076 if Nkind (L) = N_Defining_Character_Literal then
16077 Set_Is_Character_Type (T, True);
16078
16079 -- Check violation of No_Wide_Characters
16080
16081 if Restriction_Check_Required (No_Wide_Characters) then
16082 Get_Name_String (Chars (L));
16083
16084 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16085 Check_Restriction (No_Wide_Characters, L);
16086 end if;
16087 end if;
16088 end if;
16089
16090 Ev := Ev + 1;
16091 Next (L);
16092 end loop;
16093
16094 -- Now create a node representing upper bound
16095
16096 B_Node := New_Node (N_Identifier, Sloc (Def));
16097 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16098 Set_Entity (B_Node, Last (Literals (Def)));
16099 Set_Etype (B_Node, T);
16100 Set_Is_Static_Expression (B_Node, True);
16101
16102 Set_High_Bound (R_Node, B_Node);
16103
16104 -- Initialize various fields of the type. Some of this information
16105 -- may be overwritten later through rep.clauses.
16106
16107 Set_Scalar_Range (T, R_Node);
16108 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16109 Set_Enum_Esize (T);
16110 Set_Enum_Pos_To_Rep (T, Empty);
16111
16112 -- Set Discard_Names if configuration pragma set, or if there is
16113 -- a parameterless pragma in the current declarative region
16114
16115 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16116 Set_Discard_Names (T);
16117 end if;
16118
16119 -- Process end label if there is one
16120
16121 if Present (Def) then
16122 Process_End_Label (Def, 'e', T);
16123 end if;
16124 end Enumeration_Type_Declaration;
16125
16126 ---------------------------------
16127 -- Expand_To_Stored_Constraint --
16128 ---------------------------------
16129
16130 function Expand_To_Stored_Constraint
16131 (Typ : Entity_Id;
16132 Constraint : Elist_Id) return Elist_Id
16133 is
16134 Explicitly_Discriminated_Type : Entity_Id;
16135 Expansion : Elist_Id;
16136 Discriminant : Entity_Id;
16137
16138 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16139 -- Find the nearest type that actually specifies discriminants
16140
16141 ---------------------------------
16142 -- Type_With_Explicit_Discrims --
16143 ---------------------------------
16144
16145 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16146 Typ : constant E := Base_Type (Id);
16147
16148 begin
16149 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16150 if Present (Full_View (Typ)) then
16151 return Type_With_Explicit_Discrims (Full_View (Typ));
16152 end if;
16153
16154 else
16155 if Has_Discriminants (Typ) then
16156 return Typ;
16157 end if;
16158 end if;
16159
16160 if Etype (Typ) = Typ then
16161 return Empty;
16162 elsif Has_Discriminants (Typ) then
16163 return Typ;
16164 else
16165 return Type_With_Explicit_Discrims (Etype (Typ));
16166 end if;
16167
16168 end Type_With_Explicit_Discrims;
16169
16170 -- Start of processing for Expand_To_Stored_Constraint
16171
16172 begin
16173 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16174 return No_Elist;
16175 end if;
16176
16177 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16178
16179 if No (Explicitly_Discriminated_Type) then
16180 return No_Elist;
16181 end if;
16182
16183 Expansion := New_Elmt_List;
16184
16185 Discriminant :=
16186 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16187 while Present (Discriminant) loop
16188 Append_Elmt
16189 (Get_Discriminant_Value
16190 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16191 To => Expansion);
16192 Next_Stored_Discriminant (Discriminant);
16193 end loop;
16194
16195 return Expansion;
16196 end Expand_To_Stored_Constraint;
16197
16198 ---------------------------
16199 -- Find_Hidden_Interface --
16200 ---------------------------
16201
16202 function Find_Hidden_Interface
16203 (Src : Elist_Id;
16204 Dest : Elist_Id) return Entity_Id
16205 is
16206 Iface : Entity_Id;
16207 Iface_Elmt : Elmt_Id;
16208
16209 begin
16210 if Present (Src) and then Present (Dest) then
16211 Iface_Elmt := First_Elmt (Src);
16212 while Present (Iface_Elmt) loop
16213 Iface := Node (Iface_Elmt);
16214
16215 if Is_Interface (Iface)
16216 and then not Contain_Interface (Iface, Dest)
16217 then
16218 return Iface;
16219 end if;
16220
16221 Next_Elmt (Iface_Elmt);
16222 end loop;
16223 end if;
16224
16225 return Empty;
16226 end Find_Hidden_Interface;
16227
16228 --------------------
16229 -- Find_Type_Name --
16230 --------------------
16231
16232 function Find_Type_Name (N : Node_Id) return Entity_Id is
16233 Id : constant Entity_Id := Defining_Identifier (N);
16234 Prev : Entity_Id;
16235 New_Id : Entity_Id;
16236 Prev_Par : Node_Id;
16237
16238 procedure Check_Duplicate_Aspects;
16239 -- Check that aspects specified in a completion have not been specified
16240 -- already in the partial view. Type_Invariant and others can be
16241 -- specified on either view but never on both.
16242
16243 procedure Tag_Mismatch;
16244 -- Diagnose a tagged partial view whose full view is untagged.
16245 -- We post the message on the full view, with a reference to
16246 -- the previous partial view. The partial view can be private
16247 -- or incomplete, and these are handled in a different manner,
16248 -- so we determine the position of the error message from the
16249 -- respective slocs of both.
16250
16251 -----------------------------
16252 -- Check_Duplicate_Aspects --
16253 -----------------------------
16254 procedure Check_Duplicate_Aspects is
16255 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
16256 Full_Aspects : constant List_Id := Aspect_Specifications (N);
16257 F_Spec, P_Spec : Node_Id;
16258
16259 begin
16260 if Present (Prev_Aspects) and then Present (Full_Aspects) then
16261 F_Spec := First (Full_Aspects);
16262 while Present (F_Spec) loop
16263 P_Spec := First (Prev_Aspects);
16264 while Present (P_Spec) loop
16265 if Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
16266 then
16267 Error_Msg_N
16268 ("aspect already specified in private declaration",
16269 F_Spec);
16270 Remove (F_Spec);
16271 return;
16272 end if;
16273
16274 Next (P_Spec);
16275 end loop;
16276
16277 Next (F_Spec);
16278 end loop;
16279 end if;
16280 end Check_Duplicate_Aspects;
16281
16282 ------------------
16283 -- Tag_Mismatch --
16284 ------------------
16285
16286 procedure Tag_Mismatch is
16287 begin
16288 if Sloc (Prev) < Sloc (Id) then
16289 if Ada_Version >= Ada_2012
16290 and then Nkind (N) = N_Private_Type_Declaration
16291 then
16292 Error_Msg_NE
16293 ("declaration of private } must be a tagged type ", Id, Prev);
16294 else
16295 Error_Msg_NE
16296 ("full declaration of } must be a tagged type ", Id, Prev);
16297 end if;
16298
16299 else
16300 if Ada_Version >= Ada_2012
16301 and then Nkind (N) = N_Private_Type_Declaration
16302 then
16303 Error_Msg_NE
16304 ("declaration of private } must be a tagged type ", Prev, Id);
16305 else
16306 Error_Msg_NE
16307 ("full declaration of } must be a tagged type ", Prev, Id);
16308 end if;
16309 end if;
16310 end Tag_Mismatch;
16311
16312 -- Start of processing for Find_Type_Name
16313
16314 begin
16315 -- Find incomplete declaration, if one was given
16316
16317 Prev := Current_Entity_In_Scope (Id);
16318
16319 -- New type declaration
16320
16321 if No (Prev) then
16322 Enter_Name (Id);
16323 return Id;
16324
16325 -- Previous declaration exists
16326
16327 else
16328 Prev_Par := Parent (Prev);
16329
16330 -- Error if not incomplete/private case except if previous
16331 -- declaration is implicit, etc. Enter_Name will emit error if
16332 -- appropriate.
16333
16334 if not Is_Incomplete_Or_Private_Type (Prev) then
16335 Enter_Name (Id);
16336 New_Id := Id;
16337
16338 -- Check invalid completion of private or incomplete type
16339
16340 elsif not Nkind_In (N, N_Full_Type_Declaration,
16341 N_Task_Type_Declaration,
16342 N_Protected_Type_Declaration)
16343 and then
16344 (Ada_Version < Ada_2012
16345 or else not Is_Incomplete_Type (Prev)
16346 or else not Nkind_In (N, N_Private_Type_Declaration,
16347 N_Private_Extension_Declaration))
16348 then
16349 -- Completion must be a full type declarations (RM 7.3(4))
16350
16351 Error_Msg_Sloc := Sloc (Prev);
16352 Error_Msg_NE ("invalid completion of }", Id, Prev);
16353
16354 -- Set scope of Id to avoid cascaded errors. Entity is never
16355 -- examined again, except when saving globals in generics.
16356
16357 Set_Scope (Id, Current_Scope);
16358 New_Id := Id;
16359
16360 -- If this is a repeated incomplete declaration, no further
16361 -- checks are possible.
16362
16363 if Nkind (N) = N_Incomplete_Type_Declaration then
16364 return Prev;
16365 end if;
16366
16367 -- Case of full declaration of incomplete type
16368
16369 elsif Ekind (Prev) = E_Incomplete_Type
16370 and then (Ada_Version < Ada_2012
16371 or else No (Full_View (Prev))
16372 or else not Is_Private_Type (Full_View (Prev)))
16373 then
16374 -- Indicate that the incomplete declaration has a matching full
16375 -- declaration. The defining occurrence of the incomplete
16376 -- declaration remains the visible one, and the procedure
16377 -- Get_Full_View dereferences it whenever the type is used.
16378
16379 if Present (Full_View (Prev)) then
16380 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16381 end if;
16382
16383 Set_Full_View (Prev, Id);
16384 Append_Entity (Id, Current_Scope);
16385 Set_Is_Public (Id, Is_Public (Prev));
16386 Set_Is_Internal (Id);
16387 New_Id := Prev;
16388
16389 -- If the incomplete view is tagged, a class_wide type has been
16390 -- created already. Use it for the private type as well, in order
16391 -- to prevent multiple incompatible class-wide types that may be
16392 -- created for self-referential anonymous access components.
16393
16394 if Is_Tagged_Type (Prev)
16395 and then Present (Class_Wide_Type (Prev))
16396 then
16397 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16398 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16399
16400 -- The type of the classwide type is the current Id. Previously
16401 -- this was not done for private declarations because of order-
16402 -- of elaboration issues in the back-end, but gigi now handles
16403 -- this properly.
16404
16405 Set_Etype (Class_Wide_Type (Id), Id);
16406 end if;
16407
16408 -- Case of full declaration of private type
16409
16410 else
16411 -- If the private type was a completion of an incomplete type then
16412 -- update Prev to reference the private type
16413
16414 if Ada_Version >= Ada_2012
16415 and then Ekind (Prev) = E_Incomplete_Type
16416 and then Present (Full_View (Prev))
16417 and then Is_Private_Type (Full_View (Prev))
16418 then
16419 Prev := Full_View (Prev);
16420 Prev_Par := Parent (Prev);
16421 end if;
16422
16423 if Nkind (N) = N_Full_Type_Declaration
16424 and then Nkind_In
16425 (Type_Definition (N), N_Record_Definition,
16426 N_Derived_Type_Definition)
16427 and then Interface_Present (Type_Definition (N))
16428 then
16429 Error_Msg_N
16430 ("completion of private type cannot be an interface", N);
16431 end if;
16432
16433 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16434 if Etype (Prev) /= Prev then
16435
16436 -- Prev is a private subtype or a derived type, and needs
16437 -- no completion.
16438
16439 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16440 New_Id := Id;
16441
16442 elsif Ekind (Prev) = E_Private_Type
16443 and then Nkind_In (N, N_Task_Type_Declaration,
16444 N_Protected_Type_Declaration)
16445 then
16446 Error_Msg_N
16447 ("completion of nonlimited type cannot be limited", N);
16448
16449 elsif Ekind (Prev) = E_Record_Type_With_Private
16450 and then Nkind_In (N, N_Task_Type_Declaration,
16451 N_Protected_Type_Declaration)
16452 then
16453 if not Is_Limited_Record (Prev) then
16454 Error_Msg_N
16455 ("completion of nonlimited type cannot be limited", N);
16456
16457 elsif No (Interface_List (N)) then
16458 Error_Msg_N
16459 ("completion of tagged private type must be tagged",
16460 N);
16461 end if;
16462 end if;
16463
16464 -- Ada 2005 (AI-251): Private extension declaration of a task
16465 -- type or a protected type. This case arises when covering
16466 -- interface types.
16467
16468 elsif Nkind_In (N, N_Task_Type_Declaration,
16469 N_Protected_Type_Declaration)
16470 then
16471 null;
16472
16473 elsif Nkind (N) /= N_Full_Type_Declaration
16474 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16475 then
16476 Error_Msg_N
16477 ("full view of private extension must be an extension", N);
16478
16479 elsif not (Abstract_Present (Parent (Prev)))
16480 and then Abstract_Present (Type_Definition (N))
16481 then
16482 Error_Msg_N
16483 ("full view of non-abstract extension cannot be abstract", N);
16484 end if;
16485
16486 if not In_Private_Part (Current_Scope) then
16487 Error_Msg_N
16488 ("declaration of full view must appear in private part", N);
16489 end if;
16490
16491 if Ada_Version >= Ada_2012 then
16492 Check_Duplicate_Aspects;
16493 end if;
16494
16495 Copy_And_Swap (Prev, Id);
16496 Set_Has_Private_Declaration (Prev);
16497 Set_Has_Private_Declaration (Id);
16498
16499 -- AI12-0133: Indicate whether we have a partial view with
16500 -- unknown discriminants, in which case initialization of objects
16501 -- of the type do not receive an invariant check.
16502
16503 Set_Partial_View_Has_Unknown_Discr
16504 (Prev, Has_Unknown_Discriminants (Id));
16505
16506 -- Preserve aspect and iterator flags that may have been set on
16507 -- the partial view.
16508
16509 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16510 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16511
16512 -- If no error, propagate freeze_node from private to full view.
16513 -- It may have been generated for an early operational item.
16514
16515 if Present (Freeze_Node (Id))
16516 and then Serious_Errors_Detected = 0
16517 and then No (Full_View (Id))
16518 then
16519 Set_Freeze_Node (Prev, Freeze_Node (Id));
16520 Set_Freeze_Node (Id, Empty);
16521 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16522 end if;
16523
16524 Set_Full_View (Id, Prev);
16525 New_Id := Prev;
16526 end if;
16527
16528 -- Verify that full declaration conforms to partial one
16529
16530 if Is_Incomplete_Or_Private_Type (Prev)
16531 and then Present (Discriminant_Specifications (Prev_Par))
16532 then
16533 if Present (Discriminant_Specifications (N)) then
16534 if Ekind (Prev) = E_Incomplete_Type then
16535 Check_Discriminant_Conformance (N, Prev, Prev);
16536 else
16537 Check_Discriminant_Conformance (N, Prev, Id);
16538 end if;
16539
16540 else
16541 Error_Msg_N
16542 ("missing discriminants in full type declaration", N);
16543
16544 -- To avoid cascaded errors on subsequent use, share the
16545 -- discriminants of the partial view.
16546
16547 Set_Discriminant_Specifications (N,
16548 Discriminant_Specifications (Prev_Par));
16549 end if;
16550 end if;
16551
16552 -- A prior untagged partial view can have an associated class-wide
16553 -- type due to use of the class attribute, and in this case the full
16554 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16555 -- of incomplete tagged declarations, but we check for it.
16556
16557 if Is_Type (Prev)
16558 and then (Is_Tagged_Type (Prev)
16559 or else Present (Class_Wide_Type (Prev)))
16560 then
16561 -- Ada 2012 (AI05-0162): A private type may be the completion of
16562 -- an incomplete type.
16563
16564 if Ada_Version >= Ada_2012
16565 and then Is_Incomplete_Type (Prev)
16566 and then Nkind_In (N, N_Private_Type_Declaration,
16567 N_Private_Extension_Declaration)
16568 then
16569 -- No need to check private extensions since they are tagged
16570
16571 if Nkind (N) = N_Private_Type_Declaration
16572 and then not Tagged_Present (N)
16573 then
16574 Tag_Mismatch;
16575 end if;
16576
16577 -- The full declaration is either a tagged type (including
16578 -- a synchronized type that implements interfaces) or a
16579 -- type extension, otherwise this is an error.
16580
16581 elsif Nkind_In (N, N_Task_Type_Declaration,
16582 N_Protected_Type_Declaration)
16583 then
16584 if No (Interface_List (N)) and then not Error_Posted (N) then
16585 Tag_Mismatch;
16586 end if;
16587
16588 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16589
16590 -- Indicate that the previous declaration (tagged incomplete
16591 -- or private declaration) requires the same on the full one.
16592
16593 if not Tagged_Present (Type_Definition (N)) then
16594 Tag_Mismatch;
16595 Set_Is_Tagged_Type (Id);
16596 end if;
16597
16598 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16599 if No (Record_Extension_Part (Type_Definition (N))) then
16600 Error_Msg_NE
16601 ("full declaration of } must be a record extension",
16602 Prev, Id);
16603
16604 -- Set some attributes to produce a usable full view
16605
16606 Set_Is_Tagged_Type (Id);
16607 end if;
16608
16609 else
16610 Tag_Mismatch;
16611 end if;
16612 end if;
16613
16614 if Present (Prev)
16615 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16616 and then Present (Premature_Use (Parent (Prev)))
16617 then
16618 Error_Msg_Sloc := Sloc (N);
16619 Error_Msg_N
16620 ("\full declaration #", Premature_Use (Parent (Prev)));
16621 end if;
16622
16623 return New_Id;
16624 end if;
16625 end Find_Type_Name;
16626
16627 -------------------------
16628 -- Find_Type_Of_Object --
16629 -------------------------
16630
16631 function Find_Type_Of_Object
16632 (Obj_Def : Node_Id;
16633 Related_Nod : Node_Id) return Entity_Id
16634 is
16635 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16636 P : Node_Id := Parent (Obj_Def);
16637 T : Entity_Id;
16638 Nam : Name_Id;
16639
16640 begin
16641 -- If the parent is a component_definition node we climb to the
16642 -- component_declaration node
16643
16644 if Nkind (P) = N_Component_Definition then
16645 P := Parent (P);
16646 end if;
16647
16648 -- Case of an anonymous array subtype
16649
16650 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16651 N_Unconstrained_Array_Definition)
16652 then
16653 T := Empty;
16654 Array_Type_Declaration (T, Obj_Def);
16655
16656 -- Create an explicit subtype whenever possible
16657
16658 elsif Nkind (P) /= N_Component_Declaration
16659 and then Def_Kind = N_Subtype_Indication
16660 then
16661 -- Base name of subtype on object name, which will be unique in
16662 -- the current scope.
16663
16664 -- If this is a duplicate declaration, return base type, to avoid
16665 -- generating duplicate anonymous types.
16666
16667 if Error_Posted (P) then
16668 Analyze (Subtype_Mark (Obj_Def));
16669 return Entity (Subtype_Mark (Obj_Def));
16670 end if;
16671
16672 Nam :=
16673 New_External_Name
16674 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16675
16676 T := Make_Defining_Identifier (Sloc (P), Nam);
16677
16678 Insert_Action (Obj_Def,
16679 Make_Subtype_Declaration (Sloc (P),
16680 Defining_Identifier => T,
16681 Subtype_Indication => Relocate_Node (Obj_Def)));
16682
16683 -- This subtype may need freezing, and this will not be done
16684 -- automatically if the object declaration is not in declarative
16685 -- part. Since this is an object declaration, the type cannot always
16686 -- be frozen here. Deferred constants do not freeze their type
16687 -- (which often enough will be private).
16688
16689 if Nkind (P) = N_Object_Declaration
16690 and then Constant_Present (P)
16691 and then No (Expression (P))
16692 then
16693 null;
16694
16695 -- Here we freeze the base type of object type to catch premature use
16696 -- of discriminated private type without a full view.
16697
16698 else
16699 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
16700 end if;
16701
16702 -- Ada 2005 AI-406: the object definition in an object declaration
16703 -- can be an access definition.
16704
16705 elsif Def_Kind = N_Access_Definition then
16706 T := Access_Definition (Related_Nod, Obj_Def);
16707
16708 Set_Is_Local_Anonymous_Access
16709 (T,
16710 V => (Ada_Version < Ada_2012)
16711 or else (Nkind (P) /= N_Object_Declaration)
16712 or else Is_Library_Level_Entity (Defining_Identifier (P)));
16713
16714 -- Otherwise, the object definition is just a subtype_mark
16715
16716 else
16717 T := Process_Subtype (Obj_Def, Related_Nod);
16718
16719 -- If expansion is disabled an object definition that is an aggregate
16720 -- will not get expanded and may lead to scoping problems in the back
16721 -- end, if the object is referenced in an inner scope. In that case
16722 -- create an itype reference for the object definition now. This
16723 -- may be redundant in some cases, but harmless.
16724
16725 if Is_Itype (T)
16726 and then Nkind (Related_Nod) = N_Object_Declaration
16727 and then ASIS_Mode
16728 then
16729 Build_Itype_Reference (T, Related_Nod);
16730 end if;
16731 end if;
16732
16733 return T;
16734 end Find_Type_Of_Object;
16735
16736 --------------------------------
16737 -- Find_Type_Of_Subtype_Indic --
16738 --------------------------------
16739
16740 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
16741 Typ : Entity_Id;
16742
16743 begin
16744 -- Case of subtype mark with a constraint
16745
16746 if Nkind (S) = N_Subtype_Indication then
16747 Find_Type (Subtype_Mark (S));
16748 Typ := Entity (Subtype_Mark (S));
16749
16750 if not
16751 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
16752 then
16753 Error_Msg_N
16754 ("incorrect constraint for this kind of type", Constraint (S));
16755 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16756 end if;
16757
16758 -- Otherwise we have a subtype mark without a constraint
16759
16760 elsif Error_Posted (S) then
16761 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
16762 return Any_Type;
16763
16764 else
16765 Find_Type (S);
16766 Typ := Entity (S);
16767 end if;
16768
16769 -- Check No_Wide_Characters restriction
16770
16771 Check_Wide_Character_Restriction (Typ, S);
16772
16773 return Typ;
16774 end Find_Type_Of_Subtype_Indic;
16775
16776 -------------------------------------
16777 -- Floating_Point_Type_Declaration --
16778 -------------------------------------
16779
16780 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16781 Digs : constant Node_Id := Digits_Expression (Def);
16782 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
16783 Digs_Val : Uint;
16784 Base_Typ : Entity_Id;
16785 Implicit_Base : Entity_Id;
16786 Bound : Node_Id;
16787
16788 function Can_Derive_From (E : Entity_Id) return Boolean;
16789 -- Find if given digits value, and possibly a specified range, allows
16790 -- derivation from specified type
16791
16792 function Find_Base_Type return Entity_Id;
16793 -- Find a predefined base type that Def can derive from, or generate
16794 -- an error and substitute Long_Long_Float if none exists.
16795
16796 ---------------------
16797 -- Can_Derive_From --
16798 ---------------------
16799
16800 function Can_Derive_From (E : Entity_Id) return Boolean is
16801 Spec : constant Entity_Id := Real_Range_Specification (Def);
16802
16803 begin
16804 -- Check specified "digits" constraint
16805
16806 if Digs_Val > Digits_Value (E) then
16807 return False;
16808 end if;
16809
16810 -- Check for matching range, if specified
16811
16812 if Present (Spec) then
16813 if Expr_Value_R (Type_Low_Bound (E)) >
16814 Expr_Value_R (Low_Bound (Spec))
16815 then
16816 return False;
16817 end if;
16818
16819 if Expr_Value_R (Type_High_Bound (E)) <
16820 Expr_Value_R (High_Bound (Spec))
16821 then
16822 return False;
16823 end if;
16824 end if;
16825
16826 return True;
16827 end Can_Derive_From;
16828
16829 --------------------
16830 -- Find_Base_Type --
16831 --------------------
16832
16833 function Find_Base_Type return Entity_Id is
16834 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
16835
16836 begin
16837 -- Iterate over the predefined types in order, returning the first
16838 -- one that Def can derive from.
16839
16840 while Present (Choice) loop
16841 if Can_Derive_From (Node (Choice)) then
16842 return Node (Choice);
16843 end if;
16844
16845 Next_Elmt (Choice);
16846 end loop;
16847
16848 -- If we can't derive from any existing type, use Long_Long_Float
16849 -- and give appropriate message explaining the problem.
16850
16851 if Digs_Val > Max_Digs_Val then
16852 -- It might be the case that there is a type with the requested
16853 -- range, just not the combination of digits and range.
16854
16855 Error_Msg_N
16856 ("no predefined type has requested range and precision",
16857 Real_Range_Specification (Def));
16858
16859 else
16860 Error_Msg_N
16861 ("range too large for any predefined type",
16862 Real_Range_Specification (Def));
16863 end if;
16864
16865 return Standard_Long_Long_Float;
16866 end Find_Base_Type;
16867
16868 -- Start of processing for Floating_Point_Type_Declaration
16869
16870 begin
16871 Check_Restriction (No_Floating_Point, Def);
16872
16873 -- Create an implicit base type
16874
16875 Implicit_Base :=
16876 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
16877
16878 -- Analyze and verify digits value
16879
16880 Analyze_And_Resolve (Digs, Any_Integer);
16881 Check_Digits_Expression (Digs);
16882 Digs_Val := Expr_Value (Digs);
16883
16884 -- Process possible range spec and find correct type to derive from
16885
16886 Process_Real_Range_Specification (Def);
16887
16888 -- Check that requested number of digits is not too high.
16889
16890 if Digs_Val > Max_Digs_Val then
16891
16892 -- The check for Max_Base_Digits may be somewhat expensive, as it
16893 -- requires reading System, so only do it when necessary.
16894
16895 declare
16896 Max_Base_Digits : constant Uint :=
16897 Expr_Value
16898 (Expression
16899 (Parent (RTE (RE_Max_Base_Digits))));
16900
16901 begin
16902 if Digs_Val > Max_Base_Digits then
16903 Error_Msg_Uint_1 := Max_Base_Digits;
16904 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
16905
16906 elsif No (Real_Range_Specification (Def)) then
16907 Error_Msg_Uint_1 := Max_Digs_Val;
16908 Error_Msg_N ("types with more than ^ digits need range spec "
16909 & "(RM 3.5.7(6))", Digs);
16910 end if;
16911 end;
16912 end if;
16913
16914 -- Find a suitable type to derive from or complain and use a substitute
16915
16916 Base_Typ := Find_Base_Type;
16917
16918 -- If there are bounds given in the declaration use them as the bounds
16919 -- of the type, otherwise use the bounds of the predefined base type
16920 -- that was chosen based on the Digits value.
16921
16922 if Present (Real_Range_Specification (Def)) then
16923 Set_Scalar_Range (T, Real_Range_Specification (Def));
16924 Set_Is_Constrained (T);
16925
16926 -- The bounds of this range must be converted to machine numbers
16927 -- in accordance with RM 4.9(38).
16928
16929 Bound := Type_Low_Bound (T);
16930
16931 if Nkind (Bound) = N_Real_Literal then
16932 Set_Realval
16933 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
16934 Set_Is_Machine_Number (Bound);
16935 end if;
16936
16937 Bound := Type_High_Bound (T);
16938
16939 if Nkind (Bound) = N_Real_Literal then
16940 Set_Realval
16941 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
16942 Set_Is_Machine_Number (Bound);
16943 end if;
16944
16945 else
16946 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
16947 end if;
16948
16949 -- Complete definition of implicit base and declared first subtype. The
16950 -- inheritance of the rep item chain ensures that SPARK-related pragmas
16951 -- are not clobbered when the floating point type acts as a full view of
16952 -- a private type.
16953
16954 Set_Etype (Implicit_Base, Base_Typ);
16955 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
16956 Set_Size_Info (Implicit_Base, Base_Typ);
16957 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
16958 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
16959 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
16960 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
16961
16962 Set_Ekind (T, E_Floating_Point_Subtype);
16963 Set_Etype (T, Implicit_Base);
16964 Set_Size_Info (T, Implicit_Base);
16965 Set_RM_Size (T, RM_Size (Implicit_Base));
16966 Inherit_Rep_Item_Chain (T, Implicit_Base);
16967 Set_Digits_Value (T, Digs_Val);
16968 end Floating_Point_Type_Declaration;
16969
16970 ----------------------------
16971 -- Get_Discriminant_Value --
16972 ----------------------------
16973
16974 -- This is the situation:
16975
16976 -- There is a non-derived type
16977
16978 -- type T0 (Dx, Dy, Dz...)
16979
16980 -- There are zero or more levels of derivation, with each derivation
16981 -- either purely inheriting the discriminants, or defining its own.
16982
16983 -- type Ti is new Ti-1
16984 -- or
16985 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
16986 -- or
16987 -- subtype Ti is ...
16988
16989 -- The subtype issue is avoided by the use of Original_Record_Component,
16990 -- and the fact that derived subtypes also derive the constraints.
16991
16992 -- This chain leads back from
16993
16994 -- Typ_For_Constraint
16995
16996 -- Typ_For_Constraint has discriminants, and the value for each
16997 -- discriminant is given by its corresponding Elmt of Constraints.
16998
16999 -- Discriminant is some discriminant in this hierarchy
17000
17001 -- We need to return its value
17002
17003 -- We do this by recursively searching each level, and looking for
17004 -- Discriminant. Once we get to the bottom, we start backing up
17005 -- returning the value for it which may in turn be a discriminant
17006 -- further up, so on the backup we continue the substitution.
17007
17008 function Get_Discriminant_Value
17009 (Discriminant : Entity_Id;
17010 Typ_For_Constraint : Entity_Id;
17011 Constraint : Elist_Id) return Node_Id
17012 is
17013 function Root_Corresponding_Discriminant
17014 (Discr : Entity_Id) return Entity_Id;
17015 -- Given a discriminant, traverse the chain of inherited discriminants
17016 -- and return the topmost discriminant.
17017
17018 function Search_Derivation_Levels
17019 (Ti : Entity_Id;
17020 Discrim_Values : Elist_Id;
17021 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17022 -- This is the routine that performs the recursive search of levels
17023 -- as described above.
17024
17025 -------------------------------------
17026 -- Root_Corresponding_Discriminant --
17027 -------------------------------------
17028
17029 function Root_Corresponding_Discriminant
17030 (Discr : Entity_Id) return Entity_Id
17031 is
17032 D : Entity_Id;
17033
17034 begin
17035 D := Discr;
17036 while Present (Corresponding_Discriminant (D)) loop
17037 D := Corresponding_Discriminant (D);
17038 end loop;
17039
17040 return D;
17041 end Root_Corresponding_Discriminant;
17042
17043 ------------------------------
17044 -- Search_Derivation_Levels --
17045 ------------------------------
17046
17047 function Search_Derivation_Levels
17048 (Ti : Entity_Id;
17049 Discrim_Values : Elist_Id;
17050 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17051 is
17052 Assoc : Elmt_Id;
17053 Disc : Entity_Id;
17054 Result : Node_Or_Entity_Id;
17055 Result_Entity : Node_Id;
17056
17057 begin
17058 -- If inappropriate type, return Error, this happens only in
17059 -- cascaded error situations, and we want to avoid a blow up.
17060
17061 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17062 return Error;
17063 end if;
17064
17065 -- Look deeper if possible. Use Stored_Constraints only for
17066 -- untagged types. For tagged types use the given constraint.
17067 -- This asymmetry needs explanation???
17068
17069 if not Stored_Discrim_Values
17070 and then Present (Stored_Constraint (Ti))
17071 and then not Is_Tagged_Type (Ti)
17072 then
17073 Result :=
17074 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17075 else
17076 declare
17077 Td : constant Entity_Id := Etype (Ti);
17078
17079 begin
17080 if Td = Ti then
17081 Result := Discriminant;
17082
17083 else
17084 if Present (Stored_Constraint (Ti)) then
17085 Result :=
17086 Search_Derivation_Levels
17087 (Td, Stored_Constraint (Ti), True);
17088 else
17089 Result :=
17090 Search_Derivation_Levels
17091 (Td, Discrim_Values, Stored_Discrim_Values);
17092 end if;
17093 end if;
17094 end;
17095 end if;
17096
17097 -- Extra underlying places to search, if not found above. For
17098 -- concurrent types, the relevant discriminant appears in the
17099 -- corresponding record. For a type derived from a private type
17100 -- without discriminant, the full view inherits the discriminants
17101 -- of the full view of the parent.
17102
17103 if Result = Discriminant then
17104 if Is_Concurrent_Type (Ti)
17105 and then Present (Corresponding_Record_Type (Ti))
17106 then
17107 Result :=
17108 Search_Derivation_Levels (
17109 Corresponding_Record_Type (Ti),
17110 Discrim_Values,
17111 Stored_Discrim_Values);
17112
17113 elsif Is_Private_Type (Ti)
17114 and then not Has_Discriminants (Ti)
17115 and then Present (Full_View (Ti))
17116 and then Etype (Full_View (Ti)) /= Ti
17117 then
17118 Result :=
17119 Search_Derivation_Levels (
17120 Full_View (Ti),
17121 Discrim_Values,
17122 Stored_Discrim_Values);
17123 end if;
17124 end if;
17125
17126 -- If Result is not a (reference to a) discriminant, return it,
17127 -- otherwise set Result_Entity to the discriminant.
17128
17129 if Nkind (Result) = N_Defining_Identifier then
17130 pragma Assert (Result = Discriminant);
17131 Result_Entity := Result;
17132
17133 else
17134 if not Denotes_Discriminant (Result) then
17135 return Result;
17136 end if;
17137
17138 Result_Entity := Entity (Result);
17139 end if;
17140
17141 -- See if this level of derivation actually has discriminants because
17142 -- tagged derivations can add them, hence the lower levels need not
17143 -- have any.
17144
17145 if not Has_Discriminants (Ti) then
17146 return Result;
17147 end if;
17148
17149 -- Scan Ti's discriminants for Result_Entity, and return its
17150 -- corresponding value, if any.
17151
17152 Result_Entity := Original_Record_Component (Result_Entity);
17153
17154 Assoc := First_Elmt (Discrim_Values);
17155
17156 if Stored_Discrim_Values then
17157 Disc := First_Stored_Discriminant (Ti);
17158 else
17159 Disc := First_Discriminant (Ti);
17160 end if;
17161
17162 while Present (Disc) loop
17163 pragma Assert (Present (Assoc));
17164
17165 if Original_Record_Component (Disc) = Result_Entity then
17166 return Node (Assoc);
17167 end if;
17168
17169 Next_Elmt (Assoc);
17170
17171 if Stored_Discrim_Values then
17172 Next_Stored_Discriminant (Disc);
17173 else
17174 Next_Discriminant (Disc);
17175 end if;
17176 end loop;
17177
17178 -- Could not find it
17179
17180 return Result;
17181 end Search_Derivation_Levels;
17182
17183 -- Local Variables
17184
17185 Result : Node_Or_Entity_Id;
17186
17187 -- Start of processing for Get_Discriminant_Value
17188
17189 begin
17190 -- ??? This routine is a gigantic mess and will be deleted. For the
17191 -- time being just test for the trivial case before calling recurse.
17192
17193 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17194 declare
17195 D : Entity_Id;
17196 E : Elmt_Id;
17197
17198 begin
17199 D := First_Discriminant (Typ_For_Constraint);
17200 E := First_Elmt (Constraint);
17201 while Present (D) loop
17202 if Chars (D) = Chars (Discriminant) then
17203 return Node (E);
17204 end if;
17205
17206 Next_Discriminant (D);
17207 Next_Elmt (E);
17208 end loop;
17209 end;
17210 end if;
17211
17212 Result := Search_Derivation_Levels
17213 (Typ_For_Constraint, Constraint, False);
17214
17215 -- ??? hack to disappear when this routine is gone
17216
17217 if Nkind (Result) = N_Defining_Identifier then
17218 declare
17219 D : Entity_Id;
17220 E : Elmt_Id;
17221
17222 begin
17223 D := First_Discriminant (Typ_For_Constraint);
17224 E := First_Elmt (Constraint);
17225 while Present (D) loop
17226 if Root_Corresponding_Discriminant (D) = Discriminant then
17227 return Node (E);
17228 end if;
17229
17230 Next_Discriminant (D);
17231 Next_Elmt (E);
17232 end loop;
17233 end;
17234 end if;
17235
17236 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17237 return Result;
17238 end Get_Discriminant_Value;
17239
17240 --------------------------
17241 -- Has_Range_Constraint --
17242 --------------------------
17243
17244 function Has_Range_Constraint (N : Node_Id) return Boolean is
17245 C : constant Node_Id := Constraint (N);
17246
17247 begin
17248 if Nkind (C) = N_Range_Constraint then
17249 return True;
17250
17251 elsif Nkind (C) = N_Digits_Constraint then
17252 return
17253 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17254 or else Present (Range_Constraint (C));
17255
17256 elsif Nkind (C) = N_Delta_Constraint then
17257 return Present (Range_Constraint (C));
17258
17259 else
17260 return False;
17261 end if;
17262 end Has_Range_Constraint;
17263
17264 ------------------------
17265 -- Inherit_Components --
17266 ------------------------
17267
17268 function Inherit_Components
17269 (N : Node_Id;
17270 Parent_Base : Entity_Id;
17271 Derived_Base : Entity_Id;
17272 Is_Tagged : Boolean;
17273 Inherit_Discr : Boolean;
17274 Discs : Elist_Id) return Elist_Id
17275 is
17276 Assoc_List : constant Elist_Id := New_Elmt_List;
17277
17278 procedure Inherit_Component
17279 (Old_C : Entity_Id;
17280 Plain_Discrim : Boolean := False;
17281 Stored_Discrim : Boolean := False);
17282 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17283 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17284 -- True, Old_C is a stored discriminant. If they are both false then
17285 -- Old_C is a regular component.
17286
17287 -----------------------
17288 -- Inherit_Component --
17289 -----------------------
17290
17291 procedure Inherit_Component
17292 (Old_C : Entity_Id;
17293 Plain_Discrim : Boolean := False;
17294 Stored_Discrim : Boolean := False)
17295 is
17296 procedure Set_Anonymous_Type (Id : Entity_Id);
17297 -- Id denotes the entity of an access discriminant or anonymous
17298 -- access component. Set the type of Id to either the same type of
17299 -- Old_C or create a new one depending on whether the parent and
17300 -- the child types are in the same scope.
17301
17302 ------------------------
17303 -- Set_Anonymous_Type --
17304 ------------------------
17305
17306 procedure Set_Anonymous_Type (Id : Entity_Id) is
17307 Old_Typ : constant Entity_Id := Etype (Old_C);
17308
17309 begin
17310 if Scope (Parent_Base) = Scope (Derived_Base) then
17311 Set_Etype (Id, Old_Typ);
17312
17313 -- The parent and the derived type are in two different scopes.
17314 -- Reuse the type of the original discriminant / component by
17315 -- copying it in order to preserve all attributes.
17316
17317 else
17318 declare
17319 Typ : constant Entity_Id := New_Copy (Old_Typ);
17320
17321 begin
17322 Set_Etype (Id, Typ);
17323
17324 -- Since we do not generate component declarations for
17325 -- inherited components, associate the itype with the
17326 -- derived type.
17327
17328 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17329 Set_Scope (Typ, Derived_Base);
17330 end;
17331 end if;
17332 end Set_Anonymous_Type;
17333
17334 -- Local variables and constants
17335
17336 New_C : constant Entity_Id := New_Copy (Old_C);
17337
17338 Corr_Discrim : Entity_Id;
17339 Discrim : Entity_Id;
17340
17341 -- Start of processing for Inherit_Component
17342
17343 begin
17344 pragma Assert (not Is_Tagged or not Stored_Discrim);
17345
17346 Set_Parent (New_C, Parent (Old_C));
17347
17348 -- Regular discriminants and components must be inserted in the scope
17349 -- of the Derived_Base. Do it here.
17350
17351 if not Stored_Discrim then
17352 Enter_Name (New_C);
17353 end if;
17354
17355 -- For tagged types the Original_Record_Component must point to
17356 -- whatever this field was pointing to in the parent type. This has
17357 -- already been achieved by the call to New_Copy above.
17358
17359 if not Is_Tagged then
17360 Set_Original_Record_Component (New_C, New_C);
17361 end if;
17362
17363 -- Set the proper type of an access discriminant
17364
17365 if Ekind (New_C) = E_Discriminant
17366 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17367 then
17368 Set_Anonymous_Type (New_C);
17369 end if;
17370
17371 -- If we have inherited a component then see if its Etype contains
17372 -- references to Parent_Base discriminants. In this case, replace
17373 -- these references with the constraints given in Discs. We do not
17374 -- do this for the partial view of private types because this is
17375 -- not needed (only the components of the full view will be used
17376 -- for code generation) and cause problem. We also avoid this
17377 -- transformation in some error situations.
17378
17379 if Ekind (New_C) = E_Component then
17380
17381 -- Set the proper type of an anonymous access component
17382
17383 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17384 Set_Anonymous_Type (New_C);
17385
17386 elsif (Is_Private_Type (Derived_Base)
17387 and then not Is_Generic_Type (Derived_Base))
17388 or else (Is_Empty_Elmt_List (Discs)
17389 and then not Expander_Active)
17390 then
17391 Set_Etype (New_C, Etype (Old_C));
17392
17393 else
17394 -- The current component introduces a circularity of the
17395 -- following kind:
17396
17397 -- limited with Pack_2;
17398 -- package Pack_1 is
17399 -- type T_1 is tagged record
17400 -- Comp : access Pack_2.T_2;
17401 -- ...
17402 -- end record;
17403 -- end Pack_1;
17404
17405 -- with Pack_1;
17406 -- package Pack_2 is
17407 -- type T_2 is new Pack_1.T_1 with ...;
17408 -- end Pack_2;
17409
17410 Set_Etype
17411 (New_C,
17412 Constrain_Component_Type
17413 (Old_C, Derived_Base, N, Parent_Base, Discs));
17414 end if;
17415 end if;
17416
17417 -- In derived tagged types it is illegal to reference a non
17418 -- discriminant component in the parent type. To catch this, mark
17419 -- these components with an Ekind of E_Void. This will be reset in
17420 -- Record_Type_Definition after processing the record extension of
17421 -- the derived type.
17422
17423 -- If the declaration is a private extension, there is no further
17424 -- record extension to process, and the components retain their
17425 -- current kind, because they are visible at this point.
17426
17427 if Is_Tagged and then Ekind (New_C) = E_Component
17428 and then Nkind (N) /= N_Private_Extension_Declaration
17429 then
17430 Set_Ekind (New_C, E_Void);
17431 end if;
17432
17433 if Plain_Discrim then
17434 Set_Corresponding_Discriminant (New_C, Old_C);
17435 Build_Discriminal (New_C);
17436
17437 -- If we are explicitly inheriting a stored discriminant it will be
17438 -- completely hidden.
17439
17440 elsif Stored_Discrim then
17441 Set_Corresponding_Discriminant (New_C, Empty);
17442 Set_Discriminal (New_C, Empty);
17443 Set_Is_Completely_Hidden (New_C);
17444
17445 -- Set the Original_Record_Component of each discriminant in the
17446 -- derived base to point to the corresponding stored that we just
17447 -- created.
17448
17449 Discrim := First_Discriminant (Derived_Base);
17450 while Present (Discrim) loop
17451 Corr_Discrim := Corresponding_Discriminant (Discrim);
17452
17453 -- Corr_Discrim could be missing in an error situation
17454
17455 if Present (Corr_Discrim)
17456 and then Original_Record_Component (Corr_Discrim) = Old_C
17457 then
17458 Set_Original_Record_Component (Discrim, New_C);
17459 end if;
17460
17461 Next_Discriminant (Discrim);
17462 end loop;
17463
17464 Append_Entity (New_C, Derived_Base);
17465 end if;
17466
17467 if not Is_Tagged then
17468 Append_Elmt (Old_C, Assoc_List);
17469 Append_Elmt (New_C, Assoc_List);
17470 end if;
17471 end Inherit_Component;
17472
17473 -- Variables local to Inherit_Component
17474
17475 Loc : constant Source_Ptr := Sloc (N);
17476
17477 Parent_Discrim : Entity_Id;
17478 Stored_Discrim : Entity_Id;
17479 D : Entity_Id;
17480 Component : Entity_Id;
17481
17482 -- Start of processing for Inherit_Components
17483
17484 begin
17485 if not Is_Tagged then
17486 Append_Elmt (Parent_Base, Assoc_List);
17487 Append_Elmt (Derived_Base, Assoc_List);
17488 end if;
17489
17490 -- Inherit parent discriminants if needed
17491
17492 if Inherit_Discr then
17493 Parent_Discrim := First_Discriminant (Parent_Base);
17494 while Present (Parent_Discrim) loop
17495 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17496 Next_Discriminant (Parent_Discrim);
17497 end loop;
17498 end if;
17499
17500 -- Create explicit stored discrims for untagged types when necessary
17501
17502 if not Has_Unknown_Discriminants (Derived_Base)
17503 and then Has_Discriminants (Parent_Base)
17504 and then not Is_Tagged
17505 and then
17506 (not Inherit_Discr
17507 or else First_Discriminant (Parent_Base) /=
17508 First_Stored_Discriminant (Parent_Base))
17509 then
17510 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17511 while Present (Stored_Discrim) loop
17512 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17513 Next_Stored_Discriminant (Stored_Discrim);
17514 end loop;
17515 end if;
17516
17517 -- See if we can apply the second transformation for derived types, as
17518 -- explained in point 6. in the comments above Build_Derived_Record_Type
17519 -- This is achieved by appending Derived_Base discriminants into Discs,
17520 -- which has the side effect of returning a non empty Discs list to the
17521 -- caller of Inherit_Components, which is what we want. This must be
17522 -- done for private derived types if there are explicit stored
17523 -- discriminants, to ensure that we can retrieve the values of the
17524 -- constraints provided in the ancestors.
17525
17526 if Inherit_Discr
17527 and then Is_Empty_Elmt_List (Discs)
17528 and then Present (First_Discriminant (Derived_Base))
17529 and then
17530 (not Is_Private_Type (Derived_Base)
17531 or else Is_Completely_Hidden
17532 (First_Stored_Discriminant (Derived_Base))
17533 or else Is_Generic_Type (Derived_Base))
17534 then
17535 D := First_Discriminant (Derived_Base);
17536 while Present (D) loop
17537 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17538 Next_Discriminant (D);
17539 end loop;
17540 end if;
17541
17542 -- Finally, inherit non-discriminant components unless they are not
17543 -- visible because defined or inherited from the full view of the
17544 -- parent. Don't inherit the _parent field of the parent type.
17545
17546 Component := First_Entity (Parent_Base);
17547 while Present (Component) loop
17548
17549 -- Ada 2005 (AI-251): Do not inherit components associated with
17550 -- secondary tags of the parent.
17551
17552 if Ekind (Component) = E_Component
17553 and then Present (Related_Type (Component))
17554 then
17555 null;
17556
17557 elsif Ekind (Component) /= E_Component
17558 or else Chars (Component) = Name_uParent
17559 then
17560 null;
17561
17562 -- If the derived type is within the parent type's declarative
17563 -- region, then the components can still be inherited even though
17564 -- they aren't visible at this point. This can occur for cases
17565 -- such as within public child units where the components must
17566 -- become visible upon entering the child unit's private part.
17567
17568 elsif not Is_Visible_Component (Component)
17569 and then not In_Open_Scopes (Scope (Parent_Base))
17570 then
17571 null;
17572
17573 elsif Ekind_In (Derived_Base, E_Private_Type,
17574 E_Limited_Private_Type)
17575 then
17576 null;
17577
17578 else
17579 Inherit_Component (Component);
17580 end if;
17581
17582 Next_Entity (Component);
17583 end loop;
17584
17585 -- For tagged derived types, inherited discriminants cannot be used in
17586 -- component declarations of the record extension part. To achieve this
17587 -- we mark the inherited discriminants as not visible.
17588
17589 if Is_Tagged and then Inherit_Discr then
17590 D := First_Discriminant (Derived_Base);
17591 while Present (D) loop
17592 Set_Is_Immediately_Visible (D, False);
17593 Next_Discriminant (D);
17594 end loop;
17595 end if;
17596
17597 return Assoc_List;
17598 end Inherit_Components;
17599
17600 -----------------------------
17601 -- Inherit_Predicate_Flags --
17602 -----------------------------
17603
17604 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17605 begin
17606 Set_Has_Predicates (Subt, Has_Predicates (Par));
17607 Set_Has_Static_Predicate_Aspect
17608 (Subt, Has_Static_Predicate_Aspect (Par));
17609 Set_Has_Dynamic_Predicate_Aspect
17610 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17611 end Inherit_Predicate_Flags;
17612
17613 ----------------------
17614 -- Is_EVF_Procedure --
17615 ----------------------
17616
17617 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17618 Formal : Entity_Id;
17619
17620 begin
17621 -- Examine the formals of an Extensions_Visible False procedure looking
17622 -- for a controlling OUT parameter.
17623
17624 if Ekind (Subp) = E_Procedure
17625 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17626 then
17627 Formal := First_Formal (Subp);
17628 while Present (Formal) loop
17629 if Ekind (Formal) = E_Out_Parameter
17630 and then Is_Controlling_Formal (Formal)
17631 then
17632 return True;
17633 end if;
17634
17635 Next_Formal (Formal);
17636 end loop;
17637 end if;
17638
17639 return False;
17640 end Is_EVF_Procedure;
17641
17642 -----------------------
17643 -- Is_Null_Extension --
17644 -----------------------
17645
17646 function Is_Null_Extension (T : Entity_Id) return Boolean is
17647 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17648 Comp_List : Node_Id;
17649 Comp : Node_Id;
17650
17651 begin
17652 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17653 or else not Is_Tagged_Type (T)
17654 or else Nkind (Type_Definition (Type_Decl)) /=
17655 N_Derived_Type_Definition
17656 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17657 then
17658 return False;
17659 end if;
17660
17661 Comp_List :=
17662 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17663
17664 if Present (Discriminant_Specifications (Type_Decl)) then
17665 return False;
17666
17667 elsif Present (Comp_List)
17668 and then Is_Non_Empty_List (Component_Items (Comp_List))
17669 then
17670 Comp := First (Component_Items (Comp_List));
17671
17672 -- Only user-defined components are relevant. The component list
17673 -- may also contain a parent component and internal components
17674 -- corresponding to secondary tags, but these do not determine
17675 -- whether this is a null extension.
17676
17677 while Present (Comp) loop
17678 if Comes_From_Source (Comp) then
17679 return False;
17680 end if;
17681
17682 Next (Comp);
17683 end loop;
17684
17685 return True;
17686
17687 else
17688 return True;
17689 end if;
17690 end Is_Null_Extension;
17691
17692 ------------------------------
17693 -- Is_Valid_Constraint_Kind --
17694 ------------------------------
17695
17696 function Is_Valid_Constraint_Kind
17697 (T_Kind : Type_Kind;
17698 Constraint_Kind : Node_Kind) return Boolean
17699 is
17700 begin
17701 case T_Kind is
17702 when Enumeration_Kind |
17703 Integer_Kind =>
17704 return Constraint_Kind = N_Range_Constraint;
17705
17706 when Decimal_Fixed_Point_Kind =>
17707 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17708 N_Range_Constraint);
17709
17710 when Ordinary_Fixed_Point_Kind =>
17711 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
17712 N_Range_Constraint);
17713
17714 when Float_Kind =>
17715 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17716 N_Range_Constraint);
17717
17718 when Access_Kind |
17719 Array_Kind |
17720 E_Record_Type |
17721 E_Record_Subtype |
17722 Class_Wide_Kind |
17723 E_Incomplete_Type |
17724 Private_Kind |
17725 Concurrent_Kind =>
17726 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
17727
17728 when others =>
17729 return True; -- Error will be detected later
17730 end case;
17731 end Is_Valid_Constraint_Kind;
17732
17733 --------------------------
17734 -- Is_Visible_Component --
17735 --------------------------
17736
17737 function Is_Visible_Component
17738 (C : Entity_Id;
17739 N : Node_Id := Empty) return Boolean
17740 is
17741 Original_Comp : Entity_Id := Empty;
17742 Original_Scope : Entity_Id;
17743 Type_Scope : Entity_Id;
17744
17745 function Is_Local_Type (Typ : Entity_Id) return Boolean;
17746 -- Check whether parent type of inherited component is declared locally,
17747 -- possibly within a nested package or instance. The current scope is
17748 -- the derived record itself.
17749
17750 -------------------
17751 -- Is_Local_Type --
17752 -------------------
17753
17754 function Is_Local_Type (Typ : Entity_Id) return Boolean is
17755 Scop : Entity_Id;
17756
17757 begin
17758 Scop := Scope (Typ);
17759 while Present (Scop)
17760 and then Scop /= Standard_Standard
17761 loop
17762 if Scop = Scope (Current_Scope) then
17763 return True;
17764 end if;
17765
17766 Scop := Scope (Scop);
17767 end loop;
17768
17769 return False;
17770 end Is_Local_Type;
17771
17772 -- Start of processing for Is_Visible_Component
17773
17774 begin
17775 if Ekind_In (C, E_Component, E_Discriminant) then
17776 Original_Comp := Original_Record_Component (C);
17777 end if;
17778
17779 if No (Original_Comp) then
17780
17781 -- Premature usage, or previous error
17782
17783 return False;
17784
17785 else
17786 Original_Scope := Scope (Original_Comp);
17787 Type_Scope := Scope (Base_Type (Scope (C)));
17788 end if;
17789
17790 -- This test only concerns tagged types
17791
17792 if not Is_Tagged_Type (Original_Scope) then
17793 return True;
17794
17795 -- If it is _Parent or _Tag, there is no visibility issue
17796
17797 elsif not Comes_From_Source (Original_Comp) then
17798 return True;
17799
17800 -- Discriminants are visible unless the (private) type has unknown
17801 -- discriminants. If the discriminant reference is inserted for a
17802 -- discriminant check on a full view it is also visible.
17803
17804 elsif Ekind (Original_Comp) = E_Discriminant
17805 and then
17806 (not Has_Unknown_Discriminants (Original_Scope)
17807 or else (Present (N)
17808 and then Nkind (N) = N_Selected_Component
17809 and then Nkind (Prefix (N)) = N_Type_Conversion
17810 and then not Comes_From_Source (Prefix (N))))
17811 then
17812 return True;
17813
17814 -- In the body of an instantiation, no need to check for the visibility
17815 -- of a component.
17816
17817 elsif In_Instance_Body then
17818 return True;
17819
17820 -- If the component has been declared in an ancestor which is currently
17821 -- a private type, then it is not visible. The same applies if the
17822 -- component's containing type is not in an open scope and the original
17823 -- component's enclosing type is a visible full view of a private type
17824 -- (which can occur in cases where an attempt is being made to reference
17825 -- a component in a sibling package that is inherited from a visible
17826 -- component of a type in an ancestor package; the component in the
17827 -- sibling package should not be visible even though the component it
17828 -- inherited from is visible). This does not apply however in the case
17829 -- where the scope of the type is a private child unit, or when the
17830 -- parent comes from a local package in which the ancestor is currently
17831 -- visible. The latter suppression of visibility is needed for cases
17832 -- that are tested in B730006.
17833
17834 elsif Is_Private_Type (Original_Scope)
17835 or else
17836 (not Is_Private_Descendant (Type_Scope)
17837 and then not In_Open_Scopes (Type_Scope)
17838 and then Has_Private_Declaration (Original_Scope))
17839 then
17840 -- If the type derives from an entity in a formal package, there
17841 -- are no additional visible components.
17842
17843 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
17844 N_Formal_Package_Declaration
17845 then
17846 return False;
17847
17848 -- if we are not in the private part of the current package, there
17849 -- are no additional visible components.
17850
17851 elsif Ekind (Scope (Current_Scope)) = E_Package
17852 and then not In_Private_Part (Scope (Current_Scope))
17853 then
17854 return False;
17855 else
17856 return
17857 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
17858 and then In_Open_Scopes (Scope (Original_Scope))
17859 and then Is_Local_Type (Type_Scope);
17860 end if;
17861
17862 -- There is another weird way in which a component may be invisible when
17863 -- the private and the full view are not derived from the same ancestor.
17864 -- Here is an example :
17865
17866 -- type A1 is tagged record F1 : integer; end record;
17867 -- type A2 is new A1 with record F2 : integer; end record;
17868 -- type T is new A1 with private;
17869 -- private
17870 -- type T is new A2 with null record;
17871
17872 -- In this case, the full view of T inherits F1 and F2 but the private
17873 -- view inherits only F1
17874
17875 else
17876 declare
17877 Ancestor : Entity_Id := Scope (C);
17878
17879 begin
17880 loop
17881 if Ancestor = Original_Scope then
17882 return True;
17883 elsif Ancestor = Etype (Ancestor) then
17884 return False;
17885 end if;
17886
17887 Ancestor := Etype (Ancestor);
17888 end loop;
17889 end;
17890 end if;
17891 end Is_Visible_Component;
17892
17893 --------------------------
17894 -- Make_Class_Wide_Type --
17895 --------------------------
17896
17897 procedure Make_Class_Wide_Type (T : Entity_Id) is
17898 CW_Type : Entity_Id;
17899 CW_Name : Name_Id;
17900 Next_E : Entity_Id;
17901
17902 begin
17903 if Present (Class_Wide_Type (T)) then
17904
17905 -- The class-wide type is a partially decorated entity created for a
17906 -- unanalyzed tagged type referenced through a limited with clause.
17907 -- When the tagged type is analyzed, its class-wide type needs to be
17908 -- redecorated. Note that we reuse the entity created by Decorate_
17909 -- Tagged_Type in order to preserve all links.
17910
17911 if Materialize_Entity (Class_Wide_Type (T)) then
17912 CW_Type := Class_Wide_Type (T);
17913 Set_Materialize_Entity (CW_Type, False);
17914
17915 -- The class wide type can have been defined by the partial view, in
17916 -- which case everything is already done.
17917
17918 else
17919 return;
17920 end if;
17921
17922 -- Default case, we need to create a new class-wide type
17923
17924 else
17925 CW_Type :=
17926 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
17927 end if;
17928
17929 -- Inherit root type characteristics
17930
17931 CW_Name := Chars (CW_Type);
17932 Next_E := Next_Entity (CW_Type);
17933 Copy_Node (T, CW_Type);
17934 Set_Comes_From_Source (CW_Type, False);
17935 Set_Chars (CW_Type, CW_Name);
17936 Set_Parent (CW_Type, Parent (T));
17937 Set_Next_Entity (CW_Type, Next_E);
17938
17939 -- Ensure we have a new freeze node for the class-wide type. The partial
17940 -- view may have freeze action of its own, requiring a proper freeze
17941 -- node, and the same freeze node cannot be shared between the two
17942 -- types.
17943
17944 Set_Has_Delayed_Freeze (CW_Type);
17945 Set_Freeze_Node (CW_Type, Empty);
17946
17947 -- Customize the class-wide type: It has no prim. op., it cannot be
17948 -- abstract and its Etype points back to the specific root type.
17949
17950 Set_Ekind (CW_Type, E_Class_Wide_Type);
17951 Set_Is_Tagged_Type (CW_Type, True);
17952 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
17953 Set_Is_Abstract_Type (CW_Type, False);
17954 Set_Is_Constrained (CW_Type, False);
17955 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
17956 Set_Default_SSO (CW_Type);
17957
17958 if Ekind (T) = E_Class_Wide_Subtype then
17959 Set_Etype (CW_Type, Etype (Base_Type (T)));
17960 else
17961 Set_Etype (CW_Type, T);
17962 end if;
17963
17964 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
17965
17966 -- If this is the class_wide type of a constrained subtype, it does
17967 -- not have discriminants.
17968
17969 Set_Has_Discriminants (CW_Type,
17970 Has_Discriminants (T) and then not Is_Constrained (T));
17971
17972 Set_Has_Unknown_Discriminants (CW_Type, True);
17973 Set_Class_Wide_Type (T, CW_Type);
17974 Set_Equivalent_Type (CW_Type, Empty);
17975
17976 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
17977
17978 Set_Class_Wide_Type (CW_Type, CW_Type);
17979 end Make_Class_Wide_Type;
17980
17981 ----------------
17982 -- Make_Index --
17983 ----------------
17984
17985 procedure Make_Index
17986 (N : Node_Id;
17987 Related_Nod : Node_Id;
17988 Related_Id : Entity_Id := Empty;
17989 Suffix_Index : Nat := 1;
17990 In_Iter_Schm : Boolean := False)
17991 is
17992 R : Node_Id;
17993 T : Entity_Id;
17994 Def_Id : Entity_Id := Empty;
17995 Found : Boolean := False;
17996
17997 begin
17998 -- For a discrete range used in a constrained array definition and
17999 -- defined by a range, an implicit conversion to the predefined type
18000 -- INTEGER is assumed if each bound is either a numeric literal, a named
18001 -- number, or an attribute, and the type of both bounds (prior to the
18002 -- implicit conversion) is the type universal_integer. Otherwise, both
18003 -- bounds must be of the same discrete type, other than universal
18004 -- integer; this type must be determinable independently of the
18005 -- context, but using the fact that the type must be discrete and that
18006 -- both bounds must have the same type.
18007
18008 -- Character literals also have a universal type in the absence of
18009 -- of additional context, and are resolved to Standard_Character.
18010
18011 if Nkind (N) = N_Range then
18012
18013 -- The index is given by a range constraint. The bounds are known
18014 -- to be of a consistent type.
18015
18016 if not Is_Overloaded (N) then
18017 T := Etype (N);
18018
18019 -- For universal bounds, choose the specific predefined type
18020
18021 if T = Universal_Integer then
18022 T := Standard_Integer;
18023
18024 elsif T = Any_Character then
18025 Ambiguous_Character (Low_Bound (N));
18026
18027 T := Standard_Character;
18028 end if;
18029
18030 -- The node may be overloaded because some user-defined operators
18031 -- are available, but if a universal interpretation exists it is
18032 -- also the selected one.
18033
18034 elsif Universal_Interpretation (N) = Universal_Integer then
18035 T := Standard_Integer;
18036
18037 else
18038 T := Any_Type;
18039
18040 declare
18041 Ind : Interp_Index;
18042 It : Interp;
18043
18044 begin
18045 Get_First_Interp (N, Ind, It);
18046 while Present (It.Typ) loop
18047 if Is_Discrete_Type (It.Typ) then
18048
18049 if Found
18050 and then not Covers (It.Typ, T)
18051 and then not Covers (T, It.Typ)
18052 then
18053 Error_Msg_N ("ambiguous bounds in discrete range", N);
18054 exit;
18055 else
18056 T := It.Typ;
18057 Found := True;
18058 end if;
18059 end if;
18060
18061 Get_Next_Interp (Ind, It);
18062 end loop;
18063
18064 if T = Any_Type then
18065 Error_Msg_N ("discrete type required for range", N);
18066 Set_Etype (N, Any_Type);
18067 return;
18068
18069 elsif T = Universal_Integer then
18070 T := Standard_Integer;
18071 end if;
18072 end;
18073 end if;
18074
18075 if not Is_Discrete_Type (T) then
18076 Error_Msg_N ("discrete type required for range", N);
18077 Set_Etype (N, Any_Type);
18078 return;
18079 end if;
18080
18081 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18082 and then Attribute_Name (Low_Bound (N)) = Name_First
18083 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18084 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18085 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18086 then
18087 -- The type of the index will be the type of the prefix, as long
18088 -- as the upper bound is 'Last of the same type.
18089
18090 Def_Id := Entity (Prefix (Low_Bound (N)));
18091
18092 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18093 or else Attribute_Name (High_Bound (N)) /= Name_Last
18094 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18095 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18096 then
18097 Def_Id := Empty;
18098 end if;
18099 end if;
18100
18101 R := N;
18102 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18103
18104 elsif Nkind (N) = N_Subtype_Indication then
18105
18106 -- The index is given by a subtype with a range constraint
18107
18108 T := Base_Type (Entity (Subtype_Mark (N)));
18109
18110 if not Is_Discrete_Type (T) then
18111 Error_Msg_N ("discrete type required for range", N);
18112 Set_Etype (N, Any_Type);
18113 return;
18114 end if;
18115
18116 R := Range_Expression (Constraint (N));
18117
18118 Resolve (R, T);
18119 Process_Range_Expr_In_Decl
18120 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18121
18122 elsif Nkind (N) = N_Attribute_Reference then
18123
18124 -- Catch beginner's error (use of attribute other than 'Range)
18125
18126 if Attribute_Name (N) /= Name_Range then
18127 Error_Msg_N ("expect attribute ''Range", N);
18128 Set_Etype (N, Any_Type);
18129 return;
18130 end if;
18131
18132 -- If the node denotes the range of a type mark, that is also the
18133 -- resulting type, and we do not need to create an Itype for it.
18134
18135 if Is_Entity_Name (Prefix (N))
18136 and then Comes_From_Source (N)
18137 and then Is_Type (Entity (Prefix (N)))
18138 and then Is_Discrete_Type (Entity (Prefix (N)))
18139 then
18140 Def_Id := Entity (Prefix (N));
18141 end if;
18142
18143 Analyze_And_Resolve (N);
18144 T := Etype (N);
18145 R := N;
18146
18147 -- If none of the above, must be a subtype. We convert this to a
18148 -- range attribute reference because in the case of declared first
18149 -- named subtypes, the types in the range reference can be different
18150 -- from the type of the entity. A range attribute normalizes the
18151 -- reference and obtains the correct types for the bounds.
18152
18153 -- This transformation is in the nature of an expansion, is only
18154 -- done if expansion is active. In particular, it is not done on
18155 -- formal generic types, because we need to retain the name of the
18156 -- original index for instantiation purposes.
18157
18158 else
18159 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18160 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18161 Set_Etype (N, Any_Integer);
18162 return;
18163
18164 else
18165 -- The type mark may be that of an incomplete type. It is only
18166 -- now that we can get the full view, previous analysis does
18167 -- not look specifically for a type mark.
18168
18169 Set_Entity (N, Get_Full_View (Entity (N)));
18170 Set_Etype (N, Entity (N));
18171 Def_Id := Entity (N);
18172
18173 if not Is_Discrete_Type (Def_Id) then
18174 Error_Msg_N ("discrete type required for index", N);
18175 Set_Etype (N, Any_Type);
18176 return;
18177 end if;
18178 end if;
18179
18180 if Expander_Active then
18181 Rewrite (N,
18182 Make_Attribute_Reference (Sloc (N),
18183 Attribute_Name => Name_Range,
18184 Prefix => Relocate_Node (N)));
18185
18186 -- The original was a subtype mark that does not freeze. This
18187 -- means that the rewritten version must not freeze either.
18188
18189 Set_Must_Not_Freeze (N);
18190 Set_Must_Not_Freeze (Prefix (N));
18191 Analyze_And_Resolve (N);
18192 T := Etype (N);
18193 R := N;
18194
18195 -- If expander is inactive, type is legal, nothing else to construct
18196
18197 else
18198 return;
18199 end if;
18200 end if;
18201
18202 if not Is_Discrete_Type (T) then
18203 Error_Msg_N ("discrete type required for range", N);
18204 Set_Etype (N, Any_Type);
18205 return;
18206
18207 elsif T = Any_Type then
18208 Set_Etype (N, Any_Type);
18209 return;
18210 end if;
18211
18212 -- We will now create the appropriate Itype to describe the range, but
18213 -- first a check. If we originally had a subtype, then we just label
18214 -- the range with this subtype. Not only is there no need to construct
18215 -- a new subtype, but it is wrong to do so for two reasons:
18216
18217 -- 1. A legality concern, if we have a subtype, it must not freeze,
18218 -- and the Itype would cause freezing incorrectly
18219
18220 -- 2. An efficiency concern, if we created an Itype, it would not be
18221 -- recognized as the same type for the purposes of eliminating
18222 -- checks in some circumstances.
18223
18224 -- We signal this case by setting the subtype entity in Def_Id
18225
18226 if No (Def_Id) then
18227 Def_Id :=
18228 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18229 Set_Etype (Def_Id, Base_Type (T));
18230
18231 if Is_Signed_Integer_Type (T) then
18232 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18233
18234 elsif Is_Modular_Integer_Type (T) then
18235 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18236
18237 else
18238 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18239 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18240 Set_First_Literal (Def_Id, First_Literal (T));
18241 end if;
18242
18243 Set_Size_Info (Def_Id, (T));
18244 Set_RM_Size (Def_Id, RM_Size (T));
18245 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18246
18247 Set_Scalar_Range (Def_Id, R);
18248 Conditional_Delay (Def_Id, T);
18249
18250 if Nkind (N) = N_Subtype_Indication then
18251 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18252 end if;
18253
18254 -- In the subtype indication case, if the immediate parent of the
18255 -- new subtype is non-static, then the subtype we create is non-
18256 -- static, even if its bounds are static.
18257
18258 if Nkind (N) = N_Subtype_Indication
18259 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18260 then
18261 Set_Is_Non_Static_Subtype (Def_Id);
18262 end if;
18263 end if;
18264
18265 -- Final step is to label the index with this constructed type
18266
18267 Set_Etype (N, Def_Id);
18268 end Make_Index;
18269
18270 ------------------------------
18271 -- Modular_Type_Declaration --
18272 ------------------------------
18273
18274 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18275 Mod_Expr : constant Node_Id := Expression (Def);
18276 M_Val : Uint;
18277
18278 procedure Set_Modular_Size (Bits : Int);
18279 -- Sets RM_Size to Bits, and Esize to normal word size above this
18280
18281 ----------------------
18282 -- Set_Modular_Size --
18283 ----------------------
18284
18285 procedure Set_Modular_Size (Bits : Int) is
18286 begin
18287 Set_RM_Size (T, UI_From_Int (Bits));
18288
18289 if Bits <= 8 then
18290 Init_Esize (T, 8);
18291
18292 elsif Bits <= 16 then
18293 Init_Esize (T, 16);
18294
18295 elsif Bits <= 32 then
18296 Init_Esize (T, 32);
18297
18298 else
18299 Init_Esize (T, System_Max_Binary_Modulus_Power);
18300 end if;
18301
18302 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18303 Set_Is_Known_Valid (T);
18304 end if;
18305 end Set_Modular_Size;
18306
18307 -- Start of processing for Modular_Type_Declaration
18308
18309 begin
18310 -- If the mod expression is (exactly) 2 * literal, where literal is
18311 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18312
18313 if Warn_On_Suspicious_Modulus_Value
18314 and then Nkind (Mod_Expr) = N_Op_Multiply
18315 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18316 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18317 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18318 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18319 then
18320 Error_Msg_N
18321 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18322 end if;
18323
18324 -- Proceed with analysis of mod expression
18325
18326 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18327 Set_Etype (T, T);
18328 Set_Ekind (T, E_Modular_Integer_Type);
18329 Init_Alignment (T);
18330 Set_Is_Constrained (T);
18331
18332 if not Is_OK_Static_Expression (Mod_Expr) then
18333 Flag_Non_Static_Expr
18334 ("non-static expression used for modular type bound!", Mod_Expr);
18335 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18336 else
18337 M_Val := Expr_Value (Mod_Expr);
18338 end if;
18339
18340 if M_Val < 1 then
18341 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18342 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18343 end if;
18344
18345 if M_Val > 2 ** Standard_Long_Integer_Size then
18346 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18347 end if;
18348
18349 Set_Modulus (T, M_Val);
18350
18351 -- Create bounds for the modular type based on the modulus given in
18352 -- the type declaration and then analyze and resolve those bounds.
18353
18354 Set_Scalar_Range (T,
18355 Make_Range (Sloc (Mod_Expr),
18356 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18357 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18358
18359 -- Properly analyze the literals for the range. We do this manually
18360 -- because we can't go calling Resolve, since we are resolving these
18361 -- bounds with the type, and this type is certainly not complete yet.
18362
18363 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18364 Set_Etype (High_Bound (Scalar_Range (T)), T);
18365 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18366 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18367
18368 -- Loop through powers of two to find number of bits required
18369
18370 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18371
18372 -- Binary case
18373
18374 if M_Val = 2 ** Bits then
18375 Set_Modular_Size (Bits);
18376 return;
18377
18378 -- Non-binary case
18379
18380 elsif M_Val < 2 ** Bits then
18381 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18382 Set_Non_Binary_Modulus (T);
18383
18384 if Bits > System_Max_Nonbinary_Modulus_Power then
18385 Error_Msg_Uint_1 :=
18386 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18387 Error_Msg_F
18388 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18389 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18390 return;
18391
18392 else
18393 -- In the non-binary case, set size as per RM 13.3(55)
18394
18395 Set_Modular_Size (Bits);
18396 return;
18397 end if;
18398 end if;
18399
18400 end loop;
18401
18402 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18403 -- so we just signal an error and set the maximum size.
18404
18405 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18406 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18407
18408 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18409 Init_Alignment (T);
18410
18411 end Modular_Type_Declaration;
18412
18413 --------------------------
18414 -- New_Concatenation_Op --
18415 --------------------------
18416
18417 procedure New_Concatenation_Op (Typ : Entity_Id) is
18418 Loc : constant Source_Ptr := Sloc (Typ);
18419 Op : Entity_Id;
18420
18421 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18422 -- Create abbreviated declaration for the formal of a predefined
18423 -- Operator 'Op' of type 'Typ'
18424
18425 --------------------
18426 -- Make_Op_Formal --
18427 --------------------
18428
18429 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18430 Formal : Entity_Id;
18431 begin
18432 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18433 Set_Etype (Formal, Typ);
18434 Set_Mechanism (Formal, Default_Mechanism);
18435 return Formal;
18436 end Make_Op_Formal;
18437
18438 -- Start of processing for New_Concatenation_Op
18439
18440 begin
18441 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18442
18443 Set_Ekind (Op, E_Operator);
18444 Set_Scope (Op, Current_Scope);
18445 Set_Etype (Op, Typ);
18446 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18447 Set_Is_Immediately_Visible (Op);
18448 Set_Is_Intrinsic_Subprogram (Op);
18449 Set_Has_Completion (Op);
18450 Append_Entity (Op, Current_Scope);
18451
18452 Set_Name_Entity_Id (Name_Op_Concat, Op);
18453
18454 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18455 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18456 end New_Concatenation_Op;
18457
18458 -------------------------
18459 -- OK_For_Limited_Init --
18460 -------------------------
18461
18462 -- ???Check all calls of this, and compare the conditions under which it's
18463 -- called.
18464
18465 function OK_For_Limited_Init
18466 (Typ : Entity_Id;
18467 Exp : Node_Id) return Boolean
18468 is
18469 begin
18470 return Is_CPP_Constructor_Call (Exp)
18471 or else (Ada_Version >= Ada_2005
18472 and then not Debug_Flag_Dot_L
18473 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18474 end OK_For_Limited_Init;
18475
18476 -------------------------------
18477 -- OK_For_Limited_Init_In_05 --
18478 -------------------------------
18479
18480 function OK_For_Limited_Init_In_05
18481 (Typ : Entity_Id;
18482 Exp : Node_Id) return Boolean
18483 is
18484 begin
18485 -- An object of a limited interface type can be initialized with any
18486 -- expression of a nonlimited descendant type.
18487
18488 if Is_Class_Wide_Type (Typ)
18489 and then Is_Limited_Interface (Typ)
18490 and then not Is_Limited_Type (Etype (Exp))
18491 then
18492 return True;
18493 end if;
18494
18495 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18496 -- case of limited aggregates (including extension aggregates), and
18497 -- function calls. The function call may have been given in prefixed
18498 -- notation, in which case the original node is an indexed component.
18499 -- If the function is parameterless, the original node was an explicit
18500 -- dereference. The function may also be parameterless, in which case
18501 -- the source node is just an identifier.
18502
18503 case Nkind (Original_Node (Exp)) is
18504 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18505 return True;
18506
18507 when N_Identifier =>
18508 return Present (Entity (Original_Node (Exp)))
18509 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18510
18511 when N_Qualified_Expression =>
18512 return
18513 OK_For_Limited_Init_In_05
18514 (Typ, Expression (Original_Node (Exp)));
18515
18516 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18517 -- with a function call, the expander has rewritten the call into an
18518 -- N_Type_Conversion node to force displacement of the pointer to
18519 -- reference the component containing the secondary dispatch table.
18520 -- Otherwise a type conversion is not a legal context.
18521 -- A return statement for a build-in-place function returning a
18522 -- synchronized type also introduces an unchecked conversion.
18523
18524 when N_Type_Conversion |
18525 N_Unchecked_Type_Conversion =>
18526 return not Comes_From_Source (Exp)
18527 and then
18528 OK_For_Limited_Init_In_05
18529 (Typ, Expression (Original_Node (Exp)));
18530
18531 when N_Indexed_Component |
18532 N_Selected_Component |
18533 N_Explicit_Dereference =>
18534 return Nkind (Exp) = N_Function_Call;
18535
18536 -- A use of 'Input is a function call, hence allowed. Normally the
18537 -- attribute will be changed to a call, but the attribute by itself
18538 -- can occur with -gnatc.
18539
18540 when N_Attribute_Reference =>
18541 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18542
18543 -- For a case expression, all dependent expressions must be legal
18544
18545 when N_Case_Expression =>
18546 declare
18547 Alt : Node_Id;
18548
18549 begin
18550 Alt := First (Alternatives (Original_Node (Exp)));
18551 while Present (Alt) loop
18552 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18553 return False;
18554 end if;
18555
18556 Next (Alt);
18557 end loop;
18558
18559 return True;
18560 end;
18561
18562 -- For an if expression, all dependent expressions must be legal
18563
18564 when N_If_Expression =>
18565 declare
18566 Then_Expr : constant Node_Id :=
18567 Next (First (Expressions (Original_Node (Exp))));
18568 Else_Expr : constant Node_Id := Next (Then_Expr);
18569 begin
18570 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18571 and then
18572 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18573 end;
18574
18575 when others =>
18576 return False;
18577 end case;
18578 end OK_For_Limited_Init_In_05;
18579
18580 -------------------------------------------
18581 -- Ordinary_Fixed_Point_Type_Declaration --
18582 -------------------------------------------
18583
18584 procedure Ordinary_Fixed_Point_Type_Declaration
18585 (T : Entity_Id;
18586 Def : Node_Id)
18587 is
18588 Loc : constant Source_Ptr := Sloc (Def);
18589 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18590 RRS : constant Node_Id := Real_Range_Specification (Def);
18591 Implicit_Base : Entity_Id;
18592 Delta_Val : Ureal;
18593 Small_Val : Ureal;
18594 Low_Val : Ureal;
18595 High_Val : Ureal;
18596
18597 begin
18598 Check_Restriction (No_Fixed_Point, Def);
18599
18600 -- Create implicit base type
18601
18602 Implicit_Base :=
18603 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18604 Set_Etype (Implicit_Base, Implicit_Base);
18605
18606 -- Analyze and process delta expression
18607
18608 Analyze_And_Resolve (Delta_Expr, Any_Real);
18609
18610 Check_Delta_Expression (Delta_Expr);
18611 Delta_Val := Expr_Value_R (Delta_Expr);
18612
18613 Set_Delta_Value (Implicit_Base, Delta_Val);
18614
18615 -- Compute default small from given delta, which is the largest power
18616 -- of two that does not exceed the given delta value.
18617
18618 declare
18619 Tmp : Ureal;
18620 Scale : Int;
18621
18622 begin
18623 Tmp := Ureal_1;
18624 Scale := 0;
18625
18626 if Delta_Val < Ureal_1 then
18627 while Delta_Val < Tmp loop
18628 Tmp := Tmp / Ureal_2;
18629 Scale := Scale + 1;
18630 end loop;
18631
18632 else
18633 loop
18634 Tmp := Tmp * Ureal_2;
18635 exit when Tmp > Delta_Val;
18636 Scale := Scale - 1;
18637 end loop;
18638 end if;
18639
18640 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
18641 end;
18642
18643 Set_Small_Value (Implicit_Base, Small_Val);
18644
18645 -- If no range was given, set a dummy range
18646
18647 if RRS <= Empty_Or_Error then
18648 Low_Val := -Small_Val;
18649 High_Val := Small_Val;
18650
18651 -- Otherwise analyze and process given range
18652
18653 else
18654 declare
18655 Low : constant Node_Id := Low_Bound (RRS);
18656 High : constant Node_Id := High_Bound (RRS);
18657
18658 begin
18659 Analyze_And_Resolve (Low, Any_Real);
18660 Analyze_And_Resolve (High, Any_Real);
18661 Check_Real_Bound (Low);
18662 Check_Real_Bound (High);
18663
18664 -- Obtain and set the range
18665
18666 Low_Val := Expr_Value_R (Low);
18667 High_Val := Expr_Value_R (High);
18668
18669 if Low_Val > High_Val then
18670 Error_Msg_NE ("??fixed point type& has null range", Def, T);
18671 end if;
18672 end;
18673 end if;
18674
18675 -- The range for both the implicit base and the declared first subtype
18676 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
18677 -- set a temporary range in place. Note that the bounds of the base
18678 -- type will be widened to be symmetrical and to fill the available
18679 -- bits when the type is frozen.
18680
18681 -- We could do this with all discrete types, and probably should, but
18682 -- we absolutely have to do it for fixed-point, since the end-points
18683 -- of the range and the size are determined by the small value, which
18684 -- could be reset before the freeze point.
18685
18686 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
18687 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
18688
18689 -- Complete definition of first subtype. The inheritance of the rep item
18690 -- chain ensures that SPARK-related pragmas are not clobbered when the
18691 -- ordinary fixed point type acts as a full view of a private type.
18692
18693 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
18694 Set_Etype (T, Implicit_Base);
18695 Init_Size_Align (T);
18696 Inherit_Rep_Item_Chain (T, Implicit_Base);
18697 Set_Small_Value (T, Small_Val);
18698 Set_Delta_Value (T, Delta_Val);
18699 Set_Is_Constrained (T);
18700 end Ordinary_Fixed_Point_Type_Declaration;
18701
18702 ----------------------------------
18703 -- Preanalyze_Assert_Expression --
18704 ----------------------------------
18705
18706 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
18707 begin
18708 In_Assertion_Expr := In_Assertion_Expr + 1;
18709 Preanalyze_Spec_Expression (N, T);
18710 In_Assertion_Expr := In_Assertion_Expr - 1;
18711 end Preanalyze_Assert_Expression;
18712
18713 -----------------------------------
18714 -- Preanalyze_Default_Expression --
18715 -----------------------------------
18716
18717 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
18718 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
18719 begin
18720 In_Default_Expr := True;
18721 Preanalyze_Spec_Expression (N, T);
18722 In_Default_Expr := Save_In_Default_Expr;
18723 end Preanalyze_Default_Expression;
18724
18725 --------------------------------
18726 -- Preanalyze_Spec_Expression --
18727 --------------------------------
18728
18729 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18730 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18731 begin
18732 In_Spec_Expression := True;
18733 Preanalyze_And_Resolve (N, T);
18734 In_Spec_Expression := Save_In_Spec_Expression;
18735 end Preanalyze_Spec_Expression;
18736
18737 ----------------------------------------
18738 -- Prepare_Private_Subtype_Completion --
18739 ----------------------------------------
18740
18741 procedure Prepare_Private_Subtype_Completion
18742 (Id : Entity_Id;
18743 Related_Nod : Node_Id)
18744 is
18745 Id_B : constant Entity_Id := Base_Type (Id);
18746 Full_B : Entity_Id := Full_View (Id_B);
18747 Full : Entity_Id;
18748
18749 begin
18750 if Present (Full_B) then
18751
18752 -- Get to the underlying full view if necessary
18753
18754 if Is_Private_Type (Full_B)
18755 and then Present (Underlying_Full_View (Full_B))
18756 then
18757 Full_B := Underlying_Full_View (Full_B);
18758 end if;
18759
18760 -- The Base_Type is already completed, we can complete the subtype
18761 -- now. We have to create a new entity with the same name, Thus we
18762 -- can't use Create_Itype.
18763
18764 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
18765 Set_Is_Itype (Full);
18766 Set_Associated_Node_For_Itype (Full, Related_Nod);
18767 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
18768 end if;
18769
18770 -- The parent subtype may be private, but the base might not, in some
18771 -- nested instances. In that case, the subtype does not need to be
18772 -- exchanged. It would still be nice to make private subtypes and their
18773 -- bases consistent at all times ???
18774
18775 if Is_Private_Type (Id_B) then
18776 Append_Elmt (Id, Private_Dependents (Id_B));
18777 end if;
18778 end Prepare_Private_Subtype_Completion;
18779
18780 ---------------------------
18781 -- Process_Discriminants --
18782 ---------------------------
18783
18784 procedure Process_Discriminants
18785 (N : Node_Id;
18786 Prev : Entity_Id := Empty)
18787 is
18788 Elist : constant Elist_Id := New_Elmt_List;
18789 Id : Node_Id;
18790 Discr : Node_Id;
18791 Discr_Number : Uint;
18792 Discr_Type : Entity_Id;
18793 Default_Present : Boolean := False;
18794 Default_Not_Present : Boolean := False;
18795
18796 begin
18797 -- A composite type other than an array type can have discriminants.
18798 -- On entry, the current scope is the composite type.
18799
18800 -- The discriminants are initially entered into the scope of the type
18801 -- via Enter_Name with the default Ekind of E_Void to prevent premature
18802 -- use, as explained at the end of this procedure.
18803
18804 Discr := First (Discriminant_Specifications (N));
18805 while Present (Discr) loop
18806 Enter_Name (Defining_Identifier (Discr));
18807
18808 -- For navigation purposes we add a reference to the discriminant
18809 -- in the entity for the type. If the current declaration is a
18810 -- completion, place references on the partial view. Otherwise the
18811 -- type is the current scope.
18812
18813 if Present (Prev) then
18814
18815 -- The references go on the partial view, if present. If the
18816 -- partial view has discriminants, the references have been
18817 -- generated already.
18818
18819 if not Has_Discriminants (Prev) then
18820 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
18821 end if;
18822 else
18823 Generate_Reference
18824 (Current_Scope, Defining_Identifier (Discr), 'd');
18825 end if;
18826
18827 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
18828 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
18829
18830 -- Ada 2005 (AI-254)
18831
18832 if Present (Access_To_Subprogram_Definition
18833 (Discriminant_Type (Discr)))
18834 and then Protected_Present (Access_To_Subprogram_Definition
18835 (Discriminant_Type (Discr)))
18836 then
18837 Discr_Type :=
18838 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
18839 end if;
18840
18841 else
18842 Find_Type (Discriminant_Type (Discr));
18843 Discr_Type := Etype (Discriminant_Type (Discr));
18844
18845 if Error_Posted (Discriminant_Type (Discr)) then
18846 Discr_Type := Any_Type;
18847 end if;
18848 end if;
18849
18850 -- Handling of discriminants that are access types
18851
18852 if Is_Access_Type (Discr_Type) then
18853
18854 -- Ada 2005 (AI-230): Access discriminant allowed in non-
18855 -- limited record types
18856
18857 if Ada_Version < Ada_2005 then
18858 Check_Access_Discriminant_Requires_Limited
18859 (Discr, Discriminant_Type (Discr));
18860 end if;
18861
18862 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
18863 Error_Msg_N
18864 ("(Ada 83) access discriminant not allowed", Discr);
18865 end if;
18866
18867 -- If not access type, must be a discrete type
18868
18869 elsif not Is_Discrete_Type (Discr_Type) then
18870 Error_Msg_N
18871 ("discriminants must have a discrete or access type",
18872 Discriminant_Type (Discr));
18873 end if;
18874
18875 Set_Etype (Defining_Identifier (Discr), Discr_Type);
18876
18877 -- If a discriminant specification includes the assignment compound
18878 -- delimiter followed by an expression, the expression is the default
18879 -- expression of the discriminant; the default expression must be of
18880 -- the type of the discriminant. (RM 3.7.1) Since this expression is
18881 -- a default expression, we do the special preanalysis, since this
18882 -- expression does not freeze (see section "Handling of Default and
18883 -- Per-Object Expressions" in spec of package Sem).
18884
18885 if Present (Expression (Discr)) then
18886 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
18887
18888 -- Legaity checks
18889
18890 if Nkind (N) = N_Formal_Type_Declaration then
18891 Error_Msg_N
18892 ("discriminant defaults not allowed for formal type",
18893 Expression (Discr));
18894
18895 -- Flag an error for a tagged type with defaulted discriminants,
18896 -- excluding limited tagged types when compiling for Ada 2012
18897 -- (see AI05-0214).
18898
18899 elsif Is_Tagged_Type (Current_Scope)
18900 and then (not Is_Limited_Type (Current_Scope)
18901 or else Ada_Version < Ada_2012)
18902 and then Comes_From_Source (N)
18903 then
18904 -- Note: see similar test in Check_Or_Process_Discriminants, to
18905 -- handle the (illegal) case of the completion of an untagged
18906 -- view with discriminants with defaults by a tagged full view.
18907 -- We skip the check if Discr does not come from source, to
18908 -- account for the case of an untagged derived type providing
18909 -- defaults for a renamed discriminant from a private untagged
18910 -- ancestor with a tagged full view (ACATS B460006).
18911
18912 if Ada_Version >= Ada_2012 then
18913 Error_Msg_N
18914 ("discriminants of nonlimited tagged type cannot have"
18915 & " defaults",
18916 Expression (Discr));
18917 else
18918 Error_Msg_N
18919 ("discriminants of tagged type cannot have defaults",
18920 Expression (Discr));
18921 end if;
18922
18923 else
18924 Default_Present := True;
18925 Append_Elmt (Expression (Discr), Elist);
18926
18927 -- Tag the defining identifiers for the discriminants with
18928 -- their corresponding default expressions from the tree.
18929
18930 Set_Discriminant_Default_Value
18931 (Defining_Identifier (Discr), Expression (Discr));
18932 end if;
18933
18934 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
18935 -- gets set unless we can be sure that no range check is required.
18936
18937 if (GNATprove_Mode or not Expander_Active)
18938 and then not
18939 Is_In_Range
18940 (Expression (Discr), Discr_Type, Assume_Valid => True)
18941 then
18942 Set_Do_Range_Check (Expression (Discr));
18943 end if;
18944
18945 -- No default discriminant value given
18946
18947 else
18948 Default_Not_Present := True;
18949 end if;
18950
18951 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
18952 -- Discr_Type but with the null-exclusion attribute
18953
18954 if Ada_Version >= Ada_2005 then
18955
18956 -- Ada 2005 (AI-231): Static checks
18957
18958 if Can_Never_Be_Null (Discr_Type) then
18959 Null_Exclusion_Static_Checks (Discr);
18960
18961 elsif Is_Access_Type (Discr_Type)
18962 and then Null_Exclusion_Present (Discr)
18963
18964 -- No need to check itypes because in their case this check
18965 -- was done at their point of creation
18966
18967 and then not Is_Itype (Discr_Type)
18968 then
18969 if Can_Never_Be_Null (Discr_Type) then
18970 Error_Msg_NE
18971 ("`NOT NULL` not allowed (& already excludes null)",
18972 Discr,
18973 Discr_Type);
18974 end if;
18975
18976 Set_Etype (Defining_Identifier (Discr),
18977 Create_Null_Excluding_Itype
18978 (T => Discr_Type,
18979 Related_Nod => Discr));
18980
18981 -- Check for improper null exclusion if the type is otherwise
18982 -- legal for a discriminant.
18983
18984 elsif Null_Exclusion_Present (Discr)
18985 and then Is_Discrete_Type (Discr_Type)
18986 then
18987 Error_Msg_N
18988 ("null exclusion can only apply to an access type", Discr);
18989 end if;
18990
18991 -- Ada 2005 (AI-402): access discriminants of nonlimited types
18992 -- can't have defaults. Synchronized types, or types that are
18993 -- explicitly limited are fine, but special tests apply to derived
18994 -- types in generics: in a generic body we have to assume the
18995 -- worst, and therefore defaults are not allowed if the parent is
18996 -- a generic formal private type (see ACATS B370001).
18997
18998 if Is_Access_Type (Discr_Type) and then Default_Present then
18999 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19000 or else Is_Limited_Record (Current_Scope)
19001 or else Is_Concurrent_Type (Current_Scope)
19002 or else Is_Concurrent_Record_Type (Current_Scope)
19003 or else Ekind (Current_Scope) = E_Limited_Private_Type
19004 then
19005 if not Is_Derived_Type (Current_Scope)
19006 or else not Is_Generic_Type (Etype (Current_Scope))
19007 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19008 or else Limited_Present
19009 (Type_Definition (Parent (Current_Scope)))
19010 then
19011 null;
19012
19013 else
19014 Error_Msg_N
19015 ("access discriminants of nonlimited types cannot "
19016 & "have defaults", Expression (Discr));
19017 end if;
19018
19019 elsif Present (Expression (Discr)) then
19020 Error_Msg_N
19021 ("(Ada 2005) access discriminants of nonlimited types "
19022 & "cannot have defaults", Expression (Discr));
19023 end if;
19024 end if;
19025 end if;
19026
19027 -- A discriminant cannot be effectively volatile. This check is only
19028 -- relevant when SPARK_Mode is on as it is not standard Ada legality
19029 -- rule (SPARK RM 7.1.3(6)).
19030
19031 if SPARK_Mode = On
19032 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19033 then
19034 Error_Msg_N ("discriminant cannot be volatile", Discr);
19035 end if;
19036
19037 Next (Discr);
19038 end loop;
19039
19040 -- An element list consisting of the default expressions of the
19041 -- discriminants is constructed in the above loop and used to set
19042 -- the Discriminant_Constraint attribute for the type. If an object
19043 -- is declared of this (record or task) type without any explicit
19044 -- discriminant constraint given, this element list will form the
19045 -- actual parameters for the corresponding initialization procedure
19046 -- for the type.
19047
19048 Set_Discriminant_Constraint (Current_Scope, Elist);
19049 Set_Stored_Constraint (Current_Scope, No_Elist);
19050
19051 -- Default expressions must be provided either for all or for none
19052 -- of the discriminants of a discriminant part. (RM 3.7.1)
19053
19054 if Default_Present and then Default_Not_Present then
19055 Error_Msg_N
19056 ("incomplete specification of defaults for discriminants", N);
19057 end if;
19058
19059 -- The use of the name of a discriminant is not allowed in default
19060 -- expressions of a discriminant part if the specification of the
19061 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19062
19063 -- To detect this, the discriminant names are entered initially with an
19064 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19065 -- attempt to use a void entity (for example in an expression that is
19066 -- type-checked) produces the error message: premature usage. Now after
19067 -- completing the semantic analysis of the discriminant part, we can set
19068 -- the Ekind of all the discriminants appropriately.
19069
19070 Discr := First (Discriminant_Specifications (N));
19071 Discr_Number := Uint_1;
19072 while Present (Discr) loop
19073 Id := Defining_Identifier (Discr);
19074 Set_Ekind (Id, E_Discriminant);
19075 Init_Component_Location (Id);
19076 Init_Esize (Id);
19077 Set_Discriminant_Number (Id, Discr_Number);
19078
19079 -- Make sure this is always set, even in illegal programs
19080
19081 Set_Corresponding_Discriminant (Id, Empty);
19082
19083 -- Initialize the Original_Record_Component to the entity itself.
19084 -- Inherit_Components will propagate the right value to
19085 -- discriminants in derived record types.
19086
19087 Set_Original_Record_Component (Id, Id);
19088
19089 -- Create the discriminal for the discriminant
19090
19091 Build_Discriminal (Id);
19092
19093 Next (Discr);
19094 Discr_Number := Discr_Number + 1;
19095 end loop;
19096
19097 Set_Has_Discriminants (Current_Scope);
19098 end Process_Discriminants;
19099
19100 -----------------------
19101 -- Process_Full_View --
19102 -----------------------
19103
19104 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19105 procedure Collect_Implemented_Interfaces
19106 (Typ : Entity_Id;
19107 Ifaces : Elist_Id);
19108 -- Ada 2005: Gather all the interfaces that Typ directly or
19109 -- inherently implements. Duplicate entries are not added to
19110 -- the list Ifaces.
19111
19112 ------------------------------------
19113 -- Collect_Implemented_Interfaces --
19114 ------------------------------------
19115
19116 procedure Collect_Implemented_Interfaces
19117 (Typ : Entity_Id;
19118 Ifaces : Elist_Id)
19119 is
19120 Iface : Entity_Id;
19121 Iface_Elmt : Elmt_Id;
19122
19123 begin
19124 -- Abstract interfaces are only associated with tagged record types
19125
19126 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19127 return;
19128 end if;
19129
19130 -- Recursively climb to the ancestors
19131
19132 if Etype (Typ) /= Typ
19133
19134 -- Protect the frontend against wrong cyclic declarations like:
19135
19136 -- type B is new A with private;
19137 -- type C is new A with private;
19138 -- private
19139 -- type B is new C with null record;
19140 -- type C is new B with null record;
19141
19142 and then Etype (Typ) /= Priv_T
19143 and then Etype (Typ) /= Full_T
19144 then
19145 -- Keep separate the management of private type declarations
19146
19147 if Ekind (Typ) = E_Record_Type_With_Private then
19148
19149 -- Handle the following illegal usage:
19150 -- type Private_Type is tagged private;
19151 -- private
19152 -- type Private_Type is new Type_Implementing_Iface;
19153
19154 if Present (Full_View (Typ))
19155 and then Etype (Typ) /= Full_View (Typ)
19156 then
19157 if Is_Interface (Etype (Typ)) then
19158 Append_Unique_Elmt (Etype (Typ), Ifaces);
19159 end if;
19160
19161 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19162 end if;
19163
19164 -- Non-private types
19165
19166 else
19167 if Is_Interface (Etype (Typ)) then
19168 Append_Unique_Elmt (Etype (Typ), Ifaces);
19169 end if;
19170
19171 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19172 end if;
19173 end if;
19174
19175 -- Handle entities in the list of abstract interfaces
19176
19177 if Present (Interfaces (Typ)) then
19178 Iface_Elmt := First_Elmt (Interfaces (Typ));
19179 while Present (Iface_Elmt) loop
19180 Iface := Node (Iface_Elmt);
19181
19182 pragma Assert (Is_Interface (Iface));
19183
19184 if not Contain_Interface (Iface, Ifaces) then
19185 Append_Elmt (Iface, Ifaces);
19186 Collect_Implemented_Interfaces (Iface, Ifaces);
19187 end if;
19188
19189 Next_Elmt (Iface_Elmt);
19190 end loop;
19191 end if;
19192 end Collect_Implemented_Interfaces;
19193
19194 -- Local variables
19195
19196 Full_Indic : Node_Id;
19197 Full_Parent : Entity_Id;
19198 Priv_Parent : Entity_Id;
19199
19200 -- Start of processing for Process_Full_View
19201
19202 begin
19203 -- First some sanity checks that must be done after semantic
19204 -- decoration of the full view and thus cannot be placed with other
19205 -- similar checks in Find_Type_Name
19206
19207 if not Is_Limited_Type (Priv_T)
19208 and then (Is_Limited_Type (Full_T)
19209 or else Is_Limited_Composite (Full_T))
19210 then
19211 if In_Instance then
19212 null;
19213 else
19214 Error_Msg_N
19215 ("completion of nonlimited type cannot be limited", Full_T);
19216 Explain_Limited_Type (Full_T, Full_T);
19217 end if;
19218
19219 elsif Is_Abstract_Type (Full_T)
19220 and then not Is_Abstract_Type (Priv_T)
19221 then
19222 Error_Msg_N
19223 ("completion of nonabstract type cannot be abstract", Full_T);
19224
19225 elsif Is_Tagged_Type (Priv_T)
19226 and then Is_Limited_Type (Priv_T)
19227 and then not Is_Limited_Type (Full_T)
19228 then
19229 -- If pragma CPP_Class was applied to the private declaration
19230 -- propagate the limitedness to the full-view
19231
19232 if Is_CPP_Class (Priv_T) then
19233 Set_Is_Limited_Record (Full_T);
19234
19235 -- GNAT allow its own definition of Limited_Controlled to disobey
19236 -- this rule in order in ease the implementation. This test is safe
19237 -- because Root_Controlled is defined in a child of System that
19238 -- normal programs are not supposed to use.
19239
19240 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19241 Set_Is_Limited_Composite (Full_T);
19242 else
19243 Error_Msg_N
19244 ("completion of limited tagged type must be limited", Full_T);
19245 end if;
19246
19247 elsif Is_Generic_Type (Priv_T) then
19248 Error_Msg_N ("generic type cannot have a completion", Full_T);
19249 end if;
19250
19251 -- Check that ancestor interfaces of private and full views are
19252 -- consistent. We omit this check for synchronized types because
19253 -- they are performed on the corresponding record type when frozen.
19254
19255 if Ada_Version >= Ada_2005
19256 and then Is_Tagged_Type (Priv_T)
19257 and then Is_Tagged_Type (Full_T)
19258 and then not Is_Concurrent_Type (Full_T)
19259 then
19260 declare
19261 Iface : Entity_Id;
19262 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19263 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19264
19265 begin
19266 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19267 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19268
19269 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19270 -- an interface type if and only if the full type is descendant
19271 -- of the interface type (AARM 7.3 (7.3/2)).
19272
19273 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19274
19275 if Present (Iface) then
19276 Error_Msg_NE
19277 ("interface in partial view& not implemented by full type "
19278 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19279 end if;
19280
19281 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19282
19283 if Present (Iface) then
19284 Error_Msg_NE
19285 ("interface & not implemented by partial view "
19286 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19287 end if;
19288 end;
19289 end if;
19290
19291 if Is_Tagged_Type (Priv_T)
19292 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19293 and then Is_Derived_Type (Full_T)
19294 then
19295 Priv_Parent := Etype (Priv_T);
19296
19297 -- The full view of a private extension may have been transformed
19298 -- into an unconstrained derived type declaration and a subtype
19299 -- declaration (see build_derived_record_type for details).
19300
19301 if Nkind (N) = N_Subtype_Declaration then
19302 Full_Indic := Subtype_Indication (N);
19303 Full_Parent := Etype (Base_Type (Full_T));
19304 else
19305 Full_Indic := Subtype_Indication (Type_Definition (N));
19306 Full_Parent := Etype (Full_T);
19307 end if;
19308
19309 -- Check that the parent type of the full type is a descendant of
19310 -- the ancestor subtype given in the private extension. If either
19311 -- entity has an Etype equal to Any_Type then we had some previous
19312 -- error situation [7.3(8)].
19313
19314 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19315 return;
19316
19317 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19318 -- any order. Therefore we don't have to check that its parent must
19319 -- be a descendant of the parent of the private type declaration.
19320
19321 elsif Is_Interface (Priv_Parent)
19322 and then Is_Interface (Full_Parent)
19323 then
19324 null;
19325
19326 -- Ada 2005 (AI-251): If the parent of the private type declaration
19327 -- is an interface there is no need to check that it is an ancestor
19328 -- of the associated full type declaration. The required tests for
19329 -- this case are performed by Build_Derived_Record_Type.
19330
19331 elsif not Is_Interface (Base_Type (Priv_Parent))
19332 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19333 then
19334 Error_Msg_N
19335 ("parent of full type must descend from parent"
19336 & " of private extension", Full_Indic);
19337
19338 -- First check a formal restriction, and then proceed with checking
19339 -- Ada rules. Since the formal restriction is not a serious error, we
19340 -- don't prevent further error detection for this check, hence the
19341 -- ELSE.
19342
19343 else
19344 -- In formal mode, when completing a private extension the type
19345 -- named in the private part must be exactly the same as that
19346 -- named in the visible part.
19347
19348 if Priv_Parent /= Full_Parent then
19349 Error_Msg_Name_1 := Chars (Priv_Parent);
19350 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19351 end if;
19352
19353 -- Check the rules of 7.3(10): if the private extension inherits
19354 -- known discriminants, then the full type must also inherit those
19355 -- discriminants from the same (ancestor) type, and the parent
19356 -- subtype of the full type must be constrained if and only if
19357 -- the ancestor subtype of the private extension is constrained.
19358
19359 if No (Discriminant_Specifications (Parent (Priv_T)))
19360 and then not Has_Unknown_Discriminants (Priv_T)
19361 and then Has_Discriminants (Base_Type (Priv_Parent))
19362 then
19363 declare
19364 Priv_Indic : constant Node_Id :=
19365 Subtype_Indication (Parent (Priv_T));
19366
19367 Priv_Constr : constant Boolean :=
19368 Is_Constrained (Priv_Parent)
19369 or else
19370 Nkind (Priv_Indic) = N_Subtype_Indication
19371 or else
19372 Is_Constrained (Entity (Priv_Indic));
19373
19374 Full_Constr : constant Boolean :=
19375 Is_Constrained (Full_Parent)
19376 or else
19377 Nkind (Full_Indic) = N_Subtype_Indication
19378 or else
19379 Is_Constrained (Entity (Full_Indic));
19380
19381 Priv_Discr : Entity_Id;
19382 Full_Discr : Entity_Id;
19383
19384 begin
19385 Priv_Discr := First_Discriminant (Priv_Parent);
19386 Full_Discr := First_Discriminant (Full_Parent);
19387 while Present (Priv_Discr) and then Present (Full_Discr) loop
19388 if Original_Record_Component (Priv_Discr) =
19389 Original_Record_Component (Full_Discr)
19390 or else
19391 Corresponding_Discriminant (Priv_Discr) =
19392 Corresponding_Discriminant (Full_Discr)
19393 then
19394 null;
19395 else
19396 exit;
19397 end if;
19398
19399 Next_Discriminant (Priv_Discr);
19400 Next_Discriminant (Full_Discr);
19401 end loop;
19402
19403 if Present (Priv_Discr) or else Present (Full_Discr) then
19404 Error_Msg_N
19405 ("full view must inherit discriminants of the parent"
19406 & " type used in the private extension", Full_Indic);
19407
19408 elsif Priv_Constr and then not Full_Constr then
19409 Error_Msg_N
19410 ("parent subtype of full type must be constrained",
19411 Full_Indic);
19412
19413 elsif Full_Constr and then not Priv_Constr then
19414 Error_Msg_N
19415 ("parent subtype of full type must be unconstrained",
19416 Full_Indic);
19417 end if;
19418 end;
19419
19420 -- Check the rules of 7.3(12): if a partial view has neither
19421 -- known or unknown discriminants, then the full type
19422 -- declaration shall define a definite subtype.
19423
19424 elsif not Has_Unknown_Discriminants (Priv_T)
19425 and then not Has_Discriminants (Priv_T)
19426 and then not Is_Constrained (Full_T)
19427 then
19428 Error_Msg_N
19429 ("full view must define a constrained type if partial view"
19430 & " has no discriminants", Full_T);
19431 end if;
19432
19433 -- ??????? Do we implement the following properly ?????
19434 -- If the ancestor subtype of a private extension has constrained
19435 -- discriminants, then the parent subtype of the full view shall
19436 -- impose a statically matching constraint on those discriminants
19437 -- [7.3(13)].
19438 end if;
19439
19440 else
19441 -- For untagged types, verify that a type without discriminants is
19442 -- not completed with an unconstrained type. A separate error message
19443 -- is produced if the full type has defaulted discriminants.
19444
19445 if not Is_Indefinite_Subtype (Priv_T)
19446 and then Is_Indefinite_Subtype (Full_T)
19447 then
19448 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19449 Error_Msg_NE
19450 ("full view of& not compatible with declaration#",
19451 Full_T, Priv_T);
19452
19453 if not Is_Tagged_Type (Full_T) then
19454 Error_Msg_N
19455 ("\one is constrained, the other unconstrained", Full_T);
19456 end if;
19457 end if;
19458 end if;
19459
19460 -- AI-419: verify that the use of "limited" is consistent
19461
19462 declare
19463 Orig_Decl : constant Node_Id := Original_Node (N);
19464
19465 begin
19466 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19467 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19468 and then Nkind
19469 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19470 then
19471 if not Limited_Present (Parent (Priv_T))
19472 and then not Synchronized_Present (Parent (Priv_T))
19473 and then Limited_Present (Type_Definition (Orig_Decl))
19474 then
19475 Error_Msg_N
19476 ("full view of non-limited extension cannot be limited", N);
19477
19478 -- Conversely, if the partial view carries the limited keyword,
19479 -- the full view must as well, even if it may be redundant.
19480
19481 elsif Limited_Present (Parent (Priv_T))
19482 and then not Limited_Present (Type_Definition (Orig_Decl))
19483 then
19484 Error_Msg_N
19485 ("full view of limited extension must be explicitly limited",
19486 N);
19487 end if;
19488 end if;
19489 end;
19490
19491 -- Ada 2005 (AI-443): A synchronized private extension must be
19492 -- completed by a task or protected type.
19493
19494 if Ada_Version >= Ada_2005
19495 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19496 and then Synchronized_Present (Parent (Priv_T))
19497 and then not Is_Concurrent_Type (Full_T)
19498 then
19499 Error_Msg_N ("full view of synchronized extension must " &
19500 "be synchronized type", N);
19501 end if;
19502
19503 -- Ada 2005 AI-363: if the full view has discriminants with
19504 -- defaults, it is illegal to declare constrained access subtypes
19505 -- whose designated type is the current type. This allows objects
19506 -- of the type that are declared in the heap to be unconstrained.
19507
19508 if not Has_Unknown_Discriminants (Priv_T)
19509 and then not Has_Discriminants (Priv_T)
19510 and then Has_Discriminants (Full_T)
19511 and then
19512 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19513 then
19514 Set_Has_Constrained_Partial_View (Full_T);
19515 Set_Has_Constrained_Partial_View (Priv_T);
19516 end if;
19517
19518 -- Create a full declaration for all its subtypes recorded in
19519 -- Private_Dependents and swap them similarly to the base type. These
19520 -- are subtypes that have been define before the full declaration of
19521 -- the private type. We also swap the entry in Private_Dependents list
19522 -- so we can properly restore the private view on exit from the scope.
19523
19524 declare
19525 Priv_Elmt : Elmt_Id;
19526 Priv_Scop : Entity_Id;
19527 Priv : Entity_Id;
19528 Full : Entity_Id;
19529
19530 begin
19531 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19532 while Present (Priv_Elmt) loop
19533 Priv := Node (Priv_Elmt);
19534 Priv_Scop := Scope (Priv);
19535
19536 if Ekind_In (Priv, E_Private_Subtype,
19537 E_Limited_Private_Subtype,
19538 E_Record_Subtype_With_Private)
19539 then
19540 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19541 Set_Is_Itype (Full);
19542 Set_Parent (Full, Parent (Priv));
19543 Set_Associated_Node_For_Itype (Full, N);
19544
19545 -- Now we need to complete the private subtype, but since the
19546 -- base type has already been swapped, we must also swap the
19547 -- subtypes (and thus, reverse the arguments in the call to
19548 -- Complete_Private_Subtype). Also note that we may need to
19549 -- re-establish the scope of the private subtype.
19550
19551 Copy_And_Swap (Priv, Full);
19552
19553 if not In_Open_Scopes (Priv_Scop) then
19554 Push_Scope (Priv_Scop);
19555
19556 else
19557 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19558
19559 Priv_Scop := Empty;
19560 end if;
19561
19562 Complete_Private_Subtype (Full, Priv, Full_T, N);
19563
19564 if Present (Priv_Scop) then
19565 Pop_Scope;
19566 end if;
19567
19568 Replace_Elmt (Priv_Elmt, Full);
19569 end if;
19570
19571 Next_Elmt (Priv_Elmt);
19572 end loop;
19573 end;
19574
19575 -- If the private view was tagged, copy the new primitive operations
19576 -- from the private view to the full view.
19577
19578 if Is_Tagged_Type (Full_T) then
19579 declare
19580 Disp_Typ : Entity_Id;
19581 Full_List : Elist_Id;
19582 Prim : Entity_Id;
19583 Prim_Elmt : Elmt_Id;
19584 Priv_List : Elist_Id;
19585
19586 function Contains
19587 (E : Entity_Id;
19588 L : Elist_Id) return Boolean;
19589 -- Determine whether list L contains element E
19590
19591 --------------
19592 -- Contains --
19593 --------------
19594
19595 function Contains
19596 (E : Entity_Id;
19597 L : Elist_Id) return Boolean
19598 is
19599 List_Elmt : Elmt_Id;
19600
19601 begin
19602 List_Elmt := First_Elmt (L);
19603 while Present (List_Elmt) loop
19604 if Node (List_Elmt) = E then
19605 return True;
19606 end if;
19607
19608 Next_Elmt (List_Elmt);
19609 end loop;
19610
19611 return False;
19612 end Contains;
19613
19614 -- Start of processing
19615
19616 begin
19617 if Is_Tagged_Type (Priv_T) then
19618 Priv_List := Primitive_Operations (Priv_T);
19619 Prim_Elmt := First_Elmt (Priv_List);
19620
19621 -- In the case of a concurrent type completing a private tagged
19622 -- type, primitives may have been declared in between the two
19623 -- views. These subprograms need to be wrapped the same way
19624 -- entries and protected procedures are handled because they
19625 -- cannot be directly shared by the two views.
19626
19627 if Is_Concurrent_Type (Full_T) then
19628 declare
19629 Conc_Typ : constant Entity_Id :=
19630 Corresponding_Record_Type (Full_T);
19631 Curr_Nod : Node_Id := Parent (Conc_Typ);
19632 Wrap_Spec : Node_Id;
19633
19634 begin
19635 while Present (Prim_Elmt) loop
19636 Prim := Node (Prim_Elmt);
19637
19638 if Comes_From_Source (Prim)
19639 and then not Is_Abstract_Subprogram (Prim)
19640 then
19641 Wrap_Spec :=
19642 Make_Subprogram_Declaration (Sloc (Prim),
19643 Specification =>
19644 Build_Wrapper_Spec
19645 (Subp_Id => Prim,
19646 Obj_Typ => Conc_Typ,
19647 Formals =>
19648 Parameter_Specifications (
19649 Parent (Prim))));
19650
19651 Insert_After (Curr_Nod, Wrap_Spec);
19652 Curr_Nod := Wrap_Spec;
19653
19654 Analyze (Wrap_Spec);
19655 end if;
19656
19657 Next_Elmt (Prim_Elmt);
19658 end loop;
19659
19660 return;
19661 end;
19662
19663 -- For non-concurrent types, transfer explicit primitives, but
19664 -- omit those inherited from the parent of the private view
19665 -- since they will be re-inherited later on.
19666
19667 else
19668 Full_List := Primitive_Operations (Full_T);
19669
19670 while Present (Prim_Elmt) loop
19671 Prim := Node (Prim_Elmt);
19672
19673 if Comes_From_Source (Prim)
19674 and then not Contains (Prim, Full_List)
19675 then
19676 Append_Elmt (Prim, Full_List);
19677 end if;
19678
19679 Next_Elmt (Prim_Elmt);
19680 end loop;
19681 end if;
19682
19683 -- Untagged private view
19684
19685 else
19686 Full_List := Primitive_Operations (Full_T);
19687
19688 -- In this case the partial view is untagged, so here we locate
19689 -- all of the earlier primitives that need to be treated as
19690 -- dispatching (those that appear between the two views). Note
19691 -- that these additional operations must all be new operations
19692 -- (any earlier operations that override inherited operations
19693 -- of the full view will already have been inserted in the
19694 -- primitives list, marked by Check_Operation_From_Private_View
19695 -- as dispatching. Note that implicit "/=" operators are
19696 -- excluded from being added to the primitives list since they
19697 -- shouldn't be treated as dispatching (tagged "/=" is handled
19698 -- specially).
19699
19700 Prim := Next_Entity (Full_T);
19701 while Present (Prim) and then Prim /= Priv_T loop
19702 if Ekind_In (Prim, E_Procedure, E_Function) then
19703 Disp_Typ := Find_Dispatching_Type (Prim);
19704
19705 if Disp_Typ = Full_T
19706 and then (Chars (Prim) /= Name_Op_Ne
19707 or else Comes_From_Source (Prim))
19708 then
19709 Check_Controlling_Formals (Full_T, Prim);
19710
19711 if not Is_Dispatching_Operation (Prim) then
19712 Append_Elmt (Prim, Full_List);
19713 Set_Is_Dispatching_Operation (Prim, True);
19714 Set_DT_Position_Value (Prim, No_Uint);
19715 end if;
19716
19717 elsif Is_Dispatching_Operation (Prim)
19718 and then Disp_Typ /= Full_T
19719 then
19720
19721 -- Verify that it is not otherwise controlled by a
19722 -- formal or a return value of type T.
19723
19724 Check_Controlling_Formals (Disp_Typ, Prim);
19725 end if;
19726 end if;
19727
19728 Next_Entity (Prim);
19729 end loop;
19730 end if;
19731
19732 -- For the tagged case, the two views can share the same primitive
19733 -- operations list and the same class-wide type. Update attributes
19734 -- of the class-wide type which depend on the full declaration.
19735
19736 if Is_Tagged_Type (Priv_T) then
19737 Set_Direct_Primitive_Operations (Priv_T, Full_List);
19738 Set_Class_Wide_Type
19739 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
19740
19741 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
19742 Set_Has_Protected
19743 (Class_Wide_Type (Priv_T), Has_Protected (Full_T));
19744 end if;
19745 end;
19746 end if;
19747
19748 -- Ada 2005 AI 161: Check preelaborable initialization consistency
19749
19750 if Known_To_Have_Preelab_Init (Priv_T) then
19751
19752 -- Case where there is a pragma Preelaborable_Initialization. We
19753 -- always allow this in predefined units, which is cheating a bit,
19754 -- but it means we don't have to struggle to meet the requirements in
19755 -- the RM for having Preelaborable Initialization. Otherwise we
19756 -- require that the type meets the RM rules. But we can't check that
19757 -- yet, because of the rule about overriding Initialize, so we simply
19758 -- set a flag that will be checked at freeze time.
19759
19760 if not In_Predefined_Unit (Full_T) then
19761 Set_Must_Have_Preelab_Init (Full_T);
19762 end if;
19763 end if;
19764
19765 -- If pragma CPP_Class was applied to the private type declaration,
19766 -- propagate it now to the full type declaration.
19767
19768 if Is_CPP_Class (Priv_T) then
19769 Set_Is_CPP_Class (Full_T);
19770 Set_Convention (Full_T, Convention_CPP);
19771
19772 -- Check that components of imported CPP types do not have default
19773 -- expressions.
19774
19775 Check_CPP_Type_Has_No_Defaults (Full_T);
19776 end if;
19777
19778 -- If the private view has user specified stream attributes, then so has
19779 -- the full view.
19780
19781 -- Why the test, how could these flags be already set in Full_T ???
19782
19783 if Has_Specified_Stream_Read (Priv_T) then
19784 Set_Has_Specified_Stream_Read (Full_T);
19785 end if;
19786
19787 if Has_Specified_Stream_Write (Priv_T) then
19788 Set_Has_Specified_Stream_Write (Full_T);
19789 end if;
19790
19791 if Has_Specified_Stream_Input (Priv_T) then
19792 Set_Has_Specified_Stream_Input (Full_T);
19793 end if;
19794
19795 if Has_Specified_Stream_Output (Priv_T) then
19796 Set_Has_Specified_Stream_Output (Full_T);
19797 end if;
19798
19799 -- Propagate the attributes related to pragma Default_Initial_Condition
19800 -- from the private to the full view. Note that both flags are mutually
19801 -- exclusive.
19802
19803 if Has_Default_Init_Cond (Priv_T)
19804 or else Has_Inherited_Default_Init_Cond (Priv_T)
19805 then
19806 Propagate_Default_Init_Cond_Attributes
19807 (From_Typ => Priv_T,
19808 To_Typ => Full_T,
19809 Private_To_Full_View => True);
19810
19811 -- In the case where the full view is derived from another private type,
19812 -- the attributes related to pragma Default_Initial_Condition must be
19813 -- propagated from the full to the private view to maintain consistency
19814 -- of views.
19815
19816 -- package Pack is
19817 -- type Parent_Typ is private
19818 -- with Default_Initial_Condition ...;
19819 -- private
19820 -- type Parent_Typ is ...;
19821 -- end Pack;
19822
19823 -- with Pack; use Pack;
19824 -- package Pack_2 is
19825 -- type Deriv_Typ is private; -- must inherit
19826 -- private
19827 -- type Deriv_Typ is new Parent_Typ; -- must inherit
19828 -- end Pack_2;
19829
19830 elsif Has_Default_Init_Cond (Full_T)
19831 or else Has_Inherited_Default_Init_Cond (Full_T)
19832 then
19833 Propagate_Default_Init_Cond_Attributes
19834 (From_Typ => Full_T,
19835 To_Typ => Priv_T,
19836 Private_To_Full_View => True);
19837 end if;
19838
19839 -- Propagate the attributes related to pragma Ghost from the private to
19840 -- the full view.
19841
19842 if Is_Ghost_Entity (Priv_T) then
19843 Set_Is_Ghost_Entity (Full_T);
19844
19845 -- The Ghost policy in effect at the point of declaration and at the
19846 -- point of completion must match (SPARK RM 6.9(15)).
19847
19848 Check_Ghost_Completion (Priv_T, Full_T);
19849
19850 -- In the case where the private view of a tagged type lacks a parent
19851 -- type and is subject to pragma Ghost, ensure that the parent type
19852 -- specified by the full view is also Ghost (SPARK RM 6.9(9)).
19853
19854 if Is_Derived_Type (Full_T) then
19855 Check_Ghost_Derivation (Full_T);
19856 end if;
19857 end if;
19858
19859 -- Propagate invariants to full type
19860
19861 if Has_Invariants (Priv_T) then
19862 Set_Has_Invariants (Full_T);
19863 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
19864 end if;
19865
19866 if Has_Inheritable_Invariants (Priv_T) then
19867 Set_Has_Inheritable_Invariants (Full_T);
19868 end if;
19869
19870 -- Check hidden inheritance of class-wide type invariants
19871
19872 if Ada_Version >= Ada_2012
19873 and then not Has_Inheritable_Invariants (Full_T)
19874 and then In_Private_Part (Current_Scope)
19875 and then Has_Interfaces (Full_T)
19876 then
19877 declare
19878 Ifaces : Elist_Id;
19879 AI : Elmt_Id;
19880
19881 begin
19882 Collect_Interfaces (Full_T, Ifaces, Exclude_Parents => True);
19883
19884 AI := First_Elmt (Ifaces);
19885 while Present (AI) loop
19886 if Has_Inheritable_Invariants (Node (AI)) then
19887 Error_Msg_N
19888 ("hidden inheritance of class-wide type invariants " &
19889 "not allowed", N);
19890 exit;
19891 end if;
19892
19893 Next_Elmt (AI);
19894 end loop;
19895 end;
19896 end if;
19897
19898 -- Propagate predicates to full type, and predicate function if already
19899 -- defined. It is not clear that this can actually happen? the partial
19900 -- view cannot be frozen yet, and the predicate function has not been
19901 -- built. Still it is a cheap check and seems safer to make it.
19902
19903 if Has_Predicates (Priv_T) then
19904 if Present (Predicate_Function (Priv_T)) then
19905 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
19906 end if;
19907
19908 Set_Has_Predicates (Full_T);
19909 end if;
19910 end Process_Full_View;
19911
19912 -----------------------------------
19913 -- Process_Incomplete_Dependents --
19914 -----------------------------------
19915
19916 procedure Process_Incomplete_Dependents
19917 (N : Node_Id;
19918 Full_T : Entity_Id;
19919 Inc_T : Entity_Id)
19920 is
19921 Inc_Elmt : Elmt_Id;
19922 Priv_Dep : Entity_Id;
19923 New_Subt : Entity_Id;
19924
19925 Disc_Constraint : Elist_Id;
19926
19927 begin
19928 if No (Private_Dependents (Inc_T)) then
19929 return;
19930 end if;
19931
19932 -- Itypes that may be generated by the completion of an incomplete
19933 -- subtype are not used by the back-end and not attached to the tree.
19934 -- They are created only for constraint-checking purposes.
19935
19936 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
19937 while Present (Inc_Elmt) loop
19938 Priv_Dep := Node (Inc_Elmt);
19939
19940 if Ekind (Priv_Dep) = E_Subprogram_Type then
19941
19942 -- An Access_To_Subprogram type may have a return type or a
19943 -- parameter type that is incomplete. Replace with the full view.
19944
19945 if Etype (Priv_Dep) = Inc_T then
19946 Set_Etype (Priv_Dep, Full_T);
19947 end if;
19948
19949 declare
19950 Formal : Entity_Id;
19951
19952 begin
19953 Formal := First_Formal (Priv_Dep);
19954 while Present (Formal) loop
19955 if Etype (Formal) = Inc_T then
19956 Set_Etype (Formal, Full_T);
19957 end if;
19958
19959 Next_Formal (Formal);
19960 end loop;
19961 end;
19962
19963 elsif Is_Overloadable (Priv_Dep) then
19964
19965 -- If a subprogram in the incomplete dependents list is primitive
19966 -- for a tagged full type then mark it as a dispatching operation,
19967 -- check whether it overrides an inherited subprogram, and check
19968 -- restrictions on its controlling formals. Note that a protected
19969 -- operation is never dispatching: only its wrapper operation
19970 -- (which has convention Ada) is.
19971
19972 if Is_Tagged_Type (Full_T)
19973 and then Is_Primitive (Priv_Dep)
19974 and then Convention (Priv_Dep) /= Convention_Protected
19975 then
19976 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
19977 Set_Is_Dispatching_Operation (Priv_Dep);
19978 Check_Controlling_Formals (Full_T, Priv_Dep);
19979 end if;
19980
19981 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
19982
19983 -- Can happen during processing of a body before the completion
19984 -- of a TA type. Ignore, because spec is also on dependent list.
19985
19986 return;
19987
19988 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
19989 -- corresponding subtype of the full view.
19990
19991 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
19992 Set_Subtype_Indication
19993 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
19994 Set_Etype (Priv_Dep, Full_T);
19995 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
19996 Set_Analyzed (Parent (Priv_Dep), False);
19997
19998 -- Reanalyze the declaration, suppressing the call to
19999 -- Enter_Name to avoid duplicate names.
20000
20001 Analyze_Subtype_Declaration
20002 (N => Parent (Priv_Dep),
20003 Skip => True);
20004
20005 -- Dependent is a subtype
20006
20007 else
20008 -- We build a new subtype indication using the full view of the
20009 -- incomplete parent. The discriminant constraints have been
20010 -- elaborated already at the point of the subtype declaration.
20011
20012 New_Subt := Create_Itype (E_Void, N);
20013
20014 if Has_Discriminants (Full_T) then
20015 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20016 else
20017 Disc_Constraint := No_Elist;
20018 end if;
20019
20020 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20021 Set_Full_View (Priv_Dep, New_Subt);
20022 end if;
20023
20024 Next_Elmt (Inc_Elmt);
20025 end loop;
20026 end Process_Incomplete_Dependents;
20027
20028 --------------------------------
20029 -- Process_Range_Expr_In_Decl --
20030 --------------------------------
20031
20032 procedure Process_Range_Expr_In_Decl
20033 (R : Node_Id;
20034 T : Entity_Id;
20035 Subtyp : Entity_Id := Empty;
20036 Check_List : List_Id := Empty_List;
20037 R_Check_Off : Boolean := False;
20038 In_Iter_Schm : Boolean := False)
20039 is
20040 Lo, Hi : Node_Id;
20041 R_Checks : Check_Result;
20042 Insert_Node : Node_Id;
20043 Def_Id : Entity_Id;
20044
20045 begin
20046 Analyze_And_Resolve (R, Base_Type (T));
20047
20048 if Nkind (R) = N_Range then
20049
20050 -- In SPARK, all ranges should be static, with the exception of the
20051 -- discrete type definition of a loop parameter specification.
20052
20053 if not In_Iter_Schm
20054 and then not Is_OK_Static_Range (R)
20055 then
20056 Check_SPARK_05_Restriction ("range should be static", R);
20057 end if;
20058
20059 Lo := Low_Bound (R);
20060 Hi := High_Bound (R);
20061
20062 -- Validity checks on the range of a quantified expression are
20063 -- delayed until the construct is transformed into a loop.
20064
20065 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20066 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20067 then
20068 null;
20069
20070 -- We need to ensure validity of the bounds here, because if we
20071 -- go ahead and do the expansion, then the expanded code will get
20072 -- analyzed with range checks suppressed and we miss the check.
20073
20074 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20075 -- the temporaries generated by routine Remove_Side_Effects by means
20076 -- of validity checks must use the same names. When a range appears
20077 -- in the parent of a generic, the range is processed with checks
20078 -- disabled as part of the generic context and with checks enabled
20079 -- for code generation purposes. This leads to link issues as the
20080 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20081 -- template sees the temporaries generated by Remove_Side_Effects.
20082
20083 else
20084 Validity_Check_Range (R, Subtyp);
20085 end if;
20086
20087 -- If there were errors in the declaration, try and patch up some
20088 -- common mistakes in the bounds. The cases handled are literals
20089 -- which are Integer where the expected type is Real and vice versa.
20090 -- These corrections allow the compilation process to proceed further
20091 -- along since some basic assumptions of the format of the bounds
20092 -- are guaranteed.
20093
20094 if Etype (R) = Any_Type then
20095 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20096 Rewrite (Lo,
20097 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20098
20099 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20100 Rewrite (Hi,
20101 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20102
20103 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20104 Rewrite (Lo,
20105 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20106
20107 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20108 Rewrite (Hi,
20109 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20110 end if;
20111
20112 Set_Etype (Lo, T);
20113 Set_Etype (Hi, T);
20114 end if;
20115
20116 -- If the bounds of the range have been mistakenly given as string
20117 -- literals (perhaps in place of character literals), then an error
20118 -- has already been reported, but we rewrite the string literal as a
20119 -- bound of the range's type to avoid blowups in later processing
20120 -- that looks at static values.
20121
20122 if Nkind (Lo) = N_String_Literal then
20123 Rewrite (Lo,
20124 Make_Attribute_Reference (Sloc (Lo),
20125 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20126 Attribute_Name => Name_First));
20127 Analyze_And_Resolve (Lo);
20128 end if;
20129
20130 if Nkind (Hi) = N_String_Literal then
20131 Rewrite (Hi,
20132 Make_Attribute_Reference (Sloc (Hi),
20133 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20134 Attribute_Name => Name_First));
20135 Analyze_And_Resolve (Hi);
20136 end if;
20137
20138 -- If bounds aren't scalar at this point then exit, avoiding
20139 -- problems with further processing of the range in this procedure.
20140
20141 if not Is_Scalar_Type (Etype (Lo)) then
20142 return;
20143 end if;
20144
20145 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20146 -- then range of the base type. Here we check whether the bounds
20147 -- are in the range of the subtype itself. Note that if the bounds
20148 -- represent the null range the Constraint_Error exception should
20149 -- not be raised.
20150
20151 -- ??? The following code should be cleaned up as follows
20152
20153 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20154 -- is done in the call to Range_Check (R, T); below
20155
20156 -- 2. The use of R_Check_Off should be investigated and possibly
20157 -- removed, this would clean up things a bit.
20158
20159 if Is_Null_Range (Lo, Hi) then
20160 null;
20161
20162 else
20163 -- Capture values of bounds and generate temporaries for them
20164 -- if needed, before applying checks, since checks may cause
20165 -- duplication of the expression without forcing evaluation.
20166
20167 -- The forced evaluation removes side effects from expressions,
20168 -- which should occur also in GNATprove mode. Otherwise, we end up
20169 -- with unexpected insertions of actions at places where this is
20170 -- not supposed to occur, e.g. on default parameters of a call.
20171
20172 if Expander_Active or GNATprove_Mode then
20173
20174 -- If no subtype name, then just call Force_Evaluation to
20175 -- create declarations as needed to deal with side effects.
20176 -- Also ignore calls from within a record type, where we
20177 -- have possible scoping issues.
20178
20179 if No (Subtyp) or else Is_Record_Type (Current_Scope) then
20180 Force_Evaluation (Lo);
20181 Force_Evaluation (Hi);
20182
20183 -- If a subtype is given, then we capture the bounds if they
20184 -- are not known at compile time, using constant identifiers
20185 -- xxx_FIRST and xxx_LAST where xxx is the name of the subtype.
20186
20187 -- Note: we do this transformation even if expansion is not
20188 -- active, and in particular we do it in GNATprove_Mode since
20189 -- the transformation is in general required to ensure that the
20190 -- resulting tree has proper Ada semantics.
20191
20192 -- Historical note: We used to just do Force_Evaluation calls
20193 -- in all cases, but it is better to capture the bounds with
20194 -- proper non-serialized names, since these will be accessed
20195 -- from other units, and hence may be public, and also we can
20196 -- then expand 'First and 'Last references to be references to
20197 -- these special names.
20198
20199 else
20200 if not Compile_Time_Known_Value (Lo)
20201
20202 -- No need to capture bounds if they already are
20203 -- references to constants.
20204
20205 and then not (Is_Entity_Name (Lo)
20206 and then Is_Constant_Object (Entity (Lo)))
20207 then
20208 declare
20209 Loc : constant Source_Ptr := Sloc (Lo);
20210 Lov : constant Entity_Id :=
20211 Make_Defining_Identifier (Loc,
20212 Chars =>
20213 New_External_Name (Chars (Subtyp), "_FIRST"));
20214 begin
20215 Insert_Action (R,
20216 Make_Object_Declaration (Loc,
20217 Defining_Identifier => Lov,
20218 Object_Definition =>
20219 New_Occurrence_Of (Base_Type (T), Loc),
20220 Constant_Present => True,
20221 Expression => Relocate_Node (Lo)));
20222 Rewrite (Lo, New_Occurrence_Of (Lov, Loc));
20223 end;
20224 end if;
20225
20226 if not Compile_Time_Known_Value (Hi)
20227 and then not (Is_Entity_Name (Hi)
20228 and then Is_Constant_Object (Entity (Hi)))
20229 then
20230 declare
20231 Loc : constant Source_Ptr := Sloc (Hi);
20232 Hiv : constant Entity_Id :=
20233 Make_Defining_Identifier (Loc,
20234 Chars =>
20235 New_External_Name (Chars (Subtyp), "_LAST"));
20236 begin
20237 Insert_Action (R,
20238 Make_Object_Declaration (Loc,
20239 Defining_Identifier => Hiv,
20240 Object_Definition =>
20241 New_Occurrence_Of (Base_Type (T), Loc),
20242 Constant_Present => True,
20243 Expression => Relocate_Node (Hi)));
20244 Rewrite (Hi, New_Occurrence_Of (Hiv, Loc));
20245 end;
20246 end if;
20247 end if;
20248 end if;
20249
20250 -- We use a flag here instead of suppressing checks on the
20251 -- type because the type we check against isn't necessarily
20252 -- the place where we put the check.
20253
20254 if not R_Check_Off then
20255 R_Checks := Get_Range_Checks (R, T);
20256
20257 -- Look up tree to find an appropriate insertion point. We
20258 -- can't just use insert_actions because later processing
20259 -- depends on the insertion node. Prior to Ada 2012 the
20260 -- insertion point could only be a declaration or a loop, but
20261 -- quantified expressions can appear within any context in an
20262 -- expression, and the insertion point can be any statement,
20263 -- pragma, or declaration.
20264
20265 Insert_Node := Parent (R);
20266 while Present (Insert_Node) loop
20267 exit when
20268 Nkind (Insert_Node) in N_Declaration
20269 and then
20270 not Nkind_In
20271 (Insert_Node, N_Component_Declaration,
20272 N_Loop_Parameter_Specification,
20273 N_Function_Specification,
20274 N_Procedure_Specification);
20275
20276 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20277 or else Nkind (Insert_Node) in
20278 N_Statement_Other_Than_Procedure_Call
20279 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20280 N_Pragma);
20281
20282 Insert_Node := Parent (Insert_Node);
20283 end loop;
20284
20285 -- Why would Type_Decl not be present??? Without this test,
20286 -- short regression tests fail.
20287
20288 if Present (Insert_Node) then
20289
20290 -- Case of loop statement. Verify that the range is part
20291 -- of the subtype indication of the iteration scheme.
20292
20293 if Nkind (Insert_Node) = N_Loop_Statement then
20294 declare
20295 Indic : Node_Id;
20296
20297 begin
20298 Indic := Parent (R);
20299 while Present (Indic)
20300 and then Nkind (Indic) /= N_Subtype_Indication
20301 loop
20302 Indic := Parent (Indic);
20303 end loop;
20304
20305 if Present (Indic) then
20306 Def_Id := Etype (Subtype_Mark (Indic));
20307
20308 Insert_Range_Checks
20309 (R_Checks,
20310 Insert_Node,
20311 Def_Id,
20312 Sloc (Insert_Node),
20313 R,
20314 Do_Before => True);
20315 end if;
20316 end;
20317
20318 -- Insertion before a declaration. If the declaration
20319 -- includes discriminants, the list of applicable checks
20320 -- is given by the caller.
20321
20322 elsif Nkind (Insert_Node) in N_Declaration then
20323 Def_Id := Defining_Identifier (Insert_Node);
20324
20325 if (Ekind (Def_Id) = E_Record_Type
20326 and then Depends_On_Discriminant (R))
20327 or else
20328 (Ekind (Def_Id) = E_Protected_Type
20329 and then Has_Discriminants (Def_Id))
20330 then
20331 Append_Range_Checks
20332 (R_Checks,
20333 Check_List, Def_Id, Sloc (Insert_Node), R);
20334
20335 else
20336 Insert_Range_Checks
20337 (R_Checks,
20338 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20339
20340 end if;
20341
20342 -- Insertion before a statement. Range appears in the
20343 -- context of a quantified expression. Insertion will
20344 -- take place when expression is expanded.
20345
20346 else
20347 null;
20348 end if;
20349 end if;
20350 end if;
20351 end if;
20352
20353 -- Case of other than an explicit N_Range node
20354
20355 -- The forced evaluation removes side effects from expressions, which
20356 -- should occur also in GNATprove mode. Otherwise, we end up with
20357 -- unexpected insertions of actions at places where this is not
20358 -- supposed to occur, e.g. on default parameters of a call.
20359
20360 elsif Expander_Active or GNATprove_Mode then
20361 Get_Index_Bounds (R, Lo, Hi);
20362 Force_Evaluation (Lo);
20363 Force_Evaluation (Hi);
20364 end if;
20365 end Process_Range_Expr_In_Decl;
20366
20367 --------------------------------------
20368 -- Process_Real_Range_Specification --
20369 --------------------------------------
20370
20371 procedure Process_Real_Range_Specification (Def : Node_Id) is
20372 Spec : constant Node_Id := Real_Range_Specification (Def);
20373 Lo : Node_Id;
20374 Hi : Node_Id;
20375 Err : Boolean := False;
20376
20377 procedure Analyze_Bound (N : Node_Id);
20378 -- Analyze and check one bound
20379
20380 -------------------
20381 -- Analyze_Bound --
20382 -------------------
20383
20384 procedure Analyze_Bound (N : Node_Id) is
20385 begin
20386 Analyze_And_Resolve (N, Any_Real);
20387
20388 if not Is_OK_Static_Expression (N) then
20389 Flag_Non_Static_Expr
20390 ("bound in real type definition is not static!", N);
20391 Err := True;
20392 end if;
20393 end Analyze_Bound;
20394
20395 -- Start of processing for Process_Real_Range_Specification
20396
20397 begin
20398 if Present (Spec) then
20399 Lo := Low_Bound (Spec);
20400 Hi := High_Bound (Spec);
20401 Analyze_Bound (Lo);
20402 Analyze_Bound (Hi);
20403
20404 -- If error, clear away junk range specification
20405
20406 if Err then
20407 Set_Real_Range_Specification (Def, Empty);
20408 end if;
20409 end if;
20410 end Process_Real_Range_Specification;
20411
20412 ---------------------
20413 -- Process_Subtype --
20414 ---------------------
20415
20416 function Process_Subtype
20417 (S : Node_Id;
20418 Related_Nod : Node_Id;
20419 Related_Id : Entity_Id := Empty;
20420 Suffix : Character := ' ') return Entity_Id
20421 is
20422 P : Node_Id;
20423 Def_Id : Entity_Id;
20424 Error_Node : Node_Id;
20425 Full_View_Id : Entity_Id;
20426 Subtype_Mark_Id : Entity_Id;
20427
20428 May_Have_Null_Exclusion : Boolean;
20429
20430 procedure Check_Incomplete (T : Entity_Id);
20431 -- Called to verify that an incomplete type is not used prematurely
20432
20433 ----------------------
20434 -- Check_Incomplete --
20435 ----------------------
20436
20437 procedure Check_Incomplete (T : Entity_Id) is
20438 begin
20439 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20440
20441 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20442 and then
20443 not (Ada_Version >= Ada_2005
20444 and then
20445 (Nkind (Parent (T)) = N_Subtype_Declaration
20446 or else (Nkind (Parent (T)) = N_Subtype_Indication
20447 and then Nkind (Parent (Parent (T))) =
20448 N_Subtype_Declaration)))
20449 then
20450 Error_Msg_N ("invalid use of type before its full declaration", T);
20451 end if;
20452 end Check_Incomplete;
20453
20454 -- Start of processing for Process_Subtype
20455
20456 begin
20457 -- Case of no constraints present
20458
20459 if Nkind (S) /= N_Subtype_Indication then
20460 Find_Type (S);
20461 Check_Incomplete (S);
20462 P := Parent (S);
20463
20464 -- Ada 2005 (AI-231): Static check
20465
20466 if Ada_Version >= Ada_2005
20467 and then Present (P)
20468 and then Null_Exclusion_Present (P)
20469 and then Nkind (P) /= N_Access_To_Object_Definition
20470 and then not Is_Access_Type (Entity (S))
20471 then
20472 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20473 end if;
20474
20475 -- The following is ugly, can't we have a range or even a flag???
20476
20477 May_Have_Null_Exclusion :=
20478 Nkind_In (P, N_Access_Definition,
20479 N_Access_Function_Definition,
20480 N_Access_Procedure_Definition,
20481 N_Access_To_Object_Definition,
20482 N_Allocator,
20483 N_Component_Definition)
20484 or else
20485 Nkind_In (P, N_Derived_Type_Definition,
20486 N_Discriminant_Specification,
20487 N_Formal_Object_Declaration,
20488 N_Object_Declaration,
20489 N_Object_Renaming_Declaration,
20490 N_Parameter_Specification,
20491 N_Subtype_Declaration);
20492
20493 -- Create an Itype that is a duplicate of Entity (S) but with the
20494 -- null-exclusion attribute.
20495
20496 if May_Have_Null_Exclusion
20497 and then Is_Access_Type (Entity (S))
20498 and then Null_Exclusion_Present (P)
20499
20500 -- No need to check the case of an access to object definition.
20501 -- It is correct to define double not-null pointers.
20502
20503 -- Example:
20504 -- type Not_Null_Int_Ptr is not null access Integer;
20505 -- type Acc is not null access Not_Null_Int_Ptr;
20506
20507 and then Nkind (P) /= N_Access_To_Object_Definition
20508 then
20509 if Can_Never_Be_Null (Entity (S)) then
20510 case Nkind (Related_Nod) is
20511 when N_Full_Type_Declaration =>
20512 if Nkind (Type_Definition (Related_Nod))
20513 in N_Array_Type_Definition
20514 then
20515 Error_Node :=
20516 Subtype_Indication
20517 (Component_Definition
20518 (Type_Definition (Related_Nod)));
20519 else
20520 Error_Node :=
20521 Subtype_Indication (Type_Definition (Related_Nod));
20522 end if;
20523
20524 when N_Subtype_Declaration =>
20525 Error_Node := Subtype_Indication (Related_Nod);
20526
20527 when N_Object_Declaration =>
20528 Error_Node := Object_Definition (Related_Nod);
20529
20530 when N_Component_Declaration =>
20531 Error_Node :=
20532 Subtype_Indication (Component_Definition (Related_Nod));
20533
20534 when N_Allocator =>
20535 Error_Node := Expression (Related_Nod);
20536
20537 when others =>
20538 pragma Assert (False);
20539 Error_Node := Related_Nod;
20540 end case;
20541
20542 Error_Msg_NE
20543 ("`NOT NULL` not allowed (& already excludes null)",
20544 Error_Node,
20545 Entity (S));
20546 end if;
20547
20548 Set_Etype (S,
20549 Create_Null_Excluding_Itype
20550 (T => Entity (S),
20551 Related_Nod => P));
20552 Set_Entity (S, Etype (S));
20553 end if;
20554
20555 return Entity (S);
20556
20557 -- Case of constraint present, so that we have an N_Subtype_Indication
20558 -- node (this node is created only if constraints are present).
20559
20560 else
20561 Find_Type (Subtype_Mark (S));
20562
20563 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20564 and then not
20565 (Nkind (Parent (S)) = N_Subtype_Declaration
20566 and then Is_Itype (Defining_Identifier (Parent (S))))
20567 then
20568 Check_Incomplete (Subtype_Mark (S));
20569 end if;
20570
20571 P := Parent (S);
20572 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20573
20574 -- Explicit subtype declaration case
20575
20576 if Nkind (P) = N_Subtype_Declaration then
20577 Def_Id := Defining_Identifier (P);
20578
20579 -- Explicit derived type definition case
20580
20581 elsif Nkind (P) = N_Derived_Type_Definition then
20582 Def_Id := Defining_Identifier (Parent (P));
20583
20584 -- Implicit case, the Def_Id must be created as an implicit type.
20585 -- The one exception arises in the case of concurrent types, array
20586 -- and access types, where other subsidiary implicit types may be
20587 -- created and must appear before the main implicit type. In these
20588 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20589 -- has not yet been called to create Def_Id.
20590
20591 else
20592 if Is_Array_Type (Subtype_Mark_Id)
20593 or else Is_Concurrent_Type (Subtype_Mark_Id)
20594 or else Is_Access_Type (Subtype_Mark_Id)
20595 then
20596 Def_Id := Empty;
20597
20598 -- For the other cases, we create a new unattached Itype,
20599 -- and set the indication to ensure it gets attached later.
20600
20601 else
20602 Def_Id :=
20603 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20604 end if;
20605 end if;
20606
20607 -- If the kind of constraint is invalid for this kind of type,
20608 -- then give an error, and then pretend no constraint was given.
20609
20610 if not Is_Valid_Constraint_Kind
20611 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20612 then
20613 Error_Msg_N
20614 ("incorrect constraint for this kind of type", Constraint (S));
20615
20616 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20617
20618 -- Set Ekind of orphan itype, to prevent cascaded errors
20619
20620 if Present (Def_Id) then
20621 Set_Ekind (Def_Id, Ekind (Any_Type));
20622 end if;
20623
20624 -- Make recursive call, having got rid of the bogus constraint
20625
20626 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20627 end if;
20628
20629 -- Remaining processing depends on type. Select on Base_Type kind to
20630 -- ensure getting to the concrete type kind in the case of a private
20631 -- subtype (needed when only doing semantic analysis).
20632
20633 case Ekind (Base_Type (Subtype_Mark_Id)) is
20634 when Access_Kind =>
20635
20636 -- If this is a constraint on a class-wide type, discard it.
20637 -- There is currently no way to express a partial discriminant
20638 -- constraint on a type with unknown discriminants. This is
20639 -- a pathology that the ACATS wisely decides not to test.
20640
20641 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20642 if Comes_From_Source (S) then
20643 Error_Msg_N
20644 ("constraint on class-wide type ignored??",
20645 Constraint (S));
20646 end if;
20647
20648 if Nkind (P) = N_Subtype_Declaration then
20649 Set_Subtype_Indication (P,
20650 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20651 end if;
20652
20653 return Subtype_Mark_Id;
20654 end if;
20655
20656 Constrain_Access (Def_Id, S, Related_Nod);
20657
20658 if Expander_Active
20659 and then Is_Itype (Designated_Type (Def_Id))
20660 and then Nkind (Related_Nod) = N_Subtype_Declaration
20661 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20662 then
20663 Build_Itype_Reference
20664 (Designated_Type (Def_Id), Related_Nod);
20665 end if;
20666
20667 when Array_Kind =>
20668 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20669
20670 when Decimal_Fixed_Point_Kind =>
20671 Constrain_Decimal (Def_Id, S);
20672
20673 when Enumeration_Kind =>
20674 Constrain_Enumeration (Def_Id, S);
20675 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20676
20677 when Ordinary_Fixed_Point_Kind =>
20678 Constrain_Ordinary_Fixed (Def_Id, S);
20679
20680 when Float_Kind =>
20681 Constrain_Float (Def_Id, S);
20682
20683 when Integer_Kind =>
20684 Constrain_Integer (Def_Id, S);
20685 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20686
20687 when E_Record_Type |
20688 E_Record_Subtype |
20689 Class_Wide_Kind |
20690 E_Incomplete_Type =>
20691 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20692
20693 if Ekind (Def_Id) = E_Incomplete_Type then
20694 Set_Private_Dependents (Def_Id, New_Elmt_List);
20695 end if;
20696
20697 when Private_Kind =>
20698 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20699 Set_Private_Dependents (Def_Id, New_Elmt_List);
20700
20701 -- In case of an invalid constraint prevent further processing
20702 -- since the type constructed is missing expected fields.
20703
20704 if Etype (Def_Id) = Any_Type then
20705 return Def_Id;
20706 end if;
20707
20708 -- If the full view is that of a task with discriminants,
20709 -- we must constrain both the concurrent type and its
20710 -- corresponding record type. Otherwise we will just propagate
20711 -- the constraint to the full view, if available.
20712
20713 if Present (Full_View (Subtype_Mark_Id))
20714 and then Has_Discriminants (Subtype_Mark_Id)
20715 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
20716 then
20717 Full_View_Id :=
20718 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20719
20720 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
20721 Constrain_Concurrent (Full_View_Id, S,
20722 Related_Nod, Related_Id, Suffix);
20723 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
20724 Set_Full_View (Def_Id, Full_View_Id);
20725
20726 -- Introduce an explicit reference to the private subtype,
20727 -- to prevent scope anomalies in gigi if first use appears
20728 -- in a nested context, e.g. a later function body.
20729 -- Should this be generated in other contexts than a full
20730 -- type declaration?
20731
20732 if Is_Itype (Def_Id)
20733 and then
20734 Nkind (Parent (P)) = N_Full_Type_Declaration
20735 then
20736 Build_Itype_Reference (Def_Id, Parent (P));
20737 end if;
20738
20739 else
20740 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
20741 end if;
20742
20743 when Concurrent_Kind =>
20744 Constrain_Concurrent (Def_Id, S,
20745 Related_Nod, Related_Id, Suffix);
20746
20747 when others =>
20748 Error_Msg_N ("invalid subtype mark in subtype indication", S);
20749 end case;
20750
20751 -- Size and Convention are always inherited from the base type
20752
20753 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
20754 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
20755
20756 return Def_Id;
20757 end if;
20758 end Process_Subtype;
20759
20760 --------------------------------------------
20761 -- Propagate_Default_Init_Cond_Attributes --
20762 --------------------------------------------
20763
20764 procedure Propagate_Default_Init_Cond_Attributes
20765 (From_Typ : Entity_Id;
20766 To_Typ : Entity_Id;
20767 Parent_To_Derivation : Boolean := False;
20768 Private_To_Full_View : Boolean := False)
20769 is
20770 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id);
20771 -- Remove the default initial procedure (if any) from the rep chain of
20772 -- type Typ.
20773
20774 ----------------------------------------
20775 -- Remove_Default_Init_Cond_Procedure --
20776 ----------------------------------------
20777
20778 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id) is
20779 Found : Boolean := False;
20780 Prev : Entity_Id;
20781 Subp : Entity_Id;
20782
20783 begin
20784 Prev := Typ;
20785 Subp := Subprograms_For_Type (Typ);
20786 while Present (Subp) loop
20787 if Is_Default_Init_Cond_Procedure (Subp) then
20788 Found := True;
20789 exit;
20790 end if;
20791
20792 Prev := Subp;
20793 Subp := Subprograms_For_Type (Subp);
20794 end loop;
20795
20796 if Found then
20797 Set_Subprograms_For_Type (Prev, Subprograms_For_Type (Subp));
20798 Set_Subprograms_For_Type (Subp, Empty);
20799 end if;
20800 end Remove_Default_Init_Cond_Procedure;
20801
20802 -- Local variables
20803
20804 Inherit_Procedure : Boolean := False;
20805
20806 -- Start of processing for Propagate_Default_Init_Cond_Attributes
20807
20808 begin
20809 if Has_Default_Init_Cond (From_Typ) then
20810
20811 -- A derived type inherits the attributes from its parent type
20812
20813 if Parent_To_Derivation then
20814 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20815
20816 -- A full view shares the attributes with its private view
20817
20818 else
20819 Set_Has_Default_Init_Cond (To_Typ);
20820 end if;
20821
20822 Inherit_Procedure := True;
20823
20824 -- Due to the order of expansion, a derived private type is processed
20825 -- by two routines which both attempt to set the attributes related
20826 -- to pragma Default_Initial_Condition - Build_Derived_Type and then
20827 -- Process_Full_View.
20828
20829 -- package Pack is
20830 -- type Parent_Typ is private
20831 -- with Default_Initial_Condition ...;
20832 -- private
20833 -- type Parent_Typ is ...;
20834 -- end Pack;
20835
20836 -- with Pack; use Pack;
20837 -- package Pack_2 is
20838 -- type Deriv_Typ is private
20839 -- with Default_Initial_Condition ...;
20840 -- private
20841 -- type Deriv_Typ is new Parent_Typ;
20842 -- end Pack_2;
20843
20844 -- When Build_Derived_Type operates, it sets the attributes on the
20845 -- full view without taking into account that the private view may
20846 -- define its own default initial condition procedure. This becomes
20847 -- apparent in Process_Full_View which must undo some of the work by
20848 -- Build_Derived_Type and propagate the attributes from the private
20849 -- to the full view.
20850
20851 if Private_To_Full_View then
20852 Set_Has_Inherited_Default_Init_Cond (To_Typ, False);
20853 Remove_Default_Init_Cond_Procedure (To_Typ);
20854 end if;
20855
20856 -- A type must inherit the default initial condition procedure from a
20857 -- parent type when the parent itself is inheriting the procedure or
20858 -- when it is defining one. This circuitry is also used when dealing
20859 -- with the private / full view of a type.
20860
20861 elsif Has_Inherited_Default_Init_Cond (From_Typ)
20862 or (Parent_To_Derivation
20863 and Present (Get_Pragma
20864 (From_Typ, Pragma_Default_Initial_Condition)))
20865 then
20866 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20867 Inherit_Procedure := True;
20868 end if;
20869
20870 if Inherit_Procedure
20871 and then No (Default_Init_Cond_Procedure (To_Typ))
20872 then
20873 Set_Default_Init_Cond_Procedure
20874 (To_Typ, Default_Init_Cond_Procedure (From_Typ));
20875 end if;
20876 end Propagate_Default_Init_Cond_Attributes;
20877
20878 -----------------------------
20879 -- Record_Type_Declaration --
20880 -----------------------------
20881
20882 procedure Record_Type_Declaration
20883 (T : Entity_Id;
20884 N : Node_Id;
20885 Prev : Entity_Id)
20886 is
20887 Def : constant Node_Id := Type_Definition (N);
20888 Is_Tagged : Boolean;
20889 Tag_Comp : Entity_Id;
20890
20891 begin
20892 -- These flags must be initialized before calling Process_Discriminants
20893 -- because this routine makes use of them.
20894
20895 Set_Ekind (T, E_Record_Type);
20896 Set_Etype (T, T);
20897 Init_Size_Align (T);
20898 Set_Interfaces (T, No_Elist);
20899 Set_Stored_Constraint (T, No_Elist);
20900 Set_Default_SSO (T);
20901
20902 -- Normal case
20903
20904 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
20905 if Limited_Present (Def) then
20906 Check_SPARK_05_Restriction ("limited is not allowed", N);
20907 end if;
20908
20909 if Abstract_Present (Def) then
20910 Check_SPARK_05_Restriction ("abstract is not allowed", N);
20911 end if;
20912
20913 -- The flag Is_Tagged_Type might have already been set by
20914 -- Find_Type_Name if it detected an error for declaration T. This
20915 -- arises in the case of private tagged types where the full view
20916 -- omits the word tagged.
20917
20918 Is_Tagged :=
20919 Tagged_Present (Def)
20920 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
20921
20922 Set_Is_Limited_Record (T, Limited_Present (Def));
20923
20924 if Is_Tagged then
20925 Set_Is_Tagged_Type (T, True);
20926 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
20927 end if;
20928
20929 -- Type is abstract if full declaration carries keyword, or if
20930 -- previous partial view did.
20931
20932 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
20933 or else Abstract_Present (Def));
20934
20935 else
20936 Check_SPARK_05_Restriction ("interface is not allowed", N);
20937
20938 Is_Tagged := True;
20939 Analyze_Interface_Declaration (T, Def);
20940
20941 if Present (Discriminant_Specifications (N)) then
20942 Error_Msg_N
20943 ("interface types cannot have discriminants",
20944 Defining_Identifier
20945 (First (Discriminant_Specifications (N))));
20946 end if;
20947 end if;
20948
20949 -- First pass: if there are self-referential access components,
20950 -- create the required anonymous access type declarations, and if
20951 -- need be an incomplete type declaration for T itself.
20952
20953 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
20954
20955 if Ada_Version >= Ada_2005
20956 and then Present (Interface_List (Def))
20957 then
20958 Check_Interfaces (N, Def);
20959
20960 declare
20961 Ifaces_List : Elist_Id;
20962
20963 begin
20964 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
20965 -- already in the parents.
20966
20967 Collect_Interfaces
20968 (T => T,
20969 Ifaces_List => Ifaces_List,
20970 Exclude_Parents => True);
20971
20972 Set_Interfaces (T, Ifaces_List);
20973 end;
20974 end if;
20975
20976 -- Records constitute a scope for the component declarations within.
20977 -- The scope is created prior to the processing of these declarations.
20978 -- Discriminants are processed first, so that they are visible when
20979 -- processing the other components. The Ekind of the record type itself
20980 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
20981
20982 -- Enter record scope
20983
20984 Push_Scope (T);
20985
20986 -- If an incomplete or private type declaration was already given for
20987 -- the type, then this scope already exists, and the discriminants have
20988 -- been declared within. We must verify that the full declaration
20989 -- matches the incomplete one.
20990
20991 Check_Or_Process_Discriminants (N, T, Prev);
20992
20993 Set_Is_Constrained (T, not Has_Discriminants (T));
20994 Set_Has_Delayed_Freeze (T, True);
20995
20996 -- For tagged types add a manually analyzed component corresponding
20997 -- to the component _tag, the corresponding piece of tree will be
20998 -- expanded as part of the freezing actions if it is not a CPP_Class.
20999
21000 if Is_Tagged then
21001
21002 -- Do not add the tag unless we are in expansion mode
21003
21004 if Expander_Active then
21005 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21006 Enter_Name (Tag_Comp);
21007
21008 Set_Ekind (Tag_Comp, E_Component);
21009 Set_Is_Tag (Tag_Comp);
21010 Set_Is_Aliased (Tag_Comp);
21011 Set_Etype (Tag_Comp, RTE (RE_Tag));
21012 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21013 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21014 Init_Component_Location (Tag_Comp);
21015
21016 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21017 -- implemented interfaces.
21018
21019 if Has_Interfaces (T) then
21020 Add_Interface_Tag_Components (N, T);
21021 end if;
21022 end if;
21023
21024 Make_Class_Wide_Type (T);
21025 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21026 end if;
21027
21028 -- We must suppress range checks when processing record components in
21029 -- the presence of discriminants, since we don't want spurious checks to
21030 -- be generated during their analysis, but Suppress_Range_Checks flags
21031 -- must be reset the after processing the record definition.
21032
21033 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21034 -- couldn't we just use the normal range check suppression method here.
21035 -- That would seem cleaner ???
21036
21037 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21038 Set_Kill_Range_Checks (T, True);
21039 Record_Type_Definition (Def, Prev);
21040 Set_Kill_Range_Checks (T, False);
21041 else
21042 Record_Type_Definition (Def, Prev);
21043 end if;
21044
21045 -- Exit from record scope
21046
21047 End_Scope;
21048
21049 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21050 -- the implemented interfaces and associate them an aliased entity.
21051
21052 if Is_Tagged
21053 and then not Is_Empty_List (Interface_List (Def))
21054 then
21055 Derive_Progenitor_Subprograms (T, T);
21056 end if;
21057
21058 Check_Function_Writable_Actuals (N);
21059 end Record_Type_Declaration;
21060
21061 ----------------------------
21062 -- Record_Type_Definition --
21063 ----------------------------
21064
21065 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21066 Component : Entity_Id;
21067 Ctrl_Components : Boolean := False;
21068 Final_Storage_Only : Boolean;
21069 T : Entity_Id;
21070
21071 begin
21072 if Ekind (Prev_T) = E_Incomplete_Type then
21073 T := Full_View (Prev_T);
21074 else
21075 T := Prev_T;
21076 end if;
21077
21078 -- In SPARK, tagged types and type extensions may only be declared in
21079 -- the specification of library unit packages.
21080
21081 if Present (Def) and then Is_Tagged_Type (T) then
21082 declare
21083 Typ : Node_Id;
21084 Ctxt : Node_Id;
21085
21086 begin
21087 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21088 Typ := Parent (Def);
21089 else
21090 pragma Assert
21091 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21092 Typ := Parent (Parent (Def));
21093 end if;
21094
21095 Ctxt := Parent (Typ);
21096
21097 if Nkind (Ctxt) = N_Package_Body
21098 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21099 then
21100 Check_SPARK_05_Restriction
21101 ("type should be defined in package specification", Typ);
21102
21103 elsif Nkind (Ctxt) /= N_Package_Specification
21104 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21105 then
21106 Check_SPARK_05_Restriction
21107 ("type should be defined in library unit package", Typ);
21108 end if;
21109 end;
21110 end if;
21111
21112 Final_Storage_Only := not Is_Controlled (T);
21113
21114 -- Ada 2005: Check whether an explicit Limited is present in a derived
21115 -- type declaration.
21116
21117 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21118 and then Limited_Present (Parent (Def))
21119 then
21120 Set_Is_Limited_Record (T);
21121 end if;
21122
21123 -- If the component list of a record type is defined by the reserved
21124 -- word null and there is no discriminant part, then the record type has
21125 -- no components and all records of the type are null records (RM 3.7)
21126 -- This procedure is also called to process the extension part of a
21127 -- record extension, in which case the current scope may have inherited
21128 -- components.
21129
21130 if No (Def)
21131 or else No (Component_List (Def))
21132 or else Null_Present (Component_List (Def))
21133 then
21134 if not Is_Tagged_Type (T) then
21135 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21136 end if;
21137
21138 else
21139 Analyze_Declarations (Component_Items (Component_List (Def)));
21140
21141 if Present (Variant_Part (Component_List (Def))) then
21142 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21143 Analyze (Variant_Part (Component_List (Def)));
21144 end if;
21145 end if;
21146
21147 -- After completing the semantic analysis of the record definition,
21148 -- record components, both new and inherited, are accessible. Set their
21149 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21150 -- whose Ekind may be void.
21151
21152 Component := First_Entity (Current_Scope);
21153 while Present (Component) loop
21154 if Ekind (Component) = E_Void
21155 and then not Is_Itype (Component)
21156 then
21157 Set_Ekind (Component, E_Component);
21158 Init_Component_Location (Component);
21159 end if;
21160
21161 if Has_Task (Etype (Component)) then
21162 Set_Has_Task (T);
21163 end if;
21164
21165 if Has_Protected (Etype (Component)) then
21166 Set_Has_Protected (T);
21167 end if;
21168
21169 if Ekind (Component) /= E_Component then
21170 null;
21171
21172 -- Do not set Has_Controlled_Component on a class-wide equivalent
21173 -- type. See Make_CW_Equivalent_Type.
21174
21175 elsif not Is_Class_Wide_Equivalent_Type (T)
21176 and then (Has_Controlled_Component (Etype (Component))
21177 or else (Chars (Component) /= Name_uParent
21178 and then Is_Controlled (Etype (Component))))
21179 then
21180 Set_Has_Controlled_Component (T, True);
21181 Final_Storage_Only :=
21182 Final_Storage_Only
21183 and then Finalize_Storage_Only (Etype (Component));
21184 Ctrl_Components := True;
21185 end if;
21186
21187 Next_Entity (Component);
21188 end loop;
21189
21190 -- A Type is Finalize_Storage_Only only if all its controlled components
21191 -- are also.
21192
21193 if Ctrl_Components then
21194 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21195 end if;
21196
21197 -- Place reference to end record on the proper entity, which may
21198 -- be a partial view.
21199
21200 if Present (Def) then
21201 Process_End_Label (Def, 'e', Prev_T);
21202 end if;
21203 end Record_Type_Definition;
21204
21205 ------------------------
21206 -- Replace_Components --
21207 ------------------------
21208
21209 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21210 function Process (N : Node_Id) return Traverse_Result;
21211
21212 -------------
21213 -- Process --
21214 -------------
21215
21216 function Process (N : Node_Id) return Traverse_Result is
21217 Comp : Entity_Id;
21218
21219 begin
21220 if Nkind (N) = N_Discriminant_Specification then
21221 Comp := First_Discriminant (Typ);
21222 while Present (Comp) loop
21223 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21224 Set_Defining_Identifier (N, Comp);
21225 exit;
21226 end if;
21227
21228 Next_Discriminant (Comp);
21229 end loop;
21230
21231 elsif Nkind (N) = N_Component_Declaration then
21232 Comp := First_Component (Typ);
21233 while Present (Comp) loop
21234 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21235 Set_Defining_Identifier (N, Comp);
21236 exit;
21237 end if;
21238
21239 Next_Component (Comp);
21240 end loop;
21241 end if;
21242
21243 return OK;
21244 end Process;
21245
21246 procedure Replace is new Traverse_Proc (Process);
21247
21248 -- Start of processing for Replace_Components
21249
21250 begin
21251 Replace (Decl);
21252 end Replace_Components;
21253
21254 -------------------------------
21255 -- Set_Completion_Referenced --
21256 -------------------------------
21257
21258 procedure Set_Completion_Referenced (E : Entity_Id) is
21259 begin
21260 -- If in main unit, mark entity that is a completion as referenced,
21261 -- warnings go on the partial view when needed.
21262
21263 if In_Extended_Main_Source_Unit (E) then
21264 Set_Referenced (E);
21265 end if;
21266 end Set_Completion_Referenced;
21267
21268 ---------------------
21269 -- Set_Default_SSO --
21270 ---------------------
21271
21272 procedure Set_Default_SSO (T : Entity_Id) is
21273 begin
21274 case Opt.Default_SSO is
21275 when ' ' =>
21276 null;
21277 when 'L' =>
21278 Set_SSO_Set_Low_By_Default (T, True);
21279 when 'H' =>
21280 Set_SSO_Set_High_By_Default (T, True);
21281 when others =>
21282 raise Program_Error;
21283 end case;
21284 end Set_Default_SSO;
21285
21286 ---------------------
21287 -- Set_Fixed_Range --
21288 ---------------------
21289
21290 -- The range for fixed-point types is complicated by the fact that we
21291 -- do not know the exact end points at the time of the declaration. This
21292 -- is true for three reasons:
21293
21294 -- A size clause may affect the fudging of the end-points.
21295 -- A small clause may affect the values of the end-points.
21296 -- We try to include the end-points if it does not affect the size.
21297
21298 -- This means that the actual end-points must be established at the
21299 -- point when the type is frozen. Meanwhile, we first narrow the range
21300 -- as permitted (so that it will fit if necessary in a small specified
21301 -- size), and then build a range subtree with these narrowed bounds.
21302 -- Set_Fixed_Range constructs the range from real literal values, and
21303 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21304
21305 -- The parent of this range is set to point to the entity so that it is
21306 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21307 -- other scalar types, which are just pointers to the range in the
21308 -- original tree, this would otherwise be an orphan).
21309
21310 -- The tree is left unanalyzed. When the type is frozen, the processing
21311 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21312 -- analyzed, and uses this as an indication that it should complete
21313 -- work on the range (it will know the final small and size values).
21314
21315 procedure Set_Fixed_Range
21316 (E : Entity_Id;
21317 Loc : Source_Ptr;
21318 Lo : Ureal;
21319 Hi : Ureal)
21320 is
21321 S : constant Node_Id :=
21322 Make_Range (Loc,
21323 Low_Bound => Make_Real_Literal (Loc, Lo),
21324 High_Bound => Make_Real_Literal (Loc, Hi));
21325 begin
21326 Set_Scalar_Range (E, S);
21327 Set_Parent (S, E);
21328
21329 -- Before the freeze point, the bounds of a fixed point are universal
21330 -- and carry the corresponding type.
21331
21332 Set_Etype (Low_Bound (S), Universal_Real);
21333 Set_Etype (High_Bound (S), Universal_Real);
21334 end Set_Fixed_Range;
21335
21336 ----------------------------------
21337 -- Set_Scalar_Range_For_Subtype --
21338 ----------------------------------
21339
21340 procedure Set_Scalar_Range_For_Subtype
21341 (Def_Id : Entity_Id;
21342 R : Node_Id;
21343 Subt : Entity_Id)
21344 is
21345 Kind : constant Entity_Kind := Ekind (Def_Id);
21346
21347 begin
21348 -- Defend against previous error
21349
21350 if Nkind (R) = N_Error then
21351 return;
21352 end if;
21353
21354 Set_Scalar_Range (Def_Id, R);
21355
21356 -- We need to link the range into the tree before resolving it so
21357 -- that types that are referenced, including importantly the subtype
21358 -- itself, are properly frozen (Freeze_Expression requires that the
21359 -- expression be properly linked into the tree). Of course if it is
21360 -- already linked in, then we do not disturb the current link.
21361
21362 if No (Parent (R)) then
21363 Set_Parent (R, Def_Id);
21364 end if;
21365
21366 -- Reset the kind of the subtype during analysis of the range, to
21367 -- catch possible premature use in the bounds themselves.
21368
21369 Set_Ekind (Def_Id, E_Void);
21370 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21371 Set_Ekind (Def_Id, Kind);
21372 end Set_Scalar_Range_For_Subtype;
21373
21374 --------------------------------------------------------
21375 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21376 --------------------------------------------------------
21377
21378 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21379 (E : Entity_Id)
21380 is
21381 begin
21382 -- Make sure set if encountered during Expand_To_Stored_Constraint
21383
21384 Set_Stored_Constraint (E, No_Elist);
21385
21386 -- Give it the right value
21387
21388 if Is_Constrained (E) and then Has_Discriminants (E) then
21389 Set_Stored_Constraint (E,
21390 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21391 end if;
21392 end Set_Stored_Constraint_From_Discriminant_Constraint;
21393
21394 -------------------------------------
21395 -- Signed_Integer_Type_Declaration --
21396 -------------------------------------
21397
21398 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21399 Implicit_Base : Entity_Id;
21400 Base_Typ : Entity_Id;
21401 Lo_Val : Uint;
21402 Hi_Val : Uint;
21403 Errs : Boolean := False;
21404 Lo : Node_Id;
21405 Hi : Node_Id;
21406
21407 function Can_Derive_From (E : Entity_Id) return Boolean;
21408 -- Determine whether given bounds allow derivation from specified type
21409
21410 procedure Check_Bound (Expr : Node_Id);
21411 -- Check bound to make sure it is integral and static. If not, post
21412 -- appropriate error message and set Errs flag
21413
21414 ---------------------
21415 -- Can_Derive_From --
21416 ---------------------
21417
21418 -- Note we check both bounds against both end values, to deal with
21419 -- strange types like ones with a range of 0 .. -12341234.
21420
21421 function Can_Derive_From (E : Entity_Id) return Boolean is
21422 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21423 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21424 begin
21425 return Lo <= Lo_Val and then Lo_Val <= Hi
21426 and then
21427 Lo <= Hi_Val and then Hi_Val <= Hi;
21428 end Can_Derive_From;
21429
21430 -----------------
21431 -- Check_Bound --
21432 -----------------
21433
21434 procedure Check_Bound (Expr : Node_Id) is
21435 begin
21436 -- If a range constraint is used as an integer type definition, each
21437 -- bound of the range must be defined by a static expression of some
21438 -- integer type, but the two bounds need not have the same integer
21439 -- type (Negative bounds are allowed.) (RM 3.5.4)
21440
21441 if not Is_Integer_Type (Etype (Expr)) then
21442 Error_Msg_N
21443 ("integer type definition bounds must be of integer type", Expr);
21444 Errs := True;
21445
21446 elsif not Is_OK_Static_Expression (Expr) then
21447 Flag_Non_Static_Expr
21448 ("non-static expression used for integer type bound!", Expr);
21449 Errs := True;
21450
21451 -- The bounds are folded into literals, and we set their type to be
21452 -- universal, to avoid typing difficulties: we cannot set the type
21453 -- of the literal to the new type, because this would be a forward
21454 -- reference for the back end, and if the original type is user-
21455 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21456
21457 else
21458 if Is_Entity_Name (Expr) then
21459 Fold_Uint (Expr, Expr_Value (Expr), True);
21460 end if;
21461
21462 Set_Etype (Expr, Universal_Integer);
21463 end if;
21464 end Check_Bound;
21465
21466 -- Start of processing for Signed_Integer_Type_Declaration
21467
21468 begin
21469 -- Create an anonymous base type
21470
21471 Implicit_Base :=
21472 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21473
21474 -- Analyze and check the bounds, they can be of any integer type
21475
21476 Lo := Low_Bound (Def);
21477 Hi := High_Bound (Def);
21478
21479 -- Arbitrarily use Integer as the type if either bound had an error
21480
21481 if Hi = Error or else Lo = Error then
21482 Base_Typ := Any_Integer;
21483 Set_Error_Posted (T, True);
21484
21485 -- Here both bounds are OK expressions
21486
21487 else
21488 Analyze_And_Resolve (Lo, Any_Integer);
21489 Analyze_And_Resolve (Hi, Any_Integer);
21490
21491 Check_Bound (Lo);
21492 Check_Bound (Hi);
21493
21494 if Errs then
21495 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21496 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21497 end if;
21498
21499 -- Find type to derive from
21500
21501 Lo_Val := Expr_Value (Lo);
21502 Hi_Val := Expr_Value (Hi);
21503
21504 if Can_Derive_From (Standard_Short_Short_Integer) then
21505 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21506
21507 elsif Can_Derive_From (Standard_Short_Integer) then
21508 Base_Typ := Base_Type (Standard_Short_Integer);
21509
21510 elsif Can_Derive_From (Standard_Integer) then
21511 Base_Typ := Base_Type (Standard_Integer);
21512
21513 elsif Can_Derive_From (Standard_Long_Integer) then
21514 Base_Typ := Base_Type (Standard_Long_Integer);
21515
21516 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21517 Check_Restriction (No_Long_Long_Integers, Def);
21518 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21519
21520 else
21521 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21522 Error_Msg_N ("integer type definition bounds out of range", Def);
21523 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21524 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21525 end if;
21526 end if;
21527
21528 -- Complete both implicit base and declared first subtype entities. The
21529 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21530 -- are not clobbered when the signed integer type acts as a full view of
21531 -- a private type.
21532
21533 Set_Etype (Implicit_Base, Base_Typ);
21534 Set_Size_Info (Implicit_Base, Base_Typ);
21535 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21536 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21537 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21538
21539 Set_Ekind (T, E_Signed_Integer_Subtype);
21540 Set_Etype (T, Implicit_Base);
21541 Set_Size_Info (T, Implicit_Base);
21542 Inherit_Rep_Item_Chain (T, Implicit_Base);
21543 Set_Scalar_Range (T, Def);
21544 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21545 Set_Is_Constrained (T);
21546 end Signed_Integer_Type_Declaration;
21547
21548 end Sem_Ch3;
This page took 1.085925 seconds and 5 git commands to generate.