Fix unused warnings.
[official-gcc/graphite-test-results.git] / gcc / ada / sem_ch3.adb
blob92f1333bcaa974026918bb342901b3b794e9ba43
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Itypes; use Itypes;
43 with Layout; use Layout;
44 with Lib; use Lib;
45 with Lib.Xref; use Lib.Xref;
46 with Namet; use Namet;
47 with Nmake; use Nmake;
48 with Opt; use Opt;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Case; use Sem_Case;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch7; use Sem_Ch7;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Dist; use Sem_Dist;
62 with Sem_Elim; use Sem_Elim;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Mech; use Sem_Mech;
65 with Sem_Prag; use Sem_Prag;
66 with Sem_Res; use Sem_Res;
67 with Sem_Smem; use Sem_Smem;
68 with Sem_Type; use Sem_Type;
69 with Sem_Util; use Sem_Util;
70 with Sem_Warn; use Sem_Warn;
71 with Stand; use Stand;
72 with Sinfo; use Sinfo;
73 with Sinput; use Sinput;
74 with Snames; use Snames;
75 with Targparm; use Targparm;
76 with Tbuild; use Tbuild;
77 with Ttypes; use Ttypes;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
81 package body Sem_Ch3 is
83 -----------------------
84 -- Local Subprograms --
85 -----------------------
87 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
88 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
89 -- abstract interface types implemented by a record type or a derived
90 -- record type.
92 procedure Build_Derived_Type
93 (N : Node_Id;
94 Parent_Type : Entity_Id;
95 Derived_Type : Entity_Id;
96 Is_Completion : Boolean;
97 Derive_Subps : Boolean := True);
98 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
99 -- the N_Full_Type_Declaration node containing the derived type definition.
100 -- Parent_Type is the entity for the parent type in the derived type
101 -- definition and Derived_Type the actual derived type. Is_Completion must
102 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
103 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
104 -- completion of a private type declaration. If Is_Completion is set to
105 -- True, N is the completion of a private type declaration and Derived_Type
106 -- is different from the defining identifier inside N (i.e. Derived_Type /=
107 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
108 -- subprograms should be derived. The only case where this parameter is
109 -- False is when Build_Derived_Type is recursively called to process an
110 -- implicit derived full type for a type derived from a private type (in
111 -- that case the subprograms must only be derived for the private view of
112 -- the type).
114 -- ??? These flags need a bit of re-examination and re-documentation:
115 -- ??? are they both necessary (both seem related to the recursion)?
117 procedure Build_Derived_Access_Type
118 (N : Node_Id;
119 Parent_Type : Entity_Id;
120 Derived_Type : Entity_Id);
121 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
122 -- create an implicit base if the parent type is constrained or if the
123 -- subtype indication has a constraint.
125 procedure Build_Derived_Array_Type
126 (N : Node_Id;
127 Parent_Type : Entity_Id;
128 Derived_Type : Entity_Id);
129 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
130 -- create an implicit base if the parent type is constrained or if the
131 -- subtype indication has a constraint.
133 procedure Build_Derived_Concurrent_Type
134 (N : Node_Id;
135 Parent_Type : Entity_Id;
136 Derived_Type : Entity_Id);
137 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
138 -- protected type, inherit entries and protected subprograms, check
139 -- legality of discriminant constraints if any.
141 procedure Build_Derived_Enumeration_Type
142 (N : Node_Id;
143 Parent_Type : Entity_Id;
144 Derived_Type : Entity_Id);
145 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
146 -- type, we must create a new list of literals. Types derived from
147 -- Character and [Wide_]Wide_Character are special-cased.
149 procedure Build_Derived_Numeric_Type
150 (N : Node_Id;
151 Parent_Type : Entity_Id;
152 Derived_Type : Entity_Id);
153 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
154 -- an anonymous base type, and propagate constraint to subtype if needed.
156 procedure Build_Derived_Private_Type
157 (N : Node_Id;
158 Parent_Type : Entity_Id;
159 Derived_Type : Entity_Id;
160 Is_Completion : Boolean;
161 Derive_Subps : Boolean := True);
162 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
163 -- because the parent may or may not have a completion, and the derivation
164 -- may itself be a completion.
166 procedure Build_Derived_Record_Type
167 (N : Node_Id;
168 Parent_Type : Entity_Id;
169 Derived_Type : Entity_Id;
170 Derive_Subps : Boolean := True);
171 -- Subsidiary procedure for Build_Derived_Type and
172 -- Analyze_Private_Extension_Declaration used for tagged and untagged
173 -- record types. All parameters are as in Build_Derived_Type except that
174 -- N, in addition to being an N_Full_Type_Declaration node, can also be an
175 -- N_Private_Extension_Declaration node. See the definition of this routine
176 -- for much more info. Derive_Subps indicates whether subprograms should
177 -- be derived from the parent type. The only case where Derive_Subps is
178 -- False is for an implicit derived full type for a type derived from a
179 -- private type (see Build_Derived_Type).
181 procedure Build_Discriminal (Discrim : Entity_Id);
182 -- Create the discriminal corresponding to discriminant Discrim, that is
183 -- the parameter corresponding to Discrim to be used in initialization
184 -- procedures for the type where Discrim is a discriminant. Discriminals
185 -- are not used during semantic analysis, and are not fully defined
186 -- entities until expansion. Thus they are not given a scope until
187 -- initialization procedures are built.
189 function Build_Discriminant_Constraints
190 (T : Entity_Id;
191 Def : Node_Id;
192 Derived_Def : Boolean := False) return Elist_Id;
193 -- Validate discriminant constraints and return the list of the constraints
194 -- in order of discriminant declarations, where T is the discriminated
195 -- unconstrained type. Def is the N_Subtype_Indication node where the
196 -- discriminants constraints for T are specified. Derived_Def is True
197 -- when building the discriminant constraints in a derived type definition
198 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
199 -- type and Def is the constraint "(xxx)" on T and this routine sets the
200 -- Corresponding_Discriminant field of the discriminants in the derived
201 -- type D to point to the corresponding discriminants in the parent type T.
203 procedure Build_Discriminated_Subtype
204 (T : Entity_Id;
205 Def_Id : Entity_Id;
206 Elist : Elist_Id;
207 Related_Nod : Node_Id;
208 For_Access : Boolean := False);
209 -- Subsidiary procedure to Constrain_Discriminated_Type and to
210 -- Process_Incomplete_Dependents. Given
212 -- T (a possibly discriminated base type)
213 -- Def_Id (a very partially built subtype for T),
215 -- the call completes Def_Id to be the appropriate E_*_Subtype.
217 -- The Elist is the list of discriminant constraints if any (it is set
218 -- to No_Elist if T is not a discriminated type, and to an empty list if
219 -- T has discriminants but there are no discriminant constraints). The
220 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
221 -- The For_Access says whether or not this subtype is really constraining
222 -- an access type. That is its sole purpose is the designated type of an
223 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
224 -- is built to avoid freezing T when the access subtype is frozen.
226 function Build_Scalar_Bound
227 (Bound : Node_Id;
228 Par_T : Entity_Id;
229 Der_T : Entity_Id) return Node_Id;
230 -- The bounds of a derived scalar type are conversions of the bounds of
231 -- the parent type. Optimize the representation if the bounds are literals.
232 -- Needs a more complete spec--what are the parameters exactly, and what
233 -- exactly is the returned value, and how is Bound affected???
235 procedure Build_Underlying_Full_View
236 (N : Node_Id;
237 Typ : Entity_Id;
238 Par : Entity_Id);
239 -- If the completion of a private type is itself derived from a private
240 -- type, or if the full view of a private subtype is itself private, the
241 -- back-end has no way to compute the actual size of this type. We build
242 -- an internal subtype declaration of the proper parent type to convey
243 -- this information. This extra mechanism is needed because a full
244 -- view cannot itself have a full view (it would get clobbered during
245 -- view exchanges).
247 procedure Check_Access_Discriminant_Requires_Limited
248 (D : Node_Id;
249 Loc : Node_Id);
250 -- Check the restriction that the type to which an access discriminant
251 -- belongs must be a concurrent type or a descendant of a type with
252 -- the reserved word 'limited' in its declaration.
254 procedure Check_Anonymous_Access_Components
255 (Typ_Decl : Node_Id;
256 Typ : Entity_Id;
257 Prev : Entity_Id;
258 Comp_List : Node_Id);
259 -- Ada 2005 AI-382: an access component in a record definition can refer to
260 -- the enclosing record, in which case it denotes the type itself, and not
261 -- the current instance of the type. We create an anonymous access type for
262 -- the component, and flag it as an access to a component, so accessibility
263 -- checks are properly performed on it. The declaration of the access type
264 -- is placed ahead of that of the record to prevent order-of-elaboration
265 -- circularity issues in Gigi. We create an incomplete type for the record
266 -- declaration, which is the designated type of the anonymous access.
268 procedure Check_Delta_Expression (E : Node_Id);
269 -- Check that the expression represented by E is suitable for use as a
270 -- delta expression, i.e. it is of real type and is static.
272 procedure Check_Digits_Expression (E : Node_Id);
273 -- Check that the expression represented by E is suitable for use as a
274 -- digits expression, i.e. it is of integer type, positive and static.
276 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
277 -- Validate the initialization of an object declaration. T is the required
278 -- type, and Exp is the initialization expression.
280 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
281 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
283 procedure Check_Or_Process_Discriminants
284 (N : Node_Id;
285 T : Entity_Id;
286 Prev : Entity_Id := Empty);
287 -- If T is the full declaration of an incomplete or private type, check the
288 -- conformance of the discriminants, otherwise process them. Prev is the
289 -- entity of the partial declaration, if any.
291 procedure Check_Real_Bound (Bound : Node_Id);
292 -- Check given bound for being of real type and static. If not, post an
293 -- appropriate message, and rewrite the bound with the real literal zero.
295 procedure Constant_Redeclaration
296 (Id : Entity_Id;
297 N : Node_Id;
298 T : out Entity_Id);
299 -- Various checks on legality of full declaration of deferred constant.
300 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
301 -- node. The caller has not yet set any attributes of this entity.
303 function Contain_Interface
304 (Iface : Entity_Id;
305 Ifaces : Elist_Id) return Boolean;
306 -- Ada 2005: Determine whether Iface is present in the list Ifaces
308 procedure Convert_Scalar_Bounds
309 (N : Node_Id;
310 Parent_Type : Entity_Id;
311 Derived_Type : Entity_Id;
312 Loc : Source_Ptr);
313 -- For derived scalar types, convert the bounds in the type definition to
314 -- the derived type, and complete their analysis. Given a constraint of the
315 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
316 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
317 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
318 -- subtype are conversions of those bounds to the derived_type, so that
319 -- their typing is consistent.
321 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
322 -- Copies attributes from array base type T2 to array base type T1. Copies
323 -- only attributes that apply to base types, but not subtypes.
325 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
326 -- Copies attributes from array subtype T2 to array subtype T1. Copies
327 -- attributes that apply to both subtypes and base types.
329 procedure Create_Constrained_Components
330 (Subt : Entity_Id;
331 Decl_Node : Node_Id;
332 Typ : Entity_Id;
333 Constraints : Elist_Id);
334 -- Build the list of entities for a constrained discriminated record
335 -- subtype. If a component depends on a discriminant, replace its subtype
336 -- using the discriminant values in the discriminant constraint. Subt
337 -- is the defining identifier for the subtype whose list of constrained
338 -- entities we will create. Decl_Node is the type declaration node where
339 -- we will attach all the itypes created. Typ is the base discriminated
340 -- type for the subtype Subt. Constraints is the list of discriminant
341 -- constraints for Typ.
343 function Constrain_Component_Type
344 (Comp : Entity_Id;
345 Constrained_Typ : Entity_Id;
346 Related_Node : Node_Id;
347 Typ : Entity_Id;
348 Constraints : Elist_Id) return Entity_Id;
349 -- Given a discriminated base type Typ, a list of discriminant constraint
350 -- Constraints for Typ and a component of Typ, with type Compon_Type,
351 -- create and return the type corresponding to Compon_type where all
352 -- discriminant references are replaced with the corresponding constraint.
353 -- If no discriminant references occur in Compon_Typ then return it as is.
354 -- Constrained_Typ is the final constrained subtype to which the
355 -- constrained Compon_Type belongs. Related_Node is the node where we will
356 -- attach all the itypes created.
358 -- Above description is confused, what is Compon_Type???
360 procedure Constrain_Access
361 (Def_Id : in out Entity_Id;
362 S : Node_Id;
363 Related_Nod : Node_Id);
364 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
365 -- an anonymous type created for a subtype indication. In that case it is
366 -- created in the procedure and attached to Related_Nod.
368 procedure Constrain_Array
369 (Def_Id : in out Entity_Id;
370 SI : Node_Id;
371 Related_Nod : Node_Id;
372 Related_Id : Entity_Id;
373 Suffix : Character);
374 -- Apply a list of index constraints to an unconstrained array type. The
375 -- first parameter is the entity for the resulting subtype. A value of
376 -- Empty for Def_Id indicates that an implicit type must be created, but
377 -- creation is delayed (and must be done by this procedure) because other
378 -- subsidiary implicit types must be created first (which is why Def_Id
379 -- is an in/out parameter). The second parameter is a subtype indication
380 -- node for the constrained array to be created (e.g. something of the
381 -- form string (1 .. 10)). Related_Nod gives the place where this type
382 -- has to be inserted in the tree. The Related_Id and Suffix parameters
383 -- are used to build the associated Implicit type name.
385 procedure Constrain_Concurrent
386 (Def_Id : in out Entity_Id;
387 SI : Node_Id;
388 Related_Nod : Node_Id;
389 Related_Id : Entity_Id;
390 Suffix : Character);
391 -- Apply list of discriminant constraints to an unconstrained concurrent
392 -- type.
394 -- SI is the N_Subtype_Indication node containing the constraint and
395 -- the unconstrained type to constrain.
397 -- Def_Id is the entity for the resulting constrained subtype. A value
398 -- of Empty for Def_Id indicates that an implicit type must be created,
399 -- but creation is delayed (and must be done by this procedure) because
400 -- other subsidiary implicit types must be created first (which is why
401 -- Def_Id is an in/out parameter).
403 -- Related_Nod gives the place where this type has to be inserted
404 -- in the tree
406 -- The last two arguments are used to create its external name if needed.
408 function Constrain_Corresponding_Record
409 (Prot_Subt : Entity_Id;
410 Corr_Rec : Entity_Id;
411 Related_Nod : Node_Id;
412 Related_Id : Entity_Id) return Entity_Id;
413 -- When constraining a protected type or task type with discriminants,
414 -- constrain the corresponding record with the same discriminant values.
416 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
417 -- Constrain a decimal fixed point type with a digits constraint and/or a
418 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
420 procedure Constrain_Discriminated_Type
421 (Def_Id : Entity_Id;
422 S : Node_Id;
423 Related_Nod : Node_Id;
424 For_Access : Boolean := False);
425 -- Process discriminant constraints of composite type. Verify that values
426 -- have been provided for all discriminants, that the original type is
427 -- unconstrained, and that the types of the supplied expressions match
428 -- the discriminant types. The first three parameters are like in routine
429 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
430 -- of For_Access.
432 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
433 -- Constrain an enumeration type with a range constraint. This is identical
434 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
436 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
437 -- Constrain a floating point type with either a digits constraint
438 -- and/or a range constraint, building a E_Floating_Point_Subtype.
440 procedure Constrain_Index
441 (Index : Node_Id;
442 S : Node_Id;
443 Related_Nod : Node_Id;
444 Related_Id : Entity_Id;
445 Suffix : Character;
446 Suffix_Index : Nat);
447 -- Process an index constraint in a constrained array declaration. The
448 -- constraint can be a subtype name, or a range with or without an explicit
449 -- subtype mark. The index is the corresponding index of the unconstrained
450 -- array. The Related_Id and Suffix parameters are used to build the
451 -- associated Implicit type name.
453 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
454 -- Build subtype of a signed or modular integer type
456 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
457 -- Constrain an ordinary fixed point type with a range constraint, and
458 -- build an E_Ordinary_Fixed_Point_Subtype entity.
460 procedure Copy_And_Swap (Priv, Full : Entity_Id);
461 -- Copy the Priv entity into the entity of its full declaration then swap
462 -- the two entities in such a manner that the former private type is now
463 -- seen as a full type.
465 procedure Decimal_Fixed_Point_Type_Declaration
466 (T : Entity_Id;
467 Def : Node_Id);
468 -- Create a new decimal fixed point type, and apply the constraint to
469 -- obtain a subtype of this new type.
471 procedure Complete_Private_Subtype
472 (Priv : Entity_Id;
473 Full : Entity_Id;
474 Full_Base : Entity_Id;
475 Related_Nod : Node_Id);
476 -- Complete the implicit full view of a private subtype by setting the
477 -- appropriate semantic fields. If the full view of the parent is a record
478 -- type, build constrained components of subtype.
480 procedure Derive_Progenitor_Subprograms
481 (Parent_Type : Entity_Id;
482 Tagged_Type : Entity_Id);
483 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
484 -- operations of progenitors of Tagged_Type, and replace the subsidiary
485 -- subtypes with Tagged_Type, to build the specs of the inherited interface
486 -- primitives. The derived primitives are aliased to those of the
487 -- interface. This routine takes care also of transferring to the full-view
488 -- subprograms associated with the partial-view of Tagged_Type that cover
489 -- interface primitives.
491 procedure Derived_Standard_Character
492 (N : Node_Id;
493 Parent_Type : Entity_Id;
494 Derived_Type : Entity_Id);
495 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
496 -- derivations from types Standard.Character and Standard.Wide_Character.
498 procedure Derived_Type_Declaration
499 (T : Entity_Id;
500 N : Node_Id;
501 Is_Completion : Boolean);
502 -- Process a derived type declaration. Build_Derived_Type is invoked
503 -- to process the actual derived type definition. Parameters N and
504 -- Is_Completion have the same meaning as in Build_Derived_Type.
505 -- T is the N_Defining_Identifier for the entity defined in the
506 -- N_Full_Type_Declaration node N, that is T is the derived type.
508 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
509 -- Insert each literal in symbol table, as an overloadable identifier. Each
510 -- enumeration type is mapped into a sequence of integers, and each literal
511 -- is defined as a constant with integer value. If any of the literals are
512 -- character literals, the type is a character type, which means that
513 -- strings are legal aggregates for arrays of components of the type.
515 function Expand_To_Stored_Constraint
516 (Typ : Entity_Id;
517 Constraint : Elist_Id) return Elist_Id;
518 -- Given a constraint (i.e. a list of expressions) on the discriminants of
519 -- Typ, expand it into a constraint on the stored discriminants and return
520 -- the new list of expressions constraining the stored discriminants.
522 function Find_Type_Of_Object
523 (Obj_Def : Node_Id;
524 Related_Nod : Node_Id) return Entity_Id;
525 -- Get type entity for object referenced by Obj_Def, attaching the
526 -- implicit types generated to Related_Nod
528 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
529 -- Create a new float and apply the constraint to obtain subtype of it
531 function Has_Range_Constraint (N : Node_Id) return Boolean;
532 -- Given an N_Subtype_Indication node N, return True if a range constraint
533 -- is present, either directly, or as part of a digits or delta constraint.
534 -- In addition, a digits constraint in the decimal case returns True, since
535 -- it establishes a default range if no explicit range is present.
537 function Inherit_Components
538 (N : Node_Id;
539 Parent_Base : Entity_Id;
540 Derived_Base : Entity_Id;
541 Is_Tagged : Boolean;
542 Inherit_Discr : Boolean;
543 Discs : Elist_Id) return Elist_Id;
544 -- Called from Build_Derived_Record_Type to inherit the components of
545 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
546 -- For more information on derived types and component inheritance please
547 -- consult the comment above the body of Build_Derived_Record_Type.
549 -- N is the original derived type declaration
551 -- Is_Tagged is set if we are dealing with tagged types
553 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
554 -- Parent_Base, otherwise no discriminants are inherited.
556 -- Discs gives the list of constraints that apply to Parent_Base in the
557 -- derived type declaration. If Discs is set to No_Elist, then we have
558 -- the following situation:
560 -- type Parent (D1..Dn : ..) is [tagged] record ...;
561 -- type Derived is new Parent [with ...];
563 -- which gets treated as
565 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
567 -- For untagged types the returned value is an association list. The list
568 -- starts from the association (Parent_Base => Derived_Base), and then it
569 -- contains a sequence of the associations of the form
571 -- (Old_Component => New_Component),
573 -- where Old_Component is the Entity_Id of a component in Parent_Base and
574 -- New_Component is the Entity_Id of the corresponding component in
575 -- Derived_Base. For untagged records, this association list is needed when
576 -- copying the record declaration for the derived base. In the tagged case
577 -- the value returned is irrelevant.
579 function Is_Valid_Constraint_Kind
580 (T_Kind : Type_Kind;
581 Constraint_Kind : Node_Kind) return Boolean;
582 -- Returns True if it is legal to apply the given kind of constraint to the
583 -- given kind of type (index constraint to an array type, for example).
585 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
586 -- Create new modular type. Verify that modulus is in bounds and is
587 -- a power of two (implementation restriction).
589 procedure New_Concatenation_Op (Typ : Entity_Id);
590 -- Create an abbreviated declaration for an operator in order to
591 -- materialize concatenation on array types.
593 procedure Ordinary_Fixed_Point_Type_Declaration
594 (T : Entity_Id;
595 Def : Node_Id);
596 -- Create a new ordinary fixed point type, and apply the constraint to
597 -- obtain subtype of it.
599 procedure Prepare_Private_Subtype_Completion
600 (Id : Entity_Id;
601 Related_Nod : Node_Id);
602 -- Id is a subtype of some private type. Creates the full declaration
603 -- associated with Id whenever possible, i.e. when the full declaration
604 -- of the base type is already known. Records each subtype into
605 -- Private_Dependents of the base type.
607 procedure Process_Incomplete_Dependents
608 (N : Node_Id;
609 Full_T : Entity_Id;
610 Inc_T : Entity_Id);
611 -- Process all entities that depend on an incomplete type. There include
612 -- subtypes, subprogram types that mention the incomplete type in their
613 -- profiles, and subprogram with access parameters that designate the
614 -- incomplete type.
616 -- Inc_T is the defining identifier of an incomplete type declaration, its
617 -- Ekind is E_Incomplete_Type.
619 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
621 -- Full_T is N's defining identifier.
623 -- Subtypes of incomplete types with discriminants are completed when the
624 -- parent type is. This is simpler than private subtypes, because they can
625 -- only appear in the same scope, and there is no need to exchange views.
626 -- Similarly, access_to_subprogram types may have a parameter or a return
627 -- type that is an incomplete type, and that must be replaced with the
628 -- full type.
630 -- If the full type is tagged, subprogram with access parameters that
631 -- designated the incomplete may be primitive operations of the full type,
632 -- and have to be processed accordingly.
634 procedure Process_Real_Range_Specification (Def : Node_Id);
635 -- Given the type definition for a real type, this procedure processes and
636 -- checks the real range specification of this type definition if one is
637 -- present. If errors are found, error messages are posted, and the
638 -- Real_Range_Specification of Def is reset to Empty.
640 procedure Record_Type_Declaration
641 (T : Entity_Id;
642 N : Node_Id;
643 Prev : Entity_Id);
644 -- Process a record type declaration (for both untagged and tagged
645 -- records). Parameters T and N are exactly like in procedure
646 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
647 -- for this routine. If this is the completion of an incomplete type
648 -- declaration, Prev is the entity of the incomplete declaration, used for
649 -- cross-referencing. Otherwise Prev = T.
651 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
652 -- This routine is used to process the actual record type definition (both
653 -- for untagged and tagged records). Def is a record type definition node.
654 -- This procedure analyzes the components in this record type definition.
655 -- Prev_T is the entity for the enclosing record type. It is provided so
656 -- that its Has_Task flag can be set if any of the component have Has_Task
657 -- set. If the declaration is the completion of an incomplete type
658 -- declaration, Prev_T is the original incomplete type, whose full view is
659 -- the record type.
661 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
662 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
663 -- build a copy of the declaration tree of the parent, and we create
664 -- independently the list of components for the derived type. Semantic
665 -- information uses the component entities, but record representation
666 -- clauses are validated on the declaration tree. This procedure replaces
667 -- discriminants and components in the declaration with those that have
668 -- been created by Inherit_Components.
670 procedure Set_Fixed_Range
671 (E : Entity_Id;
672 Loc : Source_Ptr;
673 Lo : Ureal;
674 Hi : Ureal);
675 -- Build a range node with the given bounds and set it as the Scalar_Range
676 -- of the given fixed-point type entity. Loc is the source location used
677 -- for the constructed range. See body for further details.
679 procedure Set_Scalar_Range_For_Subtype
680 (Def_Id : Entity_Id;
681 R : Node_Id;
682 Subt : Entity_Id);
683 -- This routine is used to set the scalar range field for a subtype given
684 -- Def_Id, the entity for the subtype, and R, the range expression for the
685 -- scalar range. Subt provides the parent subtype to be used to analyze,
686 -- resolve, and check the given range.
688 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
689 -- Create a new signed integer entity, and apply the constraint to obtain
690 -- the required first named subtype of this type.
692 procedure Set_Stored_Constraint_From_Discriminant_Constraint
693 (E : Entity_Id);
694 -- E is some record type. This routine computes E's Stored_Constraint
695 -- from its Discriminant_Constraint.
697 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
698 -- Check that an entity in a list of progenitors is an interface,
699 -- emit error otherwise.
701 -----------------------
702 -- Access_Definition --
703 -----------------------
705 function Access_Definition
706 (Related_Nod : Node_Id;
707 N : Node_Id) return Entity_Id
709 Loc : constant Source_Ptr := Sloc (Related_Nod);
710 Anon_Type : Entity_Id;
711 Anon_Scope : Entity_Id;
712 Desig_Type : Entity_Id;
713 Decl : Entity_Id;
714 Enclosing_Prot_Type : Entity_Id := Empty;
716 begin
717 if Is_Entry (Current_Scope)
718 and then Is_Task_Type (Etype (Scope (Current_Scope)))
719 then
720 Error_Msg_N ("task entries cannot have access parameters", N);
721 return Empty;
722 end if;
724 -- Ada 2005: for an object declaration the corresponding anonymous
725 -- type is declared in the current scope.
727 -- If the access definition is the return type of another access to
728 -- function, scope is the current one, because it is the one of the
729 -- current type declaration.
731 if Nkind_In (Related_Nod, N_Object_Declaration,
732 N_Access_Function_Definition)
733 then
734 Anon_Scope := Current_Scope;
736 -- For the anonymous function result case, retrieve the scope of the
737 -- function specification's associated entity rather than using the
738 -- current scope. The current scope will be the function itself if the
739 -- formal part is currently being analyzed, but will be the parent scope
740 -- in the case of a parameterless function, and we always want to use
741 -- the function's parent scope. Finally, if the function is a child
742 -- unit, we must traverse the tree to retrieve the proper entity.
744 elsif Nkind (Related_Nod) = N_Function_Specification
745 and then Nkind (Parent (N)) /= N_Parameter_Specification
746 then
747 -- If the current scope is a protected type, the anonymous access
748 -- is associated with one of the protected operations, and must
749 -- be available in the scope that encloses the protected declaration.
750 -- Otherwise the type is in the scope enclosing the subprogram.
752 -- If the function has formals, The return type of a subprogram
753 -- declaration is analyzed in the scope of the subprogram (see
754 -- Process_Formals) and thus the protected type, if present, is
755 -- the scope of the current function scope.
757 if Ekind (Current_Scope) = E_Protected_Type then
758 Enclosing_Prot_Type := Current_Scope;
760 elsif Ekind (Current_Scope) = E_Function
761 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
762 then
763 Enclosing_Prot_Type := Scope (Current_Scope);
764 end if;
766 if Present (Enclosing_Prot_Type) then
767 Anon_Scope := Scope (Enclosing_Prot_Type);
769 else
770 Anon_Scope := Scope (Defining_Entity (Related_Nod));
771 end if;
773 else
774 -- For access formals, access components, and access discriminants,
775 -- the scope is that of the enclosing declaration,
777 Anon_Scope := Scope (Current_Scope);
778 end if;
780 Anon_Type :=
781 Create_Itype
782 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
784 if All_Present (N)
785 and then Ada_Version >= Ada_2005
786 then
787 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
788 end if;
790 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
791 -- the corresponding semantic routine
793 if Present (Access_To_Subprogram_Definition (N)) then
794 Access_Subprogram_Declaration
795 (T_Name => Anon_Type,
796 T_Def => Access_To_Subprogram_Definition (N));
798 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
799 Set_Ekind
800 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
801 else
802 Set_Ekind
803 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
804 end if;
806 Set_Can_Use_Internal_Rep
807 (Anon_Type, not Always_Compatible_Rep_On_Target);
809 -- If the anonymous access is associated with a protected operation
810 -- create a reference to it after the enclosing protected definition
811 -- because the itype will be used in the subsequent bodies.
813 if Ekind (Current_Scope) = E_Protected_Type then
814 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
815 end if;
817 return Anon_Type;
818 end if;
820 Find_Type (Subtype_Mark (N));
821 Desig_Type := Entity (Subtype_Mark (N));
823 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
824 Set_Etype (Anon_Type, Anon_Type);
826 -- Make sure the anonymous access type has size and alignment fields
827 -- set, as required by gigi. This is necessary in the case of the
828 -- Task_Body_Procedure.
830 if not Has_Private_Component (Desig_Type) then
831 Layout_Type (Anon_Type);
832 end if;
834 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
835 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
836 -- the null value is allowed. In Ada 95 the null value is never allowed.
838 if Ada_Version >= Ada_2005 then
839 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
840 else
841 Set_Can_Never_Be_Null (Anon_Type, True);
842 end if;
844 -- The anonymous access type is as public as the discriminated type or
845 -- subprogram that defines it. It is imported (for back-end purposes)
846 -- if the designated type is.
848 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
850 -- Ada 2005 (AI-231): Propagate the access-constant attribute
852 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
854 -- The context is either a subprogram declaration, object declaration,
855 -- or an access discriminant, in a private or a full type declaration.
856 -- In the case of a subprogram, if the designated type is incomplete,
857 -- the operation will be a primitive operation of the full type, to be
858 -- updated subsequently. If the type is imported through a limited_with
859 -- clause, the subprogram is not a primitive operation of the type
860 -- (which is declared elsewhere in some other scope).
862 if Ekind (Desig_Type) = E_Incomplete_Type
863 and then not From_With_Type (Desig_Type)
864 and then Is_Overloadable (Current_Scope)
865 then
866 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
867 Set_Has_Delayed_Freeze (Current_Scope);
868 end if;
870 -- Ada 2005: if the designated type is an interface that may contain
871 -- tasks, create a Master entity for the declaration. This must be done
872 -- before expansion of the full declaration, because the declaration may
873 -- include an expression that is an allocator, whose expansion needs the
874 -- proper Master for the created tasks.
876 if Nkind (Related_Nod) = N_Object_Declaration
877 and then Expander_Active
878 then
879 if Is_Interface (Desig_Type)
880 and then Is_Limited_Record (Desig_Type)
881 then
882 Build_Class_Wide_Master (Anon_Type);
884 -- Similarly, if the type is an anonymous access that designates
885 -- tasks, create a master entity for it in the current context.
887 elsif Has_Task (Desig_Type)
888 and then Comes_From_Source (Related_Nod)
889 and then not Restriction_Active (No_Task_Hierarchy)
890 then
891 if not Has_Master_Entity (Current_Scope) then
892 Decl :=
893 Make_Object_Declaration (Loc,
894 Defining_Identifier =>
895 Make_Defining_Identifier (Loc, Name_uMaster),
896 Constant_Present => True,
897 Object_Definition =>
898 New_Reference_To (RTE (RE_Master_Id), Loc),
899 Expression =>
900 Make_Explicit_Dereference (Loc,
901 New_Reference_To (RTE (RE_Current_Master), Loc)));
903 Insert_Before (Related_Nod, Decl);
904 Analyze (Decl);
906 Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
907 Set_Has_Master_Entity (Current_Scope);
908 else
909 Build_Master_Renaming (Related_Nod, Anon_Type);
910 end if;
911 end if;
912 end if;
914 -- For a private component of a protected type, it is imperative that
915 -- the back-end elaborate the type immediately after the protected
916 -- declaration, because this type will be used in the declarations
917 -- created for the component within each protected body, so we must
918 -- create an itype reference for it now.
920 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
921 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
923 -- Similarly, if the access definition is the return result of a
924 -- function, create an itype reference for it because it will be used
925 -- within the function body. For a regular function that is not a
926 -- compilation unit, insert reference after the declaration. For a
927 -- protected operation, insert it after the enclosing protected type
928 -- declaration. In either case, do not create a reference for a type
929 -- obtained through a limited_with clause, because this would introduce
930 -- semantic dependencies.
932 -- Similarly, do not create a reference if the designated type is a
933 -- generic formal, because no use of it will reach the backend.
935 elsif Nkind (Related_Nod) = N_Function_Specification
936 and then not From_With_Type (Desig_Type)
937 and then not Is_Generic_Type (Desig_Type)
938 then
939 if Present (Enclosing_Prot_Type) then
940 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
942 elsif Is_List_Member (Parent (Related_Nod))
943 and then Nkind (Parent (N)) /= N_Parameter_Specification
944 then
945 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
946 end if;
948 -- Finally, create an itype reference for an object declaration of an
949 -- anonymous access type. This is strictly necessary only for deferred
950 -- constants, but in any case will avoid out-of-scope problems in the
951 -- back-end.
953 elsif Nkind (Related_Nod) = N_Object_Declaration then
954 Build_Itype_Reference (Anon_Type, Related_Nod);
955 end if;
957 return Anon_Type;
958 end Access_Definition;
960 -----------------------------------
961 -- Access_Subprogram_Declaration --
962 -----------------------------------
964 procedure Access_Subprogram_Declaration
965 (T_Name : Entity_Id;
966 T_Def : Node_Id)
969 procedure Check_For_Premature_Usage (Def : Node_Id);
970 -- Check that type T_Name is not used, directly or recursively, as a
971 -- parameter or a return type in Def. Def is either a subtype, an
972 -- access_definition, or an access_to_subprogram_definition.
974 -------------------------------
975 -- Check_For_Premature_Usage --
976 -------------------------------
978 procedure Check_For_Premature_Usage (Def : Node_Id) is
979 Param : Node_Id;
981 begin
982 -- Check for a subtype mark
984 if Nkind (Def) in N_Has_Etype then
985 if Etype (Def) = T_Name then
986 Error_Msg_N
987 ("type& cannot be used before end of its declaration", Def);
988 end if;
990 -- If this is not a subtype, then this is an access_definition
992 elsif Nkind (Def) = N_Access_Definition then
993 if Present (Access_To_Subprogram_Definition (Def)) then
994 Check_For_Premature_Usage
995 (Access_To_Subprogram_Definition (Def));
996 else
997 Check_For_Premature_Usage (Subtype_Mark (Def));
998 end if;
1000 -- The only cases left are N_Access_Function_Definition and
1001 -- N_Access_Procedure_Definition.
1003 else
1004 if Present (Parameter_Specifications (Def)) then
1005 Param := First (Parameter_Specifications (Def));
1006 while Present (Param) loop
1007 Check_For_Premature_Usage (Parameter_Type (Param));
1008 Param := Next (Param);
1009 end loop;
1010 end if;
1012 if Nkind (Def) = N_Access_Function_Definition then
1013 Check_For_Premature_Usage (Result_Definition (Def));
1014 end if;
1015 end if;
1016 end Check_For_Premature_Usage;
1018 -- Local variables
1020 Formals : constant List_Id := Parameter_Specifications (T_Def);
1021 Formal : Entity_Id;
1022 D_Ityp : Node_Id;
1023 Desig_Type : constant Entity_Id :=
1024 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1026 -- Start of processing for Access_Subprogram_Declaration
1028 begin
1029 -- Associate the Itype node with the inner full-type declaration or
1030 -- subprogram spec or entry body. This is required to handle nested
1031 -- anonymous declarations. For example:
1033 -- procedure P
1034 -- (X : access procedure
1035 -- (Y : access procedure
1036 -- (Z : access T)))
1038 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1039 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1040 N_Private_Type_Declaration,
1041 N_Private_Extension_Declaration,
1042 N_Procedure_Specification,
1043 N_Function_Specification,
1044 N_Entry_Body)
1046 or else
1047 Nkind_In (D_Ityp, N_Object_Declaration,
1048 N_Object_Renaming_Declaration,
1049 N_Formal_Object_Declaration,
1050 N_Formal_Type_Declaration,
1051 N_Task_Type_Declaration,
1052 N_Protected_Type_Declaration))
1053 loop
1054 D_Ityp := Parent (D_Ityp);
1055 pragma Assert (D_Ityp /= Empty);
1056 end loop;
1058 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1060 if Nkind_In (D_Ityp, N_Procedure_Specification,
1061 N_Function_Specification)
1062 then
1063 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1065 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1066 N_Object_Declaration,
1067 N_Object_Renaming_Declaration,
1068 N_Formal_Type_Declaration)
1069 then
1070 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1071 end if;
1073 if Nkind (T_Def) = N_Access_Function_Definition then
1074 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1075 declare
1076 Acc : constant Node_Id := Result_Definition (T_Def);
1078 begin
1079 if Present (Access_To_Subprogram_Definition (Acc))
1080 and then
1081 Protected_Present (Access_To_Subprogram_Definition (Acc))
1082 then
1083 Set_Etype
1084 (Desig_Type,
1085 Replace_Anonymous_Access_To_Protected_Subprogram
1086 (T_Def));
1088 else
1089 Set_Etype
1090 (Desig_Type,
1091 Access_Definition (T_Def, Result_Definition (T_Def)));
1092 end if;
1093 end;
1095 else
1096 Analyze (Result_Definition (T_Def));
1098 declare
1099 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1101 begin
1102 -- If a null exclusion is imposed on the result type, then
1103 -- create a null-excluding itype (an access subtype) and use
1104 -- it as the function's Etype.
1106 if Is_Access_Type (Typ)
1107 and then Null_Exclusion_In_Return_Present (T_Def)
1108 then
1109 Set_Etype (Desig_Type,
1110 Create_Null_Excluding_Itype
1111 (T => Typ,
1112 Related_Nod => T_Def,
1113 Scope_Id => Current_Scope));
1115 else
1116 if From_With_Type (Typ) then
1118 -- AI05-151: Incomplete types are allowed in all basic
1119 -- declarations, including access to subprograms.
1121 if Ada_Version >= Ada_2012 then
1122 null;
1124 else
1125 Error_Msg_NE
1126 ("illegal use of incomplete type&",
1127 Result_Definition (T_Def), Typ);
1128 end if;
1130 elsif Ekind (Current_Scope) = E_Package
1131 and then In_Private_Part (Current_Scope)
1132 then
1133 if Ekind (Typ) = E_Incomplete_Type then
1134 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1136 elsif Is_Class_Wide_Type (Typ)
1137 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1138 then
1139 Append_Elmt
1140 (Desig_Type, Private_Dependents (Etype (Typ)));
1141 end if;
1142 end if;
1144 Set_Etype (Desig_Type, Typ);
1145 end if;
1146 end;
1147 end if;
1149 if not (Is_Type (Etype (Desig_Type))) then
1150 Error_Msg_N
1151 ("expect type in function specification",
1152 Result_Definition (T_Def));
1153 end if;
1155 else
1156 Set_Etype (Desig_Type, Standard_Void_Type);
1157 end if;
1159 if Present (Formals) then
1160 Push_Scope (Desig_Type);
1162 -- A bit of a kludge here. These kludges will be removed when Itypes
1163 -- have proper parent pointers to their declarations???
1165 -- Kludge 1) Link defining_identifier of formals. Required by
1166 -- First_Formal to provide its functionality.
1168 declare
1169 F : Node_Id;
1171 begin
1172 F := First (Formals);
1173 while Present (F) loop
1174 if No (Parent (Defining_Identifier (F))) then
1175 Set_Parent (Defining_Identifier (F), F);
1176 end if;
1178 Next (F);
1179 end loop;
1180 end;
1182 Process_Formals (Formals, Parent (T_Def));
1184 -- Kludge 2) End_Scope requires that the parent pointer be set to
1185 -- something reasonable, but Itypes don't have parent pointers. So
1186 -- we set it and then unset it ???
1188 Set_Parent (Desig_Type, T_Name);
1189 End_Scope;
1190 Set_Parent (Desig_Type, Empty);
1191 end if;
1193 -- Check for premature usage of the type being defined
1195 Check_For_Premature_Usage (T_Def);
1197 -- The return type and/or any parameter type may be incomplete. Mark
1198 -- the subprogram_type as depending on the incomplete type, so that
1199 -- it can be updated when the full type declaration is seen. This
1200 -- only applies to incomplete types declared in some enclosing scope,
1201 -- not to limited views from other packages.
1203 if Present (Formals) then
1204 Formal := First_Formal (Desig_Type);
1205 while Present (Formal) loop
1206 if Ekind (Formal) /= E_In_Parameter
1207 and then Nkind (T_Def) = N_Access_Function_Definition
1208 then
1209 Error_Msg_N ("functions can only have IN parameters", Formal);
1210 end if;
1212 if Ekind (Etype (Formal)) = E_Incomplete_Type
1213 and then In_Open_Scopes (Scope (Etype (Formal)))
1214 then
1215 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1216 Set_Has_Delayed_Freeze (Desig_Type);
1217 end if;
1219 Next_Formal (Formal);
1220 end loop;
1221 end if;
1223 -- If the return type is incomplete, this is legal as long as the
1224 -- type is declared in the current scope and will be completed in
1225 -- it (rather than being part of limited view).
1227 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1228 and then not Has_Delayed_Freeze (Desig_Type)
1229 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1230 then
1231 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1232 Set_Has_Delayed_Freeze (Desig_Type);
1233 end if;
1235 Check_Delayed_Subprogram (Desig_Type);
1237 if Protected_Present (T_Def) then
1238 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1239 Set_Convention (Desig_Type, Convention_Protected);
1240 else
1241 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1242 end if;
1244 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1246 Set_Etype (T_Name, T_Name);
1247 Init_Size_Align (T_Name);
1248 Set_Directly_Designated_Type (T_Name, Desig_Type);
1250 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1252 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1254 Check_Restriction (No_Access_Subprograms, T_Def);
1255 end Access_Subprogram_Declaration;
1257 ----------------------------
1258 -- Access_Type_Declaration --
1259 ----------------------------
1261 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1262 S : constant Node_Id := Subtype_Indication (Def);
1263 P : constant Node_Id := Parent (Def);
1264 begin
1265 -- Check for permissible use of incomplete type
1267 if Nkind (S) /= N_Subtype_Indication then
1268 Analyze (S);
1270 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1271 Set_Directly_Designated_Type (T, Entity (S));
1272 else
1273 Set_Directly_Designated_Type (T,
1274 Process_Subtype (S, P, T, 'P'));
1275 end if;
1277 else
1278 Set_Directly_Designated_Type (T,
1279 Process_Subtype (S, P, T, 'P'));
1280 end if;
1282 if All_Present (Def) or Constant_Present (Def) then
1283 Set_Ekind (T, E_General_Access_Type);
1284 else
1285 Set_Ekind (T, E_Access_Type);
1286 end if;
1288 if Base_Type (Designated_Type (T)) = T then
1289 Error_Msg_N ("access type cannot designate itself", S);
1291 -- In Ada 2005, the type may have a limited view through some unit
1292 -- in its own context, allowing the following circularity that cannot
1293 -- be detected earlier
1295 elsif Is_Class_Wide_Type (Designated_Type (T))
1296 and then Etype (Designated_Type (T)) = T
1297 then
1298 Error_Msg_N
1299 ("access type cannot designate its own classwide type", S);
1301 -- Clean up indication of tagged status to prevent cascaded errors
1303 Set_Is_Tagged_Type (T, False);
1304 end if;
1306 Set_Etype (T, T);
1308 -- If the type has appeared already in a with_type clause, it is
1309 -- frozen and the pointer size is already set. Else, initialize.
1311 if not From_With_Type (T) then
1312 Init_Size_Align (T);
1313 end if;
1315 -- Note that Has_Task is always false, since the access type itself
1316 -- is not a task type. See Einfo for more description on this point.
1317 -- Exactly the same consideration applies to Has_Controlled_Component.
1319 Set_Has_Task (T, False);
1320 Set_Has_Controlled_Component (T, False);
1322 -- Initialize Associated_Final_Chain explicitly to Empty, to avoid
1323 -- problems where an incomplete view of this entity has been previously
1324 -- established by a limited with and an overlaid version of this field
1325 -- (Stored_Constraint) was initialized for the incomplete view.
1327 Set_Associated_Final_Chain (T, Empty);
1329 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1330 -- attributes
1332 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1333 Set_Is_Access_Constant (T, Constant_Present (Def));
1334 end Access_Type_Declaration;
1336 ----------------------------------
1337 -- Add_Interface_Tag_Components --
1338 ----------------------------------
1340 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1341 Loc : constant Source_Ptr := Sloc (N);
1342 L : List_Id;
1343 Last_Tag : Node_Id;
1345 procedure Add_Tag (Iface : Entity_Id);
1346 -- Add tag for one of the progenitor interfaces
1348 -------------
1349 -- Add_Tag --
1350 -------------
1352 procedure Add_Tag (Iface : Entity_Id) is
1353 Decl : Node_Id;
1354 Def : Node_Id;
1355 Tag : Entity_Id;
1356 Offset : Entity_Id;
1358 begin
1359 pragma Assert (Is_Tagged_Type (Iface)
1360 and then Is_Interface (Iface));
1362 Def :=
1363 Make_Component_Definition (Loc,
1364 Aliased_Present => True,
1365 Subtype_Indication =>
1366 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1368 Tag := Make_Temporary (Loc, 'V');
1370 Decl :=
1371 Make_Component_Declaration (Loc,
1372 Defining_Identifier => Tag,
1373 Component_Definition => Def);
1375 Analyze_Component_Declaration (Decl);
1377 Set_Analyzed (Decl);
1378 Set_Ekind (Tag, E_Component);
1379 Set_Is_Tag (Tag);
1380 Set_Is_Aliased (Tag);
1381 Set_Related_Type (Tag, Iface);
1382 Init_Component_Location (Tag);
1384 pragma Assert (Is_Frozen (Iface));
1386 Set_DT_Entry_Count (Tag,
1387 DT_Entry_Count (First_Entity (Iface)));
1389 if No (Last_Tag) then
1390 Prepend (Decl, L);
1391 else
1392 Insert_After (Last_Tag, Decl);
1393 end if;
1395 Last_Tag := Decl;
1397 -- If the ancestor has discriminants we need to give special support
1398 -- to store the offset_to_top value of the secondary dispatch tables.
1399 -- For this purpose we add a supplementary component just after the
1400 -- field that contains the tag associated with each secondary DT.
1402 if Typ /= Etype (Typ)
1403 and then Has_Discriminants (Etype (Typ))
1404 then
1405 Def :=
1406 Make_Component_Definition (Loc,
1407 Subtype_Indication =>
1408 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1410 Offset := Make_Temporary (Loc, 'V');
1412 Decl :=
1413 Make_Component_Declaration (Loc,
1414 Defining_Identifier => Offset,
1415 Component_Definition => Def);
1417 Analyze_Component_Declaration (Decl);
1419 Set_Analyzed (Decl);
1420 Set_Ekind (Offset, E_Component);
1421 Set_Is_Aliased (Offset);
1422 Set_Related_Type (Offset, Iface);
1423 Init_Component_Location (Offset);
1424 Insert_After (Last_Tag, Decl);
1425 Last_Tag := Decl;
1426 end if;
1427 end Add_Tag;
1429 -- Local variables
1431 Elmt : Elmt_Id;
1432 Ext : Node_Id;
1433 Comp : Node_Id;
1435 -- Start of processing for Add_Interface_Tag_Components
1437 begin
1438 if not RTE_Available (RE_Interface_Tag) then
1439 Error_Msg
1440 ("(Ada 2005) interface types not supported by this run-time!",
1441 Sloc (N));
1442 return;
1443 end if;
1445 if Ekind (Typ) /= E_Record_Type
1446 or else (Is_Concurrent_Record_Type (Typ)
1447 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1448 or else (not Is_Concurrent_Record_Type (Typ)
1449 and then No (Interfaces (Typ))
1450 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1451 then
1452 return;
1453 end if;
1455 -- Find the current last tag
1457 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1458 Ext := Record_Extension_Part (Type_Definition (N));
1459 else
1460 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1461 Ext := Type_Definition (N);
1462 end if;
1464 Last_Tag := Empty;
1466 if not (Present (Component_List (Ext))) then
1467 Set_Null_Present (Ext, False);
1468 L := New_List;
1469 Set_Component_List (Ext,
1470 Make_Component_List (Loc,
1471 Component_Items => L,
1472 Null_Present => False));
1473 else
1474 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1475 L := Component_Items
1476 (Component_List
1477 (Record_Extension_Part
1478 (Type_Definition (N))));
1479 else
1480 L := Component_Items
1481 (Component_List
1482 (Type_Definition (N)));
1483 end if;
1485 -- Find the last tag component
1487 Comp := First (L);
1488 while Present (Comp) loop
1489 if Nkind (Comp) = N_Component_Declaration
1490 and then Is_Tag (Defining_Identifier (Comp))
1491 then
1492 Last_Tag := Comp;
1493 end if;
1495 Next (Comp);
1496 end loop;
1497 end if;
1499 -- At this point L references the list of components and Last_Tag
1500 -- references the current last tag (if any). Now we add the tag
1501 -- corresponding with all the interfaces that are not implemented
1502 -- by the parent.
1504 if Present (Interfaces (Typ)) then
1505 Elmt := First_Elmt (Interfaces (Typ));
1506 while Present (Elmt) loop
1507 Add_Tag (Node (Elmt));
1508 Next_Elmt (Elmt);
1509 end loop;
1510 end if;
1511 end Add_Interface_Tag_Components;
1513 -------------------------------------
1514 -- Add_Internal_Interface_Entities --
1515 -------------------------------------
1517 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1518 Elmt : Elmt_Id;
1519 Iface : Entity_Id;
1520 Iface_Elmt : Elmt_Id;
1521 Iface_Prim : Entity_Id;
1522 Ifaces_List : Elist_Id;
1523 New_Subp : Entity_Id := Empty;
1524 Prim : Entity_Id;
1525 Restore_Scope : Boolean := False;
1527 begin
1528 pragma Assert (Ada_Version >= Ada_2005
1529 and then Is_Record_Type (Tagged_Type)
1530 and then Is_Tagged_Type (Tagged_Type)
1531 and then Has_Interfaces (Tagged_Type)
1532 and then not Is_Interface (Tagged_Type));
1534 -- Ensure that the internal entities are added to the scope of the type
1536 if Scope (Tagged_Type) /= Current_Scope then
1537 Push_Scope (Scope (Tagged_Type));
1538 Restore_Scope := True;
1539 end if;
1541 Collect_Interfaces (Tagged_Type, Ifaces_List);
1543 Iface_Elmt := First_Elmt (Ifaces_List);
1544 while Present (Iface_Elmt) loop
1545 Iface := Node (Iface_Elmt);
1547 -- Originally we excluded here from this processing interfaces that
1548 -- are parents of Tagged_Type because their primitives are located
1549 -- in the primary dispatch table (and hence no auxiliary internal
1550 -- entities are required to handle secondary dispatch tables in such
1551 -- case). However, these auxiliary entities are also required to
1552 -- handle derivations of interfaces in formals of generics (see
1553 -- Derive_Subprograms).
1555 Elmt := First_Elmt (Primitive_Operations (Iface));
1556 while Present (Elmt) loop
1557 Iface_Prim := Node (Elmt);
1559 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1560 Prim :=
1561 Find_Primitive_Covering_Interface
1562 (Tagged_Type => Tagged_Type,
1563 Iface_Prim => Iface_Prim);
1565 pragma Assert (Present (Prim));
1567 Derive_Subprogram
1568 (New_Subp => New_Subp,
1569 Parent_Subp => Iface_Prim,
1570 Derived_Type => Tagged_Type,
1571 Parent_Type => Iface);
1573 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1574 -- associated with interface types. These entities are
1575 -- only registered in the list of primitives of its
1576 -- corresponding tagged type because they are only used
1577 -- to fill the contents of the secondary dispatch tables.
1578 -- Therefore they are removed from the homonym chains.
1580 Set_Is_Hidden (New_Subp);
1581 Set_Is_Internal (New_Subp);
1582 Set_Alias (New_Subp, Prim);
1583 Set_Is_Abstract_Subprogram
1584 (New_Subp, Is_Abstract_Subprogram (Prim));
1585 Set_Interface_Alias (New_Subp, Iface_Prim);
1587 -- Internal entities associated with interface types are
1588 -- only registered in the list of primitives of the tagged
1589 -- type. They are only used to fill the contents of the
1590 -- secondary dispatch tables. Therefore they are not needed
1591 -- in the homonym chains.
1593 Remove_Homonym (New_Subp);
1595 -- Hidden entities associated with interfaces must have set
1596 -- the Has_Delay_Freeze attribute to ensure that, in case of
1597 -- locally defined tagged types (or compiling with static
1598 -- dispatch tables generation disabled) the corresponding
1599 -- entry of the secondary dispatch table is filled when
1600 -- such an entity is frozen.
1602 Set_Has_Delayed_Freeze (New_Subp);
1603 end if;
1605 Next_Elmt (Elmt);
1606 end loop;
1608 Next_Elmt (Iface_Elmt);
1609 end loop;
1611 if Restore_Scope then
1612 Pop_Scope;
1613 end if;
1614 end Add_Internal_Interface_Entities;
1616 -----------------------------------
1617 -- Analyze_Component_Declaration --
1618 -----------------------------------
1620 procedure Analyze_Component_Declaration (N : Node_Id) is
1621 Id : constant Entity_Id := Defining_Identifier (N);
1622 E : constant Node_Id := Expression (N);
1623 T : Entity_Id;
1624 P : Entity_Id;
1626 function Contains_POC (Constr : Node_Id) return Boolean;
1627 -- Determines whether a constraint uses the discriminant of a record
1628 -- type thus becoming a per-object constraint (POC).
1630 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1631 -- Typ is the type of the current component, check whether this type is
1632 -- a limited type. Used to validate declaration against that of
1633 -- enclosing record.
1635 ------------------
1636 -- Contains_POC --
1637 ------------------
1639 function Contains_POC (Constr : Node_Id) return Boolean is
1640 begin
1641 -- Prevent cascaded errors
1643 if Error_Posted (Constr) then
1644 return False;
1645 end if;
1647 case Nkind (Constr) is
1648 when N_Attribute_Reference =>
1649 return
1650 Attribute_Name (Constr) = Name_Access
1651 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1653 when N_Discriminant_Association =>
1654 return Denotes_Discriminant (Expression (Constr));
1656 when N_Identifier =>
1657 return Denotes_Discriminant (Constr);
1659 when N_Index_Or_Discriminant_Constraint =>
1660 declare
1661 IDC : Node_Id;
1663 begin
1664 IDC := First (Constraints (Constr));
1665 while Present (IDC) loop
1667 -- One per-object constraint is sufficient
1669 if Contains_POC (IDC) then
1670 return True;
1671 end if;
1673 Next (IDC);
1674 end loop;
1676 return False;
1677 end;
1679 when N_Range =>
1680 return Denotes_Discriminant (Low_Bound (Constr))
1681 or else
1682 Denotes_Discriminant (High_Bound (Constr));
1684 when N_Range_Constraint =>
1685 return Denotes_Discriminant (Range_Expression (Constr));
1687 when others =>
1688 return False;
1690 end case;
1691 end Contains_POC;
1693 ----------------------
1694 -- Is_Known_Limited --
1695 ----------------------
1697 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1698 P : constant Entity_Id := Etype (Typ);
1699 R : constant Entity_Id := Root_Type (Typ);
1701 begin
1702 if Is_Limited_Record (Typ) then
1703 return True;
1705 -- If the root type is limited (and not a limited interface)
1706 -- so is the current type
1708 elsif Is_Limited_Record (R)
1709 and then
1710 (not Is_Interface (R)
1711 or else not Is_Limited_Interface (R))
1712 then
1713 return True;
1715 -- Else the type may have a limited interface progenitor, but a
1716 -- limited record parent.
1718 elsif R /= P
1719 and then Is_Limited_Record (P)
1720 then
1721 return True;
1723 else
1724 return False;
1725 end if;
1726 end Is_Known_Limited;
1728 -- Start of processing for Analyze_Component_Declaration
1730 begin
1731 Generate_Definition (Id);
1732 Enter_Name (Id);
1734 if Present (Subtype_Indication (Component_Definition (N))) then
1735 T := Find_Type_Of_Object
1736 (Subtype_Indication (Component_Definition (N)), N);
1738 -- Ada 2005 (AI-230): Access Definition case
1740 else
1741 pragma Assert (Present
1742 (Access_Definition (Component_Definition (N))));
1744 T := Access_Definition
1745 (Related_Nod => N,
1746 N => Access_Definition (Component_Definition (N)));
1747 Set_Is_Local_Anonymous_Access (T);
1749 -- Ada 2005 (AI-254)
1751 if Present (Access_To_Subprogram_Definition
1752 (Access_Definition (Component_Definition (N))))
1753 and then Protected_Present (Access_To_Subprogram_Definition
1754 (Access_Definition
1755 (Component_Definition (N))))
1756 then
1757 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1758 end if;
1759 end if;
1761 -- If the subtype is a constrained subtype of the enclosing record,
1762 -- (which must have a partial view) the back-end does not properly
1763 -- handle the recursion. Rewrite the component declaration with an
1764 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1765 -- the tree directly because side effects have already been removed from
1766 -- discriminant constraints.
1768 if Ekind (T) = E_Access_Subtype
1769 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1770 and then Comes_From_Source (T)
1771 and then Nkind (Parent (T)) = N_Subtype_Declaration
1772 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1773 then
1774 Rewrite
1775 (Subtype_Indication (Component_Definition (N)),
1776 New_Copy_Tree (Subtype_Indication (Parent (T))));
1777 T := Find_Type_Of_Object
1778 (Subtype_Indication (Component_Definition (N)), N);
1779 end if;
1781 -- If the component declaration includes a default expression, then we
1782 -- check that the component is not of a limited type (RM 3.7(5)),
1783 -- and do the special preanalysis of the expression (see section on
1784 -- "Handling of Default and Per-Object Expressions" in the spec of
1785 -- package Sem).
1787 if Present (E) then
1788 Preanalyze_Spec_Expression (E, T);
1789 Check_Initialization (T, E);
1791 if Ada_Version >= Ada_2005
1792 and then Ekind (T) = E_Anonymous_Access_Type
1793 and then Etype (E) /= Any_Type
1794 then
1795 -- Check RM 3.9.2(9): "if the expected type for an expression is
1796 -- an anonymous access-to-specific tagged type, then the object
1797 -- designated by the expression shall not be dynamically tagged
1798 -- unless it is a controlling operand in a call on a dispatching
1799 -- operation"
1801 if Is_Tagged_Type (Directly_Designated_Type (T))
1802 and then
1803 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1804 and then
1805 Ekind (Directly_Designated_Type (Etype (E))) =
1806 E_Class_Wide_Type
1807 then
1808 Error_Msg_N
1809 ("access to specific tagged type required (RM 3.9.2(9))", E);
1810 end if;
1812 -- (Ada 2005: AI-230): Accessibility check for anonymous
1813 -- components
1815 if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1816 Error_Msg_N
1817 ("expression has deeper access level than component " &
1818 "(RM 3.10.2 (12.2))", E);
1819 end if;
1821 -- The initialization expression is a reference to an access
1822 -- discriminant. The type of the discriminant is always deeper
1823 -- than any access type.
1825 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1826 and then Is_Entity_Name (E)
1827 and then Ekind (Entity (E)) = E_In_Parameter
1828 and then Present (Discriminal_Link (Entity (E)))
1829 then
1830 Error_Msg_N
1831 ("discriminant has deeper accessibility level than target",
1833 end if;
1834 end if;
1835 end if;
1837 -- The parent type may be a private view with unknown discriminants,
1838 -- and thus unconstrained. Regular components must be constrained.
1840 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1841 if Is_Class_Wide_Type (T) then
1842 Error_Msg_N
1843 ("class-wide subtype with unknown discriminants" &
1844 " in component declaration",
1845 Subtype_Indication (Component_Definition (N)));
1846 else
1847 Error_Msg_N
1848 ("unconstrained subtype in component declaration",
1849 Subtype_Indication (Component_Definition (N)));
1850 end if;
1852 -- Components cannot be abstract, except for the special case of
1853 -- the _Parent field (case of extending an abstract tagged type)
1855 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1856 Error_Msg_N ("type of a component cannot be abstract", N);
1857 end if;
1859 Set_Etype (Id, T);
1860 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1862 -- The component declaration may have a per-object constraint, set
1863 -- the appropriate flag in the defining identifier of the subtype.
1865 if Present (Subtype_Indication (Component_Definition (N))) then
1866 declare
1867 Sindic : constant Node_Id :=
1868 Subtype_Indication (Component_Definition (N));
1869 begin
1870 if Nkind (Sindic) = N_Subtype_Indication
1871 and then Present (Constraint (Sindic))
1872 and then Contains_POC (Constraint (Sindic))
1873 then
1874 Set_Has_Per_Object_Constraint (Id);
1875 end if;
1876 end;
1877 end if;
1879 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1880 -- out some static checks.
1882 if Ada_Version >= Ada_2005
1883 and then Can_Never_Be_Null (T)
1884 then
1885 Null_Exclusion_Static_Checks (N);
1886 end if;
1888 -- If this component is private (or depends on a private type), flag the
1889 -- record type to indicate that some operations are not available.
1891 P := Private_Component (T);
1893 if Present (P) then
1895 -- Check for circular definitions
1897 if P = Any_Type then
1898 Set_Etype (Id, Any_Type);
1900 -- There is a gap in the visibility of operations only if the
1901 -- component type is not defined in the scope of the record type.
1903 elsif Scope (P) = Scope (Current_Scope) then
1904 null;
1906 elsif Is_Limited_Type (P) then
1907 Set_Is_Limited_Composite (Current_Scope);
1909 else
1910 Set_Is_Private_Composite (Current_Scope);
1911 end if;
1912 end if;
1914 if P /= Any_Type
1915 and then Is_Limited_Type (T)
1916 and then Chars (Id) /= Name_uParent
1917 and then Is_Tagged_Type (Current_Scope)
1918 then
1919 if Is_Derived_Type (Current_Scope)
1920 and then not Is_Known_Limited (Current_Scope)
1921 then
1922 Error_Msg_N
1923 ("extension of nonlimited type cannot have limited components",
1926 if Is_Interface (Root_Type (Current_Scope)) then
1927 Error_Msg_N
1928 ("\limitedness is not inherited from limited interface", N);
1929 Error_Msg_N ("\add LIMITED to type indication", N);
1930 end if;
1932 Explain_Limited_Type (T, N);
1933 Set_Etype (Id, Any_Type);
1934 Set_Is_Limited_Composite (Current_Scope, False);
1936 elsif not Is_Derived_Type (Current_Scope)
1937 and then not Is_Limited_Record (Current_Scope)
1938 and then not Is_Concurrent_Type (Current_Scope)
1939 then
1940 Error_Msg_N
1941 ("nonlimited tagged type cannot have limited components", N);
1942 Explain_Limited_Type (T, N);
1943 Set_Etype (Id, Any_Type);
1944 Set_Is_Limited_Composite (Current_Scope, False);
1945 end if;
1946 end if;
1948 Set_Original_Record_Component (Id, Id);
1949 Analyze_Aspect_Specifications (N, Id, Aspect_Specifications (N));
1950 end Analyze_Component_Declaration;
1952 --------------------------
1953 -- Analyze_Declarations --
1954 --------------------------
1956 procedure Analyze_Declarations (L : List_Id) is
1957 D : Node_Id;
1958 Freeze_From : Entity_Id := Empty;
1959 Next_Node : Node_Id;
1961 procedure Adjust_D;
1962 -- Adjust D not to include implicit label declarations, since these
1963 -- have strange Sloc values that result in elaboration check problems.
1964 -- (They have the sloc of the label as found in the source, and that
1965 -- is ahead of the current declarative part).
1967 --------------
1968 -- Adjust_D --
1969 --------------
1971 procedure Adjust_D is
1972 begin
1973 while Present (Prev (D))
1974 and then Nkind (D) = N_Implicit_Label_Declaration
1975 loop
1976 Prev (D);
1977 end loop;
1978 end Adjust_D;
1980 -- Start of processing for Analyze_Declarations
1982 begin
1983 D := First (L);
1984 while Present (D) loop
1986 -- Complete analysis of declaration
1988 Analyze (D);
1989 Next_Node := Next (D);
1991 if No (Freeze_From) then
1992 Freeze_From := First_Entity (Current_Scope);
1993 end if;
1995 -- At the end of a declarative part, freeze remaining entities
1996 -- declared in it. The end of the visible declarations of package
1997 -- specification is not the end of a declarative part if private
1998 -- declarations are present. The end of a package declaration is a
1999 -- freezing point only if it a library package. A task definition or
2000 -- protected type definition is not a freeze point either. Finally,
2001 -- we do not freeze entities in generic scopes, because there is no
2002 -- code generated for them and freeze nodes will be generated for
2003 -- the instance.
2005 -- The end of a package instantiation is not a freeze point, but
2006 -- for now we make it one, because the generic body is inserted
2007 -- (currently) immediately after. Generic instantiations will not
2008 -- be a freeze point once delayed freezing of bodies is implemented.
2009 -- (This is needed in any case for early instantiations ???).
2011 if No (Next_Node) then
2012 if Nkind_In (Parent (L), N_Component_List,
2013 N_Task_Definition,
2014 N_Protected_Definition)
2015 then
2016 null;
2018 elsif Nkind (Parent (L)) /= N_Package_Specification then
2019 if Nkind (Parent (L)) = N_Package_Body then
2020 Freeze_From := First_Entity (Current_Scope);
2021 end if;
2023 Adjust_D;
2024 Freeze_All (Freeze_From, D);
2025 Freeze_From := Last_Entity (Current_Scope);
2027 elsif Scope (Current_Scope) /= Standard_Standard
2028 and then not Is_Child_Unit (Current_Scope)
2029 and then No (Generic_Parent (Parent (L)))
2030 then
2031 null;
2033 elsif L /= Visible_Declarations (Parent (L))
2034 or else No (Private_Declarations (Parent (L)))
2035 or else Is_Empty_List (Private_Declarations (Parent (L)))
2036 then
2037 Adjust_D;
2038 Freeze_All (Freeze_From, D);
2039 Freeze_From := Last_Entity (Current_Scope);
2040 end if;
2042 -- If next node is a body then freeze all types before the body.
2043 -- An exception occurs for some expander-generated bodies. If these
2044 -- are generated at places where in general language rules would not
2045 -- allow a freeze point, then we assume that the expander has
2046 -- explicitly checked that all required types are properly frozen,
2047 -- and we do not cause general freezing here. This special circuit
2048 -- is used when the encountered body is marked as having already
2049 -- been analyzed.
2051 -- In all other cases (bodies that come from source, and expander
2052 -- generated bodies that have not been analyzed yet), freeze all
2053 -- types now. Note that in the latter case, the expander must take
2054 -- care to attach the bodies at a proper place in the tree so as to
2055 -- not cause unwanted freezing at that point.
2057 elsif not Analyzed (Next_Node)
2058 and then (Nkind_In (Next_Node, N_Subprogram_Body,
2059 N_Entry_Body,
2060 N_Package_Body,
2061 N_Protected_Body,
2062 N_Task_Body)
2063 or else
2064 Nkind (Next_Node) in N_Body_Stub)
2065 then
2066 Adjust_D;
2067 Freeze_All (Freeze_From, D);
2068 Freeze_From := Last_Entity (Current_Scope);
2069 end if;
2071 D := Next_Node;
2072 end loop;
2074 -- One more thing to do, we need to scan the declarations to check
2075 -- for any precondition/postcondition pragmas (Pre/Post aspects have
2076 -- by this stage been converted into corresponding pragmas). It is
2077 -- at this point that we analyze the expressions in such pragmas,
2078 -- to implement the delayed visibility requirement.
2080 declare
2081 Decl : Node_Id;
2082 Spec : Node_Id;
2083 Sent : Entity_Id;
2084 Prag : Node_Id;
2086 begin
2087 Decl := First (L);
2088 while Present (Decl) loop
2089 if Nkind (Original_Node (Decl)) = N_Subprogram_Declaration then
2090 Spec := Specification (Original_Node (Decl));
2091 Sent := Defining_Unit_Name (Spec);
2092 Prag := Spec_PPC_List (Sent);
2093 while Present (Prag) loop
2094 Analyze_PPC_In_Decl_Part (Prag, Sent);
2095 Prag := Next_Pragma (Prag);
2096 end loop;
2097 end if;
2099 Next (Decl);
2100 end loop;
2101 end;
2102 end Analyze_Declarations;
2104 -----------------------------------
2105 -- Analyze_Full_Type_Declaration --
2106 -----------------------------------
2108 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2109 Def : constant Node_Id := Type_Definition (N);
2110 Def_Id : constant Entity_Id := Defining_Identifier (N);
2111 T : Entity_Id;
2112 Prev : Entity_Id;
2114 Is_Remote : constant Boolean :=
2115 (Is_Remote_Types (Current_Scope)
2116 or else Is_Remote_Call_Interface (Current_Scope))
2117 and then not (In_Private_Part (Current_Scope)
2118 or else In_Package_Body (Current_Scope));
2120 procedure Check_Ops_From_Incomplete_Type;
2121 -- If there is a tagged incomplete partial view of the type, transfer
2122 -- its operations to the full view, and indicate that the type of the
2123 -- controlling parameter (s) is this full view.
2125 ------------------------------------
2126 -- Check_Ops_From_Incomplete_Type --
2127 ------------------------------------
2129 procedure Check_Ops_From_Incomplete_Type is
2130 Elmt : Elmt_Id;
2131 Formal : Entity_Id;
2132 Op : Entity_Id;
2134 begin
2135 if Prev /= T
2136 and then Ekind (Prev) = E_Incomplete_Type
2137 and then Is_Tagged_Type (Prev)
2138 and then Is_Tagged_Type (T)
2139 then
2140 Elmt := First_Elmt (Primitive_Operations (Prev));
2141 while Present (Elmt) loop
2142 Op := Node (Elmt);
2143 Prepend_Elmt (Op, Primitive_Operations (T));
2145 Formal := First_Formal (Op);
2146 while Present (Formal) loop
2147 if Etype (Formal) = Prev then
2148 Set_Etype (Formal, T);
2149 end if;
2151 Next_Formal (Formal);
2152 end loop;
2154 if Etype (Op) = Prev then
2155 Set_Etype (Op, T);
2156 end if;
2158 Next_Elmt (Elmt);
2159 end loop;
2160 end if;
2161 end Check_Ops_From_Incomplete_Type;
2163 -- Start of processing for Analyze_Full_Type_Declaration
2165 begin
2166 Prev := Find_Type_Name (N);
2168 -- The full view, if present, now points to the current type
2170 -- Ada 2005 (AI-50217): If the type was previously decorated when
2171 -- imported through a LIMITED WITH clause, it appears as incomplete
2172 -- but has no full view.
2174 if Ekind (Prev) = E_Incomplete_Type
2175 and then Present (Full_View (Prev))
2176 then
2177 T := Full_View (Prev);
2178 else
2179 T := Prev;
2180 end if;
2182 Set_Is_Pure (T, Is_Pure (Current_Scope));
2184 -- We set the flag Is_First_Subtype here. It is needed to set the
2185 -- corresponding flag for the Implicit class-wide-type created
2186 -- during tagged types processing.
2188 Set_Is_First_Subtype (T, True);
2190 -- Only composite types other than array types are allowed to have
2191 -- discriminants.
2193 case Nkind (Def) is
2195 -- For derived types, the rule will be checked once we've figured
2196 -- out the parent type.
2198 when N_Derived_Type_Definition =>
2199 null;
2201 -- For record types, discriminants are allowed
2203 when N_Record_Definition =>
2204 null;
2206 when others =>
2207 if Present (Discriminant_Specifications (N)) then
2208 Error_Msg_N
2209 ("elementary or array type cannot have discriminants",
2210 Defining_Identifier
2211 (First (Discriminant_Specifications (N))));
2212 end if;
2213 end case;
2215 -- Elaborate the type definition according to kind, and generate
2216 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2217 -- already done (this happens during the reanalysis that follows a call
2218 -- to the high level optimizer).
2220 if not Analyzed (T) then
2221 Set_Analyzed (T);
2223 case Nkind (Def) is
2225 when N_Access_To_Subprogram_Definition =>
2226 Access_Subprogram_Declaration (T, Def);
2228 -- If this is a remote access to subprogram, we must create the
2229 -- equivalent fat pointer type, and related subprograms.
2231 if Is_Remote then
2232 Process_Remote_AST_Declaration (N);
2233 end if;
2235 -- Validate categorization rule against access type declaration
2236 -- usually a violation in Pure unit, Shared_Passive unit.
2238 Validate_Access_Type_Declaration (T, N);
2240 when N_Access_To_Object_Definition =>
2241 Access_Type_Declaration (T, Def);
2243 -- Validate categorization rule against access type declaration
2244 -- usually a violation in Pure unit, Shared_Passive unit.
2246 Validate_Access_Type_Declaration (T, N);
2248 -- If we are in a Remote_Call_Interface package and define a
2249 -- RACW, then calling stubs and specific stream attributes
2250 -- must be added.
2252 if Is_Remote
2253 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2254 then
2255 Add_RACW_Features (Def_Id);
2256 end if;
2258 -- Set no strict aliasing flag if config pragma seen
2260 if Opt.No_Strict_Aliasing then
2261 Set_No_Strict_Aliasing (Base_Type (Def_Id));
2262 end if;
2264 when N_Array_Type_Definition =>
2265 Array_Type_Declaration (T, Def);
2267 when N_Derived_Type_Definition =>
2268 Derived_Type_Declaration (T, N, T /= Def_Id);
2270 when N_Enumeration_Type_Definition =>
2271 Enumeration_Type_Declaration (T, Def);
2273 when N_Floating_Point_Definition =>
2274 Floating_Point_Type_Declaration (T, Def);
2276 when N_Decimal_Fixed_Point_Definition =>
2277 Decimal_Fixed_Point_Type_Declaration (T, Def);
2279 when N_Ordinary_Fixed_Point_Definition =>
2280 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2282 when N_Signed_Integer_Type_Definition =>
2283 Signed_Integer_Type_Declaration (T, Def);
2285 when N_Modular_Type_Definition =>
2286 Modular_Type_Declaration (T, Def);
2288 when N_Record_Definition =>
2289 Record_Type_Declaration (T, N, Prev);
2291 -- If declaration has a parse error, nothing to elaborate.
2293 when N_Error =>
2294 null;
2296 when others =>
2297 raise Program_Error;
2299 end case;
2300 end if;
2302 if Etype (T) = Any_Type then
2303 goto Leave;
2304 end if;
2306 -- Some common processing for all types
2308 Set_Depends_On_Private (T, Has_Private_Component (T));
2309 Check_Ops_From_Incomplete_Type;
2311 -- Both the declared entity, and its anonymous base type if one
2312 -- was created, need freeze nodes allocated.
2314 declare
2315 B : constant Entity_Id := Base_Type (T);
2317 begin
2318 -- In the case where the base type differs from the first subtype, we
2319 -- pre-allocate a freeze node, and set the proper link to the first
2320 -- subtype. Freeze_Entity will use this preallocated freeze node when
2321 -- it freezes the entity.
2323 -- This does not apply if the base type is a generic type, whose
2324 -- declaration is independent of the current derived definition.
2326 if B /= T and then not Is_Generic_Type (B) then
2327 Ensure_Freeze_Node (B);
2328 Set_First_Subtype_Link (Freeze_Node (B), T);
2329 end if;
2331 -- A type that is imported through a limited_with clause cannot
2332 -- generate any code, and thus need not be frozen. However, an access
2333 -- type with an imported designated type needs a finalization list,
2334 -- which may be referenced in some other package that has non-limited
2335 -- visibility on the designated type. Thus we must create the
2336 -- finalization list at the point the access type is frozen, to
2337 -- prevent unsatisfied references at link time.
2339 if not From_With_Type (T) or else Is_Access_Type (T) then
2340 Set_Has_Delayed_Freeze (T);
2341 end if;
2342 end;
2344 -- Case where T is the full declaration of some private type which has
2345 -- been swapped in Defining_Identifier (N).
2347 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2348 Process_Full_View (N, T, Def_Id);
2350 -- Record the reference. The form of this is a little strange, since
2351 -- the full declaration has been swapped in. So the first parameter
2352 -- here represents the entity to which a reference is made which is
2353 -- the "real" entity, i.e. the one swapped in, and the second
2354 -- parameter provides the reference location.
2356 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2357 -- since we don't want a complaint about the full type being an
2358 -- unwanted reference to the private type
2360 declare
2361 B : constant Boolean := Has_Pragma_Unreferenced (T);
2362 begin
2363 Set_Has_Pragma_Unreferenced (T, False);
2364 Generate_Reference (T, T, 'c');
2365 Set_Has_Pragma_Unreferenced (T, B);
2366 end;
2368 Set_Completion_Referenced (Def_Id);
2370 -- For completion of incomplete type, process incomplete dependents
2371 -- and always mark the full type as referenced (it is the incomplete
2372 -- type that we get for any real reference).
2374 elsif Ekind (Prev) = E_Incomplete_Type then
2375 Process_Incomplete_Dependents (N, T, Prev);
2376 Generate_Reference (Prev, Def_Id, 'c');
2377 Set_Completion_Referenced (Def_Id);
2379 -- If not private type or incomplete type completion, this is a real
2380 -- definition of a new entity, so record it.
2382 else
2383 Generate_Definition (Def_Id);
2384 end if;
2386 if Chars (Scope (Def_Id)) = Name_System
2387 and then Chars (Def_Id) = Name_Address
2388 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2389 then
2390 Set_Is_Descendent_Of_Address (Def_Id);
2391 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2392 Set_Is_Descendent_Of_Address (Prev);
2393 end if;
2395 Set_Optimize_Alignment_Flags (Def_Id);
2396 Check_Eliminated (Def_Id);
2398 <<Leave>>
2399 Analyze_Aspect_Specifications (N, Def_Id, Aspect_Specifications (N));
2400 end Analyze_Full_Type_Declaration;
2402 ----------------------------------
2403 -- Analyze_Incomplete_Type_Decl --
2404 ----------------------------------
2406 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2407 F : constant Boolean := Is_Pure (Current_Scope);
2408 T : Entity_Id;
2410 begin
2411 Generate_Definition (Defining_Identifier (N));
2413 -- Process an incomplete declaration. The identifier must not have been
2414 -- declared already in the scope. However, an incomplete declaration may
2415 -- appear in the private part of a package, for a private type that has
2416 -- already been declared.
2418 -- In this case, the discriminants (if any) must match
2420 T := Find_Type_Name (N);
2422 Set_Ekind (T, E_Incomplete_Type);
2423 Init_Size_Align (T);
2424 Set_Is_First_Subtype (T, True);
2425 Set_Etype (T, T);
2427 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2428 -- incomplete types.
2430 if Tagged_Present (N) then
2431 Set_Is_Tagged_Type (T);
2432 Make_Class_Wide_Type (T);
2433 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2434 end if;
2436 Push_Scope (T);
2438 Set_Stored_Constraint (T, No_Elist);
2440 if Present (Discriminant_Specifications (N)) then
2441 Process_Discriminants (N);
2442 end if;
2444 End_Scope;
2446 -- If the type has discriminants, non-trivial subtypes may be
2447 -- declared before the full view of the type. The full views of those
2448 -- subtypes will be built after the full view of the type.
2450 Set_Private_Dependents (T, New_Elmt_List);
2451 Set_Is_Pure (T, F);
2452 end Analyze_Incomplete_Type_Decl;
2454 -----------------------------------
2455 -- Analyze_Interface_Declaration --
2456 -----------------------------------
2458 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2459 CW : constant Entity_Id := Class_Wide_Type (T);
2461 begin
2462 Set_Is_Tagged_Type (T);
2464 Set_Is_Limited_Record (T, Limited_Present (Def)
2465 or else Task_Present (Def)
2466 or else Protected_Present (Def)
2467 or else Synchronized_Present (Def));
2469 -- Type is abstract if full declaration carries keyword, or if previous
2470 -- partial view did.
2472 Set_Is_Abstract_Type (T);
2473 Set_Is_Interface (T);
2475 -- Type is a limited interface if it includes the keyword limited, task,
2476 -- protected, or synchronized.
2478 Set_Is_Limited_Interface
2479 (T, Limited_Present (Def)
2480 or else Protected_Present (Def)
2481 or else Synchronized_Present (Def)
2482 or else Task_Present (Def));
2484 Set_Interfaces (T, New_Elmt_List);
2485 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2487 -- Complete the decoration of the class-wide entity if it was already
2488 -- built (i.e. during the creation of the limited view)
2490 if Present (CW) then
2491 Set_Is_Interface (CW);
2492 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2493 end if;
2495 -- Check runtime support for synchronized interfaces
2497 if VM_Target = No_VM
2498 and then (Is_Task_Interface (T)
2499 or else Is_Protected_Interface (T)
2500 or else Is_Synchronized_Interface (T))
2501 and then not RTE_Available (RE_Select_Specific_Data)
2502 then
2503 Error_Msg_CRT ("synchronized interfaces", T);
2504 end if;
2505 end Analyze_Interface_Declaration;
2507 -----------------------------
2508 -- Analyze_Itype_Reference --
2509 -----------------------------
2511 -- Nothing to do. This node is placed in the tree only for the benefit of
2512 -- back end processing, and has no effect on the semantic processing.
2514 procedure Analyze_Itype_Reference (N : Node_Id) is
2515 begin
2516 pragma Assert (Is_Itype (Itype (N)));
2517 null;
2518 end Analyze_Itype_Reference;
2520 --------------------------------
2521 -- Analyze_Number_Declaration --
2522 --------------------------------
2524 procedure Analyze_Number_Declaration (N : Node_Id) is
2525 Id : constant Entity_Id := Defining_Identifier (N);
2526 E : constant Node_Id := Expression (N);
2527 T : Entity_Id;
2528 Index : Interp_Index;
2529 It : Interp;
2531 begin
2532 Generate_Definition (Id);
2533 Enter_Name (Id);
2535 -- This is an optimization of a common case of an integer literal
2537 if Nkind (E) = N_Integer_Literal then
2538 Set_Is_Static_Expression (E, True);
2539 Set_Etype (E, Universal_Integer);
2541 Set_Etype (Id, Universal_Integer);
2542 Set_Ekind (Id, E_Named_Integer);
2543 Set_Is_Frozen (Id, True);
2544 return;
2545 end if;
2547 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2549 -- Process expression, replacing error by integer zero, to avoid
2550 -- cascaded errors or aborts further along in the processing
2552 -- Replace Error by integer zero, which seems least likely to
2553 -- cause cascaded errors.
2555 if E = Error then
2556 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2557 Set_Error_Posted (E);
2558 end if;
2560 Analyze (E);
2562 -- Verify that the expression is static and numeric. If
2563 -- the expression is overloaded, we apply the preference
2564 -- rule that favors root numeric types.
2566 if not Is_Overloaded (E) then
2567 T := Etype (E);
2569 else
2570 T := Any_Type;
2572 Get_First_Interp (E, Index, It);
2573 while Present (It.Typ) loop
2574 if (Is_Integer_Type (It.Typ)
2575 or else Is_Real_Type (It.Typ))
2576 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2577 then
2578 if T = Any_Type then
2579 T := It.Typ;
2581 elsif It.Typ = Universal_Real
2582 or else It.Typ = Universal_Integer
2583 then
2584 -- Choose universal interpretation over any other
2586 T := It.Typ;
2587 exit;
2588 end if;
2589 end if;
2591 Get_Next_Interp (Index, It);
2592 end loop;
2593 end if;
2595 if Is_Integer_Type (T) then
2596 Resolve (E, T);
2597 Set_Etype (Id, Universal_Integer);
2598 Set_Ekind (Id, E_Named_Integer);
2600 elsif Is_Real_Type (T) then
2602 -- Because the real value is converted to universal_real, this is a
2603 -- legal context for a universal fixed expression.
2605 if T = Universal_Fixed then
2606 declare
2607 Loc : constant Source_Ptr := Sloc (N);
2608 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2609 Subtype_Mark =>
2610 New_Occurrence_Of (Universal_Real, Loc),
2611 Expression => Relocate_Node (E));
2613 begin
2614 Rewrite (E, Conv);
2615 Analyze (E);
2616 end;
2618 elsif T = Any_Fixed then
2619 Error_Msg_N ("illegal context for mixed mode operation", E);
2621 -- Expression is of the form : universal_fixed * integer. Try to
2622 -- resolve as universal_real.
2624 T := Universal_Real;
2625 Set_Etype (E, T);
2626 end if;
2628 Resolve (E, T);
2629 Set_Etype (Id, Universal_Real);
2630 Set_Ekind (Id, E_Named_Real);
2632 else
2633 Wrong_Type (E, Any_Numeric);
2634 Resolve (E, T);
2636 Set_Etype (Id, T);
2637 Set_Ekind (Id, E_Constant);
2638 Set_Never_Set_In_Source (Id, True);
2639 Set_Is_True_Constant (Id, True);
2640 return;
2641 end if;
2643 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2644 Set_Etype (E, Etype (Id));
2645 end if;
2647 if not Is_OK_Static_Expression (E) then
2648 Flag_Non_Static_Expr
2649 ("non-static expression used in number declaration!", E);
2650 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2651 Set_Etype (E, Any_Type);
2652 end if;
2653 end Analyze_Number_Declaration;
2655 --------------------------------
2656 -- Analyze_Object_Declaration --
2657 --------------------------------
2659 procedure Analyze_Object_Declaration (N : Node_Id) is
2660 Loc : constant Source_Ptr := Sloc (N);
2661 Id : constant Entity_Id := Defining_Identifier (N);
2662 T : Entity_Id;
2663 Act_T : Entity_Id;
2665 E : Node_Id := Expression (N);
2666 -- E is set to Expression (N) throughout this routine. When
2667 -- Expression (N) is modified, E is changed accordingly.
2669 Prev_Entity : Entity_Id := Empty;
2671 function Count_Tasks (T : Entity_Id) return Uint;
2672 -- This function is called when a non-generic library level object of a
2673 -- task type is declared. Its function is to count the static number of
2674 -- tasks declared within the type (it is only called if Has_Tasks is set
2675 -- for T). As a side effect, if an array of tasks with non-static bounds
2676 -- or a variant record type is encountered, Check_Restrictions is called
2677 -- indicating the count is unknown.
2679 -----------------
2680 -- Count_Tasks --
2681 -----------------
2683 function Count_Tasks (T : Entity_Id) return Uint is
2684 C : Entity_Id;
2685 X : Node_Id;
2686 V : Uint;
2688 begin
2689 if Is_Task_Type (T) then
2690 return Uint_1;
2692 elsif Is_Record_Type (T) then
2693 if Has_Discriminants (T) then
2694 Check_Restriction (Max_Tasks, N);
2695 return Uint_0;
2697 else
2698 V := Uint_0;
2699 C := First_Component (T);
2700 while Present (C) loop
2701 V := V + Count_Tasks (Etype (C));
2702 Next_Component (C);
2703 end loop;
2705 return V;
2706 end if;
2708 elsif Is_Array_Type (T) then
2709 X := First_Index (T);
2710 V := Count_Tasks (Component_Type (T));
2711 while Present (X) loop
2712 C := Etype (X);
2714 if not Is_Static_Subtype (C) then
2715 Check_Restriction (Max_Tasks, N);
2716 return Uint_0;
2717 else
2718 V := V * (UI_Max (Uint_0,
2719 Expr_Value (Type_High_Bound (C)) -
2720 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2721 end if;
2723 Next_Index (X);
2724 end loop;
2726 return V;
2728 else
2729 return Uint_0;
2730 end if;
2731 end Count_Tasks;
2733 -- Start of processing for Analyze_Object_Declaration
2735 begin
2736 -- There are three kinds of implicit types generated by an
2737 -- object declaration:
2739 -- 1. Those for generated by the original Object Definition
2741 -- 2. Those generated by the Expression
2743 -- 3. Those used to constrained the Object Definition with the
2744 -- expression constraints when it is unconstrained
2746 -- They must be generated in this order to avoid order of elaboration
2747 -- issues. Thus the first step (after entering the name) is to analyze
2748 -- the object definition.
2750 if Constant_Present (N) then
2751 Prev_Entity := Current_Entity_In_Scope (Id);
2753 if Present (Prev_Entity)
2754 and then
2755 -- If the homograph is an implicit subprogram, it is overridden
2756 -- by the current declaration.
2758 ((Is_Overloadable (Prev_Entity)
2759 and then Is_Inherited_Operation (Prev_Entity))
2761 -- The current object is a discriminal generated for an entry
2762 -- family index. Even though the index is a constant, in this
2763 -- particular context there is no true constant redeclaration.
2764 -- Enter_Name will handle the visibility.
2766 or else
2767 (Is_Discriminal (Id)
2768 and then Ekind (Discriminal_Link (Id)) =
2769 E_Entry_Index_Parameter)
2771 -- The current object is the renaming for a generic declared
2772 -- within the instance.
2774 or else
2775 (Ekind (Prev_Entity) = E_Package
2776 and then Nkind (Parent (Prev_Entity)) =
2777 N_Package_Renaming_Declaration
2778 and then not Comes_From_Source (Prev_Entity)
2779 and then Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
2780 then
2781 Prev_Entity := Empty;
2782 end if;
2783 end if;
2785 if Present (Prev_Entity) then
2786 Constant_Redeclaration (Id, N, T);
2788 Generate_Reference (Prev_Entity, Id, 'c');
2789 Set_Completion_Referenced (Id);
2791 if Error_Posted (N) then
2793 -- Type mismatch or illegal redeclaration, Do not analyze
2794 -- expression to avoid cascaded errors.
2796 T := Find_Type_Of_Object (Object_Definition (N), N);
2797 Set_Etype (Id, T);
2798 Set_Ekind (Id, E_Variable);
2799 goto Leave;
2800 end if;
2802 -- In the normal case, enter identifier at the start to catch premature
2803 -- usage in the initialization expression.
2805 else
2806 Generate_Definition (Id);
2807 Enter_Name (Id);
2809 Mark_Coextensions (N, Object_Definition (N));
2811 T := Find_Type_Of_Object (Object_Definition (N), N);
2813 if Nkind (Object_Definition (N)) = N_Access_Definition
2814 and then Present
2815 (Access_To_Subprogram_Definition (Object_Definition (N)))
2816 and then Protected_Present
2817 (Access_To_Subprogram_Definition (Object_Definition (N)))
2818 then
2819 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2820 end if;
2822 if Error_Posted (Id) then
2823 Set_Etype (Id, T);
2824 Set_Ekind (Id, E_Variable);
2825 goto Leave;
2826 end if;
2827 end if;
2829 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2830 -- out some static checks
2832 if Ada_Version >= Ada_2005
2833 and then Can_Never_Be_Null (T)
2834 then
2835 -- In case of aggregates we must also take care of the correct
2836 -- initialization of nested aggregates bug this is done at the
2837 -- point of the analysis of the aggregate (see sem_aggr.adb)
2839 if Present (Expression (N))
2840 and then Nkind (Expression (N)) = N_Aggregate
2841 then
2842 null;
2844 else
2845 declare
2846 Save_Typ : constant Entity_Id := Etype (Id);
2847 begin
2848 Set_Etype (Id, T); -- Temp. decoration for static checks
2849 Null_Exclusion_Static_Checks (N);
2850 Set_Etype (Id, Save_Typ);
2851 end;
2852 end if;
2853 end if;
2855 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2857 -- If deferred constant, make sure context is appropriate. We detect
2858 -- a deferred constant as a constant declaration with no expression.
2859 -- A deferred constant can appear in a package body if its completion
2860 -- is by means of an interface pragma.
2862 if Constant_Present (N)
2863 and then No (E)
2864 then
2865 -- A deferred constant may appear in the declarative part of the
2866 -- following constructs:
2868 -- blocks
2869 -- entry bodies
2870 -- extended return statements
2871 -- package specs
2872 -- package bodies
2873 -- subprogram bodies
2874 -- task bodies
2876 -- When declared inside a package spec, a deferred constant must be
2877 -- completed by a full constant declaration or pragma Import. In all
2878 -- other cases, the only proper completion is pragma Import. Extended
2879 -- return statements are flagged as invalid contexts because they do
2880 -- not have a declarative part and so cannot accommodate the pragma.
2882 if Ekind (Current_Scope) = E_Return_Statement then
2883 Error_Msg_N
2884 ("invalid context for deferred constant declaration (RM 7.4)",
2886 Error_Msg_N
2887 ("\declaration requires an initialization expression",
2889 Set_Constant_Present (N, False);
2891 -- In Ada 83, deferred constant must be of private type
2893 elsif not Is_Private_Type (T) then
2894 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2895 Error_Msg_N
2896 ("(Ada 83) deferred constant must be private type", N);
2897 end if;
2898 end if;
2900 -- If not a deferred constant, then object declaration freezes its type
2902 else
2903 Check_Fully_Declared (T, N);
2904 Freeze_Before (N, T);
2905 end if;
2907 -- If the object was created by a constrained array definition, then
2908 -- set the link in both the anonymous base type and anonymous subtype
2909 -- that are built to represent the array type to point to the object.
2911 if Nkind (Object_Definition (Declaration_Node (Id))) =
2912 N_Constrained_Array_Definition
2913 then
2914 Set_Related_Array_Object (T, Id);
2915 Set_Related_Array_Object (Base_Type (T), Id);
2916 end if;
2918 -- Special checks for protected objects not at library level
2920 if Is_Protected_Type (T)
2921 and then not Is_Library_Level_Entity (Id)
2922 then
2923 Check_Restriction (No_Local_Protected_Objects, Id);
2925 -- Protected objects with interrupt handlers must be at library level
2927 -- Ada 2005: this test is not needed (and the corresponding clause
2928 -- in the RM is removed) because accessibility checks are sufficient
2929 -- to make handlers not at the library level illegal.
2931 if Has_Interrupt_Handler (T)
2932 and then Ada_Version < Ada_2005
2933 then
2934 Error_Msg_N
2935 ("interrupt object can only be declared at library level", Id);
2936 end if;
2937 end if;
2939 -- The actual subtype of the object is the nominal subtype, unless
2940 -- the nominal one is unconstrained and obtained from the expression.
2942 Act_T := T;
2944 -- Process initialization expression if present and not in error
2946 if Present (E) and then E /= Error then
2948 -- Generate an error in case of CPP class-wide object initialization.
2949 -- Required because otherwise the expansion of the class-wide
2950 -- assignment would try to use 'size to initialize the object
2951 -- (primitive that is not available in CPP tagged types).
2953 if Is_Class_Wide_Type (Act_T)
2954 and then
2955 (Is_CPP_Class (Root_Type (Etype (Act_T)))
2956 or else
2957 (Present (Full_View (Root_Type (Etype (Act_T))))
2958 and then
2959 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
2960 then
2961 Error_Msg_N
2962 ("predefined assignment not available for 'C'P'P tagged types",
2964 end if;
2966 Mark_Coextensions (N, E);
2967 Analyze (E);
2969 -- In case of errors detected in the analysis of the expression,
2970 -- decorate it with the expected type to avoid cascaded errors
2972 if No (Etype (E)) then
2973 Set_Etype (E, T);
2974 end if;
2976 -- If an initialization expression is present, then we set the
2977 -- Is_True_Constant flag. It will be reset if this is a variable
2978 -- and it is indeed modified.
2980 Set_Is_True_Constant (Id, True);
2982 -- If we are analyzing a constant declaration, set its completion
2983 -- flag after analyzing and resolving the expression.
2985 if Constant_Present (N) then
2986 Set_Has_Completion (Id);
2987 end if;
2989 -- Set type and resolve (type may be overridden later on)
2991 Set_Etype (Id, T);
2992 Resolve (E, T);
2994 -- If E is null and has been replaced by an N_Raise_Constraint_Error
2995 -- node (which was marked already-analyzed), we need to set the type
2996 -- to something other than Any_Access in order to keep gigi happy.
2998 if Etype (E) = Any_Access then
2999 Set_Etype (E, T);
3000 end if;
3002 -- If the object is an access to variable, the initialization
3003 -- expression cannot be an access to constant.
3005 if Is_Access_Type (T)
3006 and then not Is_Access_Constant (T)
3007 and then Is_Access_Type (Etype (E))
3008 and then Is_Access_Constant (Etype (E))
3009 then
3010 Error_Msg_N
3011 ("access to variable cannot be initialized "
3012 & "with an access-to-constant expression", E);
3013 end if;
3015 if not Assignment_OK (N) then
3016 Check_Initialization (T, E);
3017 end if;
3019 Check_Unset_Reference (E);
3021 -- If this is a variable, then set current value. If this is a
3022 -- declared constant of a scalar type with a static expression,
3023 -- indicate that it is always valid.
3025 if not Constant_Present (N) then
3026 if Compile_Time_Known_Value (E) then
3027 Set_Current_Value (Id, E);
3028 end if;
3030 elsif Is_Scalar_Type (T)
3031 and then Is_OK_Static_Expression (E)
3032 then
3033 Set_Is_Known_Valid (Id);
3034 end if;
3036 -- Deal with setting of null flags
3038 if Is_Access_Type (T) then
3039 if Known_Non_Null (E) then
3040 Set_Is_Known_Non_Null (Id, True);
3041 elsif Known_Null (E)
3042 and then not Can_Never_Be_Null (Id)
3043 then
3044 Set_Is_Known_Null (Id, True);
3045 end if;
3046 end if;
3048 -- Check incorrect use of dynamically tagged expressions.
3050 if Is_Tagged_Type (T) then
3051 Check_Dynamically_Tagged_Expression
3052 (Expr => E,
3053 Typ => T,
3054 Related_Nod => N);
3055 end if;
3057 Apply_Scalar_Range_Check (E, T);
3058 Apply_Static_Length_Check (E, T);
3059 end if;
3061 -- If the No_Streams restriction is set, check that the type of the
3062 -- object is not, and does not contain, any subtype derived from
3063 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3064 -- Has_Stream just for efficiency reasons. There is no point in
3065 -- spending time on a Has_Stream check if the restriction is not set.
3067 if Restriction_Check_Required (No_Streams) then
3068 if Has_Stream (T) then
3069 Check_Restriction (No_Streams, N);
3070 end if;
3071 end if;
3073 -- Case of unconstrained type
3075 if Is_Indefinite_Subtype (T) then
3077 -- Nothing to do in deferred constant case
3079 if Constant_Present (N) and then No (E) then
3080 null;
3082 -- Case of no initialization present
3084 elsif No (E) then
3085 if No_Initialization (N) then
3086 null;
3088 elsif Is_Class_Wide_Type (T) then
3089 Error_Msg_N
3090 ("initialization required in class-wide declaration ", N);
3092 else
3093 Error_Msg_N
3094 ("unconstrained subtype not allowed (need initialization)",
3095 Object_Definition (N));
3097 if Is_Record_Type (T) and then Has_Discriminants (T) then
3098 Error_Msg_N
3099 ("\provide initial value or explicit discriminant values",
3100 Object_Definition (N));
3102 Error_Msg_NE
3103 ("\or give default discriminant values for type&",
3104 Object_Definition (N), T);
3106 elsif Is_Array_Type (T) then
3107 Error_Msg_N
3108 ("\provide initial value or explicit array bounds",
3109 Object_Definition (N));
3110 end if;
3111 end if;
3113 -- Case of initialization present but in error. Set initial
3114 -- expression as absent (but do not make above complaints)
3116 elsif E = Error then
3117 Set_Expression (N, Empty);
3118 E := Empty;
3120 -- Case of initialization present
3122 else
3123 -- Not allowed in Ada 83
3125 if not Constant_Present (N) then
3126 if Ada_Version = Ada_83
3127 and then Comes_From_Source (Object_Definition (N))
3128 then
3129 Error_Msg_N
3130 ("(Ada 83) unconstrained variable not allowed",
3131 Object_Definition (N));
3132 end if;
3133 end if;
3135 -- Now we constrain the variable from the initializing expression
3137 -- If the expression is an aggregate, it has been expanded into
3138 -- individual assignments. Retrieve the actual type from the
3139 -- expanded construct.
3141 if Is_Array_Type (T)
3142 and then No_Initialization (N)
3143 and then Nkind (Original_Node (E)) = N_Aggregate
3144 then
3145 Act_T := Etype (E);
3147 -- In case of class-wide interface object declarations we delay
3148 -- the generation of the equivalent record type declarations until
3149 -- its expansion because there are cases in they are not required.
3151 elsif Is_Interface (T) then
3152 null;
3154 else
3155 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3156 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3157 end if;
3159 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
3161 if Aliased_Present (N) then
3162 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3163 end if;
3165 Freeze_Before (N, Act_T);
3166 Freeze_Before (N, T);
3167 end if;
3169 elsif Is_Array_Type (T)
3170 and then No_Initialization (N)
3171 and then Nkind (Original_Node (E)) = N_Aggregate
3172 then
3173 if not Is_Entity_Name (Object_Definition (N)) then
3174 Act_T := Etype (E);
3175 Check_Compile_Time_Size (Act_T);
3177 if Aliased_Present (N) then
3178 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3179 end if;
3180 end if;
3182 -- When the given object definition and the aggregate are specified
3183 -- independently, and their lengths might differ do a length check.
3184 -- This cannot happen if the aggregate is of the form (others =>...)
3186 if not Is_Constrained (T) then
3187 null;
3189 elsif Nkind (E) = N_Raise_Constraint_Error then
3191 -- Aggregate is statically illegal. Place back in declaration
3193 Set_Expression (N, E);
3194 Set_No_Initialization (N, False);
3196 elsif T = Etype (E) then
3197 null;
3199 elsif Nkind (E) = N_Aggregate
3200 and then Present (Component_Associations (E))
3201 and then Present (Choices (First (Component_Associations (E))))
3202 and then Nkind (First
3203 (Choices (First (Component_Associations (E))))) = N_Others_Choice
3204 then
3205 null;
3207 else
3208 Apply_Length_Check (E, T);
3209 end if;
3211 -- If the type is limited unconstrained with defaulted discriminants and
3212 -- there is no expression, then the object is constrained by the
3213 -- defaults, so it is worthwhile building the corresponding subtype.
3215 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
3216 and then not Is_Constrained (T)
3217 and then Has_Discriminants (T)
3218 then
3219 if No (E) then
3220 Act_T := Build_Default_Subtype (T, N);
3221 else
3222 -- Ada 2005: a limited object may be initialized by means of an
3223 -- aggregate. If the type has default discriminants it has an
3224 -- unconstrained nominal type, Its actual subtype will be obtained
3225 -- from the aggregate, and not from the default discriminants.
3227 Act_T := Etype (E);
3228 end if;
3230 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
3232 elsif Present (Underlying_Type (T))
3233 and then not Is_Constrained (Underlying_Type (T))
3234 and then Has_Discriminants (Underlying_Type (T))
3235 and then Nkind (E) = N_Function_Call
3236 and then Constant_Present (N)
3237 then
3238 -- The back-end has problems with constants of a discriminated type
3239 -- with defaults, if the initial value is a function call. We
3240 -- generate an intermediate temporary for the result of the call.
3241 -- It is unclear why this should make it acceptable to gcc. ???
3243 Remove_Side_Effects (E);
3244 end if;
3246 -- Check No_Wide_Characters restriction
3248 Check_Wide_Character_Restriction (T, Object_Definition (N));
3250 -- Indicate this is not set in source. Certainly true for constants,
3251 -- and true for variables so far (will be reset for a variable if and
3252 -- when we encounter a modification in the source).
3254 Set_Never_Set_In_Source (Id, True);
3256 -- Now establish the proper kind and type of the object
3258 if Constant_Present (N) then
3259 Set_Ekind (Id, E_Constant);
3260 Set_Is_True_Constant (Id, True);
3262 else
3263 Set_Ekind (Id, E_Variable);
3265 -- A variable is set as shared passive if it appears in a shared
3266 -- passive package, and is at the outer level. This is not done
3267 -- for entities generated during expansion, because those are
3268 -- always manipulated locally.
3270 if Is_Shared_Passive (Current_Scope)
3271 and then Is_Library_Level_Entity (Id)
3272 and then Comes_From_Source (Id)
3273 then
3274 Set_Is_Shared_Passive (Id);
3275 Check_Shared_Var (Id, T, N);
3276 end if;
3278 -- Set Has_Initial_Value if initializing expression present. Note
3279 -- that if there is no initializing expression, we leave the state
3280 -- of this flag unchanged (usually it will be False, but notably in
3281 -- the case of exception choice variables, it will already be true).
3283 if Present (E) then
3284 Set_Has_Initial_Value (Id, True);
3285 end if;
3286 end if;
3288 -- Initialize alignment and size and capture alignment setting
3290 Init_Alignment (Id);
3291 Init_Esize (Id);
3292 Set_Optimize_Alignment_Flags (Id);
3294 -- Deal with aliased case
3296 if Aliased_Present (N) then
3297 Set_Is_Aliased (Id);
3299 -- If the object is aliased and the type is unconstrained with
3300 -- defaulted discriminants and there is no expression, then the
3301 -- object is constrained by the defaults, so it is worthwhile
3302 -- building the corresponding subtype.
3304 -- Ada 2005 (AI-363): If the aliased object is discriminated and
3305 -- unconstrained, then only establish an actual subtype if the
3306 -- nominal subtype is indefinite. In definite cases the object is
3307 -- unconstrained in Ada 2005.
3309 if No (E)
3310 and then Is_Record_Type (T)
3311 and then not Is_Constrained (T)
3312 and then Has_Discriminants (T)
3313 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
3314 then
3315 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
3316 end if;
3317 end if;
3319 -- Now we can set the type of the object
3321 Set_Etype (Id, Act_T);
3323 -- Deal with controlled types
3325 if Has_Controlled_Component (Etype (Id))
3326 or else Is_Controlled (Etype (Id))
3327 then
3328 if not Is_Library_Level_Entity (Id) then
3329 Check_Restriction (No_Nested_Finalization, N);
3330 else
3331 Validate_Controlled_Object (Id);
3332 end if;
3334 -- Generate a warning when an initialization causes an obvious ABE
3335 -- violation. If the init expression is a simple aggregate there
3336 -- shouldn't be any initialize/adjust call generated. This will be
3337 -- true as soon as aggregates are built in place when possible.
3339 -- ??? at the moment we do not generate warnings for temporaries
3340 -- created for those aggregates although Program_Error might be
3341 -- generated if compiled with -gnato.
3343 if Is_Controlled (Etype (Id))
3344 and then Comes_From_Source (Id)
3345 then
3346 declare
3347 BT : constant Entity_Id := Base_Type (Etype (Id));
3349 Implicit_Call : Entity_Id;
3350 pragma Warnings (Off, Implicit_Call);
3351 -- ??? what is this for (never referenced!)
3353 function Is_Aggr (N : Node_Id) return Boolean;
3354 -- Check that N is an aggregate
3356 -------------
3357 -- Is_Aggr --
3358 -------------
3360 function Is_Aggr (N : Node_Id) return Boolean is
3361 begin
3362 case Nkind (Original_Node (N)) is
3363 when N_Aggregate | N_Extension_Aggregate =>
3364 return True;
3366 when N_Qualified_Expression |
3367 N_Type_Conversion |
3368 N_Unchecked_Type_Conversion =>
3369 return Is_Aggr (Expression (Original_Node (N)));
3371 when others =>
3372 return False;
3373 end case;
3374 end Is_Aggr;
3376 begin
3377 -- If no underlying type, we already are in an error situation.
3378 -- Do not try to add a warning since we do not have access to
3379 -- prim-op list.
3381 if No (Underlying_Type (BT)) then
3382 Implicit_Call := Empty;
3384 -- A generic type does not have usable primitive operators.
3385 -- Initialization calls are built for instances.
3387 elsif Is_Generic_Type (BT) then
3388 Implicit_Call := Empty;
3390 -- If the init expression is not an aggregate, an adjust call
3391 -- will be generated
3393 elsif Present (E) and then not Is_Aggr (E) then
3394 Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
3396 -- If no init expression and we are not in the deferred
3397 -- constant case, an Initialize call will be generated
3399 elsif No (E) and then not Constant_Present (N) then
3400 Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
3402 else
3403 Implicit_Call := Empty;
3404 end if;
3405 end;
3406 end if;
3407 end if;
3409 if Has_Task (Etype (Id)) then
3410 Check_Restriction (No_Tasking, N);
3412 -- Deal with counting max tasks
3414 -- Nothing to do if inside a generic
3416 if Inside_A_Generic then
3417 null;
3419 -- If library level entity, then count tasks
3421 elsif Is_Library_Level_Entity (Id) then
3422 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
3424 -- If not library level entity, then indicate we don't know max
3425 -- tasks and also check task hierarchy restriction and blocking
3426 -- operation (since starting a task is definitely blocking!)
3428 else
3429 Check_Restriction (Max_Tasks, N);
3430 Check_Restriction (No_Task_Hierarchy, N);
3431 Check_Potentially_Blocking_Operation (N);
3432 end if;
3434 -- A rather specialized test. If we see two tasks being declared
3435 -- of the same type in the same object declaration, and the task
3436 -- has an entry with an address clause, we know that program error
3437 -- will be raised at run time since we can't have two tasks with
3438 -- entries at the same address.
3440 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3441 declare
3442 E : Entity_Id;
3444 begin
3445 E := First_Entity (Etype (Id));
3446 while Present (E) loop
3447 if Ekind (E) = E_Entry
3448 and then Present (Get_Attribute_Definition_Clause
3449 (E, Attribute_Address))
3450 then
3451 Error_Msg_N
3452 ("?more than one task with same entry address", N);
3453 Error_Msg_N
3454 ("\?Program_Error will be raised at run time", N);
3455 Insert_Action (N,
3456 Make_Raise_Program_Error (Loc,
3457 Reason => PE_Duplicated_Entry_Address));
3458 exit;
3459 end if;
3461 Next_Entity (E);
3462 end loop;
3463 end;
3464 end if;
3465 end if;
3467 -- Some simple constant-propagation: if the expression is a constant
3468 -- string initialized with a literal, share the literal. This avoids
3469 -- a run-time copy.
3471 if Present (E)
3472 and then Is_Entity_Name (E)
3473 and then Ekind (Entity (E)) = E_Constant
3474 and then Base_Type (Etype (E)) = Standard_String
3475 then
3476 declare
3477 Val : constant Node_Id := Constant_Value (Entity (E));
3478 begin
3479 if Present (Val)
3480 and then Nkind (Val) = N_String_Literal
3481 then
3482 Rewrite (E, New_Copy (Val));
3483 end if;
3484 end;
3485 end if;
3487 -- Another optimization: if the nominal subtype is unconstrained and
3488 -- the expression is a function call that returns an unconstrained
3489 -- type, rewrite the declaration as a renaming of the result of the
3490 -- call. The exceptions below are cases where the copy is expected,
3491 -- either by the back end (Aliased case) or by the semantics, as for
3492 -- initializing controlled types or copying tags for classwide types.
3494 if Present (E)
3495 and then Nkind (E) = N_Explicit_Dereference
3496 and then Nkind (Original_Node (E)) = N_Function_Call
3497 and then not Is_Library_Level_Entity (Id)
3498 and then not Is_Constrained (Underlying_Type (T))
3499 and then not Is_Aliased (Id)
3500 and then not Is_Class_Wide_Type (T)
3501 and then not Is_Controlled (T)
3502 and then not Has_Controlled_Component (Base_Type (T))
3503 and then Expander_Active
3504 then
3505 Rewrite (N,
3506 Make_Object_Renaming_Declaration (Loc,
3507 Defining_Identifier => Id,
3508 Access_Definition => Empty,
3509 Subtype_Mark => New_Occurrence_Of
3510 (Base_Type (Etype (Id)), Loc),
3511 Name => E));
3513 Set_Renamed_Object (Id, E);
3515 -- Force generation of debugging information for the constant and for
3516 -- the renamed function call.
3518 Set_Debug_Info_Needed (Id);
3519 Set_Debug_Info_Needed (Entity (Prefix (E)));
3520 end if;
3522 if Present (Prev_Entity)
3523 and then Is_Frozen (Prev_Entity)
3524 and then not Error_Posted (Id)
3525 then
3526 Error_Msg_N ("full constant declaration appears too late", N);
3527 end if;
3529 Check_Eliminated (Id);
3531 -- Deal with setting In_Private_Part flag if in private part
3533 if Ekind (Scope (Id)) = E_Package
3534 and then In_Private_Part (Scope (Id))
3535 then
3536 Set_In_Private_Part (Id);
3537 end if;
3539 -- Check for violation of No_Local_Timing_Events
3541 if Is_RTE (Etype (Id), RE_Timing_Event)
3542 and then not Is_Library_Level_Entity (Id)
3543 then
3544 Check_Restriction (No_Local_Timing_Events, N);
3545 end if;
3547 <<Leave>>
3548 Analyze_Aspect_Specifications (N, Id, Aspect_Specifications (N));
3549 end Analyze_Object_Declaration;
3551 ---------------------------
3552 -- Analyze_Others_Choice --
3553 ---------------------------
3555 -- Nothing to do for the others choice node itself, the semantic analysis
3556 -- of the others choice will occur as part of the processing of the parent
3558 procedure Analyze_Others_Choice (N : Node_Id) is
3559 pragma Warnings (Off, N);
3560 begin
3561 null;
3562 end Analyze_Others_Choice;
3564 -------------------------------------------
3565 -- Analyze_Private_Extension_Declaration --
3566 -------------------------------------------
3568 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3569 T : constant Entity_Id := Defining_Identifier (N);
3570 Indic : constant Node_Id := Subtype_Indication (N);
3571 Parent_Type : Entity_Id;
3572 Parent_Base : Entity_Id;
3574 begin
3575 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3577 if Is_Non_Empty_List (Interface_List (N)) then
3578 declare
3579 Intf : Node_Id;
3580 T : Entity_Id;
3582 begin
3583 Intf := First (Interface_List (N));
3584 while Present (Intf) loop
3585 T := Find_Type_Of_Subtype_Indic (Intf);
3587 Diagnose_Interface (Intf, T);
3588 Next (Intf);
3589 end loop;
3590 end;
3591 end if;
3593 Generate_Definition (T);
3595 -- For other than Ada 2012, just enter the name in the current scope
3597 if Ada_Version < Ada_2012 then
3598 Enter_Name (T);
3600 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
3601 -- case of private type that completes an incomplete type.
3603 else
3604 declare
3605 Prev : Entity_Id;
3607 begin
3608 Prev := Find_Type_Name (N);
3610 pragma Assert (Prev = T
3611 or else (Ekind (Prev) = E_Incomplete_Type
3612 and then Present (Full_View (Prev))
3613 and then Full_View (Prev) = T));
3614 end;
3615 end if;
3617 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3618 Parent_Base := Base_Type (Parent_Type);
3620 if Parent_Type = Any_Type
3621 or else Etype (Parent_Type) = Any_Type
3622 then
3623 Set_Ekind (T, Ekind (Parent_Type));
3624 Set_Etype (T, Any_Type);
3625 goto Leave;
3627 elsif not Is_Tagged_Type (Parent_Type) then
3628 Error_Msg_N
3629 ("parent of type extension must be a tagged type ", Indic);
3630 goto Leave;
3632 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
3633 Error_Msg_N ("premature derivation of incomplete type", Indic);
3634 goto Leave;
3636 elsif Is_Concurrent_Type (Parent_Type) then
3637 Error_Msg_N
3638 ("parent type of a private extension cannot be "
3639 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3641 Set_Etype (T, Any_Type);
3642 Set_Ekind (T, E_Limited_Private_Type);
3643 Set_Private_Dependents (T, New_Elmt_List);
3644 Set_Error_Posted (T);
3645 goto Leave;
3646 end if;
3648 -- Perhaps the parent type should be changed to the class-wide type's
3649 -- specific type in this case to prevent cascading errors ???
3651 if Is_Class_Wide_Type (Parent_Type) then
3652 Error_Msg_N
3653 ("parent of type extension must not be a class-wide type", Indic);
3654 goto Leave;
3655 end if;
3657 if (not Is_Package_Or_Generic_Package (Current_Scope)
3658 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3659 or else In_Private_Part (Current_Scope)
3661 then
3662 Error_Msg_N ("invalid context for private extension", N);
3663 end if;
3665 -- Set common attributes
3667 Set_Is_Pure (T, Is_Pure (Current_Scope));
3668 Set_Scope (T, Current_Scope);
3669 Set_Ekind (T, E_Record_Type_With_Private);
3670 Init_Size_Align (T);
3672 Set_Etype (T, Parent_Base);
3673 Set_Has_Task (T, Has_Task (Parent_Base));
3675 Set_Convention (T, Convention (Parent_Type));
3676 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3677 Set_Is_First_Subtype (T);
3678 Make_Class_Wide_Type (T);
3680 if Unknown_Discriminants_Present (N) then
3681 Set_Discriminant_Constraint (T, No_Elist);
3682 end if;
3684 Build_Derived_Record_Type (N, Parent_Type, T);
3686 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3687 -- synchronized formal derived type.
3689 if Ada_Version >= Ada_2005
3690 and then Synchronized_Present (N)
3691 then
3692 Set_Is_Limited_Record (T);
3694 -- Formal derived type case
3696 if Is_Generic_Type (T) then
3698 -- The parent must be a tagged limited type or a synchronized
3699 -- interface.
3701 if (not Is_Tagged_Type (Parent_Type)
3702 or else not Is_Limited_Type (Parent_Type))
3703 and then
3704 (not Is_Interface (Parent_Type)
3705 or else not Is_Synchronized_Interface (Parent_Type))
3706 then
3707 Error_Msg_NE ("parent type of & must be tagged limited " &
3708 "or synchronized", N, T);
3709 end if;
3711 -- The progenitors (if any) must be limited or synchronized
3712 -- interfaces.
3714 if Present (Interfaces (T)) then
3715 declare
3716 Iface : Entity_Id;
3717 Iface_Elmt : Elmt_Id;
3719 begin
3720 Iface_Elmt := First_Elmt (Interfaces (T));
3721 while Present (Iface_Elmt) loop
3722 Iface := Node (Iface_Elmt);
3724 if not Is_Limited_Interface (Iface)
3725 and then not Is_Synchronized_Interface (Iface)
3726 then
3727 Error_Msg_NE ("progenitor & must be limited " &
3728 "or synchronized", N, Iface);
3729 end if;
3731 Next_Elmt (Iface_Elmt);
3732 end loop;
3733 end;
3734 end if;
3736 -- Regular derived extension, the parent must be a limited or
3737 -- synchronized interface.
3739 else
3740 if not Is_Interface (Parent_Type)
3741 or else (not Is_Limited_Interface (Parent_Type)
3742 and then
3743 not Is_Synchronized_Interface (Parent_Type))
3744 then
3745 Error_Msg_NE
3746 ("parent type of & must be limited interface", N, T);
3747 end if;
3748 end if;
3750 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
3751 -- extension with a synchronized parent must be explicitly declared
3752 -- synchronized, because the full view will be a synchronized type.
3753 -- This must be checked before the check for limited types below,
3754 -- to ensure that types declared limited are not allowed to extend
3755 -- synchronized interfaces.
3757 elsif Is_Interface (Parent_Type)
3758 and then Is_Synchronized_Interface (Parent_Type)
3759 and then not Synchronized_Present (N)
3760 then
3761 Error_Msg_NE
3762 ("private extension of& must be explicitly synchronized",
3763 N, Parent_Type);
3765 elsif Limited_Present (N) then
3766 Set_Is_Limited_Record (T);
3768 if not Is_Limited_Type (Parent_Type)
3769 and then
3770 (not Is_Interface (Parent_Type)
3771 or else not Is_Limited_Interface (Parent_Type))
3772 then
3773 Error_Msg_NE ("parent type& of limited extension must be limited",
3774 N, Parent_Type);
3775 end if;
3776 end if;
3778 <<Leave>>
3779 Analyze_Aspect_Specifications (N, T, Aspect_Specifications (N));
3780 end Analyze_Private_Extension_Declaration;
3782 ---------------------------------
3783 -- Analyze_Subtype_Declaration --
3784 ---------------------------------
3786 procedure Analyze_Subtype_Declaration
3787 (N : Node_Id;
3788 Skip : Boolean := False)
3790 Id : constant Entity_Id := Defining_Identifier (N);
3791 T : Entity_Id;
3792 R_Checks : Check_Result;
3794 begin
3795 Generate_Definition (Id);
3796 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3797 Init_Size_Align (Id);
3799 -- The following guard condition on Enter_Name is to handle cases where
3800 -- the defining identifier has already been entered into the scope but
3801 -- the declaration as a whole needs to be analyzed.
3803 -- This case in particular happens for derived enumeration types. The
3804 -- derived enumeration type is processed as an inserted enumeration type
3805 -- declaration followed by a rewritten subtype declaration. The defining
3806 -- identifier, however, is entered into the name scope very early in the
3807 -- processing of the original type declaration and therefore needs to be
3808 -- avoided here, when the created subtype declaration is analyzed. (See
3809 -- Build_Derived_Types)
3811 -- This also happens when the full view of a private type is derived
3812 -- type with constraints. In this case the entity has been introduced
3813 -- in the private declaration.
3815 if Skip
3816 or else (Present (Etype (Id))
3817 and then (Is_Private_Type (Etype (Id))
3818 or else Is_Task_Type (Etype (Id))
3819 or else Is_Rewrite_Substitution (N)))
3820 then
3821 null;
3823 else
3824 Enter_Name (Id);
3825 end if;
3827 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3829 -- Inherit common attributes
3831 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
3832 Set_Is_Volatile (Id, Is_Volatile (T));
3833 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3834 Set_Is_Atomic (Id, Is_Atomic (T));
3835 Set_Is_Ada_2005_Only (Id, Is_Ada_2005_Only (T));
3836 Set_Is_Ada_2012_Only (Id, Is_Ada_2012_Only (T));
3837 Set_Convention (Id, Convention (T));
3839 -- In the case where there is no constraint given in the subtype
3840 -- indication, Process_Subtype just returns the Subtype_Mark, so its
3841 -- semantic attributes must be established here.
3843 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
3844 Set_Etype (Id, Base_Type (T));
3846 case Ekind (T) is
3847 when Array_Kind =>
3848 Set_Ekind (Id, E_Array_Subtype);
3849 Copy_Array_Subtype_Attributes (Id, T);
3851 when Decimal_Fixed_Point_Kind =>
3852 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
3853 Set_Digits_Value (Id, Digits_Value (T));
3854 Set_Delta_Value (Id, Delta_Value (T));
3855 Set_Scale_Value (Id, Scale_Value (T));
3856 Set_Small_Value (Id, Small_Value (T));
3857 Set_Scalar_Range (Id, Scalar_Range (T));
3858 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
3859 Set_Is_Constrained (Id, Is_Constrained (T));
3860 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3861 Set_RM_Size (Id, RM_Size (T));
3863 when Enumeration_Kind =>
3864 Set_Ekind (Id, E_Enumeration_Subtype);
3865 Set_First_Literal (Id, First_Literal (Base_Type (T)));
3866 Set_Scalar_Range (Id, Scalar_Range (T));
3867 Set_Is_Character_Type (Id, Is_Character_Type (T));
3868 Set_Is_Constrained (Id, Is_Constrained (T));
3869 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3870 Set_RM_Size (Id, RM_Size (T));
3872 when Ordinary_Fixed_Point_Kind =>
3873 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
3874 Set_Scalar_Range (Id, Scalar_Range (T));
3875 Set_Small_Value (Id, Small_Value (T));
3876 Set_Delta_Value (Id, Delta_Value (T));
3877 Set_Is_Constrained (Id, Is_Constrained (T));
3878 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3879 Set_RM_Size (Id, RM_Size (T));
3881 when Float_Kind =>
3882 Set_Ekind (Id, E_Floating_Point_Subtype);
3883 Set_Scalar_Range (Id, Scalar_Range (T));
3884 Set_Digits_Value (Id, Digits_Value (T));
3885 Set_Is_Constrained (Id, Is_Constrained (T));
3887 when Signed_Integer_Kind =>
3888 Set_Ekind (Id, E_Signed_Integer_Subtype);
3889 Set_Scalar_Range (Id, Scalar_Range (T));
3890 Set_Is_Constrained (Id, Is_Constrained (T));
3891 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3892 Set_RM_Size (Id, RM_Size (T));
3894 when Modular_Integer_Kind =>
3895 Set_Ekind (Id, E_Modular_Integer_Subtype);
3896 Set_Scalar_Range (Id, Scalar_Range (T));
3897 Set_Is_Constrained (Id, Is_Constrained (T));
3898 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3899 Set_RM_Size (Id, RM_Size (T));
3901 when Class_Wide_Kind =>
3902 Set_Ekind (Id, E_Class_Wide_Subtype);
3903 Set_First_Entity (Id, First_Entity (T));
3904 Set_Last_Entity (Id, Last_Entity (T));
3905 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3906 Set_Cloned_Subtype (Id, T);
3907 Set_Is_Tagged_Type (Id, True);
3908 Set_Has_Unknown_Discriminants
3909 (Id, True);
3911 if Ekind (T) = E_Class_Wide_Subtype then
3912 Set_Equivalent_Type (Id, Equivalent_Type (T));
3913 end if;
3915 when E_Record_Type | E_Record_Subtype =>
3916 Set_Ekind (Id, E_Record_Subtype);
3918 if Ekind (T) = E_Record_Subtype
3919 and then Present (Cloned_Subtype (T))
3920 then
3921 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
3922 else
3923 Set_Cloned_Subtype (Id, T);
3924 end if;
3926 Set_First_Entity (Id, First_Entity (T));
3927 Set_Last_Entity (Id, Last_Entity (T));
3928 Set_Has_Discriminants (Id, Has_Discriminants (T));
3929 Set_Is_Constrained (Id, Is_Constrained (T));
3930 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
3931 Set_Has_Unknown_Discriminants
3932 (Id, Has_Unknown_Discriminants (T));
3934 if Has_Discriminants (T) then
3935 Set_Discriminant_Constraint
3936 (Id, Discriminant_Constraint (T));
3937 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3939 elsif Has_Unknown_Discriminants (Id) then
3940 Set_Discriminant_Constraint (Id, No_Elist);
3941 end if;
3943 if Is_Tagged_Type (T) then
3944 Set_Is_Tagged_Type (Id);
3945 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
3946 Set_Direct_Primitive_Operations
3947 (Id, Direct_Primitive_Operations (T));
3948 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3950 if Is_Interface (T) then
3951 Set_Is_Interface (Id);
3952 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
3953 end if;
3954 end if;
3956 when Private_Kind =>
3957 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
3958 Set_Has_Discriminants (Id, Has_Discriminants (T));
3959 Set_Is_Constrained (Id, Is_Constrained (T));
3960 Set_First_Entity (Id, First_Entity (T));
3961 Set_Last_Entity (Id, Last_Entity (T));
3962 Set_Private_Dependents (Id, New_Elmt_List);
3963 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
3964 Set_Has_Unknown_Discriminants
3965 (Id, Has_Unknown_Discriminants (T));
3966 Set_Known_To_Have_Preelab_Init
3967 (Id, Known_To_Have_Preelab_Init (T));
3969 if Is_Tagged_Type (T) then
3970 Set_Is_Tagged_Type (Id);
3971 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
3972 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3973 Set_Direct_Primitive_Operations (Id,
3974 Direct_Primitive_Operations (T));
3975 end if;
3977 -- In general the attributes of the subtype of a private type
3978 -- are the attributes of the partial view of parent. However,
3979 -- the full view may be a discriminated type, and the subtype
3980 -- must share the discriminant constraint to generate correct
3981 -- calls to initialization procedures.
3983 if Has_Discriminants (T) then
3984 Set_Discriminant_Constraint
3985 (Id, Discriminant_Constraint (T));
3986 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3988 elsif Present (Full_View (T))
3989 and then Has_Discriminants (Full_View (T))
3990 then
3991 Set_Discriminant_Constraint
3992 (Id, Discriminant_Constraint (Full_View (T)));
3993 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3995 -- This would seem semantically correct, but apparently
3996 -- confuses the back-end. To be explained and checked with
3997 -- current version ???
3999 -- Set_Has_Discriminants (Id);
4000 end if;
4002 Prepare_Private_Subtype_Completion (Id, N);
4004 when Access_Kind =>
4005 Set_Ekind (Id, E_Access_Subtype);
4006 Set_Is_Constrained (Id, Is_Constrained (T));
4007 Set_Is_Access_Constant
4008 (Id, Is_Access_Constant (T));
4009 Set_Directly_Designated_Type
4010 (Id, Designated_Type (T));
4011 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4013 -- A Pure library_item must not contain the declaration of a
4014 -- named access type, except within a subprogram, generic
4015 -- subprogram, task unit, or protected unit, or if it has
4016 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4018 if Comes_From_Source (Id)
4019 and then In_Pure_Unit
4020 and then not In_Subprogram_Task_Protected_Unit
4021 and then not No_Pool_Assigned (Id)
4022 then
4023 Error_Msg_N
4024 ("named access types not allowed in pure unit", N);
4025 end if;
4027 when Concurrent_Kind =>
4028 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4029 Set_Corresponding_Record_Type (Id,
4030 Corresponding_Record_Type (T));
4031 Set_First_Entity (Id, First_Entity (T));
4032 Set_First_Private_Entity (Id, First_Private_Entity (T));
4033 Set_Has_Discriminants (Id, Has_Discriminants (T));
4034 Set_Is_Constrained (Id, Is_Constrained (T));
4035 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4036 Set_Last_Entity (Id, Last_Entity (T));
4038 if Has_Discriminants (T) then
4039 Set_Discriminant_Constraint (Id,
4040 Discriminant_Constraint (T));
4041 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4042 end if;
4044 when E_Incomplete_Type =>
4045 if Ada_Version >= Ada_2005 then
4046 Set_Ekind (Id, E_Incomplete_Subtype);
4048 -- Ada 2005 (AI-412): Decorate an incomplete subtype
4049 -- of an incomplete type visible through a limited
4050 -- with clause.
4052 if From_With_Type (T)
4053 and then Present (Non_Limited_View (T))
4054 then
4055 Set_From_With_Type (Id);
4056 Set_Non_Limited_View (Id, Non_Limited_View (T));
4058 -- Ada 2005 (AI-412): Add the regular incomplete subtype
4059 -- to the private dependents of the original incomplete
4060 -- type for future transformation.
4062 else
4063 Append_Elmt (Id, Private_Dependents (T));
4064 end if;
4066 -- If the subtype name denotes an incomplete type an error
4067 -- was already reported by Process_Subtype.
4069 else
4070 Set_Etype (Id, Any_Type);
4071 end if;
4073 when others =>
4074 raise Program_Error;
4075 end case;
4076 end if;
4078 if Etype (Id) = Any_Type then
4079 goto Leave;
4080 end if;
4082 -- Some common processing on all types
4084 Set_Size_Info (Id, T);
4085 Set_First_Rep_Item (Id, First_Rep_Item (T));
4087 T := Etype (Id);
4089 Set_Is_Immediately_Visible (Id, True);
4090 Set_Depends_On_Private (Id, Has_Private_Component (T));
4091 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
4093 if Is_Interface (T) then
4094 Set_Is_Interface (Id);
4095 end if;
4097 if Present (Generic_Parent_Type (N))
4098 and then
4099 (Nkind
4100 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
4101 or else Nkind
4102 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
4103 /= N_Formal_Private_Type_Definition)
4104 then
4105 if Is_Tagged_Type (Id) then
4107 -- If this is a generic actual subtype for a synchronized type,
4108 -- the primitive operations are those of the corresponding record
4109 -- for which there is a separate subtype declaration.
4111 if Is_Concurrent_Type (Id) then
4112 null;
4113 elsif Is_Class_Wide_Type (Id) then
4114 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
4115 else
4116 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
4117 end if;
4119 elsif Scope (Etype (Id)) /= Standard_Standard then
4120 Derive_Subprograms (Generic_Parent_Type (N), Id);
4121 end if;
4122 end if;
4124 if Is_Private_Type (T)
4125 and then Present (Full_View (T))
4126 then
4127 Conditional_Delay (Id, Full_View (T));
4129 -- The subtypes of components or subcomponents of protected types
4130 -- do not need freeze nodes, which would otherwise appear in the
4131 -- wrong scope (before the freeze node for the protected type). The
4132 -- proper subtypes are those of the subcomponents of the corresponding
4133 -- record.
4135 elsif Ekind (Scope (Id)) /= E_Protected_Type
4136 and then Present (Scope (Scope (Id))) -- error defense!
4137 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
4138 then
4139 Conditional_Delay (Id, T);
4140 end if;
4142 -- Check that constraint_error is raised for a scalar subtype
4143 -- indication when the lower or upper bound of a non-null range
4144 -- lies outside the range of the type mark.
4146 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4147 if Is_Scalar_Type (Etype (Id))
4148 and then Scalar_Range (Id) /=
4149 Scalar_Range (Etype (Subtype_Mark
4150 (Subtype_Indication (N))))
4151 then
4152 Apply_Range_Check
4153 (Scalar_Range (Id),
4154 Etype (Subtype_Mark (Subtype_Indication (N))));
4156 elsif Is_Array_Type (Etype (Id))
4157 and then Present (First_Index (Id))
4158 then
4159 -- This really should be a subprogram that finds the indications
4160 -- to check???
4162 if ((Nkind (First_Index (Id)) = N_Identifier
4163 and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
4164 or else Nkind (First_Index (Id)) = N_Subtype_Indication)
4165 and then
4166 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
4167 then
4168 declare
4169 Target_Typ : constant Entity_Id :=
4170 Etype
4171 (First_Index (Etype
4172 (Subtype_Mark (Subtype_Indication (N)))));
4173 begin
4174 R_Checks :=
4175 Get_Range_Checks
4176 (Scalar_Range (Etype (First_Index (Id))),
4177 Target_Typ,
4178 Etype (First_Index (Id)),
4179 Defining_Identifier (N));
4181 Insert_Range_Checks
4182 (R_Checks,
4184 Target_Typ,
4185 Sloc (Defining_Identifier (N)));
4186 end;
4187 end if;
4188 end if;
4189 end if;
4191 -- Make sure that generic actual types are properly frozen. The subtype
4192 -- is marked as a generic actual type when the enclosing instance is
4193 -- analyzed, so here we identify the subtype from the tree structure.
4195 if Expander_Active
4196 and then Is_Generic_Actual_Type (Id)
4197 and then In_Instance
4198 and then not Comes_From_Source (N)
4199 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
4200 and then Is_Frozen (T)
4201 then
4202 Freeze_Before (N, Id);
4203 end if;
4205 Set_Optimize_Alignment_Flags (Id);
4206 Check_Eliminated (Id);
4208 <<Leave>>
4209 Analyze_Aspect_Specifications (N, Id, Aspect_Specifications (N));
4210 end Analyze_Subtype_Declaration;
4212 --------------------------------
4213 -- Analyze_Subtype_Indication --
4214 --------------------------------
4216 procedure Analyze_Subtype_Indication (N : Node_Id) is
4217 T : constant Entity_Id := Subtype_Mark (N);
4218 R : constant Node_Id := Range_Expression (Constraint (N));
4220 begin
4221 Analyze (T);
4223 if R /= Error then
4224 Analyze (R);
4225 Set_Etype (N, Etype (R));
4226 Resolve (R, Entity (T));
4227 else
4228 Set_Error_Posted (R);
4229 Set_Error_Posted (T);
4230 end if;
4231 end Analyze_Subtype_Indication;
4233 --------------------------
4234 -- Analyze_Variant_Part --
4235 --------------------------
4237 procedure Analyze_Variant_Part (N : Node_Id) is
4239 procedure Non_Static_Choice_Error (Choice : Node_Id);
4240 -- Error routine invoked by the generic instantiation below when the
4241 -- variant part has a non static choice.
4243 procedure Process_Declarations (Variant : Node_Id);
4244 -- Analyzes all the declarations associated with a Variant. Needed by
4245 -- the generic instantiation below.
4247 package Variant_Choices_Processing is new
4248 Generic_Choices_Processing
4249 (Get_Alternatives => Variants,
4250 Get_Choices => Discrete_Choices,
4251 Process_Empty_Choice => No_OP,
4252 Process_Non_Static_Choice => Non_Static_Choice_Error,
4253 Process_Associated_Node => Process_Declarations);
4254 use Variant_Choices_Processing;
4255 -- Instantiation of the generic choice processing package
4257 -----------------------------
4258 -- Non_Static_Choice_Error --
4259 -----------------------------
4261 procedure Non_Static_Choice_Error (Choice : Node_Id) is
4262 begin
4263 Flag_Non_Static_Expr
4264 ("choice given in variant part is not static!", Choice);
4265 end Non_Static_Choice_Error;
4267 --------------------------
4268 -- Process_Declarations --
4269 --------------------------
4271 procedure Process_Declarations (Variant : Node_Id) is
4272 begin
4273 if not Null_Present (Component_List (Variant)) then
4274 Analyze_Declarations (Component_Items (Component_List (Variant)));
4276 if Present (Variant_Part (Component_List (Variant))) then
4277 Analyze (Variant_Part (Component_List (Variant)));
4278 end if;
4279 end if;
4280 end Process_Declarations;
4282 -- Local Variables
4284 Discr_Name : Node_Id;
4285 Discr_Type : Entity_Id;
4287 Case_Table : Choice_Table_Type (1 .. Number_Of_Choices (N));
4288 Last_Choice : Nat;
4289 Dont_Care : Boolean;
4290 Others_Present : Boolean := False;
4292 pragma Warnings (Off, Case_Table);
4293 pragma Warnings (Off, Last_Choice);
4294 pragma Warnings (Off, Dont_Care);
4295 pragma Warnings (Off, Others_Present);
4296 -- We don't care about the assigned values of any of these
4298 -- Start of processing for Analyze_Variant_Part
4300 begin
4301 Discr_Name := Name (N);
4302 Analyze (Discr_Name);
4304 -- If Discr_Name bad, get out (prevent cascaded errors)
4306 if Etype (Discr_Name) = Any_Type then
4307 return;
4308 end if;
4310 -- Check invalid discriminant in variant part
4312 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4313 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4314 end if;
4316 Discr_Type := Etype (Entity (Discr_Name));
4318 if not Is_Discrete_Type (Discr_Type) then
4319 Error_Msg_N
4320 ("discriminant in a variant part must be of a discrete type",
4321 Name (N));
4322 return;
4323 end if;
4325 -- Call the instantiated Analyze_Choices which does the rest of the work
4327 Analyze_Choices
4328 (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
4329 end Analyze_Variant_Part;
4331 ----------------------------
4332 -- Array_Type_Declaration --
4333 ----------------------------
4335 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4336 Component_Def : constant Node_Id := Component_Definition (Def);
4337 Element_Type : Entity_Id;
4338 Implicit_Base : Entity_Id;
4339 Index : Node_Id;
4340 Related_Id : Entity_Id := Empty;
4341 Nb_Index : Nat;
4342 P : constant Node_Id := Parent (Def);
4343 Priv : Entity_Id;
4345 begin
4346 if Nkind (Def) = N_Constrained_Array_Definition then
4347 Index := First (Discrete_Subtype_Definitions (Def));
4348 else
4349 Index := First (Subtype_Marks (Def));
4350 end if;
4352 -- Find proper names for the implicit types which may be public. In case
4353 -- of anonymous arrays we use the name of the first object of that type
4354 -- as prefix.
4356 if No (T) then
4357 Related_Id := Defining_Identifier (P);
4358 else
4359 Related_Id := T;
4360 end if;
4362 Nb_Index := 1;
4363 while Present (Index) loop
4364 Analyze (Index);
4366 -- Add a subtype declaration for each index of private array type
4367 -- declaration whose etype is also private. For example:
4369 -- package Pkg is
4370 -- type Index is private;
4371 -- private
4372 -- type Table is array (Index) of ...
4373 -- end;
4375 -- This is currently required by the expander for the internally
4376 -- generated equality subprogram of records with variant parts in
4377 -- which the etype of some component is such private type.
4379 if Ekind (Current_Scope) = E_Package
4380 and then In_Private_Part (Current_Scope)
4381 and then Has_Private_Declaration (Etype (Index))
4382 then
4383 declare
4384 Loc : constant Source_Ptr := Sloc (Def);
4385 New_E : Entity_Id;
4386 Decl : Entity_Id;
4388 begin
4389 New_E := Make_Temporary (Loc, 'T');
4390 Set_Is_Internal (New_E);
4392 Decl :=
4393 Make_Subtype_Declaration (Loc,
4394 Defining_Identifier => New_E,
4395 Subtype_Indication =>
4396 New_Occurrence_Of (Etype (Index), Loc));
4398 Insert_Before (Parent (Def), Decl);
4399 Analyze (Decl);
4400 Set_Etype (Index, New_E);
4402 -- If the index is a range the Entity attribute is not
4403 -- available. Example:
4405 -- package Pkg is
4406 -- type T is private;
4407 -- private
4408 -- type T is new Natural;
4409 -- Table : array (T(1) .. T(10)) of Boolean;
4410 -- end Pkg;
4412 if Nkind (Index) /= N_Range then
4413 Set_Entity (Index, New_E);
4414 end if;
4415 end;
4416 end if;
4418 Make_Index (Index, P, Related_Id, Nb_Index);
4419 Next_Index (Index);
4420 Nb_Index := Nb_Index + 1;
4421 end loop;
4423 -- Process subtype indication if one is present
4425 if Present (Subtype_Indication (Component_Def)) then
4426 Element_Type :=
4427 Process_Subtype
4428 (Subtype_Indication (Component_Def), P, Related_Id, 'C');
4430 -- Ada 2005 (AI-230): Access Definition case
4432 else pragma Assert (Present (Access_Definition (Component_Def)));
4434 -- Indicate that the anonymous access type is created by the
4435 -- array type declaration.
4437 Element_Type := Access_Definition
4438 (Related_Nod => P,
4439 N => Access_Definition (Component_Def));
4440 Set_Is_Local_Anonymous_Access (Element_Type);
4442 -- Propagate the parent. This field is needed if we have to generate
4443 -- the master_id associated with an anonymous access to task type
4444 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4446 Set_Parent (Element_Type, Parent (T));
4448 -- Ada 2005 (AI-230): In case of components that are anonymous access
4449 -- types the level of accessibility depends on the enclosing type
4450 -- declaration
4452 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4454 -- Ada 2005 (AI-254)
4456 declare
4457 CD : constant Node_Id :=
4458 Access_To_Subprogram_Definition
4459 (Access_Definition (Component_Def));
4460 begin
4461 if Present (CD) and then Protected_Present (CD) then
4462 Element_Type :=
4463 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4464 end if;
4465 end;
4466 end if;
4468 -- Constrained array case
4470 if No (T) then
4471 T := Create_Itype (E_Void, P, Related_Id, 'T');
4472 end if;
4474 if Nkind (Def) = N_Constrained_Array_Definition then
4476 -- Establish Implicit_Base as unconstrained base type
4478 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4480 Set_Etype (Implicit_Base, Implicit_Base);
4481 Set_Scope (Implicit_Base, Current_Scope);
4482 Set_Has_Delayed_Freeze (Implicit_Base);
4484 -- The constrained array type is a subtype of the unconstrained one
4486 Set_Ekind (T, E_Array_Subtype);
4487 Init_Size_Align (T);
4488 Set_Etype (T, Implicit_Base);
4489 Set_Scope (T, Current_Scope);
4490 Set_Is_Constrained (T, True);
4491 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4492 Set_Has_Delayed_Freeze (T);
4494 -- Complete setup of implicit base type
4496 Set_First_Index (Implicit_Base, First_Index (T));
4497 Set_Component_Type (Implicit_Base, Element_Type);
4498 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4499 Set_Component_Size (Implicit_Base, Uint_0);
4500 Set_Packed_Array_Type (Implicit_Base, Empty);
4501 Set_Has_Controlled_Component
4502 (Implicit_Base, Has_Controlled_Component
4503 (Element_Type)
4504 or else Is_Controlled
4505 (Element_Type));
4506 Set_Finalize_Storage_Only
4507 (Implicit_Base, Finalize_Storage_Only
4508 (Element_Type));
4510 -- Unconstrained array case
4512 else
4513 Set_Ekind (T, E_Array_Type);
4514 Init_Size_Align (T);
4515 Set_Etype (T, T);
4516 Set_Scope (T, Current_Scope);
4517 Set_Component_Size (T, Uint_0);
4518 Set_Is_Constrained (T, False);
4519 Set_First_Index (T, First (Subtype_Marks (Def)));
4520 Set_Has_Delayed_Freeze (T, True);
4521 Set_Has_Task (T, Has_Task (Element_Type));
4522 Set_Has_Controlled_Component (T, Has_Controlled_Component
4523 (Element_Type)
4524 or else
4525 Is_Controlled (Element_Type));
4526 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4527 (Element_Type));
4528 end if;
4530 -- Common attributes for both cases
4532 Set_Component_Type (Base_Type (T), Element_Type);
4533 Set_Packed_Array_Type (T, Empty);
4535 if Aliased_Present (Component_Definition (Def)) then
4536 Set_Has_Aliased_Components (Etype (T));
4537 end if;
4539 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4540 -- array type to ensure that objects of this type are initialized.
4542 if Ada_Version >= Ada_2005
4543 and then Can_Never_Be_Null (Element_Type)
4544 then
4545 Set_Can_Never_Be_Null (T);
4547 if Null_Exclusion_Present (Component_Definition (Def))
4549 -- No need to check itypes because in their case this check was
4550 -- done at their point of creation
4552 and then not Is_Itype (Element_Type)
4553 then
4554 Error_Msg_N
4555 ("`NOT NULL` not allowed (null already excluded)",
4556 Subtype_Indication (Component_Definition (Def)));
4557 end if;
4558 end if;
4560 Priv := Private_Component (Element_Type);
4562 if Present (Priv) then
4564 -- Check for circular definitions
4566 if Priv = Any_Type then
4567 Set_Component_Type (Etype (T), Any_Type);
4569 -- There is a gap in the visibility of operations on the composite
4570 -- type only if the component type is defined in a different scope.
4572 elsif Scope (Priv) = Current_Scope then
4573 null;
4575 elsif Is_Limited_Type (Priv) then
4576 Set_Is_Limited_Composite (Etype (T));
4577 Set_Is_Limited_Composite (T);
4578 else
4579 Set_Is_Private_Composite (Etype (T));
4580 Set_Is_Private_Composite (T);
4581 end if;
4582 end if;
4584 -- A syntax error in the declaration itself may lead to an empty index
4585 -- list, in which case do a minimal patch.
4587 if No (First_Index (T)) then
4588 Error_Msg_N ("missing index definition in array type declaration", T);
4590 declare
4591 Indices : constant List_Id :=
4592 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4593 begin
4594 Set_Discrete_Subtype_Definitions (Def, Indices);
4595 Set_First_Index (T, First (Indices));
4596 return;
4597 end;
4598 end if;
4600 -- Create a concatenation operator for the new type. Internal array
4601 -- types created for packed entities do not need such, they are
4602 -- compatible with the user-defined type.
4604 if Number_Dimensions (T) = 1
4605 and then not Is_Packed_Array_Type (T)
4606 then
4607 New_Concatenation_Op (T);
4608 end if;
4610 -- In the case of an unconstrained array the parser has already verified
4611 -- that all the indices are unconstrained but we still need to make sure
4612 -- that the element type is constrained.
4614 if Is_Indefinite_Subtype (Element_Type) then
4615 Error_Msg_N
4616 ("unconstrained element type in array declaration",
4617 Subtype_Indication (Component_Def));
4619 elsif Is_Abstract_Type (Element_Type) then
4620 Error_Msg_N
4621 ("the type of a component cannot be abstract",
4622 Subtype_Indication (Component_Def));
4623 end if;
4624 end Array_Type_Declaration;
4626 ------------------------------------------------------
4627 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4628 ------------------------------------------------------
4630 function Replace_Anonymous_Access_To_Protected_Subprogram
4631 (N : Node_Id) return Entity_Id
4633 Loc : constant Source_Ptr := Sloc (N);
4635 Curr_Scope : constant Scope_Stack_Entry :=
4636 Scope_Stack.Table (Scope_Stack.Last);
4638 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
4639 Acc : Node_Id;
4640 Comp : Node_Id;
4641 Decl : Node_Id;
4642 P : Node_Id;
4644 begin
4645 Set_Is_Internal (Anon);
4647 case Nkind (N) is
4648 when N_Component_Declaration |
4649 N_Unconstrained_Array_Definition |
4650 N_Constrained_Array_Definition =>
4651 Comp := Component_Definition (N);
4652 Acc := Access_Definition (Comp);
4654 when N_Discriminant_Specification =>
4655 Comp := Discriminant_Type (N);
4656 Acc := Comp;
4658 when N_Parameter_Specification =>
4659 Comp := Parameter_Type (N);
4660 Acc := Comp;
4662 when N_Access_Function_Definition =>
4663 Comp := Result_Definition (N);
4664 Acc := Comp;
4666 when N_Object_Declaration =>
4667 Comp := Object_Definition (N);
4668 Acc := Comp;
4670 when N_Function_Specification =>
4671 Comp := Result_Definition (N);
4672 Acc := Comp;
4674 when others =>
4675 raise Program_Error;
4676 end case;
4678 Decl := Make_Full_Type_Declaration (Loc,
4679 Defining_Identifier => Anon,
4680 Type_Definition =>
4681 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
4683 Mark_Rewrite_Insertion (Decl);
4685 -- Insert the new declaration in the nearest enclosing scope. If the
4686 -- node is a body and N is its return type, the declaration belongs in
4687 -- the enclosing scope.
4689 P := Parent (N);
4691 if Nkind (P) = N_Subprogram_Body
4692 and then Nkind (N) = N_Function_Specification
4693 then
4694 P := Parent (P);
4695 end if;
4697 while Present (P) and then not Has_Declarations (P) loop
4698 P := Parent (P);
4699 end loop;
4701 pragma Assert (Present (P));
4703 if Nkind (P) = N_Package_Specification then
4704 Prepend (Decl, Visible_Declarations (P));
4705 else
4706 Prepend (Decl, Declarations (P));
4707 end if;
4709 -- Replace the anonymous type with an occurrence of the new declaration.
4710 -- In all cases the rewritten node does not have the null-exclusion
4711 -- attribute because (if present) it was already inherited by the
4712 -- anonymous entity (Anon). Thus, in case of components we do not
4713 -- inherit this attribute.
4715 if Nkind (N) = N_Parameter_Specification then
4716 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4717 Set_Etype (Defining_Identifier (N), Anon);
4718 Set_Null_Exclusion_Present (N, False);
4720 elsif Nkind (N) = N_Object_Declaration then
4721 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4722 Set_Etype (Defining_Identifier (N), Anon);
4724 elsif Nkind (N) = N_Access_Function_Definition then
4725 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4727 elsif Nkind (N) = N_Function_Specification then
4728 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4729 Set_Etype (Defining_Unit_Name (N), Anon);
4731 else
4732 Rewrite (Comp,
4733 Make_Component_Definition (Loc,
4734 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4735 end if;
4737 Mark_Rewrite_Insertion (Comp);
4739 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
4740 Analyze (Decl);
4742 else
4743 -- Temporarily remove the current scope (record or subprogram) from
4744 -- the stack to add the new declarations to the enclosing scope.
4746 Scope_Stack.Decrement_Last;
4747 Analyze (Decl);
4748 Set_Is_Itype (Anon);
4749 Scope_Stack.Append (Curr_Scope);
4750 end if;
4752 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
4753 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
4754 return Anon;
4755 end Replace_Anonymous_Access_To_Protected_Subprogram;
4757 -------------------------------
4758 -- Build_Derived_Access_Type --
4759 -------------------------------
4761 procedure Build_Derived_Access_Type
4762 (N : Node_Id;
4763 Parent_Type : Entity_Id;
4764 Derived_Type : Entity_Id)
4766 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4768 Desig_Type : Entity_Id;
4769 Discr : Entity_Id;
4770 Discr_Con_Elist : Elist_Id;
4771 Discr_Con_El : Elmt_Id;
4772 Subt : Entity_Id;
4774 begin
4775 -- Set the designated type so it is available in case this is an access
4776 -- to a self-referential type, e.g. a standard list type with a next
4777 -- pointer. Will be reset after subtype is built.
4779 Set_Directly_Designated_Type
4780 (Derived_Type, Designated_Type (Parent_Type));
4782 Subt := Process_Subtype (S, N);
4784 if Nkind (S) /= N_Subtype_Indication
4785 and then Subt /= Base_Type (Subt)
4786 then
4787 Set_Ekind (Derived_Type, E_Access_Subtype);
4788 end if;
4790 if Ekind (Derived_Type) = E_Access_Subtype then
4791 declare
4792 Pbase : constant Entity_Id := Base_Type (Parent_Type);
4793 Ibase : constant Entity_Id :=
4794 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
4795 Svg_Chars : constant Name_Id := Chars (Ibase);
4796 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
4798 begin
4799 Copy_Node (Pbase, Ibase);
4801 Set_Chars (Ibase, Svg_Chars);
4802 Set_Next_Entity (Ibase, Svg_Next_E);
4803 Set_Sloc (Ibase, Sloc (Derived_Type));
4804 Set_Scope (Ibase, Scope (Derived_Type));
4805 Set_Freeze_Node (Ibase, Empty);
4806 Set_Is_Frozen (Ibase, False);
4807 Set_Comes_From_Source (Ibase, False);
4808 Set_Is_First_Subtype (Ibase, False);
4810 Set_Etype (Ibase, Pbase);
4811 Set_Etype (Derived_Type, Ibase);
4812 end;
4813 end if;
4815 Set_Directly_Designated_Type
4816 (Derived_Type, Designated_Type (Subt));
4818 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
4819 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
4820 Set_Size_Info (Derived_Type, Parent_Type);
4821 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
4822 Set_Depends_On_Private (Derived_Type,
4823 Has_Private_Component (Derived_Type));
4824 Conditional_Delay (Derived_Type, Subt);
4826 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
4827 -- that it is not redundant.
4829 if Null_Exclusion_Present (Type_Definition (N)) then
4830 Set_Can_Never_Be_Null (Derived_Type);
4832 if Can_Never_Be_Null (Parent_Type)
4833 and then False
4834 then
4835 Error_Msg_NE
4836 ("`NOT NULL` not allowed (& already excludes null)",
4837 N, Parent_Type);
4838 end if;
4840 elsif Can_Never_Be_Null (Parent_Type) then
4841 Set_Can_Never_Be_Null (Derived_Type);
4842 end if;
4844 -- Note: we do not copy the Storage_Size_Variable, since we always go to
4845 -- the root type for this information.
4847 -- Apply range checks to discriminants for derived record case
4848 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
4850 Desig_Type := Designated_Type (Derived_Type);
4851 if Is_Composite_Type (Desig_Type)
4852 and then (not Is_Array_Type (Desig_Type))
4853 and then Has_Discriminants (Desig_Type)
4854 and then Base_Type (Desig_Type) /= Desig_Type
4855 then
4856 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
4857 Discr_Con_El := First_Elmt (Discr_Con_Elist);
4859 Discr := First_Discriminant (Base_Type (Desig_Type));
4860 while Present (Discr_Con_El) loop
4861 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
4862 Next_Elmt (Discr_Con_El);
4863 Next_Discriminant (Discr);
4864 end loop;
4865 end if;
4866 end Build_Derived_Access_Type;
4868 ------------------------------
4869 -- Build_Derived_Array_Type --
4870 ------------------------------
4872 procedure Build_Derived_Array_Type
4873 (N : Node_Id;
4874 Parent_Type : Entity_Id;
4875 Derived_Type : Entity_Id)
4877 Loc : constant Source_Ptr := Sloc (N);
4878 Tdef : constant Node_Id := Type_Definition (N);
4879 Indic : constant Node_Id := Subtype_Indication (Tdef);
4880 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
4881 Implicit_Base : Entity_Id;
4882 New_Indic : Node_Id;
4884 procedure Make_Implicit_Base;
4885 -- If the parent subtype is constrained, the derived type is a subtype
4886 -- of an implicit base type derived from the parent base.
4888 ------------------------
4889 -- Make_Implicit_Base --
4890 ------------------------
4892 procedure Make_Implicit_Base is
4893 begin
4894 Implicit_Base :=
4895 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4897 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
4898 Set_Etype (Implicit_Base, Parent_Base);
4900 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
4901 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
4903 Set_Has_Delayed_Freeze (Implicit_Base, True);
4904 end Make_Implicit_Base;
4906 -- Start of processing for Build_Derived_Array_Type
4908 begin
4909 if not Is_Constrained (Parent_Type) then
4910 if Nkind (Indic) /= N_Subtype_Indication then
4911 Set_Ekind (Derived_Type, E_Array_Type);
4913 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4914 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
4916 Set_Has_Delayed_Freeze (Derived_Type, True);
4918 else
4919 Make_Implicit_Base;
4920 Set_Etype (Derived_Type, Implicit_Base);
4922 New_Indic :=
4923 Make_Subtype_Declaration (Loc,
4924 Defining_Identifier => Derived_Type,
4925 Subtype_Indication =>
4926 Make_Subtype_Indication (Loc,
4927 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
4928 Constraint => Constraint (Indic)));
4930 Rewrite (N, New_Indic);
4931 Analyze (N);
4932 end if;
4934 else
4935 if Nkind (Indic) /= N_Subtype_Indication then
4936 Make_Implicit_Base;
4938 Set_Ekind (Derived_Type, Ekind (Parent_Type));
4939 Set_Etype (Derived_Type, Implicit_Base);
4940 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4942 else
4943 Error_Msg_N ("illegal constraint on constrained type", Indic);
4944 end if;
4945 end if;
4947 -- If parent type is not a derived type itself, and is declared in
4948 -- closed scope (e.g. a subprogram), then we must explicitly introduce
4949 -- the new type's concatenation operator since Derive_Subprograms
4950 -- will not inherit the parent's operator. If the parent type is
4951 -- unconstrained, the operator is of the unconstrained base type.
4953 if Number_Dimensions (Parent_Type) = 1
4954 and then not Is_Limited_Type (Parent_Type)
4955 and then not Is_Derived_Type (Parent_Type)
4956 and then not Is_Package_Or_Generic_Package
4957 (Scope (Base_Type (Parent_Type)))
4958 then
4959 if not Is_Constrained (Parent_Type)
4960 and then Is_Constrained (Derived_Type)
4961 then
4962 New_Concatenation_Op (Implicit_Base);
4963 else
4964 New_Concatenation_Op (Derived_Type);
4965 end if;
4966 end if;
4967 end Build_Derived_Array_Type;
4969 -----------------------------------
4970 -- Build_Derived_Concurrent_Type --
4971 -----------------------------------
4973 procedure Build_Derived_Concurrent_Type
4974 (N : Node_Id;
4975 Parent_Type : Entity_Id;
4976 Derived_Type : Entity_Id)
4978 Loc : constant Source_Ptr := Sloc (N);
4980 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
4981 Corr_Decl : Node_Id;
4982 Corr_Decl_Needed : Boolean;
4983 -- If the derived type has fewer discriminants than its parent, the
4984 -- corresponding record is also a derived type, in order to account for
4985 -- the bound discriminants. We create a full type declaration for it in
4986 -- this case.
4988 Constraint_Present : constant Boolean :=
4989 Nkind (Subtype_Indication (Type_Definition (N))) =
4990 N_Subtype_Indication;
4992 D_Constraint : Node_Id;
4993 New_Constraint : Elist_Id;
4994 Old_Disc : Entity_Id;
4995 New_Disc : Entity_Id;
4996 New_N : Node_Id;
4998 begin
4999 Set_Stored_Constraint (Derived_Type, No_Elist);
5000 Corr_Decl_Needed := False;
5001 Old_Disc := Empty;
5003 if Present (Discriminant_Specifications (N))
5004 and then Constraint_Present
5005 then
5006 Old_Disc := First_Discriminant (Parent_Type);
5007 New_Disc := First (Discriminant_Specifications (N));
5008 while Present (New_Disc) and then Present (Old_Disc) loop
5009 Next_Discriminant (Old_Disc);
5010 Next (New_Disc);
5011 end loop;
5012 end if;
5014 if Present (Old_Disc) then
5016 -- The new type has fewer discriminants, so we need to create a new
5017 -- corresponding record, which is derived from the corresponding
5018 -- record of the parent, and has a stored constraint that captures
5019 -- the values of the discriminant constraints.
5021 -- The type declaration for the derived corresponding record has
5022 -- the same discriminant part and constraints as the current
5023 -- declaration. Copy the unanalyzed tree to build declaration.
5025 Corr_Decl_Needed := True;
5026 New_N := Copy_Separate_Tree (N);
5028 Corr_Decl :=
5029 Make_Full_Type_Declaration (Loc,
5030 Defining_Identifier => Corr_Record,
5031 Discriminant_Specifications =>
5032 Discriminant_Specifications (New_N),
5033 Type_Definition =>
5034 Make_Derived_Type_Definition (Loc,
5035 Subtype_Indication =>
5036 Make_Subtype_Indication (Loc,
5037 Subtype_Mark =>
5038 New_Occurrence_Of
5039 (Corresponding_Record_Type (Parent_Type), Loc),
5040 Constraint =>
5041 Constraint
5042 (Subtype_Indication (Type_Definition (New_N))))));
5043 end if;
5045 -- Copy Storage_Size and Relative_Deadline variables if task case
5047 if Is_Task_Type (Parent_Type) then
5048 Set_Storage_Size_Variable (Derived_Type,
5049 Storage_Size_Variable (Parent_Type));
5050 Set_Relative_Deadline_Variable (Derived_Type,
5051 Relative_Deadline_Variable (Parent_Type));
5052 end if;
5054 if Present (Discriminant_Specifications (N)) then
5055 Push_Scope (Derived_Type);
5056 Check_Or_Process_Discriminants (N, Derived_Type);
5058 if Constraint_Present then
5059 New_Constraint :=
5060 Expand_To_Stored_Constraint
5061 (Parent_Type,
5062 Build_Discriminant_Constraints
5063 (Parent_Type,
5064 Subtype_Indication (Type_Definition (N)), True));
5065 end if;
5067 End_Scope;
5069 elsif Constraint_Present then
5071 -- Build constrained subtype and derive from it
5073 declare
5074 Loc : constant Source_Ptr := Sloc (N);
5075 Anon : constant Entity_Id :=
5076 Make_Defining_Identifier (Loc,
5077 New_External_Name (Chars (Derived_Type), 'T'));
5078 Decl : Node_Id;
5080 begin
5081 Decl :=
5082 Make_Subtype_Declaration (Loc,
5083 Defining_Identifier => Anon,
5084 Subtype_Indication =>
5085 Subtype_Indication (Type_Definition (N)));
5086 Insert_Before (N, Decl);
5087 Analyze (Decl);
5089 Rewrite (Subtype_Indication (Type_Definition (N)),
5090 New_Occurrence_Of (Anon, Loc));
5091 Set_Analyzed (Derived_Type, False);
5092 Analyze (N);
5093 return;
5094 end;
5095 end if;
5097 -- By default, operations and private data are inherited from parent.
5098 -- However, in the presence of bound discriminants, a new corresponding
5099 -- record will be created, see below.
5101 Set_Has_Discriminants
5102 (Derived_Type, Has_Discriminants (Parent_Type));
5103 Set_Corresponding_Record_Type
5104 (Derived_Type, Corresponding_Record_Type (Parent_Type));
5106 -- Is_Constrained is set according the parent subtype, but is set to
5107 -- False if the derived type is declared with new discriminants.
5109 Set_Is_Constrained
5110 (Derived_Type,
5111 (Is_Constrained (Parent_Type) or else Constraint_Present)
5112 and then not Present (Discriminant_Specifications (N)));
5114 if Constraint_Present then
5115 if not Has_Discriminants (Parent_Type) then
5116 Error_Msg_N ("untagged parent must have discriminants", N);
5118 elsif Present (Discriminant_Specifications (N)) then
5120 -- Verify that new discriminants are used to constrain old ones
5122 D_Constraint :=
5123 First
5124 (Constraints
5125 (Constraint (Subtype_Indication (Type_Definition (N)))));
5127 Old_Disc := First_Discriminant (Parent_Type);
5129 while Present (D_Constraint) loop
5130 if Nkind (D_Constraint) /= N_Discriminant_Association then
5132 -- Positional constraint. If it is a reference to a new
5133 -- discriminant, it constrains the corresponding old one.
5135 if Nkind (D_Constraint) = N_Identifier then
5136 New_Disc := First_Discriminant (Derived_Type);
5137 while Present (New_Disc) loop
5138 exit when Chars (New_Disc) = Chars (D_Constraint);
5139 Next_Discriminant (New_Disc);
5140 end loop;
5142 if Present (New_Disc) then
5143 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
5144 end if;
5145 end if;
5147 Next_Discriminant (Old_Disc);
5149 -- if this is a named constraint, search by name for the old
5150 -- discriminants constrained by the new one.
5152 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
5154 -- Find new discriminant with that name
5156 New_Disc := First_Discriminant (Derived_Type);
5157 while Present (New_Disc) loop
5158 exit when
5159 Chars (New_Disc) = Chars (Expression (D_Constraint));
5160 Next_Discriminant (New_Disc);
5161 end loop;
5163 if Present (New_Disc) then
5165 -- Verify that new discriminant renames some discriminant
5166 -- of the parent type, and associate the new discriminant
5167 -- with one or more old ones that it renames.
5169 declare
5170 Selector : Node_Id;
5172 begin
5173 Selector := First (Selector_Names (D_Constraint));
5174 while Present (Selector) loop
5175 Old_Disc := First_Discriminant (Parent_Type);
5176 while Present (Old_Disc) loop
5177 exit when Chars (Old_Disc) = Chars (Selector);
5178 Next_Discriminant (Old_Disc);
5179 end loop;
5181 if Present (Old_Disc) then
5182 Set_Corresponding_Discriminant
5183 (New_Disc, Old_Disc);
5184 end if;
5186 Next (Selector);
5187 end loop;
5188 end;
5189 end if;
5190 end if;
5192 Next (D_Constraint);
5193 end loop;
5195 New_Disc := First_Discriminant (Derived_Type);
5196 while Present (New_Disc) loop
5197 if No (Corresponding_Discriminant (New_Disc)) then
5198 Error_Msg_NE
5199 ("new discriminant& must constrain old one", N, New_Disc);
5201 elsif not
5202 Subtypes_Statically_Compatible
5203 (Etype (New_Disc),
5204 Etype (Corresponding_Discriminant (New_Disc)))
5205 then
5206 Error_Msg_NE
5207 ("& not statically compatible with parent discriminant",
5208 N, New_Disc);
5209 end if;
5211 Next_Discriminant (New_Disc);
5212 end loop;
5213 end if;
5215 elsif Present (Discriminant_Specifications (N)) then
5216 Error_Msg_N
5217 ("missing discriminant constraint in untagged derivation", N);
5218 end if;
5220 -- The entity chain of the derived type includes the new discriminants
5221 -- but shares operations with the parent.
5223 if Present (Discriminant_Specifications (N)) then
5224 Old_Disc := First_Discriminant (Parent_Type);
5225 while Present (Old_Disc) loop
5226 if No (Next_Entity (Old_Disc))
5227 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
5228 then
5229 Set_Next_Entity
5230 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
5231 exit;
5232 end if;
5234 Next_Discriminant (Old_Disc);
5235 end loop;
5237 else
5238 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
5239 if Has_Discriminants (Parent_Type) then
5240 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5241 Set_Discriminant_Constraint (
5242 Derived_Type, Discriminant_Constraint (Parent_Type));
5243 end if;
5244 end if;
5246 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
5248 Set_Has_Completion (Derived_Type);
5250 if Corr_Decl_Needed then
5251 Set_Stored_Constraint (Derived_Type, New_Constraint);
5252 Insert_After (N, Corr_Decl);
5253 Analyze (Corr_Decl);
5254 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
5255 end if;
5256 end Build_Derived_Concurrent_Type;
5258 ------------------------------------
5259 -- Build_Derived_Enumeration_Type --
5260 ------------------------------------
5262 procedure Build_Derived_Enumeration_Type
5263 (N : Node_Id;
5264 Parent_Type : Entity_Id;
5265 Derived_Type : Entity_Id)
5267 Loc : constant Source_Ptr := Sloc (N);
5268 Def : constant Node_Id := Type_Definition (N);
5269 Indic : constant Node_Id := Subtype_Indication (Def);
5270 Implicit_Base : Entity_Id;
5271 Literal : Entity_Id;
5272 New_Lit : Entity_Id;
5273 Literals_List : List_Id;
5274 Type_Decl : Node_Id;
5275 Hi, Lo : Node_Id;
5276 Rang_Expr : Node_Id;
5278 begin
5279 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
5280 -- not have explicit literals lists we need to process types derived
5281 -- from them specially. This is handled by Derived_Standard_Character.
5282 -- If the parent type is a generic type, there are no literals either,
5283 -- and we construct the same skeletal representation as for the generic
5284 -- parent type.
5286 if Is_Standard_Character_Type (Parent_Type) then
5287 Derived_Standard_Character (N, Parent_Type, Derived_Type);
5289 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
5290 declare
5291 Lo : Node_Id;
5292 Hi : Node_Id;
5294 begin
5295 if Nkind (Indic) /= N_Subtype_Indication then
5296 Lo :=
5297 Make_Attribute_Reference (Loc,
5298 Attribute_Name => Name_First,
5299 Prefix => New_Reference_To (Derived_Type, Loc));
5300 Set_Etype (Lo, Derived_Type);
5302 Hi :=
5303 Make_Attribute_Reference (Loc,
5304 Attribute_Name => Name_Last,
5305 Prefix => New_Reference_To (Derived_Type, Loc));
5306 Set_Etype (Hi, Derived_Type);
5308 Set_Scalar_Range (Derived_Type,
5309 Make_Range (Loc,
5310 Low_Bound => Lo,
5311 High_Bound => Hi));
5312 else
5314 -- Analyze subtype indication and verify compatibility
5315 -- with parent type.
5317 if Base_Type (Process_Subtype (Indic, N)) /=
5318 Base_Type (Parent_Type)
5319 then
5320 Error_Msg_N
5321 ("illegal constraint for formal discrete type", N);
5322 end if;
5323 end if;
5324 end;
5326 else
5327 -- If a constraint is present, analyze the bounds to catch
5328 -- premature usage of the derived literals.
5330 if Nkind (Indic) = N_Subtype_Indication
5331 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5332 then
5333 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
5334 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5335 end if;
5337 -- Introduce an implicit base type for the derived type even if there
5338 -- is no constraint attached to it, since this seems closer to the
5339 -- Ada semantics. Build a full type declaration tree for the derived
5340 -- type using the implicit base type as the defining identifier. The
5341 -- build a subtype declaration tree which applies the constraint (if
5342 -- any) have it replace the derived type declaration.
5344 Literal := First_Literal (Parent_Type);
5345 Literals_List := New_List;
5346 while Present (Literal)
5347 and then Ekind (Literal) = E_Enumeration_Literal
5348 loop
5349 -- Literals of the derived type have the same representation as
5350 -- those of the parent type, but this representation can be
5351 -- overridden by an explicit representation clause. Indicate
5352 -- that there is no explicit representation given yet. These
5353 -- derived literals are implicit operations of the new type,
5354 -- and can be overridden by explicit ones.
5356 if Nkind (Literal) = N_Defining_Character_Literal then
5357 New_Lit :=
5358 Make_Defining_Character_Literal (Loc, Chars (Literal));
5359 else
5360 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5361 end if;
5363 Set_Ekind (New_Lit, E_Enumeration_Literal);
5364 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
5365 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
5366 Set_Enumeration_Rep_Expr (New_Lit, Empty);
5367 Set_Alias (New_Lit, Literal);
5368 Set_Is_Known_Valid (New_Lit, True);
5370 Append (New_Lit, Literals_List);
5371 Next_Literal (Literal);
5372 end loop;
5374 Implicit_Base :=
5375 Make_Defining_Identifier (Sloc (Derived_Type),
5376 New_External_Name (Chars (Derived_Type), 'B'));
5378 -- Indicate the proper nature of the derived type. This must be done
5379 -- before analysis of the literals, to recognize cases when a literal
5380 -- may be hidden by a previous explicit function definition (cf.
5381 -- c83031a).
5383 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5384 Set_Etype (Derived_Type, Implicit_Base);
5386 Type_Decl :=
5387 Make_Full_Type_Declaration (Loc,
5388 Defining_Identifier => Implicit_Base,
5389 Discriminant_Specifications => No_List,
5390 Type_Definition =>
5391 Make_Enumeration_Type_Definition (Loc, Literals_List));
5393 Mark_Rewrite_Insertion (Type_Decl);
5394 Insert_Before (N, Type_Decl);
5395 Analyze (Type_Decl);
5397 -- After the implicit base is analyzed its Etype needs to be changed
5398 -- to reflect the fact that it is derived from the parent type which
5399 -- was ignored during analysis. We also set the size at this point.
5401 Set_Etype (Implicit_Base, Parent_Type);
5403 Set_Size_Info (Implicit_Base, Parent_Type);
5404 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5405 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5407 -- Copy other flags from parent type
5409 Set_Has_Non_Standard_Rep
5410 (Implicit_Base, Has_Non_Standard_Rep
5411 (Parent_Type));
5412 Set_Has_Pragma_Ordered
5413 (Implicit_Base, Has_Pragma_Ordered
5414 (Parent_Type));
5415 Set_Has_Delayed_Freeze (Implicit_Base);
5417 -- Process the subtype indication including a validation check on the
5418 -- constraint, if any. If a constraint is given, its bounds must be
5419 -- implicitly converted to the new type.
5421 if Nkind (Indic) = N_Subtype_Indication then
5422 declare
5423 R : constant Node_Id :=
5424 Range_Expression (Constraint (Indic));
5426 begin
5427 if Nkind (R) = N_Range then
5428 Hi := Build_Scalar_Bound
5429 (High_Bound (R), Parent_Type, Implicit_Base);
5430 Lo := Build_Scalar_Bound
5431 (Low_Bound (R), Parent_Type, Implicit_Base);
5433 else
5434 -- Constraint is a Range attribute. Replace with explicit
5435 -- mention of the bounds of the prefix, which must be a
5436 -- subtype.
5438 Analyze (Prefix (R));
5439 Hi :=
5440 Convert_To (Implicit_Base,
5441 Make_Attribute_Reference (Loc,
5442 Attribute_Name => Name_Last,
5443 Prefix =>
5444 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5446 Lo :=
5447 Convert_To (Implicit_Base,
5448 Make_Attribute_Reference (Loc,
5449 Attribute_Name => Name_First,
5450 Prefix =>
5451 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5452 end if;
5453 end;
5455 else
5456 Hi :=
5457 Build_Scalar_Bound
5458 (Type_High_Bound (Parent_Type),
5459 Parent_Type, Implicit_Base);
5460 Lo :=
5461 Build_Scalar_Bound
5462 (Type_Low_Bound (Parent_Type),
5463 Parent_Type, Implicit_Base);
5464 end if;
5466 Rang_Expr :=
5467 Make_Range (Loc,
5468 Low_Bound => Lo,
5469 High_Bound => Hi);
5471 -- If we constructed a default range for the case where no range
5472 -- was given, then the expressions in the range must not freeze
5473 -- since they do not correspond to expressions in the source.
5475 if Nkind (Indic) /= N_Subtype_Indication then
5476 Set_Must_Not_Freeze (Lo);
5477 Set_Must_Not_Freeze (Hi);
5478 Set_Must_Not_Freeze (Rang_Expr);
5479 end if;
5481 Rewrite (N,
5482 Make_Subtype_Declaration (Loc,
5483 Defining_Identifier => Derived_Type,
5484 Subtype_Indication =>
5485 Make_Subtype_Indication (Loc,
5486 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5487 Constraint =>
5488 Make_Range_Constraint (Loc,
5489 Range_Expression => Rang_Expr))));
5491 Analyze (N);
5493 -- If pragma Discard_Names applies on the first subtype of the parent
5494 -- type, then it must be applied on this subtype as well.
5496 if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5497 Set_Discard_Names (Derived_Type);
5498 end if;
5500 -- Apply a range check. Since this range expression doesn't have an
5501 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5502 -- this right???
5504 if Nkind (Indic) = N_Subtype_Indication then
5505 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5506 Parent_Type,
5507 Source_Typ => Entity (Subtype_Mark (Indic)));
5508 end if;
5509 end if;
5510 end Build_Derived_Enumeration_Type;
5512 --------------------------------
5513 -- Build_Derived_Numeric_Type --
5514 --------------------------------
5516 procedure Build_Derived_Numeric_Type
5517 (N : Node_Id;
5518 Parent_Type : Entity_Id;
5519 Derived_Type : Entity_Id)
5521 Loc : constant Source_Ptr := Sloc (N);
5522 Tdef : constant Node_Id := Type_Definition (N);
5523 Indic : constant Node_Id := Subtype_Indication (Tdef);
5524 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5525 No_Constraint : constant Boolean := Nkind (Indic) /=
5526 N_Subtype_Indication;
5527 Implicit_Base : Entity_Id;
5529 Lo : Node_Id;
5530 Hi : Node_Id;
5532 begin
5533 -- Process the subtype indication including a validation check on
5534 -- the constraint if any.
5536 Discard_Node (Process_Subtype (Indic, N));
5538 -- Introduce an implicit base type for the derived type even if there
5539 -- is no constraint attached to it, since this seems closer to the Ada
5540 -- semantics.
5542 Implicit_Base :=
5543 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5545 Set_Etype (Implicit_Base, Parent_Base);
5546 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5547 Set_Size_Info (Implicit_Base, Parent_Base);
5548 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5549 Set_Parent (Implicit_Base, Parent (Derived_Type));
5550 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
5552 -- Set RM Size for discrete type or decimal fixed-point type
5553 -- Ordinary fixed-point is excluded, why???
5555 if Is_Discrete_Type (Parent_Base)
5556 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5557 then
5558 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5559 end if;
5561 Set_Has_Delayed_Freeze (Implicit_Base);
5563 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5564 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5566 Set_Scalar_Range (Implicit_Base,
5567 Make_Range (Loc,
5568 Low_Bound => Lo,
5569 High_Bound => Hi));
5571 if Has_Infinities (Parent_Base) then
5572 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5573 end if;
5575 -- The Derived_Type, which is the entity of the declaration, is a
5576 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5577 -- absence of an explicit constraint.
5579 Set_Etype (Derived_Type, Implicit_Base);
5581 -- If we did not have a constraint, then the Ekind is set from the
5582 -- parent type (otherwise Process_Subtype has set the bounds)
5584 if No_Constraint then
5585 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5586 end if;
5588 -- If we did not have a range constraint, then set the range from the
5589 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
5591 if No_Constraint
5592 or else not Has_Range_Constraint (Indic)
5593 then
5594 Set_Scalar_Range (Derived_Type,
5595 Make_Range (Loc,
5596 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
5597 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5598 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5600 if Has_Infinities (Parent_Type) then
5601 Set_Includes_Infinities (Scalar_Range (Derived_Type));
5602 end if;
5604 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
5605 end if;
5607 Set_Is_Descendent_Of_Address (Derived_Type,
5608 Is_Descendent_Of_Address (Parent_Type));
5609 Set_Is_Descendent_Of_Address (Implicit_Base,
5610 Is_Descendent_Of_Address (Parent_Type));
5612 -- Set remaining type-specific fields, depending on numeric type
5614 if Is_Modular_Integer_Type (Parent_Type) then
5615 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5617 Set_Non_Binary_Modulus
5618 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5620 Set_Is_Known_Valid
5621 (Implicit_Base, Is_Known_Valid (Parent_Base));
5623 elsif Is_Floating_Point_Type (Parent_Type) then
5625 -- Digits of base type is always copied from the digits value of
5626 -- the parent base type, but the digits of the derived type will
5627 -- already have been set if there was a constraint present.
5629 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5630 Set_Vax_Float (Implicit_Base, Vax_Float (Parent_Base));
5632 if No_Constraint then
5633 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5634 end if;
5636 elsif Is_Fixed_Point_Type (Parent_Type) then
5638 -- Small of base type and derived type are always copied from the
5639 -- parent base type, since smalls never change. The delta of the
5640 -- base type is also copied from the parent base type. However the
5641 -- delta of the derived type will have been set already if a
5642 -- constraint was present.
5644 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
5645 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5646 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5648 if No_Constraint then
5649 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
5650 end if;
5652 -- The scale and machine radix in the decimal case are always
5653 -- copied from the parent base type.
5655 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5656 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
5657 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5659 Set_Machine_Radix_10
5660 (Derived_Type, Machine_Radix_10 (Parent_Base));
5661 Set_Machine_Radix_10
5662 (Implicit_Base, Machine_Radix_10 (Parent_Base));
5664 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5666 if No_Constraint then
5667 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5669 else
5670 -- the analysis of the subtype_indication sets the
5671 -- digits value of the derived type.
5673 null;
5674 end if;
5675 end if;
5676 end if;
5678 -- The type of the bounds is that of the parent type, and they
5679 -- must be converted to the derived type.
5681 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5683 -- The implicit_base should be frozen when the derived type is frozen,
5684 -- but note that it is used in the conversions of the bounds. For fixed
5685 -- types we delay the determination of the bounds until the proper
5686 -- freezing point. For other numeric types this is rejected by GCC, for
5687 -- reasons that are currently unclear (???), so we choose to freeze the
5688 -- implicit base now. In the case of integers and floating point types
5689 -- this is harmless because subsequent representation clauses cannot
5690 -- affect anything, but it is still baffling that we cannot use the
5691 -- same mechanism for all derived numeric types.
5693 -- There is a further complication: actually *some* representation
5694 -- clauses can affect the implicit base type. Namely, attribute
5695 -- definition clauses for stream-oriented attributes need to set the
5696 -- corresponding TSS entries on the base type, and this normally cannot
5697 -- be done after the base type is frozen, so the circuitry in
5698 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5699 -- not use Set_TSS in this case.
5701 if Is_Fixed_Point_Type (Parent_Type) then
5702 Conditional_Delay (Implicit_Base, Parent_Type);
5703 else
5704 Freeze_Before (N, Implicit_Base);
5705 end if;
5706 end Build_Derived_Numeric_Type;
5708 --------------------------------
5709 -- Build_Derived_Private_Type --
5710 --------------------------------
5712 procedure Build_Derived_Private_Type
5713 (N : Node_Id;
5714 Parent_Type : Entity_Id;
5715 Derived_Type : Entity_Id;
5716 Is_Completion : Boolean;
5717 Derive_Subps : Boolean := True)
5719 Loc : constant Source_Ptr := Sloc (N);
5720 Der_Base : Entity_Id;
5721 Discr : Entity_Id;
5722 Full_Decl : Node_Id := Empty;
5723 Full_Der : Entity_Id;
5724 Full_P : Entity_Id;
5725 Last_Discr : Entity_Id;
5726 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
5727 Swapped : Boolean := False;
5729 procedure Copy_And_Build;
5730 -- Copy derived type declaration, replace parent with its full view,
5731 -- and analyze new declaration.
5733 --------------------
5734 -- Copy_And_Build --
5735 --------------------
5737 procedure Copy_And_Build is
5738 Full_N : Node_Id;
5740 begin
5741 if Ekind (Parent_Type) in Record_Kind
5742 or else
5743 (Ekind (Parent_Type) in Enumeration_Kind
5744 and then not Is_Standard_Character_Type (Parent_Type)
5745 and then not Is_Generic_Type (Root_Type (Parent_Type)))
5746 then
5747 Full_N := New_Copy_Tree (N);
5748 Insert_After (N, Full_N);
5749 Build_Derived_Type (
5750 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5752 else
5753 Build_Derived_Type (
5754 N, Parent_Type, Full_Der, True, Derive_Subps => False);
5755 end if;
5756 end Copy_And_Build;
5758 -- Start of processing for Build_Derived_Private_Type
5760 begin
5761 if Is_Tagged_Type (Parent_Type) then
5762 Full_P := Full_View (Parent_Type);
5764 -- A type extension of a type with unknown discriminants is an
5765 -- indefinite type that the back-end cannot handle directly.
5766 -- We treat it as a private type, and build a completion that is
5767 -- derived from the full view of the parent, and hopefully has
5768 -- known discriminants.
5770 -- If the full view of the parent type has an underlying record view,
5771 -- use it to generate the underlying record view of this derived type
5772 -- (required for chains of derivations with unknown discriminants).
5774 -- Minor optimization: we avoid the generation of useless underlying
5775 -- record view entities if the private type declaration has unknown
5776 -- discriminants but its corresponding full view has no
5777 -- discriminants.
5779 if Has_Unknown_Discriminants (Parent_Type)
5780 and then Present (Full_P)
5781 and then (Has_Discriminants (Full_P)
5782 or else Present (Underlying_Record_View (Full_P)))
5783 and then not In_Open_Scopes (Par_Scope)
5784 and then Expander_Active
5785 then
5786 declare
5787 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
5788 New_Ext : constant Node_Id :=
5789 Copy_Separate_Tree
5790 (Record_Extension_Part (Type_Definition (N)));
5791 Decl : Node_Id;
5793 begin
5794 Build_Derived_Record_Type
5795 (N, Parent_Type, Derived_Type, Derive_Subps);
5797 -- Build anonymous completion, as a derivation from the full
5798 -- view of the parent. This is not a completion in the usual
5799 -- sense, because the current type is not private.
5801 Decl :=
5802 Make_Full_Type_Declaration (Loc,
5803 Defining_Identifier => Full_Der,
5804 Type_Definition =>
5805 Make_Derived_Type_Definition (Loc,
5806 Subtype_Indication =>
5807 New_Copy_Tree
5808 (Subtype_Indication (Type_Definition (N))),
5809 Record_Extension_Part => New_Ext));
5811 -- If the parent type has an underlying record view, use it
5812 -- here to build the new underlying record view.
5814 if Present (Underlying_Record_View (Full_P)) then
5815 pragma Assert
5816 (Nkind (Subtype_Indication (Type_Definition (Decl)))
5817 = N_Identifier);
5818 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
5819 Underlying_Record_View (Full_P));
5820 end if;
5822 Install_Private_Declarations (Par_Scope);
5823 Install_Visible_Declarations (Par_Scope);
5824 Insert_Before (N, Decl);
5826 -- Mark entity as an underlying record view before analysis,
5827 -- to avoid generating the list of its primitive operations
5828 -- (which is not really required for this entity) and thus
5829 -- prevent spurious errors associated with missing overriding
5830 -- of abstract primitives (overridden only for Derived_Type).
5832 Set_Ekind (Full_Der, E_Record_Type);
5833 Set_Is_Underlying_Record_View (Full_Der);
5835 Analyze (Decl);
5837 pragma Assert (Has_Discriminants (Full_Der)
5838 and then not Has_Unknown_Discriminants (Full_Der));
5840 Uninstall_Declarations (Par_Scope);
5842 -- Freeze the underlying record view, to prevent generation of
5843 -- useless dispatching information, which is simply shared with
5844 -- the real derived type.
5846 Set_Is_Frozen (Full_Der);
5848 -- Set up links between real entity and underlying record view
5850 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
5851 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
5852 end;
5854 -- If discriminants are known, build derived record
5856 else
5857 Build_Derived_Record_Type
5858 (N, Parent_Type, Derived_Type, Derive_Subps);
5859 end if;
5861 return;
5863 elsif Has_Discriminants (Parent_Type) then
5864 if Present (Full_View (Parent_Type)) then
5865 if not Is_Completion then
5867 -- Copy declaration for subsequent analysis, to provide a
5868 -- completion for what is a private declaration. Indicate that
5869 -- the full type is internally generated.
5871 Full_Decl := New_Copy_Tree (N);
5872 Full_Der := New_Copy (Derived_Type);
5873 Set_Comes_From_Source (Full_Decl, False);
5874 Set_Comes_From_Source (Full_Der, False);
5875 Set_Parent (Full_Der, Full_Decl);
5877 Insert_After (N, Full_Decl);
5879 else
5880 -- If this is a completion, the full view being built is itself
5881 -- private. We build a subtype of the parent with the same
5882 -- constraints as this full view, to convey to the back end the
5883 -- constrained components and the size of this subtype. If the
5884 -- parent is constrained, its full view can serve as the
5885 -- underlying full view of the derived type.
5887 if No (Discriminant_Specifications (N)) then
5888 if Nkind (Subtype_Indication (Type_Definition (N))) =
5889 N_Subtype_Indication
5890 then
5891 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
5893 elsif Is_Constrained (Full_View (Parent_Type)) then
5894 Set_Underlying_Full_View
5895 (Derived_Type, Full_View (Parent_Type));
5896 end if;
5898 else
5899 -- If there are new discriminants, the parent subtype is
5900 -- constrained by them, but it is not clear how to build
5901 -- the Underlying_Full_View in this case???
5903 null;
5904 end if;
5905 end if;
5906 end if;
5908 -- Build partial view of derived type from partial view of parent
5910 Build_Derived_Record_Type
5911 (N, Parent_Type, Derived_Type, Derive_Subps);
5913 if Present (Full_View (Parent_Type)) and then not Is_Completion then
5914 if not In_Open_Scopes (Par_Scope)
5915 or else not In_Same_Source_Unit (N, Parent_Type)
5916 then
5917 -- Swap partial and full views temporarily
5919 Install_Private_Declarations (Par_Scope);
5920 Install_Visible_Declarations (Par_Scope);
5921 Swapped := True;
5922 end if;
5924 -- Build full view of derived type from full view of parent which
5925 -- is now installed. Subprograms have been derived on the partial
5926 -- view, the completion does not derive them anew.
5928 if not Is_Tagged_Type (Parent_Type) then
5930 -- If the parent is itself derived from another private type,
5931 -- installing the private declarations has not affected its
5932 -- privacy status, so use its own full view explicitly.
5934 if Is_Private_Type (Parent_Type) then
5935 Build_Derived_Record_Type
5936 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
5937 else
5938 Build_Derived_Record_Type
5939 (Full_Decl, Parent_Type, Full_Der, False);
5940 end if;
5942 else
5943 -- If full view of parent is tagged, the completion inherits
5944 -- the proper primitive operations.
5946 Set_Defining_Identifier (Full_Decl, Full_Der);
5947 Build_Derived_Record_Type
5948 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
5949 end if;
5951 -- The full declaration has been introduced into the tree and
5952 -- processed in the step above. It should not be analyzed again
5953 -- (when encountered later in the current list of declarations)
5954 -- to prevent spurious name conflicts. The full entity remains
5955 -- invisible.
5957 Set_Analyzed (Full_Decl);
5959 if Swapped then
5960 Uninstall_Declarations (Par_Scope);
5962 if In_Open_Scopes (Par_Scope) then
5963 Install_Visible_Declarations (Par_Scope);
5964 end if;
5965 end if;
5967 Der_Base := Base_Type (Derived_Type);
5968 Set_Full_View (Derived_Type, Full_Der);
5969 Set_Full_View (Der_Base, Base_Type (Full_Der));
5971 -- Copy the discriminant list from full view to the partial views
5972 -- (base type and its subtype). Gigi requires that the partial and
5973 -- full views have the same discriminants.
5975 -- Note that since the partial view is pointing to discriminants
5976 -- in the full view, their scope will be that of the full view.
5977 -- This might cause some front end problems and need adjustment???
5979 Discr := First_Discriminant (Base_Type (Full_Der));
5980 Set_First_Entity (Der_Base, Discr);
5982 loop
5983 Last_Discr := Discr;
5984 Next_Discriminant (Discr);
5985 exit when No (Discr);
5986 end loop;
5988 Set_Last_Entity (Der_Base, Last_Discr);
5990 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
5991 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
5992 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
5994 else
5995 -- If this is a completion, the derived type stays private and
5996 -- there is no need to create a further full view, except in the
5997 -- unusual case when the derivation is nested within a child unit,
5998 -- see below.
6000 null;
6001 end if;
6003 elsif Present (Full_View (Parent_Type))
6004 and then Has_Discriminants (Full_View (Parent_Type))
6005 then
6006 if Has_Unknown_Discriminants (Parent_Type)
6007 and then Nkind (Subtype_Indication (Type_Definition (N))) =
6008 N_Subtype_Indication
6009 then
6010 Error_Msg_N
6011 ("cannot constrain type with unknown discriminants",
6012 Subtype_Indication (Type_Definition (N)));
6013 return;
6014 end if;
6016 -- If full view of parent is a record type, build full view as a
6017 -- derivation from the parent's full view. Partial view remains
6018 -- private. For code generation and linking, the full view must have
6019 -- the same public status as the partial one. This full view is only
6020 -- needed if the parent type is in an enclosing scope, so that the
6021 -- full view may actually become visible, e.g. in a child unit. This
6022 -- is both more efficient, and avoids order of freezing problems with
6023 -- the added entities.
6025 if not Is_Private_Type (Full_View (Parent_Type))
6026 and then (In_Open_Scopes (Scope (Parent_Type)))
6027 then
6028 Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
6029 Chars (Derived_Type));
6030 Set_Is_Itype (Full_Der);
6031 Set_Has_Private_Declaration (Full_Der);
6032 Set_Has_Private_Declaration (Derived_Type);
6033 Set_Associated_Node_For_Itype (Full_Der, N);
6034 Set_Parent (Full_Der, Parent (Derived_Type));
6035 Set_Full_View (Derived_Type, Full_Der);
6036 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
6037 Full_P := Full_View (Parent_Type);
6038 Exchange_Declarations (Parent_Type);
6039 Copy_And_Build;
6040 Exchange_Declarations (Full_P);
6042 else
6043 Build_Derived_Record_Type
6044 (N, Full_View (Parent_Type), Derived_Type,
6045 Derive_Subps => False);
6046 end if;
6048 -- In any case, the primitive operations are inherited from the
6049 -- parent type, not from the internal full view.
6051 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
6053 if Derive_Subps then
6054 Derive_Subprograms (Parent_Type, Derived_Type);
6055 end if;
6057 else
6058 -- Untagged type, No discriminants on either view
6060 if Nkind (Subtype_Indication (Type_Definition (N))) =
6061 N_Subtype_Indication
6062 then
6063 Error_Msg_N
6064 ("illegal constraint on type without discriminants", N);
6065 end if;
6067 if Present (Discriminant_Specifications (N))
6068 and then Present (Full_View (Parent_Type))
6069 and then not Is_Tagged_Type (Full_View (Parent_Type))
6070 then
6071 Error_Msg_N ("cannot add discriminants to untagged type", N);
6072 end if;
6074 Set_Stored_Constraint (Derived_Type, No_Elist);
6075 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6076 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
6077 Set_Has_Controlled_Component
6078 (Derived_Type, Has_Controlled_Component
6079 (Parent_Type));
6081 -- Direct controlled types do not inherit Finalize_Storage_Only flag
6083 if not Is_Controlled (Parent_Type) then
6084 Set_Finalize_Storage_Only
6085 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
6086 end if;
6088 -- Construct the implicit full view by deriving from full view of the
6089 -- parent type. In order to get proper visibility, we install the
6090 -- parent scope and its declarations.
6092 -- ??? If the parent is untagged private and its completion is
6093 -- tagged, this mechanism will not work because we cannot derive from
6094 -- the tagged full view unless we have an extension.
6096 if Present (Full_View (Parent_Type))
6097 and then not Is_Tagged_Type (Full_View (Parent_Type))
6098 and then not Is_Completion
6099 then
6100 Full_Der :=
6101 Make_Defining_Identifier (Sloc (Derived_Type),
6102 Chars => Chars (Derived_Type));
6103 Set_Is_Itype (Full_Der);
6104 Set_Has_Private_Declaration (Full_Der);
6105 Set_Has_Private_Declaration (Derived_Type);
6106 Set_Associated_Node_For_Itype (Full_Der, N);
6107 Set_Parent (Full_Der, Parent (Derived_Type));
6108 Set_Full_View (Derived_Type, Full_Der);
6110 if not In_Open_Scopes (Par_Scope) then
6111 Install_Private_Declarations (Par_Scope);
6112 Install_Visible_Declarations (Par_Scope);
6113 Copy_And_Build;
6114 Uninstall_Declarations (Par_Scope);
6116 -- If parent scope is open and in another unit, and parent has a
6117 -- completion, then the derivation is taking place in the visible
6118 -- part of a child unit. In that case retrieve the full view of
6119 -- the parent momentarily.
6121 elsif not In_Same_Source_Unit (N, Parent_Type) then
6122 Full_P := Full_View (Parent_Type);
6123 Exchange_Declarations (Parent_Type);
6124 Copy_And_Build;
6125 Exchange_Declarations (Full_P);
6127 -- Otherwise it is a local derivation
6129 else
6130 Copy_And_Build;
6131 end if;
6133 Set_Scope (Full_Der, Current_Scope);
6134 Set_Is_First_Subtype (Full_Der,
6135 Is_First_Subtype (Derived_Type));
6136 Set_Has_Size_Clause (Full_Der, False);
6137 Set_Has_Alignment_Clause (Full_Der, False);
6138 Set_Next_Entity (Full_Der, Empty);
6139 Set_Has_Delayed_Freeze (Full_Der);
6140 Set_Is_Frozen (Full_Der, False);
6141 Set_Freeze_Node (Full_Der, Empty);
6142 Set_Depends_On_Private (Full_Der,
6143 Has_Private_Component (Full_Der));
6144 Set_Public_Status (Full_Der);
6145 end if;
6146 end if;
6148 Set_Has_Unknown_Discriminants (Derived_Type,
6149 Has_Unknown_Discriminants (Parent_Type));
6151 if Is_Private_Type (Derived_Type) then
6152 Set_Private_Dependents (Derived_Type, New_Elmt_List);
6153 end if;
6155 if Is_Private_Type (Parent_Type)
6156 and then Base_Type (Parent_Type) = Parent_Type
6157 and then In_Open_Scopes (Scope (Parent_Type))
6158 then
6159 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
6161 if Is_Child_Unit (Scope (Current_Scope))
6162 and then Is_Completion
6163 and then In_Private_Part (Current_Scope)
6164 and then Scope (Parent_Type) /= Current_Scope
6165 then
6166 -- This is the unusual case where a type completed by a private
6167 -- derivation occurs within a package nested in a child unit, and
6168 -- the parent is declared in an ancestor. In this case, the full
6169 -- view of the parent type will become visible in the body of
6170 -- the enclosing child, and only then will the current type be
6171 -- possibly non-private. We build a underlying full view that
6172 -- will be installed when the enclosing child body is compiled.
6174 Full_Der :=
6175 Make_Defining_Identifier (Sloc (Derived_Type),
6176 Chars => Chars (Derived_Type));
6177 Set_Is_Itype (Full_Der);
6178 Build_Itype_Reference (Full_Der, N);
6180 -- The full view will be used to swap entities on entry/exit to
6181 -- the body, and must appear in the entity list for the package.
6183 Append_Entity (Full_Der, Scope (Derived_Type));
6184 Set_Has_Private_Declaration (Full_Der);
6185 Set_Has_Private_Declaration (Derived_Type);
6186 Set_Associated_Node_For_Itype (Full_Der, N);
6187 Set_Parent (Full_Der, Parent (Derived_Type));
6188 Full_P := Full_View (Parent_Type);
6189 Exchange_Declarations (Parent_Type);
6190 Copy_And_Build;
6191 Exchange_Declarations (Full_P);
6192 Set_Underlying_Full_View (Derived_Type, Full_Der);
6193 end if;
6194 end if;
6195 end Build_Derived_Private_Type;
6197 -------------------------------
6198 -- Build_Derived_Record_Type --
6199 -------------------------------
6201 -- 1. INTRODUCTION
6203 -- Ideally we would like to use the same model of type derivation for
6204 -- tagged and untagged record types. Unfortunately this is not quite
6205 -- possible because the semantics of representation clauses is different
6206 -- for tagged and untagged records under inheritance. Consider the
6207 -- following:
6209 -- type R (...) is [tagged] record ... end record;
6210 -- type T (...) is new R (...) [with ...];
6212 -- The representation clauses for T can specify a completely different
6213 -- record layout from R's. Hence the same component can be placed in two
6214 -- very different positions in objects of type T and R. If R and T are
6215 -- tagged types, representation clauses for T can only specify the layout
6216 -- of non inherited components, thus components that are common in R and T
6217 -- have the same position in objects of type R and T.
6219 -- This has two implications. The first is that the entire tree for R's
6220 -- declaration needs to be copied for T in the untagged case, so that T
6221 -- can be viewed as a record type of its own with its own representation
6222 -- clauses. The second implication is the way we handle discriminants.
6223 -- Specifically, in the untagged case we need a way to communicate to Gigi
6224 -- what are the real discriminants in the record, while for the semantics
6225 -- we need to consider those introduced by the user to rename the
6226 -- discriminants in the parent type. This is handled by introducing the
6227 -- notion of stored discriminants. See below for more.
6229 -- Fortunately the way regular components are inherited can be handled in
6230 -- the same way in tagged and untagged types.
6232 -- To complicate things a bit more the private view of a private extension
6233 -- cannot be handled in the same way as the full view (for one thing the
6234 -- semantic rules are somewhat different). We will explain what differs
6235 -- below.
6237 -- 2. DISCRIMINANTS UNDER INHERITANCE
6239 -- The semantic rules governing the discriminants of derived types are
6240 -- quite subtle.
6242 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
6243 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
6245 -- If parent type has discriminants, then the discriminants that are
6246 -- declared in the derived type are [3.4 (11)]:
6248 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
6249 -- there is one;
6251 -- o Otherwise, each discriminant of the parent type (implicitly declared
6252 -- in the same order with the same specifications). In this case, the
6253 -- discriminants are said to be "inherited", or if unknown in the parent
6254 -- are also unknown in the derived type.
6256 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
6258 -- o The parent subtype shall be constrained;
6260 -- o If the parent type is not a tagged type, then each discriminant of
6261 -- the derived type shall be used in the constraint defining a parent
6262 -- subtype. [Implementation note: This ensures that the new discriminant
6263 -- can share storage with an existing discriminant.]
6265 -- For the derived type each discriminant of the parent type is either
6266 -- inherited, constrained to equal some new discriminant of the derived
6267 -- type, or constrained to the value of an expression.
6269 -- When inherited or constrained to equal some new discriminant, the
6270 -- parent discriminant and the discriminant of the derived type are said
6271 -- to "correspond".
6273 -- If a discriminant of the parent type is constrained to a specific value
6274 -- in the derived type definition, then the discriminant is said to be
6275 -- "specified" by that derived type definition.
6277 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
6279 -- We have spoken about stored discriminants in point 1 (introduction)
6280 -- above. There are two sort of stored discriminants: implicit and
6281 -- explicit. As long as the derived type inherits the same discriminants as
6282 -- the root record type, stored discriminants are the same as regular
6283 -- discriminants, and are said to be implicit. However, if any discriminant
6284 -- in the root type was renamed in the derived type, then the derived
6285 -- type will contain explicit stored discriminants. Explicit stored
6286 -- discriminants are discriminants in addition to the semantically visible
6287 -- discriminants defined for the derived type. Stored discriminants are
6288 -- used by Gigi to figure out what are the physical discriminants in
6289 -- objects of the derived type (see precise definition in einfo.ads).
6290 -- As an example, consider the following:
6292 -- type R (D1, D2, D3 : Int) is record ... end record;
6293 -- type T1 is new R;
6294 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
6295 -- type T3 is new T2;
6296 -- type T4 (Y : Int) is new T3 (Y, 99);
6298 -- The following table summarizes the discriminants and stored
6299 -- discriminants in R and T1 through T4.
6301 -- Type Discrim Stored Discrim Comment
6302 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
6303 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
6304 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
6305 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
6306 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
6308 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
6309 -- find the corresponding discriminant in the parent type, while
6310 -- Original_Record_Component (abbreviated ORC below), the actual physical
6311 -- component that is renamed. Finally the field Is_Completely_Hidden
6312 -- (abbreviated ICH below) is set for all explicit stored discriminants
6313 -- (see einfo.ads for more info). For the above example this gives:
6315 -- Discrim CD ORC ICH
6316 -- ^^^^^^^ ^^ ^^^ ^^^
6317 -- D1 in R empty itself no
6318 -- D2 in R empty itself no
6319 -- D3 in R empty itself no
6321 -- D1 in T1 D1 in R itself no
6322 -- D2 in T1 D2 in R itself no
6323 -- D3 in T1 D3 in R itself no
6325 -- X1 in T2 D3 in T1 D3 in T2 no
6326 -- X2 in T2 D1 in T1 D1 in T2 no
6327 -- D1 in T2 empty itself yes
6328 -- D2 in T2 empty itself yes
6329 -- D3 in T2 empty itself yes
6331 -- X1 in T3 X1 in T2 D3 in T3 no
6332 -- X2 in T3 X2 in T2 D1 in T3 no
6333 -- D1 in T3 empty itself yes
6334 -- D2 in T3 empty itself yes
6335 -- D3 in T3 empty itself yes
6337 -- Y in T4 X1 in T3 D3 in T3 no
6338 -- D1 in T3 empty itself yes
6339 -- D2 in T3 empty itself yes
6340 -- D3 in T3 empty itself yes
6342 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
6344 -- Type derivation for tagged types is fairly straightforward. If no
6345 -- discriminants are specified by the derived type, these are inherited
6346 -- from the parent. No explicit stored discriminants are ever necessary.
6347 -- The only manipulation that is done to the tree is that of adding a
6348 -- _parent field with parent type and constrained to the same constraint
6349 -- specified for the parent in the derived type definition. For instance:
6351 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
6352 -- type T1 is new R with null record;
6353 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
6355 -- are changed into:
6357 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
6358 -- _parent : R (D1, D2, D3);
6359 -- end record;
6361 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
6362 -- _parent : T1 (X2, 88, X1);
6363 -- end record;
6365 -- The discriminants actually present in R, T1 and T2 as well as their CD,
6366 -- ORC and ICH fields are:
6368 -- Discrim CD ORC ICH
6369 -- ^^^^^^^ ^^ ^^^ ^^^
6370 -- D1 in R empty itself no
6371 -- D2 in R empty itself no
6372 -- D3 in R empty itself no
6374 -- D1 in T1 D1 in R D1 in R no
6375 -- D2 in T1 D2 in R D2 in R no
6376 -- D3 in T1 D3 in R D3 in R no
6378 -- X1 in T2 D3 in T1 D3 in R no
6379 -- X2 in T2 D1 in T1 D1 in R no
6381 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
6383 -- Regardless of whether we dealing with a tagged or untagged type
6384 -- we will transform all derived type declarations of the form
6386 -- type T is new R (...) [with ...];
6387 -- or
6388 -- subtype S is R (...);
6389 -- type T is new S [with ...];
6390 -- into
6391 -- type BT is new R [with ...];
6392 -- subtype T is BT (...);
6394 -- That is, the base derived type is constrained only if it has no
6395 -- discriminants. The reason for doing this is that GNAT's semantic model
6396 -- assumes that a base type with discriminants is unconstrained.
6398 -- Note that, strictly speaking, the above transformation is not always
6399 -- correct. Consider for instance the following excerpt from ACVC b34011a:
6401 -- procedure B34011A is
6402 -- type REC (D : integer := 0) is record
6403 -- I : Integer;
6404 -- end record;
6406 -- package P is
6407 -- type T6 is new Rec;
6408 -- function F return T6;
6409 -- end P;
6411 -- use P;
6412 -- package Q6 is
6413 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
6414 -- end Q6;
6416 -- The definition of Q6.U is illegal. However transforming Q6.U into
6418 -- type BaseU is new T6;
6419 -- subtype U is BaseU (Q6.F.I)
6421 -- turns U into a legal subtype, which is incorrect. To avoid this problem
6422 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
6423 -- the transformation described above.
6425 -- There is another instance where the above transformation is incorrect.
6426 -- Consider:
6428 -- package Pack is
6429 -- type Base (D : Integer) is tagged null record;
6430 -- procedure P (X : Base);
6432 -- type Der is new Base (2) with null record;
6433 -- procedure P (X : Der);
6434 -- end Pack;
6436 -- Then the above transformation turns this into
6438 -- type Der_Base is new Base with null record;
6439 -- -- procedure P (X : Base) is implicitly inherited here
6440 -- -- as procedure P (X : Der_Base).
6442 -- subtype Der is Der_Base (2);
6443 -- procedure P (X : Der);
6444 -- -- The overriding of P (X : Der_Base) is illegal since we
6445 -- -- have a parameter conformance problem.
6447 -- To get around this problem, after having semantically processed Der_Base
6448 -- and the rewritten subtype declaration for Der, we copy Der_Base field
6449 -- Discriminant_Constraint from Der so that when parameter conformance is
6450 -- checked when P is overridden, no semantic errors are flagged.
6452 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
6454 -- Regardless of whether we are dealing with a tagged or untagged type
6455 -- we will transform all derived type declarations of the form
6457 -- type R (D1, .., Dn : ...) is [tagged] record ...;
6458 -- type T is new R [with ...];
6459 -- into
6460 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6462 -- The reason for such transformation is that it allows us to implement a
6463 -- very clean form of component inheritance as explained below.
6465 -- Note that this transformation is not achieved by direct tree rewriting
6466 -- and manipulation, but rather by redoing the semantic actions that the
6467 -- above transformation will entail. This is done directly in routine
6468 -- Inherit_Components.
6470 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
6472 -- In both tagged and untagged derived types, regular non discriminant
6473 -- components are inherited in the derived type from the parent type. In
6474 -- the absence of discriminants component, inheritance is straightforward
6475 -- as components can simply be copied from the parent.
6477 -- If the parent has discriminants, inheriting components constrained with
6478 -- these discriminants requires caution. Consider the following example:
6480 -- type R (D1, D2 : Positive) is [tagged] record
6481 -- S : String (D1 .. D2);
6482 -- end record;
6484 -- type T1 is new R [with null record];
6485 -- type T2 (X : positive) is new R (1, X) [with null record];
6487 -- As explained in 6. above, T1 is rewritten as
6488 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
6489 -- which makes the treatment for T1 and T2 identical.
6491 -- What we want when inheriting S, is that references to D1 and D2 in R are
6492 -- replaced with references to their correct constraints, i.e. D1 and D2 in
6493 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
6494 -- with either discriminant references in the derived type or expressions.
6495 -- This replacement is achieved as follows: before inheriting R's
6496 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6497 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
6498 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6499 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
6500 -- by String (1 .. X).
6502 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6504 -- We explain here the rules governing private type extensions relevant to
6505 -- type derivation. These rules are explained on the following example:
6507 -- type D [(...)] is new A [(...)] with private; <-- partial view
6508 -- type D [(...)] is new P [(...)] with null record; <-- full view
6510 -- Type A is called the ancestor subtype of the private extension.
6511 -- Type P is the parent type of the full view of the private extension. It
6512 -- must be A or a type derived from A.
6514 -- The rules concerning the discriminants of private type extensions are
6515 -- [7.3(10-13)]:
6517 -- o If a private extension inherits known discriminants from the ancestor
6518 -- subtype, then the full view shall also inherit its discriminants from
6519 -- the ancestor subtype and the parent subtype of the full view shall be
6520 -- constrained if and only if the ancestor subtype is constrained.
6522 -- o If a partial view has unknown discriminants, then the full view may
6523 -- define a definite or an indefinite subtype, with or without
6524 -- discriminants.
6526 -- o If a partial view has neither known nor unknown discriminants, then
6527 -- the full view shall define a definite subtype.
6529 -- o If the ancestor subtype of a private extension has constrained
6530 -- discriminants, then the parent subtype of the full view shall impose a
6531 -- statically matching constraint on those discriminants.
6533 -- This means that only the following forms of private extensions are
6534 -- allowed:
6536 -- type D is new A with private; <-- partial view
6537 -- type D is new P with null record; <-- full view
6539 -- If A has no discriminants than P has no discriminants, otherwise P must
6540 -- inherit A's discriminants.
6542 -- type D is new A (...) with private; <-- partial view
6543 -- type D is new P (:::) with null record; <-- full view
6545 -- P must inherit A's discriminants and (...) and (:::) must statically
6546 -- match.
6548 -- subtype A is R (...);
6549 -- type D is new A with private; <-- partial view
6550 -- type D is new P with null record; <-- full view
6552 -- P must have inherited R's discriminants and must be derived from A or
6553 -- any of its subtypes.
6555 -- type D (..) is new A with private; <-- partial view
6556 -- type D (..) is new P [(:::)] with null record; <-- full view
6558 -- No specific constraints on P's discriminants or constraint (:::).
6559 -- Note that A can be unconstrained, but the parent subtype P must either
6560 -- be constrained or (:::) must be present.
6562 -- type D (..) is new A [(...)] with private; <-- partial view
6563 -- type D (..) is new P [(:::)] with null record; <-- full view
6565 -- P's constraints on A's discriminants must statically match those
6566 -- imposed by (...).
6568 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6570 -- The full view of a private extension is handled exactly as described
6571 -- above. The model chose for the private view of a private extension is
6572 -- the same for what concerns discriminants (i.e. they receive the same
6573 -- treatment as in the tagged case). However, the private view of the
6574 -- private extension always inherits the components of the parent base,
6575 -- without replacing any discriminant reference. Strictly speaking this is
6576 -- incorrect. However, Gigi never uses this view to generate code so this
6577 -- is a purely semantic issue. In theory, a set of transformations similar
6578 -- to those given in 5. and 6. above could be applied to private views of
6579 -- private extensions to have the same model of component inheritance as
6580 -- for non private extensions. However, this is not done because it would
6581 -- further complicate private type processing. Semantically speaking, this
6582 -- leaves us in an uncomfortable situation. As an example consider:
6584 -- package Pack is
6585 -- type R (D : integer) is tagged record
6586 -- S : String (1 .. D);
6587 -- end record;
6588 -- procedure P (X : R);
6589 -- type T is new R (1) with private;
6590 -- private
6591 -- type T is new R (1) with null record;
6592 -- end;
6594 -- This is transformed into:
6596 -- package Pack is
6597 -- type R (D : integer) is tagged record
6598 -- S : String (1 .. D);
6599 -- end record;
6600 -- procedure P (X : R);
6601 -- type T is new R (1) with private;
6602 -- private
6603 -- type BaseT is new R with null record;
6604 -- subtype T is BaseT (1);
6605 -- end;
6607 -- (strictly speaking the above is incorrect Ada)
6609 -- From the semantic standpoint the private view of private extension T
6610 -- should be flagged as constrained since one can clearly have
6612 -- Obj : T;
6614 -- in a unit withing Pack. However, when deriving subprograms for the
6615 -- private view of private extension T, T must be seen as unconstrained
6616 -- since T has discriminants (this is a constraint of the current
6617 -- subprogram derivation model). Thus, when processing the private view of
6618 -- a private extension such as T, we first mark T as unconstrained, we
6619 -- process it, we perform program derivation and just before returning from
6620 -- Build_Derived_Record_Type we mark T as constrained.
6622 -- ??? Are there are other uncomfortable cases that we will have to
6623 -- deal with.
6625 -- 10. RECORD_TYPE_WITH_PRIVATE complications
6627 -- Types that are derived from a visible record type and have a private
6628 -- extension present other peculiarities. They behave mostly like private
6629 -- types, but if they have primitive operations defined, these will not
6630 -- have the proper signatures for further inheritance, because other
6631 -- primitive operations will use the implicit base that we define for
6632 -- private derivations below. This affect subprogram inheritance (see
6633 -- Derive_Subprograms for details). We also derive the implicit base from
6634 -- the base type of the full view, so that the implicit base is a record
6635 -- type and not another private type, This avoids infinite loops.
6637 procedure Build_Derived_Record_Type
6638 (N : Node_Id;
6639 Parent_Type : Entity_Id;
6640 Derived_Type : Entity_Id;
6641 Derive_Subps : Boolean := True)
6643 Loc : constant Source_Ptr := Sloc (N);
6644 Parent_Base : Entity_Id;
6645 Type_Def : Node_Id;
6646 Indic : Node_Id;
6647 Discrim : Entity_Id;
6648 Last_Discrim : Entity_Id;
6649 Constrs : Elist_Id;
6651 Discs : Elist_Id := New_Elmt_List;
6652 -- An empty Discs list means that there were no constraints in the
6653 -- subtype indication or that there was an error processing it.
6655 Assoc_List : Elist_Id;
6656 New_Discrs : Elist_Id;
6657 New_Base : Entity_Id;
6658 New_Decl : Node_Id;
6659 New_Indic : Node_Id;
6661 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
6662 Discriminant_Specs : constant Boolean :=
6663 Present (Discriminant_Specifications (N));
6664 Private_Extension : constant Boolean :=
6665 Nkind (N) = N_Private_Extension_Declaration;
6667 Constraint_Present : Boolean;
6668 Inherit_Discrims : Boolean := False;
6669 Save_Etype : Entity_Id;
6670 Save_Discr_Constr : Elist_Id;
6671 Save_Next_Entity : Entity_Id;
6673 begin
6674 if Ekind (Parent_Type) = E_Record_Type_With_Private
6675 and then Present (Full_View (Parent_Type))
6676 and then Has_Discriminants (Parent_Type)
6677 then
6678 Parent_Base := Base_Type (Full_View (Parent_Type));
6679 else
6680 Parent_Base := Base_Type (Parent_Type);
6681 end if;
6683 -- Before we start the previously documented transformations, here is
6684 -- little fix for size and alignment of tagged types. Normally when we
6685 -- derive type D from type P, we copy the size and alignment of P as the
6686 -- default for D, and in the absence of explicit representation clauses
6687 -- for D, the size and alignment are indeed the same as the parent.
6689 -- But this is wrong for tagged types, since fields may be added, and
6690 -- the default size may need to be larger, and the default alignment may
6691 -- need to be larger.
6693 -- We therefore reset the size and alignment fields in the tagged case.
6694 -- Note that the size and alignment will in any case be at least as
6695 -- large as the parent type (since the derived type has a copy of the
6696 -- parent type in the _parent field)
6698 -- The type is also marked as being tagged here, which is needed when
6699 -- processing components with a self-referential anonymous access type
6700 -- in the call to Check_Anonymous_Access_Components below. Note that
6701 -- this flag is also set later on for completeness.
6703 if Is_Tagged then
6704 Set_Is_Tagged_Type (Derived_Type);
6705 Init_Size_Align (Derived_Type);
6706 end if;
6708 -- STEP 0a: figure out what kind of derived type declaration we have
6710 if Private_Extension then
6711 Type_Def := N;
6712 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6714 else
6715 Type_Def := Type_Definition (N);
6717 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
6718 -- Parent_Base can be a private type or private extension. However,
6719 -- for tagged types with an extension the newly added fields are
6720 -- visible and hence the Derived_Type is always an E_Record_Type.
6721 -- (except that the parent may have its own private fields).
6722 -- For untagged types we preserve the Ekind of the Parent_Base.
6724 if Present (Record_Extension_Part (Type_Def)) then
6725 Set_Ekind (Derived_Type, E_Record_Type);
6727 -- Create internal access types for components with anonymous
6728 -- access types.
6730 if Ada_Version >= Ada_2005 then
6731 Check_Anonymous_Access_Components
6732 (N, Derived_Type, Derived_Type,
6733 Component_List (Record_Extension_Part (Type_Def)));
6734 end if;
6736 else
6737 Set_Ekind (Derived_Type, Ekind (Parent_Base));
6738 end if;
6739 end if;
6741 -- Indic can either be an N_Identifier if the subtype indication
6742 -- contains no constraint or an N_Subtype_Indication if the subtype
6743 -- indication has a constraint.
6745 Indic := Subtype_Indication (Type_Def);
6746 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6748 -- Check that the type has visible discriminants. The type may be
6749 -- a private type with unknown discriminants whose full view has
6750 -- discriminants which are invisible.
6752 if Constraint_Present then
6753 if not Has_Discriminants (Parent_Base)
6754 or else
6755 (Has_Unknown_Discriminants (Parent_Base)
6756 and then Is_Private_Type (Parent_Base))
6757 then
6758 Error_Msg_N
6759 ("invalid constraint: type has no discriminant",
6760 Constraint (Indic));
6762 Constraint_Present := False;
6763 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6765 elsif Is_Constrained (Parent_Type) then
6766 Error_Msg_N
6767 ("invalid constraint: parent type is already constrained",
6768 Constraint (Indic));
6770 Constraint_Present := False;
6771 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6772 end if;
6773 end if;
6775 -- STEP 0b: If needed, apply transformation given in point 5. above
6777 if not Private_Extension
6778 and then Has_Discriminants (Parent_Type)
6779 and then not Discriminant_Specs
6780 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
6781 then
6782 -- First, we must analyze the constraint (see comment in point 5.)
6784 if Constraint_Present then
6785 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
6787 if Has_Discriminants (Derived_Type)
6788 and then Has_Private_Declaration (Derived_Type)
6789 and then Present (Discriminant_Constraint (Derived_Type))
6790 then
6791 -- Verify that constraints of the full view statically match
6792 -- those given in the partial view.
6794 declare
6795 C1, C2 : Elmt_Id;
6797 begin
6798 C1 := First_Elmt (New_Discrs);
6799 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
6800 while Present (C1) and then Present (C2) loop
6801 if Fully_Conformant_Expressions (Node (C1), Node (C2))
6802 or else
6803 (Is_OK_Static_Expression (Node (C1))
6804 and then
6805 Is_OK_Static_Expression (Node (C2))
6806 and then
6807 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
6808 then
6809 null;
6811 else
6812 Error_Msg_N (
6813 "constraint not conformant to previous declaration",
6814 Node (C1));
6815 end if;
6817 Next_Elmt (C1);
6818 Next_Elmt (C2);
6819 end loop;
6820 end;
6821 end if;
6822 end if;
6824 -- Insert and analyze the declaration for the unconstrained base type
6826 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6828 New_Decl :=
6829 Make_Full_Type_Declaration (Loc,
6830 Defining_Identifier => New_Base,
6831 Type_Definition =>
6832 Make_Derived_Type_Definition (Loc,
6833 Abstract_Present => Abstract_Present (Type_Def),
6834 Limited_Present => Limited_Present (Type_Def),
6835 Subtype_Indication =>
6836 New_Occurrence_Of (Parent_Base, Loc),
6837 Record_Extension_Part =>
6838 Relocate_Node (Record_Extension_Part (Type_Def)),
6839 Interface_List => Interface_List (Type_Def)));
6841 Set_Parent (New_Decl, Parent (N));
6842 Mark_Rewrite_Insertion (New_Decl);
6843 Insert_Before (N, New_Decl);
6845 -- In the extension case, make sure ancestor is frozen appropriately
6846 -- (see also non-discriminated case below).
6848 if Present (Record_Extension_Part (Type_Def))
6849 or else Is_Interface (Parent_Base)
6850 then
6851 Freeze_Before (New_Decl, Parent_Type);
6852 end if;
6854 -- Note that this call passes False for the Derive_Subps parameter
6855 -- because subprogram derivation is deferred until after creating
6856 -- the subtype (see below).
6858 Build_Derived_Type
6859 (New_Decl, Parent_Base, New_Base,
6860 Is_Completion => True, Derive_Subps => False);
6862 -- ??? This needs re-examination to determine whether the
6863 -- above call can simply be replaced by a call to Analyze.
6865 Set_Analyzed (New_Decl);
6867 -- Insert and analyze the declaration for the constrained subtype
6869 if Constraint_Present then
6870 New_Indic :=
6871 Make_Subtype_Indication (Loc,
6872 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6873 Constraint => Relocate_Node (Constraint (Indic)));
6875 else
6876 declare
6877 Constr_List : constant List_Id := New_List;
6878 C : Elmt_Id;
6879 Expr : Node_Id;
6881 begin
6882 C := First_Elmt (Discriminant_Constraint (Parent_Type));
6883 while Present (C) loop
6884 Expr := Node (C);
6886 -- It is safe here to call New_Copy_Tree since
6887 -- Force_Evaluation was called on each constraint in
6888 -- Build_Discriminant_Constraints.
6890 Append (New_Copy_Tree (Expr), To => Constr_List);
6892 Next_Elmt (C);
6893 end loop;
6895 New_Indic :=
6896 Make_Subtype_Indication (Loc,
6897 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6898 Constraint =>
6899 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
6900 end;
6901 end if;
6903 Rewrite (N,
6904 Make_Subtype_Declaration (Loc,
6905 Defining_Identifier => Derived_Type,
6906 Subtype_Indication => New_Indic));
6908 Analyze (N);
6910 -- Derivation of subprograms must be delayed until the full subtype
6911 -- has been established to ensure proper overriding of subprograms
6912 -- inherited by full types. If the derivations occurred as part of
6913 -- the call to Build_Derived_Type above, then the check for type
6914 -- conformance would fail because earlier primitive subprograms
6915 -- could still refer to the full type prior the change to the new
6916 -- subtype and hence would not match the new base type created here.
6918 Derive_Subprograms (Parent_Type, Derived_Type);
6920 -- For tagged types the Discriminant_Constraint of the new base itype
6921 -- is inherited from the first subtype so that no subtype conformance
6922 -- problem arise when the first subtype overrides primitive
6923 -- operations inherited by the implicit base type.
6925 if Is_Tagged then
6926 Set_Discriminant_Constraint
6927 (New_Base, Discriminant_Constraint (Derived_Type));
6928 end if;
6930 return;
6931 end if;
6933 -- If we get here Derived_Type will have no discriminants or it will be
6934 -- a discriminated unconstrained base type.
6936 -- STEP 1a: perform preliminary actions/checks for derived tagged types
6938 if Is_Tagged then
6940 -- The parent type is frozen for non-private extensions (RM 13.14(7))
6941 -- The declaration of a specific descendant of an interface type
6942 -- freezes the interface type (RM 13.14).
6944 if not Private_Extension or else Is_Interface (Parent_Base) then
6945 Freeze_Before (N, Parent_Type);
6946 end if;
6948 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
6949 -- cannot be declared at a deeper level than its parent type is
6950 -- removed. The check on derivation within a generic body is also
6951 -- relaxed, but there's a restriction that a derived tagged type
6952 -- cannot be declared in a generic body if it's derived directly
6953 -- or indirectly from a formal type of that generic.
6955 if Ada_Version >= Ada_2005 then
6956 if Present (Enclosing_Generic_Body (Derived_Type)) then
6957 declare
6958 Ancestor_Type : Entity_Id;
6960 begin
6961 -- Check to see if any ancestor of the derived type is a
6962 -- formal type.
6964 Ancestor_Type := Parent_Type;
6965 while not Is_Generic_Type (Ancestor_Type)
6966 and then Etype (Ancestor_Type) /= Ancestor_Type
6967 loop
6968 Ancestor_Type := Etype (Ancestor_Type);
6969 end loop;
6971 -- If the derived type does have a formal type as an
6972 -- ancestor, then it's an error if the derived type is
6973 -- declared within the body of the generic unit that
6974 -- declares the formal type in its generic formal part. It's
6975 -- sufficient to check whether the ancestor type is declared
6976 -- inside the same generic body as the derived type (such as
6977 -- within a nested generic spec), in which case the
6978 -- derivation is legal. If the formal type is declared
6979 -- outside of that generic body, then it's guaranteed that
6980 -- the derived type is declared within the generic body of
6981 -- the generic unit declaring the formal type.
6983 if Is_Generic_Type (Ancestor_Type)
6984 and then Enclosing_Generic_Body (Ancestor_Type) /=
6985 Enclosing_Generic_Body (Derived_Type)
6986 then
6987 Error_Msg_NE
6988 ("parent type of& must not be descendant of formal type"
6989 & " of an enclosing generic body",
6990 Indic, Derived_Type);
6991 end if;
6992 end;
6993 end if;
6995 elsif Type_Access_Level (Derived_Type) /=
6996 Type_Access_Level (Parent_Type)
6997 and then not Is_Generic_Type (Derived_Type)
6998 then
6999 if Is_Controlled (Parent_Type) then
7000 Error_Msg_N
7001 ("controlled type must be declared at the library level",
7002 Indic);
7003 else
7004 Error_Msg_N
7005 ("type extension at deeper accessibility level than parent",
7006 Indic);
7007 end if;
7009 else
7010 declare
7011 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
7013 begin
7014 if Present (GB)
7015 and then GB /= Enclosing_Generic_Body (Parent_Base)
7016 then
7017 Error_Msg_NE
7018 ("parent type of& must not be outside generic body"
7019 & " (RM 3.9.1(4))",
7020 Indic, Derived_Type);
7021 end if;
7022 end;
7023 end if;
7024 end if;
7026 -- Ada 2005 (AI-251)
7028 if Ada_Version >= Ada_2005 and then Is_Tagged then
7030 -- "The declaration of a specific descendant of an interface type
7031 -- freezes the interface type" (RM 13.14).
7033 declare
7034 Iface : Node_Id;
7035 begin
7036 if Is_Non_Empty_List (Interface_List (Type_Def)) then
7037 Iface := First (Interface_List (Type_Def));
7038 while Present (Iface) loop
7039 Freeze_Before (N, Etype (Iface));
7040 Next (Iface);
7041 end loop;
7042 end if;
7043 end;
7044 end if;
7046 -- STEP 1b : preliminary cleanup of the full view of private types
7048 -- If the type is already marked as having discriminants, then it's the
7049 -- completion of a private type or private extension and we need to
7050 -- retain the discriminants from the partial view if the current
7051 -- declaration has Discriminant_Specifications so that we can verify
7052 -- conformance. However, we must remove any existing components that
7053 -- were inherited from the parent (and attached in Copy_And_Swap)
7054 -- because the full type inherits all appropriate components anyway, and
7055 -- we do not want the partial view's components interfering.
7057 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
7058 Discrim := First_Discriminant (Derived_Type);
7059 loop
7060 Last_Discrim := Discrim;
7061 Next_Discriminant (Discrim);
7062 exit when No (Discrim);
7063 end loop;
7065 Set_Last_Entity (Derived_Type, Last_Discrim);
7067 -- In all other cases wipe out the list of inherited components (even
7068 -- inherited discriminants), it will be properly rebuilt here.
7070 else
7071 Set_First_Entity (Derived_Type, Empty);
7072 Set_Last_Entity (Derived_Type, Empty);
7073 end if;
7075 -- STEP 1c: Initialize some flags for the Derived_Type
7077 -- The following flags must be initialized here so that
7078 -- Process_Discriminants can check that discriminants of tagged types do
7079 -- not have a default initial value and that access discriminants are
7080 -- only specified for limited records. For completeness, these flags are
7081 -- also initialized along with all the other flags below.
7083 -- AI-419: Limitedness is not inherited from an interface parent, so to
7084 -- be limited in that case the type must be explicitly declared as
7085 -- limited. However, task and protected interfaces are always limited.
7087 if Limited_Present (Type_Def) then
7088 Set_Is_Limited_Record (Derived_Type);
7090 elsif Is_Limited_Record (Parent_Type)
7091 or else (Present (Full_View (Parent_Type))
7092 and then Is_Limited_Record (Full_View (Parent_Type)))
7093 then
7094 if not Is_Interface (Parent_Type)
7095 or else Is_Synchronized_Interface (Parent_Type)
7096 or else Is_Protected_Interface (Parent_Type)
7097 or else Is_Task_Interface (Parent_Type)
7098 then
7099 Set_Is_Limited_Record (Derived_Type);
7100 end if;
7101 end if;
7103 -- STEP 2a: process discriminants of derived type if any
7105 Push_Scope (Derived_Type);
7107 if Discriminant_Specs then
7108 Set_Has_Unknown_Discriminants (Derived_Type, False);
7110 -- The following call initializes fields Has_Discriminants and
7111 -- Discriminant_Constraint, unless we are processing the completion
7112 -- of a private type declaration.
7114 Check_Or_Process_Discriminants (N, Derived_Type);
7116 -- For untagged types, the constraint on the Parent_Type must be
7117 -- present and is used to rename the discriminants.
7119 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
7120 Error_Msg_N ("untagged parent must have discriminants", Indic);
7122 elsif not Is_Tagged and then not Constraint_Present then
7123 Error_Msg_N
7124 ("discriminant constraint needed for derived untagged records",
7125 Indic);
7127 -- Otherwise the parent subtype must be constrained unless we have a
7128 -- private extension.
7130 elsif not Constraint_Present
7131 and then not Private_Extension
7132 and then not Is_Constrained (Parent_Type)
7133 then
7134 Error_Msg_N
7135 ("unconstrained type not allowed in this context", Indic);
7137 elsif Constraint_Present then
7138 -- The following call sets the field Corresponding_Discriminant
7139 -- for the discriminants in the Derived_Type.
7141 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
7143 -- For untagged types all new discriminants must rename
7144 -- discriminants in the parent. For private extensions new
7145 -- discriminants cannot rename old ones (implied by [7.3(13)]).
7147 Discrim := First_Discriminant (Derived_Type);
7148 while Present (Discrim) loop
7149 if not Is_Tagged
7150 and then No (Corresponding_Discriminant (Discrim))
7151 then
7152 Error_Msg_N
7153 ("new discriminants must constrain old ones", Discrim);
7155 elsif Private_Extension
7156 and then Present (Corresponding_Discriminant (Discrim))
7157 then
7158 Error_Msg_N
7159 ("only static constraints allowed for parent"
7160 & " discriminants in the partial view", Indic);
7161 exit;
7162 end if;
7164 -- If a new discriminant is used in the constraint, then its
7165 -- subtype must be statically compatible with the parent
7166 -- discriminant's subtype (3.7(15)).
7168 if Present (Corresponding_Discriminant (Discrim))
7169 and then
7170 not Subtypes_Statically_Compatible
7171 (Etype (Discrim),
7172 Etype (Corresponding_Discriminant (Discrim)))
7173 then
7174 Error_Msg_N
7175 ("subtype must be compatible with parent discriminant",
7176 Discrim);
7177 end if;
7179 Next_Discriminant (Discrim);
7180 end loop;
7182 -- Check whether the constraints of the full view statically
7183 -- match those imposed by the parent subtype [7.3(13)].
7185 if Present (Stored_Constraint (Derived_Type)) then
7186 declare
7187 C1, C2 : Elmt_Id;
7189 begin
7190 C1 := First_Elmt (Discs);
7191 C2 := First_Elmt (Stored_Constraint (Derived_Type));
7192 while Present (C1) and then Present (C2) loop
7193 if not
7194 Fully_Conformant_Expressions (Node (C1), Node (C2))
7195 then
7196 Error_Msg_N
7197 ("not conformant with previous declaration",
7198 Node (C1));
7199 end if;
7201 Next_Elmt (C1);
7202 Next_Elmt (C2);
7203 end loop;
7204 end;
7205 end if;
7206 end if;
7208 -- STEP 2b: No new discriminants, inherit discriminants if any
7210 else
7211 if Private_Extension then
7212 Set_Has_Unknown_Discriminants
7213 (Derived_Type,
7214 Has_Unknown_Discriminants (Parent_Type)
7215 or else Unknown_Discriminants_Present (N));
7217 -- The partial view of the parent may have unknown discriminants,
7218 -- but if the full view has discriminants and the parent type is
7219 -- in scope they must be inherited.
7221 elsif Has_Unknown_Discriminants (Parent_Type)
7222 and then
7223 (not Has_Discriminants (Parent_Type)
7224 or else not In_Open_Scopes (Scope (Parent_Type)))
7225 then
7226 Set_Has_Unknown_Discriminants (Derived_Type);
7227 end if;
7229 if not Has_Unknown_Discriminants (Derived_Type)
7230 and then not Has_Unknown_Discriminants (Parent_Base)
7231 and then Has_Discriminants (Parent_Type)
7232 then
7233 Inherit_Discrims := True;
7234 Set_Has_Discriminants
7235 (Derived_Type, True);
7236 Set_Discriminant_Constraint
7237 (Derived_Type, Discriminant_Constraint (Parent_Base));
7238 end if;
7240 -- The following test is true for private types (remember
7241 -- transformation 5. is not applied to those) and in an error
7242 -- situation.
7244 if Constraint_Present then
7245 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
7246 end if;
7248 -- For now mark a new derived type as constrained only if it has no
7249 -- discriminants. At the end of Build_Derived_Record_Type we properly
7250 -- set this flag in the case of private extensions. See comments in
7251 -- point 9. just before body of Build_Derived_Record_Type.
7253 Set_Is_Constrained
7254 (Derived_Type,
7255 not (Inherit_Discrims
7256 or else Has_Unknown_Discriminants (Derived_Type)));
7257 end if;
7259 -- STEP 3: initialize fields of derived type
7261 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
7262 Set_Stored_Constraint (Derived_Type, No_Elist);
7264 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
7265 -- but cannot be interfaces
7267 if not Private_Extension
7268 and then Ekind (Derived_Type) /= E_Private_Type
7269 and then Ekind (Derived_Type) /= E_Limited_Private_Type
7270 then
7271 if Interface_Present (Type_Def) then
7272 Analyze_Interface_Declaration (Derived_Type, Type_Def);
7273 end if;
7275 Set_Interfaces (Derived_Type, No_Elist);
7276 end if;
7278 -- Fields inherited from the Parent_Type
7280 Set_Discard_Names
7281 (Derived_Type, Einfo.Discard_Names (Parent_Type));
7282 Set_Has_Specified_Layout
7283 (Derived_Type, Has_Specified_Layout (Parent_Type));
7284 Set_Is_Limited_Composite
7285 (Derived_Type, Is_Limited_Composite (Parent_Type));
7286 Set_Is_Private_Composite
7287 (Derived_Type, Is_Private_Composite (Parent_Type));
7289 -- Fields inherited from the Parent_Base
7291 Set_Has_Controlled_Component
7292 (Derived_Type, Has_Controlled_Component (Parent_Base));
7293 Set_Has_Non_Standard_Rep
7294 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7295 Set_Has_Primitive_Operations
7296 (Derived_Type, Has_Primitive_Operations (Parent_Base));
7298 -- Fields inherited from the Parent_Base in the non-private case
7300 if Ekind (Derived_Type) = E_Record_Type then
7301 Set_Has_Complex_Representation
7302 (Derived_Type, Has_Complex_Representation (Parent_Base));
7303 end if;
7305 -- Fields inherited from the Parent_Base for record types
7307 if Is_Record_Type (Derived_Type) then
7309 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7310 -- Parent_Base can be a private type or private extension.
7312 if Present (Full_View (Parent_Base)) then
7313 Set_OK_To_Reorder_Components
7314 (Derived_Type,
7315 OK_To_Reorder_Components (Full_View (Parent_Base)));
7316 Set_Reverse_Bit_Order
7317 (Derived_Type, Reverse_Bit_Order (Full_View (Parent_Base)));
7318 else
7319 Set_OK_To_Reorder_Components
7320 (Derived_Type, OK_To_Reorder_Components (Parent_Base));
7321 Set_Reverse_Bit_Order
7322 (Derived_Type, Reverse_Bit_Order (Parent_Base));
7323 end if;
7324 end if;
7326 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7328 if not Is_Controlled (Parent_Type) then
7329 Set_Finalize_Storage_Only
7330 (Derived_Type, Finalize_Storage_Only (Parent_Type));
7331 end if;
7333 -- Set fields for private derived types
7335 if Is_Private_Type (Derived_Type) then
7336 Set_Depends_On_Private (Derived_Type, True);
7337 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7339 -- Inherit fields from non private record types. If this is the
7340 -- completion of a derivation from a private type, the parent itself
7341 -- is private, and the attributes come from its full view, which must
7342 -- be present.
7344 else
7345 if Is_Private_Type (Parent_Base)
7346 and then not Is_Record_Type (Parent_Base)
7347 then
7348 Set_Component_Alignment
7349 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
7350 Set_C_Pass_By_Copy
7351 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
7352 else
7353 Set_Component_Alignment
7354 (Derived_Type, Component_Alignment (Parent_Base));
7355 Set_C_Pass_By_Copy
7356 (Derived_Type, C_Pass_By_Copy (Parent_Base));
7357 end if;
7358 end if;
7360 -- Set fields for tagged types
7362 if Is_Tagged then
7363 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
7365 -- All tagged types defined in Ada.Finalization are controlled
7367 if Chars (Scope (Derived_Type)) = Name_Finalization
7368 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
7369 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
7370 then
7371 Set_Is_Controlled (Derived_Type);
7372 else
7373 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
7374 end if;
7376 -- Minor optimization: there is no need to generate the class-wide
7377 -- entity associated with an underlying record view.
7379 if not Is_Underlying_Record_View (Derived_Type) then
7380 Make_Class_Wide_Type (Derived_Type);
7381 end if;
7383 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
7385 if Has_Discriminants (Derived_Type)
7386 and then Constraint_Present
7387 then
7388 Set_Stored_Constraint
7389 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
7390 end if;
7392 if Ada_Version >= Ada_2005 then
7393 declare
7394 Ifaces_List : Elist_Id;
7396 begin
7397 -- Checks rules 3.9.4 (13/2 and 14/2)
7399 if Comes_From_Source (Derived_Type)
7400 and then not Is_Private_Type (Derived_Type)
7401 and then Is_Interface (Parent_Type)
7402 and then not Is_Interface (Derived_Type)
7403 then
7404 if Is_Task_Interface (Parent_Type) then
7405 Error_Msg_N
7406 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
7407 Derived_Type);
7409 elsif Is_Protected_Interface (Parent_Type) then
7410 Error_Msg_N
7411 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
7412 Derived_Type);
7413 end if;
7414 end if;
7416 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
7418 Check_Interfaces (N, Type_Def);
7420 -- Ada 2005 (AI-251): Collect the list of progenitors that are
7421 -- not already in the parents.
7423 Collect_Interfaces
7424 (T => Derived_Type,
7425 Ifaces_List => Ifaces_List,
7426 Exclude_Parents => True);
7428 Set_Interfaces (Derived_Type, Ifaces_List);
7430 -- If the derived type is the anonymous type created for
7431 -- a declaration whose parent has a constraint, propagate
7432 -- the interface list to the source type. This must be done
7433 -- prior to the completion of the analysis of the source type
7434 -- because the components in the extension may contain current
7435 -- instances whose legality depends on some ancestor.
7437 if Is_Itype (Derived_Type) then
7438 declare
7439 Def : constant Node_Id :=
7440 Associated_Node_For_Itype (Derived_Type);
7441 begin
7442 if Present (Def)
7443 and then Nkind (Def) = N_Full_Type_Declaration
7444 then
7445 Set_Interfaces
7446 (Defining_Identifier (Def), Ifaces_List);
7447 end if;
7448 end;
7449 end if;
7450 end;
7451 end if;
7453 else
7454 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
7455 Set_Has_Non_Standard_Rep
7456 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7457 end if;
7459 -- STEP 4: Inherit components from the parent base and constrain them.
7460 -- Apply the second transformation described in point 6. above.
7462 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
7463 or else not Has_Discriminants (Parent_Type)
7464 or else not Is_Constrained (Parent_Type)
7465 then
7466 Constrs := Discs;
7467 else
7468 Constrs := Discriminant_Constraint (Parent_Type);
7469 end if;
7471 Assoc_List :=
7472 Inherit_Components
7473 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
7475 -- STEP 5a: Copy the parent record declaration for untagged types
7477 if not Is_Tagged then
7479 -- Discriminant_Constraint (Derived_Type) has been properly
7480 -- constructed. Save it and temporarily set it to Empty because we
7481 -- do not want the call to New_Copy_Tree below to mess this list.
7483 if Has_Discriminants (Derived_Type) then
7484 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7485 Set_Discriminant_Constraint (Derived_Type, No_Elist);
7486 else
7487 Save_Discr_Constr := No_Elist;
7488 end if;
7490 -- Save the Etype field of Derived_Type. It is correctly set now,
7491 -- but the call to New_Copy tree may remap it to point to itself,
7492 -- which is not what we want. Ditto for the Next_Entity field.
7494 Save_Etype := Etype (Derived_Type);
7495 Save_Next_Entity := Next_Entity (Derived_Type);
7497 -- Assoc_List maps all stored discriminants in the Parent_Base to
7498 -- stored discriminants in the Derived_Type. It is fundamental that
7499 -- no types or itypes with discriminants other than the stored
7500 -- discriminants appear in the entities declared inside
7501 -- Derived_Type, since the back end cannot deal with it.
7503 New_Decl :=
7504 New_Copy_Tree
7505 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7507 -- Restore the fields saved prior to the New_Copy_Tree call
7508 -- and compute the stored constraint.
7510 Set_Etype (Derived_Type, Save_Etype);
7511 Set_Next_Entity (Derived_Type, Save_Next_Entity);
7513 if Has_Discriminants (Derived_Type) then
7514 Set_Discriminant_Constraint
7515 (Derived_Type, Save_Discr_Constr);
7516 Set_Stored_Constraint
7517 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
7518 Replace_Components (Derived_Type, New_Decl);
7519 end if;
7521 -- Insert the new derived type declaration
7523 Rewrite (N, New_Decl);
7525 -- STEP 5b: Complete the processing for record extensions in generics
7527 -- There is no completion for record extensions declared in the
7528 -- parameter part of a generic, so we need to complete processing for
7529 -- these generic record extensions here. The Record_Type_Definition call
7530 -- will change the Ekind of the components from E_Void to E_Component.
7532 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
7533 Record_Type_Definition (Empty, Derived_Type);
7535 -- STEP 5c: Process the record extension for non private tagged types
7537 elsif not Private_Extension then
7539 -- Add the _parent field in the derived type
7541 Expand_Record_Extension (Derived_Type, Type_Def);
7543 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
7544 -- implemented interfaces if we are in expansion mode
7546 if Expander_Active
7547 and then Has_Interfaces (Derived_Type)
7548 then
7549 Add_Interface_Tag_Components (N, Derived_Type);
7550 end if;
7552 -- Analyze the record extension
7554 Record_Type_Definition
7555 (Record_Extension_Part (Type_Def), Derived_Type);
7556 end if;
7558 End_Scope;
7560 -- Nothing else to do if there is an error in the derivation.
7561 -- An unusual case: the full view may be derived from a type in an
7562 -- instance, when the partial view was used illegally as an actual
7563 -- in that instance, leading to a circular definition.
7565 if Etype (Derived_Type) = Any_Type
7566 or else Etype (Parent_Type) = Derived_Type
7567 then
7568 return;
7569 end if;
7571 -- Set delayed freeze and then derive subprograms, we need to do
7572 -- this in this order so that derived subprograms inherit the
7573 -- derived freeze if necessary.
7575 Set_Has_Delayed_Freeze (Derived_Type);
7577 if Derive_Subps then
7578 Derive_Subprograms (Parent_Type, Derived_Type);
7579 end if;
7581 -- If we have a private extension which defines a constrained derived
7582 -- type mark as constrained here after we have derived subprograms. See
7583 -- comment on point 9. just above the body of Build_Derived_Record_Type.
7585 if Private_Extension and then Inherit_Discrims then
7586 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7587 Set_Is_Constrained (Derived_Type, True);
7588 Set_Discriminant_Constraint (Derived_Type, Discs);
7590 elsif Is_Constrained (Parent_Type) then
7591 Set_Is_Constrained
7592 (Derived_Type, True);
7593 Set_Discriminant_Constraint
7594 (Derived_Type, Discriminant_Constraint (Parent_Type));
7595 end if;
7596 end if;
7598 -- Update the class-wide type, which shares the now-completed entity
7599 -- list with its specific type. In case of underlying record views,
7600 -- we do not generate the corresponding class wide entity.
7602 if Is_Tagged
7603 and then not Is_Underlying_Record_View (Derived_Type)
7604 then
7605 Set_First_Entity
7606 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7607 Set_Last_Entity
7608 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7609 end if;
7611 -- Update the scope of anonymous access types of discriminants and other
7612 -- components, to prevent scope anomalies in gigi, when the derivation
7613 -- appears in a scope nested within that of the parent.
7615 declare
7616 D : Entity_Id;
7618 begin
7619 D := First_Entity (Derived_Type);
7620 while Present (D) loop
7621 if Ekind_In (D, E_Discriminant, E_Component) then
7622 if Is_Itype (Etype (D))
7623 and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7624 then
7625 Set_Scope (Etype (D), Current_Scope);
7626 end if;
7627 end if;
7629 Next_Entity (D);
7630 end loop;
7631 end;
7632 end Build_Derived_Record_Type;
7634 ------------------------
7635 -- Build_Derived_Type --
7636 ------------------------
7638 procedure Build_Derived_Type
7639 (N : Node_Id;
7640 Parent_Type : Entity_Id;
7641 Derived_Type : Entity_Id;
7642 Is_Completion : Boolean;
7643 Derive_Subps : Boolean := True)
7645 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7647 begin
7648 -- Set common attributes
7650 Set_Scope (Derived_Type, Current_Scope);
7652 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7653 Set_Etype (Derived_Type, Parent_Base);
7654 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
7656 Set_Size_Info (Derived_Type, Parent_Type);
7657 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
7658 Set_Convention (Derived_Type, Convention (Parent_Type));
7659 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7660 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
7662 -- Propagate invariant information. The new type has invariants if
7663 -- they are inherited from the parent type, and these invariants can
7664 -- be further inherited, so both flags are set.
7666 if Has_Inheritable_Invariants (Parent_Type) then
7667 Set_Has_Inheritable_Invariants (Derived_Type);
7668 Set_Has_Invariants (Derived_Type);
7669 end if;
7671 -- The derived type inherits the representation clauses of the parent.
7672 -- However, for a private type that is completed by a derivation, there
7673 -- may be operation attributes that have been specified already (stream
7674 -- attributes and External_Tag) and those must be provided. Finally,
7675 -- if the partial view is a private extension, the representation items
7676 -- of the parent have been inherited already, and should not be chained
7677 -- twice to the derived type.
7679 if Is_Tagged_Type (Parent_Type)
7680 and then Present (First_Rep_Item (Derived_Type))
7681 then
7682 -- The existing items are either operational items or items inherited
7683 -- from a private extension declaration.
7685 declare
7686 Rep : Node_Id;
7687 -- Used to iterate over representation items of the derived type
7689 Last_Rep : Node_Id;
7690 -- Last representation item of the (non-empty) representation
7691 -- item list of the derived type.
7693 Found : Boolean := False;
7695 begin
7696 Rep := First_Rep_Item (Derived_Type);
7697 Last_Rep := Rep;
7698 while Present (Rep) loop
7699 if Rep = First_Rep_Item (Parent_Type) then
7700 Found := True;
7701 exit;
7703 else
7704 Rep := Next_Rep_Item (Rep);
7706 if Present (Rep) then
7707 Last_Rep := Rep;
7708 end if;
7709 end if;
7710 end loop;
7712 -- Here if we either encountered the parent type's first rep
7713 -- item on the derived type's rep item list (in which case
7714 -- Found is True, and we have nothing else to do), or if we
7715 -- reached the last rep item of the derived type, which is
7716 -- Last_Rep, in which case we further chain the parent type's
7717 -- rep items to those of the derived type.
7719 if not Found then
7720 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
7721 end if;
7722 end;
7724 else
7725 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7726 end if;
7728 case Ekind (Parent_Type) is
7729 when Numeric_Kind =>
7730 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7732 when Array_Kind =>
7733 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
7735 when E_Record_Type
7736 | E_Record_Subtype
7737 | Class_Wide_Kind =>
7738 Build_Derived_Record_Type
7739 (N, Parent_Type, Derived_Type, Derive_Subps);
7740 return;
7742 when Enumeration_Kind =>
7743 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7745 when Access_Kind =>
7746 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7748 when Incomplete_Or_Private_Kind =>
7749 Build_Derived_Private_Type
7750 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
7752 -- For discriminated types, the derivation includes deriving
7753 -- primitive operations. For others it is done below.
7755 if Is_Tagged_Type (Parent_Type)
7756 or else Has_Discriminants (Parent_Type)
7757 or else (Present (Full_View (Parent_Type))
7758 and then Has_Discriminants (Full_View (Parent_Type)))
7759 then
7760 return;
7761 end if;
7763 when Concurrent_Kind =>
7764 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
7766 when others =>
7767 raise Program_Error;
7768 end case;
7770 if Etype (Derived_Type) = Any_Type then
7771 return;
7772 end if;
7774 -- Set delayed freeze and then derive subprograms, we need to do this
7775 -- in this order so that derived subprograms inherit the derived freeze
7776 -- if necessary.
7778 Set_Has_Delayed_Freeze (Derived_Type);
7779 if Derive_Subps then
7780 Derive_Subprograms (Parent_Type, Derived_Type);
7781 end if;
7783 Set_Has_Primitive_Operations
7784 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
7785 end Build_Derived_Type;
7787 -----------------------
7788 -- Build_Discriminal --
7789 -----------------------
7791 procedure Build_Discriminal (Discrim : Entity_Id) is
7792 D_Minal : Entity_Id;
7793 CR_Disc : Entity_Id;
7795 begin
7796 -- A discriminal has the same name as the discriminant
7798 D_Minal :=
7799 Make_Defining_Identifier (Sloc (Discrim),
7800 Chars => Chars (Discrim));
7802 Set_Ekind (D_Minal, E_In_Parameter);
7803 Set_Mechanism (D_Minal, Default_Mechanism);
7804 Set_Etype (D_Minal, Etype (Discrim));
7805 Set_Scope (D_Minal, Current_Scope);
7807 Set_Discriminal (Discrim, D_Minal);
7808 Set_Discriminal_Link (D_Minal, Discrim);
7810 -- For task types, build at once the discriminants of the corresponding
7811 -- record, which are needed if discriminants are used in entry defaults
7812 -- and in family bounds.
7814 if Is_Concurrent_Type (Current_Scope)
7815 or else Is_Limited_Type (Current_Scope)
7816 then
7817 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
7819 Set_Ekind (CR_Disc, E_In_Parameter);
7820 Set_Mechanism (CR_Disc, Default_Mechanism);
7821 Set_Etype (CR_Disc, Etype (Discrim));
7822 Set_Scope (CR_Disc, Current_Scope);
7823 Set_Discriminal_Link (CR_Disc, Discrim);
7824 Set_CR_Discriminant (Discrim, CR_Disc);
7825 end if;
7826 end Build_Discriminal;
7828 ------------------------------------
7829 -- Build_Discriminant_Constraints --
7830 ------------------------------------
7832 function Build_Discriminant_Constraints
7833 (T : Entity_Id;
7834 Def : Node_Id;
7835 Derived_Def : Boolean := False) return Elist_Id
7837 C : constant Node_Id := Constraint (Def);
7838 Nb_Discr : constant Nat := Number_Discriminants (T);
7840 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
7841 -- Saves the expression corresponding to a given discriminant in T
7843 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
7844 -- Return the Position number within array Discr_Expr of a discriminant
7845 -- D within the discriminant list of the discriminated type T.
7847 ------------------
7848 -- Pos_Of_Discr --
7849 ------------------
7851 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
7852 Disc : Entity_Id;
7854 begin
7855 Disc := First_Discriminant (T);
7856 for J in Discr_Expr'Range loop
7857 if Disc = D then
7858 return J;
7859 end if;
7861 Next_Discriminant (Disc);
7862 end loop;
7864 -- Note: Since this function is called on discriminants that are
7865 -- known to belong to the discriminated type, falling through the
7866 -- loop with no match signals an internal compiler error.
7868 raise Program_Error;
7869 end Pos_Of_Discr;
7871 -- Declarations local to Build_Discriminant_Constraints
7873 Discr : Entity_Id;
7874 E : Entity_Id;
7875 Elist : constant Elist_Id := New_Elmt_List;
7877 Constr : Node_Id;
7878 Expr : Node_Id;
7879 Id : Node_Id;
7880 Position : Nat;
7881 Found : Boolean;
7883 Discrim_Present : Boolean := False;
7885 -- Start of processing for Build_Discriminant_Constraints
7887 begin
7888 -- The following loop will process positional associations only.
7889 -- For a positional association, the (single) discriminant is
7890 -- implicitly specified by position, in textual order (RM 3.7.2).
7892 Discr := First_Discriminant (T);
7893 Constr := First (Constraints (C));
7894 for D in Discr_Expr'Range loop
7895 exit when Nkind (Constr) = N_Discriminant_Association;
7897 if No (Constr) then
7898 Error_Msg_N ("too few discriminants given in constraint", C);
7899 return New_Elmt_List;
7901 elsif Nkind (Constr) = N_Range
7902 or else (Nkind (Constr) = N_Attribute_Reference
7903 and then
7904 Attribute_Name (Constr) = Name_Range)
7905 then
7906 Error_Msg_N
7907 ("a range is not a valid discriminant constraint", Constr);
7908 Discr_Expr (D) := Error;
7910 else
7911 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
7912 Discr_Expr (D) := Constr;
7913 end if;
7915 Next_Discriminant (Discr);
7916 Next (Constr);
7917 end loop;
7919 if No (Discr) and then Present (Constr) then
7920 Error_Msg_N ("too many discriminants given in constraint", Constr);
7921 return New_Elmt_List;
7922 end if;
7924 -- Named associations can be given in any order, but if both positional
7925 -- and named associations are used in the same discriminant constraint,
7926 -- then positional associations must occur first, at their normal
7927 -- position. Hence once a named association is used, the rest of the
7928 -- discriminant constraint must use only named associations.
7930 while Present (Constr) loop
7932 -- Positional association forbidden after a named association
7934 if Nkind (Constr) /= N_Discriminant_Association then
7935 Error_Msg_N ("positional association follows named one", Constr);
7936 return New_Elmt_List;
7938 -- Otherwise it is a named association
7940 else
7941 -- E records the type of the discriminants in the named
7942 -- association. All the discriminants specified in the same name
7943 -- association must have the same type.
7945 E := Empty;
7947 -- Search the list of discriminants in T to see if the simple name
7948 -- given in the constraint matches any of them.
7950 Id := First (Selector_Names (Constr));
7951 while Present (Id) loop
7952 Found := False;
7954 -- If Original_Discriminant is present, we are processing a
7955 -- generic instantiation and this is an instance node. We need
7956 -- to find the name of the corresponding discriminant in the
7957 -- actual record type T and not the name of the discriminant in
7958 -- the generic formal. Example:
7960 -- generic
7961 -- type G (D : int) is private;
7962 -- package P is
7963 -- subtype W is G (D => 1);
7964 -- end package;
7965 -- type Rec (X : int) is record ... end record;
7966 -- package Q is new P (G => Rec);
7968 -- At the point of the instantiation, formal type G is Rec
7969 -- and therefore when reanalyzing "subtype W is G (D => 1);"
7970 -- which really looks like "subtype W is Rec (D => 1);" at
7971 -- the point of instantiation, we want to find the discriminant
7972 -- that corresponds to D in Rec, i.e. X.
7974 if Present (Original_Discriminant (Id)) then
7975 Discr := Find_Corresponding_Discriminant (Id, T);
7976 Found := True;
7978 else
7979 Discr := First_Discriminant (T);
7980 while Present (Discr) loop
7981 if Chars (Discr) = Chars (Id) then
7982 Found := True;
7983 exit;
7984 end if;
7986 Next_Discriminant (Discr);
7987 end loop;
7989 if not Found then
7990 Error_Msg_N ("& does not match any discriminant", Id);
7991 return New_Elmt_List;
7993 -- The following is only useful for the benefit of generic
7994 -- instances but it does not interfere with other
7995 -- processing for the non-generic case so we do it in all
7996 -- cases (for generics this statement is executed when
7997 -- processing the generic definition, see comment at the
7998 -- beginning of this if statement).
8000 else
8001 Set_Original_Discriminant (Id, Discr);
8002 end if;
8003 end if;
8005 Position := Pos_Of_Discr (T, Discr);
8007 if Present (Discr_Expr (Position)) then
8008 Error_Msg_N ("duplicate constraint for discriminant&", Id);
8010 else
8011 -- Each discriminant specified in the same named association
8012 -- must be associated with a separate copy of the
8013 -- corresponding expression.
8015 if Present (Next (Id)) then
8016 Expr := New_Copy_Tree (Expression (Constr));
8017 Set_Parent (Expr, Parent (Expression (Constr)));
8018 else
8019 Expr := Expression (Constr);
8020 end if;
8022 Discr_Expr (Position) := Expr;
8023 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
8024 end if;
8026 -- A discriminant association with more than one discriminant
8027 -- name is only allowed if the named discriminants are all of
8028 -- the same type (RM 3.7.1(8)).
8030 if E = Empty then
8031 E := Base_Type (Etype (Discr));
8033 elsif Base_Type (Etype (Discr)) /= E then
8034 Error_Msg_N
8035 ("all discriminants in an association " &
8036 "must have the same type", Id);
8037 end if;
8039 Next (Id);
8040 end loop;
8041 end if;
8043 Next (Constr);
8044 end loop;
8046 -- A discriminant constraint must provide exactly one value for each
8047 -- discriminant of the type (RM 3.7.1(8)).
8049 for J in Discr_Expr'Range loop
8050 if No (Discr_Expr (J)) then
8051 Error_Msg_N ("too few discriminants given in constraint", C);
8052 return New_Elmt_List;
8053 end if;
8054 end loop;
8056 -- Determine if there are discriminant expressions in the constraint
8058 for J in Discr_Expr'Range loop
8059 if Denotes_Discriminant
8060 (Discr_Expr (J), Check_Concurrent => True)
8061 then
8062 Discrim_Present := True;
8063 end if;
8064 end loop;
8066 -- Build an element list consisting of the expressions given in the
8067 -- discriminant constraint and apply the appropriate checks. The list
8068 -- is constructed after resolving any named discriminant associations
8069 -- and therefore the expressions appear in the textual order of the
8070 -- discriminants.
8072 Discr := First_Discriminant (T);
8073 for J in Discr_Expr'Range loop
8074 if Discr_Expr (J) /= Error then
8075 Append_Elmt (Discr_Expr (J), Elist);
8077 -- If any of the discriminant constraints is given by a
8078 -- discriminant and we are in a derived type declaration we
8079 -- have a discriminant renaming. Establish link between new
8080 -- and old discriminant.
8082 if Denotes_Discriminant (Discr_Expr (J)) then
8083 if Derived_Def then
8084 Set_Corresponding_Discriminant
8085 (Entity (Discr_Expr (J)), Discr);
8086 end if;
8088 -- Force the evaluation of non-discriminant expressions.
8089 -- If we have found a discriminant in the constraint 3.4(26)
8090 -- and 3.8(18) demand that no range checks are performed are
8091 -- after evaluation. If the constraint is for a component
8092 -- definition that has a per-object constraint, expressions are
8093 -- evaluated but not checked either. In all other cases perform
8094 -- a range check.
8096 else
8097 if Discrim_Present then
8098 null;
8100 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
8101 and then
8102 Has_Per_Object_Constraint
8103 (Defining_Identifier (Parent (Parent (Def))))
8104 then
8105 null;
8107 elsif Is_Access_Type (Etype (Discr)) then
8108 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
8110 else
8111 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
8112 end if;
8114 Force_Evaluation (Discr_Expr (J));
8115 end if;
8117 -- Check that the designated type of an access discriminant's
8118 -- expression is not a class-wide type unless the discriminant's
8119 -- designated type is also class-wide.
8121 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
8122 and then not Is_Class_Wide_Type
8123 (Designated_Type (Etype (Discr)))
8124 and then Etype (Discr_Expr (J)) /= Any_Type
8125 and then Is_Class_Wide_Type
8126 (Designated_Type (Etype (Discr_Expr (J))))
8127 then
8128 Wrong_Type (Discr_Expr (J), Etype (Discr));
8130 elsif Is_Access_Type (Etype (Discr))
8131 and then not Is_Access_Constant (Etype (Discr))
8132 and then Is_Access_Type (Etype (Discr_Expr (J)))
8133 and then Is_Access_Constant (Etype (Discr_Expr (J)))
8134 then
8135 Error_Msg_NE
8136 ("constraint for discriminant& must be access to variable",
8137 Def, Discr);
8138 end if;
8139 end if;
8141 Next_Discriminant (Discr);
8142 end loop;
8144 return Elist;
8145 end Build_Discriminant_Constraints;
8147 ---------------------------------
8148 -- Build_Discriminated_Subtype --
8149 ---------------------------------
8151 procedure Build_Discriminated_Subtype
8152 (T : Entity_Id;
8153 Def_Id : Entity_Id;
8154 Elist : Elist_Id;
8155 Related_Nod : Node_Id;
8156 For_Access : Boolean := False)
8158 Has_Discrs : constant Boolean := Has_Discriminants (T);
8159 Constrained : constant Boolean :=
8160 (Has_Discrs
8161 and then not Is_Empty_Elmt_List (Elist)
8162 and then not Is_Class_Wide_Type (T))
8163 or else Is_Constrained (T);
8165 begin
8166 if Ekind (T) = E_Record_Type then
8167 if For_Access then
8168 Set_Ekind (Def_Id, E_Private_Subtype);
8169 Set_Is_For_Access_Subtype (Def_Id, True);
8170 else
8171 Set_Ekind (Def_Id, E_Record_Subtype);
8172 end if;
8174 -- Inherit preelaboration flag from base, for types for which it
8175 -- may have been set: records, private types, protected types.
8177 Set_Known_To_Have_Preelab_Init
8178 (Def_Id, Known_To_Have_Preelab_Init (T));
8180 elsif Ekind (T) = E_Task_Type then
8181 Set_Ekind (Def_Id, E_Task_Subtype);
8183 elsif Ekind (T) = E_Protected_Type then
8184 Set_Ekind (Def_Id, E_Protected_Subtype);
8185 Set_Known_To_Have_Preelab_Init
8186 (Def_Id, Known_To_Have_Preelab_Init (T));
8188 elsif Is_Private_Type (T) then
8189 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
8190 Set_Known_To_Have_Preelab_Init
8191 (Def_Id, Known_To_Have_Preelab_Init (T));
8193 elsif Is_Class_Wide_Type (T) then
8194 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
8196 else
8197 -- Incomplete type. Attach subtype to list of dependents, to be
8198 -- completed with full view of parent type, unless is it the
8199 -- designated subtype of a record component within an init_proc.
8200 -- This last case arises for a component of an access type whose
8201 -- designated type is incomplete (e.g. a Taft Amendment type).
8202 -- The designated subtype is within an inner scope, and needs no
8203 -- elaboration, because only the access type is needed in the
8204 -- initialization procedure.
8206 Set_Ekind (Def_Id, Ekind (T));
8208 if For_Access and then Within_Init_Proc then
8209 null;
8210 else
8211 Append_Elmt (Def_Id, Private_Dependents (T));
8212 end if;
8213 end if;
8215 Set_Etype (Def_Id, T);
8216 Init_Size_Align (Def_Id);
8217 Set_Has_Discriminants (Def_Id, Has_Discrs);
8218 Set_Is_Constrained (Def_Id, Constrained);
8220 Set_First_Entity (Def_Id, First_Entity (T));
8221 Set_Last_Entity (Def_Id, Last_Entity (T));
8223 -- If the subtype is the completion of a private declaration, there may
8224 -- have been representation clauses for the partial view, and they must
8225 -- be preserved. Build_Derived_Type chains the inherited clauses with
8226 -- the ones appearing on the extension. If this comes from a subtype
8227 -- declaration, all clauses are inherited.
8229 if No (First_Rep_Item (Def_Id)) then
8230 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8231 end if;
8233 if Is_Tagged_Type (T) then
8234 Set_Is_Tagged_Type (Def_Id);
8235 Make_Class_Wide_Type (Def_Id);
8236 end if;
8238 Set_Stored_Constraint (Def_Id, No_Elist);
8240 if Has_Discrs then
8241 Set_Discriminant_Constraint (Def_Id, Elist);
8242 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
8243 end if;
8245 if Is_Tagged_Type (T) then
8247 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
8248 -- concurrent record type (which has the list of primitive
8249 -- operations).
8251 if Ada_Version >= Ada_2005
8252 and then Is_Concurrent_Type (T)
8253 then
8254 Set_Corresponding_Record_Type (Def_Id,
8255 Corresponding_Record_Type (T));
8256 else
8257 Set_Direct_Primitive_Operations (Def_Id,
8258 Direct_Primitive_Operations (T));
8259 end if;
8261 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
8262 end if;
8264 -- Subtypes introduced by component declarations do not need to be
8265 -- marked as delayed, and do not get freeze nodes, because the semantics
8266 -- verifies that the parents of the subtypes are frozen before the
8267 -- enclosing record is frozen.
8269 if not Is_Type (Scope (Def_Id)) then
8270 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
8272 if Is_Private_Type (T)
8273 and then Present (Full_View (T))
8274 then
8275 Conditional_Delay (Def_Id, Full_View (T));
8276 else
8277 Conditional_Delay (Def_Id, T);
8278 end if;
8279 end if;
8281 if Is_Record_Type (T) then
8282 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
8284 if Has_Discrs
8285 and then not Is_Empty_Elmt_List (Elist)
8286 and then not For_Access
8287 then
8288 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
8289 elsif not For_Access then
8290 Set_Cloned_Subtype (Def_Id, T);
8291 end if;
8292 end if;
8293 end Build_Discriminated_Subtype;
8295 ---------------------------
8296 -- Build_Itype_Reference --
8297 ---------------------------
8299 procedure Build_Itype_Reference
8300 (Ityp : Entity_Id;
8301 Nod : Node_Id)
8303 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
8304 begin
8305 Set_Itype (IR, Ityp);
8306 Insert_After (Nod, IR);
8307 end Build_Itype_Reference;
8309 ------------------------
8310 -- Build_Scalar_Bound --
8311 ------------------------
8313 function Build_Scalar_Bound
8314 (Bound : Node_Id;
8315 Par_T : Entity_Id;
8316 Der_T : Entity_Id) return Node_Id
8318 New_Bound : Entity_Id;
8320 begin
8321 -- Note: not clear why this is needed, how can the original bound
8322 -- be unanalyzed at this point? and if it is, what business do we
8323 -- have messing around with it? and why is the base type of the
8324 -- parent type the right type for the resolution. It probably is
8325 -- not! It is OK for the new bound we are creating, but not for
8326 -- the old one??? Still if it never happens, no problem!
8328 Analyze_And_Resolve (Bound, Base_Type (Par_T));
8330 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
8331 New_Bound := New_Copy (Bound);
8332 Set_Etype (New_Bound, Der_T);
8333 Set_Analyzed (New_Bound);
8335 elsif Is_Entity_Name (Bound) then
8336 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
8338 -- The following is almost certainly wrong. What business do we have
8339 -- relocating a node (Bound) that is presumably still attached to
8340 -- the tree elsewhere???
8342 else
8343 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
8344 end if;
8346 Set_Etype (New_Bound, Der_T);
8347 return New_Bound;
8348 end Build_Scalar_Bound;
8350 --------------------------------
8351 -- Build_Underlying_Full_View --
8352 --------------------------------
8354 procedure Build_Underlying_Full_View
8355 (N : Node_Id;
8356 Typ : Entity_Id;
8357 Par : Entity_Id)
8359 Loc : constant Source_Ptr := Sloc (N);
8360 Subt : constant Entity_Id :=
8361 Make_Defining_Identifier
8362 (Loc, New_External_Name (Chars (Typ), 'S'));
8364 Constr : Node_Id;
8365 Indic : Node_Id;
8366 C : Node_Id;
8367 Id : Node_Id;
8369 procedure Set_Discriminant_Name (Id : Node_Id);
8370 -- If the derived type has discriminants, they may rename discriminants
8371 -- of the parent. When building the full view of the parent, we need to
8372 -- recover the names of the original discriminants if the constraint is
8373 -- given by named associations.
8375 ---------------------------
8376 -- Set_Discriminant_Name --
8377 ---------------------------
8379 procedure Set_Discriminant_Name (Id : Node_Id) is
8380 Disc : Entity_Id;
8382 begin
8383 Set_Original_Discriminant (Id, Empty);
8385 if Has_Discriminants (Typ) then
8386 Disc := First_Discriminant (Typ);
8387 while Present (Disc) loop
8388 if Chars (Disc) = Chars (Id)
8389 and then Present (Corresponding_Discriminant (Disc))
8390 then
8391 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
8392 end if;
8393 Next_Discriminant (Disc);
8394 end loop;
8395 end if;
8396 end Set_Discriminant_Name;
8398 -- Start of processing for Build_Underlying_Full_View
8400 begin
8401 if Nkind (N) = N_Full_Type_Declaration then
8402 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
8404 elsif Nkind (N) = N_Subtype_Declaration then
8405 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
8407 elsif Nkind (N) = N_Component_Declaration then
8408 Constr :=
8409 New_Copy_Tree
8410 (Constraint (Subtype_Indication (Component_Definition (N))));
8412 else
8413 raise Program_Error;
8414 end if;
8416 C := First (Constraints (Constr));
8417 while Present (C) loop
8418 if Nkind (C) = N_Discriminant_Association then
8419 Id := First (Selector_Names (C));
8420 while Present (Id) loop
8421 Set_Discriminant_Name (Id);
8422 Next (Id);
8423 end loop;
8424 end if;
8426 Next (C);
8427 end loop;
8429 Indic :=
8430 Make_Subtype_Declaration (Loc,
8431 Defining_Identifier => Subt,
8432 Subtype_Indication =>
8433 Make_Subtype_Indication (Loc,
8434 Subtype_Mark => New_Reference_To (Par, Loc),
8435 Constraint => New_Copy_Tree (Constr)));
8437 -- If this is a component subtype for an outer itype, it is not
8438 -- a list member, so simply set the parent link for analysis: if
8439 -- the enclosing type does not need to be in a declarative list,
8440 -- neither do the components.
8442 if Is_List_Member (N)
8443 and then Nkind (N) /= N_Component_Declaration
8444 then
8445 Insert_Before (N, Indic);
8446 else
8447 Set_Parent (Indic, Parent (N));
8448 end if;
8450 Analyze (Indic);
8451 Set_Underlying_Full_View (Typ, Full_View (Subt));
8452 end Build_Underlying_Full_View;
8454 -------------------------------
8455 -- Check_Abstract_Overriding --
8456 -------------------------------
8458 procedure Check_Abstract_Overriding (T : Entity_Id) is
8459 Alias_Subp : Entity_Id;
8460 Elmt : Elmt_Id;
8461 Op_List : Elist_Id;
8462 Subp : Entity_Id;
8463 Type_Def : Node_Id;
8465 procedure Check_Pragma_Implemented (Subp : Entity_Id);
8466 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
8467 -- which has pragma Implemented already set. Check whether Subp's entity
8468 -- kind conforms to the implementation kind of the overridden routine.
8470 procedure Check_Pragma_Implemented
8471 (Subp : Entity_Id;
8472 Iface_Subp : Entity_Id);
8473 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
8474 -- Iface_Subp and both entities have pragma Implemented already set on
8475 -- them. Check whether the two implementation kinds are conforming.
8477 procedure Inherit_Pragma_Implemented
8478 (Subp : Entity_Id;
8479 Iface_Subp : Entity_Id);
8480 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
8481 -- subprogram Iface_Subp which has been marked by pragma Implemented.
8482 -- Propagate the implementation kind of Iface_Subp to Subp.
8484 ------------------------------
8485 -- Check_Pragma_Implemented --
8486 ------------------------------
8488 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
8489 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
8490 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
8491 Contr_Typ : Entity_Id;
8493 begin
8494 -- Subp must have an alias since it is a hidden entity used to link
8495 -- an interface subprogram to its overriding counterpart.
8497 pragma Assert (Present (Alias (Subp)));
8499 -- Extract the type of the controlling formal
8501 Contr_Typ := Etype (First_Formal (Alias (Subp)));
8503 if Is_Concurrent_Record_Type (Contr_Typ) then
8504 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
8505 end if;
8507 -- An interface subprogram whose implementation kind is By_Entry must
8508 -- be implemented by an entry.
8510 if Impl_Kind = Name_By_Entry
8511 and then Ekind (Wrapped_Entity (Alias (Subp))) /= E_Entry
8512 then
8513 Error_Msg_Node_2 := Iface_Alias;
8514 Error_Msg_NE
8515 ("type & must implement abstract subprogram & with an entry",
8516 Alias (Subp), Contr_Typ);
8518 elsif Impl_Kind = Name_By_Protected_Procedure then
8520 -- An interface subprogram whose implementation kind is By_
8521 -- Protected_Procedure cannot be implemented by a primitive
8522 -- procedure of a task type.
8524 if Ekind (Contr_Typ) /= E_Protected_Type then
8525 Error_Msg_Node_2 := Contr_Typ;
8526 Error_Msg_NE
8527 ("interface subprogram & cannot be implemented by a " &
8528 "primitive procedure of task type &", Alias (Subp),
8529 Iface_Alias);
8531 -- An interface subprogram whose implementation kind is By_
8532 -- Protected_Procedure must be implemented by a procedure.
8534 elsif Is_Primitive_Wrapper (Alias (Subp))
8535 and then Ekind (Wrapped_Entity (Alias (Subp))) /= E_Procedure
8536 then
8537 Error_Msg_Node_2 := Iface_Alias;
8538 Error_Msg_NE
8539 ("type & must implement abstract subprogram & with a " &
8540 "procedure", Alias (Subp), Contr_Typ);
8541 end if;
8542 end if;
8543 end Check_Pragma_Implemented;
8545 ------------------------------
8546 -- Check_Pragma_Implemented --
8547 ------------------------------
8549 procedure Check_Pragma_Implemented
8550 (Subp : Entity_Id;
8551 Iface_Subp : Entity_Id)
8553 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8554 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
8556 begin
8557 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
8558 -- and overriding subprogram are different. In general this is an
8559 -- error except when the implementation kind of the overridden
8560 -- subprograms is By_Any.
8562 if Iface_Kind /= Subp_Kind
8563 and then Iface_Kind /= Name_By_Any
8564 then
8565 if Iface_Kind = Name_By_Entry then
8566 Error_Msg_N
8567 ("incompatible implementation kind, overridden subprogram " &
8568 "is marked By_Entry", Subp);
8569 else
8570 Error_Msg_N
8571 ("incompatible implementation kind, overridden subprogram " &
8572 "is marked By_Protected_Procedure", Subp);
8573 end if;
8574 end if;
8575 end Check_Pragma_Implemented;
8577 --------------------------------
8578 -- Inherit_Pragma_Implemented --
8579 --------------------------------
8581 procedure Inherit_Pragma_Implemented
8582 (Subp : Entity_Id;
8583 Iface_Subp : Entity_Id)
8585 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8586 Loc : constant Source_Ptr := Sloc (Subp);
8587 Impl_Prag : Node_Id;
8589 begin
8590 -- Since the implementation kind is stored as a representation item
8591 -- rather than a flag, create a pragma node.
8593 Impl_Prag :=
8594 Make_Pragma (Loc,
8595 Chars => Name_Implemented,
8596 Pragma_Argument_Associations => New_List (
8597 Make_Pragma_Argument_Association (Loc,
8598 Expression =>
8599 New_Reference_To (Subp, Loc)),
8601 Make_Pragma_Argument_Association (Loc,
8602 Expression =>
8603 Make_Identifier (Loc, Iface_Kind))));
8605 -- The pragma doesn't need to be analyzed because it is internaly
8606 -- build. It is safe to directly register it as a rep item since we
8607 -- are only interested in the characters of the implementation kind.
8609 Record_Rep_Item (Subp, Impl_Prag);
8610 end Inherit_Pragma_Implemented;
8612 -- Start of processing for Check_Abstract_Overriding
8614 begin
8615 Op_List := Primitive_Operations (T);
8617 -- Loop to check primitive operations
8619 Elmt := First_Elmt (Op_List);
8620 while Present (Elmt) loop
8621 Subp := Node (Elmt);
8622 Alias_Subp := Alias (Subp);
8624 -- Inherited subprograms are identified by the fact that they do not
8625 -- come from source, and the associated source location is the
8626 -- location of the first subtype of the derived type.
8628 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
8629 -- subprograms that "require overriding".
8631 -- Special exception, do not complain about failure to override the
8632 -- stream routines _Input and _Output, as well as the primitive
8633 -- operations used in dispatching selects since we always provide
8634 -- automatic overridings for these subprograms.
8636 -- Also ignore this rule for convention CIL since .NET libraries
8637 -- do bizarre things with interfaces???
8639 -- The partial view of T may have been a private extension, for
8640 -- which inherited functions dispatching on result are abstract.
8641 -- If the full view is a null extension, there is no need for
8642 -- overriding in Ada2005, but wrappers need to be built for them
8643 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
8645 if Is_Null_Extension (T)
8646 and then Has_Controlling_Result (Subp)
8647 and then Ada_Version >= Ada_2005
8648 and then Present (Alias_Subp)
8649 and then not Comes_From_Source (Subp)
8650 and then not Is_Abstract_Subprogram (Alias_Subp)
8651 and then not Is_Access_Type (Etype (Subp))
8652 then
8653 null;
8655 -- Ada 2005 (AI-251): Internal entities of interfaces need no
8656 -- processing because this check is done with the aliased
8657 -- entity
8659 elsif Present (Interface_Alias (Subp)) then
8660 null;
8662 elsif (Is_Abstract_Subprogram (Subp)
8663 or else Requires_Overriding (Subp)
8664 or else
8665 (Has_Controlling_Result (Subp)
8666 and then Present (Alias_Subp)
8667 and then not Comes_From_Source (Subp)
8668 and then Sloc (Subp) = Sloc (First_Subtype (T))))
8669 and then not Is_TSS (Subp, TSS_Stream_Input)
8670 and then not Is_TSS (Subp, TSS_Stream_Output)
8671 and then not Is_Abstract_Type (T)
8672 and then Convention (T) /= Convention_CIL
8673 and then not Is_Predefined_Interface_Primitive (Subp)
8675 -- Ada 2005 (AI-251): Do not consider hidden entities associated
8676 -- with abstract interface types because the check will be done
8677 -- with the aliased entity (otherwise we generate a duplicated
8678 -- error message).
8680 and then not Present (Interface_Alias (Subp))
8681 then
8682 if Present (Alias_Subp) then
8684 -- Only perform the check for a derived subprogram when the
8685 -- type has an explicit record extension. This avoids incorrect
8686 -- flagging of abstract subprograms for the case of a type
8687 -- without an extension that is derived from a formal type
8688 -- with a tagged actual (can occur within a private part).
8690 -- Ada 2005 (AI-391): In the case of an inherited function with
8691 -- a controlling result of the type, the rule does not apply if
8692 -- the type is a null extension (unless the parent function
8693 -- itself is abstract, in which case the function must still be
8694 -- be overridden). The expander will generate an overriding
8695 -- wrapper function calling the parent subprogram (see
8696 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
8698 Type_Def := Type_Definition (Parent (T));
8700 if Nkind (Type_Def) = N_Derived_Type_Definition
8701 and then Present (Record_Extension_Part (Type_Def))
8702 and then
8703 (Ada_Version < Ada_2005
8704 or else not Is_Null_Extension (T)
8705 or else Ekind (Subp) = E_Procedure
8706 or else not Has_Controlling_Result (Subp)
8707 or else Is_Abstract_Subprogram (Alias_Subp)
8708 or else Requires_Overriding (Subp)
8709 or else Is_Access_Type (Etype (Subp)))
8710 then
8711 -- Avoid reporting error in case of abstract predefined
8712 -- primitive inherited from interface type because the
8713 -- body of internally generated predefined primitives
8714 -- of tagged types are generated later by Freeze_Type
8716 if Is_Interface (Root_Type (T))
8717 and then Is_Abstract_Subprogram (Subp)
8718 and then Is_Predefined_Dispatching_Operation (Subp)
8719 and then not Comes_From_Source (Ultimate_Alias (Subp))
8720 then
8721 null;
8723 else
8724 Error_Msg_NE
8725 ("type must be declared abstract or & overridden",
8726 T, Subp);
8728 -- Traverse the whole chain of aliased subprograms to
8729 -- complete the error notification. This is especially
8730 -- useful for traceability of the chain of entities when
8731 -- the subprogram corresponds with an interface
8732 -- subprogram (which may be defined in another package).
8734 if Present (Alias_Subp) then
8735 declare
8736 E : Entity_Id;
8738 begin
8739 E := Subp;
8740 while Present (Alias (E)) loop
8741 Error_Msg_Sloc := Sloc (E);
8742 Error_Msg_NE
8743 ("\& has been inherited #", T, Subp);
8744 E := Alias (E);
8745 end loop;
8747 Error_Msg_Sloc := Sloc (E);
8748 Error_Msg_NE
8749 ("\& has been inherited from subprogram #",
8750 T, Subp);
8751 end;
8752 end if;
8753 end if;
8755 -- Ada 2005 (AI-345): Protected or task type implementing
8756 -- abstract interfaces.
8758 elsif Is_Concurrent_Record_Type (T)
8759 and then Present (Interfaces (T))
8760 then
8761 -- The controlling formal of Subp must be of mode "out",
8762 -- "in out" or an access-to-variable to be overridden.
8764 -- Error message below needs rewording (remember comma
8765 -- in -gnatj mode) ???
8767 if Ekind (First_Formal (Subp)) = E_In_Parameter
8768 and then Ekind (Subp) /= E_Function
8769 then
8770 if not Is_Predefined_Dispatching_Operation (Subp) then
8771 Error_Msg_NE
8772 ("first formal of & must be of mode `OUT`, " &
8773 "`IN OUT` or access-to-variable", T, Subp);
8774 Error_Msg_N
8775 ("\to be overridden by protected procedure or " &
8776 "entry (RM 9.4(11.9/2))", T);
8777 end if;
8779 -- Some other kind of overriding failure
8781 else
8782 Error_Msg_NE
8783 ("interface subprogram & must be overridden",
8784 T, Subp);
8786 -- Examine primitive operations of synchronized type,
8787 -- to find homonyms that have the wrong profile.
8789 declare
8790 Prim : Entity_Id;
8792 begin
8793 Prim :=
8794 First_Entity (Corresponding_Concurrent_Type (T));
8795 while Present (Prim) loop
8796 if Chars (Prim) = Chars (Subp) then
8797 Error_Msg_NE
8798 ("profile is not type conformant with "
8799 & "prefixed view profile of "
8800 & "inherited operation&", Prim, Subp);
8801 end if;
8803 Next_Entity (Prim);
8804 end loop;
8805 end;
8806 end if;
8807 end if;
8809 else
8810 Error_Msg_Node_2 := T;
8811 Error_Msg_N
8812 ("abstract subprogram& not allowed for type&", Subp);
8814 -- Also post unconditional warning on the type (unconditional
8815 -- so that if there are more than one of these cases, we get
8816 -- them all, and not just the first one).
8818 Error_Msg_Node_2 := Subp;
8819 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
8820 end if;
8821 end if;
8823 -- Ada 2012 (AI05-0030): Perform some checks related to pragma
8824 -- Implemented
8826 -- Subp is an expander-generated procedure which maps an interface
8827 -- alias to a protected wrapper. The interface alias is flagged by
8828 -- pragma Implemented. Ensure that Subp is a procedure when the
8829 -- implementation kind is By_Protected_Procedure or an entry when
8830 -- By_Entry.
8832 if Ada_Version >= Ada_2012
8833 and then Is_Hidden (Subp)
8834 and then Present (Interface_Alias (Subp))
8835 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
8836 then
8837 Check_Pragma_Implemented (Subp);
8838 end if;
8840 -- Subp is an interface primitive which overrides another interface
8841 -- primitive marked with pragma Implemented.
8843 if Ada_Version >= Ada_2012
8844 and then Is_Overriding_Operation (Subp)
8845 and then Present (Overridden_Operation (Subp))
8846 and then Has_Rep_Pragma
8847 (Overridden_Operation (Subp), Name_Implemented)
8848 then
8849 -- If the overriding routine is also marked by Implemented, check
8850 -- that the two implementation kinds are conforming.
8852 if Has_Rep_Pragma (Subp, Name_Implemented) then
8853 Check_Pragma_Implemented
8854 (Subp => Subp,
8855 Iface_Subp => Overridden_Operation (Subp));
8857 -- Otherwise the overriding routine inherits the implementation
8858 -- kind from the overridden subprogram.
8860 else
8861 Inherit_Pragma_Implemented
8862 (Subp => Subp,
8863 Iface_Subp => Overridden_Operation (Subp));
8864 end if;
8865 end if;
8867 Next_Elmt (Elmt);
8868 end loop;
8869 end Check_Abstract_Overriding;
8871 ------------------------------------------------
8872 -- Check_Access_Discriminant_Requires_Limited --
8873 ------------------------------------------------
8875 procedure Check_Access_Discriminant_Requires_Limited
8876 (D : Node_Id;
8877 Loc : Node_Id)
8879 begin
8880 -- A discriminant_specification for an access discriminant shall appear
8881 -- only in the declaration for a task or protected type, or for a type
8882 -- with the reserved word 'limited' in its definition or in one of its
8883 -- ancestors (RM 3.7(10)).
8885 -- AI-0063: The proper condition is that type must be immutably limited,
8886 -- or else be a partial view.
8888 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
8889 if Is_Immutably_Limited_Type (Current_Scope)
8890 or else
8891 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
8892 and then Limited_Present (Parent (Current_Scope)))
8893 then
8894 null;
8896 else
8897 Error_Msg_N
8898 ("access discriminants allowed only for limited types", Loc);
8899 end if;
8900 end if;
8901 end Check_Access_Discriminant_Requires_Limited;
8903 -----------------------------------
8904 -- Check_Aliased_Component_Types --
8905 -----------------------------------
8907 procedure Check_Aliased_Component_Types (T : Entity_Id) is
8908 C : Entity_Id;
8910 begin
8911 -- ??? Also need to check components of record extensions, but not
8912 -- components of protected types (which are always limited).
8914 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
8915 -- types to be unconstrained. This is safe because it is illegal to
8916 -- create access subtypes to such types with explicit discriminant
8917 -- constraints.
8919 if not Is_Limited_Type (T) then
8920 if Ekind (T) = E_Record_Type then
8921 C := First_Component (T);
8922 while Present (C) loop
8923 if Is_Aliased (C)
8924 and then Has_Discriminants (Etype (C))
8925 and then not Is_Constrained (Etype (C))
8926 and then not In_Instance_Body
8927 and then Ada_Version < Ada_2005
8928 then
8929 Error_Msg_N
8930 ("aliased component must be constrained (RM 3.6(11))",
8932 end if;
8934 Next_Component (C);
8935 end loop;
8937 elsif Ekind (T) = E_Array_Type then
8938 if Has_Aliased_Components (T)
8939 and then Has_Discriminants (Component_Type (T))
8940 and then not Is_Constrained (Component_Type (T))
8941 and then not In_Instance_Body
8942 and then Ada_Version < Ada_2005
8943 then
8944 Error_Msg_N
8945 ("aliased component type must be constrained (RM 3.6(11))",
8947 end if;
8948 end if;
8949 end if;
8950 end Check_Aliased_Component_Types;
8952 ----------------------
8953 -- Check_Completion --
8954 ----------------------
8956 procedure Check_Completion (Body_Id : Node_Id := Empty) is
8957 E : Entity_Id;
8959 procedure Post_Error;
8960 -- Post error message for lack of completion for entity E
8962 ----------------
8963 -- Post_Error --
8964 ----------------
8966 procedure Post_Error is
8968 procedure Missing_Body;
8969 -- Output missing body message
8971 ------------------
8972 -- Missing_Body --
8973 ------------------
8975 procedure Missing_Body is
8976 begin
8977 -- Spec is in same unit, so we can post on spec
8979 if In_Same_Source_Unit (Body_Id, E) then
8980 Error_Msg_N ("missing body for &", E);
8982 -- Spec is in a separate unit, so we have to post on the body
8984 else
8985 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
8986 end if;
8987 end Missing_Body;
8989 -- Start of processing for Post_Error
8991 begin
8992 if not Comes_From_Source (E) then
8994 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
8995 -- It may be an anonymous protected type created for a
8996 -- single variable. Post error on variable, if present.
8998 declare
8999 Var : Entity_Id;
9001 begin
9002 Var := First_Entity (Current_Scope);
9003 while Present (Var) loop
9004 exit when Etype (Var) = E
9005 and then Comes_From_Source (Var);
9007 Next_Entity (Var);
9008 end loop;
9010 if Present (Var) then
9011 E := Var;
9012 end if;
9013 end;
9014 end if;
9015 end if;
9017 -- If a generated entity has no completion, then either previous
9018 -- semantic errors have disabled the expansion phase, or else we had
9019 -- missing subunits, or else we are compiling without expansion,
9020 -- or else something is very wrong.
9022 if not Comes_From_Source (E) then
9023 pragma Assert
9024 (Serious_Errors_Detected > 0
9025 or else Configurable_Run_Time_Violations > 0
9026 or else Subunits_Missing
9027 or else not Expander_Active);
9028 return;
9030 -- Here for source entity
9032 else
9033 -- Here if no body to post the error message, so we post the error
9034 -- on the declaration that has no completion. This is not really
9035 -- the right place to post it, think about this later ???
9037 if No (Body_Id) then
9038 if Is_Type (E) then
9039 Error_Msg_NE
9040 ("missing full declaration for }", Parent (E), E);
9041 else
9042 Error_Msg_NE ("missing body for &", Parent (E), E);
9043 end if;
9045 -- Package body has no completion for a declaration that appears
9046 -- in the corresponding spec. Post error on the body, with a
9047 -- reference to the non-completed declaration.
9049 else
9050 Error_Msg_Sloc := Sloc (E);
9052 if Is_Type (E) then
9053 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
9055 elsif Is_Overloadable (E)
9056 and then Current_Entity_In_Scope (E) /= E
9057 then
9058 -- It may be that the completion is mistyped and appears as
9059 -- a distinct overloading of the entity.
9061 declare
9062 Candidate : constant Entity_Id :=
9063 Current_Entity_In_Scope (E);
9064 Decl : constant Node_Id :=
9065 Unit_Declaration_Node (Candidate);
9067 begin
9068 if Is_Overloadable (Candidate)
9069 and then Ekind (Candidate) = Ekind (E)
9070 and then Nkind (Decl) = N_Subprogram_Body
9071 and then Acts_As_Spec (Decl)
9072 then
9073 Check_Type_Conformant (Candidate, E);
9075 else
9076 Missing_Body;
9077 end if;
9078 end;
9080 else
9081 Missing_Body;
9082 end if;
9083 end if;
9084 end if;
9085 end Post_Error;
9087 -- Start of processing for Check_Completion
9089 begin
9090 E := First_Entity (Current_Scope);
9091 while Present (E) loop
9092 if Is_Intrinsic_Subprogram (E) then
9093 null;
9095 -- The following situation requires special handling: a child unit
9096 -- that appears in the context clause of the body of its parent:
9098 -- procedure Parent.Child (...);
9100 -- with Parent.Child;
9101 -- package body Parent is
9103 -- Here Parent.Child appears as a local entity, but should not be
9104 -- flagged as requiring completion, because it is a compilation
9105 -- unit.
9107 -- Ignore missing completion for a subprogram that does not come from
9108 -- source (including the _Call primitive operation of RAS types,
9109 -- which has to have the flag Comes_From_Source for other purposes):
9110 -- we assume that the expander will provide the missing completion.
9111 -- In case of previous errors, other expansion actions that provide
9112 -- bodies for null procedures with not be invoked, so inhibit message
9113 -- in those cases.
9114 -- Note that E_Operator is not in the list that follows, because
9115 -- this kind is reserved for predefined operators, that are
9116 -- intrinsic and do not need completion.
9118 elsif Ekind (E) = E_Function
9119 or else Ekind (E) = E_Procedure
9120 or else Ekind (E) = E_Generic_Function
9121 or else Ekind (E) = E_Generic_Procedure
9122 then
9123 if Has_Completion (E) then
9124 null;
9126 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
9127 null;
9129 elsif Is_Subprogram (E)
9130 and then (not Comes_From_Source (E)
9131 or else Chars (E) = Name_uCall)
9132 then
9133 null;
9135 elsif
9136 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9137 then
9138 null;
9140 elsif Nkind (Parent (E)) = N_Procedure_Specification
9141 and then Null_Present (Parent (E))
9142 and then Serious_Errors_Detected > 0
9143 then
9144 null;
9146 else
9147 Post_Error;
9148 end if;
9150 elsif Is_Entry (E) then
9151 if not Has_Completion (E) and then
9152 (Ekind (Scope (E)) = E_Protected_Object
9153 or else Ekind (Scope (E)) = E_Protected_Type)
9154 then
9155 Post_Error;
9156 end if;
9158 elsif Is_Package_Or_Generic_Package (E) then
9159 if Unit_Requires_Body (E) then
9160 if not Has_Completion (E)
9161 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
9162 N_Compilation_Unit
9163 then
9164 Post_Error;
9165 end if;
9167 elsif not Is_Child_Unit (E) then
9168 May_Need_Implicit_Body (E);
9169 end if;
9171 elsif Ekind (E) = E_Incomplete_Type
9172 and then No (Underlying_Type (E))
9173 then
9174 Post_Error;
9176 elsif (Ekind (E) = E_Task_Type or else
9177 Ekind (E) = E_Protected_Type)
9178 and then not Has_Completion (E)
9179 then
9180 Post_Error;
9182 -- A single task declared in the current scope is a constant, verify
9183 -- that the body of its anonymous type is in the same scope. If the
9184 -- task is defined elsewhere, this may be a renaming declaration for
9185 -- which no completion is needed.
9187 elsif Ekind (E) = E_Constant
9188 and then Ekind (Etype (E)) = E_Task_Type
9189 and then not Has_Completion (Etype (E))
9190 and then Scope (Etype (E)) = Current_Scope
9191 then
9192 Post_Error;
9194 elsif Ekind (E) = E_Protected_Object
9195 and then not Has_Completion (Etype (E))
9196 then
9197 Post_Error;
9199 elsif Ekind (E) = E_Record_Type then
9200 if Is_Tagged_Type (E) then
9201 Check_Abstract_Overriding (E);
9202 Check_Conventions (E);
9203 end if;
9205 Check_Aliased_Component_Types (E);
9207 elsif Ekind (E) = E_Array_Type then
9208 Check_Aliased_Component_Types (E);
9210 end if;
9212 Next_Entity (E);
9213 end loop;
9214 end Check_Completion;
9216 ----------------------------
9217 -- Check_Delta_Expression --
9218 ----------------------------
9220 procedure Check_Delta_Expression (E : Node_Id) is
9221 begin
9222 if not (Is_Real_Type (Etype (E))) then
9223 Wrong_Type (E, Any_Real);
9225 elsif not Is_OK_Static_Expression (E) then
9226 Flag_Non_Static_Expr
9227 ("non-static expression used for delta value!", E);
9229 elsif not UR_Is_Positive (Expr_Value_R (E)) then
9230 Error_Msg_N ("delta expression must be positive", E);
9232 else
9233 return;
9234 end if;
9236 -- If any of above errors occurred, then replace the incorrect
9237 -- expression by the real 0.1, which should prevent further errors.
9239 Rewrite (E,
9240 Make_Real_Literal (Sloc (E), Ureal_Tenth));
9241 Analyze_And_Resolve (E, Standard_Float);
9242 end Check_Delta_Expression;
9244 -----------------------------
9245 -- Check_Digits_Expression --
9246 -----------------------------
9248 procedure Check_Digits_Expression (E : Node_Id) is
9249 begin
9250 if not (Is_Integer_Type (Etype (E))) then
9251 Wrong_Type (E, Any_Integer);
9253 elsif not Is_OK_Static_Expression (E) then
9254 Flag_Non_Static_Expr
9255 ("non-static expression used for digits value!", E);
9257 elsif Expr_Value (E) <= 0 then
9258 Error_Msg_N ("digits value must be greater than zero", E);
9260 else
9261 return;
9262 end if;
9264 -- If any of above errors occurred, then replace the incorrect
9265 -- expression by the integer 1, which should prevent further errors.
9267 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
9268 Analyze_And_Resolve (E, Standard_Integer);
9270 end Check_Digits_Expression;
9272 --------------------------
9273 -- Check_Initialization --
9274 --------------------------
9276 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
9277 begin
9278 if Is_Limited_Type (T)
9279 and then not In_Instance
9280 and then not In_Inlined_Body
9281 then
9282 if not OK_For_Limited_Init (T, Exp) then
9284 -- In GNAT mode, this is just a warning, to allow it to be evilly
9285 -- turned off. Otherwise it is a real error.
9287 if GNAT_Mode then
9288 Error_Msg_N
9289 ("?cannot initialize entities of limited type!", Exp);
9291 elsif Ada_Version < Ada_2005 then
9292 Error_Msg_N
9293 ("cannot initialize entities of limited type", Exp);
9294 Explain_Limited_Type (T, Exp);
9296 else
9297 -- Specialize error message according to kind of illegal
9298 -- initial expression.
9300 if Nkind (Exp) = N_Type_Conversion
9301 and then Nkind (Expression (Exp)) = N_Function_Call
9302 then
9303 Error_Msg_N
9304 ("illegal context for call"
9305 & " to function with limited result", Exp);
9307 else
9308 Error_Msg_N
9309 ("initialization of limited object requires aggregate "
9310 & "or function call", Exp);
9311 end if;
9312 end if;
9313 end if;
9314 end if;
9315 end Check_Initialization;
9317 ----------------------
9318 -- Check_Interfaces --
9319 ----------------------
9321 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
9322 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
9324 Iface : Node_Id;
9325 Iface_Def : Node_Id;
9326 Iface_Typ : Entity_Id;
9327 Parent_Node : Node_Id;
9329 Is_Task : Boolean := False;
9330 -- Set True if parent type or any progenitor is a task interface
9332 Is_Protected : Boolean := False;
9333 -- Set True if parent type or any progenitor is a protected interface
9335 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
9336 -- Check that a progenitor is compatible with declaration.
9337 -- Error is posted on Error_Node.
9339 ------------------
9340 -- Check_Ifaces --
9341 ------------------
9343 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
9344 Iface_Id : constant Entity_Id :=
9345 Defining_Identifier (Parent (Iface_Def));
9346 Type_Def : Node_Id;
9348 begin
9349 if Nkind (N) = N_Private_Extension_Declaration then
9350 Type_Def := N;
9351 else
9352 Type_Def := Type_Definition (N);
9353 end if;
9355 if Is_Task_Interface (Iface_Id) then
9356 Is_Task := True;
9358 elsif Is_Protected_Interface (Iface_Id) then
9359 Is_Protected := True;
9360 end if;
9362 if Is_Synchronized_Interface (Iface_Id) then
9364 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
9365 -- extension derived from a synchronized interface must explicitly
9366 -- be declared synchronized, because the full view will be a
9367 -- synchronized type.
9369 if Nkind (N) = N_Private_Extension_Declaration then
9370 if not Synchronized_Present (N) then
9371 Error_Msg_NE
9372 ("private extension of& must be explicitly synchronized",
9373 N, Iface_Id);
9374 end if;
9376 -- However, by 3.9.4(16/2), a full type that is a record extension
9377 -- is never allowed to derive from a synchronized interface (note
9378 -- that interfaces must be excluded from this check, because those
9379 -- are represented by derived type definitions in some cases).
9381 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9382 and then not Interface_Present (Type_Definition (N))
9383 then
9384 Error_Msg_N ("record extension cannot derive from synchronized"
9385 & " interface", Error_Node);
9386 end if;
9387 end if;
9389 -- Check that the characteristics of the progenitor are compatible
9390 -- with the explicit qualifier in the declaration.
9391 -- The check only applies to qualifiers that come from source.
9392 -- Limited_Present also appears in the declaration of corresponding
9393 -- records, and the check does not apply to them.
9395 if Limited_Present (Type_Def)
9396 and then not
9397 Is_Concurrent_Record_Type (Defining_Identifier (N))
9398 then
9399 if Is_Limited_Interface (Parent_Type)
9400 and then not Is_Limited_Interface (Iface_Id)
9401 then
9402 Error_Msg_NE
9403 ("progenitor& must be limited interface",
9404 Error_Node, Iface_Id);
9406 elsif
9407 (Task_Present (Iface_Def)
9408 or else Protected_Present (Iface_Def)
9409 or else Synchronized_Present (Iface_Def))
9410 and then Nkind (N) /= N_Private_Extension_Declaration
9411 and then not Error_Posted (N)
9412 then
9413 Error_Msg_NE
9414 ("progenitor& must be limited interface",
9415 Error_Node, Iface_Id);
9416 end if;
9418 -- Protected interfaces can only inherit from limited, synchronized
9419 -- or protected interfaces.
9421 elsif Nkind (N) = N_Full_Type_Declaration
9422 and then Protected_Present (Type_Def)
9423 then
9424 if Limited_Present (Iface_Def)
9425 or else Synchronized_Present (Iface_Def)
9426 or else Protected_Present (Iface_Def)
9427 then
9428 null;
9430 elsif Task_Present (Iface_Def) then
9431 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9432 & " from task interface", Error_Node);
9434 else
9435 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9436 & " from non-limited interface", Error_Node);
9437 end if;
9439 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
9440 -- limited and synchronized.
9442 elsif Synchronized_Present (Type_Def) then
9443 if Limited_Present (Iface_Def)
9444 or else Synchronized_Present (Iface_Def)
9445 then
9446 null;
9448 elsif Protected_Present (Iface_Def)
9449 and then Nkind (N) /= N_Private_Extension_Declaration
9450 then
9451 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9452 & " from protected interface", Error_Node);
9454 elsif Task_Present (Iface_Def)
9455 and then Nkind (N) /= N_Private_Extension_Declaration
9456 then
9457 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9458 & " from task interface", Error_Node);
9460 elsif not Is_Limited_Interface (Iface_Id) then
9461 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9462 & " from non-limited interface", Error_Node);
9463 end if;
9465 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
9466 -- synchronized or task interfaces.
9468 elsif Nkind (N) = N_Full_Type_Declaration
9469 and then Task_Present (Type_Def)
9470 then
9471 if Limited_Present (Iface_Def)
9472 or else Synchronized_Present (Iface_Def)
9473 or else Task_Present (Iface_Def)
9474 then
9475 null;
9477 elsif Protected_Present (Iface_Def) then
9478 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9479 & " protected interface", Error_Node);
9481 else
9482 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9483 & " non-limited interface", Error_Node);
9484 end if;
9485 end if;
9486 end Check_Ifaces;
9488 -- Start of processing for Check_Interfaces
9490 begin
9491 if Is_Interface (Parent_Type) then
9492 if Is_Task_Interface (Parent_Type) then
9493 Is_Task := True;
9495 elsif Is_Protected_Interface (Parent_Type) then
9496 Is_Protected := True;
9497 end if;
9498 end if;
9500 if Nkind (N) = N_Private_Extension_Declaration then
9502 -- Check that progenitors are compatible with declaration
9504 Iface := First (Interface_List (Def));
9505 while Present (Iface) loop
9506 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9508 Parent_Node := Parent (Base_Type (Iface_Typ));
9509 Iface_Def := Type_Definition (Parent_Node);
9511 if not Is_Interface (Iface_Typ) then
9512 Diagnose_Interface (Iface, Iface_Typ);
9514 else
9515 Check_Ifaces (Iface_Def, Iface);
9516 end if;
9518 Next (Iface);
9519 end loop;
9521 if Is_Task and Is_Protected then
9522 Error_Msg_N
9523 ("type cannot derive from task and protected interface", N);
9524 end if;
9526 return;
9527 end if;
9529 -- Full type declaration of derived type.
9530 -- Check compatibility with parent if it is interface type
9532 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9533 and then Is_Interface (Parent_Type)
9534 then
9535 Parent_Node := Parent (Parent_Type);
9537 -- More detailed checks for interface varieties
9539 Check_Ifaces
9540 (Iface_Def => Type_Definition (Parent_Node),
9541 Error_Node => Subtype_Indication (Type_Definition (N)));
9542 end if;
9544 Iface := First (Interface_List (Def));
9545 while Present (Iface) loop
9546 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9548 Parent_Node := Parent (Base_Type (Iface_Typ));
9549 Iface_Def := Type_Definition (Parent_Node);
9551 if not Is_Interface (Iface_Typ) then
9552 Diagnose_Interface (Iface, Iface_Typ);
9554 else
9555 -- "The declaration of a specific descendant of an interface
9556 -- type freezes the interface type" RM 13.14
9558 Freeze_Before (N, Iface_Typ);
9559 Check_Ifaces (Iface_Def, Error_Node => Iface);
9560 end if;
9562 Next (Iface);
9563 end loop;
9565 if Is_Task and Is_Protected then
9566 Error_Msg_N
9567 ("type cannot derive from task and protected interface", N);
9568 end if;
9569 end Check_Interfaces;
9571 ------------------------------------
9572 -- Check_Or_Process_Discriminants --
9573 ------------------------------------
9575 -- If an incomplete or private type declaration was already given for the
9576 -- type, the discriminants may have already been processed if they were
9577 -- present on the incomplete declaration. In this case a full conformance
9578 -- check is performed otherwise just process them.
9580 procedure Check_Or_Process_Discriminants
9581 (N : Node_Id;
9582 T : Entity_Id;
9583 Prev : Entity_Id := Empty)
9585 begin
9586 if Has_Discriminants (T) then
9588 -- Make the discriminants visible to component declarations
9590 declare
9591 D : Entity_Id;
9592 Prev : Entity_Id;
9594 begin
9595 D := First_Discriminant (T);
9596 while Present (D) loop
9597 Prev := Current_Entity (D);
9598 Set_Current_Entity (D);
9599 Set_Is_Immediately_Visible (D);
9600 Set_Homonym (D, Prev);
9602 -- Ada 2005 (AI-230): Access discriminant allowed in
9603 -- non-limited record types.
9605 if Ada_Version < Ada_2005 then
9607 -- This restriction gets applied to the full type here. It
9608 -- has already been applied earlier to the partial view.
9610 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
9611 end if;
9613 Next_Discriminant (D);
9614 end loop;
9615 end;
9617 elsif Present (Discriminant_Specifications (N)) then
9618 Process_Discriminants (N, Prev);
9619 end if;
9620 end Check_Or_Process_Discriminants;
9622 ----------------------
9623 -- Check_Real_Bound --
9624 ----------------------
9626 procedure Check_Real_Bound (Bound : Node_Id) is
9627 begin
9628 if not Is_Real_Type (Etype (Bound)) then
9629 Error_Msg_N
9630 ("bound in real type definition must be of real type", Bound);
9632 elsif not Is_OK_Static_Expression (Bound) then
9633 Flag_Non_Static_Expr
9634 ("non-static expression used for real type bound!", Bound);
9636 else
9637 return;
9638 end if;
9640 Rewrite
9641 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
9642 Analyze (Bound);
9643 Resolve (Bound, Standard_Float);
9644 end Check_Real_Bound;
9646 ------------------------------
9647 -- Complete_Private_Subtype --
9648 ------------------------------
9650 procedure Complete_Private_Subtype
9651 (Priv : Entity_Id;
9652 Full : Entity_Id;
9653 Full_Base : Entity_Id;
9654 Related_Nod : Node_Id)
9656 Save_Next_Entity : Entity_Id;
9657 Save_Homonym : Entity_Id;
9659 begin
9660 -- Set semantic attributes for (implicit) private subtype completion.
9661 -- If the full type has no discriminants, then it is a copy of the full
9662 -- view of the base. Otherwise, it is a subtype of the base with a
9663 -- possible discriminant constraint. Save and restore the original
9664 -- Next_Entity field of full to ensure that the calls to Copy_Node
9665 -- do not corrupt the entity chain.
9667 -- Note that the type of the full view is the same entity as the type of
9668 -- the partial view. In this fashion, the subtype has access to the
9669 -- correct view of the parent.
9671 Save_Next_Entity := Next_Entity (Full);
9672 Save_Homonym := Homonym (Priv);
9674 case Ekind (Full_Base) is
9675 when E_Record_Type |
9676 E_Record_Subtype |
9677 Class_Wide_Kind |
9678 Private_Kind |
9679 Task_Kind |
9680 Protected_Kind =>
9681 Copy_Node (Priv, Full);
9683 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
9684 Set_First_Entity (Full, First_Entity (Full_Base));
9685 Set_Last_Entity (Full, Last_Entity (Full_Base));
9687 when others =>
9688 Copy_Node (Full_Base, Full);
9689 Set_Chars (Full, Chars (Priv));
9690 Conditional_Delay (Full, Priv);
9691 Set_Sloc (Full, Sloc (Priv));
9692 end case;
9694 Set_Next_Entity (Full, Save_Next_Entity);
9695 Set_Homonym (Full, Save_Homonym);
9696 Set_Associated_Node_For_Itype (Full, Related_Nod);
9698 -- Set common attributes for all subtypes
9700 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
9702 -- The Etype of the full view is inconsistent. Gigi needs to see the
9703 -- structural full view, which is what the current scheme gives:
9704 -- the Etype of the full view is the etype of the full base. However,
9705 -- if the full base is a derived type, the full view then looks like
9706 -- a subtype of the parent, not a subtype of the full base. If instead
9707 -- we write:
9709 -- Set_Etype (Full, Full_Base);
9711 -- then we get inconsistencies in the front-end (confusion between
9712 -- views). Several outstanding bugs are related to this ???
9714 Set_Is_First_Subtype (Full, False);
9715 Set_Scope (Full, Scope (Priv));
9716 Set_Size_Info (Full, Full_Base);
9717 Set_RM_Size (Full, RM_Size (Full_Base));
9718 Set_Is_Itype (Full);
9720 -- A subtype of a private-type-without-discriminants, whose full-view
9721 -- has discriminants with default expressions, is not constrained!
9723 if not Has_Discriminants (Priv) then
9724 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
9726 if Has_Discriminants (Full_Base) then
9727 Set_Discriminant_Constraint
9728 (Full, Discriminant_Constraint (Full_Base));
9730 -- The partial view may have been indefinite, the full view
9731 -- might not be.
9733 Set_Has_Unknown_Discriminants
9734 (Full, Has_Unknown_Discriminants (Full_Base));
9735 end if;
9736 end if;
9738 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
9739 Set_Depends_On_Private (Full, Has_Private_Component (Full));
9741 -- Freeze the private subtype entity if its parent is delayed, and not
9742 -- already frozen. We skip this processing if the type is an anonymous
9743 -- subtype of a record component, or is the corresponding record of a
9744 -- protected type, since ???
9746 if not Is_Type (Scope (Full)) then
9747 Set_Has_Delayed_Freeze (Full,
9748 Has_Delayed_Freeze (Full_Base)
9749 and then (not Is_Frozen (Full_Base)));
9750 end if;
9752 Set_Freeze_Node (Full, Empty);
9753 Set_Is_Frozen (Full, False);
9754 Set_Full_View (Priv, Full);
9756 if Has_Discriminants (Full) then
9757 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
9758 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
9760 if Has_Unknown_Discriminants (Full) then
9761 Set_Discriminant_Constraint (Full, No_Elist);
9762 end if;
9763 end if;
9765 if Ekind (Full_Base) = E_Record_Type
9766 and then Has_Discriminants (Full_Base)
9767 and then Has_Discriminants (Priv) -- might not, if errors
9768 and then not Has_Unknown_Discriminants (Priv)
9769 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
9770 then
9771 Create_Constrained_Components
9772 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
9774 -- If the full base is itself derived from private, build a congruent
9775 -- subtype of its underlying type, for use by the back end. For a
9776 -- constrained record component, the declaration cannot be placed on
9777 -- the component list, but it must nevertheless be built an analyzed, to
9778 -- supply enough information for Gigi to compute the size of component.
9780 elsif Ekind (Full_Base) in Private_Kind
9781 and then Is_Derived_Type (Full_Base)
9782 and then Has_Discriminants (Full_Base)
9783 and then (Ekind (Current_Scope) /= E_Record_Subtype)
9784 then
9785 if not Is_Itype (Priv)
9786 and then
9787 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
9788 then
9789 Build_Underlying_Full_View
9790 (Parent (Priv), Full, Etype (Full_Base));
9792 elsif Nkind (Related_Nod) = N_Component_Declaration then
9793 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
9794 end if;
9796 elsif Is_Record_Type (Full_Base) then
9798 -- Show Full is simply a renaming of Full_Base
9800 Set_Cloned_Subtype (Full, Full_Base);
9801 end if;
9803 -- It is unsafe to share to bounds of a scalar type, because the Itype
9804 -- is elaborated on demand, and if a bound is non-static then different
9805 -- orders of elaboration in different units will lead to different
9806 -- external symbols.
9808 if Is_Scalar_Type (Full_Base) then
9809 Set_Scalar_Range (Full,
9810 Make_Range (Sloc (Related_Nod),
9811 Low_Bound =>
9812 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
9813 High_Bound =>
9814 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
9816 -- This completion inherits the bounds of the full parent, but if
9817 -- the parent is an unconstrained floating point type, so is the
9818 -- completion.
9820 if Is_Floating_Point_Type (Full_Base) then
9821 Set_Includes_Infinities
9822 (Scalar_Range (Full), Has_Infinities (Full_Base));
9823 end if;
9824 end if;
9826 -- ??? It seems that a lot of fields are missing that should be copied
9827 -- from Full_Base to Full. Here are some that are introduced in a
9828 -- non-disruptive way but a cleanup is necessary.
9830 if Is_Tagged_Type (Full_Base) then
9831 Set_Is_Tagged_Type (Full);
9832 Set_Direct_Primitive_Operations (Full,
9833 Direct_Primitive_Operations (Full_Base));
9835 -- Inherit class_wide type of full_base in case the partial view was
9836 -- not tagged. Otherwise it has already been created when the private
9837 -- subtype was analyzed.
9839 if No (Class_Wide_Type (Full)) then
9840 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
9841 end if;
9843 -- If this is a subtype of a protected or task type, constrain its
9844 -- corresponding record, unless this is a subtype without constraints,
9845 -- i.e. a simple renaming as with an actual subtype in an instance.
9847 elsif Is_Concurrent_Type (Full_Base) then
9848 if Has_Discriminants (Full)
9849 and then Present (Corresponding_Record_Type (Full_Base))
9850 and then
9851 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
9852 then
9853 Set_Corresponding_Record_Type (Full,
9854 Constrain_Corresponding_Record
9855 (Full, Corresponding_Record_Type (Full_Base),
9856 Related_Nod, Full_Base));
9858 else
9859 Set_Corresponding_Record_Type (Full,
9860 Corresponding_Record_Type (Full_Base));
9861 end if;
9862 end if;
9863 end Complete_Private_Subtype;
9865 ----------------------------
9866 -- Constant_Redeclaration --
9867 ----------------------------
9869 procedure Constant_Redeclaration
9870 (Id : Entity_Id;
9871 N : Node_Id;
9872 T : out Entity_Id)
9874 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
9875 Obj_Def : constant Node_Id := Object_Definition (N);
9876 New_T : Entity_Id;
9878 procedure Check_Possible_Deferred_Completion
9879 (Prev_Id : Entity_Id;
9880 Prev_Obj_Def : Node_Id;
9881 Curr_Obj_Def : Node_Id);
9882 -- Determine whether the two object definitions describe the partial
9883 -- and the full view of a constrained deferred constant. Generate
9884 -- a subtype for the full view and verify that it statically matches
9885 -- the subtype of the partial view.
9887 procedure Check_Recursive_Declaration (Typ : Entity_Id);
9888 -- If deferred constant is an access type initialized with an allocator,
9889 -- check whether there is an illegal recursion in the definition,
9890 -- through a default value of some record subcomponent. This is normally
9891 -- detected when generating init procs, but requires this additional
9892 -- mechanism when expansion is disabled.
9894 ----------------------------------------
9895 -- Check_Possible_Deferred_Completion --
9896 ----------------------------------------
9898 procedure Check_Possible_Deferred_Completion
9899 (Prev_Id : Entity_Id;
9900 Prev_Obj_Def : Node_Id;
9901 Curr_Obj_Def : Node_Id)
9903 begin
9904 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
9905 and then Present (Constraint (Prev_Obj_Def))
9906 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
9907 and then Present (Constraint (Curr_Obj_Def))
9908 then
9909 declare
9910 Loc : constant Source_Ptr := Sloc (N);
9911 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
9912 Decl : constant Node_Id :=
9913 Make_Subtype_Declaration (Loc,
9914 Defining_Identifier => Def_Id,
9915 Subtype_Indication =>
9916 Relocate_Node (Curr_Obj_Def));
9918 begin
9919 Insert_Before_And_Analyze (N, Decl);
9920 Set_Etype (Id, Def_Id);
9922 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
9923 Error_Msg_Sloc := Sloc (Prev_Id);
9924 Error_Msg_N ("subtype does not statically match deferred " &
9925 "declaration#", N);
9926 end if;
9927 end;
9928 end if;
9929 end Check_Possible_Deferred_Completion;
9931 ---------------------------------
9932 -- Check_Recursive_Declaration --
9933 ---------------------------------
9935 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
9936 Comp : Entity_Id;
9938 begin
9939 if Is_Record_Type (Typ) then
9940 Comp := First_Component (Typ);
9941 while Present (Comp) loop
9942 if Comes_From_Source (Comp) then
9943 if Present (Expression (Parent (Comp)))
9944 and then Is_Entity_Name (Expression (Parent (Comp)))
9945 and then Entity (Expression (Parent (Comp))) = Prev
9946 then
9947 Error_Msg_Sloc := Sloc (Parent (Comp));
9948 Error_Msg_NE
9949 ("illegal circularity with declaration for&#",
9950 N, Comp);
9951 return;
9953 elsif Is_Record_Type (Etype (Comp)) then
9954 Check_Recursive_Declaration (Etype (Comp));
9955 end if;
9956 end if;
9958 Next_Component (Comp);
9959 end loop;
9960 end if;
9961 end Check_Recursive_Declaration;
9963 -- Start of processing for Constant_Redeclaration
9965 begin
9966 if Nkind (Parent (Prev)) = N_Object_Declaration then
9967 if Nkind (Object_Definition
9968 (Parent (Prev))) = N_Subtype_Indication
9969 then
9970 -- Find type of new declaration. The constraints of the two
9971 -- views must match statically, but there is no point in
9972 -- creating an itype for the full view.
9974 if Nkind (Obj_Def) = N_Subtype_Indication then
9975 Find_Type (Subtype_Mark (Obj_Def));
9976 New_T := Entity (Subtype_Mark (Obj_Def));
9978 else
9979 Find_Type (Obj_Def);
9980 New_T := Entity (Obj_Def);
9981 end if;
9983 T := Etype (Prev);
9985 else
9986 -- The full view may impose a constraint, even if the partial
9987 -- view does not, so construct the subtype.
9989 New_T := Find_Type_Of_Object (Obj_Def, N);
9990 T := New_T;
9991 end if;
9993 else
9994 -- Current declaration is illegal, diagnosed below in Enter_Name
9996 T := Empty;
9997 New_T := Any_Type;
9998 end if;
10000 -- If previous full declaration or a renaming declaration exists, or if
10001 -- a homograph is present, let Enter_Name handle it, either with an
10002 -- error or with the removal of an overridden implicit subprogram.
10004 if Ekind (Prev) /= E_Constant
10005 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
10006 or else Present (Expression (Parent (Prev)))
10007 or else Present (Full_View (Prev))
10008 then
10009 Enter_Name (Id);
10011 -- Verify that types of both declarations match, or else that both types
10012 -- are anonymous access types whose designated subtypes statically match
10013 -- (as allowed in Ada 2005 by AI-385).
10015 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
10016 and then
10017 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
10018 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
10019 or else Is_Access_Constant (Etype (New_T)) /=
10020 Is_Access_Constant (Etype (Prev))
10021 or else Can_Never_Be_Null (Etype (New_T)) /=
10022 Can_Never_Be_Null (Etype (Prev))
10023 or else Null_Exclusion_Present (Parent (Prev)) /=
10024 Null_Exclusion_Present (Parent (Id))
10025 or else not Subtypes_Statically_Match
10026 (Designated_Type (Etype (Prev)),
10027 Designated_Type (Etype (New_T))))
10028 then
10029 Error_Msg_Sloc := Sloc (Prev);
10030 Error_Msg_N ("type does not match declaration#", N);
10031 Set_Full_View (Prev, Id);
10032 Set_Etype (Id, Any_Type);
10034 elsif
10035 Null_Exclusion_Present (Parent (Prev))
10036 and then not Null_Exclusion_Present (N)
10037 then
10038 Error_Msg_Sloc := Sloc (Prev);
10039 Error_Msg_N ("null-exclusion does not match declaration#", N);
10040 Set_Full_View (Prev, Id);
10041 Set_Etype (Id, Any_Type);
10043 -- If so, process the full constant declaration
10045 else
10046 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
10047 -- the deferred declaration is constrained, then the subtype defined
10048 -- by the subtype_indication in the full declaration shall match it
10049 -- statically.
10051 Check_Possible_Deferred_Completion
10052 (Prev_Id => Prev,
10053 Prev_Obj_Def => Object_Definition (Parent (Prev)),
10054 Curr_Obj_Def => Obj_Def);
10056 Set_Full_View (Prev, Id);
10057 Set_Is_Public (Id, Is_Public (Prev));
10058 Set_Is_Internal (Id);
10059 Append_Entity (Id, Current_Scope);
10061 -- Check ALIASED present if present before (RM 7.4(7))
10063 if Is_Aliased (Prev)
10064 and then not Aliased_Present (N)
10065 then
10066 Error_Msg_Sloc := Sloc (Prev);
10067 Error_Msg_N ("ALIASED required (see declaration#)", N);
10068 end if;
10070 -- Check that placement is in private part and that the incomplete
10071 -- declaration appeared in the visible part.
10073 if Ekind (Current_Scope) = E_Package
10074 and then not In_Private_Part (Current_Scope)
10075 then
10076 Error_Msg_Sloc := Sloc (Prev);
10077 Error_Msg_N
10078 ("full constant for declaration#"
10079 & " must be in private part", N);
10081 elsif Ekind (Current_Scope) = E_Package
10082 and then
10083 List_Containing (Parent (Prev)) /=
10084 Visible_Declarations
10085 (Specification (Unit_Declaration_Node (Current_Scope)))
10086 then
10087 Error_Msg_N
10088 ("deferred constant must be declared in visible part",
10089 Parent (Prev));
10090 end if;
10092 if Is_Access_Type (T)
10093 and then Nkind (Expression (N)) = N_Allocator
10094 then
10095 Check_Recursive_Declaration (Designated_Type (T));
10096 end if;
10097 end if;
10098 end Constant_Redeclaration;
10100 ----------------------
10101 -- Constrain_Access --
10102 ----------------------
10104 procedure Constrain_Access
10105 (Def_Id : in out Entity_Id;
10106 S : Node_Id;
10107 Related_Nod : Node_Id)
10109 T : constant Entity_Id := Entity (Subtype_Mark (S));
10110 Desig_Type : constant Entity_Id := Designated_Type (T);
10111 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
10112 Constraint_OK : Boolean := True;
10114 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
10115 -- Simple predicate to test for defaulted discriminants
10116 -- Shouldn't this be in sem_util???
10118 ---------------------------------
10119 -- Has_Defaulted_Discriminants --
10120 ---------------------------------
10122 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
10123 begin
10124 return Has_Discriminants (Typ)
10125 and then Present (First_Discriminant (Typ))
10126 and then Present
10127 (Discriminant_Default_Value (First_Discriminant (Typ)));
10128 end Has_Defaulted_Discriminants;
10130 -- Start of processing for Constrain_Access
10132 begin
10133 if Is_Array_Type (Desig_Type) then
10134 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
10136 elsif (Is_Record_Type (Desig_Type)
10137 or else Is_Incomplete_Or_Private_Type (Desig_Type))
10138 and then not Is_Constrained (Desig_Type)
10139 then
10140 -- ??? The following code is a temporary kludge to ignore a
10141 -- discriminant constraint on access type if it is constraining
10142 -- the current record. Avoid creating the implicit subtype of the
10143 -- record we are currently compiling since right now, we cannot
10144 -- handle these. For now, just return the access type itself.
10146 if Desig_Type = Current_Scope
10147 and then No (Def_Id)
10148 then
10149 Set_Ekind (Desig_Subtype, E_Record_Subtype);
10150 Def_Id := Entity (Subtype_Mark (S));
10152 -- This call added to ensure that the constraint is analyzed
10153 -- (needed for a B test). Note that we still return early from
10154 -- this procedure to avoid recursive processing. ???
10156 Constrain_Discriminated_Type
10157 (Desig_Subtype, S, Related_Nod, For_Access => True);
10158 return;
10159 end if;
10161 if (Ekind (T) = E_General_Access_Type
10162 or else Ada_Version >= Ada_2005)
10163 and then Has_Private_Declaration (Desig_Type)
10164 and then In_Open_Scopes (Scope (Desig_Type))
10165 and then Has_Discriminants (Desig_Type)
10166 then
10167 -- Enforce rule that the constraint is illegal if there is
10168 -- an unconstrained view of the designated type. This means
10169 -- that the partial view (either a private type declaration or
10170 -- a derivation from a private type) has no discriminants.
10171 -- (Defect Report 8652/0008, Technical Corrigendum 1, checked
10172 -- by ACATS B371001).
10174 -- Rule updated for Ada 2005: the private type is said to have
10175 -- a constrained partial view, given that objects of the type
10176 -- can be declared. Furthermore, the rule applies to all access
10177 -- types, unlike the rule concerning default discriminants.
10179 declare
10180 Pack : constant Node_Id :=
10181 Unit_Declaration_Node (Scope (Desig_Type));
10182 Decls : List_Id;
10183 Decl : Node_Id;
10185 begin
10186 if Nkind (Pack) = N_Package_Declaration then
10187 Decls := Visible_Declarations (Specification (Pack));
10188 Decl := First (Decls);
10189 while Present (Decl) loop
10190 if (Nkind (Decl) = N_Private_Type_Declaration
10191 and then
10192 Chars (Defining_Identifier (Decl)) =
10193 Chars (Desig_Type))
10195 or else
10196 (Nkind (Decl) = N_Full_Type_Declaration
10197 and then
10198 Chars (Defining_Identifier (Decl)) =
10199 Chars (Desig_Type)
10200 and then Is_Derived_Type (Desig_Type)
10201 and then
10202 Has_Private_Declaration (Etype (Desig_Type)))
10203 then
10204 if No (Discriminant_Specifications (Decl)) then
10205 Error_Msg_N
10206 ("cannot constrain general access type if " &
10207 "designated type has constrained partial view",
10209 end if;
10211 exit;
10212 end if;
10214 Next (Decl);
10215 end loop;
10216 end if;
10217 end;
10218 end if;
10220 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
10221 For_Access => True);
10223 elsif (Is_Task_Type (Desig_Type)
10224 or else Is_Protected_Type (Desig_Type))
10225 and then not Is_Constrained (Desig_Type)
10226 then
10227 Constrain_Concurrent
10228 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
10230 else
10231 Error_Msg_N ("invalid constraint on access type", S);
10232 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
10233 Constraint_OK := False;
10234 end if;
10236 if No (Def_Id) then
10237 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
10238 else
10239 Set_Ekind (Def_Id, E_Access_Subtype);
10240 end if;
10242 if Constraint_OK then
10243 Set_Etype (Def_Id, Base_Type (T));
10245 if Is_Private_Type (Desig_Type) then
10246 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
10247 end if;
10248 else
10249 Set_Etype (Def_Id, Any_Type);
10250 end if;
10252 Set_Size_Info (Def_Id, T);
10253 Set_Is_Constrained (Def_Id, Constraint_OK);
10254 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
10255 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10256 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
10258 Conditional_Delay (Def_Id, T);
10260 -- AI-363 : Subtypes of general access types whose designated types have
10261 -- default discriminants are disallowed. In instances, the rule has to
10262 -- be checked against the actual, of which T is the subtype. In a
10263 -- generic body, the rule is checked assuming that the actual type has
10264 -- defaulted discriminants.
10266 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
10267 if Ekind (Base_Type (T)) = E_General_Access_Type
10268 and then Has_Defaulted_Discriminants (Desig_Type)
10269 then
10270 if Ada_Version < Ada_2005 then
10271 Error_Msg_N
10272 ("access subtype of general access type would not " &
10273 "be allowed in Ada 2005?", S);
10274 else
10275 Error_Msg_N
10276 ("access subype of general access type not allowed", S);
10277 end if;
10279 Error_Msg_N ("\discriminants have defaults", S);
10281 elsif Is_Access_Type (T)
10282 and then Is_Generic_Type (Desig_Type)
10283 and then Has_Discriminants (Desig_Type)
10284 and then In_Package_Body (Current_Scope)
10285 then
10286 if Ada_Version < Ada_2005 then
10287 Error_Msg_N
10288 ("access subtype would not be allowed in generic body " &
10289 "in Ada 2005?", S);
10290 else
10291 Error_Msg_N
10292 ("access subtype not allowed in generic body", S);
10293 end if;
10295 Error_Msg_N
10296 ("\designated type is a discriminated formal", S);
10297 end if;
10298 end if;
10299 end Constrain_Access;
10301 ---------------------
10302 -- Constrain_Array --
10303 ---------------------
10305 procedure Constrain_Array
10306 (Def_Id : in out Entity_Id;
10307 SI : Node_Id;
10308 Related_Nod : Node_Id;
10309 Related_Id : Entity_Id;
10310 Suffix : Character)
10312 C : constant Node_Id := Constraint (SI);
10313 Number_Of_Constraints : Nat := 0;
10314 Index : Node_Id;
10315 S, T : Entity_Id;
10316 Constraint_OK : Boolean := True;
10318 begin
10319 T := Entity (Subtype_Mark (SI));
10321 if Ekind (T) in Access_Kind then
10322 T := Designated_Type (T);
10323 end if;
10325 -- If an index constraint follows a subtype mark in a subtype indication
10326 -- then the type or subtype denoted by the subtype mark must not already
10327 -- impose an index constraint. The subtype mark must denote either an
10328 -- unconstrained array type or an access type whose designated type
10329 -- is such an array type... (RM 3.6.1)
10331 if Is_Constrained (T) then
10332 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
10333 Constraint_OK := False;
10335 else
10336 S := First (Constraints (C));
10337 while Present (S) loop
10338 Number_Of_Constraints := Number_Of_Constraints + 1;
10339 Next (S);
10340 end loop;
10342 -- In either case, the index constraint must provide a discrete
10343 -- range for each index of the array type and the type of each
10344 -- discrete range must be the same as that of the corresponding
10345 -- index. (RM 3.6.1)
10347 if Number_Of_Constraints /= Number_Dimensions (T) then
10348 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
10349 Constraint_OK := False;
10351 else
10352 S := First (Constraints (C));
10353 Index := First_Index (T);
10354 Analyze (Index);
10356 -- Apply constraints to each index type
10358 for J in 1 .. Number_Of_Constraints loop
10359 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
10360 Next (Index);
10361 Next (S);
10362 end loop;
10364 end if;
10365 end if;
10367 if No (Def_Id) then
10368 Def_Id :=
10369 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
10370 Set_Parent (Def_Id, Related_Nod);
10372 else
10373 Set_Ekind (Def_Id, E_Array_Subtype);
10374 end if;
10376 Set_Size_Info (Def_Id, (T));
10377 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10378 Set_Etype (Def_Id, Base_Type (T));
10380 if Constraint_OK then
10381 Set_First_Index (Def_Id, First (Constraints (C)));
10382 else
10383 Set_First_Index (Def_Id, First_Index (T));
10384 end if;
10386 Set_Is_Constrained (Def_Id, True);
10387 Set_Is_Aliased (Def_Id, Is_Aliased (T));
10388 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10390 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
10391 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
10393 -- A subtype does not inherit the packed_array_type of is parent. We
10394 -- need to initialize the attribute because if Def_Id is previously
10395 -- analyzed through a limited_with clause, it will have the attributes
10396 -- of an incomplete type, one of which is an Elist that overlaps the
10397 -- Packed_Array_Type field.
10399 Set_Packed_Array_Type (Def_Id, Empty);
10401 -- Build a freeze node if parent still needs one. Also make sure that
10402 -- the Depends_On_Private status is set because the subtype will need
10403 -- reprocessing at the time the base type does, and also we must set a
10404 -- conditional delay.
10406 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10407 Conditional_Delay (Def_Id, T);
10408 end Constrain_Array;
10410 ------------------------------
10411 -- Constrain_Component_Type --
10412 ------------------------------
10414 function Constrain_Component_Type
10415 (Comp : Entity_Id;
10416 Constrained_Typ : Entity_Id;
10417 Related_Node : Node_Id;
10418 Typ : Entity_Id;
10419 Constraints : Elist_Id) return Entity_Id
10421 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
10422 Compon_Type : constant Entity_Id := Etype (Comp);
10424 function Build_Constrained_Array_Type
10425 (Old_Type : Entity_Id) return Entity_Id;
10426 -- If Old_Type is an array type, one of whose indices is constrained
10427 -- by a discriminant, build an Itype whose constraint replaces the
10428 -- discriminant with its value in the constraint.
10430 function Build_Constrained_Discriminated_Type
10431 (Old_Type : Entity_Id) return Entity_Id;
10432 -- Ditto for record components
10434 function Build_Constrained_Access_Type
10435 (Old_Type : Entity_Id) return Entity_Id;
10436 -- Ditto for access types. Makes use of previous two functions, to
10437 -- constrain designated type.
10439 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
10440 -- T is an array or discriminated type, C is a list of constraints
10441 -- that apply to T. This routine builds the constrained subtype.
10443 function Is_Discriminant (Expr : Node_Id) return Boolean;
10444 -- Returns True if Expr is a discriminant
10446 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
10447 -- Find the value of discriminant Discrim in Constraint
10449 -----------------------------------
10450 -- Build_Constrained_Access_Type --
10451 -----------------------------------
10453 function Build_Constrained_Access_Type
10454 (Old_Type : Entity_Id) return Entity_Id
10456 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
10457 Itype : Entity_Id;
10458 Desig_Subtype : Entity_Id;
10459 Scop : Entity_Id;
10461 begin
10462 -- if the original access type was not embedded in the enclosing
10463 -- type definition, there is no need to produce a new access
10464 -- subtype. In fact every access type with an explicit constraint
10465 -- generates an itype whose scope is the enclosing record.
10467 if not Is_Type (Scope (Old_Type)) then
10468 return Old_Type;
10470 elsif Is_Array_Type (Desig_Type) then
10471 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
10473 elsif Has_Discriminants (Desig_Type) then
10475 -- This may be an access type to an enclosing record type for
10476 -- which we are constructing the constrained components. Return
10477 -- the enclosing record subtype. This is not always correct,
10478 -- but avoids infinite recursion. ???
10480 Desig_Subtype := Any_Type;
10482 for J in reverse 0 .. Scope_Stack.Last loop
10483 Scop := Scope_Stack.Table (J).Entity;
10485 if Is_Type (Scop)
10486 and then Base_Type (Scop) = Base_Type (Desig_Type)
10487 then
10488 Desig_Subtype := Scop;
10489 end if;
10491 exit when not Is_Type (Scop);
10492 end loop;
10494 if Desig_Subtype = Any_Type then
10495 Desig_Subtype :=
10496 Build_Constrained_Discriminated_Type (Desig_Type);
10497 end if;
10499 else
10500 return Old_Type;
10501 end if;
10503 if Desig_Subtype /= Desig_Type then
10505 -- The Related_Node better be here or else we won't be able
10506 -- to attach new itypes to a node in the tree.
10508 pragma Assert (Present (Related_Node));
10510 Itype := Create_Itype (E_Access_Subtype, Related_Node);
10512 Set_Etype (Itype, Base_Type (Old_Type));
10513 Set_Size_Info (Itype, (Old_Type));
10514 Set_Directly_Designated_Type (Itype, Desig_Subtype);
10515 Set_Depends_On_Private (Itype, Has_Private_Component
10516 (Old_Type));
10517 Set_Is_Access_Constant (Itype, Is_Access_Constant
10518 (Old_Type));
10520 -- The new itype needs freezing when it depends on a not frozen
10521 -- type and the enclosing subtype needs freezing.
10523 if Has_Delayed_Freeze (Constrained_Typ)
10524 and then not Is_Frozen (Constrained_Typ)
10525 then
10526 Conditional_Delay (Itype, Base_Type (Old_Type));
10527 end if;
10529 return Itype;
10531 else
10532 return Old_Type;
10533 end if;
10534 end Build_Constrained_Access_Type;
10536 ----------------------------------
10537 -- Build_Constrained_Array_Type --
10538 ----------------------------------
10540 function Build_Constrained_Array_Type
10541 (Old_Type : Entity_Id) return Entity_Id
10543 Lo_Expr : Node_Id;
10544 Hi_Expr : Node_Id;
10545 Old_Index : Node_Id;
10546 Range_Node : Node_Id;
10547 Constr_List : List_Id;
10549 Need_To_Create_Itype : Boolean := False;
10551 begin
10552 Old_Index := First_Index (Old_Type);
10553 while Present (Old_Index) loop
10554 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10556 if Is_Discriminant (Lo_Expr)
10557 or else Is_Discriminant (Hi_Expr)
10558 then
10559 Need_To_Create_Itype := True;
10560 end if;
10562 Next_Index (Old_Index);
10563 end loop;
10565 if Need_To_Create_Itype then
10566 Constr_List := New_List;
10568 Old_Index := First_Index (Old_Type);
10569 while Present (Old_Index) loop
10570 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10572 if Is_Discriminant (Lo_Expr) then
10573 Lo_Expr := Get_Discr_Value (Lo_Expr);
10574 end if;
10576 if Is_Discriminant (Hi_Expr) then
10577 Hi_Expr := Get_Discr_Value (Hi_Expr);
10578 end if;
10580 Range_Node :=
10581 Make_Range
10582 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
10584 Append (Range_Node, To => Constr_List);
10586 Next_Index (Old_Index);
10587 end loop;
10589 return Build_Subtype (Old_Type, Constr_List);
10591 else
10592 return Old_Type;
10593 end if;
10594 end Build_Constrained_Array_Type;
10596 ------------------------------------------
10597 -- Build_Constrained_Discriminated_Type --
10598 ------------------------------------------
10600 function Build_Constrained_Discriminated_Type
10601 (Old_Type : Entity_Id) return Entity_Id
10603 Expr : Node_Id;
10604 Constr_List : List_Id;
10605 Old_Constraint : Elmt_Id;
10607 Need_To_Create_Itype : Boolean := False;
10609 begin
10610 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10611 while Present (Old_Constraint) loop
10612 Expr := Node (Old_Constraint);
10614 if Is_Discriminant (Expr) then
10615 Need_To_Create_Itype := True;
10616 end if;
10618 Next_Elmt (Old_Constraint);
10619 end loop;
10621 if Need_To_Create_Itype then
10622 Constr_List := New_List;
10624 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10625 while Present (Old_Constraint) loop
10626 Expr := Node (Old_Constraint);
10628 if Is_Discriminant (Expr) then
10629 Expr := Get_Discr_Value (Expr);
10630 end if;
10632 Append (New_Copy_Tree (Expr), To => Constr_List);
10634 Next_Elmt (Old_Constraint);
10635 end loop;
10637 return Build_Subtype (Old_Type, Constr_List);
10639 else
10640 return Old_Type;
10641 end if;
10642 end Build_Constrained_Discriminated_Type;
10644 -------------------
10645 -- Build_Subtype --
10646 -------------------
10648 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
10649 Indic : Node_Id;
10650 Subtyp_Decl : Node_Id;
10651 Def_Id : Entity_Id;
10652 Btyp : Entity_Id := Base_Type (T);
10654 begin
10655 -- The Related_Node better be here or else we won't be able to
10656 -- attach new itypes to a node in the tree.
10658 pragma Assert (Present (Related_Node));
10660 -- If the view of the component's type is incomplete or private
10661 -- with unknown discriminants, then the constraint must be applied
10662 -- to the full type.
10664 if Has_Unknown_Discriminants (Btyp)
10665 and then Present (Underlying_Type (Btyp))
10666 then
10667 Btyp := Underlying_Type (Btyp);
10668 end if;
10670 Indic :=
10671 Make_Subtype_Indication (Loc,
10672 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
10673 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
10675 Def_Id := Create_Itype (Ekind (T), Related_Node);
10677 Subtyp_Decl :=
10678 Make_Subtype_Declaration (Loc,
10679 Defining_Identifier => Def_Id,
10680 Subtype_Indication => Indic);
10682 Set_Parent (Subtyp_Decl, Parent (Related_Node));
10684 -- Itypes must be analyzed with checks off (see package Itypes)
10686 Analyze (Subtyp_Decl, Suppress => All_Checks);
10688 return Def_Id;
10689 end Build_Subtype;
10691 ---------------------
10692 -- Get_Discr_Value --
10693 ---------------------
10695 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
10696 D : Entity_Id;
10697 E : Elmt_Id;
10699 begin
10700 -- The discriminant may be declared for the type, in which case we
10701 -- find it by iterating over the list of discriminants. If the
10702 -- discriminant is inherited from a parent type, it appears as the
10703 -- corresponding discriminant of the current type. This will be the
10704 -- case when constraining an inherited component whose constraint is
10705 -- given by a discriminant of the parent.
10707 D := First_Discriminant (Typ);
10708 E := First_Elmt (Constraints);
10710 while Present (D) loop
10711 if D = Entity (Discrim)
10712 or else D = CR_Discriminant (Entity (Discrim))
10713 or else Corresponding_Discriminant (D) = Entity (Discrim)
10714 then
10715 return Node (E);
10716 end if;
10718 Next_Discriminant (D);
10719 Next_Elmt (E);
10720 end loop;
10722 -- The corresponding_Discriminant mechanism is incomplete, because
10723 -- the correspondence between new and old discriminants is not one
10724 -- to one: one new discriminant can constrain several old ones. In
10725 -- that case, scan sequentially the stored_constraint, the list of
10726 -- discriminants of the parents, and the constraints.
10727 -- Previous code checked for the present of the Stored_Constraint
10728 -- list for the derived type, but did not use it at all. Should it
10729 -- be present when the component is a discriminated task type?
10731 if Is_Derived_Type (Typ)
10732 and then Scope (Entity (Discrim)) = Etype (Typ)
10733 then
10734 D := First_Discriminant (Etype (Typ));
10735 E := First_Elmt (Constraints);
10736 while Present (D) loop
10737 if D = Entity (Discrim) then
10738 return Node (E);
10739 end if;
10741 Next_Discriminant (D);
10742 Next_Elmt (E);
10743 end loop;
10744 end if;
10746 -- Something is wrong if we did not find the value
10748 raise Program_Error;
10749 end Get_Discr_Value;
10751 ---------------------
10752 -- Is_Discriminant --
10753 ---------------------
10755 function Is_Discriminant (Expr : Node_Id) return Boolean is
10756 Discrim_Scope : Entity_Id;
10758 begin
10759 if Denotes_Discriminant (Expr) then
10760 Discrim_Scope := Scope (Entity (Expr));
10762 -- Either we have a reference to one of Typ's discriminants,
10764 pragma Assert (Discrim_Scope = Typ
10766 -- or to the discriminants of the parent type, in the case
10767 -- of a derivation of a tagged type with variants.
10769 or else Discrim_Scope = Etype (Typ)
10770 or else Full_View (Discrim_Scope) = Etype (Typ)
10772 -- or same as above for the case where the discriminants
10773 -- were declared in Typ's private view.
10775 or else (Is_Private_Type (Discrim_Scope)
10776 and then Chars (Discrim_Scope) = Chars (Typ))
10778 -- or else we are deriving from the full view and the
10779 -- discriminant is declared in the private entity.
10781 or else (Is_Private_Type (Typ)
10782 and then Chars (Discrim_Scope) = Chars (Typ))
10784 -- Or we are constrained the corresponding record of a
10785 -- synchronized type that completes a private declaration.
10787 or else (Is_Concurrent_Record_Type (Typ)
10788 and then
10789 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
10791 -- or we have a class-wide type, in which case make sure the
10792 -- discriminant found belongs to the root type.
10794 or else (Is_Class_Wide_Type (Typ)
10795 and then Etype (Typ) = Discrim_Scope));
10797 return True;
10798 end if;
10800 -- In all other cases we have something wrong
10802 return False;
10803 end Is_Discriminant;
10805 -- Start of processing for Constrain_Component_Type
10807 begin
10808 if Nkind (Parent (Comp)) = N_Component_Declaration
10809 and then Comes_From_Source (Parent (Comp))
10810 and then Comes_From_Source
10811 (Subtype_Indication (Component_Definition (Parent (Comp))))
10812 and then
10813 Is_Entity_Name
10814 (Subtype_Indication (Component_Definition (Parent (Comp))))
10815 then
10816 return Compon_Type;
10818 elsif Is_Array_Type (Compon_Type) then
10819 return Build_Constrained_Array_Type (Compon_Type);
10821 elsif Has_Discriminants (Compon_Type) then
10822 return Build_Constrained_Discriminated_Type (Compon_Type);
10824 elsif Is_Access_Type (Compon_Type) then
10825 return Build_Constrained_Access_Type (Compon_Type);
10827 else
10828 return Compon_Type;
10829 end if;
10830 end Constrain_Component_Type;
10832 --------------------------
10833 -- Constrain_Concurrent --
10834 --------------------------
10836 -- For concurrent types, the associated record value type carries the same
10837 -- discriminants, so when we constrain a concurrent type, we must constrain
10838 -- the corresponding record type as well.
10840 procedure Constrain_Concurrent
10841 (Def_Id : in out Entity_Id;
10842 SI : Node_Id;
10843 Related_Nod : Node_Id;
10844 Related_Id : Entity_Id;
10845 Suffix : Character)
10847 T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
10848 T_Val : Entity_Id;
10850 begin
10851 if Ekind (T_Ent) in Access_Kind then
10852 T_Ent := Designated_Type (T_Ent);
10853 end if;
10855 T_Val := Corresponding_Record_Type (T_Ent);
10857 if Present (T_Val) then
10859 if No (Def_Id) then
10860 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10861 end if;
10863 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10865 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10866 Set_Corresponding_Record_Type (Def_Id,
10867 Constrain_Corresponding_Record
10868 (Def_Id, T_Val, Related_Nod, Related_Id));
10870 else
10871 -- If there is no associated record, expansion is disabled and this
10872 -- is a generic context. Create a subtype in any case, so that
10873 -- semantic analysis can proceed.
10875 if No (Def_Id) then
10876 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10877 end if;
10879 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10880 end if;
10881 end Constrain_Concurrent;
10883 ------------------------------------
10884 -- Constrain_Corresponding_Record --
10885 ------------------------------------
10887 function Constrain_Corresponding_Record
10888 (Prot_Subt : Entity_Id;
10889 Corr_Rec : Entity_Id;
10890 Related_Nod : Node_Id;
10891 Related_Id : Entity_Id) return Entity_Id
10893 T_Sub : constant Entity_Id :=
10894 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
10896 begin
10897 Set_Etype (T_Sub, Corr_Rec);
10898 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
10899 Set_Is_Constrained (T_Sub, True);
10900 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
10901 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
10903 -- As elsewhere, we do not want to create a freeze node for this itype
10904 -- if it is created for a constrained component of an enclosing record
10905 -- because references to outer discriminants will appear out of scope.
10907 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
10908 Conditional_Delay (T_Sub, Corr_Rec);
10909 else
10910 Set_Is_Frozen (T_Sub);
10911 end if;
10913 if Has_Discriminants (Prot_Subt) then -- False only if errors.
10914 Set_Discriminant_Constraint
10915 (T_Sub, Discriminant_Constraint (Prot_Subt));
10916 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
10917 Create_Constrained_Components
10918 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
10919 end if;
10921 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
10923 return T_Sub;
10924 end Constrain_Corresponding_Record;
10926 -----------------------
10927 -- Constrain_Decimal --
10928 -----------------------
10930 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
10931 T : constant Entity_Id := Entity (Subtype_Mark (S));
10932 C : constant Node_Id := Constraint (S);
10933 Loc : constant Source_Ptr := Sloc (C);
10934 Range_Expr : Node_Id;
10935 Digits_Expr : Node_Id;
10936 Digits_Val : Uint;
10937 Bound_Val : Ureal;
10939 begin
10940 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
10942 if Nkind (C) = N_Range_Constraint then
10943 Range_Expr := Range_Expression (C);
10944 Digits_Val := Digits_Value (T);
10946 else
10947 pragma Assert (Nkind (C) = N_Digits_Constraint);
10948 Digits_Expr := Digits_Expression (C);
10949 Analyze_And_Resolve (Digits_Expr, Any_Integer);
10951 Check_Digits_Expression (Digits_Expr);
10952 Digits_Val := Expr_Value (Digits_Expr);
10954 if Digits_Val > Digits_Value (T) then
10955 Error_Msg_N
10956 ("digits expression is incompatible with subtype", C);
10957 Digits_Val := Digits_Value (T);
10958 end if;
10960 if Present (Range_Constraint (C)) then
10961 Range_Expr := Range_Expression (Range_Constraint (C));
10962 else
10963 Range_Expr := Empty;
10964 end if;
10965 end if;
10967 Set_Etype (Def_Id, Base_Type (T));
10968 Set_Size_Info (Def_Id, (T));
10969 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10970 Set_Delta_Value (Def_Id, Delta_Value (T));
10971 Set_Scale_Value (Def_Id, Scale_Value (T));
10972 Set_Small_Value (Def_Id, Small_Value (T));
10973 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
10974 Set_Digits_Value (Def_Id, Digits_Val);
10976 -- Manufacture range from given digits value if no range present
10978 if No (Range_Expr) then
10979 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
10980 Range_Expr :=
10981 Make_Range (Loc,
10982 Low_Bound =>
10983 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
10984 High_Bound =>
10985 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
10986 end if;
10988 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
10989 Set_Discrete_RM_Size (Def_Id);
10991 -- Unconditionally delay the freeze, since we cannot set size
10992 -- information in all cases correctly until the freeze point.
10994 Set_Has_Delayed_Freeze (Def_Id);
10995 end Constrain_Decimal;
10997 ----------------------------------
10998 -- Constrain_Discriminated_Type --
10999 ----------------------------------
11001 procedure Constrain_Discriminated_Type
11002 (Def_Id : Entity_Id;
11003 S : Node_Id;
11004 Related_Nod : Node_Id;
11005 For_Access : Boolean := False)
11007 E : constant Entity_Id := Entity (Subtype_Mark (S));
11008 T : Entity_Id;
11009 C : Node_Id;
11010 Elist : Elist_Id := New_Elmt_List;
11012 procedure Fixup_Bad_Constraint;
11013 -- This is called after finding a bad constraint, and after having
11014 -- posted an appropriate error message. The mission is to leave the
11015 -- entity T in as reasonable state as possible!
11017 --------------------------
11018 -- Fixup_Bad_Constraint --
11019 --------------------------
11021 procedure Fixup_Bad_Constraint is
11022 begin
11023 -- Set a reasonable Ekind for the entity. For an incomplete type,
11024 -- we can't do much, but for other types, we can set the proper
11025 -- corresponding subtype kind.
11027 if Ekind (T) = E_Incomplete_Type then
11028 Set_Ekind (Def_Id, Ekind (T));
11029 else
11030 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
11031 end if;
11033 -- Set Etype to the known type, to reduce chances of cascaded errors
11035 Set_Etype (Def_Id, E);
11036 Set_Error_Posted (Def_Id);
11037 end Fixup_Bad_Constraint;
11039 -- Start of processing for Constrain_Discriminated_Type
11041 begin
11042 C := Constraint (S);
11044 -- A discriminant constraint is only allowed in a subtype indication,
11045 -- after a subtype mark. This subtype mark must denote either a type
11046 -- with discriminants, or an access type whose designated type is a
11047 -- type with discriminants. A discriminant constraint specifies the
11048 -- values of these discriminants (RM 3.7.2(5)).
11050 T := Base_Type (Entity (Subtype_Mark (S)));
11052 if Ekind (T) in Access_Kind then
11053 T := Designated_Type (T);
11054 end if;
11056 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
11057 -- Avoid generating an error for access-to-incomplete subtypes.
11059 if Ada_Version >= Ada_2005
11060 and then Ekind (T) = E_Incomplete_Type
11061 and then Nkind (Parent (S)) = N_Subtype_Declaration
11062 and then not Is_Itype (Def_Id)
11063 then
11064 -- A little sanity check, emit an error message if the type
11065 -- has discriminants to begin with. Type T may be a regular
11066 -- incomplete type or imported via a limited with clause.
11068 if Has_Discriminants (T)
11069 or else
11070 (From_With_Type (T)
11071 and then Present (Non_Limited_View (T))
11072 and then Nkind (Parent (Non_Limited_View (T))) =
11073 N_Full_Type_Declaration
11074 and then Present (Discriminant_Specifications
11075 (Parent (Non_Limited_View (T)))))
11076 then
11077 Error_Msg_N
11078 ("(Ada 2005) incomplete subtype may not be constrained", C);
11079 else
11080 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11081 end if;
11083 Fixup_Bad_Constraint;
11084 return;
11086 -- Check that the type has visible discriminants. The type may be
11087 -- a private type with unknown discriminants whose full view has
11088 -- discriminants which are invisible.
11090 elsif not Has_Discriminants (T)
11091 or else
11092 (Has_Unknown_Discriminants (T)
11093 and then Is_Private_Type (T))
11094 then
11095 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11096 Fixup_Bad_Constraint;
11097 return;
11099 elsif Is_Constrained (E)
11100 or else (Ekind (E) = E_Class_Wide_Subtype
11101 and then Present (Discriminant_Constraint (E)))
11102 then
11103 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
11104 Fixup_Bad_Constraint;
11105 return;
11106 end if;
11108 -- T may be an unconstrained subtype (e.g. a generic actual).
11109 -- Constraint applies to the base type.
11111 T := Base_Type (T);
11113 Elist := Build_Discriminant_Constraints (T, S);
11115 -- If the list returned was empty we had an error in building the
11116 -- discriminant constraint. We have also already signalled an error
11117 -- in the incomplete type case
11119 if Is_Empty_Elmt_List (Elist) then
11120 Fixup_Bad_Constraint;
11121 return;
11122 end if;
11124 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
11125 end Constrain_Discriminated_Type;
11127 ---------------------------
11128 -- Constrain_Enumeration --
11129 ---------------------------
11131 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
11132 T : constant Entity_Id := Entity (Subtype_Mark (S));
11133 C : constant Node_Id := Constraint (S);
11135 begin
11136 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11138 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
11140 Set_Etype (Def_Id, Base_Type (T));
11141 Set_Size_Info (Def_Id, (T));
11142 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11143 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11145 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11147 Set_Discrete_RM_Size (Def_Id);
11148 end Constrain_Enumeration;
11150 ----------------------
11151 -- Constrain_Float --
11152 ----------------------
11154 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
11155 T : constant Entity_Id := Entity (Subtype_Mark (S));
11156 C : Node_Id;
11157 D : Node_Id;
11158 Rais : Node_Id;
11160 begin
11161 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
11163 Set_Etype (Def_Id, Base_Type (T));
11164 Set_Size_Info (Def_Id, (T));
11165 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11167 -- Process the constraint
11169 C := Constraint (S);
11171 -- Digits constraint present
11173 if Nkind (C) = N_Digits_Constraint then
11174 Check_Restriction (No_Obsolescent_Features, C);
11176 if Warn_On_Obsolescent_Feature then
11177 Error_Msg_N
11178 ("subtype digits constraint is an " &
11179 "obsolescent feature (RM J.3(8))?", C);
11180 end if;
11182 D := Digits_Expression (C);
11183 Analyze_And_Resolve (D, Any_Integer);
11184 Check_Digits_Expression (D);
11185 Set_Digits_Value (Def_Id, Expr_Value (D));
11187 -- Check that digits value is in range. Obviously we can do this
11188 -- at compile time, but it is strictly a runtime check, and of
11189 -- course there is an ACVC test that checks this!
11191 if Digits_Value (Def_Id) > Digits_Value (T) then
11192 Error_Msg_Uint_1 := Digits_Value (T);
11193 Error_Msg_N ("?digits value is too large, maximum is ^", D);
11194 Rais :=
11195 Make_Raise_Constraint_Error (Sloc (D),
11196 Reason => CE_Range_Check_Failed);
11197 Insert_Action (Declaration_Node (Def_Id), Rais);
11198 end if;
11200 C := Range_Constraint (C);
11202 -- No digits constraint present
11204 else
11205 Set_Digits_Value (Def_Id, Digits_Value (T));
11206 end if;
11208 -- Range constraint present
11210 if Nkind (C) = N_Range_Constraint then
11211 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11213 -- No range constraint present
11215 else
11216 pragma Assert (No (C));
11217 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11218 end if;
11220 Set_Is_Constrained (Def_Id);
11221 end Constrain_Float;
11223 ---------------------
11224 -- Constrain_Index --
11225 ---------------------
11227 procedure Constrain_Index
11228 (Index : Node_Id;
11229 S : Node_Id;
11230 Related_Nod : Node_Id;
11231 Related_Id : Entity_Id;
11232 Suffix : Character;
11233 Suffix_Index : Nat)
11235 Def_Id : Entity_Id;
11236 R : Node_Id := Empty;
11237 T : constant Entity_Id := Etype (Index);
11239 begin
11240 if Nkind (S) = N_Range
11241 or else
11242 (Nkind (S) = N_Attribute_Reference
11243 and then Attribute_Name (S) = Name_Range)
11244 then
11245 -- A Range attribute will transformed into N_Range by Resolve
11247 Analyze (S);
11248 Set_Etype (S, T);
11249 R := S;
11251 Process_Range_Expr_In_Decl (R, T, Empty_List);
11253 if not Error_Posted (S)
11254 and then
11255 (Nkind (S) /= N_Range
11256 or else not Covers (T, (Etype (Low_Bound (S))))
11257 or else not Covers (T, (Etype (High_Bound (S)))))
11258 then
11259 if Base_Type (T) /= Any_Type
11260 and then Etype (Low_Bound (S)) /= Any_Type
11261 and then Etype (High_Bound (S)) /= Any_Type
11262 then
11263 Error_Msg_N ("range expected", S);
11264 end if;
11265 end if;
11267 elsif Nkind (S) = N_Subtype_Indication then
11269 -- The parser has verified that this is a discrete indication
11271 Resolve_Discrete_Subtype_Indication (S, T);
11272 R := Range_Expression (Constraint (S));
11274 elsif Nkind (S) = N_Discriminant_Association then
11276 -- Syntactically valid in subtype indication
11278 Error_Msg_N ("invalid index constraint", S);
11279 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11280 return;
11282 -- Subtype_Mark case, no anonymous subtypes to construct
11284 else
11285 Analyze (S);
11287 if Is_Entity_Name (S) then
11288 if not Is_Type (Entity (S)) then
11289 Error_Msg_N ("expect subtype mark for index constraint", S);
11291 elsif Base_Type (Entity (S)) /= Base_Type (T) then
11292 Wrong_Type (S, Base_Type (T));
11293 end if;
11295 return;
11297 else
11298 Error_Msg_N ("invalid index constraint", S);
11299 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11300 return;
11301 end if;
11302 end if;
11304 Def_Id :=
11305 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
11307 Set_Etype (Def_Id, Base_Type (T));
11309 if Is_Modular_Integer_Type (T) then
11310 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11312 elsif Is_Integer_Type (T) then
11313 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11315 else
11316 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11317 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11318 Set_First_Literal (Def_Id, First_Literal (T));
11319 end if;
11321 Set_Size_Info (Def_Id, (T));
11322 Set_RM_Size (Def_Id, RM_Size (T));
11323 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11325 Set_Scalar_Range (Def_Id, R);
11327 Set_Etype (S, Def_Id);
11328 Set_Discrete_RM_Size (Def_Id);
11329 end Constrain_Index;
11331 -----------------------
11332 -- Constrain_Integer --
11333 -----------------------
11335 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
11336 T : constant Entity_Id := Entity (Subtype_Mark (S));
11337 C : constant Node_Id := Constraint (S);
11339 begin
11340 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11342 if Is_Modular_Integer_Type (T) then
11343 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11344 else
11345 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11346 end if;
11348 Set_Etype (Def_Id, Base_Type (T));
11349 Set_Size_Info (Def_Id, (T));
11350 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11351 Set_Discrete_RM_Size (Def_Id);
11352 end Constrain_Integer;
11354 ------------------------------
11355 -- Constrain_Ordinary_Fixed --
11356 ------------------------------
11358 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
11359 T : constant Entity_Id := Entity (Subtype_Mark (S));
11360 C : Node_Id;
11361 D : Node_Id;
11362 Rais : Node_Id;
11364 begin
11365 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
11366 Set_Etype (Def_Id, Base_Type (T));
11367 Set_Size_Info (Def_Id, (T));
11368 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11369 Set_Small_Value (Def_Id, Small_Value (T));
11371 -- Process the constraint
11373 C := Constraint (S);
11375 -- Delta constraint present
11377 if Nkind (C) = N_Delta_Constraint then
11378 Check_Restriction (No_Obsolescent_Features, C);
11380 if Warn_On_Obsolescent_Feature then
11381 Error_Msg_S
11382 ("subtype delta constraint is an " &
11383 "obsolescent feature (RM J.3(7))?");
11384 end if;
11386 D := Delta_Expression (C);
11387 Analyze_And_Resolve (D, Any_Real);
11388 Check_Delta_Expression (D);
11389 Set_Delta_Value (Def_Id, Expr_Value_R (D));
11391 -- Check that delta value is in range. Obviously we can do this
11392 -- at compile time, but it is strictly a runtime check, and of
11393 -- course there is an ACVC test that checks this!
11395 if Delta_Value (Def_Id) < Delta_Value (T) then
11396 Error_Msg_N ("?delta value is too small", D);
11397 Rais :=
11398 Make_Raise_Constraint_Error (Sloc (D),
11399 Reason => CE_Range_Check_Failed);
11400 Insert_Action (Declaration_Node (Def_Id), Rais);
11401 end if;
11403 C := Range_Constraint (C);
11405 -- No delta constraint present
11407 else
11408 Set_Delta_Value (Def_Id, Delta_Value (T));
11409 end if;
11411 -- Range constraint present
11413 if Nkind (C) = N_Range_Constraint then
11414 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11416 -- No range constraint present
11418 else
11419 pragma Assert (No (C));
11420 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11422 end if;
11424 Set_Discrete_RM_Size (Def_Id);
11426 -- Unconditionally delay the freeze, since we cannot set size
11427 -- information in all cases correctly until the freeze point.
11429 Set_Has_Delayed_Freeze (Def_Id);
11430 end Constrain_Ordinary_Fixed;
11432 -----------------------
11433 -- Contain_Interface --
11434 -----------------------
11436 function Contain_Interface
11437 (Iface : Entity_Id;
11438 Ifaces : Elist_Id) return Boolean
11440 Iface_Elmt : Elmt_Id;
11442 begin
11443 if Present (Ifaces) then
11444 Iface_Elmt := First_Elmt (Ifaces);
11445 while Present (Iface_Elmt) loop
11446 if Node (Iface_Elmt) = Iface then
11447 return True;
11448 end if;
11450 Next_Elmt (Iface_Elmt);
11451 end loop;
11452 end if;
11454 return False;
11455 end Contain_Interface;
11457 ---------------------------
11458 -- Convert_Scalar_Bounds --
11459 ---------------------------
11461 procedure Convert_Scalar_Bounds
11462 (N : Node_Id;
11463 Parent_Type : Entity_Id;
11464 Derived_Type : Entity_Id;
11465 Loc : Source_Ptr)
11467 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
11469 Lo : Node_Id;
11470 Hi : Node_Id;
11471 Rng : Node_Id;
11473 begin
11474 -- Defend against previous errors
11476 if No (Scalar_Range (Derived_Type)) then
11477 return;
11478 end if;
11480 Lo := Build_Scalar_Bound
11481 (Type_Low_Bound (Derived_Type),
11482 Parent_Type, Implicit_Base);
11484 Hi := Build_Scalar_Bound
11485 (Type_High_Bound (Derived_Type),
11486 Parent_Type, Implicit_Base);
11488 Rng :=
11489 Make_Range (Loc,
11490 Low_Bound => Lo,
11491 High_Bound => Hi);
11493 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
11495 Set_Parent (Rng, N);
11496 Set_Scalar_Range (Derived_Type, Rng);
11498 -- Analyze the bounds
11500 Analyze_And_Resolve (Lo, Implicit_Base);
11501 Analyze_And_Resolve (Hi, Implicit_Base);
11503 -- Analyze the range itself, except that we do not analyze it if
11504 -- the bounds are real literals, and we have a fixed-point type.
11505 -- The reason for this is that we delay setting the bounds in this
11506 -- case till we know the final Small and Size values (see circuit
11507 -- in Freeze.Freeze_Fixed_Point_Type for further details).
11509 if Is_Fixed_Point_Type (Parent_Type)
11510 and then Nkind (Lo) = N_Real_Literal
11511 and then Nkind (Hi) = N_Real_Literal
11512 then
11513 return;
11515 -- Here we do the analysis of the range
11517 -- Note: we do this manually, since if we do a normal Analyze and
11518 -- Resolve call, there are problems with the conversions used for
11519 -- the derived type range.
11521 else
11522 Set_Etype (Rng, Implicit_Base);
11523 Set_Analyzed (Rng, True);
11524 end if;
11525 end Convert_Scalar_Bounds;
11527 -------------------
11528 -- Copy_And_Swap --
11529 -------------------
11531 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
11532 begin
11533 -- Initialize new full declaration entity by copying the pertinent
11534 -- fields of the corresponding private declaration entity.
11536 -- We temporarily set Ekind to a value appropriate for a type to
11537 -- avoid assert failures in Einfo from checking for setting type
11538 -- attributes on something that is not a type. Ekind (Priv) is an
11539 -- appropriate choice, since it allowed the attributes to be set
11540 -- in the first place. This Ekind value will be modified later.
11542 Set_Ekind (Full, Ekind (Priv));
11544 -- Also set Etype temporarily to Any_Type, again, in the absence
11545 -- of errors, it will be properly reset, and if there are errors,
11546 -- then we want a value of Any_Type to remain.
11548 Set_Etype (Full, Any_Type);
11550 -- Now start copying attributes
11552 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
11554 if Has_Discriminants (Full) then
11555 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
11556 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
11557 end if;
11559 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11560 Set_Homonym (Full, Homonym (Priv));
11561 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
11562 Set_Is_Public (Full, Is_Public (Priv));
11563 Set_Is_Pure (Full, Is_Pure (Priv));
11564 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
11565 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
11566 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
11567 Set_Has_Pragma_Unreferenced_Objects
11568 (Full, Has_Pragma_Unreferenced_Objects
11569 (Priv));
11571 Conditional_Delay (Full, Priv);
11573 if Is_Tagged_Type (Full) then
11574 Set_Direct_Primitive_Operations (Full,
11575 Direct_Primitive_Operations (Priv));
11577 if Priv = Base_Type (Priv) then
11578 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
11579 end if;
11580 end if;
11582 Set_Is_Volatile (Full, Is_Volatile (Priv));
11583 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
11584 Set_Scope (Full, Scope (Priv));
11585 Set_Next_Entity (Full, Next_Entity (Priv));
11586 Set_First_Entity (Full, First_Entity (Priv));
11587 Set_Last_Entity (Full, Last_Entity (Priv));
11589 -- If access types have been recorded for later handling, keep them in
11590 -- the full view so that they get handled when the full view freeze
11591 -- node is expanded.
11593 if Present (Freeze_Node (Priv))
11594 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
11595 then
11596 Ensure_Freeze_Node (Full);
11597 Set_Access_Types_To_Process
11598 (Freeze_Node (Full),
11599 Access_Types_To_Process (Freeze_Node (Priv)));
11600 end if;
11602 -- Swap the two entities. Now Privat is the full type entity and Full is
11603 -- the private one. They will be swapped back at the end of the private
11604 -- part. This swapping ensures that the entity that is visible in the
11605 -- private part is the full declaration.
11607 Exchange_Entities (Priv, Full);
11608 Append_Entity (Full, Scope (Full));
11609 end Copy_And_Swap;
11611 -------------------------------------
11612 -- Copy_Array_Base_Type_Attributes --
11613 -------------------------------------
11615 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
11616 begin
11617 Set_Component_Alignment (T1, Component_Alignment (T2));
11618 Set_Component_Type (T1, Component_Type (T2));
11619 Set_Component_Size (T1, Component_Size (T2));
11620 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
11621 Set_Finalize_Storage_Only (T1, Finalize_Storage_Only (T2));
11622 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
11623 Set_Has_Task (T1, Has_Task (T2));
11624 Set_Is_Packed (T1, Is_Packed (T2));
11625 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
11626 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
11627 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
11628 end Copy_Array_Base_Type_Attributes;
11630 -----------------------------------
11631 -- Copy_Array_Subtype_Attributes --
11632 -----------------------------------
11634 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
11635 begin
11636 Set_Size_Info (T1, T2);
11638 Set_First_Index (T1, First_Index (T2));
11639 Set_Is_Aliased (T1, Is_Aliased (T2));
11640 Set_Is_Atomic (T1, Is_Atomic (T2));
11641 Set_Is_Volatile (T1, Is_Volatile (T2));
11642 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
11643 Set_Is_Constrained (T1, Is_Constrained (T2));
11644 Set_Depends_On_Private (T1, Has_Private_Component (T2));
11645 Set_First_Rep_Item (T1, First_Rep_Item (T2));
11646 Set_Convention (T1, Convention (T2));
11647 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
11648 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
11649 Set_Packed_Array_Type (T1, Packed_Array_Type (T2));
11650 end Copy_Array_Subtype_Attributes;
11652 -----------------------------------
11653 -- Create_Constrained_Components --
11654 -----------------------------------
11656 procedure Create_Constrained_Components
11657 (Subt : Entity_Id;
11658 Decl_Node : Node_Id;
11659 Typ : Entity_Id;
11660 Constraints : Elist_Id)
11662 Loc : constant Source_Ptr := Sloc (Subt);
11663 Comp_List : constant Elist_Id := New_Elmt_List;
11664 Parent_Type : constant Entity_Id := Etype (Typ);
11665 Assoc_List : constant List_Id := New_List;
11666 Discr_Val : Elmt_Id;
11667 Errors : Boolean;
11668 New_C : Entity_Id;
11669 Old_C : Entity_Id;
11670 Is_Static : Boolean := True;
11672 procedure Collect_Fixed_Components (Typ : Entity_Id);
11673 -- Collect parent type components that do not appear in a variant part
11675 procedure Create_All_Components;
11676 -- Iterate over Comp_List to create the components of the subtype
11678 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
11679 -- Creates a new component from Old_Compon, copying all the fields from
11680 -- it, including its Etype, inserts the new component in the Subt entity
11681 -- chain and returns the new component.
11683 function Is_Variant_Record (T : Entity_Id) return Boolean;
11684 -- If true, and discriminants are static, collect only components from
11685 -- variants selected by discriminant values.
11687 ------------------------------
11688 -- Collect_Fixed_Components --
11689 ------------------------------
11691 procedure Collect_Fixed_Components (Typ : Entity_Id) is
11692 begin
11693 -- Build association list for discriminants, and find components of the
11694 -- variant part selected by the values of the discriminants.
11696 Old_C := First_Discriminant (Typ);
11697 Discr_Val := First_Elmt (Constraints);
11698 while Present (Old_C) loop
11699 Append_To (Assoc_List,
11700 Make_Component_Association (Loc,
11701 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
11702 Expression => New_Copy (Node (Discr_Val))));
11704 Next_Elmt (Discr_Val);
11705 Next_Discriminant (Old_C);
11706 end loop;
11708 -- The tag, and the possible parent and controller components
11709 -- are unconditionally in the subtype.
11711 if Is_Tagged_Type (Typ)
11712 or else Has_Controlled_Component (Typ)
11713 then
11714 Old_C := First_Component (Typ);
11715 while Present (Old_C) loop
11716 if Chars ((Old_C)) = Name_uTag
11717 or else Chars ((Old_C)) = Name_uParent
11718 or else Chars ((Old_C)) = Name_uController
11719 then
11720 Append_Elmt (Old_C, Comp_List);
11721 end if;
11723 Next_Component (Old_C);
11724 end loop;
11725 end if;
11726 end Collect_Fixed_Components;
11728 ---------------------------
11729 -- Create_All_Components --
11730 ---------------------------
11732 procedure Create_All_Components is
11733 Comp : Elmt_Id;
11735 begin
11736 Comp := First_Elmt (Comp_List);
11737 while Present (Comp) loop
11738 Old_C := Node (Comp);
11739 New_C := Create_Component (Old_C);
11741 Set_Etype
11742 (New_C,
11743 Constrain_Component_Type
11744 (Old_C, Subt, Decl_Node, Typ, Constraints));
11745 Set_Is_Public (New_C, Is_Public (Subt));
11747 Next_Elmt (Comp);
11748 end loop;
11749 end Create_All_Components;
11751 ----------------------
11752 -- Create_Component --
11753 ----------------------
11755 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
11756 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
11758 begin
11759 if Ekind (Old_Compon) = E_Discriminant
11760 and then Is_Completely_Hidden (Old_Compon)
11761 then
11762 -- This is a shadow discriminant created for a discriminant of
11763 -- the parent type, which needs to be present in the subtype.
11764 -- Give the shadow discriminant an internal name that cannot
11765 -- conflict with that of visible components.
11767 Set_Chars (New_Compon, New_Internal_Name ('C'));
11768 end if;
11770 -- Set the parent so we have a proper link for freezing etc. This is
11771 -- not a real parent pointer, since of course our parent does not own
11772 -- up to us and reference us, we are an illegitimate child of the
11773 -- original parent!
11775 Set_Parent (New_Compon, Parent (Old_Compon));
11777 -- If the old component's Esize was already determined and is a
11778 -- static value, then the new component simply inherits it. Otherwise
11779 -- the old component's size may require run-time determination, but
11780 -- the new component's size still might be statically determinable
11781 -- (if, for example it has a static constraint). In that case we want
11782 -- Layout_Type to recompute the component's size, so we reset its
11783 -- size and positional fields.
11785 if Frontend_Layout_On_Target
11786 and then not Known_Static_Esize (Old_Compon)
11787 then
11788 Set_Esize (New_Compon, Uint_0);
11789 Init_Normalized_First_Bit (New_Compon);
11790 Init_Normalized_Position (New_Compon);
11791 Init_Normalized_Position_Max (New_Compon);
11792 end if;
11794 -- We do not want this node marked as Comes_From_Source, since
11795 -- otherwise it would get first class status and a separate cross-
11796 -- reference line would be generated. Illegitimate children do not
11797 -- rate such recognition.
11799 Set_Comes_From_Source (New_Compon, False);
11801 -- But it is a real entity, and a birth certificate must be properly
11802 -- registered by entering it into the entity list.
11804 Enter_Name (New_Compon);
11806 return New_Compon;
11807 end Create_Component;
11809 -----------------------
11810 -- Is_Variant_Record --
11811 -----------------------
11813 function Is_Variant_Record (T : Entity_Id) return Boolean is
11814 begin
11815 return Nkind (Parent (T)) = N_Full_Type_Declaration
11816 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
11817 and then Present (Component_List (Type_Definition (Parent (T))))
11818 and then
11819 Present
11820 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
11821 end Is_Variant_Record;
11823 -- Start of processing for Create_Constrained_Components
11825 begin
11826 pragma Assert (Subt /= Base_Type (Subt));
11827 pragma Assert (Typ = Base_Type (Typ));
11829 Set_First_Entity (Subt, Empty);
11830 Set_Last_Entity (Subt, Empty);
11832 -- Check whether constraint is fully static, in which case we can
11833 -- optimize the list of components.
11835 Discr_Val := First_Elmt (Constraints);
11836 while Present (Discr_Val) loop
11837 if not Is_OK_Static_Expression (Node (Discr_Val)) then
11838 Is_Static := False;
11839 exit;
11840 end if;
11842 Next_Elmt (Discr_Val);
11843 end loop;
11845 Set_Has_Static_Discriminants (Subt, Is_Static);
11847 Push_Scope (Subt);
11849 -- Inherit the discriminants of the parent type
11851 Add_Discriminants : declare
11852 Num_Disc : Int;
11853 Num_Gird : Int;
11855 begin
11856 Num_Disc := 0;
11857 Old_C := First_Discriminant (Typ);
11859 while Present (Old_C) loop
11860 Num_Disc := Num_Disc + 1;
11861 New_C := Create_Component (Old_C);
11862 Set_Is_Public (New_C, Is_Public (Subt));
11863 Next_Discriminant (Old_C);
11864 end loop;
11866 -- For an untagged derived subtype, the number of discriminants may
11867 -- be smaller than the number of inherited discriminants, because
11868 -- several of them may be renamed by a single new discriminant or
11869 -- constrained. In this case, add the hidden discriminants back into
11870 -- the subtype, because they need to be present if the optimizer of
11871 -- the GCC 4.x back-end decides to break apart assignments between
11872 -- objects using the parent view into member-wise assignments.
11874 Num_Gird := 0;
11876 if Is_Derived_Type (Typ)
11877 and then not Is_Tagged_Type (Typ)
11878 then
11879 Old_C := First_Stored_Discriminant (Typ);
11881 while Present (Old_C) loop
11882 Num_Gird := Num_Gird + 1;
11883 Next_Stored_Discriminant (Old_C);
11884 end loop;
11885 end if;
11887 if Num_Gird > Num_Disc then
11889 -- Find out multiple uses of new discriminants, and add hidden
11890 -- components for the extra renamed discriminants. We recognize
11891 -- multiple uses through the Corresponding_Discriminant of a
11892 -- new discriminant: if it constrains several old discriminants,
11893 -- this field points to the last one in the parent type. The
11894 -- stored discriminants of the derived type have the same name
11895 -- as those of the parent.
11897 declare
11898 Constr : Elmt_Id;
11899 New_Discr : Entity_Id;
11900 Old_Discr : Entity_Id;
11902 begin
11903 Constr := First_Elmt (Stored_Constraint (Typ));
11904 Old_Discr := First_Stored_Discriminant (Typ);
11905 while Present (Constr) loop
11906 if Is_Entity_Name (Node (Constr))
11907 and then Ekind (Entity (Node (Constr))) = E_Discriminant
11908 then
11909 New_Discr := Entity (Node (Constr));
11911 if Chars (Corresponding_Discriminant (New_Discr)) /=
11912 Chars (Old_Discr)
11913 then
11914 -- The new discriminant has been used to rename a
11915 -- subsequent old discriminant. Introduce a shadow
11916 -- component for the current old discriminant.
11918 New_C := Create_Component (Old_Discr);
11919 Set_Original_Record_Component (New_C, Old_Discr);
11920 end if;
11922 else
11923 -- The constraint has eliminated the old discriminant.
11924 -- Introduce a shadow component.
11926 New_C := Create_Component (Old_Discr);
11927 Set_Original_Record_Component (New_C, Old_Discr);
11928 end if;
11930 Next_Elmt (Constr);
11931 Next_Stored_Discriminant (Old_Discr);
11932 end loop;
11933 end;
11934 end if;
11935 end Add_Discriminants;
11937 if Is_Static
11938 and then Is_Variant_Record (Typ)
11939 then
11940 Collect_Fixed_Components (Typ);
11942 Gather_Components (
11943 Typ,
11944 Component_List (Type_Definition (Parent (Typ))),
11945 Governed_By => Assoc_List,
11946 Into => Comp_List,
11947 Report_Errors => Errors);
11948 pragma Assert (not Errors);
11950 Create_All_Components;
11952 -- If the subtype declaration is created for a tagged type derivation
11953 -- with constraints, we retrieve the record definition of the parent
11954 -- type to select the components of the proper variant.
11956 elsif Is_Static
11957 and then Is_Tagged_Type (Typ)
11958 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11959 and then
11960 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
11961 and then Is_Variant_Record (Parent_Type)
11962 then
11963 Collect_Fixed_Components (Typ);
11965 Gather_Components (
11966 Typ,
11967 Component_List (Type_Definition (Parent (Parent_Type))),
11968 Governed_By => Assoc_List,
11969 Into => Comp_List,
11970 Report_Errors => Errors);
11971 pragma Assert (not Errors);
11973 -- If the tagged derivation has a type extension, collect all the
11974 -- new components therein.
11976 if Present
11977 (Record_Extension_Part (Type_Definition (Parent (Typ))))
11978 then
11979 Old_C := First_Component (Typ);
11980 while Present (Old_C) loop
11981 if Original_Record_Component (Old_C) = Old_C
11982 and then Chars (Old_C) /= Name_uTag
11983 and then Chars (Old_C) /= Name_uParent
11984 and then Chars (Old_C) /= Name_uController
11985 then
11986 Append_Elmt (Old_C, Comp_List);
11987 end if;
11989 Next_Component (Old_C);
11990 end loop;
11991 end if;
11993 Create_All_Components;
11995 else
11996 -- If discriminants are not static, or if this is a multi-level type
11997 -- extension, we have to include all components of the parent type.
11999 Old_C := First_Component (Typ);
12000 while Present (Old_C) loop
12001 New_C := Create_Component (Old_C);
12003 Set_Etype
12004 (New_C,
12005 Constrain_Component_Type
12006 (Old_C, Subt, Decl_Node, Typ, Constraints));
12007 Set_Is_Public (New_C, Is_Public (Subt));
12009 Next_Component (Old_C);
12010 end loop;
12011 end if;
12013 End_Scope;
12014 end Create_Constrained_Components;
12016 ------------------------------------------
12017 -- Decimal_Fixed_Point_Type_Declaration --
12018 ------------------------------------------
12020 procedure Decimal_Fixed_Point_Type_Declaration
12021 (T : Entity_Id;
12022 Def : Node_Id)
12024 Loc : constant Source_Ptr := Sloc (Def);
12025 Digs_Expr : constant Node_Id := Digits_Expression (Def);
12026 Delta_Expr : constant Node_Id := Delta_Expression (Def);
12027 Implicit_Base : Entity_Id;
12028 Digs_Val : Uint;
12029 Delta_Val : Ureal;
12030 Scale_Val : Uint;
12031 Bound_Val : Ureal;
12033 begin
12034 Check_Restriction (No_Fixed_Point, Def);
12036 -- Create implicit base type
12038 Implicit_Base :=
12039 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
12040 Set_Etype (Implicit_Base, Implicit_Base);
12042 -- Analyze and process delta expression
12044 Analyze_And_Resolve (Delta_Expr, Universal_Real);
12046 Check_Delta_Expression (Delta_Expr);
12047 Delta_Val := Expr_Value_R (Delta_Expr);
12049 -- Check delta is power of 10, and determine scale value from it
12051 declare
12052 Val : Ureal;
12054 begin
12055 Scale_Val := Uint_0;
12056 Val := Delta_Val;
12058 if Val < Ureal_1 then
12059 while Val < Ureal_1 loop
12060 Val := Val * Ureal_10;
12061 Scale_Val := Scale_Val + 1;
12062 end loop;
12064 if Scale_Val > 18 then
12065 Error_Msg_N ("scale exceeds maximum value of 18", Def);
12066 Scale_Val := UI_From_Int (+18);
12067 end if;
12069 else
12070 while Val > Ureal_1 loop
12071 Val := Val / Ureal_10;
12072 Scale_Val := Scale_Val - 1;
12073 end loop;
12075 if Scale_Val < -18 then
12076 Error_Msg_N ("scale is less than minimum value of -18", Def);
12077 Scale_Val := UI_From_Int (-18);
12078 end if;
12079 end if;
12081 if Val /= Ureal_1 then
12082 Error_Msg_N ("delta expression must be a power of 10", Def);
12083 Delta_Val := Ureal_10 ** (-Scale_Val);
12084 end if;
12085 end;
12087 -- Set delta, scale and small (small = delta for decimal type)
12089 Set_Delta_Value (Implicit_Base, Delta_Val);
12090 Set_Scale_Value (Implicit_Base, Scale_Val);
12091 Set_Small_Value (Implicit_Base, Delta_Val);
12093 -- Analyze and process digits expression
12095 Analyze_And_Resolve (Digs_Expr, Any_Integer);
12096 Check_Digits_Expression (Digs_Expr);
12097 Digs_Val := Expr_Value (Digs_Expr);
12099 if Digs_Val > 18 then
12100 Digs_Val := UI_From_Int (+18);
12101 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
12102 end if;
12104 Set_Digits_Value (Implicit_Base, Digs_Val);
12105 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
12107 -- Set range of base type from digits value for now. This will be
12108 -- expanded to represent the true underlying base range by Freeze.
12110 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
12112 -- Note: We leave size as zero for now, size will be set at freeze
12113 -- time. We have to do this for ordinary fixed-point, because the size
12114 -- depends on the specified small, and we might as well do the same for
12115 -- decimal fixed-point.
12117 pragma Assert (Esize (Implicit_Base) = Uint_0);
12119 -- If there are bounds given in the declaration use them as the
12120 -- bounds of the first named subtype.
12122 if Present (Real_Range_Specification (Def)) then
12123 declare
12124 RRS : constant Node_Id := Real_Range_Specification (Def);
12125 Low : constant Node_Id := Low_Bound (RRS);
12126 High : constant Node_Id := High_Bound (RRS);
12127 Low_Val : Ureal;
12128 High_Val : Ureal;
12130 begin
12131 Analyze_And_Resolve (Low, Any_Real);
12132 Analyze_And_Resolve (High, Any_Real);
12133 Check_Real_Bound (Low);
12134 Check_Real_Bound (High);
12135 Low_Val := Expr_Value_R (Low);
12136 High_Val := Expr_Value_R (High);
12138 if Low_Val < (-Bound_Val) then
12139 Error_Msg_N
12140 ("range low bound too small for digits value", Low);
12141 Low_Val := -Bound_Val;
12142 end if;
12144 if High_Val > Bound_Val then
12145 Error_Msg_N
12146 ("range high bound too large for digits value", High);
12147 High_Val := Bound_Val;
12148 end if;
12150 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
12151 end;
12153 -- If no explicit range, use range that corresponds to given
12154 -- digits value. This will end up as the final range for the
12155 -- first subtype.
12157 else
12158 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
12159 end if;
12161 -- Complete entity for first subtype
12163 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
12164 Set_Etype (T, Implicit_Base);
12165 Set_Size_Info (T, Implicit_Base);
12166 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
12167 Set_Digits_Value (T, Digs_Val);
12168 Set_Delta_Value (T, Delta_Val);
12169 Set_Small_Value (T, Delta_Val);
12170 Set_Scale_Value (T, Scale_Val);
12171 Set_Is_Constrained (T);
12172 end Decimal_Fixed_Point_Type_Declaration;
12174 -----------------------------------
12175 -- Derive_Progenitor_Subprograms --
12176 -----------------------------------
12178 procedure Derive_Progenitor_Subprograms
12179 (Parent_Type : Entity_Id;
12180 Tagged_Type : Entity_Id)
12182 E : Entity_Id;
12183 Elmt : Elmt_Id;
12184 Iface : Entity_Id;
12185 Iface_Elmt : Elmt_Id;
12186 Iface_Subp : Entity_Id;
12187 New_Subp : Entity_Id := Empty;
12188 Prim_Elmt : Elmt_Id;
12189 Subp : Entity_Id;
12190 Typ : Entity_Id;
12192 begin
12193 pragma Assert (Ada_Version >= Ada_2005
12194 and then Is_Record_Type (Tagged_Type)
12195 and then Is_Tagged_Type (Tagged_Type)
12196 and then Has_Interfaces (Tagged_Type));
12198 -- Step 1: Transfer to the full-view primitives associated with the
12199 -- partial-view that cover interface primitives. Conceptually this
12200 -- work should be done later by Process_Full_View; done here to
12201 -- simplify its implementation at later stages. It can be safely
12202 -- done here because interfaces must be visible in the partial and
12203 -- private view (RM 7.3(7.3/2)).
12205 -- Small optimization: This work is only required if the parent is
12206 -- abstract. If the tagged type is not abstract, it cannot have
12207 -- abstract primitives (the only entities in the list of primitives of
12208 -- non-abstract tagged types that can reference abstract primitives
12209 -- through its Alias attribute are the internal entities that have
12210 -- attribute Interface_Alias, and these entities are generated later
12211 -- by Add_Internal_Interface_Entities).
12213 if In_Private_Part (Current_Scope)
12214 and then Is_Abstract_Type (Parent_Type)
12215 then
12216 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
12217 while Present (Elmt) loop
12218 Subp := Node (Elmt);
12220 -- At this stage it is not possible to have entities in the list
12221 -- of primitives that have attribute Interface_Alias
12223 pragma Assert (No (Interface_Alias (Subp)));
12225 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
12227 if Is_Interface (Typ) then
12228 E := Find_Primitive_Covering_Interface
12229 (Tagged_Type => Tagged_Type,
12230 Iface_Prim => Subp);
12232 if Present (E)
12233 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
12234 then
12235 Replace_Elmt (Elmt, E);
12236 Remove_Homonym (Subp);
12237 end if;
12238 end if;
12240 Next_Elmt (Elmt);
12241 end loop;
12242 end if;
12244 -- Step 2: Add primitives of progenitors that are not implemented by
12245 -- parents of Tagged_Type
12247 if Present (Interfaces (Base_Type (Tagged_Type))) then
12248 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
12249 while Present (Iface_Elmt) loop
12250 Iface := Node (Iface_Elmt);
12252 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
12253 while Present (Prim_Elmt) loop
12254 Iface_Subp := Node (Prim_Elmt);
12256 -- Exclude derivation of predefined primitives except those
12257 -- that come from source. Required to catch declarations of
12258 -- equality operators of interfaces. For example:
12260 -- type Iface is interface;
12261 -- function "=" (Left, Right : Iface) return Boolean;
12263 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
12264 or else Comes_From_Source (Iface_Subp)
12265 then
12266 E := Find_Primitive_Covering_Interface
12267 (Tagged_Type => Tagged_Type,
12268 Iface_Prim => Iface_Subp);
12270 -- If not found we derive a new primitive leaving its alias
12271 -- attribute referencing the interface primitive
12273 if No (E) then
12274 Derive_Subprogram
12275 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12277 -- Propagate to the full view interface entities associated
12278 -- with the partial view
12280 elsif In_Private_Part (Current_Scope)
12281 and then Present (Alias (E))
12282 and then Alias (E) = Iface_Subp
12283 and then
12284 List_Containing (Parent (E)) /=
12285 Private_Declarations
12286 (Specification
12287 (Unit_Declaration_Node (Current_Scope)))
12288 then
12289 Append_Elmt (E, Primitive_Operations (Tagged_Type));
12290 end if;
12291 end if;
12293 Next_Elmt (Prim_Elmt);
12294 end loop;
12296 Next_Elmt (Iface_Elmt);
12297 end loop;
12298 end if;
12299 end Derive_Progenitor_Subprograms;
12301 -----------------------
12302 -- Derive_Subprogram --
12303 -----------------------
12305 procedure Derive_Subprogram
12306 (New_Subp : in out Entity_Id;
12307 Parent_Subp : Entity_Id;
12308 Derived_Type : Entity_Id;
12309 Parent_Type : Entity_Id;
12310 Actual_Subp : Entity_Id := Empty)
12312 Formal : Entity_Id;
12313 -- Formal parameter of parent primitive operation
12315 Formal_Of_Actual : Entity_Id;
12316 -- Formal parameter of actual operation, when the derivation is to
12317 -- create a renaming for a primitive operation of an actual in an
12318 -- instantiation.
12320 New_Formal : Entity_Id;
12321 -- Formal of inherited operation
12323 Visible_Subp : Entity_Id := Parent_Subp;
12325 function Is_Private_Overriding return Boolean;
12326 -- If Subp is a private overriding of a visible operation, the inherited
12327 -- operation derives from the overridden op (even though its body is the
12328 -- overriding one) and the inherited operation is visible now. See
12329 -- sem_disp to see the full details of the handling of the overridden
12330 -- subprogram, which is removed from the list of primitive operations of
12331 -- the type. The overridden subprogram is saved locally in Visible_Subp,
12332 -- and used to diagnose abstract operations that need overriding in the
12333 -- derived type.
12335 procedure Replace_Type (Id, New_Id : Entity_Id);
12336 -- When the type is an anonymous access type, create a new access type
12337 -- designating the derived type.
12339 procedure Set_Derived_Name;
12340 -- This procedure sets the appropriate Chars name for New_Subp. This
12341 -- is normally just a copy of the parent name. An exception arises for
12342 -- type support subprograms, where the name is changed to reflect the
12343 -- name of the derived type, e.g. if type foo is derived from type bar,
12344 -- then a procedure barDA is derived with a name fooDA.
12346 ---------------------------
12347 -- Is_Private_Overriding --
12348 ---------------------------
12350 function Is_Private_Overriding return Boolean is
12351 Prev : Entity_Id;
12353 begin
12354 -- If the parent is not a dispatching operation there is no
12355 -- need to investigate overridings
12357 if not Is_Dispatching_Operation (Parent_Subp) then
12358 return False;
12359 end if;
12361 -- The visible operation that is overridden is a homonym of the
12362 -- parent subprogram. We scan the homonym chain to find the one
12363 -- whose alias is the subprogram we are deriving.
12365 Prev := Current_Entity (Parent_Subp);
12366 while Present (Prev) loop
12367 if Ekind (Prev) = Ekind (Parent_Subp)
12368 and then Alias (Prev) = Parent_Subp
12369 and then Scope (Parent_Subp) = Scope (Prev)
12370 and then not Is_Hidden (Prev)
12371 then
12372 Visible_Subp := Prev;
12373 return True;
12374 end if;
12376 Prev := Homonym (Prev);
12377 end loop;
12379 return False;
12380 end Is_Private_Overriding;
12382 ------------------
12383 -- Replace_Type --
12384 ------------------
12386 procedure Replace_Type (Id, New_Id : Entity_Id) is
12387 Acc_Type : Entity_Id;
12388 Par : constant Node_Id := Parent (Derived_Type);
12390 begin
12391 -- When the type is an anonymous access type, create a new access
12392 -- type designating the derived type. This itype must be elaborated
12393 -- at the point of the derivation, not on subsequent calls that may
12394 -- be out of the proper scope for Gigi, so we insert a reference to
12395 -- it after the derivation.
12397 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
12398 declare
12399 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
12401 begin
12402 if Ekind (Desig_Typ) = E_Record_Type_With_Private
12403 and then Present (Full_View (Desig_Typ))
12404 and then not Is_Private_Type (Parent_Type)
12405 then
12406 Desig_Typ := Full_View (Desig_Typ);
12407 end if;
12409 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
12411 -- Ada 2005 (AI-251): Handle also derivations of abstract
12412 -- interface primitives.
12414 or else (Is_Interface (Desig_Typ)
12415 and then not Is_Class_Wide_Type (Desig_Typ))
12416 then
12417 Acc_Type := New_Copy (Etype (Id));
12418 Set_Etype (Acc_Type, Acc_Type);
12419 Set_Scope (Acc_Type, New_Subp);
12421 -- Compute size of anonymous access type
12423 if Is_Array_Type (Desig_Typ)
12424 and then not Is_Constrained (Desig_Typ)
12425 then
12426 Init_Size (Acc_Type, 2 * System_Address_Size);
12427 else
12428 Init_Size (Acc_Type, System_Address_Size);
12429 end if;
12431 Init_Alignment (Acc_Type);
12432 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
12434 Set_Etype (New_Id, Acc_Type);
12435 Set_Scope (New_Id, New_Subp);
12437 -- Create a reference to it
12438 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
12440 else
12441 Set_Etype (New_Id, Etype (Id));
12442 end if;
12443 end;
12445 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
12446 or else
12447 (Ekind (Etype (Id)) = E_Record_Type_With_Private
12448 and then Present (Full_View (Etype (Id)))
12449 and then
12450 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
12451 then
12452 -- Constraint checks on formals are generated during expansion,
12453 -- based on the signature of the original subprogram. The bounds
12454 -- of the derived type are not relevant, and thus we can use
12455 -- the base type for the formals. However, the return type may be
12456 -- used in a context that requires that the proper static bounds
12457 -- be used (a case statement, for example) and for those cases
12458 -- we must use the derived type (first subtype), not its base.
12460 -- If the derived_type_definition has no constraints, we know that
12461 -- the derived type has the same constraints as the first subtype
12462 -- of the parent, and we can also use it rather than its base,
12463 -- which can lead to more efficient code.
12465 if Etype (Id) = Parent_Type then
12466 if Is_Scalar_Type (Parent_Type)
12467 and then
12468 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
12469 then
12470 Set_Etype (New_Id, Derived_Type);
12472 elsif Nkind (Par) = N_Full_Type_Declaration
12473 and then
12474 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
12475 and then
12476 Is_Entity_Name
12477 (Subtype_Indication (Type_Definition (Par)))
12478 then
12479 Set_Etype (New_Id, Derived_Type);
12481 else
12482 Set_Etype (New_Id, Base_Type (Derived_Type));
12483 end if;
12485 else
12486 Set_Etype (New_Id, Base_Type (Derived_Type));
12487 end if;
12489 else
12490 Set_Etype (New_Id, Etype (Id));
12491 end if;
12492 end Replace_Type;
12494 ----------------------
12495 -- Set_Derived_Name --
12496 ----------------------
12498 procedure Set_Derived_Name is
12499 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
12500 begin
12501 if Nm = TSS_Null then
12502 Set_Chars (New_Subp, Chars (Parent_Subp));
12503 else
12504 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
12505 end if;
12506 end Set_Derived_Name;
12508 -- Start of processing for Derive_Subprogram
12510 begin
12511 New_Subp :=
12512 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
12513 Set_Ekind (New_Subp, Ekind (Parent_Subp));
12515 -- Check whether the inherited subprogram is a private operation that
12516 -- should be inherited but not yet made visible. Such subprograms can
12517 -- become visible at a later point (e.g., the private part of a public
12518 -- child unit) via Declare_Inherited_Private_Subprograms. If the
12519 -- following predicate is true, then this is not such a private
12520 -- operation and the subprogram simply inherits the name of the parent
12521 -- subprogram. Note the special check for the names of controlled
12522 -- operations, which are currently exempted from being inherited with
12523 -- a hidden name because they must be findable for generation of
12524 -- implicit run-time calls.
12526 if not Is_Hidden (Parent_Subp)
12527 or else Is_Internal (Parent_Subp)
12528 or else Is_Private_Overriding
12529 or else Is_Internal_Name (Chars (Parent_Subp))
12530 or else Chars (Parent_Subp) = Name_Initialize
12531 or else Chars (Parent_Subp) = Name_Adjust
12532 or else Chars (Parent_Subp) = Name_Finalize
12533 then
12534 Set_Derived_Name;
12536 -- An inherited dispatching equality will be overridden by an internally
12537 -- generated one, or by an explicit one, so preserve its name and thus
12538 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
12539 -- private operation it may become invisible if the full view has
12540 -- progenitors, and the dispatch table will be malformed.
12541 -- We check that the type is limited to handle the anomalous declaration
12542 -- of Limited_Controlled, which is derived from a non-limited type, and
12543 -- which is handled specially elsewhere as well.
12545 elsif Chars (Parent_Subp) = Name_Op_Eq
12546 and then Is_Dispatching_Operation (Parent_Subp)
12547 and then Etype (Parent_Subp) = Standard_Boolean
12548 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
12549 and then
12550 Etype (First_Formal (Parent_Subp)) =
12551 Etype (Next_Formal (First_Formal (Parent_Subp)))
12552 then
12553 Set_Derived_Name;
12555 -- If parent is hidden, this can be a regular derivation if the
12556 -- parent is immediately visible in a non-instantiating context,
12557 -- or if we are in the private part of an instance. This test
12558 -- should still be refined ???
12560 -- The test for In_Instance_Not_Visible avoids inheriting the derived
12561 -- operation as a non-visible operation in cases where the parent
12562 -- subprogram might not be visible now, but was visible within the
12563 -- original generic, so it would be wrong to make the inherited
12564 -- subprogram non-visible now. (Not clear if this test is fully
12565 -- correct; are there any cases where we should declare the inherited
12566 -- operation as not visible to avoid it being overridden, e.g., when
12567 -- the parent type is a generic actual with private primitives ???)
12569 -- (they should be treated the same as other private inherited
12570 -- subprograms, but it's not clear how to do this cleanly). ???
12572 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
12573 and then Is_Immediately_Visible (Parent_Subp)
12574 and then not In_Instance)
12575 or else In_Instance_Not_Visible
12576 then
12577 Set_Derived_Name;
12579 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
12580 -- overrides an interface primitive because interface primitives
12581 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
12583 elsif Ada_Version >= Ada_2005
12584 and then Is_Dispatching_Operation (Parent_Subp)
12585 and then Covers_Some_Interface (Parent_Subp)
12586 then
12587 Set_Derived_Name;
12589 -- Otherwise, the type is inheriting a private operation, so enter
12590 -- it with a special name so it can't be overridden.
12592 else
12593 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
12594 end if;
12596 Set_Parent (New_Subp, Parent (Derived_Type));
12598 if Present (Actual_Subp) then
12599 Replace_Type (Actual_Subp, New_Subp);
12600 else
12601 Replace_Type (Parent_Subp, New_Subp);
12602 end if;
12604 Conditional_Delay (New_Subp, Parent_Subp);
12606 -- If we are creating a renaming for a primitive operation of an
12607 -- actual of a generic derived type, we must examine the signature
12608 -- of the actual primitive, not that of the generic formal, which for
12609 -- example may be an interface. However the name and initial value
12610 -- of the inherited operation are those of the formal primitive.
12612 Formal := First_Formal (Parent_Subp);
12614 if Present (Actual_Subp) then
12615 Formal_Of_Actual := First_Formal (Actual_Subp);
12616 else
12617 Formal_Of_Actual := Empty;
12618 end if;
12620 while Present (Formal) loop
12621 New_Formal := New_Copy (Formal);
12623 -- Normally we do not go copying parents, but in the case of
12624 -- formals, we need to link up to the declaration (which is the
12625 -- parameter specification), and it is fine to link up to the
12626 -- original formal's parameter specification in this case.
12628 Set_Parent (New_Formal, Parent (Formal));
12629 Append_Entity (New_Formal, New_Subp);
12631 if Present (Formal_Of_Actual) then
12632 Replace_Type (Formal_Of_Actual, New_Formal);
12633 Next_Formal (Formal_Of_Actual);
12634 else
12635 Replace_Type (Formal, New_Formal);
12636 end if;
12638 Next_Formal (Formal);
12639 end loop;
12641 -- If this derivation corresponds to a tagged generic actual, then
12642 -- primitive operations rename those of the actual. Otherwise the
12643 -- primitive operations rename those of the parent type, If the parent
12644 -- renames an intrinsic operator, so does the new subprogram. We except
12645 -- concatenation, which is always properly typed, and does not get
12646 -- expanded as other intrinsic operations.
12648 if No (Actual_Subp) then
12649 if Is_Intrinsic_Subprogram (Parent_Subp) then
12650 Set_Is_Intrinsic_Subprogram (New_Subp);
12652 if Present (Alias (Parent_Subp))
12653 and then Chars (Parent_Subp) /= Name_Op_Concat
12654 then
12655 Set_Alias (New_Subp, Alias (Parent_Subp));
12656 else
12657 Set_Alias (New_Subp, Parent_Subp);
12658 end if;
12660 else
12661 Set_Alias (New_Subp, Parent_Subp);
12662 end if;
12664 else
12665 Set_Alias (New_Subp, Actual_Subp);
12666 end if;
12668 -- Derived subprograms of a tagged type must inherit the convention
12669 -- of the parent subprogram (a requirement of AI-117). Derived
12670 -- subprograms of untagged types simply get convention Ada by default.
12672 if Is_Tagged_Type (Derived_Type) then
12673 Set_Convention (New_Subp, Convention (Parent_Subp));
12674 end if;
12676 -- Predefined controlled operations retain their name even if the parent
12677 -- is hidden (see above), but they are not primitive operations if the
12678 -- ancestor is not visible, for example if the parent is a private
12679 -- extension completed with a controlled extension. Note that a full
12680 -- type that is controlled can break privacy: the flag Is_Controlled is
12681 -- set on both views of the type.
12683 if Is_Controlled (Parent_Type)
12684 and then
12685 (Chars (Parent_Subp) = Name_Initialize
12686 or else Chars (Parent_Subp) = Name_Adjust
12687 or else Chars (Parent_Subp) = Name_Finalize)
12688 and then Is_Hidden (Parent_Subp)
12689 and then not Is_Visibly_Controlled (Parent_Type)
12690 then
12691 Set_Is_Hidden (New_Subp);
12692 end if;
12694 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
12695 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
12697 if Ekind (Parent_Subp) = E_Procedure then
12698 Set_Is_Valued_Procedure
12699 (New_Subp, Is_Valued_Procedure (Parent_Subp));
12700 else
12701 Set_Has_Controlling_Result
12702 (New_Subp, Has_Controlling_Result (Parent_Subp));
12703 end if;
12705 -- No_Return must be inherited properly. If this is overridden in the
12706 -- case of a dispatching operation, then a check is made in Sem_Disp
12707 -- that the overriding operation is also No_Return (no such check is
12708 -- required for the case of non-dispatching operation.
12710 Set_No_Return (New_Subp, No_Return (Parent_Subp));
12712 -- A derived function with a controlling result is abstract. If the
12713 -- Derived_Type is a nonabstract formal generic derived type, then
12714 -- inherited operations are not abstract: the required check is done at
12715 -- instantiation time. If the derivation is for a generic actual, the
12716 -- function is not abstract unless the actual is.
12718 if Is_Generic_Type (Derived_Type)
12719 and then not Is_Abstract_Type (Derived_Type)
12720 then
12721 null;
12723 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
12724 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
12726 elsif Ada_Version >= Ada_2005
12727 and then (Is_Abstract_Subprogram (Alias (New_Subp))
12728 or else (Is_Tagged_Type (Derived_Type)
12729 and then Etype (New_Subp) = Derived_Type
12730 and then not Is_Null_Extension (Derived_Type))
12731 or else (Is_Tagged_Type (Derived_Type)
12732 and then Ekind (Etype (New_Subp)) =
12733 E_Anonymous_Access_Type
12734 and then Designated_Type (Etype (New_Subp)) =
12735 Derived_Type
12736 and then not Is_Null_Extension (Derived_Type)))
12737 and then No (Actual_Subp)
12738 then
12739 if not Is_Tagged_Type (Derived_Type)
12740 or else Is_Abstract_Type (Derived_Type)
12741 or else Is_Abstract_Subprogram (Alias (New_Subp))
12742 then
12743 Set_Is_Abstract_Subprogram (New_Subp);
12744 else
12745 Set_Requires_Overriding (New_Subp);
12746 end if;
12748 elsif Ada_Version < Ada_2005
12749 and then (Is_Abstract_Subprogram (Alias (New_Subp))
12750 or else (Is_Tagged_Type (Derived_Type)
12751 and then Etype (New_Subp) = Derived_Type
12752 and then No (Actual_Subp)))
12753 then
12754 Set_Is_Abstract_Subprogram (New_Subp);
12756 -- AI05-0097 : an inherited operation that dispatches on result is
12757 -- abstract if the derived type is abstract, even if the parent type
12758 -- is concrete and the derived type is a null extension.
12760 elsif Has_Controlling_Result (Alias (New_Subp))
12761 and then Is_Abstract_Type (Etype (New_Subp))
12762 then
12763 Set_Is_Abstract_Subprogram (New_Subp);
12765 -- Finally, if the parent type is abstract we must verify that all
12766 -- inherited operations are either non-abstract or overridden, or that
12767 -- the derived type itself is abstract (this check is performed at the
12768 -- end of a package declaration, in Check_Abstract_Overriding). A
12769 -- private overriding in the parent type will not be visible in the
12770 -- derivation if we are not in an inner package or in a child unit of
12771 -- the parent type, in which case the abstractness of the inherited
12772 -- operation is carried to the new subprogram.
12774 elsif Is_Abstract_Type (Parent_Type)
12775 and then not In_Open_Scopes (Scope (Parent_Type))
12776 and then Is_Private_Overriding
12777 and then Is_Abstract_Subprogram (Visible_Subp)
12778 then
12779 if No (Actual_Subp) then
12780 Set_Alias (New_Subp, Visible_Subp);
12781 Set_Is_Abstract_Subprogram (New_Subp, True);
12783 else
12784 -- If this is a derivation for an instance of a formal derived
12785 -- type, abstractness comes from the primitive operation of the
12786 -- actual, not from the operation inherited from the ancestor.
12788 Set_Is_Abstract_Subprogram
12789 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
12790 end if;
12791 end if;
12793 New_Overloaded_Entity (New_Subp, Derived_Type);
12795 -- Check for case of a derived subprogram for the instantiation of a
12796 -- formal derived tagged type, if so mark the subprogram as dispatching
12797 -- and inherit the dispatching attributes of the parent subprogram. The
12798 -- derived subprogram is effectively renaming of the actual subprogram,
12799 -- so it needs to have the same attributes as the actual.
12801 if Present (Actual_Subp)
12802 and then Is_Dispatching_Operation (Parent_Subp)
12803 then
12804 Set_Is_Dispatching_Operation (New_Subp);
12806 if Present (DTC_Entity (Parent_Subp)) then
12807 Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
12808 Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
12809 end if;
12810 end if;
12812 -- Indicate that a derived subprogram does not require a body and that
12813 -- it does not require processing of default expressions.
12815 Set_Has_Completion (New_Subp);
12816 Set_Default_Expressions_Processed (New_Subp);
12818 if Ekind (New_Subp) = E_Function then
12819 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
12820 end if;
12821 end Derive_Subprogram;
12823 ------------------------
12824 -- Derive_Subprograms --
12825 ------------------------
12827 procedure Derive_Subprograms
12828 (Parent_Type : Entity_Id;
12829 Derived_Type : Entity_Id;
12830 Generic_Actual : Entity_Id := Empty)
12832 Op_List : constant Elist_Id :=
12833 Collect_Primitive_Operations (Parent_Type);
12835 function Check_Derived_Type return Boolean;
12836 -- Check that all primitive inherited from Parent_Type are found in
12837 -- the list of primitives of Derived_Type exactly in the same order.
12839 function Check_Derived_Type return Boolean is
12840 E : Entity_Id;
12841 Elmt : Elmt_Id;
12842 List : Elist_Id;
12843 New_Subp : Entity_Id;
12844 Op_Elmt : Elmt_Id;
12845 Subp : Entity_Id;
12847 begin
12848 -- Traverse list of entities in the current scope searching for
12849 -- an incomplete type whose full-view is derived type
12851 E := First_Entity (Scope (Derived_Type));
12852 while Present (E)
12853 and then E /= Derived_Type
12854 loop
12855 if Ekind (E) = E_Incomplete_Type
12856 and then Present (Full_View (E))
12857 and then Full_View (E) = Derived_Type
12858 then
12859 -- Disable this test if Derived_Type completes an incomplete
12860 -- type because in such case more primitives can be added
12861 -- later to the list of primitives of Derived_Type by routine
12862 -- Process_Incomplete_Dependents
12864 return True;
12865 end if;
12867 E := Next_Entity (E);
12868 end loop;
12870 List := Collect_Primitive_Operations (Derived_Type);
12871 Elmt := First_Elmt (List);
12873 Op_Elmt := First_Elmt (Op_List);
12874 while Present (Op_Elmt) loop
12875 Subp := Node (Op_Elmt);
12876 New_Subp := Node (Elmt);
12878 -- At this early stage Derived_Type has no entities with attribute
12879 -- Interface_Alias. In addition, such primitives are always
12880 -- located at the end of the list of primitives of Parent_Type.
12881 -- Therefore, if found we can safely stop processing pending
12882 -- entities.
12884 exit when Present (Interface_Alias (Subp));
12886 -- Handle hidden entities
12888 if not Is_Predefined_Dispatching_Operation (Subp)
12889 and then Is_Hidden (Subp)
12890 then
12891 if Present (New_Subp)
12892 and then Primitive_Names_Match (Subp, New_Subp)
12893 then
12894 Next_Elmt (Elmt);
12895 end if;
12897 else
12898 if not Present (New_Subp)
12899 or else Ekind (Subp) /= Ekind (New_Subp)
12900 or else not Primitive_Names_Match (Subp, New_Subp)
12901 then
12902 return False;
12903 end if;
12905 Next_Elmt (Elmt);
12906 end if;
12908 Next_Elmt (Op_Elmt);
12909 end loop;
12911 return True;
12912 end Check_Derived_Type;
12914 -- Local variables
12916 Alias_Subp : Entity_Id;
12917 Act_List : Elist_Id;
12918 Act_Elmt : Elmt_Id := No_Elmt;
12919 Act_Subp : Entity_Id := Empty;
12920 Elmt : Elmt_Id;
12921 Need_Search : Boolean := False;
12922 New_Subp : Entity_Id := Empty;
12923 Parent_Base : Entity_Id;
12924 Subp : Entity_Id;
12926 -- Start of processing for Derive_Subprograms
12928 begin
12929 if Ekind (Parent_Type) = E_Record_Type_With_Private
12930 and then Has_Discriminants (Parent_Type)
12931 and then Present (Full_View (Parent_Type))
12932 then
12933 Parent_Base := Full_View (Parent_Type);
12934 else
12935 Parent_Base := Parent_Type;
12936 end if;
12938 if Present (Generic_Actual) then
12939 Act_List := Collect_Primitive_Operations (Generic_Actual);
12940 Act_Elmt := First_Elmt (Act_List);
12941 end if;
12943 -- Derive primitives inherited from the parent. Note that if the generic
12944 -- actual is present, this is not really a type derivation, it is a
12945 -- completion within an instance.
12947 -- Case 1: Derived_Type does not implement interfaces
12949 if not Is_Tagged_Type (Derived_Type)
12950 or else (not Has_Interfaces (Derived_Type)
12951 and then not (Present (Generic_Actual)
12952 and then
12953 Has_Interfaces (Generic_Actual)))
12954 then
12955 Elmt := First_Elmt (Op_List);
12956 while Present (Elmt) loop
12957 Subp := Node (Elmt);
12959 -- Literals are derived earlier in the process of building the
12960 -- derived type, and are skipped here.
12962 if Ekind (Subp) = E_Enumeration_Literal then
12963 null;
12965 -- The actual is a direct descendant and the common primitive
12966 -- operations appear in the same order.
12968 -- If the generic parent type is present, the derived type is an
12969 -- instance of a formal derived type, and within the instance its
12970 -- operations are those of the actual. We derive from the formal
12971 -- type but make the inherited operations aliases of the
12972 -- corresponding operations of the actual.
12974 else
12975 pragma Assert (No (Node (Act_Elmt))
12976 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
12977 and then
12978 Type_Conformant (Subp, Node (Act_Elmt),
12979 Skip_Controlling_Formals => True)));
12981 Derive_Subprogram
12982 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
12984 if Present (Act_Elmt) then
12985 Next_Elmt (Act_Elmt);
12986 end if;
12987 end if;
12989 Next_Elmt (Elmt);
12990 end loop;
12992 -- Case 2: Derived_Type implements interfaces
12994 else
12995 -- If the parent type has no predefined primitives we remove
12996 -- predefined primitives from the list of primitives of generic
12997 -- actual to simplify the complexity of this algorithm.
12999 if Present (Generic_Actual) then
13000 declare
13001 Has_Predefined_Primitives : Boolean := False;
13003 begin
13004 -- Check if the parent type has predefined primitives
13006 Elmt := First_Elmt (Op_List);
13007 while Present (Elmt) loop
13008 Subp := Node (Elmt);
13010 if Is_Predefined_Dispatching_Operation (Subp)
13011 and then not Comes_From_Source (Ultimate_Alias (Subp))
13012 then
13013 Has_Predefined_Primitives := True;
13014 exit;
13015 end if;
13017 Next_Elmt (Elmt);
13018 end loop;
13020 -- Remove predefined primitives of Generic_Actual. We must use
13021 -- an auxiliary list because in case of tagged types the value
13022 -- returned by Collect_Primitive_Operations is the value stored
13023 -- in its Primitive_Operations attribute (and we don't want to
13024 -- modify its current contents).
13026 if not Has_Predefined_Primitives then
13027 declare
13028 Aux_List : constant Elist_Id := New_Elmt_List;
13030 begin
13031 Elmt := First_Elmt (Act_List);
13032 while Present (Elmt) loop
13033 Subp := Node (Elmt);
13035 if not Is_Predefined_Dispatching_Operation (Subp)
13036 or else Comes_From_Source (Subp)
13037 then
13038 Append_Elmt (Subp, Aux_List);
13039 end if;
13041 Next_Elmt (Elmt);
13042 end loop;
13044 Act_List := Aux_List;
13045 end;
13046 end if;
13048 Act_Elmt := First_Elmt (Act_List);
13049 Act_Subp := Node (Act_Elmt);
13050 end;
13051 end if;
13053 -- Stage 1: If the generic actual is not present we derive the
13054 -- primitives inherited from the parent type. If the generic parent
13055 -- type is present, the derived type is an instance of a formal
13056 -- derived type, and within the instance its operations are those of
13057 -- the actual. We derive from the formal type but make the inherited
13058 -- operations aliases of the corresponding operations of the actual.
13060 Elmt := First_Elmt (Op_List);
13061 while Present (Elmt) loop
13062 Subp := Node (Elmt);
13063 Alias_Subp := Ultimate_Alias (Subp);
13065 -- Do not derive internal entities of the parent that link
13066 -- interface primitives and its covering primitive. These
13067 -- entities will be added to this type when frozen.
13069 if Present (Interface_Alias (Subp)) then
13070 goto Continue;
13071 end if;
13073 -- If the generic actual is present find the corresponding
13074 -- operation in the generic actual. If the parent type is a
13075 -- direct ancestor of the derived type then, even if it is an
13076 -- interface, the operations are inherited from the primary
13077 -- dispatch table and are in the proper order. If we detect here
13078 -- that primitives are not in the same order we traverse the list
13079 -- of primitive operations of the actual to find the one that
13080 -- implements the interface primitive.
13082 if Need_Search
13083 or else
13084 (Present (Generic_Actual)
13085 and then Present (Act_Subp)
13086 and then not
13087 (Primitive_Names_Match (Subp, Act_Subp)
13088 and then
13089 Type_Conformant (Subp, Act_Subp,
13090 Skip_Controlling_Formals => True)))
13091 then
13092 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual));
13094 -- Remember that we need searching for all pending primitives
13096 Need_Search := True;
13098 -- Handle entities associated with interface primitives
13100 if Present (Alias_Subp)
13101 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13102 and then not Is_Predefined_Dispatching_Operation (Subp)
13103 then
13104 -- Search for the primitive in the homonym chain
13106 Act_Subp :=
13107 Find_Primitive_Covering_Interface
13108 (Tagged_Type => Generic_Actual,
13109 Iface_Prim => Alias_Subp);
13111 -- Previous search may not locate primitives covering
13112 -- interfaces defined in generics units or instantiations.
13113 -- (it fails if the covering primitive has formals whose
13114 -- type is also defined in generics or instantiations).
13115 -- In such case we search in the list of primitives of the
13116 -- generic actual for the internal entity that links the
13117 -- interface primitive and the covering primitive.
13119 if No (Act_Subp)
13120 and then Is_Generic_Type (Parent_Type)
13121 then
13122 -- This code has been designed to handle only generic
13123 -- formals that implement interfaces that are defined
13124 -- in a generic unit or instantiation. If this code is
13125 -- needed for other cases we must review it because
13126 -- (given that it relies on Original_Location to locate
13127 -- the primitive of Generic_Actual that covers the
13128 -- interface) it could leave linked through attribute
13129 -- Alias entities of unrelated instantiations).
13131 pragma Assert
13132 (Is_Generic_Unit
13133 (Scope (Find_Dispatching_Type (Alias_Subp)))
13134 or else
13135 Instantiation_Depth
13136 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
13138 declare
13139 Iface_Prim_Loc : constant Source_Ptr :=
13140 Original_Location (Sloc (Alias_Subp));
13141 Elmt : Elmt_Id;
13142 Prim : Entity_Id;
13143 begin
13144 Elmt :=
13145 First_Elmt (Primitive_Operations (Generic_Actual));
13147 Search : while Present (Elmt) loop
13148 Prim := Node (Elmt);
13150 if Present (Interface_Alias (Prim))
13151 and then Original_Location
13152 (Sloc (Interface_Alias (Prim)))
13153 = Iface_Prim_Loc
13154 then
13155 Act_Subp := Alias (Prim);
13156 exit Search;
13157 end if;
13159 Next_Elmt (Elmt);
13160 end loop Search;
13161 end;
13162 end if;
13164 pragma Assert (Present (Act_Subp)
13165 or else Is_Abstract_Type (Generic_Actual)
13166 or else Serious_Errors_Detected > 0);
13168 -- Handle predefined primitives plus the rest of user-defined
13169 -- primitives
13171 else
13172 Act_Elmt := First_Elmt (Act_List);
13173 while Present (Act_Elmt) loop
13174 Act_Subp := Node (Act_Elmt);
13176 exit when Primitive_Names_Match (Subp, Act_Subp)
13177 and then Type_Conformant
13178 (Subp, Act_Subp,
13179 Skip_Controlling_Formals => True)
13180 and then No (Interface_Alias (Act_Subp));
13182 Next_Elmt (Act_Elmt);
13183 end loop;
13185 if No (Act_Elmt) then
13186 Act_Subp := Empty;
13187 end if;
13188 end if;
13189 end if;
13191 -- Case 1: If the parent is a limited interface then it has the
13192 -- predefined primitives of synchronized interfaces. However, the
13193 -- actual type may be a non-limited type and hence it does not
13194 -- have such primitives.
13196 if Present (Generic_Actual)
13197 and then not Present (Act_Subp)
13198 and then Is_Limited_Interface (Parent_Base)
13199 and then Is_Predefined_Interface_Primitive (Subp)
13200 then
13201 null;
13203 -- Case 2: Inherit entities associated with interfaces that were
13204 -- not covered by the parent type. We exclude here null interface
13205 -- primitives because they do not need special management.
13207 -- We also exclude interface operations that are renamings. If the
13208 -- subprogram is an explicit renaming of an interface primitive,
13209 -- it is a regular primitive operation, and the presence of its
13210 -- alias is not relevant: it has to be derived like any other
13211 -- primitive.
13213 elsif Present (Alias (Subp))
13214 and then Nkind (Unit_Declaration_Node (Subp)) /=
13215 N_Subprogram_Renaming_Declaration
13216 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13217 and then not
13218 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
13219 and then Null_Present (Parent (Alias_Subp)))
13220 then
13221 Derive_Subprogram
13222 (New_Subp => New_Subp,
13223 Parent_Subp => Alias_Subp,
13224 Derived_Type => Derived_Type,
13225 Parent_Type => Find_Dispatching_Type (Alias_Subp),
13226 Actual_Subp => Act_Subp);
13228 if No (Generic_Actual) then
13229 Set_Alias (New_Subp, Subp);
13230 end if;
13232 -- Case 3: Common derivation
13234 else
13235 Derive_Subprogram
13236 (New_Subp => New_Subp,
13237 Parent_Subp => Subp,
13238 Derived_Type => Derived_Type,
13239 Parent_Type => Parent_Base,
13240 Actual_Subp => Act_Subp);
13241 end if;
13243 -- No need to update Act_Elm if we must search for the
13244 -- corresponding operation in the generic actual
13246 if not Need_Search
13247 and then Present (Act_Elmt)
13248 then
13249 Next_Elmt (Act_Elmt);
13250 Act_Subp := Node (Act_Elmt);
13251 end if;
13253 <<Continue>>
13254 Next_Elmt (Elmt);
13255 end loop;
13257 -- Inherit additional operations from progenitors. If the derived
13258 -- type is a generic actual, there are not new primitive operations
13259 -- for the type because it has those of the actual, and therefore
13260 -- nothing needs to be done. The renamings generated above are not
13261 -- primitive operations, and their purpose is simply to make the
13262 -- proper operations visible within an instantiation.
13264 if No (Generic_Actual) then
13265 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
13266 end if;
13267 end if;
13269 -- Final check: Direct descendants must have their primitives in the
13270 -- same order. We exclude from this test untagged types and instances
13271 -- of formal derived types. We skip this test if we have already
13272 -- reported serious errors in the sources.
13274 pragma Assert (not Is_Tagged_Type (Derived_Type)
13275 or else Present (Generic_Actual)
13276 or else Serious_Errors_Detected > 0
13277 or else Check_Derived_Type);
13278 end Derive_Subprograms;
13280 --------------------------------
13281 -- Derived_Standard_Character --
13282 --------------------------------
13284 procedure Derived_Standard_Character
13285 (N : Node_Id;
13286 Parent_Type : Entity_Id;
13287 Derived_Type : Entity_Id)
13289 Loc : constant Source_Ptr := Sloc (N);
13290 Def : constant Node_Id := Type_Definition (N);
13291 Indic : constant Node_Id := Subtype_Indication (Def);
13292 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
13293 Implicit_Base : constant Entity_Id :=
13294 Create_Itype
13295 (E_Enumeration_Type, N, Derived_Type, 'B');
13297 Lo : Node_Id;
13298 Hi : Node_Id;
13300 begin
13301 Discard_Node (Process_Subtype (Indic, N));
13303 Set_Etype (Implicit_Base, Parent_Base);
13304 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
13305 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
13307 Set_Is_Character_Type (Implicit_Base, True);
13308 Set_Has_Delayed_Freeze (Implicit_Base);
13310 -- The bounds of the implicit base are the bounds of the parent base.
13311 -- Note that their type is the parent base.
13313 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
13314 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
13316 Set_Scalar_Range (Implicit_Base,
13317 Make_Range (Loc,
13318 Low_Bound => Lo,
13319 High_Bound => Hi));
13321 Conditional_Delay (Derived_Type, Parent_Type);
13323 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
13324 Set_Etype (Derived_Type, Implicit_Base);
13325 Set_Size_Info (Derived_Type, Parent_Type);
13327 if Unknown_RM_Size (Derived_Type) then
13328 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
13329 end if;
13331 Set_Is_Character_Type (Derived_Type, True);
13333 if Nkind (Indic) /= N_Subtype_Indication then
13335 -- If no explicit constraint, the bounds are those
13336 -- of the parent type.
13338 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
13339 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
13340 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
13341 end if;
13343 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
13345 -- Because the implicit base is used in the conversion of the bounds, we
13346 -- have to freeze it now. This is similar to what is done for numeric
13347 -- types, and it equally suspicious, but otherwise a non-static bound
13348 -- will have a reference to an unfrozen type, which is rejected by Gigi
13349 -- (???). This requires specific care for definition of stream
13350 -- attributes. For details, see comments at the end of
13351 -- Build_Derived_Numeric_Type.
13353 Freeze_Before (N, Implicit_Base);
13354 end Derived_Standard_Character;
13356 ------------------------------
13357 -- Derived_Type_Declaration --
13358 ------------------------------
13360 procedure Derived_Type_Declaration
13361 (T : Entity_Id;
13362 N : Node_Id;
13363 Is_Completion : Boolean)
13365 Parent_Type : Entity_Id;
13367 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
13368 -- Check whether the parent type is a generic formal, or derives
13369 -- directly or indirectly from one.
13371 ------------------------
13372 -- Comes_From_Generic --
13373 ------------------------
13375 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
13376 begin
13377 if Is_Generic_Type (Typ) then
13378 return True;
13380 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
13381 return True;
13383 elsif Is_Private_Type (Typ)
13384 and then Present (Full_View (Typ))
13385 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
13386 then
13387 return True;
13389 elsif Is_Generic_Actual_Type (Typ) then
13390 return True;
13392 else
13393 return False;
13394 end if;
13395 end Comes_From_Generic;
13397 -- Local variables
13399 Def : constant Node_Id := Type_Definition (N);
13400 Iface_Def : Node_Id;
13401 Indic : constant Node_Id := Subtype_Indication (Def);
13402 Extension : constant Node_Id := Record_Extension_Part (Def);
13403 Parent_Node : Node_Id;
13404 Parent_Scope : Entity_Id;
13405 Taggd : Boolean;
13407 -- Start of processing for Derived_Type_Declaration
13409 begin
13410 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
13412 -- Ada 2005 (AI-251): In case of interface derivation check that the
13413 -- parent is also an interface.
13415 if Interface_Present (Def) then
13416 if not Is_Interface (Parent_Type) then
13417 Diagnose_Interface (Indic, Parent_Type);
13419 else
13420 Parent_Node := Parent (Base_Type (Parent_Type));
13421 Iface_Def := Type_Definition (Parent_Node);
13423 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
13424 -- other limited interfaces.
13426 if Limited_Present (Def) then
13427 if Limited_Present (Iface_Def) then
13428 null;
13430 elsif Protected_Present (Iface_Def) then
13431 Error_Msg_NE
13432 ("descendant of& must be declared"
13433 & " as a protected interface",
13434 N, Parent_Type);
13436 elsif Synchronized_Present (Iface_Def) then
13437 Error_Msg_NE
13438 ("descendant of& must be declared"
13439 & " as a synchronized interface",
13440 N, Parent_Type);
13442 elsif Task_Present (Iface_Def) then
13443 Error_Msg_NE
13444 ("descendant of& must be declared as a task interface",
13445 N, Parent_Type);
13447 else
13448 Error_Msg_N
13449 ("(Ada 2005) limited interface cannot "
13450 & "inherit from non-limited interface", Indic);
13451 end if;
13453 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
13454 -- from non-limited or limited interfaces.
13456 elsif not Protected_Present (Def)
13457 and then not Synchronized_Present (Def)
13458 and then not Task_Present (Def)
13459 then
13460 if Limited_Present (Iface_Def) then
13461 null;
13463 elsif Protected_Present (Iface_Def) then
13464 Error_Msg_NE
13465 ("descendant of& must be declared"
13466 & " as a protected interface",
13467 N, Parent_Type);
13469 elsif Synchronized_Present (Iface_Def) then
13470 Error_Msg_NE
13471 ("descendant of& must be declared"
13472 & " as a synchronized interface",
13473 N, Parent_Type);
13475 elsif Task_Present (Iface_Def) then
13476 Error_Msg_NE
13477 ("descendant of& must be declared as a task interface",
13478 N, Parent_Type);
13479 else
13480 null;
13481 end if;
13482 end if;
13483 end if;
13484 end if;
13486 if Is_Tagged_Type (Parent_Type)
13487 and then Is_Concurrent_Type (Parent_Type)
13488 and then not Is_Interface (Parent_Type)
13489 then
13490 Error_Msg_N
13491 ("parent type of a record extension cannot be "
13492 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
13493 Set_Etype (T, Any_Type);
13494 return;
13495 end if;
13497 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
13498 -- interfaces
13500 if Is_Tagged_Type (Parent_Type)
13501 and then Is_Non_Empty_List (Interface_List (Def))
13502 then
13503 declare
13504 Intf : Node_Id;
13505 T : Entity_Id;
13507 begin
13508 Intf := First (Interface_List (Def));
13509 while Present (Intf) loop
13510 T := Find_Type_Of_Subtype_Indic (Intf);
13512 if not Is_Interface (T) then
13513 Diagnose_Interface (Intf, T);
13515 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
13516 -- a limited type from having a nonlimited progenitor.
13518 elsif (Limited_Present (Def)
13519 or else (not Is_Interface (Parent_Type)
13520 and then Is_Limited_Type (Parent_Type)))
13521 and then not Is_Limited_Interface (T)
13522 then
13523 Error_Msg_NE
13524 ("progenitor interface& of limited type must be limited",
13525 N, T);
13526 end if;
13528 Next (Intf);
13529 end loop;
13530 end;
13531 end if;
13533 if Parent_Type = Any_Type
13534 or else Etype (Parent_Type) = Any_Type
13535 or else (Is_Class_Wide_Type (Parent_Type)
13536 and then Etype (Parent_Type) = T)
13537 then
13538 -- If Parent_Type is undefined or illegal, make new type into a
13539 -- subtype of Any_Type, and set a few attributes to prevent cascaded
13540 -- errors. If this is a self-definition, emit error now.
13542 if T = Parent_Type
13543 or else T = Etype (Parent_Type)
13544 then
13545 Error_Msg_N ("type cannot be used in its own definition", Indic);
13546 end if;
13548 Set_Ekind (T, Ekind (Parent_Type));
13549 Set_Etype (T, Any_Type);
13550 Set_Scalar_Range (T, Scalar_Range (Any_Type));
13552 if Is_Tagged_Type (T)
13553 and then Is_Record_Type (T)
13554 then
13555 Set_Direct_Primitive_Operations (T, New_Elmt_List);
13556 end if;
13558 return;
13559 end if;
13561 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
13562 -- an interface is special because the list of interfaces in the full
13563 -- view can be given in any order. For example:
13565 -- type A is interface;
13566 -- type B is interface and A;
13567 -- type D is new B with private;
13568 -- private
13569 -- type D is new A and B with null record; -- 1 --
13571 -- In this case we perform the following transformation of -1-:
13573 -- type D is new B and A with null record;
13575 -- If the parent of the full-view covers the parent of the partial-view
13576 -- we have two possible cases:
13578 -- 1) They have the same parent
13579 -- 2) The parent of the full-view implements some further interfaces
13581 -- In both cases we do not need to perform the transformation. In the
13582 -- first case the source program is correct and the transformation is
13583 -- not needed; in the second case the source program does not fulfill
13584 -- the no-hidden interfaces rule (AI-396) and the error will be reported
13585 -- later.
13587 -- This transformation not only simplifies the rest of the analysis of
13588 -- this type declaration but also simplifies the correct generation of
13589 -- the object layout to the expander.
13591 if In_Private_Part (Current_Scope)
13592 and then Is_Interface (Parent_Type)
13593 then
13594 declare
13595 Iface : Node_Id;
13596 Partial_View : Entity_Id;
13597 Partial_View_Parent : Entity_Id;
13598 New_Iface : Node_Id;
13600 begin
13601 -- Look for the associated private type declaration
13603 Partial_View := First_Entity (Current_Scope);
13604 loop
13605 exit when No (Partial_View)
13606 or else (Has_Private_Declaration (Partial_View)
13607 and then Full_View (Partial_View) = T);
13609 Next_Entity (Partial_View);
13610 end loop;
13612 -- If the partial view was not found then the source code has
13613 -- errors and the transformation is not needed.
13615 if Present (Partial_View) then
13616 Partial_View_Parent := Etype (Partial_View);
13618 -- If the parent of the full-view covers the parent of the
13619 -- partial-view we have nothing else to do.
13621 if Interface_Present_In_Ancestor
13622 (Parent_Type, Partial_View_Parent)
13623 then
13624 null;
13626 -- Traverse the list of interfaces of the full-view to look
13627 -- for the parent of the partial-view and perform the tree
13628 -- transformation.
13630 else
13631 Iface := First (Interface_List (Def));
13632 while Present (Iface) loop
13633 if Etype (Iface) = Etype (Partial_View) then
13634 Rewrite (Subtype_Indication (Def),
13635 New_Copy (Subtype_Indication
13636 (Parent (Partial_View))));
13638 New_Iface := Make_Identifier (Sloc (N),
13639 Chars (Parent_Type));
13640 Append (New_Iface, Interface_List (Def));
13642 -- Analyze the transformed code
13644 Derived_Type_Declaration (T, N, Is_Completion);
13645 return;
13646 end if;
13648 Next (Iface);
13649 end loop;
13650 end if;
13651 end if;
13652 end;
13653 end if;
13655 -- Only composite types other than array types are allowed to have
13656 -- discriminants.
13658 if Present (Discriminant_Specifications (N))
13659 and then (Is_Elementary_Type (Parent_Type)
13660 or else Is_Array_Type (Parent_Type))
13661 and then not Error_Posted (N)
13662 then
13663 Error_Msg_N
13664 ("elementary or array type cannot have discriminants",
13665 Defining_Identifier (First (Discriminant_Specifications (N))));
13666 Set_Has_Discriminants (T, False);
13667 end if;
13669 -- In Ada 83, a derived type defined in a package specification cannot
13670 -- be used for further derivation until the end of its visible part.
13671 -- Note that derivation in the private part of the package is allowed.
13673 if Ada_Version = Ada_83
13674 and then Is_Derived_Type (Parent_Type)
13675 and then In_Visible_Part (Scope (Parent_Type))
13676 then
13677 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
13678 Error_Msg_N
13679 ("(Ada 83): premature use of type for derivation", Indic);
13680 end if;
13681 end if;
13683 -- Check for early use of incomplete or private type
13685 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
13686 Error_Msg_N ("premature derivation of incomplete type", Indic);
13687 return;
13689 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
13690 and then not Comes_From_Generic (Parent_Type))
13691 or else Has_Private_Component (Parent_Type)
13692 then
13693 -- The ancestor type of a formal type can be incomplete, in which
13694 -- case only the operations of the partial view are available in
13695 -- the generic. Subsequent checks may be required when the full
13696 -- view is analyzed, to verify that derivation from a tagged type
13697 -- has an extension.
13699 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
13700 null;
13702 elsif No (Underlying_Type (Parent_Type))
13703 or else Has_Private_Component (Parent_Type)
13704 then
13705 Error_Msg_N
13706 ("premature derivation of derived or private type", Indic);
13708 -- Flag the type itself as being in error, this prevents some
13709 -- nasty problems with subsequent uses of the malformed type.
13711 Set_Error_Posted (T);
13713 -- Check that within the immediate scope of an untagged partial
13714 -- view it's illegal to derive from the partial view if the
13715 -- full view is tagged. (7.3(7))
13717 -- We verify that the Parent_Type is a partial view by checking
13718 -- that it is not a Full_Type_Declaration (i.e. a private type or
13719 -- private extension declaration), to distinguish a partial view
13720 -- from a derivation from a private type which also appears as
13721 -- E_Private_Type.
13723 elsif Present (Full_View (Parent_Type))
13724 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
13725 and then not Is_Tagged_Type (Parent_Type)
13726 and then Is_Tagged_Type (Full_View (Parent_Type))
13727 then
13728 Parent_Scope := Scope (T);
13729 while Present (Parent_Scope)
13730 and then Parent_Scope /= Standard_Standard
13731 loop
13732 if Parent_Scope = Scope (Parent_Type) then
13733 Error_Msg_N
13734 ("premature derivation from type with tagged full view",
13735 Indic);
13736 end if;
13738 Parent_Scope := Scope (Parent_Scope);
13739 end loop;
13740 end if;
13741 end if;
13743 -- Check that form of derivation is appropriate
13745 Taggd := Is_Tagged_Type (Parent_Type);
13747 -- Perhaps the parent type should be changed to the class-wide type's
13748 -- specific type in this case to prevent cascading errors ???
13750 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
13751 Error_Msg_N ("parent type must not be a class-wide type", Indic);
13752 return;
13753 end if;
13755 if Present (Extension) and then not Taggd then
13756 Error_Msg_N
13757 ("type derived from untagged type cannot have extension", Indic);
13759 elsif No (Extension) and then Taggd then
13761 -- If this declaration is within a private part (or body) of a
13762 -- generic instantiation then the derivation is allowed (the parent
13763 -- type can only appear tagged in this case if it's a generic actual
13764 -- type, since it would otherwise have been rejected in the analysis
13765 -- of the generic template).
13767 if not Is_Generic_Actual_Type (Parent_Type)
13768 or else In_Visible_Part (Scope (Parent_Type))
13769 then
13770 if Is_Class_Wide_Type (Parent_Type) then
13771 Error_Msg_N
13772 ("parent type must not be a class-wide type", Indic);
13774 -- Use specific type to prevent cascaded errors.
13776 Parent_Type := Etype (Parent_Type);
13778 else
13779 Error_Msg_N
13780 ("type derived from tagged type must have extension", Indic);
13781 end if;
13782 end if;
13783 end if;
13785 -- AI-443: Synchronized formal derived types require a private
13786 -- extension. There is no point in checking the ancestor type or
13787 -- the progenitors since the construct is wrong to begin with.
13789 if Ada_Version >= Ada_2005
13790 and then Is_Generic_Type (T)
13791 and then Present (Original_Node (N))
13792 then
13793 declare
13794 Decl : constant Node_Id := Original_Node (N);
13796 begin
13797 if Nkind (Decl) = N_Formal_Type_Declaration
13798 and then Nkind (Formal_Type_Definition (Decl)) =
13799 N_Formal_Derived_Type_Definition
13800 and then Synchronized_Present (Formal_Type_Definition (Decl))
13801 and then No (Extension)
13803 -- Avoid emitting a duplicate error message
13805 and then not Error_Posted (Indic)
13806 then
13807 Error_Msg_N
13808 ("synchronized derived type must have extension", N);
13809 end if;
13810 end;
13811 end if;
13813 if Null_Exclusion_Present (Def)
13814 and then not Is_Access_Type (Parent_Type)
13815 then
13816 Error_Msg_N ("null exclusion can only apply to an access type", N);
13817 end if;
13819 -- Avoid deriving parent primitives of underlying record views
13821 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
13822 Derive_Subps => not Is_Underlying_Record_View (T));
13824 -- AI-419: The parent type of an explicitly limited derived type must
13825 -- be a limited type or a limited interface.
13827 if Limited_Present (Def) then
13828 Set_Is_Limited_Record (T);
13830 if Is_Interface (T) then
13831 Set_Is_Limited_Interface (T);
13832 end if;
13834 if not Is_Limited_Type (Parent_Type)
13835 and then
13836 (not Is_Interface (Parent_Type)
13837 or else not Is_Limited_Interface (Parent_Type))
13838 then
13839 -- AI05-0096: a derivation in the private part of an instance is
13840 -- legal if the generic formal is untagged limited, and the actual
13841 -- is non-limited.
13843 if Is_Generic_Actual_Type (Parent_Type)
13844 and then In_Private_Part (Current_Scope)
13845 and then
13846 not Is_Tagged_Type
13847 (Generic_Parent_Type (Parent (Parent_Type)))
13848 then
13849 null;
13851 else
13852 Error_Msg_NE
13853 ("parent type& of limited type must be limited",
13854 N, Parent_Type);
13855 end if;
13856 end if;
13857 end if;
13858 end Derived_Type_Declaration;
13860 ------------------------
13861 -- Diagnose_Interface --
13862 ------------------------
13864 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
13865 begin
13866 if not Is_Interface (E)
13867 and then E /= Any_Type
13868 then
13869 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
13870 end if;
13871 end Diagnose_Interface;
13873 ----------------------------------
13874 -- Enumeration_Type_Declaration --
13875 ----------------------------------
13877 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13878 Ev : Uint;
13879 L : Node_Id;
13880 R_Node : Node_Id;
13881 B_Node : Node_Id;
13883 begin
13884 -- Create identifier node representing lower bound
13886 B_Node := New_Node (N_Identifier, Sloc (Def));
13887 L := First (Literals (Def));
13888 Set_Chars (B_Node, Chars (L));
13889 Set_Entity (B_Node, L);
13890 Set_Etype (B_Node, T);
13891 Set_Is_Static_Expression (B_Node, True);
13893 R_Node := New_Node (N_Range, Sloc (Def));
13894 Set_Low_Bound (R_Node, B_Node);
13896 Set_Ekind (T, E_Enumeration_Type);
13897 Set_First_Literal (T, L);
13898 Set_Etype (T, T);
13899 Set_Is_Constrained (T);
13901 Ev := Uint_0;
13903 -- Loop through literals of enumeration type setting pos and rep values
13904 -- except that if the Ekind is already set, then it means the literal
13905 -- was already constructed (case of a derived type declaration and we
13906 -- should not disturb the Pos and Rep values.
13908 while Present (L) loop
13909 if Ekind (L) /= E_Enumeration_Literal then
13910 Set_Ekind (L, E_Enumeration_Literal);
13911 Set_Enumeration_Pos (L, Ev);
13912 Set_Enumeration_Rep (L, Ev);
13913 Set_Is_Known_Valid (L, True);
13914 end if;
13916 Set_Etype (L, T);
13917 New_Overloaded_Entity (L);
13918 Generate_Definition (L);
13919 Set_Convention (L, Convention_Intrinsic);
13921 -- Case of character literal
13923 if Nkind (L) = N_Defining_Character_Literal then
13924 Set_Is_Character_Type (T, True);
13926 -- Check violation of No_Wide_Characters
13928 if Restriction_Check_Required (No_Wide_Characters) then
13929 Get_Name_String (Chars (L));
13931 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
13932 Check_Restriction (No_Wide_Characters, L);
13933 end if;
13934 end if;
13935 end if;
13937 Ev := Ev + 1;
13938 Next (L);
13939 end loop;
13941 -- Now create a node representing upper bound
13943 B_Node := New_Node (N_Identifier, Sloc (Def));
13944 Set_Chars (B_Node, Chars (Last (Literals (Def))));
13945 Set_Entity (B_Node, Last (Literals (Def)));
13946 Set_Etype (B_Node, T);
13947 Set_Is_Static_Expression (B_Node, True);
13949 Set_High_Bound (R_Node, B_Node);
13951 -- Initialize various fields of the type. Some of this information
13952 -- may be overwritten later through rep.clauses.
13954 Set_Scalar_Range (T, R_Node);
13955 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
13956 Set_Enum_Esize (T);
13957 Set_Enum_Pos_To_Rep (T, Empty);
13959 -- Set Discard_Names if configuration pragma set, or if there is
13960 -- a parameterless pragma in the current declarative region
13962 if Global_Discard_Names
13963 or else Discard_Names (Scope (T))
13964 then
13965 Set_Discard_Names (T);
13966 end if;
13968 -- Process end label if there is one
13970 if Present (Def) then
13971 Process_End_Label (Def, 'e', T);
13972 end if;
13973 end Enumeration_Type_Declaration;
13975 ---------------------------------
13976 -- Expand_To_Stored_Constraint --
13977 ---------------------------------
13979 function Expand_To_Stored_Constraint
13980 (Typ : Entity_Id;
13981 Constraint : Elist_Id) return Elist_Id
13983 Explicitly_Discriminated_Type : Entity_Id;
13984 Expansion : Elist_Id;
13985 Discriminant : Entity_Id;
13987 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
13988 -- Find the nearest type that actually specifies discriminants
13990 ---------------------------------
13991 -- Type_With_Explicit_Discrims --
13992 ---------------------------------
13994 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
13995 Typ : constant E := Base_Type (Id);
13997 begin
13998 if Ekind (Typ) in Incomplete_Or_Private_Kind then
13999 if Present (Full_View (Typ)) then
14000 return Type_With_Explicit_Discrims (Full_View (Typ));
14001 end if;
14003 else
14004 if Has_Discriminants (Typ) then
14005 return Typ;
14006 end if;
14007 end if;
14009 if Etype (Typ) = Typ then
14010 return Empty;
14011 elsif Has_Discriminants (Typ) then
14012 return Typ;
14013 else
14014 return Type_With_Explicit_Discrims (Etype (Typ));
14015 end if;
14017 end Type_With_Explicit_Discrims;
14019 -- Start of processing for Expand_To_Stored_Constraint
14021 begin
14022 if No (Constraint)
14023 or else Is_Empty_Elmt_List (Constraint)
14024 then
14025 return No_Elist;
14026 end if;
14028 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
14030 if No (Explicitly_Discriminated_Type) then
14031 return No_Elist;
14032 end if;
14034 Expansion := New_Elmt_List;
14036 Discriminant :=
14037 First_Stored_Discriminant (Explicitly_Discriminated_Type);
14038 while Present (Discriminant) loop
14039 Append_Elmt (
14040 Get_Discriminant_Value (
14041 Discriminant, Explicitly_Discriminated_Type, Constraint),
14042 Expansion);
14043 Next_Stored_Discriminant (Discriminant);
14044 end loop;
14046 return Expansion;
14047 end Expand_To_Stored_Constraint;
14049 ---------------------------
14050 -- Find_Hidden_Interface --
14051 ---------------------------
14053 function Find_Hidden_Interface
14054 (Src : Elist_Id;
14055 Dest : Elist_Id) return Entity_Id
14057 Iface : Entity_Id;
14058 Iface_Elmt : Elmt_Id;
14060 begin
14061 if Present (Src) and then Present (Dest) then
14062 Iface_Elmt := First_Elmt (Src);
14063 while Present (Iface_Elmt) loop
14064 Iface := Node (Iface_Elmt);
14066 if Is_Interface (Iface)
14067 and then not Contain_Interface (Iface, Dest)
14068 then
14069 return Iface;
14070 end if;
14072 Next_Elmt (Iface_Elmt);
14073 end loop;
14074 end if;
14076 return Empty;
14077 end Find_Hidden_Interface;
14079 --------------------
14080 -- Find_Type_Name --
14081 --------------------
14083 function Find_Type_Name (N : Node_Id) return Entity_Id is
14084 Id : constant Entity_Id := Defining_Identifier (N);
14085 Prev : Entity_Id;
14086 New_Id : Entity_Id;
14087 Prev_Par : Node_Id;
14089 procedure Tag_Mismatch;
14090 -- Diagnose a tagged partial view whose full view is untagged.
14091 -- We post the message on the full view, with a reference to
14092 -- the previous partial view. The partial view can be private
14093 -- or incomplete, and these are handled in a different manner,
14094 -- so we determine the position of the error message from the
14095 -- respective slocs of both.
14097 ------------------
14098 -- Tag_Mismatch --
14099 ------------------
14101 procedure Tag_Mismatch is
14102 begin
14103 if Sloc (Prev) < Sloc (Id) then
14104 if Ada_Version >= Ada_2012
14105 and then Nkind (N) = N_Private_Type_Declaration
14106 then
14107 Error_Msg_NE
14108 ("declaration of private } must be a tagged type ", Id, Prev);
14109 else
14110 Error_Msg_NE
14111 ("full declaration of } must be a tagged type ", Id, Prev);
14112 end if;
14113 else
14114 if Ada_Version >= Ada_2012
14115 and then Nkind (N) = N_Private_Type_Declaration
14116 then
14117 Error_Msg_NE
14118 ("declaration of private } must be a tagged type ", Prev, Id);
14119 else
14120 Error_Msg_NE
14121 ("full declaration of } must be a tagged type ", Prev, Id);
14122 end if;
14123 end if;
14124 end Tag_Mismatch;
14126 -- Start of processing for Find_Type_Name
14128 begin
14129 -- Find incomplete declaration, if one was given
14131 Prev := Current_Entity_In_Scope (Id);
14133 -- New type declaration
14135 if No (Prev) then
14136 Enter_Name (Id);
14137 return Id;
14139 -- Previous declaration exists
14141 else
14142 Prev_Par := Parent (Prev);
14144 -- Error if not incomplete/private case except if previous
14145 -- declaration is implicit, etc. Enter_Name will emit error if
14146 -- appropriate.
14148 if not Is_Incomplete_Or_Private_Type (Prev) then
14149 Enter_Name (Id);
14150 New_Id := Id;
14152 -- Check invalid completion of private or incomplete type
14154 elsif not Nkind_In (N, N_Full_Type_Declaration,
14155 N_Task_Type_Declaration,
14156 N_Protected_Type_Declaration)
14157 and then
14158 (Ada_Version < Ada_2012
14159 or else not Is_Incomplete_Type (Prev)
14160 or else not Nkind_In (N, N_Private_Type_Declaration,
14161 N_Private_Extension_Declaration))
14162 then
14163 -- Completion must be a full type declarations (RM 7.3(4))
14165 Error_Msg_Sloc := Sloc (Prev);
14166 Error_Msg_NE ("invalid completion of }", Id, Prev);
14168 -- Set scope of Id to avoid cascaded errors. Entity is never
14169 -- examined again, except when saving globals in generics.
14171 Set_Scope (Id, Current_Scope);
14172 New_Id := Id;
14174 -- If this is a repeated incomplete declaration, no further
14175 -- checks are possible.
14177 if Nkind (N) = N_Incomplete_Type_Declaration then
14178 return Prev;
14179 end if;
14181 -- Case of full declaration of incomplete type
14183 elsif Ekind (Prev) = E_Incomplete_Type
14184 and then (Ada_Version < Ada_2012
14185 or else No (Full_View (Prev))
14186 or else not Is_Private_Type (Full_View (Prev)))
14187 then
14189 -- Indicate that the incomplete declaration has a matching full
14190 -- declaration. The defining occurrence of the incomplete
14191 -- declaration remains the visible one, and the procedure
14192 -- Get_Full_View dereferences it whenever the type is used.
14194 if Present (Full_View (Prev)) then
14195 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14196 end if;
14198 Set_Full_View (Prev, Id);
14199 Append_Entity (Id, Current_Scope);
14200 Set_Is_Public (Id, Is_Public (Prev));
14201 Set_Is_Internal (Id);
14202 New_Id := Prev;
14204 -- If the incomplete view is tagged, a class_wide type has been
14205 -- created already. Use it for the private type as well, in order
14206 -- to prevent multiple incompatible class-wide types that may be
14207 -- created for self-referential anonymous access components.
14209 if Is_Tagged_Type (Prev)
14210 and then Present (Class_Wide_Type (Prev))
14211 then
14212 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
14213 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
14214 Set_Etype (Class_Wide_Type (Id), Id);
14215 end if;
14217 -- Case of full declaration of private type
14219 else
14220 -- If the private type was a completion of an incomplete type then
14221 -- update Prev to reference the private type
14223 if Ada_Version >= Ada_2012
14224 and then Ekind (Prev) = E_Incomplete_Type
14225 and then Present (Full_View (Prev))
14226 and then Is_Private_Type (Full_View (Prev))
14227 then
14228 Prev := Full_View (Prev);
14229 Prev_Par := Parent (Prev);
14230 end if;
14232 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
14233 if Etype (Prev) /= Prev then
14235 -- Prev is a private subtype or a derived type, and needs
14236 -- no completion.
14238 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14239 New_Id := Id;
14241 elsif Ekind (Prev) = E_Private_Type
14242 and then Nkind_In (N, N_Task_Type_Declaration,
14243 N_Protected_Type_Declaration)
14244 then
14245 Error_Msg_N
14246 ("completion of nonlimited type cannot be limited", N);
14248 elsif Ekind (Prev) = E_Record_Type_With_Private
14249 and then Nkind_In (N, N_Task_Type_Declaration,
14250 N_Protected_Type_Declaration)
14251 then
14252 if not Is_Limited_Record (Prev) then
14253 Error_Msg_N
14254 ("completion of nonlimited type cannot be limited", N);
14256 elsif No (Interface_List (N)) then
14257 Error_Msg_N
14258 ("completion of tagged private type must be tagged",
14260 end if;
14262 elsif Nkind (N) = N_Full_Type_Declaration
14263 and then
14264 Nkind (Type_Definition (N)) = N_Record_Definition
14265 and then Interface_Present (Type_Definition (N))
14266 then
14267 Error_Msg_N
14268 ("completion of private type cannot be an interface", N);
14269 end if;
14271 -- Ada 2005 (AI-251): Private extension declaration of a task
14272 -- type or a protected type. This case arises when covering
14273 -- interface types.
14275 elsif Nkind_In (N, N_Task_Type_Declaration,
14276 N_Protected_Type_Declaration)
14277 then
14278 null;
14280 elsif Nkind (N) /= N_Full_Type_Declaration
14281 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
14282 then
14283 Error_Msg_N
14284 ("full view of private extension must be an extension", N);
14286 elsif not (Abstract_Present (Parent (Prev)))
14287 and then Abstract_Present (Type_Definition (N))
14288 then
14289 Error_Msg_N
14290 ("full view of non-abstract extension cannot be abstract", N);
14291 end if;
14293 if not In_Private_Part (Current_Scope) then
14294 Error_Msg_N
14295 ("declaration of full view must appear in private part", N);
14296 end if;
14298 Copy_And_Swap (Prev, Id);
14299 Set_Has_Private_Declaration (Prev);
14300 Set_Has_Private_Declaration (Id);
14302 -- If no error, propagate freeze_node from private to full view.
14303 -- It may have been generated for an early operational item.
14305 if Present (Freeze_Node (Id))
14306 and then Serious_Errors_Detected = 0
14307 and then No (Full_View (Id))
14308 then
14309 Set_Freeze_Node (Prev, Freeze_Node (Id));
14310 Set_Freeze_Node (Id, Empty);
14311 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
14312 end if;
14314 Set_Full_View (Id, Prev);
14315 New_Id := Prev;
14316 end if;
14318 -- Verify that full declaration conforms to partial one
14320 if Is_Incomplete_Or_Private_Type (Prev)
14321 and then Present (Discriminant_Specifications (Prev_Par))
14322 then
14323 if Present (Discriminant_Specifications (N)) then
14324 if Ekind (Prev) = E_Incomplete_Type then
14325 Check_Discriminant_Conformance (N, Prev, Prev);
14326 else
14327 Check_Discriminant_Conformance (N, Prev, Id);
14328 end if;
14330 else
14331 Error_Msg_N
14332 ("missing discriminants in full type declaration", N);
14334 -- To avoid cascaded errors on subsequent use, share the
14335 -- discriminants of the partial view.
14337 Set_Discriminant_Specifications (N,
14338 Discriminant_Specifications (Prev_Par));
14339 end if;
14340 end if;
14342 -- A prior untagged partial view can have an associated class-wide
14343 -- type due to use of the class attribute, and in this case the full
14344 -- type must also be tagged. This Ada 95 usage is deprecated in favor
14345 -- of incomplete tagged declarations, but we check for it.
14347 if Is_Type (Prev)
14348 and then (Is_Tagged_Type (Prev)
14349 or else Present (Class_Wide_Type (Prev)))
14350 then
14351 -- Ada 2012 (AI05-0162): A private type may be the completion of
14352 -- an incomplete type
14354 if Ada_Version >= Ada_2012
14355 and then Is_Incomplete_Type (Prev)
14356 and then Nkind_In (N, N_Private_Type_Declaration,
14357 N_Private_Extension_Declaration)
14358 then
14359 -- No need to check private extensions since they are tagged
14361 if Nkind (N) = N_Private_Type_Declaration
14362 and then not Tagged_Present (N)
14363 then
14364 Tag_Mismatch;
14365 end if;
14367 -- The full declaration is either a tagged type (including
14368 -- a synchronized type that implements interfaces) or a
14369 -- type extension, otherwise this is an error.
14371 elsif Nkind_In (N, N_Task_Type_Declaration,
14372 N_Protected_Type_Declaration)
14373 then
14374 if No (Interface_List (N))
14375 and then not Error_Posted (N)
14376 then
14377 Tag_Mismatch;
14378 end if;
14380 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
14382 -- Indicate that the previous declaration (tagged incomplete
14383 -- or private declaration) requires the same on the full one.
14385 if not Tagged_Present (Type_Definition (N)) then
14386 Tag_Mismatch;
14387 Set_Is_Tagged_Type (Id);
14388 end if;
14390 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
14391 if No (Record_Extension_Part (Type_Definition (N))) then
14392 Error_Msg_NE
14393 ("full declaration of } must be a record extension",
14394 Prev, Id);
14396 -- Set some attributes to produce a usable full view
14398 Set_Is_Tagged_Type (Id);
14399 end if;
14401 else
14402 Tag_Mismatch;
14403 end if;
14404 end if;
14406 return New_Id;
14407 end if;
14408 end Find_Type_Name;
14410 -------------------------
14411 -- Find_Type_Of_Object --
14412 -------------------------
14414 function Find_Type_Of_Object
14415 (Obj_Def : Node_Id;
14416 Related_Nod : Node_Id) return Entity_Id
14418 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
14419 P : Node_Id := Parent (Obj_Def);
14420 T : Entity_Id;
14421 Nam : Name_Id;
14423 begin
14424 -- If the parent is a component_definition node we climb to the
14425 -- component_declaration node
14427 if Nkind (P) = N_Component_Definition then
14428 P := Parent (P);
14429 end if;
14431 -- Case of an anonymous array subtype
14433 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
14434 N_Unconstrained_Array_Definition)
14435 then
14436 T := Empty;
14437 Array_Type_Declaration (T, Obj_Def);
14439 -- Create an explicit subtype whenever possible
14441 elsif Nkind (P) /= N_Component_Declaration
14442 and then Def_Kind = N_Subtype_Indication
14443 then
14444 -- Base name of subtype on object name, which will be unique in
14445 -- the current scope.
14447 -- If this is a duplicate declaration, return base type, to avoid
14448 -- generating duplicate anonymous types.
14450 if Error_Posted (P) then
14451 Analyze (Subtype_Mark (Obj_Def));
14452 return Entity (Subtype_Mark (Obj_Def));
14453 end if;
14455 Nam :=
14456 New_External_Name
14457 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
14459 T := Make_Defining_Identifier (Sloc (P), Nam);
14461 Insert_Action (Obj_Def,
14462 Make_Subtype_Declaration (Sloc (P),
14463 Defining_Identifier => T,
14464 Subtype_Indication => Relocate_Node (Obj_Def)));
14466 -- This subtype may need freezing, and this will not be done
14467 -- automatically if the object declaration is not in declarative
14468 -- part. Since this is an object declaration, the type cannot always
14469 -- be frozen here. Deferred constants do not freeze their type
14470 -- (which often enough will be private).
14472 if Nkind (P) = N_Object_Declaration
14473 and then Constant_Present (P)
14474 and then No (Expression (P))
14475 then
14476 null;
14477 else
14478 Insert_Actions (Obj_Def, Freeze_Entity (T, P));
14479 end if;
14481 -- Ada 2005 AI-406: the object definition in an object declaration
14482 -- can be an access definition.
14484 elsif Def_Kind = N_Access_Definition then
14485 T := Access_Definition (Related_Nod, Obj_Def);
14486 Set_Is_Local_Anonymous_Access (T);
14488 -- Otherwise, the object definition is just a subtype_mark
14490 else
14491 T := Process_Subtype (Obj_Def, Related_Nod);
14492 end if;
14494 return T;
14495 end Find_Type_Of_Object;
14497 --------------------------------
14498 -- Find_Type_Of_Subtype_Indic --
14499 --------------------------------
14501 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
14502 Typ : Entity_Id;
14504 begin
14505 -- Case of subtype mark with a constraint
14507 if Nkind (S) = N_Subtype_Indication then
14508 Find_Type (Subtype_Mark (S));
14509 Typ := Entity (Subtype_Mark (S));
14511 if not
14512 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
14513 then
14514 Error_Msg_N
14515 ("incorrect constraint for this kind of type", Constraint (S));
14516 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
14517 end if;
14519 -- Otherwise we have a subtype mark without a constraint
14521 elsif Error_Posted (S) then
14522 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
14523 return Any_Type;
14525 else
14526 Find_Type (S);
14527 Typ := Entity (S);
14528 end if;
14530 -- Check No_Wide_Characters restriction
14532 Check_Wide_Character_Restriction (Typ, S);
14534 return Typ;
14535 end Find_Type_Of_Subtype_Indic;
14537 -------------------------------------
14538 -- Floating_Point_Type_Declaration --
14539 -------------------------------------
14541 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14542 Digs : constant Node_Id := Digits_Expression (Def);
14543 Digs_Val : Uint;
14544 Base_Typ : Entity_Id;
14545 Implicit_Base : Entity_Id;
14546 Bound : Node_Id;
14548 function Can_Derive_From (E : Entity_Id) return Boolean;
14549 -- Find if given digits value allows derivation from specified type
14551 ---------------------
14552 -- Can_Derive_From --
14553 ---------------------
14555 function Can_Derive_From (E : Entity_Id) return Boolean is
14556 Spec : constant Entity_Id := Real_Range_Specification (Def);
14558 begin
14559 if Digs_Val > Digits_Value (E) then
14560 return False;
14561 end if;
14563 if Present (Spec) then
14564 if Expr_Value_R (Type_Low_Bound (E)) >
14565 Expr_Value_R (Low_Bound (Spec))
14566 then
14567 return False;
14568 end if;
14570 if Expr_Value_R (Type_High_Bound (E)) <
14571 Expr_Value_R (High_Bound (Spec))
14572 then
14573 return False;
14574 end if;
14575 end if;
14577 return True;
14578 end Can_Derive_From;
14580 -- Start of processing for Floating_Point_Type_Declaration
14582 begin
14583 Check_Restriction (No_Floating_Point, Def);
14585 -- Create an implicit base type
14587 Implicit_Base :=
14588 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
14590 -- Analyze and verify digits value
14592 Analyze_And_Resolve (Digs, Any_Integer);
14593 Check_Digits_Expression (Digs);
14594 Digs_Val := Expr_Value (Digs);
14596 -- Process possible range spec and find correct type to derive from
14598 Process_Real_Range_Specification (Def);
14600 if Can_Derive_From (Standard_Short_Float) then
14601 Base_Typ := Standard_Short_Float;
14602 elsif Can_Derive_From (Standard_Float) then
14603 Base_Typ := Standard_Float;
14604 elsif Can_Derive_From (Standard_Long_Float) then
14605 Base_Typ := Standard_Long_Float;
14606 elsif Can_Derive_From (Standard_Long_Long_Float) then
14607 Base_Typ := Standard_Long_Long_Float;
14609 -- If we can't derive from any existing type, use long_long_float
14610 -- and give appropriate message explaining the problem.
14612 else
14613 Base_Typ := Standard_Long_Long_Float;
14615 if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
14616 Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
14617 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
14619 else
14620 Error_Msg_N
14621 ("range too large for any predefined type",
14622 Real_Range_Specification (Def));
14623 end if;
14624 end if;
14626 -- If there are bounds given in the declaration use them as the bounds
14627 -- of the type, otherwise use the bounds of the predefined base type
14628 -- that was chosen based on the Digits value.
14630 if Present (Real_Range_Specification (Def)) then
14631 Set_Scalar_Range (T, Real_Range_Specification (Def));
14632 Set_Is_Constrained (T);
14634 -- The bounds of this range must be converted to machine numbers
14635 -- in accordance with RM 4.9(38).
14637 Bound := Type_Low_Bound (T);
14639 if Nkind (Bound) = N_Real_Literal then
14640 Set_Realval
14641 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
14642 Set_Is_Machine_Number (Bound);
14643 end if;
14645 Bound := Type_High_Bound (T);
14647 if Nkind (Bound) = N_Real_Literal then
14648 Set_Realval
14649 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
14650 Set_Is_Machine_Number (Bound);
14651 end if;
14653 else
14654 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
14655 end if;
14657 -- Complete definition of implicit base and declared first subtype
14659 Set_Etype (Implicit_Base, Base_Typ);
14661 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
14662 Set_Size_Info (Implicit_Base, (Base_Typ));
14663 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
14664 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
14665 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
14666 Set_Vax_Float (Implicit_Base, Vax_Float (Base_Typ));
14668 Set_Ekind (T, E_Floating_Point_Subtype);
14669 Set_Etype (T, Implicit_Base);
14671 Set_Size_Info (T, (Implicit_Base));
14672 Set_RM_Size (T, RM_Size (Implicit_Base));
14673 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
14674 Set_Digits_Value (T, Digs_Val);
14675 end Floating_Point_Type_Declaration;
14677 ----------------------------
14678 -- Get_Discriminant_Value --
14679 ----------------------------
14681 -- This is the situation:
14683 -- There is a non-derived type
14685 -- type T0 (Dx, Dy, Dz...)
14687 -- There are zero or more levels of derivation, with each derivation
14688 -- either purely inheriting the discriminants, or defining its own.
14690 -- type Ti is new Ti-1
14691 -- or
14692 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
14693 -- or
14694 -- subtype Ti is ...
14696 -- The subtype issue is avoided by the use of Original_Record_Component,
14697 -- and the fact that derived subtypes also derive the constraints.
14699 -- This chain leads back from
14701 -- Typ_For_Constraint
14703 -- Typ_For_Constraint has discriminants, and the value for each
14704 -- discriminant is given by its corresponding Elmt of Constraints.
14706 -- Discriminant is some discriminant in this hierarchy
14708 -- We need to return its value
14710 -- We do this by recursively searching each level, and looking for
14711 -- Discriminant. Once we get to the bottom, we start backing up
14712 -- returning the value for it which may in turn be a discriminant
14713 -- further up, so on the backup we continue the substitution.
14715 function Get_Discriminant_Value
14716 (Discriminant : Entity_Id;
14717 Typ_For_Constraint : Entity_Id;
14718 Constraint : Elist_Id) return Node_Id
14720 function Search_Derivation_Levels
14721 (Ti : Entity_Id;
14722 Discrim_Values : Elist_Id;
14723 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
14724 -- This is the routine that performs the recursive search of levels
14725 -- as described above.
14727 ------------------------------
14728 -- Search_Derivation_Levels --
14729 ------------------------------
14731 function Search_Derivation_Levels
14732 (Ti : Entity_Id;
14733 Discrim_Values : Elist_Id;
14734 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
14736 Assoc : Elmt_Id;
14737 Disc : Entity_Id;
14738 Result : Node_Or_Entity_Id;
14739 Result_Entity : Node_Id;
14741 begin
14742 -- If inappropriate type, return Error, this happens only in
14743 -- cascaded error situations, and we want to avoid a blow up.
14745 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
14746 return Error;
14747 end if;
14749 -- Look deeper if possible. Use Stored_Constraints only for
14750 -- untagged types. For tagged types use the given constraint.
14751 -- This asymmetry needs explanation???
14753 if not Stored_Discrim_Values
14754 and then Present (Stored_Constraint (Ti))
14755 and then not Is_Tagged_Type (Ti)
14756 then
14757 Result :=
14758 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
14759 else
14760 declare
14761 Td : constant Entity_Id := Etype (Ti);
14763 begin
14764 if Td = Ti then
14765 Result := Discriminant;
14767 else
14768 if Present (Stored_Constraint (Ti)) then
14769 Result :=
14770 Search_Derivation_Levels
14771 (Td, Stored_Constraint (Ti), True);
14772 else
14773 Result :=
14774 Search_Derivation_Levels
14775 (Td, Discrim_Values, Stored_Discrim_Values);
14776 end if;
14777 end if;
14778 end;
14779 end if;
14781 -- Extra underlying places to search, if not found above. For
14782 -- concurrent types, the relevant discriminant appears in the
14783 -- corresponding record. For a type derived from a private type
14784 -- without discriminant, the full view inherits the discriminants
14785 -- of the full view of the parent.
14787 if Result = Discriminant then
14788 if Is_Concurrent_Type (Ti)
14789 and then Present (Corresponding_Record_Type (Ti))
14790 then
14791 Result :=
14792 Search_Derivation_Levels (
14793 Corresponding_Record_Type (Ti),
14794 Discrim_Values,
14795 Stored_Discrim_Values);
14797 elsif Is_Private_Type (Ti)
14798 and then not Has_Discriminants (Ti)
14799 and then Present (Full_View (Ti))
14800 and then Etype (Full_View (Ti)) /= Ti
14801 then
14802 Result :=
14803 Search_Derivation_Levels (
14804 Full_View (Ti),
14805 Discrim_Values,
14806 Stored_Discrim_Values);
14807 end if;
14808 end if;
14810 -- If Result is not a (reference to a) discriminant, return it,
14811 -- otherwise set Result_Entity to the discriminant.
14813 if Nkind (Result) = N_Defining_Identifier then
14814 pragma Assert (Result = Discriminant);
14815 Result_Entity := Result;
14817 else
14818 if not Denotes_Discriminant (Result) then
14819 return Result;
14820 end if;
14822 Result_Entity := Entity (Result);
14823 end if;
14825 -- See if this level of derivation actually has discriminants
14826 -- because tagged derivations can add them, hence the lower
14827 -- levels need not have any.
14829 if not Has_Discriminants (Ti) then
14830 return Result;
14831 end if;
14833 -- Scan Ti's discriminants for Result_Entity,
14834 -- and return its corresponding value, if any.
14836 Result_Entity := Original_Record_Component (Result_Entity);
14838 Assoc := First_Elmt (Discrim_Values);
14840 if Stored_Discrim_Values then
14841 Disc := First_Stored_Discriminant (Ti);
14842 else
14843 Disc := First_Discriminant (Ti);
14844 end if;
14846 while Present (Disc) loop
14847 pragma Assert (Present (Assoc));
14849 if Original_Record_Component (Disc) = Result_Entity then
14850 return Node (Assoc);
14851 end if;
14853 Next_Elmt (Assoc);
14855 if Stored_Discrim_Values then
14856 Next_Stored_Discriminant (Disc);
14857 else
14858 Next_Discriminant (Disc);
14859 end if;
14860 end loop;
14862 -- Could not find it
14864 return Result;
14865 end Search_Derivation_Levels;
14867 -- Local Variables
14869 Result : Node_Or_Entity_Id;
14871 -- Start of processing for Get_Discriminant_Value
14873 begin
14874 -- ??? This routine is a gigantic mess and will be deleted. For the
14875 -- time being just test for the trivial case before calling recurse.
14877 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
14878 declare
14879 D : Entity_Id;
14880 E : Elmt_Id;
14882 begin
14883 D := First_Discriminant (Typ_For_Constraint);
14884 E := First_Elmt (Constraint);
14885 while Present (D) loop
14886 if Chars (D) = Chars (Discriminant) then
14887 return Node (E);
14888 end if;
14890 Next_Discriminant (D);
14891 Next_Elmt (E);
14892 end loop;
14893 end;
14894 end if;
14896 Result := Search_Derivation_Levels
14897 (Typ_For_Constraint, Constraint, False);
14899 -- ??? hack to disappear when this routine is gone
14901 if Nkind (Result) = N_Defining_Identifier then
14902 declare
14903 D : Entity_Id;
14904 E : Elmt_Id;
14906 begin
14907 D := First_Discriminant (Typ_For_Constraint);
14908 E := First_Elmt (Constraint);
14909 while Present (D) loop
14910 if Corresponding_Discriminant (D) = Discriminant then
14911 return Node (E);
14912 end if;
14914 Next_Discriminant (D);
14915 Next_Elmt (E);
14916 end loop;
14917 end;
14918 end if;
14920 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
14921 return Result;
14922 end Get_Discriminant_Value;
14924 --------------------------
14925 -- Has_Range_Constraint --
14926 --------------------------
14928 function Has_Range_Constraint (N : Node_Id) return Boolean is
14929 C : constant Node_Id := Constraint (N);
14931 begin
14932 if Nkind (C) = N_Range_Constraint then
14933 return True;
14935 elsif Nkind (C) = N_Digits_Constraint then
14936 return
14937 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
14938 or else
14939 Present (Range_Constraint (C));
14941 elsif Nkind (C) = N_Delta_Constraint then
14942 return Present (Range_Constraint (C));
14944 else
14945 return False;
14946 end if;
14947 end Has_Range_Constraint;
14949 ------------------------
14950 -- Inherit_Components --
14951 ------------------------
14953 function Inherit_Components
14954 (N : Node_Id;
14955 Parent_Base : Entity_Id;
14956 Derived_Base : Entity_Id;
14957 Is_Tagged : Boolean;
14958 Inherit_Discr : Boolean;
14959 Discs : Elist_Id) return Elist_Id
14961 Assoc_List : constant Elist_Id := New_Elmt_List;
14963 procedure Inherit_Component
14964 (Old_C : Entity_Id;
14965 Plain_Discrim : Boolean := False;
14966 Stored_Discrim : Boolean := False);
14967 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
14968 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
14969 -- True, Old_C is a stored discriminant. If they are both false then
14970 -- Old_C is a regular component.
14972 -----------------------
14973 -- Inherit_Component --
14974 -----------------------
14976 procedure Inherit_Component
14977 (Old_C : Entity_Id;
14978 Plain_Discrim : Boolean := False;
14979 Stored_Discrim : Boolean := False)
14981 New_C : constant Entity_Id := New_Copy (Old_C);
14983 Discrim : Entity_Id;
14984 Corr_Discrim : Entity_Id;
14986 begin
14987 pragma Assert (not Is_Tagged or else not Stored_Discrim);
14989 Set_Parent (New_C, Parent (Old_C));
14991 -- Regular discriminants and components must be inserted in the scope
14992 -- of the Derived_Base. Do it here.
14994 if not Stored_Discrim then
14995 Enter_Name (New_C);
14996 end if;
14998 -- For tagged types the Original_Record_Component must point to
14999 -- whatever this field was pointing to in the parent type. This has
15000 -- already been achieved by the call to New_Copy above.
15002 if not Is_Tagged then
15003 Set_Original_Record_Component (New_C, New_C);
15004 end if;
15006 -- If we have inherited a component then see if its Etype contains
15007 -- references to Parent_Base discriminants. In this case, replace
15008 -- these references with the constraints given in Discs. We do not
15009 -- do this for the partial view of private types because this is
15010 -- not needed (only the components of the full view will be used
15011 -- for code generation) and cause problem. We also avoid this
15012 -- transformation in some error situations.
15014 if Ekind (New_C) = E_Component then
15015 if (Is_Private_Type (Derived_Base)
15016 and then not Is_Generic_Type (Derived_Base))
15017 or else (Is_Empty_Elmt_List (Discs)
15018 and then not Expander_Active)
15019 then
15020 Set_Etype (New_C, Etype (Old_C));
15022 else
15023 -- The current component introduces a circularity of the
15024 -- following kind:
15026 -- limited with Pack_2;
15027 -- package Pack_1 is
15028 -- type T_1 is tagged record
15029 -- Comp : access Pack_2.T_2;
15030 -- ...
15031 -- end record;
15032 -- end Pack_1;
15034 -- with Pack_1;
15035 -- package Pack_2 is
15036 -- type T_2 is new Pack_1.T_1 with ...;
15037 -- end Pack_2;
15039 Set_Etype
15040 (New_C,
15041 Constrain_Component_Type
15042 (Old_C, Derived_Base, N, Parent_Base, Discs));
15043 end if;
15044 end if;
15046 -- In derived tagged types it is illegal to reference a non
15047 -- discriminant component in the parent type. To catch this, mark
15048 -- these components with an Ekind of E_Void. This will be reset in
15049 -- Record_Type_Definition after processing the record extension of
15050 -- the derived type.
15052 -- If the declaration is a private extension, there is no further
15053 -- record extension to process, and the components retain their
15054 -- current kind, because they are visible at this point.
15056 if Is_Tagged and then Ekind (New_C) = E_Component
15057 and then Nkind (N) /= N_Private_Extension_Declaration
15058 then
15059 Set_Ekind (New_C, E_Void);
15060 end if;
15062 if Plain_Discrim then
15063 Set_Corresponding_Discriminant (New_C, Old_C);
15064 Build_Discriminal (New_C);
15066 -- If we are explicitly inheriting a stored discriminant it will be
15067 -- completely hidden.
15069 elsif Stored_Discrim then
15070 Set_Corresponding_Discriminant (New_C, Empty);
15071 Set_Discriminal (New_C, Empty);
15072 Set_Is_Completely_Hidden (New_C);
15074 -- Set the Original_Record_Component of each discriminant in the
15075 -- derived base to point to the corresponding stored that we just
15076 -- created.
15078 Discrim := First_Discriminant (Derived_Base);
15079 while Present (Discrim) loop
15080 Corr_Discrim := Corresponding_Discriminant (Discrim);
15082 -- Corr_Discrim could be missing in an error situation
15084 if Present (Corr_Discrim)
15085 and then Original_Record_Component (Corr_Discrim) = Old_C
15086 then
15087 Set_Original_Record_Component (Discrim, New_C);
15088 end if;
15090 Next_Discriminant (Discrim);
15091 end loop;
15093 Append_Entity (New_C, Derived_Base);
15094 end if;
15096 if not Is_Tagged then
15097 Append_Elmt (Old_C, Assoc_List);
15098 Append_Elmt (New_C, Assoc_List);
15099 end if;
15100 end Inherit_Component;
15102 -- Variables local to Inherit_Component
15104 Loc : constant Source_Ptr := Sloc (N);
15106 Parent_Discrim : Entity_Id;
15107 Stored_Discrim : Entity_Id;
15108 D : Entity_Id;
15109 Component : Entity_Id;
15111 -- Start of processing for Inherit_Components
15113 begin
15114 if not Is_Tagged then
15115 Append_Elmt (Parent_Base, Assoc_List);
15116 Append_Elmt (Derived_Base, Assoc_List);
15117 end if;
15119 -- Inherit parent discriminants if needed
15121 if Inherit_Discr then
15122 Parent_Discrim := First_Discriminant (Parent_Base);
15123 while Present (Parent_Discrim) loop
15124 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
15125 Next_Discriminant (Parent_Discrim);
15126 end loop;
15127 end if;
15129 -- Create explicit stored discrims for untagged types when necessary
15131 if not Has_Unknown_Discriminants (Derived_Base)
15132 and then Has_Discriminants (Parent_Base)
15133 and then not Is_Tagged
15134 and then
15135 (not Inherit_Discr
15136 or else First_Discriminant (Parent_Base) /=
15137 First_Stored_Discriminant (Parent_Base))
15138 then
15139 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
15140 while Present (Stored_Discrim) loop
15141 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
15142 Next_Stored_Discriminant (Stored_Discrim);
15143 end loop;
15144 end if;
15146 -- See if we can apply the second transformation for derived types, as
15147 -- explained in point 6. in the comments above Build_Derived_Record_Type
15148 -- This is achieved by appending Derived_Base discriminants into Discs,
15149 -- which has the side effect of returning a non empty Discs list to the
15150 -- caller of Inherit_Components, which is what we want. This must be
15151 -- done for private derived types if there are explicit stored
15152 -- discriminants, to ensure that we can retrieve the values of the
15153 -- constraints provided in the ancestors.
15155 if Inherit_Discr
15156 and then Is_Empty_Elmt_List (Discs)
15157 and then Present (First_Discriminant (Derived_Base))
15158 and then
15159 (not Is_Private_Type (Derived_Base)
15160 or else Is_Completely_Hidden
15161 (First_Stored_Discriminant (Derived_Base))
15162 or else Is_Generic_Type (Derived_Base))
15163 then
15164 D := First_Discriminant (Derived_Base);
15165 while Present (D) loop
15166 Append_Elmt (New_Reference_To (D, Loc), Discs);
15167 Next_Discriminant (D);
15168 end loop;
15169 end if;
15171 -- Finally, inherit non-discriminant components unless they are not
15172 -- visible because defined or inherited from the full view of the
15173 -- parent. Don't inherit the _parent field of the parent type.
15175 Component := First_Entity (Parent_Base);
15176 while Present (Component) loop
15178 -- Ada 2005 (AI-251): Do not inherit components associated with
15179 -- secondary tags of the parent.
15181 if Ekind (Component) = E_Component
15182 and then Present (Related_Type (Component))
15183 then
15184 null;
15186 elsif Ekind (Component) /= E_Component
15187 or else Chars (Component) = Name_uParent
15188 then
15189 null;
15191 -- If the derived type is within the parent type's declarative
15192 -- region, then the components can still be inherited even though
15193 -- they aren't visible at this point. This can occur for cases
15194 -- such as within public child units where the components must
15195 -- become visible upon entering the child unit's private part.
15197 elsif not Is_Visible_Component (Component)
15198 and then not In_Open_Scopes (Scope (Parent_Base))
15199 then
15200 null;
15202 elsif Ekind_In (Derived_Base, E_Private_Type,
15203 E_Limited_Private_Type)
15204 then
15205 null;
15207 else
15208 Inherit_Component (Component);
15209 end if;
15211 Next_Entity (Component);
15212 end loop;
15214 -- For tagged derived types, inherited discriminants cannot be used in
15215 -- component declarations of the record extension part. To achieve this
15216 -- we mark the inherited discriminants as not visible.
15218 if Is_Tagged and then Inherit_Discr then
15219 D := First_Discriminant (Derived_Base);
15220 while Present (D) loop
15221 Set_Is_Immediately_Visible (D, False);
15222 Next_Discriminant (D);
15223 end loop;
15224 end if;
15226 return Assoc_List;
15227 end Inherit_Components;
15229 -----------------------
15230 -- Is_Null_Extension --
15231 -----------------------
15233 function Is_Null_Extension (T : Entity_Id) return Boolean is
15234 Type_Decl : constant Node_Id := Parent (Base_Type (T));
15235 Comp_List : Node_Id;
15236 Comp : Node_Id;
15238 begin
15239 if Nkind (Type_Decl) /= N_Full_Type_Declaration
15240 or else not Is_Tagged_Type (T)
15241 or else Nkind (Type_Definition (Type_Decl)) /=
15242 N_Derived_Type_Definition
15243 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
15244 then
15245 return False;
15246 end if;
15248 Comp_List :=
15249 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
15251 if Present (Discriminant_Specifications (Type_Decl)) then
15252 return False;
15254 elsif Present (Comp_List)
15255 and then Is_Non_Empty_List (Component_Items (Comp_List))
15256 then
15257 Comp := First (Component_Items (Comp_List));
15259 -- Only user-defined components are relevant. The component list
15260 -- may also contain a parent component and internal components
15261 -- corresponding to secondary tags, but these do not determine
15262 -- whether this is a null extension.
15264 while Present (Comp) loop
15265 if Comes_From_Source (Comp) then
15266 return False;
15267 end if;
15269 Next (Comp);
15270 end loop;
15272 return True;
15273 else
15274 return True;
15275 end if;
15276 end Is_Null_Extension;
15278 ------------------------------
15279 -- Is_Valid_Constraint_Kind --
15280 ------------------------------
15282 function Is_Valid_Constraint_Kind
15283 (T_Kind : Type_Kind;
15284 Constraint_Kind : Node_Kind) return Boolean
15286 begin
15287 case T_Kind is
15288 when Enumeration_Kind |
15289 Integer_Kind =>
15290 return Constraint_Kind = N_Range_Constraint;
15292 when Decimal_Fixed_Point_Kind =>
15293 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
15294 N_Range_Constraint);
15296 when Ordinary_Fixed_Point_Kind =>
15297 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
15298 N_Range_Constraint);
15300 when Float_Kind =>
15301 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
15302 N_Range_Constraint);
15304 when Access_Kind |
15305 Array_Kind |
15306 E_Record_Type |
15307 E_Record_Subtype |
15308 Class_Wide_Kind |
15309 E_Incomplete_Type |
15310 Private_Kind |
15311 Concurrent_Kind =>
15312 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
15314 when others =>
15315 return True; -- Error will be detected later
15316 end case;
15317 end Is_Valid_Constraint_Kind;
15319 --------------------------
15320 -- Is_Visible_Component --
15321 --------------------------
15323 function Is_Visible_Component (C : Entity_Id) return Boolean is
15324 Original_Comp : Entity_Id := Empty;
15325 Original_Scope : Entity_Id;
15326 Type_Scope : Entity_Id;
15328 function Is_Local_Type (Typ : Entity_Id) return Boolean;
15329 -- Check whether parent type of inherited component is declared locally,
15330 -- possibly within a nested package or instance. The current scope is
15331 -- the derived record itself.
15333 -------------------
15334 -- Is_Local_Type --
15335 -------------------
15337 function Is_Local_Type (Typ : Entity_Id) return Boolean is
15338 Scop : Entity_Id;
15340 begin
15341 Scop := Scope (Typ);
15342 while Present (Scop)
15343 and then Scop /= Standard_Standard
15344 loop
15345 if Scop = Scope (Current_Scope) then
15346 return True;
15347 end if;
15349 Scop := Scope (Scop);
15350 end loop;
15352 return False;
15353 end Is_Local_Type;
15355 -- Start of processing for Is_Visible_Component
15357 begin
15358 if Ekind_In (C, E_Component, E_Discriminant) then
15359 Original_Comp := Original_Record_Component (C);
15360 end if;
15362 if No (Original_Comp) then
15364 -- Premature usage, or previous error
15366 return False;
15368 else
15369 Original_Scope := Scope (Original_Comp);
15370 Type_Scope := Scope (Base_Type (Scope (C)));
15371 end if;
15373 -- This test only concerns tagged types
15375 if not Is_Tagged_Type (Original_Scope) then
15376 return True;
15378 -- If it is _Parent or _Tag, there is no visibility issue
15380 elsif not Comes_From_Source (Original_Comp) then
15381 return True;
15383 -- If we are in the body of an instantiation, the component is visible
15384 -- even when the parent type (possibly defined in an enclosing unit or
15385 -- in a parent unit) might not.
15387 elsif In_Instance_Body then
15388 return True;
15390 -- Discriminants are always visible
15392 elsif Ekind (Original_Comp) = E_Discriminant
15393 and then not Has_Unknown_Discriminants (Original_Scope)
15394 then
15395 return True;
15397 -- If the component has been declared in an ancestor which is currently
15398 -- a private type, then it is not visible. The same applies if the
15399 -- component's containing type is not in an open scope and the original
15400 -- component's enclosing type is a visible full view of a private type
15401 -- (which can occur in cases where an attempt is being made to reference
15402 -- a component in a sibling package that is inherited from a visible
15403 -- component of a type in an ancestor package; the component in the
15404 -- sibling package should not be visible even though the component it
15405 -- inherited from is visible). This does not apply however in the case
15406 -- where the scope of the type is a private child unit, or when the
15407 -- parent comes from a local package in which the ancestor is currently
15408 -- visible. The latter suppression of visibility is needed for cases
15409 -- that are tested in B730006.
15411 elsif Is_Private_Type (Original_Scope)
15412 or else
15413 (not Is_Private_Descendant (Type_Scope)
15414 and then not In_Open_Scopes (Type_Scope)
15415 and then Has_Private_Declaration (Original_Scope))
15416 then
15417 -- If the type derives from an entity in a formal package, there
15418 -- are no additional visible components.
15420 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
15421 N_Formal_Package_Declaration
15422 then
15423 return False;
15425 -- if we are not in the private part of the current package, there
15426 -- are no additional visible components.
15428 elsif Ekind (Scope (Current_Scope)) = E_Package
15429 and then not In_Private_Part (Scope (Current_Scope))
15430 then
15431 return False;
15432 else
15433 return
15434 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
15435 and then In_Open_Scopes (Scope (Original_Scope))
15436 and then Is_Local_Type (Type_Scope);
15437 end if;
15439 -- There is another weird way in which a component may be invisible
15440 -- when the private and the full view are not derived from the same
15441 -- ancestor. Here is an example :
15443 -- type A1 is tagged record F1 : integer; end record;
15444 -- type A2 is new A1 with record F2 : integer; end record;
15445 -- type T is new A1 with private;
15446 -- private
15447 -- type T is new A2 with null record;
15449 -- In this case, the full view of T inherits F1 and F2 but the private
15450 -- view inherits only F1
15452 else
15453 declare
15454 Ancestor : Entity_Id := Scope (C);
15456 begin
15457 loop
15458 if Ancestor = Original_Scope then
15459 return True;
15460 elsif Ancestor = Etype (Ancestor) then
15461 return False;
15462 end if;
15464 Ancestor := Etype (Ancestor);
15465 end loop;
15466 end;
15467 end if;
15468 end Is_Visible_Component;
15470 --------------------------
15471 -- Make_Class_Wide_Type --
15472 --------------------------
15474 procedure Make_Class_Wide_Type (T : Entity_Id) is
15475 CW_Type : Entity_Id;
15476 CW_Name : Name_Id;
15477 Next_E : Entity_Id;
15479 begin
15480 -- The class wide type can have been defined by the partial view, in
15481 -- which case everything is already done.
15483 if Present (Class_Wide_Type (T)) then
15484 return;
15485 end if;
15487 CW_Type :=
15488 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
15490 -- Inherit root type characteristics
15492 CW_Name := Chars (CW_Type);
15493 Next_E := Next_Entity (CW_Type);
15494 Copy_Node (T, CW_Type);
15495 Set_Comes_From_Source (CW_Type, False);
15496 Set_Chars (CW_Type, CW_Name);
15497 Set_Parent (CW_Type, Parent (T));
15498 Set_Next_Entity (CW_Type, Next_E);
15500 -- Ensure we have a new freeze node for the class-wide type. The partial
15501 -- view may have freeze action of its own, requiring a proper freeze
15502 -- node, and the same freeze node cannot be shared between the two
15503 -- types.
15505 Set_Has_Delayed_Freeze (CW_Type);
15506 Set_Freeze_Node (CW_Type, Empty);
15508 -- Customize the class-wide type: It has no prim. op., it cannot be
15509 -- abstract and its Etype points back to the specific root type.
15511 Set_Ekind (CW_Type, E_Class_Wide_Type);
15512 Set_Is_Tagged_Type (CW_Type, True);
15513 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
15514 Set_Is_Abstract_Type (CW_Type, False);
15515 Set_Is_Constrained (CW_Type, False);
15516 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
15518 if Ekind (T) = E_Class_Wide_Subtype then
15519 Set_Etype (CW_Type, Etype (Base_Type (T)));
15520 else
15521 Set_Etype (CW_Type, T);
15522 end if;
15524 -- If this is the class_wide type of a constrained subtype, it does
15525 -- not have discriminants.
15527 Set_Has_Discriminants (CW_Type,
15528 Has_Discriminants (T) and then not Is_Constrained (T));
15530 Set_Has_Unknown_Discriminants (CW_Type, True);
15531 Set_Class_Wide_Type (T, CW_Type);
15532 Set_Equivalent_Type (CW_Type, Empty);
15534 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
15536 Set_Class_Wide_Type (CW_Type, CW_Type);
15537 end Make_Class_Wide_Type;
15539 ----------------
15540 -- Make_Index --
15541 ----------------
15543 procedure Make_Index
15544 (I : Node_Id;
15545 Related_Nod : Node_Id;
15546 Related_Id : Entity_Id := Empty;
15547 Suffix_Index : Nat := 1)
15549 R : Node_Id;
15550 T : Entity_Id;
15551 Def_Id : Entity_Id := Empty;
15552 Found : Boolean := False;
15554 begin
15555 -- For a discrete range used in a constrained array definition and
15556 -- defined by a range, an implicit conversion to the predefined type
15557 -- INTEGER is assumed if each bound is either a numeric literal, a named
15558 -- number, or an attribute, and the type of both bounds (prior to the
15559 -- implicit conversion) is the type universal_integer. Otherwise, both
15560 -- bounds must be of the same discrete type, other than universal
15561 -- integer; this type must be determinable independently of the
15562 -- context, but using the fact that the type must be discrete and that
15563 -- both bounds must have the same type.
15565 -- Character literals also have a universal type in the absence of
15566 -- of additional context, and are resolved to Standard_Character.
15568 if Nkind (I) = N_Range then
15570 -- The index is given by a range constraint. The bounds are known
15571 -- to be of a consistent type.
15573 if not Is_Overloaded (I) then
15574 T := Etype (I);
15576 -- For universal bounds, choose the specific predefined type
15578 if T = Universal_Integer then
15579 T := Standard_Integer;
15581 elsif T = Any_Character then
15582 Ambiguous_Character (Low_Bound (I));
15584 T := Standard_Character;
15585 end if;
15587 -- The node may be overloaded because some user-defined operators
15588 -- are available, but if a universal interpretation exists it is
15589 -- also the selected one.
15591 elsif Universal_Interpretation (I) = Universal_Integer then
15592 T := Standard_Integer;
15594 else
15595 T := Any_Type;
15597 declare
15598 Ind : Interp_Index;
15599 It : Interp;
15601 begin
15602 Get_First_Interp (I, Ind, It);
15603 while Present (It.Typ) loop
15604 if Is_Discrete_Type (It.Typ) then
15606 if Found
15607 and then not Covers (It.Typ, T)
15608 and then not Covers (T, It.Typ)
15609 then
15610 Error_Msg_N ("ambiguous bounds in discrete range", I);
15611 exit;
15612 else
15613 T := It.Typ;
15614 Found := True;
15615 end if;
15616 end if;
15618 Get_Next_Interp (Ind, It);
15619 end loop;
15621 if T = Any_Type then
15622 Error_Msg_N ("discrete type required for range", I);
15623 Set_Etype (I, Any_Type);
15624 return;
15626 elsif T = Universal_Integer then
15627 T := Standard_Integer;
15628 end if;
15629 end;
15630 end if;
15632 if not Is_Discrete_Type (T) then
15633 Error_Msg_N ("discrete type required for range", I);
15634 Set_Etype (I, Any_Type);
15635 return;
15636 end if;
15638 if Nkind (Low_Bound (I)) = N_Attribute_Reference
15639 and then Attribute_Name (Low_Bound (I)) = Name_First
15640 and then Is_Entity_Name (Prefix (Low_Bound (I)))
15641 and then Is_Type (Entity (Prefix (Low_Bound (I))))
15642 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
15643 then
15644 -- The type of the index will be the type of the prefix, as long
15645 -- as the upper bound is 'Last of the same type.
15647 Def_Id := Entity (Prefix (Low_Bound (I)));
15649 if Nkind (High_Bound (I)) /= N_Attribute_Reference
15650 or else Attribute_Name (High_Bound (I)) /= Name_Last
15651 or else not Is_Entity_Name (Prefix (High_Bound (I)))
15652 or else Entity (Prefix (High_Bound (I))) /= Def_Id
15653 then
15654 Def_Id := Empty;
15655 end if;
15656 end if;
15658 R := I;
15659 Process_Range_Expr_In_Decl (R, T);
15661 elsif Nkind (I) = N_Subtype_Indication then
15663 -- The index is given by a subtype with a range constraint
15665 T := Base_Type (Entity (Subtype_Mark (I)));
15667 if not Is_Discrete_Type (T) then
15668 Error_Msg_N ("discrete type required for range", I);
15669 Set_Etype (I, Any_Type);
15670 return;
15671 end if;
15673 R := Range_Expression (Constraint (I));
15675 Resolve (R, T);
15676 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
15678 elsif Nkind (I) = N_Attribute_Reference then
15680 -- The parser guarantees that the attribute is a RANGE attribute
15682 -- If the node denotes the range of a type mark, that is also the
15683 -- resulting type, and we do no need to create an Itype for it.
15685 if Is_Entity_Name (Prefix (I))
15686 and then Comes_From_Source (I)
15687 and then Is_Type (Entity (Prefix (I)))
15688 and then Is_Discrete_Type (Entity (Prefix (I)))
15689 then
15690 Def_Id := Entity (Prefix (I));
15691 end if;
15693 Analyze_And_Resolve (I);
15694 T := Etype (I);
15695 R := I;
15697 -- If none of the above, must be a subtype. We convert this to a
15698 -- range attribute reference because in the case of declared first
15699 -- named subtypes, the types in the range reference can be different
15700 -- from the type of the entity. A range attribute normalizes the
15701 -- reference and obtains the correct types for the bounds.
15703 -- This transformation is in the nature of an expansion, is only
15704 -- done if expansion is active. In particular, it is not done on
15705 -- formal generic types, because we need to retain the name of the
15706 -- original index for instantiation purposes.
15708 else
15709 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
15710 Error_Msg_N ("invalid subtype mark in discrete range ", I);
15711 Set_Etype (I, Any_Integer);
15712 return;
15714 else
15715 -- The type mark may be that of an incomplete type. It is only
15716 -- now that we can get the full view, previous analysis does
15717 -- not look specifically for a type mark.
15719 Set_Entity (I, Get_Full_View (Entity (I)));
15720 Set_Etype (I, Entity (I));
15721 Def_Id := Entity (I);
15723 if not Is_Discrete_Type (Def_Id) then
15724 Error_Msg_N ("discrete type required for index", I);
15725 Set_Etype (I, Any_Type);
15726 return;
15727 end if;
15728 end if;
15730 if Expander_Active then
15731 Rewrite (I,
15732 Make_Attribute_Reference (Sloc (I),
15733 Attribute_Name => Name_Range,
15734 Prefix => Relocate_Node (I)));
15736 -- The original was a subtype mark that does not freeze. This
15737 -- means that the rewritten version must not freeze either.
15739 Set_Must_Not_Freeze (I);
15740 Set_Must_Not_Freeze (Prefix (I));
15742 -- Is order critical??? if so, document why, if not
15743 -- use Analyze_And_Resolve
15745 Analyze_And_Resolve (I);
15746 T := Etype (I);
15747 R := I;
15749 -- If expander is inactive, type is legal, nothing else to construct
15751 else
15752 return;
15753 end if;
15754 end if;
15756 if not Is_Discrete_Type (T) then
15757 Error_Msg_N ("discrete type required for range", I);
15758 Set_Etype (I, Any_Type);
15759 return;
15761 elsif T = Any_Type then
15762 Set_Etype (I, Any_Type);
15763 return;
15764 end if;
15766 -- We will now create the appropriate Itype to describe the range, but
15767 -- first a check. If we originally had a subtype, then we just label
15768 -- the range with this subtype. Not only is there no need to construct
15769 -- a new subtype, but it is wrong to do so for two reasons:
15771 -- 1. A legality concern, if we have a subtype, it must not freeze,
15772 -- and the Itype would cause freezing incorrectly
15774 -- 2. An efficiency concern, if we created an Itype, it would not be
15775 -- recognized as the same type for the purposes of eliminating
15776 -- checks in some circumstances.
15778 -- We signal this case by setting the subtype entity in Def_Id
15780 if No (Def_Id) then
15781 Def_Id :=
15782 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
15783 Set_Etype (Def_Id, Base_Type (T));
15785 if Is_Signed_Integer_Type (T) then
15786 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
15788 elsif Is_Modular_Integer_Type (T) then
15789 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
15791 else
15792 Set_Ekind (Def_Id, E_Enumeration_Subtype);
15793 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
15794 Set_First_Literal (Def_Id, First_Literal (T));
15795 end if;
15797 Set_Size_Info (Def_Id, (T));
15798 Set_RM_Size (Def_Id, RM_Size (T));
15799 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
15801 Set_Scalar_Range (Def_Id, R);
15802 Conditional_Delay (Def_Id, T);
15804 -- In the subtype indication case, if the immediate parent of the
15805 -- new subtype is non-static, then the subtype we create is non-
15806 -- static, even if its bounds are static.
15808 if Nkind (I) = N_Subtype_Indication
15809 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
15810 then
15811 Set_Is_Non_Static_Subtype (Def_Id);
15812 end if;
15813 end if;
15815 -- Final step is to label the index with this constructed type
15817 Set_Etype (I, Def_Id);
15818 end Make_Index;
15820 ------------------------------
15821 -- Modular_Type_Declaration --
15822 ------------------------------
15824 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15825 Mod_Expr : constant Node_Id := Expression (Def);
15826 M_Val : Uint;
15828 procedure Set_Modular_Size (Bits : Int);
15829 -- Sets RM_Size to Bits, and Esize to normal word size above this
15831 ----------------------
15832 -- Set_Modular_Size --
15833 ----------------------
15835 procedure Set_Modular_Size (Bits : Int) is
15836 begin
15837 Set_RM_Size (T, UI_From_Int (Bits));
15839 if Bits <= 8 then
15840 Init_Esize (T, 8);
15842 elsif Bits <= 16 then
15843 Init_Esize (T, 16);
15845 elsif Bits <= 32 then
15846 Init_Esize (T, 32);
15848 else
15849 Init_Esize (T, System_Max_Binary_Modulus_Power);
15850 end if;
15852 if not Non_Binary_Modulus (T)
15853 and then Esize (T) = RM_Size (T)
15854 then
15855 Set_Is_Known_Valid (T);
15856 end if;
15857 end Set_Modular_Size;
15859 -- Start of processing for Modular_Type_Declaration
15861 begin
15862 Analyze_And_Resolve (Mod_Expr, Any_Integer);
15863 Set_Etype (T, T);
15864 Set_Ekind (T, E_Modular_Integer_Type);
15865 Init_Alignment (T);
15866 Set_Is_Constrained (T);
15868 if not Is_OK_Static_Expression (Mod_Expr) then
15869 Flag_Non_Static_Expr
15870 ("non-static expression used for modular type bound!", Mod_Expr);
15871 M_Val := 2 ** System_Max_Binary_Modulus_Power;
15872 else
15873 M_Val := Expr_Value (Mod_Expr);
15874 end if;
15876 if M_Val < 1 then
15877 Error_Msg_N ("modulus value must be positive", Mod_Expr);
15878 M_Val := 2 ** System_Max_Binary_Modulus_Power;
15879 end if;
15881 Set_Modulus (T, M_Val);
15883 -- Create bounds for the modular type based on the modulus given in
15884 -- the type declaration and then analyze and resolve those bounds.
15886 Set_Scalar_Range (T,
15887 Make_Range (Sloc (Mod_Expr),
15888 Low_Bound =>
15889 Make_Integer_Literal (Sloc (Mod_Expr), 0),
15890 High_Bound =>
15891 Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
15893 -- Properly analyze the literals for the range. We do this manually
15894 -- because we can't go calling Resolve, since we are resolving these
15895 -- bounds with the type, and this type is certainly not complete yet!
15897 Set_Etype (Low_Bound (Scalar_Range (T)), T);
15898 Set_Etype (High_Bound (Scalar_Range (T)), T);
15899 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
15900 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
15902 -- Loop through powers of two to find number of bits required
15904 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
15906 -- Binary case
15908 if M_Val = 2 ** Bits then
15909 Set_Modular_Size (Bits);
15910 return;
15912 -- Non-binary case
15914 elsif M_Val < 2 ** Bits then
15915 Set_Non_Binary_Modulus (T);
15917 if Bits > System_Max_Nonbinary_Modulus_Power then
15918 Error_Msg_Uint_1 :=
15919 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
15920 Error_Msg_F
15921 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
15922 Set_Modular_Size (System_Max_Binary_Modulus_Power);
15923 return;
15925 else
15926 -- In the non-binary case, set size as per RM 13.3(55)
15928 Set_Modular_Size (Bits);
15929 return;
15930 end if;
15931 end if;
15933 end loop;
15935 -- If we fall through, then the size exceed System.Max_Binary_Modulus
15936 -- so we just signal an error and set the maximum size.
15938 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
15939 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
15941 Set_Modular_Size (System_Max_Binary_Modulus_Power);
15942 Init_Alignment (T);
15944 end Modular_Type_Declaration;
15946 --------------------------
15947 -- New_Concatenation_Op --
15948 --------------------------
15950 procedure New_Concatenation_Op (Typ : Entity_Id) is
15951 Loc : constant Source_Ptr := Sloc (Typ);
15952 Op : Entity_Id;
15954 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
15955 -- Create abbreviated declaration for the formal of a predefined
15956 -- Operator 'Op' of type 'Typ'
15958 --------------------
15959 -- Make_Op_Formal --
15960 --------------------
15962 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
15963 Formal : Entity_Id;
15964 begin
15965 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
15966 Set_Etype (Formal, Typ);
15967 Set_Mechanism (Formal, Default_Mechanism);
15968 return Formal;
15969 end Make_Op_Formal;
15971 -- Start of processing for New_Concatenation_Op
15973 begin
15974 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
15976 Set_Ekind (Op, E_Operator);
15977 Set_Scope (Op, Current_Scope);
15978 Set_Etype (Op, Typ);
15979 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
15980 Set_Is_Immediately_Visible (Op);
15981 Set_Is_Intrinsic_Subprogram (Op);
15982 Set_Has_Completion (Op);
15983 Append_Entity (Op, Current_Scope);
15985 Set_Name_Entity_Id (Name_Op_Concat, Op);
15987 Append_Entity (Make_Op_Formal (Typ, Op), Op);
15988 Append_Entity (Make_Op_Formal (Typ, Op), Op);
15989 end New_Concatenation_Op;
15991 -------------------------
15992 -- OK_For_Limited_Init --
15993 -------------------------
15995 -- ???Check all calls of this, and compare the conditions under which it's
15996 -- called.
15998 function OK_For_Limited_Init
15999 (Typ : Entity_Id;
16000 Exp : Node_Id) return Boolean
16002 begin
16003 return Is_CPP_Constructor_Call (Exp)
16004 or else (Ada_Version >= Ada_2005
16005 and then not Debug_Flag_Dot_L
16006 and then OK_For_Limited_Init_In_05 (Typ, Exp));
16007 end OK_For_Limited_Init;
16009 -------------------------------
16010 -- OK_For_Limited_Init_In_05 --
16011 -------------------------------
16013 function OK_For_Limited_Init_In_05
16014 (Typ : Entity_Id;
16015 Exp : Node_Id) return Boolean
16017 begin
16018 -- An object of a limited interface type can be initialized with any
16019 -- expression of a nonlimited descendant type.
16021 if Is_Class_Wide_Type (Typ)
16022 and then Is_Limited_Interface (Typ)
16023 and then not Is_Limited_Type (Etype (Exp))
16024 then
16025 return True;
16026 end if;
16028 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
16029 -- case of limited aggregates (including extension aggregates), and
16030 -- function calls. The function call may have been given in prefixed
16031 -- notation, in which case the original node is an indexed component.
16032 -- If the function is parameterless, the original node was an explicit
16033 -- dereference.
16035 case Nkind (Original_Node (Exp)) is
16036 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
16037 return True;
16039 when N_Qualified_Expression =>
16040 return
16041 OK_For_Limited_Init_In_05
16042 (Typ, Expression (Original_Node (Exp)));
16044 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
16045 -- with a function call, the expander has rewritten the call into an
16046 -- N_Type_Conversion node to force displacement of the pointer to
16047 -- reference the component containing the secondary dispatch table.
16048 -- Otherwise a type conversion is not a legal context.
16049 -- A return statement for a build-in-place function returning a
16050 -- synchronized type also introduces an unchecked conversion.
16052 when N_Type_Conversion |
16053 N_Unchecked_Type_Conversion =>
16054 return not Comes_From_Source (Exp)
16055 and then
16056 OK_For_Limited_Init_In_05
16057 (Typ, Expression (Original_Node (Exp)));
16059 when N_Indexed_Component |
16060 N_Selected_Component |
16061 N_Explicit_Dereference =>
16062 return Nkind (Exp) = N_Function_Call;
16064 -- A use of 'Input is a function call, hence allowed. Normally the
16065 -- attribute will be changed to a call, but the attribute by itself
16066 -- can occur with -gnatc.
16068 when N_Attribute_Reference =>
16069 return Attribute_Name (Original_Node (Exp)) = Name_Input;
16071 when others =>
16072 return False;
16073 end case;
16074 end OK_For_Limited_Init_In_05;
16076 -------------------------------------------
16077 -- Ordinary_Fixed_Point_Type_Declaration --
16078 -------------------------------------------
16080 procedure Ordinary_Fixed_Point_Type_Declaration
16081 (T : Entity_Id;
16082 Def : Node_Id)
16084 Loc : constant Source_Ptr := Sloc (Def);
16085 Delta_Expr : constant Node_Id := Delta_Expression (Def);
16086 RRS : constant Node_Id := Real_Range_Specification (Def);
16087 Implicit_Base : Entity_Id;
16088 Delta_Val : Ureal;
16089 Small_Val : Ureal;
16090 Low_Val : Ureal;
16091 High_Val : Ureal;
16093 begin
16094 Check_Restriction (No_Fixed_Point, Def);
16096 -- Create implicit base type
16098 Implicit_Base :=
16099 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
16100 Set_Etype (Implicit_Base, Implicit_Base);
16102 -- Analyze and process delta expression
16104 Analyze_And_Resolve (Delta_Expr, Any_Real);
16106 Check_Delta_Expression (Delta_Expr);
16107 Delta_Val := Expr_Value_R (Delta_Expr);
16109 Set_Delta_Value (Implicit_Base, Delta_Val);
16111 -- Compute default small from given delta, which is the largest power
16112 -- of two that does not exceed the given delta value.
16114 declare
16115 Tmp : Ureal;
16116 Scale : Int;
16118 begin
16119 Tmp := Ureal_1;
16120 Scale := 0;
16122 if Delta_Val < Ureal_1 then
16123 while Delta_Val < Tmp loop
16124 Tmp := Tmp / Ureal_2;
16125 Scale := Scale + 1;
16126 end loop;
16128 else
16129 loop
16130 Tmp := Tmp * Ureal_2;
16131 exit when Tmp > Delta_Val;
16132 Scale := Scale - 1;
16133 end loop;
16134 end if;
16136 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
16137 end;
16139 Set_Small_Value (Implicit_Base, Small_Val);
16141 -- If no range was given, set a dummy range
16143 if RRS <= Empty_Or_Error then
16144 Low_Val := -Small_Val;
16145 High_Val := Small_Val;
16147 -- Otherwise analyze and process given range
16149 else
16150 declare
16151 Low : constant Node_Id := Low_Bound (RRS);
16152 High : constant Node_Id := High_Bound (RRS);
16154 begin
16155 Analyze_And_Resolve (Low, Any_Real);
16156 Analyze_And_Resolve (High, Any_Real);
16157 Check_Real_Bound (Low);
16158 Check_Real_Bound (High);
16160 -- Obtain and set the range
16162 Low_Val := Expr_Value_R (Low);
16163 High_Val := Expr_Value_R (High);
16165 if Low_Val > High_Val then
16166 Error_Msg_NE ("?fixed point type& has null range", Def, T);
16167 end if;
16168 end;
16169 end if;
16171 -- The range for both the implicit base and the declared first subtype
16172 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
16173 -- set a temporary range in place. Note that the bounds of the base
16174 -- type will be widened to be symmetrical and to fill the available
16175 -- bits when the type is frozen.
16177 -- We could do this with all discrete types, and probably should, but
16178 -- we absolutely have to do it for fixed-point, since the end-points
16179 -- of the range and the size are determined by the small value, which
16180 -- could be reset before the freeze point.
16182 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
16183 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
16185 -- Complete definition of first subtype
16187 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
16188 Set_Etype (T, Implicit_Base);
16189 Init_Size_Align (T);
16190 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
16191 Set_Small_Value (T, Small_Val);
16192 Set_Delta_Value (T, Delta_Val);
16193 Set_Is_Constrained (T);
16195 end Ordinary_Fixed_Point_Type_Declaration;
16197 ----------------------------------------
16198 -- Prepare_Private_Subtype_Completion --
16199 ----------------------------------------
16201 procedure Prepare_Private_Subtype_Completion
16202 (Id : Entity_Id;
16203 Related_Nod : Node_Id)
16205 Id_B : constant Entity_Id := Base_Type (Id);
16206 Full_B : constant Entity_Id := Full_View (Id_B);
16207 Full : Entity_Id;
16209 begin
16210 if Present (Full_B) then
16212 -- The Base_Type is already completed, we can complete the subtype
16213 -- now. We have to create a new entity with the same name, Thus we
16214 -- can't use Create_Itype.
16216 -- This is messy, should be fixed ???
16218 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
16219 Set_Is_Itype (Full);
16220 Set_Associated_Node_For_Itype (Full, Related_Nod);
16221 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
16222 end if;
16224 -- The parent subtype may be private, but the base might not, in some
16225 -- nested instances. In that case, the subtype does not need to be
16226 -- exchanged. It would still be nice to make private subtypes and their
16227 -- bases consistent at all times ???
16229 if Is_Private_Type (Id_B) then
16230 Append_Elmt (Id, Private_Dependents (Id_B));
16231 end if;
16233 end Prepare_Private_Subtype_Completion;
16235 ---------------------------
16236 -- Process_Discriminants --
16237 ---------------------------
16239 procedure Process_Discriminants
16240 (N : Node_Id;
16241 Prev : Entity_Id := Empty)
16243 Elist : constant Elist_Id := New_Elmt_List;
16244 Id : Node_Id;
16245 Discr : Node_Id;
16246 Discr_Number : Uint;
16247 Discr_Type : Entity_Id;
16248 Default_Present : Boolean := False;
16249 Default_Not_Present : Boolean := False;
16251 begin
16252 -- A composite type other than an array type can have discriminants.
16253 -- On entry, the current scope is the composite type.
16255 -- The discriminants are initially entered into the scope of the type
16256 -- via Enter_Name with the default Ekind of E_Void to prevent premature
16257 -- use, as explained at the end of this procedure.
16259 Discr := First (Discriminant_Specifications (N));
16260 while Present (Discr) loop
16261 Enter_Name (Defining_Identifier (Discr));
16263 -- For navigation purposes we add a reference to the discriminant
16264 -- in the entity for the type. If the current declaration is a
16265 -- completion, place references on the partial view. Otherwise the
16266 -- type is the current scope.
16268 if Present (Prev) then
16270 -- The references go on the partial view, if present. If the
16271 -- partial view has discriminants, the references have been
16272 -- generated already.
16274 if not Has_Discriminants (Prev) then
16275 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
16276 end if;
16277 else
16278 Generate_Reference
16279 (Current_Scope, Defining_Identifier (Discr), 'd');
16280 end if;
16282 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
16283 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
16285 -- Ada 2005 (AI-254)
16287 if Present (Access_To_Subprogram_Definition
16288 (Discriminant_Type (Discr)))
16289 and then Protected_Present (Access_To_Subprogram_Definition
16290 (Discriminant_Type (Discr)))
16291 then
16292 Discr_Type :=
16293 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
16294 end if;
16296 else
16297 Find_Type (Discriminant_Type (Discr));
16298 Discr_Type := Etype (Discriminant_Type (Discr));
16300 if Error_Posted (Discriminant_Type (Discr)) then
16301 Discr_Type := Any_Type;
16302 end if;
16303 end if;
16305 if Is_Access_Type (Discr_Type) then
16307 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
16308 -- record types
16310 if Ada_Version < Ada_2005 then
16311 Check_Access_Discriminant_Requires_Limited
16312 (Discr, Discriminant_Type (Discr));
16313 end if;
16315 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
16316 Error_Msg_N
16317 ("(Ada 83) access discriminant not allowed", Discr);
16318 end if;
16320 elsif not Is_Discrete_Type (Discr_Type) then
16321 Error_Msg_N ("discriminants must have a discrete or access type",
16322 Discriminant_Type (Discr));
16323 end if;
16325 Set_Etype (Defining_Identifier (Discr), Discr_Type);
16327 -- If a discriminant specification includes the assignment compound
16328 -- delimiter followed by an expression, the expression is the default
16329 -- expression of the discriminant; the default expression must be of
16330 -- the type of the discriminant. (RM 3.7.1) Since this expression is
16331 -- a default expression, we do the special preanalysis, since this
16332 -- expression does not freeze (see "Handling of Default and Per-
16333 -- Object Expressions" in spec of package Sem).
16335 if Present (Expression (Discr)) then
16336 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
16338 if Nkind (N) = N_Formal_Type_Declaration then
16339 Error_Msg_N
16340 ("discriminant defaults not allowed for formal type",
16341 Expression (Discr));
16343 -- Tagged types declarations cannot have defaulted discriminants,
16344 -- but an untagged private type with defaulted discriminants can
16345 -- have a tagged completion.
16347 elsif Is_Tagged_Type (Current_Scope)
16348 and then Comes_From_Source (N)
16349 then
16350 Error_Msg_N
16351 ("discriminants of tagged type cannot have defaults",
16352 Expression (Discr));
16354 else
16355 Default_Present := True;
16356 Append_Elmt (Expression (Discr), Elist);
16358 -- Tag the defining identifiers for the discriminants with
16359 -- their corresponding default expressions from the tree.
16361 Set_Discriminant_Default_Value
16362 (Defining_Identifier (Discr), Expression (Discr));
16363 end if;
16365 else
16366 Default_Not_Present := True;
16367 end if;
16369 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
16370 -- Discr_Type but with the null-exclusion attribute
16372 if Ada_Version >= Ada_2005 then
16374 -- Ada 2005 (AI-231): Static checks
16376 if Can_Never_Be_Null (Discr_Type) then
16377 Null_Exclusion_Static_Checks (Discr);
16379 elsif Is_Access_Type (Discr_Type)
16380 and then Null_Exclusion_Present (Discr)
16382 -- No need to check itypes because in their case this check
16383 -- was done at their point of creation
16385 and then not Is_Itype (Discr_Type)
16386 then
16387 if Can_Never_Be_Null (Discr_Type) then
16388 Error_Msg_NE
16389 ("`NOT NULL` not allowed (& already excludes null)",
16390 Discr,
16391 Discr_Type);
16392 end if;
16394 Set_Etype (Defining_Identifier (Discr),
16395 Create_Null_Excluding_Itype
16396 (T => Discr_Type,
16397 Related_Nod => Discr));
16399 -- Check for improper null exclusion if the type is otherwise
16400 -- legal for a discriminant.
16402 elsif Null_Exclusion_Present (Discr)
16403 and then Is_Discrete_Type (Discr_Type)
16404 then
16405 Error_Msg_N
16406 ("null exclusion can only apply to an access type", Discr);
16407 end if;
16409 -- Ada 2005 (AI-402): access discriminants of nonlimited types
16410 -- can't have defaults. Synchronized types, or types that are
16411 -- explicitly limited are fine, but special tests apply to derived
16412 -- types in generics: in a generic body we have to assume the
16413 -- worst, and therefore defaults are not allowed if the parent is
16414 -- a generic formal private type (see ACATS B370001).
16416 if Is_Access_Type (Discr_Type) then
16417 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
16418 or else not Default_Present
16419 or else Is_Limited_Record (Current_Scope)
16420 or else Is_Concurrent_Type (Current_Scope)
16421 or else Is_Concurrent_Record_Type (Current_Scope)
16422 or else Ekind (Current_Scope) = E_Limited_Private_Type
16423 then
16424 if not Is_Derived_Type (Current_Scope)
16425 or else not Is_Generic_Type (Etype (Current_Scope))
16426 or else not In_Package_Body (Scope (Etype (Current_Scope)))
16427 or else Limited_Present
16428 (Type_Definition (Parent (Current_Scope)))
16429 then
16430 null;
16432 else
16433 Error_Msg_N ("access discriminants of nonlimited types",
16434 Expression (Discr));
16435 Error_Msg_N ("\cannot have defaults", Expression (Discr));
16436 end if;
16438 elsif Present (Expression (Discr)) then
16439 Error_Msg_N
16440 ("(Ada 2005) access discriminants of nonlimited types",
16441 Expression (Discr));
16442 Error_Msg_N ("\cannot have defaults", Expression (Discr));
16443 end if;
16444 end if;
16445 end if;
16447 Next (Discr);
16448 end loop;
16450 -- An element list consisting of the default expressions of the
16451 -- discriminants is constructed in the above loop and used to set
16452 -- the Discriminant_Constraint attribute for the type. If an object
16453 -- is declared of this (record or task) type without any explicit
16454 -- discriminant constraint given, this element list will form the
16455 -- actual parameters for the corresponding initialization procedure
16456 -- for the type.
16458 Set_Discriminant_Constraint (Current_Scope, Elist);
16459 Set_Stored_Constraint (Current_Scope, No_Elist);
16461 -- Default expressions must be provided either for all or for none
16462 -- of the discriminants of a discriminant part. (RM 3.7.1)
16464 if Default_Present and then Default_Not_Present then
16465 Error_Msg_N
16466 ("incomplete specification of defaults for discriminants", N);
16467 end if;
16469 -- The use of the name of a discriminant is not allowed in default
16470 -- expressions of a discriminant part if the specification of the
16471 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
16473 -- To detect this, the discriminant names are entered initially with an
16474 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
16475 -- attempt to use a void entity (for example in an expression that is
16476 -- type-checked) produces the error message: premature usage. Now after
16477 -- completing the semantic analysis of the discriminant part, we can set
16478 -- the Ekind of all the discriminants appropriately.
16480 Discr := First (Discriminant_Specifications (N));
16481 Discr_Number := Uint_1;
16482 while Present (Discr) loop
16483 Id := Defining_Identifier (Discr);
16484 Set_Ekind (Id, E_Discriminant);
16485 Init_Component_Location (Id);
16486 Init_Esize (Id);
16487 Set_Discriminant_Number (Id, Discr_Number);
16489 -- Make sure this is always set, even in illegal programs
16491 Set_Corresponding_Discriminant (Id, Empty);
16493 -- Initialize the Original_Record_Component to the entity itself.
16494 -- Inherit_Components will propagate the right value to
16495 -- discriminants in derived record types.
16497 Set_Original_Record_Component (Id, Id);
16499 -- Create the discriminal for the discriminant
16501 Build_Discriminal (Id);
16503 Next (Discr);
16504 Discr_Number := Discr_Number + 1;
16505 end loop;
16507 Set_Has_Discriminants (Current_Scope);
16508 end Process_Discriminants;
16510 -----------------------
16511 -- Process_Full_View --
16512 -----------------------
16514 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
16515 Priv_Parent : Entity_Id;
16516 Full_Parent : Entity_Id;
16517 Full_Indic : Node_Id;
16519 procedure Collect_Implemented_Interfaces
16520 (Typ : Entity_Id;
16521 Ifaces : Elist_Id);
16522 -- Ada 2005: Gather all the interfaces that Typ directly or
16523 -- inherently implements. Duplicate entries are not added to
16524 -- the list Ifaces.
16526 ------------------------------------
16527 -- Collect_Implemented_Interfaces --
16528 ------------------------------------
16530 procedure Collect_Implemented_Interfaces
16531 (Typ : Entity_Id;
16532 Ifaces : Elist_Id)
16534 Iface : Entity_Id;
16535 Iface_Elmt : Elmt_Id;
16537 begin
16538 -- Abstract interfaces are only associated with tagged record types
16540 if not Is_Tagged_Type (Typ)
16541 or else not Is_Record_Type (Typ)
16542 then
16543 return;
16544 end if;
16546 -- Recursively climb to the ancestors
16548 if Etype (Typ) /= Typ
16550 -- Protect the frontend against wrong cyclic declarations like:
16552 -- type B is new A with private;
16553 -- type C is new A with private;
16554 -- private
16555 -- type B is new C with null record;
16556 -- type C is new B with null record;
16558 and then Etype (Typ) /= Priv_T
16559 and then Etype (Typ) /= Full_T
16560 then
16561 -- Keep separate the management of private type declarations
16563 if Ekind (Typ) = E_Record_Type_With_Private then
16565 -- Handle the following erronous case:
16566 -- type Private_Type is tagged private;
16567 -- private
16568 -- type Private_Type is new Type_Implementing_Iface;
16570 if Present (Full_View (Typ))
16571 and then Etype (Typ) /= Full_View (Typ)
16572 then
16573 if Is_Interface (Etype (Typ)) then
16574 Append_Unique_Elmt (Etype (Typ), Ifaces);
16575 end if;
16577 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
16578 end if;
16580 -- Non-private types
16582 else
16583 if Is_Interface (Etype (Typ)) then
16584 Append_Unique_Elmt (Etype (Typ), Ifaces);
16585 end if;
16587 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
16588 end if;
16589 end if;
16591 -- Handle entities in the list of abstract interfaces
16593 if Present (Interfaces (Typ)) then
16594 Iface_Elmt := First_Elmt (Interfaces (Typ));
16595 while Present (Iface_Elmt) loop
16596 Iface := Node (Iface_Elmt);
16598 pragma Assert (Is_Interface (Iface));
16600 if not Contain_Interface (Iface, Ifaces) then
16601 Append_Elmt (Iface, Ifaces);
16602 Collect_Implemented_Interfaces (Iface, Ifaces);
16603 end if;
16605 Next_Elmt (Iface_Elmt);
16606 end loop;
16607 end if;
16608 end Collect_Implemented_Interfaces;
16610 -- Start of processing for Process_Full_View
16612 begin
16613 -- First some sanity checks that must be done after semantic
16614 -- decoration of the full view and thus cannot be placed with other
16615 -- similar checks in Find_Type_Name
16617 if not Is_Limited_Type (Priv_T)
16618 and then (Is_Limited_Type (Full_T)
16619 or else Is_Limited_Composite (Full_T))
16620 then
16621 Error_Msg_N
16622 ("completion of nonlimited type cannot be limited", Full_T);
16623 Explain_Limited_Type (Full_T, Full_T);
16625 elsif Is_Abstract_Type (Full_T)
16626 and then not Is_Abstract_Type (Priv_T)
16627 then
16628 Error_Msg_N
16629 ("completion of nonabstract type cannot be abstract", Full_T);
16631 elsif Is_Tagged_Type (Priv_T)
16632 and then Is_Limited_Type (Priv_T)
16633 and then not Is_Limited_Type (Full_T)
16634 then
16635 -- If pragma CPP_Class was applied to the private declaration
16636 -- propagate the limitedness to the full-view
16638 if Is_CPP_Class (Priv_T) then
16639 Set_Is_Limited_Record (Full_T);
16641 -- GNAT allow its own definition of Limited_Controlled to disobey
16642 -- this rule in order in ease the implementation. The next test is
16643 -- safe because Root_Controlled is defined in a private system child
16645 elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
16646 Set_Is_Limited_Composite (Full_T);
16647 else
16648 Error_Msg_N
16649 ("completion of limited tagged type must be limited", Full_T);
16650 end if;
16652 elsif Is_Generic_Type (Priv_T) then
16653 Error_Msg_N ("generic type cannot have a completion", Full_T);
16654 end if;
16656 -- Check that ancestor interfaces of private and full views are
16657 -- consistent. We omit this check for synchronized types because
16658 -- they are performed on the corresponding record type when frozen.
16660 if Ada_Version >= Ada_2005
16661 and then Is_Tagged_Type (Priv_T)
16662 and then Is_Tagged_Type (Full_T)
16663 and then not Is_Concurrent_Type (Full_T)
16664 then
16665 declare
16666 Iface : Entity_Id;
16667 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
16668 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
16670 begin
16671 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
16672 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
16674 -- Ada 2005 (AI-251): The partial view shall be a descendant of
16675 -- an interface type if and only if the full type is descendant
16676 -- of the interface type (AARM 7.3 (7.3/2).
16678 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
16680 if Present (Iface) then
16681 Error_Msg_NE
16682 ("interface & not implemented by full type " &
16683 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
16684 end if;
16686 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
16688 if Present (Iface) then
16689 Error_Msg_NE
16690 ("interface & not implemented by partial view " &
16691 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
16692 end if;
16693 end;
16694 end if;
16696 if Is_Tagged_Type (Priv_T)
16697 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
16698 and then Is_Derived_Type (Full_T)
16699 then
16700 Priv_Parent := Etype (Priv_T);
16702 -- The full view of a private extension may have been transformed
16703 -- into an unconstrained derived type declaration and a subtype
16704 -- declaration (see build_derived_record_type for details).
16706 if Nkind (N) = N_Subtype_Declaration then
16707 Full_Indic := Subtype_Indication (N);
16708 Full_Parent := Etype (Base_Type (Full_T));
16709 else
16710 Full_Indic := Subtype_Indication (Type_Definition (N));
16711 Full_Parent := Etype (Full_T);
16712 end if;
16714 -- Check that the parent type of the full type is a descendant of
16715 -- the ancestor subtype given in the private extension. If either
16716 -- entity has an Etype equal to Any_Type then we had some previous
16717 -- error situation [7.3(8)].
16719 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
16720 return;
16722 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
16723 -- any order. Therefore we don't have to check that its parent must
16724 -- be a descendant of the parent of the private type declaration.
16726 elsif Is_Interface (Priv_Parent)
16727 and then Is_Interface (Full_Parent)
16728 then
16729 null;
16731 -- Ada 2005 (AI-251): If the parent of the private type declaration
16732 -- is an interface there is no need to check that it is an ancestor
16733 -- of the associated full type declaration. The required tests for
16734 -- this case are performed by Build_Derived_Record_Type.
16736 elsif not Is_Interface (Base_Type (Priv_Parent))
16737 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
16738 then
16739 Error_Msg_N
16740 ("parent of full type must descend from parent"
16741 & " of private extension", Full_Indic);
16743 -- Check the rules of 7.3(10): if the private extension inherits
16744 -- known discriminants, then the full type must also inherit those
16745 -- discriminants from the same (ancestor) type, and the parent
16746 -- subtype of the full type must be constrained if and only if
16747 -- the ancestor subtype of the private extension is constrained.
16749 elsif No (Discriminant_Specifications (Parent (Priv_T)))
16750 and then not Has_Unknown_Discriminants (Priv_T)
16751 and then Has_Discriminants (Base_Type (Priv_Parent))
16752 then
16753 declare
16754 Priv_Indic : constant Node_Id :=
16755 Subtype_Indication (Parent (Priv_T));
16757 Priv_Constr : constant Boolean :=
16758 Is_Constrained (Priv_Parent)
16759 or else
16760 Nkind (Priv_Indic) = N_Subtype_Indication
16761 or else Is_Constrained (Entity (Priv_Indic));
16763 Full_Constr : constant Boolean :=
16764 Is_Constrained (Full_Parent)
16765 or else
16766 Nkind (Full_Indic) = N_Subtype_Indication
16767 or else Is_Constrained (Entity (Full_Indic));
16769 Priv_Discr : Entity_Id;
16770 Full_Discr : Entity_Id;
16772 begin
16773 Priv_Discr := First_Discriminant (Priv_Parent);
16774 Full_Discr := First_Discriminant (Full_Parent);
16775 while Present (Priv_Discr) and then Present (Full_Discr) loop
16776 if Original_Record_Component (Priv_Discr) =
16777 Original_Record_Component (Full_Discr)
16778 or else
16779 Corresponding_Discriminant (Priv_Discr) =
16780 Corresponding_Discriminant (Full_Discr)
16781 then
16782 null;
16783 else
16784 exit;
16785 end if;
16787 Next_Discriminant (Priv_Discr);
16788 Next_Discriminant (Full_Discr);
16789 end loop;
16791 if Present (Priv_Discr) or else Present (Full_Discr) then
16792 Error_Msg_N
16793 ("full view must inherit discriminants of the parent type"
16794 & " used in the private extension", Full_Indic);
16796 elsif Priv_Constr and then not Full_Constr then
16797 Error_Msg_N
16798 ("parent subtype of full type must be constrained",
16799 Full_Indic);
16801 elsif Full_Constr and then not Priv_Constr then
16802 Error_Msg_N
16803 ("parent subtype of full type must be unconstrained",
16804 Full_Indic);
16805 end if;
16806 end;
16808 -- Check the rules of 7.3(12): if a partial view has neither known
16809 -- or unknown discriminants, then the full type declaration shall
16810 -- define a definite subtype.
16812 elsif not Has_Unknown_Discriminants (Priv_T)
16813 and then not Has_Discriminants (Priv_T)
16814 and then not Is_Constrained (Full_T)
16815 then
16816 Error_Msg_N
16817 ("full view must define a constrained type if partial view"
16818 & " has no discriminants", Full_T);
16819 end if;
16821 -- ??????? Do we implement the following properly ?????
16822 -- If the ancestor subtype of a private extension has constrained
16823 -- discriminants, then the parent subtype of the full view shall
16824 -- impose a statically matching constraint on those discriminants
16825 -- [7.3(13)].
16827 else
16828 -- For untagged types, verify that a type without discriminants
16829 -- is not completed with an unconstrained type.
16831 if not Is_Indefinite_Subtype (Priv_T)
16832 and then Is_Indefinite_Subtype (Full_T)
16833 then
16834 Error_Msg_N ("full view of type must be definite subtype", Full_T);
16835 end if;
16836 end if;
16838 -- AI-419: verify that the use of "limited" is consistent
16840 declare
16841 Orig_Decl : constant Node_Id := Original_Node (N);
16843 begin
16844 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
16845 and then not Limited_Present (Parent (Priv_T))
16846 and then not Synchronized_Present (Parent (Priv_T))
16847 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
16848 and then Nkind
16849 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
16850 and then Limited_Present (Type_Definition (Orig_Decl))
16851 then
16852 Error_Msg_N
16853 ("full view of non-limited extension cannot be limited", N);
16854 end if;
16855 end;
16857 -- Ada 2005 (AI-443): A synchronized private extension must be
16858 -- completed by a task or protected type.
16860 if Ada_Version >= Ada_2005
16861 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
16862 and then Synchronized_Present (Parent (Priv_T))
16863 and then not Is_Concurrent_Type (Full_T)
16864 then
16865 Error_Msg_N ("full view of synchronized extension must " &
16866 "be synchronized type", N);
16867 end if;
16869 -- Ada 2005 AI-363: if the full view has discriminants with
16870 -- defaults, it is illegal to declare constrained access subtypes
16871 -- whose designated type is the current type. This allows objects
16872 -- of the type that are declared in the heap to be unconstrained.
16874 if not Has_Unknown_Discriminants (Priv_T)
16875 and then not Has_Discriminants (Priv_T)
16876 and then Has_Discriminants (Full_T)
16877 and then
16878 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
16879 then
16880 Set_Has_Constrained_Partial_View (Full_T);
16881 Set_Has_Constrained_Partial_View (Priv_T);
16882 end if;
16884 -- Create a full declaration for all its subtypes recorded in
16885 -- Private_Dependents and swap them similarly to the base type. These
16886 -- are subtypes that have been define before the full declaration of
16887 -- the private type. We also swap the entry in Private_Dependents list
16888 -- so we can properly restore the private view on exit from the scope.
16890 declare
16891 Priv_Elmt : Elmt_Id;
16892 Priv : Entity_Id;
16893 Full : Entity_Id;
16895 begin
16896 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
16897 while Present (Priv_Elmt) loop
16898 Priv := Node (Priv_Elmt);
16900 if Ekind_In (Priv, E_Private_Subtype,
16901 E_Limited_Private_Subtype,
16902 E_Record_Subtype_With_Private)
16903 then
16904 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
16905 Set_Is_Itype (Full);
16906 Set_Parent (Full, Parent (Priv));
16907 Set_Associated_Node_For_Itype (Full, N);
16909 -- Now we need to complete the private subtype, but since the
16910 -- base type has already been swapped, we must also swap the
16911 -- subtypes (and thus, reverse the arguments in the call to
16912 -- Complete_Private_Subtype).
16914 Copy_And_Swap (Priv, Full);
16915 Complete_Private_Subtype (Full, Priv, Full_T, N);
16916 Replace_Elmt (Priv_Elmt, Full);
16917 end if;
16919 Next_Elmt (Priv_Elmt);
16920 end loop;
16921 end;
16923 -- If the private view was tagged, copy the new primitive operations
16924 -- from the private view to the full view.
16926 if Is_Tagged_Type (Full_T) then
16927 declare
16928 Disp_Typ : Entity_Id;
16929 Full_List : Elist_Id;
16930 Prim : Entity_Id;
16931 Prim_Elmt : Elmt_Id;
16932 Priv_List : Elist_Id;
16934 function Contains
16935 (E : Entity_Id;
16936 L : Elist_Id) return Boolean;
16937 -- Determine whether list L contains element E
16939 --------------
16940 -- Contains --
16941 --------------
16943 function Contains
16944 (E : Entity_Id;
16945 L : Elist_Id) return Boolean
16947 List_Elmt : Elmt_Id;
16949 begin
16950 List_Elmt := First_Elmt (L);
16951 while Present (List_Elmt) loop
16952 if Node (List_Elmt) = E then
16953 return True;
16954 end if;
16956 Next_Elmt (List_Elmt);
16957 end loop;
16959 return False;
16960 end Contains;
16962 -- Start of processing
16964 begin
16965 if Is_Tagged_Type (Priv_T) then
16966 Priv_List := Primitive_Operations (Priv_T);
16967 Prim_Elmt := First_Elmt (Priv_List);
16969 -- In the case of a concurrent type completing a private tagged
16970 -- type, primitives may have been declared in between the two
16971 -- views. These subprograms need to be wrapped the same way
16972 -- entries and protected procedures are handled because they
16973 -- cannot be directly shared by the two views.
16975 if Is_Concurrent_Type (Full_T) then
16976 declare
16977 Conc_Typ : constant Entity_Id :=
16978 Corresponding_Record_Type (Full_T);
16979 Curr_Nod : Node_Id := Parent (Conc_Typ);
16980 Wrap_Spec : Node_Id;
16982 begin
16983 while Present (Prim_Elmt) loop
16984 Prim := Node (Prim_Elmt);
16986 if Comes_From_Source (Prim)
16987 and then not Is_Abstract_Subprogram (Prim)
16988 then
16989 Wrap_Spec :=
16990 Make_Subprogram_Declaration (Sloc (Prim),
16991 Specification =>
16992 Build_Wrapper_Spec
16993 (Subp_Id => Prim,
16994 Obj_Typ => Conc_Typ,
16995 Formals =>
16996 Parameter_Specifications (
16997 Parent (Prim))));
16999 Insert_After (Curr_Nod, Wrap_Spec);
17000 Curr_Nod := Wrap_Spec;
17002 Analyze (Wrap_Spec);
17003 end if;
17005 Next_Elmt (Prim_Elmt);
17006 end loop;
17008 return;
17009 end;
17011 -- For non-concurrent types, transfer explicit primitives, but
17012 -- omit those inherited from the parent of the private view
17013 -- since they will be re-inherited later on.
17015 else
17016 Full_List := Primitive_Operations (Full_T);
17018 while Present (Prim_Elmt) loop
17019 Prim := Node (Prim_Elmt);
17021 if Comes_From_Source (Prim)
17022 and then not Contains (Prim, Full_List)
17023 then
17024 Append_Elmt (Prim, Full_List);
17025 end if;
17027 Next_Elmt (Prim_Elmt);
17028 end loop;
17029 end if;
17031 -- Untagged private view
17033 else
17034 Full_List := Primitive_Operations (Full_T);
17036 -- In this case the partial view is untagged, so here we locate
17037 -- all of the earlier primitives that need to be treated as
17038 -- dispatching (those that appear between the two views). Note
17039 -- that these additional operations must all be new operations
17040 -- (any earlier operations that override inherited operations
17041 -- of the full view will already have been inserted in the
17042 -- primitives list, marked by Check_Operation_From_Private_View
17043 -- as dispatching. Note that implicit "/=" operators are
17044 -- excluded from being added to the primitives list since they
17045 -- shouldn't be treated as dispatching (tagged "/=" is handled
17046 -- specially).
17048 Prim := Next_Entity (Full_T);
17049 while Present (Prim) and then Prim /= Priv_T loop
17050 if Ekind_In (Prim, E_Procedure, E_Function) then
17051 Disp_Typ := Find_Dispatching_Type (Prim);
17053 if Disp_Typ = Full_T
17054 and then (Chars (Prim) /= Name_Op_Ne
17055 or else Comes_From_Source (Prim))
17056 then
17057 Check_Controlling_Formals (Full_T, Prim);
17059 if not Is_Dispatching_Operation (Prim) then
17060 Append_Elmt (Prim, Full_List);
17061 Set_Is_Dispatching_Operation (Prim, True);
17062 Set_DT_Position (Prim, No_Uint);
17063 end if;
17065 elsif Is_Dispatching_Operation (Prim)
17066 and then Disp_Typ /= Full_T
17067 then
17069 -- Verify that it is not otherwise controlled by a
17070 -- formal or a return value of type T.
17072 Check_Controlling_Formals (Disp_Typ, Prim);
17073 end if;
17074 end if;
17076 Next_Entity (Prim);
17077 end loop;
17078 end if;
17080 -- For the tagged case, the two views can share the same primitive
17081 -- operations list and the same class-wide type. Update attributes
17082 -- of the class-wide type which depend on the full declaration.
17084 if Is_Tagged_Type (Priv_T) then
17085 Set_Direct_Primitive_Operations (Priv_T, Full_List);
17086 Set_Class_Wide_Type
17087 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
17089 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
17090 end if;
17091 end;
17092 end if;
17094 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
17096 if Known_To_Have_Preelab_Init (Priv_T) then
17098 -- Case where there is a pragma Preelaborable_Initialization. We
17099 -- always allow this in predefined units, which is a bit of a kludge,
17100 -- but it means we don't have to struggle to meet the requirements in
17101 -- the RM for having Preelaborable Initialization. Otherwise we
17102 -- require that the type meets the RM rules. But we can't check that
17103 -- yet, because of the rule about overriding Ininitialize, so we
17104 -- simply set a flag that will be checked at freeze time.
17106 if not In_Predefined_Unit (Full_T) then
17107 Set_Must_Have_Preelab_Init (Full_T);
17108 end if;
17109 end if;
17111 -- If pragma CPP_Class was applied to the private type declaration,
17112 -- propagate it now to the full type declaration.
17114 if Is_CPP_Class (Priv_T) then
17115 Set_Is_CPP_Class (Full_T);
17116 Set_Convention (Full_T, Convention_CPP);
17117 end if;
17119 -- If the private view has user specified stream attributes, then so has
17120 -- the full view.
17122 -- Why the test, how could these flags be already set in Full_T ???
17124 if Has_Specified_Stream_Read (Priv_T) then
17125 Set_Has_Specified_Stream_Read (Full_T);
17126 end if;
17128 if Has_Specified_Stream_Write (Priv_T) then
17129 Set_Has_Specified_Stream_Write (Full_T);
17130 end if;
17132 if Has_Specified_Stream_Input (Priv_T) then
17133 Set_Has_Specified_Stream_Input (Full_T);
17134 end if;
17136 if Has_Specified_Stream_Output (Priv_T) then
17137 Set_Has_Specified_Stream_Output (Full_T);
17138 end if;
17140 -- Deal with invariants
17142 if Has_Invariants (Full_T)
17143 or else
17144 Has_Invariants (Priv_T)
17145 then
17146 Set_Has_Invariants (Full_T);
17147 Set_Has_Invariants (Priv_T);
17148 end if;
17150 if Has_Inheritable_Invariants (Full_T)
17151 or else
17152 Has_Inheritable_Invariants (Priv_T)
17153 then
17154 Set_Has_Inheritable_Invariants (Full_T);
17155 Set_Has_Inheritable_Invariants (Priv_T);
17156 end if;
17158 -- This is where we build the invariant procedure if needed
17160 if Has_Invariants (Priv_T) then
17161 declare
17162 PDecl : Entity_Id;
17163 PBody : Entity_Id;
17164 Packg : constant Node_Id := Declaration_Node (Scope (Priv_T));
17166 begin
17167 Build_Invariant_Procedure (Full_T, PDecl, PBody);
17169 -- Error defense, normally these should be set
17171 if Present (PDecl) and then Present (PBody) then
17173 -- Spec goes at the end of the public part of the package.
17174 -- That's behind us, so we have to manually analyze the
17175 -- inserted spec.
17177 Append_To (Visible_Declarations (Packg), PDecl);
17178 Analyze (PDecl);
17180 -- Body goes at the end of the private part of the package.
17181 -- That's ahead of us so it will get analyzed later on when
17182 -- we come to it.
17184 Append_To (Private_Declarations (Packg), PBody);
17186 -- Copy Invariant procedure to private declaration
17188 Set_Invariant_Procedure (Priv_T, Invariant_Procedure (Full_T));
17189 end if;
17190 end;
17191 end if;
17192 end Process_Full_View;
17194 -----------------------------------
17195 -- Process_Incomplete_Dependents --
17196 -----------------------------------
17198 procedure Process_Incomplete_Dependents
17199 (N : Node_Id;
17200 Full_T : Entity_Id;
17201 Inc_T : Entity_Id)
17203 Inc_Elmt : Elmt_Id;
17204 Priv_Dep : Entity_Id;
17205 New_Subt : Entity_Id;
17207 Disc_Constraint : Elist_Id;
17209 begin
17210 if No (Private_Dependents (Inc_T)) then
17211 return;
17212 end if;
17214 -- Itypes that may be generated by the completion of an incomplete
17215 -- subtype are not used by the back-end and not attached to the tree.
17216 -- They are created only for constraint-checking purposes.
17218 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
17219 while Present (Inc_Elmt) loop
17220 Priv_Dep := Node (Inc_Elmt);
17222 if Ekind (Priv_Dep) = E_Subprogram_Type then
17224 -- An Access_To_Subprogram type may have a return type or a
17225 -- parameter type that is incomplete. Replace with the full view.
17227 if Etype (Priv_Dep) = Inc_T then
17228 Set_Etype (Priv_Dep, Full_T);
17229 end if;
17231 declare
17232 Formal : Entity_Id;
17234 begin
17235 Formal := First_Formal (Priv_Dep);
17236 while Present (Formal) loop
17237 if Etype (Formal) = Inc_T then
17238 Set_Etype (Formal, Full_T);
17239 end if;
17241 Next_Formal (Formal);
17242 end loop;
17243 end;
17245 elsif Is_Overloadable (Priv_Dep) then
17247 -- A protected operation is never dispatching: only its
17248 -- wrapper operation (which has convention Ada) is.
17250 if Is_Tagged_Type (Full_T)
17251 and then Convention (Priv_Dep) /= Convention_Protected
17252 then
17254 -- Subprogram has an access parameter whose designated type
17255 -- was incomplete. Reexamine declaration now, because it may
17256 -- be a primitive operation of the full type.
17258 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
17259 Set_Is_Dispatching_Operation (Priv_Dep);
17260 Check_Controlling_Formals (Full_T, Priv_Dep);
17261 end if;
17263 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
17265 -- Can happen during processing of a body before the completion
17266 -- of a TA type. Ignore, because spec is also on dependent list.
17268 return;
17270 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
17271 -- corresponding subtype of the full view.
17273 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
17274 Set_Subtype_Indication
17275 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
17276 Set_Etype (Priv_Dep, Full_T);
17277 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
17278 Set_Analyzed (Parent (Priv_Dep), False);
17280 -- Reanalyze the declaration, suppressing the call to
17281 -- Enter_Name to avoid duplicate names.
17283 Analyze_Subtype_Declaration
17284 (N => Parent (Priv_Dep),
17285 Skip => True);
17287 -- Dependent is a subtype
17289 else
17290 -- We build a new subtype indication using the full view of the
17291 -- incomplete parent. The discriminant constraints have been
17292 -- elaborated already at the point of the subtype declaration.
17294 New_Subt := Create_Itype (E_Void, N);
17296 if Has_Discriminants (Full_T) then
17297 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
17298 else
17299 Disc_Constraint := No_Elist;
17300 end if;
17302 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
17303 Set_Full_View (Priv_Dep, New_Subt);
17304 end if;
17306 Next_Elmt (Inc_Elmt);
17307 end loop;
17308 end Process_Incomplete_Dependents;
17310 --------------------------------
17311 -- Process_Range_Expr_In_Decl --
17312 --------------------------------
17314 procedure Process_Range_Expr_In_Decl
17315 (R : Node_Id;
17316 T : Entity_Id;
17317 Check_List : List_Id := Empty_List;
17318 R_Check_Off : Boolean := False)
17320 Lo, Hi : Node_Id;
17321 R_Checks : Check_Result;
17322 Type_Decl : Node_Id;
17323 Def_Id : Entity_Id;
17325 begin
17326 Analyze_And_Resolve (R, Base_Type (T));
17328 if Nkind (R) = N_Range then
17329 Lo := Low_Bound (R);
17330 Hi := High_Bound (R);
17332 -- We need to ensure validity of the bounds here, because if we
17333 -- go ahead and do the expansion, then the expanded code will get
17334 -- analyzed with range checks suppressed and we miss the check.
17336 Validity_Check_Range (R);
17338 -- If there were errors in the declaration, try and patch up some
17339 -- common mistakes in the bounds. The cases handled are literals
17340 -- which are Integer where the expected type is Real and vice versa.
17341 -- These corrections allow the compilation process to proceed further
17342 -- along since some basic assumptions of the format of the bounds
17343 -- are guaranteed.
17345 if Etype (R) = Any_Type then
17347 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
17348 Rewrite (Lo,
17349 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
17351 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
17352 Rewrite (Hi,
17353 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
17355 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
17356 Rewrite (Lo,
17357 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
17359 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
17360 Rewrite (Hi,
17361 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
17362 end if;
17364 Set_Etype (Lo, T);
17365 Set_Etype (Hi, T);
17366 end if;
17368 -- If the bounds of the range have been mistakenly given as string
17369 -- literals (perhaps in place of character literals), then an error
17370 -- has already been reported, but we rewrite the string literal as a
17371 -- bound of the range's type to avoid blowups in later processing
17372 -- that looks at static values.
17374 if Nkind (Lo) = N_String_Literal then
17375 Rewrite (Lo,
17376 Make_Attribute_Reference (Sloc (Lo),
17377 Attribute_Name => Name_First,
17378 Prefix => New_Reference_To (T, Sloc (Lo))));
17379 Analyze_And_Resolve (Lo);
17380 end if;
17382 if Nkind (Hi) = N_String_Literal then
17383 Rewrite (Hi,
17384 Make_Attribute_Reference (Sloc (Hi),
17385 Attribute_Name => Name_First,
17386 Prefix => New_Reference_To (T, Sloc (Hi))));
17387 Analyze_And_Resolve (Hi);
17388 end if;
17390 -- If bounds aren't scalar at this point then exit, avoiding
17391 -- problems with further processing of the range in this procedure.
17393 if not Is_Scalar_Type (Etype (Lo)) then
17394 return;
17395 end if;
17397 -- Resolve (actually Sem_Eval) has checked that the bounds are in
17398 -- then range of the base type. Here we check whether the bounds
17399 -- are in the range of the subtype itself. Note that if the bounds
17400 -- represent the null range the Constraint_Error exception should
17401 -- not be raised.
17403 -- ??? The following code should be cleaned up as follows
17405 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
17406 -- is done in the call to Range_Check (R, T); below
17408 -- 2. The use of R_Check_Off should be investigated and possibly
17409 -- removed, this would clean up things a bit.
17411 if Is_Null_Range (Lo, Hi) then
17412 null;
17414 else
17415 -- Capture values of bounds and generate temporaries for them
17416 -- if needed, before applying checks, since checks may cause
17417 -- duplication of the expression without forcing evaluation.
17419 if Expander_Active then
17420 Force_Evaluation (Lo);
17421 Force_Evaluation (Hi);
17422 end if;
17424 -- We use a flag here instead of suppressing checks on the
17425 -- type because the type we check against isn't necessarily
17426 -- the place where we put the check.
17428 if not R_Check_Off then
17429 R_Checks := Get_Range_Checks (R, T);
17431 -- Look up tree to find an appropriate insertion point.
17432 -- This seems really junk code, and very brittle, couldn't
17433 -- we just use an insert actions call of some kind ???
17435 Type_Decl := Parent (R);
17436 while Present (Type_Decl) and then not
17437 (Nkind_In (Type_Decl, N_Full_Type_Declaration,
17438 N_Subtype_Declaration,
17439 N_Loop_Statement,
17440 N_Task_Type_Declaration)
17441 or else
17442 Nkind_In (Type_Decl, N_Single_Task_Declaration,
17443 N_Protected_Type_Declaration,
17444 N_Single_Protected_Declaration))
17445 loop
17446 Type_Decl := Parent (Type_Decl);
17447 end loop;
17449 -- Why would Type_Decl not be present??? Without this test,
17450 -- short regression tests fail.
17452 if Present (Type_Decl) then
17454 -- Case of loop statement (more comments ???)
17456 if Nkind (Type_Decl) = N_Loop_Statement then
17457 declare
17458 Indic : Node_Id;
17460 begin
17461 Indic := Parent (R);
17462 while Present (Indic)
17463 and then Nkind (Indic) /= N_Subtype_Indication
17464 loop
17465 Indic := Parent (Indic);
17466 end loop;
17468 if Present (Indic) then
17469 Def_Id := Etype (Subtype_Mark (Indic));
17471 Insert_Range_Checks
17472 (R_Checks,
17473 Type_Decl,
17474 Def_Id,
17475 Sloc (Type_Decl),
17477 Do_Before => True);
17478 end if;
17479 end;
17481 -- All other cases (more comments ???)
17483 else
17484 Def_Id := Defining_Identifier (Type_Decl);
17486 if (Ekind (Def_Id) = E_Record_Type
17487 and then Depends_On_Discriminant (R))
17488 or else
17489 (Ekind (Def_Id) = E_Protected_Type
17490 and then Has_Discriminants (Def_Id))
17491 then
17492 Append_Range_Checks
17493 (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
17495 else
17496 Insert_Range_Checks
17497 (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
17499 end if;
17500 end if;
17501 end if;
17502 end if;
17503 end if;
17505 elsif Expander_Active then
17506 Get_Index_Bounds (R, Lo, Hi);
17507 Force_Evaluation (Lo);
17508 Force_Evaluation (Hi);
17509 end if;
17510 end Process_Range_Expr_In_Decl;
17512 --------------------------------------
17513 -- Process_Real_Range_Specification --
17514 --------------------------------------
17516 procedure Process_Real_Range_Specification (Def : Node_Id) is
17517 Spec : constant Node_Id := Real_Range_Specification (Def);
17518 Lo : Node_Id;
17519 Hi : Node_Id;
17520 Err : Boolean := False;
17522 procedure Analyze_Bound (N : Node_Id);
17523 -- Analyze and check one bound
17525 -------------------
17526 -- Analyze_Bound --
17527 -------------------
17529 procedure Analyze_Bound (N : Node_Id) is
17530 begin
17531 Analyze_And_Resolve (N, Any_Real);
17533 if not Is_OK_Static_Expression (N) then
17534 Flag_Non_Static_Expr
17535 ("bound in real type definition is not static!", N);
17536 Err := True;
17537 end if;
17538 end Analyze_Bound;
17540 -- Start of processing for Process_Real_Range_Specification
17542 begin
17543 if Present (Spec) then
17544 Lo := Low_Bound (Spec);
17545 Hi := High_Bound (Spec);
17546 Analyze_Bound (Lo);
17547 Analyze_Bound (Hi);
17549 -- If error, clear away junk range specification
17551 if Err then
17552 Set_Real_Range_Specification (Def, Empty);
17553 end if;
17554 end if;
17555 end Process_Real_Range_Specification;
17557 ---------------------
17558 -- Process_Subtype --
17559 ---------------------
17561 function Process_Subtype
17562 (S : Node_Id;
17563 Related_Nod : Node_Id;
17564 Related_Id : Entity_Id := Empty;
17565 Suffix : Character := ' ') return Entity_Id
17567 P : Node_Id;
17568 Def_Id : Entity_Id;
17569 Error_Node : Node_Id;
17570 Full_View_Id : Entity_Id;
17571 Subtype_Mark_Id : Entity_Id;
17573 May_Have_Null_Exclusion : Boolean;
17575 procedure Check_Incomplete (T : Entity_Id);
17576 -- Called to verify that an incomplete type is not used prematurely
17578 ----------------------
17579 -- Check_Incomplete --
17580 ----------------------
17582 procedure Check_Incomplete (T : Entity_Id) is
17583 begin
17584 -- Ada 2005 (AI-412): Incomplete subtypes are legal
17586 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
17587 and then
17588 not (Ada_Version >= Ada_2005
17589 and then
17590 (Nkind (Parent (T)) = N_Subtype_Declaration
17591 or else
17592 (Nkind (Parent (T)) = N_Subtype_Indication
17593 and then Nkind (Parent (Parent (T))) =
17594 N_Subtype_Declaration)))
17595 then
17596 Error_Msg_N ("invalid use of type before its full declaration", T);
17597 end if;
17598 end Check_Incomplete;
17600 -- Start of processing for Process_Subtype
17602 begin
17603 -- Case of no constraints present
17605 if Nkind (S) /= N_Subtype_Indication then
17606 Find_Type (S);
17607 Check_Incomplete (S);
17608 P := Parent (S);
17610 -- Ada 2005 (AI-231): Static check
17612 if Ada_Version >= Ada_2005
17613 and then Present (P)
17614 and then Null_Exclusion_Present (P)
17615 and then Nkind (P) /= N_Access_To_Object_Definition
17616 and then not Is_Access_Type (Entity (S))
17617 then
17618 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
17619 end if;
17621 -- The following is ugly, can't we have a range or even a flag???
17623 May_Have_Null_Exclusion :=
17624 Nkind_In (P, N_Access_Definition,
17625 N_Access_Function_Definition,
17626 N_Access_Procedure_Definition,
17627 N_Access_To_Object_Definition,
17628 N_Allocator,
17629 N_Component_Definition)
17630 or else
17631 Nkind_In (P, N_Derived_Type_Definition,
17632 N_Discriminant_Specification,
17633 N_Formal_Object_Declaration,
17634 N_Object_Declaration,
17635 N_Object_Renaming_Declaration,
17636 N_Parameter_Specification,
17637 N_Subtype_Declaration);
17639 -- Create an Itype that is a duplicate of Entity (S) but with the
17640 -- null-exclusion attribute.
17642 if May_Have_Null_Exclusion
17643 and then Is_Access_Type (Entity (S))
17644 and then Null_Exclusion_Present (P)
17646 -- No need to check the case of an access to object definition.
17647 -- It is correct to define double not-null pointers.
17649 -- Example:
17650 -- type Not_Null_Int_Ptr is not null access Integer;
17651 -- type Acc is not null access Not_Null_Int_Ptr;
17653 and then Nkind (P) /= N_Access_To_Object_Definition
17654 then
17655 if Can_Never_Be_Null (Entity (S)) then
17656 case Nkind (Related_Nod) is
17657 when N_Full_Type_Declaration =>
17658 if Nkind (Type_Definition (Related_Nod))
17659 in N_Array_Type_Definition
17660 then
17661 Error_Node :=
17662 Subtype_Indication
17663 (Component_Definition
17664 (Type_Definition (Related_Nod)));
17665 else
17666 Error_Node :=
17667 Subtype_Indication (Type_Definition (Related_Nod));
17668 end if;
17670 when N_Subtype_Declaration =>
17671 Error_Node := Subtype_Indication (Related_Nod);
17673 when N_Object_Declaration =>
17674 Error_Node := Object_Definition (Related_Nod);
17676 when N_Component_Declaration =>
17677 Error_Node :=
17678 Subtype_Indication (Component_Definition (Related_Nod));
17680 when N_Allocator =>
17681 Error_Node := Expression (Related_Nod);
17683 when others =>
17684 pragma Assert (False);
17685 Error_Node := Related_Nod;
17686 end case;
17688 Error_Msg_NE
17689 ("`NOT NULL` not allowed (& already excludes null)",
17690 Error_Node,
17691 Entity (S));
17692 end if;
17694 Set_Etype (S,
17695 Create_Null_Excluding_Itype
17696 (T => Entity (S),
17697 Related_Nod => P));
17698 Set_Entity (S, Etype (S));
17699 end if;
17701 return Entity (S);
17703 -- Case of constraint present, so that we have an N_Subtype_Indication
17704 -- node (this node is created only if constraints are present).
17706 else
17707 Find_Type (Subtype_Mark (S));
17709 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
17710 and then not
17711 (Nkind (Parent (S)) = N_Subtype_Declaration
17712 and then Is_Itype (Defining_Identifier (Parent (S))))
17713 then
17714 Check_Incomplete (Subtype_Mark (S));
17715 end if;
17717 P := Parent (S);
17718 Subtype_Mark_Id := Entity (Subtype_Mark (S));
17720 -- Explicit subtype declaration case
17722 if Nkind (P) = N_Subtype_Declaration then
17723 Def_Id := Defining_Identifier (P);
17725 -- Explicit derived type definition case
17727 elsif Nkind (P) = N_Derived_Type_Definition then
17728 Def_Id := Defining_Identifier (Parent (P));
17730 -- Implicit case, the Def_Id must be created as an implicit type.
17731 -- The one exception arises in the case of concurrent types, array
17732 -- and access types, where other subsidiary implicit types may be
17733 -- created and must appear before the main implicit type. In these
17734 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
17735 -- has not yet been called to create Def_Id.
17737 else
17738 if Is_Array_Type (Subtype_Mark_Id)
17739 or else Is_Concurrent_Type (Subtype_Mark_Id)
17740 or else Is_Access_Type (Subtype_Mark_Id)
17741 then
17742 Def_Id := Empty;
17744 -- For the other cases, we create a new unattached Itype,
17745 -- and set the indication to ensure it gets attached later.
17747 else
17748 Def_Id :=
17749 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
17750 end if;
17751 end if;
17753 -- If the kind of constraint is invalid for this kind of type,
17754 -- then give an error, and then pretend no constraint was given.
17756 if not Is_Valid_Constraint_Kind
17757 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
17758 then
17759 Error_Msg_N
17760 ("incorrect constraint for this kind of type", Constraint (S));
17762 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17764 -- Set Ekind of orphan itype, to prevent cascaded errors
17766 if Present (Def_Id) then
17767 Set_Ekind (Def_Id, Ekind (Any_Type));
17768 end if;
17770 -- Make recursive call, having got rid of the bogus constraint
17772 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
17773 end if;
17775 -- Remaining processing depends on type
17777 case Ekind (Subtype_Mark_Id) is
17778 when Access_Kind =>
17779 Constrain_Access (Def_Id, S, Related_Nod);
17781 if Expander_Active
17782 and then Is_Itype (Designated_Type (Def_Id))
17783 and then Nkind (Related_Nod) = N_Subtype_Declaration
17784 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
17785 then
17786 Build_Itype_Reference
17787 (Designated_Type (Def_Id), Related_Nod);
17788 end if;
17790 when Array_Kind =>
17791 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
17793 when Decimal_Fixed_Point_Kind =>
17794 Constrain_Decimal (Def_Id, S);
17796 when Enumeration_Kind =>
17797 Constrain_Enumeration (Def_Id, S);
17799 when Ordinary_Fixed_Point_Kind =>
17800 Constrain_Ordinary_Fixed (Def_Id, S);
17802 when Float_Kind =>
17803 Constrain_Float (Def_Id, S);
17805 when Integer_Kind =>
17806 Constrain_Integer (Def_Id, S);
17808 when E_Record_Type |
17809 E_Record_Subtype |
17810 Class_Wide_Kind |
17811 E_Incomplete_Type =>
17812 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
17814 if Ekind (Def_Id) = E_Incomplete_Type then
17815 Set_Private_Dependents (Def_Id, New_Elmt_List);
17816 end if;
17818 when Private_Kind =>
17819 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
17820 Set_Private_Dependents (Def_Id, New_Elmt_List);
17822 -- In case of an invalid constraint prevent further processing
17823 -- since the type constructed is missing expected fields.
17825 if Etype (Def_Id) = Any_Type then
17826 return Def_Id;
17827 end if;
17829 -- If the full view is that of a task with discriminants,
17830 -- we must constrain both the concurrent type and its
17831 -- corresponding record type. Otherwise we will just propagate
17832 -- the constraint to the full view, if available.
17834 if Present (Full_View (Subtype_Mark_Id))
17835 and then Has_Discriminants (Subtype_Mark_Id)
17836 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
17837 then
17838 Full_View_Id :=
17839 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
17841 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
17842 Constrain_Concurrent (Full_View_Id, S,
17843 Related_Nod, Related_Id, Suffix);
17844 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
17845 Set_Full_View (Def_Id, Full_View_Id);
17847 -- Introduce an explicit reference to the private subtype,
17848 -- to prevent scope anomalies in gigi if first use appears
17849 -- in a nested context, e.g. a later function body.
17850 -- Should this be generated in other contexts than a full
17851 -- type declaration?
17853 if Is_Itype (Def_Id)
17854 and then
17855 Nkind (Parent (P)) = N_Full_Type_Declaration
17856 then
17857 Build_Itype_Reference (Def_Id, Parent (P));
17858 end if;
17860 else
17861 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
17862 end if;
17864 when Concurrent_Kind =>
17865 Constrain_Concurrent (Def_Id, S,
17866 Related_Nod, Related_Id, Suffix);
17868 when others =>
17869 Error_Msg_N ("invalid subtype mark in subtype indication", S);
17870 end case;
17872 -- Size and Convention are always inherited from the base type
17874 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
17875 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
17877 return Def_Id;
17878 end if;
17879 end Process_Subtype;
17881 ---------------------------------------
17882 -- Check_Anonymous_Access_Components --
17883 ---------------------------------------
17885 procedure Check_Anonymous_Access_Components
17886 (Typ_Decl : Node_Id;
17887 Typ : Entity_Id;
17888 Prev : Entity_Id;
17889 Comp_List : Node_Id)
17891 Loc : constant Source_Ptr := Sloc (Typ_Decl);
17892 Anon_Access : Entity_Id;
17893 Acc_Def : Node_Id;
17894 Comp : Node_Id;
17895 Comp_Def : Node_Id;
17896 Decl : Node_Id;
17897 Type_Def : Node_Id;
17899 procedure Build_Incomplete_Type_Declaration;
17900 -- If the record type contains components that include an access to the
17901 -- current record, then create an incomplete type declaration for the
17902 -- record, to be used as the designated type of the anonymous access.
17903 -- This is done only once, and only if there is no previous partial
17904 -- view of the type.
17906 function Designates_T (Subt : Node_Id) return Boolean;
17907 -- Check whether a node designates the enclosing record type, or 'Class
17908 -- of that type
17910 function Mentions_T (Acc_Def : Node_Id) return Boolean;
17911 -- Check whether an access definition includes a reference to
17912 -- the enclosing record type. The reference can be a subtype mark
17913 -- in the access definition itself, a 'Class attribute reference, or
17914 -- recursively a reference appearing in a parameter specification
17915 -- or result definition of an access_to_subprogram definition.
17917 --------------------------------------
17918 -- Build_Incomplete_Type_Declaration --
17919 --------------------------------------
17921 procedure Build_Incomplete_Type_Declaration is
17922 Decl : Node_Id;
17923 Inc_T : Entity_Id;
17924 H : Entity_Id;
17926 -- Is_Tagged indicates whether the type is tagged. It is tagged if
17927 -- it's "is new ... with record" or else "is tagged record ...".
17929 Is_Tagged : constant Boolean :=
17930 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
17931 and then
17932 Present
17933 (Record_Extension_Part (Type_Definition (Typ_Decl))))
17934 or else
17935 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
17936 and then Tagged_Present (Type_Definition (Typ_Decl)));
17938 begin
17939 -- If there is a previous partial view, no need to create a new one
17940 -- If the partial view, given by Prev, is incomplete, If Prev is
17941 -- a private declaration, full declaration is flagged accordingly.
17943 if Prev /= Typ then
17944 if Is_Tagged then
17945 Make_Class_Wide_Type (Prev);
17946 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
17947 Set_Etype (Class_Wide_Type (Typ), Typ);
17948 end if;
17950 return;
17952 elsif Has_Private_Declaration (Typ) then
17954 -- If we refer to T'Class inside T, and T is the completion of a
17955 -- private type, then we need to make sure the class-wide type
17956 -- exists.
17958 if Is_Tagged then
17959 Make_Class_Wide_Type (Typ);
17960 end if;
17962 return;
17964 -- If there was a previous anonymous access type, the incomplete
17965 -- type declaration will have been created already.
17967 elsif Present (Current_Entity (Typ))
17968 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
17969 and then Full_View (Current_Entity (Typ)) = Typ
17970 then
17971 if Is_Tagged
17972 and then Comes_From_Source (Current_Entity (Typ))
17973 and then not Is_Tagged_Type (Current_Entity (Typ))
17974 then
17975 Make_Class_Wide_Type (Typ);
17976 Error_Msg_N
17977 ("incomplete view of tagged type should be declared tagged?",
17978 Parent (Current_Entity (Typ)));
17979 end if;
17980 return;
17982 else
17983 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
17984 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
17986 -- Type has already been inserted into the current scope. Remove
17987 -- it, and add incomplete declaration for type, so that subsequent
17988 -- anonymous access types can use it. The entity is unchained from
17989 -- the homonym list and from immediate visibility. After analysis,
17990 -- the entity in the incomplete declaration becomes immediately
17991 -- visible in the record declaration that follows.
17993 H := Current_Entity (Typ);
17995 if H = Typ then
17996 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
17997 else
17998 while Present (H)
17999 and then Homonym (H) /= Typ
18000 loop
18001 H := Homonym (Typ);
18002 end loop;
18004 Set_Homonym (H, Homonym (Typ));
18005 end if;
18007 Insert_Before (Typ_Decl, Decl);
18008 Analyze (Decl);
18009 Set_Full_View (Inc_T, Typ);
18011 if Is_Tagged then
18013 -- Create a common class-wide type for both views, and set the
18014 -- Etype of the class-wide type to the full view.
18016 Make_Class_Wide_Type (Inc_T);
18017 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
18018 Set_Etype (Class_Wide_Type (Typ), Typ);
18019 end if;
18020 end if;
18021 end Build_Incomplete_Type_Declaration;
18023 ------------------
18024 -- Designates_T --
18025 ------------------
18027 function Designates_T (Subt : Node_Id) return Boolean is
18028 Type_Id : constant Name_Id := Chars (Typ);
18030 function Names_T (Nam : Node_Id) return Boolean;
18031 -- The record type has not been introduced in the current scope
18032 -- yet, so we must examine the name of the type itself, either
18033 -- an identifier T, or an expanded name of the form P.T, where
18034 -- P denotes the current scope.
18036 -------------
18037 -- Names_T --
18038 -------------
18040 function Names_T (Nam : Node_Id) return Boolean is
18041 begin
18042 if Nkind (Nam) = N_Identifier then
18043 return Chars (Nam) = Type_Id;
18045 elsif Nkind (Nam) = N_Selected_Component then
18046 if Chars (Selector_Name (Nam)) = Type_Id then
18047 if Nkind (Prefix (Nam)) = N_Identifier then
18048 return Chars (Prefix (Nam)) = Chars (Current_Scope);
18050 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
18051 return Chars (Selector_Name (Prefix (Nam))) =
18052 Chars (Current_Scope);
18053 else
18054 return False;
18055 end if;
18057 else
18058 return False;
18059 end if;
18061 else
18062 return False;
18063 end if;
18064 end Names_T;
18066 -- Start of processing for Designates_T
18068 begin
18069 if Nkind (Subt) = N_Identifier then
18070 return Chars (Subt) = Type_Id;
18072 -- Reference can be through an expanded name which has not been
18073 -- analyzed yet, and which designates enclosing scopes.
18075 elsif Nkind (Subt) = N_Selected_Component then
18076 if Names_T (Subt) then
18077 return True;
18079 -- Otherwise it must denote an entity that is already visible.
18080 -- The access definition may name a subtype of the enclosing
18081 -- type, if there is a previous incomplete declaration for it.
18083 else
18084 Find_Selected_Component (Subt);
18085 return
18086 Is_Entity_Name (Subt)
18087 and then Scope (Entity (Subt)) = Current_Scope
18088 and then
18089 (Chars (Base_Type (Entity (Subt))) = Type_Id
18090 or else
18091 (Is_Class_Wide_Type (Entity (Subt))
18092 and then
18093 Chars (Etype (Base_Type (Entity (Subt)))) =
18094 Type_Id));
18095 end if;
18097 -- A reference to the current type may appear as the prefix of
18098 -- a 'Class attribute.
18100 elsif Nkind (Subt) = N_Attribute_Reference
18101 and then Attribute_Name (Subt) = Name_Class
18102 then
18103 return Names_T (Prefix (Subt));
18105 else
18106 return False;
18107 end if;
18108 end Designates_T;
18110 ----------------
18111 -- Mentions_T --
18112 ----------------
18114 function Mentions_T (Acc_Def : Node_Id) return Boolean is
18115 Param_Spec : Node_Id;
18117 Acc_Subprg : constant Node_Id :=
18118 Access_To_Subprogram_Definition (Acc_Def);
18120 begin
18121 if No (Acc_Subprg) then
18122 return Designates_T (Subtype_Mark (Acc_Def));
18123 end if;
18125 -- Component is an access_to_subprogram: examine its formals,
18126 -- and result definition in the case of an access_to_function.
18128 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
18129 while Present (Param_Spec) loop
18130 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
18131 and then Mentions_T (Parameter_Type (Param_Spec))
18132 then
18133 return True;
18135 elsif Designates_T (Parameter_Type (Param_Spec)) then
18136 return True;
18137 end if;
18139 Next (Param_Spec);
18140 end loop;
18142 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
18143 if Nkind (Result_Definition (Acc_Subprg)) =
18144 N_Access_Definition
18145 then
18146 return Mentions_T (Result_Definition (Acc_Subprg));
18147 else
18148 return Designates_T (Result_Definition (Acc_Subprg));
18149 end if;
18150 end if;
18152 return False;
18153 end Mentions_T;
18155 -- Start of processing for Check_Anonymous_Access_Components
18157 begin
18158 if No (Comp_List) then
18159 return;
18160 end if;
18162 Comp := First (Component_Items (Comp_List));
18163 while Present (Comp) loop
18164 if Nkind (Comp) = N_Component_Declaration
18165 and then Present
18166 (Access_Definition (Component_Definition (Comp)))
18167 and then
18168 Mentions_T (Access_Definition (Component_Definition (Comp)))
18169 then
18170 Comp_Def := Component_Definition (Comp);
18171 Acc_Def :=
18172 Access_To_Subprogram_Definition
18173 (Access_Definition (Comp_Def));
18175 Build_Incomplete_Type_Declaration;
18176 Anon_Access := Make_Temporary (Loc, 'S');
18178 -- Create a declaration for the anonymous access type: either
18179 -- an access_to_object or an access_to_subprogram.
18181 if Present (Acc_Def) then
18182 if Nkind (Acc_Def) = N_Access_Function_Definition then
18183 Type_Def :=
18184 Make_Access_Function_Definition (Loc,
18185 Parameter_Specifications =>
18186 Parameter_Specifications (Acc_Def),
18187 Result_Definition => Result_Definition (Acc_Def));
18188 else
18189 Type_Def :=
18190 Make_Access_Procedure_Definition (Loc,
18191 Parameter_Specifications =>
18192 Parameter_Specifications (Acc_Def));
18193 end if;
18195 else
18196 Type_Def :=
18197 Make_Access_To_Object_Definition (Loc,
18198 Subtype_Indication =>
18199 Relocate_Node
18200 (Subtype_Mark
18201 (Access_Definition (Comp_Def))));
18203 Set_Constant_Present
18204 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
18205 Set_All_Present
18206 (Type_Def, All_Present (Access_Definition (Comp_Def)));
18207 end if;
18209 Set_Null_Exclusion_Present
18210 (Type_Def,
18211 Null_Exclusion_Present (Access_Definition (Comp_Def)));
18213 Decl :=
18214 Make_Full_Type_Declaration (Loc,
18215 Defining_Identifier => Anon_Access,
18216 Type_Definition => Type_Def);
18218 Insert_Before (Typ_Decl, Decl);
18219 Analyze (Decl);
18221 -- If an access to object, Preserve entity of designated type,
18222 -- for ASIS use, before rewriting the component definition.
18224 if No (Acc_Def) then
18225 declare
18226 Desig : Entity_Id;
18228 begin
18229 Desig := Entity (Subtype_Indication (Type_Def));
18231 -- If the access definition is to the current record,
18232 -- the visible entity at this point is an incomplete
18233 -- type. Retrieve the full view to simplify ASIS queries
18235 if Ekind (Desig) = E_Incomplete_Type then
18236 Desig := Full_View (Desig);
18237 end if;
18239 Set_Entity
18240 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
18241 end;
18242 end if;
18244 Rewrite (Comp_Def,
18245 Make_Component_Definition (Loc,
18246 Subtype_Indication =>
18247 New_Occurrence_Of (Anon_Access, Loc)));
18249 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
18250 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
18251 else
18252 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
18253 end if;
18255 Set_Is_Local_Anonymous_Access (Anon_Access);
18256 end if;
18258 Next (Comp);
18259 end loop;
18261 if Present (Variant_Part (Comp_List)) then
18262 declare
18263 V : Node_Id;
18264 begin
18265 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
18266 while Present (V) loop
18267 Check_Anonymous_Access_Components
18268 (Typ_Decl, Typ, Prev, Component_List (V));
18269 Next_Non_Pragma (V);
18270 end loop;
18271 end;
18272 end if;
18273 end Check_Anonymous_Access_Components;
18275 --------------------------------
18276 -- Preanalyze_Spec_Expression --
18277 --------------------------------
18279 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18280 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18281 begin
18282 In_Spec_Expression := True;
18283 Preanalyze_And_Resolve (N, T);
18284 In_Spec_Expression := Save_In_Spec_Expression;
18285 end Preanalyze_Spec_Expression;
18287 -----------------------------
18288 -- Record_Type_Declaration --
18289 -----------------------------
18291 procedure Record_Type_Declaration
18292 (T : Entity_Id;
18293 N : Node_Id;
18294 Prev : Entity_Id)
18296 Def : constant Node_Id := Type_Definition (N);
18297 Is_Tagged : Boolean;
18298 Tag_Comp : Entity_Id;
18300 begin
18301 -- These flags must be initialized before calling Process_Discriminants
18302 -- because this routine makes use of them.
18304 Set_Ekind (T, E_Record_Type);
18305 Set_Etype (T, T);
18306 Init_Size_Align (T);
18307 Set_Interfaces (T, No_Elist);
18308 Set_Stored_Constraint (T, No_Elist);
18310 -- Normal case
18312 if Ada_Version < Ada_2005
18313 or else not Interface_Present (Def)
18314 then
18315 -- The flag Is_Tagged_Type might have already been set by
18316 -- Find_Type_Name if it detected an error for declaration T. This
18317 -- arises in the case of private tagged types where the full view
18318 -- omits the word tagged.
18320 Is_Tagged :=
18321 Tagged_Present (Def)
18322 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
18324 Set_Is_Tagged_Type (T, Is_Tagged);
18325 Set_Is_Limited_Record (T, Limited_Present (Def));
18327 -- Type is abstract if full declaration carries keyword, or if
18328 -- previous partial view did.
18330 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
18331 or else Abstract_Present (Def));
18333 else
18334 Is_Tagged := True;
18335 Analyze_Interface_Declaration (T, Def);
18337 if Present (Discriminant_Specifications (N)) then
18338 Error_Msg_N
18339 ("interface types cannot have discriminants",
18340 Defining_Identifier
18341 (First (Discriminant_Specifications (N))));
18342 end if;
18343 end if;
18345 -- First pass: if there are self-referential access components,
18346 -- create the required anonymous access type declarations, and if
18347 -- need be an incomplete type declaration for T itself.
18349 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
18351 if Ada_Version >= Ada_2005
18352 and then Present (Interface_List (Def))
18353 then
18354 Check_Interfaces (N, Def);
18356 declare
18357 Ifaces_List : Elist_Id;
18359 begin
18360 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
18361 -- already in the parents.
18363 Collect_Interfaces
18364 (T => T,
18365 Ifaces_List => Ifaces_List,
18366 Exclude_Parents => True);
18368 Set_Interfaces (T, Ifaces_List);
18369 end;
18370 end if;
18372 -- Records constitute a scope for the component declarations within.
18373 -- The scope is created prior to the processing of these declarations.
18374 -- Discriminants are processed first, so that they are visible when
18375 -- processing the other components. The Ekind of the record type itself
18376 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
18378 -- Enter record scope
18380 Push_Scope (T);
18382 -- If an incomplete or private type declaration was already given for
18383 -- the type, then this scope already exists, and the discriminants have
18384 -- been declared within. We must verify that the full declaration
18385 -- matches the incomplete one.
18387 Check_Or_Process_Discriminants (N, T, Prev);
18389 Set_Is_Constrained (T, not Has_Discriminants (T));
18390 Set_Has_Delayed_Freeze (T, True);
18392 -- For tagged types add a manually analyzed component corresponding
18393 -- to the component _tag, the corresponding piece of tree will be
18394 -- expanded as part of the freezing actions if it is not a CPP_Class.
18396 if Is_Tagged then
18398 -- Do not add the tag unless we are in expansion mode
18400 if Expander_Active then
18401 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
18402 Enter_Name (Tag_Comp);
18404 Set_Ekind (Tag_Comp, E_Component);
18405 Set_Is_Tag (Tag_Comp);
18406 Set_Is_Aliased (Tag_Comp);
18407 Set_Etype (Tag_Comp, RTE (RE_Tag));
18408 Set_DT_Entry_Count (Tag_Comp, No_Uint);
18409 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
18410 Init_Component_Location (Tag_Comp);
18412 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
18413 -- implemented interfaces.
18415 if Has_Interfaces (T) then
18416 Add_Interface_Tag_Components (N, T);
18417 end if;
18418 end if;
18420 Make_Class_Wide_Type (T);
18421 Set_Direct_Primitive_Operations (T, New_Elmt_List);
18422 end if;
18424 -- We must suppress range checks when processing record components in
18425 -- the presence of discriminants, since we don't want spurious checks to
18426 -- be generated during their analysis, but Suppress_Range_Checks flags
18427 -- must be reset the after processing the record definition.
18429 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
18430 -- couldn't we just use the normal range check suppression method here.
18431 -- That would seem cleaner ???
18433 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
18434 Set_Kill_Range_Checks (T, True);
18435 Record_Type_Definition (Def, Prev);
18436 Set_Kill_Range_Checks (T, False);
18437 else
18438 Record_Type_Definition (Def, Prev);
18439 end if;
18441 -- Exit from record scope
18443 End_Scope;
18445 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
18446 -- the implemented interfaces and associate them an aliased entity.
18448 if Is_Tagged
18449 and then not Is_Empty_List (Interface_List (Def))
18450 then
18451 Derive_Progenitor_Subprograms (T, T);
18452 end if;
18453 end Record_Type_Declaration;
18455 ----------------------------
18456 -- Record_Type_Definition --
18457 ----------------------------
18459 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
18460 Component : Entity_Id;
18461 Ctrl_Components : Boolean := False;
18462 Final_Storage_Only : Boolean;
18463 T : Entity_Id;
18465 begin
18466 if Ekind (Prev_T) = E_Incomplete_Type then
18467 T := Full_View (Prev_T);
18468 else
18469 T := Prev_T;
18470 end if;
18472 Final_Storage_Only := not Is_Controlled (T);
18474 -- Ada 2005: check whether an explicit Limited is present in a derived
18475 -- type declaration.
18477 if Nkind (Parent (Def)) = N_Derived_Type_Definition
18478 and then Limited_Present (Parent (Def))
18479 then
18480 Set_Is_Limited_Record (T);
18481 end if;
18483 -- If the component list of a record type is defined by the reserved
18484 -- word null and there is no discriminant part, then the record type has
18485 -- no components and all records of the type are null records (RM 3.7)
18486 -- This procedure is also called to process the extension part of a
18487 -- record extension, in which case the current scope may have inherited
18488 -- components.
18490 if No (Def)
18491 or else No (Component_List (Def))
18492 or else Null_Present (Component_List (Def))
18493 then
18494 null;
18496 else
18497 Analyze_Declarations (Component_Items (Component_List (Def)));
18499 if Present (Variant_Part (Component_List (Def))) then
18500 Analyze (Variant_Part (Component_List (Def)));
18501 end if;
18502 end if;
18504 -- After completing the semantic analysis of the record definition,
18505 -- record components, both new and inherited, are accessible. Set their
18506 -- kind accordingly. Exclude malformed itypes from illegal declarations,
18507 -- whose Ekind may be void.
18509 Component := First_Entity (Current_Scope);
18510 while Present (Component) loop
18511 if Ekind (Component) = E_Void
18512 and then not Is_Itype (Component)
18513 then
18514 Set_Ekind (Component, E_Component);
18515 Init_Component_Location (Component);
18516 end if;
18518 if Has_Task (Etype (Component)) then
18519 Set_Has_Task (T);
18520 end if;
18522 if Ekind (Component) /= E_Component then
18523 null;
18525 -- Do not set Has_Controlled_Component on a class-wide equivalent
18526 -- type. See Make_CW_Equivalent_Type.
18528 elsif not Is_Class_Wide_Equivalent_Type (T)
18529 and then (Has_Controlled_Component (Etype (Component))
18530 or else (Chars (Component) /= Name_uParent
18531 and then Is_Controlled (Etype (Component))))
18532 then
18533 Set_Has_Controlled_Component (T, True);
18534 Final_Storage_Only :=
18535 Final_Storage_Only
18536 and then Finalize_Storage_Only (Etype (Component));
18537 Ctrl_Components := True;
18538 end if;
18540 Next_Entity (Component);
18541 end loop;
18543 -- A Type is Finalize_Storage_Only only if all its controlled components
18544 -- are also.
18546 if Ctrl_Components then
18547 Set_Finalize_Storage_Only (T, Final_Storage_Only);
18548 end if;
18550 -- Place reference to end record on the proper entity, which may
18551 -- be a partial view.
18553 if Present (Def) then
18554 Process_End_Label (Def, 'e', Prev_T);
18555 end if;
18556 end Record_Type_Definition;
18558 ------------------------
18559 -- Replace_Components --
18560 ------------------------
18562 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
18563 function Process (N : Node_Id) return Traverse_Result;
18565 -------------
18566 -- Process --
18567 -------------
18569 function Process (N : Node_Id) return Traverse_Result is
18570 Comp : Entity_Id;
18572 begin
18573 if Nkind (N) = N_Discriminant_Specification then
18574 Comp := First_Discriminant (Typ);
18575 while Present (Comp) loop
18576 if Chars (Comp) = Chars (Defining_Identifier (N)) then
18577 Set_Defining_Identifier (N, Comp);
18578 exit;
18579 end if;
18581 Next_Discriminant (Comp);
18582 end loop;
18584 elsif Nkind (N) = N_Component_Declaration then
18585 Comp := First_Component (Typ);
18586 while Present (Comp) loop
18587 if Chars (Comp) = Chars (Defining_Identifier (N)) then
18588 Set_Defining_Identifier (N, Comp);
18589 exit;
18590 end if;
18592 Next_Component (Comp);
18593 end loop;
18594 end if;
18596 return OK;
18597 end Process;
18599 procedure Replace is new Traverse_Proc (Process);
18601 -- Start of processing for Replace_Components
18603 begin
18604 Replace (Decl);
18605 end Replace_Components;
18607 -------------------------------
18608 -- Set_Completion_Referenced --
18609 -------------------------------
18611 procedure Set_Completion_Referenced (E : Entity_Id) is
18612 begin
18613 -- If in main unit, mark entity that is a completion as referenced,
18614 -- warnings go on the partial view when needed.
18616 if In_Extended_Main_Source_Unit (E) then
18617 Set_Referenced (E);
18618 end if;
18619 end Set_Completion_Referenced;
18621 ---------------------
18622 -- Set_Fixed_Range --
18623 ---------------------
18625 -- The range for fixed-point types is complicated by the fact that we
18626 -- do not know the exact end points at the time of the declaration. This
18627 -- is true for three reasons:
18629 -- A size clause may affect the fudging of the end-points
18630 -- A small clause may affect the values of the end-points
18631 -- We try to include the end-points if it does not affect the size
18633 -- This means that the actual end-points must be established at the point
18634 -- when the type is frozen. Meanwhile, we first narrow the range as
18635 -- permitted (so that it will fit if necessary in a small specified size),
18636 -- and then build a range subtree with these narrowed bounds.
18638 -- Set_Fixed_Range constructs the range from real literal values, and sets
18639 -- the range as the Scalar_Range of the given fixed-point type entity.
18641 -- The parent of this range is set to point to the entity so that it is
18642 -- properly hooked into the tree (unlike normal Scalar_Range entries for
18643 -- other scalar types, which are just pointers to the range in the
18644 -- original tree, this would otherwise be an orphan).
18646 -- The tree is left unanalyzed. When the type is frozen, the processing
18647 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
18648 -- analyzed, and uses this as an indication that it should complete
18649 -- work on the range (it will know the final small and size values).
18651 procedure Set_Fixed_Range
18652 (E : Entity_Id;
18653 Loc : Source_Ptr;
18654 Lo : Ureal;
18655 Hi : Ureal)
18657 S : constant Node_Id :=
18658 Make_Range (Loc,
18659 Low_Bound => Make_Real_Literal (Loc, Lo),
18660 High_Bound => Make_Real_Literal (Loc, Hi));
18661 begin
18662 Set_Scalar_Range (E, S);
18663 Set_Parent (S, E);
18664 end Set_Fixed_Range;
18666 ----------------------------------
18667 -- Set_Scalar_Range_For_Subtype --
18668 ----------------------------------
18670 procedure Set_Scalar_Range_For_Subtype
18671 (Def_Id : Entity_Id;
18672 R : Node_Id;
18673 Subt : Entity_Id)
18675 Kind : constant Entity_Kind := Ekind (Def_Id);
18677 begin
18678 -- Defend against previous error
18680 if Nkind (R) = N_Error then
18681 return;
18682 end if;
18684 Set_Scalar_Range (Def_Id, R);
18686 -- We need to link the range into the tree before resolving it so
18687 -- that types that are referenced, including importantly the subtype
18688 -- itself, are properly frozen (Freeze_Expression requires that the
18689 -- expression be properly linked into the tree). Of course if it is
18690 -- already linked in, then we do not disturb the current link.
18692 if No (Parent (R)) then
18693 Set_Parent (R, Def_Id);
18694 end if;
18696 -- Reset the kind of the subtype during analysis of the range, to
18697 -- catch possible premature use in the bounds themselves.
18699 Set_Ekind (Def_Id, E_Void);
18700 Process_Range_Expr_In_Decl (R, Subt);
18701 Set_Ekind (Def_Id, Kind);
18702 end Set_Scalar_Range_For_Subtype;
18704 --------------------------------------------------------
18705 -- Set_Stored_Constraint_From_Discriminant_Constraint --
18706 --------------------------------------------------------
18708 procedure Set_Stored_Constraint_From_Discriminant_Constraint
18709 (E : Entity_Id)
18711 begin
18712 -- Make sure set if encountered during Expand_To_Stored_Constraint
18714 Set_Stored_Constraint (E, No_Elist);
18716 -- Give it the right value
18718 if Is_Constrained (E) and then Has_Discriminants (E) then
18719 Set_Stored_Constraint (E,
18720 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
18721 end if;
18722 end Set_Stored_Constraint_From_Discriminant_Constraint;
18724 -------------------------------------
18725 -- Signed_Integer_Type_Declaration --
18726 -------------------------------------
18728 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18729 Implicit_Base : Entity_Id;
18730 Base_Typ : Entity_Id;
18731 Lo_Val : Uint;
18732 Hi_Val : Uint;
18733 Errs : Boolean := False;
18734 Lo : Node_Id;
18735 Hi : Node_Id;
18737 function Can_Derive_From (E : Entity_Id) return Boolean;
18738 -- Determine whether given bounds allow derivation from specified type
18740 procedure Check_Bound (Expr : Node_Id);
18741 -- Check bound to make sure it is integral and static. If not, post
18742 -- appropriate error message and set Errs flag
18744 ---------------------
18745 -- Can_Derive_From --
18746 ---------------------
18748 -- Note we check both bounds against both end values, to deal with
18749 -- strange types like ones with a range of 0 .. -12341234.
18751 function Can_Derive_From (E : Entity_Id) return Boolean is
18752 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
18753 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
18754 begin
18755 return Lo <= Lo_Val and then Lo_Val <= Hi
18756 and then
18757 Lo <= Hi_Val and then Hi_Val <= Hi;
18758 end Can_Derive_From;
18760 -----------------
18761 -- Check_Bound --
18762 -----------------
18764 procedure Check_Bound (Expr : Node_Id) is
18765 begin
18766 -- If a range constraint is used as an integer type definition, each
18767 -- bound of the range must be defined by a static expression of some
18768 -- integer type, but the two bounds need not have the same integer
18769 -- type (Negative bounds are allowed.) (RM 3.5.4)
18771 if not Is_Integer_Type (Etype (Expr)) then
18772 Error_Msg_N
18773 ("integer type definition bounds must be of integer type", Expr);
18774 Errs := True;
18776 elsif not Is_OK_Static_Expression (Expr) then
18777 Flag_Non_Static_Expr
18778 ("non-static expression used for integer type bound!", Expr);
18779 Errs := True;
18781 -- The bounds are folded into literals, and we set their type to be
18782 -- universal, to avoid typing difficulties: we cannot set the type
18783 -- of the literal to the new type, because this would be a forward
18784 -- reference for the back end, and if the original type is user-
18785 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
18787 else
18788 if Is_Entity_Name (Expr) then
18789 Fold_Uint (Expr, Expr_Value (Expr), True);
18790 end if;
18792 Set_Etype (Expr, Universal_Integer);
18793 end if;
18794 end Check_Bound;
18796 -- Start of processing for Signed_Integer_Type_Declaration
18798 begin
18799 -- Create an anonymous base type
18801 Implicit_Base :=
18802 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
18804 -- Analyze and check the bounds, they can be of any integer type
18806 Lo := Low_Bound (Def);
18807 Hi := High_Bound (Def);
18809 -- Arbitrarily use Integer as the type if either bound had an error
18811 if Hi = Error or else Lo = Error then
18812 Base_Typ := Any_Integer;
18813 Set_Error_Posted (T, True);
18815 -- Here both bounds are OK expressions
18817 else
18818 Analyze_And_Resolve (Lo, Any_Integer);
18819 Analyze_And_Resolve (Hi, Any_Integer);
18821 Check_Bound (Lo);
18822 Check_Bound (Hi);
18824 if Errs then
18825 Hi := Type_High_Bound (Standard_Long_Long_Integer);
18826 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
18827 end if;
18829 -- Find type to derive from
18831 Lo_Val := Expr_Value (Lo);
18832 Hi_Val := Expr_Value (Hi);
18834 if Can_Derive_From (Standard_Short_Short_Integer) then
18835 Base_Typ := Base_Type (Standard_Short_Short_Integer);
18837 elsif Can_Derive_From (Standard_Short_Integer) then
18838 Base_Typ := Base_Type (Standard_Short_Integer);
18840 elsif Can_Derive_From (Standard_Integer) then
18841 Base_Typ := Base_Type (Standard_Integer);
18843 elsif Can_Derive_From (Standard_Long_Integer) then
18844 Base_Typ := Base_Type (Standard_Long_Integer);
18846 elsif Can_Derive_From (Standard_Long_Long_Integer) then
18847 Base_Typ := Base_Type (Standard_Long_Long_Integer);
18849 else
18850 Base_Typ := Base_Type (Standard_Long_Long_Integer);
18851 Error_Msg_N ("integer type definition bounds out of range", Def);
18852 Hi := Type_High_Bound (Standard_Long_Long_Integer);
18853 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
18854 end if;
18855 end if;
18857 -- Complete both implicit base and declared first subtype entities
18859 Set_Etype (Implicit_Base, Base_Typ);
18860 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18861 Set_Size_Info (Implicit_Base, (Base_Typ));
18862 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18863 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18865 Set_Ekind (T, E_Signed_Integer_Subtype);
18866 Set_Etype (T, Implicit_Base);
18868 Set_Size_Info (T, (Implicit_Base));
18869 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
18870 Set_Scalar_Range (T, Def);
18871 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
18872 Set_Is_Constrained (T);
18873 end Signed_Integer_Type_Declaration;
18875 end Sem_Ch3;