c95f5da65352f7d3900a49199336281e5f1e2adf
[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-2008, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Elists; use Elists;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Eval_Fat; use Eval_Fat;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch9; use Exp_Ch9;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
42 with Layout; use Layout;
43 with Lib; use Lib;
44 with Lib.Xref; use Lib.Xref;
45 with Namet; use Namet;
46 with Nmake; use Nmake;
47 with Opt; use Opt;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Sem; use Sem;
52 with Sem_Case; use Sem_Case;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch7; use Sem_Ch7;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch13; use Sem_Ch13;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Dist; use Sem_Dist;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Mech; use Sem_Mech;
63 with Sem_Res; use Sem_Res;
64 with Sem_Smem; use Sem_Smem;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sem_Warn; use Sem_Warn;
68 with Stand; use Stand;
69 with Sinfo; use Sinfo;
70 with Snames; use Snames;
71 with Targparm; use Targparm;
72 with Tbuild; use Tbuild;
73 with Ttypes; use Ttypes;
74 with Uintp; use Uintp;
75 with Urealp; use Urealp;
76
77 package body Sem_Ch3 is
78
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
82
83 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
84 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
85 -- abstract interface types implemented by a record type or a derived
86 -- record type.
87
88 procedure Build_Derived_Type
89 (N : Node_Id;
90 Parent_Type : Entity_Id;
91 Derived_Type : Entity_Id;
92 Is_Completion : Boolean;
93 Derive_Subps : Boolean := True);
94 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
95 -- the N_Full_Type_Declaration node containing the derived type definition.
96 -- Parent_Type is the entity for the parent type in the derived type
97 -- definition and Derived_Type the actual derived type. Is_Completion must
98 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
99 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
100 -- completion of a private type declaration. If Is_Completion is set to
101 -- True, N is the completion of a private type declaration and Derived_Type
102 -- is different from the defining identifier inside N (i.e. Derived_Type /=
103 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
104 -- subprograms should be derived. The only case where this parameter is
105 -- False is when Build_Derived_Type is recursively called to process an
106 -- implicit derived full type for a type derived from a private type (in
107 -- that case the subprograms must only be derived for the private view of
108 -- the type).
109 --
110 -- ??? These flags need a bit of re-examination and re-documentation:
111 -- ??? are they both necessary (both seem related to the recursion)?
112
113 procedure Build_Derived_Access_Type
114 (N : Node_Id;
115 Parent_Type : Entity_Id;
116 Derived_Type : Entity_Id);
117 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
118 -- create an implicit base if the parent type is constrained or if the
119 -- subtype indication has a constraint.
120
121 procedure Build_Derived_Array_Type
122 (N : Node_Id;
123 Parent_Type : Entity_Id;
124 Derived_Type : Entity_Id);
125 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
126 -- create an implicit base if the parent type is constrained or if the
127 -- subtype indication has a constraint.
128
129 procedure Build_Derived_Concurrent_Type
130 (N : Node_Id;
131 Parent_Type : Entity_Id;
132 Derived_Type : Entity_Id);
133 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
134 -- protected type, inherit entries and protected subprograms, check
135 -- legality of discriminant constraints if any.
136
137 procedure Build_Derived_Enumeration_Type
138 (N : Node_Id;
139 Parent_Type : Entity_Id;
140 Derived_Type : Entity_Id);
141 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
142 -- type, we must create a new list of literals. Types derived from
143 -- Character and Wide_Character are special-cased.
144
145 procedure Build_Derived_Numeric_Type
146 (N : Node_Id;
147 Parent_Type : Entity_Id;
148 Derived_Type : Entity_Id);
149 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
150 -- an anonymous base type, and propagate constraint to subtype if needed.
151
152 procedure Build_Derived_Private_Type
153 (N : Node_Id;
154 Parent_Type : Entity_Id;
155 Derived_Type : Entity_Id;
156 Is_Completion : Boolean;
157 Derive_Subps : Boolean := True);
158 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
159 -- because the parent may or may not have a completion, and the derivation
160 -- may itself be a completion.
161
162 procedure Build_Derived_Record_Type
163 (N : Node_Id;
164 Parent_Type : Entity_Id;
165 Derived_Type : Entity_Id;
166 Derive_Subps : Boolean := True);
167 -- Subsidiary procedure for Build_Derived_Type and
168 -- Analyze_Private_Extension_Declaration used for tagged and untagged
169 -- record types. All parameters are as in Build_Derived_Type except that
170 -- N, in addition to being an N_Full_Type_Declaration node, can also be an
171 -- N_Private_Extension_Declaration node. See the definition of this routine
172 -- for much more info. Derive_Subps indicates whether subprograms should
173 -- be derived from the parent type. The only case where Derive_Subps is
174 -- False is for an implicit derived full type for a type derived from a
175 -- private type (see Build_Derived_Type).
176
177 procedure Build_Discriminal (Discrim : Entity_Id);
178 -- Create the discriminal corresponding to discriminant Discrim, that is
179 -- the parameter corresponding to Discrim to be used in initialization
180 -- procedures for the type where Discrim is a discriminant. Discriminals
181 -- are not used during semantic analysis, and are not fully defined
182 -- entities until expansion. Thus they are not given a scope until
183 -- initialization procedures are built.
184
185 function Build_Discriminant_Constraints
186 (T : Entity_Id;
187 Def : Node_Id;
188 Derived_Def : Boolean := False) return Elist_Id;
189 -- Validate discriminant constraints and return the list of the constraints
190 -- in order of discriminant declarations, where T is the discriminated
191 -- unconstrained type. Def is the N_Subtype_Indication node where the
192 -- discriminants constraints for T are specified. Derived_Def is True
193 -- when building the discriminant constraints in a derived type definition
194 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
195 -- type and Def is the constraint "(xxx)" on T and this routine sets the
196 -- Corresponding_Discriminant field of the discriminants in the derived
197 -- type D to point to the corresponding discriminants in the parent type T.
198
199 procedure Build_Discriminated_Subtype
200 (T : Entity_Id;
201 Def_Id : Entity_Id;
202 Elist : Elist_Id;
203 Related_Nod : Node_Id;
204 For_Access : Boolean := False);
205 -- Subsidiary procedure to Constrain_Discriminated_Type and to
206 -- Process_Incomplete_Dependents. Given
207 --
208 -- T (a possibly discriminated base type)
209 -- Def_Id (a very partially built subtype for T),
210 --
211 -- the call completes Def_Id to be the appropriate E_*_Subtype.
212 --
213 -- The Elist is the list of discriminant constraints if any (it is set
214 -- to No_Elist if T is not a discriminated type, and to an empty list if
215 -- T has discriminants but there are no discriminant constraints). The
216 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
217 -- The For_Access says whether or not this subtype is really constraining
218 -- an access type. That is its sole purpose is the designated type of an
219 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
220 -- is built to avoid freezing T when the access subtype is frozen.
221
222 function Build_Scalar_Bound
223 (Bound : Node_Id;
224 Par_T : Entity_Id;
225 Der_T : Entity_Id) return Node_Id;
226 -- The bounds of a derived scalar type are conversions of the bounds of
227 -- the parent type. Optimize the representation if the bounds are literals.
228 -- Needs a more complete spec--what are the parameters exactly, and what
229 -- exactly is the returned value, and how is Bound affected???
230
231 procedure Build_Itype_Reference
232 (Ityp : Entity_Id;
233 Nod : Node_Id);
234 -- Create a reference to an internal type, for use by Gigi. The back-end
235 -- elaborates itypes on demand, i.e. when their first use is seen. This
236 -- can lead to scope anomalies if the first use is within a scope that is
237 -- nested within the scope that contains the point of definition of the
238 -- itype. The Itype_Reference node forces the elaboration of the itype
239 -- in the proper scope. The node is inserted after Nod, which is the
240 -- enclosing declaration that generated Ityp.
241 --
242 -- A related mechanism is used during expansion, for itypes created in
243 -- branches of conditionals. See Ensure_Defined in exp_util.
244 -- Could both mechanisms be merged ???
245
246 procedure Build_Underlying_Full_View
247 (N : Node_Id;
248 Typ : Entity_Id;
249 Par : Entity_Id);
250 -- If the completion of a private type is itself derived from a private
251 -- type, or if the full view of a private subtype is itself private, the
252 -- back-end has no way to compute the actual size of this type. We build
253 -- an internal subtype declaration of the proper parent type to convey
254 -- this information. This extra mechanism is needed because a full
255 -- view cannot itself have a full view (it would get clobbered during
256 -- view exchanges).
257
258 procedure Check_Access_Discriminant_Requires_Limited
259 (D : Node_Id;
260 Loc : Node_Id);
261 -- Check the restriction that the type to which an access discriminant
262 -- belongs must be a concurrent type or a descendant of a type with
263 -- the reserved word 'limited' in its declaration.
264
265 procedure Check_Anonymous_Access_Components
266 (Typ_Decl : Node_Id;
267 Typ : Entity_Id;
268 Prev : Entity_Id;
269 Comp_List : Node_Id);
270 -- Ada 2005 AI-382: an access component in a record definition can refer to
271 -- the enclosing record, in which case it denotes the type itself, and not
272 -- the current instance of the type. We create an anonymous access type for
273 -- the component, and flag it as an access to a component, so accessibility
274 -- checks are properly performed on it. The declaration of the access type
275 -- is placed ahead of that of the record to prevent order-of-elaboration
276 -- circularity issues in Gigi. We create an incomplete type for the record
277 -- declaration, which is the designated type of the anonymous access.
278
279 procedure Check_Delta_Expression (E : Node_Id);
280 -- Check that the expression represented by E is suitable for use as a
281 -- delta expression, i.e. it is of real type and is static.
282
283 procedure Check_Digits_Expression (E : Node_Id);
284 -- Check that the expression represented by E is suitable for use as a
285 -- digits expression, i.e. it is of integer type, positive and static.
286
287 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
288 -- Validate the initialization of an object declaration. T is the required
289 -- type, and Exp is the initialization expression.
290
291 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
292 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
293
294 procedure Check_Or_Process_Discriminants
295 (N : Node_Id;
296 T : Entity_Id;
297 Prev : Entity_Id := Empty);
298 -- If T is the full declaration of an incomplete or private type, check the
299 -- conformance of the discriminants, otherwise process them. Prev is the
300 -- entity of the partial declaration, if any.
301
302 procedure Check_Real_Bound (Bound : Node_Id);
303 -- Check given bound for being of real type and static. If not, post an
304 -- appropriate message, and rewrite the bound with the real literal zero.
305
306 procedure Constant_Redeclaration
307 (Id : Entity_Id;
308 N : Node_Id;
309 T : out Entity_Id);
310 -- Various checks on legality of full declaration of deferred constant.
311 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
312 -- node. The caller has not yet set any attributes of this entity.
313
314 function Contain_Interface
315 (Iface : Entity_Id;
316 Ifaces : Elist_Id) return Boolean;
317 -- Ada 2005: Determine whether Iface is present in the list Ifaces
318
319 procedure Convert_Scalar_Bounds
320 (N : Node_Id;
321 Parent_Type : Entity_Id;
322 Derived_Type : Entity_Id;
323 Loc : Source_Ptr);
324 -- For derived scalar types, convert the bounds in the type definition to
325 -- the derived type, and complete their analysis. Given a constraint of the
326 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
327 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
328 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
329 -- subtype are conversions of those bounds to the derived_type, so that
330 -- their typing is consistent.
331
332 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
333 -- Copies attributes from array base type T2 to array base type T1. Copies
334 -- only attributes that apply to base types, but not subtypes.
335
336 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
337 -- Copies attributes from array subtype T2 to array subtype T1. Copies
338 -- attributes that apply to both subtypes and base types.
339
340 procedure Create_Constrained_Components
341 (Subt : Entity_Id;
342 Decl_Node : Node_Id;
343 Typ : Entity_Id;
344 Constraints : Elist_Id);
345 -- Build the list of entities for a constrained discriminated record
346 -- subtype. If a component depends on a discriminant, replace its subtype
347 -- using the discriminant values in the discriminant constraint. Subt
348 -- is the defining identifier for the subtype whose list of constrained
349 -- entities we will create. Decl_Node is the type declaration node where
350 -- we will attach all the itypes created. Typ is the base discriminated
351 -- type for the subtype Subt. Constraints is the list of discriminant
352 -- constraints for Typ.
353
354 function Constrain_Component_Type
355 (Comp : Entity_Id;
356 Constrained_Typ : Entity_Id;
357 Related_Node : Node_Id;
358 Typ : Entity_Id;
359 Constraints : Elist_Id) return Entity_Id;
360 -- Given a discriminated base type Typ, a list of discriminant constraint
361 -- Constraints for Typ and a component of Typ, with type Compon_Type,
362 -- create and return the type corresponding to Compon_type where all
363 -- discriminant references are replaced with the corresponding constraint.
364 -- If no discriminant references occur in Compon_Typ then return it as is.
365 -- Constrained_Typ is the final constrained subtype to which the
366 -- constrained Compon_Type belongs. Related_Node is the node where we will
367 -- attach all the itypes created.
368 --
369 -- Above description is confused, what is Compon_Type???
370
371 procedure Constrain_Access
372 (Def_Id : in out Entity_Id;
373 S : Node_Id;
374 Related_Nod : Node_Id);
375 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
376 -- an anonymous type created for a subtype indication. In that case it is
377 -- created in the procedure and attached to Related_Nod.
378
379 procedure Constrain_Array
380 (Def_Id : in out Entity_Id;
381 SI : Node_Id;
382 Related_Nod : Node_Id;
383 Related_Id : Entity_Id;
384 Suffix : Character);
385 -- Apply a list of index constraints to an unconstrained array type. The
386 -- first parameter is the entity for the resulting subtype. A value of
387 -- Empty for Def_Id indicates that an implicit type must be created, but
388 -- creation is delayed (and must be done by this procedure) because other
389 -- subsidiary implicit types must be created first (which is why Def_Id
390 -- is an in/out parameter). The second parameter is a subtype indication
391 -- node for the constrained array to be created (e.g. something of the
392 -- form string (1 .. 10)). Related_Nod gives the place where this type
393 -- has to be inserted in the tree. The Related_Id and Suffix parameters
394 -- are used to build the associated Implicit type name.
395
396 procedure Constrain_Concurrent
397 (Def_Id : in out Entity_Id;
398 SI : Node_Id;
399 Related_Nod : Node_Id;
400 Related_Id : Entity_Id;
401 Suffix : Character);
402 -- Apply list of discriminant constraints to an unconstrained concurrent
403 -- type.
404 --
405 -- SI is the N_Subtype_Indication node containing the constraint and
406 -- the unconstrained type to constrain.
407 --
408 -- Def_Id is the entity for the resulting constrained subtype. A value
409 -- of Empty for Def_Id indicates that an implicit type must be created,
410 -- but creation is delayed (and must be done by this procedure) because
411 -- other subsidiary implicit types must be created first (which is why
412 -- Def_Id is an in/out parameter).
413 --
414 -- Related_Nod gives the place where this type has to be inserted
415 -- in the tree
416 --
417 -- The last two arguments are used to create its external name if needed.
418
419 function Constrain_Corresponding_Record
420 (Prot_Subt : Entity_Id;
421 Corr_Rec : Entity_Id;
422 Related_Nod : Node_Id;
423 Related_Id : Entity_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 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
537 -- implicit 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 function Is_Progenitor
591 (Iface : Entity_Id;
592 Typ : Entity_Id) return Boolean;
593 -- Determine whether type Typ implements interface Iface. This requires
594 -- traversing the list of abstract interfaces of the type, as well as that
595 -- of the ancestor types. The predicate is used to determine when a formal
596 -- in the signature of an inherited operation must carry the derived type.
597
598 function Is_Valid_Constraint_Kind
599 (T_Kind : Type_Kind;
600 Constraint_Kind : Node_Kind) return Boolean;
601 -- Returns True if it is legal to apply the given kind of constraint to the
602 -- given kind of type (index constraint to an array type, for example).
603
604 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
605 -- Create new modular type. Verify that modulus is in bounds and is
606 -- a power of two (implementation restriction).
607
608 procedure New_Concatenation_Op (Typ : Entity_Id);
609 -- Create an abbreviated declaration for an operator in order to
610 -- materialize concatenation on array types.
611
612 procedure Ordinary_Fixed_Point_Type_Declaration
613 (T : Entity_Id;
614 Def : Node_Id);
615 -- Create a new ordinary fixed point type, and apply the constraint to
616 -- obtain subtype of it.
617
618 procedure Prepare_Private_Subtype_Completion
619 (Id : Entity_Id;
620 Related_Nod : Node_Id);
621 -- Id is a subtype of some private type. Creates the full declaration
622 -- associated with Id whenever possible, i.e. when the full declaration
623 -- of the base type is already known. Records each subtype into
624 -- Private_Dependents of the base type.
625
626 procedure Process_Incomplete_Dependents
627 (N : Node_Id;
628 Full_T : Entity_Id;
629 Inc_T : Entity_Id);
630 -- Process all entities that depend on an incomplete type. There include
631 -- subtypes, subprogram types that mention the incomplete type in their
632 -- profiles, and subprogram with access parameters that designate the
633 -- incomplete type.
634
635 -- Inc_T is the defining identifier of an incomplete type declaration, its
636 -- Ekind is E_Incomplete_Type.
637 --
638 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
639 --
640 -- Full_T is N's defining identifier.
641 --
642 -- Subtypes of incomplete types with discriminants are completed when the
643 -- parent type is. This is simpler than private subtypes, because they can
644 -- only appear in the same scope, and there is no need to exchange views.
645 -- Similarly, access_to_subprogram types may have a parameter or a return
646 -- type that is an incomplete type, and that must be replaced with the
647 -- full type.
648 --
649 -- If the full type is tagged, subprogram with access parameters that
650 -- designated the incomplete may be primitive operations of the full type,
651 -- and have to be processed accordingly.
652
653 procedure Process_Real_Range_Specification (Def : Node_Id);
654 -- Given the type definition for a real type, this procedure processes and
655 -- checks the real range specification of this type definition if one is
656 -- present. If errors are found, error messages are posted, and the
657 -- Real_Range_Specification of Def is reset to Empty.
658
659 procedure Record_Type_Declaration
660 (T : Entity_Id;
661 N : Node_Id;
662 Prev : Entity_Id);
663 -- Process a record type declaration (for both untagged and tagged
664 -- records). Parameters T and N are exactly like in procedure
665 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
666 -- for this routine. If this is the completion of an incomplete type
667 -- declaration, Prev is the entity of the incomplete declaration, used for
668 -- cross-referencing. Otherwise Prev = T.
669
670 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
671 -- This routine is used to process the actual record type definition (both
672 -- for untagged and tagged records). Def is a record type definition node.
673 -- This procedure analyzes the components in this record type definition.
674 -- Prev_T is the entity for the enclosing record type. It is provided so
675 -- that its Has_Task flag can be set if any of the component have Has_Task
676 -- set. If the declaration is the completion of an incomplete type
677 -- declaration, Prev_T is the original incomplete type, whose full view is
678 -- the record type.
679
680 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
681 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
682 -- build a copy of the declaration tree of the parent, and we create
683 -- independently the list of components for the derived type. Semantic
684 -- information uses the component entities, but record representation
685 -- clauses are validated on the declaration tree. This procedure replaces
686 -- discriminants and components in the declaration with those that have
687 -- been created by Inherit_Components.
688
689 procedure Set_Fixed_Range
690 (E : Entity_Id;
691 Loc : Source_Ptr;
692 Lo : Ureal;
693 Hi : Ureal);
694 -- Build a range node with the given bounds and set it as the Scalar_Range
695 -- of the given fixed-point type entity. Loc is the source location used
696 -- for the constructed range. See body for further details.
697
698 procedure Set_Scalar_Range_For_Subtype
699 (Def_Id : Entity_Id;
700 R : Node_Id;
701 Subt : Entity_Id);
702 -- This routine is used to set the scalar range field for a subtype given
703 -- Def_Id, the entity for the subtype, and R, the range expression for the
704 -- scalar range. Subt provides the parent subtype to be used to analyze,
705 -- resolve, and check the given range.
706
707 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
708 -- Create a new signed integer entity, and apply the constraint to obtain
709 -- the required first named subtype of this type.
710
711 procedure Set_Stored_Constraint_From_Discriminant_Constraint
712 (E : Entity_Id);
713 -- E is some record type. This routine computes E's Stored_Constraint
714 -- from its Discriminant_Constraint.
715
716 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
717 -- Check that an entity in a list of progenitors is an interface,
718 -- emit error otherwise.
719
720 -----------------------
721 -- Access_Definition --
722 -----------------------
723
724 function Access_Definition
725 (Related_Nod : Node_Id;
726 N : Node_Id) return Entity_Id
727 is
728 Loc : constant Source_Ptr := Sloc (Related_Nod);
729 Anon_Type : Entity_Id;
730 Anon_Scope : Entity_Id;
731 Desig_Type : Entity_Id;
732 Decl : Entity_Id;
733
734 begin
735 if Is_Entry (Current_Scope)
736 and then Is_Task_Type (Etype (Scope (Current_Scope)))
737 then
738 Error_Msg_N ("task entries cannot have access parameters", N);
739 return Empty;
740 end if;
741
742 -- Ada 2005: for an object declaration the corresponding anonymous
743 -- type is declared in the current scope.
744
745 -- If the access definition is the return type of another access to
746 -- function, scope is the current one, because it is the one of the
747 -- current type declaration.
748
749 if Nkind_In (Related_Nod, N_Object_Declaration,
750 N_Access_Function_Definition)
751 then
752 Anon_Scope := Current_Scope;
753
754 -- For the anonymous function result case, retrieve the scope of the
755 -- function specification's associated entity rather than using the
756 -- current scope. The current scope will be the function itself if the
757 -- formal part is currently being analyzed, but will be the parent scope
758 -- in the case of a parameterless function, and we always want to use
759 -- the function's parent scope. Finally, if the function is a child
760 -- unit, we must traverse the tree to retrieve the proper entity.
761
762 elsif Nkind (Related_Nod) = N_Function_Specification
763 and then Nkind (Parent (N)) /= N_Parameter_Specification
764 then
765 -- If the current scope is a protected type, the anonymous access
766 -- is associated with one of the protected operations, and must
767 -- be available in the scope that encloses the protected declaration.
768 -- Otherwise the type is is in the scope enclosing the subprogram.
769
770 if Ekind (Current_Scope) = E_Protected_Type then
771 Anon_Scope := Scope (Scope (Defining_Entity (Related_Nod)));
772 else
773 Anon_Scope := Scope (Defining_Entity (Related_Nod));
774 end if;
775
776 else
777 -- For access formals, access components, and access discriminants,
778 -- the scope is that of the enclosing declaration,
779
780 Anon_Scope := Scope (Current_Scope);
781 end if;
782
783 Anon_Type :=
784 Create_Itype
785 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
786
787 if All_Present (N)
788 and then Ada_Version >= Ada_05
789 then
790 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
791 end if;
792
793 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
794 -- the corresponding semantic routine
795
796 if Present (Access_To_Subprogram_Definition (N)) then
797 Access_Subprogram_Declaration
798 (T_Name => Anon_Type,
799 T_Def => Access_To_Subprogram_Definition (N));
800
801 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
802 Set_Ekind
803 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
804 else
805 Set_Ekind
806 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
807 end if;
808
809 Set_Can_Use_Internal_Rep
810 (Anon_Type, not Always_Compatible_Rep_On_Target);
811
812 -- If the anonymous access is associated with a protected operation
813 -- create a reference to it after the enclosing protected definition
814 -- because the itype will be used in the subsequent bodies.
815
816 if Ekind (Current_Scope) = E_Protected_Type then
817 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
818 end if;
819
820 return Anon_Type;
821 end if;
822
823 Find_Type (Subtype_Mark (N));
824 Desig_Type := Entity (Subtype_Mark (N));
825
826 Set_Directly_Designated_Type
827 (Anon_Type, Desig_Type);
828 Set_Etype (Anon_Type, Anon_Type);
829
830 -- Make sure the anonymous access type has size and alignment fields
831 -- set, as required by gigi. This is necessary in the case of the
832 -- Task_Body_Procedure.
833
834 if not Has_Private_Component (Desig_Type) then
835 Layout_Type (Anon_Type);
836 end if;
837
838 -- ???The following makes no sense, because Anon_Type is an access type
839 -- and therefore cannot have components, private or otherwise. Hence
840 -- the assertion. Not sure what was meant, here.
841 Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
842 pragma Assert (not Depends_On_Private (Anon_Type));
843
844 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
845 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
846 -- the null value is allowed. In Ada 95 the null value is never allowed.
847
848 if Ada_Version >= Ada_05 then
849 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
850 else
851 Set_Can_Never_Be_Null (Anon_Type, True);
852 end if;
853
854 -- The anonymous access type is as public as the discriminated type or
855 -- subprogram that defines it. It is imported (for back-end purposes)
856 -- if the designated type is.
857
858 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
859
860 -- Ada 2005 (AI-50217): Propagate the attribute that indicates that the
861 -- designated type comes from the limited view.
862
863 Set_From_With_Type (Anon_Type, From_With_Type (Desig_Type));
864
865 -- Ada 2005 (AI-231): Propagate the access-constant attribute
866
867 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
868
869 -- The context is either a subprogram declaration, object declaration,
870 -- or an access discriminant, in a private or a full type declaration.
871 -- In the case of a subprogram, if the designated type is incomplete,
872 -- the operation will be a primitive operation of the full type, to be
873 -- updated subsequently. If the type is imported through a limited_with
874 -- clause, the subprogram is not a primitive operation of the type
875 -- (which is declared elsewhere in some other scope).
876
877 if Ekind (Desig_Type) = E_Incomplete_Type
878 and then not From_With_Type (Desig_Type)
879 and then Is_Overloadable (Current_Scope)
880 then
881 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
882 Set_Has_Delayed_Freeze (Current_Scope);
883 end if;
884
885 -- Ada 2005: if the designated type is an interface that may contain
886 -- tasks, create a Master entity for the declaration. This must be done
887 -- before expansion of the full declaration, because the declaration may
888 -- include an expression that is an allocator, whose expansion needs the
889 -- proper Master for the created tasks.
890
891 if Nkind (Related_Nod) = N_Object_Declaration
892 and then Expander_Active
893 then
894 if Is_Interface (Desig_Type)
895 and then Is_Limited_Record (Desig_Type)
896 then
897 Build_Class_Wide_Master (Anon_Type);
898
899 -- Similarly, if the type is an anonymous access that designates
900 -- tasks, create a master entity for it in the current context.
901
902 elsif Has_Task (Desig_Type)
903 and then Comes_From_Source (Related_Nod)
904 then
905 if not Has_Master_Entity (Current_Scope) then
906 Decl :=
907 Make_Object_Declaration (Loc,
908 Defining_Identifier =>
909 Make_Defining_Identifier (Loc, Name_uMaster),
910 Constant_Present => True,
911 Object_Definition =>
912 New_Reference_To (RTE (RE_Master_Id), Loc),
913 Expression =>
914 Make_Explicit_Dereference (Loc,
915 New_Reference_To (RTE (RE_Current_Master), Loc)));
916
917 Insert_Before (Related_Nod, Decl);
918 Analyze (Decl);
919
920 Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
921 Set_Has_Master_Entity (Current_Scope);
922 else
923 Build_Master_Renaming (Related_Nod, Anon_Type);
924 end if;
925 end if;
926 end if;
927
928 -- For a private component of a protected type, it is imperative that
929 -- the back-end elaborate the type immediately after the protected
930 -- declaration, because this type will be used in the declarations
931 -- created for the component within each protected body, so we must
932 -- create an itype reference for it now.
933
934 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
935 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
936
937 -- Similarly, if the access definition is the return result of a
938 -- function, create an itype reference for it because it
939 -- will be used within the function body. For a regular function that
940 -- is not a compilation unit, insert reference after the declaration.
941 -- For a protected operation, insert it after the enclosing protected
942 -- type declaration. In either case, do not create a reference for a
943 -- type obtained through a limited_with clause, because this would
944 -- introduce semantic dependencies.
945
946 elsif Nkind (Related_Nod) = N_Function_Specification
947 and then not From_With_Type (Anon_Type)
948 then
949 if Ekind (Current_Scope) = E_Protected_Type then
950 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
951
952 elsif Is_List_Member (Parent (Related_Nod))
953 and then Nkind (Parent (N)) /= N_Parameter_Specification
954 then
955 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
956 end if;
957
958 -- Finally, create an itype reference for an object declaration of
959 -- an anonymous access type. This is strictly necessary only for
960 -- deferred constants, but in any case will avoid out-of-scope
961 -- problems in the back-end.
962
963 elsif Nkind (Related_Nod) = N_Object_Declaration then
964 Build_Itype_Reference (Anon_Type, Related_Nod);
965 end if;
966
967 return Anon_Type;
968 end Access_Definition;
969
970 -----------------------------------
971 -- Access_Subprogram_Declaration --
972 -----------------------------------
973
974 procedure Access_Subprogram_Declaration
975 (T_Name : Entity_Id;
976 T_Def : Node_Id)
977 is
978
979 procedure Check_For_Premature_Usage (Def : Node_Id);
980 -- Check that type T_Name is not used, directly or recursively,
981 -- as a parameter or a return type in Def. Def is either a subtype,
982 -- an access_definition, or an access_to_subprogram_definition.
983
984 -------------------------------
985 -- Check_For_Premature_Usage --
986 -------------------------------
987
988 procedure Check_For_Premature_Usage (Def : Node_Id) is
989 Param : Node_Id;
990
991 begin
992 -- Check for a subtype mark
993
994 if Nkind (Def) in N_Has_Etype then
995 if Etype (Def) = T_Name then
996 Error_Msg_N
997 ("type& cannot be used before end of its declaration", Def);
998 end if;
999
1000 -- If this is not a subtype, then this is an access_definition
1001
1002 elsif Nkind (Def) = N_Access_Definition then
1003 if Present (Access_To_Subprogram_Definition (Def)) then
1004 Check_For_Premature_Usage
1005 (Access_To_Subprogram_Definition (Def));
1006 else
1007 Check_For_Premature_Usage (Subtype_Mark (Def));
1008 end if;
1009
1010 -- The only cases left are N_Access_Function_Definition and
1011 -- N_Access_Procedure_Definition.
1012
1013 else
1014 if Present (Parameter_Specifications (Def)) then
1015 Param := First (Parameter_Specifications (Def));
1016 while Present (Param) loop
1017 Check_For_Premature_Usage (Parameter_Type (Param));
1018 Param := Next (Param);
1019 end loop;
1020 end if;
1021
1022 if Nkind (Def) = N_Access_Function_Definition then
1023 Check_For_Premature_Usage (Result_Definition (Def));
1024 end if;
1025 end if;
1026 end Check_For_Premature_Usage;
1027
1028 -- Local variables
1029
1030 Formals : constant List_Id := Parameter_Specifications (T_Def);
1031 Formal : Entity_Id;
1032 D_Ityp : Node_Id;
1033 Desig_Type : constant Entity_Id :=
1034 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1035
1036 -- Start of processing for Access_Subprogram_Declaration
1037
1038 begin
1039 -- Associate the Itype node with the inner full-type declaration or
1040 -- subprogram spec. This is required to handle nested anonymous
1041 -- declarations. For example:
1042
1043 -- procedure P
1044 -- (X : access procedure
1045 -- (Y : access procedure
1046 -- (Z : access T)))
1047
1048 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1049 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1050 N_Private_Type_Declaration,
1051 N_Private_Extension_Declaration,
1052 N_Procedure_Specification,
1053 N_Function_Specification)
1054 or else
1055 Nkind_In (D_Ityp, N_Object_Declaration,
1056 N_Object_Renaming_Declaration,
1057 N_Formal_Type_Declaration,
1058 N_Task_Type_Declaration,
1059 N_Protected_Type_Declaration))
1060 loop
1061 D_Ityp := Parent (D_Ityp);
1062 pragma Assert (D_Ityp /= Empty);
1063 end loop;
1064
1065 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1066
1067 if Nkind_In (D_Ityp, N_Procedure_Specification,
1068 N_Function_Specification)
1069 then
1070 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1071
1072 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1073 N_Object_Declaration,
1074 N_Object_Renaming_Declaration,
1075 N_Formal_Type_Declaration)
1076 then
1077 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1078 end if;
1079
1080 if Nkind (T_Def) = N_Access_Function_Definition then
1081 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1082 declare
1083 Acc : constant Node_Id := Result_Definition (T_Def);
1084
1085 begin
1086 if Present (Access_To_Subprogram_Definition (Acc))
1087 and then
1088 Protected_Present (Access_To_Subprogram_Definition (Acc))
1089 then
1090 Set_Etype
1091 (Desig_Type,
1092 Replace_Anonymous_Access_To_Protected_Subprogram
1093 (T_Def));
1094
1095 else
1096 Set_Etype
1097 (Desig_Type,
1098 Access_Definition (T_Def, Result_Definition (T_Def)));
1099 end if;
1100 end;
1101
1102 else
1103 Analyze (Result_Definition (T_Def));
1104 Set_Etype (Desig_Type, Entity (Result_Definition (T_Def)));
1105 end if;
1106
1107 if not (Is_Type (Etype (Desig_Type))) then
1108 Error_Msg_N
1109 ("expect type in function specification",
1110 Result_Definition (T_Def));
1111 end if;
1112
1113 else
1114 Set_Etype (Desig_Type, Standard_Void_Type);
1115 end if;
1116
1117 if Present (Formals) then
1118 Push_Scope (Desig_Type);
1119 Process_Formals (Formals, Parent (T_Def));
1120
1121 -- A bit of a kludge here, End_Scope requires that the parent
1122 -- pointer be set to something reasonable, but Itypes don't have
1123 -- parent pointers. So we set it and then unset it ??? If and when
1124 -- Itypes have proper parent pointers to their declarations, this
1125 -- kludge can be removed.
1126
1127 Set_Parent (Desig_Type, T_Name);
1128 End_Scope;
1129 Set_Parent (Desig_Type, Empty);
1130 end if;
1131
1132 -- Check for premature usage of the type being defined
1133
1134 Check_For_Premature_Usage (T_Def);
1135
1136 -- The return type and/or any parameter type may be incomplete. Mark
1137 -- the subprogram_type as depending on the incomplete type, so that
1138 -- it can be updated when the full type declaration is seen. This
1139 -- only applies to incomplete types declared in some enclosing scope,
1140 -- not to limited views from other packages.
1141
1142 if Present (Formals) then
1143 Formal := First_Formal (Desig_Type);
1144 while Present (Formal) loop
1145 if Ekind (Formal) /= E_In_Parameter
1146 and then Nkind (T_Def) = N_Access_Function_Definition
1147 then
1148 Error_Msg_N ("functions can only have IN parameters", Formal);
1149 end if;
1150
1151 if Ekind (Etype (Formal)) = E_Incomplete_Type
1152 and then In_Open_Scopes (Scope (Etype (Formal)))
1153 then
1154 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1155 Set_Has_Delayed_Freeze (Desig_Type);
1156 end if;
1157
1158 Next_Formal (Formal);
1159 end loop;
1160 end if;
1161
1162 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1163 and then not Has_Delayed_Freeze (Desig_Type)
1164 then
1165 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1166 Set_Has_Delayed_Freeze (Desig_Type);
1167 end if;
1168
1169 Check_Delayed_Subprogram (Desig_Type);
1170
1171 if Protected_Present (T_Def) then
1172 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1173 Set_Convention (Desig_Type, Convention_Protected);
1174 else
1175 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1176 end if;
1177
1178 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1179
1180 Set_Etype (T_Name, T_Name);
1181 Init_Size_Align (T_Name);
1182 Set_Directly_Designated_Type (T_Name, Desig_Type);
1183
1184 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1185
1186 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1187
1188 Check_Restriction (No_Access_Subprograms, T_Def);
1189 end Access_Subprogram_Declaration;
1190
1191 ----------------------------
1192 -- Access_Type_Declaration --
1193 ----------------------------
1194
1195 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1196 S : constant Node_Id := Subtype_Indication (Def);
1197 P : constant Node_Id := Parent (Def);
1198
1199 Desig : Entity_Id;
1200 -- Designated type
1201
1202 begin
1203 -- Check for permissible use of incomplete type
1204
1205 if Nkind (S) /= N_Subtype_Indication then
1206 Analyze (S);
1207
1208 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1209 Set_Directly_Designated_Type (T, Entity (S));
1210 else
1211 Set_Directly_Designated_Type (T,
1212 Process_Subtype (S, P, T, 'P'));
1213 end if;
1214
1215 else
1216 Set_Directly_Designated_Type (T,
1217 Process_Subtype (S, P, T, 'P'));
1218 end if;
1219
1220 if All_Present (Def) or Constant_Present (Def) then
1221 Set_Ekind (T, E_General_Access_Type);
1222 else
1223 Set_Ekind (T, E_Access_Type);
1224 end if;
1225
1226 if Base_Type (Designated_Type (T)) = T then
1227 Error_Msg_N ("access type cannot designate itself", S);
1228
1229 -- In Ada 2005, the type may have a limited view through some unit
1230 -- in its own context, allowing the following circularity that cannot
1231 -- be detected earlier
1232
1233 elsif Is_Class_Wide_Type (Designated_Type (T))
1234 and then Etype (Designated_Type (T)) = T
1235 then
1236 Error_Msg_N
1237 ("access type cannot designate its own classwide type", S);
1238
1239 -- Clean up indication of tagged status to prevent cascaded errors
1240
1241 Set_Is_Tagged_Type (T, False);
1242 end if;
1243
1244 Set_Etype (T, T);
1245
1246 -- If the type has appeared already in a with_type clause, it is
1247 -- frozen and the pointer size is already set. Else, initialize.
1248
1249 if not From_With_Type (T) then
1250 Init_Size_Align (T);
1251 end if;
1252
1253 Desig := Designated_Type (T);
1254
1255 -- If designated type is an imported tagged type, indicate that the
1256 -- access type is also imported, and therefore restricted in its use.
1257 -- The access type may already be imported, so keep setting otherwise.
1258
1259 -- Ada 2005 (AI-50217): If the non-limited view of the designated type
1260 -- is available, use it as the designated type of the access type, so
1261 -- that the back-end gets a usable entity.
1262
1263 if From_With_Type (Desig)
1264 and then Ekind (Desig) /= E_Access_Type
1265 then
1266 Set_From_With_Type (T);
1267 end if;
1268
1269 -- Note that Has_Task is always false, since the access type itself
1270 -- is not a task type. See Einfo for more description on this point.
1271 -- Exactly the same consideration applies to Has_Controlled_Component.
1272
1273 Set_Has_Task (T, False);
1274 Set_Has_Controlled_Component (T, False);
1275
1276 -- Initialize Associated_Final_Chain explicitly to Empty, to avoid
1277 -- problems where an incomplete view of this entity has been previously
1278 -- established by a limited with and an overlaid version of this field
1279 -- (Stored_Constraint) was initialized for the incomplete view.
1280
1281 Set_Associated_Final_Chain (T, Empty);
1282
1283 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1284 -- attributes
1285
1286 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1287 Set_Is_Access_Constant (T, Constant_Present (Def));
1288 end Access_Type_Declaration;
1289
1290 ----------------------------------
1291 -- Add_Interface_Tag_Components --
1292 ----------------------------------
1293
1294 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1295 Loc : constant Source_Ptr := Sloc (N);
1296 L : List_Id;
1297 Last_Tag : Node_Id;
1298
1299 procedure Add_Tag (Iface : Entity_Id);
1300 -- Add tag for one of the progenitor interfaces
1301
1302 -------------
1303 -- Add_Tag --
1304 -------------
1305
1306 procedure Add_Tag (Iface : Entity_Id) is
1307 Decl : Node_Id;
1308 Def : Node_Id;
1309 Tag : Entity_Id;
1310 Offset : Entity_Id;
1311
1312 begin
1313 pragma Assert (Is_Tagged_Type (Iface)
1314 and then Is_Interface (Iface));
1315
1316 Def :=
1317 Make_Component_Definition (Loc,
1318 Aliased_Present => True,
1319 Subtype_Indication =>
1320 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1321
1322 Tag := Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1323
1324 Decl :=
1325 Make_Component_Declaration (Loc,
1326 Defining_Identifier => Tag,
1327 Component_Definition => Def);
1328
1329 Analyze_Component_Declaration (Decl);
1330
1331 Set_Analyzed (Decl);
1332 Set_Ekind (Tag, E_Component);
1333 Set_Is_Tag (Tag);
1334 Set_Is_Aliased (Tag);
1335 Set_Related_Type (Tag, Iface);
1336 Init_Component_Location (Tag);
1337
1338 pragma Assert (Is_Frozen (Iface));
1339
1340 Set_DT_Entry_Count (Tag,
1341 DT_Entry_Count (First_Entity (Iface)));
1342
1343 if No (Last_Tag) then
1344 Prepend (Decl, L);
1345 else
1346 Insert_After (Last_Tag, Decl);
1347 end if;
1348
1349 Last_Tag := Decl;
1350
1351 -- If the ancestor has discriminants we need to give special support
1352 -- to store the offset_to_top value of the secondary dispatch tables.
1353 -- For this purpose we add a supplementary component just after the
1354 -- field that contains the tag associated with each secondary DT.
1355
1356 if Typ /= Etype (Typ)
1357 and then Has_Discriminants (Etype (Typ))
1358 then
1359 Def :=
1360 Make_Component_Definition (Loc,
1361 Subtype_Indication =>
1362 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1363
1364 Offset :=
1365 Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1366
1367 Decl :=
1368 Make_Component_Declaration (Loc,
1369 Defining_Identifier => Offset,
1370 Component_Definition => Def);
1371
1372 Analyze_Component_Declaration (Decl);
1373
1374 Set_Analyzed (Decl);
1375 Set_Ekind (Offset, E_Component);
1376 Set_Is_Aliased (Offset);
1377 Set_Related_Type (Offset, Iface);
1378 Init_Component_Location (Offset);
1379 Insert_After (Last_Tag, Decl);
1380 Last_Tag := Decl;
1381 end if;
1382 end Add_Tag;
1383
1384 -- Local variables
1385
1386 Elmt : Elmt_Id;
1387 Ext : Node_Id;
1388 Comp : Node_Id;
1389
1390 -- Start of processing for Add_Interface_Tag_Components
1391
1392 begin
1393 if not RTE_Available (RE_Interface_Tag) then
1394 Error_Msg
1395 ("(Ada 2005) interface types not supported by this run-time!",
1396 Sloc (N));
1397 return;
1398 end if;
1399
1400 if Ekind (Typ) /= E_Record_Type
1401 or else (Is_Concurrent_Record_Type (Typ)
1402 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1403 or else (not Is_Concurrent_Record_Type (Typ)
1404 and then No (Interfaces (Typ))
1405 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1406 then
1407 return;
1408 end if;
1409
1410 -- Find the current last tag
1411
1412 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1413 Ext := Record_Extension_Part (Type_Definition (N));
1414 else
1415 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1416 Ext := Type_Definition (N);
1417 end if;
1418
1419 Last_Tag := Empty;
1420
1421 if not (Present (Component_List (Ext))) then
1422 Set_Null_Present (Ext, False);
1423 L := New_List;
1424 Set_Component_List (Ext,
1425 Make_Component_List (Loc,
1426 Component_Items => L,
1427 Null_Present => False));
1428 else
1429 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1430 L := Component_Items
1431 (Component_List
1432 (Record_Extension_Part
1433 (Type_Definition (N))));
1434 else
1435 L := Component_Items
1436 (Component_List
1437 (Type_Definition (N)));
1438 end if;
1439
1440 -- Find the last tag component
1441
1442 Comp := First (L);
1443 while Present (Comp) loop
1444 if Nkind (Comp) = N_Component_Declaration
1445 and then Is_Tag (Defining_Identifier (Comp))
1446 then
1447 Last_Tag := Comp;
1448 end if;
1449
1450 Next (Comp);
1451 end loop;
1452 end if;
1453
1454 -- At this point L references the list of components and Last_Tag
1455 -- references the current last tag (if any). Now we add the tag
1456 -- corresponding with all the interfaces that are not implemented
1457 -- by the parent.
1458
1459 if Present (Interfaces (Typ)) then
1460 Elmt := First_Elmt (Interfaces (Typ));
1461 while Present (Elmt) loop
1462 Add_Tag (Node (Elmt));
1463 Next_Elmt (Elmt);
1464 end loop;
1465 end if;
1466 end Add_Interface_Tag_Components;
1467
1468 -----------------------------------
1469 -- Analyze_Component_Declaration --
1470 -----------------------------------
1471
1472 procedure Analyze_Component_Declaration (N : Node_Id) is
1473 Id : constant Entity_Id := Defining_Identifier (N);
1474 E : constant Node_Id := Expression (N);
1475 T : Entity_Id;
1476 P : Entity_Id;
1477
1478 function Contains_POC (Constr : Node_Id) return Boolean;
1479 -- Determines whether a constraint uses the discriminant of a record
1480 -- type thus becoming a per-object constraint (POC).
1481
1482 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1483 -- Typ is the type of the current component, check whether this type is
1484 -- a limited type. Used to validate declaration against that of
1485 -- enclosing record.
1486
1487 ------------------
1488 -- Contains_POC --
1489 ------------------
1490
1491 function Contains_POC (Constr : Node_Id) return Boolean is
1492 begin
1493 -- Prevent cascaded errors
1494
1495 if Error_Posted (Constr) then
1496 return False;
1497 end if;
1498
1499 case Nkind (Constr) is
1500 when N_Attribute_Reference =>
1501 return
1502 Attribute_Name (Constr) = Name_Access
1503 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1504
1505 when N_Discriminant_Association =>
1506 return Denotes_Discriminant (Expression (Constr));
1507
1508 when N_Identifier =>
1509 return Denotes_Discriminant (Constr);
1510
1511 when N_Index_Or_Discriminant_Constraint =>
1512 declare
1513 IDC : Node_Id;
1514
1515 begin
1516 IDC := First (Constraints (Constr));
1517 while Present (IDC) loop
1518
1519 -- One per-object constraint is sufficient
1520
1521 if Contains_POC (IDC) then
1522 return True;
1523 end if;
1524
1525 Next (IDC);
1526 end loop;
1527
1528 return False;
1529 end;
1530
1531 when N_Range =>
1532 return Denotes_Discriminant (Low_Bound (Constr))
1533 or else
1534 Denotes_Discriminant (High_Bound (Constr));
1535
1536 when N_Range_Constraint =>
1537 return Denotes_Discriminant (Range_Expression (Constr));
1538
1539 when others =>
1540 return False;
1541
1542 end case;
1543 end Contains_POC;
1544
1545 ----------------------
1546 -- Is_Known_Limited --
1547 ----------------------
1548
1549 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1550 P : constant Entity_Id := Etype (Typ);
1551 R : constant Entity_Id := Root_Type (Typ);
1552
1553 begin
1554 if Is_Limited_Record (Typ) then
1555 return True;
1556
1557 -- If the root type is limited (and not a limited interface)
1558 -- so is the current type
1559
1560 elsif Is_Limited_Record (R)
1561 and then
1562 (not Is_Interface (R)
1563 or else not Is_Limited_Interface (R))
1564 then
1565 return True;
1566
1567 -- Else the type may have a limited interface progenitor, but a
1568 -- limited record parent.
1569
1570 elsif R /= P
1571 and then Is_Limited_Record (P)
1572 then
1573 return True;
1574
1575 else
1576 return False;
1577 end if;
1578 end Is_Known_Limited;
1579
1580 -- Start of processing for Analyze_Component_Declaration
1581
1582 begin
1583 Generate_Definition (Id);
1584 Enter_Name (Id);
1585
1586 if Present (Subtype_Indication (Component_Definition (N))) then
1587 T := Find_Type_Of_Object
1588 (Subtype_Indication (Component_Definition (N)), N);
1589
1590 -- Ada 2005 (AI-230): Access Definition case
1591
1592 else
1593 pragma Assert (Present
1594 (Access_Definition (Component_Definition (N))));
1595
1596 T := Access_Definition
1597 (Related_Nod => N,
1598 N => Access_Definition (Component_Definition (N)));
1599 Set_Is_Local_Anonymous_Access (T);
1600
1601 -- Ada 2005 (AI-254)
1602
1603 if Present (Access_To_Subprogram_Definition
1604 (Access_Definition (Component_Definition (N))))
1605 and then Protected_Present (Access_To_Subprogram_Definition
1606 (Access_Definition
1607 (Component_Definition (N))))
1608 then
1609 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1610 end if;
1611 end if;
1612
1613 -- If the subtype is a constrained subtype of the enclosing record,
1614 -- (which must have a partial view) the back-end does not properly
1615 -- handle the recursion. Rewrite the component declaration with an
1616 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1617 -- the tree directly because side effects have already been removed from
1618 -- discriminant constraints.
1619
1620 if Ekind (T) = E_Access_Subtype
1621 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1622 and then Comes_From_Source (T)
1623 and then Nkind (Parent (T)) = N_Subtype_Declaration
1624 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1625 then
1626 Rewrite
1627 (Subtype_Indication (Component_Definition (N)),
1628 New_Copy_Tree (Subtype_Indication (Parent (T))));
1629 T := Find_Type_Of_Object
1630 (Subtype_Indication (Component_Definition (N)), N);
1631 end if;
1632
1633 -- If the component declaration includes a default expression, then we
1634 -- check that the component is not of a limited type (RM 3.7(5)),
1635 -- and do the special preanalysis of the expression (see section on
1636 -- "Handling of Default and Per-Object Expressions" in the spec of
1637 -- package Sem).
1638
1639 if Present (E) then
1640 Preanalyze_Spec_Expression (E, T);
1641 Check_Initialization (T, E);
1642
1643 if Ada_Version >= Ada_05
1644 and then Ekind (T) = E_Anonymous_Access_Type
1645 then
1646 -- Check RM 3.9.2(9): "if the expected type for an expression is
1647 -- an anonymous access-to-specific tagged type, then the object
1648 -- designated by the expression shall not be dynamically tagged
1649 -- unless it is a controlling operand in a call on a dispatching
1650 -- operation"
1651
1652 if Is_Tagged_Type (Directly_Designated_Type (T))
1653 and then
1654 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1655 and then
1656 Ekind (Directly_Designated_Type (Etype (E))) =
1657 E_Class_Wide_Type
1658 then
1659 Error_Msg_N
1660 ("access to specific tagged type required (RM 3.9.2(9))", E);
1661 end if;
1662
1663 -- (Ada 2005: AI-230): Accessibility check for anonymous
1664 -- components
1665
1666 if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1667 Error_Msg_N
1668 ("expression has deeper access level than component " &
1669 "(RM 3.10.2 (12.2))", E);
1670 end if;
1671
1672 -- The initialization expression is a reference to an access
1673 -- discriminant. The type of the discriminant is always deeper
1674 -- than any access type.
1675
1676 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1677 and then Is_Entity_Name (E)
1678 and then Ekind (Entity (E)) = E_In_Parameter
1679 and then Present (Discriminal_Link (Entity (E)))
1680 then
1681 Error_Msg_N
1682 ("discriminant has deeper accessibility level than target",
1683 E);
1684 end if;
1685 end if;
1686 end if;
1687
1688 -- The parent type may be a private view with unknown discriminants,
1689 -- and thus unconstrained. Regular components must be constrained.
1690
1691 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1692 if Is_Class_Wide_Type (T) then
1693 Error_Msg_N
1694 ("class-wide subtype with unknown discriminants" &
1695 " in component declaration",
1696 Subtype_Indication (Component_Definition (N)));
1697 else
1698 Error_Msg_N
1699 ("unconstrained subtype in component declaration",
1700 Subtype_Indication (Component_Definition (N)));
1701 end if;
1702
1703 -- Components cannot be abstract, except for the special case of
1704 -- the _Parent field (case of extending an abstract tagged type)
1705
1706 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1707 Error_Msg_N ("type of a component cannot be abstract", N);
1708 end if;
1709
1710 Set_Etype (Id, T);
1711 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1712
1713 -- The component declaration may have a per-object constraint, set
1714 -- the appropriate flag in the defining identifier of the subtype.
1715
1716 if Present (Subtype_Indication (Component_Definition (N))) then
1717 declare
1718 Sindic : constant Node_Id :=
1719 Subtype_Indication (Component_Definition (N));
1720 begin
1721 if Nkind (Sindic) = N_Subtype_Indication
1722 and then Present (Constraint (Sindic))
1723 and then Contains_POC (Constraint (Sindic))
1724 then
1725 Set_Has_Per_Object_Constraint (Id);
1726 end if;
1727 end;
1728 end if;
1729
1730 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1731 -- out some static checks.
1732
1733 if Ada_Version >= Ada_05
1734 and then Can_Never_Be_Null (T)
1735 then
1736 Null_Exclusion_Static_Checks (N);
1737 end if;
1738
1739 -- If this component is private (or depends on a private type), flag the
1740 -- record type to indicate that some operations are not available.
1741
1742 P := Private_Component (T);
1743
1744 if Present (P) then
1745
1746 -- Check for circular definitions
1747
1748 if P = Any_Type then
1749 Set_Etype (Id, Any_Type);
1750
1751 -- There is a gap in the visibility of operations only if the
1752 -- component type is not defined in the scope of the record type.
1753
1754 elsif Scope (P) = Scope (Current_Scope) then
1755 null;
1756
1757 elsif Is_Limited_Type (P) then
1758 Set_Is_Limited_Composite (Current_Scope);
1759
1760 else
1761 Set_Is_Private_Composite (Current_Scope);
1762 end if;
1763 end if;
1764
1765 if P /= Any_Type
1766 and then Is_Limited_Type (T)
1767 and then Chars (Id) /= Name_uParent
1768 and then Is_Tagged_Type (Current_Scope)
1769 then
1770 if Is_Derived_Type (Current_Scope)
1771 and then not Is_Known_Limited (Current_Scope)
1772 then
1773 Error_Msg_N
1774 ("extension of nonlimited type cannot have limited components",
1775 N);
1776
1777 if Is_Interface (Root_Type (Current_Scope)) then
1778 Error_Msg_N
1779 ("\limitedness is not inherited from limited interface", N);
1780 Error_Msg_N
1781 ("\add LIMITED to type indication", N);
1782 end if;
1783
1784 Explain_Limited_Type (T, N);
1785 Set_Etype (Id, Any_Type);
1786 Set_Is_Limited_Composite (Current_Scope, False);
1787
1788 elsif not Is_Derived_Type (Current_Scope)
1789 and then not Is_Limited_Record (Current_Scope)
1790 and then not Is_Concurrent_Type (Current_Scope)
1791 then
1792 Error_Msg_N
1793 ("nonlimited tagged type cannot have limited components", N);
1794 Explain_Limited_Type (T, N);
1795 Set_Etype (Id, Any_Type);
1796 Set_Is_Limited_Composite (Current_Scope, False);
1797 end if;
1798 end if;
1799
1800 Set_Original_Record_Component (Id, Id);
1801 end Analyze_Component_Declaration;
1802
1803 --------------------------
1804 -- Analyze_Declarations --
1805 --------------------------
1806
1807 procedure Analyze_Declarations (L : List_Id) is
1808 D : Node_Id;
1809 Freeze_From : Entity_Id := Empty;
1810 Next_Node : Node_Id;
1811
1812 procedure Adjust_D;
1813 -- Adjust D not to include implicit label declarations, since these
1814 -- have strange Sloc values that result in elaboration check problems.
1815 -- (They have the sloc of the label as found in the source, and that
1816 -- is ahead of the current declarative part).
1817
1818 --------------
1819 -- Adjust_D --
1820 --------------
1821
1822 procedure Adjust_D is
1823 begin
1824 while Present (Prev (D))
1825 and then Nkind (D) = N_Implicit_Label_Declaration
1826 loop
1827 Prev (D);
1828 end loop;
1829 end Adjust_D;
1830
1831 -- Start of processing for Analyze_Declarations
1832
1833 begin
1834 D := First (L);
1835 while Present (D) loop
1836
1837 -- Complete analysis of declaration
1838
1839 Analyze (D);
1840 Next_Node := Next (D);
1841
1842 if No (Freeze_From) then
1843 Freeze_From := First_Entity (Current_Scope);
1844 end if;
1845
1846 -- At the end of a declarative part, freeze remaining entities
1847 -- declared in it. The end of the visible declarations of package
1848 -- specification is not the end of a declarative part if private
1849 -- declarations are present. The end of a package declaration is a
1850 -- freezing point only if it a library package. A task definition or
1851 -- protected type definition is not a freeze point either. Finally,
1852 -- we do not freeze entities in generic scopes, because there is no
1853 -- code generated for them and freeze nodes will be generated for
1854 -- the instance.
1855
1856 -- The end of a package instantiation is not a freeze point, but
1857 -- for now we make it one, because the generic body is inserted
1858 -- (currently) immediately after. Generic instantiations will not
1859 -- be a freeze point once delayed freezing of bodies is implemented.
1860 -- (This is needed in any case for early instantiations ???).
1861
1862 if No (Next_Node) then
1863 if Nkind_In (Parent (L), N_Component_List,
1864 N_Task_Definition,
1865 N_Protected_Definition)
1866 then
1867 null;
1868
1869 elsif Nkind (Parent (L)) /= N_Package_Specification then
1870 if Nkind (Parent (L)) = N_Package_Body then
1871 Freeze_From := First_Entity (Current_Scope);
1872 end if;
1873
1874 Adjust_D;
1875 Freeze_All (Freeze_From, D);
1876 Freeze_From := Last_Entity (Current_Scope);
1877
1878 elsif Scope (Current_Scope) /= Standard_Standard
1879 and then not Is_Child_Unit (Current_Scope)
1880 and then No (Generic_Parent (Parent (L)))
1881 then
1882 null;
1883
1884 elsif L /= Visible_Declarations (Parent (L))
1885 or else No (Private_Declarations (Parent (L)))
1886 or else Is_Empty_List (Private_Declarations (Parent (L)))
1887 then
1888 Adjust_D;
1889 Freeze_All (Freeze_From, D);
1890 Freeze_From := Last_Entity (Current_Scope);
1891 end if;
1892
1893 -- If next node is a body then freeze all types before the body.
1894 -- An exception occurs for some expander-generated bodies. If these
1895 -- are generated at places where in general language rules would not
1896 -- allow a freeze point, then we assume that the expander has
1897 -- explicitly checked that all required types are properly frozen,
1898 -- and we do not cause general freezing here. This special circuit
1899 -- is used when the encountered body is marked as having already
1900 -- been analyzed.
1901
1902 -- In all other cases (bodies that come from source, and expander
1903 -- generated bodies that have not been analyzed yet), freeze all
1904 -- types now. Note that in the latter case, the expander must take
1905 -- care to attach the bodies at a proper place in the tree so as to
1906 -- not cause unwanted freezing at that point.
1907
1908 elsif not Analyzed (Next_Node)
1909 and then (Nkind_In (Next_Node, N_Subprogram_Body,
1910 N_Entry_Body,
1911 N_Package_Body,
1912 N_Protected_Body,
1913 N_Task_Body)
1914 or else
1915 Nkind (Next_Node) in N_Body_Stub)
1916 then
1917 Adjust_D;
1918 Freeze_All (Freeze_From, D);
1919 Freeze_From := Last_Entity (Current_Scope);
1920 end if;
1921
1922 D := Next_Node;
1923 end loop;
1924 end Analyze_Declarations;
1925
1926 ----------------------------------
1927 -- Analyze_Incomplete_Type_Decl --
1928 ----------------------------------
1929
1930 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1931 F : constant Boolean := Is_Pure (Current_Scope);
1932 T : Entity_Id;
1933
1934 begin
1935 Generate_Definition (Defining_Identifier (N));
1936
1937 -- Process an incomplete declaration. The identifier must not have been
1938 -- declared already in the scope. However, an incomplete declaration may
1939 -- appear in the private part of a package, for a private type that has
1940 -- already been declared.
1941
1942 -- In this case, the discriminants (if any) must match
1943
1944 T := Find_Type_Name (N);
1945
1946 Set_Ekind (T, E_Incomplete_Type);
1947 Init_Size_Align (T);
1948 Set_Is_First_Subtype (T, True);
1949 Set_Etype (T, T);
1950
1951 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
1952 -- incomplete types.
1953
1954 if Tagged_Present (N) then
1955 Set_Is_Tagged_Type (T);
1956 Make_Class_Wide_Type (T);
1957 Set_Primitive_Operations (T, New_Elmt_List);
1958 end if;
1959
1960 Push_Scope (T);
1961
1962 Set_Stored_Constraint (T, No_Elist);
1963
1964 if Present (Discriminant_Specifications (N)) then
1965 Process_Discriminants (N);
1966 end if;
1967
1968 End_Scope;
1969
1970 -- If the type has discriminants, non-trivial subtypes may be be
1971 -- declared before the full view of the type. The full views of those
1972 -- subtypes will be built after the full view of the type.
1973
1974 Set_Private_Dependents (T, New_Elmt_List);
1975 Set_Is_Pure (T, F);
1976 end Analyze_Incomplete_Type_Decl;
1977
1978 -----------------------------------
1979 -- Analyze_Interface_Declaration --
1980 -----------------------------------
1981
1982 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
1983 CW : constant Entity_Id := Class_Wide_Type (T);
1984
1985 begin
1986 Set_Is_Tagged_Type (T);
1987
1988 Set_Is_Limited_Record (T, Limited_Present (Def)
1989 or else Task_Present (Def)
1990 or else Protected_Present (Def)
1991 or else Synchronized_Present (Def));
1992
1993 -- Type is abstract if full declaration carries keyword, or if previous
1994 -- partial view did.
1995
1996 Set_Is_Abstract_Type (T);
1997 Set_Is_Interface (T);
1998
1999 -- Type is a limited interface if it includes the keyword limited, task,
2000 -- protected, or synchronized.
2001
2002 Set_Is_Limited_Interface
2003 (T, Limited_Present (Def)
2004 or else Protected_Present (Def)
2005 or else Synchronized_Present (Def)
2006 or else Task_Present (Def));
2007
2008 Set_Is_Protected_Interface (T, Protected_Present (Def));
2009 Set_Is_Task_Interface (T, Task_Present (Def));
2010
2011 -- Type is a synchronized interface if it includes the keyword task,
2012 -- protected, or synchronized.
2013
2014 Set_Is_Synchronized_Interface
2015 (T, Synchronized_Present (Def)
2016 or else Protected_Present (Def)
2017 or else Task_Present (Def));
2018
2019 Set_Interfaces (T, New_Elmt_List);
2020 Set_Primitive_Operations (T, New_Elmt_List);
2021
2022 -- Complete the decoration of the class-wide entity if it was already
2023 -- built (i.e. during the creation of the limited view)
2024
2025 if Present (CW) then
2026 Set_Is_Interface (CW);
2027 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2028 Set_Is_Protected_Interface (CW, Is_Protected_Interface (T));
2029 Set_Is_Synchronized_Interface (CW, Is_Synchronized_Interface (T));
2030 Set_Is_Task_Interface (CW, Is_Task_Interface (T));
2031 end if;
2032
2033 -- Check runtime support for synchronized interfaces
2034
2035 if VM_Target = No_VM
2036 and then (Is_Task_Interface (T)
2037 or else Is_Protected_Interface (T)
2038 or else Is_Synchronized_Interface (T))
2039 and then not RTE_Available (RE_Select_Specific_Data)
2040 then
2041 Error_Msg_CRT ("synchronized interfaces", T);
2042 end if;
2043 end Analyze_Interface_Declaration;
2044
2045 -----------------------------
2046 -- Analyze_Itype_Reference --
2047 -----------------------------
2048
2049 -- Nothing to do. This node is placed in the tree only for the benefit of
2050 -- back end processing, and has no effect on the semantic processing.
2051
2052 procedure Analyze_Itype_Reference (N : Node_Id) is
2053 begin
2054 pragma Assert (Is_Itype (Itype (N)));
2055 null;
2056 end Analyze_Itype_Reference;
2057
2058 --------------------------------
2059 -- Analyze_Number_Declaration --
2060 --------------------------------
2061
2062 procedure Analyze_Number_Declaration (N : Node_Id) is
2063 Id : constant Entity_Id := Defining_Identifier (N);
2064 E : constant Node_Id := Expression (N);
2065 T : Entity_Id;
2066 Index : Interp_Index;
2067 It : Interp;
2068
2069 begin
2070 Generate_Definition (Id);
2071 Enter_Name (Id);
2072
2073 -- This is an optimization of a common case of an integer literal
2074
2075 if Nkind (E) = N_Integer_Literal then
2076 Set_Is_Static_Expression (E, True);
2077 Set_Etype (E, Universal_Integer);
2078
2079 Set_Etype (Id, Universal_Integer);
2080 Set_Ekind (Id, E_Named_Integer);
2081 Set_Is_Frozen (Id, True);
2082 return;
2083 end if;
2084
2085 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2086
2087 -- Process expression, replacing error by integer zero, to avoid
2088 -- cascaded errors or aborts further along in the processing
2089
2090 -- Replace Error by integer zero, which seems least likely to
2091 -- cause cascaded errors.
2092
2093 if E = Error then
2094 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2095 Set_Error_Posted (E);
2096 end if;
2097
2098 Analyze (E);
2099
2100 -- Verify that the expression is static and numeric. If
2101 -- the expression is overloaded, we apply the preference
2102 -- rule that favors root numeric types.
2103
2104 if not Is_Overloaded (E) then
2105 T := Etype (E);
2106
2107 else
2108 T := Any_Type;
2109
2110 Get_First_Interp (E, Index, It);
2111 while Present (It.Typ) loop
2112 if (Is_Integer_Type (It.Typ)
2113 or else Is_Real_Type (It.Typ))
2114 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2115 then
2116 if T = Any_Type then
2117 T := It.Typ;
2118
2119 elsif It.Typ = Universal_Real
2120 or else It.Typ = Universal_Integer
2121 then
2122 -- Choose universal interpretation over any other
2123
2124 T := It.Typ;
2125 exit;
2126 end if;
2127 end if;
2128
2129 Get_Next_Interp (Index, It);
2130 end loop;
2131 end if;
2132
2133 if Is_Integer_Type (T) then
2134 Resolve (E, T);
2135 Set_Etype (Id, Universal_Integer);
2136 Set_Ekind (Id, E_Named_Integer);
2137
2138 elsif Is_Real_Type (T) then
2139
2140 -- Because the real value is converted to universal_real, this is a
2141 -- legal context for a universal fixed expression.
2142
2143 if T = Universal_Fixed then
2144 declare
2145 Loc : constant Source_Ptr := Sloc (N);
2146 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2147 Subtype_Mark =>
2148 New_Occurrence_Of (Universal_Real, Loc),
2149 Expression => Relocate_Node (E));
2150
2151 begin
2152 Rewrite (E, Conv);
2153 Analyze (E);
2154 end;
2155
2156 elsif T = Any_Fixed then
2157 Error_Msg_N ("illegal context for mixed mode operation", E);
2158
2159 -- Expression is of the form : universal_fixed * integer. Try to
2160 -- resolve as universal_real.
2161
2162 T := Universal_Real;
2163 Set_Etype (E, T);
2164 end if;
2165
2166 Resolve (E, T);
2167 Set_Etype (Id, Universal_Real);
2168 Set_Ekind (Id, E_Named_Real);
2169
2170 else
2171 Wrong_Type (E, Any_Numeric);
2172 Resolve (E, T);
2173
2174 Set_Etype (Id, T);
2175 Set_Ekind (Id, E_Constant);
2176 Set_Never_Set_In_Source (Id, True);
2177 Set_Is_True_Constant (Id, True);
2178 return;
2179 end if;
2180
2181 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2182 Set_Etype (E, Etype (Id));
2183 end if;
2184
2185 if not Is_OK_Static_Expression (E) then
2186 Flag_Non_Static_Expr
2187 ("non-static expression used in number declaration!", E);
2188 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2189 Set_Etype (E, Any_Type);
2190 end if;
2191 end Analyze_Number_Declaration;
2192
2193 --------------------------------
2194 -- Analyze_Object_Declaration --
2195 --------------------------------
2196
2197 procedure Analyze_Object_Declaration (N : Node_Id) is
2198 Loc : constant Source_Ptr := Sloc (N);
2199 Id : constant Entity_Id := Defining_Identifier (N);
2200 T : Entity_Id;
2201 Act_T : Entity_Id;
2202
2203 E : Node_Id := Expression (N);
2204 -- E is set to Expression (N) throughout this routine. When
2205 -- Expression (N) is modified, E is changed accordingly.
2206
2207 Prev_Entity : Entity_Id := Empty;
2208
2209 function Count_Tasks (T : Entity_Id) return Uint;
2210 -- This function is called when a non-generic library level object of a
2211 -- task type is declared. Its function is to count the static number of
2212 -- tasks declared within the type (it is only called if Has_Tasks is set
2213 -- for T). As a side effect, if an array of tasks with non-static bounds
2214 -- or a variant record type is encountered, Check_Restrictions is called
2215 -- indicating the count is unknown.
2216
2217 -----------------
2218 -- Count_Tasks --
2219 -----------------
2220
2221 function Count_Tasks (T : Entity_Id) return Uint is
2222 C : Entity_Id;
2223 X : Node_Id;
2224 V : Uint;
2225
2226 begin
2227 if Is_Task_Type (T) then
2228 return Uint_1;
2229
2230 elsif Is_Record_Type (T) then
2231 if Has_Discriminants (T) then
2232 Check_Restriction (Max_Tasks, N);
2233 return Uint_0;
2234
2235 else
2236 V := Uint_0;
2237 C := First_Component (T);
2238 while Present (C) loop
2239 V := V + Count_Tasks (Etype (C));
2240 Next_Component (C);
2241 end loop;
2242
2243 return V;
2244 end if;
2245
2246 elsif Is_Array_Type (T) then
2247 X := First_Index (T);
2248 V := Count_Tasks (Component_Type (T));
2249 while Present (X) loop
2250 C := Etype (X);
2251
2252 if not Is_Static_Subtype (C) then
2253 Check_Restriction (Max_Tasks, N);
2254 return Uint_0;
2255 else
2256 V := V * (UI_Max (Uint_0,
2257 Expr_Value (Type_High_Bound (C)) -
2258 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2259 end if;
2260
2261 Next_Index (X);
2262 end loop;
2263
2264 return V;
2265
2266 else
2267 return Uint_0;
2268 end if;
2269 end Count_Tasks;
2270
2271 -- Start of processing for Analyze_Object_Declaration
2272
2273 begin
2274 -- There are three kinds of implicit types generated by an
2275 -- object declaration:
2276
2277 -- 1. Those for generated by the original Object Definition
2278
2279 -- 2. Those generated by the Expression
2280
2281 -- 3. Those used to constrained the Object Definition with the
2282 -- expression constraints when it is unconstrained
2283
2284 -- They must be generated in this order to avoid order of elaboration
2285 -- issues. Thus the first step (after entering the name) is to analyze
2286 -- the object definition.
2287
2288 if Constant_Present (N) then
2289 Prev_Entity := Current_Entity_In_Scope (Id);
2290
2291 -- If the homograph is an implicit subprogram, it is overridden by
2292 -- the current declaration.
2293
2294 if Present (Prev_Entity)
2295 and then
2296 ((Is_Overloadable (Prev_Entity)
2297 and then Is_Inherited_Operation (Prev_Entity))
2298
2299 -- The current object is a discriminal generated for an entry
2300 -- family index. Even though the index is a constant, in this
2301 -- particular context there is no true constant redeclaration.
2302 -- Enter_Name will handle the visibility.
2303
2304 or else
2305 (Is_Discriminal (Id)
2306 and then Ekind (Discriminal_Link (Id)) =
2307 E_Entry_Index_Parameter))
2308 then
2309 Prev_Entity := Empty;
2310 end if;
2311 end if;
2312
2313 if Present (Prev_Entity) then
2314 Constant_Redeclaration (Id, N, T);
2315
2316 Generate_Reference (Prev_Entity, Id, 'c');
2317 Set_Completion_Referenced (Id);
2318
2319 if Error_Posted (N) then
2320
2321 -- Type mismatch or illegal redeclaration, Do not analyze
2322 -- expression to avoid cascaded errors.
2323
2324 T := Find_Type_Of_Object (Object_Definition (N), N);
2325 Set_Etype (Id, T);
2326 Set_Ekind (Id, E_Variable);
2327 return;
2328 end if;
2329
2330 -- In the normal case, enter identifier at the start to catch premature
2331 -- usage in the initialization expression.
2332
2333 else
2334 Generate_Definition (Id);
2335 Enter_Name (Id);
2336
2337 Mark_Coextensions (N, Object_Definition (N));
2338
2339 T := Find_Type_Of_Object (Object_Definition (N), N);
2340
2341 if Nkind (Object_Definition (N)) = N_Access_Definition
2342 and then Present
2343 (Access_To_Subprogram_Definition (Object_Definition (N)))
2344 and then Protected_Present
2345 (Access_To_Subprogram_Definition (Object_Definition (N)))
2346 then
2347 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2348 end if;
2349
2350 if Error_Posted (Id) then
2351 Set_Etype (Id, T);
2352 Set_Ekind (Id, E_Variable);
2353 return;
2354 end if;
2355 end if;
2356
2357 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2358 -- out some static checks
2359
2360 if Ada_Version >= Ada_05
2361 and then Can_Never_Be_Null (T)
2362 then
2363 -- In case of aggregates we must also take care of the correct
2364 -- initialization of nested aggregates bug this is done at the
2365 -- point of the analysis of the aggregate (see sem_aggr.adb)
2366
2367 if Present (Expression (N))
2368 and then Nkind (Expression (N)) = N_Aggregate
2369 then
2370 null;
2371
2372 else
2373 declare
2374 Save_Typ : constant Entity_Id := Etype (Id);
2375 begin
2376 Set_Etype (Id, T); -- Temp. decoration for static checks
2377 Null_Exclusion_Static_Checks (N);
2378 Set_Etype (Id, Save_Typ);
2379 end;
2380 end if;
2381 end if;
2382
2383 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2384
2385 -- If deferred constant, make sure context is appropriate. We detect
2386 -- a deferred constant as a constant declaration with no expression.
2387 -- A deferred constant can appear in a package body if its completion
2388 -- is by means of an interface pragma.
2389
2390 if Constant_Present (N)
2391 and then No (E)
2392 then
2393 -- We exclude forward references to tags
2394
2395 if Is_Imported (Defining_Identifier (N))
2396 and then
2397 (T = RTE (RE_Tag)
2398 or else
2399 (Present (Full_View (T))
2400 and then Full_View (T) = RTE (RE_Tag)))
2401 then
2402 null;
2403
2404 -- A deferred constant may appear in the declarative part of the
2405 -- following constructs:
2406
2407 -- blocks
2408 -- entry bodies
2409 -- extended return statements
2410 -- package specs
2411 -- package bodies
2412 -- subprogram bodies
2413 -- task bodies
2414
2415 -- When declared inside a package spec, a deferred constant must be
2416 -- completed by a full constant declaration or pragma Import. In all
2417 -- other cases, the only proper completion is pragma Import. Extended
2418 -- return statements are flagged as invalid contexts because they do
2419 -- not have a declarative part and so cannot accommodate the pragma.
2420
2421 elsif Ekind (Current_Scope) = E_Return_Statement then
2422 Error_Msg_N
2423 ("invalid context for deferred constant declaration (RM 7.4)",
2424 N);
2425 Error_Msg_N
2426 ("\declaration requires an initialization expression",
2427 N);
2428 Set_Constant_Present (N, False);
2429
2430 -- In Ada 83, deferred constant must be of private type
2431
2432 elsif not Is_Private_Type (T) then
2433 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2434 Error_Msg_N
2435 ("(Ada 83) deferred constant must be private type", N);
2436 end if;
2437 end if;
2438
2439 -- If not a deferred constant, then object declaration freezes its type
2440
2441 else
2442 Check_Fully_Declared (T, N);
2443 Freeze_Before (N, T);
2444 end if;
2445
2446 -- If the object was created by a constrained array definition, then
2447 -- set the link in both the anonymous base type and anonymous subtype
2448 -- that are built to represent the array type to point to the object.
2449
2450 if Nkind (Object_Definition (Declaration_Node (Id))) =
2451 N_Constrained_Array_Definition
2452 then
2453 Set_Related_Array_Object (T, Id);
2454 Set_Related_Array_Object (Base_Type (T), Id);
2455 end if;
2456
2457 -- Special checks for protected objects not at library level
2458
2459 if Is_Protected_Type (T)
2460 and then not Is_Library_Level_Entity (Id)
2461 then
2462 Check_Restriction (No_Local_Protected_Objects, Id);
2463
2464 -- Protected objects with interrupt handlers must be at library level
2465
2466 -- Ada 2005: this test is not needed (and the corresponding clause
2467 -- in the RM is removed) because accessibility checks are sufficient
2468 -- to make handlers not at the library level illegal.
2469
2470 if Has_Interrupt_Handler (T)
2471 and then Ada_Version < Ada_05
2472 then
2473 Error_Msg_N
2474 ("interrupt object can only be declared at library level", Id);
2475 end if;
2476 end if;
2477
2478 -- The actual subtype of the object is the nominal subtype, unless
2479 -- the nominal one is unconstrained and obtained from the expression.
2480
2481 Act_T := T;
2482
2483 -- Process initialization expression if present and not in error
2484
2485 if Present (E) and then E /= Error then
2486
2487 -- Generate an error in case of CPP class-wide object initialization.
2488 -- Required because otherwise the expansion of the class-wide
2489 -- assignment would try to use 'size to initialize the object
2490 -- (primitive that is not available in CPP tagged types).
2491
2492 if Is_Class_Wide_Type (Act_T)
2493 and then
2494 (Is_CPP_Class (Root_Type (Etype (Act_T)))
2495 or else
2496 (Present (Full_View (Root_Type (Etype (Act_T))))
2497 and then
2498 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
2499 then
2500 Error_Msg_N
2501 ("predefined assignment not available for 'C'P'P tagged types",
2502 E);
2503 end if;
2504
2505 Mark_Coextensions (N, E);
2506 Analyze (E);
2507
2508 -- In case of errors detected in the analysis of the expression,
2509 -- decorate it with the expected type to avoid cascaded errors
2510
2511 if No (Etype (E)) then
2512 Set_Etype (E, T);
2513 end if;
2514
2515 -- If an initialization expression is present, then we set the
2516 -- Is_True_Constant flag. It will be reset if this is a variable
2517 -- and it is indeed modified.
2518
2519 Set_Is_True_Constant (Id, True);
2520
2521 -- If we are analyzing a constant declaration, set its completion
2522 -- flag after analyzing and resolving the expression.
2523
2524 if Constant_Present (N) then
2525 Set_Has_Completion (Id);
2526 end if;
2527
2528 -- Set type and resolve (type may be overridden later on)
2529
2530 Set_Etype (Id, T);
2531 Resolve (E, T);
2532
2533 -- If the object is an access to variable, the initialization
2534 -- expression cannot be an access to constant.
2535
2536 if Is_Access_Type (T)
2537 and then not Is_Access_Constant (T)
2538 and then Is_Access_Type (Etype (E))
2539 and then Is_Access_Constant (Etype (E))
2540 then
2541 Error_Msg_N
2542 ("object that is an access to variable cannot be initialized " &
2543 "with an access-to-constant expression", E);
2544 end if;
2545
2546 if not Assignment_OK (N) then
2547 Check_Initialization (T, E);
2548 end if;
2549
2550 Check_Unset_Reference (E);
2551
2552 -- If this is a variable, then set current value
2553
2554 if not Constant_Present (N) then
2555 if Compile_Time_Known_Value (E) then
2556 Set_Current_Value (Id, E);
2557 end if;
2558 end if;
2559
2560 -- Deal with setting of null flags
2561
2562 if Is_Access_Type (T) then
2563 if Known_Non_Null (E) then
2564 Set_Is_Known_Non_Null (Id, True);
2565 elsif Known_Null (E)
2566 and then not Can_Never_Be_Null (Id)
2567 then
2568 Set_Is_Known_Null (Id, True);
2569 end if;
2570 end if;
2571
2572 -- Check incorrect use of dynamically tagged expressions. Note
2573 -- the use of Is_Tagged_Type (T) which seems redundant but is in
2574 -- fact important to avoid spurious errors due to expanded code
2575 -- for dispatching functions over an anonymous access type
2576
2577 if (Is_Class_Wide_Type (Etype (E)) or else Is_Dynamically_Tagged (E))
2578 and then Is_Tagged_Type (T)
2579 and then not Is_Class_Wide_Type (T)
2580 then
2581 Error_Msg_N ("dynamically tagged expression not allowed!", E);
2582 end if;
2583
2584 Apply_Scalar_Range_Check (E, T);
2585 Apply_Static_Length_Check (E, T);
2586 end if;
2587
2588 -- If the No_Streams restriction is set, check that the type of the
2589 -- object is not, and does not contain, any subtype derived from
2590 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
2591 -- Has_Stream just for efficiency reasons. There is no point in
2592 -- spending time on a Has_Stream check if the restriction is not set.
2593
2594 if Restrictions.Set (No_Streams) then
2595 if Has_Stream (T) then
2596 Check_Restriction (No_Streams, N);
2597 end if;
2598 end if;
2599
2600 -- Abstract type is never permitted for a variable or constant.
2601 -- Note: we inhibit this check for objects that do not come from
2602 -- source because there is at least one case (the expansion of
2603 -- x'class'input where x is abstract) where we legitimately
2604 -- generate an abstract object.
2605
2606 if Is_Abstract_Type (T) and then Comes_From_Source (N) then
2607 Error_Msg_N ("type of object cannot be abstract",
2608 Object_Definition (N));
2609
2610 if Is_CPP_Class (T) then
2611 Error_Msg_NE ("\} may need a cpp_constructor",
2612 Object_Definition (N), T);
2613 end if;
2614
2615 -- Case of unconstrained type
2616
2617 elsif Is_Indefinite_Subtype (T) then
2618
2619 -- Nothing to do in deferred constant case
2620
2621 if Constant_Present (N) and then No (E) then
2622 null;
2623
2624 -- Case of no initialization present
2625
2626 elsif No (E) then
2627 if No_Initialization (N) then
2628 null;
2629
2630 elsif Is_Class_Wide_Type (T) then
2631 Error_Msg_N
2632 ("initialization required in class-wide declaration ", N);
2633
2634 else
2635 Error_Msg_N
2636 ("unconstrained subtype not allowed (need initialization)",
2637 Object_Definition (N));
2638
2639 if Is_Record_Type (T) and then Has_Discriminants (T) then
2640 Error_Msg_N
2641 ("\provide initial value or explicit discriminant values",
2642 Object_Definition (N));
2643
2644 Error_Msg_NE
2645 ("\or give default discriminant values for type&",
2646 Object_Definition (N), T);
2647
2648 elsif Is_Array_Type (T) then
2649 Error_Msg_N
2650 ("\provide initial value or explicit array bounds",
2651 Object_Definition (N));
2652 end if;
2653 end if;
2654
2655 -- Case of initialization present but in error. Set initial
2656 -- expression as absent (but do not make above complaints)
2657
2658 elsif E = Error then
2659 Set_Expression (N, Empty);
2660 E := Empty;
2661
2662 -- Case of initialization present
2663
2664 else
2665 -- Not allowed in Ada 83
2666
2667 if not Constant_Present (N) then
2668 if Ada_Version = Ada_83
2669 and then Comes_From_Source (Object_Definition (N))
2670 then
2671 Error_Msg_N
2672 ("(Ada 83) unconstrained variable not allowed",
2673 Object_Definition (N));
2674 end if;
2675 end if;
2676
2677 -- Now we constrain the variable from the initializing expression
2678
2679 -- If the expression is an aggregate, it has been expanded into
2680 -- individual assignments. Retrieve the actual type from the
2681 -- expanded construct.
2682
2683 if Is_Array_Type (T)
2684 and then No_Initialization (N)
2685 and then Nkind (Original_Node (E)) = N_Aggregate
2686 then
2687 Act_T := Etype (E);
2688
2689 else
2690 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
2691 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
2692 end if;
2693
2694 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
2695
2696 if Aliased_Present (N) then
2697 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2698 end if;
2699
2700 Freeze_Before (N, Act_T);
2701 Freeze_Before (N, T);
2702 end if;
2703
2704 elsif Is_Array_Type (T)
2705 and then No_Initialization (N)
2706 and then Nkind (Original_Node (E)) = N_Aggregate
2707 then
2708 if not Is_Entity_Name (Object_Definition (N)) then
2709 Act_T := Etype (E);
2710 Check_Compile_Time_Size (Act_T);
2711
2712 if Aliased_Present (N) then
2713 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2714 end if;
2715 end if;
2716
2717 -- When the given object definition and the aggregate are specified
2718 -- independently, and their lengths might differ do a length check.
2719 -- This cannot happen if the aggregate is of the form (others =>...)
2720
2721 if not Is_Constrained (T) then
2722 null;
2723
2724 elsif Nkind (E) = N_Raise_Constraint_Error then
2725
2726 -- Aggregate is statically illegal. Place back in declaration
2727
2728 Set_Expression (N, E);
2729 Set_No_Initialization (N, False);
2730
2731 elsif T = Etype (E) then
2732 null;
2733
2734 elsif Nkind (E) = N_Aggregate
2735 and then Present (Component_Associations (E))
2736 and then Present (Choices (First (Component_Associations (E))))
2737 and then Nkind (First
2738 (Choices (First (Component_Associations (E))))) = N_Others_Choice
2739 then
2740 null;
2741
2742 else
2743 Apply_Length_Check (E, T);
2744 end if;
2745
2746 -- If the type is limited unconstrained with defaulted discriminants
2747 -- and there is no expression, then the object is constrained by the
2748 -- defaults, so it is worthwhile building the corresponding subtype.
2749
2750 elsif (Is_Limited_Record (T)
2751 or else Is_Concurrent_Type (T))
2752 and then not Is_Constrained (T)
2753 and then Has_Discriminants (T)
2754 then
2755 if No (E) then
2756 Act_T := Build_Default_Subtype (T, N);
2757 else
2758 -- Ada 2005: a limited object may be initialized by means of an
2759 -- aggregate. If the type has default discriminants it has an
2760 -- unconstrained nominal type, Its actual subtype will be obtained
2761 -- from the aggregate, and not from the default discriminants.
2762
2763 Act_T := Etype (E);
2764 end if;
2765
2766 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
2767
2768 elsif Present (Underlying_Type (T))
2769 and then not Is_Constrained (Underlying_Type (T))
2770 and then Has_Discriminants (Underlying_Type (T))
2771 and then Nkind (E) = N_Function_Call
2772 and then Constant_Present (N)
2773 then
2774 -- The back-end has problems with constants of a discriminated type
2775 -- with defaults, if the initial value is a function call. We
2776 -- generate an intermediate temporary for the result of the call.
2777 -- It is unclear why this should make it acceptable to gcc. ???
2778
2779 Remove_Side_Effects (E);
2780 end if;
2781
2782 -- Check No_Wide_Characters restriction
2783
2784 if T = Standard_Wide_Character
2785 or else T = Standard_Wide_Wide_Character
2786 or else Root_Type (T) = Standard_Wide_String
2787 or else Root_Type (T) = Standard_Wide_Wide_String
2788 then
2789 Check_Restriction (No_Wide_Characters, Object_Definition (N));
2790 end if;
2791
2792 -- Indicate this is not set in source. Certainly true for constants,
2793 -- and true for variables so far (will be reset for a variable if and
2794 -- when we encounter a modification in the source).
2795
2796 Set_Never_Set_In_Source (Id, True);
2797
2798 -- Now establish the proper kind and type of the object
2799
2800 if Constant_Present (N) then
2801 Set_Ekind (Id, E_Constant);
2802 Set_Is_True_Constant (Id, True);
2803
2804 else
2805 Set_Ekind (Id, E_Variable);
2806
2807 -- A variable is set as shared passive if it appears in a shared
2808 -- passive package, and is at the outer level. This is not done
2809 -- for entities generated during expansion, because those are
2810 -- always manipulated locally.
2811
2812 if Is_Shared_Passive (Current_Scope)
2813 and then Is_Library_Level_Entity (Id)
2814 and then Comes_From_Source (Id)
2815 then
2816 Set_Is_Shared_Passive (Id);
2817 Check_Shared_Var (Id, T, N);
2818 end if;
2819
2820 -- Set Has_Initial_Value if initializing expression present. Note
2821 -- that if there is no initializing expression, we leave the state
2822 -- of this flag unchanged (usually it will be False, but notably in
2823 -- the case of exception choice variables, it will already be true).
2824
2825 if Present (E) then
2826 Set_Has_Initial_Value (Id, True);
2827 end if;
2828 end if;
2829
2830 -- Initialize alignment and size and capture alignment setting
2831
2832 Init_Alignment (Id);
2833 Init_Esize (Id);
2834 Set_Optimize_Alignment_Flags (Id);
2835
2836 -- Deal with aliased case
2837
2838 if Aliased_Present (N) then
2839 Set_Is_Aliased (Id);
2840
2841 -- If the object is aliased and the type is unconstrained with
2842 -- defaulted discriminants and there is no expression, then the
2843 -- object is constrained by the defaults, so it is worthwhile
2844 -- building the corresponding subtype.
2845
2846 -- Ada 2005 (AI-363): If the aliased object is discriminated and
2847 -- unconstrained, then only establish an actual subtype if the
2848 -- nominal subtype is indefinite. In definite cases the object is
2849 -- unconstrained in Ada 2005.
2850
2851 if No (E)
2852 and then Is_Record_Type (T)
2853 and then not Is_Constrained (T)
2854 and then Has_Discriminants (T)
2855 and then (Ada_Version < Ada_05 or else Is_Indefinite_Subtype (T))
2856 then
2857 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
2858 end if;
2859 end if;
2860
2861 -- Now we can set the type of the object
2862
2863 Set_Etype (Id, Act_T);
2864
2865 -- Deal with controlled types
2866
2867 if Has_Controlled_Component (Etype (Id))
2868 or else Is_Controlled (Etype (Id))
2869 then
2870 if not Is_Library_Level_Entity (Id) then
2871 Check_Restriction (No_Nested_Finalization, N);
2872 else
2873 Validate_Controlled_Object (Id);
2874 end if;
2875
2876 -- Generate a warning when an initialization causes an obvious ABE
2877 -- violation. If the init expression is a simple aggregate there
2878 -- shouldn't be any initialize/adjust call generated. This will be
2879 -- true as soon as aggregates are built in place when possible.
2880
2881 -- ??? at the moment we do not generate warnings for temporaries
2882 -- created for those aggregates although Program_Error might be
2883 -- generated if compiled with -gnato.
2884
2885 if Is_Controlled (Etype (Id))
2886 and then Comes_From_Source (Id)
2887 then
2888 declare
2889 BT : constant Entity_Id := Base_Type (Etype (Id));
2890
2891 Implicit_Call : Entity_Id;
2892 pragma Warnings (Off, Implicit_Call);
2893 -- ??? what is this for (never referenced!)
2894
2895 function Is_Aggr (N : Node_Id) return Boolean;
2896 -- Check that N is an aggregate
2897
2898 -------------
2899 -- Is_Aggr --
2900 -------------
2901
2902 function Is_Aggr (N : Node_Id) return Boolean is
2903 begin
2904 case Nkind (Original_Node (N)) is
2905 when N_Aggregate | N_Extension_Aggregate =>
2906 return True;
2907
2908 when N_Qualified_Expression |
2909 N_Type_Conversion |
2910 N_Unchecked_Type_Conversion =>
2911 return Is_Aggr (Expression (Original_Node (N)));
2912
2913 when others =>
2914 return False;
2915 end case;
2916 end Is_Aggr;
2917
2918 begin
2919 -- If no underlying type, we already are in an error situation.
2920 -- Do not try to add a warning since we do not have access to
2921 -- prim-op list.
2922
2923 if No (Underlying_Type (BT)) then
2924 Implicit_Call := Empty;
2925
2926 -- A generic type does not have usable primitive operators.
2927 -- Initialization calls are built for instances.
2928
2929 elsif Is_Generic_Type (BT) then
2930 Implicit_Call := Empty;
2931
2932 -- If the init expression is not an aggregate, an adjust call
2933 -- will be generated
2934
2935 elsif Present (E) and then not Is_Aggr (E) then
2936 Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2937
2938 -- If no init expression and we are not in the deferred
2939 -- constant case, an Initialize call will be generated
2940
2941 elsif No (E) and then not Constant_Present (N) then
2942 Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2943
2944 else
2945 Implicit_Call := Empty;
2946 end if;
2947 end;
2948 end if;
2949 end if;
2950
2951 if Has_Task (Etype (Id)) then
2952 Check_Restriction (No_Tasking, N);
2953
2954 -- Deal with counting max tasks
2955
2956 -- Nothing to do if inside a generic
2957
2958 if Inside_A_Generic then
2959 null;
2960
2961 -- If library level entity, then count tasks
2962
2963 elsif Is_Library_Level_Entity (Id) then
2964 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
2965
2966 -- If not library level entity, then indicate we don't know max
2967 -- tasks and also check task hierarchy restriction and blocking
2968 -- operation (since starting a task is definitely blocking!)
2969
2970 else
2971 Check_Restriction (Max_Tasks, N);
2972 Check_Restriction (No_Task_Hierarchy, N);
2973 Check_Potentially_Blocking_Operation (N);
2974 end if;
2975
2976 -- A rather specialized test. If we see two tasks being declared
2977 -- of the same type in the same object declaration, and the task
2978 -- has an entry with an address clause, we know that program error
2979 -- will be raised at run-time since we can't have two tasks with
2980 -- entries at the same address.
2981
2982 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
2983 declare
2984 E : Entity_Id;
2985
2986 begin
2987 E := First_Entity (Etype (Id));
2988 while Present (E) loop
2989 if Ekind (E) = E_Entry
2990 and then Present (Get_Attribute_Definition_Clause
2991 (E, Attribute_Address))
2992 then
2993 Error_Msg_N
2994 ("?more than one task with same entry address", N);
2995 Error_Msg_N
2996 ("\?Program_Error will be raised at run time", N);
2997 Insert_Action (N,
2998 Make_Raise_Program_Error (Loc,
2999 Reason => PE_Duplicated_Entry_Address));
3000 exit;
3001 end if;
3002
3003 Next_Entity (E);
3004 end loop;
3005 end;
3006 end if;
3007 end if;
3008
3009 -- Some simple constant-propagation: if the expression is a constant
3010 -- string initialized with a literal, share the literal. This avoids
3011 -- a run-time copy.
3012
3013 if Present (E)
3014 and then Is_Entity_Name (E)
3015 and then Ekind (Entity (E)) = E_Constant
3016 and then Base_Type (Etype (E)) = Standard_String
3017 then
3018 declare
3019 Val : constant Node_Id := Constant_Value (Entity (E));
3020 begin
3021 if Present (Val)
3022 and then Nkind (Val) = N_String_Literal
3023 then
3024 Rewrite (E, New_Copy (Val));
3025 end if;
3026 end;
3027 end if;
3028
3029 -- Another optimization: if the nominal subtype is unconstrained and
3030 -- the expression is a function call that returns an unconstrained
3031 -- type, rewrite the declaration as a renaming of the result of the
3032 -- call. The exceptions below are cases where the copy is expected,
3033 -- either by the back end (Aliased case) or by the semantics, as for
3034 -- initializing controlled types or copying tags for classwide types.
3035
3036 if Present (E)
3037 and then Nkind (E) = N_Explicit_Dereference
3038 and then Nkind (Original_Node (E)) = N_Function_Call
3039 and then not Is_Library_Level_Entity (Id)
3040 and then not Is_Constrained (Underlying_Type (T))
3041 and then not Is_Aliased (Id)
3042 and then not Is_Class_Wide_Type (T)
3043 and then not Is_Controlled (T)
3044 and then not Has_Controlled_Component (Base_Type (T))
3045 and then Expander_Active
3046 then
3047 Rewrite (N,
3048 Make_Object_Renaming_Declaration (Loc,
3049 Defining_Identifier => Id,
3050 Access_Definition => Empty,
3051 Subtype_Mark => New_Occurrence_Of
3052 (Base_Type (Etype (Id)), Loc),
3053 Name => E));
3054
3055 Set_Renamed_Object (Id, E);
3056
3057 -- Force generation of debugging information for the constant and for
3058 -- the renamed function call.
3059
3060 Set_Debug_Info_Needed (Id);
3061 Set_Debug_Info_Needed (Entity (Prefix (E)));
3062 end if;
3063
3064 if Present (Prev_Entity)
3065 and then Is_Frozen (Prev_Entity)
3066 and then not Error_Posted (Id)
3067 then
3068 Error_Msg_N ("full constant declaration appears too late", N);
3069 end if;
3070
3071 Check_Eliminated (Id);
3072
3073 -- Deal with setting In_Private_Part flag if in private part
3074
3075 if Ekind (Scope (Id)) = E_Package
3076 and then In_Private_Part (Scope (Id))
3077 then
3078 Set_In_Private_Part (Id);
3079 end if;
3080
3081 -- Check for violation of No_Local_Timing_Events
3082
3083 if Is_RTE (Etype (Id), RE_Timing_Event)
3084 and then not Is_Library_Level_Entity (Id)
3085 then
3086 Check_Restriction (No_Local_Timing_Events, N);
3087 end if;
3088 end Analyze_Object_Declaration;
3089
3090 ---------------------------
3091 -- Analyze_Others_Choice --
3092 ---------------------------
3093
3094 -- Nothing to do for the others choice node itself, the semantic analysis
3095 -- of the others choice will occur as part of the processing of the parent
3096
3097 procedure Analyze_Others_Choice (N : Node_Id) is
3098 pragma Warnings (Off, N);
3099 begin
3100 null;
3101 end Analyze_Others_Choice;
3102
3103 -------------------------------------------
3104 -- Analyze_Private_Extension_Declaration --
3105 -------------------------------------------
3106
3107 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3108 T : constant Entity_Id := Defining_Identifier (N);
3109 Indic : constant Node_Id := Subtype_Indication (N);
3110 Parent_Type : Entity_Id;
3111 Parent_Base : Entity_Id;
3112
3113 begin
3114 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3115
3116 if Is_Non_Empty_List (Interface_List (N)) then
3117 declare
3118 Intf : Node_Id;
3119 T : Entity_Id;
3120
3121 begin
3122 Intf := First (Interface_List (N));
3123 while Present (Intf) loop
3124 T := Find_Type_Of_Subtype_Indic (Intf);
3125
3126 Diagnose_Interface (Intf, T);
3127 Next (Intf);
3128 end loop;
3129 end;
3130 end if;
3131
3132 Generate_Definition (T);
3133 Enter_Name (T);
3134
3135 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3136 Parent_Base := Base_Type (Parent_Type);
3137
3138 if Parent_Type = Any_Type
3139 or else Etype (Parent_Type) = Any_Type
3140 then
3141 Set_Ekind (T, Ekind (Parent_Type));
3142 Set_Etype (T, Any_Type);
3143 return;
3144
3145 elsif not Is_Tagged_Type (Parent_Type) then
3146 Error_Msg_N
3147 ("parent of type extension must be a tagged type ", Indic);
3148 return;
3149
3150 elsif Ekind (Parent_Type) = E_Void
3151 or else Ekind (Parent_Type) = E_Incomplete_Type
3152 then
3153 Error_Msg_N ("premature derivation of incomplete type", Indic);
3154 return;
3155
3156 elsif Is_Concurrent_Type (Parent_Type) then
3157 Error_Msg_N
3158 ("parent type of a private extension cannot be "
3159 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3160
3161 Set_Etype (T, Any_Type);
3162 Set_Ekind (T, E_Limited_Private_Type);
3163 Set_Private_Dependents (T, New_Elmt_List);
3164 Set_Error_Posted (T);
3165 return;
3166 end if;
3167
3168 -- Perhaps the parent type should be changed to the class-wide type's
3169 -- specific type in this case to prevent cascading errors ???
3170
3171 if Is_Class_Wide_Type (Parent_Type) then
3172 Error_Msg_N
3173 ("parent of type extension must not be a class-wide type", Indic);
3174 return;
3175 end if;
3176
3177 if (not Is_Package_Or_Generic_Package (Current_Scope)
3178 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3179 or else In_Private_Part (Current_Scope)
3180
3181 then
3182 Error_Msg_N ("invalid context for private extension", N);
3183 end if;
3184
3185 -- Set common attributes
3186
3187 Set_Is_Pure (T, Is_Pure (Current_Scope));
3188 Set_Scope (T, Current_Scope);
3189 Set_Ekind (T, E_Record_Type_With_Private);
3190 Init_Size_Align (T);
3191
3192 Set_Etype (T, Parent_Base);
3193 Set_Has_Task (T, Has_Task (Parent_Base));
3194
3195 Set_Convention (T, Convention (Parent_Type));
3196 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3197 Set_Is_First_Subtype (T);
3198 Make_Class_Wide_Type (T);
3199
3200 if Unknown_Discriminants_Present (N) then
3201 Set_Discriminant_Constraint (T, No_Elist);
3202 end if;
3203
3204 Build_Derived_Record_Type (N, Parent_Type, T);
3205
3206 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3207 -- synchronized formal derived type.
3208
3209 if Ada_Version >= Ada_05
3210 and then Synchronized_Present (N)
3211 then
3212 Set_Is_Limited_Record (T);
3213
3214 -- Formal derived type case
3215
3216 if Is_Generic_Type (T) then
3217
3218 -- The parent must be a tagged limited type or a synchronized
3219 -- interface.
3220
3221 if (not Is_Tagged_Type (Parent_Type)
3222 or else not Is_Limited_Type (Parent_Type))
3223 and then
3224 (not Is_Interface (Parent_Type)
3225 or else not Is_Synchronized_Interface (Parent_Type))
3226 then
3227 Error_Msg_NE ("parent type of & must be tagged limited " &
3228 "or synchronized", N, T);
3229 end if;
3230
3231 -- The progenitors (if any) must be limited or synchronized
3232 -- interfaces.
3233
3234 if Present (Interfaces (T)) then
3235 declare
3236 Iface : Entity_Id;
3237 Iface_Elmt : Elmt_Id;
3238
3239 begin
3240 Iface_Elmt := First_Elmt (Interfaces (T));
3241 while Present (Iface_Elmt) loop
3242 Iface := Node (Iface_Elmt);
3243
3244 if not Is_Limited_Interface (Iface)
3245 and then not Is_Synchronized_Interface (Iface)
3246 then
3247 Error_Msg_NE ("progenitor & must be limited " &
3248 "or synchronized", N, Iface);
3249 end if;
3250
3251 Next_Elmt (Iface_Elmt);
3252 end loop;
3253 end;
3254 end if;
3255
3256 -- Regular derived extension, the parent must be a limited or
3257 -- synchronized interface.
3258
3259 else
3260 if not Is_Interface (Parent_Type)
3261 or else (not Is_Limited_Interface (Parent_Type)
3262 and then
3263 not Is_Synchronized_Interface (Parent_Type))
3264 then
3265 Error_Msg_NE
3266 ("parent type of & must be limited interface", N, T);
3267 end if;
3268 end if;
3269
3270 elsif Limited_Present (N) then
3271 Set_Is_Limited_Record (T);
3272
3273 if not Is_Limited_Type (Parent_Type)
3274 and then
3275 (not Is_Interface (Parent_Type)
3276 or else not Is_Limited_Interface (Parent_Type))
3277 then
3278 Error_Msg_NE ("parent type& of limited extension must be limited",
3279 N, Parent_Type);
3280 end if;
3281 end if;
3282 end Analyze_Private_Extension_Declaration;
3283
3284 ---------------------------------
3285 -- Analyze_Subtype_Declaration --
3286 ---------------------------------
3287
3288 procedure Analyze_Subtype_Declaration
3289 (N : Node_Id;
3290 Skip : Boolean := False)
3291 is
3292 Id : constant Entity_Id := Defining_Identifier (N);
3293 T : Entity_Id;
3294 R_Checks : Check_Result;
3295
3296 begin
3297 Generate_Definition (Id);
3298 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3299 Init_Size_Align (Id);
3300
3301 -- The following guard condition on Enter_Name is to handle cases where
3302 -- the defining identifier has already been entered into the scope but
3303 -- the declaration as a whole needs to be analyzed.
3304
3305 -- This case in particular happens for derived enumeration types. The
3306 -- derived enumeration type is processed as an inserted enumeration type
3307 -- declaration followed by a rewritten subtype declaration. The defining
3308 -- identifier, however, is entered into the name scope very early in the
3309 -- processing of the original type declaration and therefore needs to be
3310 -- avoided here, when the created subtype declaration is analyzed. (See
3311 -- Build_Derived_Types)
3312
3313 -- This also happens when the full view of a private type is derived
3314 -- type with constraints. In this case the entity has been introduced
3315 -- in the private declaration.
3316
3317 if Skip
3318 or else (Present (Etype (Id))
3319 and then (Is_Private_Type (Etype (Id))
3320 or else Is_Task_Type (Etype (Id))
3321 or else Is_Rewrite_Substitution (N)))
3322 then
3323 null;
3324
3325 else
3326 Enter_Name (Id);
3327 end if;
3328
3329 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3330
3331 -- Inherit common attributes
3332
3333 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
3334 Set_Is_Volatile (Id, Is_Volatile (T));
3335 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3336 Set_Is_Atomic (Id, Is_Atomic (T));
3337 Set_Is_Ada_2005_Only (Id, Is_Ada_2005_Only (T));
3338 Set_Convention (Id, Convention (T));
3339
3340 -- In the case where there is no constraint given in the subtype
3341 -- indication, Process_Subtype just returns the Subtype_Mark, so its
3342 -- semantic attributes must be established here.
3343
3344 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
3345 Set_Etype (Id, Base_Type (T));
3346
3347 case Ekind (T) is
3348 when Array_Kind =>
3349 Set_Ekind (Id, E_Array_Subtype);
3350 Copy_Array_Subtype_Attributes (Id, T);
3351
3352 when Decimal_Fixed_Point_Kind =>
3353 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
3354 Set_Digits_Value (Id, Digits_Value (T));
3355 Set_Delta_Value (Id, Delta_Value (T));
3356 Set_Scale_Value (Id, Scale_Value (T));
3357 Set_Small_Value (Id, Small_Value (T));
3358 Set_Scalar_Range (Id, Scalar_Range (T));
3359 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
3360 Set_Is_Constrained (Id, Is_Constrained (T));
3361 Set_RM_Size (Id, RM_Size (T));
3362
3363 when Enumeration_Kind =>
3364 Set_Ekind (Id, E_Enumeration_Subtype);
3365 Set_First_Literal (Id, First_Literal (Base_Type (T)));
3366 Set_Scalar_Range (Id, Scalar_Range (T));
3367 Set_Is_Character_Type (Id, Is_Character_Type (T));
3368 Set_Is_Constrained (Id, Is_Constrained (T));
3369 Set_RM_Size (Id, RM_Size (T));
3370
3371 when Ordinary_Fixed_Point_Kind =>
3372 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
3373 Set_Scalar_Range (Id, Scalar_Range (T));
3374 Set_Small_Value (Id, Small_Value (T));
3375 Set_Delta_Value (Id, Delta_Value (T));
3376 Set_Is_Constrained (Id, Is_Constrained (T));
3377 Set_RM_Size (Id, RM_Size (T));
3378
3379 when Float_Kind =>
3380 Set_Ekind (Id, E_Floating_Point_Subtype);
3381 Set_Scalar_Range (Id, Scalar_Range (T));
3382 Set_Digits_Value (Id, Digits_Value (T));
3383 Set_Is_Constrained (Id, Is_Constrained (T));
3384
3385 when Signed_Integer_Kind =>
3386 Set_Ekind (Id, E_Signed_Integer_Subtype);
3387 Set_Scalar_Range (Id, Scalar_Range (T));
3388 Set_Is_Constrained (Id, Is_Constrained (T));
3389 Set_RM_Size (Id, RM_Size (T));
3390
3391 when Modular_Integer_Kind =>
3392 Set_Ekind (Id, E_Modular_Integer_Subtype);
3393 Set_Scalar_Range (Id, Scalar_Range (T));
3394 Set_Is_Constrained (Id, Is_Constrained (T));
3395 Set_RM_Size (Id, RM_Size (T));
3396
3397 when Class_Wide_Kind =>
3398 Set_Ekind (Id, E_Class_Wide_Subtype);
3399 Set_First_Entity (Id, First_Entity (T));
3400 Set_Last_Entity (Id, Last_Entity (T));
3401 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3402 Set_Cloned_Subtype (Id, T);
3403 Set_Is_Tagged_Type (Id, True);
3404 Set_Has_Unknown_Discriminants
3405 (Id, True);
3406
3407 if Ekind (T) = E_Class_Wide_Subtype then
3408 Set_Equivalent_Type (Id, Equivalent_Type (T));
3409 end if;
3410
3411 when E_Record_Type | E_Record_Subtype =>
3412 Set_Ekind (Id, E_Record_Subtype);
3413
3414 if Ekind (T) = E_Record_Subtype
3415 and then Present (Cloned_Subtype (T))
3416 then
3417 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
3418 else
3419 Set_Cloned_Subtype (Id, T);
3420 end if;
3421
3422 Set_First_Entity (Id, First_Entity (T));
3423 Set_Last_Entity (Id, Last_Entity (T));
3424 Set_Has_Discriminants (Id, Has_Discriminants (T));
3425 Set_Is_Constrained (Id, Is_Constrained (T));
3426 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
3427 Set_Has_Unknown_Discriminants
3428 (Id, Has_Unknown_Discriminants (T));
3429
3430 if Has_Discriminants (T) then
3431 Set_Discriminant_Constraint
3432 (Id, Discriminant_Constraint (T));
3433 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3434
3435 elsif Has_Unknown_Discriminants (Id) then
3436 Set_Discriminant_Constraint (Id, No_Elist);
3437 end if;
3438
3439 if Is_Tagged_Type (T) then
3440 Set_Is_Tagged_Type (Id);
3441 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
3442 Set_Primitive_Operations
3443 (Id, Primitive_Operations (T));
3444 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3445
3446 if Is_Interface (T) then
3447 Set_Is_Interface (Id);
3448 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
3449 end if;
3450 end if;
3451
3452 when Private_Kind =>
3453 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
3454 Set_Has_Discriminants (Id, Has_Discriminants (T));
3455 Set_Is_Constrained (Id, Is_Constrained (T));
3456 Set_First_Entity (Id, First_Entity (T));
3457 Set_Last_Entity (Id, Last_Entity (T));
3458 Set_Private_Dependents (Id, New_Elmt_List);
3459 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
3460 Set_Has_Unknown_Discriminants
3461 (Id, Has_Unknown_Discriminants (T));
3462 Set_Known_To_Have_Preelab_Init
3463 (Id, Known_To_Have_Preelab_Init (T));
3464
3465 if Is_Tagged_Type (T) then
3466 Set_Is_Tagged_Type (Id);
3467 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
3468 Set_Primitive_Operations (Id, Primitive_Operations (T));
3469 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3470 end if;
3471
3472 -- In general the attributes of the subtype of a private type
3473 -- are the attributes of the partial view of parent. However,
3474 -- the full view may be a discriminated type, and the subtype
3475 -- must share the discriminant constraint to generate correct
3476 -- calls to initialization procedures.
3477
3478 if Has_Discriminants (T) then
3479 Set_Discriminant_Constraint
3480 (Id, Discriminant_Constraint (T));
3481 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3482
3483 elsif Present (Full_View (T))
3484 and then Has_Discriminants (Full_View (T))
3485 then
3486 Set_Discriminant_Constraint
3487 (Id, Discriminant_Constraint (Full_View (T)));
3488 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3489
3490 -- This would seem semantically correct, but apparently
3491 -- confuses the back-end. To be explained and checked with
3492 -- current version ???
3493
3494 -- Set_Has_Discriminants (Id);
3495 end if;
3496
3497 Prepare_Private_Subtype_Completion (Id, N);
3498
3499 when Access_Kind =>
3500 Set_Ekind (Id, E_Access_Subtype);
3501 Set_Is_Constrained (Id, Is_Constrained (T));
3502 Set_Is_Access_Constant
3503 (Id, Is_Access_Constant (T));
3504 Set_Directly_Designated_Type
3505 (Id, Designated_Type (T));
3506 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
3507
3508 -- A Pure library_item must not contain the declaration of a
3509 -- named access type, except within a subprogram, generic
3510 -- subprogram, task unit, or protected unit (RM 10.2.1(16)).
3511
3512 if Comes_From_Source (Id)
3513 and then In_Pure_Unit
3514 and then not In_Subprogram_Task_Protected_Unit
3515 then
3516 Error_Msg_N
3517 ("named access types not allowed in pure unit", N);
3518 end if;
3519
3520 when Concurrent_Kind =>
3521 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
3522 Set_Corresponding_Record_Type (Id,
3523 Corresponding_Record_Type (T));
3524 Set_First_Entity (Id, First_Entity (T));
3525 Set_First_Private_Entity (Id, First_Private_Entity (T));
3526 Set_Has_Discriminants (Id, Has_Discriminants (T));
3527 Set_Is_Constrained (Id, Is_Constrained (T));
3528 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
3529 Set_Last_Entity (Id, Last_Entity (T));
3530
3531 if Has_Discriminants (T) then
3532 Set_Discriminant_Constraint (Id,
3533 Discriminant_Constraint (T));
3534 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3535 end if;
3536
3537 when E_Incomplete_Type =>
3538 if Ada_Version >= Ada_05 then
3539 Set_Ekind (Id, E_Incomplete_Subtype);
3540
3541 -- Ada 2005 (AI-412): Decorate an incomplete subtype
3542 -- of an incomplete type visible through a limited
3543 -- with clause.
3544
3545 if From_With_Type (T)
3546 and then Present (Non_Limited_View (T))
3547 then
3548 Set_From_With_Type (Id);
3549 Set_Non_Limited_View (Id, Non_Limited_View (T));
3550
3551 -- Ada 2005 (AI-412): Add the regular incomplete subtype
3552 -- to the private dependents of the original incomplete
3553 -- type for future transformation.
3554
3555 else
3556 Append_Elmt (Id, Private_Dependents (T));
3557 end if;
3558
3559 -- If the subtype name denotes an incomplete type an error
3560 -- was already reported by Process_Subtype.
3561
3562 else
3563 Set_Etype (Id, Any_Type);
3564 end if;
3565
3566 when others =>
3567 raise Program_Error;
3568 end case;
3569 end if;
3570
3571 if Etype (Id) = Any_Type then
3572 return;
3573 end if;
3574
3575 -- Some common processing on all types
3576
3577 Set_Size_Info (Id, T);
3578 Set_First_Rep_Item (Id, First_Rep_Item (T));
3579
3580 T := Etype (Id);
3581
3582 Set_Is_Immediately_Visible (Id, True);
3583 Set_Depends_On_Private (Id, Has_Private_Component (T));
3584 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
3585
3586 if Is_Interface (T) then
3587 Set_Is_Interface (Id);
3588 end if;
3589
3590 if Present (Generic_Parent_Type (N))
3591 and then
3592 (Nkind
3593 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
3594 or else Nkind
3595 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
3596 /= N_Formal_Private_Type_Definition)
3597 then
3598 if Is_Tagged_Type (Id) then
3599
3600 -- If this is a generic actual subtype for a synchronized type,
3601 -- the primitive operations are those of the corresponding record
3602 -- for which there is a separate subtype declaration.
3603
3604 if Is_Concurrent_Type (Id) then
3605 null;
3606 elsif Is_Class_Wide_Type (Id) then
3607 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
3608 else
3609 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
3610 end if;
3611
3612 elsif Scope (Etype (Id)) /= Standard_Standard then
3613 Derive_Subprograms (Generic_Parent_Type (N), Id);
3614 end if;
3615 end if;
3616
3617 if Is_Private_Type (T)
3618 and then Present (Full_View (T))
3619 then
3620 Conditional_Delay (Id, Full_View (T));
3621
3622 -- The subtypes of components or subcomponents of protected types
3623 -- do not need freeze nodes, which would otherwise appear in the
3624 -- wrong scope (before the freeze node for the protected type). The
3625 -- proper subtypes are those of the subcomponents of the corresponding
3626 -- record.
3627
3628 elsif Ekind (Scope (Id)) /= E_Protected_Type
3629 and then Present (Scope (Scope (Id))) -- error defense!
3630 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
3631 then
3632 Conditional_Delay (Id, T);
3633 end if;
3634
3635 -- Check that constraint_error is raised for a scalar subtype
3636 -- indication when the lower or upper bound of a non-null range
3637 -- lies outside the range of the type mark.
3638
3639 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
3640 if Is_Scalar_Type (Etype (Id))
3641 and then Scalar_Range (Id) /=
3642 Scalar_Range (Etype (Subtype_Mark
3643 (Subtype_Indication (N))))
3644 then
3645 Apply_Range_Check
3646 (Scalar_Range (Id),
3647 Etype (Subtype_Mark (Subtype_Indication (N))));
3648
3649 elsif Is_Array_Type (Etype (Id))
3650 and then Present (First_Index (Id))
3651 then
3652 -- This really should be a subprogram that finds the indications
3653 -- to check???
3654
3655 if ((Nkind (First_Index (Id)) = N_Identifier
3656 and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
3657 or else Nkind (First_Index (Id)) = N_Subtype_Indication)
3658 and then
3659 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
3660 then
3661 declare
3662 Target_Typ : constant Entity_Id :=
3663 Etype
3664 (First_Index (Etype
3665 (Subtype_Mark (Subtype_Indication (N)))));
3666 begin
3667 R_Checks :=
3668 Get_Range_Checks
3669 (Scalar_Range (Etype (First_Index (Id))),
3670 Target_Typ,
3671 Etype (First_Index (Id)),
3672 Defining_Identifier (N));
3673
3674 Insert_Range_Checks
3675 (R_Checks,
3676 N,
3677 Target_Typ,
3678 Sloc (Defining_Identifier (N)));
3679 end;
3680 end if;
3681 end if;
3682 end if;
3683
3684 Set_Optimize_Alignment_Flags (Id);
3685 Check_Eliminated (Id);
3686 end Analyze_Subtype_Declaration;
3687
3688 --------------------------------
3689 -- Analyze_Subtype_Indication --
3690 --------------------------------
3691
3692 procedure Analyze_Subtype_Indication (N : Node_Id) is
3693 T : constant Entity_Id := Subtype_Mark (N);
3694 R : constant Node_Id := Range_Expression (Constraint (N));
3695
3696 begin
3697 Analyze (T);
3698
3699 if R /= Error then
3700 Analyze (R);
3701 Set_Etype (N, Etype (R));
3702 Resolve (R, Entity (T));
3703 else
3704 Set_Error_Posted (R);
3705 Set_Error_Posted (T);
3706 end if;
3707 end Analyze_Subtype_Indication;
3708
3709 ------------------------------
3710 -- Analyze_Type_Declaration --
3711 ------------------------------
3712
3713 procedure Analyze_Type_Declaration (N : Node_Id) is
3714 Def : constant Node_Id := Type_Definition (N);
3715 Def_Id : constant Entity_Id := Defining_Identifier (N);
3716 T : Entity_Id;
3717 Prev : Entity_Id;
3718
3719 Is_Remote : constant Boolean :=
3720 (Is_Remote_Types (Current_Scope)
3721 or else Is_Remote_Call_Interface (Current_Scope))
3722 and then not (In_Private_Part (Current_Scope)
3723 or else In_Package_Body (Current_Scope));
3724
3725 procedure Check_Ops_From_Incomplete_Type;
3726 -- If there is a tagged incomplete partial view of the type, transfer
3727 -- its operations to the full view, and indicate that the type of the
3728 -- controlling parameter (s) is this full view.
3729
3730 ------------------------------------
3731 -- Check_Ops_From_Incomplete_Type --
3732 ------------------------------------
3733
3734 procedure Check_Ops_From_Incomplete_Type is
3735 Elmt : Elmt_Id;
3736 Formal : Entity_Id;
3737 Op : Entity_Id;
3738
3739 begin
3740 if Prev /= T
3741 and then Ekind (Prev) = E_Incomplete_Type
3742 and then Is_Tagged_Type (Prev)
3743 and then Is_Tagged_Type (T)
3744 then
3745 Elmt := First_Elmt (Primitive_Operations (Prev));
3746 while Present (Elmt) loop
3747 Op := Node (Elmt);
3748 Prepend_Elmt (Op, Primitive_Operations (T));
3749
3750 Formal := First_Formal (Op);
3751 while Present (Formal) loop
3752 if Etype (Formal) = Prev then
3753 Set_Etype (Formal, T);
3754 end if;
3755
3756 Next_Formal (Formal);
3757 end loop;
3758
3759 if Etype (Op) = Prev then
3760 Set_Etype (Op, T);
3761 end if;
3762
3763 Next_Elmt (Elmt);
3764 end loop;
3765 end if;
3766 end Check_Ops_From_Incomplete_Type;
3767
3768 -- Start of processing for Analyze_Type_Declaration
3769
3770 begin
3771 Prev := Find_Type_Name (N);
3772
3773 -- The full view, if present, now points to the current type
3774
3775 -- Ada 2005 (AI-50217): If the type was previously decorated when
3776 -- imported through a LIMITED WITH clause, it appears as incomplete
3777 -- but has no full view.
3778 -- If the incomplete view is tagged, a class_wide type has been
3779 -- created already. Use it for the full view as well, to prevent
3780 -- multiple incompatible class-wide types that may be created for
3781 -- self-referential anonymous access components.
3782
3783 if Ekind (Prev) = E_Incomplete_Type
3784 and then Present (Full_View (Prev))
3785 then
3786 T := Full_View (Prev);
3787
3788 if Is_Tagged_Type (Prev)
3789 and then Present (Class_Wide_Type (Prev))
3790 then
3791 Set_Ekind (T, Ekind (Prev)); -- will be reset later
3792 Set_Class_Wide_Type (T, Class_Wide_Type (Prev));
3793 Set_Etype (Class_Wide_Type (T), T);
3794 end if;
3795
3796 else
3797 T := Prev;
3798 end if;
3799
3800 Set_Is_Pure (T, Is_Pure (Current_Scope));
3801
3802 -- We set the flag Is_First_Subtype here. It is needed to set the
3803 -- corresponding flag for the Implicit class-wide-type created
3804 -- during tagged types processing.
3805
3806 Set_Is_First_Subtype (T, True);
3807
3808 -- Only composite types other than array types are allowed to have
3809 -- discriminants.
3810
3811 case Nkind (Def) is
3812
3813 -- For derived types, the rule will be checked once we've figured
3814 -- out the parent type.
3815
3816 when N_Derived_Type_Definition =>
3817 null;
3818
3819 -- For record types, discriminants are allowed
3820
3821 when N_Record_Definition =>
3822 null;
3823
3824 when others =>
3825 if Present (Discriminant_Specifications (N)) then
3826 Error_Msg_N
3827 ("elementary or array type cannot have discriminants",
3828 Defining_Identifier
3829 (First (Discriminant_Specifications (N))));
3830 end if;
3831 end case;
3832
3833 -- Elaborate the type definition according to kind, and generate
3834 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3835 -- already done (this happens during the reanalysis that follows a call
3836 -- to the high level optimizer).
3837
3838 if not Analyzed (T) then
3839 Set_Analyzed (T);
3840
3841 case Nkind (Def) is
3842
3843 when N_Access_To_Subprogram_Definition =>
3844 Access_Subprogram_Declaration (T, Def);
3845
3846 -- If this is a remote access to subprogram, we must create the
3847 -- equivalent fat pointer type, and related subprograms.
3848
3849 if Is_Remote then
3850 Process_Remote_AST_Declaration (N);
3851 end if;
3852
3853 -- Validate categorization rule against access type declaration
3854 -- usually a violation in Pure unit, Shared_Passive unit.
3855
3856 Validate_Access_Type_Declaration (T, N);
3857
3858 when N_Access_To_Object_Definition =>
3859 Access_Type_Declaration (T, Def);
3860
3861 -- Validate categorization rule against access type declaration
3862 -- usually a violation in Pure unit, Shared_Passive unit.
3863
3864 Validate_Access_Type_Declaration (T, N);
3865
3866 -- If we are in a Remote_Call_Interface package and define a
3867 -- RACW, then calling stubs and specific stream attributes
3868 -- must be added.
3869
3870 if Is_Remote
3871 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3872 then
3873 Add_RACW_Features (Def_Id);
3874 end if;
3875
3876 -- Set no strict aliasing flag if config pragma seen
3877
3878 if Opt.No_Strict_Aliasing then
3879 Set_No_Strict_Aliasing (Base_Type (Def_Id));
3880 end if;
3881
3882 when N_Array_Type_Definition =>
3883 Array_Type_Declaration (T, Def);
3884
3885 when N_Derived_Type_Definition =>
3886 Derived_Type_Declaration (T, N, T /= Def_Id);
3887
3888 when N_Enumeration_Type_Definition =>
3889 Enumeration_Type_Declaration (T, Def);
3890
3891 when N_Floating_Point_Definition =>
3892 Floating_Point_Type_Declaration (T, Def);
3893
3894 when N_Decimal_Fixed_Point_Definition =>
3895 Decimal_Fixed_Point_Type_Declaration (T, Def);
3896
3897 when N_Ordinary_Fixed_Point_Definition =>
3898 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3899
3900 when N_Signed_Integer_Type_Definition =>
3901 Signed_Integer_Type_Declaration (T, Def);
3902
3903 when N_Modular_Type_Definition =>
3904 Modular_Type_Declaration (T, Def);
3905
3906 when N_Record_Definition =>
3907 Record_Type_Declaration (T, N, Prev);
3908
3909 when others =>
3910 raise Program_Error;
3911
3912 end case;
3913 end if;
3914
3915 if Etype (T) = Any_Type then
3916 return;
3917 end if;
3918
3919 -- Some common processing for all types
3920
3921 Set_Depends_On_Private (T, Has_Private_Component (T));
3922 Check_Ops_From_Incomplete_Type;
3923
3924 -- Both the declared entity, and its anonymous base type if one
3925 -- was created, need freeze nodes allocated.
3926
3927 declare
3928 B : constant Entity_Id := Base_Type (T);
3929
3930 begin
3931 -- In the case where the base type differs from the first subtype, we
3932 -- pre-allocate a freeze node, and set the proper link to the first
3933 -- subtype. Freeze_Entity will use this preallocated freeze node when
3934 -- it freezes the entity.
3935
3936 if B /= T then
3937 Ensure_Freeze_Node (B);
3938 Set_First_Subtype_Link (Freeze_Node (B), T);
3939 end if;
3940
3941 if not From_With_Type (T) then
3942 Set_Has_Delayed_Freeze (T);
3943 end if;
3944 end;
3945
3946 -- Case of T is the full declaration of some private type which has
3947 -- been swapped in Defining_Identifier (N).
3948
3949 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3950 Process_Full_View (N, T, Def_Id);
3951
3952 -- Record the reference. The form of this is a little strange, since
3953 -- the full declaration has been swapped in. So the first parameter
3954 -- here represents the entity to which a reference is made which is
3955 -- the "real" entity, i.e. the one swapped in, and the second
3956 -- parameter provides the reference location.
3957
3958 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3959 -- since we don't want a complaint about the full type being an
3960 -- unwanted reference to the private type
3961
3962 declare
3963 B : constant Boolean := Has_Pragma_Unreferenced (T);
3964 begin
3965 Set_Has_Pragma_Unreferenced (T, False);
3966 Generate_Reference (T, T, 'c');
3967 Set_Has_Pragma_Unreferenced (T, B);
3968 end;
3969
3970 Set_Completion_Referenced (Def_Id);
3971
3972 -- For completion of incomplete type, process incomplete dependents
3973 -- and always mark the full type as referenced (it is the incomplete
3974 -- type that we get for any real reference).
3975
3976 elsif Ekind (Prev) = E_Incomplete_Type then
3977 Process_Incomplete_Dependents (N, T, Prev);
3978 Generate_Reference (Prev, Def_Id, 'c');
3979 Set_Completion_Referenced (Def_Id);
3980
3981 -- If not private type or incomplete type completion, this is a real
3982 -- definition of a new entity, so record it.
3983
3984 else
3985 Generate_Definition (Def_Id);
3986 end if;
3987
3988 if Chars (Scope (Def_Id)) = Name_System
3989 and then Chars (Def_Id) = Name_Address
3990 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
3991 then
3992 Set_Is_Descendent_Of_Address (Def_Id);
3993 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
3994 Set_Is_Descendent_Of_Address (Prev);
3995 end if;
3996
3997 Set_Optimize_Alignment_Flags (Def_Id);
3998 Check_Eliminated (Def_Id);
3999 end Analyze_Type_Declaration;
4000
4001 --------------------------
4002 -- Analyze_Variant_Part --
4003 --------------------------
4004
4005 procedure Analyze_Variant_Part (N : Node_Id) is
4006
4007 procedure Non_Static_Choice_Error (Choice : Node_Id);
4008 -- Error routine invoked by the generic instantiation below when the
4009 -- variant part has a non static choice.
4010
4011 procedure Process_Declarations (Variant : Node_Id);
4012 -- Analyzes all the declarations associated with a Variant. Needed by
4013 -- the generic instantiation below.
4014
4015 package Variant_Choices_Processing is new
4016 Generic_Choices_Processing
4017 (Get_Alternatives => Variants,
4018 Get_Choices => Discrete_Choices,
4019 Process_Empty_Choice => No_OP,
4020 Process_Non_Static_Choice => Non_Static_Choice_Error,
4021 Process_Associated_Node => Process_Declarations);
4022 use Variant_Choices_Processing;
4023 -- Instantiation of the generic choice processing package
4024
4025 -----------------------------
4026 -- Non_Static_Choice_Error --
4027 -----------------------------
4028
4029 procedure Non_Static_Choice_Error (Choice : Node_Id) is
4030 begin
4031 Flag_Non_Static_Expr
4032 ("choice given in variant part is not static!", Choice);
4033 end Non_Static_Choice_Error;
4034
4035 --------------------------
4036 -- Process_Declarations --
4037 --------------------------
4038
4039 procedure Process_Declarations (Variant : Node_Id) is
4040 begin
4041 if not Null_Present (Component_List (Variant)) then
4042 Analyze_Declarations (Component_Items (Component_List (Variant)));
4043
4044 if Present (Variant_Part (Component_List (Variant))) then
4045 Analyze (Variant_Part (Component_List (Variant)));
4046 end if;
4047 end if;
4048 end Process_Declarations;
4049
4050 -- Local Variables
4051
4052 Discr_Name : Node_Id;
4053 Discr_Type : Entity_Id;
4054
4055 Case_Table : Choice_Table_Type (1 .. Number_Of_Choices (N));
4056 Last_Choice : Nat;
4057 Dont_Care : Boolean;
4058 Others_Present : Boolean := False;
4059
4060 pragma Warnings (Off, Case_Table);
4061 pragma Warnings (Off, Last_Choice);
4062 pragma Warnings (Off, Dont_Care);
4063 pragma Warnings (Off, Others_Present);
4064 -- We don't care about the assigned values of any of these
4065
4066 -- Start of processing for Analyze_Variant_Part
4067
4068 begin
4069 Discr_Name := Name (N);
4070 Analyze (Discr_Name);
4071
4072 -- If Discr_Name bad, get out (prevent cascaded errors)
4073
4074 if Etype (Discr_Name) = Any_Type then
4075 return;
4076 end if;
4077
4078 -- Check invalid discriminant in variant part
4079
4080 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4081 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4082 end if;
4083
4084 Discr_Type := Etype (Entity (Discr_Name));
4085
4086 if not Is_Discrete_Type (Discr_Type) then
4087 Error_Msg_N
4088 ("discriminant in a variant part must be of a discrete type",
4089 Name (N));
4090 return;
4091 end if;
4092
4093 -- Call the instantiated Analyze_Choices which does the rest of the work
4094
4095 Analyze_Choices
4096 (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
4097 end Analyze_Variant_Part;
4098
4099 ----------------------------
4100 -- Array_Type_Declaration --
4101 ----------------------------
4102
4103 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4104 Component_Def : constant Node_Id := Component_Definition (Def);
4105 Element_Type : Entity_Id;
4106 Implicit_Base : Entity_Id;
4107 Index : Node_Id;
4108 Related_Id : Entity_Id := Empty;
4109 Nb_Index : Nat;
4110 P : constant Node_Id := Parent (Def);
4111 Priv : Entity_Id;
4112
4113 begin
4114 if Nkind (Def) = N_Constrained_Array_Definition then
4115 Index := First (Discrete_Subtype_Definitions (Def));
4116 else
4117 Index := First (Subtype_Marks (Def));
4118 end if;
4119
4120 -- Find proper names for the implicit types which may be public. In case
4121 -- of anonymous arrays we use the name of the first object of that type
4122 -- as prefix.
4123
4124 if No (T) then
4125 Related_Id := Defining_Identifier (P);
4126 else
4127 Related_Id := T;
4128 end if;
4129
4130 Nb_Index := 1;
4131 while Present (Index) loop
4132 Analyze (Index);
4133
4134 -- Add a subtype declaration for each index of private array type
4135 -- declaration whose etype is also private. For example:
4136
4137 -- package Pkg is
4138 -- type Index is private;
4139 -- private
4140 -- type Table is array (Index) of ...
4141 -- end;
4142
4143 -- This is currently required by the expander for the internally
4144 -- generated equality subprogram of records with variant parts in
4145 -- which the etype of some component is such private type.
4146
4147 if Ekind (Current_Scope) = E_Package
4148 and then In_Private_Part (Current_Scope)
4149 and then Has_Private_Declaration (Etype (Index))
4150 then
4151 declare
4152 Loc : constant Source_Ptr := Sloc (Def);
4153 New_E : Entity_Id;
4154 Decl : Entity_Id;
4155
4156 begin
4157 New_E :=
4158 Make_Defining_Identifier (Loc,
4159 Chars => New_Internal_Name ('T'));
4160 Set_Is_Internal (New_E);
4161
4162 Decl :=
4163 Make_Subtype_Declaration (Loc,
4164 Defining_Identifier => New_E,
4165 Subtype_Indication =>
4166 New_Occurrence_Of (Etype (Index), Loc));
4167
4168 Insert_Before (Parent (Def), Decl);
4169 Analyze (Decl);
4170 Set_Etype (Index, New_E);
4171
4172 -- If the index is a range the Entity attribute is not
4173 -- available. Example:
4174
4175 -- package Pkg is
4176 -- type T is private;
4177 -- private
4178 -- type T is new Natural;
4179 -- Table : array (T(1) .. T(10)) of Boolean;
4180 -- end Pkg;
4181
4182 if Nkind (Index) /= N_Range then
4183 Set_Entity (Index, New_E);
4184 end if;
4185 end;
4186 end if;
4187
4188 Make_Index (Index, P, Related_Id, Nb_Index);
4189 Next_Index (Index);
4190 Nb_Index := Nb_Index + 1;
4191 end loop;
4192
4193 -- Process subtype indication if one is present
4194
4195 if Present (Subtype_Indication (Component_Def)) then
4196 Element_Type :=
4197 Process_Subtype
4198 (Subtype_Indication (Component_Def), P, Related_Id, 'C');
4199
4200 -- Ada 2005 (AI-230): Access Definition case
4201
4202 else pragma Assert (Present (Access_Definition (Component_Def)));
4203
4204 -- Indicate that the anonymous access type is created by the
4205 -- array type declaration.
4206
4207 Element_Type := Access_Definition
4208 (Related_Nod => P,
4209 N => Access_Definition (Component_Def));
4210 Set_Is_Local_Anonymous_Access (Element_Type);
4211
4212 -- Propagate the parent. This field is needed if we have to generate
4213 -- the master_id associated with an anonymous access to task type
4214 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4215
4216 Set_Parent (Element_Type, Parent (T));
4217
4218 -- Ada 2005 (AI-230): In case of components that are anonymous access
4219 -- types the level of accessibility depends on the enclosing type
4220 -- declaration
4221
4222 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4223
4224 -- Ada 2005 (AI-254)
4225
4226 declare
4227 CD : constant Node_Id :=
4228 Access_To_Subprogram_Definition
4229 (Access_Definition (Component_Def));
4230 begin
4231 if Present (CD) and then Protected_Present (CD) then
4232 Element_Type :=
4233 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4234 end if;
4235 end;
4236 end if;
4237
4238 -- Constrained array case
4239
4240 if No (T) then
4241 T := Create_Itype (E_Void, P, Related_Id, 'T');
4242 end if;
4243
4244 if Nkind (Def) = N_Constrained_Array_Definition then
4245
4246 -- Establish Implicit_Base as unconstrained base type
4247
4248 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4249
4250 Set_Etype (Implicit_Base, Implicit_Base);
4251 Set_Scope (Implicit_Base, Current_Scope);
4252 Set_Has_Delayed_Freeze (Implicit_Base);
4253
4254 -- The constrained array type is a subtype of the unconstrained one
4255
4256 Set_Ekind (T, E_Array_Subtype);
4257 Init_Size_Align (T);
4258 Set_Etype (T, Implicit_Base);
4259 Set_Scope (T, Current_Scope);
4260 Set_Is_Constrained (T, True);
4261 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4262 Set_Has_Delayed_Freeze (T);
4263
4264 -- Complete setup of implicit base type
4265
4266 Set_First_Index (Implicit_Base, First_Index (T));
4267 Set_Component_Type (Implicit_Base, Element_Type);
4268 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4269 Set_Component_Size (Implicit_Base, Uint_0);
4270 Set_Packed_Array_Type (Implicit_Base, Empty);
4271 Set_Has_Controlled_Component
4272 (Implicit_Base, Has_Controlled_Component
4273 (Element_Type)
4274 or else Is_Controlled
4275 (Element_Type));
4276 Set_Finalize_Storage_Only
4277 (Implicit_Base, Finalize_Storage_Only
4278 (Element_Type));
4279
4280 -- Unconstrained array case
4281
4282 else
4283 Set_Ekind (T, E_Array_Type);
4284 Init_Size_Align (T);
4285 Set_Etype (T, T);
4286 Set_Scope (T, Current_Scope);
4287 Set_Component_Size (T, Uint_0);
4288 Set_Is_Constrained (T, False);
4289 Set_First_Index (T, First (Subtype_Marks (Def)));
4290 Set_Has_Delayed_Freeze (T, True);
4291 Set_Has_Task (T, Has_Task (Element_Type));
4292 Set_Has_Controlled_Component (T, Has_Controlled_Component
4293 (Element_Type)
4294 or else
4295 Is_Controlled (Element_Type));
4296 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4297 (Element_Type));
4298 end if;
4299
4300 -- Common attributes for both cases
4301
4302 Set_Component_Type (Base_Type (T), Element_Type);
4303 Set_Packed_Array_Type (T, Empty);
4304
4305 if Aliased_Present (Component_Definition (Def)) then
4306 Set_Has_Aliased_Components (Etype (T));
4307 end if;
4308
4309 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4310 -- array type to ensure that objects of this type are initialized.
4311
4312 if Ada_Version >= Ada_05
4313 and then Can_Never_Be_Null (Element_Type)
4314 then
4315 Set_Can_Never_Be_Null (T);
4316
4317 if Null_Exclusion_Present (Component_Definition (Def))
4318
4319 -- No need to check itypes because in their case this check was
4320 -- done at their point of creation
4321
4322 and then not Is_Itype (Element_Type)
4323 then
4324 Error_Msg_N
4325 ("`NOT NULL` not allowed (null already excluded)",
4326 Subtype_Indication (Component_Definition (Def)));
4327 end if;
4328 end if;
4329
4330 Priv := Private_Component (Element_Type);
4331
4332 if Present (Priv) then
4333
4334 -- Check for circular definitions
4335
4336 if Priv = Any_Type then
4337 Set_Component_Type (Etype (T), Any_Type);
4338
4339 -- There is a gap in the visibility of operations on the composite
4340 -- type only if the component type is defined in a different scope.
4341
4342 elsif Scope (Priv) = Current_Scope then
4343 null;
4344
4345 elsif Is_Limited_Type (Priv) then
4346 Set_Is_Limited_Composite (Etype (T));
4347 Set_Is_Limited_Composite (T);
4348 else
4349 Set_Is_Private_Composite (Etype (T));
4350 Set_Is_Private_Composite (T);
4351 end if;
4352 end if;
4353
4354 -- A syntax error in the declaration itself may lead to an empty index
4355 -- list, in which case do a minimal patch.
4356
4357 if No (First_Index (T)) then
4358 Error_Msg_N ("missing index definition in array type declaration", T);
4359
4360 declare
4361 Indices : constant List_Id :=
4362 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4363 begin
4364 Set_Discrete_Subtype_Definitions (Def, Indices);
4365 Set_First_Index (T, First (Indices));
4366 return;
4367 end;
4368 end if;
4369
4370 -- Create a concatenation operator for the new type. Internal array
4371 -- types created for packed entities do not need such, they are
4372 -- compatible with the user-defined type.
4373
4374 if Number_Dimensions (T) = 1
4375 and then not Is_Packed_Array_Type (T)
4376 then
4377 New_Concatenation_Op (T);
4378 end if;
4379
4380 -- In the case of an unconstrained array the parser has already verified
4381 -- that all the indices are unconstrained but we still need to make sure
4382 -- that the element type is constrained.
4383
4384 if Is_Indefinite_Subtype (Element_Type) then
4385 Error_Msg_N
4386 ("unconstrained element type in array declaration",
4387 Subtype_Indication (Component_Def));
4388
4389 elsif Is_Abstract_Type (Element_Type) then
4390 Error_Msg_N
4391 ("the type of a component cannot be abstract",
4392 Subtype_Indication (Component_Def));
4393 end if;
4394 end Array_Type_Declaration;
4395
4396 ------------------------------------------------------
4397 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4398 ------------------------------------------------------
4399
4400 function Replace_Anonymous_Access_To_Protected_Subprogram
4401 (N : Node_Id) return Entity_Id
4402 is
4403 Loc : constant Source_Ptr := Sloc (N);
4404
4405 Curr_Scope : constant Scope_Stack_Entry :=
4406 Scope_Stack.Table (Scope_Stack.Last);
4407
4408 Anon : constant Entity_Id :=
4409 Make_Defining_Identifier (Loc,
4410 Chars => New_Internal_Name ('S'));
4411
4412 Acc : Node_Id;
4413 Comp : Node_Id;
4414 Decl : Node_Id;
4415 P : Node_Id;
4416
4417 begin
4418 Set_Is_Internal (Anon);
4419
4420 case Nkind (N) is
4421 when N_Component_Declaration |
4422 N_Unconstrained_Array_Definition |
4423 N_Constrained_Array_Definition =>
4424 Comp := Component_Definition (N);
4425 Acc := Access_Definition (Comp);
4426
4427 when N_Discriminant_Specification =>
4428 Comp := Discriminant_Type (N);
4429 Acc := Comp;
4430
4431 when N_Parameter_Specification =>
4432 Comp := Parameter_Type (N);
4433 Acc := Comp;
4434
4435 when N_Access_Function_Definition =>
4436 Comp := Result_Definition (N);
4437 Acc := Comp;
4438
4439 when N_Object_Declaration =>
4440 Comp := Object_Definition (N);
4441 Acc := Comp;
4442
4443 when others =>
4444 raise Program_Error;
4445 end case;
4446
4447 Decl := Make_Full_Type_Declaration (Loc,
4448 Defining_Identifier => Anon,
4449 Type_Definition =>
4450 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
4451
4452 Mark_Rewrite_Insertion (Decl);
4453
4454 -- Insert the new declaration in the nearest enclosing scope
4455
4456 P := Parent (N);
4457 while Present (P) and then not Has_Declarations (P) loop
4458 P := Parent (P);
4459 end loop;
4460
4461 pragma Assert (Present (P));
4462
4463 if Nkind (P) = N_Package_Specification then
4464 Prepend (Decl, Visible_Declarations (P));
4465 else
4466 Prepend (Decl, Declarations (P));
4467 end if;
4468
4469 -- Replace the anonymous type with an occurrence of the new declaration.
4470 -- In all cases the rewritten node does not have the null-exclusion
4471 -- attribute because (if present) it was already inherited by the
4472 -- anonymous entity (Anon). Thus, in case of components we do not
4473 -- inherit this attribute.
4474
4475 if Nkind (N) = N_Parameter_Specification then
4476 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4477 Set_Etype (Defining_Identifier (N), Anon);
4478 Set_Null_Exclusion_Present (N, False);
4479
4480 elsif Nkind (N) = N_Object_Declaration then
4481 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4482 Set_Etype (Defining_Identifier (N), Anon);
4483
4484 elsif Nkind (N) = N_Access_Function_Definition then
4485 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4486
4487 else
4488 Rewrite (Comp,
4489 Make_Component_Definition (Loc,
4490 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4491 end if;
4492
4493 Mark_Rewrite_Insertion (Comp);
4494
4495 -- Temporarily remove the current scope from the stack to add the new
4496 -- declarations to the enclosing scope
4497
4498 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
4499 Analyze (Decl);
4500
4501 else
4502 Scope_Stack.Decrement_Last;
4503 Analyze (Decl);
4504 Set_Is_Itype (Anon);
4505 Scope_Stack.Append (Curr_Scope);
4506 end if;
4507
4508 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
4509 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
4510 return Anon;
4511 end Replace_Anonymous_Access_To_Protected_Subprogram;
4512
4513 -------------------------------
4514 -- Build_Derived_Access_Type --
4515 -------------------------------
4516
4517 procedure Build_Derived_Access_Type
4518 (N : Node_Id;
4519 Parent_Type : Entity_Id;
4520 Derived_Type : Entity_Id)
4521 is
4522 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4523
4524 Desig_Type : Entity_Id;
4525 Discr : Entity_Id;
4526 Discr_Con_Elist : Elist_Id;
4527 Discr_Con_El : Elmt_Id;
4528 Subt : Entity_Id;
4529
4530 begin
4531 -- Set the designated type so it is available in case this is an access
4532 -- to a self-referential type, e.g. a standard list type with a next
4533 -- pointer. Will be reset after subtype is built.
4534
4535 Set_Directly_Designated_Type
4536 (Derived_Type, Designated_Type (Parent_Type));
4537
4538 Subt := Process_Subtype (S, N);
4539
4540 if Nkind (S) /= N_Subtype_Indication
4541 and then Subt /= Base_Type (Subt)
4542 then
4543 Set_Ekind (Derived_Type, E_Access_Subtype);
4544 end if;
4545
4546 if Ekind (Derived_Type) = E_Access_Subtype then
4547 declare
4548 Pbase : constant Entity_Id := Base_Type (Parent_Type);
4549 Ibase : constant Entity_Id :=
4550 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
4551 Svg_Chars : constant Name_Id := Chars (Ibase);
4552 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
4553
4554 begin
4555 Copy_Node (Pbase, Ibase);
4556
4557 Set_Chars (Ibase, Svg_Chars);
4558 Set_Next_Entity (Ibase, Svg_Next_E);
4559 Set_Sloc (Ibase, Sloc (Derived_Type));
4560 Set_Scope (Ibase, Scope (Derived_Type));
4561 Set_Freeze_Node (Ibase, Empty);
4562 Set_Is_Frozen (Ibase, False);
4563 Set_Comes_From_Source (Ibase, False);
4564 Set_Is_First_Subtype (Ibase, False);
4565
4566 Set_Etype (Ibase, Pbase);
4567 Set_Etype (Derived_Type, Ibase);
4568 end;
4569 end if;
4570
4571 Set_Directly_Designated_Type
4572 (Derived_Type, Designated_Type (Subt));
4573
4574 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
4575 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
4576 Set_Size_Info (Derived_Type, Parent_Type);
4577 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
4578 Set_Depends_On_Private (Derived_Type,
4579 Has_Private_Component (Derived_Type));
4580 Conditional_Delay (Derived_Type, Subt);
4581
4582 -- Ada 2005 (AI-231). Set the null-exclusion attribute
4583
4584 if Null_Exclusion_Present (Type_Definition (N))
4585 or else Can_Never_Be_Null (Parent_Type)
4586 then
4587 Set_Can_Never_Be_Null (Derived_Type);
4588 end if;
4589
4590 -- Note: we do not copy the Storage_Size_Variable, since we always go to
4591 -- the root type for this information.
4592
4593 -- Apply range checks to discriminants for derived record case
4594 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
4595
4596 Desig_Type := Designated_Type (Derived_Type);
4597 if Is_Composite_Type (Desig_Type)
4598 and then (not Is_Array_Type (Desig_Type))
4599 and then Has_Discriminants (Desig_Type)
4600 and then Base_Type (Desig_Type) /= Desig_Type
4601 then
4602 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
4603 Discr_Con_El := First_Elmt (Discr_Con_Elist);
4604
4605 Discr := First_Discriminant (Base_Type (Desig_Type));
4606 while Present (Discr_Con_El) loop
4607 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
4608 Next_Elmt (Discr_Con_El);
4609 Next_Discriminant (Discr);
4610 end loop;
4611 end if;
4612 end Build_Derived_Access_Type;
4613
4614 ------------------------------
4615 -- Build_Derived_Array_Type --
4616 ------------------------------
4617
4618 procedure Build_Derived_Array_Type
4619 (N : Node_Id;
4620 Parent_Type : Entity_Id;
4621 Derived_Type : Entity_Id)
4622 is
4623 Loc : constant Source_Ptr := Sloc (N);
4624 Tdef : constant Node_Id := Type_Definition (N);
4625 Indic : constant Node_Id := Subtype_Indication (Tdef);
4626 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
4627 Implicit_Base : Entity_Id;
4628 New_Indic : Node_Id;
4629
4630 procedure Make_Implicit_Base;
4631 -- If the parent subtype is constrained, the derived type is a subtype
4632 -- of an implicit base type derived from the parent base.
4633
4634 ------------------------
4635 -- Make_Implicit_Base --
4636 ------------------------
4637
4638 procedure Make_Implicit_Base is
4639 begin
4640 Implicit_Base :=
4641 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4642
4643 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
4644 Set_Etype (Implicit_Base, Parent_Base);
4645
4646 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
4647 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
4648
4649 Set_Has_Delayed_Freeze (Implicit_Base, True);
4650 end Make_Implicit_Base;
4651
4652 -- Start of processing for Build_Derived_Array_Type
4653
4654 begin
4655 if not Is_Constrained (Parent_Type) then
4656 if Nkind (Indic) /= N_Subtype_Indication then
4657 Set_Ekind (Derived_Type, E_Array_Type);
4658
4659 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4660 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
4661
4662 Set_Has_Delayed_Freeze (Derived_Type, True);
4663
4664 else
4665 Make_Implicit_Base;
4666 Set_Etype (Derived_Type, Implicit_Base);
4667
4668 New_Indic :=
4669 Make_Subtype_Declaration (Loc,
4670 Defining_Identifier => Derived_Type,
4671 Subtype_Indication =>
4672 Make_Subtype_Indication (Loc,
4673 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
4674 Constraint => Constraint (Indic)));
4675
4676 Rewrite (N, New_Indic);
4677 Analyze (N);
4678 end if;
4679
4680 else
4681 if Nkind (Indic) /= N_Subtype_Indication then
4682 Make_Implicit_Base;
4683
4684 Set_Ekind (Derived_Type, Ekind (Parent_Type));
4685 Set_Etype (Derived_Type, Implicit_Base);
4686 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4687
4688 else
4689 Error_Msg_N ("illegal constraint on constrained type", Indic);
4690 end if;
4691 end if;
4692
4693 -- If parent type is not a derived type itself, and is declared in
4694 -- closed scope (e.g. a subprogram), then we must explicitly introduce
4695 -- the new type's concatenation operator since Derive_Subprograms
4696 -- will not inherit the parent's operator. If the parent type is
4697 -- unconstrained, the operator is of the unconstrained base type.
4698
4699 if Number_Dimensions (Parent_Type) = 1
4700 and then not Is_Limited_Type (Parent_Type)
4701 and then not Is_Derived_Type (Parent_Type)
4702 and then not Is_Package_Or_Generic_Package
4703 (Scope (Base_Type (Parent_Type)))
4704 then
4705 if not Is_Constrained (Parent_Type)
4706 and then Is_Constrained (Derived_Type)
4707 then
4708 New_Concatenation_Op (Implicit_Base);
4709 else
4710 New_Concatenation_Op (Derived_Type);
4711 end if;
4712 end if;
4713 end Build_Derived_Array_Type;
4714
4715 -----------------------------------
4716 -- Build_Derived_Concurrent_Type --
4717 -----------------------------------
4718
4719 procedure Build_Derived_Concurrent_Type
4720 (N : Node_Id;
4721 Parent_Type : Entity_Id;
4722 Derived_Type : Entity_Id)
4723 is
4724 D_Constraint : Node_Id;
4725 Disc_Spec : Node_Id;
4726 Old_Disc : Entity_Id;
4727 New_Disc : Entity_Id;
4728
4729 Constraint_Present : constant Boolean :=
4730 Nkind (Subtype_Indication (Type_Definition (N)))
4731 = N_Subtype_Indication;
4732
4733 begin
4734 Set_Stored_Constraint (Derived_Type, No_Elist);
4735
4736 -- Copy Storage_Size and Relative_Deadline variables if task case
4737
4738 if Is_Task_Type (Parent_Type) then
4739 Set_Storage_Size_Variable (Derived_Type,
4740 Storage_Size_Variable (Parent_Type));
4741 Set_Relative_Deadline_Variable (Derived_Type,
4742 Relative_Deadline_Variable (Parent_Type));
4743 end if;
4744
4745 if Present (Discriminant_Specifications (N)) then
4746 Push_Scope (Derived_Type);
4747 Check_Or_Process_Discriminants (N, Derived_Type);
4748 End_Scope;
4749
4750 elsif Constraint_Present then
4751
4752 -- Build constrained subtype and derive from it
4753
4754 declare
4755 Loc : constant Source_Ptr := Sloc (N);
4756 Anon : constant Entity_Id :=
4757 Make_Defining_Identifier (Loc,
4758 New_External_Name (Chars (Derived_Type), 'T'));
4759 Decl : Node_Id;
4760
4761 begin
4762 Decl :=
4763 Make_Subtype_Declaration (Loc,
4764 Defining_Identifier => Anon,
4765 Subtype_Indication =>
4766 Subtype_Indication (Type_Definition (N)));
4767 Insert_Before (N, Decl);
4768 Analyze (Decl);
4769
4770 Rewrite (Subtype_Indication (Type_Definition (N)),
4771 New_Occurrence_Of (Anon, Loc));
4772 Set_Analyzed (Derived_Type, False);
4773 Analyze (N);
4774 return;
4775 end;
4776 end if;
4777
4778 -- All attributes are inherited from parent. In particular,
4779 -- entries and the corresponding record type are the same.
4780 -- Discriminants may be renamed, and must be treated separately.
4781
4782 Set_Has_Discriminants
4783 (Derived_Type, Has_Discriminants (Parent_Type));
4784 Set_Corresponding_Record_Type
4785 (Derived_Type, Corresponding_Record_Type (Parent_Type));
4786
4787 -- Is_Constrained is set according the parent subtype, but is set to
4788 -- False if the derived type is declared with new discriminants.
4789
4790 Set_Is_Constrained
4791 (Derived_Type,
4792 (Is_Constrained (Parent_Type) or else Constraint_Present)
4793 and then not Present (Discriminant_Specifications (N)));
4794
4795 if Constraint_Present then
4796 if not Has_Discriminants (Parent_Type) then
4797 Error_Msg_N ("untagged parent must have discriminants", N);
4798
4799 elsif Present (Discriminant_Specifications (N)) then
4800
4801 -- Verify that new discriminants are used to constrain old ones
4802
4803 D_Constraint :=
4804 First
4805 (Constraints
4806 (Constraint (Subtype_Indication (Type_Definition (N)))));
4807
4808 Old_Disc := First_Discriminant (Parent_Type);
4809 New_Disc := First_Discriminant (Derived_Type);
4810 Disc_Spec := First (Discriminant_Specifications (N));
4811 while Present (Old_Disc) and then Present (Disc_Spec) loop
4812 if Nkind (Discriminant_Type (Disc_Spec)) /=
4813 N_Access_Definition
4814 then
4815 Analyze (Discriminant_Type (Disc_Spec));
4816
4817 if not Subtypes_Statically_Compatible (
4818 Etype (Discriminant_Type (Disc_Spec)),
4819 Etype (Old_Disc))
4820 then
4821 Error_Msg_N
4822 ("not statically compatible with parent discriminant",
4823 Discriminant_Type (Disc_Spec));
4824 end if;
4825 end if;
4826
4827 if Nkind (D_Constraint) = N_Identifier
4828 and then Chars (D_Constraint) /=
4829 Chars (Defining_Identifier (Disc_Spec))
4830 then
4831 Error_Msg_N ("new discriminants must constrain old ones",
4832 D_Constraint);
4833 else
4834 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
4835 end if;
4836
4837 Next_Discriminant (Old_Disc);
4838 Next_Discriminant (New_Disc);
4839 Next (Disc_Spec);
4840 end loop;
4841
4842 if Present (Old_Disc) or else Present (Disc_Spec) then
4843 Error_Msg_N ("discriminant mismatch in derivation", N);
4844 end if;
4845
4846 end if;
4847
4848 elsif Present (Discriminant_Specifications (N)) then
4849 Error_Msg_N
4850 ("missing discriminant constraint in untagged derivation",
4851 N);
4852 end if;
4853
4854 if Present (Discriminant_Specifications (N)) then
4855 Old_Disc := First_Discriminant (Parent_Type);
4856 while Present (Old_Disc) loop
4857
4858 if No (Next_Entity (Old_Disc))
4859 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
4860 then
4861 Set_Next_Entity (Last_Entity (Derived_Type),
4862 Next_Entity (Old_Disc));
4863 exit;
4864 end if;
4865
4866 Next_Discriminant (Old_Disc);
4867 end loop;
4868
4869 else
4870 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
4871 if Has_Discriminants (Parent_Type) then
4872 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4873 Set_Discriminant_Constraint (
4874 Derived_Type, Discriminant_Constraint (Parent_Type));
4875 end if;
4876 end if;
4877
4878 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
4879
4880 Set_Has_Completion (Derived_Type);
4881 end Build_Derived_Concurrent_Type;
4882
4883 ------------------------------------
4884 -- Build_Derived_Enumeration_Type --
4885 ------------------------------------
4886
4887 procedure Build_Derived_Enumeration_Type
4888 (N : Node_Id;
4889 Parent_Type : Entity_Id;
4890 Derived_Type : Entity_Id)
4891 is
4892 Loc : constant Source_Ptr := Sloc (N);
4893 Def : constant Node_Id := Type_Definition (N);
4894 Indic : constant Node_Id := Subtype_Indication (Def);
4895 Implicit_Base : Entity_Id;
4896 Literal : Entity_Id;
4897 New_Lit : Entity_Id;
4898 Literals_List : List_Id;
4899 Type_Decl : Node_Id;
4900 Hi, Lo : Node_Id;
4901 Rang_Expr : Node_Id;
4902
4903 begin
4904 -- Since types Standard.Character and Standard.Wide_Character do
4905 -- not have explicit literals lists we need to process types derived
4906 -- from them specially. This is handled by Derived_Standard_Character.
4907 -- If the parent type is a generic type, there are no literals either,
4908 -- and we construct the same skeletal representation as for the generic
4909 -- parent type.
4910
4911 if Is_Standard_Character_Type (Parent_Type) then
4912 Derived_Standard_Character (N, Parent_Type, Derived_Type);
4913
4914 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
4915 declare
4916 Lo : Node_Id;
4917 Hi : Node_Id;
4918
4919 begin
4920 Lo :=
4921 Make_Attribute_Reference (Loc,
4922 Attribute_Name => Name_First,
4923 Prefix => New_Reference_To (Derived_Type, Loc));
4924 Set_Etype (Lo, Derived_Type);
4925
4926 Hi :=
4927 Make_Attribute_Reference (Loc,
4928 Attribute_Name => Name_Last,
4929 Prefix => New_Reference_To (Derived_Type, Loc));
4930 Set_Etype (Hi, Derived_Type);
4931
4932 Set_Scalar_Range (Derived_Type,
4933 Make_Range (Loc,
4934 Low_Bound => Lo,
4935 High_Bound => Hi));
4936 end;
4937
4938 else
4939 -- If a constraint is present, analyze the bounds to catch
4940 -- premature usage of the derived literals.
4941
4942 if Nkind (Indic) = N_Subtype_Indication
4943 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
4944 then
4945 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
4946 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
4947 end if;
4948
4949 -- Introduce an implicit base type for the derived type even if there
4950 -- is no constraint attached to it, since this seems closer to the
4951 -- Ada semantics. Build a full type declaration tree for the derived
4952 -- type using the implicit base type as the defining identifier. The
4953 -- build a subtype declaration tree which applies the constraint (if
4954 -- any) have it replace the derived type declaration.
4955
4956 Literal := First_Literal (Parent_Type);
4957 Literals_List := New_List;
4958 while Present (Literal)
4959 and then Ekind (Literal) = E_Enumeration_Literal
4960 loop
4961 -- Literals of the derived type have the same representation as
4962 -- those of the parent type, but this representation can be
4963 -- overridden by an explicit representation clause. Indicate
4964 -- that there is no explicit representation given yet. These
4965 -- derived literals are implicit operations of the new type,
4966 -- and can be overridden by explicit ones.
4967
4968 if Nkind (Literal) = N_Defining_Character_Literal then
4969 New_Lit :=
4970 Make_Defining_Character_Literal (Loc, Chars (Literal));
4971 else
4972 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
4973 end if;
4974
4975 Set_Ekind (New_Lit, E_Enumeration_Literal);
4976 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
4977 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
4978 Set_Enumeration_Rep_Expr (New_Lit, Empty);
4979 Set_Alias (New_Lit, Literal);
4980 Set_Is_Known_Valid (New_Lit, True);
4981
4982 Append (New_Lit, Literals_List);
4983 Next_Literal (Literal);
4984 end loop;
4985
4986 Implicit_Base :=
4987 Make_Defining_Identifier (Sloc (Derived_Type),
4988 New_External_Name (Chars (Derived_Type), 'B'));
4989
4990 -- Indicate the proper nature of the derived type. This must be done
4991 -- before analysis of the literals, to recognize cases when a literal
4992 -- may be hidden by a previous explicit function definition (cf.
4993 -- c83031a).
4994
4995 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
4996 Set_Etype (Derived_Type, Implicit_Base);
4997
4998 Type_Decl :=
4999 Make_Full_Type_Declaration (Loc,
5000 Defining_Identifier => Implicit_Base,
5001 Discriminant_Specifications => No_List,
5002 Type_Definition =>
5003 Make_Enumeration_Type_Definition (Loc, Literals_List));
5004
5005 Mark_Rewrite_Insertion (Type_Decl);
5006 Insert_Before (N, Type_Decl);
5007 Analyze (Type_Decl);
5008
5009 -- After the implicit base is analyzed its Etype needs to be changed
5010 -- to reflect the fact that it is derived from the parent type which
5011 -- was ignored during analysis. We also set the size at this point.
5012
5013 Set_Etype (Implicit_Base, Parent_Type);
5014
5015 Set_Size_Info (Implicit_Base, Parent_Type);
5016 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5017 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5018
5019 Set_Has_Non_Standard_Rep
5020 (Implicit_Base, Has_Non_Standard_Rep
5021 (Parent_Type));
5022 Set_Has_Delayed_Freeze (Implicit_Base);
5023
5024 -- Process the subtype indication including a validation check on the
5025 -- constraint, if any. If a constraint is given, its bounds must be
5026 -- implicitly converted to the new type.
5027
5028 if Nkind (Indic) = N_Subtype_Indication then
5029 declare
5030 R : constant Node_Id :=
5031 Range_Expression (Constraint (Indic));
5032
5033 begin
5034 if Nkind (R) = N_Range then
5035 Hi := Build_Scalar_Bound
5036 (High_Bound (R), Parent_Type, Implicit_Base);
5037 Lo := Build_Scalar_Bound
5038 (Low_Bound (R), Parent_Type, Implicit_Base);
5039
5040 else
5041 -- Constraint is a Range attribute. Replace with explicit
5042 -- mention of the bounds of the prefix, which must be a
5043 -- subtype.
5044
5045 Analyze (Prefix (R));
5046 Hi :=
5047 Convert_To (Implicit_Base,
5048 Make_Attribute_Reference (Loc,
5049 Attribute_Name => Name_Last,
5050 Prefix =>
5051 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5052
5053 Lo :=
5054 Convert_To (Implicit_Base,
5055 Make_Attribute_Reference (Loc,
5056 Attribute_Name => Name_First,
5057 Prefix =>
5058 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5059 end if;
5060 end;
5061
5062 else
5063 Hi :=
5064 Build_Scalar_Bound
5065 (Type_High_Bound (Parent_Type),
5066 Parent_Type, Implicit_Base);
5067 Lo :=
5068 Build_Scalar_Bound
5069 (Type_Low_Bound (Parent_Type),
5070 Parent_Type, Implicit_Base);
5071 end if;
5072
5073 Rang_Expr :=
5074 Make_Range (Loc,
5075 Low_Bound => Lo,
5076 High_Bound => Hi);
5077
5078 -- If we constructed a default range for the case where no range
5079 -- was given, then the expressions in the range must not freeze
5080 -- since they do not correspond to expressions in the source.
5081
5082 if Nkind (Indic) /= N_Subtype_Indication then
5083 Set_Must_Not_Freeze (Lo);
5084 Set_Must_Not_Freeze (Hi);
5085 Set_Must_Not_Freeze (Rang_Expr);
5086 end if;
5087
5088 Rewrite (N,
5089 Make_Subtype_Declaration (Loc,
5090 Defining_Identifier => Derived_Type,
5091 Subtype_Indication =>
5092 Make_Subtype_Indication (Loc,
5093 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5094 Constraint =>
5095 Make_Range_Constraint (Loc,
5096 Range_Expression => Rang_Expr))));
5097
5098 Analyze (N);
5099
5100 -- If pragma Discard_Names applies on the first subtype of the parent
5101 -- type, then it must be applied on this subtype as well.
5102
5103 if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5104 Set_Discard_Names (Derived_Type);
5105 end if;
5106
5107 -- Apply a range check. Since this range expression doesn't have an
5108 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5109 -- this right???
5110
5111 if Nkind (Indic) = N_Subtype_Indication then
5112 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5113 Parent_Type,
5114 Source_Typ => Entity (Subtype_Mark (Indic)));
5115 end if;
5116 end if;
5117 end Build_Derived_Enumeration_Type;
5118
5119 --------------------------------
5120 -- Build_Derived_Numeric_Type --
5121 --------------------------------
5122
5123 procedure Build_Derived_Numeric_Type
5124 (N : Node_Id;
5125 Parent_Type : Entity_Id;
5126 Derived_Type : Entity_Id)
5127 is
5128 Loc : constant Source_Ptr := Sloc (N);
5129 Tdef : constant Node_Id := Type_Definition (N);
5130 Indic : constant Node_Id := Subtype_Indication (Tdef);
5131 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5132 No_Constraint : constant Boolean := Nkind (Indic) /=
5133 N_Subtype_Indication;
5134 Implicit_Base : Entity_Id;
5135
5136 Lo : Node_Id;
5137 Hi : Node_Id;
5138
5139 begin
5140 -- Process the subtype indication including a validation check on
5141 -- the constraint if any.
5142
5143 Discard_Node (Process_Subtype (Indic, N));
5144
5145 -- Introduce an implicit base type for the derived type even if there
5146 -- is no constraint attached to it, since this seems closer to the Ada
5147 -- semantics.
5148
5149 Implicit_Base :=
5150 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5151
5152 Set_Etype (Implicit_Base, Parent_Base);
5153 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5154 Set_Size_Info (Implicit_Base, Parent_Base);
5155 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5156 Set_Parent (Implicit_Base, Parent (Derived_Type));
5157
5158 -- Set RM Size for discrete type or decimal fixed-point type
5159 -- Ordinary fixed-point is excluded, why???
5160
5161 if Is_Discrete_Type (Parent_Base)
5162 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5163 then
5164 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5165 end if;
5166
5167 Set_Has_Delayed_Freeze (Implicit_Base);
5168
5169 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5170 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5171
5172 Set_Scalar_Range (Implicit_Base,
5173 Make_Range (Loc,
5174 Low_Bound => Lo,
5175 High_Bound => Hi));
5176
5177 if Has_Infinities (Parent_Base) then
5178 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5179 end if;
5180
5181 -- The Derived_Type, which is the entity of the declaration, is a
5182 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5183 -- absence of an explicit constraint.
5184
5185 Set_Etype (Derived_Type, Implicit_Base);
5186
5187 -- If we did not have a constraint, then the Ekind is set from the
5188 -- parent type (otherwise Process_Subtype has set the bounds)
5189
5190 if No_Constraint then
5191 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5192 end if;
5193
5194 -- If we did not have a range constraint, then set the range from the
5195 -- parent type. Otherwise, the call to Process_Subtype has set the
5196 -- bounds.
5197
5198 if No_Constraint
5199 or else not Has_Range_Constraint (Indic)
5200 then
5201 Set_Scalar_Range (Derived_Type,
5202 Make_Range (Loc,
5203 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
5204 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5205 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5206
5207 if Has_Infinities (Parent_Type) then
5208 Set_Includes_Infinities (Scalar_Range (Derived_Type));
5209 end if;
5210 end if;
5211
5212 Set_Is_Descendent_Of_Address (Derived_Type,
5213 Is_Descendent_Of_Address (Parent_Type));
5214 Set_Is_Descendent_Of_Address (Implicit_Base,
5215 Is_Descendent_Of_Address (Parent_Type));
5216
5217 -- Set remaining type-specific fields, depending on numeric type
5218
5219 if Is_Modular_Integer_Type (Parent_Type) then
5220 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5221
5222 Set_Non_Binary_Modulus
5223 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5224
5225 elsif Is_Floating_Point_Type (Parent_Type) then
5226
5227 -- Digits of base type is always copied from the digits value of
5228 -- the parent base type, but the digits of the derived type will
5229 -- already have been set if there was a constraint present.
5230
5231 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5232 Set_Vax_Float (Implicit_Base, Vax_Float (Parent_Base));
5233
5234 if No_Constraint then
5235 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5236 end if;
5237
5238 elsif Is_Fixed_Point_Type (Parent_Type) then
5239
5240 -- Small of base type and derived type are always copied from the
5241 -- parent base type, since smalls never change. The delta of the
5242 -- base type is also copied from the parent base type. However the
5243 -- delta of the derived type will have been set already if a
5244 -- constraint was present.
5245
5246 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
5247 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5248 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5249
5250 if No_Constraint then
5251 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
5252 end if;
5253
5254 -- The scale and machine radix in the decimal case are always
5255 -- copied from the parent base type.
5256
5257 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5258 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
5259 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5260
5261 Set_Machine_Radix_10
5262 (Derived_Type, Machine_Radix_10 (Parent_Base));
5263 Set_Machine_Radix_10
5264 (Implicit_Base, Machine_Radix_10 (Parent_Base));
5265
5266 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5267
5268 if No_Constraint then
5269 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5270
5271 else
5272 -- the analysis of the subtype_indication sets the
5273 -- digits value of the derived type.
5274
5275 null;
5276 end if;
5277 end if;
5278 end if;
5279
5280 -- The type of the bounds is that of the parent type, and they
5281 -- must be converted to the derived type.
5282
5283 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5284
5285 -- The implicit_base should be frozen when the derived type is frozen,
5286 -- but note that it is used in the conversions of the bounds. For fixed
5287 -- types we delay the determination of the bounds until the proper
5288 -- freezing point. For other numeric types this is rejected by GCC, for
5289 -- reasons that are currently unclear (???), so we choose to freeze the
5290 -- implicit base now. In the case of integers and floating point types
5291 -- this is harmless because subsequent representation clauses cannot
5292 -- affect anything, but it is still baffling that we cannot use the
5293 -- same mechanism for all derived numeric types.
5294
5295 -- There is a further complication: actually *some* representation
5296 -- clauses can affect the implicit base type. Namely, attribute
5297 -- definition clauses for stream-oriented attributes need to set the
5298 -- corresponding TSS entries on the base type, and this normally cannot
5299 -- be done after the base type is frozen, so the circuitry in
5300 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5301 -- not use Set_TSS in this case.
5302
5303 if Is_Fixed_Point_Type (Parent_Type) then
5304 Conditional_Delay (Implicit_Base, Parent_Type);
5305 else
5306 Freeze_Before (N, Implicit_Base);
5307 end if;
5308 end Build_Derived_Numeric_Type;
5309
5310 --------------------------------
5311 -- Build_Derived_Private_Type --
5312 --------------------------------
5313
5314 procedure Build_Derived_Private_Type
5315 (N : Node_Id;
5316 Parent_Type : Entity_Id;
5317 Derived_Type : Entity_Id;
5318 Is_Completion : Boolean;
5319 Derive_Subps : Boolean := True)
5320 is
5321 Der_Base : Entity_Id;
5322 Discr : Entity_Id;
5323 Full_Decl : Node_Id := Empty;
5324 Full_Der : Entity_Id;
5325 Full_P : Entity_Id;
5326 Last_Discr : Entity_Id;
5327 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
5328 Swapped : Boolean := False;
5329
5330 procedure Copy_And_Build;
5331 -- Copy derived type declaration, replace parent with its full view,
5332 -- and analyze new declaration.
5333
5334 --------------------
5335 -- Copy_And_Build --
5336 --------------------
5337
5338 procedure Copy_And_Build is
5339 Full_N : Node_Id;
5340
5341 begin
5342 if Ekind (Parent_Type) in Record_Kind
5343 or else
5344 (Ekind (Parent_Type) in Enumeration_Kind
5345 and then not Is_Standard_Character_Type (Parent_Type)
5346 and then not Is_Generic_Type (Root_Type (Parent_Type)))
5347 then
5348 Full_N := New_Copy_Tree (N);
5349 Insert_After (N, Full_N);
5350 Build_Derived_Type (
5351 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5352
5353 else
5354 Build_Derived_Type (
5355 N, Parent_Type, Full_Der, True, Derive_Subps => False);
5356 end if;
5357 end Copy_And_Build;
5358
5359 -- Start of processing for Build_Derived_Private_Type
5360
5361 begin
5362 if Is_Tagged_Type (Parent_Type) then
5363 Build_Derived_Record_Type
5364 (N, Parent_Type, Derived_Type, Derive_Subps);
5365 return;
5366
5367 elsif Has_Discriminants (Parent_Type) then
5368 if Present (Full_View (Parent_Type)) then
5369 if not Is_Completion then
5370
5371 -- Copy declaration for subsequent analysis, to provide a
5372 -- completion for what is a private declaration. Indicate that
5373 -- the full type is internally generated.
5374
5375 Full_Decl := New_Copy_Tree (N);
5376 Full_Der := New_Copy (Derived_Type);
5377 Set_Comes_From_Source (Full_Decl, False);
5378 Set_Comes_From_Source (Full_Der, False);
5379
5380 Insert_After (N, Full_Decl);
5381
5382 else
5383 -- If this is a completion, the full view being built is
5384 -- itself private. We build a subtype of the parent with
5385 -- the same constraints as this full view, to convey to the
5386 -- back end the constrained components and the size of this
5387 -- subtype. If the parent is constrained, its full view can
5388 -- serve as the underlying full view of the derived type.
5389
5390 if No (Discriminant_Specifications (N)) then
5391 if Nkind (Subtype_Indication (Type_Definition (N))) =
5392 N_Subtype_Indication
5393 then
5394 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
5395
5396 elsif Is_Constrained (Full_View (Parent_Type)) then
5397 Set_Underlying_Full_View (Derived_Type,
5398 Full_View (Parent_Type));
5399 end if;
5400
5401 else
5402 -- If there are new discriminants, the parent subtype is
5403 -- constrained by them, but it is not clear how to build
5404 -- the underlying_full_view in this case ???
5405
5406 null;
5407 end if;
5408 end if;
5409 end if;
5410
5411 -- Build partial view of derived type from partial view of parent
5412
5413 Build_Derived_Record_Type
5414 (N, Parent_Type, Derived_Type, Derive_Subps);
5415
5416 if Present (Full_View (Parent_Type))
5417 and then not Is_Completion
5418 then
5419 if not In_Open_Scopes (Par_Scope)
5420 or else not In_Same_Source_Unit (N, Parent_Type)
5421 then
5422 -- Swap partial and full views temporarily
5423
5424 Install_Private_Declarations (Par_Scope);
5425 Install_Visible_Declarations (Par_Scope);
5426 Swapped := True;
5427 end if;
5428
5429 -- Build full view of derived type from full view of parent which
5430 -- is now installed. Subprograms have been derived on the partial
5431 -- view, the completion does not derive them anew.
5432
5433 if not Is_Tagged_Type (Parent_Type) then
5434
5435 -- If the parent is itself derived from another private type,
5436 -- installing the private declarations has not affected its
5437 -- privacy status, so use its own full view explicitly.
5438
5439 if Is_Private_Type (Parent_Type) then
5440 Build_Derived_Record_Type
5441 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
5442 else
5443 Build_Derived_Record_Type
5444 (Full_Decl, Parent_Type, Full_Der, False);
5445 end if;
5446
5447 else
5448 -- If full view of parent is tagged, the completion
5449 -- inherits the proper primitive operations.
5450
5451 Set_Defining_Identifier (Full_Decl, Full_Der);
5452 Build_Derived_Record_Type
5453 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
5454 Set_Analyzed (Full_Decl);
5455 end if;
5456
5457 if Swapped then
5458 Uninstall_Declarations (Par_Scope);
5459
5460 if In_Open_Scopes (Par_Scope) then
5461 Install_Visible_Declarations (Par_Scope);
5462 end if;
5463 end if;
5464
5465 Der_Base := Base_Type (Derived_Type);
5466 Set_Full_View (Derived_Type, Full_Der);
5467 Set_Full_View (Der_Base, Base_Type (Full_Der));
5468
5469 -- Copy the discriminant list from full view to the partial views
5470 -- (base type and its subtype). Gigi requires that the partial
5471 -- and full views have the same discriminants.
5472
5473 -- Note that since the partial view is pointing to discriminants
5474 -- in the full view, their scope will be that of the full view.
5475 -- This might cause some front end problems and need
5476 -- adjustment???
5477
5478 Discr := First_Discriminant (Base_Type (Full_Der));
5479 Set_First_Entity (Der_Base, Discr);
5480
5481 loop
5482 Last_Discr := Discr;
5483 Next_Discriminant (Discr);
5484 exit when No (Discr);
5485 end loop;
5486
5487 Set_Last_Entity (Der_Base, Last_Discr);
5488
5489 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
5490 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
5491 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
5492
5493 else
5494 -- If this is a completion, the derived type stays private
5495 -- and there is no need to create a further full view, except
5496 -- in the unusual case when the derivation is nested within a
5497 -- child unit, see below.
5498
5499 null;
5500 end if;
5501
5502 elsif Present (Full_View (Parent_Type))
5503 and then Has_Discriminants (Full_View (Parent_Type))
5504 then
5505 if Has_Unknown_Discriminants (Parent_Type)
5506 and then Nkind (Subtype_Indication (Type_Definition (N))) =
5507 N_Subtype_Indication
5508 then
5509 Error_Msg_N
5510 ("cannot constrain type with unknown discriminants",
5511 Subtype_Indication (Type_Definition (N)));
5512 return;
5513 end if;
5514
5515 -- If full view of parent is a record type, Build full view as
5516 -- a derivation from the parent's full view. Partial view remains
5517 -- private. For code generation and linking, the full view must
5518 -- have the same public status as the partial one. This full view
5519 -- is only needed if the parent type is in an enclosing scope, so
5520 -- that the full view may actually become visible, e.g. in a child
5521 -- unit. This is both more efficient, and avoids order of freezing
5522 -- problems with the added entities.
5523
5524 if not Is_Private_Type (Full_View (Parent_Type))
5525 and then (In_Open_Scopes (Scope (Parent_Type)))
5526 then
5527 Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
5528 Chars (Derived_Type));
5529 Set_Is_Itype (Full_Der);
5530 Set_Has_Private_Declaration (Full_Der);
5531 Set_Has_Private_Declaration (Derived_Type);
5532 Set_Associated_Node_For_Itype (Full_Der, N);
5533 Set_Parent (Full_Der, Parent (Derived_Type));
5534 Set_Full_View (Derived_Type, Full_Der);
5535 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
5536 Full_P := Full_View (Parent_Type);
5537 Exchange_Declarations (Parent_Type);
5538 Copy_And_Build;
5539 Exchange_Declarations (Full_P);
5540
5541 else
5542 Build_Derived_Record_Type
5543 (N, Full_View (Parent_Type), Derived_Type,
5544 Derive_Subps => False);
5545 end if;
5546
5547 -- In any case, the primitive operations are inherited from
5548 -- the parent type, not from the internal full view.
5549
5550 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
5551
5552 if Derive_Subps then
5553 Derive_Subprograms (Parent_Type, Derived_Type);
5554 end if;
5555
5556 else
5557 -- Untagged type, No discriminants on either view
5558
5559 if Nkind (Subtype_Indication (Type_Definition (N))) =
5560 N_Subtype_Indication
5561 then
5562 Error_Msg_N
5563 ("illegal constraint on type without discriminants", N);
5564 end if;
5565
5566 if Present (Discriminant_Specifications (N))
5567 and then Present (Full_View (Parent_Type))
5568 and then not Is_Tagged_Type (Full_View (Parent_Type))
5569 then
5570 Error_Msg_N
5571 ("cannot add discriminants to untagged type", N);
5572 end if;
5573
5574 Set_Stored_Constraint (Derived_Type, No_Elist);
5575 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5576 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
5577 Set_Has_Controlled_Component
5578 (Derived_Type, Has_Controlled_Component
5579 (Parent_Type));
5580
5581 -- Direct controlled types do not inherit Finalize_Storage_Only flag
5582
5583 if not Is_Controlled (Parent_Type) then
5584 Set_Finalize_Storage_Only
5585 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
5586 end if;
5587
5588 -- Construct the implicit full view by deriving from full view of
5589 -- the parent type. In order to get proper visibility, we install
5590 -- the parent scope and its declarations.
5591
5592 -- ??? if the parent is untagged private and its completion is
5593 -- tagged, this mechanism will not work because we cannot derive
5594 -- from the tagged full view unless we have an extension
5595
5596 if Present (Full_View (Parent_Type))
5597 and then not Is_Tagged_Type (Full_View (Parent_Type))
5598 and then not Is_Completion
5599 then
5600 Full_Der :=
5601 Make_Defining_Identifier (Sloc (Derived_Type),
5602 Chars => Chars (Derived_Type));
5603 Set_Is_Itype (Full_Der);
5604 Set_Has_Private_Declaration (Full_Der);
5605 Set_Has_Private_Declaration (Derived_Type);
5606 Set_Associated_Node_For_Itype (Full_Der, N);
5607 Set_Parent (Full_Der, Parent (Derived_Type));
5608 Set_Full_View (Derived_Type, Full_Der);
5609
5610 if not In_Open_Scopes (Par_Scope) then
5611 Install_Private_Declarations (Par_Scope);
5612 Install_Visible_Declarations (Par_Scope);
5613 Copy_And_Build;
5614 Uninstall_Declarations (Par_Scope);
5615
5616 -- If parent scope is open and in another unit, and parent has a
5617 -- completion, then the derivation is taking place in the visible
5618 -- part of a child unit. In that case retrieve the full view of
5619 -- the parent momentarily.
5620
5621 elsif not In_Same_Source_Unit (N, Parent_Type) then
5622 Full_P := Full_View (Parent_Type);
5623 Exchange_Declarations (Parent_Type);
5624 Copy_And_Build;
5625 Exchange_Declarations (Full_P);
5626
5627 -- Otherwise it is a local derivation
5628
5629 else
5630 Copy_And_Build;
5631 end if;
5632
5633 Set_Scope (Full_Der, Current_Scope);
5634 Set_Is_First_Subtype (Full_Der,
5635 Is_First_Subtype (Derived_Type));
5636 Set_Has_Size_Clause (Full_Der, False);
5637 Set_Has_Alignment_Clause (Full_Der, False);
5638 Set_Next_Entity (Full_Der, Empty);
5639 Set_Has_Delayed_Freeze (Full_Der);
5640 Set_Is_Frozen (Full_Der, False);
5641 Set_Freeze_Node (Full_Der, Empty);
5642 Set_Depends_On_Private (Full_Der,
5643 Has_Private_Component (Full_Der));
5644 Set_Public_Status (Full_Der);
5645 end if;
5646 end if;
5647
5648 Set_Has_Unknown_Discriminants (Derived_Type,
5649 Has_Unknown_Discriminants (Parent_Type));
5650
5651 if Is_Private_Type (Derived_Type) then
5652 Set_Private_Dependents (Derived_Type, New_Elmt_List);
5653 end if;
5654
5655 if Is_Private_Type (Parent_Type)
5656 and then Base_Type (Parent_Type) = Parent_Type
5657 and then In_Open_Scopes (Scope (Parent_Type))
5658 then
5659 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
5660
5661 if Is_Child_Unit (Scope (Current_Scope))
5662 and then Is_Completion
5663 and then In_Private_Part (Current_Scope)
5664 and then Scope (Parent_Type) /= Current_Scope
5665 then
5666 -- This is the unusual case where a type completed by a private
5667 -- derivation occurs within a package nested in a child unit,
5668 -- and the parent is declared in an ancestor. In this case, the
5669 -- full view of the parent type will become visible in the body
5670 -- of the enclosing child, and only then will the current type
5671 -- be possibly non-private. We build a underlying full view that
5672 -- will be installed when the enclosing child body is compiled.
5673
5674 Full_Der :=
5675 Make_Defining_Identifier (Sloc (Derived_Type),
5676 Chars => Chars (Derived_Type));
5677 Set_Is_Itype (Full_Der);
5678 Build_Itype_Reference (Full_Der, N);
5679
5680 -- The full view will be used to swap entities on entry/exit to
5681 -- the body, and must appear in the entity list for the package.
5682
5683 Append_Entity (Full_Der, Scope (Derived_Type));
5684 Set_Has_Private_Declaration (Full_Der);
5685 Set_Has_Private_Declaration (Derived_Type);
5686 Set_Associated_Node_For_Itype (Full_Der, N);
5687 Set_Parent (Full_Der, Parent (Derived_Type));
5688 Full_P := Full_View (Parent_Type);
5689 Exchange_Declarations (Parent_Type);
5690 Copy_And_Build;
5691 Exchange_Declarations (Full_P);
5692 Set_Underlying_Full_View (Derived_Type, Full_Der);
5693 end if;
5694 end if;
5695 end Build_Derived_Private_Type;
5696
5697 -------------------------------
5698 -- Build_Derived_Record_Type --
5699 -------------------------------
5700
5701 -- 1. INTRODUCTION
5702
5703 -- Ideally we would like to use the same model of type derivation for
5704 -- tagged and untagged record types. Unfortunately this is not quite
5705 -- possible because the semantics of representation clauses is different
5706 -- for tagged and untagged records under inheritance. Consider the
5707 -- following:
5708
5709 -- type R (...) is [tagged] record ... end record;
5710 -- type T (...) is new R (...) [with ...];
5711
5712 -- The representation clauses for T can specify a completely different
5713 -- record layout from R's. Hence the same component can be placed in two
5714 -- very different positions in objects of type T and R. If R and are tagged
5715 -- types, representation clauses for T can only specify the layout of non
5716 -- inherited components, thus components that are common in R and T have
5717 -- the same position in objects of type R and T.
5718
5719 -- This has two implications. The first is that the entire tree for R's
5720 -- declaration needs to be copied for T in the untagged case, so that T
5721 -- can be viewed as a record type of its own with its own representation
5722 -- clauses. The second implication is the way we handle discriminants.
5723 -- Specifically, in the untagged case we need a way to communicate to Gigi
5724 -- what are the real discriminants in the record, while for the semantics
5725 -- we need to consider those introduced by the user to rename the
5726 -- discriminants in the parent type. This is handled by introducing the
5727 -- notion of stored discriminants. See below for more.
5728
5729 -- Fortunately the way regular components are inherited can be handled in
5730 -- the same way in tagged and untagged types.
5731
5732 -- To complicate things a bit more the private view of a private extension
5733 -- cannot be handled in the same way as the full view (for one thing the
5734 -- semantic rules are somewhat different). We will explain what differs
5735 -- below.
5736
5737 -- 2. DISCRIMINANTS UNDER INHERITANCE
5738
5739 -- The semantic rules governing the discriminants of derived types are
5740 -- quite subtle.
5741
5742 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
5743 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
5744
5745 -- If parent type has discriminants, then the discriminants that are
5746 -- declared in the derived type are [3.4 (11)]:
5747
5748 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
5749 -- there is one;
5750
5751 -- o Otherwise, each discriminant of the parent type (implicitly declared
5752 -- in the same order with the same specifications). In this case, the
5753 -- discriminants are said to be "inherited", or if unknown in the parent
5754 -- are also unknown in the derived type.
5755
5756 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
5757
5758 -- o The parent subtype shall be constrained;
5759
5760 -- o If the parent type is not a tagged type, then each discriminant of
5761 -- the derived type shall be used in the constraint defining a parent
5762 -- subtype. [Implementation note: This ensures that the new discriminant
5763 -- can share storage with an existing discriminant.]
5764
5765 -- For the derived type each discriminant of the parent type is either
5766 -- inherited, constrained to equal some new discriminant of the derived
5767 -- type, or constrained to the value of an expression.
5768
5769 -- When inherited or constrained to equal some new discriminant, the
5770 -- parent discriminant and the discriminant of the derived type are said
5771 -- to "correspond".
5772
5773 -- If a discriminant of the parent type is constrained to a specific value
5774 -- in the derived type definition, then the discriminant is said to be
5775 -- "specified" by that derived type definition.
5776
5777 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
5778
5779 -- We have spoken about stored discriminants in point 1 (introduction)
5780 -- above. There are two sort of stored discriminants: implicit and
5781 -- explicit. As long as the derived type inherits the same discriminants as
5782 -- the root record type, stored discriminants are the same as regular
5783 -- discriminants, and are said to be implicit. However, if any discriminant
5784 -- in the root type was renamed in the derived type, then the derived
5785 -- type will contain explicit stored discriminants. Explicit stored
5786 -- discriminants are discriminants in addition to the semantically visible
5787 -- discriminants defined for the derived type. Stored discriminants are
5788 -- used by Gigi to figure out what are the physical discriminants in
5789 -- objects of the derived type (see precise definition in einfo.ads).
5790 -- As an example, consider the following:
5791
5792 -- type R (D1, D2, D3 : Int) is record ... end record;
5793 -- type T1 is new R;
5794 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
5795 -- type T3 is new T2;
5796 -- type T4 (Y : Int) is new T3 (Y, 99);
5797
5798 -- The following table summarizes the discriminants and stored
5799 -- discriminants in R and T1 through T4.
5800
5801 -- Type Discrim Stored Discrim Comment
5802 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
5803 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
5804 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
5805 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
5806 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
5807
5808 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
5809 -- find the corresponding discriminant in the parent type, while
5810 -- Original_Record_Component (abbreviated ORC below), the actual physical
5811 -- component that is renamed. Finally the field Is_Completely_Hidden
5812 -- (abbreviated ICH below) is set for all explicit stored discriminants
5813 -- (see einfo.ads for more info). For the above example this gives:
5814
5815 -- Discrim CD ORC ICH
5816 -- ^^^^^^^ ^^ ^^^ ^^^
5817 -- D1 in R empty itself no
5818 -- D2 in R empty itself no
5819 -- D3 in R empty itself no
5820
5821 -- D1 in T1 D1 in R itself no
5822 -- D2 in T1 D2 in R itself no
5823 -- D3 in T1 D3 in R itself no
5824
5825 -- X1 in T2 D3 in T1 D3 in T2 no
5826 -- X2 in T2 D1 in T1 D1 in T2 no
5827 -- D1 in T2 empty itself yes
5828 -- D2 in T2 empty itself yes
5829 -- D3 in T2 empty itself yes
5830
5831 -- X1 in T3 X1 in T2 D3 in T3 no
5832 -- X2 in T3 X2 in T2 D1 in T3 no
5833 -- D1 in T3 empty itself yes
5834 -- D2 in T3 empty itself yes
5835 -- D3 in T3 empty itself yes
5836
5837 -- Y in T4 X1 in T3 D3 in T3 no
5838 -- D1 in T3 empty itself yes
5839 -- D2 in T3 empty itself yes
5840 -- D3 in T3 empty itself yes
5841
5842 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
5843
5844 -- Type derivation for tagged types is fairly straightforward. If no
5845 -- discriminants are specified by the derived type, these are inherited
5846 -- from the parent. No explicit stored discriminants are ever necessary.
5847 -- The only manipulation that is done to the tree is that of adding a
5848 -- _parent field with parent type and constrained to the same constraint
5849 -- specified for the parent in the derived type definition. For instance:
5850
5851 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
5852 -- type T1 is new R with null record;
5853 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
5854
5855 -- are changed into:
5856
5857 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
5858 -- _parent : R (D1, D2, D3);
5859 -- end record;
5860
5861 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
5862 -- _parent : T1 (X2, 88, X1);
5863 -- end record;
5864
5865 -- The discriminants actually present in R, T1 and T2 as well as their CD,
5866 -- ORC and ICH fields are:
5867
5868 -- Discrim CD ORC ICH
5869 -- ^^^^^^^ ^^ ^^^ ^^^
5870 -- D1 in R empty itself no
5871 -- D2 in R empty itself no
5872 -- D3 in R empty itself no
5873
5874 -- D1 in T1 D1 in R D1 in R no
5875 -- D2 in T1 D2 in R D2 in R no
5876 -- D3 in T1 D3 in R D3 in R no
5877
5878 -- X1 in T2 D3 in T1 D3 in R no
5879 -- X2 in T2 D1 in T1 D1 in R no
5880
5881 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
5882 --
5883 -- Regardless of whether we dealing with a tagged or untagged type
5884 -- we will transform all derived type declarations of the form
5885 --
5886 -- type T is new R (...) [with ...];
5887 -- or
5888 -- subtype S is R (...);
5889 -- type T is new S [with ...];
5890 -- into
5891 -- type BT is new R [with ...];
5892 -- subtype T is BT (...);
5893 --
5894 -- That is, the base derived type is constrained only if it has no
5895 -- discriminants. The reason for doing this is that GNAT's semantic model
5896 -- assumes that a base type with discriminants is unconstrained.
5897 --
5898 -- Note that, strictly speaking, the above transformation is not always
5899 -- correct. Consider for instance the following excerpt from ACVC b34011a:
5900 --
5901 -- procedure B34011A is
5902 -- type REC (D : integer := 0) is record
5903 -- I : Integer;
5904 -- end record;
5905
5906 -- package P is
5907 -- type T6 is new Rec;
5908 -- function F return T6;
5909 -- end P;
5910
5911 -- use P;
5912 -- package Q6 is
5913 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
5914 -- end Q6;
5915 --
5916 -- The definition of Q6.U is illegal. However transforming Q6.U into
5917
5918 -- type BaseU is new T6;
5919 -- subtype U is BaseU (Q6.F.I)
5920
5921 -- turns U into a legal subtype, which is incorrect. To avoid this problem
5922 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
5923 -- the transformation described above.
5924
5925 -- There is another instance where the above transformation is incorrect.
5926 -- Consider:
5927
5928 -- package Pack is
5929 -- type Base (D : Integer) is tagged null record;
5930 -- procedure P (X : Base);
5931
5932 -- type Der is new Base (2) with null record;
5933 -- procedure P (X : Der);
5934 -- end Pack;
5935
5936 -- Then the above transformation turns this into
5937
5938 -- type Der_Base is new Base with null record;
5939 -- -- procedure P (X : Base) is implicitly inherited here
5940 -- -- as procedure P (X : Der_Base).
5941
5942 -- subtype Der is Der_Base (2);
5943 -- procedure P (X : Der);
5944 -- -- The overriding of P (X : Der_Base) is illegal since we
5945 -- -- have a parameter conformance problem.
5946
5947 -- To get around this problem, after having semantically processed Der_Base
5948 -- and the rewritten subtype declaration for Der, we copy Der_Base field
5949 -- Discriminant_Constraint from Der so that when parameter conformance is
5950 -- checked when P is overridden, no semantic errors are flagged.
5951
5952 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
5953
5954 -- Regardless of whether we are dealing with a tagged or untagged type
5955 -- we will transform all derived type declarations of the form
5956
5957 -- type R (D1, .., Dn : ...) is [tagged] record ...;
5958 -- type T is new R [with ...];
5959 -- into
5960 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
5961
5962 -- The reason for such transformation is that it allows us to implement a
5963 -- very clean form of component inheritance as explained below.
5964
5965 -- Note that this transformation is not achieved by direct tree rewriting
5966 -- and manipulation, but rather by redoing the semantic actions that the
5967 -- above transformation will entail. This is done directly in routine
5968 -- Inherit_Components.
5969
5970 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
5971
5972 -- In both tagged and untagged derived types, regular non discriminant
5973 -- components are inherited in the derived type from the parent type. In
5974 -- the absence of discriminants component, inheritance is straightforward
5975 -- as components can simply be copied from the parent.
5976
5977 -- If the parent has discriminants, inheriting components constrained with
5978 -- these discriminants requires caution. Consider the following example:
5979
5980 -- type R (D1, D2 : Positive) is [tagged] record
5981 -- S : String (D1 .. D2);
5982 -- end record;
5983
5984 -- type T1 is new R [with null record];
5985 -- type T2 (X : positive) is new R (1, X) [with null record];
5986
5987 -- As explained in 6. above, T1 is rewritten as
5988 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
5989 -- which makes the treatment for T1 and T2 identical.
5990
5991 -- What we want when inheriting S, is that references to D1 and D2 in R are
5992 -- replaced with references to their correct constraints, i.e. D1 and D2 in
5993 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
5994 -- with either discriminant references in the derived type or expressions.
5995 -- This replacement is achieved as follows: before inheriting R's
5996 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
5997 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
5998 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
5999 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
6000 -- by String (1 .. X).
6001
6002 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6003
6004 -- We explain here the rules governing private type extensions relevant to
6005 -- type derivation. These rules are explained on the following example:
6006
6007 -- type D [(...)] is new A [(...)] with private; <-- partial view
6008 -- type D [(...)] is new P [(...)] with null record; <-- full view
6009
6010 -- Type A is called the ancestor subtype of the private extension.
6011 -- Type P is the parent type of the full view of the private extension. It
6012 -- must be A or a type derived from A.
6013
6014 -- The rules concerning the discriminants of private type extensions are
6015 -- [7.3(10-13)]:
6016
6017 -- o If a private extension inherits known discriminants from the ancestor
6018 -- subtype, then the full view shall also inherit its discriminants from
6019 -- the ancestor subtype and the parent subtype of the full view shall be
6020 -- constrained if and only if the ancestor subtype is constrained.
6021
6022 -- o If a partial view has unknown discriminants, then the full view may
6023 -- define a definite or an indefinite subtype, with or without
6024 -- discriminants.
6025
6026 -- o If a partial view has neither known nor unknown discriminants, then
6027 -- the full view shall define a definite subtype.
6028
6029 -- o If the ancestor subtype of a private extension has constrained
6030 -- discriminants, then the parent subtype of the full view shall impose a
6031 -- statically matching constraint on those discriminants.
6032
6033 -- This means that only the following forms of private extensions are
6034 -- allowed:
6035
6036 -- type D is new A with private; <-- partial view
6037 -- type D is new P with null record; <-- full view
6038
6039 -- If A has no discriminants than P has no discriminants, otherwise P must
6040 -- inherit A's discriminants.
6041
6042 -- type D is new A (...) with private; <-- partial view
6043 -- type D is new P (:::) with null record; <-- full view
6044
6045 -- P must inherit A's discriminants and (...) and (:::) must statically
6046 -- match.
6047
6048 -- subtype A is R (...);
6049 -- type D is new A with private; <-- partial view
6050 -- type D is new P with null record; <-- full view
6051
6052 -- P must have inherited R's discriminants and must be derived from A or
6053 -- any of its subtypes.
6054
6055 -- type D (..) is new A with private; <-- partial view
6056 -- type D (..) is new P [(:::)] with null record; <-- full view
6057
6058 -- No specific constraints on P's discriminants or constraint (:::).
6059 -- Note that A can be unconstrained, but the parent subtype P must either
6060 -- be constrained or (:::) must be present.
6061
6062 -- type D (..) is new A [(...)] with private; <-- partial view
6063 -- type D (..) is new P [(:::)] with null record; <-- full view
6064
6065 -- P's constraints on A's discriminants must statically match those
6066 -- imposed by (...).
6067
6068 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6069
6070 -- The full view of a private extension is handled exactly as described
6071 -- above. The model chose for the private view of a private extension is
6072 -- the same for what concerns discriminants (i.e. they receive the same
6073 -- treatment as in the tagged case). However, the private view of the
6074 -- private extension always inherits the components of the parent base,
6075 -- without replacing any discriminant reference. Strictly speaking this is
6076 -- incorrect. However, Gigi never uses this view to generate code so this
6077 -- is a purely semantic issue. In theory, a set of transformations similar
6078 -- to those given in 5. and 6. above could be applied to private views of
6079 -- private extensions to have the same model of component inheritance as
6080 -- for non private extensions. However, this is not done because it would
6081 -- further complicate private type processing. Semantically speaking, this
6082 -- leaves us in an uncomfortable situation. As an example consider:
6083
6084 -- package Pack is
6085 -- type R (D : integer) is tagged record
6086 -- S : String (1 .. D);
6087 -- end record;
6088 -- procedure P (X : R);
6089 -- type T is new R (1) with private;
6090 -- private
6091 -- type T is new R (1) with null record;
6092 -- end;
6093
6094 -- This is transformed into:
6095
6096 -- package Pack is
6097 -- type R (D : integer) is tagged record
6098 -- S : String (1 .. D);
6099 -- end record;
6100 -- procedure P (X : R);
6101 -- type T is new R (1) with private;
6102 -- private
6103 -- type BaseT is new R with null record;
6104 -- subtype T is BaseT (1);
6105 -- end;
6106
6107 -- (strictly speaking the above is incorrect Ada)
6108
6109 -- From the semantic standpoint the private view of private extension T
6110 -- should be flagged as constrained since one can clearly have
6111 --
6112 -- Obj : T;
6113 --
6114 -- in a unit withing Pack. However, when deriving subprograms for the
6115 -- private view of private extension T, T must be seen as unconstrained
6116 -- since T has discriminants (this is a constraint of the current
6117 -- subprogram derivation model). Thus, when processing the private view of
6118 -- a private extension such as T, we first mark T as unconstrained, we
6119 -- process it, we perform program derivation and just before returning from
6120 -- Build_Derived_Record_Type we mark T as constrained.
6121
6122 -- ??? Are there are other uncomfortable cases that we will have to
6123 -- deal with.
6124
6125 -- 10. RECORD_TYPE_WITH_PRIVATE complications
6126
6127 -- Types that are derived from a visible record type and have a private
6128 -- extension present other peculiarities. They behave mostly like private
6129 -- types, but if they have primitive operations defined, these will not
6130 -- have the proper signatures for further inheritance, because other
6131 -- primitive operations will use the implicit base that we define for
6132 -- private derivations below. This affect subprogram inheritance (see
6133 -- Derive_Subprograms for details). We also derive the implicit base from
6134 -- the base type of the full view, so that the implicit base is a record
6135 -- type and not another private type, This avoids infinite loops.
6136
6137 procedure Build_Derived_Record_Type
6138 (N : Node_Id;
6139 Parent_Type : Entity_Id;
6140 Derived_Type : Entity_Id;
6141 Derive_Subps : Boolean := True)
6142 is
6143 Loc : constant Source_Ptr := Sloc (N);
6144 Parent_Base : Entity_Id;
6145 Type_Def : Node_Id;
6146 Indic : Node_Id;
6147 Discrim : Entity_Id;
6148 Last_Discrim : Entity_Id;
6149 Constrs : Elist_Id;
6150
6151 Discs : Elist_Id := New_Elmt_List;
6152 -- An empty Discs list means that there were no constraints in the
6153 -- subtype indication or that there was an error processing it.
6154
6155 Assoc_List : Elist_Id;
6156 New_Discrs : Elist_Id;
6157 New_Base : Entity_Id;
6158 New_Decl : Node_Id;
6159 New_Indic : Node_Id;
6160
6161 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
6162 Discriminant_Specs : constant Boolean :=
6163 Present (Discriminant_Specifications (N));
6164 Private_Extension : constant Boolean :=
6165 Nkind (N) = N_Private_Extension_Declaration;
6166
6167 Constraint_Present : Boolean;
6168 Inherit_Discrims : Boolean := False;
6169 Save_Etype : Entity_Id;
6170 Save_Discr_Constr : Elist_Id;
6171 Save_Next_Entity : Entity_Id;
6172
6173 begin
6174 if Ekind (Parent_Type) = E_Record_Type_With_Private
6175 and then Present (Full_View (Parent_Type))
6176 and then Has_Discriminants (Parent_Type)
6177 then
6178 Parent_Base := Base_Type (Full_View (Parent_Type));
6179 else
6180 Parent_Base := Base_Type (Parent_Type);
6181 end if;
6182
6183 -- Before we start the previously documented transformations, here is
6184 -- little fix for size and alignment of tagged types. Normally when we
6185 -- derive type D from type P, we copy the size and alignment of P as the
6186 -- default for D, and in the absence of explicit representation clauses
6187 -- for D, the size and alignment are indeed the same as the parent.
6188
6189 -- But this is wrong for tagged types, since fields may be added, and
6190 -- the default size may need to be larger, and the default alignment may
6191 -- need to be larger.
6192
6193 -- We therefore reset the size and alignment fields in the tagged case.
6194 -- Note that the size and alignment will in any case be at least as
6195 -- large as the parent type (since the derived type has a copy of the
6196 -- parent type in the _parent field)
6197
6198 -- The type is also marked as being tagged here, which is needed when
6199 -- processing components with a self-referential anonymous access type
6200 -- in the call to Check_Anonymous_Access_Components below. Note that
6201 -- this flag is also set later on for completeness.
6202
6203 if Is_Tagged then
6204 Set_Is_Tagged_Type (Derived_Type);
6205 Init_Size_Align (Derived_Type);
6206 end if;
6207
6208 -- STEP 0a: figure out what kind of derived type declaration we have
6209
6210 if Private_Extension then
6211 Type_Def := N;
6212 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6213
6214 else
6215 Type_Def := Type_Definition (N);
6216
6217 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
6218 -- Parent_Base can be a private type or private extension. However,
6219 -- for tagged types with an extension the newly added fields are
6220 -- visible and hence the Derived_Type is always an E_Record_Type.
6221 -- (except that the parent may have its own private fields).
6222 -- For untagged types we preserve the Ekind of the Parent_Base.
6223
6224 if Present (Record_Extension_Part (Type_Def)) then
6225 Set_Ekind (Derived_Type, E_Record_Type);
6226
6227 -- Create internal access types for components with anonymous
6228 -- access types.
6229
6230 if Ada_Version >= Ada_05 then
6231 Check_Anonymous_Access_Components
6232 (N, Derived_Type, Derived_Type,
6233 Component_List (Record_Extension_Part (Type_Def)));
6234 end if;
6235
6236 else
6237 Set_Ekind (Derived_Type, Ekind (Parent_Base));
6238 end if;
6239 end if;
6240
6241 -- Indic can either be an N_Identifier if the subtype indication
6242 -- contains no constraint or an N_Subtype_Indication if the subtype
6243 -- indication has a constraint.
6244
6245 Indic := Subtype_Indication (Type_Def);
6246 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6247
6248 -- Check that the type has visible discriminants. The type may be
6249 -- a private type with unknown discriminants whose full view has
6250 -- discriminants which are invisible.
6251
6252 if Constraint_Present then
6253 if not Has_Discriminants (Parent_Base)
6254 or else
6255 (Has_Unknown_Discriminants (Parent_Base)
6256 and then Is_Private_Type (Parent_Base))
6257 then
6258 Error_Msg_N
6259 ("invalid constraint: type has no discriminant",
6260 Constraint (Indic));
6261
6262 Constraint_Present := False;
6263 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6264
6265 elsif Is_Constrained (Parent_Type) then
6266 Error_Msg_N
6267 ("invalid constraint: parent type is already constrained",
6268 Constraint (Indic));
6269
6270 Constraint_Present := False;
6271 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6272 end if;
6273 end if;
6274
6275 -- STEP 0b: If needed, apply transformation given in point 5. above
6276
6277 if not Private_Extension
6278 and then Has_Discriminants (Parent_Type)
6279 and then not Discriminant_Specs
6280 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
6281 then
6282 -- First, we must analyze the constraint (see comment in point 5.)
6283
6284 if Constraint_Present then
6285 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
6286
6287 if Has_Discriminants (Derived_Type)
6288 and then Has_Private_Declaration (Derived_Type)
6289 and then Present (Discriminant_Constraint (Derived_Type))
6290 then
6291 -- Verify that constraints of the full view statically match
6292 -- those given in the partial view.
6293
6294 declare
6295 C1, C2 : Elmt_Id;
6296
6297 begin
6298 C1 := First_Elmt (New_Discrs);
6299 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
6300 while Present (C1) and then Present (C2) loop
6301 if Fully_Conformant_Expressions (Node (C1), Node (C2))
6302 or else
6303 (Is_OK_Static_Expression (Node (C1))
6304 and then
6305 Is_OK_Static_Expression (Node (C2))
6306 and then
6307 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
6308 then
6309 null;
6310
6311 else
6312 Error_Msg_N (
6313 "constraint not conformant to previous declaration",
6314 Node (C1));
6315 end if;
6316
6317 Next_Elmt (C1);
6318 Next_Elmt (C2);
6319 end loop;
6320 end;
6321 end if;
6322 end if;
6323
6324 -- Insert and analyze the declaration for the unconstrained base type
6325
6326 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6327
6328 New_Decl :=
6329 Make_Full_Type_Declaration (Loc,
6330 Defining_Identifier => New_Base,
6331 Type_Definition =>
6332 Make_Derived_Type_Definition (Loc,
6333 Abstract_Present => Abstract_Present (Type_Def),
6334 Subtype_Indication =>
6335 New_Occurrence_Of (Parent_Base, Loc),
6336 Record_Extension_Part =>
6337 Relocate_Node (Record_Extension_Part (Type_Def))));
6338
6339 Set_Parent (New_Decl, Parent (N));
6340 Mark_Rewrite_Insertion (New_Decl);
6341 Insert_Before (N, New_Decl);
6342
6343 -- Note that this call passes False for the Derive_Subps parameter
6344 -- because subprogram derivation is deferred until after creating
6345 -- the subtype (see below).
6346
6347 Build_Derived_Type
6348 (New_Decl, Parent_Base, New_Base,
6349 Is_Completion => True, Derive_Subps => False);
6350
6351 -- ??? This needs re-examination to determine whether the
6352 -- above call can simply be replaced by a call to Analyze.
6353
6354 Set_Analyzed (New_Decl);
6355
6356 -- Insert and analyze the declaration for the constrained subtype
6357
6358 if Constraint_Present then
6359 New_Indic :=
6360 Make_Subtype_Indication (Loc,
6361 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6362 Constraint => Relocate_Node (Constraint (Indic)));
6363
6364 else
6365 declare
6366 Constr_List : constant List_Id := New_List;
6367 C : Elmt_Id;
6368 Expr : Node_Id;
6369
6370 begin
6371 C := First_Elmt (Discriminant_Constraint (Parent_Type));
6372 while Present (C) loop
6373 Expr := Node (C);
6374
6375 -- It is safe here to call New_Copy_Tree since
6376 -- Force_Evaluation was called on each constraint in
6377 -- Build_Discriminant_Constraints.
6378
6379 Append (New_Copy_Tree (Expr), To => Constr_List);
6380
6381 Next_Elmt (C);
6382 end loop;
6383
6384 New_Indic :=
6385 Make_Subtype_Indication (Loc,
6386 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6387 Constraint =>
6388 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
6389 end;
6390 end if;
6391
6392 Rewrite (N,
6393 Make_Subtype_Declaration (Loc,
6394 Defining_Identifier => Derived_Type,
6395 Subtype_Indication => New_Indic));
6396
6397 Analyze (N);
6398
6399 -- Derivation of subprograms must be delayed until the full subtype
6400 -- has been established to ensure proper overriding of subprograms
6401 -- inherited by full types. If the derivations occurred as part of
6402 -- the call to Build_Derived_Type above, then the check for type
6403 -- conformance would fail because earlier primitive subprograms
6404 -- could still refer to the full type prior the change to the new
6405 -- subtype and hence would not match the new base type created here.
6406
6407 Derive_Subprograms (Parent_Type, Derived_Type);
6408
6409 -- For tagged types the Discriminant_Constraint of the new base itype
6410 -- is inherited from the first subtype so that no subtype conformance
6411 -- problem arise when the first subtype overrides primitive
6412 -- operations inherited by the implicit base type.
6413
6414 if Is_Tagged then
6415 Set_Discriminant_Constraint
6416 (New_Base, Discriminant_Constraint (Derived_Type));
6417 end if;
6418
6419 return;
6420 end if;
6421
6422 -- If we get here Derived_Type will have no discriminants or it will be
6423 -- a discriminated unconstrained base type.
6424
6425 -- STEP 1a: perform preliminary actions/checks for derived tagged types
6426
6427 if Is_Tagged then
6428
6429 -- The parent type is frozen for non-private extensions (RM 13.14(7))
6430 -- The declaration of a specific descendant of an interface type
6431 -- freezes the interface type (RM 13.14).
6432
6433 if not Private_Extension
6434 or else Is_Interface (Parent_Base)
6435 then
6436 Freeze_Before (N, Parent_Type);
6437 end if;
6438
6439 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
6440 -- cannot be declared at a deeper level than its parent type is
6441 -- removed. The check on derivation within a generic body is also
6442 -- relaxed, but there's a restriction that a derived tagged type
6443 -- cannot be declared in a generic body if it's derived directly
6444 -- or indirectly from a formal type of that generic.
6445
6446 if Ada_Version >= Ada_05 then
6447 if Present (Enclosing_Generic_Body (Derived_Type)) then
6448 declare
6449 Ancestor_Type : Entity_Id;
6450
6451 begin
6452 -- Check to see if any ancestor of the derived type is a
6453 -- formal type.
6454
6455 Ancestor_Type := Parent_Type;
6456 while not Is_Generic_Type (Ancestor_Type)
6457 and then Etype (Ancestor_Type) /= Ancestor_Type
6458 loop
6459 Ancestor_Type := Etype (Ancestor_Type);
6460 end loop;
6461
6462 -- If the derived type does have a formal type as an
6463 -- ancestor, then it's an error if the derived type is
6464 -- declared within the body of the generic unit that
6465 -- declares the formal type in its generic formal part. It's
6466 -- sufficient to check whether the ancestor type is declared
6467 -- inside the same generic body as the derived type (such as
6468 -- within a nested generic spec), in which case the
6469 -- derivation is legal. If the formal type is declared
6470 -- outside of that generic body, then it's guaranteed that
6471 -- the derived type is declared within the generic body of
6472 -- the generic unit declaring the formal type.
6473
6474 if Is_Generic_Type (Ancestor_Type)
6475 and then Enclosing_Generic_Body (Ancestor_Type) /=
6476 Enclosing_Generic_Body (Derived_Type)
6477 then
6478 Error_Msg_NE
6479 ("parent type of& must not be descendant of formal type"
6480 & " of an enclosing generic body",
6481 Indic, Derived_Type);
6482 end if;
6483 end;
6484 end if;
6485
6486 elsif Type_Access_Level (Derived_Type) /=
6487 Type_Access_Level (Parent_Type)
6488 and then not Is_Generic_Type (Derived_Type)
6489 then
6490 if Is_Controlled (Parent_Type) then
6491 Error_Msg_N
6492 ("controlled type must be declared at the library level",
6493 Indic);
6494 else
6495 Error_Msg_N
6496 ("type extension at deeper accessibility level than parent",
6497 Indic);
6498 end if;
6499
6500 else
6501 declare
6502 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
6503
6504 begin
6505 if Present (GB)
6506 and then GB /= Enclosing_Generic_Body (Parent_Base)
6507 then
6508 Error_Msg_NE
6509 ("parent type of& must not be outside generic body"
6510 & " (RM 3.9.1(4))",
6511 Indic, Derived_Type);
6512 end if;
6513 end;
6514 end if;
6515 end if;
6516
6517 -- Ada 2005 (AI-251)
6518
6519 if Ada_Version = Ada_05
6520 and then Is_Tagged
6521 then
6522 -- "The declaration of a specific descendant of an interface type
6523 -- freezes the interface type" (RM 13.14).
6524
6525 declare
6526 Iface : Node_Id;
6527 begin
6528 if Is_Non_Empty_List (Interface_List (Type_Def)) then
6529 Iface := First (Interface_List (Type_Def));
6530 while Present (Iface) loop
6531 Freeze_Before (N, Etype (Iface));
6532 Next (Iface);
6533 end loop;
6534 end if;
6535 end;
6536 end if;
6537
6538 -- STEP 1b : preliminary cleanup of the full view of private types
6539
6540 -- If the type is already marked as having discriminants, then it's the
6541 -- completion of a private type or private extension and we need to
6542 -- retain the discriminants from the partial view if the current
6543 -- declaration has Discriminant_Specifications so that we can verify
6544 -- conformance. However, we must remove any existing components that
6545 -- were inherited from the parent (and attached in Copy_And_Swap)
6546 -- because the full type inherits all appropriate components anyway, and
6547 -- we do not want the partial view's components interfering.
6548
6549 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
6550 Discrim := First_Discriminant (Derived_Type);
6551 loop
6552 Last_Discrim := Discrim;
6553 Next_Discriminant (Discrim);
6554 exit when No (Discrim);
6555 end loop;
6556
6557 Set_Last_Entity (Derived_Type, Last_Discrim);
6558
6559 -- In all other cases wipe out the list of inherited components (even
6560 -- inherited discriminants), it will be properly rebuilt here.
6561
6562 else
6563 Set_First_Entity (Derived_Type, Empty);
6564 Set_Last_Entity (Derived_Type, Empty);
6565 end if;
6566
6567 -- STEP 1c: Initialize some flags for the Derived_Type
6568
6569 -- The following flags must be initialized here so that
6570 -- Process_Discriminants can check that discriminants of tagged types do
6571 -- not have a default initial value and that access discriminants are
6572 -- only specified for limited records. For completeness, these flags are
6573 -- also initialized along with all the other flags below.
6574
6575 -- AI-419: Limitedness is not inherited from an interface parent, so to
6576 -- be limited in that case the type must be explicitly declared as
6577 -- limited. However, task and protected interfaces are always limited.
6578
6579 if Limited_Present (Type_Def) then
6580 Set_Is_Limited_Record (Derived_Type);
6581
6582 elsif Is_Limited_Record (Parent_Type)
6583 or else (Present (Full_View (Parent_Type))
6584 and then Is_Limited_Record (Full_View (Parent_Type)))
6585 then
6586 if not Is_Interface (Parent_Type)
6587 or else Is_Synchronized_Interface (Parent_Type)
6588 or else Is_Protected_Interface (Parent_Type)
6589 or else Is_Task_Interface (Parent_Type)
6590 then
6591 Set_Is_Limited_Record (Derived_Type);
6592 end if;
6593 end if;
6594
6595 -- STEP 2a: process discriminants of derived type if any
6596
6597 Push_Scope (Derived_Type);
6598
6599 if Discriminant_Specs then
6600 Set_Has_Unknown_Discriminants (Derived_Type, False);
6601
6602 -- The following call initializes fields Has_Discriminants and
6603 -- Discriminant_Constraint, unless we are processing the completion
6604 -- of a private type declaration.
6605
6606 Check_Or_Process_Discriminants (N, Derived_Type);
6607
6608 -- For non-tagged types the constraint on the Parent_Type must be
6609 -- present and is used to rename the discriminants.
6610
6611 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
6612 Error_Msg_N ("untagged parent must have discriminants", Indic);
6613
6614 elsif not Is_Tagged and then not Constraint_Present then
6615 Error_Msg_N
6616 ("discriminant constraint needed for derived untagged records",
6617 Indic);
6618
6619 -- Otherwise the parent subtype must be constrained unless we have a
6620 -- private extension.
6621
6622 elsif not Constraint_Present
6623 and then not Private_Extension
6624 and then not Is_Constrained (Parent_Type)
6625 then
6626 Error_Msg_N
6627 ("unconstrained type not allowed in this context", Indic);
6628
6629 elsif Constraint_Present then
6630 -- The following call sets the field Corresponding_Discriminant
6631 -- for the discriminants in the Derived_Type.
6632
6633 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
6634
6635 -- For untagged types all new discriminants must rename
6636 -- discriminants in the parent. For private extensions new
6637 -- discriminants cannot rename old ones (implied by [7.3(13)]).
6638
6639 Discrim := First_Discriminant (Derived_Type);
6640 while Present (Discrim) loop
6641 if not Is_Tagged
6642 and then No (Corresponding_Discriminant (Discrim))
6643 then
6644 Error_Msg_N
6645 ("new discriminants must constrain old ones", Discrim);
6646
6647 elsif Private_Extension
6648 and then Present (Corresponding_Discriminant (Discrim))
6649 then
6650 Error_Msg_N
6651 ("only static constraints allowed for parent"
6652 & " discriminants in the partial view", Indic);
6653 exit;
6654 end if;
6655
6656 -- If a new discriminant is used in the constraint, then its
6657 -- subtype must be statically compatible with the parent
6658 -- discriminant's subtype (3.7(15)).
6659
6660 if Present (Corresponding_Discriminant (Discrim))
6661 and then
6662 not Subtypes_Statically_Compatible
6663 (Etype (Discrim),
6664 Etype (Corresponding_Discriminant (Discrim)))
6665 then
6666 Error_Msg_N
6667 ("subtype must be compatible with parent discriminant",
6668 Discrim);
6669 end if;
6670
6671 Next_Discriminant (Discrim);
6672 end loop;
6673
6674 -- Check whether the constraints of the full view statically
6675 -- match those imposed by the parent subtype [7.3(13)].
6676
6677 if Present (Stored_Constraint (Derived_Type)) then
6678 declare
6679 C1, C2 : Elmt_Id;
6680
6681 begin
6682 C1 := First_Elmt (Discs);
6683 C2 := First_Elmt (Stored_Constraint (Derived_Type));
6684 while Present (C1) and then Present (C2) loop
6685 if not
6686 Fully_Conformant_Expressions (Node (C1), Node (C2))
6687 then
6688 Error_Msg_N
6689 ("not conformant with previous declaration",
6690 Node (C1));
6691 end if;
6692
6693 Next_Elmt (C1);
6694 Next_Elmt (C2);
6695 end loop;
6696 end;
6697 end if;
6698 end if;
6699
6700 -- STEP 2b: No new discriminants, inherit discriminants if any
6701
6702 else
6703 if Private_Extension then
6704 Set_Has_Unknown_Discriminants
6705 (Derived_Type,
6706 Has_Unknown_Discriminants (Parent_Type)
6707 or else Unknown_Discriminants_Present (N));
6708
6709 -- The partial view of the parent may have unknown discriminants,
6710 -- but if the full view has discriminants and the parent type is
6711 -- in scope they must be inherited.
6712
6713 elsif Has_Unknown_Discriminants (Parent_Type)
6714 and then
6715 (not Has_Discriminants (Parent_Type)
6716 or else not In_Open_Scopes (Scope (Parent_Type)))
6717 then
6718 Set_Has_Unknown_Discriminants (Derived_Type);
6719 end if;
6720
6721 if not Has_Unknown_Discriminants (Derived_Type)
6722 and then not Has_Unknown_Discriminants (Parent_Base)
6723 and then Has_Discriminants (Parent_Type)
6724 then
6725 Inherit_Discrims := True;
6726 Set_Has_Discriminants
6727 (Derived_Type, True);
6728 Set_Discriminant_Constraint
6729 (Derived_Type, Discriminant_Constraint (Parent_Base));
6730 end if;
6731
6732 -- The following test is true for private types (remember
6733 -- transformation 5. is not applied to those) and in an error
6734 -- situation.
6735
6736 if Constraint_Present then
6737 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
6738 end if;
6739
6740 -- For now mark a new derived type as constrained only if it has no
6741 -- discriminants. At the end of Build_Derived_Record_Type we properly
6742 -- set this flag in the case of private extensions. See comments in
6743 -- point 9. just before body of Build_Derived_Record_Type.
6744
6745 Set_Is_Constrained
6746 (Derived_Type,
6747 not (Inherit_Discrims
6748 or else Has_Unknown_Discriminants (Derived_Type)));
6749 end if;
6750
6751 -- STEP 3: initialize fields of derived type
6752
6753 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
6754 Set_Stored_Constraint (Derived_Type, No_Elist);
6755
6756 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
6757 -- but cannot be interfaces
6758
6759 if not Private_Extension
6760 and then Ekind (Derived_Type) /= E_Private_Type
6761 and then Ekind (Derived_Type) /= E_Limited_Private_Type
6762 then
6763 if Interface_Present (Type_Def) then
6764 Analyze_Interface_Declaration (Derived_Type, Type_Def);
6765 end if;
6766
6767 Set_Interfaces (Derived_Type, No_Elist);
6768 end if;
6769
6770 -- Fields inherited from the Parent_Type
6771
6772 Set_Discard_Names
6773 (Derived_Type, Einfo.Discard_Names (Parent_Type));
6774 Set_Has_Specified_Layout
6775 (Derived_Type, Has_Specified_Layout (Parent_Type));
6776 Set_Is_Limited_Composite
6777 (Derived_Type, Is_Limited_Composite (Parent_Type));
6778 Set_Is_Private_Composite
6779 (Derived_Type, Is_Private_Composite (Parent_Type));
6780
6781 -- Fields inherited from the Parent_Base
6782
6783 Set_Has_Controlled_Component
6784 (Derived_Type, Has_Controlled_Component (Parent_Base));
6785 Set_Has_Non_Standard_Rep
6786 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
6787 Set_Has_Primitive_Operations
6788 (Derived_Type, Has_Primitive_Operations (Parent_Base));
6789
6790 -- Fields inherited from the Parent_Base in the non-private case
6791
6792 if Ekind (Derived_Type) = E_Record_Type then
6793 Set_Has_Complex_Representation
6794 (Derived_Type, Has_Complex_Representation (Parent_Base));
6795 end if;
6796
6797 -- Fields inherited from the Parent_Base for record types
6798
6799 if Is_Record_Type (Derived_Type) then
6800 Set_OK_To_Reorder_Components
6801 (Derived_Type, OK_To_Reorder_Components (Parent_Base));
6802 Set_Reverse_Bit_Order
6803 (Derived_Type, Reverse_Bit_Order (Parent_Base));
6804 end if;
6805
6806 -- Direct controlled types do not inherit Finalize_Storage_Only flag
6807
6808 if not Is_Controlled (Parent_Type) then
6809 Set_Finalize_Storage_Only
6810 (Derived_Type, Finalize_Storage_Only (Parent_Type));
6811 end if;
6812
6813 -- Set fields for private derived types
6814
6815 if Is_Private_Type (Derived_Type) then
6816 Set_Depends_On_Private (Derived_Type, True);
6817 Set_Private_Dependents (Derived_Type, New_Elmt_List);
6818
6819 -- Inherit fields from non private record types. If this is the
6820 -- completion of a derivation from a private type, the parent itself
6821 -- is private, and the attributes come from its full view, which must
6822 -- be present.
6823
6824 else
6825 if Is_Private_Type (Parent_Base)
6826 and then not Is_Record_Type (Parent_Base)
6827 then
6828 Set_Component_Alignment
6829 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
6830 Set_C_Pass_By_Copy
6831 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
6832 else
6833 Set_Component_Alignment
6834 (Derived_Type, Component_Alignment (Parent_Base));
6835
6836 Set_C_Pass_By_Copy
6837 (Derived_Type, C_Pass_By_Copy (Parent_Base));
6838 end if;
6839 end if;
6840
6841 -- Set fields for tagged types
6842
6843 if Is_Tagged then
6844 Set_Primitive_Operations (Derived_Type, New_Elmt_List);
6845
6846 -- All tagged types defined in Ada.Finalization are controlled
6847
6848 if Chars (Scope (Derived_Type)) = Name_Finalization
6849 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
6850 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
6851 then
6852 Set_Is_Controlled (Derived_Type);
6853 else
6854 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
6855 end if;
6856
6857 Make_Class_Wide_Type (Derived_Type);
6858 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
6859
6860 if Has_Discriminants (Derived_Type)
6861 and then Constraint_Present
6862 then
6863 Set_Stored_Constraint
6864 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
6865 end if;
6866
6867 if Ada_Version >= Ada_05 then
6868 declare
6869 Ifaces_List : Elist_Id;
6870
6871 begin
6872 -- Checks rules 3.9.4 (13/2 and 14/2)
6873
6874 if Comes_From_Source (Derived_Type)
6875 and then not Is_Private_Type (Derived_Type)
6876 and then Is_Interface (Parent_Type)
6877 and then not Is_Interface (Derived_Type)
6878 then
6879 if Is_Task_Interface (Parent_Type) then
6880 Error_Msg_N
6881 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
6882 Derived_Type);
6883
6884 elsif Is_Protected_Interface (Parent_Type) then
6885 Error_Msg_N
6886 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
6887 Derived_Type);
6888 end if;
6889 end if;
6890
6891 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
6892
6893 Check_Interfaces (N, Type_Def);
6894
6895 -- Ada 2005 (AI-251): Collect the list of progenitors that are
6896 -- not already in the parents.
6897
6898 Collect_Interfaces
6899 (T => Derived_Type,
6900 Ifaces_List => Ifaces_List,
6901 Exclude_Parents => True);
6902
6903 Set_Interfaces (Derived_Type, Ifaces_List);
6904 end;
6905 end if;
6906
6907 else
6908 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
6909 Set_Has_Non_Standard_Rep
6910 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
6911 end if;
6912
6913 -- STEP 4: Inherit components from the parent base and constrain them.
6914 -- Apply the second transformation described in point 6. above.
6915
6916 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
6917 or else not Has_Discriminants (Parent_Type)
6918 or else not Is_Constrained (Parent_Type)
6919 then
6920 Constrs := Discs;
6921 else
6922 Constrs := Discriminant_Constraint (Parent_Type);
6923 end if;
6924
6925 Assoc_List :=
6926 Inherit_Components
6927 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
6928
6929 -- STEP 5a: Copy the parent record declaration for untagged types
6930
6931 if not Is_Tagged then
6932
6933 -- Discriminant_Constraint (Derived_Type) has been properly
6934 -- constructed. Save it and temporarily set it to Empty because we
6935 -- do not want the call to New_Copy_Tree below to mess this list.
6936
6937 if Has_Discriminants (Derived_Type) then
6938 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
6939 Set_Discriminant_Constraint (Derived_Type, No_Elist);
6940 else
6941 Save_Discr_Constr := No_Elist;
6942 end if;
6943
6944 -- Save the Etype field of Derived_Type. It is correctly set now,
6945 -- but the call to New_Copy tree may remap it to point to itself,
6946 -- which is not what we want. Ditto for the Next_Entity field.
6947
6948 Save_Etype := Etype (Derived_Type);
6949 Save_Next_Entity := Next_Entity (Derived_Type);
6950
6951 -- Assoc_List maps all stored discriminants in the Parent_Base to
6952 -- stored discriminants in the Derived_Type. It is fundamental that
6953 -- no types or itypes with discriminants other than the stored
6954 -- discriminants appear in the entities declared inside
6955 -- Derived_Type, since the back end cannot deal with it.
6956
6957 New_Decl :=
6958 New_Copy_Tree
6959 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
6960
6961 -- Restore the fields saved prior to the New_Copy_Tree call
6962 -- and compute the stored constraint.
6963
6964 Set_Etype (Derived_Type, Save_Etype);
6965 Set_Next_Entity (Derived_Type, Save_Next_Entity);
6966
6967 if Has_Discriminants (Derived_Type) then
6968 Set_Discriminant_Constraint
6969 (Derived_Type, Save_Discr_Constr);
6970 Set_Stored_Constraint
6971 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
6972 Replace_Components (Derived_Type, New_Decl);
6973 end if;
6974
6975 -- Insert the new derived type declaration
6976
6977 Rewrite (N, New_Decl);
6978
6979 -- STEP 5b: Complete the processing for record extensions in generics
6980
6981 -- There is no completion for record extensions declared in the
6982 -- parameter part of a generic, so we need to complete processing for
6983 -- these generic record extensions here. The Record_Type_Definition call
6984 -- will change the Ekind of the components from E_Void to E_Component.
6985
6986 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
6987 Record_Type_Definition (Empty, Derived_Type);
6988
6989 -- STEP 5c: Process the record extension for non private tagged types
6990
6991 elsif not Private_Extension then
6992
6993 -- Add the _parent field in the derived type
6994
6995 Expand_Record_Extension (Derived_Type, Type_Def);
6996
6997 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
6998 -- implemented interfaces if we are in expansion mode
6999
7000 if Expander_Active
7001 and then Has_Interfaces (Derived_Type)
7002 then
7003 Add_Interface_Tag_Components (N, Derived_Type);
7004 end if;
7005
7006 -- Analyze the record extension
7007
7008 Record_Type_Definition
7009 (Record_Extension_Part (Type_Def), Derived_Type);
7010 end if;
7011
7012 End_Scope;
7013
7014 -- Nothing else to do if there is an error in the derivation.
7015 -- An unusual case: the full view may be derived from a type in an
7016 -- instance, when the partial view was used illegally as an actual
7017 -- in that instance, leading to a circular definition.
7018
7019 if Etype (Derived_Type) = Any_Type
7020 or else Etype (Parent_Type) = Derived_Type
7021 then
7022 return;
7023 end if;
7024
7025 -- Set delayed freeze and then derive subprograms, we need to do
7026 -- this in this order so that derived subprograms inherit the
7027 -- derived freeze if necessary.
7028
7029 Set_Has_Delayed_Freeze (Derived_Type);
7030
7031 if Derive_Subps then
7032 Derive_Subprograms (Parent_Type, Derived_Type);
7033 end if;
7034
7035 -- If we have a private extension which defines a constrained derived
7036 -- type mark as constrained here after we have derived subprograms. See
7037 -- comment on point 9. just above the body of Build_Derived_Record_Type.
7038
7039 if Private_Extension and then Inherit_Discrims then
7040 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7041 Set_Is_Constrained (Derived_Type, True);
7042 Set_Discriminant_Constraint (Derived_Type, Discs);
7043
7044 elsif Is_Constrained (Parent_Type) then
7045 Set_Is_Constrained
7046 (Derived_Type, True);
7047 Set_Discriminant_Constraint
7048 (Derived_Type, Discriminant_Constraint (Parent_Type));
7049 end if;
7050 end if;
7051
7052 -- Update the class_wide type, which shares the now-completed
7053 -- entity list with its specific type.
7054
7055 if Is_Tagged then
7056 Set_First_Entity
7057 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7058 Set_Last_Entity
7059 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7060 end if;
7061
7062 -- Update the scope of anonymous access types of discriminants and other
7063 -- components, to prevent scope anomalies in gigi, when the derivation
7064 -- appears in a scope nested within that of the parent.
7065
7066 declare
7067 D : Entity_Id;
7068
7069 begin
7070 D := First_Entity (Derived_Type);
7071 while Present (D) loop
7072 if Ekind (D) = E_Discriminant
7073 or else Ekind (D) = E_Component
7074 then
7075 if Is_Itype (Etype (D))
7076 and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7077 then
7078 Set_Scope (Etype (D), Current_Scope);
7079 end if;
7080 end if;
7081
7082 Next_Entity (D);
7083 end loop;
7084 end;
7085 end Build_Derived_Record_Type;
7086
7087 ------------------------
7088 -- Build_Derived_Type --
7089 ------------------------
7090
7091 procedure Build_Derived_Type
7092 (N : Node_Id;
7093 Parent_Type : Entity_Id;
7094 Derived_Type : Entity_Id;
7095 Is_Completion : Boolean;
7096 Derive_Subps : Boolean := True)
7097 is
7098 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7099
7100 begin
7101 -- Set common attributes
7102
7103 Set_Scope (Derived_Type, Current_Scope);
7104
7105 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7106 Set_Etype (Derived_Type, Parent_Base);
7107 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
7108
7109 Set_Size_Info (Derived_Type, Parent_Type);
7110 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
7111 Set_Convention (Derived_Type, Convention (Parent_Type));
7112 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7113
7114 -- The derived type inherits the representation clauses of the parent.
7115 -- However, for a private type that is completed by a derivation, there
7116 -- may be operation attributes that have been specified already (stream
7117 -- attributes and External_Tag) and those must be provided. Finally,
7118 -- if the partial view is a private extension, the representation items
7119 -- of the parent have been inherited already, and should not be chained
7120 -- twice to the derived type.
7121
7122 if Is_Tagged_Type (Parent_Type)
7123 and then Present (First_Rep_Item (Derived_Type))
7124 then
7125 -- The existing items are either operational items or items inherited
7126 -- from a private extension declaration.
7127
7128 declare
7129 Rep : Node_Id;
7130 -- Used to iterate over representation items of the derived type
7131
7132 Last_Rep : Node_Id;
7133 -- Last representation item of the (non-empty) representation
7134 -- item list of the derived type.
7135
7136 Found : Boolean := False;
7137
7138 begin
7139 Rep := First_Rep_Item (Derived_Type);
7140 Last_Rep := Rep;
7141 while Present (Rep) loop
7142 if Rep = First_Rep_Item (Parent_Type) then
7143 Found := True;
7144 exit;
7145
7146 else
7147 Rep := Next_Rep_Item (Rep);
7148
7149 if Present (Rep) then
7150 Last_Rep := Rep;
7151 end if;
7152 end if;
7153 end loop;
7154
7155 -- Here if we either encountered the parent type's first rep
7156 -- item on the derived type's rep item list (in which case
7157 -- Found is True, and we have nothing else to do), or if we
7158 -- reached the last rep item of the derived type, which is
7159 -- Last_Rep, in which case we further chain the parent type's
7160 -- rep items to those of the derived type.
7161
7162 if not Found then
7163 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
7164 end if;
7165 end;
7166
7167 else
7168 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7169 end if;
7170
7171 case Ekind (Parent_Type) is
7172 when Numeric_Kind =>
7173 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7174
7175 when Array_Kind =>
7176 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
7177
7178 when E_Record_Type
7179 | E_Record_Subtype
7180 | Class_Wide_Kind =>
7181 Build_Derived_Record_Type
7182 (N, Parent_Type, Derived_Type, Derive_Subps);
7183 return;
7184
7185 when Enumeration_Kind =>
7186 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7187
7188 when Access_Kind =>
7189 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7190
7191 when Incomplete_Or_Private_Kind =>
7192 Build_Derived_Private_Type
7193 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
7194
7195 -- For discriminated types, the derivation includes deriving
7196 -- primitive operations. For others it is done below.
7197
7198 if Is_Tagged_Type (Parent_Type)
7199 or else Has_Discriminants (Parent_Type)
7200 or else (Present (Full_View (Parent_Type))
7201 and then Has_Discriminants (Full_View (Parent_Type)))
7202 then
7203 return;
7204 end if;
7205
7206 when Concurrent_Kind =>
7207 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
7208
7209 when others =>
7210 raise Program_Error;
7211 end case;
7212
7213 if Etype (Derived_Type) = Any_Type then
7214 return;
7215 end if;
7216
7217 -- Set delayed freeze and then derive subprograms, we need to do this
7218 -- in this order so that derived subprograms inherit the derived freeze
7219 -- if necessary.
7220
7221 Set_Has_Delayed_Freeze (Derived_Type);
7222 if Derive_Subps then
7223 Derive_Subprograms (Parent_Type, Derived_Type);
7224 end if;
7225
7226 Set_Has_Primitive_Operations
7227 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
7228 end Build_Derived_Type;
7229
7230 -----------------------
7231 -- Build_Discriminal --
7232 -----------------------
7233
7234 procedure Build_Discriminal (Discrim : Entity_Id) is
7235 D_Minal : Entity_Id;
7236 CR_Disc : Entity_Id;
7237
7238 begin
7239 -- A discriminal has the same name as the discriminant
7240
7241 D_Minal :=
7242 Make_Defining_Identifier (Sloc (Discrim),
7243 Chars => Chars (Discrim));
7244
7245 Set_Ekind (D_Minal, E_In_Parameter);
7246 Set_Mechanism (D_Minal, Default_Mechanism);
7247 Set_Etype (D_Minal, Etype (Discrim));
7248
7249 Set_Discriminal (Discrim, D_Minal);
7250 Set_Discriminal_Link (D_Minal, Discrim);
7251
7252 -- For task types, build at once the discriminants of the corresponding
7253 -- record, which are needed if discriminants are used in entry defaults
7254 -- and in family bounds.
7255
7256 if Is_Concurrent_Type (Current_Scope)
7257 or else Is_Limited_Type (Current_Scope)
7258 then
7259 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
7260
7261 Set_Ekind (CR_Disc, E_In_Parameter);
7262 Set_Mechanism (CR_Disc, Default_Mechanism);
7263 Set_Etype (CR_Disc, Etype (Discrim));
7264 Set_Discriminal_Link (CR_Disc, Discrim);
7265 Set_CR_Discriminant (Discrim, CR_Disc);
7266 end if;
7267 end Build_Discriminal;
7268
7269 ------------------------------------
7270 -- Build_Discriminant_Constraints --
7271 ------------------------------------
7272
7273 function Build_Discriminant_Constraints
7274 (T : Entity_Id;
7275 Def : Node_Id;
7276 Derived_Def : Boolean := False) return Elist_Id
7277 is
7278 C : constant Node_Id := Constraint (Def);
7279 Nb_Discr : constant Nat := Number_Discriminants (T);
7280
7281 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
7282 -- Saves the expression corresponding to a given discriminant in T
7283
7284 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
7285 -- Return the Position number within array Discr_Expr of a discriminant
7286 -- D within the discriminant list of the discriminated type T.
7287
7288 ------------------
7289 -- Pos_Of_Discr --
7290 ------------------
7291
7292 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
7293 Disc : Entity_Id;
7294
7295 begin
7296 Disc := First_Discriminant (T);
7297 for J in Discr_Expr'Range loop
7298 if Disc = D then
7299 return J;
7300 end if;
7301
7302 Next_Discriminant (Disc);
7303 end loop;
7304
7305 -- Note: Since this function is called on discriminants that are
7306 -- known to belong to the discriminated type, falling through the
7307 -- loop with no match signals an internal compiler error.
7308
7309 raise Program_Error;
7310 end Pos_Of_Discr;
7311
7312 -- Declarations local to Build_Discriminant_Constraints
7313
7314 Discr : Entity_Id;
7315 E : Entity_Id;
7316 Elist : constant Elist_Id := New_Elmt_List;
7317
7318 Constr : Node_Id;
7319 Expr : Node_Id;
7320 Id : Node_Id;
7321 Position : Nat;
7322 Found : Boolean;
7323
7324 Discrim_Present : Boolean := False;
7325
7326 -- Start of processing for Build_Discriminant_Constraints
7327
7328 begin
7329 -- The following loop will process positional associations only.
7330 -- For a positional association, the (single) discriminant is
7331 -- implicitly specified by position, in textual order (RM 3.7.2).
7332
7333 Discr := First_Discriminant (T);
7334 Constr := First (Constraints (C));
7335 for D in Discr_Expr'Range loop
7336 exit when Nkind (Constr) = N_Discriminant_Association;
7337
7338 if No (Constr) then
7339 Error_Msg_N ("too few discriminants given in constraint", C);
7340 return New_Elmt_List;
7341
7342 elsif Nkind (Constr) = N_Range
7343 or else (Nkind (Constr) = N_Attribute_Reference
7344 and then
7345 Attribute_Name (Constr) = Name_Range)
7346 then
7347 Error_Msg_N
7348 ("a range is not a valid discriminant constraint", Constr);
7349 Discr_Expr (D) := Error;
7350
7351 else
7352 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
7353 Discr_Expr (D) := Constr;
7354 end if;
7355
7356 Next_Discriminant (Discr);
7357 Next (Constr);
7358 end loop;
7359
7360 if No (Discr) and then Present (Constr) then
7361 Error_Msg_N ("too many discriminants given in constraint", Constr);
7362 return New_Elmt_List;
7363 end if;
7364
7365 -- Named associations can be given in any order, but if both positional
7366 -- and named associations are used in the same discriminant constraint,
7367 -- then positional associations must occur first, at their normal
7368 -- position. Hence once a named association is used, the rest of the
7369 -- discriminant constraint must use only named associations.
7370
7371 while Present (Constr) loop
7372
7373 -- Positional association forbidden after a named association
7374
7375 if Nkind (Constr) /= N_Discriminant_Association then
7376 Error_Msg_N ("positional association follows named one", Constr);
7377 return New_Elmt_List;
7378
7379 -- Otherwise it is a named association
7380
7381 else
7382 -- E records the type of the discriminants in the named
7383 -- association. All the discriminants specified in the same name
7384 -- association must have the same type.
7385
7386 E := Empty;
7387
7388 -- Search the list of discriminants in T to see if the simple name
7389 -- given in the constraint matches any of them.
7390
7391 Id := First (Selector_Names (Constr));
7392 while Present (Id) loop
7393 Found := False;
7394
7395 -- If Original_Discriminant is present, we are processing a
7396 -- generic instantiation and this is an instance node. We need
7397 -- to find the name of the corresponding discriminant in the
7398 -- actual record type T and not the name of the discriminant in
7399 -- the generic formal. Example:
7400
7401 -- generic
7402 -- type G (D : int) is private;
7403 -- package P is
7404 -- subtype W is G (D => 1);
7405 -- end package;
7406 -- type Rec (X : int) is record ... end record;
7407 -- package Q is new P (G => Rec);
7408
7409 -- At the point of the instantiation, formal type G is Rec
7410 -- and therefore when reanalyzing "subtype W is G (D => 1);"
7411 -- which really looks like "subtype W is Rec (D => 1);" at
7412 -- the point of instantiation, we want to find the discriminant
7413 -- that corresponds to D in Rec, i.e. X.
7414
7415 if Present (Original_Discriminant (Id)) then
7416 Discr := Find_Corresponding_Discriminant (Id, T);
7417 Found := True;
7418
7419 else
7420 Discr := First_Discriminant (T);
7421 while Present (Discr) loop
7422 if Chars (Discr) = Chars (Id) then
7423 Found := True;
7424 exit;
7425 end if;
7426
7427 Next_Discriminant (Discr);
7428 end loop;
7429
7430 if not Found then
7431 Error_Msg_N ("& does not match any discriminant", Id);
7432 return New_Elmt_List;
7433
7434 -- The following is only useful for the benefit of generic
7435 -- instances but it does not interfere with other
7436 -- processing for the non-generic case so we do it in all
7437 -- cases (for generics this statement is executed when
7438 -- processing the generic definition, see comment at the
7439 -- beginning of this if statement).
7440
7441 else
7442 Set_Original_Discriminant (Id, Discr);
7443 end if;
7444 end if;
7445
7446 Position := Pos_Of_Discr (T, Discr);
7447
7448 if Present (Discr_Expr (Position)) then
7449 Error_Msg_N ("duplicate constraint for discriminant&", Id);
7450
7451 else
7452 -- Each discriminant specified in the same named association
7453 -- must be associated with a separate copy of the
7454 -- corresponding expression.
7455
7456 if Present (Next (Id)) then
7457 Expr := New_Copy_Tree (Expression (Constr));
7458 Set_Parent (Expr, Parent (Expression (Constr)));
7459 else
7460 Expr := Expression (Constr);
7461 end if;
7462
7463 Discr_Expr (Position) := Expr;
7464 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
7465 end if;
7466
7467 -- A discriminant association with more than one discriminant
7468 -- name is only allowed if the named discriminants are all of
7469 -- the same type (RM 3.7.1(8)).
7470
7471 if E = Empty then
7472 E := Base_Type (Etype (Discr));
7473
7474 elsif Base_Type (Etype (Discr)) /= E then
7475 Error_Msg_N
7476 ("all discriminants in an association " &
7477 "must have the same type", Id);
7478 end if;
7479
7480 Next (Id);
7481 end loop;
7482 end if;
7483
7484 Next (Constr);
7485 end loop;
7486
7487 -- A discriminant constraint must provide exactly one value for each
7488 -- discriminant of the type (RM 3.7.1(8)).
7489
7490 for J in Discr_Expr'Range loop
7491 if No (Discr_Expr (J)) then
7492 Error_Msg_N ("too few discriminants given in constraint", C);
7493 return New_Elmt_List;
7494 end if;
7495 end loop;
7496
7497 -- Determine if there are discriminant expressions in the constraint
7498
7499 for J in Discr_Expr'Range loop
7500 if Denotes_Discriminant
7501 (Discr_Expr (J), Check_Concurrent => True)
7502 then
7503 Discrim_Present := True;
7504 end if;
7505 end loop;
7506
7507 -- Build an element list consisting of the expressions given in the
7508 -- discriminant constraint and apply the appropriate checks. The list
7509 -- is constructed after resolving any named discriminant associations
7510 -- and therefore the expressions appear in the textual order of the
7511 -- discriminants.
7512
7513 Discr := First_Discriminant (T);
7514 for J in Discr_Expr'Range loop
7515 if Discr_Expr (J) /= Error then
7516 Append_Elmt (Discr_Expr (J), Elist);
7517
7518 -- If any of the discriminant constraints is given by a
7519 -- discriminant and we are in a derived type declaration we
7520 -- have a discriminant renaming. Establish link between new
7521 -- and old discriminant.
7522
7523 if Denotes_Discriminant (Discr_Expr (J)) then
7524 if Derived_Def then
7525 Set_Corresponding_Discriminant
7526 (Entity (Discr_Expr (J)), Discr);
7527 end if;
7528
7529 -- Force the evaluation of non-discriminant expressions.
7530 -- If we have found a discriminant in the constraint 3.4(26)
7531 -- and 3.8(18) demand that no range checks are performed are
7532 -- after evaluation. If the constraint is for a component
7533 -- definition that has a per-object constraint, expressions are
7534 -- evaluated but not checked either. In all other cases perform
7535 -- a range check.
7536
7537 else
7538 if Discrim_Present then
7539 null;
7540
7541 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
7542 and then
7543 Has_Per_Object_Constraint
7544 (Defining_Identifier (Parent (Parent (Def))))
7545 then
7546 null;
7547
7548 elsif Is_Access_Type (Etype (Discr)) then
7549 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
7550
7551 else
7552 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
7553 end if;
7554
7555 Force_Evaluation (Discr_Expr (J));
7556 end if;
7557
7558 -- Check that the designated type of an access discriminant's
7559 -- expression is not a class-wide type unless the discriminant's
7560 -- designated type is also class-wide.
7561
7562 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
7563 and then not Is_Class_Wide_Type
7564 (Designated_Type (Etype (Discr)))
7565 and then Etype (Discr_Expr (J)) /= Any_Type
7566 and then Is_Class_Wide_Type
7567 (Designated_Type (Etype (Discr_Expr (J))))
7568 then
7569 Wrong_Type (Discr_Expr (J), Etype (Discr));
7570 end if;
7571 end if;
7572
7573 Next_Discriminant (Discr);
7574 end loop;
7575
7576 return Elist;
7577 end Build_Discriminant_Constraints;
7578
7579 ---------------------------------
7580 -- Build_Discriminated_Subtype --
7581 ---------------------------------
7582
7583 procedure Build_Discriminated_Subtype
7584 (T : Entity_Id;
7585 Def_Id : Entity_Id;
7586 Elist : Elist_Id;
7587 Related_Nod : Node_Id;
7588 For_Access : Boolean := False)
7589 is
7590 Has_Discrs : constant Boolean := Has_Discriminants (T);
7591 Constrained : constant Boolean :=
7592 (Has_Discrs
7593 and then not Is_Empty_Elmt_List (Elist)
7594 and then not Is_Class_Wide_Type (T))
7595 or else Is_Constrained (T);
7596
7597 begin
7598 if Ekind (T) = E_Record_Type then
7599 if For_Access then
7600 Set_Ekind (Def_Id, E_Private_Subtype);
7601 Set_Is_For_Access_Subtype (Def_Id, True);
7602 else
7603 Set_Ekind (Def_Id, E_Record_Subtype);
7604 end if;
7605
7606 -- Inherit preelaboration flag from base, for types for which it
7607 -- may have been set: records, private types, protected types.
7608
7609 Set_Known_To_Have_Preelab_Init
7610 (Def_Id, Known_To_Have_Preelab_Init (T));
7611
7612 elsif Ekind (T) = E_Task_Type then
7613 Set_Ekind (Def_Id, E_Task_Subtype);
7614
7615 elsif Ekind (T) = E_Protected_Type then
7616 Set_Ekind (Def_Id, E_Protected_Subtype);
7617 Set_Known_To_Have_Preelab_Init
7618 (Def_Id, Known_To_Have_Preelab_Init (T));
7619
7620 elsif Is_Private_Type (T) then
7621 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
7622 Set_Known_To_Have_Preelab_Init
7623 (Def_Id, Known_To_Have_Preelab_Init (T));
7624
7625 elsif Is_Class_Wide_Type (T) then
7626 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
7627
7628 else
7629 -- Incomplete type. Attach subtype to list of dependents, to be
7630 -- completed with full view of parent type, unless is it the
7631 -- designated subtype of a record component within an init_proc.
7632 -- This last case arises for a component of an access type whose
7633 -- designated type is incomplete (e.g. a Taft Amendment type).
7634 -- The designated subtype is within an inner scope, and needs no
7635 -- elaboration, because only the access type is needed in the
7636 -- initialization procedure.
7637
7638 Set_Ekind (Def_Id, Ekind (T));
7639
7640 if For_Access and then Within_Init_Proc then
7641 null;
7642 else
7643 Append_Elmt (Def_Id, Private_Dependents (T));
7644 end if;
7645 end if;
7646
7647 Set_Etype (Def_Id, T);
7648 Init_Size_Align (Def_Id);
7649 Set_Has_Discriminants (Def_Id, Has_Discrs);
7650 Set_Is_Constrained (Def_Id, Constrained);
7651
7652 Set_First_Entity (Def_Id, First_Entity (T));
7653 Set_Last_Entity (Def_Id, Last_Entity (T));
7654
7655 -- If the subtype is the completion of a private declaration, there may
7656 -- have been representation clauses for the partial view, and they must
7657 -- be preserved. Build_Derived_Type chains the inherited clauses with
7658 -- the ones appearing on the extension. If this comes from a subtype
7659 -- declaration, all clauses are inherited.
7660
7661 if No (First_Rep_Item (Def_Id)) then
7662 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
7663 end if;
7664
7665 if Is_Tagged_Type (T) then
7666 Set_Is_Tagged_Type (Def_Id);
7667 Make_Class_Wide_Type (Def_Id);
7668 end if;
7669
7670 Set_Stored_Constraint (Def_Id, No_Elist);
7671
7672 if Has_Discrs then
7673 Set_Discriminant_Constraint (Def_Id, Elist);
7674 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
7675 end if;
7676
7677 if Is_Tagged_Type (T) then
7678
7679 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
7680 -- concurrent record type (which has the list of primitive
7681 -- operations).
7682
7683 if Ada_Version >= Ada_05
7684 and then Is_Concurrent_Type (T)
7685 then
7686 Set_Corresponding_Record_Type (Def_Id,
7687 Corresponding_Record_Type (T));
7688 else
7689 Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
7690 end if;
7691
7692 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
7693 end if;
7694
7695 -- Subtypes introduced by component declarations do not need to be
7696 -- marked as delayed, and do not get freeze nodes, because the semantics
7697 -- verifies that the parents of the subtypes are frozen before the
7698 -- enclosing record is frozen.
7699
7700 if not Is_Type (Scope (Def_Id)) then
7701 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
7702
7703 if Is_Private_Type (T)
7704 and then Present (Full_View (T))
7705 then
7706 Conditional_Delay (Def_Id, Full_View (T));
7707 else
7708 Conditional_Delay (Def_Id, T);
7709 end if;
7710 end if;
7711
7712 if Is_Record_Type (T) then
7713 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
7714
7715 if Has_Discrs
7716 and then not Is_Empty_Elmt_List (Elist)
7717 and then not For_Access
7718 then
7719 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
7720 elsif not For_Access then
7721 Set_Cloned_Subtype (Def_Id, T);
7722 end if;
7723 end if;
7724 end Build_Discriminated_Subtype;
7725
7726 ---------------------------
7727 -- Build_Itype_Reference --
7728 ---------------------------
7729
7730 procedure Build_Itype_Reference
7731 (Ityp : Entity_Id;
7732 Nod : Node_Id)
7733 is
7734 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
7735 begin
7736 Set_Itype (IR, Ityp);
7737 Insert_After (Nod, IR);
7738 end Build_Itype_Reference;
7739
7740 ------------------------
7741 -- Build_Scalar_Bound --
7742 ------------------------
7743
7744 function Build_Scalar_Bound
7745 (Bound : Node_Id;
7746 Par_T : Entity_Id;
7747 Der_T : Entity_Id) return Node_Id
7748 is
7749 New_Bound : Entity_Id;
7750
7751 begin
7752 -- Note: not clear why this is needed, how can the original bound
7753 -- be unanalyzed at this point? and if it is, what business do we
7754 -- have messing around with it? and why is the base type of the
7755 -- parent type the right type for the resolution. It probably is
7756 -- not! It is OK for the new bound we are creating, but not for
7757 -- the old one??? Still if it never happens, no problem!
7758
7759 Analyze_And_Resolve (Bound, Base_Type (Par_T));
7760
7761 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
7762 New_Bound := New_Copy (Bound);
7763 Set_Etype (New_Bound, Der_T);
7764 Set_Analyzed (New_Bound);
7765
7766 elsif Is_Entity_Name (Bound) then
7767 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
7768
7769 -- The following is almost certainly wrong. What business do we have
7770 -- relocating a node (Bound) that is presumably still attached to
7771 -- the tree elsewhere???
7772
7773 else
7774 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
7775 end if;
7776
7777 Set_Etype (New_Bound, Der_T);
7778 return New_Bound;
7779 end Build_Scalar_Bound;
7780
7781 --------------------------------
7782 -- Build_Underlying_Full_View --
7783 --------------------------------
7784
7785 procedure Build_Underlying_Full_View
7786 (N : Node_Id;
7787 Typ : Entity_Id;
7788 Par : Entity_Id)
7789 is
7790 Loc : constant Source_Ptr := Sloc (N);
7791 Subt : constant Entity_Id :=
7792 Make_Defining_Identifier
7793 (Loc, New_External_Name (Chars (Typ), 'S'));
7794
7795 Constr : Node_Id;
7796 Indic : Node_Id;
7797 C : Node_Id;
7798 Id : Node_Id;
7799
7800 procedure Set_Discriminant_Name (Id : Node_Id);
7801 -- If the derived type has discriminants, they may rename discriminants
7802 -- of the parent. When building the full view of the parent, we need to
7803 -- recover the names of the original discriminants if the constraint is
7804 -- given by named associations.
7805
7806 ---------------------------
7807 -- Set_Discriminant_Name --
7808 ---------------------------
7809
7810 procedure Set_Discriminant_Name (Id : Node_Id) is
7811 Disc : Entity_Id;
7812
7813 begin
7814 Set_Original_Discriminant (Id, Empty);
7815
7816 if Has_Discriminants (Typ) then
7817 Disc := First_Discriminant (Typ);
7818 while Present (Disc) loop
7819 if Chars (Disc) = Chars (Id)
7820 and then Present (Corresponding_Discriminant (Disc))
7821 then
7822 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
7823 end if;
7824 Next_Discriminant (Disc);
7825 end loop;
7826 end if;
7827 end Set_Discriminant_Name;
7828
7829 -- Start of processing for Build_Underlying_Full_View
7830
7831 begin
7832 if Nkind (N) = N_Full_Type_Declaration then
7833 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
7834
7835 elsif Nkind (N) = N_Subtype_Declaration then
7836 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
7837
7838 elsif Nkind (N) = N_Component_Declaration then
7839 Constr :=
7840 New_Copy_Tree
7841 (Constraint (Subtype_Indication (Component_Definition (N))));
7842
7843 else
7844 raise Program_Error;
7845 end if;
7846
7847 C := First (Constraints (Constr));
7848 while Present (C) loop
7849 if Nkind (C) = N_Discriminant_Association then
7850 Id := First (Selector_Names (C));
7851 while Present (Id) loop
7852 Set_Discriminant_Name (Id);
7853 Next (Id);
7854 end loop;
7855 end if;
7856
7857 Next (C);
7858 end loop;
7859
7860 Indic :=
7861 Make_Subtype_Declaration (Loc,
7862 Defining_Identifier => Subt,
7863 Subtype_Indication =>
7864 Make_Subtype_Indication (Loc,
7865 Subtype_Mark => New_Reference_To (Par, Loc),
7866 Constraint => New_Copy_Tree (Constr)));
7867
7868 -- If this is a component subtype for an outer itype, it is not
7869 -- a list member, so simply set the parent link for analysis: if
7870 -- the enclosing type does not need to be in a declarative list,
7871 -- neither do the components.
7872
7873 if Is_List_Member (N)
7874 and then Nkind (N) /= N_Component_Declaration
7875 then
7876 Insert_Before (N, Indic);
7877 else
7878 Set_Parent (Indic, Parent (N));
7879 end if;
7880
7881 Analyze (Indic);
7882 Set_Underlying_Full_View (Typ, Full_View (Subt));
7883 end Build_Underlying_Full_View;
7884
7885 -------------------------------
7886 -- Check_Abstract_Overriding --
7887 -------------------------------
7888
7889 procedure Check_Abstract_Overriding (T : Entity_Id) is
7890 Alias_Subp : Entity_Id;
7891 Elmt : Elmt_Id;
7892 Op_List : Elist_Id;
7893 Subp : Entity_Id;
7894 Type_Def : Node_Id;
7895
7896 begin
7897 Op_List := Primitive_Operations (T);
7898
7899 -- Loop to check primitive operations
7900
7901 Elmt := First_Elmt (Op_List);
7902 while Present (Elmt) loop
7903 Subp := Node (Elmt);
7904 Alias_Subp := Alias (Subp);
7905
7906 -- Inherited subprograms are identified by the fact that they do not
7907 -- come from source, and the associated source location is the
7908 -- location of the first subtype of the derived type.
7909
7910 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
7911 -- subprograms that "require overriding".
7912
7913 -- Special exception, do not complain about failure to override the
7914 -- stream routines _Input and _Output, as well as the primitive
7915 -- operations used in dispatching selects since we always provide
7916 -- automatic overridings for these subprograms.
7917
7918 -- Also ignore this rule for convention CIL since .NET libraries
7919 -- do bizarre things with interfaces???
7920
7921 -- The partial view of T may have been a private extension, for
7922 -- which inherited functions dispatching on result are abstract.
7923 -- If the full view is a null extension, there is no need for
7924 -- overriding in Ada2005, but wrappers need to be built for them
7925 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
7926
7927 if Is_Null_Extension (T)
7928 and then Has_Controlling_Result (Subp)
7929 and then Ada_Version >= Ada_05
7930 and then Present (Alias_Subp)
7931 and then not Comes_From_Source (Subp)
7932 and then not Is_Abstract_Subprogram (Alias_Subp)
7933 and then not Is_Access_Type (Etype (Subp))
7934 then
7935 null;
7936
7937 -- Ada 2005 (AI-251): Internal entities of interfaces need no
7938 -- processing because this check is done with the aliased
7939 -- entity
7940
7941 elsif Present (Interface_Alias (Subp)) then
7942 null;
7943
7944 elsif (Is_Abstract_Subprogram (Subp)
7945 or else Requires_Overriding (Subp)
7946 or else
7947 (Has_Controlling_Result (Subp)
7948 and then Present (Alias_Subp)
7949 and then not Comes_From_Source (Subp)
7950 and then Sloc (Subp) = Sloc (First_Subtype (T))))
7951 and then not Is_TSS (Subp, TSS_Stream_Input)
7952 and then not Is_TSS (Subp, TSS_Stream_Output)
7953 and then not Is_Abstract_Type (T)
7954 and then Convention (T) /= Convention_CIL
7955 and then not Is_Predefined_Interface_Primitive (Subp)
7956
7957 -- Ada 2005 (AI-251): Do not consider hidden entities associated
7958 -- with abstract interface types because the check will be done
7959 -- with the aliased entity (otherwise we generate a duplicated
7960 -- error message).
7961
7962 and then not Present (Interface_Alias (Subp))
7963 then
7964 if Present (Alias_Subp) then
7965
7966 -- Only perform the check for a derived subprogram when the
7967 -- type has an explicit record extension. This avoids incorrect
7968 -- flagging of abstract subprograms for the case of a type
7969 -- without an extension that is derived from a formal type
7970 -- with a tagged actual (can occur within a private part).
7971
7972 -- Ada 2005 (AI-391): In the case of an inherited function with
7973 -- a controlling result of the type, the rule does not apply if
7974 -- the type is a null extension (unless the parent function
7975 -- itself is abstract, in which case the function must still be
7976 -- be overridden). The expander will generate an overriding
7977 -- wrapper function calling the parent subprogram (see
7978 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
7979
7980 Type_Def := Type_Definition (Parent (T));
7981
7982 if Nkind (Type_Def) = N_Derived_Type_Definition
7983 and then Present (Record_Extension_Part (Type_Def))
7984 and then
7985 (Ada_Version < Ada_05
7986 or else not Is_Null_Extension (T)
7987 or else Ekind (Subp) = E_Procedure
7988 or else not Has_Controlling_Result (Subp)
7989 or else Is_Abstract_Subprogram (Alias_Subp)
7990 or else Requires_Overriding (Subp)
7991 or else Is_Access_Type (Etype (Subp)))
7992 then
7993 -- Avoid reporting error in case of abstract predefined
7994 -- primitive inherited from interface type because the
7995 -- body of internally generated predefined primitives
7996 -- of tagged types are generated later by Freeze_Type
7997
7998 if Is_Interface (Root_Type (T))
7999 and then Is_Abstract_Subprogram (Subp)
8000 and then Is_Predefined_Dispatching_Operation (Subp)
8001 and then not Comes_From_Source (Ultimate_Alias (Subp))
8002 then
8003 null;
8004
8005 else
8006 Error_Msg_NE
8007 ("type must be declared abstract or & overridden",
8008 T, Subp);
8009
8010 -- Traverse the whole chain of aliased subprograms to
8011 -- complete the error notification. This is especially
8012 -- useful for traceability of the chain of entities when
8013 -- the subprogram corresponds with an interface
8014 -- subprogram (which may be defined in another package).
8015
8016 if Present (Alias_Subp) then
8017 declare
8018 E : Entity_Id;
8019
8020 begin
8021 E := Subp;
8022 while Present (Alias (E)) loop
8023 Error_Msg_Sloc := Sloc (E);
8024 Error_Msg_NE
8025 ("\& has been inherited #", T, Subp);
8026 E := Alias (E);
8027 end loop;
8028
8029 Error_Msg_Sloc := Sloc (E);
8030 Error_Msg_NE
8031 ("\& has been inherited from subprogram #",
8032 T, Subp);
8033 end;
8034 end if;
8035 end if;
8036
8037 -- Ada 2005 (AI-345): Protected or task type implementing
8038 -- abstract interfaces.
8039
8040 elsif Is_Concurrent_Record_Type (T)
8041 and then Present (Interfaces (T))
8042 then
8043 -- The controlling formal of Subp must be of mode "out",
8044 -- "in out" or an access-to-variable to be overridden.
8045
8046 -- Error message below needs rewording (remember comma
8047 -- in -gnatj mode) ???
8048
8049 if Ekind (First_Formal (Subp)) = E_In_Parameter then
8050 if not Is_Predefined_Dispatching_Operation (Subp) then
8051 Error_Msg_NE
8052 ("first formal of & must be of mode `OUT`, " &
8053 "`IN OUT` or access-to-variable", T, Subp);
8054 Error_Msg_N
8055 ("\to be overridden by protected procedure or " &
8056 "entry (RM 9.4(11.9/2))", T);
8057 end if;
8058
8059 -- Some other kind of overriding failure
8060
8061 else
8062 Error_Msg_NE
8063 ("interface subprogram & must be overridden",
8064 T, Subp);
8065 end if;
8066 end if;
8067
8068 else
8069 Error_Msg_Node_2 := T;
8070 Error_Msg_N
8071 ("abstract subprogram& not allowed for type&", Subp);
8072
8073 -- Also post unconditional warning on the type (unconditional
8074 -- so that if there are more than one of these cases, we get
8075 -- them all, and not just the first one).
8076
8077 Error_Msg_Node_2 := Subp;
8078 Error_Msg_N
8079 ("nonabstract type& has abstract subprogram&!", T);
8080 end if;
8081 end if;
8082
8083 -- Ada 2005 (AI05-0030): Inspect hidden subprograms which provide
8084 -- the mapping between interface and implementing type primitives.
8085 -- If the interface alias is marked as Implemented_By_Entry, the
8086 -- alias must be an entry wrapper.
8087
8088 if Ada_Version >= Ada_05
8089 and then Is_Hidden (Subp)
8090 and then Present (Interface_Alias (Subp))
8091 and then Implemented_By_Entry (Interface_Alias (Subp))
8092 and then Present (Alias_Subp)
8093 and then
8094 (not Is_Primitive_Wrapper (Alias_Subp)
8095 or else Ekind (Wrapped_Entity (Alias_Subp)) /= E_Entry)
8096 then
8097 declare
8098 Error_Ent : Entity_Id := T;
8099
8100 begin
8101 if Is_Concurrent_Record_Type (Error_Ent) then
8102 Error_Ent := Corresponding_Concurrent_Type (Error_Ent);
8103 end if;
8104
8105 Error_Msg_Node_2 := Interface_Alias (Subp);
8106 Error_Msg_NE
8107 ("type & must implement abstract subprogram & with an entry",
8108 Error_Ent, Error_Ent);
8109 end;
8110 end if;
8111
8112 Next_Elmt (Elmt);
8113 end loop;
8114 end Check_Abstract_Overriding;
8115
8116 ------------------------------------------------
8117 -- Check_Access_Discriminant_Requires_Limited --
8118 ------------------------------------------------
8119
8120 procedure Check_Access_Discriminant_Requires_Limited
8121 (D : Node_Id;
8122 Loc : Node_Id)
8123 is
8124 begin
8125 -- A discriminant_specification for an access discriminant shall appear
8126 -- only in the declaration for a task or protected type, or for a type
8127 -- with the reserved word 'limited' in its definition or in one of its
8128 -- ancestors. (RM 3.7(10))
8129
8130 if Nkind (Discriminant_Type (D)) = N_Access_Definition
8131 and then not Is_Concurrent_Type (Current_Scope)
8132 and then not Is_Concurrent_Record_Type (Current_Scope)
8133 and then not Is_Limited_Record (Current_Scope)
8134 and then Ekind (Current_Scope) /= E_Limited_Private_Type
8135 then
8136 Error_Msg_N
8137 ("access discriminants allowed only for limited types", Loc);
8138 end if;
8139 end Check_Access_Discriminant_Requires_Limited;
8140
8141 -----------------------------------
8142 -- Check_Aliased_Component_Types --
8143 -----------------------------------
8144
8145 procedure Check_Aliased_Component_Types (T : Entity_Id) is
8146 C : Entity_Id;
8147
8148 begin
8149 -- ??? Also need to check components of record extensions, but not
8150 -- components of protected types (which are always limited).
8151
8152 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
8153 -- types to be unconstrained. This is safe because it is illegal to
8154 -- create access subtypes to such types with explicit discriminant
8155 -- constraints.
8156
8157 if not Is_Limited_Type (T) then
8158 if Ekind (T) = E_Record_Type then
8159 C := First_Component (T);
8160 while Present (C) loop
8161 if Is_Aliased (C)
8162 and then Has_Discriminants (Etype (C))
8163 and then not Is_Constrained (Etype (C))
8164 and then not In_Instance_Body
8165 and then Ada_Version < Ada_05
8166 then
8167 Error_Msg_N
8168 ("aliased component must be constrained (RM 3.6(11))",
8169 C);
8170 end if;
8171
8172 Next_Component (C);
8173 end loop;
8174
8175 elsif Ekind (T) = E_Array_Type then
8176 if Has_Aliased_Components (T)
8177 and then Has_Discriminants (Component_Type (T))
8178 and then not Is_Constrained (Component_Type (T))
8179 and then not In_Instance_Body
8180 and then Ada_Version < Ada_05
8181 then
8182 Error_Msg_N
8183 ("aliased component type must be constrained (RM 3.6(11))",
8184 T);
8185 end if;
8186 end if;
8187 end if;
8188 end Check_Aliased_Component_Types;
8189
8190 ----------------------
8191 -- Check_Completion --
8192 ----------------------
8193
8194 procedure Check_Completion (Body_Id : Node_Id := Empty) is
8195 E : Entity_Id;
8196
8197 procedure Post_Error;
8198 -- Post error message for lack of completion for entity E
8199
8200 ----------------
8201 -- Post_Error --
8202 ----------------
8203
8204 procedure Post_Error is
8205 begin
8206 if not Comes_From_Source (E) then
8207
8208 if Ekind (E) = E_Task_Type
8209 or else Ekind (E) = E_Protected_Type
8210 then
8211 -- It may be an anonymous protected type created for a
8212 -- single variable. Post error on variable, if present.
8213
8214 declare
8215 Var : Entity_Id;
8216
8217 begin
8218 Var := First_Entity (Current_Scope);
8219 while Present (Var) loop
8220 exit when Etype (Var) = E
8221 and then Comes_From_Source (Var);
8222
8223 Next_Entity (Var);
8224 end loop;
8225
8226 if Present (Var) then
8227 E := Var;
8228 end if;
8229 end;
8230 end if;
8231 end if;
8232
8233 -- If a generated entity has no completion, then either previous
8234 -- semantic errors have disabled the expansion phase, or else we had
8235 -- missing subunits, or else we are compiling without expansion,
8236 -- or else something is very wrong.
8237
8238 if not Comes_From_Source (E) then
8239 pragma Assert
8240 (Serious_Errors_Detected > 0
8241 or else Configurable_Run_Time_Violations > 0
8242 or else Subunits_Missing
8243 or else not Expander_Active);
8244 return;
8245
8246 -- Here for source entity
8247
8248 else
8249 -- Here if no body to post the error message, so we post the error
8250 -- on the declaration that has no completion. This is not really
8251 -- the right place to post it, think about this later ???
8252
8253 if No (Body_Id) then
8254 if Is_Type (E) then
8255 Error_Msg_NE
8256 ("missing full declaration for }", Parent (E), E);
8257 else
8258 Error_Msg_NE
8259 ("missing body for &", Parent (E), E);
8260 end if;
8261
8262 -- Package body has no completion for a declaration that appears
8263 -- in the corresponding spec. Post error on the body, with a
8264 -- reference to the non-completed declaration.
8265
8266 else
8267 Error_Msg_Sloc := Sloc (E);
8268
8269 if Is_Type (E) then
8270 Error_Msg_NE
8271 ("missing full declaration for }!", Body_Id, E);
8272
8273 elsif Is_Overloadable (E)
8274 and then Current_Entity_In_Scope (E) /= E
8275 then
8276 -- It may be that the completion is mistyped and appears as
8277 -- a distinct overloading of the entity.
8278
8279 declare
8280 Candidate : constant Entity_Id :=
8281 Current_Entity_In_Scope (E);
8282 Decl : constant Node_Id :=
8283 Unit_Declaration_Node (Candidate);
8284
8285 begin
8286 if Is_Overloadable (Candidate)
8287 and then Ekind (Candidate) = Ekind (E)
8288 and then Nkind (Decl) = N_Subprogram_Body
8289 and then Acts_As_Spec (Decl)
8290 then
8291 Check_Type_Conformant (Candidate, E);
8292
8293 else
8294 Error_Msg_NE ("missing body for & declared#!",
8295 Body_Id, E);
8296 end if;
8297 end;
8298 else
8299 Error_Msg_NE ("missing body for & declared#!",
8300 Body_Id, E);
8301 end if;
8302 end if;
8303 end if;
8304 end Post_Error;
8305
8306 -- Start processing for Check_Completion
8307
8308 begin
8309 E := First_Entity (Current_Scope);
8310 while Present (E) loop
8311 if Is_Intrinsic_Subprogram (E) then
8312 null;
8313
8314 -- The following situation requires special handling: a child unit
8315 -- that appears in the context clause of the body of its parent:
8316
8317 -- procedure Parent.Child (...);
8318
8319 -- with Parent.Child;
8320 -- package body Parent is
8321
8322 -- Here Parent.Child appears as a local entity, but should not be
8323 -- flagged as requiring completion, because it is a compilation
8324 -- unit.
8325
8326 -- Ignore missing completion for a subprogram that does not come from
8327 -- source (including the _Call primitive operation of RAS types,
8328 -- which has to have the flag Comes_From_Source for other purposes):
8329 -- we assume that the expander will provide the missing completion.
8330
8331 elsif Ekind (E) = E_Function
8332 or else Ekind (E) = E_Procedure
8333 or else Ekind (E) = E_Generic_Function
8334 or else Ekind (E) = E_Generic_Procedure
8335 then
8336 if not Has_Completion (E)
8337 and then not (Is_Subprogram (E)
8338 and then Is_Abstract_Subprogram (E))
8339 and then not (Is_Subprogram (E)
8340 and then
8341 (not Comes_From_Source (E)
8342 or else Chars (E) = Name_uCall))
8343 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8344 N_Compilation_Unit
8345 and then Chars (E) /= Name_uSize
8346 then
8347 Post_Error;
8348 end if;
8349
8350 elsif Is_Entry (E) then
8351 if not Has_Completion (E) and then
8352 (Ekind (Scope (E)) = E_Protected_Object
8353 or else Ekind (Scope (E)) = E_Protected_Type)
8354 then
8355 Post_Error;
8356 end if;
8357
8358 elsif Is_Package_Or_Generic_Package (E) then
8359 if Unit_Requires_Body (E) then
8360 if not Has_Completion (E)
8361 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8362 N_Compilation_Unit
8363 then
8364 Post_Error;
8365 end if;
8366
8367 elsif not Is_Child_Unit (E) then
8368 May_Need_Implicit_Body (E);
8369 end if;
8370
8371 elsif Ekind (E) = E_Incomplete_Type
8372 and then No (Underlying_Type (E))
8373 then
8374 Post_Error;
8375
8376 elsif (Ekind (E) = E_Task_Type or else
8377 Ekind (E) = E_Protected_Type)
8378 and then not Has_Completion (E)
8379 then
8380 Post_Error;
8381
8382 -- A single task declared in the current scope is a constant, verify
8383 -- that the body of its anonymous type is in the same scope. If the
8384 -- task is defined elsewhere, this may be a renaming declaration for
8385 -- which no completion is needed.
8386
8387 elsif Ekind (E) = E_Constant
8388 and then Ekind (Etype (E)) = E_Task_Type
8389 and then not Has_Completion (Etype (E))
8390 and then Scope (Etype (E)) = Current_Scope
8391 then
8392 Post_Error;
8393
8394 elsif Ekind (E) = E_Protected_Object
8395 and then not Has_Completion (Etype (E))
8396 then
8397 Post_Error;
8398
8399 elsif Ekind (E) = E_Record_Type then
8400 if Is_Tagged_Type (E) then
8401 Check_Abstract_Overriding (E);
8402 Check_Conventions (E);
8403 end if;
8404
8405 Check_Aliased_Component_Types (E);
8406
8407 elsif Ekind (E) = E_Array_Type then
8408 Check_Aliased_Component_Types (E);
8409
8410 end if;
8411
8412 Next_Entity (E);
8413 end loop;
8414 end Check_Completion;
8415
8416 ----------------------------
8417 -- Check_Delta_Expression --
8418 ----------------------------
8419
8420 procedure Check_Delta_Expression (E : Node_Id) is
8421 begin
8422 if not (Is_Real_Type (Etype (E))) then
8423 Wrong_Type (E, Any_Real);
8424
8425 elsif not Is_OK_Static_Expression (E) then
8426 Flag_Non_Static_Expr
8427 ("non-static expression used for delta value!", E);
8428
8429 elsif not UR_Is_Positive (Expr_Value_R (E)) then
8430 Error_Msg_N ("delta expression must be positive", E);
8431
8432 else
8433 return;
8434 end if;
8435
8436 -- If any of above errors occurred, then replace the incorrect
8437 -- expression by the real 0.1, which should prevent further errors.
8438
8439 Rewrite (E,
8440 Make_Real_Literal (Sloc (E), Ureal_Tenth));
8441 Analyze_And_Resolve (E, Standard_Float);
8442 end Check_Delta_Expression;
8443
8444 -----------------------------
8445 -- Check_Digits_Expression --
8446 -----------------------------
8447
8448 procedure Check_Digits_Expression (E : Node_Id) is
8449 begin
8450 if not (Is_Integer_Type (Etype (E))) then
8451 Wrong_Type (E, Any_Integer);
8452
8453 elsif not Is_OK_Static_Expression (E) then
8454 Flag_Non_Static_Expr
8455 ("non-static expression used for digits value!", E);
8456
8457 elsif Expr_Value (E) <= 0 then
8458 Error_Msg_N ("digits value must be greater than zero", E);
8459
8460 else
8461 return;
8462 end if;
8463
8464 -- If any of above errors occurred, then replace the incorrect
8465 -- expression by the integer 1, which should prevent further errors.
8466
8467 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
8468 Analyze_And_Resolve (E, Standard_Integer);
8469
8470 end Check_Digits_Expression;
8471
8472 --------------------------
8473 -- Check_Initialization --
8474 --------------------------
8475
8476 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
8477 begin
8478 if Is_Limited_Type (T)
8479 and then not In_Instance
8480 and then not In_Inlined_Body
8481 then
8482 if not OK_For_Limited_Init (Exp) then
8483
8484 -- In GNAT mode, this is just a warning, to allow it to be evilly
8485 -- turned off. Otherwise it is a real error.
8486
8487 if GNAT_Mode then
8488 Error_Msg_N
8489 ("?cannot initialize entities of limited type!", Exp);
8490
8491 elsif Ada_Version < Ada_05 then
8492 Error_Msg_N
8493 ("cannot initialize entities of limited type", Exp);
8494 Explain_Limited_Type (T, Exp);
8495
8496 else
8497 -- Specialize error message according to kind of illegal
8498 -- initial expression.
8499
8500 if Nkind (Exp) = N_Type_Conversion
8501 and then Nkind (Expression (Exp)) = N_Function_Call
8502 then
8503 Error_Msg_N
8504 ("illegal context for call"
8505 & " to function with limited result", Exp);
8506
8507 else
8508 Error_Msg_N
8509 ("initialization of limited object requires aggregate "
8510 & "or function call", Exp);
8511 end if;
8512 end if;
8513 end if;
8514 end if;
8515 end Check_Initialization;
8516
8517 ----------------------
8518 -- Check_Interfaces --
8519 ----------------------
8520
8521 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
8522 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
8523
8524 Iface : Node_Id;
8525 Iface_Def : Node_Id;
8526 Iface_Typ : Entity_Id;
8527 Parent_Node : Node_Id;
8528
8529 Is_Task : Boolean := False;
8530 -- Set True if parent type or any progenitor is a task interface
8531
8532 Is_Protected : Boolean := False;
8533 -- Set True if parent type or any progenitor is a protected interface
8534
8535 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
8536 -- Check that a progenitor is compatible with declaration.
8537 -- Error is posted on Error_Node.
8538
8539 ------------------
8540 -- Check_Ifaces --
8541 ------------------
8542
8543 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
8544 Iface_Id : constant Entity_Id :=
8545 Defining_Identifier (Parent (Iface_Def));
8546 Type_Def : Node_Id;
8547
8548 begin
8549 if Nkind (N) = N_Private_Extension_Declaration then
8550 Type_Def := N;
8551 else
8552 Type_Def := Type_Definition (N);
8553 end if;
8554
8555 if Is_Task_Interface (Iface_Id) then
8556 Is_Task := True;
8557
8558 elsif Is_Protected_Interface (Iface_Id) then
8559 Is_Protected := True;
8560 end if;
8561
8562 -- Check that the characteristics of the progenitor are compatible
8563 -- with the explicit qualifier in the declaration.
8564 -- The check only applies to qualifiers that come from source.
8565 -- Limited_Present also appears in the declaration of corresponding
8566 -- records, and the check does not apply to them.
8567
8568 if Limited_Present (Type_Def)
8569 and then not
8570 Is_Concurrent_Record_Type (Defining_Identifier (N))
8571 then
8572 if Is_Limited_Interface (Parent_Type)
8573 and then not Is_Limited_Interface (Iface_Id)
8574 then
8575 Error_Msg_NE
8576 ("progenitor& must be limited interface",
8577 Error_Node, Iface_Id);
8578
8579 elsif
8580 (Task_Present (Iface_Def)
8581 or else Protected_Present (Iface_Def)
8582 or else Synchronized_Present (Iface_Def))
8583 and then Nkind (N) /= N_Private_Extension_Declaration
8584 then
8585 Error_Msg_NE
8586 ("progenitor& must be limited interface",
8587 Error_Node, Iface_Id);
8588 end if;
8589
8590 -- Protected interfaces can only inherit from limited, synchronized
8591 -- or protected interfaces.
8592
8593 elsif Nkind (N) = N_Full_Type_Declaration
8594 and then Protected_Present (Type_Def)
8595 then
8596 if Limited_Present (Iface_Def)
8597 or else Synchronized_Present (Iface_Def)
8598 or else Protected_Present (Iface_Def)
8599 then
8600 null;
8601
8602 elsif Task_Present (Iface_Def) then
8603 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8604 & " from task interface", Error_Node);
8605
8606 else
8607 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8608 & " from non-limited interface", Error_Node);
8609 end if;
8610
8611 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
8612 -- limited and synchronized.
8613
8614 elsif Synchronized_Present (Type_Def) then
8615 if Limited_Present (Iface_Def)
8616 or else Synchronized_Present (Iface_Def)
8617 then
8618 null;
8619
8620 elsif Protected_Present (Iface_Def)
8621 and then Nkind (N) /= N_Private_Extension_Declaration
8622 then
8623 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8624 & " from protected interface", Error_Node);
8625
8626 elsif Task_Present (Iface_Def)
8627 and then Nkind (N) /= N_Private_Extension_Declaration
8628 then
8629 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8630 & " from task interface", Error_Node);
8631
8632 elsif not Is_Limited_Interface (Iface_Id) then
8633 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8634 & " from non-limited interface", Error_Node);
8635 end if;
8636
8637 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
8638 -- synchronized or task interfaces.
8639
8640 elsif Nkind (N) = N_Full_Type_Declaration
8641 and then Task_Present (Type_Def)
8642 then
8643 if Limited_Present (Iface_Def)
8644 or else Synchronized_Present (Iface_Def)
8645 or else Task_Present (Iface_Def)
8646 then
8647 null;
8648
8649 elsif Protected_Present (Iface_Def) then
8650 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8651 & " protected interface", Error_Node);
8652
8653 else
8654 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8655 & " non-limited interface", Error_Node);
8656 end if;
8657 end if;
8658 end Check_Ifaces;
8659
8660 -- Start of processing for Check_Interfaces
8661
8662 begin
8663 if Is_Interface (Parent_Type) then
8664 if Is_Task_Interface (Parent_Type) then
8665 Is_Task := True;
8666
8667 elsif Is_Protected_Interface (Parent_Type) then
8668 Is_Protected := True;
8669 end if;
8670 end if;
8671
8672 if Nkind (N) = N_Private_Extension_Declaration then
8673
8674 -- Check that progenitors are compatible with declaration
8675
8676 Iface := First (Interface_List (Def));
8677 while Present (Iface) loop
8678 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
8679
8680 Parent_Node := Parent (Base_Type (Iface_Typ));
8681 Iface_Def := Type_Definition (Parent_Node);
8682
8683 if not Is_Interface (Iface_Typ) then
8684 Diagnose_Interface (Iface, Iface_Typ);
8685
8686 else
8687 Check_Ifaces (Iface_Def, Iface);
8688 end if;
8689
8690 Next (Iface);
8691 end loop;
8692
8693 if Is_Task and Is_Protected then
8694 Error_Msg_N
8695 ("type cannot derive from task and protected interface", N);
8696 end if;
8697
8698 return;
8699 end if;
8700
8701 -- Full type declaration of derived type.
8702 -- Check compatibility with parent if it is interface type
8703
8704 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
8705 and then Is_Interface (Parent_Type)
8706 then
8707 Parent_Node := Parent (Parent_Type);
8708
8709 -- More detailed checks for interface varieties
8710
8711 Check_Ifaces
8712 (Iface_Def => Type_Definition (Parent_Node),
8713 Error_Node => Subtype_Indication (Type_Definition (N)));
8714 end if;
8715
8716 Iface := First (Interface_List (Def));
8717 while Present (Iface) loop
8718 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
8719
8720 Parent_Node := Parent (Base_Type (Iface_Typ));
8721 Iface_Def := Type_Definition (Parent_Node);
8722
8723 if not Is_Interface (Iface_Typ) then
8724 Diagnose_Interface (Iface, Iface_Typ);
8725
8726 else
8727 -- "The declaration of a specific descendant of an interface
8728 -- type freezes the interface type" RM 13.14
8729
8730 Freeze_Before (N, Iface_Typ);
8731 Check_Ifaces (Iface_Def, Error_Node => Iface);
8732 end if;
8733
8734 Next (Iface);
8735 end loop;
8736
8737 if Is_Task and Is_Protected then
8738 Error_Msg_N
8739 ("type cannot derive from task and protected interface", N);
8740 end if;
8741 end Check_Interfaces;
8742
8743 ------------------------------------
8744 -- Check_Or_Process_Discriminants --
8745 ------------------------------------
8746
8747 -- If an incomplete or private type declaration was already given for the
8748 -- type, the discriminants may have already been processed if they were
8749 -- present on the incomplete declaration. In this case a full conformance
8750 -- check is performed otherwise just process them.
8751
8752 procedure Check_Or_Process_Discriminants
8753 (N : Node_Id;
8754 T : Entity_Id;
8755 Prev : Entity_Id := Empty)
8756 is
8757 begin
8758 if Has_Discriminants (T) then
8759
8760 -- Make the discriminants visible to component declarations
8761
8762 declare
8763 D : Entity_Id;
8764 Prev : Entity_Id;
8765
8766 begin
8767 D := First_Discriminant (T);
8768 while Present (D) loop
8769 Prev := Current_Entity (D);
8770 Set_Current_Entity (D);
8771 Set_Is_Immediately_Visible (D);
8772 Set_Homonym (D, Prev);
8773
8774 -- Ada 2005 (AI-230): Access discriminant allowed in
8775 -- non-limited record types.
8776
8777 if Ada_Version < Ada_05 then
8778
8779 -- This restriction gets applied to the full type here. It
8780 -- has already been applied earlier to the partial view.
8781
8782 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
8783 end if;
8784
8785 Next_Discriminant (D);
8786 end loop;
8787 end;
8788
8789 elsif Present (Discriminant_Specifications (N)) then
8790 Process_Discriminants (N, Prev);
8791 end if;
8792 end Check_Or_Process_Discriminants;
8793
8794 ----------------------
8795 -- Check_Real_Bound --
8796 ----------------------
8797
8798 procedure Check_Real_Bound (Bound : Node_Id) is
8799 begin
8800 if not Is_Real_Type (Etype (Bound)) then
8801 Error_Msg_N
8802 ("bound in real type definition must be of real type", Bound);
8803
8804 elsif not Is_OK_Static_Expression (Bound) then
8805 Flag_Non_Static_Expr
8806 ("non-static expression used for real type bound!", Bound);
8807
8808 else
8809 return;
8810 end if;
8811
8812 Rewrite
8813 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
8814 Analyze (Bound);
8815 Resolve (Bound, Standard_Float);
8816 end Check_Real_Bound;
8817
8818 ------------------------------
8819 -- Complete_Private_Subtype --
8820 ------------------------------
8821
8822 procedure Complete_Private_Subtype
8823 (Priv : Entity_Id;
8824 Full : Entity_Id;
8825 Full_Base : Entity_Id;
8826 Related_Nod : Node_Id)
8827 is
8828 Save_Next_Entity : Entity_Id;
8829 Save_Homonym : Entity_Id;
8830
8831 begin
8832 -- Set semantic attributes for (implicit) private subtype completion.
8833 -- If the full type has no discriminants, then it is a copy of the full
8834 -- view of the base. Otherwise, it is a subtype of the base with a
8835 -- possible discriminant constraint. Save and restore the original
8836 -- Next_Entity field of full to ensure that the calls to Copy_Node
8837 -- do not corrupt the entity chain.
8838
8839 -- Note that the type of the full view is the same entity as the type of
8840 -- the partial view. In this fashion, the subtype has access to the
8841 -- correct view of the parent.
8842
8843 Save_Next_Entity := Next_Entity (Full);
8844 Save_Homonym := Homonym (Priv);
8845
8846 case Ekind (Full_Base) is
8847 when E_Record_Type |
8848 E_Record_Subtype |
8849 Class_Wide_Kind |
8850 Private_Kind |
8851 Task_Kind |
8852 Protected_Kind =>
8853 Copy_Node (Priv, Full);
8854
8855 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
8856 Set_First_Entity (Full, First_Entity (Full_Base));
8857 Set_Last_Entity (Full, Last_Entity (Full_Base));
8858
8859 when others =>
8860 Copy_Node (Full_Base, Full);
8861 Set_Chars (Full, Chars (Priv));
8862 Conditional_Delay (Full, Priv);
8863 Set_Sloc (Full, Sloc (Priv));
8864 end case;
8865
8866 Set_Next_Entity (Full, Save_Next_Entity);
8867 Set_Homonym (Full, Save_Homonym);
8868 Set_Associated_Node_For_Itype (Full, Related_Nod);
8869
8870 -- Set common attributes for all subtypes
8871
8872 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
8873
8874 -- The Etype of the full view is inconsistent. Gigi needs to see the
8875 -- structural full view, which is what the current scheme gives:
8876 -- the Etype of the full view is the etype of the full base. However,
8877 -- if the full base is a derived type, the full view then looks like
8878 -- a subtype of the parent, not a subtype of the full base. If instead
8879 -- we write:
8880
8881 -- Set_Etype (Full, Full_Base);
8882
8883 -- then we get inconsistencies in the front-end (confusion between
8884 -- views). Several outstanding bugs are related to this ???
8885
8886 Set_Is_First_Subtype (Full, False);
8887 Set_Scope (Full, Scope (Priv));
8888 Set_Size_Info (Full, Full_Base);
8889 Set_RM_Size (Full, RM_Size (Full_Base));
8890 Set_Is_Itype (Full);
8891
8892 -- A subtype of a private-type-without-discriminants, whose full-view
8893 -- has discriminants with default expressions, is not constrained!
8894
8895 if not Has_Discriminants (Priv) then
8896 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
8897
8898 if Has_Discriminants (Full_Base) then
8899 Set_Discriminant_Constraint
8900 (Full, Discriminant_Constraint (Full_Base));
8901
8902 -- The partial view may have been indefinite, the full view
8903 -- might not be.
8904
8905 Set_Has_Unknown_Discriminants
8906 (Full, Has_Unknown_Discriminants (Full_Base));
8907 end if;
8908 end if;
8909
8910 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
8911 Set_Depends_On_Private (Full, Has_Private_Component (Full));
8912
8913 -- Freeze the private subtype entity if its parent is delayed, and not
8914 -- already frozen. We skip this processing if the type is an anonymous
8915 -- subtype of a record component, or is the corresponding record of a
8916 -- protected type, since ???
8917
8918 if not Is_Type (Scope (Full)) then
8919 Set_Has_Delayed_Freeze (Full,
8920 Has_Delayed_Freeze (Full_Base)
8921 and then (not Is_Frozen (Full_Base)));
8922 end if;
8923
8924 Set_Freeze_Node (Full, Empty);
8925 Set_Is_Frozen (Full, False);
8926 Set_Full_View (Priv, Full);
8927
8928 if Has_Discriminants (Full) then
8929 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
8930 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
8931
8932 if Has_Unknown_Discriminants (Full) then
8933 Set_Discriminant_Constraint (Full, No_Elist);
8934 end if;
8935 end if;
8936
8937 if Ekind (Full_Base) = E_Record_Type
8938 and then Has_Discriminants (Full_Base)
8939 and then Has_Discriminants (Priv) -- might not, if errors
8940 and then not Has_Unknown_Discriminants (Priv)
8941 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
8942 then
8943 Create_Constrained_Components
8944 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
8945
8946 -- If the full base is itself derived from private, build a congruent
8947 -- subtype of its underlying type, for use by the back end. For a
8948 -- constrained record component, the declaration cannot be placed on
8949 -- the component list, but it must nevertheless be built an analyzed, to
8950 -- supply enough information for Gigi to compute the size of component.
8951
8952 elsif Ekind (Full_Base) in Private_Kind
8953 and then Is_Derived_Type (Full_Base)
8954 and then Has_Discriminants (Full_Base)
8955 and then (Ekind (Current_Scope) /= E_Record_Subtype)
8956 then
8957 if not Is_Itype (Priv)
8958 and then
8959 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
8960 then
8961 Build_Underlying_Full_View
8962 (Parent (Priv), Full, Etype (Full_Base));
8963
8964 elsif Nkind (Related_Nod) = N_Component_Declaration then
8965 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
8966 end if;
8967
8968 elsif Is_Record_Type (Full_Base) then
8969
8970 -- Show Full is simply a renaming of Full_Base
8971
8972 Set_Cloned_Subtype (Full, Full_Base);
8973 end if;
8974
8975 -- It is unsafe to share to bounds of a scalar type, because the Itype
8976 -- is elaborated on demand, and if a bound is non-static then different
8977 -- orders of elaboration in different units will lead to different
8978 -- external symbols.
8979
8980 if Is_Scalar_Type (Full_Base) then
8981 Set_Scalar_Range (Full,
8982 Make_Range (Sloc (Related_Nod),
8983 Low_Bound =>
8984 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
8985 High_Bound =>
8986 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
8987
8988 -- This completion inherits the bounds of the full parent, but if
8989 -- the parent is an unconstrained floating point type, so is the
8990 -- completion.
8991
8992 if Is_Floating_Point_Type (Full_Base) then
8993 Set_Includes_Infinities
8994 (Scalar_Range (Full), Has_Infinities (Full_Base));
8995 end if;
8996 end if;
8997
8998 -- ??? It seems that a lot of fields are missing that should be copied
8999 -- from Full_Base to Full. Here are some that are introduced in a
9000 -- non-disruptive way but a cleanup is necessary.
9001
9002 if Is_Tagged_Type (Full_Base) then
9003 Set_Is_Tagged_Type (Full);
9004 Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
9005 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
9006
9007 -- If this is a subtype of a protected or task type, constrain its
9008 -- corresponding record, unless this is a subtype without constraints,
9009 -- i.e. a simple renaming as with an actual subtype in an instance.
9010
9011 elsif Is_Concurrent_Type (Full_Base) then
9012 if Has_Discriminants (Full)
9013 and then Present (Corresponding_Record_Type (Full_Base))
9014 and then
9015 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
9016 then
9017 Set_Corresponding_Record_Type (Full,
9018 Constrain_Corresponding_Record
9019 (Full, Corresponding_Record_Type (Full_Base),
9020 Related_Nod, Full_Base));
9021
9022 else
9023 Set_Corresponding_Record_Type (Full,
9024 Corresponding_Record_Type (Full_Base));
9025 end if;
9026 end if;
9027 end Complete_Private_Subtype;
9028
9029 ----------------------------
9030 -- Constant_Redeclaration --
9031 ----------------------------
9032
9033 procedure Constant_Redeclaration
9034 (Id : Entity_Id;
9035 N : Node_Id;
9036 T : out Entity_Id)
9037 is
9038 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
9039 Obj_Def : constant Node_Id := Object_Definition (N);
9040 New_T : Entity_Id;
9041
9042 procedure Check_Possible_Deferred_Completion
9043 (Prev_Id : Entity_Id;
9044 Prev_Obj_Def : Node_Id;
9045 Curr_Obj_Def : Node_Id);
9046 -- Determine whether the two object definitions describe the partial
9047 -- and the full view of a constrained deferred constant. Generate
9048 -- a subtype for the full view and verify that it statically matches
9049 -- the subtype of the partial view.
9050
9051 procedure Check_Recursive_Declaration (Typ : Entity_Id);
9052 -- If deferred constant is an access type initialized with an allocator,
9053 -- check whether there is an illegal recursion in the definition,
9054 -- through a default value of some record subcomponent. This is normally
9055 -- detected when generating init procs, but requires this additional
9056 -- mechanism when expansion is disabled.
9057
9058 ----------------------------------------
9059 -- Check_Possible_Deferred_Completion --
9060 ----------------------------------------
9061
9062 procedure Check_Possible_Deferred_Completion
9063 (Prev_Id : Entity_Id;
9064 Prev_Obj_Def : Node_Id;
9065 Curr_Obj_Def : Node_Id)
9066 is
9067 begin
9068 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
9069 and then Present (Constraint (Prev_Obj_Def))
9070 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
9071 and then Present (Constraint (Curr_Obj_Def))
9072 then
9073 declare
9074 Loc : constant Source_Ptr := Sloc (N);
9075 Def_Id : constant Entity_Id :=
9076 Make_Defining_Identifier (Loc,
9077 New_Internal_Name ('S'));
9078 Decl : constant Node_Id :=
9079 Make_Subtype_Declaration (Loc,
9080 Defining_Identifier =>
9081 Def_Id,
9082 Subtype_Indication =>
9083 Relocate_Node (Curr_Obj_Def));
9084
9085 begin
9086 Insert_Before_And_Analyze (N, Decl);
9087 Set_Etype (Id, Def_Id);
9088
9089 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
9090 Error_Msg_Sloc := Sloc (Prev_Id);
9091 Error_Msg_N ("subtype does not statically match deferred " &
9092 "declaration#", N);
9093 end if;
9094 end;
9095 end if;
9096 end Check_Possible_Deferred_Completion;
9097
9098 ---------------------------------
9099 -- Check_Recursive_Declaration --
9100 ---------------------------------
9101
9102 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
9103 Comp : Entity_Id;
9104
9105 begin
9106 if Is_Record_Type (Typ) then
9107 Comp := First_Component (Typ);
9108 while Present (Comp) loop
9109 if Comes_From_Source (Comp) then
9110 if Present (Expression (Parent (Comp)))
9111 and then Is_Entity_Name (Expression (Parent (Comp)))
9112 and then Entity (Expression (Parent (Comp))) = Prev
9113 then
9114 Error_Msg_Sloc := Sloc (Parent (Comp));
9115 Error_Msg_NE
9116 ("illegal circularity with declaration for&#",
9117 N, Comp);
9118 return;
9119
9120 elsif Is_Record_Type (Etype (Comp)) then
9121 Check_Recursive_Declaration (Etype (Comp));
9122 end if;
9123 end if;
9124
9125 Next_Component (Comp);
9126 end loop;
9127 end if;
9128 end Check_Recursive_Declaration;
9129
9130 -- Start of processing for Constant_Redeclaration
9131
9132 begin
9133 if Nkind (Parent (Prev)) = N_Object_Declaration then
9134 if Nkind (Object_Definition
9135 (Parent (Prev))) = N_Subtype_Indication
9136 then
9137 -- Find type of new declaration. The constraints of the two
9138 -- views must match statically, but there is no point in
9139 -- creating an itype for the full view.
9140
9141 if Nkind (Obj_Def) = N_Subtype_Indication then
9142 Find_Type (Subtype_Mark (Obj_Def));
9143 New_T := Entity (Subtype_Mark (Obj_Def));
9144
9145 else
9146 Find_Type (Obj_Def);
9147 New_T := Entity (Obj_Def);
9148 end if;
9149
9150 T := Etype (Prev);
9151
9152 else
9153 -- The full view may impose a constraint, even if the partial
9154 -- view does not, so construct the subtype.
9155
9156 New_T := Find_Type_Of_Object (Obj_Def, N);
9157 T := New_T;
9158 end if;
9159
9160 else
9161 -- Current declaration is illegal, diagnosed below in Enter_Name
9162
9163 T := Empty;
9164 New_T := Any_Type;
9165 end if;
9166
9167 -- If previous full declaration exists, or if a homograph is present,
9168 -- let Enter_Name handle it, either with an error, or with the removal
9169 -- of an overridden implicit subprogram.
9170
9171 if Ekind (Prev) /= E_Constant
9172 or else Present (Expression (Parent (Prev)))
9173 or else Present (Full_View (Prev))
9174 then
9175 Enter_Name (Id);
9176
9177 -- Verify that types of both declarations match, or else that both types
9178 -- are anonymous access types whose designated subtypes statically match
9179 -- (as allowed in Ada 2005 by AI-385).
9180
9181 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
9182 and then
9183 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
9184 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
9185 or else Is_Access_Constant (Etype (New_T)) /=
9186 Is_Access_Constant (Etype (Prev))
9187 or else Can_Never_Be_Null (Etype (New_T)) /=
9188 Can_Never_Be_Null (Etype (Prev))
9189 or else Null_Exclusion_Present (Parent (Prev)) /=
9190 Null_Exclusion_Present (Parent (Id))
9191 or else not Subtypes_Statically_Match
9192 (Designated_Type (Etype (Prev)),
9193 Designated_Type (Etype (New_T))))
9194 then
9195 Error_Msg_Sloc := Sloc (Prev);
9196 Error_Msg_N ("type does not match declaration#", N);
9197 Set_Full_View (Prev, Id);
9198 Set_Etype (Id, Any_Type);
9199
9200 elsif
9201 Null_Exclusion_Present (Parent (Prev))
9202 and then not Null_Exclusion_Present (N)
9203 then
9204 Error_Msg_Sloc := Sloc (Prev);
9205 Error_Msg_N ("null-exclusion does not match declaration#", N);
9206 Set_Full_View (Prev, Id);
9207 Set_Etype (Id, Any_Type);
9208
9209 -- If so, process the full constant declaration
9210
9211 else
9212 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
9213 -- the deferred declaration is constrained, then the subtype defined
9214 -- by the subtype_indication in the full declaration shall match it
9215 -- statically.
9216
9217 Check_Possible_Deferred_Completion
9218 (Prev_Id => Prev,
9219 Prev_Obj_Def => Object_Definition (Parent (Prev)),
9220 Curr_Obj_Def => Obj_Def);
9221
9222 Set_Full_View (Prev, Id);
9223 Set_Is_Public (Id, Is_Public (Prev));
9224 Set_Is_Internal (Id);
9225 Append_Entity (Id, Current_Scope);
9226
9227 -- Check ALIASED present if present before (RM 7.4(7))
9228
9229 if Is_Aliased (Prev)
9230 and then not Aliased_Present (N)
9231 then
9232 Error_Msg_Sloc := Sloc (Prev);
9233 Error_Msg_N ("ALIASED required (see declaration#)", N);
9234 end if;
9235
9236 -- Allow incomplete declaration of tags (used to handle forward
9237 -- references to tags). The check on Ada_Tags avoids circularities
9238 -- when rebuilding the compiler.
9239
9240 if RTU_Loaded (Ada_Tags)
9241 and then T = RTE (RE_Tag)
9242 then
9243 null;
9244
9245 -- Check that placement is in private part and that the incomplete
9246 -- declaration appeared in the visible part.
9247
9248 elsif Ekind (Current_Scope) = E_Package
9249 and then not In_Private_Part (Current_Scope)
9250 then
9251 Error_Msg_Sloc := Sloc (Prev);
9252 Error_Msg_N ("full constant for declaration#"
9253 & " must be in private part", N);
9254
9255 elsif Ekind (Current_Scope) = E_Package
9256 and then List_Containing (Parent (Prev))
9257 /= Visible_Declarations
9258 (Specification (Unit_Declaration_Node (Current_Scope)))
9259 then
9260 Error_Msg_N
9261 ("deferred constant must be declared in visible part",
9262 Parent (Prev));
9263 end if;
9264
9265 if Is_Access_Type (T)
9266 and then Nkind (Expression (N)) = N_Allocator
9267 then
9268 Check_Recursive_Declaration (Designated_Type (T));
9269 end if;
9270 end if;
9271 end Constant_Redeclaration;
9272
9273 ----------------------
9274 -- Constrain_Access --
9275 ----------------------
9276
9277 procedure Constrain_Access
9278 (Def_Id : in out Entity_Id;
9279 S : Node_Id;
9280 Related_Nod : Node_Id)
9281 is
9282 T : constant Entity_Id := Entity (Subtype_Mark (S));
9283 Desig_Type : constant Entity_Id := Designated_Type (T);
9284 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
9285 Constraint_OK : Boolean := True;
9286
9287 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
9288 -- Simple predicate to test for defaulted discriminants
9289 -- Shouldn't this be in sem_util???
9290
9291 ---------------------------------
9292 -- Has_Defaulted_Discriminants --
9293 ---------------------------------
9294
9295 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
9296 begin
9297 return Has_Discriminants (Typ)
9298 and then Present (First_Discriminant (Typ))
9299 and then Present
9300 (Discriminant_Default_Value (First_Discriminant (Typ)));
9301 end Has_Defaulted_Discriminants;
9302
9303 -- Start of processing for Constrain_Access
9304
9305 begin
9306 if Is_Array_Type (Desig_Type) then
9307 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
9308
9309 elsif (Is_Record_Type (Desig_Type)
9310 or else Is_Incomplete_Or_Private_Type (Desig_Type))
9311 and then not Is_Constrained (Desig_Type)
9312 then
9313 -- ??? The following code is a temporary kludge to ignore a
9314 -- discriminant constraint on access type if it is constraining
9315 -- the current record. Avoid creating the implicit subtype of the
9316 -- record we are currently compiling since right now, we cannot
9317 -- handle these. For now, just return the access type itself.
9318
9319 if Desig_Type = Current_Scope
9320 and then No (Def_Id)
9321 then
9322 Set_Ekind (Desig_Subtype, E_Record_Subtype);
9323 Def_Id := Entity (Subtype_Mark (S));
9324
9325 -- This call added to ensure that the constraint is analyzed
9326 -- (needed for a B test). Note that we still return early from
9327 -- this procedure to avoid recursive processing. ???
9328
9329 Constrain_Discriminated_Type
9330 (Desig_Subtype, S, Related_Nod, For_Access => True);
9331 return;
9332 end if;
9333
9334 if (Ekind (T) = E_General_Access_Type
9335 or else Ada_Version >= Ada_05)
9336 and then Has_Private_Declaration (Desig_Type)
9337 and then In_Open_Scopes (Scope (Desig_Type))
9338 and then Has_Discriminants (Desig_Type)
9339 then
9340 -- Enforce rule that the constraint is illegal if there is
9341 -- an unconstrained view of the designated type. This means
9342 -- that the partial view (either a private type declaration or
9343 -- a derivation from a private type) has no discriminants.
9344 -- (Defect Report 8652/0008, Technical Corrigendum 1, checked
9345 -- by ACATS B371001).
9346
9347 -- Rule updated for Ada 2005: the private type is said to have
9348 -- a constrained partial view, given that objects of the type
9349 -- can be declared. Furthermore, the rule applies to all access
9350 -- types, unlike the rule concerning default discriminants.
9351
9352 declare
9353 Pack : constant Node_Id :=
9354 Unit_Declaration_Node (Scope (Desig_Type));
9355 Decls : List_Id;
9356 Decl : Node_Id;
9357
9358 begin
9359 if Nkind (Pack) = N_Package_Declaration then
9360 Decls := Visible_Declarations (Specification (Pack));
9361 Decl := First (Decls);
9362 while Present (Decl) loop
9363 if (Nkind (Decl) = N_Private_Type_Declaration
9364 and then
9365 Chars (Defining_Identifier (Decl)) =
9366 Chars (Desig_Type))
9367
9368 or else
9369 (Nkind (Decl) = N_Full_Type_Declaration
9370 and then
9371 Chars (Defining_Identifier (Decl)) =
9372 Chars (Desig_Type)
9373 and then Is_Derived_Type (Desig_Type)
9374 and then
9375 Has_Private_Declaration (Etype (Desig_Type)))
9376 then
9377 if No (Discriminant_Specifications (Decl)) then
9378 Error_Msg_N
9379 ("cannot constrain general access type if " &
9380 "designated type has constrained partial view",
9381 S);
9382 end if;
9383
9384 exit;
9385 end if;
9386
9387 Next (Decl);
9388 end loop;
9389 end if;
9390 end;
9391 end if;
9392
9393 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
9394 For_Access => True);
9395
9396 elsif (Is_Task_Type (Desig_Type)
9397 or else Is_Protected_Type (Desig_Type))
9398 and then not Is_Constrained (Desig_Type)
9399 then
9400 Constrain_Concurrent
9401 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
9402
9403 else
9404 Error_Msg_N ("invalid constraint on access type", S);
9405 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
9406 Constraint_OK := False;
9407 end if;
9408
9409 if No (Def_Id) then
9410 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
9411 else
9412 Set_Ekind (Def_Id, E_Access_Subtype);
9413 end if;
9414
9415 if Constraint_OK then
9416 Set_Etype (Def_Id, Base_Type (T));
9417
9418 if Is_Private_Type (Desig_Type) then
9419 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
9420 end if;
9421 else
9422 Set_Etype (Def_Id, Any_Type);
9423 end if;
9424
9425 Set_Size_Info (Def_Id, T);
9426 Set_Is_Constrained (Def_Id, Constraint_OK);
9427 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
9428 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9429 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
9430
9431 Conditional_Delay (Def_Id, T);
9432
9433 -- AI-363 : Subtypes of general access types whose designated types have
9434 -- default discriminants are disallowed. In instances, the rule has to
9435 -- be checked against the actual, of which T is the subtype. In a
9436 -- generic body, the rule is checked assuming that the actual type has
9437 -- defaulted discriminants.
9438
9439 if Ada_Version >= Ada_05 or else Warn_On_Ada_2005_Compatibility then
9440 if Ekind (Base_Type (T)) = E_General_Access_Type
9441 and then Has_Defaulted_Discriminants (Desig_Type)
9442 then
9443 if Ada_Version < Ada_05 then
9444 Error_Msg_N
9445 ("access subtype of general access type would not " &
9446 "be allowed in Ada 2005?", S);
9447 else
9448 Error_Msg_N
9449 ("access subype of general access type not allowed", S);
9450 end if;
9451
9452 Error_Msg_N ("\discriminants have defaults", S);
9453
9454 elsif Is_Access_Type (T)
9455 and then Is_Generic_Type (Desig_Type)
9456 and then Has_Discriminants (Desig_Type)
9457 and then In_Package_Body (Current_Scope)
9458 then
9459 if Ada_Version < Ada_05 then
9460 Error_Msg_N
9461 ("access subtype would not be allowed in generic body " &
9462 "in Ada 2005?", S);
9463 else
9464 Error_Msg_N
9465 ("access subtype not allowed in generic body", S);
9466 end if;
9467
9468 Error_Msg_N
9469 ("\designated type is a discriminated formal", S);
9470 end if;
9471 end if;
9472 end Constrain_Access;
9473
9474 ---------------------
9475 -- Constrain_Array --
9476 ---------------------
9477
9478 procedure Constrain_Array
9479 (Def_Id : in out Entity_Id;
9480 SI : Node_Id;
9481 Related_Nod : Node_Id;
9482 Related_Id : Entity_Id;
9483 Suffix : Character)
9484 is
9485 C : constant Node_Id := Constraint (SI);
9486 Number_Of_Constraints : Nat := 0;
9487 Index : Node_Id;
9488 S, T : Entity_Id;
9489 Constraint_OK : Boolean := True;
9490
9491 begin
9492 T := Entity (Subtype_Mark (SI));
9493
9494 if Ekind (T) in Access_Kind then
9495 T := Designated_Type (T);
9496 end if;
9497
9498 -- If an index constraint follows a subtype mark in a subtype indication
9499 -- then the type or subtype denoted by the subtype mark must not already
9500 -- impose an index constraint. The subtype mark must denote either an
9501 -- unconstrained array type or an access type whose designated type
9502 -- is such an array type... (RM 3.6.1)
9503
9504 if Is_Constrained (T) then
9505 Error_Msg_N
9506 ("array type is already constrained", Subtype_Mark (SI));
9507 Constraint_OK := False;
9508
9509 else
9510 S := First (Constraints (C));
9511 while Present (S) loop
9512 Number_Of_Constraints := Number_Of_Constraints + 1;
9513 Next (S);
9514 end loop;
9515
9516 -- In either case, the index constraint must provide a discrete
9517 -- range for each index of the array type and the type of each
9518 -- discrete range must be the same as that of the corresponding
9519 -- index. (RM 3.6.1)
9520
9521 if Number_Of_Constraints /= Number_Dimensions (T) then
9522 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
9523 Constraint_OK := False;
9524
9525 else
9526 S := First (Constraints (C));
9527 Index := First_Index (T);
9528 Analyze (Index);
9529
9530 -- Apply constraints to each index type
9531
9532 for J in 1 .. Number_Of_Constraints loop
9533 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
9534 Next (Index);
9535 Next (S);
9536 end loop;
9537
9538 end if;
9539 end if;
9540
9541 if No (Def_Id) then
9542 Def_Id :=
9543 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
9544 Set_Parent (Def_Id, Related_Nod);
9545
9546 else
9547 Set_Ekind (Def_Id, E_Array_Subtype);
9548 end if;
9549
9550 Set_Size_Info (Def_Id, (T));
9551 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9552 Set_Etype (Def_Id, Base_Type (T));
9553
9554 if Constraint_OK then
9555 Set_First_Index (Def_Id, First (Constraints (C)));
9556 else
9557 Set_First_Index (Def_Id, First_Index (T));
9558 end if;
9559
9560 Set_Is_Constrained (Def_Id, True);
9561 Set_Is_Aliased (Def_Id, Is_Aliased (T));
9562 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9563
9564 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
9565 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
9566
9567 -- A subtype does not inherit the packed_array_type of is parent. We
9568 -- need to initialize the attribute because if Def_Id is previously
9569 -- analyzed through a limited_with clause, it will have the attributes
9570 -- of an incomplete type, one of which is an Elist that overlaps the
9571 -- Packed_Array_Type field.
9572
9573 Set_Packed_Array_Type (Def_Id, Empty);
9574
9575 -- Build a freeze node if parent still needs one. Also make sure that
9576 -- the Depends_On_Private status is set because the subtype will need
9577 -- reprocessing at the time the base type does, and also we must set a
9578 -- conditional delay.
9579
9580 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9581 Conditional_Delay (Def_Id, T);
9582 end Constrain_Array;
9583
9584 ------------------------------
9585 -- Constrain_Component_Type --
9586 ------------------------------
9587
9588 function Constrain_Component_Type
9589 (Comp : Entity_Id;
9590 Constrained_Typ : Entity_Id;
9591 Related_Node : Node_Id;
9592 Typ : Entity_Id;
9593 Constraints : Elist_Id) return Entity_Id
9594 is
9595 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
9596 Compon_Type : constant Entity_Id := Etype (Comp);
9597
9598 function Build_Constrained_Array_Type
9599 (Old_Type : Entity_Id) return Entity_Id;
9600 -- If Old_Type is an array type, one of whose indices is constrained
9601 -- by a discriminant, build an Itype whose constraint replaces the
9602 -- discriminant with its value in the constraint.
9603
9604 function Build_Constrained_Discriminated_Type
9605 (Old_Type : Entity_Id) return Entity_Id;
9606 -- Ditto for record components
9607
9608 function Build_Constrained_Access_Type
9609 (Old_Type : Entity_Id) return Entity_Id;
9610 -- Ditto for access types. Makes use of previous two functions, to
9611 -- constrain designated type.
9612
9613 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
9614 -- T is an array or discriminated type, C is a list of constraints
9615 -- that apply to T. This routine builds the constrained subtype.
9616
9617 function Is_Discriminant (Expr : Node_Id) return Boolean;
9618 -- Returns True if Expr is a discriminant
9619
9620 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
9621 -- Find the value of discriminant Discrim in Constraint
9622
9623 -----------------------------------
9624 -- Build_Constrained_Access_Type --
9625 -----------------------------------
9626
9627 function Build_Constrained_Access_Type
9628 (Old_Type : Entity_Id) return Entity_Id
9629 is
9630 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
9631 Itype : Entity_Id;
9632 Desig_Subtype : Entity_Id;
9633 Scop : Entity_Id;
9634
9635 begin
9636 -- if the original access type was not embedded in the enclosing
9637 -- type definition, there is no need to produce a new access
9638 -- subtype. In fact every access type with an explicit constraint
9639 -- generates an itype whose scope is the enclosing record.
9640
9641 if not Is_Type (Scope (Old_Type)) then
9642 return Old_Type;
9643
9644 elsif Is_Array_Type (Desig_Type) then
9645 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
9646
9647 elsif Has_Discriminants (Desig_Type) then
9648
9649 -- This may be an access type to an enclosing record type for
9650 -- which we are constructing the constrained components. Return
9651 -- the enclosing record subtype. This is not always correct,
9652 -- but avoids infinite recursion. ???
9653
9654 Desig_Subtype := Any_Type;
9655
9656 for J in reverse 0 .. Scope_Stack.Last loop
9657 Scop := Scope_Stack.Table (J).Entity;
9658
9659 if Is_Type (Scop)
9660 and then Base_Type (Scop) = Base_Type (Desig_Type)
9661 then
9662 Desig_Subtype := Scop;
9663 end if;
9664
9665 exit when not Is_Type (Scop);
9666 end loop;
9667
9668 if Desig_Subtype = Any_Type then
9669 Desig_Subtype :=
9670 Build_Constrained_Discriminated_Type (Desig_Type);
9671 end if;
9672
9673 else
9674 return Old_Type;
9675 end if;
9676
9677 if Desig_Subtype /= Desig_Type then
9678
9679 -- The Related_Node better be here or else we won't be able
9680 -- to attach new itypes to a node in the tree.
9681
9682 pragma Assert (Present (Related_Node));
9683
9684 Itype := Create_Itype (E_Access_Subtype, Related_Node);
9685
9686 Set_Etype (Itype, Base_Type (Old_Type));
9687 Set_Size_Info (Itype, (Old_Type));
9688 Set_Directly_Designated_Type (Itype, Desig_Subtype);
9689 Set_Depends_On_Private (Itype, Has_Private_Component
9690 (Old_Type));
9691 Set_Is_Access_Constant (Itype, Is_Access_Constant
9692 (Old_Type));
9693
9694 -- The new itype needs freezing when it depends on a not frozen
9695 -- type and the enclosing subtype needs freezing.
9696
9697 if Has_Delayed_Freeze (Constrained_Typ)
9698 and then not Is_Frozen (Constrained_Typ)
9699 then
9700 Conditional_Delay (Itype, Base_Type (Old_Type));
9701 end if;
9702
9703 return Itype;
9704
9705 else
9706 return Old_Type;
9707 end if;
9708 end Build_Constrained_Access_Type;
9709
9710 ----------------------------------
9711 -- Build_Constrained_Array_Type --
9712 ----------------------------------
9713
9714 function Build_Constrained_Array_Type
9715 (Old_Type : Entity_Id) return Entity_Id
9716 is
9717 Lo_Expr : Node_Id;
9718 Hi_Expr : Node_Id;
9719 Old_Index : Node_Id;
9720 Range_Node : Node_Id;
9721 Constr_List : List_Id;
9722
9723 Need_To_Create_Itype : Boolean := False;
9724
9725 begin
9726 Old_Index := First_Index (Old_Type);
9727 while Present (Old_Index) loop
9728 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
9729
9730 if Is_Discriminant (Lo_Expr)
9731 or else Is_Discriminant (Hi_Expr)
9732 then
9733 Need_To_Create_Itype := True;
9734 end if;
9735
9736 Next_Index (Old_Index);
9737 end loop;
9738
9739 if Need_To_Create_Itype then
9740 Constr_List := New_List;
9741
9742 Old_Index := First_Index (Old_Type);
9743 while Present (Old_Index) loop
9744 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
9745
9746 if Is_Discriminant (Lo_Expr) then
9747 Lo_Expr := Get_Discr_Value (Lo_Expr);
9748 end if;
9749
9750 if Is_Discriminant (Hi_Expr) then
9751 Hi_Expr := Get_Discr_Value (Hi_Expr);
9752 end if;
9753
9754 Range_Node :=
9755 Make_Range
9756 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
9757
9758 Append (Range_Node, To => Constr_List);
9759
9760 Next_Index (Old_Index);
9761 end loop;
9762
9763 return Build_Subtype (Old_Type, Constr_List);
9764
9765 else
9766 return Old_Type;
9767 end if;
9768 end Build_Constrained_Array_Type;
9769
9770 ------------------------------------------
9771 -- Build_Constrained_Discriminated_Type --
9772 ------------------------------------------
9773
9774 function Build_Constrained_Discriminated_Type
9775 (Old_Type : Entity_Id) return Entity_Id
9776 is
9777 Expr : Node_Id;
9778 Constr_List : List_Id;
9779 Old_Constraint : Elmt_Id;
9780
9781 Need_To_Create_Itype : Boolean := False;
9782
9783 begin
9784 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
9785 while Present (Old_Constraint) loop
9786 Expr := Node (Old_Constraint);
9787
9788 if Is_Discriminant (Expr) then
9789 Need_To_Create_Itype := True;
9790 end if;
9791
9792 Next_Elmt (Old_Constraint);
9793 end loop;
9794
9795 if Need_To_Create_Itype then
9796 Constr_List := New_List;
9797
9798 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
9799 while Present (Old_Constraint) loop
9800 Expr := Node (Old_Constraint);
9801
9802 if Is_Discriminant (Expr) then
9803 Expr := Get_Discr_Value (Expr);
9804 end if;
9805
9806 Append (New_Copy_Tree (Expr), To => Constr_List);
9807
9808 Next_Elmt (Old_Constraint);
9809 end loop;
9810
9811 return Build_Subtype (Old_Type, Constr_List);
9812
9813 else
9814 return Old_Type;
9815 end if;
9816 end Build_Constrained_Discriminated_Type;
9817
9818 -------------------
9819 -- Build_Subtype --
9820 -------------------
9821
9822 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
9823 Indic : Node_Id;
9824 Subtyp_Decl : Node_Id;
9825 Def_Id : Entity_Id;
9826 Btyp : Entity_Id := Base_Type (T);
9827
9828 begin
9829 -- The Related_Node better be here or else we won't be able to
9830 -- attach new itypes to a node in the tree.
9831
9832 pragma Assert (Present (Related_Node));
9833
9834 -- If the view of the component's type is incomplete or private
9835 -- with unknown discriminants, then the constraint must be applied
9836 -- to the full type.
9837
9838 if Has_Unknown_Discriminants (Btyp)
9839 and then Present (Underlying_Type (Btyp))
9840 then
9841 Btyp := Underlying_Type (Btyp);
9842 end if;
9843
9844 Indic :=
9845 Make_Subtype_Indication (Loc,
9846 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9847 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
9848
9849 Def_Id := Create_Itype (Ekind (T), Related_Node);
9850
9851 Subtyp_Decl :=
9852 Make_Subtype_Declaration (Loc,
9853 Defining_Identifier => Def_Id,
9854 Subtype_Indication => Indic);
9855
9856 Set_Parent (Subtyp_Decl, Parent (Related_Node));
9857
9858 -- Itypes must be analyzed with checks off (see package Itypes)
9859
9860 Analyze (Subtyp_Decl, Suppress => All_Checks);
9861
9862 return Def_Id;
9863 end Build_Subtype;
9864
9865 ---------------------
9866 -- Get_Discr_Value --
9867 ---------------------
9868
9869 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
9870 D : Entity_Id;
9871 E : Elmt_Id;
9872
9873 begin
9874 -- The discriminant may be declared for the type, in which case we
9875 -- find it by iterating over the list of discriminants. If the
9876 -- discriminant is inherited from a parent type, it appears as the
9877 -- corresponding discriminant of the current type. This will be the
9878 -- case when constraining an inherited component whose constraint is
9879 -- given by a discriminant of the parent.
9880
9881 D := First_Discriminant (Typ);
9882 E := First_Elmt (Constraints);
9883
9884 while Present (D) loop
9885 if D = Entity (Discrim)
9886 or else D = CR_Discriminant (Entity (Discrim))
9887 or else Corresponding_Discriminant (D) = Entity (Discrim)
9888 then
9889 return Node (E);
9890 end if;
9891
9892 Next_Discriminant (D);
9893 Next_Elmt (E);
9894 end loop;
9895
9896 -- The corresponding_Discriminant mechanism is incomplete, because
9897 -- the correspondence between new and old discriminants is not one
9898 -- to one: one new discriminant can constrain several old ones. In
9899 -- that case, scan sequentially the stored_constraint, the list of
9900 -- discriminants of the parents, and the constraints.
9901 -- Previous code checked for the present of the Stored_Constraint
9902 -- list for the derived type, but did not use it at all. Should it
9903 -- be present when the component is a discriminated task type?
9904
9905 if Is_Derived_Type (Typ)
9906 and then Scope (Entity (Discrim)) = Etype (Typ)
9907 then
9908 D := First_Discriminant (Etype (Typ));
9909 E := First_Elmt (Constraints);
9910 while Present (D) loop
9911 if D = Entity (Discrim) then
9912 return Node (E);
9913 end if;
9914
9915 Next_Discriminant (D);
9916 Next_Elmt (E);
9917 end loop;
9918 end if;
9919
9920 -- Something is wrong if we did not find the value
9921
9922 raise Program_Error;
9923 end Get_Discr_Value;
9924
9925 ---------------------
9926 -- Is_Discriminant --
9927 ---------------------
9928
9929 function Is_Discriminant (Expr : Node_Id) return Boolean is
9930 Discrim_Scope : Entity_Id;
9931
9932 begin
9933 if Denotes_Discriminant (Expr) then
9934 Discrim_Scope := Scope (Entity (Expr));
9935
9936 -- Either we have a reference to one of Typ's discriminants,
9937
9938 pragma Assert (Discrim_Scope = Typ
9939
9940 -- or to the discriminants of the parent type, in the case
9941 -- of a derivation of a tagged type with variants.
9942
9943 or else Discrim_Scope = Etype (Typ)
9944 or else Full_View (Discrim_Scope) = Etype (Typ)
9945
9946 -- or same as above for the case where the discriminants
9947 -- were declared in Typ's private view.
9948
9949 or else (Is_Private_Type (Discrim_Scope)
9950 and then Chars (Discrim_Scope) = Chars (Typ))
9951
9952 -- or else we are deriving from the full view and the
9953 -- discriminant is declared in the private entity.
9954
9955 or else (Is_Private_Type (Typ)
9956 and then Chars (Discrim_Scope) = Chars (Typ))
9957
9958 -- Or we are constrained the corresponding record of a
9959 -- synchronized type that completes a private declaration.
9960
9961 or else (Is_Concurrent_Record_Type (Typ)
9962 and then
9963 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
9964
9965 -- or we have a class-wide type, in which case make sure the
9966 -- discriminant found belongs to the root type.
9967
9968 or else (Is_Class_Wide_Type (Typ)
9969 and then Etype (Typ) = Discrim_Scope));
9970
9971 return True;
9972 end if;
9973
9974 -- In all other cases we have something wrong
9975
9976 return False;
9977 end Is_Discriminant;
9978
9979 -- Start of processing for Constrain_Component_Type
9980
9981 begin
9982 if Nkind (Parent (Comp)) = N_Component_Declaration
9983 and then Comes_From_Source (Parent (Comp))
9984 and then Comes_From_Source
9985 (Subtype_Indication (Component_Definition (Parent (Comp))))
9986 and then
9987 Is_Entity_Name
9988 (Subtype_Indication (Component_Definition (Parent (Comp))))
9989 then
9990 return Compon_Type;
9991
9992 elsif Is_Array_Type (Compon_Type) then
9993 return Build_Constrained_Array_Type (Compon_Type);
9994
9995 elsif Has_Discriminants (Compon_Type) then
9996 return Build_Constrained_Discriminated_Type (Compon_Type);
9997
9998 elsif Is_Access_Type (Compon_Type) then
9999 return Build_Constrained_Access_Type (Compon_Type);
10000
10001 else
10002 return Compon_Type;
10003 end if;
10004 end Constrain_Component_Type;
10005
10006 --------------------------
10007 -- Constrain_Concurrent --
10008 --------------------------
10009
10010 -- For concurrent types, the associated record value type carries the same
10011 -- discriminants, so when we constrain a concurrent type, we must constrain
10012 -- the corresponding record type as well.
10013
10014 procedure Constrain_Concurrent
10015 (Def_Id : in out Entity_Id;
10016 SI : Node_Id;
10017 Related_Nod : Node_Id;
10018 Related_Id : Entity_Id;
10019 Suffix : Character)
10020 is
10021 T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
10022 T_Val : Entity_Id;
10023
10024 begin
10025 if Ekind (T_Ent) in Access_Kind then
10026 T_Ent := Designated_Type (T_Ent);
10027 end if;
10028
10029 T_Val := Corresponding_Record_Type (T_Ent);
10030
10031 if Present (T_Val) then
10032
10033 if No (Def_Id) then
10034 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10035 end if;
10036
10037 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10038
10039 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10040 Set_Corresponding_Record_Type (Def_Id,
10041 Constrain_Corresponding_Record
10042 (Def_Id, T_Val, Related_Nod, Related_Id));
10043
10044 else
10045 -- If there is no associated record, expansion is disabled and this
10046 -- is a generic context. Create a subtype in any case, so that
10047 -- semantic analysis can proceed.
10048
10049 if No (Def_Id) then
10050 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10051 end if;
10052
10053 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10054 end if;
10055 end Constrain_Concurrent;
10056
10057 ------------------------------------
10058 -- Constrain_Corresponding_Record --
10059 ------------------------------------
10060
10061 function Constrain_Corresponding_Record
10062 (Prot_Subt : Entity_Id;
10063 Corr_Rec : Entity_Id;
10064 Related_Nod : Node_Id;
10065 Related_Id : Entity_Id) return Entity_Id
10066 is
10067 T_Sub : constant Entity_Id :=
10068 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
10069
10070 begin
10071 Set_Etype (T_Sub, Corr_Rec);
10072 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
10073 Set_Is_Constrained (T_Sub, True);
10074 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
10075 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
10076
10077 -- As elsewhere, we do not want to create a freeze node for this itype
10078 -- if it is created for a constrained component of an enclosing record
10079 -- because references to outer discriminants will appear out of scope.
10080
10081 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
10082 Conditional_Delay (T_Sub, Corr_Rec);
10083 else
10084 Set_Is_Frozen (T_Sub);
10085 end if;
10086
10087 if Has_Discriminants (Prot_Subt) then -- False only if errors.
10088 Set_Discriminant_Constraint
10089 (T_Sub, Discriminant_Constraint (Prot_Subt));
10090 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
10091 Create_Constrained_Components
10092 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
10093 end if;
10094
10095 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
10096
10097 return T_Sub;
10098 end Constrain_Corresponding_Record;
10099
10100 -----------------------
10101 -- Constrain_Decimal --
10102 -----------------------
10103
10104 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
10105 T : constant Entity_Id := Entity (Subtype_Mark (S));
10106 C : constant Node_Id := Constraint (S);
10107 Loc : constant Source_Ptr := Sloc (C);
10108 Range_Expr : Node_Id;
10109 Digits_Expr : Node_Id;
10110 Digits_Val : Uint;
10111 Bound_Val : Ureal;
10112
10113 begin
10114 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
10115
10116 if Nkind (C) = N_Range_Constraint then
10117 Range_Expr := Range_Expression (C);
10118 Digits_Val := Digits_Value (T);
10119
10120 else
10121 pragma Assert (Nkind (C) = N_Digits_Constraint);
10122 Digits_Expr := Digits_Expression (C);
10123 Analyze_And_Resolve (Digits_Expr, Any_Integer);
10124
10125 Check_Digits_Expression (Digits_Expr);
10126 Digits_Val := Expr_Value (Digits_Expr);
10127
10128 if Digits_Val > Digits_Value (T) then
10129 Error_Msg_N
10130 ("digits expression is incompatible with subtype", C);
10131 Digits_Val := Digits_Value (T);
10132 end if;
10133
10134 if Present (Range_Constraint (C)) then
10135 Range_Expr := Range_Expression (Range_Constraint (C));
10136 else
10137 Range_Expr := Empty;
10138 end if;
10139 end if;
10140
10141 Set_Etype (Def_Id, Base_Type (T));
10142 Set_Size_Info (Def_Id, (T));
10143 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10144 Set_Delta_Value (Def_Id, Delta_Value (T));
10145 Set_Scale_Value (Def_Id, Scale_Value (T));
10146 Set_Small_Value (Def_Id, Small_Value (T));
10147 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
10148 Set_Digits_Value (Def_Id, Digits_Val);
10149
10150 -- Manufacture range from given digits value if no range present
10151
10152 if No (Range_Expr) then
10153 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
10154 Range_Expr :=
10155 Make_Range (Loc,
10156 Low_Bound =>
10157 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
10158 High_Bound =>
10159 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
10160 end if;
10161
10162 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
10163 Set_Discrete_RM_Size (Def_Id);
10164
10165 -- Unconditionally delay the freeze, since we cannot set size
10166 -- information in all cases correctly until the freeze point.
10167
10168 Set_Has_Delayed_Freeze (Def_Id);
10169 end Constrain_Decimal;
10170
10171 ----------------------------------
10172 -- Constrain_Discriminated_Type --
10173 ----------------------------------
10174
10175 procedure Constrain_Discriminated_Type
10176 (Def_Id : Entity_Id;
10177 S : Node_Id;
10178 Related_Nod : Node_Id;
10179 For_Access : Boolean := False)
10180 is
10181 E : constant Entity_Id := Entity (Subtype_Mark (S));
10182 T : Entity_Id;
10183 C : Node_Id;
10184 Elist : Elist_Id := New_Elmt_List;
10185
10186 procedure Fixup_Bad_Constraint;
10187 -- This is called after finding a bad constraint, and after having
10188 -- posted an appropriate error message. The mission is to leave the
10189 -- entity T in as reasonable state as possible!
10190
10191 --------------------------
10192 -- Fixup_Bad_Constraint --
10193 --------------------------
10194
10195 procedure Fixup_Bad_Constraint is
10196 begin
10197 -- Set a reasonable Ekind for the entity. For an incomplete type,
10198 -- we can't do much, but for other types, we can set the proper
10199 -- corresponding subtype kind.
10200
10201 if Ekind (T) = E_Incomplete_Type then
10202 Set_Ekind (Def_Id, Ekind (T));
10203 else
10204 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10205 end if;
10206
10207 -- Set Etype to the known type, to reduce chances of cascaded errors
10208
10209 Set_Etype (Def_Id, E);
10210 Set_Error_Posted (Def_Id);
10211 end Fixup_Bad_Constraint;
10212
10213 -- Start of processing for Constrain_Discriminated_Type
10214
10215 begin
10216 C := Constraint (S);
10217
10218 -- A discriminant constraint is only allowed in a subtype indication,
10219 -- after a subtype mark. This subtype mark must denote either a type
10220 -- with discriminants, or an access type whose designated type is a
10221 -- type with discriminants. A discriminant constraint specifies the
10222 -- values of these discriminants (RM 3.7.2(5)).
10223
10224 T := Base_Type (Entity (Subtype_Mark (S)));
10225
10226 if Ekind (T) in Access_Kind then
10227 T := Designated_Type (T);
10228 end if;
10229
10230 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
10231 -- Avoid generating an error for access-to-incomplete subtypes.
10232
10233 if Ada_Version >= Ada_05
10234 and then Ekind (T) = E_Incomplete_Type
10235 and then Nkind (Parent (S)) = N_Subtype_Declaration
10236 and then not Is_Itype (Def_Id)
10237 then
10238 -- A little sanity check, emit an error message if the type
10239 -- has discriminants to begin with. Type T may be a regular
10240 -- incomplete type or imported via a limited with clause.
10241
10242 if Has_Discriminants (T)
10243 or else
10244 (From_With_Type (T)
10245 and then Present (Non_Limited_View (T))
10246 and then Nkind (Parent (Non_Limited_View (T))) =
10247 N_Full_Type_Declaration
10248 and then Present (Discriminant_Specifications
10249 (Parent (Non_Limited_View (T)))))
10250 then
10251 Error_Msg_N
10252 ("(Ada 2005) incomplete subtype may not be constrained", C);
10253 else
10254 Error_Msg_N
10255 ("invalid constraint: type has no discriminant", C);
10256 end if;
10257
10258 Fixup_Bad_Constraint;
10259 return;
10260
10261 -- Check that the type has visible discriminants. The type may be
10262 -- a private type with unknown discriminants whose full view has
10263 -- discriminants which are invisible.
10264
10265 elsif not Has_Discriminants (T)
10266 or else
10267 (Has_Unknown_Discriminants (T)
10268 and then Is_Private_Type (T))
10269 then
10270 Error_Msg_N ("invalid constraint: type has no discriminant", C);
10271 Fixup_Bad_Constraint;
10272 return;
10273
10274 elsif Is_Constrained (E)
10275 or else (Ekind (E) = E_Class_Wide_Subtype
10276 and then Present (Discriminant_Constraint (E)))
10277 then
10278 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
10279 Fixup_Bad_Constraint;
10280 return;
10281 end if;
10282
10283 -- T may be an unconstrained subtype (e.g. a generic actual).
10284 -- Constraint applies to the base type.
10285
10286 T := Base_Type (T);
10287
10288 Elist := Build_Discriminant_Constraints (T, S);
10289
10290 -- If the list returned was empty we had an error in building the
10291 -- discriminant constraint. We have also already signalled an error
10292 -- in the incomplete type case
10293
10294 if Is_Empty_Elmt_List (Elist) then
10295 Fixup_Bad_Constraint;
10296 return;
10297 end if;
10298
10299 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
10300 end Constrain_Discriminated_Type;
10301
10302 ---------------------------
10303 -- Constrain_Enumeration --
10304 ---------------------------
10305
10306 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
10307 T : constant Entity_Id := Entity (Subtype_Mark (S));
10308 C : constant Node_Id := Constraint (S);
10309
10310 begin
10311 Set_Ekind (Def_Id, E_Enumeration_Subtype);
10312
10313 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
10314
10315 Set_Etype (Def_Id, Base_Type (T));
10316 Set_Size_Info (Def_Id, (T));
10317 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10318 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10319
10320 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10321
10322 Set_Discrete_RM_Size (Def_Id);
10323 end Constrain_Enumeration;
10324
10325 ----------------------
10326 -- Constrain_Float --
10327 ----------------------
10328
10329 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
10330 T : constant Entity_Id := Entity (Subtype_Mark (S));
10331 C : Node_Id;
10332 D : Node_Id;
10333 Rais : Node_Id;
10334
10335 begin
10336 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
10337
10338 Set_Etype (Def_Id, Base_Type (T));
10339 Set_Size_Info (Def_Id, (T));
10340 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10341
10342 -- Process the constraint
10343
10344 C := Constraint (S);
10345
10346 -- Digits constraint present
10347
10348 if Nkind (C) = N_Digits_Constraint then
10349 Check_Restriction (No_Obsolescent_Features, C);
10350
10351 if Warn_On_Obsolescent_Feature then
10352 Error_Msg_N
10353 ("subtype digits constraint is an " &
10354 "obsolescent feature (RM J.3(8))?", C);
10355 end if;
10356
10357 D := Digits_Expression (C);
10358 Analyze_And_Resolve (D, Any_Integer);
10359 Check_Digits_Expression (D);
10360 Set_Digits_Value (Def_Id, Expr_Value (D));
10361
10362 -- Check that digits value is in range. Obviously we can do this
10363 -- at compile time, but it is strictly a runtime check, and of
10364 -- course there is an ACVC test that checks this!
10365
10366 if Digits_Value (Def_Id) > Digits_Value (T) then
10367 Error_Msg_Uint_1 := Digits_Value (T);
10368 Error_Msg_N ("?digits value is too large, maximum is ^", D);
10369 Rais :=
10370 Make_Raise_Constraint_Error (Sloc (D),
10371 Reason => CE_Range_Check_Failed);
10372 Insert_Action (Declaration_Node (Def_Id), Rais);
10373 end if;
10374
10375 C := Range_Constraint (C);
10376
10377 -- No digits constraint present
10378
10379 else
10380 Set_Digits_Value (Def_Id, Digits_Value (T));
10381 end if;
10382
10383 -- Range constraint present
10384
10385 if Nkind (C) = N_Range_Constraint then
10386 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10387
10388 -- No range constraint present
10389
10390 else
10391 pragma Assert (No (C));
10392 Set_Scalar_Range (Def_Id, Scalar_Range (T));
10393 end if;
10394
10395 Set_Is_Constrained (Def_Id);
10396 end Constrain_Float;
10397
10398 ---------------------
10399 -- Constrain_Index --
10400 ---------------------
10401
10402 procedure Constrain_Index
10403 (Index : Node_Id;
10404 S : Node_Id;
10405 Related_Nod : Node_Id;
10406 Related_Id : Entity_Id;
10407 Suffix : Character;
10408 Suffix_Index : Nat)
10409 is
10410 Def_Id : Entity_Id;
10411 R : Node_Id := Empty;
10412 T : constant Entity_Id := Etype (Index);
10413
10414 begin
10415 if Nkind (S) = N_Range
10416 or else
10417 (Nkind (S) = N_Attribute_Reference
10418 and then Attribute_Name (S) = Name_Range)
10419 then
10420 -- A Range attribute will transformed into N_Range by Resolve
10421
10422 Analyze (S);
10423 Set_Etype (S, T);
10424 R := S;
10425
10426 Process_Range_Expr_In_Decl (R, T, Empty_List);
10427
10428 if not Error_Posted (S)
10429 and then
10430 (Nkind (S) /= N_Range
10431 or else not Covers (T, (Etype (Low_Bound (S))))
10432 or else not Covers (T, (Etype (High_Bound (S)))))
10433 then
10434 if Base_Type (T) /= Any_Type
10435 and then Etype (Low_Bound (S)) /= Any_Type
10436 and then Etype (High_Bound (S)) /= Any_Type
10437 then
10438 Error_Msg_N ("range expected", S);
10439 end if;
10440 end if;
10441
10442 elsif Nkind (S) = N_Subtype_Indication then
10443
10444 -- The parser has verified that this is a discrete indication
10445
10446 Resolve_Discrete_Subtype_Indication (S, T);
10447 R := Range_Expression (Constraint (S));
10448
10449 elsif Nkind (S) = N_Discriminant_Association then
10450
10451 -- Syntactically valid in subtype indication
10452
10453 Error_Msg_N ("invalid index constraint", S);
10454 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10455 return;
10456
10457 -- Subtype_Mark case, no anonymous subtypes to construct
10458
10459 else
10460 Analyze (S);
10461
10462 if Is_Entity_Name (S) then
10463 if not Is_Type (Entity (S)) then
10464 Error_Msg_N ("expect subtype mark for index constraint", S);
10465
10466 elsif Base_Type (Entity (S)) /= Base_Type (T) then
10467 Wrong_Type (S, Base_Type (T));
10468 end if;
10469
10470 return;
10471
10472 else
10473 Error_Msg_N ("invalid index constraint", S);
10474 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10475 return;
10476 end if;
10477 end if;
10478
10479 Def_Id :=
10480 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
10481
10482 Set_Etype (Def_Id, Base_Type (T));
10483
10484 if Is_Modular_Integer_Type (T) then
10485 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10486
10487 elsif Is_Integer_Type (T) then
10488 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10489
10490 else
10491 Set_Ekind (Def_Id, E_Enumeration_Subtype);
10492 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10493 end if;
10494
10495 Set_Size_Info (Def_Id, (T));
10496 Set_RM_Size (Def_Id, RM_Size (T));
10497 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10498
10499 Set_Scalar_Range (Def_Id, R);
10500
10501 Set_Etype (S, Def_Id);
10502 Set_Discrete_RM_Size (Def_Id);
10503 end Constrain_Index;
10504
10505 -----------------------
10506 -- Constrain_Integer --
10507 -----------------------
10508
10509 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
10510 T : constant Entity_Id := Entity (Subtype_Mark (S));
10511 C : constant Node_Id := Constraint (S);
10512
10513 begin
10514 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10515
10516 if Is_Modular_Integer_Type (T) then
10517 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10518 else
10519 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10520 end if;
10521
10522 Set_Etype (Def_Id, Base_Type (T));
10523 Set_Size_Info (Def_Id, (T));
10524 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10525 Set_Discrete_RM_Size (Def_Id);
10526 end Constrain_Integer;
10527
10528 ------------------------------
10529 -- Constrain_Ordinary_Fixed --
10530 ------------------------------
10531
10532 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
10533 T : constant Entity_Id := Entity (Subtype_Mark (S));
10534 C : Node_Id;
10535 D : Node_Id;
10536 Rais : Node_Id;
10537
10538 begin
10539 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
10540 Set_Etype (Def_Id, Base_Type (T));
10541 Set_Size_Info (Def_Id, (T));
10542 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10543 Set_Small_Value (Def_Id, Small_Value (T));
10544
10545 -- Process the constraint
10546
10547 C := Constraint (S);
10548
10549 -- Delta constraint present
10550
10551 if Nkind (C) = N_Delta_Constraint then
10552 Check_Restriction (No_Obsolescent_Features, C);
10553
10554 if Warn_On_Obsolescent_Feature then
10555 Error_Msg_S
10556 ("subtype delta constraint is an " &
10557 "obsolescent feature (RM J.3(7))?");
10558 end if;
10559
10560 D := Delta_Expression (C);
10561 Analyze_And_Resolve (D, Any_Real);
10562 Check_Delta_Expression (D);
10563 Set_Delta_Value (Def_Id, Expr_Value_R (D));
10564
10565 -- Check that delta value is in range. Obviously we can do this
10566 -- at compile time, but it is strictly a runtime check, and of
10567 -- course there is an ACVC test that checks this!
10568
10569 if Delta_Value (Def_Id) < Delta_Value (T) then
10570 Error_Msg_N ("?delta value is too small", D);
10571 Rais :=
10572 Make_Raise_Constraint_Error (Sloc (D),
10573 Reason => CE_Range_Check_Failed);
10574 Insert_Action (Declaration_Node (Def_Id), Rais);
10575 end if;
10576
10577 C := Range_Constraint (C);
10578
10579 -- No delta constraint present
10580
10581 else
10582 Set_Delta_Value (Def_Id, Delta_Value (T));
10583 end if;
10584
10585 -- Range constraint present
10586
10587 if Nkind (C) = N_Range_Constraint then
10588 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10589
10590 -- No range constraint present
10591
10592 else
10593 pragma Assert (No (C));
10594 Set_Scalar_Range (Def_Id, Scalar_Range (T));
10595
10596 end if;
10597
10598 Set_Discrete_RM_Size (Def_Id);
10599
10600 -- Unconditionally delay the freeze, since we cannot set size
10601 -- information in all cases correctly until the freeze point.
10602
10603 Set_Has_Delayed_Freeze (Def_Id);
10604 end Constrain_Ordinary_Fixed;
10605
10606 -----------------------
10607 -- Contain_Interface --
10608 -----------------------
10609
10610 function Contain_Interface
10611 (Iface : Entity_Id;
10612 Ifaces : Elist_Id) return Boolean
10613 is
10614 Iface_Elmt : Elmt_Id;
10615
10616 begin
10617 if Present (Ifaces) then
10618 Iface_Elmt := First_Elmt (Ifaces);
10619 while Present (Iface_Elmt) loop
10620 if Node (Iface_Elmt) = Iface then
10621 return True;
10622 end if;
10623
10624 Next_Elmt (Iface_Elmt);
10625 end loop;
10626 end if;
10627
10628 return False;
10629 end Contain_Interface;
10630
10631 ---------------------------
10632 -- Convert_Scalar_Bounds --
10633 ---------------------------
10634
10635 procedure Convert_Scalar_Bounds
10636 (N : Node_Id;
10637 Parent_Type : Entity_Id;
10638 Derived_Type : Entity_Id;
10639 Loc : Source_Ptr)
10640 is
10641 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
10642
10643 Lo : Node_Id;
10644 Hi : Node_Id;
10645 Rng : Node_Id;
10646
10647 begin
10648 Lo := Build_Scalar_Bound
10649 (Type_Low_Bound (Derived_Type),
10650 Parent_Type, Implicit_Base);
10651
10652 Hi := Build_Scalar_Bound
10653 (Type_High_Bound (Derived_Type),
10654 Parent_Type, Implicit_Base);
10655
10656 Rng :=
10657 Make_Range (Loc,
10658 Low_Bound => Lo,
10659 High_Bound => Hi);
10660
10661 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
10662
10663 Set_Parent (Rng, N);
10664 Set_Scalar_Range (Derived_Type, Rng);
10665
10666 -- Analyze the bounds
10667
10668 Analyze_And_Resolve (Lo, Implicit_Base);
10669 Analyze_And_Resolve (Hi, Implicit_Base);
10670
10671 -- Analyze the range itself, except that we do not analyze it if
10672 -- the bounds are real literals, and we have a fixed-point type.
10673 -- The reason for this is that we delay setting the bounds in this
10674 -- case till we know the final Small and Size values (see circuit
10675 -- in Freeze.Freeze_Fixed_Point_Type for further details).
10676
10677 if Is_Fixed_Point_Type (Parent_Type)
10678 and then Nkind (Lo) = N_Real_Literal
10679 and then Nkind (Hi) = N_Real_Literal
10680 then
10681 return;
10682
10683 -- Here we do the analysis of the range
10684
10685 -- Note: we do this manually, since if we do a normal Analyze and
10686 -- Resolve call, there are problems with the conversions used for
10687 -- the derived type range.
10688
10689 else
10690 Set_Etype (Rng, Implicit_Base);
10691 Set_Analyzed (Rng, True);
10692 end if;
10693 end Convert_Scalar_Bounds;
10694
10695 -------------------
10696 -- Copy_And_Swap --
10697 -------------------
10698
10699 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
10700 begin
10701 -- Initialize new full declaration entity by copying the pertinent
10702 -- fields of the corresponding private declaration entity.
10703
10704 -- We temporarily set Ekind to a value appropriate for a type to
10705 -- avoid assert failures in Einfo from checking for setting type
10706 -- attributes on something that is not a type. Ekind (Priv) is an
10707 -- appropriate choice, since it allowed the attributes to be set
10708 -- in the first place. This Ekind value will be modified later.
10709
10710 Set_Ekind (Full, Ekind (Priv));
10711
10712 -- Also set Etype temporarily to Any_Type, again, in the absence
10713 -- of errors, it will be properly reset, and if there are errors,
10714 -- then we want a value of Any_Type to remain.
10715
10716 Set_Etype (Full, Any_Type);
10717
10718 -- Now start copying attributes
10719
10720 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
10721
10722 if Has_Discriminants (Full) then
10723 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
10724 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
10725 end if;
10726
10727 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
10728 Set_Homonym (Full, Homonym (Priv));
10729 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
10730 Set_Is_Public (Full, Is_Public (Priv));
10731 Set_Is_Pure (Full, Is_Pure (Priv));
10732 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
10733 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
10734 Set_Has_Pragma_Unreferenced_Objects
10735 (Full, Has_Pragma_Unreferenced_Objects
10736 (Priv));
10737
10738 Conditional_Delay (Full, Priv);
10739
10740 if Is_Tagged_Type (Full) then
10741 Set_Primitive_Operations (Full, Primitive_Operations (Priv));
10742
10743 if Priv = Base_Type (Priv) then
10744 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
10745 end if;
10746 end if;
10747
10748 Set_Is_Volatile (Full, Is_Volatile (Priv));
10749 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
10750 Set_Scope (Full, Scope (Priv));
10751 Set_Next_Entity (Full, Next_Entity (Priv));
10752 Set_First_Entity (Full, First_Entity (Priv));
10753 Set_Last_Entity (Full, Last_Entity (Priv));
10754
10755 -- If access types have been recorded for later handling, keep them in
10756 -- the full view so that they get handled when the full view freeze
10757 -- node is expanded.
10758
10759 if Present (Freeze_Node (Priv))
10760 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
10761 then
10762 Ensure_Freeze_Node (Full);
10763 Set_Access_Types_To_Process
10764 (Freeze_Node (Full),
10765 Access_Types_To_Process (Freeze_Node (Priv)));
10766 end if;
10767
10768 -- Swap the two entities. Now Privat is the full type entity and
10769 -- Full is the private one. They will be swapped back at the end
10770 -- of the private part. This swapping ensures that the entity that
10771 -- is visible in the private part is the full declaration.
10772
10773 Exchange_Entities (Priv, Full);
10774 Append_Entity (Full, Scope (Full));
10775 end Copy_And_Swap;
10776
10777 -------------------------------------
10778 -- Copy_Array_Base_Type_Attributes --
10779 -------------------------------------
10780
10781 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
10782 begin
10783 Set_Component_Alignment (T1, Component_Alignment (T2));
10784 Set_Component_Type (T1, Component_Type (T2));
10785 Set_Component_Size (T1, Component_Size (T2));
10786 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
10787 Set_Finalize_Storage_Only (T1, Finalize_Storage_Only (T2));
10788 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
10789 Set_Has_Task (T1, Has_Task (T2));
10790 Set_Is_Packed (T1, Is_Packed (T2));
10791 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
10792 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
10793 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
10794 end Copy_Array_Base_Type_Attributes;
10795
10796 -----------------------------------
10797 -- Copy_Array_Subtype_Attributes --
10798 -----------------------------------
10799
10800 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
10801 begin
10802 Set_Size_Info (T1, T2);
10803
10804 Set_First_Index (T1, First_Index (T2));
10805 Set_Is_Aliased (T1, Is_Aliased (T2));
10806 Set_Is_Atomic (T1, Is_Atomic (T2));
10807 Set_Is_Volatile (T1, Is_Volatile (T2));
10808 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
10809 Set_Is_Constrained (T1, Is_Constrained (T2));
10810 Set_Depends_On_Private (T1, Has_Private_Component (T2));
10811 Set_First_Rep_Item (T1, First_Rep_Item (T2));
10812 Set_Convention (T1, Convention (T2));
10813 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
10814 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
10815 end Copy_Array_Subtype_Attributes;
10816
10817 -----------------------------------
10818 -- Create_Constrained_Components --
10819 -----------------------------------
10820
10821 procedure Create_Constrained_Components
10822 (Subt : Entity_Id;
10823 Decl_Node : Node_Id;
10824 Typ : Entity_Id;
10825 Constraints : Elist_Id)
10826 is
10827 Loc : constant Source_Ptr := Sloc (Subt);
10828 Comp_List : constant Elist_Id := New_Elmt_List;
10829 Parent_Type : constant Entity_Id := Etype (Typ);
10830 Assoc_List : constant List_Id := New_List;
10831 Discr_Val : Elmt_Id;
10832 Errors : Boolean;
10833 New_C : Entity_Id;
10834 Old_C : Entity_Id;
10835 Is_Static : Boolean := True;
10836
10837 procedure Collect_Fixed_Components (Typ : Entity_Id);
10838 -- Collect parent type components that do not appear in a variant part
10839
10840 procedure Create_All_Components;
10841 -- Iterate over Comp_List to create the components of the subtype
10842
10843 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
10844 -- Creates a new component from Old_Compon, copying all the fields from
10845 -- it, including its Etype, inserts the new component in the Subt entity
10846 -- chain and returns the new component.
10847
10848 function Is_Variant_Record (T : Entity_Id) return Boolean;
10849 -- If true, and discriminants are static, collect only components from
10850 -- variants selected by discriminant values.
10851
10852 ------------------------------
10853 -- Collect_Fixed_Components --
10854 ------------------------------
10855
10856 procedure Collect_Fixed_Components (Typ : Entity_Id) is
10857 begin
10858 -- Build association list for discriminants, and find components of the
10859 -- variant part selected by the values of the discriminants.
10860
10861 Old_C := First_Discriminant (Typ);
10862 Discr_Val := First_Elmt (Constraints);
10863 while Present (Old_C) loop
10864 Append_To (Assoc_List,
10865 Make_Component_Association (Loc,
10866 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
10867 Expression => New_Copy (Node (Discr_Val))));
10868
10869 Next_Elmt (Discr_Val);
10870 Next_Discriminant (Old_C);
10871 end loop;
10872
10873 -- The tag, and the possible parent and controller components
10874 -- are unconditionally in the subtype.
10875
10876 if Is_Tagged_Type (Typ)
10877 or else Has_Controlled_Component (Typ)
10878 then
10879 Old_C := First_Component (Typ);
10880 while Present (Old_C) loop
10881 if Chars ((Old_C)) = Name_uTag
10882 or else Chars ((Old_C)) = Name_uParent
10883 or else Chars ((Old_C)) = Name_uController
10884 then
10885 Append_Elmt (Old_C, Comp_List);
10886 end if;
10887
10888 Next_Component (Old_C);
10889 end loop;
10890 end if;
10891 end Collect_Fixed_Components;
10892
10893 ---------------------------
10894 -- Create_All_Components --
10895 ---------------------------
10896
10897 procedure Create_All_Components is
10898 Comp : Elmt_Id;
10899
10900 begin
10901 Comp := First_Elmt (Comp_List);
10902 while Present (Comp) loop
10903 Old_C := Node (Comp);
10904 New_C := Create_Component (Old_C);
10905
10906 Set_Etype
10907 (New_C,
10908 Constrain_Component_Type
10909 (Old_C, Subt, Decl_Node, Typ, Constraints));
10910 Set_Is_Public (New_C, Is_Public (Subt));
10911
10912 Next_Elmt (Comp);
10913 end loop;
10914 end Create_All_Components;
10915
10916 ----------------------
10917 -- Create_Component --
10918 ----------------------
10919
10920 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
10921 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
10922
10923 begin
10924 if Ekind (Old_Compon) = E_Discriminant
10925 and then Is_Completely_Hidden (Old_Compon)
10926 then
10927 -- This is a shadow discriminant created for a discriminant of
10928 -- the parent type that is one of several renamed by the same
10929 -- new discriminant. Give the shadow discriminant an internal
10930 -- name that cannot conflict with that of visible components.
10931
10932 Set_Chars (New_Compon, New_Internal_Name ('C'));
10933 end if;
10934
10935 -- Set the parent so we have a proper link for freezing etc. This is
10936 -- not a real parent pointer, since of course our parent does not own
10937 -- up to us and reference us, we are an illegitimate child of the
10938 -- original parent!
10939
10940 Set_Parent (New_Compon, Parent (Old_Compon));
10941
10942 -- If the old component's Esize was already determined and is a
10943 -- static value, then the new component simply inherits it. Otherwise
10944 -- the old component's size may require run-time determination, but
10945 -- the new component's size still might be statically determinable
10946 -- (if, for example it has a static constraint). In that case we want
10947 -- Layout_Type to recompute the component's size, so we reset its
10948 -- size and positional fields.
10949
10950 if Frontend_Layout_On_Target
10951 and then not Known_Static_Esize (Old_Compon)
10952 then
10953 Set_Esize (New_Compon, Uint_0);
10954 Init_Normalized_First_Bit (New_Compon);
10955 Init_Normalized_Position (New_Compon);
10956 Init_Normalized_Position_Max (New_Compon);
10957 end if;
10958
10959 -- We do not want this node marked as Comes_From_Source, since
10960 -- otherwise it would get first class status and a separate cross-
10961 -- reference line would be generated. Illegitimate children do not
10962 -- rate such recognition.
10963
10964 Set_Comes_From_Source (New_Compon, False);
10965
10966 -- But it is a real entity, and a birth certificate must be properly
10967 -- registered by entering it into the entity list.
10968
10969 Enter_Name (New_Compon);
10970
10971 return New_Compon;
10972 end Create_Component;
10973
10974 -----------------------
10975 -- Is_Variant_Record --
10976 -----------------------
10977
10978 function Is_Variant_Record (T : Entity_Id) return Boolean is
10979 begin
10980 return Nkind (Parent (T)) = N_Full_Type_Declaration
10981 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
10982 and then Present (Component_List (Type_Definition (Parent (T))))
10983 and then
10984 Present
10985 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
10986 end Is_Variant_Record;
10987
10988 -- Start of processing for Create_Constrained_Components
10989
10990 begin
10991 pragma Assert (Subt /= Base_Type (Subt));
10992 pragma Assert (Typ = Base_Type (Typ));
10993
10994 Set_First_Entity (Subt, Empty);
10995 Set_Last_Entity (Subt, Empty);
10996
10997 -- Check whether constraint is fully static, in which case we can
10998 -- optimize the list of components.
10999
11000 Discr_Val := First_Elmt (Constraints);
11001 while Present (Discr_Val) loop
11002 if not Is_OK_Static_Expression (Node (Discr_Val)) then
11003 Is_Static := False;
11004 exit;
11005 end if;
11006
11007 Next_Elmt (Discr_Val);
11008 end loop;
11009
11010 Set_Has_Static_Discriminants (Subt, Is_Static);
11011
11012 Push_Scope (Subt);
11013
11014 -- Inherit the discriminants of the parent type
11015
11016 Add_Discriminants : declare
11017 Num_Disc : Int;
11018 Num_Gird : Int;
11019
11020 begin
11021 Num_Disc := 0;
11022 Old_C := First_Discriminant (Typ);
11023
11024 while Present (Old_C) loop
11025 Num_Disc := Num_Disc + 1;
11026 New_C := Create_Component (Old_C);
11027 Set_Is_Public (New_C, Is_Public (Subt));
11028 Next_Discriminant (Old_C);
11029 end loop;
11030
11031 -- For an untagged derived subtype, the number of discriminants may
11032 -- be smaller than the number of inherited discriminants, because
11033 -- several of them may be renamed by a single new discriminant.
11034 -- In this case, add the hidden discriminants back into the subtype,
11035 -- because otherwise the size of the subtype is computed incorrectly
11036 -- in GCC 4.1.
11037
11038 Num_Gird := 0;
11039
11040 if Is_Derived_Type (Typ)
11041 and then not Is_Tagged_Type (Typ)
11042 then
11043 Old_C := First_Stored_Discriminant (Typ);
11044
11045 while Present (Old_C) loop
11046 Num_Gird := Num_Gird + 1;
11047 Next_Stored_Discriminant (Old_C);
11048 end loop;
11049 end if;
11050
11051 if Num_Gird > Num_Disc then
11052
11053 -- Find out multiple uses of new discriminants, and add hidden
11054 -- components for the extra renamed discriminants. We recognize
11055 -- multiple uses through the Corresponding_Discriminant of a
11056 -- new discriminant: if it constrains several old discriminants,
11057 -- this field points to the last one in the parent type. The
11058 -- stored discriminants of the derived type have the same name
11059 -- as those of the parent.
11060
11061 declare
11062 Constr : Elmt_Id;
11063 New_Discr : Entity_Id;
11064 Old_Discr : Entity_Id;
11065
11066 begin
11067 Constr := First_Elmt (Stored_Constraint (Typ));
11068 Old_Discr := First_Stored_Discriminant (Typ);
11069 while Present (Constr) loop
11070 if Is_Entity_Name (Node (Constr))
11071 and then Ekind (Entity (Node (Constr))) = E_Discriminant
11072 then
11073 New_Discr := Entity (Node (Constr));
11074
11075 if Chars (Corresponding_Discriminant (New_Discr)) /=
11076 Chars (Old_Discr)
11077 then
11078 -- The new discriminant has been used to rename a
11079 -- subsequent old discriminant. Introduce a shadow
11080 -- component for the current old discriminant.
11081
11082 New_C := Create_Component (Old_Discr);
11083 Set_Original_Record_Component (New_C, Old_Discr);
11084 end if;
11085 end if;
11086
11087 Next_Elmt (Constr);
11088 Next_Stored_Discriminant (Old_Discr);
11089 end loop;
11090 end;
11091 end if;
11092 end Add_Discriminants;
11093
11094 if Is_Static
11095 and then Is_Variant_Record (Typ)
11096 then
11097 Collect_Fixed_Components (Typ);
11098
11099 Gather_Components (
11100 Typ,
11101 Component_List (Type_Definition (Parent (Typ))),
11102 Governed_By => Assoc_List,
11103 Into => Comp_List,
11104 Report_Errors => Errors);
11105 pragma Assert (not Errors);
11106
11107 Create_All_Components;
11108
11109 -- If the subtype declaration is created for a tagged type derivation
11110 -- with constraints, we retrieve the record definition of the parent
11111 -- type to select the components of the proper variant.
11112
11113 elsif Is_Static
11114 and then Is_Tagged_Type (Typ)
11115 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11116 and then
11117 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
11118 and then Is_Variant_Record (Parent_Type)
11119 then
11120 Collect_Fixed_Components (Typ);
11121
11122 Gather_Components (
11123 Typ,
11124 Component_List (Type_Definition (Parent (Parent_Type))),
11125 Governed_By => Assoc_List,
11126 Into => Comp_List,
11127 Report_Errors => Errors);
11128 pragma Assert (not Errors);
11129
11130 -- If the tagged derivation has a type extension, collect all the
11131 -- new components therein.
11132
11133 if Present
11134 (Record_Extension_Part (Type_Definition (Parent (Typ))))
11135 then
11136 Old_C := First_Component (Typ);
11137 while Present (Old_C) loop
11138 if Original_Record_Component (Old_C) = Old_C
11139 and then Chars (Old_C) /= Name_uTag
11140 and then Chars (Old_C) /= Name_uParent
11141 and then Chars (Old_C) /= Name_uController
11142 then
11143 Append_Elmt (Old_C, Comp_List);
11144 end if;
11145
11146 Next_Component (Old_C);
11147 end loop;
11148 end if;
11149
11150 Create_All_Components;
11151
11152 else
11153 -- If discriminants are not static, or if this is a multi-level type
11154 -- extension, we have to include all components of the parent type.
11155
11156 Old_C := First_Component (Typ);
11157 while Present (Old_C) loop
11158 New_C := Create_Component (Old_C);
11159
11160 Set_Etype
11161 (New_C,
11162 Constrain_Component_Type
11163 (Old_C, Subt, Decl_Node, Typ, Constraints));
11164 Set_Is_Public (New_C, Is_Public (Subt));
11165
11166 Next_Component (Old_C);
11167 end loop;
11168 end if;
11169
11170 End_Scope;
11171 end Create_Constrained_Components;
11172
11173 ------------------------------------------
11174 -- Decimal_Fixed_Point_Type_Declaration --
11175 ------------------------------------------
11176
11177 procedure Decimal_Fixed_Point_Type_Declaration
11178 (T : Entity_Id;
11179 Def : Node_Id)
11180 is
11181 Loc : constant Source_Ptr := Sloc (Def);
11182 Digs_Expr : constant Node_Id := Digits_Expression (Def);
11183 Delta_Expr : constant Node_Id := Delta_Expression (Def);
11184 Implicit_Base : Entity_Id;
11185 Digs_Val : Uint;
11186 Delta_Val : Ureal;
11187 Scale_Val : Uint;
11188 Bound_Val : Ureal;
11189
11190 begin
11191 Check_Restriction (No_Fixed_Point, Def);
11192
11193 -- Create implicit base type
11194
11195 Implicit_Base :=
11196 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
11197 Set_Etype (Implicit_Base, Implicit_Base);
11198
11199 -- Analyze and process delta expression
11200
11201 Analyze_And_Resolve (Delta_Expr, Universal_Real);
11202
11203 Check_Delta_Expression (Delta_Expr);
11204 Delta_Val := Expr_Value_R (Delta_Expr);
11205
11206 -- Check delta is power of 10, and determine scale value from it
11207
11208 declare
11209 Val : Ureal;
11210
11211 begin
11212 Scale_Val := Uint_0;
11213 Val := Delta_Val;
11214
11215 if Val < Ureal_1 then
11216 while Val < Ureal_1 loop
11217 Val := Val * Ureal_10;
11218 Scale_Val := Scale_Val + 1;
11219 end loop;
11220
11221 if Scale_Val > 18 then
11222 Error_Msg_N ("scale exceeds maximum value of 18", Def);
11223 Scale_Val := UI_From_Int (+18);
11224 end if;
11225
11226 else
11227 while Val > Ureal_1 loop
11228 Val := Val / Ureal_10;
11229 Scale_Val := Scale_Val - 1;
11230 end loop;
11231
11232 if Scale_Val < -18 then
11233 Error_Msg_N ("scale is less than minimum value of -18", Def);
11234 Scale_Val := UI_From_Int (-18);
11235 end if;
11236 end if;
11237
11238 if Val /= Ureal_1 then
11239 Error_Msg_N ("delta expression must be a power of 10", Def);
11240 Delta_Val := Ureal_10 ** (-Scale_Val);
11241 end if;
11242 end;
11243
11244 -- Set delta, scale and small (small = delta for decimal type)
11245
11246 Set_Delta_Value (Implicit_Base, Delta_Val);
11247 Set_Scale_Value (Implicit_Base, Scale_Val);
11248 Set_Small_Value (Implicit_Base, Delta_Val);
11249
11250 -- Analyze and process digits expression
11251
11252 Analyze_And_Resolve (Digs_Expr, Any_Integer);
11253 Check_Digits_Expression (Digs_Expr);
11254 Digs_Val := Expr_Value (Digs_Expr);
11255
11256 if Digs_Val > 18 then
11257 Digs_Val := UI_From_Int (+18);
11258 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
11259 end if;
11260
11261 Set_Digits_Value (Implicit_Base, Digs_Val);
11262 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
11263
11264 -- Set range of base type from digits value for now. This will be
11265 -- expanded to represent the true underlying base range by Freeze.
11266
11267 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
11268
11269 -- Note: We leave size as zero for now, size will be set at freeze
11270 -- time. We have to do this for ordinary fixed-point, because the size
11271 -- depends on the specified small, and we might as well do the same for
11272 -- decimal fixed-point.
11273
11274 pragma Assert (Esize (Implicit_Base) = Uint_0);
11275
11276 -- If there are bounds given in the declaration use them as the
11277 -- bounds of the first named subtype.
11278
11279 if Present (Real_Range_Specification (Def)) then
11280 declare
11281 RRS : constant Node_Id := Real_Range_Specification (Def);
11282 Low : constant Node_Id := Low_Bound (RRS);
11283 High : constant Node_Id := High_Bound (RRS);
11284 Low_Val : Ureal;
11285 High_Val : Ureal;
11286
11287 begin
11288 Analyze_And_Resolve (Low, Any_Real);
11289 Analyze_And_Resolve (High, Any_Real);
11290 Check_Real_Bound (Low);
11291 Check_Real_Bound (High);
11292 Low_Val := Expr_Value_R (Low);
11293 High_Val := Expr_Value_R (High);
11294
11295 if Low_Val < (-Bound_Val) then
11296 Error_Msg_N
11297 ("range low bound too small for digits value", Low);
11298 Low_Val := -Bound_Val;
11299 end if;
11300
11301 if High_Val > Bound_Val then
11302 Error_Msg_N
11303 ("range high bound too large for digits value", High);
11304 High_Val := Bound_Val;
11305 end if;
11306
11307 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
11308 end;
11309
11310 -- If no explicit range, use range that corresponds to given
11311 -- digits value. This will end up as the final range for the
11312 -- first subtype.
11313
11314 else
11315 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
11316 end if;
11317
11318 -- Complete entity for first subtype
11319
11320 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
11321 Set_Etype (T, Implicit_Base);
11322 Set_Size_Info (T, Implicit_Base);
11323 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
11324 Set_Digits_Value (T, Digs_Val);
11325 Set_Delta_Value (T, Delta_Val);
11326 Set_Small_Value (T, Delta_Val);
11327 Set_Scale_Value (T, Scale_Val);
11328 Set_Is_Constrained (T);
11329 end Decimal_Fixed_Point_Type_Declaration;
11330
11331 -----------------------------------
11332 -- Derive_Progenitor_Subprograms --
11333 -----------------------------------
11334
11335 procedure Derive_Progenitor_Subprograms
11336 (Parent_Type : Entity_Id;
11337 Tagged_Type : Entity_Id)
11338 is
11339 E : Entity_Id;
11340 Elmt : Elmt_Id;
11341 Iface : Entity_Id;
11342 Iface_Elmt : Elmt_Id;
11343 Iface_Subp : Entity_Id;
11344 New_Subp : Entity_Id := Empty;
11345 Prim_Elmt : Elmt_Id;
11346 Subp : Entity_Id;
11347 Typ : Entity_Id;
11348
11349 begin
11350 pragma Assert (Ada_Version >= Ada_05
11351 and then Is_Record_Type (Tagged_Type)
11352 and then Is_Tagged_Type (Tagged_Type)
11353 and then Has_Interfaces (Tagged_Type));
11354
11355 -- Step 1: Transfer to the full-view primitives associated with the
11356 -- partial-view that cover interface primitives. Conceptually this
11357 -- work should be done later by Process_Full_View; done here to
11358 -- simplify its implementation at later stages. It can be safely
11359 -- done here because interfaces must be visible in the partial and
11360 -- private view (RM 7.3(7.3/2)).
11361
11362 -- Small optimization: This work is only required if the parent is
11363 -- abstract. If the tagged type is not abstract, it cannot have
11364 -- abstract primitives (the only entities in the list of primitives of
11365 -- non-abstract tagged types that can reference abstract primitives
11366 -- through its Alias attribute are the internal entities that have
11367 -- attribute Interface_Alias, and these entities are generated later
11368 -- by Freeze_Record_Type).
11369
11370 if In_Private_Part (Current_Scope)
11371 and then Is_Abstract_Type (Parent_Type)
11372 then
11373 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
11374 while Present (Elmt) loop
11375 Subp := Node (Elmt);
11376
11377 -- At this stage it is not possible to have entities in the list
11378 -- of primitives that have attribute Interface_Alias
11379
11380 pragma Assert (No (Interface_Alias (Subp)));
11381
11382 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
11383
11384 if Is_Interface (Typ) then
11385 E := Find_Primitive_Covering_Interface
11386 (Tagged_Type => Tagged_Type,
11387 Iface_Prim => Subp);
11388
11389 if Present (E)
11390 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
11391 then
11392 Replace_Elmt (Elmt, E);
11393 Remove_Homonym (Subp);
11394 end if;
11395 end if;
11396
11397 Next_Elmt (Elmt);
11398 end loop;
11399 end if;
11400
11401 -- Step 2: Add primitives of progenitors that are not implemented by
11402 -- parents of Tagged_Type
11403
11404 if Present (Interfaces (Tagged_Type)) then
11405 Iface_Elmt := First_Elmt (Interfaces (Tagged_Type));
11406 while Present (Iface_Elmt) loop
11407 Iface := Node (Iface_Elmt);
11408
11409 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
11410 while Present (Prim_Elmt) loop
11411 Iface_Subp := Node (Prim_Elmt);
11412
11413 -- Exclude derivation of predefined primitives except those
11414 -- that come from source. Required to catch declarations of
11415 -- equality operators of interfaces. For example:
11416
11417 -- type Iface is interface;
11418 -- function "=" (Left, Right : Iface) return Boolean;
11419
11420 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
11421 or else Comes_From_Source (Iface_Subp)
11422 then
11423 E := Find_Primitive_Covering_Interface
11424 (Tagged_Type => Tagged_Type,
11425 Iface_Prim => Iface_Subp);
11426
11427 -- If not found we derive a new primitive leaving its alias
11428 -- attribute referencing the interface primitive
11429
11430 if No (E) then
11431 Derive_Subprogram
11432 (New_Subp, Iface_Subp, Tagged_Type, Iface);
11433
11434 -- Propagate to the full view interface entities associated
11435 -- with the partial view
11436
11437 elsif In_Private_Part (Current_Scope)
11438 and then Present (Alias (E))
11439 and then Alias (E) = Iface_Subp
11440 and then
11441 List_Containing (Parent (E)) /=
11442 Private_Declarations
11443 (Specification
11444 (Unit_Declaration_Node (Current_Scope)))
11445 then
11446 Append_Elmt (E, Primitive_Operations (Tagged_Type));
11447 end if;
11448 end if;
11449
11450 Next_Elmt (Prim_Elmt);
11451 end loop;
11452
11453 Next_Elmt (Iface_Elmt);
11454 end loop;
11455 end if;
11456 end Derive_Progenitor_Subprograms;
11457
11458 -----------------------
11459 -- Derive_Subprogram --
11460 -----------------------
11461
11462 procedure Derive_Subprogram
11463 (New_Subp : in out Entity_Id;
11464 Parent_Subp : Entity_Id;
11465 Derived_Type : Entity_Id;
11466 Parent_Type : Entity_Id;
11467 Actual_Subp : Entity_Id := Empty)
11468 is
11469 Formal : Entity_Id;
11470 -- Formal parameter of parent primitive operation
11471
11472 Formal_Of_Actual : Entity_Id;
11473 -- Formal parameter of actual operation, when the derivation is to
11474 -- create a renaming for a primitive operation of an actual in an
11475 -- instantiation.
11476
11477 New_Formal : Entity_Id;
11478 -- Formal of inherited operation
11479
11480 Visible_Subp : Entity_Id := Parent_Subp;
11481
11482 function Is_Private_Overriding return Boolean;
11483 -- If Subp is a private overriding of a visible operation, the inherited
11484 -- operation derives from the overridden op (even though its body is the
11485 -- overriding one) and the inherited operation is visible now. See
11486 -- sem_disp to see the full details of the handling of the overridden
11487 -- subprogram, which is removed from the list of primitive operations of
11488 -- the type. The overridden subprogram is saved locally in Visible_Subp,
11489 -- and used to diagnose abstract operations that need overriding in the
11490 -- derived type.
11491
11492 procedure Replace_Type (Id, New_Id : Entity_Id);
11493 -- When the type is an anonymous access type, create a new access type
11494 -- designating the derived type.
11495
11496 procedure Set_Derived_Name;
11497 -- This procedure sets the appropriate Chars name for New_Subp. This
11498 -- is normally just a copy of the parent name. An exception arises for
11499 -- type support subprograms, where the name is changed to reflect the
11500 -- name of the derived type, e.g. if type foo is derived from type bar,
11501 -- then a procedure barDA is derived with a name fooDA.
11502
11503 ---------------------------
11504 -- Is_Private_Overriding --
11505 ---------------------------
11506
11507 function Is_Private_Overriding return Boolean is
11508 Prev : Entity_Id;
11509
11510 begin
11511 -- If the parent is not a dispatching operation there is no
11512 -- need to investigate overridings
11513
11514 if not Is_Dispatching_Operation (Parent_Subp) then
11515 return False;
11516 end if;
11517
11518 -- The visible operation that is overridden is a homonym of the
11519 -- parent subprogram. We scan the homonym chain to find the one
11520 -- whose alias is the subprogram we are deriving.
11521
11522 Prev := Current_Entity (Parent_Subp);
11523 while Present (Prev) loop
11524 if Ekind (Prev) = Ekind (Parent_Subp)
11525 and then Alias (Prev) = Parent_Subp
11526 and then Scope (Parent_Subp) = Scope (Prev)
11527 and then not Is_Hidden (Prev)
11528 then
11529 Visible_Subp := Prev;
11530 return True;
11531 end if;
11532
11533 Prev := Homonym (Prev);
11534 end loop;
11535
11536 return False;
11537 end Is_Private_Overriding;
11538
11539 ------------------
11540 -- Replace_Type --
11541 ------------------
11542
11543 procedure Replace_Type (Id, New_Id : Entity_Id) is
11544 Acc_Type : Entity_Id;
11545 Par : constant Node_Id := Parent (Derived_Type);
11546
11547 begin
11548 -- When the type is an anonymous access type, create a new access
11549 -- type designating the derived type. This itype must be elaborated
11550 -- at the point of the derivation, not on subsequent calls that may
11551 -- be out of the proper scope for Gigi, so we insert a reference to
11552 -- it after the derivation.
11553
11554 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
11555 declare
11556 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
11557
11558 begin
11559 if Ekind (Desig_Typ) = E_Record_Type_With_Private
11560 and then Present (Full_View (Desig_Typ))
11561 and then not Is_Private_Type (Parent_Type)
11562 then
11563 Desig_Typ := Full_View (Desig_Typ);
11564 end if;
11565
11566 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
11567
11568 -- Ada 2005 (AI-251): Handle also derivations of abstract
11569 -- interface primitives.
11570
11571 or else (Is_Interface (Desig_Typ)
11572 and then not Is_Class_Wide_Type (Desig_Typ))
11573 then
11574 Acc_Type := New_Copy (Etype (Id));
11575 Set_Etype (Acc_Type, Acc_Type);
11576 Set_Scope (Acc_Type, New_Subp);
11577
11578 -- Compute size of anonymous access type
11579
11580 if Is_Array_Type (Desig_Typ)
11581 and then not Is_Constrained (Desig_Typ)
11582 then
11583 Init_Size (Acc_Type, 2 * System_Address_Size);
11584 else
11585 Init_Size (Acc_Type, System_Address_Size);
11586 end if;
11587
11588 Init_Alignment (Acc_Type);
11589 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
11590
11591 Set_Etype (New_Id, Acc_Type);
11592 Set_Scope (New_Id, New_Subp);
11593
11594 -- Create a reference to it
11595 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
11596
11597 else
11598 Set_Etype (New_Id, Etype (Id));
11599 end if;
11600 end;
11601
11602 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
11603 or else
11604 (Ekind (Etype (Id)) = E_Record_Type_With_Private
11605 and then Present (Full_View (Etype (Id)))
11606 and then
11607 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
11608 then
11609 -- Constraint checks on formals are generated during expansion,
11610 -- based on the signature of the original subprogram. The bounds
11611 -- of the derived type are not relevant, and thus we can use
11612 -- the base type for the formals. However, the return type may be
11613 -- used in a context that requires that the proper static bounds
11614 -- be used (a case statement, for example) and for those cases
11615 -- we must use the derived type (first subtype), not its base.
11616
11617 -- If the derived_type_definition has no constraints, we know that
11618 -- the derived type has the same constraints as the first subtype
11619 -- of the parent, and we can also use it rather than its base,
11620 -- which can lead to more efficient code.
11621
11622 if Etype (Id) = Parent_Type then
11623 if Is_Scalar_Type (Parent_Type)
11624 and then
11625 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
11626 then
11627 Set_Etype (New_Id, Derived_Type);
11628
11629 elsif Nkind (Par) = N_Full_Type_Declaration
11630 and then
11631 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
11632 and then
11633 Is_Entity_Name
11634 (Subtype_Indication (Type_Definition (Par)))
11635 then
11636 Set_Etype (New_Id, Derived_Type);
11637
11638 else
11639 Set_Etype (New_Id, Base_Type (Derived_Type));
11640 end if;
11641
11642 else
11643 Set_Etype (New_Id, Base_Type (Derived_Type));
11644 end if;
11645
11646 -- Ada 2005 (AI-251): Handle derivations of abstract interface
11647 -- primitives.
11648
11649 elsif Is_Interface (Etype (Id))
11650 and then not Is_Class_Wide_Type (Etype (Id))
11651 and then Is_Progenitor (Etype (Id), Derived_Type)
11652 then
11653 Set_Etype (New_Id, Derived_Type);
11654
11655 else
11656 Set_Etype (New_Id, Etype (Id));
11657 end if;
11658 end Replace_Type;
11659
11660 ----------------------
11661 -- Set_Derived_Name --
11662 ----------------------
11663
11664 procedure Set_Derived_Name is
11665 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
11666 begin
11667 if Nm = TSS_Null then
11668 Set_Chars (New_Subp, Chars (Parent_Subp));
11669 else
11670 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
11671 end if;
11672 end Set_Derived_Name;
11673
11674 -- Local variables
11675
11676 Parent_Overrides_Interface_Primitive : Boolean := False;
11677
11678 -- Start of processing for Derive_Subprogram
11679
11680 begin
11681 New_Subp :=
11682 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
11683 Set_Ekind (New_Subp, Ekind (Parent_Subp));
11684
11685 -- Check whether the parent overrides an interface primitive
11686
11687 if Is_Overriding_Operation (Parent_Subp) then
11688 declare
11689 E : Entity_Id := Parent_Subp;
11690 begin
11691 while Present (Overridden_Operation (E)) loop
11692 E := Ultimate_Alias (Overridden_Operation (E));
11693 end loop;
11694
11695 Parent_Overrides_Interface_Primitive :=
11696 Is_Dispatching_Operation (E)
11697 and then Present (Find_Dispatching_Type (E))
11698 and then Is_Interface (Find_Dispatching_Type (E));
11699 end;
11700 end if;
11701
11702 -- Check whether the inherited subprogram is a private operation that
11703 -- should be inherited but not yet made visible. Such subprograms can
11704 -- become visible at a later point (e.g., the private part of a public
11705 -- child unit) via Declare_Inherited_Private_Subprograms. If the
11706 -- following predicate is true, then this is not such a private
11707 -- operation and the subprogram simply inherits the name of the parent
11708 -- subprogram. Note the special check for the names of controlled
11709 -- operations, which are currently exempted from being inherited with
11710 -- a hidden name because they must be findable for generation of
11711 -- implicit run-time calls.
11712
11713 if not Is_Hidden (Parent_Subp)
11714 or else Is_Internal (Parent_Subp)
11715 or else Is_Private_Overriding
11716 or else Is_Internal_Name (Chars (Parent_Subp))
11717 or else Chars (Parent_Subp) = Name_Initialize
11718 or else Chars (Parent_Subp) = Name_Adjust
11719 or else Chars (Parent_Subp) = Name_Finalize
11720 then
11721 Set_Derived_Name;
11722
11723 -- If parent is hidden, this can be a regular derivation if the
11724 -- parent is immediately visible in a non-instantiating context,
11725 -- or if we are in the private part of an instance. This test
11726 -- should still be refined ???
11727
11728 -- The test for In_Instance_Not_Visible avoids inheriting the derived
11729 -- operation as a non-visible operation in cases where the parent
11730 -- subprogram might not be visible now, but was visible within the
11731 -- original generic, so it would be wrong to make the inherited
11732 -- subprogram non-visible now. (Not clear if this test is fully
11733 -- correct; are there any cases where we should declare the inherited
11734 -- operation as not visible to avoid it being overridden, e.g., when
11735 -- the parent type is a generic actual with private primitives ???)
11736
11737 -- (they should be treated the same as other private inherited
11738 -- subprograms, but it's not clear how to do this cleanly). ???
11739
11740 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
11741 and then Is_Immediately_Visible (Parent_Subp)
11742 and then not In_Instance)
11743 or else In_Instance_Not_Visible
11744 then
11745 Set_Derived_Name;
11746
11747 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
11748 -- overrides an interface primitive because interface primitives
11749 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
11750
11751 elsif Parent_Overrides_Interface_Primitive then
11752 Set_Derived_Name;
11753
11754 -- The type is inheriting a private operation, so enter
11755 -- it with a special name so it can't be overridden.
11756
11757 else
11758 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
11759 end if;
11760
11761 Set_Parent (New_Subp, Parent (Derived_Type));
11762
11763 if Present (Actual_Subp) then
11764 Replace_Type (Actual_Subp, New_Subp);
11765 else
11766 Replace_Type (Parent_Subp, New_Subp);
11767 end if;
11768
11769 Conditional_Delay (New_Subp, Parent_Subp);
11770
11771 -- If we are creating a renaming for a primitive operation of an
11772 -- actual of a generic derived type, we must examine the signature
11773 -- of the actual primitive, not that of the generic formal, which for
11774 -- example may be an interface. However the name and initial value
11775 -- of the inherited operation are those of the formal primitive.
11776
11777 Formal := First_Formal (Parent_Subp);
11778
11779 if Present (Actual_Subp) then
11780 Formal_Of_Actual := First_Formal (Actual_Subp);
11781 else
11782 Formal_Of_Actual := Empty;
11783 end if;
11784
11785 while Present (Formal) loop
11786 New_Formal := New_Copy (Formal);
11787
11788 -- Normally we do not go copying parents, but in the case of
11789 -- formals, we need to link up to the declaration (which is the
11790 -- parameter specification), and it is fine to link up to the
11791 -- original formal's parameter specification in this case.
11792
11793 Set_Parent (New_Formal, Parent (Formal));
11794 Append_Entity (New_Formal, New_Subp);
11795
11796 if Present (Formal_Of_Actual) then
11797 Replace_Type (Formal_Of_Actual, New_Formal);
11798 Next_Formal (Formal_Of_Actual);
11799 else
11800 Replace_Type (Formal, New_Formal);
11801 end if;
11802
11803 Next_Formal (Formal);
11804 end loop;
11805
11806 -- If this derivation corresponds to a tagged generic actual, then
11807 -- primitive operations rename those of the actual. Otherwise the
11808 -- primitive operations rename those of the parent type, If the parent
11809 -- renames an intrinsic operator, so does the new subprogram. We except
11810 -- concatenation, which is always properly typed, and does not get
11811 -- expanded as other intrinsic operations.
11812
11813 if No (Actual_Subp) then
11814 if Is_Intrinsic_Subprogram (Parent_Subp) then
11815 Set_Is_Intrinsic_Subprogram (New_Subp);
11816
11817 if Present (Alias (Parent_Subp))
11818 and then Chars (Parent_Subp) /= Name_Op_Concat
11819 then
11820 Set_Alias (New_Subp, Alias (Parent_Subp));
11821 else
11822 Set_Alias (New_Subp, Parent_Subp);
11823 end if;
11824
11825 else
11826 Set_Alias (New_Subp, Parent_Subp);
11827 end if;
11828
11829 else
11830 Set_Alias (New_Subp, Actual_Subp);
11831 end if;
11832
11833 -- Derived subprograms of a tagged type must inherit the convention
11834 -- of the parent subprogram (a requirement of AI-117). Derived
11835 -- subprograms of untagged types simply get convention Ada by default.
11836
11837 if Is_Tagged_Type (Derived_Type) then
11838 Set_Convention (New_Subp, Convention (Parent_Subp));
11839 end if;
11840
11841 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
11842 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
11843
11844 if Ekind (Parent_Subp) = E_Procedure then
11845 Set_Is_Valued_Procedure
11846 (New_Subp, Is_Valued_Procedure (Parent_Subp));
11847 end if;
11848
11849 -- No_Return must be inherited properly. If this is overridden in the
11850 -- case of a dispatching operation, then a check is made in Sem_Disp
11851 -- that the overriding operation is also No_Return (no such check is
11852 -- required for the case of non-dispatching operation.
11853
11854 Set_No_Return (New_Subp, No_Return (Parent_Subp));
11855
11856 -- A derived function with a controlling result is abstract. If the
11857 -- Derived_Type is a nonabstract formal generic derived type, then
11858 -- inherited operations are not abstract: the required check is done at
11859 -- instantiation time. If the derivation is for a generic actual, the
11860 -- function is not abstract unless the actual is.
11861
11862 if Is_Generic_Type (Derived_Type)
11863 and then not Is_Abstract_Type (Derived_Type)
11864 then
11865 null;
11866
11867 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
11868 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
11869
11870 elsif Ada_Version >= Ada_05
11871 and then (Is_Abstract_Subprogram (Alias (New_Subp))
11872 or else (Is_Tagged_Type (Derived_Type)
11873 and then Etype (New_Subp) = Derived_Type
11874 and then not Is_Null_Extension (Derived_Type))
11875 or else (Is_Tagged_Type (Derived_Type)
11876 and then Ekind (Etype (New_Subp)) =
11877 E_Anonymous_Access_Type
11878 and then Designated_Type (Etype (New_Subp)) =
11879 Derived_Type
11880 and then not Is_Null_Extension (Derived_Type)))
11881 and then No (Actual_Subp)
11882 then
11883 if not Is_Tagged_Type (Derived_Type)
11884 or else Is_Abstract_Type (Derived_Type)
11885 or else Is_Abstract_Subprogram (Alias (New_Subp))
11886 then
11887 Set_Is_Abstract_Subprogram (New_Subp);
11888 else
11889 Set_Requires_Overriding (New_Subp);
11890 end if;
11891
11892 elsif Ada_Version < Ada_05
11893 and then (Is_Abstract_Subprogram (Alias (New_Subp))
11894 or else (Is_Tagged_Type (Derived_Type)
11895 and then Etype (New_Subp) = Derived_Type
11896 and then No (Actual_Subp)))
11897 then
11898 Set_Is_Abstract_Subprogram (New_Subp);
11899
11900 -- Finally, if the parent type is abstract we must verify that all
11901 -- inherited operations are either non-abstract or overridden, or that
11902 -- the derived type itself is abstract (this check is performed at the
11903 -- end of a package declaration, in Check_Abstract_Overriding). A
11904 -- private overriding in the parent type will not be visible in the
11905 -- derivation if we are not in an inner package or in a child unit of
11906 -- the parent type, in which case the abstractness of the inherited
11907 -- operation is carried to the new subprogram.
11908
11909 elsif Is_Abstract_Type (Parent_Type)
11910 and then not In_Open_Scopes (Scope (Parent_Type))
11911 and then Is_Private_Overriding
11912 and then Is_Abstract_Subprogram (Visible_Subp)
11913 then
11914 if No (Actual_Subp) then
11915 Set_Alias (New_Subp, Visible_Subp);
11916 Set_Is_Abstract_Subprogram
11917 (New_Subp, True);
11918 else
11919 -- If this is a derivation for an instance of a formal derived
11920 -- type, abstractness comes from the primitive operation of the
11921 -- actual, not from the operation inherited from the ancestor.
11922
11923 Set_Is_Abstract_Subprogram
11924 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
11925 end if;
11926 end if;
11927
11928 New_Overloaded_Entity (New_Subp, Derived_Type);
11929
11930 -- Check for case of a derived subprogram for the instantiation of a
11931 -- formal derived tagged type, if so mark the subprogram as dispatching
11932 -- and inherit the dispatching attributes of the parent subprogram. The
11933 -- derived subprogram is effectively renaming of the actual subprogram,
11934 -- so it needs to have the same attributes as the actual.
11935
11936 if Present (Actual_Subp)
11937 and then Is_Dispatching_Operation (Parent_Subp)
11938 then
11939 Set_Is_Dispatching_Operation (New_Subp);
11940
11941 if Present (DTC_Entity (Parent_Subp)) then
11942 Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
11943 Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
11944 end if;
11945 end if;
11946
11947 -- Indicate that a derived subprogram does not require a body and that
11948 -- it does not require processing of default expressions.
11949
11950 Set_Has_Completion (New_Subp);
11951 Set_Default_Expressions_Processed (New_Subp);
11952
11953 if Ekind (New_Subp) = E_Function then
11954 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
11955 end if;
11956 end Derive_Subprogram;
11957
11958 ------------------------
11959 -- Derive_Subprograms --
11960 ------------------------
11961
11962 procedure Derive_Subprograms
11963 (Parent_Type : Entity_Id;
11964 Derived_Type : Entity_Id;
11965 Generic_Actual : Entity_Id := Empty)
11966 is
11967 Op_List : constant Elist_Id :=
11968 Collect_Primitive_Operations (Parent_Type);
11969
11970 function Check_Derived_Type return Boolean;
11971 -- Check that all primitive inherited from Parent_Type are found in
11972 -- the list of primitives of Derived_Type exactly in the same order.
11973
11974 function Check_Derived_Type return Boolean is
11975 E : Entity_Id;
11976 Elmt : Elmt_Id;
11977 List : Elist_Id;
11978 New_Subp : Entity_Id;
11979 Op_Elmt : Elmt_Id;
11980 Subp : Entity_Id;
11981
11982 begin
11983 -- Traverse list of entities in the current scope searching for
11984 -- an incomplete type whose full-view is derived type
11985
11986 E := First_Entity (Scope (Derived_Type));
11987 while Present (E)
11988 and then E /= Derived_Type
11989 loop
11990 if Ekind (E) = E_Incomplete_Type
11991 and then Present (Full_View (E))
11992 and then Full_View (E) = Derived_Type
11993 then
11994 -- Disable this test if Derived_Type completes an incomplete
11995 -- type because in such case more primitives can be added
11996 -- later to the list of primitives of Derived_Type by routine
11997 -- Process_Incomplete_Dependents
11998
11999 return True;
12000 end if;
12001
12002 E := Next_Entity (E);
12003 end loop;
12004
12005 List := Collect_Primitive_Operations (Derived_Type);
12006 Elmt := First_Elmt (List);
12007
12008 Op_Elmt := First_Elmt (Op_List);
12009 while Present (Op_Elmt) loop
12010 Subp := Node (Op_Elmt);
12011 New_Subp := Node (Elmt);
12012
12013 -- At this early stage Derived_Type has no entities with attribute
12014 -- Interface_Alias. In addition, such primitives are always
12015 -- located at the end of the list of primitives of Parent_Type.
12016 -- Therefore, if found we can safely stop processing pending
12017 -- entities.
12018
12019 exit when Present (Interface_Alias (Subp));
12020
12021 -- Handle hidden entities
12022
12023 if not Is_Predefined_Dispatching_Operation (Subp)
12024 and then Is_Hidden (Subp)
12025 then
12026 if Present (New_Subp)
12027 and then Primitive_Names_Match (Subp, New_Subp)
12028 then
12029 Next_Elmt (Elmt);
12030 end if;
12031
12032 else
12033 if not Present (New_Subp)
12034 or else Ekind (Subp) /= Ekind (New_Subp)
12035 or else not Primitive_Names_Match (Subp, New_Subp)
12036 then
12037 return False;
12038 end if;
12039
12040 Next_Elmt (Elmt);
12041 end if;
12042
12043 Next_Elmt (Op_Elmt);
12044 end loop;
12045
12046 return True;
12047 end Check_Derived_Type;
12048
12049 -- Local variables
12050
12051 Alias_Subp : Entity_Id;
12052 Act_List : Elist_Id;
12053 Act_Elmt : Elmt_Id := No_Elmt;
12054 Act_Subp : Entity_Id := Empty;
12055 Elmt : Elmt_Id;
12056 Need_Search : Boolean := False;
12057 New_Subp : Entity_Id := Empty;
12058 Parent_Base : Entity_Id;
12059 Subp : Entity_Id;
12060
12061 -- Start of processing for Derive_Subprograms
12062
12063 begin
12064 if Ekind (Parent_Type) = E_Record_Type_With_Private
12065 and then Has_Discriminants (Parent_Type)
12066 and then Present (Full_View (Parent_Type))
12067 then
12068 Parent_Base := Full_View (Parent_Type);
12069 else
12070 Parent_Base := Parent_Type;
12071 end if;
12072
12073 if Present (Generic_Actual) then
12074 Act_List := Collect_Primitive_Operations (Generic_Actual);
12075 Act_Elmt := First_Elmt (Act_List);
12076 end if;
12077
12078 -- Derive primitives inherited from the parent. Note that if the generic
12079 -- actual is present, this is not really a type derivation, it is a
12080 -- completion within an instance.
12081
12082 -- Case 1: Derived_Type does not implement interfaces
12083
12084 if not Is_Tagged_Type (Derived_Type)
12085 or else (not Has_Interfaces (Derived_Type)
12086 and then not (Present (Generic_Actual)
12087 and then
12088 Has_Interfaces (Generic_Actual)))
12089 then
12090 Elmt := First_Elmt (Op_List);
12091 while Present (Elmt) loop
12092 Subp := Node (Elmt);
12093
12094 -- Literals are derived earlier in the process of building the
12095 -- derived type, and are skipped here.
12096
12097 if Ekind (Subp) = E_Enumeration_Literal then
12098 null;
12099
12100 -- The actual is a direct descendant and the common primitive
12101 -- operations appear in the same order.
12102
12103 -- If the generic parent type is present, the derived type is an
12104 -- instance of a formal derived type, and within the instance its
12105 -- operations are those of the actual. We derive from the formal
12106 -- type but make the inherited operations aliases of the
12107 -- corresponding operations of the actual.
12108
12109 else
12110 Derive_Subprogram
12111 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
12112
12113 if Present (Act_Elmt) then
12114 Next_Elmt (Act_Elmt);
12115 end if;
12116 end if;
12117
12118 Next_Elmt (Elmt);
12119 end loop;
12120
12121 -- Case 2: Derived_Type implements interfaces
12122
12123 else
12124 -- If the parent type has no predefined primitives we remove
12125 -- predefined primitives from the list of primitives of generic
12126 -- actual to simplify the complexity of this algorithm.
12127
12128 if Present (Generic_Actual) then
12129 declare
12130 Has_Predefined_Primitives : Boolean := False;
12131
12132 begin
12133 -- Check if the parent type has predefined primitives
12134
12135 Elmt := First_Elmt (Op_List);
12136 while Present (Elmt) loop
12137 Subp := Node (Elmt);
12138
12139 if Is_Predefined_Dispatching_Operation (Subp)
12140 and then not Comes_From_Source (Ultimate_Alias (Subp))
12141 then
12142 Has_Predefined_Primitives := True;
12143 exit;
12144 end if;
12145
12146 Next_Elmt (Elmt);
12147 end loop;
12148
12149 -- Remove predefined primitives of Generic_Actual. We must use
12150 -- an auxiliary list because in case of tagged types the value
12151 -- returned by Collect_Primitive_Operations is the value stored
12152 -- in its Primitive_Operations attribute (and we don't want to
12153 -- modify its current contents).
12154
12155 if not Has_Predefined_Primitives then
12156 declare
12157 Aux_List : constant Elist_Id := New_Elmt_List;
12158
12159 begin
12160 Elmt := First_Elmt (Act_List);
12161 while Present (Elmt) loop
12162 Subp := Node (Elmt);
12163
12164 if not Is_Predefined_Dispatching_Operation (Subp)
12165 or else Comes_From_Source (Subp)
12166 then
12167 Append_Elmt (Subp, Aux_List);
12168 end if;
12169
12170 Next_Elmt (Elmt);
12171 end loop;
12172
12173 Act_List := Aux_List;
12174 end;
12175 end if;
12176
12177 Act_Elmt := First_Elmt (Act_List);
12178 Act_Subp := Node (Act_Elmt);
12179 end;
12180 end if;
12181
12182 -- Stage 1: If the generic actual is not present we derive the
12183 -- primitives inherited from the parent type. If the generic parent
12184 -- type is present, the derived type is an instance of a formal
12185 -- derived type, and within the instance its operations are those of
12186 -- the actual. We derive from the formal type but make the inherited
12187 -- operations aliases of the corresponding operations of the actual.
12188
12189 Elmt := First_Elmt (Op_List);
12190 while Present (Elmt) loop
12191 Subp := Node (Elmt);
12192 Alias_Subp := Ultimate_Alias (Subp);
12193
12194 -- At this early stage Derived_Type has no entities with attribute
12195 -- Interface_Alias. In addition, such primitives are always
12196 -- located at the end of the list of primitives of Parent_Type.
12197 -- Therefore, if found we can safely stop processing pending
12198 -- entities.
12199
12200 exit when Present (Interface_Alias (Subp));
12201
12202 -- If the generic actual is present find the corresponding
12203 -- operation in the generic actual. If the parent type is a
12204 -- direct ancestor of the derived type then, even if it is an
12205 -- interface, the operations are inherited from the primary
12206 -- dispatch table and are in the proper order. If we detect here
12207 -- that primitives are not in the same order we traverse the list
12208 -- of primitive operations of the actual to find the one that
12209 -- implements the interface primitive.
12210
12211 if Need_Search
12212 or else
12213 (Present (Generic_Actual)
12214 and then Present (Act_Subp)
12215 and then not Primitive_Names_Match (Subp, Act_Subp))
12216 then
12217 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual));
12218 pragma Assert (Is_Interface (Parent_Base));
12219
12220 -- Remember that we need searching for all the pending
12221 -- primitives
12222
12223 Need_Search := True;
12224
12225 -- Handle entities associated with interface primitives
12226
12227 if Present (Alias (Subp))
12228 and then Is_Interface (Find_Dispatching_Type (Alias (Subp)))
12229 and then not Is_Predefined_Dispatching_Operation (Subp)
12230 then
12231 Act_Subp :=
12232 Find_Primitive_Covering_Interface
12233 (Tagged_Type => Generic_Actual,
12234 Iface_Prim => Subp);
12235
12236 -- Handle predefined primitives plus the rest of user-defined
12237 -- primitives
12238
12239 else
12240 Act_Elmt := First_Elmt (Act_List);
12241 while Present (Act_Elmt) loop
12242 Act_Subp := Node (Act_Elmt);
12243
12244 exit when Primitive_Names_Match (Subp, Act_Subp)
12245 and then Type_Conformant (Subp, Act_Subp,
12246 Skip_Controlling_Formals => True)
12247 and then No (Interface_Alias (Act_Subp));
12248
12249 Next_Elmt (Act_Elmt);
12250 end loop;
12251 end if;
12252 end if;
12253
12254 -- Case 1: If the parent is a limited interface then it has the
12255 -- predefined primitives of synchronized interfaces. However, the
12256 -- actual type may be a non-limited type and hence it does not
12257 -- have such primitives.
12258
12259 if Present (Generic_Actual)
12260 and then not Present (Act_Subp)
12261 and then Is_Limited_Interface (Parent_Base)
12262 and then Is_Predefined_Interface_Primitive (Subp)
12263 then
12264 null;
12265
12266 -- Case 2: Inherit entities associated with interfaces that
12267 -- were not covered by the parent type. We exclude here null
12268 -- interface primitives because they do not need special
12269 -- management.
12270
12271 elsif Present (Alias (Subp))
12272 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
12273 and then not
12274 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
12275 and then Null_Present (Parent (Alias_Subp)))
12276 then
12277 Derive_Subprogram
12278 (New_Subp => New_Subp,
12279 Parent_Subp => Alias_Subp,
12280 Derived_Type => Derived_Type,
12281 Parent_Type => Find_Dispatching_Type (Alias_Subp),
12282 Actual_Subp => Act_Subp);
12283
12284 if No (Generic_Actual) then
12285 Set_Alias (New_Subp, Subp);
12286 end if;
12287
12288 -- Case 3: Common derivation
12289
12290 else
12291 Derive_Subprogram
12292 (New_Subp => New_Subp,
12293 Parent_Subp => Subp,
12294 Derived_Type => Derived_Type,
12295 Parent_Type => Parent_Base,
12296 Actual_Subp => Act_Subp);
12297 end if;
12298
12299 -- No need to update Act_Elm if we must search for the
12300 -- corresponding operation in the generic actual
12301
12302 if not Need_Search
12303 and then Present (Act_Elmt)
12304 then
12305 Next_Elmt (Act_Elmt);
12306 Act_Subp := Node (Act_Elmt);
12307 end if;
12308
12309 Next_Elmt (Elmt);
12310 end loop;
12311
12312 -- Inherit additional operations from progenitors. If the derived
12313 -- type is a generic actual, there are not new primitive operations
12314 -- for the type because it has those of the actual, and therefore
12315 -- nothing needs to be done. The renamings generated above are not
12316 -- primitive operations, and their purpose is simply to make the
12317 -- proper operations visible within an instantiation.
12318
12319 if No (Generic_Actual) then
12320 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
12321 end if;
12322 end if;
12323
12324 -- Final check: Direct descendants must have their primitives in the
12325 -- same order. We exclude from this test non-tagged types and instances
12326 -- of formal derived types. We skip this test if we have already
12327 -- reported serious errors in the sources.
12328
12329 pragma Assert (not Is_Tagged_Type (Derived_Type)
12330 or else Present (Generic_Actual)
12331 or else Serious_Errors_Detected > 0
12332 or else Check_Derived_Type);
12333 end Derive_Subprograms;
12334
12335 --------------------------------
12336 -- Derived_Standard_Character --
12337 --------------------------------
12338
12339 procedure Derived_Standard_Character
12340 (N : Node_Id;
12341 Parent_Type : Entity_Id;
12342 Derived_Type : Entity_Id)
12343 is
12344 Loc : constant Source_Ptr := Sloc (N);
12345 Def : constant Node_Id := Type_Definition (N);
12346 Indic : constant Node_Id := Subtype_Indication (Def);
12347 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
12348 Implicit_Base : constant Entity_Id :=
12349 Create_Itype
12350 (E_Enumeration_Type, N, Derived_Type, 'B');
12351
12352 Lo : Node_Id;
12353 Hi : Node_Id;
12354
12355 begin
12356 Discard_Node (Process_Subtype (Indic, N));
12357
12358 Set_Etype (Implicit_Base, Parent_Base);
12359 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
12360 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
12361
12362 Set_Is_Character_Type (Implicit_Base, True);
12363 Set_Has_Delayed_Freeze (Implicit_Base);
12364
12365 -- The bounds of the implicit base are the bounds of the parent base.
12366 -- Note that their type is the parent base.
12367
12368 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
12369 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
12370
12371 Set_Scalar_Range (Implicit_Base,
12372 Make_Range (Loc,
12373 Low_Bound => Lo,
12374 High_Bound => Hi));
12375
12376 Conditional_Delay (Derived_Type, Parent_Type);
12377
12378 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
12379 Set_Etype (Derived_Type, Implicit_Base);
12380 Set_Size_Info (Derived_Type, Parent_Type);
12381
12382 if Unknown_RM_Size (Derived_Type) then
12383 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
12384 end if;
12385
12386 Set_Is_Character_Type (Derived_Type, True);
12387
12388 if Nkind (Indic) /= N_Subtype_Indication then
12389
12390 -- If no explicit constraint, the bounds are those
12391 -- of the parent type.
12392
12393 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
12394 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
12395 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
12396 end if;
12397
12398 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
12399
12400 -- Because the implicit base is used in the conversion of the bounds, we
12401 -- have to freeze it now. This is similar to what is done for numeric
12402 -- types, and it equally suspicious, but otherwise a non-static bound
12403 -- will have a reference to an unfrozen type, which is rejected by Gigi
12404 -- (???). This requires specific care for definition of stream
12405 -- attributes. For details, see comments at the end of
12406 -- Build_Derived_Numeric_Type.
12407
12408 Freeze_Before (N, Implicit_Base);
12409 end Derived_Standard_Character;
12410
12411 ------------------------------
12412 -- Derived_Type_Declaration --
12413 ------------------------------
12414
12415 procedure Derived_Type_Declaration
12416 (T : Entity_Id;
12417 N : Node_Id;
12418 Is_Completion : Boolean)
12419 is
12420 Parent_Type : Entity_Id;
12421
12422 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
12423 -- Check whether the parent type is a generic formal, or derives
12424 -- directly or indirectly from one.
12425
12426 ------------------------
12427 -- Comes_From_Generic --
12428 ------------------------
12429
12430 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
12431 begin
12432 if Is_Generic_Type (Typ) then
12433 return True;
12434
12435 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
12436 return True;
12437
12438 elsif Is_Private_Type (Typ)
12439 and then Present (Full_View (Typ))
12440 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
12441 then
12442 return True;
12443
12444 elsif Is_Generic_Actual_Type (Typ) then
12445 return True;
12446
12447 else
12448 return False;
12449 end if;
12450 end Comes_From_Generic;
12451
12452 -- Local variables
12453
12454 Def : constant Node_Id := Type_Definition (N);
12455 Iface_Def : Node_Id;
12456 Indic : constant Node_Id := Subtype_Indication (Def);
12457 Extension : constant Node_Id := Record_Extension_Part (Def);
12458 Parent_Node : Node_Id;
12459 Parent_Scope : Entity_Id;
12460 Taggd : Boolean;
12461
12462 -- Start of processing for Derived_Type_Declaration
12463
12464 begin
12465 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
12466
12467 -- Ada 2005 (AI-251): In case of interface derivation check that the
12468 -- parent is also an interface.
12469
12470 if Interface_Present (Def) then
12471 if not Is_Interface (Parent_Type) then
12472 Diagnose_Interface (Indic, Parent_Type);
12473
12474 else
12475 Parent_Node := Parent (Base_Type (Parent_Type));
12476 Iface_Def := Type_Definition (Parent_Node);
12477
12478 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
12479 -- other limited interfaces.
12480
12481 if Limited_Present (Def) then
12482 if Limited_Present (Iface_Def) then
12483 null;
12484
12485 elsif Protected_Present (Iface_Def) then
12486 Error_Msg_N
12487 ("(Ada 2005) limited interface cannot "
12488 & "inherit from protected interface", Indic);
12489
12490 elsif Synchronized_Present (Iface_Def) then
12491 Error_Msg_N
12492 ("(Ada 2005) limited interface cannot "
12493 & "inherit from synchronized interface", Indic);
12494
12495 elsif Task_Present (Iface_Def) then
12496 Error_Msg_N
12497 ("(Ada 2005) limited interface cannot "
12498 & "inherit from task interface", Indic);
12499
12500 else
12501 Error_Msg_N
12502 ("(Ada 2005) limited interface cannot "
12503 & "inherit from non-limited interface", Indic);
12504 end if;
12505
12506 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
12507 -- from non-limited or limited interfaces.
12508
12509 elsif not Protected_Present (Def)
12510 and then not Synchronized_Present (Def)
12511 and then not Task_Present (Def)
12512 then
12513 if Limited_Present (Iface_Def) then
12514 null;
12515
12516 elsif Protected_Present (Iface_Def) then
12517 Error_Msg_N
12518 ("(Ada 2005) non-limited interface cannot "
12519 & "inherit from protected interface", Indic);
12520
12521 elsif Synchronized_Present (Iface_Def) then
12522 Error_Msg_N
12523 ("(Ada 2005) non-limited interface cannot "
12524 & "inherit from synchronized interface", Indic);
12525
12526 elsif Task_Present (Iface_Def) then
12527 Error_Msg_N
12528 ("(Ada 2005) non-limited interface cannot "
12529 & "inherit from task interface", Indic);
12530
12531 else
12532 null;
12533 end if;
12534 end if;
12535 end if;
12536 end if;
12537
12538 if Is_Tagged_Type (Parent_Type)
12539 and then Is_Concurrent_Type (Parent_Type)
12540 and then not Is_Interface (Parent_Type)
12541 then
12542 Error_Msg_N
12543 ("parent type of a record extension cannot be "
12544 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
12545 Set_Etype (T, Any_Type);
12546 return;
12547 end if;
12548
12549 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
12550 -- interfaces
12551
12552 if Is_Tagged_Type (Parent_Type)
12553 and then Is_Non_Empty_List (Interface_List (Def))
12554 then
12555 declare
12556 Intf : Node_Id;
12557 T : Entity_Id;
12558
12559 begin
12560 Intf := First (Interface_List (Def));
12561 while Present (Intf) loop
12562 T := Find_Type_Of_Subtype_Indic (Intf);
12563
12564 if not Is_Interface (T) then
12565 Diagnose_Interface (Intf, T);
12566
12567 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
12568 -- a limited type from having a nonlimited progenitor.
12569
12570 elsif (Limited_Present (Def)
12571 or else (not Is_Interface (Parent_Type)
12572 and then Is_Limited_Type (Parent_Type)))
12573 and then not Is_Limited_Interface (T)
12574 then
12575 Error_Msg_NE
12576 ("progenitor interface& of limited type must be limited",
12577 N, T);
12578 end if;
12579
12580 Next (Intf);
12581 end loop;
12582 end;
12583 end if;
12584
12585 if Parent_Type = Any_Type
12586 or else Etype (Parent_Type) = Any_Type
12587 or else (Is_Class_Wide_Type (Parent_Type)
12588 and then Etype (Parent_Type) = T)
12589 then
12590 -- If Parent_Type is undefined or illegal, make new type into a
12591 -- subtype of Any_Type, and set a few attributes to prevent cascaded
12592 -- errors. If this is a self-definition, emit error now.
12593
12594 if T = Parent_Type
12595 or else T = Etype (Parent_Type)
12596 then
12597 Error_Msg_N ("type cannot be used in its own definition", Indic);
12598 end if;
12599
12600 Set_Ekind (T, Ekind (Parent_Type));
12601 Set_Etype (T, Any_Type);
12602 Set_Scalar_Range (T, Scalar_Range (Any_Type));
12603
12604 if Is_Tagged_Type (T) then
12605 Set_Primitive_Operations (T, New_Elmt_List);
12606 end if;
12607
12608 return;
12609 end if;
12610
12611 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
12612 -- an interface is special because the list of interfaces in the full
12613 -- view can be given in any order. For example:
12614
12615 -- type A is interface;
12616 -- type B is interface and A;
12617 -- type D is new B with private;
12618 -- private
12619 -- type D is new A and B with null record; -- 1 --
12620
12621 -- In this case we perform the following transformation of -1-:
12622
12623 -- type D is new B and A with null record;
12624
12625 -- If the parent of the full-view covers the parent of the partial-view
12626 -- we have two possible cases:
12627
12628 -- 1) They have the same parent
12629 -- 2) The parent of the full-view implements some further interfaces
12630
12631 -- In both cases we do not need to perform the transformation. In the
12632 -- first case the source program is correct and the transformation is
12633 -- not needed; in the second case the source program does not fulfill
12634 -- the no-hidden interfaces rule (AI-396) and the error will be reported
12635 -- later.
12636
12637 -- This transformation not only simplifies the rest of the analysis of
12638 -- this type declaration but also simplifies the correct generation of
12639 -- the object layout to the expander.
12640
12641 if In_Private_Part (Current_Scope)
12642 and then Is_Interface (Parent_Type)
12643 then
12644 declare
12645 Iface : Node_Id;
12646 Partial_View : Entity_Id;
12647 Partial_View_Parent : Entity_Id;
12648 New_Iface : Node_Id;
12649
12650 begin
12651 -- Look for the associated private type declaration
12652
12653 Partial_View := First_Entity (Current_Scope);
12654 loop
12655 exit when No (Partial_View)
12656 or else (Has_Private_Declaration (Partial_View)
12657 and then Full_View (Partial_View) = T);
12658
12659 Next_Entity (Partial_View);
12660 end loop;
12661
12662 -- If the partial view was not found then the source code has
12663 -- errors and the transformation is not needed.
12664
12665 if Present (Partial_View) then
12666 Partial_View_Parent := Etype (Partial_View);
12667
12668 -- If the parent of the full-view covers the parent of the
12669 -- partial-view we have nothing else to do.
12670
12671 if Interface_Present_In_Ancestor
12672 (Parent_Type, Partial_View_Parent)
12673 then
12674 null;
12675
12676 -- Traverse the list of interfaces of the full-view to look
12677 -- for the parent of the partial-view and perform the tree
12678 -- transformation.
12679
12680 else
12681 Iface := First (Interface_List (Def));
12682 while Present (Iface) loop
12683 if Etype (Iface) = Etype (Partial_View) then
12684 Rewrite (Subtype_Indication (Def),
12685 New_Copy (Subtype_Indication
12686 (Parent (Partial_View))));
12687
12688 New_Iface := Make_Identifier (Sloc (N),
12689 Chars (Parent_Type));
12690 Append (New_Iface, Interface_List (Def));
12691
12692 -- Analyze the transformed code
12693
12694 Derived_Type_Declaration (T, N, Is_Completion);
12695 return;
12696 end if;
12697
12698 Next (Iface);
12699 end loop;
12700 end if;
12701 end if;
12702 end;
12703 end if;
12704
12705 -- Only composite types other than array types are allowed to have
12706 -- discriminants.
12707
12708 if Present (Discriminant_Specifications (N))
12709 and then (Is_Elementary_Type (Parent_Type)
12710 or else Is_Array_Type (Parent_Type))
12711 and then not Error_Posted (N)
12712 then
12713 Error_Msg_N
12714 ("elementary or array type cannot have discriminants",
12715 Defining_Identifier (First (Discriminant_Specifications (N))));
12716 Set_Has_Discriminants (T, False);
12717 end if;
12718
12719 -- In Ada 83, a derived type defined in a package specification cannot
12720 -- be used for further derivation until the end of its visible part.
12721 -- Note that derivation in the private part of the package is allowed.
12722
12723 if Ada_Version = Ada_83
12724 and then Is_Derived_Type (Parent_Type)
12725 and then In_Visible_Part (Scope (Parent_Type))
12726 then
12727 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
12728 Error_Msg_N
12729 ("(Ada 83): premature use of type for derivation", Indic);
12730 end if;
12731 end if;
12732
12733 -- Check for early use of incomplete or private type
12734
12735 if Ekind (Parent_Type) = E_Void
12736 or else Ekind (Parent_Type) = E_Incomplete_Type
12737 then
12738 Error_Msg_N ("premature derivation of incomplete type", Indic);
12739 return;
12740
12741 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
12742 and then not Comes_From_Generic (Parent_Type))
12743 or else Has_Private_Component (Parent_Type)
12744 then
12745 -- The ancestor type of a formal type can be incomplete, in which
12746 -- case only the operations of the partial view are available in
12747 -- the generic. Subsequent checks may be required when the full
12748 -- view is analyzed, to verify that derivation from a tagged type
12749 -- has an extension.
12750
12751 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
12752 null;
12753
12754 elsif No (Underlying_Type (Parent_Type))
12755 or else Has_Private_Component (Parent_Type)
12756 then
12757 Error_Msg_N
12758 ("premature derivation of derived or private type", Indic);
12759
12760 -- Flag the type itself as being in error, this prevents some
12761 -- nasty problems with subsequent uses of the malformed type.
12762
12763 Set_Error_Posted (T);
12764
12765 -- Check that within the immediate scope of an untagged partial
12766 -- view it's illegal to derive from the partial view if the
12767 -- full view is tagged. (7.3(7))
12768
12769 -- We verify that the Parent_Type is a partial view by checking
12770 -- that it is not a Full_Type_Declaration (i.e. a private type or
12771 -- private extension declaration), to distinguish a partial view
12772 -- from a derivation from a private type which also appears as
12773 -- E_Private_Type.
12774
12775 elsif Present (Full_View (Parent_Type))
12776 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
12777 and then not Is_Tagged_Type (Parent_Type)
12778 and then Is_Tagged_Type (Full_View (Parent_Type))
12779 then
12780 Parent_Scope := Scope (T);
12781 while Present (Parent_Scope)
12782 and then Parent_Scope /= Standard_Standard
12783 loop
12784 if Parent_Scope = Scope (Parent_Type) then
12785 Error_Msg_N
12786 ("premature derivation from type with tagged full view",
12787 Indic);
12788 end if;
12789
12790 Parent_Scope := Scope (Parent_Scope);
12791 end loop;
12792 end if;
12793 end if;
12794
12795 -- Check that form of derivation is appropriate
12796
12797 Taggd := Is_Tagged_Type (Parent_Type);
12798
12799 -- Perhaps the parent type should be changed to the class-wide type's
12800 -- specific type in this case to prevent cascading errors ???
12801
12802 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
12803 Error_Msg_N ("parent type must not be a class-wide type", Indic);
12804 return;
12805 end if;
12806
12807 if Present (Extension) and then not Taggd then
12808 Error_Msg_N
12809 ("type derived from untagged type cannot have extension", Indic);
12810
12811 elsif No (Extension) and then Taggd then
12812
12813 -- If this declaration is within a private part (or body) of a
12814 -- generic instantiation then the derivation is allowed (the parent
12815 -- type can only appear tagged in this case if it's a generic actual
12816 -- type, since it would otherwise have been rejected in the analysis
12817 -- of the generic template).
12818
12819 if not Is_Generic_Actual_Type (Parent_Type)
12820 or else In_Visible_Part (Scope (Parent_Type))
12821 then
12822 Error_Msg_N
12823 ("type derived from tagged type must have extension", Indic);
12824 end if;
12825 end if;
12826
12827 -- AI-443: Synchronized formal derived types require a private
12828 -- extension. There is no point in checking the ancestor type or
12829 -- the progenitors since the construct is wrong to begin with.
12830
12831 if Ada_Version >= Ada_05
12832 and then Is_Generic_Type (T)
12833 and then Present (Original_Node (N))
12834 then
12835 declare
12836 Decl : constant Node_Id := Original_Node (N);
12837
12838 begin
12839 if Nkind (Decl) = N_Formal_Type_Declaration
12840 and then Nkind (Formal_Type_Definition (Decl)) =
12841 N_Formal_Derived_Type_Definition
12842 and then Synchronized_Present (Formal_Type_Definition (Decl))
12843 and then No (Extension)
12844
12845 -- Avoid emitting a duplicate error message
12846
12847 and then not Error_Posted (Indic)
12848 then
12849 Error_Msg_N
12850 ("synchronized derived type must have extension", N);
12851 end if;
12852 end;
12853 end if;
12854
12855 Build_Derived_Type (N, Parent_Type, T, Is_Completion);
12856
12857 -- AI-419: The parent type of an explicitly limited derived type must
12858 -- be a limited type or a limited interface.
12859
12860 if Limited_Present (Def) then
12861 Set_Is_Limited_Record (T);
12862
12863 if Is_Interface (T) then
12864 Set_Is_Limited_Interface (T);
12865 end if;
12866
12867 if not Is_Limited_Type (Parent_Type)
12868 and then
12869 (not Is_Interface (Parent_Type)
12870 or else not Is_Limited_Interface (Parent_Type))
12871 then
12872 Error_Msg_NE ("parent type& of limited type must be limited",
12873 N, Parent_Type);
12874 end if;
12875 end if;
12876 end Derived_Type_Declaration;
12877
12878 ------------------------
12879 -- Diagnose_Interface --
12880 ------------------------
12881
12882 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
12883 begin
12884 if not Is_Interface (E)
12885 and then E /= Any_Type
12886 then
12887 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
12888 end if;
12889 end Diagnose_Interface;
12890
12891 ----------------------------------
12892 -- Enumeration_Type_Declaration --
12893 ----------------------------------
12894
12895 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
12896 Ev : Uint;
12897 L : Node_Id;
12898 R_Node : Node_Id;
12899 B_Node : Node_Id;
12900
12901 begin
12902 -- Create identifier node representing lower bound
12903
12904 B_Node := New_Node (N_Identifier, Sloc (Def));
12905 L := First (Literals (Def));
12906 Set_Chars (B_Node, Chars (L));
12907 Set_Entity (B_Node, L);
12908 Set_Etype (B_Node, T);
12909 Set_Is_Static_Expression (B_Node, True);
12910
12911 R_Node := New_Node (N_Range, Sloc (Def));
12912 Set_Low_Bound (R_Node, B_Node);
12913
12914 Set_Ekind (T, E_Enumeration_Type);
12915 Set_First_Literal (T, L);
12916 Set_Etype (T, T);
12917 Set_Is_Constrained (T);
12918
12919 Ev := Uint_0;
12920
12921 -- Loop through literals of enumeration type setting pos and rep values
12922 -- except that if the Ekind is already set, then it means that the
12923 -- literal was already constructed (case of a derived type declaration
12924 -- and we should not disturb the Pos and Rep values.
12925
12926 while Present (L) loop
12927 if Ekind (L) /= E_Enumeration_Literal then
12928 Set_Ekind (L, E_Enumeration_Literal);
12929 Set_Enumeration_Pos (L, Ev);
12930 Set_Enumeration_Rep (L, Ev);
12931 Set_Is_Known_Valid (L, True);
12932 end if;
12933
12934 Set_Etype (L, T);
12935 New_Overloaded_Entity (L);
12936 Generate_Definition (L);
12937 Set_Convention (L, Convention_Intrinsic);
12938
12939 if Nkind (L) = N_Defining_Character_Literal then
12940 Set_Is_Character_Type (T, True);
12941 end if;
12942
12943 Ev := Ev + 1;
12944 Next (L);
12945 end loop;
12946
12947 -- Now create a node representing upper bound
12948
12949 B_Node := New_Node (N_Identifier, Sloc (Def));
12950 Set_Chars (B_Node, Chars (Last (Literals (Def))));
12951 Set_Entity (B_Node, Last (Literals (Def)));
12952 Set_Etype (B_Node, T);
12953 Set_Is_Static_Expression (B_Node, True);
12954
12955 Set_High_Bound (R_Node, B_Node);
12956
12957 -- Initialize various fields of the type. Some of this information
12958 -- may be overwritten later through rep.clauses.
12959
12960 Set_Scalar_Range (T, R_Node);
12961 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
12962 Set_Enum_Esize (T);
12963 Set_Enum_Pos_To_Rep (T, Empty);
12964
12965 -- Set Discard_Names if configuration pragma set, or if there is
12966 -- a parameterless pragma in the current declarative region
12967
12968 if Global_Discard_Names
12969 or else Discard_Names (Scope (T))
12970 then
12971 Set_Discard_Names (T);
12972 end if;
12973
12974 -- Process end label if there is one
12975
12976 if Present (Def) then
12977 Process_End_Label (Def, 'e', T);
12978 end if;
12979 end Enumeration_Type_Declaration;
12980
12981 ---------------------------------
12982 -- Expand_To_Stored_Constraint --
12983 ---------------------------------
12984
12985 function Expand_To_Stored_Constraint
12986 (Typ : Entity_Id;
12987 Constraint : Elist_Id) return Elist_Id
12988 is
12989 Explicitly_Discriminated_Type : Entity_Id;
12990 Expansion : Elist_Id;
12991 Discriminant : Entity_Id;
12992
12993 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
12994 -- Find the nearest type that actually specifies discriminants
12995
12996 ---------------------------------
12997 -- Type_With_Explicit_Discrims --
12998 ---------------------------------
12999
13000 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
13001 Typ : constant E := Base_Type (Id);
13002
13003 begin
13004 if Ekind (Typ) in Incomplete_Or_Private_Kind then
13005 if Present (Full_View (Typ)) then
13006 return Type_With_Explicit_Discrims (Full_View (Typ));
13007 end if;
13008
13009 else
13010 if Has_Discriminants (Typ) then
13011 return Typ;
13012 end if;
13013 end if;
13014
13015 if Etype (Typ) = Typ then
13016 return Empty;
13017 elsif Has_Discriminants (Typ) then
13018 return Typ;
13019 else
13020 return Type_With_Explicit_Discrims (Etype (Typ));
13021 end if;
13022
13023 end Type_With_Explicit_Discrims;
13024
13025 -- Start of processing for Expand_To_Stored_Constraint
13026
13027 begin
13028 if No (Constraint)
13029 or else Is_Empty_Elmt_List (Constraint)
13030 then
13031 return No_Elist;
13032 end if;
13033
13034 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
13035
13036 if No (Explicitly_Discriminated_Type) then
13037 return No_Elist;
13038 end if;
13039
13040 Expansion := New_Elmt_List;
13041
13042 Discriminant :=
13043 First_Stored_Discriminant (Explicitly_Discriminated_Type);
13044 while Present (Discriminant) loop
13045 Append_Elmt (
13046 Get_Discriminant_Value (
13047 Discriminant, Explicitly_Discriminated_Type, Constraint),
13048 Expansion);
13049 Next_Stored_Discriminant (Discriminant);
13050 end loop;
13051
13052 return Expansion;
13053 end Expand_To_Stored_Constraint;
13054
13055 ---------------------------
13056 -- Find_Hidden_Interface --
13057 ---------------------------
13058
13059 function Find_Hidden_Interface
13060 (Src : Elist_Id;
13061 Dest : Elist_Id) return Entity_Id
13062 is
13063 Iface : Entity_Id;
13064 Iface_Elmt : Elmt_Id;
13065
13066 begin
13067 if Present (Src) and then Present (Dest) then
13068 Iface_Elmt := First_Elmt (Src);
13069 while Present (Iface_Elmt) loop
13070 Iface := Node (Iface_Elmt);
13071
13072 if Is_Interface (Iface)
13073 and then not Contain_Interface (Iface, Dest)
13074 then
13075 return Iface;
13076 end if;
13077
13078 Next_Elmt (Iface_Elmt);
13079 end loop;
13080 end if;
13081
13082 return Empty;
13083 end Find_Hidden_Interface;
13084
13085 --------------------
13086 -- Find_Type_Name --
13087 --------------------
13088
13089 function Find_Type_Name (N : Node_Id) return Entity_Id is
13090 Id : constant Entity_Id := Defining_Identifier (N);
13091 Prev : Entity_Id;
13092 New_Id : Entity_Id;
13093 Prev_Par : Node_Id;
13094
13095 procedure Tag_Mismatch;
13096 -- Diagnose a tagged partial view whose full view is untagged.
13097 -- We post the message on the full view, with a reference to
13098 -- the previous partial view. The partial view can be private
13099 -- or incomplete, and these are handled in a different manner,
13100 -- so we determine the position of the error message from the
13101 -- respective slocs of both.
13102
13103 ------------------
13104 -- Tag_Mismatch --
13105 ------------------
13106
13107 procedure Tag_Mismatch is
13108 begin
13109 if Sloc (Prev) < Sloc (Id) then
13110 Error_Msg_NE
13111 ("full declaration of } must be a tagged type ", Id, Prev);
13112 else
13113 Error_Msg_NE
13114 ("full declaration of } must be a tagged type ", Prev, Id);
13115 end if;
13116 end Tag_Mismatch;
13117
13118 -- Start processing for Find_Type_Name
13119
13120 begin
13121 -- Find incomplete declaration, if one was given
13122
13123 Prev := Current_Entity_In_Scope (Id);
13124
13125 if Present (Prev) then
13126
13127 -- Previous declaration exists. Error if not incomplete/private case
13128 -- except if previous declaration is implicit, etc. Enter_Name will
13129 -- emit error if appropriate.
13130
13131 Prev_Par := Parent (Prev);
13132
13133 if not Is_Incomplete_Or_Private_Type (Prev) then
13134 Enter_Name (Id);
13135 New_Id := Id;
13136
13137 elsif not Nkind_In (N, N_Full_Type_Declaration,
13138 N_Task_Type_Declaration,
13139 N_Protected_Type_Declaration)
13140 then
13141 -- Completion must be a full type declarations (RM 7.3(4))
13142
13143 Error_Msg_Sloc := Sloc (Prev);
13144 Error_Msg_NE ("invalid completion of }", Id, Prev);
13145
13146 -- Set scope of Id to avoid cascaded errors. Entity is never
13147 -- examined again, except when saving globals in generics.
13148
13149 Set_Scope (Id, Current_Scope);
13150 New_Id := Id;
13151
13152 -- Case of full declaration of incomplete type
13153
13154 elsif Ekind (Prev) = E_Incomplete_Type then
13155
13156 -- Indicate that the incomplete declaration has a matching full
13157 -- declaration. The defining occurrence of the incomplete
13158 -- declaration remains the visible one, and the procedure
13159 -- Get_Full_View dereferences it whenever the type is used.
13160
13161 if Present (Full_View (Prev)) then
13162 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13163 end if;
13164
13165 Set_Full_View (Prev, Id);
13166 Append_Entity (Id, Current_Scope);
13167 Set_Is_Public (Id, Is_Public (Prev));
13168 Set_Is_Internal (Id);
13169 New_Id := Prev;
13170
13171 -- Case of full declaration of private type
13172
13173 else
13174 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
13175 if Etype (Prev) /= Prev then
13176
13177 -- Prev is a private subtype or a derived type, and needs
13178 -- no completion.
13179
13180 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13181 New_Id := Id;
13182
13183 elsif Ekind (Prev) = E_Private_Type
13184 and then Nkind_In (N, N_Task_Type_Declaration,
13185 N_Protected_Type_Declaration)
13186 then
13187 Error_Msg_N
13188 ("completion of nonlimited type cannot be limited", N);
13189
13190 elsif Ekind (Prev) = E_Record_Type_With_Private
13191 and then Nkind_In (N, N_Task_Type_Declaration,
13192 N_Protected_Type_Declaration)
13193 then
13194 if not Is_Limited_Record (Prev) then
13195 Error_Msg_N
13196 ("completion of nonlimited type cannot be limited", N);
13197
13198 elsif No (Interface_List (N)) then
13199 Error_Msg_N
13200 ("completion of tagged private type must be tagged",
13201 N);
13202 end if;
13203 end if;
13204
13205 -- Ada 2005 (AI-251): Private extension declaration of a task
13206 -- type or a protected type. This case arises when covering
13207 -- interface types.
13208
13209 elsif Nkind_In (N, N_Task_Type_Declaration,
13210 N_Protected_Type_Declaration)
13211 then
13212 null;
13213
13214 elsif Nkind (N) /= N_Full_Type_Declaration
13215 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
13216 then
13217 Error_Msg_N
13218 ("full view of private extension must be an extension", N);
13219
13220 elsif not (Abstract_Present (Parent (Prev)))
13221 and then Abstract_Present (Type_Definition (N))
13222 then
13223 Error_Msg_N
13224 ("full view of non-abstract extension cannot be abstract", N);
13225 end if;
13226
13227 if not In_Private_Part (Current_Scope) then
13228 Error_Msg_N
13229 ("declaration of full view must appear in private part", N);
13230 end if;
13231
13232 Copy_And_Swap (Prev, Id);
13233 Set_Has_Private_Declaration (Prev);
13234 Set_Has_Private_Declaration (Id);
13235
13236 -- If no error, propagate freeze_node from private to full view.
13237 -- It may have been generated for an early operational item.
13238
13239 if Present (Freeze_Node (Id))
13240 and then Serious_Errors_Detected = 0
13241 and then No (Full_View (Id))
13242 then
13243 Set_Freeze_Node (Prev, Freeze_Node (Id));
13244 Set_Freeze_Node (Id, Empty);
13245 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
13246 end if;
13247
13248 Set_Full_View (Id, Prev);
13249 New_Id := Prev;
13250 end if;
13251
13252 -- Verify that full declaration conforms to partial one
13253
13254 if Is_Incomplete_Or_Private_Type (Prev)
13255 and then Present (Discriminant_Specifications (Prev_Par))
13256 then
13257 if Present (Discriminant_Specifications (N)) then
13258 if Ekind (Prev) = E_Incomplete_Type then
13259 Check_Discriminant_Conformance (N, Prev, Prev);
13260 else
13261 Check_Discriminant_Conformance (N, Prev, Id);
13262 end if;
13263
13264 else
13265 Error_Msg_N
13266 ("missing discriminants in full type declaration", N);
13267
13268 -- To avoid cascaded errors on subsequent use, share the
13269 -- discriminants of the partial view.
13270
13271 Set_Discriminant_Specifications (N,
13272 Discriminant_Specifications (Prev_Par));
13273 end if;
13274 end if;
13275
13276 -- A prior untagged partial view can have an associated class-wide
13277 -- type due to use of the class attribute, and in this case the full
13278 -- type must also be tagged. This Ada 95 usage is deprecated in favor
13279 -- of incomplete tagged declarations, but we check for it.
13280
13281 if Is_Type (Prev)
13282 and then (Is_Tagged_Type (Prev)
13283 or else Present (Class_Wide_Type (Prev)))
13284 then
13285 -- The full declaration is either a tagged type (including
13286 -- a synchronized type that implements interfaces) or a
13287 -- type extension, otherwise this is an error.
13288
13289 if Nkind_In (N, N_Task_Type_Declaration,
13290 N_Protected_Type_Declaration)
13291 then
13292 if No (Interface_List (N))
13293 and then not Error_Posted (N)
13294 then
13295 Tag_Mismatch;
13296 end if;
13297
13298 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
13299
13300 -- Indicate that the previous declaration (tagged incomplete
13301 -- or private declaration) requires the same on the full one.
13302
13303 if not Tagged_Present (Type_Definition (N)) then
13304 Tag_Mismatch;
13305 Set_Is_Tagged_Type (Id);
13306 Set_Primitive_Operations (Id, New_Elmt_List);
13307 end if;
13308
13309 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
13310 if No (Record_Extension_Part (Type_Definition (N))) then
13311 Error_Msg_NE (
13312 "full declaration of } must be a record extension",
13313 Prev, Id);
13314 Set_Is_Tagged_Type (Id);
13315 Set_Primitive_Operations (Id, New_Elmt_List);
13316 end if;
13317
13318 else
13319 Tag_Mismatch;
13320 end if;
13321 end if;
13322
13323 return New_Id;
13324
13325 else
13326 -- New type declaration
13327
13328 Enter_Name (Id);
13329 return Id;
13330 end if;
13331 end Find_Type_Name;
13332
13333 -------------------------
13334 -- Find_Type_Of_Object --
13335 -------------------------
13336
13337 function Find_Type_Of_Object
13338 (Obj_Def : Node_Id;
13339 Related_Nod : Node_Id) return Entity_Id
13340 is
13341 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
13342 P : Node_Id := Parent (Obj_Def);
13343 T : Entity_Id;
13344 Nam : Name_Id;
13345
13346 begin
13347 -- If the parent is a component_definition node we climb to the
13348 -- component_declaration node
13349
13350 if Nkind (P) = N_Component_Definition then
13351 P := Parent (P);
13352 end if;
13353
13354 -- Case of an anonymous array subtype
13355
13356 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
13357 N_Unconstrained_Array_Definition)
13358 then
13359 T := Empty;
13360 Array_Type_Declaration (T, Obj_Def);
13361
13362 -- Create an explicit subtype whenever possible
13363
13364 elsif Nkind (P) /= N_Component_Declaration
13365 and then Def_Kind = N_Subtype_Indication
13366 then
13367 -- Base name of subtype on object name, which will be unique in
13368 -- the current scope.
13369
13370 -- If this is a duplicate declaration, return base type, to avoid
13371 -- generating duplicate anonymous types.
13372
13373 if Error_Posted (P) then
13374 Analyze (Subtype_Mark (Obj_Def));
13375 return Entity (Subtype_Mark (Obj_Def));
13376 end if;
13377
13378 Nam :=
13379 New_External_Name
13380 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
13381
13382 T := Make_Defining_Identifier (Sloc (P), Nam);
13383
13384 Insert_Action (Obj_Def,
13385 Make_Subtype_Declaration (Sloc (P),
13386 Defining_Identifier => T,
13387 Subtype_Indication => Relocate_Node (Obj_Def)));
13388
13389 -- This subtype may need freezing, and this will not be done
13390 -- automatically if the object declaration is not in declarative
13391 -- part. Since this is an object declaration, the type cannot always
13392 -- be frozen here. Deferred constants do not freeze their type
13393 -- (which often enough will be private).
13394
13395 if Nkind (P) = N_Object_Declaration
13396 and then Constant_Present (P)
13397 and then No (Expression (P))
13398 then
13399 null;
13400 else
13401 Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
13402 end if;
13403
13404 -- Ada 2005 AI-406: the object definition in an object declaration
13405 -- can be an access definition.
13406
13407 elsif Def_Kind = N_Access_Definition then
13408 T := Access_Definition (Related_Nod, Obj_Def);
13409 Set_Is_Local_Anonymous_Access (T);
13410
13411 -- Otherwise, the object definition is just a subtype_mark
13412
13413 else
13414 T := Process_Subtype (Obj_Def, Related_Nod);
13415 end if;
13416
13417 return T;
13418 end Find_Type_Of_Object;
13419
13420 --------------------------------
13421 -- Find_Type_Of_Subtype_Indic --
13422 --------------------------------
13423
13424 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
13425 Typ : Entity_Id;
13426
13427 begin
13428 -- Case of subtype mark with a constraint
13429
13430 if Nkind (S) = N_Subtype_Indication then
13431 Find_Type (Subtype_Mark (S));
13432 Typ := Entity (Subtype_Mark (S));
13433
13434 if not
13435 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
13436 then
13437 Error_Msg_N
13438 ("incorrect constraint for this kind of type", Constraint (S));
13439 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
13440 end if;
13441
13442 -- Otherwise we have a subtype mark without a constraint
13443
13444 elsif Error_Posted (S) then
13445 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
13446 return Any_Type;
13447
13448 else
13449 Find_Type (S);
13450 Typ := Entity (S);
13451 end if;
13452
13453 -- Check No_Wide_Characters restriction
13454
13455 if Typ = Standard_Wide_Character
13456 or else Typ = Standard_Wide_Wide_Character
13457 or else Typ = Standard_Wide_String
13458 or else Typ = Standard_Wide_Wide_String
13459 then
13460 Check_Restriction (No_Wide_Characters, S);
13461 end if;
13462
13463 return Typ;
13464 end Find_Type_Of_Subtype_Indic;
13465
13466 -------------------------------------
13467 -- Floating_Point_Type_Declaration --
13468 -------------------------------------
13469
13470 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13471 Digs : constant Node_Id := Digits_Expression (Def);
13472 Digs_Val : Uint;
13473 Base_Typ : Entity_Id;
13474 Implicit_Base : Entity_Id;
13475 Bound : Node_Id;
13476
13477 function Can_Derive_From (E : Entity_Id) return Boolean;
13478 -- Find if given digits value allows derivation from specified type
13479
13480 ---------------------
13481 -- Can_Derive_From --
13482 ---------------------
13483
13484 function Can_Derive_From (E : Entity_Id) return Boolean is
13485 Spec : constant Entity_Id := Real_Range_Specification (Def);
13486
13487 begin
13488 if Digs_Val > Digits_Value (E) then
13489 return False;
13490 end if;
13491
13492 if Present (Spec) then
13493 if Expr_Value_R (Type_Low_Bound (E)) >
13494 Expr_Value_R (Low_Bound (Spec))
13495 then
13496 return False;
13497 end if;
13498
13499 if Expr_Value_R (Type_High_Bound (E)) <
13500 Expr_Value_R (High_Bound (Spec))
13501 then
13502 return False;
13503 end if;
13504 end if;
13505
13506 return True;
13507 end Can_Derive_From;
13508
13509 -- Start of processing for Floating_Point_Type_Declaration
13510
13511 begin
13512 Check_Restriction (No_Floating_Point, Def);
13513
13514 -- Create an implicit base type
13515
13516 Implicit_Base :=
13517 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
13518
13519 -- Analyze and verify digits value
13520
13521 Analyze_And_Resolve (Digs, Any_Integer);
13522 Check_Digits_Expression (Digs);
13523 Digs_Val := Expr_Value (Digs);
13524
13525 -- Process possible range spec and find correct type to derive from
13526
13527 Process_Real_Range_Specification (Def);
13528
13529 if Can_Derive_From (Standard_Short_Float) then
13530 Base_Typ := Standard_Short_Float;
13531 elsif Can_Derive_From (Standard_Float) then
13532 Base_Typ := Standard_Float;
13533 elsif Can_Derive_From (Standard_Long_Float) then
13534 Base_Typ := Standard_Long_Float;
13535 elsif Can_Derive_From (Standard_Long_Long_Float) then
13536 Base_Typ := Standard_Long_Long_Float;
13537
13538 -- If we can't derive from any existing type, use long_long_float
13539 -- and give appropriate message explaining the problem.
13540
13541 else
13542 Base_Typ := Standard_Long_Long_Float;
13543
13544 if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
13545 Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
13546 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
13547
13548 else
13549 Error_Msg_N
13550 ("range too large for any predefined type",
13551 Real_Range_Specification (Def));
13552 end if;
13553 end if;
13554
13555 -- If there are bounds given in the declaration use them as the bounds
13556 -- of the type, otherwise use the bounds of the predefined base type
13557 -- that was chosen based on the Digits value.
13558
13559 if Present (Real_Range_Specification (Def)) then
13560 Set_Scalar_Range (T, Real_Range_Specification (Def));
13561 Set_Is_Constrained (T);
13562
13563 -- The bounds of this range must be converted to machine numbers
13564 -- in accordance with RM 4.9(38).
13565
13566 Bound := Type_Low_Bound (T);
13567
13568 if Nkind (Bound) = N_Real_Literal then
13569 Set_Realval
13570 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13571 Set_Is_Machine_Number (Bound);
13572 end if;
13573
13574 Bound := Type_High_Bound (T);
13575
13576 if Nkind (Bound) = N_Real_Literal then
13577 Set_Realval
13578 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13579 Set_Is_Machine_Number (Bound);
13580 end if;
13581
13582 else
13583 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
13584 end if;
13585
13586 -- Complete definition of implicit base and declared first subtype
13587
13588 Set_Etype (Implicit_Base, Base_Typ);
13589
13590 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
13591 Set_Size_Info (Implicit_Base, (Base_Typ));
13592 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
13593 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
13594 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
13595 Set_Vax_Float (Implicit_Base, Vax_Float (Base_Typ));
13596
13597 Set_Ekind (T, E_Floating_Point_Subtype);
13598 Set_Etype (T, Implicit_Base);
13599
13600 Set_Size_Info (T, (Implicit_Base));
13601 Set_RM_Size (T, RM_Size (Implicit_Base));
13602 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13603 Set_Digits_Value (T, Digs_Val);
13604 end Floating_Point_Type_Declaration;
13605
13606 ----------------------------
13607 -- Get_Discriminant_Value --
13608 ----------------------------
13609
13610 -- This is the situation:
13611
13612 -- There is a non-derived type
13613
13614 -- type T0 (Dx, Dy, Dz...)
13615
13616 -- There are zero or more levels of derivation, with each derivation
13617 -- either purely inheriting the discriminants, or defining its own.
13618
13619 -- type Ti is new Ti-1
13620 -- or
13621 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
13622 -- or
13623 -- subtype Ti is ...
13624
13625 -- The subtype issue is avoided by the use of Original_Record_Component,
13626 -- and the fact that derived subtypes also derive the constraints.
13627
13628 -- This chain leads back from
13629
13630 -- Typ_For_Constraint
13631
13632 -- Typ_For_Constraint has discriminants, and the value for each
13633 -- discriminant is given by its corresponding Elmt of Constraints.
13634
13635 -- Discriminant is some discriminant in this hierarchy
13636
13637 -- We need to return its value
13638
13639 -- We do this by recursively searching each level, and looking for
13640 -- Discriminant. Once we get to the bottom, we start backing up
13641 -- returning the value for it which may in turn be a discriminant
13642 -- further up, so on the backup we continue the substitution.
13643
13644 function Get_Discriminant_Value
13645 (Discriminant : Entity_Id;
13646 Typ_For_Constraint : Entity_Id;
13647 Constraint : Elist_Id) return Node_Id
13648 is
13649 function Search_Derivation_Levels
13650 (Ti : Entity_Id;
13651 Discrim_Values : Elist_Id;
13652 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
13653 -- This is the routine that performs the recursive search of levels
13654 -- as described above.
13655
13656 ------------------------------
13657 -- Search_Derivation_Levels --
13658 ------------------------------
13659
13660 function Search_Derivation_Levels
13661 (Ti : Entity_Id;
13662 Discrim_Values : Elist_Id;
13663 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
13664 is
13665 Assoc : Elmt_Id;
13666 Disc : Entity_Id;
13667 Result : Node_Or_Entity_Id;
13668 Result_Entity : Node_Id;
13669
13670 begin
13671 -- If inappropriate type, return Error, this happens only in
13672 -- cascaded error situations, and we want to avoid a blow up.
13673
13674 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
13675 return Error;
13676 end if;
13677
13678 -- Look deeper if possible. Use Stored_Constraints only for
13679 -- untagged types. For tagged types use the given constraint.
13680 -- This asymmetry needs explanation???
13681
13682 if not Stored_Discrim_Values
13683 and then Present (Stored_Constraint (Ti))
13684 and then not Is_Tagged_Type (Ti)
13685 then
13686 Result :=
13687 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
13688 else
13689 declare
13690 Td : constant Entity_Id := Etype (Ti);
13691
13692 begin
13693 if Td = Ti then
13694 Result := Discriminant;
13695
13696 else
13697 if Present (Stored_Constraint (Ti)) then
13698 Result :=
13699 Search_Derivation_Levels
13700 (Td, Stored_Constraint (Ti), True);
13701 else
13702 Result :=
13703 Search_Derivation_Levels
13704 (Td, Discrim_Values, Stored_Discrim_Values);
13705 end if;
13706 end if;
13707 end;
13708 end if;
13709
13710 -- Extra underlying places to search, if not found above. For
13711 -- concurrent types, the relevant discriminant appears in the
13712 -- corresponding record. For a type derived from a private type
13713 -- without discriminant, the full view inherits the discriminants
13714 -- of the full view of the parent.
13715
13716 if Result = Discriminant then
13717 if Is_Concurrent_Type (Ti)
13718 and then Present (Corresponding_Record_Type (Ti))
13719 then
13720 Result :=
13721 Search_Derivation_Levels (
13722 Corresponding_Record_Type (Ti),
13723 Discrim_Values,
13724 Stored_Discrim_Values);
13725
13726 elsif Is_Private_Type (Ti)
13727 and then not Has_Discriminants (Ti)
13728 and then Present (Full_View (Ti))
13729 and then Etype (Full_View (Ti)) /= Ti
13730 then
13731 Result :=
13732 Search_Derivation_Levels (
13733 Full_View (Ti),
13734 Discrim_Values,
13735 Stored_Discrim_Values);
13736 end if;
13737 end if;
13738
13739 -- If Result is not a (reference to a) discriminant, return it,
13740 -- otherwise set Result_Entity to the discriminant.
13741
13742 if Nkind (Result) = N_Defining_Identifier then
13743 pragma Assert (Result = Discriminant);
13744 Result_Entity := Result;
13745
13746 else
13747 if not Denotes_Discriminant (Result) then
13748 return Result;
13749 end if;
13750
13751 Result_Entity := Entity (Result);
13752 end if;
13753
13754 -- See if this level of derivation actually has discriminants
13755 -- because tagged derivations can add them, hence the lower
13756 -- levels need not have any.
13757
13758 if not Has_Discriminants (Ti) then
13759 return Result;
13760 end if;
13761
13762 -- Scan Ti's discriminants for Result_Entity,
13763 -- and return its corresponding value, if any.
13764
13765 Result_Entity := Original_Record_Component (Result_Entity);
13766
13767 Assoc := First_Elmt (Discrim_Values);
13768
13769 if Stored_Discrim_Values then
13770 Disc := First_Stored_Discriminant (Ti);
13771 else
13772 Disc := First_Discriminant (Ti);
13773 end if;
13774
13775 while Present (Disc) loop
13776 pragma Assert (Present (Assoc));
13777
13778 if Original_Record_Component (Disc) = Result_Entity then
13779 return Node (Assoc);
13780 end if;
13781
13782 Next_Elmt (Assoc);
13783
13784 if Stored_Discrim_Values then
13785 Next_Stored_Discriminant (Disc);
13786 else
13787 Next_Discriminant (Disc);
13788 end if;
13789 end loop;
13790
13791 -- Could not find it
13792 --
13793 return Result;
13794 end Search_Derivation_Levels;
13795
13796 -- Local Variables
13797
13798 Result : Node_Or_Entity_Id;
13799
13800 -- Start of processing for Get_Discriminant_Value
13801
13802 begin
13803 -- ??? This routine is a gigantic mess and will be deleted. For the
13804 -- time being just test for the trivial case before calling recurse.
13805
13806 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
13807 declare
13808 D : Entity_Id;
13809 E : Elmt_Id;
13810
13811 begin
13812 D := First_Discriminant (Typ_For_Constraint);
13813 E := First_Elmt (Constraint);
13814 while Present (D) loop
13815 if Chars (D) = Chars (Discriminant) then
13816 return Node (E);
13817 end if;
13818
13819 Next_Discriminant (D);
13820 Next_Elmt (E);
13821 end loop;
13822 end;
13823 end if;
13824
13825 Result := Search_Derivation_Levels
13826 (Typ_For_Constraint, Constraint, False);
13827
13828 -- ??? hack to disappear when this routine is gone
13829
13830 if Nkind (Result) = N_Defining_Identifier then
13831 declare
13832 D : Entity_Id;
13833 E : Elmt_Id;
13834
13835 begin
13836 D := First_Discriminant (Typ_For_Constraint);
13837 E := First_Elmt (Constraint);
13838 while Present (D) loop
13839 if Corresponding_Discriminant (D) = Discriminant then
13840 return Node (E);
13841 end if;
13842
13843 Next_Discriminant (D);
13844 Next_Elmt (E);
13845 end loop;
13846 end;
13847 end if;
13848
13849 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
13850 return Result;
13851 end Get_Discriminant_Value;
13852
13853 --------------------------
13854 -- Has_Range_Constraint --
13855 --------------------------
13856
13857 function Has_Range_Constraint (N : Node_Id) return Boolean is
13858 C : constant Node_Id := Constraint (N);
13859
13860 begin
13861 if Nkind (C) = N_Range_Constraint then
13862 return True;
13863
13864 elsif Nkind (C) = N_Digits_Constraint then
13865 return
13866 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
13867 or else
13868 Present (Range_Constraint (C));
13869
13870 elsif Nkind (C) = N_Delta_Constraint then
13871 return Present (Range_Constraint (C));
13872
13873 else
13874 return False;
13875 end if;
13876 end Has_Range_Constraint;
13877
13878 ------------------------
13879 -- Inherit_Components --
13880 ------------------------
13881
13882 function Inherit_Components
13883 (N : Node_Id;
13884 Parent_Base : Entity_Id;
13885 Derived_Base : Entity_Id;
13886 Is_Tagged : Boolean;
13887 Inherit_Discr : Boolean;
13888 Discs : Elist_Id) return Elist_Id
13889 is
13890 Assoc_List : constant Elist_Id := New_Elmt_List;
13891
13892 procedure Inherit_Component
13893 (Old_C : Entity_Id;
13894 Plain_Discrim : Boolean := False;
13895 Stored_Discrim : Boolean := False);
13896 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
13897 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
13898 -- True, Old_C is a stored discriminant. If they are both false then
13899 -- Old_C is a regular component.
13900
13901 -----------------------
13902 -- Inherit_Component --
13903 -----------------------
13904
13905 procedure Inherit_Component
13906 (Old_C : Entity_Id;
13907 Plain_Discrim : Boolean := False;
13908 Stored_Discrim : Boolean := False)
13909 is
13910 New_C : constant Entity_Id := New_Copy (Old_C);
13911
13912 Discrim : Entity_Id;
13913 Corr_Discrim : Entity_Id;
13914
13915 begin
13916 pragma Assert (not Is_Tagged or else not Stored_Discrim);
13917
13918 Set_Parent (New_C, Parent (Old_C));
13919
13920 -- Regular discriminants and components must be inserted in the scope
13921 -- of the Derived_Base. Do it here.
13922
13923 if not Stored_Discrim then
13924 Enter_Name (New_C);
13925 end if;
13926
13927 -- For tagged types the Original_Record_Component must point to
13928 -- whatever this field was pointing to in the parent type. This has
13929 -- already been achieved by the call to New_Copy above.
13930
13931 if not Is_Tagged then
13932 Set_Original_Record_Component (New_C, New_C);
13933 end if;
13934
13935 -- If we have inherited a component then see if its Etype contains
13936 -- references to Parent_Base discriminants. In this case, replace
13937 -- these references with the constraints given in Discs. We do not
13938 -- do this for the partial view of private types because this is
13939 -- not needed (only the components of the full view will be used
13940 -- for code generation) and cause problem. We also avoid this
13941 -- transformation in some error situations.
13942
13943 if Ekind (New_C) = E_Component then
13944 if (Is_Private_Type (Derived_Base)
13945 and then not Is_Generic_Type (Derived_Base))
13946 or else (Is_Empty_Elmt_List (Discs)
13947 and then not Expander_Active)
13948 then
13949 Set_Etype (New_C, Etype (Old_C));
13950
13951 else
13952 -- The current component introduces a circularity of the
13953 -- following kind:
13954
13955 -- limited with Pack_2;
13956 -- package Pack_1 is
13957 -- type T_1 is tagged record
13958 -- Comp : access Pack_2.T_2;
13959 -- ...
13960 -- end record;
13961 -- end Pack_1;
13962
13963 -- with Pack_1;
13964 -- package Pack_2 is
13965 -- type T_2 is new Pack_1.T_1 with ...;
13966 -- end Pack_2;
13967
13968 Set_Etype
13969 (New_C,
13970 Constrain_Component_Type
13971 (Old_C, Derived_Base, N, Parent_Base, Discs));
13972 end if;
13973 end if;
13974
13975 -- In derived tagged types it is illegal to reference a non
13976 -- discriminant component in the parent type. To catch this, mark
13977 -- these components with an Ekind of E_Void. This will be reset in
13978 -- Record_Type_Definition after processing the record extension of
13979 -- the derived type.
13980
13981 -- If the declaration is a private extension, there is no further
13982 -- record extension to process, and the components retain their
13983 -- current kind, because they are visible at this point.
13984
13985 if Is_Tagged and then Ekind (New_C) = E_Component
13986 and then Nkind (N) /= N_Private_Extension_Declaration
13987 then
13988 Set_Ekind (New_C, E_Void);
13989 end if;
13990
13991 if Plain_Discrim then
13992 Set_Corresponding_Discriminant (New_C, Old_C);
13993 Build_Discriminal (New_C);
13994
13995 -- If we are explicitly inheriting a stored discriminant it will be
13996 -- completely hidden.
13997
13998 elsif Stored_Discrim then
13999 Set_Corresponding_Discriminant (New_C, Empty);
14000 Set_Discriminal (New_C, Empty);
14001 Set_Is_Completely_Hidden (New_C);
14002
14003 -- Set the Original_Record_Component of each discriminant in the
14004 -- derived base to point to the corresponding stored that we just
14005 -- created.
14006
14007 Discrim := First_Discriminant (Derived_Base);
14008 while Present (Discrim) loop
14009 Corr_Discrim := Corresponding_Discriminant (Discrim);
14010
14011 -- Corr_Discrim could be missing in an error situation
14012
14013 if Present (Corr_Discrim)
14014 and then Original_Record_Component (Corr_Discrim) = Old_C
14015 then
14016 Set_Original_Record_Component (Discrim, New_C);
14017 end if;
14018
14019 Next_Discriminant (Discrim);
14020 end loop;
14021
14022 Append_Entity (New_C, Derived_Base);
14023 end if;
14024
14025 if not Is_Tagged then
14026 Append_Elmt (Old_C, Assoc_List);
14027 Append_Elmt (New_C, Assoc_List);
14028 end if;
14029 end Inherit_Component;
14030
14031 -- Variables local to Inherit_Component
14032
14033 Loc : constant Source_Ptr := Sloc (N);
14034
14035 Parent_Discrim : Entity_Id;
14036 Stored_Discrim : Entity_Id;
14037 D : Entity_Id;
14038 Component : Entity_Id;
14039
14040 -- Start of processing for Inherit_Components
14041
14042 begin
14043 if not Is_Tagged then
14044 Append_Elmt (Parent_Base, Assoc_List);
14045 Append_Elmt (Derived_Base, Assoc_List);
14046 end if;
14047
14048 -- Inherit parent discriminants if needed
14049
14050 if Inherit_Discr then
14051 Parent_Discrim := First_Discriminant (Parent_Base);
14052 while Present (Parent_Discrim) loop
14053 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
14054 Next_Discriminant (Parent_Discrim);
14055 end loop;
14056 end if;
14057
14058 -- Create explicit stored discrims for untagged types when necessary
14059
14060 if not Has_Unknown_Discriminants (Derived_Base)
14061 and then Has_Discriminants (Parent_Base)
14062 and then not Is_Tagged
14063 and then
14064 (not Inherit_Discr
14065 or else First_Discriminant (Parent_Base) /=
14066 First_Stored_Discriminant (Parent_Base))
14067 then
14068 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
14069 while Present (Stored_Discrim) loop
14070 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
14071 Next_Stored_Discriminant (Stored_Discrim);
14072 end loop;
14073 end if;
14074
14075 -- See if we can apply the second transformation for derived types, as
14076 -- explained in point 6. in the comments above Build_Derived_Record_Type
14077 -- This is achieved by appending Derived_Base discriminants into Discs,
14078 -- which has the side effect of returning a non empty Discs list to the
14079 -- caller of Inherit_Components, which is what we want. This must be
14080 -- done for private derived types if there are explicit stored
14081 -- discriminants, to ensure that we can retrieve the values of the
14082 -- constraints provided in the ancestors.
14083
14084 if Inherit_Discr
14085 and then Is_Empty_Elmt_List (Discs)
14086 and then Present (First_Discriminant (Derived_Base))
14087 and then
14088 (not Is_Private_Type (Derived_Base)
14089 or else Is_Completely_Hidden
14090 (First_Stored_Discriminant (Derived_Base))
14091 or else Is_Generic_Type (Derived_Base))
14092 then
14093 D := First_Discriminant (Derived_Base);
14094 while Present (D) loop
14095 Append_Elmt (New_Reference_To (D, Loc), Discs);
14096 Next_Discriminant (D);
14097 end loop;
14098 end if;
14099
14100 -- Finally, inherit non-discriminant components unless they are not
14101 -- visible because defined or inherited from the full view of the
14102 -- parent. Don't inherit the _parent field of the parent type.
14103
14104 Component := First_Entity (Parent_Base);
14105 while Present (Component) loop
14106
14107 -- Ada 2005 (AI-251): Do not inherit components associated with
14108 -- secondary tags of the parent.
14109
14110 if Ekind (Component) = E_Component
14111 and then Present (Related_Type (Component))
14112 then
14113 null;
14114
14115 elsif Ekind (Component) /= E_Component
14116 or else Chars (Component) = Name_uParent
14117 then
14118 null;
14119
14120 -- If the derived type is within the parent type's declarative
14121 -- region, then the components can still be inherited even though
14122 -- they aren't visible at this point. This can occur for cases
14123 -- such as within public child units where the components must
14124 -- become visible upon entering the child unit's private part.
14125
14126 elsif not Is_Visible_Component (Component)
14127 and then not In_Open_Scopes (Scope (Parent_Base))
14128 then
14129 null;
14130
14131 elsif Ekind (Derived_Base) = E_Private_Type
14132 or else Ekind (Derived_Base) = E_Limited_Private_Type
14133 then
14134 null;
14135
14136 else
14137 Inherit_Component (Component);
14138 end if;
14139
14140 Next_Entity (Component);
14141 end loop;
14142
14143 -- For tagged derived types, inherited discriminants cannot be used in
14144 -- component declarations of the record extension part. To achieve this
14145 -- we mark the inherited discriminants as not visible.
14146
14147 if Is_Tagged and then Inherit_Discr then
14148 D := First_Discriminant (Derived_Base);
14149 while Present (D) loop
14150 Set_Is_Immediately_Visible (D, False);
14151 Next_Discriminant (D);
14152 end loop;
14153 end if;
14154
14155 return Assoc_List;
14156 end Inherit_Components;
14157
14158 -----------------------
14159 -- Is_Null_Extension --
14160 -----------------------
14161
14162 function Is_Null_Extension (T : Entity_Id) return Boolean is
14163 Type_Decl : constant Node_Id := Parent (T);
14164 Comp_List : Node_Id;
14165 Comp : Node_Id;
14166
14167 begin
14168 if Nkind (Type_Decl) /= N_Full_Type_Declaration
14169 or else not Is_Tagged_Type (T)
14170 or else Nkind (Type_Definition (Type_Decl)) /=
14171 N_Derived_Type_Definition
14172 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
14173 then
14174 return False;
14175 end if;
14176
14177 Comp_List :=
14178 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
14179
14180 if Present (Discriminant_Specifications (Type_Decl)) then
14181 return False;
14182
14183 elsif Present (Comp_List)
14184 and then Is_Non_Empty_List (Component_Items (Comp_List))
14185 then
14186 Comp := First (Component_Items (Comp_List));
14187
14188 -- Only user-defined components are relevant. The component list
14189 -- may also contain a parent component and internal components
14190 -- corresponding to secondary tags, but these do not determine
14191 -- whether this is a null extension.
14192
14193 while Present (Comp) loop
14194 if Comes_From_Source (Comp) then
14195 return False;
14196 end if;
14197
14198 Next (Comp);
14199 end loop;
14200
14201 return True;
14202 else
14203 return True;
14204 end if;
14205 end Is_Null_Extension;
14206
14207 --------------------
14208 -- Is_Progenitor --
14209 --------------------
14210
14211 function Is_Progenitor
14212 (Iface : Entity_Id;
14213 Typ : Entity_Id) return Boolean
14214 is
14215 begin
14216 return Implements_Interface (Typ, Iface,
14217 Exclude_Parents => True);
14218 end Is_Progenitor;
14219
14220 ------------------------------
14221 -- Is_Valid_Constraint_Kind --
14222 ------------------------------
14223
14224 function Is_Valid_Constraint_Kind
14225 (T_Kind : Type_Kind;
14226 Constraint_Kind : Node_Kind) return Boolean
14227 is
14228 begin
14229 case T_Kind is
14230 when Enumeration_Kind |
14231 Integer_Kind =>
14232 return Constraint_Kind = N_Range_Constraint;
14233
14234 when Decimal_Fixed_Point_Kind =>
14235 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14236 N_Range_Constraint);
14237
14238 when Ordinary_Fixed_Point_Kind =>
14239 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
14240 N_Range_Constraint);
14241
14242 when Float_Kind =>
14243 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14244 N_Range_Constraint);
14245
14246 when Access_Kind |
14247 Array_Kind |
14248 E_Record_Type |
14249 E_Record_Subtype |
14250 Class_Wide_Kind |
14251 E_Incomplete_Type |
14252 Private_Kind |
14253 Concurrent_Kind =>
14254 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
14255
14256 when others =>
14257 return True; -- Error will be detected later
14258 end case;
14259 end Is_Valid_Constraint_Kind;
14260
14261 --------------------------
14262 -- Is_Visible_Component --
14263 --------------------------
14264
14265 function Is_Visible_Component (C : Entity_Id) return Boolean is
14266 Original_Comp : Entity_Id := Empty;
14267 Original_Scope : Entity_Id;
14268 Type_Scope : Entity_Id;
14269
14270 function Is_Local_Type (Typ : Entity_Id) return Boolean;
14271 -- Check whether parent type of inherited component is declared locally,
14272 -- possibly within a nested package or instance. The current scope is
14273 -- the derived record itself.
14274
14275 -------------------
14276 -- Is_Local_Type --
14277 -------------------
14278
14279 function Is_Local_Type (Typ : Entity_Id) return Boolean is
14280 Scop : Entity_Id;
14281
14282 begin
14283 Scop := Scope (Typ);
14284 while Present (Scop)
14285 and then Scop /= Standard_Standard
14286 loop
14287 if Scop = Scope (Current_Scope) then
14288 return True;
14289 end if;
14290
14291 Scop := Scope (Scop);
14292 end loop;
14293
14294 return False;
14295 end Is_Local_Type;
14296
14297 -- Start of processing for Is_Visible_Component
14298
14299 begin
14300 if Ekind (C) = E_Component
14301 or else Ekind (C) = E_Discriminant
14302 then
14303 Original_Comp := Original_Record_Component (C);
14304 end if;
14305
14306 if No (Original_Comp) then
14307
14308 -- Premature usage, or previous error
14309
14310 return False;
14311
14312 else
14313 Original_Scope := Scope (Original_Comp);
14314 Type_Scope := Scope (Base_Type (Scope (C)));
14315 end if;
14316
14317 -- This test only concerns tagged types
14318
14319 if not Is_Tagged_Type (Original_Scope) then
14320 return True;
14321
14322 -- If it is _Parent or _Tag, there is no visibility issue
14323
14324 elsif not Comes_From_Source (Original_Comp) then
14325 return True;
14326
14327 -- If we are in the body of an instantiation, the component is visible
14328 -- even when the parent type (possibly defined in an enclosing unit or
14329 -- in a parent unit) might not.
14330
14331 elsif In_Instance_Body then
14332 return True;
14333
14334 -- Discriminants are always visible
14335
14336 elsif Ekind (Original_Comp) = E_Discriminant
14337 and then not Has_Unknown_Discriminants (Original_Scope)
14338 then
14339 return True;
14340
14341 -- If the component has been declared in an ancestor which is currently
14342 -- a private type, then it is not visible. The same applies if the
14343 -- component's containing type is not in an open scope and the original
14344 -- component's enclosing type is a visible full view of a private type
14345 -- (which can occur in cases where an attempt is being made to reference
14346 -- a component in a sibling package that is inherited from a visible
14347 -- component of a type in an ancestor package; the component in the
14348 -- sibling package should not be visible even though the component it
14349 -- inherited from is visible). This does not apply however in the case
14350 -- where the scope of the type is a private child unit, or when the
14351 -- parent comes from a local package in which the ancestor is currently
14352 -- visible. The latter suppression of visibility is needed for cases
14353 -- that are tested in B730006.
14354
14355 elsif Is_Private_Type (Original_Scope)
14356 or else
14357 (not Is_Private_Descendant (Type_Scope)
14358 and then not In_Open_Scopes (Type_Scope)
14359 and then Has_Private_Declaration (Original_Scope))
14360 then
14361 -- If the type derives from an entity in a formal package, there
14362 -- are no additional visible components.
14363
14364 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
14365 N_Formal_Package_Declaration
14366 then
14367 return False;
14368
14369 -- if we are not in the private part of the current package, there
14370 -- are no additional visible components.
14371
14372 elsif Ekind (Scope (Current_Scope)) = E_Package
14373 and then not In_Private_Part (Scope (Current_Scope))
14374 then
14375 return False;
14376 else
14377 return
14378 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
14379 and then In_Open_Scopes (Scope (Original_Scope))
14380 and then Is_Local_Type (Type_Scope);
14381 end if;
14382
14383 -- There is another weird way in which a component may be invisible
14384 -- when the private and the full view are not derived from the same
14385 -- ancestor. Here is an example :
14386
14387 -- type A1 is tagged record F1 : integer; end record;
14388 -- type A2 is new A1 with record F2 : integer; end record;
14389 -- type T is new A1 with private;
14390 -- private
14391 -- type T is new A2 with null record;
14392
14393 -- In this case, the full view of T inherits F1 and F2 but the private
14394 -- view inherits only F1
14395
14396 else
14397 declare
14398 Ancestor : Entity_Id := Scope (C);
14399
14400 begin
14401 loop
14402 if Ancestor = Original_Scope then
14403 return True;
14404 elsif Ancestor = Etype (Ancestor) then
14405 return False;
14406 end if;
14407
14408 Ancestor := Etype (Ancestor);
14409 end loop;
14410 end;
14411 end if;
14412 end Is_Visible_Component;
14413
14414 --------------------------
14415 -- Make_Class_Wide_Type --
14416 --------------------------
14417
14418 procedure Make_Class_Wide_Type (T : Entity_Id) is
14419 CW_Type : Entity_Id;
14420 CW_Name : Name_Id;
14421 Next_E : Entity_Id;
14422
14423 begin
14424 -- The class wide type can have been defined by the partial view, in
14425 -- which case everything is already done.
14426
14427 if Present (Class_Wide_Type (T)) then
14428 return;
14429 end if;
14430
14431 CW_Type :=
14432 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
14433
14434 -- Inherit root type characteristics
14435
14436 CW_Name := Chars (CW_Type);
14437 Next_E := Next_Entity (CW_Type);
14438 Copy_Node (T, CW_Type);
14439 Set_Comes_From_Source (CW_Type, False);
14440 Set_Chars (CW_Type, CW_Name);
14441 Set_Parent (CW_Type, Parent (T));
14442 Set_Next_Entity (CW_Type, Next_E);
14443
14444 -- Ensure we have a new freeze node for the class-wide type. The partial
14445 -- view may have freeze action of its own, requiring a proper freeze
14446 -- node, and the same freeze node cannot be shared between the two
14447 -- types.
14448
14449 Set_Has_Delayed_Freeze (CW_Type);
14450 Set_Freeze_Node (CW_Type, Empty);
14451
14452 -- Customize the class-wide type: It has no prim. op., it cannot be
14453 -- abstract and its Etype points back to the specific root type.
14454
14455 Set_Ekind (CW_Type, E_Class_Wide_Type);
14456 Set_Is_Tagged_Type (CW_Type, True);
14457 Set_Primitive_Operations (CW_Type, New_Elmt_List);
14458 Set_Is_Abstract_Type (CW_Type, False);
14459 Set_Is_Constrained (CW_Type, False);
14460 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
14461
14462 if Ekind (T) = E_Class_Wide_Subtype then
14463 Set_Etype (CW_Type, Etype (Base_Type (T)));
14464 else
14465 Set_Etype (CW_Type, T);
14466 end if;
14467
14468 -- If this is the class_wide type of a constrained subtype, it does
14469 -- not have discriminants.
14470
14471 Set_Has_Discriminants (CW_Type,
14472 Has_Discriminants (T) and then not Is_Constrained (T));
14473
14474 Set_Has_Unknown_Discriminants (CW_Type, True);
14475 Set_Class_Wide_Type (T, CW_Type);
14476 Set_Equivalent_Type (CW_Type, Empty);
14477
14478 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
14479
14480 Set_Class_Wide_Type (CW_Type, CW_Type);
14481 end Make_Class_Wide_Type;
14482
14483 ----------------
14484 -- Make_Index --
14485 ----------------
14486
14487 procedure Make_Index
14488 (I : Node_Id;
14489 Related_Nod : Node_Id;
14490 Related_Id : Entity_Id := Empty;
14491 Suffix_Index : Nat := 1)
14492 is
14493 R : Node_Id;
14494 T : Entity_Id;
14495 Def_Id : Entity_Id := Empty;
14496 Found : Boolean := False;
14497
14498 begin
14499 -- For a discrete range used in a constrained array definition and
14500 -- defined by a range, an implicit conversion to the predefined type
14501 -- INTEGER is assumed if each bound is either a numeric literal, a named
14502 -- number, or an attribute, and the type of both bounds (prior to the
14503 -- implicit conversion) is the type universal_integer. Otherwise, both
14504 -- bounds must be of the same discrete type, other than universal
14505 -- integer; this type must be determinable independently of the
14506 -- context, but using the fact that the type must be discrete and that
14507 -- both bounds must have the same type.
14508
14509 -- Character literals also have a universal type in the absence of
14510 -- of additional context, and are resolved to Standard_Character.
14511
14512 if Nkind (I) = N_Range then
14513
14514 -- The index is given by a range constraint. The bounds are known
14515 -- to be of a consistent type.
14516
14517 if not Is_Overloaded (I) then
14518 T := Etype (I);
14519
14520 -- For universal bounds, choose the specific predefined type
14521
14522 if T = Universal_Integer then
14523 T := Standard_Integer;
14524
14525 elsif T = Any_Character then
14526 Ambiguous_Character (Low_Bound (I));
14527
14528 T := Standard_Character;
14529 end if;
14530
14531 -- The node may be overloaded because some user-defined operators
14532 -- are available, but if a universal interpretation exists it is
14533 -- also the selected one.
14534
14535 elsif Universal_Interpretation (I) = Universal_Integer then
14536 T := Standard_Integer;
14537
14538 else
14539 T := Any_Type;
14540
14541 declare
14542 Ind : Interp_Index;
14543 It : Interp;
14544
14545 begin
14546 Get_First_Interp (I, Ind, It);
14547 while Present (It.Typ) loop
14548 if Is_Discrete_Type (It.Typ) then
14549
14550 if Found
14551 and then not Covers (It.Typ, T)
14552 and then not Covers (T, It.Typ)
14553 then
14554 Error_Msg_N ("ambiguous bounds in discrete range", I);
14555 exit;
14556 else
14557 T := It.Typ;
14558 Found := True;
14559 end if;
14560 end if;
14561
14562 Get_Next_Interp (Ind, It);
14563 end loop;
14564
14565 if T = Any_Type then
14566 Error_Msg_N ("discrete type required for range", I);
14567 Set_Etype (I, Any_Type);
14568 return;
14569
14570 elsif T = Universal_Integer then
14571 T := Standard_Integer;
14572 end if;
14573 end;
14574 end if;
14575
14576 if not Is_Discrete_Type (T) then
14577 Error_Msg_N ("discrete type required for range", I);
14578 Set_Etype (I, Any_Type);
14579 return;
14580 end if;
14581
14582 if Nkind (Low_Bound (I)) = N_Attribute_Reference
14583 and then Attribute_Name (Low_Bound (I)) = Name_First
14584 and then Is_Entity_Name (Prefix (Low_Bound (I)))
14585 and then Is_Type (Entity (Prefix (Low_Bound (I))))
14586 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
14587 then
14588 -- The type of the index will be the type of the prefix, as long
14589 -- as the upper bound is 'Last of the same type.
14590
14591 Def_Id := Entity (Prefix (Low_Bound (I)));
14592
14593 if Nkind (High_Bound (I)) /= N_Attribute_Reference
14594 or else Attribute_Name (High_Bound (I)) /= Name_Last
14595 or else not Is_Entity_Name (Prefix (High_Bound (I)))
14596 or else Entity (Prefix (High_Bound (I))) /= Def_Id
14597 then
14598 Def_Id := Empty;
14599 end if;
14600 end if;
14601
14602 R := I;
14603 Process_Range_Expr_In_Decl (R, T);
14604
14605 elsif Nkind (I) = N_Subtype_Indication then
14606
14607 -- The index is given by a subtype with a range constraint
14608
14609 T := Base_Type (Entity (Subtype_Mark (I)));
14610
14611 if not Is_Discrete_Type (T) then
14612 Error_Msg_N ("discrete type required for range", I);
14613 Set_Etype (I, Any_Type);
14614 return;
14615 end if;
14616
14617 R := Range_Expression (Constraint (I));
14618
14619 Resolve (R, T);
14620 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
14621
14622 elsif Nkind (I) = N_Attribute_Reference then
14623
14624 -- The parser guarantees that the attribute is a RANGE attribute
14625
14626 -- If the node denotes the range of a type mark, that is also the
14627 -- resulting type, and we do no need to create an Itype for it.
14628
14629 if Is_Entity_Name (Prefix (I))
14630 and then Comes_From_Source (I)
14631 and then Is_Type (Entity (Prefix (I)))
14632 and then Is_Discrete_Type (Entity (Prefix (I)))
14633 then
14634 Def_Id := Entity (Prefix (I));
14635 end if;
14636
14637 Analyze_And_Resolve (I);
14638 T := Etype (I);
14639 R := I;
14640
14641 -- If none of the above, must be a subtype. We convert this to a
14642 -- range attribute reference because in the case of declared first
14643 -- named subtypes, the types in the range reference can be different
14644 -- from the type of the entity. A range attribute normalizes the
14645 -- reference and obtains the correct types for the bounds.
14646
14647 -- This transformation is in the nature of an expansion, is only
14648 -- done if expansion is active. In particular, it is not done on
14649 -- formal generic types, because we need to retain the name of the
14650 -- original index for instantiation purposes.
14651
14652 else
14653 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
14654 Error_Msg_N ("invalid subtype mark in discrete range ", I);
14655 Set_Etype (I, Any_Integer);
14656 return;
14657
14658 else
14659 -- The type mark may be that of an incomplete type. It is only
14660 -- now that we can get the full view, previous analysis does
14661 -- not look specifically for a type mark.
14662
14663 Set_Entity (I, Get_Full_View (Entity (I)));
14664 Set_Etype (I, Entity (I));
14665 Def_Id := Entity (I);
14666
14667 if not Is_Discrete_Type (Def_Id) then
14668 Error_Msg_N ("discrete type required for index", I);
14669 Set_Etype (I, Any_Type);
14670 return;
14671 end if;
14672 end if;
14673
14674 if Expander_Active then
14675 Rewrite (I,
14676 Make_Attribute_Reference (Sloc (I),
14677 Attribute_Name => Name_Range,
14678 Prefix => Relocate_Node (I)));
14679
14680 -- The original was a subtype mark that does not freeze. This
14681 -- means that the rewritten version must not freeze either.
14682
14683 Set_Must_Not_Freeze (I);
14684 Set_Must_Not_Freeze (Prefix (I));
14685
14686 -- Is order critical??? if so, document why, if not
14687 -- use Analyze_And_Resolve
14688
14689 Analyze_And_Resolve (I);
14690 T := Etype (I);
14691 R := I;
14692
14693 -- If expander is inactive, type is legal, nothing else to construct
14694
14695 else
14696 return;
14697 end if;
14698 end if;
14699
14700 if not Is_Discrete_Type (T) then
14701 Error_Msg_N ("discrete type required for range", I);
14702 Set_Etype (I, Any_Type);
14703 return;
14704
14705 elsif T = Any_Type then
14706 Set_Etype (I, Any_Type);
14707 return;
14708 end if;
14709
14710 -- We will now create the appropriate Itype to describe the range, but
14711 -- first a check. If we originally had a subtype, then we just label
14712 -- the range with this subtype. Not only is there no need to construct
14713 -- a new subtype, but it is wrong to do so for two reasons:
14714
14715 -- 1. A legality concern, if we have a subtype, it must not freeze,
14716 -- and the Itype would cause freezing incorrectly
14717
14718 -- 2. An efficiency concern, if we created an Itype, it would not be
14719 -- recognized as the same type for the purposes of eliminating
14720 -- checks in some circumstances.
14721
14722 -- We signal this case by setting the subtype entity in Def_Id
14723
14724 if No (Def_Id) then
14725 Def_Id :=
14726 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
14727 Set_Etype (Def_Id, Base_Type (T));
14728
14729 if Is_Signed_Integer_Type (T) then
14730 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14731
14732 elsif Is_Modular_Integer_Type (T) then
14733 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14734
14735 else
14736 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14737 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14738 Set_First_Literal (Def_Id, First_Literal (T));
14739 end if;
14740
14741 Set_Size_Info (Def_Id, (T));
14742 Set_RM_Size (Def_Id, RM_Size (T));
14743 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14744
14745 Set_Scalar_Range (Def_Id, R);
14746 Conditional_Delay (Def_Id, T);
14747
14748 -- In the subtype indication case, if the immediate parent of the
14749 -- new subtype is non-static, then the subtype we create is non-
14750 -- static, even if its bounds are static.
14751
14752 if Nkind (I) = N_Subtype_Indication
14753 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
14754 then
14755 Set_Is_Non_Static_Subtype (Def_Id);
14756 end if;
14757 end if;
14758
14759 -- Final step is to label the index with this constructed type
14760
14761 Set_Etype (I, Def_Id);
14762 end Make_Index;
14763
14764 ------------------------------
14765 -- Modular_Type_Declaration --
14766 ------------------------------
14767
14768 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14769 Mod_Expr : constant Node_Id := Expression (Def);
14770 M_Val : Uint;
14771
14772 procedure Set_Modular_Size (Bits : Int);
14773 -- Sets RM_Size to Bits, and Esize to normal word size above this
14774
14775 ----------------------
14776 -- Set_Modular_Size --
14777 ----------------------
14778
14779 procedure Set_Modular_Size (Bits : Int) is
14780 begin
14781 Set_RM_Size (T, UI_From_Int (Bits));
14782
14783 if Bits <= 8 then
14784 Init_Esize (T, 8);
14785
14786 elsif Bits <= 16 then
14787 Init_Esize (T, 16);
14788
14789 elsif Bits <= 32 then
14790 Init_Esize (T, 32);
14791
14792 else
14793 Init_Esize (T, System_Max_Binary_Modulus_Power);
14794 end if;
14795 end Set_Modular_Size;
14796
14797 -- Start of processing for Modular_Type_Declaration
14798
14799 begin
14800 Analyze_And_Resolve (Mod_Expr, Any_Integer);
14801 Set_Etype (T, T);
14802 Set_Ekind (T, E_Modular_Integer_Type);
14803 Init_Alignment (T);
14804 Set_Is_Constrained (T);
14805
14806 if not Is_OK_Static_Expression (Mod_Expr) then
14807 Flag_Non_Static_Expr
14808 ("non-static expression used for modular type bound!", Mod_Expr);
14809 M_Val := 2 ** System_Max_Binary_Modulus_Power;
14810 else
14811 M_Val := Expr_Value (Mod_Expr);
14812 end if;
14813
14814 if M_Val < 1 then
14815 Error_Msg_N ("modulus value must be positive", Mod_Expr);
14816 M_Val := 2 ** System_Max_Binary_Modulus_Power;
14817 end if;
14818
14819 Set_Modulus (T, M_Val);
14820
14821 -- Create bounds for the modular type based on the modulus given in
14822 -- the type declaration and then analyze and resolve those bounds.
14823
14824 Set_Scalar_Range (T,
14825 Make_Range (Sloc (Mod_Expr),
14826 Low_Bound =>
14827 Make_Integer_Literal (Sloc (Mod_Expr), 0),
14828 High_Bound =>
14829 Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
14830
14831 -- Properly analyze the literals for the range. We do this manually
14832 -- because we can't go calling Resolve, since we are resolving these
14833 -- bounds with the type, and this type is certainly not complete yet!
14834
14835 Set_Etype (Low_Bound (Scalar_Range (T)), T);
14836 Set_Etype (High_Bound (Scalar_Range (T)), T);
14837 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
14838 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
14839
14840 -- Loop through powers of two to find number of bits required
14841
14842 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
14843
14844 -- Binary case
14845
14846 if M_Val = 2 ** Bits then
14847 Set_Modular_Size (Bits);
14848 return;
14849
14850 -- Non-binary case
14851
14852 elsif M_Val < 2 ** Bits then
14853 Set_Non_Binary_Modulus (T);
14854
14855 if Bits > System_Max_Nonbinary_Modulus_Power then
14856 Error_Msg_Uint_1 :=
14857 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
14858 Error_Msg_F
14859 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
14860 Set_Modular_Size (System_Max_Binary_Modulus_Power);
14861 return;
14862
14863 else
14864 -- In the non-binary case, set size as per RM 13.3(55)
14865
14866 Set_Modular_Size (Bits);
14867 return;
14868 end if;
14869 end if;
14870
14871 end loop;
14872
14873 -- If we fall through, then the size exceed System.Max_Binary_Modulus
14874 -- so we just signal an error and set the maximum size.
14875
14876 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
14877 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
14878
14879 Set_Modular_Size (System_Max_Binary_Modulus_Power);
14880 Init_Alignment (T);
14881
14882 end Modular_Type_Declaration;
14883
14884 --------------------------
14885 -- New_Concatenation_Op --
14886 --------------------------
14887
14888 procedure New_Concatenation_Op (Typ : Entity_Id) is
14889 Loc : constant Source_Ptr := Sloc (Typ);
14890 Op : Entity_Id;
14891
14892 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
14893 -- Create abbreviated declaration for the formal of a predefined
14894 -- Operator 'Op' of type 'Typ'
14895
14896 --------------------
14897 -- Make_Op_Formal --
14898 --------------------
14899
14900 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
14901 Formal : Entity_Id;
14902 begin
14903 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
14904 Set_Etype (Formal, Typ);
14905 Set_Mechanism (Formal, Default_Mechanism);
14906 return Formal;
14907 end Make_Op_Formal;
14908
14909 -- Start of processing for New_Concatenation_Op
14910
14911 begin
14912 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
14913
14914 Set_Ekind (Op, E_Operator);
14915 Set_Scope (Op, Current_Scope);
14916 Set_Etype (Op, Typ);
14917 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
14918 Set_Is_Immediately_Visible (Op);
14919 Set_Is_Intrinsic_Subprogram (Op);
14920 Set_Has_Completion (Op);
14921 Append_Entity (Op, Current_Scope);
14922
14923 Set_Name_Entity_Id (Name_Op_Concat, Op);
14924
14925 Append_Entity (Make_Op_Formal (Typ, Op), Op);
14926 Append_Entity (Make_Op_Formal (Typ, Op), Op);
14927 end New_Concatenation_Op;
14928
14929 -------------------------
14930 -- OK_For_Limited_Init --
14931 -------------------------
14932
14933 -- ???Check all calls of this, and compare the conditions under which it's
14934 -- called.
14935
14936 function OK_For_Limited_Init (Exp : Node_Id) return Boolean is
14937 begin
14938 return Ada_Version >= Ada_05
14939 and then not Debug_Flag_Dot_L
14940 and then OK_For_Limited_Init_In_05 (Exp);
14941 end OK_For_Limited_Init;
14942
14943 -------------------------------
14944 -- OK_For_Limited_Init_In_05 --
14945 -------------------------------
14946
14947 function OK_For_Limited_Init_In_05 (Exp : Node_Id) return Boolean is
14948 begin
14949 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
14950 -- case of limited aggregates (including extension aggregates), and
14951 -- function calls. The function call may have been give in prefixed
14952 -- notation, in which case the original node is an indexed component.
14953
14954 case Nkind (Original_Node (Exp)) is
14955 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
14956 return True;
14957
14958 when N_Qualified_Expression =>
14959 return
14960 OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
14961
14962 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
14963 -- with a function call, the expander has rewritten the call into an
14964 -- N_Type_Conversion node to force displacement of the pointer to
14965 -- reference the component containing the secondary dispatch table.
14966 -- Otherwise a type conversion is not a legal context.
14967
14968 when N_Type_Conversion =>
14969 return not Comes_From_Source (Exp)
14970 and then
14971 OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
14972
14973 when N_Indexed_Component | N_Selected_Component =>
14974 return Nkind (Exp) = N_Function_Call;
14975
14976 -- A use of 'Input is a function call, hence allowed. Normally the
14977 -- attribute will be changed to a call, but the attribute by itself
14978 -- can occur with -gnatc.
14979
14980 when N_Attribute_Reference =>
14981 return Attribute_Name (Original_Node (Exp)) = Name_Input;
14982
14983 when others =>
14984 return False;
14985 end case;
14986 end OK_For_Limited_Init_In_05;
14987
14988 -------------------------------------------
14989 -- Ordinary_Fixed_Point_Type_Declaration --
14990 -------------------------------------------
14991
14992 procedure Ordinary_Fixed_Point_Type_Declaration
14993 (T : Entity_Id;
14994 Def : Node_Id)
14995 is
14996 Loc : constant Source_Ptr := Sloc (Def);
14997 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14998 RRS : constant Node_Id := Real_Range_Specification (Def);
14999 Implicit_Base : Entity_Id;
15000 Delta_Val : Ureal;
15001 Small_Val : Ureal;
15002 Low_Val : Ureal;
15003 High_Val : Ureal;
15004
15005 begin
15006 Check_Restriction (No_Fixed_Point, Def);
15007
15008 -- Create implicit base type
15009
15010 Implicit_Base :=
15011 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
15012 Set_Etype (Implicit_Base, Implicit_Base);
15013
15014 -- Analyze and process delta expression
15015
15016 Analyze_And_Resolve (Delta_Expr, Any_Real);
15017
15018 Check_Delta_Expression (Delta_Expr);
15019 Delta_Val := Expr_Value_R (Delta_Expr);
15020
15021 Set_Delta_Value (Implicit_Base, Delta_Val);
15022
15023 -- Compute default small from given delta, which is the largest power
15024 -- of two that does not exceed the given delta value.
15025
15026 declare
15027 Tmp : Ureal;
15028 Scale : Int;
15029
15030 begin
15031 Tmp := Ureal_1;
15032 Scale := 0;
15033
15034 if Delta_Val < Ureal_1 then
15035 while Delta_Val < Tmp loop
15036 Tmp := Tmp / Ureal_2;
15037 Scale := Scale + 1;
15038 end loop;
15039
15040 else
15041 loop
15042 Tmp := Tmp * Ureal_2;
15043 exit when Tmp > Delta_Val;
15044 Scale := Scale - 1;
15045 end loop;
15046 end if;
15047
15048 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
15049 end;
15050
15051 Set_Small_Value (Implicit_Base, Small_Val);
15052
15053 -- If no range was given, set a dummy range
15054
15055 if RRS <= Empty_Or_Error then
15056 Low_Val := -Small_Val;
15057 High_Val := Small_Val;
15058
15059 -- Otherwise analyze and process given range
15060
15061 else
15062 declare
15063 Low : constant Node_Id := Low_Bound (RRS);
15064 High : constant Node_Id := High_Bound (RRS);
15065
15066 begin
15067 Analyze_And_Resolve (Low, Any_Real);
15068 Analyze_And_Resolve (High, Any_Real);
15069 Check_Real_Bound (Low);
15070 Check_Real_Bound (High);
15071
15072 -- Obtain and set the range
15073
15074 Low_Val := Expr_Value_R (Low);
15075 High_Val := Expr_Value_R (High);
15076
15077 if Low_Val > High_Val then
15078 Error_Msg_NE ("?fixed point type& has null range", Def, T);
15079 end if;
15080 end;
15081 end if;
15082
15083 -- The range for both the implicit base and the declared first subtype
15084 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
15085 -- set a temporary range in place. Note that the bounds of the base
15086 -- type will be widened to be symmetrical and to fill the available
15087 -- bits when the type is frozen.
15088
15089 -- We could do this with all discrete types, and probably should, but
15090 -- we absolutely have to do it for fixed-point, since the end-points
15091 -- of the range and the size are determined by the small value, which
15092 -- could be reset before the freeze point.
15093
15094 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
15095 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15096
15097 -- Complete definition of first subtype
15098
15099 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
15100 Set_Etype (T, Implicit_Base);
15101 Init_Size_Align (T);
15102 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15103 Set_Small_Value (T, Small_Val);
15104 Set_Delta_Value (T, Delta_Val);
15105 Set_Is_Constrained (T);
15106
15107 end Ordinary_Fixed_Point_Type_Declaration;
15108
15109 ----------------------------------------
15110 -- Prepare_Private_Subtype_Completion --
15111 ----------------------------------------
15112
15113 procedure Prepare_Private_Subtype_Completion
15114 (Id : Entity_Id;
15115 Related_Nod : Node_Id)
15116 is
15117 Id_B : constant Entity_Id := Base_Type (Id);
15118 Full_B : constant Entity_Id := Full_View (Id_B);
15119 Full : Entity_Id;
15120
15121 begin
15122 if Present (Full_B) then
15123
15124 -- The Base_Type is already completed, we can complete the subtype
15125 -- now. We have to create a new entity with the same name, Thus we
15126 -- can't use Create_Itype.
15127
15128 -- This is messy, should be fixed ???
15129
15130 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
15131 Set_Is_Itype (Full);
15132 Set_Associated_Node_For_Itype (Full, Related_Nod);
15133 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
15134 end if;
15135
15136 -- The parent subtype may be private, but the base might not, in some
15137 -- nested instances. In that case, the subtype does not need to be
15138 -- exchanged. It would still be nice to make private subtypes and their
15139 -- bases consistent at all times ???
15140
15141 if Is_Private_Type (Id_B) then
15142 Append_Elmt (Id, Private_Dependents (Id_B));
15143 end if;
15144
15145 end Prepare_Private_Subtype_Completion;
15146
15147 ---------------------------
15148 -- Process_Discriminants --
15149 ---------------------------
15150
15151 procedure Process_Discriminants
15152 (N : Node_Id;
15153 Prev : Entity_Id := Empty)
15154 is
15155 Elist : constant Elist_Id := New_Elmt_List;
15156 Id : Node_Id;
15157 Discr : Node_Id;
15158 Discr_Number : Uint;
15159 Discr_Type : Entity_Id;
15160 Default_Present : Boolean := False;
15161 Default_Not_Present : Boolean := False;
15162
15163 begin
15164 -- A composite type other than an array type can have discriminants.
15165 -- On entry, the current scope is the composite type.
15166
15167 -- The discriminants are initially entered into the scope of the type
15168 -- via Enter_Name with the default Ekind of E_Void to prevent premature
15169 -- use, as explained at the end of this procedure.
15170
15171 Discr := First (Discriminant_Specifications (N));
15172 while Present (Discr) loop
15173 Enter_Name (Defining_Identifier (Discr));
15174
15175 -- For navigation purposes we add a reference to the discriminant
15176 -- in the entity for the type. If the current declaration is a
15177 -- completion, place references on the partial view. Otherwise the
15178 -- type is the current scope.
15179
15180 if Present (Prev) then
15181
15182 -- The references go on the partial view, if present. If the
15183 -- partial view has discriminants, the references have been
15184 -- generated already.
15185
15186 if not Has_Discriminants (Prev) then
15187 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
15188 end if;
15189 else
15190 Generate_Reference
15191 (Current_Scope, Defining_Identifier (Discr), 'd');
15192 end if;
15193
15194 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
15195 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
15196
15197 -- Ada 2005 (AI-254)
15198
15199 if Present (Access_To_Subprogram_Definition
15200 (Discriminant_Type (Discr)))
15201 and then Protected_Present (Access_To_Subprogram_Definition
15202 (Discriminant_Type (Discr)))
15203 then
15204 Discr_Type :=
15205 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
15206 end if;
15207
15208 else
15209 Find_Type (Discriminant_Type (Discr));
15210 Discr_Type := Etype (Discriminant_Type (Discr));
15211
15212 if Error_Posted (Discriminant_Type (Discr)) then
15213 Discr_Type := Any_Type;
15214 end if;
15215 end if;
15216
15217 if Is_Access_Type (Discr_Type) then
15218
15219 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
15220 -- record types
15221
15222 if Ada_Version < Ada_05 then
15223 Check_Access_Discriminant_Requires_Limited
15224 (Discr, Discriminant_Type (Discr));
15225 end if;
15226
15227 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
15228 Error_Msg_N
15229 ("(Ada 83) access discriminant not allowed", Discr);
15230 end if;
15231
15232 elsif not Is_Discrete_Type (Discr_Type) then
15233 Error_Msg_N ("discriminants must have a discrete or access type",
15234 Discriminant_Type (Discr));
15235 end if;
15236
15237 Set_Etype (Defining_Identifier (Discr), Discr_Type);
15238
15239 -- If a discriminant specification includes the assignment compound
15240 -- delimiter followed by an expression, the expression is the default
15241 -- expression of the discriminant; the default expression must be of
15242 -- the type of the discriminant. (RM 3.7.1) Since this expression is
15243 -- a default expression, we do the special preanalysis, since this
15244 -- expression does not freeze (see "Handling of Default and Per-
15245 -- Object Expressions" in spec of package Sem).
15246
15247 if Present (Expression (Discr)) then
15248 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
15249
15250 if Nkind (N) = N_Formal_Type_Declaration then
15251 Error_Msg_N
15252 ("discriminant defaults not allowed for formal type",
15253 Expression (Discr));
15254
15255 -- Tagged types cannot have defaulted discriminants, but a
15256 -- non-tagged private type with defaulted discriminants
15257 -- can have a tagged completion.
15258
15259 elsif Is_Tagged_Type (Current_Scope)
15260 and then Comes_From_Source (N)
15261 then
15262 Error_Msg_N
15263 ("discriminants of tagged type cannot have defaults",
15264 Expression (Discr));
15265
15266 else
15267 Default_Present := True;
15268 Append_Elmt (Expression (Discr), Elist);
15269
15270 -- Tag the defining identifiers for the discriminants with
15271 -- their corresponding default expressions from the tree.
15272
15273 Set_Discriminant_Default_Value
15274 (Defining_Identifier (Discr), Expression (Discr));
15275 end if;
15276
15277 else
15278 Default_Not_Present := True;
15279 end if;
15280
15281 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
15282 -- Discr_Type but with the null-exclusion attribute
15283
15284 if Ada_Version >= Ada_05 then
15285
15286 -- Ada 2005 (AI-231): Static checks
15287
15288 if Can_Never_Be_Null (Discr_Type) then
15289 Null_Exclusion_Static_Checks (Discr);
15290
15291 elsif Is_Access_Type (Discr_Type)
15292 and then Null_Exclusion_Present (Discr)
15293
15294 -- No need to check itypes because in their case this check
15295 -- was done at their point of creation
15296
15297 and then not Is_Itype (Discr_Type)
15298 then
15299 if Can_Never_Be_Null (Discr_Type) then
15300 Error_Msg_NE
15301 ("`NOT NULL` not allowed (& already excludes null)",
15302 Discr,
15303 Discr_Type);
15304 end if;
15305
15306 Set_Etype (Defining_Identifier (Discr),
15307 Create_Null_Excluding_Itype
15308 (T => Discr_Type,
15309 Related_Nod => Discr));
15310 end if;
15311
15312 -- Ada 2005 (AI-402): access discriminants of nonlimited types
15313 -- can't have defaults. Synchronized types, or types that are
15314 -- explicitly limited are fine, but special tests apply to derived
15315 -- types in generics: in a generic body we have to assume the
15316 -- worst, and therefore defaults are not allowed if the parent is
15317 -- a generic formal private type (see ACATS B370001).
15318
15319 if Is_Access_Type (Discr_Type) then
15320 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
15321 or else not Default_Present
15322 or else Is_Limited_Record (Current_Scope)
15323 or else Is_Concurrent_Type (Current_Scope)
15324 or else Is_Concurrent_Record_Type (Current_Scope)
15325 or else Ekind (Current_Scope) = E_Limited_Private_Type
15326 then
15327 if not Is_Derived_Type (Current_Scope)
15328 or else not Is_Generic_Type (Etype (Current_Scope))
15329 or else not In_Package_Body (Scope (Etype (Current_Scope)))
15330 or else Limited_Present
15331 (Type_Definition (Parent (Current_Scope)))
15332 then
15333 null;
15334
15335 else
15336 Error_Msg_N ("access discriminants of nonlimited types",
15337 Expression (Discr));
15338 Error_Msg_N ("\cannot have defaults", Expression (Discr));
15339 end if;
15340
15341 elsif Present (Expression (Discr)) then
15342 Error_Msg_N
15343 ("(Ada 2005) access discriminants of nonlimited types",
15344 Expression (Discr));
15345 Error_Msg_N ("\cannot have defaults", Expression (Discr));
15346 end if;
15347 end if;
15348 end if;
15349
15350 Next (Discr);
15351 end loop;
15352
15353 -- An element list consisting of the default expressions of the
15354 -- discriminants is constructed in the above loop and used to set
15355 -- the Discriminant_Constraint attribute for the type. If an object
15356 -- is declared of this (record or task) type without any explicit
15357 -- discriminant constraint given, this element list will form the
15358 -- actual parameters for the corresponding initialization procedure
15359 -- for the type.
15360
15361 Set_Discriminant_Constraint (Current_Scope, Elist);
15362 Set_Stored_Constraint (Current_Scope, No_Elist);
15363
15364 -- Default expressions must be provided either for all or for none
15365 -- of the discriminants of a discriminant part. (RM 3.7.1)
15366
15367 if Default_Present and then Default_Not_Present then
15368 Error_Msg_N
15369 ("incomplete specification of defaults for discriminants", N);
15370 end if;
15371
15372 -- The use of the name of a discriminant is not allowed in default
15373 -- expressions of a discriminant part if the specification of the
15374 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
15375
15376 -- To detect this, the discriminant names are entered initially with an
15377 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
15378 -- attempt to use a void entity (for example in an expression that is
15379 -- type-checked) produces the error message: premature usage. Now after
15380 -- completing the semantic analysis of the discriminant part, we can set
15381 -- the Ekind of all the discriminants appropriately.
15382
15383 Discr := First (Discriminant_Specifications (N));
15384 Discr_Number := Uint_1;
15385 while Present (Discr) loop
15386 Id := Defining_Identifier (Discr);
15387 Set_Ekind (Id, E_Discriminant);
15388 Init_Component_Location (Id);
15389 Init_Esize (Id);
15390 Set_Discriminant_Number (Id, Discr_Number);
15391
15392 -- Make sure this is always set, even in illegal programs
15393
15394 Set_Corresponding_Discriminant (Id, Empty);
15395
15396 -- Initialize the Original_Record_Component to the entity itself.
15397 -- Inherit_Components will propagate the right value to
15398 -- discriminants in derived record types.
15399
15400 Set_Original_Record_Component (Id, Id);
15401
15402 -- Create the discriminal for the discriminant
15403
15404 Build_Discriminal (Id);
15405
15406 Next (Discr);
15407 Discr_Number := Discr_Number + 1;
15408 end loop;
15409
15410 Set_Has_Discriminants (Current_Scope);
15411 end Process_Discriminants;
15412
15413 -----------------------
15414 -- Process_Full_View --
15415 -----------------------
15416
15417 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
15418 Priv_Parent : Entity_Id;
15419 Full_Parent : Entity_Id;
15420 Full_Indic : Node_Id;
15421
15422 procedure Collect_Implemented_Interfaces
15423 (Typ : Entity_Id;
15424 Ifaces : Elist_Id);
15425 -- Ada 2005: Gather all the interfaces that Typ directly or
15426 -- inherently implements. Duplicate entries are not added to
15427 -- the list Ifaces.
15428
15429 ------------------------------------
15430 -- Collect_Implemented_Interfaces --
15431 ------------------------------------
15432
15433 procedure Collect_Implemented_Interfaces
15434 (Typ : Entity_Id;
15435 Ifaces : Elist_Id)
15436 is
15437 Iface : Entity_Id;
15438 Iface_Elmt : Elmt_Id;
15439
15440 begin
15441 -- Abstract interfaces are only associated with tagged record types
15442
15443 if not Is_Tagged_Type (Typ)
15444 or else not Is_Record_Type (Typ)
15445 then
15446 return;
15447 end if;
15448
15449 -- Recursively climb to the ancestors
15450
15451 if Etype (Typ) /= Typ
15452
15453 -- Protect the frontend against wrong cyclic declarations like:
15454
15455 -- type B is new A with private;
15456 -- type C is new A with private;
15457 -- private
15458 -- type B is new C with null record;
15459 -- type C is new B with null record;
15460
15461 and then Etype (Typ) /= Priv_T
15462 and then Etype (Typ) /= Full_T
15463 then
15464 -- Keep separate the management of private type declarations
15465
15466 if Ekind (Typ) = E_Record_Type_With_Private then
15467
15468 -- Handle the following erronous case:
15469 -- type Private_Type is tagged private;
15470 -- private
15471 -- type Private_Type is new Type_Implementing_Iface;
15472
15473 if Present (Full_View (Typ))
15474 and then Etype (Typ) /= Full_View (Typ)
15475 then
15476 if Is_Interface (Etype (Typ)) then
15477 Append_Unique_Elmt (Etype (Typ), Ifaces);
15478 end if;
15479
15480 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15481 end if;
15482
15483 -- Non-private types
15484
15485 else
15486 if Is_Interface (Etype (Typ)) then
15487 Append_Unique_Elmt (Etype (Typ), Ifaces);
15488 end if;
15489
15490 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15491 end if;
15492 end if;
15493
15494 -- Handle entities in the list of abstract interfaces
15495
15496 if Present (Interfaces (Typ)) then
15497 Iface_Elmt := First_Elmt (Interfaces (Typ));
15498 while Present (Iface_Elmt) loop
15499 Iface := Node (Iface_Elmt);
15500
15501 pragma Assert (Is_Interface (Iface));
15502
15503 if not Contain_Interface (Iface, Ifaces) then
15504 Append_Elmt (Iface, Ifaces);
15505 Collect_Implemented_Interfaces (Iface, Ifaces);
15506 end if;
15507
15508 Next_Elmt (Iface_Elmt);
15509 end loop;
15510 end if;
15511 end Collect_Implemented_Interfaces;
15512
15513 -- Start of processing for Process_Full_View
15514
15515 begin
15516 -- First some sanity checks that must be done after semantic
15517 -- decoration of the full view and thus cannot be placed with other
15518 -- similar checks in Find_Type_Name
15519
15520 if not Is_Limited_Type (Priv_T)
15521 and then (Is_Limited_Type (Full_T)
15522 or else Is_Limited_Composite (Full_T))
15523 then
15524 Error_Msg_N
15525 ("completion of nonlimited type cannot be limited", Full_T);
15526 Explain_Limited_Type (Full_T, Full_T);
15527
15528 elsif Is_Abstract_Type (Full_T)
15529 and then not Is_Abstract_Type (Priv_T)
15530 then
15531 Error_Msg_N
15532 ("completion of nonabstract type cannot be abstract", Full_T);
15533
15534 elsif Is_Tagged_Type (Priv_T)
15535 and then Is_Limited_Type (Priv_T)
15536 and then not Is_Limited_Type (Full_T)
15537 then
15538 -- If pragma CPP_Class was applied to the private declaration
15539 -- propagate the limitedness to the full-view
15540
15541 if Is_CPP_Class (Priv_T) then
15542 Set_Is_Limited_Record (Full_T);
15543
15544 -- GNAT allow its own definition of Limited_Controlled to disobey
15545 -- this rule in order in ease the implementation. The next test is
15546 -- safe because Root_Controlled is defined in a private system child
15547
15548 elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
15549 Set_Is_Limited_Composite (Full_T);
15550 else
15551 Error_Msg_N
15552 ("completion of limited tagged type must be limited", Full_T);
15553 end if;
15554
15555 elsif Is_Generic_Type (Priv_T) then
15556 Error_Msg_N ("generic type cannot have a completion", Full_T);
15557 end if;
15558
15559 -- Check that ancestor interfaces of private and full views are
15560 -- consistent. We omit this check for synchronized types because
15561 -- they are performed on the corresponding record type when frozen.
15562
15563 if Ada_Version >= Ada_05
15564 and then Is_Tagged_Type (Priv_T)
15565 and then Is_Tagged_Type (Full_T)
15566 and then not Is_Concurrent_Type (Full_T)
15567 then
15568 declare
15569 Iface : Entity_Id;
15570 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
15571 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
15572
15573 begin
15574 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
15575 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
15576
15577 -- Ada 2005 (AI-251): The partial view shall be a descendant of
15578 -- an interface type if and only if the full type is descendant
15579 -- of the interface type (AARM 7.3 (7.3/2).
15580
15581 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
15582
15583 if Present (Iface) then
15584 Error_Msg_NE ("interface & not implemented by full type " &
15585 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
15586 end if;
15587
15588 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
15589
15590 if Present (Iface) then
15591 Error_Msg_NE ("interface & not implemented by partial view " &
15592 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
15593 end if;
15594 end;
15595 end if;
15596
15597 if Is_Tagged_Type (Priv_T)
15598 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15599 and then Is_Derived_Type (Full_T)
15600 then
15601 Priv_Parent := Etype (Priv_T);
15602
15603 -- The full view of a private extension may have been transformed
15604 -- into an unconstrained derived type declaration and a subtype
15605 -- declaration (see build_derived_record_type for details).
15606
15607 if Nkind (N) = N_Subtype_Declaration then
15608 Full_Indic := Subtype_Indication (N);
15609 Full_Parent := Etype (Base_Type (Full_T));
15610 else
15611 Full_Indic := Subtype_Indication (Type_Definition (N));
15612 Full_Parent := Etype (Full_T);
15613 end if;
15614
15615 -- Check that the parent type of the full type is a descendant of
15616 -- the ancestor subtype given in the private extension. If either
15617 -- entity has an Etype equal to Any_Type then we had some previous
15618 -- error situation [7.3(8)].
15619
15620 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
15621 return;
15622
15623 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
15624 -- any order. Therefore we don't have to check that its parent must
15625 -- be a descendant of the parent of the private type declaration.
15626
15627 elsif Is_Interface (Priv_Parent)
15628 and then Is_Interface (Full_Parent)
15629 then
15630 null;
15631
15632 -- Ada 2005 (AI-251): If the parent of the private type declaration
15633 -- is an interface there is no need to check that it is an ancestor
15634 -- of the associated full type declaration. The required tests for
15635 -- this case case are performed by Build_Derived_Record_Type.
15636
15637 elsif not Is_Interface (Base_Type (Priv_Parent))
15638 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
15639 then
15640 Error_Msg_N
15641 ("parent of full type must descend from parent"
15642 & " of private extension", Full_Indic);
15643
15644 -- Check the rules of 7.3(10): if the private extension inherits
15645 -- known discriminants, then the full type must also inherit those
15646 -- discriminants from the same (ancestor) type, and the parent
15647 -- subtype of the full type must be constrained if and only if
15648 -- the ancestor subtype of the private extension is constrained.
15649
15650 elsif No (Discriminant_Specifications (Parent (Priv_T)))
15651 and then not Has_Unknown_Discriminants (Priv_T)
15652 and then Has_Discriminants (Base_Type (Priv_Parent))
15653 then
15654 declare
15655 Priv_Indic : constant Node_Id :=
15656 Subtype_Indication (Parent (Priv_T));
15657
15658 Priv_Constr : constant Boolean :=
15659 Is_Constrained (Priv_Parent)
15660 or else
15661 Nkind (Priv_Indic) = N_Subtype_Indication
15662 or else Is_Constrained (Entity (Priv_Indic));
15663
15664 Full_Constr : constant Boolean :=
15665 Is_Constrained (Full_Parent)
15666 or else
15667 Nkind (Full_Indic) = N_Subtype_Indication
15668 or else Is_Constrained (Entity (Full_Indic));
15669
15670 Priv_Discr : Entity_Id;
15671 Full_Discr : Entity_Id;
15672
15673 begin
15674 Priv_Discr := First_Discriminant (Priv_Parent);
15675 Full_Discr := First_Discriminant (Full_Parent);
15676 while Present (Priv_Discr) and then Present (Full_Discr) loop
15677 if Original_Record_Component (Priv_Discr) =
15678 Original_Record_Component (Full_Discr)
15679 or else
15680 Corresponding_Discriminant (Priv_Discr) =
15681 Corresponding_Discriminant (Full_Discr)
15682 then
15683 null;
15684 else
15685 exit;
15686 end if;
15687
15688 Next_Discriminant (Priv_Discr);
15689 Next_Discriminant (Full_Discr);
15690 end loop;
15691
15692 if Present (Priv_Discr) or else Present (Full_Discr) then
15693 Error_Msg_N
15694 ("full view must inherit discriminants of the parent type"
15695 & " used in the private extension", Full_Indic);
15696
15697 elsif Priv_Constr and then not Full_Constr then
15698 Error_Msg_N
15699 ("parent subtype of full type must be constrained",
15700 Full_Indic);
15701
15702 elsif Full_Constr and then not Priv_Constr then
15703 Error_Msg_N
15704 ("parent subtype of full type must be unconstrained",
15705 Full_Indic);
15706 end if;
15707 end;
15708
15709 -- Check the rules of 7.3(12): if a partial view has neither known
15710 -- or unknown discriminants, then the full type declaration shall
15711 -- define a definite subtype.
15712
15713 elsif not Has_Unknown_Discriminants (Priv_T)
15714 and then not Has_Discriminants (Priv_T)
15715 and then not Is_Constrained (Full_T)
15716 then
15717 Error_Msg_N
15718 ("full view must define a constrained type if partial view"
15719 & " has no discriminants", Full_T);
15720 end if;
15721
15722 -- ??????? Do we implement the following properly ?????
15723 -- If the ancestor subtype of a private extension has constrained
15724 -- discriminants, then the parent subtype of the full view shall
15725 -- impose a statically matching constraint on those discriminants
15726 -- [7.3(13)].
15727
15728 else
15729 -- For untagged types, verify that a type without discriminants
15730 -- is not completed with an unconstrained type.
15731
15732 if not Is_Indefinite_Subtype (Priv_T)
15733 and then Is_Indefinite_Subtype (Full_T)
15734 then
15735 Error_Msg_N ("full view of type must be definite subtype", Full_T);
15736 end if;
15737 end if;
15738
15739 -- AI-419: verify that the use of "limited" is consistent
15740
15741 declare
15742 Orig_Decl : constant Node_Id := Original_Node (N);
15743
15744 begin
15745 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15746 and then not Limited_Present (Parent (Priv_T))
15747 and then not Synchronized_Present (Parent (Priv_T))
15748 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
15749 and then Nkind
15750 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
15751 and then Limited_Present (Type_Definition (Orig_Decl))
15752 then
15753 Error_Msg_N
15754 ("full view of non-limited extension cannot be limited", N);
15755 end if;
15756 end;
15757
15758 -- Ada 2005 (AI-443): A synchronized private extension must be
15759 -- completed by a task or protected type.
15760
15761 if Ada_Version >= Ada_05
15762 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15763 and then Synchronized_Present (Parent (Priv_T))
15764 and then not Is_Concurrent_Type (Full_T)
15765 then
15766 Error_Msg_N ("full view of synchronized extension must " &
15767 "be synchronized type", N);
15768 end if;
15769
15770 -- Ada 2005 AI-363: if the full view has discriminants with
15771 -- defaults, it is illegal to declare constrained access subtypes
15772 -- whose designated type is the current type. This allows objects
15773 -- of the type that are declared in the heap to be unconstrained.
15774
15775 if not Has_Unknown_Discriminants (Priv_T)
15776 and then not Has_Discriminants (Priv_T)
15777 and then Has_Discriminants (Full_T)
15778 and then
15779 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
15780 then
15781 Set_Has_Constrained_Partial_View (Full_T);
15782 Set_Has_Constrained_Partial_View (Priv_T);
15783 end if;
15784
15785 -- Create a full declaration for all its subtypes recorded in
15786 -- Private_Dependents and swap them similarly to the base type. These
15787 -- are subtypes that have been define before the full declaration of
15788 -- the private type. We also swap the entry in Private_Dependents list
15789 -- so we can properly restore the private view on exit from the scope.
15790
15791 declare
15792 Priv_Elmt : Elmt_Id;
15793 Priv : Entity_Id;
15794 Full : Entity_Id;
15795
15796 begin
15797 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
15798 while Present (Priv_Elmt) loop
15799 Priv := Node (Priv_Elmt);
15800
15801 if Ekind (Priv) = E_Private_Subtype
15802 or else Ekind (Priv) = E_Limited_Private_Subtype
15803 or else Ekind (Priv) = E_Record_Subtype_With_Private
15804 then
15805 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
15806 Set_Is_Itype (Full);
15807 Set_Parent (Full, Parent (Priv));
15808 Set_Associated_Node_For_Itype (Full, N);
15809
15810 -- Now we need to complete the private subtype, but since the
15811 -- base type has already been swapped, we must also swap the
15812 -- subtypes (and thus, reverse the arguments in the call to
15813 -- Complete_Private_Subtype).
15814
15815 Copy_And_Swap (Priv, Full);
15816 Complete_Private_Subtype (Full, Priv, Full_T, N);
15817 Replace_Elmt (Priv_Elmt, Full);
15818 end if;
15819
15820 Next_Elmt (Priv_Elmt);
15821 end loop;
15822 end;
15823
15824 -- If the private view was tagged, copy the new primitive operations
15825 -- from the private view to the full view.
15826
15827 if Is_Tagged_Type (Full_T) then
15828 declare
15829 Disp_Typ : Entity_Id;
15830 Full_List : Elist_Id;
15831 Prim : Entity_Id;
15832 Prim_Elmt : Elmt_Id;
15833 Priv_List : Elist_Id;
15834
15835 function Contains
15836 (E : Entity_Id;
15837 L : Elist_Id) return Boolean;
15838 -- Determine whether list L contains element E
15839
15840 --------------
15841 -- Contains --
15842 --------------
15843
15844 function Contains
15845 (E : Entity_Id;
15846 L : Elist_Id) return Boolean
15847 is
15848 List_Elmt : Elmt_Id;
15849
15850 begin
15851 List_Elmt := First_Elmt (L);
15852 while Present (List_Elmt) loop
15853 if Node (List_Elmt) = E then
15854 return True;
15855 end if;
15856
15857 Next_Elmt (List_Elmt);
15858 end loop;
15859
15860 return False;
15861 end Contains;
15862
15863 -- Start of processing
15864
15865 begin
15866 if Is_Tagged_Type (Priv_T) then
15867 Priv_List := Primitive_Operations (Priv_T);
15868 Prim_Elmt := First_Elmt (Priv_List);
15869
15870 -- In the case of a concurrent type completing a private tagged
15871 -- type, primivies may have been declared in between the two
15872 -- views. These subprograms need to be wrapped the same way
15873 -- entries and protected procedures are handled because they
15874 -- cannot be directly shared by the two views.
15875
15876 if Is_Concurrent_Type (Full_T) then
15877 declare
15878 Conc_Typ : constant Entity_Id :=
15879 Corresponding_Record_Type (Full_T);
15880 Loc : constant Source_Ptr := Sloc (Conc_Typ);
15881 Curr_Nod : Node_Id := Parent (Conc_Typ);
15882 Wrap_Spec : Node_Id;
15883
15884 begin
15885 while Present (Prim_Elmt) loop
15886 Prim := Node (Prim_Elmt);
15887
15888 if Comes_From_Source (Prim)
15889 and then not Is_Abstract_Subprogram (Prim)
15890 then
15891 Wrap_Spec :=
15892 Make_Subprogram_Declaration (Loc,
15893 Specification =>
15894 Build_Wrapper_Spec (Loc,
15895 Subp_Id => Prim,
15896 Obj_Typ => Conc_Typ,
15897 Formals =>
15898 Parameter_Specifications (
15899 Parent (Prim))));
15900
15901 Insert_After (Curr_Nod, Wrap_Spec);
15902 Curr_Nod := Wrap_Spec;
15903
15904 Analyze (Wrap_Spec);
15905 end if;
15906
15907 Next_Elmt (Prim_Elmt);
15908 end loop;
15909
15910 return;
15911 end;
15912
15913 -- For non-concurrent types, transfer explicit primitives, but
15914 -- omit those inherited from the parent of the private view
15915 -- since they will be re-inherited later on.
15916
15917 else
15918 Full_List := Primitive_Operations (Full_T);
15919
15920 while Present (Prim_Elmt) loop
15921 Prim := Node (Prim_Elmt);
15922
15923 if Comes_From_Source (Prim)
15924 and then not Contains (Prim, Full_List)
15925 then
15926 Append_Elmt (Prim, Full_List);
15927 end if;
15928
15929 Next_Elmt (Prim_Elmt);
15930 end loop;
15931 end if;
15932
15933 -- Untagged private view
15934
15935 else
15936 Full_List := Primitive_Operations (Full_T);
15937
15938 -- In this case the partial view is untagged, so here we locate
15939 -- all of the earlier primitives that need to be treated as
15940 -- dispatching (those that appear between the two views). Note
15941 -- that these additional operations must all be new operations
15942 -- (any earlier operations that override inherited operations
15943 -- of the full view will already have been inserted in the
15944 -- primitives list, marked by Check_Operation_From_Private_View
15945 -- as dispatching. Note that implicit "/=" operators are
15946 -- excluded from being added to the primitives list since they
15947 -- shouldn't be treated as dispatching (tagged "/=" is handled
15948 -- specially).
15949
15950 Prim := Next_Entity (Full_T);
15951 while Present (Prim) and then Prim /= Priv_T loop
15952 if Ekind (Prim) = E_Procedure
15953 or else
15954 Ekind (Prim) = E_Function
15955 then
15956 Disp_Typ := Find_Dispatching_Type (Prim);
15957
15958 if Disp_Typ = Full_T
15959 and then (Chars (Prim) /= Name_Op_Ne
15960 or else Comes_From_Source (Prim))
15961 then
15962 Check_Controlling_Formals (Full_T, Prim);
15963
15964 if not Is_Dispatching_Operation (Prim) then
15965 Append_Elmt (Prim, Full_List);
15966 Set_Is_Dispatching_Operation (Prim, True);
15967 Set_DT_Position (Prim, No_Uint);
15968 end if;
15969
15970 elsif Is_Dispatching_Operation (Prim)
15971 and then Disp_Typ /= Full_T
15972 then
15973
15974 -- Verify that it is not otherwise controlled by a
15975 -- formal or a return value of type T.
15976
15977 Check_Controlling_Formals (Disp_Typ, Prim);
15978 end if;
15979 end if;
15980
15981 Next_Entity (Prim);
15982 end loop;
15983 end if;
15984
15985 -- For the tagged case, the two views can share the same
15986 -- Primitive Operation list and the same class wide type.
15987 -- Update attributes of the class-wide type which depend on
15988 -- the full declaration.
15989
15990 if Is_Tagged_Type (Priv_T) then
15991 Set_Primitive_Operations (Priv_T, Full_List);
15992 Set_Class_Wide_Type
15993 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
15994
15995 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
15996 end if;
15997 end;
15998 end if;
15999
16000 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
16001
16002 if Known_To_Have_Preelab_Init (Priv_T) then
16003
16004 -- Case where there is a pragma Preelaborable_Initialization. We
16005 -- always allow this in predefined units, which is a bit of a kludge,
16006 -- but it means we don't have to struggle to meet the requirements in
16007 -- the RM for having Preelaborable Initialization. Otherwise we
16008 -- require that the type meets the RM rules. But we can't check that
16009 -- yet, because of the rule about overriding Ininitialize, so we
16010 -- simply set a flag that will be checked at freeze time.
16011
16012 if not In_Predefined_Unit (Full_T) then
16013 Set_Must_Have_Preelab_Init (Full_T);
16014 end if;
16015 end if;
16016
16017 -- If pragma CPP_Class was applied to the private type declaration,
16018 -- propagate it now to the full type declaration.
16019
16020 if Is_CPP_Class (Priv_T) then
16021 Set_Is_CPP_Class (Full_T);
16022 Set_Convention (Full_T, Convention_CPP);
16023 end if;
16024 end Process_Full_View;
16025
16026 -----------------------------------
16027 -- Process_Incomplete_Dependents --
16028 -----------------------------------
16029
16030 procedure Process_Incomplete_Dependents
16031 (N : Node_Id;
16032 Full_T : Entity_Id;
16033 Inc_T : Entity_Id)
16034 is
16035 Inc_Elmt : Elmt_Id;
16036 Priv_Dep : Entity_Id;
16037 New_Subt : Entity_Id;
16038
16039 Disc_Constraint : Elist_Id;
16040
16041 begin
16042 if No (Private_Dependents (Inc_T)) then
16043 return;
16044 end if;
16045
16046 -- Itypes that may be generated by the completion of an incomplete
16047 -- subtype are not used by the back-end and not attached to the tree.
16048 -- They are created only for constraint-checking purposes.
16049
16050 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
16051 while Present (Inc_Elmt) loop
16052 Priv_Dep := Node (Inc_Elmt);
16053
16054 if Ekind (Priv_Dep) = E_Subprogram_Type then
16055
16056 -- An Access_To_Subprogram type may have a return type or a
16057 -- parameter type that is incomplete. Replace with the full view.
16058
16059 if Etype (Priv_Dep) = Inc_T then
16060 Set_Etype (Priv_Dep, Full_T);
16061 end if;
16062
16063 declare
16064 Formal : Entity_Id;
16065
16066 begin
16067 Formal := First_Formal (Priv_Dep);
16068 while Present (Formal) loop
16069 if Etype (Formal) = Inc_T then
16070 Set_Etype (Formal, Full_T);
16071 end if;
16072
16073 Next_Formal (Formal);
16074 end loop;
16075 end;
16076
16077 elsif Is_Overloadable (Priv_Dep) then
16078
16079 -- A protected operation is never dispatching: only its
16080 -- wrapper operation (which has convention Ada) is.
16081
16082 if Is_Tagged_Type (Full_T)
16083 and then Convention (Priv_Dep) /= Convention_Protected
16084 then
16085
16086 -- Subprogram has an access parameter whose designated type
16087 -- was incomplete. Reexamine declaration now, because it may
16088 -- be a primitive operation of the full type.
16089
16090 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
16091 Set_Is_Dispatching_Operation (Priv_Dep);
16092 Check_Controlling_Formals (Full_T, Priv_Dep);
16093 end if;
16094
16095 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
16096
16097 -- Can happen during processing of a body before the completion
16098 -- of a TA type. Ignore, because spec is also on dependent list.
16099
16100 return;
16101
16102 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
16103 -- corresponding subtype of the full view.
16104
16105 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
16106 Set_Subtype_Indication
16107 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
16108 Set_Etype (Priv_Dep, Full_T);
16109 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
16110 Set_Analyzed (Parent (Priv_Dep), False);
16111
16112 -- Reanalyze the declaration, suppressing the call to
16113 -- Enter_Name to avoid duplicate names.
16114
16115 Analyze_Subtype_Declaration
16116 (N => Parent (Priv_Dep),
16117 Skip => True);
16118
16119 -- Dependent is a subtype
16120
16121 else
16122 -- We build a new subtype indication using the full view of the
16123 -- incomplete parent. The discriminant constraints have been
16124 -- elaborated already at the point of the subtype declaration.
16125
16126 New_Subt := Create_Itype (E_Void, N);
16127
16128 if Has_Discriminants (Full_T) then
16129 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
16130 else
16131 Disc_Constraint := No_Elist;
16132 end if;
16133
16134 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
16135 Set_Full_View (Priv_Dep, New_Subt);
16136 end if;
16137
16138 Next_Elmt (Inc_Elmt);
16139 end loop;
16140 end Process_Incomplete_Dependents;
16141
16142 --------------------------------
16143 -- Process_Range_Expr_In_Decl --
16144 --------------------------------
16145
16146 procedure Process_Range_Expr_In_Decl
16147 (R : Node_Id;
16148 T : Entity_Id;
16149 Check_List : List_Id := Empty_List;
16150 R_Check_Off : Boolean := False)
16151 is
16152 Lo, Hi : Node_Id;
16153 R_Checks : Check_Result;
16154 Type_Decl : Node_Id;
16155 Def_Id : Entity_Id;
16156
16157 begin
16158 Analyze_And_Resolve (R, Base_Type (T));
16159
16160 if Nkind (R) = N_Range then
16161 Lo := Low_Bound (R);
16162 Hi := High_Bound (R);
16163
16164 -- We need to ensure validity of the bounds here, because if we
16165 -- go ahead and do the expansion, then the expanded code will get
16166 -- analyzed with range checks suppressed and we miss the check.
16167
16168 Validity_Check_Range (R);
16169
16170 -- If there were errors in the declaration, try and patch up some
16171 -- common mistakes in the bounds. The cases handled are literals
16172 -- which are Integer where the expected type is Real and vice versa.
16173 -- These corrections allow the compilation process to proceed further
16174 -- along since some basic assumptions of the format of the bounds
16175 -- are guaranteed.
16176
16177 if Etype (R) = Any_Type then
16178
16179 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
16180 Rewrite (Lo,
16181 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
16182
16183 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
16184 Rewrite (Hi,
16185 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
16186
16187 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
16188 Rewrite (Lo,
16189 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
16190
16191 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
16192 Rewrite (Hi,
16193 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
16194 end if;
16195
16196 Set_Etype (Lo, T);
16197 Set_Etype (Hi, T);
16198 end if;
16199
16200 -- If the bounds of the range have been mistakenly given as string
16201 -- literals (perhaps in place of character literals), then an error
16202 -- has already been reported, but we rewrite the string literal as a
16203 -- bound of the range's type to avoid blowups in later processing
16204 -- that looks at static values.
16205
16206 if Nkind (Lo) = N_String_Literal then
16207 Rewrite (Lo,
16208 Make_Attribute_Reference (Sloc (Lo),
16209 Attribute_Name => Name_First,
16210 Prefix => New_Reference_To (T, Sloc (Lo))));
16211 Analyze_And_Resolve (Lo);
16212 end if;
16213
16214 if Nkind (Hi) = N_String_Literal then
16215 Rewrite (Hi,
16216 Make_Attribute_Reference (Sloc (Hi),
16217 Attribute_Name => Name_First,
16218 Prefix => New_Reference_To (T, Sloc (Hi))));
16219 Analyze_And_Resolve (Hi);
16220 end if;
16221
16222 -- If bounds aren't scalar at this point then exit, avoiding
16223 -- problems with further processing of the range in this procedure.
16224
16225 if not Is_Scalar_Type (Etype (Lo)) then
16226 return;
16227 end if;
16228
16229 -- Resolve (actually Sem_Eval) has checked that the bounds are in
16230 -- then range of the base type. Here we check whether the bounds
16231 -- are in the range of the subtype itself. Note that if the bounds
16232 -- represent the null range the Constraint_Error exception should
16233 -- not be raised.
16234
16235 -- ??? The following code should be cleaned up as follows
16236
16237 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
16238 -- is done in the call to Range_Check (R, T); below
16239
16240 -- 2. The use of R_Check_Off should be investigated and possibly
16241 -- removed, this would clean up things a bit.
16242
16243 if Is_Null_Range (Lo, Hi) then
16244 null;
16245
16246 else
16247 -- Capture values of bounds and generate temporaries for them
16248 -- if needed, before applying checks, since checks may cause
16249 -- duplication of the expression without forcing evaluation.
16250
16251 if Expander_Active then
16252 Force_Evaluation (Lo);
16253 Force_Evaluation (Hi);
16254 end if;
16255
16256 -- We use a flag here instead of suppressing checks on the
16257 -- type because the type we check against isn't necessarily
16258 -- the place where we put the check.
16259
16260 if not R_Check_Off then
16261 R_Checks := Get_Range_Checks (R, T);
16262
16263 -- Look up tree to find an appropriate insertion point.
16264 -- This seems really junk code, and very brittle, couldn't
16265 -- we just use an insert actions call of some kind ???
16266
16267 Type_Decl := Parent (R);
16268 while Present (Type_Decl) and then not
16269 (Nkind_In (Type_Decl, N_Full_Type_Declaration,
16270 N_Subtype_Declaration,
16271 N_Loop_Statement,
16272 N_Task_Type_Declaration)
16273 or else
16274 Nkind_In (Type_Decl, N_Single_Task_Declaration,
16275 N_Protected_Type_Declaration,
16276 N_Single_Protected_Declaration))
16277 loop
16278 Type_Decl := Parent (Type_Decl);
16279 end loop;
16280
16281 -- Why would Type_Decl not be present??? Without this test,
16282 -- short regression tests fail.
16283
16284 if Present (Type_Decl) then
16285
16286 -- Case of loop statement (more comments ???)
16287
16288 if Nkind (Type_Decl) = N_Loop_Statement then
16289 declare
16290 Indic : Node_Id;
16291
16292 begin
16293 Indic := Parent (R);
16294 while Present (Indic)
16295 and then Nkind (Indic) /= N_Subtype_Indication
16296 loop
16297 Indic := Parent (Indic);
16298 end loop;
16299
16300 if Present (Indic) then
16301 Def_Id := Etype (Subtype_Mark (Indic));
16302
16303 Insert_Range_Checks
16304 (R_Checks,
16305 Type_Decl,
16306 Def_Id,
16307 Sloc (Type_Decl),
16308 R,
16309 Do_Before => True);
16310 end if;
16311 end;
16312
16313 -- All other cases (more comments ???)
16314
16315 else
16316 Def_Id := Defining_Identifier (Type_Decl);
16317
16318 if (Ekind (Def_Id) = E_Record_Type
16319 and then Depends_On_Discriminant (R))
16320 or else
16321 (Ekind (Def_Id) = E_Protected_Type
16322 and then Has_Discriminants (Def_Id))
16323 then
16324 Append_Range_Checks
16325 (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
16326
16327 else
16328 Insert_Range_Checks
16329 (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
16330
16331 end if;
16332 end if;
16333 end if;
16334 end if;
16335 end if;
16336
16337 elsif Expander_Active then
16338 Get_Index_Bounds (R, Lo, Hi);
16339 Force_Evaluation (Lo);
16340 Force_Evaluation (Hi);
16341 end if;
16342 end Process_Range_Expr_In_Decl;
16343
16344 --------------------------------------
16345 -- Process_Real_Range_Specification --
16346 --------------------------------------
16347
16348 procedure Process_Real_Range_Specification (Def : Node_Id) is
16349 Spec : constant Node_Id := Real_Range_Specification (Def);
16350 Lo : Node_Id;
16351 Hi : Node_Id;
16352 Err : Boolean := False;
16353
16354 procedure Analyze_Bound (N : Node_Id);
16355 -- Analyze and check one bound
16356
16357 -------------------
16358 -- Analyze_Bound --
16359 -------------------
16360
16361 procedure Analyze_Bound (N : Node_Id) is
16362 begin
16363 Analyze_And_Resolve (N, Any_Real);
16364
16365 if not Is_OK_Static_Expression (N) then
16366 Flag_Non_Static_Expr
16367 ("bound in real type definition is not static!", N);
16368 Err := True;
16369 end if;
16370 end Analyze_Bound;
16371
16372 -- Start of processing for Process_Real_Range_Specification
16373
16374 begin
16375 if Present (Spec) then
16376 Lo := Low_Bound (Spec);
16377 Hi := High_Bound (Spec);
16378 Analyze_Bound (Lo);
16379 Analyze_Bound (Hi);
16380
16381 -- If error, clear away junk range specification
16382
16383 if Err then
16384 Set_Real_Range_Specification (Def, Empty);
16385 end if;
16386 end if;
16387 end Process_Real_Range_Specification;
16388
16389 ---------------------
16390 -- Process_Subtype --
16391 ---------------------
16392
16393 function Process_Subtype
16394 (S : Node_Id;
16395 Related_Nod : Node_Id;
16396 Related_Id : Entity_Id := Empty;
16397 Suffix : Character := ' ') return Entity_Id
16398 is
16399 P : Node_Id;
16400 Def_Id : Entity_Id;
16401 Error_Node : Node_Id;
16402 Full_View_Id : Entity_Id;
16403 Subtype_Mark_Id : Entity_Id;
16404
16405 May_Have_Null_Exclusion : Boolean;
16406
16407 procedure Check_Incomplete (T : Entity_Id);
16408 -- Called to verify that an incomplete type is not used prematurely
16409
16410 ----------------------
16411 -- Check_Incomplete --
16412 ----------------------
16413
16414 procedure Check_Incomplete (T : Entity_Id) is
16415 begin
16416 -- Ada 2005 (AI-412): Incomplete subtypes are legal
16417
16418 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
16419 and then
16420 not (Ada_Version >= Ada_05
16421 and then
16422 (Nkind (Parent (T)) = N_Subtype_Declaration
16423 or else
16424 (Nkind (Parent (T)) = N_Subtype_Indication
16425 and then Nkind (Parent (Parent (T))) =
16426 N_Subtype_Declaration)))
16427 then
16428 Error_Msg_N ("invalid use of type before its full declaration", T);
16429 end if;
16430 end Check_Incomplete;
16431
16432 -- Start of processing for Process_Subtype
16433
16434 begin
16435 -- Case of no constraints present
16436
16437 if Nkind (S) /= N_Subtype_Indication then
16438 Find_Type (S);
16439 Check_Incomplete (S);
16440 P := Parent (S);
16441
16442 -- Ada 2005 (AI-231): Static check
16443
16444 if Ada_Version >= Ada_05
16445 and then Present (P)
16446 and then Null_Exclusion_Present (P)
16447 and then Nkind (P) /= N_Access_To_Object_Definition
16448 and then not Is_Access_Type (Entity (S))
16449 then
16450 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
16451 end if;
16452
16453 -- The following is ugly, can't we have a range or even a flag???
16454
16455 May_Have_Null_Exclusion :=
16456 Nkind_In (P, N_Access_Definition,
16457 N_Access_Function_Definition,
16458 N_Access_Procedure_Definition,
16459 N_Access_To_Object_Definition,
16460 N_Allocator,
16461 N_Component_Definition)
16462 or else
16463 Nkind_In (P, N_Derived_Type_Definition,
16464 N_Discriminant_Specification,
16465 N_Object_Declaration,
16466 N_Parameter_Specification,
16467 N_Subtype_Declaration);
16468
16469 -- Create an Itype that is a duplicate of Entity (S) but with the
16470 -- null-exclusion attribute
16471
16472 if May_Have_Null_Exclusion
16473 and then Is_Access_Type (Entity (S))
16474 and then Null_Exclusion_Present (P)
16475
16476 -- No need to check the case of an access to object definition.
16477 -- It is correct to define double not-null pointers.
16478
16479 -- Example:
16480 -- type Not_Null_Int_Ptr is not null access Integer;
16481 -- type Acc is not null access Not_Null_Int_Ptr;
16482
16483 and then Nkind (P) /= N_Access_To_Object_Definition
16484 then
16485 if Can_Never_Be_Null (Entity (S)) then
16486 case Nkind (Related_Nod) is
16487 when N_Full_Type_Declaration =>
16488 if Nkind (Type_Definition (Related_Nod))
16489 in N_Array_Type_Definition
16490 then
16491 Error_Node :=
16492 Subtype_Indication
16493 (Component_Definition
16494 (Type_Definition (Related_Nod)));
16495 else
16496 Error_Node :=
16497 Subtype_Indication (Type_Definition (Related_Nod));
16498 end if;
16499
16500 when N_Subtype_Declaration =>
16501 Error_Node := Subtype_Indication (Related_Nod);
16502
16503 when N_Object_Declaration =>
16504 Error_Node := Object_Definition (Related_Nod);
16505
16506 when N_Component_Declaration =>
16507 Error_Node :=
16508 Subtype_Indication (Component_Definition (Related_Nod));
16509
16510 when others =>
16511 pragma Assert (False);
16512 Error_Node := Related_Nod;
16513 end case;
16514
16515 Error_Msg_NE
16516 ("`NOT NULL` not allowed (& already excludes null)",
16517 Error_Node,
16518 Entity (S));
16519 end if;
16520
16521 Set_Etype (S,
16522 Create_Null_Excluding_Itype
16523 (T => Entity (S),
16524 Related_Nod => P));
16525 Set_Entity (S, Etype (S));
16526 end if;
16527
16528 return Entity (S);
16529
16530 -- Case of constraint present, so that we have an N_Subtype_Indication
16531 -- node (this node is created only if constraints are present).
16532
16533 else
16534 Find_Type (Subtype_Mark (S));
16535
16536 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
16537 and then not
16538 (Nkind (Parent (S)) = N_Subtype_Declaration
16539 and then Is_Itype (Defining_Identifier (Parent (S))))
16540 then
16541 Check_Incomplete (Subtype_Mark (S));
16542 end if;
16543
16544 P := Parent (S);
16545 Subtype_Mark_Id := Entity (Subtype_Mark (S));
16546
16547 -- Explicit subtype declaration case
16548
16549 if Nkind (P) = N_Subtype_Declaration then
16550 Def_Id := Defining_Identifier (P);
16551
16552 -- Explicit derived type definition case
16553
16554 elsif Nkind (P) = N_Derived_Type_Definition then
16555 Def_Id := Defining_Identifier (Parent (P));
16556
16557 -- Implicit case, the Def_Id must be created as an implicit type.
16558 -- The one exception arises in the case of concurrent types, array
16559 -- and access types, where other subsidiary implicit types may be
16560 -- created and must appear before the main implicit type. In these
16561 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
16562 -- has not yet been called to create Def_Id.
16563
16564 else
16565 if Is_Array_Type (Subtype_Mark_Id)
16566 or else Is_Concurrent_Type (Subtype_Mark_Id)
16567 or else Is_Access_Type (Subtype_Mark_Id)
16568 then
16569 Def_Id := Empty;
16570
16571 -- For the other cases, we create a new unattached Itype,
16572 -- and set the indication to ensure it gets attached later.
16573
16574 else
16575 Def_Id :=
16576 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
16577 end if;
16578 end if;
16579
16580 -- If the kind of constraint is invalid for this kind of type,
16581 -- then give an error, and then pretend no constraint was given.
16582
16583 if not Is_Valid_Constraint_Kind
16584 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
16585 then
16586 Error_Msg_N
16587 ("incorrect constraint for this kind of type", Constraint (S));
16588
16589 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16590
16591 -- Set Ekind of orphan itype, to prevent cascaded errors
16592
16593 if Present (Def_Id) then
16594 Set_Ekind (Def_Id, Ekind (Any_Type));
16595 end if;
16596
16597 -- Make recursive call, having got rid of the bogus constraint
16598
16599 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
16600 end if;
16601
16602 -- Remaining processing depends on type
16603
16604 case Ekind (Subtype_Mark_Id) is
16605 when Access_Kind =>
16606 Constrain_Access (Def_Id, S, Related_Nod);
16607
16608 if Expander_Active
16609 and then Is_Itype (Designated_Type (Def_Id))
16610 and then Nkind (Related_Nod) = N_Subtype_Declaration
16611 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
16612 then
16613 Build_Itype_Reference
16614 (Designated_Type (Def_Id), Related_Nod);
16615 end if;
16616
16617 when Array_Kind =>
16618 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
16619
16620 when Decimal_Fixed_Point_Kind =>
16621 Constrain_Decimal (Def_Id, S);
16622
16623 when Enumeration_Kind =>
16624 Constrain_Enumeration (Def_Id, S);
16625
16626 when Ordinary_Fixed_Point_Kind =>
16627 Constrain_Ordinary_Fixed (Def_Id, S);
16628
16629 when Float_Kind =>
16630 Constrain_Float (Def_Id, S);
16631
16632 when Integer_Kind =>
16633 Constrain_Integer (Def_Id, S);
16634
16635 when E_Record_Type |
16636 E_Record_Subtype |
16637 Class_Wide_Kind |
16638 E_Incomplete_Type =>
16639 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
16640
16641 when Private_Kind =>
16642 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
16643 Set_Private_Dependents (Def_Id, New_Elmt_List);
16644
16645 -- In case of an invalid constraint prevent further processing
16646 -- since the type constructed is missing expected fields.
16647
16648 if Etype (Def_Id) = Any_Type then
16649 return Def_Id;
16650 end if;
16651
16652 -- If the full view is that of a task with discriminants,
16653 -- we must constrain both the concurrent type and its
16654 -- corresponding record type. Otherwise we will just propagate
16655 -- the constraint to the full view, if available.
16656
16657 if Present (Full_View (Subtype_Mark_Id))
16658 and then Has_Discriminants (Subtype_Mark_Id)
16659 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
16660 then
16661 Full_View_Id :=
16662 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
16663
16664 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
16665 Constrain_Concurrent (Full_View_Id, S,
16666 Related_Nod, Related_Id, Suffix);
16667 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
16668 Set_Full_View (Def_Id, Full_View_Id);
16669
16670 -- Introduce an explicit reference to the private subtype,
16671 -- to prevent scope anomalies in gigi if first use appears
16672 -- in a nested context, e.g. a later function body.
16673 -- Should this be generated in other contexts than a full
16674 -- type declaration?
16675
16676 if Is_Itype (Def_Id)
16677 and then
16678 Nkind (Parent (P)) = N_Full_Type_Declaration
16679 then
16680 Build_Itype_Reference (Def_Id, Parent (P));
16681 end if;
16682
16683 else
16684 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
16685 end if;
16686
16687 when Concurrent_Kind =>
16688 Constrain_Concurrent (Def_Id, S,
16689 Related_Nod, Related_Id, Suffix);
16690
16691 when others =>
16692 Error_Msg_N ("invalid subtype mark in subtype indication", S);
16693 end case;
16694
16695 -- Size and Convention are always inherited from the base type
16696
16697 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
16698 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
16699
16700 return Def_Id;
16701 end if;
16702 end Process_Subtype;
16703
16704 ---------------------------------------
16705 -- Check_Anonymous_Access_Components --
16706 ---------------------------------------
16707
16708 procedure Check_Anonymous_Access_Components
16709 (Typ_Decl : Node_Id;
16710 Typ : Entity_Id;
16711 Prev : Entity_Id;
16712 Comp_List : Node_Id)
16713 is
16714 Loc : constant Source_Ptr := Sloc (Typ_Decl);
16715 Anon_Access : Entity_Id;
16716 Acc_Def : Node_Id;
16717 Comp : Node_Id;
16718 Comp_Def : Node_Id;
16719 Decl : Node_Id;
16720 Type_Def : Node_Id;
16721
16722 procedure Build_Incomplete_Type_Declaration;
16723 -- If the record type contains components that include an access to the
16724 -- current record, then create an incomplete type declaration for the
16725 -- record, to be used as the designated type of the anonymous access.
16726 -- This is done only once, and only if there is no previous partial
16727 -- view of the type.
16728
16729 function Designates_T (Subt : Node_Id) return Boolean;
16730 -- Check whether a node designates the enclosing record type, or 'Class
16731 -- of that type
16732
16733 function Mentions_T (Acc_Def : Node_Id) return Boolean;
16734 -- Check whether an access definition includes a reference to
16735 -- the enclosing record type. The reference can be a subtype mark
16736 -- in the access definition itself, a 'Class attribute reference, or
16737 -- recursively a reference appearing in a parameter specification
16738 -- or result definition of an access_to_subprogram definition.
16739
16740 --------------------------------------
16741 -- Build_Incomplete_Type_Declaration --
16742 --------------------------------------
16743
16744 procedure Build_Incomplete_Type_Declaration is
16745 Decl : Node_Id;
16746 Inc_T : Entity_Id;
16747 H : Entity_Id;
16748
16749 -- Is_Tagged indicates whether the type is tagged. It is tagged if
16750 -- it's "is new ... with record" or else "is tagged record ...".
16751
16752 Is_Tagged : constant Boolean :=
16753 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
16754 and then
16755 Present
16756 (Record_Extension_Part (Type_Definition (Typ_Decl))))
16757 or else
16758 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
16759 and then Tagged_Present (Type_Definition (Typ_Decl)));
16760
16761 begin
16762 -- If there is a previous partial view, no need to create a new one
16763 -- If the partial view, given by Prev, is incomplete, If Prev is
16764 -- a private declaration, full declaration is flagged accordingly.
16765
16766 if Prev /= Typ then
16767 if Is_Tagged then
16768 Make_Class_Wide_Type (Prev);
16769 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
16770 Set_Etype (Class_Wide_Type (Typ), Typ);
16771 end if;
16772
16773 return;
16774
16775 elsif Has_Private_Declaration (Typ) then
16776
16777 -- If we refer to T'Class inside T, and T is the completion of a
16778 -- private type, then we need to make sure the class-wide type
16779 -- exists.
16780
16781 if Is_Tagged then
16782 Make_Class_Wide_Type (Typ);
16783 end if;
16784
16785 return;
16786
16787 -- If there was a previous anonymous access type, the incomplete
16788 -- type declaration will have been created already.
16789
16790 elsif Present (Current_Entity (Typ))
16791 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
16792 and then Full_View (Current_Entity (Typ)) = Typ
16793 then
16794 return;
16795
16796 else
16797 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
16798 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
16799
16800 -- Type has already been inserted into the current scope.
16801 -- Remove it, and add incomplete declaration for type, so
16802 -- that subsequent anonymous access types can use it.
16803 -- The entity is unchained from the homonym list and from
16804 -- immediate visibility. After analysis, the entity in the
16805 -- incomplete declaration becomes immediately visible in the
16806 -- record declaration that follows.
16807
16808 H := Current_Entity (Typ);
16809
16810 if H = Typ then
16811 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
16812 else
16813 while Present (H)
16814 and then Homonym (H) /= Typ
16815 loop
16816 H := Homonym (Typ);
16817 end loop;
16818
16819 Set_Homonym (H, Homonym (Typ));
16820 end if;
16821
16822 Insert_Before (Typ_Decl, Decl);
16823 Analyze (Decl);
16824 Set_Full_View (Inc_T, Typ);
16825
16826 if Is_Tagged then
16827 -- Create a common class-wide type for both views, and set
16828 -- the Etype of the class-wide type to the full view.
16829
16830 Make_Class_Wide_Type (Inc_T);
16831 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
16832 Set_Etype (Class_Wide_Type (Typ), Typ);
16833 end if;
16834 end if;
16835 end Build_Incomplete_Type_Declaration;
16836
16837 ------------------
16838 -- Designates_T --
16839 ------------------
16840
16841 function Designates_T (Subt : Node_Id) return Boolean is
16842 Type_Id : constant Name_Id := Chars (Typ);
16843
16844 function Names_T (Nam : Node_Id) return Boolean;
16845 -- The record type has not been introduced in the current scope
16846 -- yet, so we must examine the name of the type itself, either
16847 -- an identifier T, or an expanded name of the form P.T, where
16848 -- P denotes the current scope.
16849
16850 -------------
16851 -- Names_T --
16852 -------------
16853
16854 function Names_T (Nam : Node_Id) return Boolean is
16855 begin
16856 if Nkind (Nam) = N_Identifier then
16857 return Chars (Nam) = Type_Id;
16858
16859 elsif Nkind (Nam) = N_Selected_Component then
16860 if Chars (Selector_Name (Nam)) = Type_Id then
16861 if Nkind (Prefix (Nam)) = N_Identifier then
16862 return Chars (Prefix (Nam)) = Chars (Current_Scope);
16863
16864 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
16865 return Chars (Selector_Name (Prefix (Nam))) =
16866 Chars (Current_Scope);
16867 else
16868 return False;
16869 end if;
16870
16871 else
16872 return False;
16873 end if;
16874
16875 else
16876 return False;
16877 end if;
16878 end Names_T;
16879
16880 -- Start of processing for Designates_T
16881
16882 begin
16883 if Nkind (Subt) = N_Identifier then
16884 return Chars (Subt) = Type_Id;
16885
16886 -- Reference can be through an expanded name which has not been
16887 -- analyzed yet, and which designates enclosing scopes.
16888
16889 elsif Nkind (Subt) = N_Selected_Component then
16890 if Names_T (Subt) then
16891 return True;
16892
16893 -- Otherwise it must denote an entity that is already visible.
16894 -- The access definition may name a subtype of the enclosing
16895 -- type, if there is a previous incomplete declaration for it.
16896
16897 else
16898 Find_Selected_Component (Subt);
16899 return
16900 Is_Entity_Name (Subt)
16901 and then Scope (Entity (Subt)) = Current_Scope
16902 and then
16903 (Chars (Base_Type (Entity (Subt))) = Type_Id
16904 or else
16905 (Is_Class_Wide_Type (Entity (Subt))
16906 and then
16907 Chars (Etype (Base_Type (Entity (Subt)))) =
16908 Type_Id));
16909 end if;
16910
16911 -- A reference to the current type may appear as the prefix of
16912 -- a 'Class attribute.
16913
16914 elsif Nkind (Subt) = N_Attribute_Reference
16915 and then Attribute_Name (Subt) = Name_Class
16916 then
16917 return Names_T (Prefix (Subt));
16918
16919 else
16920 return False;
16921 end if;
16922 end Designates_T;
16923
16924 ----------------
16925 -- Mentions_T --
16926 ----------------
16927
16928 function Mentions_T (Acc_Def : Node_Id) return Boolean is
16929 Param_Spec : Node_Id;
16930
16931 Acc_Subprg : constant Node_Id :=
16932 Access_To_Subprogram_Definition (Acc_Def);
16933
16934 begin
16935 if No (Acc_Subprg) then
16936 return Designates_T (Subtype_Mark (Acc_Def));
16937 end if;
16938
16939 -- Component is an access_to_subprogram: examine its formals,
16940 -- and result definition in the case of an access_to_function.
16941
16942 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
16943 while Present (Param_Spec) loop
16944 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
16945 and then Mentions_T (Parameter_Type (Param_Spec))
16946 then
16947 return True;
16948
16949 elsif Designates_T (Parameter_Type (Param_Spec)) then
16950 return True;
16951 end if;
16952
16953 Next (Param_Spec);
16954 end loop;
16955
16956 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
16957 if Nkind (Result_Definition (Acc_Subprg)) =
16958 N_Access_Definition
16959 then
16960 return Mentions_T (Result_Definition (Acc_Subprg));
16961 else
16962 return Designates_T (Result_Definition (Acc_Subprg));
16963 end if;
16964 end if;
16965
16966 return False;
16967 end Mentions_T;
16968
16969 -- Start of processing for Check_Anonymous_Access_Components
16970
16971 begin
16972 if No (Comp_List) then
16973 return;
16974 end if;
16975
16976 Comp := First (Component_Items (Comp_List));
16977 while Present (Comp) loop
16978 if Nkind (Comp) = N_Component_Declaration
16979 and then Present
16980 (Access_Definition (Component_Definition (Comp)))
16981 and then
16982 Mentions_T (Access_Definition (Component_Definition (Comp)))
16983 then
16984 Comp_Def := Component_Definition (Comp);
16985 Acc_Def :=
16986 Access_To_Subprogram_Definition
16987 (Access_Definition (Comp_Def));
16988
16989 Build_Incomplete_Type_Declaration;
16990 Anon_Access :=
16991 Make_Defining_Identifier (Loc,
16992 Chars => New_Internal_Name ('S'));
16993
16994 -- Create a declaration for the anonymous access type: either
16995 -- an access_to_object or an access_to_subprogram.
16996
16997 if Present (Acc_Def) then
16998 if Nkind (Acc_Def) = N_Access_Function_Definition then
16999 Type_Def :=
17000 Make_Access_Function_Definition (Loc,
17001 Parameter_Specifications =>
17002 Parameter_Specifications (Acc_Def),
17003 Result_Definition => Result_Definition (Acc_Def));
17004 else
17005 Type_Def :=
17006 Make_Access_Procedure_Definition (Loc,
17007 Parameter_Specifications =>
17008 Parameter_Specifications (Acc_Def));
17009 end if;
17010
17011 else
17012 Type_Def :=
17013 Make_Access_To_Object_Definition (Loc,
17014 Subtype_Indication =>
17015 Relocate_Node
17016 (Subtype_Mark
17017 (Access_Definition (Comp_Def))));
17018
17019 Set_Constant_Present
17020 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
17021 Set_All_Present
17022 (Type_Def, All_Present (Access_Definition (Comp_Def)));
17023 end if;
17024
17025 Set_Null_Exclusion_Present
17026 (Type_Def,
17027 Null_Exclusion_Present (Access_Definition (Comp_Def)));
17028
17029 Decl :=
17030 Make_Full_Type_Declaration (Loc,
17031 Defining_Identifier => Anon_Access,
17032 Type_Definition => Type_Def);
17033
17034 Insert_Before (Typ_Decl, Decl);
17035 Analyze (Decl);
17036
17037 -- If an access to object, Preserve entity of designated type,
17038 -- for ASIS use, before rewriting the component definition.
17039
17040 if No (Acc_Def) then
17041 declare
17042 Desig : Entity_Id;
17043
17044 begin
17045 Desig := Entity (Subtype_Indication (Type_Def));
17046
17047 -- If the access definition is to the current record,
17048 -- the visible entity at this point is an incomplete
17049 -- type. Retrieve the full view to simplify ASIS queries
17050
17051 if Ekind (Desig) = E_Incomplete_Type then
17052 Desig := Full_View (Desig);
17053 end if;
17054
17055 Set_Entity
17056 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
17057 end;
17058 end if;
17059
17060 Rewrite (Comp_Def,
17061 Make_Component_Definition (Loc,
17062 Subtype_Indication =>
17063 New_Occurrence_Of (Anon_Access, Loc)));
17064
17065 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
17066 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
17067 else
17068 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
17069 end if;
17070
17071 Set_Is_Local_Anonymous_Access (Anon_Access);
17072 end if;
17073
17074 Next (Comp);
17075 end loop;
17076
17077 if Present (Variant_Part (Comp_List)) then
17078 declare
17079 V : Node_Id;
17080 begin
17081 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
17082 while Present (V) loop
17083 Check_Anonymous_Access_Components
17084 (Typ_Decl, Typ, Prev, Component_List (V));
17085 Next_Non_Pragma (V);
17086 end loop;
17087 end;
17088 end if;
17089 end Check_Anonymous_Access_Components;
17090
17091 --------------------------------
17092 -- Preanalyze_Spec_Expression --
17093 --------------------------------
17094
17095 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
17096 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
17097 begin
17098 In_Spec_Expression := True;
17099 Preanalyze_And_Resolve (N, T);
17100 In_Spec_Expression := Save_In_Spec_Expression;
17101 end Preanalyze_Spec_Expression;
17102
17103 -----------------------------
17104 -- Record_Type_Declaration --
17105 -----------------------------
17106
17107 procedure Record_Type_Declaration
17108 (T : Entity_Id;
17109 N : Node_Id;
17110 Prev : Entity_Id)
17111 is
17112 Def : constant Node_Id := Type_Definition (N);
17113 Is_Tagged : Boolean;
17114 Tag_Comp : Entity_Id;
17115
17116 begin
17117 -- These flags must be initialized before calling Process_Discriminants
17118 -- because this routine makes use of them.
17119
17120 Set_Ekind (T, E_Record_Type);
17121 Set_Etype (T, T);
17122 Init_Size_Align (T);
17123 Set_Interfaces (T, No_Elist);
17124 Set_Stored_Constraint (T, No_Elist);
17125
17126 -- Normal case
17127
17128 if Ada_Version < Ada_05
17129 or else not Interface_Present (Def)
17130 then
17131 -- The flag Is_Tagged_Type might have already been set by
17132 -- Find_Type_Name if it detected an error for declaration T. This
17133 -- arises in the case of private tagged types where the full view
17134 -- omits the word tagged.
17135
17136 Is_Tagged :=
17137 Tagged_Present (Def)
17138 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
17139
17140 Set_Is_Tagged_Type (T, Is_Tagged);
17141 Set_Is_Limited_Record (T, Limited_Present (Def));
17142
17143 -- Type is abstract if full declaration carries keyword, or if
17144 -- previous partial view did.
17145
17146 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
17147 or else Abstract_Present (Def));
17148
17149 else
17150 Is_Tagged := True;
17151 Analyze_Interface_Declaration (T, Def);
17152
17153 if Present (Discriminant_Specifications (N)) then
17154 Error_Msg_N
17155 ("interface types cannot have discriminants",
17156 Defining_Identifier
17157 (First (Discriminant_Specifications (N))));
17158 end if;
17159 end if;
17160
17161 -- First pass: if there are self-referential access components,
17162 -- create the required anonymous access type declarations, and if
17163 -- need be an incomplete type declaration for T itself.
17164
17165 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
17166
17167 if Ada_Version >= Ada_05
17168 and then Present (Interface_List (Def))
17169 then
17170 Check_Interfaces (N, Def);
17171
17172 declare
17173 Ifaces_List : Elist_Id;
17174
17175 begin
17176 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
17177 -- already in the parents.
17178
17179 Collect_Interfaces
17180 (T => T,
17181 Ifaces_List => Ifaces_List,
17182 Exclude_Parents => True);
17183
17184 Set_Interfaces (T, Ifaces_List);
17185 end;
17186 end if;
17187
17188 -- Records constitute a scope for the component declarations within.
17189 -- The scope is created prior to the processing of these declarations.
17190 -- Discriminants are processed first, so that they are visible when
17191 -- processing the other components. The Ekind of the record type itself
17192 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
17193
17194 -- Enter record scope
17195
17196 Push_Scope (T);
17197
17198 -- If an incomplete or private type declaration was already given for
17199 -- the type, then this scope already exists, and the discriminants have
17200 -- been declared within. We must verify that the full declaration
17201 -- matches the incomplete one.
17202
17203 Check_Or_Process_Discriminants (N, T, Prev);
17204
17205 Set_Is_Constrained (T, not Has_Discriminants (T));
17206 Set_Has_Delayed_Freeze (T, True);
17207
17208 -- For tagged types add a manually analyzed component corresponding
17209 -- to the component _tag, the corresponding piece of tree will be
17210 -- expanded as part of the freezing actions if it is not a CPP_Class.
17211
17212 if Is_Tagged then
17213
17214 -- Do not add the tag unless we are in expansion mode
17215
17216 if Expander_Active then
17217 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
17218 Enter_Name (Tag_Comp);
17219
17220 Set_Ekind (Tag_Comp, E_Component);
17221 Set_Is_Tag (Tag_Comp);
17222 Set_Is_Aliased (Tag_Comp);
17223 Set_Etype (Tag_Comp, RTE (RE_Tag));
17224 Set_DT_Entry_Count (Tag_Comp, No_Uint);
17225 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
17226 Init_Component_Location (Tag_Comp);
17227
17228 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
17229 -- implemented interfaces.
17230
17231 if Has_Interfaces (T) then
17232 Add_Interface_Tag_Components (N, T);
17233 end if;
17234 end if;
17235
17236 Make_Class_Wide_Type (T);
17237 Set_Primitive_Operations (T, New_Elmt_List);
17238 end if;
17239
17240 -- We must suppress range checks when processing the components
17241 -- of a record in the presence of discriminants, since we don't
17242 -- want spurious checks to be generated during their analysis, but
17243 -- must reset the Suppress_Range_Checks flags after having processed
17244 -- the record definition.
17245
17246 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
17247 -- couldn't we just use the normal range check suppression method here.
17248 -- That would seem cleaner ???
17249
17250 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
17251 Set_Kill_Range_Checks (T, True);
17252 Record_Type_Definition (Def, Prev);
17253 Set_Kill_Range_Checks (T, False);
17254 else
17255 Record_Type_Definition (Def, Prev);
17256 end if;
17257
17258 -- Exit from record scope
17259
17260 End_Scope;
17261
17262 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
17263 -- the implemented interfaces and associate them an aliased entity.
17264
17265 if Is_Tagged
17266 and then not Is_Empty_List (Interface_List (Def))
17267 then
17268 Derive_Progenitor_Subprograms (T, T);
17269 end if;
17270 end Record_Type_Declaration;
17271
17272 ----------------------------
17273 -- Record_Type_Definition --
17274 ----------------------------
17275
17276 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
17277 Component : Entity_Id;
17278 Ctrl_Components : Boolean := False;
17279 Final_Storage_Only : Boolean;
17280 T : Entity_Id;
17281
17282 begin
17283 if Ekind (Prev_T) = E_Incomplete_Type then
17284 T := Full_View (Prev_T);
17285 else
17286 T := Prev_T;
17287 end if;
17288
17289 Final_Storage_Only := not Is_Controlled (T);
17290
17291 -- Ada 2005: check whether an explicit Limited is present in a derived
17292 -- type declaration.
17293
17294 if Nkind (Parent (Def)) = N_Derived_Type_Definition
17295 and then Limited_Present (Parent (Def))
17296 then
17297 Set_Is_Limited_Record (T);
17298 end if;
17299
17300 -- If the component list of a record type is defined by the reserved
17301 -- word null and there is no discriminant part, then the record type has
17302 -- no components and all records of the type are null records (RM 3.7)
17303 -- This procedure is also called to process the extension part of a
17304 -- record extension, in which case the current scope may have inherited
17305 -- components.
17306
17307 if No (Def)
17308 or else No (Component_List (Def))
17309 or else Null_Present (Component_List (Def))
17310 then
17311 null;
17312
17313 else
17314 Analyze_Declarations (Component_Items (Component_List (Def)));
17315
17316 if Present (Variant_Part (Component_List (Def))) then
17317 Analyze (Variant_Part (Component_List (Def)));
17318 end if;
17319 end if;
17320
17321 -- After completing the semantic analysis of the record definition,
17322 -- record components, both new and inherited, are accessible. Set their
17323 -- kind accordingly. Exclude malformed itypes from illegal declarations,
17324 -- whose Ekind may be void.
17325
17326 Component := First_Entity (Current_Scope);
17327 while Present (Component) loop
17328 if Ekind (Component) = E_Void
17329 and then not Is_Itype (Component)
17330 then
17331 Set_Ekind (Component, E_Component);
17332 Init_Component_Location (Component);
17333 end if;
17334
17335 if Has_Task (Etype (Component)) then
17336 Set_Has_Task (T);
17337 end if;
17338
17339 if Ekind (Component) /= E_Component then
17340 null;
17341
17342 elsif Has_Controlled_Component (Etype (Component))
17343 or else (Chars (Component) /= Name_uParent
17344 and then Is_Controlled (Etype (Component)))
17345 then
17346 Set_Has_Controlled_Component (T, True);
17347 Final_Storage_Only :=
17348 Final_Storage_Only
17349 and then Finalize_Storage_Only (Etype (Component));
17350 Ctrl_Components := True;
17351 end if;
17352
17353 Next_Entity (Component);
17354 end loop;
17355
17356 -- A Type is Finalize_Storage_Only only if all its controlled components
17357 -- are also.
17358
17359 if Ctrl_Components then
17360 Set_Finalize_Storage_Only (T, Final_Storage_Only);
17361 end if;
17362
17363 -- Place reference to end record on the proper entity, which may
17364 -- be a partial view.
17365
17366 if Present (Def) then
17367 Process_End_Label (Def, 'e', Prev_T);
17368 end if;
17369 end Record_Type_Definition;
17370
17371 ------------------------
17372 -- Replace_Components --
17373 ------------------------
17374
17375 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
17376 function Process (N : Node_Id) return Traverse_Result;
17377
17378 -------------
17379 -- Process --
17380 -------------
17381
17382 function Process (N : Node_Id) return Traverse_Result is
17383 Comp : Entity_Id;
17384
17385 begin
17386 if Nkind (N) = N_Discriminant_Specification then
17387 Comp := First_Discriminant (Typ);
17388 while Present (Comp) loop
17389 if Chars (Comp) = Chars (Defining_Identifier (N)) then
17390 Set_Defining_Identifier (N, Comp);
17391 exit;
17392 end if;
17393
17394 Next_Discriminant (Comp);
17395 end loop;
17396
17397 elsif Nkind (N) = N_Component_Declaration then
17398 Comp := First_Component (Typ);
17399 while Present (Comp) loop
17400 if Chars (Comp) = Chars (Defining_Identifier (N)) then
17401 Set_Defining_Identifier (N, Comp);
17402 exit;
17403 end if;
17404
17405 Next_Component (Comp);
17406 end loop;
17407 end if;
17408
17409 return OK;
17410 end Process;
17411
17412 procedure Replace is new Traverse_Proc (Process);
17413
17414 -- Start of processing for Replace_Components
17415
17416 begin
17417 Replace (Decl);
17418 end Replace_Components;
17419
17420 -------------------------------
17421 -- Set_Completion_Referenced --
17422 -------------------------------
17423
17424 procedure Set_Completion_Referenced (E : Entity_Id) is
17425 begin
17426 -- If in main unit, mark entity that is a completion as referenced,
17427 -- warnings go on the partial view when needed.
17428
17429 if In_Extended_Main_Source_Unit (E) then
17430 Set_Referenced (E);
17431 end if;
17432 end Set_Completion_Referenced;
17433
17434 ---------------------
17435 -- Set_Fixed_Range --
17436 ---------------------
17437
17438 -- The range for fixed-point types is complicated by the fact that we
17439 -- do not know the exact end points at the time of the declaration. This
17440 -- is true for three reasons:
17441
17442 -- A size clause may affect the fudging of the end-points
17443 -- A small clause may affect the values of the end-points
17444 -- We try to include the end-points if it does not affect the size
17445
17446 -- This means that the actual end-points must be established at the point
17447 -- when the type is frozen. Meanwhile, we first narrow the range as
17448 -- permitted (so that it will fit if necessary in a small specified size),
17449 -- and then build a range subtree with these narrowed bounds.
17450
17451 -- Set_Fixed_Range constructs the range from real literal values, and sets
17452 -- the range as the Scalar_Range of the given fixed-point type entity.
17453
17454 -- The parent of this range is set to point to the entity so that it is
17455 -- properly hooked into the tree (unlike normal Scalar_Range entries for
17456 -- other scalar types, which are just pointers to the range in the
17457 -- original tree, this would otherwise be an orphan).
17458
17459 -- The tree is left unanalyzed. When the type is frozen, the processing
17460 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
17461 -- analyzed, and uses this as an indication that it should complete
17462 -- work on the range (it will know the final small and size values).
17463
17464 procedure Set_Fixed_Range
17465 (E : Entity_Id;
17466 Loc : Source_Ptr;
17467 Lo : Ureal;
17468 Hi : Ureal)
17469 is
17470 S : constant Node_Id :=
17471 Make_Range (Loc,
17472 Low_Bound => Make_Real_Literal (Loc, Lo),
17473 High_Bound => Make_Real_Literal (Loc, Hi));
17474 begin
17475 Set_Scalar_Range (E, S);
17476 Set_Parent (S, E);
17477 end Set_Fixed_Range;
17478
17479 ----------------------------------
17480 -- Set_Scalar_Range_For_Subtype --
17481 ----------------------------------
17482
17483 procedure Set_Scalar_Range_For_Subtype
17484 (Def_Id : Entity_Id;
17485 R : Node_Id;
17486 Subt : Entity_Id)
17487 is
17488 Kind : constant Entity_Kind := Ekind (Def_Id);
17489
17490 begin
17491 Set_Scalar_Range (Def_Id, R);
17492
17493 -- We need to link the range into the tree before resolving it so
17494 -- that types that are referenced, including importantly the subtype
17495 -- itself, are properly frozen (Freeze_Expression requires that the
17496 -- expression be properly linked into the tree). Of course if it is
17497 -- already linked in, then we do not disturb the current link.
17498
17499 if No (Parent (R)) then
17500 Set_Parent (R, Def_Id);
17501 end if;
17502
17503 -- Reset the kind of the subtype during analysis of the range, to
17504 -- catch possible premature use in the bounds themselves.
17505
17506 Set_Ekind (Def_Id, E_Void);
17507 Process_Range_Expr_In_Decl (R, Subt);
17508 Set_Ekind (Def_Id, Kind);
17509 end Set_Scalar_Range_For_Subtype;
17510
17511 --------------------------------------------------------
17512 -- Set_Stored_Constraint_From_Discriminant_Constraint --
17513 --------------------------------------------------------
17514
17515 procedure Set_Stored_Constraint_From_Discriminant_Constraint
17516 (E : Entity_Id)
17517 is
17518 begin
17519 -- Make sure set if encountered during Expand_To_Stored_Constraint
17520
17521 Set_Stored_Constraint (E, No_Elist);
17522
17523 -- Give it the right value
17524
17525 if Is_Constrained (E) and then Has_Discriminants (E) then
17526 Set_Stored_Constraint (E,
17527 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
17528 end if;
17529 end Set_Stored_Constraint_From_Discriminant_Constraint;
17530
17531 -------------------------------------
17532 -- Signed_Integer_Type_Declaration --
17533 -------------------------------------
17534
17535 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17536 Implicit_Base : Entity_Id;
17537 Base_Typ : Entity_Id;
17538 Lo_Val : Uint;
17539 Hi_Val : Uint;
17540 Errs : Boolean := False;
17541 Lo : Node_Id;
17542 Hi : Node_Id;
17543
17544 function Can_Derive_From (E : Entity_Id) return Boolean;
17545 -- Determine whether given bounds allow derivation from specified type
17546
17547 procedure Check_Bound (Expr : Node_Id);
17548 -- Check bound to make sure it is integral and static. If not, post
17549 -- appropriate error message and set Errs flag
17550
17551 ---------------------
17552 -- Can_Derive_From --
17553 ---------------------
17554
17555 -- Note we check both bounds against both end values, to deal with
17556 -- strange types like ones with a range of 0 .. -12341234.
17557
17558 function Can_Derive_From (E : Entity_Id) return Boolean is
17559 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
17560 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
17561 begin
17562 return Lo <= Lo_Val and then Lo_Val <= Hi
17563 and then
17564 Lo <= Hi_Val and then Hi_Val <= Hi;
17565 end Can_Derive_From;
17566
17567 -----------------
17568 -- Check_Bound --
17569 -----------------
17570
17571 procedure Check_Bound (Expr : Node_Id) is
17572 begin
17573 -- If a range constraint is used as an integer type definition, each
17574 -- bound of the range must be defined by a static expression of some
17575 -- integer type, but the two bounds need not have the same integer
17576 -- type (Negative bounds are allowed.) (RM 3.5.4)
17577
17578 if not Is_Integer_Type (Etype (Expr)) then
17579 Error_Msg_N
17580 ("integer type definition bounds must be of integer type", Expr);
17581 Errs := True;
17582
17583 elsif not Is_OK_Static_Expression (Expr) then
17584 Flag_Non_Static_Expr
17585 ("non-static expression used for integer type bound!", Expr);
17586 Errs := True;
17587
17588 -- The bounds are folded into literals, and we set their type to be
17589 -- universal, to avoid typing difficulties: we cannot set the type
17590 -- of the literal to the new type, because this would be a forward
17591 -- reference for the back end, and if the original type is user-
17592 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
17593
17594 else
17595 if Is_Entity_Name (Expr) then
17596 Fold_Uint (Expr, Expr_Value (Expr), True);
17597 end if;
17598
17599 Set_Etype (Expr, Universal_Integer);
17600 end if;
17601 end Check_Bound;
17602
17603 -- Start of processing for Signed_Integer_Type_Declaration
17604
17605 begin
17606 -- Create an anonymous base type
17607
17608 Implicit_Base :=
17609 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
17610
17611 -- Analyze and check the bounds, they can be of any integer type
17612
17613 Lo := Low_Bound (Def);
17614 Hi := High_Bound (Def);
17615
17616 -- Arbitrarily use Integer as the type if either bound had an error
17617
17618 if Hi = Error or else Lo = Error then
17619 Base_Typ := Any_Integer;
17620 Set_Error_Posted (T, True);
17621
17622 -- Here both bounds are OK expressions
17623
17624 else
17625 Analyze_And_Resolve (Lo, Any_Integer);
17626 Analyze_And_Resolve (Hi, Any_Integer);
17627
17628 Check_Bound (Lo);
17629 Check_Bound (Hi);
17630
17631 if Errs then
17632 Hi := Type_High_Bound (Standard_Long_Long_Integer);
17633 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
17634 end if;
17635
17636 -- Find type to derive from
17637
17638 Lo_Val := Expr_Value (Lo);
17639 Hi_Val := Expr_Value (Hi);
17640
17641 if Can_Derive_From (Standard_Short_Short_Integer) then
17642 Base_Typ := Base_Type (Standard_Short_Short_Integer);
17643
17644 elsif Can_Derive_From (Standard_Short_Integer) then
17645 Base_Typ := Base_Type (Standard_Short_Integer);
17646
17647 elsif Can_Derive_From (Standard_Integer) then
17648 Base_Typ := Base_Type (Standard_Integer);
17649
17650 elsif Can_Derive_From (Standard_Long_Integer) then
17651 Base_Typ := Base_Type (Standard_Long_Integer);
17652
17653 elsif Can_Derive_From (Standard_Long_Long_Integer) then
17654 Base_Typ := Base_Type (Standard_Long_Long_Integer);
17655
17656 else
17657 Base_Typ := Base_Type (Standard_Long_Long_Integer);
17658 Error_Msg_N ("integer type definition bounds out of range", Def);
17659 Hi := Type_High_Bound (Standard_Long_Long_Integer);
17660 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
17661 end if;
17662 end if;
17663
17664 -- Complete both implicit base and declared first subtype entities
17665
17666 Set_Etype (Implicit_Base, Base_Typ);
17667 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17668 Set_Size_Info (Implicit_Base, (Base_Typ));
17669 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17670 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17671
17672 Set_Ekind (T, E_Signed_Integer_Subtype);
17673 Set_Etype (T, Implicit_Base);
17674
17675 Set_Size_Info (T, (Implicit_Base));
17676 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
17677 Set_Scalar_Range (T, Def);
17678 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17679 Set_Is_Constrained (T);
17680 end Signed_Integer_Type_Declaration;
17681
17682 end Sem_Ch3;