]> gcc.gnu.org Git - gcc.git/blame - gcc/ada/sem_ch3.adb
[multiple changes]
[gcc.git] / gcc / ada / sem_ch3.adb
CommitLineData
996ae0b0
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S E M _ C H 3 --
6-- --
7-- B o d y --
8-- --
8f983e64 9-- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
996ae0b0
RK
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- --
b5c84c3c 13-- ware Foundation; either version 3, or (at your option) any later ver- --
996ae0b0
RK
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 --
b5c84c3c
RD
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. --
996ae0b0
RK
20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
996ae0b0
RK
23-- --
24------------------------------------------------------------------------------
25
26with Atree; use Atree;
27with Checks; use Checks;
88b32fc3 28with Debug; use Debug;
996ae0b0
RK
29with Elists; use Elists;
30with Einfo; use Einfo;
31with Errout; use Errout;
32with Eval_Fat; use Eval_Fat;
33with Exp_Ch3; use Exp_Ch3;
d44202ba 34with Exp_Ch9; use Exp_Ch9;
ce2b6ba5 35with Exp_Disp; use Exp_Disp;
996ae0b0 36with Exp_Dist; use Exp_Dist;
fbf5a39b 37with Exp_Tss; use Exp_Tss;
996ae0b0 38with Exp_Util; use Exp_Util;
9c510803 39with Fname; use Fname;
996ae0b0
RK
40with Freeze; use Freeze;
41with Itypes; use Itypes;
42with Layout; use Layout;
43with Lib; use Lib;
44with Lib.Xref; use Lib.Xref;
45with Namet; use Namet;
46with Nmake; use Nmake;
47with Opt; use Opt;
48with Restrict; use Restrict;
6e937c1c 49with Rident; use Rident;
996ae0b0
RK
50with Rtsfind; use Rtsfind;
51with Sem; use Sem;
a4100e55 52with Sem_Aux; use Sem_Aux;
996ae0b0
RK
53with Sem_Case; use Sem_Case;
54with Sem_Cat; use Sem_Cat;
55with Sem_Ch6; use Sem_Ch6;
56with Sem_Ch7; use Sem_Ch7;
57with Sem_Ch8; use Sem_Ch8;
58with Sem_Ch13; use Sem_Ch13;
59with Sem_Disp; use Sem_Disp;
60with Sem_Dist; use Sem_Dist;
61with Sem_Elim; use Sem_Elim;
62with Sem_Eval; use Sem_Eval;
63with Sem_Mech; use Sem_Mech;
64with Sem_Res; use Sem_Res;
65with Sem_Smem; use Sem_Smem;
66with Sem_Type; use Sem_Type;
67with Sem_Util; use Sem_Util;
fbf5a39b 68with Sem_Warn; use Sem_Warn;
996ae0b0
RK
69with Stand; use Stand;
70with Sinfo; use Sinfo;
71with Snames; use Snames;
653da906 72with Targparm; use Targparm;
996ae0b0
RK
73with Tbuild; use Tbuild;
74with Ttypes; use Ttypes;
75with Uintp; use Uintp;
76with Urealp; use Urealp;
77
78package body Sem_Ch3 is
79
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
83
88b32fc3 84 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
758c442c
GD
85 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
86 -- abstract interface types implemented by a record type or a derived
87 -- record type.
88
996ae0b0
RK
89 procedure Build_Derived_Type
90 (N : Node_Id;
91 Parent_Type : Entity_Id;
92 Derived_Type : Entity_Id;
93 Is_Completion : Boolean;
94 Derive_Subps : Boolean := True);
9dfd2ff8
CC
95 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
96 -- the N_Full_Type_Declaration node containing the derived type definition.
97 -- Parent_Type is the entity for the parent type in the derived type
98 -- definition and Derived_Type the actual derived type. Is_Completion must
99 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
f3d57416 100 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
9dfd2ff8
CC
101 -- completion of a private type declaration. If Is_Completion is set to
102 -- True, N is the completion of a private type declaration and Derived_Type
103 -- is different from the defining identifier inside N (i.e. Derived_Type /=
104 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
105 -- subprograms should be derived. The only case where this parameter is
106 -- False is when Build_Derived_Type is recursively called to process an
107 -- implicit derived full type for a type derived from a private type (in
108 -- that case the subprograms must only be derived for the private view of
109 -- the type).
fea9e956 110 --
44d6a706 111 -- ??? These flags need a bit of re-examination and re-documentation:
996ae0b0
RK
112 -- ??? are they both necessary (both seem related to the recursion)?
113
114 procedure Build_Derived_Access_Type
115 (N : Node_Id;
116 Parent_Type : Entity_Id;
117 Derived_Type : Entity_Id);
118 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
119 -- create an implicit base if the parent type is constrained or if the
120 -- subtype indication has a constraint.
121
122 procedure Build_Derived_Array_Type
123 (N : Node_Id;
124 Parent_Type : Entity_Id;
125 Derived_Type : Entity_Id);
126 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
127 -- create an implicit base if the parent type is constrained or if the
128 -- subtype indication has a constraint.
129
130 procedure Build_Derived_Concurrent_Type
131 (N : Node_Id;
132 Parent_Type : Entity_Id;
133 Derived_Type : Entity_Id);
88b32fc3
BD
134 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
135 -- protected type, inherit entries and protected subprograms, check
136 -- legality of discriminant constraints if any.
996ae0b0
RK
137
138 procedure Build_Derived_Enumeration_Type
139 (N : Node_Id;
140 Parent_Type : Entity_Id;
141 Derived_Type : Entity_Id);
142 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
143 -- type, we must create a new list of literals. Types derived from
94fd3dc6 144 -- Character and [Wide_]Wide_Character are special-cased.
996ae0b0
RK
145
146 procedure Build_Derived_Numeric_Type
147 (N : Node_Id;
148 Parent_Type : Entity_Id;
149 Derived_Type : Entity_Id);
150 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
151 -- an anonymous base type, and propagate constraint to subtype if needed.
152
153 procedure Build_Derived_Private_Type
71d9e9f2
ES
154 (N : Node_Id;
155 Parent_Type : Entity_Id;
156 Derived_Type : Entity_Id;
996ae0b0
RK
157 Is_Completion : Boolean;
158 Derive_Subps : Boolean := True);
fbf5a39b 159 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
996ae0b0
RK
160 -- because the parent may or may not have a completion, and the derivation
161 -- may itself be a completion.
162
163 procedure Build_Derived_Record_Type
164 (N : Node_Id;
165 Parent_Type : Entity_Id;
166 Derived_Type : Entity_Id;
167 Derive_Subps : Boolean := True);
9dfd2ff8 168 -- Subsidiary procedure for Build_Derived_Type and
996ae0b0
RK
169 -- Analyze_Private_Extension_Declaration used for tagged and untagged
170 -- record types. All parameters are as in Build_Derived_Type except that
171 -- N, in addition to being an N_Full_Type_Declaration node, can also be an
172 -- N_Private_Extension_Declaration node. See the definition of this routine
173 -- for much more info. Derive_Subps indicates whether subprograms should
174 -- be derived from the parent type. The only case where Derive_Subps is
175 -- False is for an implicit derived full type for a type derived from a
176 -- private type (see Build_Derived_Type).
177
996ae0b0
RK
178 procedure Build_Discriminal (Discrim : Entity_Id);
179 -- Create the discriminal corresponding to discriminant Discrim, that is
180 -- the parameter corresponding to Discrim to be used in initialization
181 -- procedures for the type where Discrim is a discriminant. Discriminals
182 -- are not used during semantic analysis, and are not fully defined
183 -- entities until expansion. Thus they are not given a scope until
44d6a706 184 -- initialization procedures are built.
996ae0b0
RK
185
186 function Build_Discriminant_Constraints
187 (T : Entity_Id;
188 Def : Node_Id;
b0f26df5 189 Derived_Def : Boolean := False) return Elist_Id;
2b73cf68
JM
190 -- Validate discriminant constraints and return the list of the constraints
191 -- in order of discriminant declarations, where T is the discriminated
192 -- unconstrained type. Def is the N_Subtype_Indication node where the
193 -- discriminants constraints for T are specified. Derived_Def is True
194 -- when building the discriminant constraints in a derived type definition
195 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
196 -- type and Def is the constraint "(xxx)" on T and this routine sets the
197 -- Corresponding_Discriminant field of the discriminants in the derived
198 -- type D to point to the corresponding discriminants in the parent type T.
996ae0b0
RK
199
200 procedure Build_Discriminated_Subtype
201 (T : Entity_Id;
202 Def_Id : Entity_Id;
203 Elist : Elist_Id;
204 Related_Nod : Node_Id;
205 For_Access : Boolean := False);
206 -- Subsidiary procedure to Constrain_Discriminated_Type and to
207 -- Process_Incomplete_Dependents. Given
208 --
209 -- T (a possibly discriminated base type)
210 -- Def_Id (a very partially built subtype for T),
211 --
212 -- the call completes Def_Id to be the appropriate E_*_Subtype.
213 --
dc06abec
RD
214 -- The Elist is the list of discriminant constraints if any (it is set
215 -- to No_Elist if T is not a discriminated type, and to an empty list if
996ae0b0
RK
216 -- T has discriminants but there are no discriminant constraints). The
217 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
218 -- The For_Access says whether or not this subtype is really constraining
219 -- an access type. That is its sole purpose is the designated type of an
220 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
221 -- is built to avoid freezing T when the access subtype is frozen.
222
223 function Build_Scalar_Bound
224 (Bound : Node_Id;
225 Par_T : Entity_Id;
b0f26df5 226 Der_T : Entity_Id) return Node_Id;
996ae0b0
RK
227 -- The bounds of a derived scalar type are conversions of the bounds of
228 -- the parent type. Optimize the representation if the bounds are literals.
229 -- Needs a more complete spec--what are the parameters exactly, and what
230 -- exactly is the returned value, and how is Bound affected???
231
232 procedure Build_Underlying_Full_View
233 (N : Node_Id;
234 Typ : Entity_Id;
235 Par : Entity_Id);
236 -- If the completion of a private type is itself derived from a private
237 -- type, or if the full view of a private subtype is itself private, the
238 -- back-end has no way to compute the actual size of this type. We build
239 -- an internal subtype declaration of the proper parent type to convey
240 -- this information. This extra mechanism is needed because a full
241 -- view cannot itself have a full view (it would get clobbered during
242 -- view exchanges).
243
244 procedure Check_Access_Discriminant_Requires_Limited
245 (D : Node_Id;
246 Loc : Node_Id);
247 -- Check the restriction that the type to which an access discriminant
248 -- belongs must be a concurrent type or a descendant of a type with
249 -- the reserved word 'limited' in its declaration.
250
fea9e956
ES
251 procedure Check_Anonymous_Access_Components
252 (Typ_Decl : Node_Id;
253 Typ : Entity_Id;
254 Prev : Entity_Id;
255 Comp_List : Node_Id);
256 -- Ada 2005 AI-382: an access component in a record definition can refer to
257 -- the enclosing record, in which case it denotes the type itself, and not
258 -- the current instance of the type. We create an anonymous access type for
259 -- the component, and flag it as an access to a component, so accessibility
260 -- checks are properly performed on it. The declaration of the access type
261 -- is placed ahead of that of the record to prevent order-of-elaboration
262 -- circularity issues in Gigi. We create an incomplete type for the record
263 -- declaration, which is the designated type of the anonymous access.
264
996ae0b0 265 procedure Check_Delta_Expression (E : Node_Id);
fea9e956
ES
266 -- Check that the expression represented by E is suitable for use as a
267 -- delta expression, i.e. it is of real type and is static.
996ae0b0
RK
268
269 procedure Check_Digits_Expression (E : Node_Id);
fea9e956
ES
270 -- Check that the expression represented by E is suitable for use as a
271 -- digits expression, i.e. it is of integer type, positive and static.
996ae0b0 272
996ae0b0 273 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
fea9e956
ES
274 -- Validate the initialization of an object declaration. T is the required
275 -- type, and Exp is the initialization expression.
996ae0b0 276
ce2b6ba5
JM
277 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
278 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
279
fbf5a39b
AC
280 procedure Check_Or_Process_Discriminants
281 (N : Node_Id;
282 T : Entity_Id;
283 Prev : Entity_Id := Empty);
fea9e956
ES
284 -- If T is the full declaration of an incomplete or private type, check the
285 -- conformance of the discriminants, otherwise process them. Prev is the
286 -- entity of the partial declaration, if any.
996ae0b0
RK
287
288 procedure Check_Real_Bound (Bound : Node_Id);
289 -- Check given bound for being of real type and static. If not, post an
290 -- appropriate message, and rewrite the bound with the real literal zero.
291
292 procedure Constant_Redeclaration
293 (Id : Entity_Id;
294 N : Node_Id;
295 T : out Entity_Id);
296 -- Various checks on legality of full declaration of deferred constant.
297 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
298 -- node. The caller has not yet set any attributes of this entity.
299
dc06abec
RD
300 function Contain_Interface
301 (Iface : Entity_Id;
302 Ifaces : Elist_Id) return Boolean;
303 -- Ada 2005: Determine whether Iface is present in the list Ifaces
304
996ae0b0
RK
305 procedure Convert_Scalar_Bounds
306 (N : Node_Id;
307 Parent_Type : Entity_Id;
308 Derived_Type : Entity_Id;
309 Loc : Source_Ptr);
fea9e956
ES
310 -- For derived scalar types, convert the bounds in the type definition to
311 -- the derived type, and complete their analysis. Given a constraint of the
312 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
313 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
314 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
315 -- subtype are conversions of those bounds to the derived_type, so that
316 -- their typing is consistent.
996ae0b0
RK
317
318 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
fea9e956
ES
319 -- Copies attributes from array base type T2 to array base type T1. Copies
320 -- only attributes that apply to base types, but not subtypes.
996ae0b0
RK
321
322 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
323 -- Copies attributes from array subtype T2 to array subtype T1. Copies
324 -- attributes that apply to both subtypes and base types.
325
326 procedure Create_Constrained_Components
327 (Subt : Entity_Id;
328 Decl_Node : Node_Id;
329 Typ : Entity_Id;
330 Constraints : Elist_Id);
331 -- Build the list of entities for a constrained discriminated record
332 -- subtype. If a component depends on a discriminant, replace its subtype
ce4a6e84
RD
333 -- using the discriminant values in the discriminant constraint. Subt
334 -- is the defining identifier for the subtype whose list of constrained
335 -- entities we will create. Decl_Node is the type declaration node where
336 -- we will attach all the itypes created. Typ is the base discriminated
337 -- type for the subtype Subt. Constraints is the list of discriminant
fea9e956 338 -- constraints for Typ.
996ae0b0
RK
339
340 function Constrain_Component_Type
c6823a20 341 (Comp : Entity_Id;
996ae0b0
RK
342 Constrained_Typ : Entity_Id;
343 Related_Node : Node_Id;
344 Typ : Entity_Id;
b0f26df5 345 Constraints : Elist_Id) return Entity_Id;
996ae0b0 346 -- Given a discriminated base type Typ, a list of discriminant constraint
c6823a20 347 -- Constraints for Typ and a component of Typ, with type Compon_Type,
996ae0b0 348 -- create and return the type corresponding to Compon_type where all
fea9e956
ES
349 -- discriminant references are replaced with the corresponding constraint.
350 -- If no discriminant references occur in Compon_Typ then return it as is.
351 -- Constrained_Typ is the final constrained subtype to which the
352 -- constrained Compon_Type belongs. Related_Node is the node where we will
353 -- attach all the itypes created.
ce4a6e84 354 --
fea9e956 355 -- Above description is confused, what is Compon_Type???
996ae0b0
RK
356
357 procedure Constrain_Access
358 (Def_Id : in out Entity_Id;
359 S : Node_Id;
360 Related_Nod : Node_Id);
9dfd2ff8
CC
361 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
362 -- an anonymous type created for a subtype indication. In that case it is
363 -- created in the procedure and attached to Related_Nod.
996ae0b0
RK
364
365 procedure Constrain_Array
366 (Def_Id : in out Entity_Id;
367 SI : Node_Id;
368 Related_Nod : Node_Id;
369 Related_Id : Entity_Id;
370 Suffix : Character);
371 -- Apply a list of index constraints to an unconstrained array type. The
372 -- first parameter is the entity for the resulting subtype. A value of
373 -- Empty for Def_Id indicates that an implicit type must be created, but
374 -- creation is delayed (and must be done by this procedure) because other
375 -- subsidiary implicit types must be created first (which is why Def_Id
07fc65c4
GB
376 -- is an in/out parameter). The second parameter is a subtype indication
377 -- node for the constrained array to be created (e.g. something of the
378 -- form string (1 .. 10)). Related_Nod gives the place where this type
379 -- has to be inserted in the tree. The Related_Id and Suffix parameters
380 -- are used to build the associated Implicit type name.
996ae0b0
RK
381
382 procedure Constrain_Concurrent
383 (Def_Id : in out Entity_Id;
384 SI : Node_Id;
385 Related_Nod : Node_Id;
386 Related_Id : Entity_Id;
387 Suffix : Character);
388 -- Apply list of discriminant constraints to an unconstrained concurrent
389 -- type.
390 --
391 -- SI is the N_Subtype_Indication node containing the constraint and
392 -- the unconstrained type to constrain.
393 --
a5b62485
AC
394 -- Def_Id is the entity for the resulting constrained subtype. A value
395 -- of Empty for Def_Id indicates that an implicit type must be created,
396 -- but creation is delayed (and must be done by this procedure) because
397 -- other subsidiary implicit types must be created first (which is why
398 -- Def_Id is an in/out parameter).
996ae0b0
RK
399 --
400 -- Related_Nod gives the place where this type has to be inserted
401 -- in the tree
402 --
403 -- The last two arguments are used to create its external name if needed.
404
405 function Constrain_Corresponding_Record
406 (Prot_Subt : Entity_Id;
407 Corr_Rec : Entity_Id;
408 Related_Nod : Node_Id;
b0f26df5 409 Related_Id : Entity_Id) return Entity_Id;
996ae0b0
RK
410 -- When constraining a protected type or task type with discriminants,
411 -- constrain the corresponding record with the same discriminant values.
412
07fc65c4 413 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
996ae0b0
RK
414 -- Constrain a decimal fixed point type with a digits constraint and/or a
415 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
416
417 procedure Constrain_Discriminated_Type
418 (Def_Id : Entity_Id;
419 S : Node_Id;
420 Related_Nod : Node_Id;
421 For_Access : Boolean := False);
422 -- Process discriminant constraints of composite type. Verify that values
423 -- have been provided for all discriminants, that the original type is
424 -- unconstrained, and that the types of the supplied expressions match
425 -- the discriminant types. The first three parameters are like in routine
fbf5a39b 426 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
996ae0b0
RK
427 -- of For_Access.
428
07fc65c4 429 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
9dfd2ff8
CC
430 -- Constrain an enumeration type with a range constraint. This is identical
431 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
996ae0b0 432
07fc65c4 433 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
996ae0b0
RK
434 -- Constrain a floating point type with either a digits constraint
435 -- and/or a range constraint, building a E_Floating_Point_Subtype.
436
437 procedure Constrain_Index
438 (Index : Node_Id;
439 S : Node_Id;
440 Related_Nod : Node_Id;
441 Related_Id : Entity_Id;
442 Suffix : Character;
443 Suffix_Index : Nat);
a5b62485 444 -- Process an index constraint in a constrained array declaration. The
fea9e956
ES
445 -- constraint can be a subtype name, or a range with or without an explicit
446 -- subtype mark. The index is the corresponding index of the unconstrained
447 -- array. The Related_Id and Suffix parameters are used to build the
448 -- associated Implicit type name.
996ae0b0 449
07fc65c4 450 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
ffe9aba8 451 -- Build subtype of a signed or modular integer type
996ae0b0 452
07fc65c4 453 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
996ae0b0
RK
454 -- Constrain an ordinary fixed point type with a range constraint, and
455 -- build an E_Ordinary_Fixed_Point_Subtype entity.
456
fbf5a39b 457 procedure Copy_And_Swap (Priv, Full : Entity_Id);
fea9e956
ES
458 -- Copy the Priv entity into the entity of its full declaration then swap
459 -- the two entities in such a manner that the former private type is now
460 -- seen as a full type.
996ae0b0 461
996ae0b0
RK
462 procedure Decimal_Fixed_Point_Type_Declaration
463 (T : Entity_Id;
464 Def : Node_Id);
465 -- Create a new decimal fixed point type, and apply the constraint to
466 -- obtain a subtype of this new type.
467
468 procedure Complete_Private_Subtype
469 (Priv : Entity_Id;
470 Full : Entity_Id;
471 Full_Base : Entity_Id;
472 Related_Nod : Node_Id);
9dfd2ff8
CC
473 -- Complete the implicit full view of a private subtype by setting the
474 -- appropriate semantic fields. If the full view of the parent is a record
475 -- type, build constrained components of subtype.
996ae0b0 476
ce2b6ba5 477 procedure Derive_Progenitor_Subprograms
88b32fc3 478 (Parent_Type : Entity_Id;
ce2b6ba5
JM
479 Tagged_Type : Entity_Id);
480 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
481 -- operations of progenitors of Tagged_Type, and replace the subsidiary
482 -- subtypes with Tagged_Type, to build the specs of the inherited interface
483 -- primitives. The derived primitives are aliased to those of the
484 -- interface. This routine takes care also of transferring to the full-view
485 -- subprograms associated with the partial-view of Tagged_Type that cover
486 -- interface primitives.
758c442c 487
996ae0b0
RK
488 procedure Derived_Standard_Character
489 (N : Node_Id;
490 Parent_Type : Entity_Id;
491 Derived_Type : Entity_Id);
492 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
493 -- derivations from types Standard.Character and Standard.Wide_Character.
494
495 procedure Derived_Type_Declaration
496 (T : Entity_Id;
497 N : Node_Id;
498 Is_Completion : Boolean);
ce4a6e84
RD
499 -- Process a derived type declaration. Build_Derived_Type is invoked
500 -- to process the actual derived type definition. Parameters N and
501 -- Is_Completion have the same meaning as in Build_Derived_Type.
502 -- T is the N_Defining_Identifier for the entity defined in the
503 -- N_Full_Type_Declaration node N, that is T is the derived type.
996ae0b0 504
996ae0b0 505 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
9dfd2ff8
CC
506 -- Insert each literal in symbol table, as an overloadable identifier. Each
507 -- enumeration type is mapped into a sequence of integers, and each literal
508 -- is defined as a constant with integer value. If any of the literals are
509 -- character literals, the type is a character type, which means that
510 -- strings are legal aggregates for arrays of components of the type.
996ae0b0 511
fbf5a39b
AC
512 function Expand_To_Stored_Constraint
513 (Typ : Entity_Id;
b0f26df5 514 Constraint : Elist_Id) return Elist_Id;
ce4a6e84 515 -- Given a constraint (i.e. a list of expressions) on the discriminants of
9dfd2ff8
CC
516 -- Typ, expand it into a constraint on the stored discriminants and return
517 -- the new list of expressions constraining the stored discriminants.
996ae0b0
RK
518
519 function Find_Type_Of_Object
520 (Obj_Def : Node_Id;
b0f26df5 521 Related_Nod : Node_Id) return Entity_Id;
996ae0b0
RK
522 -- Get type entity for object referenced by Obj_Def, attaching the
523 -- implicit types generated to Related_Nod
524
525 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
ce4a6e84 526 -- Create a new float and apply the constraint to obtain subtype of it
996ae0b0
RK
527
528 function Has_Range_Constraint (N : Node_Id) return Boolean;
529 -- Given an N_Subtype_Indication node N, return True if a range constraint
530 -- is present, either directly, or as part of a digits or delta constraint.
531 -- In addition, a digits constraint in the decimal case returns True, since
532 -- it establishes a default range if no explicit range is present.
533
88b32fc3
BD
534 function Inherit_Components
535 (N : Node_Id;
536 Parent_Base : Entity_Id;
537 Derived_Base : Entity_Id;
538 Is_Tagged : Boolean;
539 Inherit_Discr : Boolean;
540 Discs : Elist_Id) return Elist_Id;
541 -- Called from Build_Derived_Record_Type to inherit the components of
542 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
543 -- For more information on derived types and component inheritance please
544 -- consult the comment above the body of Build_Derived_Record_Type.
545 --
546 -- N is the original derived type declaration
547 --
548 -- Is_Tagged is set if we are dealing with tagged types
549 --
fea9e956
ES
550 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
551 -- Parent_Base, otherwise no discriminants are inherited.
88b32fc3
BD
552 --
553 -- Discs gives the list of constraints that apply to Parent_Base in the
554 -- derived type declaration. If Discs is set to No_Elist, then we have
555 -- the following situation:
556 --
557 -- type Parent (D1..Dn : ..) is [tagged] record ...;
558 -- type Derived is new Parent [with ...];
559 --
560 -- which gets treated as
561 --
562 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
563 --
564 -- For untagged types the returned value is an association list. The list
565 -- starts from the association (Parent_Base => Derived_Base), and then it
566 -- contains a sequence of the associations of the form
567 --
568 -- (Old_Component => New_Component),
569 --
fea9e956
ES
570 -- where Old_Component is the Entity_Id of a component in Parent_Base and
571 -- New_Component is the Entity_Id of the corresponding component in
88b32fc3
BD
572 -- Derived_Base. For untagged records, this association list is needed when
573 -- copying the record declaration for the derived base. In the tagged case
574 -- the value returned is irrelevant.
575
ce4a6e84
RD
576 function Is_Progenitor
577 (Iface : Entity_Id;
0e41a941
AC
578 Typ : Entity_Id) return Boolean;
579 -- Determine whether the interface Iface is implemented by Typ. It requires
ce4a6e84
RD
580 -- traversing the list of abstract interfaces of the type, as well as that
581 -- of the ancestor types. The predicate is used to determine when a formal
582 -- in the signature of an inherited operation must carry the derived type.
583
996ae0b0
RK
584 function Is_Valid_Constraint_Kind
585 (T_Kind : Type_Kind;
b0f26df5 586 Constraint_Kind : Node_Kind) return Boolean;
9dfd2ff8
CC
587 -- Returns True if it is legal to apply the given kind of constraint to the
588 -- given kind of type (index constraint to an array type, for example).
996ae0b0
RK
589
590 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
8dc2ddaf 591 -- Create new modular type. Verify that modulus is in bounds and is
996ae0b0
RK
592 -- a power of two (implementation restriction).
593
6c1e24d3 594 procedure New_Concatenation_Op (Typ : Entity_Id);
996ae0b0 595 -- Create an abbreviated declaration for an operator in order to
6c1e24d3 596 -- materialize concatenation on array types.
996ae0b0
RK
597
598 procedure Ordinary_Fixed_Point_Type_Declaration
599 (T : Entity_Id;
600 Def : Node_Id);
9dfd2ff8
CC
601 -- Create a new ordinary fixed point type, and apply the constraint to
602 -- obtain subtype of it.
996ae0b0
RK
603
604 procedure Prepare_Private_Subtype_Completion
605 (Id : Entity_Id;
606 Related_Nod : Node_Id);
607 -- Id is a subtype of some private type. Creates the full declaration
608 -- associated with Id whenever possible, i.e. when the full declaration
609 -- of the base type is already known. Records each subtype into
610 -- Private_Dependents of the base type.
611
612 procedure Process_Incomplete_Dependents
613 (N : Node_Id;
614 Full_T : Entity_Id;
615 Inc_T : Entity_Id);
616 -- Process all entities that depend on an incomplete type. There include
617 -- subtypes, subprogram types that mention the incomplete type in their
618 -- profiles, and subprogram with access parameters that designate the
619 -- incomplete type.
620
621 -- Inc_T is the defining identifier of an incomplete type declaration, its
622 -- Ekind is E_Incomplete_Type.
623 --
624 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
625 --
626 -- Full_T is N's defining identifier.
627 --
628 -- Subtypes of incomplete types with discriminants are completed when the
629 -- parent type is. This is simpler than private subtypes, because they can
630 -- only appear in the same scope, and there is no need to exchange views.
631 -- Similarly, access_to_subprogram types may have a parameter or a return
632 -- type that is an incomplete type, and that must be replaced with the
633 -- full type.
ce4a6e84 634 --
996ae0b0
RK
635 -- If the full type is tagged, subprogram with access parameters that
636 -- designated the incomplete may be primitive operations of the full type,
637 -- and have to be processed accordingly.
638
639 procedure Process_Real_Range_Specification (Def : Node_Id);
ce4a6e84
RD
640 -- Given the type definition for a real type, this procedure processes and
641 -- checks the real range specification of this type definition if one is
642 -- present. If errors are found, error messages are posted, and the
643 -- Real_Range_Specification of Def is reset to Empty.
996ae0b0 644
fbf5a39b
AC
645 procedure Record_Type_Declaration
646 (T : Entity_Id;
647 N : Node_Id;
648 Prev : Entity_Id);
996ae0b0
RK
649 -- Process a record type declaration (for both untagged and tagged
650 -- records). Parameters T and N are exactly like in procedure
9dfd2ff8
CC
651 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
652 -- for this routine. If this is the completion of an incomplete type
653 -- declaration, Prev is the entity of the incomplete declaration, used for
654 -- cross-referencing. Otherwise Prev = T.
996ae0b0 655
fbf5a39b 656 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
ce4a6e84
RD
657 -- This routine is used to process the actual record type definition (both
658 -- for untagged and tagged records). Def is a record type definition node.
659 -- This procedure analyzes the components in this record type definition.
660 -- Prev_T is the entity for the enclosing record type. It is provided so
661 -- that its Has_Task flag can be set if any of the component have Has_Task
662 -- set. If the declaration is the completion of an incomplete type
663 -- declaration, Prev_T is the original incomplete type, whose full view is
664 -- the record type.
996ae0b0 665
07fc65c4
GB
666 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
667 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
668 -- build a copy of the declaration tree of the parent, and we create
669 -- independently the list of components for the derived type. Semantic
670 -- information uses the component entities, but record representation
671 -- clauses are validated on the declaration tree. This procedure replaces
672 -- discriminants and components in the declaration with those that have
673 -- been created by Inherit_Components.
674
996ae0b0
RK
675 procedure Set_Fixed_Range
676 (E : Entity_Id;
677 Loc : Source_Ptr;
678 Lo : Ureal;
679 Hi : Ureal);
680 -- Build a range node with the given bounds and set it as the Scalar_Range
681 -- of the given fixed-point type entity. Loc is the source location used
682 -- for the constructed range. See body for further details.
683
684 procedure Set_Scalar_Range_For_Subtype
07fc65c4
GB
685 (Def_Id : Entity_Id;
686 R : Node_Id;
687 Subt : Entity_Id);
57193e09
TQ
688 -- This routine is used to set the scalar range field for a subtype given
689 -- Def_Id, the entity for the subtype, and R, the range expression for the
690 -- scalar range. Subt provides the parent subtype to be used to analyze,
691 -- resolve, and check the given range.
996ae0b0
RK
692
693 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
694 -- Create a new signed integer entity, and apply the constraint to obtain
695 -- the required first named subtype of this type.
696
fbf5a39b
AC
697 procedure Set_Stored_Constraint_From_Discriminant_Constraint
698 (E : Entity_Id);
699 -- E is some record type. This routine computes E's Stored_Constraint
700 -- from its Discriminant_Constraint.
701
6765b310
ES
702 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
703 -- Check that an entity in a list of progenitors is an interface,
704 -- emit error otherwise.
705
996ae0b0
RK
706 -----------------------
707 -- Access_Definition --
708 -----------------------
709
710 function Access_Definition
711 (Related_Nod : Node_Id;
b0f26df5 712 N : Node_Id) return Entity_Id
996ae0b0 713 is
550f4135
AC
714 Loc : constant Source_Ptr := Sloc (Related_Nod);
715 Anon_Type : Entity_Id;
716 Anon_Scope : Entity_Id;
717 Desig_Type : Entity_Id;
718 Decl : Entity_Id;
719 Enclosing_Prot_Type : Entity_Id := Empty;
996ae0b0
RK
720
721 begin
722 if Is_Entry (Current_Scope)
723 and then Is_Task_Type (Etype (Scope (Current_Scope)))
724 then
725 Error_Msg_N ("task entries cannot have access parameters", N);
fea9e956 726 return Empty;
996ae0b0
RK
727 end if;
728
57193e09
TQ
729 -- Ada 2005: for an object declaration the corresponding anonymous
730 -- type is declared in the current scope.
758c442c 731
88b32fc3
BD
732 -- If the access definition is the return type of another access to
733 -- function, scope is the current one, because it is the one of the
734 -- current type declaration.
735
7d7af38a
JM
736 if Nkind_In (Related_Nod, N_Object_Declaration,
737 N_Access_Function_Definition)
88b32fc3 738 then
2b73cf68 739 Anon_Scope := Current_Scope;
9dfd2ff8 740
fea9e956
ES
741 -- For the anonymous function result case, retrieve the scope of the
742 -- function specification's associated entity rather than using the
743 -- current scope. The current scope will be the function itself if the
744 -- formal part is currently being analyzed, but will be the parent scope
745 -- in the case of a parameterless function, and we always want to use
746 -- the function's parent scope. Finally, if the function is a child
f3d57416 747 -- unit, we must traverse the tree to retrieve the proper entity.
9dfd2ff8
CC
748
749 elsif Nkind (Related_Nod) = N_Function_Specification
7d7af38a 750 and then Nkind (Parent (N)) /= N_Parameter_Specification
9dfd2ff8 751 then
2b73cf68
JM
752 -- If the current scope is a protected type, the anonymous access
753 -- is associated with one of the protected operations, and must
754 -- be available in the scope that encloses the protected declaration.
16b05213 755 -- Otherwise the type is in the scope enclosing the subprogram.
550f4135
AC
756 -- If the function has formals, The return type of a subprogram
757 -- declaration is analyzed in the scope of the subprogram (see
758 -- Process_Formals) and thus the protected type, if present, is
759 -- the scope of the current function scope.
2b73cf68
JM
760
761 if Ekind (Current_Scope) = E_Protected_Type then
550f4135
AC
762 Enclosing_Prot_Type := Current_Scope;
763
764 elsif Ekind (Current_Scope) = E_Function
765 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
766 then
767 Enclosing_Prot_Type := Scope (Current_Scope);
768 end if;
769
770 if Present (Enclosing_Prot_Type) then
771 Anon_Scope := Scope (Enclosing_Prot_Type);
772
2b73cf68
JM
773 else
774 Anon_Scope := Scope (Defining_Entity (Related_Nod));
775 end if;
57193e09
TQ
776
777 else
fea9e956
ES
778 -- For access formals, access components, and access discriminants,
779 -- the scope is that of the enclosing declaration,
57193e09 780
2b73cf68 781 Anon_Scope := Scope (Current_Scope);
758c442c
GD
782 end if;
783
2b73cf68
JM
784 Anon_Type :=
785 Create_Itype
786 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
787
758c442c
GD
788 if All_Present (N)
789 and then Ada_Version >= Ada_05
790 then
791 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
792 end if;
793
fea9e956
ES
794 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
795 -- the corresponding semantic routine
7324bf49
AC
796
797 if Present (Access_To_Subprogram_Definition (N)) then
798 Access_Subprogram_Declaration
799 (T_Name => Anon_Type,
800 T_Def => Access_To_Subprogram_Definition (N));
af4b9434
AC
801
802 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
803 Set_Ekind
804 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
805 else
806 Set_Ekind
807 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
808 end if;
809
7d7af38a
JM
810 Set_Can_Use_Internal_Rep
811 (Anon_Type, not Always_Compatible_Rep_On_Target);
812
2b73cf68
JM
813 -- If the anonymous access is associated with a protected operation
814 -- create a reference to it after the enclosing protected definition
815 -- because the itype will be used in the subsequent bodies.
816
817 if Ekind (Current_Scope) = E_Protected_Type then
818 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
819 end if;
820
7324bf49
AC
821 return Anon_Type;
822 end if;
823
996ae0b0
RK
824 Find_Type (Subtype_Mark (N));
825 Desig_Type := Entity (Subtype_Mark (N));
826
827 Set_Directly_Designated_Type
c0985d4e
HK
828 (Anon_Type, Desig_Type);
829 Set_Etype (Anon_Type, Anon_Type);
ce4a6e84
RD
830
831 -- Make sure the anonymous access type has size and alignment fields
832 -- set, as required by gigi. This is necessary in the case of the
833 -- Task_Body_Procedure.
834
835 if not Has_Private_Component (Desig_Type) then
836 Layout_Type (Anon_Type);
837 end if;
838
839 -- ???The following makes no sense, because Anon_Type is an access type
840 -- and therefore cannot have components, private or otherwise. Hence
841 -- the assertion. Not sure what was meant, here.
996ae0b0 842 Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
ce4a6e84 843 pragma Assert (not Depends_On_Private (Anon_Type));
996ae0b0 844
0ab80019 845 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
fea9e956
ES
846 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
847 -- the null value is allowed. In Ada 95 the null value is never allowed.
2820d220 848
0ab80019 849 if Ada_Version >= Ada_05 then
6b6fcd3e 850 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
2820d220 851 else
6b6fcd3e 852 Set_Can_Never_Be_Null (Anon_Type, True);
2820d220
AC
853 end if;
854
996ae0b0
RK
855 -- The anonymous access type is as public as the discriminated type or
856 -- subprogram that defines it. It is imported (for back-end purposes)
857 -- if the designated type is.
858
6b6fcd3e 859 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
19f0526a 860
0ab80019 861 -- Ada 2005 (AI-231): Propagate the access-constant attribute
2820d220
AC
862
863 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
864
758c442c
GD
865 -- The context is either a subprogram declaration, object declaration,
866 -- or an access discriminant, in a private or a full type declaration.
867 -- In the case of a subprogram, if the designated type is incomplete,
868 -- the operation will be a primitive operation of the full type, to be
869 -- updated subsequently. If the type is imported through a limited_with
870 -- clause, the subprogram is not a primitive operation of the type
871 -- (which is declared elsewhere in some other scope).
996ae0b0
RK
872
873 if Ekind (Desig_Type) = E_Incomplete_Type
aa720a54 874 and then not From_With_Type (Desig_Type)
996ae0b0
RK
875 and then Is_Overloadable (Current_Scope)
876 then
877 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
878 Set_Has_Delayed_Freeze (Current_Scope);
879 end if;
880
950d3e7d
ES
881 -- Ada 2005: if the designated type is an interface that may contain
882 -- tasks, create a Master entity for the declaration. This must be done
fea9e956
ES
883 -- before expansion of the full declaration, because the declaration may
884 -- include an expression that is an allocator, whose expansion needs the
885 -- proper Master for the created tasks.
950d3e7d
ES
886
887 if Nkind (Related_Nod) = N_Object_Declaration
888 and then Expander_Active
950d3e7d 889 then
88b32fc3
BD
890 if Is_Interface (Desig_Type)
891 and then Is_Limited_Record (Desig_Type)
892 then
893 Build_Class_Wide_Master (Anon_Type);
894
895 -- Similarly, if the type is an anonymous access that designates
896 -- tasks, create a master entity for it in the current context.
897
898 elsif Has_Task (Desig_Type)
899 and then Comes_From_Source (Related_Nod)
900 then
901 if not Has_Master_Entity (Current_Scope) then
902 Decl :=
903 Make_Object_Declaration (Loc,
904 Defining_Identifier =>
905 Make_Defining_Identifier (Loc, Name_uMaster),
906 Constant_Present => True,
907 Object_Definition =>
908 New_Reference_To (RTE (RE_Master_Id), Loc),
909 Expression =>
910 Make_Explicit_Dereference (Loc,
911 New_Reference_To (RTE (RE_Current_Master), Loc)));
912
913 Insert_Before (Related_Nod, Decl);
914 Analyze (Decl);
915
916 Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
917 Set_Has_Master_Entity (Current_Scope);
918 else
919 Build_Master_Renaming (Related_Nod, Anon_Type);
920 end if;
921 end if;
950d3e7d
ES
922 end if;
923
fea9e956
ES
924 -- For a private component of a protected type, it is imperative that
925 -- the back-end elaborate the type immediately after the protected
926 -- declaration, because this type will be used in the declarations
927 -- created for the component within each protected body, so we must
928 -- create an itype reference for it now.
929
930 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
931 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
df89ab66
ES
932
933 -- Similarly, if the access definition is the return result of a
0f5177ad
ES
934 -- function, create an itype reference for it because it
935 -- will be used within the function body. For a regular function that
936 -- is not a compilation unit, insert reference after the declaration.
937 -- For a protected operation, insert it after the enclosing protected
938 -- type declaration. In either case, do not create a reference for a
939 -- type obtained through a limited_with clause, because this would
940 -- introduce semantic dependencies.
df89ab66
ES
941
942 elsif Nkind (Related_Nod) = N_Function_Specification
c0985d4e 943 and then not From_With_Type (Desig_Type)
df89ab66 944 then
550f4135
AC
945 if Present (Enclosing_Prot_Type) then
946 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
0f5177ad
ES
947
948 elsif Is_List_Member (Parent (Related_Nod))
949 and then Nkind (Parent (N)) /= N_Parameter_Specification
950 then
951 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
952 end if;
df89ab66
ES
953
954 -- Finally, create an itype reference for an object declaration of
955 -- an anonymous access type. This is strictly necessary only for
956 -- deferred constants, but in any case will avoid out-of-scope
957 -- problems in the back-end.
958
959 elsif Nkind (Related_Nod) = N_Object_Declaration then
960 Build_Itype_Reference (Anon_Type, Related_Nod);
fea9e956
ES
961 end if;
962
996ae0b0
RK
963 return Anon_Type;
964 end Access_Definition;
965
966 -----------------------------------
967 -- Access_Subprogram_Declaration --
968 -----------------------------------
969
970 procedure Access_Subprogram_Declaration
971 (T_Name : Entity_Id;
972 T_Def : Node_Id)
973 is
b0f26df5 974
f29b857f 975 procedure Check_For_Premature_Usage (Def : Node_Id);
8dbf3473
AC
976 -- Check that type T_Name is not used, directly or recursively, as a
977 -- parameter or a return type in Def. Def is either a subtype, an
978 -- access_definition, or an access_to_subprogram_definition.
f29b857f
ES
979
980 -------------------------------
981 -- Check_For_Premature_Usage --
982 -------------------------------
983
984 procedure Check_For_Premature_Usage (Def : Node_Id) is
985 Param : Node_Id;
986
987 begin
988 -- Check for a subtype mark
989
990 if Nkind (Def) in N_Has_Etype then
991 if Etype (Def) = T_Name then
992 Error_Msg_N
993 ("type& cannot be used before end of its declaration", Def);
994 end if;
995
996 -- If this is not a subtype, then this is an access_definition
997
998 elsif Nkind (Def) = N_Access_Definition then
999 if Present (Access_To_Subprogram_Definition (Def)) then
1000 Check_For_Premature_Usage
1001 (Access_To_Subprogram_Definition (Def));
1002 else
1003 Check_For_Premature_Usage (Subtype_Mark (Def));
1004 end if;
1005
1006 -- The only cases left are N_Access_Function_Definition and
1007 -- N_Access_Procedure_Definition.
1008
1009 else
1010 if Present (Parameter_Specifications (Def)) then
1011 Param := First (Parameter_Specifications (Def));
1012 while Present (Param) loop
1013 Check_For_Premature_Usage (Parameter_Type (Param));
1014 Param := Next (Param);
1015 end loop;
1016 end if;
1017
1018 if Nkind (Def) = N_Access_Function_Definition then
1019 Check_For_Premature_Usage (Result_Definition (Def));
1020 end if;
1021 end if;
1022 end Check_For_Premature_Usage;
1023
1024 -- Local variables
1025
1026 Formals : constant List_Id := Parameter_Specifications (T_Def);
1027 Formal : Entity_Id;
1028 D_Ityp : Node_Id;
996ae0b0 1029 Desig_Type : constant Entity_Id :=
0da2c8ac 1030 Create_Itype (E_Subprogram_Type, Parent (T_Def));
996ae0b0 1031
f29b857f
ES
1032 -- Start of processing for Access_Subprogram_Declaration
1033
996ae0b0 1034 begin
fea9e956
ES
1035 -- Associate the Itype node with the inner full-type declaration or
1036 -- subprogram spec. This is required to handle nested anonymous
758c442c
GD
1037 -- declarations. For example:
1038
1039 -- procedure P
1040 -- (X : access procedure
1041 -- (Y : access procedure
1042 -- (Z : access T)))
1043
9dfd2ff8 1044 D_Ityp := Associated_Node_For_Itype (Desig_Type);
7d7af38a
JM
1045 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1046 N_Private_Type_Declaration,
1047 N_Private_Extension_Declaration,
1048 N_Procedure_Specification,
1049 N_Function_Specification)
1050 or else
1051 Nkind_In (D_Ityp, N_Object_Declaration,
1052 N_Object_Renaming_Declaration,
53cf4600 1053 N_Formal_Object_Declaration,
7d7af38a
JM
1054 N_Formal_Type_Declaration,
1055 N_Task_Type_Declaration,
1056 N_Protected_Type_Declaration))
758c442c
GD
1057 loop
1058 D_Ityp := Parent (D_Ityp);
1059 pragma Assert (D_Ityp /= Empty);
1060 end loop;
1061
1062 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1063
7d7af38a
JM
1064 if Nkind_In (D_Ityp, N_Procedure_Specification,
1065 N_Function_Specification)
758c442c 1066 then
88b32fc3 1067 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
758c442c 1068
7d7af38a
JM
1069 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1070 N_Object_Declaration,
1071 N_Object_Renaming_Declaration,
1072 N_Formal_Type_Declaration)
758c442c
GD
1073 then
1074 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1075 end if;
1076
996ae0b0 1077 if Nkind (T_Def) = N_Access_Function_Definition then
9dfd2ff8 1078 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
2b73cf68
JM
1079 declare
1080 Acc : constant Node_Id := Result_Definition (T_Def);
1081
1082 begin
1083 if Present (Access_To_Subprogram_Definition (Acc))
1084 and then
1085 Protected_Present (Access_To_Subprogram_Definition (Acc))
1086 then
1087 Set_Etype
1088 (Desig_Type,
1089 Replace_Anonymous_Access_To_Protected_Subprogram
1090 (T_Def));
1091
1092 else
1093 Set_Etype
1094 (Desig_Type,
1095 Access_Definition (T_Def, Result_Definition (T_Def)));
1096 end if;
1097 end;
1098
9dfd2ff8
CC
1099 else
1100 Analyze (Result_Definition (T_Def));
b66c3ff4
AC
1101
1102 declare
1103 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1104
1105 begin
1106 -- If a null exclusion is imposed on the result type, then
1107 -- create a null-excluding itype (an access subtype) and use
1108 -- it as the function's Etype.
1109
1110 if Is_Access_Type (Typ)
1111 and then Null_Exclusion_In_Return_Present (T_Def)
1112 then
1113 Set_Etype (Desig_Type,
1114 Create_Null_Excluding_Itype
1115 (T => Typ,
1116 Related_Nod => T_Def,
1117 Scope_Id => Current_Scope));
cec29135 1118
b66c3ff4 1119 else
cec29135
ES
1120 if From_With_Type (Typ) then
1121 Error_Msg_NE
1122 ("illegal use of incomplete type&",
1123 Result_Definition (T_Def), Typ);
1124
1125 elsif Ekind (Current_Scope) = E_Package
1126 and then In_Private_Part (Current_Scope)
1127 then
1128 if Ekind (Typ) = E_Incomplete_Type then
1129 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1130
1131 elsif Is_Class_Wide_Type (Typ)
1132 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1133 then
1134 Append_Elmt
1135 (Desig_Type, Private_Dependents (Etype (Typ)));
1136 end if;
1137 end if;
1138
b66c3ff4
AC
1139 Set_Etype (Desig_Type, Typ);
1140 end if;
1141 end;
9dfd2ff8 1142 end if;
0c644933
AC
1143
1144 if not (Is_Type (Etype (Desig_Type))) then
1145 Error_Msg_N
9dfd2ff8
CC
1146 ("expect type in function specification",
1147 Result_Definition (T_Def));
0c644933 1148 end if;
b0f26df5 1149
996ae0b0
RK
1150 else
1151 Set_Etype (Desig_Type, Standard_Void_Type);
1152 end if;
1153
1154 if Present (Formals) then
2b73cf68 1155 Push_Scope (Desig_Type);
b1c11e0e
JM
1156
1157 -- A bit of a kludge here. These kludges will be removed when Itypes
1158 -- have proper parent pointers to their declarations???
1159
16b05213 1160 -- Kludge 1) Link defining_identifier of formals. Required by
b1c11e0e
JM
1161 -- First_Formal to provide its functionality.
1162
1163 declare
1164 F : Node_Id;
1165
1166 begin
1167 F := First (Formals);
1168 while Present (F) loop
1169 if No (Parent (Defining_Identifier (F))) then
1170 Set_Parent (Defining_Identifier (F), F);
1171 end if;
1172
1173 Next (F);
1174 end loop;
1175 end;
1176
07fc65c4 1177 Process_Formals (Formals, Parent (T_Def));
996ae0b0 1178
b1c11e0e
JM
1179 -- Kludge 2) End_Scope requires that the parent pointer be set to
1180 -- something reasonable, but Itypes don't have parent pointers. So
1181 -- we set it and then unset it ???
996ae0b0
RK
1182
1183 Set_Parent (Desig_Type, T_Name);
1184 End_Scope;
1185 Set_Parent (Desig_Type, Empty);
1186 end if;
1187
f29b857f
ES
1188 -- Check for premature usage of the type being defined
1189
1190 Check_For_Premature_Usage (T_Def);
1191
996ae0b0
RK
1192 -- The return type and/or any parameter type may be incomplete. Mark
1193 -- the subprogram_type as depending on the incomplete type, so that
2b73cf68
JM
1194 -- it can be updated when the full type declaration is seen. This
1195 -- only applies to incomplete types declared in some enclosing scope,
1196 -- not to limited views from other packages.
996ae0b0
RK
1197
1198 if Present (Formals) then
1199 Formal := First_Formal (Desig_Type);
996ae0b0 1200 while Present (Formal) loop
996ae0b0
RK
1201 if Ekind (Formal) /= E_In_Parameter
1202 and then Nkind (T_Def) = N_Access_Function_Definition
1203 then
1204 Error_Msg_N ("functions can only have IN parameters", Formal);
1205 end if;
1206
2b73cf68
JM
1207 if Ekind (Etype (Formal)) = E_Incomplete_Type
1208 and then In_Open_Scopes (Scope (Etype (Formal)))
1209 then
996ae0b0
RK
1210 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1211 Set_Has_Delayed_Freeze (Desig_Type);
1212 end if;
1213
1214 Next_Formal (Formal);
1215 end loop;
1216 end if;
1217
49d8b802
ES
1218 -- If the return type is incomplete, this is legal as long as the
1219 -- type is declared in the current scope and will be completed in
1220 -- it (rather than being part of limited view).
1221
996ae0b0
RK
1222 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1223 and then not Has_Delayed_Freeze (Desig_Type)
49d8b802 1224 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
996ae0b0
RK
1225 then
1226 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1227 Set_Has_Delayed_Freeze (Desig_Type);
1228 end if;
1229
1230 Check_Delayed_Subprogram (Desig_Type);
1231
1232 if Protected_Present (T_Def) then
1233 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1234 Set_Convention (Desig_Type, Convention_Protected);
1235 else
1236 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1237 end if;
1238
7d7af38a
JM
1239 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1240
996ae0b0
RK
1241 Set_Etype (T_Name, T_Name);
1242 Init_Size_Align (T_Name);
1243 Set_Directly_Designated_Type (T_Name, Desig_Type);
1244
0ab80019 1245 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
2820d220
AC
1246
1247 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1248
996ae0b0
RK
1249 Check_Restriction (No_Access_Subprograms, T_Def);
1250 end Access_Subprogram_Declaration;
1251
1252 ----------------------------
1253 -- Access_Type_Declaration --
1254 ----------------------------
1255
1256 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1257 S : constant Node_Id := Subtype_Indication (Def);
1258 P : constant Node_Id := Parent (Def);
996ae0b0
RK
1259 begin
1260 -- Check for permissible use of incomplete type
1261
1262 if Nkind (S) /= N_Subtype_Indication then
1263 Analyze (S);
1264
1265 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1266 Set_Directly_Designated_Type (T, Entity (S));
1267 else
1268 Set_Directly_Designated_Type (T,
1269 Process_Subtype (S, P, T, 'P'));
1270 end if;
1271
1272 else
1273 Set_Directly_Designated_Type (T,
1274 Process_Subtype (S, P, T, 'P'));
1275 end if;
1276
1277 if All_Present (Def) or Constant_Present (Def) then
1278 Set_Ekind (T, E_General_Access_Type);
1279 else
1280 Set_Ekind (T, E_Access_Type);
1281 end if;
1282
1283 if Base_Type (Designated_Type (T)) = T then
1284 Error_Msg_N ("access type cannot designate itself", S);
9dfd2ff8
CC
1285
1286 -- In Ada 2005, the type may have a limited view through some unit
1287 -- in its own context, allowing the following circularity that cannot
1288 -- be detected earlier
1289
1290 elsif Is_Class_Wide_Type (Designated_Type (T))
1291 and then Etype (Designated_Type (T)) = T
1292 then
1293 Error_Msg_N
1294 ("access type cannot designate its own classwide type", S);
950d3e7d
ES
1295
1296 -- Clean up indication of tagged status to prevent cascaded errors
1297
1298 Set_Is_Tagged_Type (T, False);
996ae0b0
RK
1299 end if;
1300
fbf5a39b 1301 Set_Etype (T, T);
996ae0b0
RK
1302
1303 -- If the type has appeared already in a with_type clause, it is
1304 -- frozen and the pointer size is already set. Else, initialize.
1305
1306 if not From_With_Type (T) then
1307 Init_Size_Align (T);
1308 end if;
1309
996ae0b0
RK
1310 -- Note that Has_Task is always false, since the access type itself
1311 -- is not a task type. See Einfo for more description on this point.
1312 -- Exactly the same consideration applies to Has_Controlled_Component.
1313
1314 Set_Has_Task (T, False);
1315 Set_Has_Controlled_Component (T, False);
2820d220 1316
ce4a6e84
RD
1317 -- Initialize Associated_Final_Chain explicitly to Empty, to avoid
1318 -- problems where an incomplete view of this entity has been previously
1319 -- established by a limited with and an overlaid version of this field
1320 -- (Stored_Constraint) was initialized for the incomplete view.
1321
1322 Set_Associated_Final_Chain (T, Empty);
1323
0ab80019 1324 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
2820d220
AC
1325 -- attributes
1326
1327 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1328 Set_Is_Access_Constant (T, Constant_Present (Def));
996ae0b0
RK
1329 end Access_Type_Declaration;
1330
758c442c
GD
1331 ----------------------------------
1332 -- Add_Interface_Tag_Components --
1333 ----------------------------------
1334
88b32fc3 1335 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
758c442c 1336 Loc : constant Source_Ptr := Sloc (N);
758c442c
GD
1337 L : List_Id;
1338 Last_Tag : Node_Id;
fea9e956 1339
758c442c 1340 procedure Add_Tag (Iface : Entity_Id);
88b32fc3 1341 -- Add tag for one of the progenitor interfaces
758c442c
GD
1342
1343 -------------
1344 -- Add_Tag --
1345 -------------
1346
1347 procedure Add_Tag (Iface : Entity_Id) is
57193e09
TQ
1348 Decl : Node_Id;
1349 Def : Node_Id;
1350 Tag : Entity_Id;
1351 Offset : Entity_Id;
758c442c
GD
1352
1353 begin
1354 pragma Assert (Is_Tagged_Type (Iface)
1355 and then Is_Interface (Iface));
1356
1357 Def :=
1358 Make_Component_Definition (Loc,
1359 Aliased_Present => True,
1360 Subtype_Indication =>
1361 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1362
1363 Tag := Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1364
1365 Decl :=
1366 Make_Component_Declaration (Loc,
1367 Defining_Identifier => Tag,
1368 Component_Definition => Def);
1369
1370 Analyze_Component_Declaration (Decl);
1371
1372 Set_Analyzed (Decl);
1373 Set_Ekind (Tag, E_Component);
758c442c 1374 Set_Is_Tag (Tag);
2b73cf68 1375 Set_Is_Aliased (Tag);
7d7af38a 1376 Set_Related_Type (Tag, Iface);
758c442c
GD
1377 Init_Component_Location (Tag);
1378
1379 pragma Assert (Is_Frozen (Iface));
1380
1381 Set_DT_Entry_Count (Tag,
1382 DT_Entry_Count (First_Entity (Iface)));
1383
57193e09 1384 if No (Last_Tag) then
758c442c
GD
1385 Prepend (Decl, L);
1386 else
1387 Insert_After (Last_Tag, Decl);
1388 end if;
1389
1390 Last_Tag := Decl;
57193e09
TQ
1391
1392 -- If the ancestor has discriminants we need to give special support
1393 -- to store the offset_to_top value of the secondary dispatch tables.
1394 -- For this purpose we add a supplementary component just after the
1395 -- field that contains the tag associated with each secondary DT.
1396
1397 if Typ /= Etype (Typ)
1398 and then Has_Discriminants (Etype (Typ))
1399 then
1400 Def :=
1401 Make_Component_Definition (Loc,
1402 Subtype_Indication =>
1403 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1404
1405 Offset :=
1406 Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1407
1408 Decl :=
1409 Make_Component_Declaration (Loc,
1410 Defining_Identifier => Offset,
1411 Component_Definition => Def);
1412
1413 Analyze_Component_Declaration (Decl);
1414
1415 Set_Analyzed (Decl);
1416 Set_Ekind (Offset, E_Component);
2b73cf68 1417 Set_Is_Aliased (Offset);
7d7af38a 1418 Set_Related_Type (Offset, Iface);
57193e09
TQ
1419 Init_Component_Location (Offset);
1420 Insert_After (Last_Tag, Decl);
1421 Last_Tag := Decl;
1422 end if;
758c442c
GD
1423 end Add_Tag;
1424
fea9e956
ES
1425 -- Local variables
1426
ce2b6ba5
JM
1427 Elmt : Elmt_Id;
1428 Ext : Node_Id;
1429 Comp : Node_Id;
fea9e956 1430
9dfd2ff8 1431 -- Start of processing for Add_Interface_Tag_Components
758c442c
GD
1432
1433 begin
2b73cf68
JM
1434 if not RTE_Available (RE_Interface_Tag) then
1435 Error_Msg
1436 ("(Ada 2005) interface types not supported by this run-time!",
1437 Sloc (N));
1438 return;
1439 end if;
1440
758c442c 1441 if Ekind (Typ) /= E_Record_Type
fea9e956
ES
1442 or else (Is_Concurrent_Record_Type (Typ)
1443 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1444 or else (not Is_Concurrent_Record_Type (Typ)
ce2b6ba5
JM
1445 and then No (Interfaces (Typ))
1446 and then Is_Empty_Elmt_List (Interfaces (Typ)))
758c442c
GD
1447 then
1448 return;
1449 end if;
1450
fea9e956
ES
1451 -- Find the current last tag
1452
1453 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1454 Ext := Record_Extension_Part (Type_Definition (N));
1455 else
1456 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1457 Ext := Type_Definition (N);
1458 end if;
758c442c 1459
fea9e956 1460 Last_Tag := Empty;
758c442c 1461
fea9e956
ES
1462 if not (Present (Component_List (Ext))) then
1463 Set_Null_Present (Ext, False);
1464 L := New_List;
1465 Set_Component_List (Ext,
1466 Make_Component_List (Loc,
1467 Component_Items => L,
1468 Null_Present => False));
1469 else
758c442c 1470 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
fea9e956
ES
1471 L := Component_Items
1472 (Component_List
1473 (Record_Extension_Part
1474 (Type_Definition (N))));
758c442c 1475 else
fea9e956
ES
1476 L := Component_Items
1477 (Component_List
1478 (Type_Definition (N)));
758c442c
GD
1479 end if;
1480
fea9e956 1481 -- Find the last tag component
758c442c 1482
fea9e956
ES
1483 Comp := First (L);
1484 while Present (Comp) loop
2b73cf68
JM
1485 if Nkind (Comp) = N_Component_Declaration
1486 and then Is_Tag (Defining_Identifier (Comp))
1487 then
fea9e956 1488 Last_Tag := Comp;
758c442c
GD
1489 end if;
1490
fea9e956
ES
1491 Next (Comp);
1492 end loop;
1493 end if;
758c442c 1494
fea9e956
ES
1495 -- At this point L references the list of components and Last_Tag
1496 -- references the current last tag (if any). Now we add the tag
1497 -- corresponding with all the interfaces that are not implemented
1498 -- by the parent.
758c442c 1499
ce2b6ba5
JM
1500 if Present (Interfaces (Typ)) then
1501 Elmt := First_Elmt (Interfaces (Typ));
758c442c
GD
1502 while Present (Elmt) loop
1503 Add_Tag (Node (Elmt));
1504 Next_Elmt (Elmt);
1505 end loop;
1506 end if;
1507 end Add_Interface_Tag_Components;
1508
996ae0b0
RK
1509 -----------------------------------
1510 -- Analyze_Component_Declaration --
1511 -----------------------------------
1512
1513 procedure Analyze_Component_Declaration (N : Node_Id) is
1514 Id : constant Entity_Id := Defining_Identifier (N);
2b73cf68 1515 E : constant Node_Id := Expression (N);
996ae0b0
RK
1516 T : Entity_Id;
1517 P : Entity_Id;
1518
5d09245e
AC
1519 function Contains_POC (Constr : Node_Id) return Boolean;
1520 -- Determines whether a constraint uses the discriminant of a record
1521 -- type thus becoming a per-object constraint (POC).
1522
57193e09 1523 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
88b32fc3
BD
1524 -- Typ is the type of the current component, check whether this type is
1525 -- a limited type. Used to validate declaration against that of
1526 -- enclosing record.
57193e09 1527
5d09245e
AC
1528 ------------------
1529 -- Contains_POC --
1530 ------------------
1531
1532 function Contains_POC (Constr : Node_Id) return Boolean is
1533 begin
dc06abec 1534 -- Prevent cascaded errors
2b73cf68
JM
1535
1536 if Error_Posted (Constr) then
1537 return False;
1538 end if;
1539
5d09245e 1540 case Nkind (Constr) is
5d09245e 1541 when N_Attribute_Reference =>
2b73cf68
JM
1542 return
1543 Attribute_Name (Constr) = Name_Access
1544 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
5d09245e
AC
1545
1546 when N_Discriminant_Association =>
1547 return Denotes_Discriminant (Expression (Constr));
1548
1549 when N_Identifier =>
1550 return Denotes_Discriminant (Constr);
1551
1552 when N_Index_Or_Discriminant_Constraint =>
1553 declare
9dfd2ff8 1554 IDC : Node_Id;
71d9e9f2 1555
5d09245e 1556 begin
9dfd2ff8 1557 IDC := First (Constraints (Constr));
5d09245e
AC
1558 while Present (IDC) loop
1559
9dfd2ff8 1560 -- One per-object constraint is sufficient
5d09245e
AC
1561
1562 if Contains_POC (IDC) then
1563 return True;
1564 end if;
1565
1566 Next (IDC);
1567 end loop;
1568
1569 return False;
1570 end;
1571
1572 when N_Range =>
1573 return Denotes_Discriminant (Low_Bound (Constr))
71d9e9f2 1574 or else
5d09245e
AC
1575 Denotes_Discriminant (High_Bound (Constr));
1576
1577 when N_Range_Constraint =>
1578 return Denotes_Discriminant (Range_Expression (Constr));
1579
1580 when others =>
1581 return False;
1582
1583 end case;
1584 end Contains_POC;
1585
57193e09
TQ
1586 ----------------------
1587 -- Is_Known_Limited --
1588 ----------------------
1589
1590 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1591 P : constant Entity_Id := Etype (Typ);
1592 R : constant Entity_Id := Root_Type (Typ);
1593
1594 begin
1595 if Is_Limited_Record (Typ) then
1596 return True;
1597
1598 -- If the root type is limited (and not a limited interface)
1599 -- so is the current type
1600
1601 elsif Is_Limited_Record (R)
1602 and then
1603 (not Is_Interface (R)
1604 or else not Is_Limited_Interface (R))
1605 then
1606 return True;
1607
1608 -- Else the type may have a limited interface progenitor, but a
1609 -- limited record parent.
1610
1611 elsif R /= P
1612 and then Is_Limited_Record (P)
1613 then
1614 return True;
1615
1616 else
1617 return False;
1618 end if;
1619 end Is_Known_Limited;
1620
5d09245e
AC
1621 -- Start of processing for Analyze_Component_Declaration
1622
996ae0b0
RK
1623 begin
1624 Generate_Definition (Id);
1625 Enter_Name (Id);
6e937c1c
AC
1626
1627 if Present (Subtype_Indication (Component_Definition (N))) then
1628 T := Find_Type_Of_Object
1629 (Subtype_Indication (Component_Definition (N)), N);
1630
0ab80019 1631 -- Ada 2005 (AI-230): Access Definition case
6e937c1c 1632
9bc856dd
AC
1633 else
1634 pragma Assert (Present
1635 (Access_Definition (Component_Definition (N))));
1636
6e937c1c
AC
1637 T := Access_Definition
1638 (Related_Nod => N,
1639 N => Access_Definition (Component_Definition (N)));
758c442c 1640 Set_Is_Local_Anonymous_Access (T);
35b7fa6a 1641
0ab80019 1642 -- Ada 2005 (AI-254)
7324bf49
AC
1643
1644 if Present (Access_To_Subprogram_Definition
1645 (Access_Definition (Component_Definition (N))))
1646 and then Protected_Present (Access_To_Subprogram_Definition
1647 (Access_Definition
1648 (Component_Definition (N))))
1649 then
fea9e956 1650 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
7324bf49 1651 end if;
6e937c1c 1652 end if;
996ae0b0 1653
fbf5a39b 1654 -- If the subtype is a constrained subtype of the enclosing record,
9dfd2ff8
CC
1655 -- (which must have a partial view) the back-end does not properly
1656 -- handle the recursion. Rewrite the component declaration with an
758c442c
GD
1657 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1658 -- the tree directly because side effects have already been removed from
1659 -- discriminant constraints.
fbf5a39b
AC
1660
1661 if Ekind (T) = E_Access_Subtype
a397db96 1662 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
fbf5a39b
AC
1663 and then Comes_From_Source (T)
1664 and then Nkind (Parent (T)) = N_Subtype_Declaration
1665 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1666 then
1667 Rewrite
a397db96 1668 (Subtype_Indication (Component_Definition (N)),
fbf5a39b 1669 New_Copy_Tree (Subtype_Indication (Parent (T))));
a397db96
AC
1670 T := Find_Type_Of_Object
1671 (Subtype_Indication (Component_Definition (N)), N);
fbf5a39b
AC
1672 end if;
1673
996ae0b0
RK
1674 -- If the component declaration includes a default expression, then we
1675 -- check that the component is not of a limited type (RM 3.7(5)),
1676 -- and do the special preanalysis of the expression (see section on
fbf5a39b
AC
1677 -- "Handling of Default and Per-Object Expressions" in the spec of
1678 -- package Sem).
996ae0b0 1679
2b73cf68 1680 if Present (E) then
ce4a6e84 1681 Preanalyze_Spec_Expression (E, T);
2b73cf68 1682 Check_Initialization (T, E);
57193e09
TQ
1683
1684 if Ada_Version >= Ada_05
57193e09 1685 and then Ekind (T) = E_Anonymous_Access_Type
3c829e3c 1686 and then Etype (E) /= Any_Type
57193e09
TQ
1687 then
1688 -- Check RM 3.9.2(9): "if the expected type for an expression is
1689 -- an anonymous access-to-specific tagged type, then the object
1690 -- designated by the expression shall not be dynamically tagged
1691 -- unless it is a controlling operand in a call on a dispatching
1692 -- operation"
1693
1694 if Is_Tagged_Type (Directly_Designated_Type (T))
1695 and then
1696 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1697 and then
2b73cf68
JM
1698 Ekind (Directly_Designated_Type (Etype (E))) =
1699 E_Class_Wide_Type
57193e09
TQ
1700 then
1701 Error_Msg_N
dc06abec 1702 ("access to specific tagged type required (RM 3.9.2(9))", E);
57193e09
TQ
1703 end if;
1704
1705 -- (Ada 2005: AI-230): Accessibility check for anonymous
1706 -- components
1707
2b73cf68
JM
1708 if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1709 Error_Msg_N
1710 ("expression has deeper access level than component " &
dc06abec 1711 "(RM 3.10.2 (12.2))", E);
2b73cf68
JM
1712 end if;
1713
1714 -- The initialization expression is a reference to an access
1715 -- discriminant. The type of the discriminant is always deeper
1716 -- than any access type.
88b32fc3 1717
2b73cf68
JM
1718 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1719 and then Is_Entity_Name (E)
1720 and then Ekind (Entity (E)) = E_In_Parameter
1721 and then Present (Discriminal_Link (Entity (E)))
57193e09
TQ
1722 then
1723 Error_Msg_N
2b73cf68
JM
1724 ("discriminant has deeper accessibility level than target",
1725 E);
57193e09
TQ
1726 end if;
1727 end if;
996ae0b0
RK
1728 end if;
1729
1730 -- The parent type may be a private view with unknown discriminants,
1731 -- and thus unconstrained. Regular components must be constrained.
1732
1733 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
8a6a52dc
AC
1734 if Is_Class_Wide_Type (T) then
1735 Error_Msg_N
1736 ("class-wide subtype with unknown discriminants" &
1737 " in component declaration",
1738 Subtype_Indication (Component_Definition (N)));
1739 else
1740 Error_Msg_N
1741 ("unconstrained subtype in component declaration",
1742 Subtype_Indication (Component_Definition (N)));
1743 end if;
996ae0b0
RK
1744
1745 -- Components cannot be abstract, except for the special case of
1746 -- the _Parent field (case of extending an abstract tagged type)
1747
fea9e956 1748 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
996ae0b0
RK
1749 Error_Msg_N ("type of a component cannot be abstract", N);
1750 end if;
1751
1752 Set_Etype (Id, T);
a397db96 1753 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
996ae0b0 1754
a5b62485
AC
1755 -- The component declaration may have a per-object constraint, set
1756 -- the appropriate flag in the defining identifier of the subtype.
5d09245e
AC
1757
1758 if Present (Subtype_Indication (Component_Definition (N))) then
1759 declare
1760 Sindic : constant Node_Id :=
71d9e9f2 1761 Subtype_Indication (Component_Definition (N));
5d09245e
AC
1762 begin
1763 if Nkind (Sindic) = N_Subtype_Indication
1764 and then Present (Constraint (Sindic))
1765 and then Contains_POC (Constraint (Sindic))
1766 then
1767 Set_Has_Per_Object_Constraint (Id);
1768 end if;
1769 end;
1770 end if;
1771
0ab80019 1772 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
71d9e9f2 1773 -- out some static checks.
2820d220 1774
0ab80019 1775 if Ada_Version >= Ada_05
9dfd2ff8 1776 and then Can_Never_Be_Null (T)
2820d220 1777 then
2820d220
AC
1778 Null_Exclusion_Static_Checks (N);
1779 end if;
1780
758c442c
GD
1781 -- If this component is private (or depends on a private type), flag the
1782 -- record type to indicate that some operations are not available.
996ae0b0
RK
1783
1784 P := Private_Component (T);
1785
1786 if Present (P) then
030d25f4 1787
71d9e9f2 1788 -- Check for circular definitions
996ae0b0
RK
1789
1790 if P = Any_Type then
1791 Set_Etype (Id, Any_Type);
1792
1793 -- There is a gap in the visibility of operations only if the
1794 -- component type is not defined in the scope of the record type.
1795
1796 elsif Scope (P) = Scope (Current_Scope) then
1797 null;
1798
1799 elsif Is_Limited_Type (P) then
1800 Set_Is_Limited_Composite (Current_Scope);
1801
1802 else
1803 Set_Is_Private_Composite (Current_Scope);
1804 end if;
1805 end if;
1806
1807 if P /= Any_Type
1808 and then Is_Limited_Type (T)
1809 and then Chars (Id) /= Name_uParent
1810 and then Is_Tagged_Type (Current_Scope)
1811 then
1812 if Is_Derived_Type (Current_Scope)
57193e09 1813 and then not Is_Known_Limited (Current_Scope)
996ae0b0
RK
1814 then
1815 Error_Msg_N
1816 ("extension of nonlimited type cannot have limited components",
1817 N);
57193e09
TQ
1818
1819 if Is_Interface (Root_Type (Current_Scope)) then
1820 Error_Msg_N
1821 ("\limitedness is not inherited from limited interface", N);
1822 Error_Msg_N
1823 ("\add LIMITED to type indication", N);
1824 end if;
1825
fbf5a39b 1826 Explain_Limited_Type (T, N);
996ae0b0
RK
1827 Set_Etype (Id, Any_Type);
1828 Set_Is_Limited_Composite (Current_Scope, False);
1829
1830 elsif not Is_Derived_Type (Current_Scope)
1831 and then not Is_Limited_Record (Current_Scope)
653da906 1832 and then not Is_Concurrent_Type (Current_Scope)
996ae0b0 1833 then
fbf5a39b
AC
1834 Error_Msg_N
1835 ("nonlimited tagged type cannot have limited components", N);
1836 Explain_Limited_Type (T, N);
996ae0b0
RK
1837 Set_Etype (Id, Any_Type);
1838 Set_Is_Limited_Composite (Current_Scope, False);
1839 end if;
1840 end if;
1841
1842 Set_Original_Record_Component (Id, Id);
1843 end Analyze_Component_Declaration;
1844
1845 --------------------------
1846 -- Analyze_Declarations --
1847 --------------------------
1848
1849 procedure Analyze_Declarations (L : List_Id) is
1850 D : Node_Id;
996ae0b0 1851 Freeze_From : Entity_Id := Empty;
88b32fc3 1852 Next_Node : Node_Id;
996ae0b0
RK
1853
1854 procedure Adjust_D;
1855 -- Adjust D not to include implicit label declarations, since these
1856 -- have strange Sloc values that result in elaboration check problems.
fbf5a39b
AC
1857 -- (They have the sloc of the label as found in the source, and that
1858 -- is ahead of the current declarative part).
1859
1860 --------------
1861 -- Adjust_D --
1862 --------------
996ae0b0
RK
1863
1864 procedure Adjust_D is
1865 begin
1866 while Present (Prev (D))
1867 and then Nkind (D) = N_Implicit_Label_Declaration
1868 loop
1869 Prev (D);
1870 end loop;
1871 end Adjust_D;
1872
1873 -- Start of processing for Analyze_Declarations
1874
1875 begin
1876 D := First (L);
1877 while Present (D) loop
1878
1879 -- Complete analysis of declaration
1880
1881 Analyze (D);
1882 Next_Node := Next (D);
1883
1884 if No (Freeze_From) then
1885 Freeze_From := First_Entity (Current_Scope);
1886 end if;
1887
1888 -- At the end of a declarative part, freeze remaining entities
a5b62485
AC
1889 -- declared in it. The end of the visible declarations of package
1890 -- specification is not the end of a declarative part if private
1891 -- declarations are present. The end of a package declaration is a
1892 -- freezing point only if it a library package. A task definition or
1893 -- protected type definition is not a freeze point either. Finally,
1894 -- we do not freeze entities in generic scopes, because there is no
1895 -- code generated for them and freeze nodes will be generated for
1896 -- the instance.
996ae0b0
RK
1897
1898 -- The end of a package instantiation is not a freeze point, but
1899 -- for now we make it one, because the generic body is inserted
1900 -- (currently) immediately after. Generic instantiations will not
1901 -- be a freeze point once delayed freezing of bodies is implemented.
1902 -- (This is needed in any case for early instantiations ???).
1903
1904 if No (Next_Node) then
7d7af38a
JM
1905 if Nkind_In (Parent (L), N_Component_List,
1906 N_Task_Definition,
1907 N_Protected_Definition)
996ae0b0
RK
1908 then
1909 null;
1910
1911 elsif Nkind (Parent (L)) /= N_Package_Specification then
996ae0b0
RK
1912 if Nkind (Parent (L)) = N_Package_Body then
1913 Freeze_From := First_Entity (Current_Scope);
1914 end if;
1915
1916 Adjust_D;
1917 Freeze_All (Freeze_From, D);
1918 Freeze_From := Last_Entity (Current_Scope);
1919
1920 elsif Scope (Current_Scope) /= Standard_Standard
1921 and then not Is_Child_Unit (Current_Scope)
1922 and then No (Generic_Parent (Parent (L)))
1923 then
1924 null;
1925
1926 elsif L /= Visible_Declarations (Parent (L))
1927 or else No (Private_Declarations (Parent (L)))
1928 or else Is_Empty_List (Private_Declarations (Parent (L)))
1929 then
1930 Adjust_D;
1931 Freeze_All (Freeze_From, D);
1932 Freeze_From := Last_Entity (Current_Scope);
1933 end if;
1934
1935 -- If next node is a body then freeze all types before the body.
fea9e956
ES
1936 -- An exception occurs for some expander-generated bodies. If these
1937 -- are generated at places where in general language rules would not
1938 -- allow a freeze point, then we assume that the expander has
1939 -- explicitly checked that all required types are properly frozen,
1940 -- and we do not cause general freezing here. This special circuit
1941 -- is used when the encountered body is marked as having already
1942 -- been analyzed.
1943
1944 -- In all other cases (bodies that come from source, and expander
1945 -- generated bodies that have not been analyzed yet), freeze all
1946 -- types now. Note that in the latter case, the expander must take
1947 -- care to attach the bodies at a proper place in the tree so as to
1948 -- not cause unwanted freezing at that point.
996ae0b0
RK
1949
1950 elsif not Analyzed (Next_Node)
7d7af38a
JM
1951 and then (Nkind_In (Next_Node, N_Subprogram_Body,
1952 N_Entry_Body,
1953 N_Package_Body,
1954 N_Protected_Body,
1955 N_Task_Body)
1956 or else
1957 Nkind (Next_Node) in N_Body_Stub)
996ae0b0
RK
1958 then
1959 Adjust_D;
1960 Freeze_All (Freeze_From, D);
1961 Freeze_From := Last_Entity (Current_Scope);
1962 end if;
1963
1964 D := Next_Node;
1965 end loop;
996ae0b0
RK
1966 end Analyze_Declarations;
1967
996ae0b0
RK
1968 ----------------------------------
1969 -- Analyze_Incomplete_Type_Decl --
1970 ----------------------------------
1971
1972 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1973 F : constant Boolean := Is_Pure (Current_Scope);
1974 T : Entity_Id;
1975
1976 begin
1977 Generate_Definition (Defining_Identifier (N));
1978
1979 -- Process an incomplete declaration. The identifier must not have been
1980 -- declared already in the scope. However, an incomplete declaration may
1981 -- appear in the private part of a package, for a private type that has
1982 -- already been declared.
1983
523456db 1984 -- In this case, the discriminants (if any) must match
996ae0b0
RK
1985
1986 T := Find_Type_Name (N);
1987
1988 Set_Ekind (T, E_Incomplete_Type);
1989 Init_Size_Align (T);
1990 Set_Is_First_Subtype (T, True);
1991 Set_Etype (T, T);
d8db0bca 1992
9dfd2ff8
CC
1993 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
1994 -- incomplete types.
d8db0bca
JM
1995
1996 if Tagged_Present (N) then
1997 Set_Is_Tagged_Type (T);
1998 Make_Class_Wide_Type (T);
1999 Set_Primitive_Operations (T, New_Elmt_List);
2000 end if;
2001
2b73cf68 2002 Push_Scope (T);
996ae0b0 2003
fbf5a39b 2004 Set_Stored_Constraint (T, No_Elist);
996ae0b0
RK
2005
2006 if Present (Discriminant_Specifications (N)) then
2007 Process_Discriminants (N);
2008 end if;
2009
2010 End_Scope;
2011
16b05213 2012 -- If the type has discriminants, non-trivial subtypes may be
a5b62485
AC
2013 -- declared before the full view of the type. The full views of those
2014 -- subtypes will be built after the full view of the type.
996ae0b0
RK
2015
2016 Set_Private_Dependents (T, New_Elmt_List);
2017 Set_Is_Pure (T, F);
2018 end Analyze_Incomplete_Type_Decl;
2019
950d3e7d
ES
2020 -----------------------------------
2021 -- Analyze_Interface_Declaration --
2022 -----------------------------------
2023
2024 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2b73cf68
JM
2025 CW : constant Entity_Id := Class_Wide_Type (T);
2026
950d3e7d 2027 begin
ce2b6ba5 2028 Set_Is_Tagged_Type (T);
950d3e7d 2029
ce2b6ba5
JM
2030 Set_Is_Limited_Record (T, Limited_Present (Def)
2031 or else Task_Present (Def)
2032 or else Protected_Present (Def)
2033 or else Synchronized_Present (Def));
950d3e7d 2034
2b73cf68
JM
2035 -- Type is abstract if full declaration carries keyword, or if previous
2036 -- partial view did.
950d3e7d 2037
fea9e956 2038 Set_Is_Abstract_Type (T);
ce2b6ba5 2039 Set_Is_Interface (T);
950d3e7d 2040
2b73cf68
JM
2041 -- Type is a limited interface if it includes the keyword limited, task,
2042 -- protected, or synchronized.
2043
2044 Set_Is_Limited_Interface
2045 (T, Limited_Present (Def)
2046 or else Protected_Present (Def)
2047 or else Synchronized_Present (Def)
2048 or else Task_Present (Def));
2049
ce2b6ba5
JM
2050 Set_Is_Protected_Interface (T, Protected_Present (Def));
2051 Set_Is_Task_Interface (T, Task_Present (Def));
2b73cf68
JM
2052
2053 -- Type is a synchronized interface if it includes the keyword task,
2054 -- protected, or synchronized.
2055
2056 Set_Is_Synchronized_Interface
2057 (T, Synchronized_Present (Def)
2058 or else Protected_Present (Def)
2059 or else Task_Present (Def));
2060
ce2b6ba5
JM
2061 Set_Interfaces (T, New_Elmt_List);
2062 Set_Primitive_Operations (T, New_Elmt_List);
2b73cf68
JM
2063
2064 -- Complete the decoration of the class-wide entity if it was already
f3d57416 2065 -- built (i.e. during the creation of the limited view)
2b73cf68
JM
2066
2067 if Present (CW) then
2068 Set_Is_Interface (CW);
2069 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2070 Set_Is_Protected_Interface (CW, Is_Protected_Interface (T));
2071 Set_Is_Synchronized_Interface (CW, Is_Synchronized_Interface (T));
2072 Set_Is_Task_Interface (CW, Is_Task_Interface (T));
2073 end if;
ce4a6e84
RD
2074
2075 -- Check runtime support for synchronized interfaces
2076
2077 if VM_Target = No_VM
2078 and then (Is_Task_Interface (T)
2079 or else Is_Protected_Interface (T)
2080 or else Is_Synchronized_Interface (T))
2081 and then not RTE_Available (RE_Select_Specific_Data)
2082 then
2083 Error_Msg_CRT ("synchronized interfaces", T);
2084 end if;
950d3e7d
ES
2085 end Analyze_Interface_Declaration;
2086
996ae0b0
RK
2087 -----------------------------
2088 -- Analyze_Itype_Reference --
2089 -----------------------------
2090
9dfd2ff8
CC
2091 -- Nothing to do. This node is placed in the tree only for the benefit of
2092 -- back end processing, and has no effect on the semantic processing.
996ae0b0
RK
2093
2094 procedure Analyze_Itype_Reference (N : Node_Id) is
2095 begin
2096 pragma Assert (Is_Itype (Itype (N)));
2097 null;
2098 end Analyze_Itype_Reference;
2099
2100 --------------------------------
2101 -- Analyze_Number_Declaration --
2102 --------------------------------
2103
2104 procedure Analyze_Number_Declaration (N : Node_Id) is
2105 Id : constant Entity_Id := Defining_Identifier (N);
2106 E : constant Node_Id := Expression (N);
2107 T : Entity_Id;
2108 Index : Interp_Index;
2109 It : Interp;
2110
2111 begin
2112 Generate_Definition (Id);
2113 Enter_Name (Id);
2114
2115 -- This is an optimization of a common case of an integer literal
2116
2117 if Nkind (E) = N_Integer_Literal then
2118 Set_Is_Static_Expression (E, True);
2119 Set_Etype (E, Universal_Integer);
2120
2121 Set_Etype (Id, Universal_Integer);
2122 Set_Ekind (Id, E_Named_Integer);
2123 Set_Is_Frozen (Id, True);
2124 return;
2125 end if;
2126
2127 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2128
ce9e9122
RD
2129 -- Process expression, replacing error by integer zero, to avoid
2130 -- cascaded errors or aborts further along in the processing
2131
2132 -- Replace Error by integer zero, which seems least likely to
2133 -- cause cascaded errors.
2134
2135 if E = Error then
2136 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2137 Set_Error_Posted (E);
2138 end if;
2139
996ae0b0
RK
2140 Analyze (E);
2141
2142 -- Verify that the expression is static and numeric. If
2143 -- the expression is overloaded, we apply the preference
2144 -- rule that favors root numeric types.
2145
2146 if not Is_Overloaded (E) then
2147 T := Etype (E);
2148
2149 else
2150 T := Any_Type;
996ae0b0 2151
9dfd2ff8 2152 Get_First_Interp (E, Index, It);
996ae0b0
RK
2153 while Present (It.Typ) loop
2154 if (Is_Integer_Type (It.Typ)
2155 or else Is_Real_Type (It.Typ))
2156 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2157 then
2158 if T = Any_Type then
2159 T := It.Typ;
2160
2161 elsif It.Typ = Universal_Real
2162 or else It.Typ = Universal_Integer
2163 then
ffe9aba8 2164 -- Choose universal interpretation over any other
996ae0b0
RK
2165
2166 T := It.Typ;
2167 exit;
2168 end if;
2169 end if;
2170
2171 Get_Next_Interp (Index, It);
2172 end loop;
2173 end if;
2174
2175 if Is_Integer_Type (T) then
2176 Resolve (E, T);
2177 Set_Etype (Id, Universal_Integer);
2178 Set_Ekind (Id, E_Named_Integer);
2179
2180 elsif Is_Real_Type (T) then
2181
9dfd2ff8
CC
2182 -- Because the real value is converted to universal_real, this is a
2183 -- legal context for a universal fixed expression.
996ae0b0
RK
2184
2185 if T = Universal_Fixed then
2186 declare
2187 Loc : constant Source_Ptr := Sloc (N);
2188 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2189 Subtype_Mark =>
2190 New_Occurrence_Of (Universal_Real, Loc),
2191 Expression => Relocate_Node (E));
2192
2193 begin
2194 Rewrite (E, Conv);
2195 Analyze (E);
2196 end;
2197
2198 elsif T = Any_Fixed then
2199 Error_Msg_N ("illegal context for mixed mode operation", E);
2200
9dfd2ff8
CC
2201 -- Expression is of the form : universal_fixed * integer. Try to
2202 -- resolve as universal_real.
996ae0b0
RK
2203
2204 T := Universal_Real;
2205 Set_Etype (E, T);
2206 end if;
2207
2208 Resolve (E, T);
2209 Set_Etype (Id, Universal_Real);
2210 Set_Ekind (Id, E_Named_Real);
2211
2212 else
2213 Wrong_Type (E, Any_Numeric);
2214 Resolve (E, T);
fbf5a39b 2215
996ae0b0
RK
2216 Set_Etype (Id, T);
2217 Set_Ekind (Id, E_Constant);
fbf5a39b 2218 Set_Never_Set_In_Source (Id, True);
996ae0b0
RK
2219 Set_Is_True_Constant (Id, True);
2220 return;
2221 end if;
2222
7d7af38a 2223 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
996ae0b0
RK
2224 Set_Etype (E, Etype (Id));
2225 end if;
2226
2227 if not Is_OK_Static_Expression (E) then
fbf5a39b
AC
2228 Flag_Non_Static_Expr
2229 ("non-static expression used in number declaration!", E);
996ae0b0
RK
2230 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2231 Set_Etype (E, Any_Type);
2232 end if;
996ae0b0
RK
2233 end Analyze_Number_Declaration;
2234
2235 --------------------------------
2236 -- Analyze_Object_Declaration --
2237 --------------------------------
2238
2239 procedure Analyze_Object_Declaration (N : Node_Id) is
2240 Loc : constant Source_Ptr := Sloc (N);
2241 Id : constant Entity_Id := Defining_Identifier (N);
2242 T : Entity_Id;
2243 Act_T : Entity_Id;
2244
2245 E : Node_Id := Expression (N);
2246 -- E is set to Expression (N) throughout this routine. When
2247 -- Expression (N) is modified, E is changed accordingly.
2248
2249 Prev_Entity : Entity_Id := Empty;
2250
6e937c1c 2251 function Count_Tasks (T : Entity_Id) return Uint;
ce4a6e84
RD
2252 -- This function is called when a non-generic library level object of a
2253 -- task type is declared. Its function is to count the static number of
2254 -- tasks declared within the type (it is only called if Has_Tasks is set
2255 -- for T). As a side effect, if an array of tasks with non-static bounds
2256 -- or a variant record type is encountered, Check_Restrictions is called
a5b62485 2257 -- indicating the count is unknown.
6e937c1c 2258
6e937c1c
AC
2259 -----------------
2260 -- Count_Tasks --
2261 -----------------
2262
2263 function Count_Tasks (T : Entity_Id) return Uint is
2264 C : Entity_Id;
2265 X : Node_Id;
2266 V : Uint;
2267
2268 begin
2269 if Is_Task_Type (T) then
2270 return Uint_1;
2271
2272 elsif Is_Record_Type (T) then
2273 if Has_Discriminants (T) then
2274 Check_Restriction (Max_Tasks, N);
2275 return Uint_0;
2276
2277 else
2278 V := Uint_0;
2279 C := First_Component (T);
2280 while Present (C) loop
2281 V := V + Count_Tasks (Etype (C));
2282 Next_Component (C);
2283 end loop;
2284
2285 return V;
2286 end if;
2287
2288 elsif Is_Array_Type (T) then
2289 X := First_Index (T);
2290 V := Count_Tasks (Component_Type (T));
2291 while Present (X) loop
2292 C := Etype (X);
2293
2294 if not Is_Static_Subtype (C) then
2295 Check_Restriction (Max_Tasks, N);
2296 return Uint_0;
2297 else
2298 V := V * (UI_Max (Uint_0,
2299 Expr_Value (Type_High_Bound (C)) -
2300 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2301 end if;
2302
2303 Next_Index (X);
2304 end loop;
2305
2306 return V;
2307
2308 else
2309 return Uint_0;
2310 end if;
2311 end Count_Tasks;
2312
996ae0b0
RK
2313 -- Start of processing for Analyze_Object_Declaration
2314
2315 begin
2316 -- There are three kinds of implicit types generated by an
2317 -- object declaration:
2318
2319 -- 1. Those for generated by the original Object Definition
2320
2321 -- 2. Those generated by the Expression
2322
2323 -- 3. Those used to constrained the Object Definition with the
2324 -- expression constraints when it is unconstrained
2325
2326 -- They must be generated in this order to avoid order of elaboration
2327 -- issues. Thus the first step (after entering the name) is to analyze
2328 -- the object definition.
2329
2330 if Constant_Present (N) then
2331 Prev_Entity := Current_Entity_In_Scope (Id);
2332
996ae0b0 2333 if Present (Prev_Entity)
ce4a6e84 2334 and then
5a989c6b
AC
2335 -- If the homograph is an implicit subprogram, it is overridden
2336 -- by the current declaration.
2337
ce4a6e84
RD
2338 ((Is_Overloadable (Prev_Entity)
2339 and then Is_Inherited_Operation (Prev_Entity))
2340
2341 -- The current object is a discriminal generated for an entry
2342 -- family index. Even though the index is a constant, in this
f3d0f304 2343 -- particular context there is no true constant redeclaration.
ce4a6e84
RD
2344 -- Enter_Name will handle the visibility.
2345
2346 or else
2347 (Is_Discriminal (Id)
2348 and then Ekind (Discriminal_Link (Id)) =
5a989c6b
AC
2349 E_Entry_Index_Parameter)
2350
2351 -- The current object is the renaming for a generic declared
2352 -- within the instance.
2353
2354 or else
2355 (Ekind (Prev_Entity) = E_Package
2356 and then
2357 Nkind (Parent (Prev_Entity)) = N_Package_Renaming_Declaration
2358 and then
2359 not Comes_From_Source (Prev_Entity)
2360 and then
2361 Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
996ae0b0
RK
2362 then
2363 Prev_Entity := Empty;
2364 end if;
2365 end if;
2366
2367 if Present (Prev_Entity) then
2368 Constant_Redeclaration (Id, N, T);
2369
2370 Generate_Reference (Prev_Entity, Id, 'c');
07fc65c4 2371 Set_Completion_Referenced (Id);
996ae0b0
RK
2372
2373 if Error_Posted (N) then
71d9e9f2 2374
996ae0b0
RK
2375 -- Type mismatch or illegal redeclaration, Do not analyze
2376 -- expression to avoid cascaded errors.
2377
2378 T := Find_Type_Of_Object (Object_Definition (N), N);
2379 Set_Etype (Id, T);
2380 Set_Ekind (Id, E_Variable);
2381 return;
2382 end if;
2383
9dfd2ff8
CC
2384 -- In the normal case, enter identifier at the start to catch premature
2385 -- usage in the initialization expression.
996ae0b0
RK
2386
2387 else
2388 Generate_Definition (Id);
2389 Enter_Name (Id);
2390
dc06abec
RD
2391 Mark_Coextensions (N, Object_Definition (N));
2392
996ae0b0
RK
2393 T := Find_Type_Of_Object (Object_Definition (N), N);
2394
fea9e956
ES
2395 if Nkind (Object_Definition (N)) = N_Access_Definition
2396 and then Present
2397 (Access_To_Subprogram_Definition (Object_Definition (N)))
2398 and then Protected_Present
2399 (Access_To_Subprogram_Definition (Object_Definition (N)))
2400 then
2401 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2402 end if;
2403
996ae0b0
RK
2404 if Error_Posted (Id) then
2405 Set_Etype (Id, T);
2406 Set_Ekind (Id, E_Variable);
2407 return;
2408 end if;
2409 end if;
2410
0ab80019 2411 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2820d220
AC
2412 -- out some static checks
2413
0ab80019 2414 if Ada_Version >= Ada_05
9dfd2ff8 2415 and then Can_Never_Be_Null (T)
2820d220 2416 then
9dfd2ff8
CC
2417 -- In case of aggregates we must also take care of the correct
2418 -- initialization of nested aggregates bug this is done at the
2419 -- point of the analysis of the aggregate (see sem_aggr.adb)
2420
2421 if Present (Expression (N))
2422 and then Nkind (Expression (N)) = N_Aggregate
2423 then
2424 null;
2425
2426 else
2427 declare
2428 Save_Typ : constant Entity_Id := Etype (Id);
2429 begin
2430 Set_Etype (Id, T); -- Temp. decoration for static checks
2431 Null_Exclusion_Static_Checks (N);
2432 Set_Etype (Id, Save_Typ);
2433 end;
2434 end if;
2820d220
AC
2435 end if;
2436
996ae0b0
RK
2437 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2438
2439 -- If deferred constant, make sure context is appropriate. We detect
2440 -- a deferred constant as a constant declaration with no expression.
07fc65c4
GB
2441 -- A deferred constant can appear in a package body if its completion
2442 -- is by means of an interface pragma.
996ae0b0
RK
2443
2444 if Constant_Present (N)
2445 and then No (E)
2446 then
33931112
JM
2447 -- A deferred constant may appear in the declarative part of the
2448 -- following constructs:
2449
2450 -- blocks
2451 -- entry bodies
2452 -- extended return statements
2453 -- package specs
2454 -- package bodies
2455 -- subprogram bodies
2456 -- task bodies
2457
2458 -- When declared inside a package spec, a deferred constant must be
2459 -- completed by a full constant declaration or pragma Import. In all
2460 -- other cases, the only proper completion is pragma Import. Extended
2461 -- return statements are flagged as invalid contexts because they do
2462 -- not have a declarative part and so cannot accommodate the pragma.
2463
b16d9747 2464 if Ekind (Current_Scope) = E_Return_Statement then
996ae0b0 2465 Error_Msg_N
dc06abec 2466 ("invalid context for deferred constant declaration (RM 7.4)",
33931112 2467 N);
fbf5a39b
AC
2468 Error_Msg_N
2469 ("\declaration requires an initialization expression",
2470 N);
996ae0b0
RK
2471 Set_Constant_Present (N, False);
2472
2473 -- In Ada 83, deferred constant must be of private type
2474
2475 elsif not Is_Private_Type (T) then
0ab80019 2476 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
996ae0b0
RK
2477 Error_Msg_N
2478 ("(Ada 83) deferred constant must be private type", N);
2479 end if;
2480 end if;
2481
2482 -- If not a deferred constant, then object declaration freezes its type
2483
2484 else
2485 Check_Fully_Declared (T, N);
2486 Freeze_Before (N, T);
2487 end if;
2488
2489 -- If the object was created by a constrained array definition, then
2490 -- set the link in both the anonymous base type and anonymous subtype
2491 -- that are built to represent the array type to point to the object.
2492
2493 if Nkind (Object_Definition (Declaration_Node (Id))) =
2494 N_Constrained_Array_Definition
2495 then
2496 Set_Related_Array_Object (T, Id);
2497 Set_Related_Array_Object (Base_Type (T), Id);
2498 end if;
2499
2500 -- Special checks for protected objects not at library level
2501
2502 if Is_Protected_Type (T)
2503 and then not Is_Library_Level_Entity (Id)
2504 then
2505 Check_Restriction (No_Local_Protected_Objects, Id);
2506
2507 -- Protected objects with interrupt handlers must be at library level
2508
758c442c
GD
2509 -- Ada 2005: this test is not needed (and the corresponding clause
2510 -- in the RM is removed) because accessibility checks are sufficient
2511 -- to make handlers not at the library level illegal.
2512
2513 if Has_Interrupt_Handler (T)
2514 and then Ada_Version < Ada_05
2515 then
996ae0b0
RK
2516 Error_Msg_N
2517 ("interrupt object can only be declared at library level", Id);
2518 end if;
2519 end if;
2520
2521 -- The actual subtype of the object is the nominal subtype, unless
2522 -- the nominal one is unconstrained and obtained from the expression.
2523
2524 Act_T := T;
2525
2526 -- Process initialization expression if present and not in error
2527
2528 if Present (E) and then E /= Error then
ce4a6e84
RD
2529
2530 -- Generate an error in case of CPP class-wide object initialization.
2531 -- Required because otherwise the expansion of the class-wide
2532 -- assignment would try to use 'size to initialize the object
2533 -- (primitive that is not available in CPP tagged types).
2534
2535 if Is_Class_Wide_Type (Act_T)
33931112
JM
2536 and then
2537 (Is_CPP_Class (Root_Type (Etype (Act_T)))
2538 or else
2539 (Present (Full_View (Root_Type (Etype (Act_T))))
2540 and then
2541 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
ce4a6e84
RD
2542 then
2543 Error_Msg_N
33931112
JM
2544 ("predefined assignment not available for 'C'P'P tagged types",
2545 E);
ce4a6e84
RD
2546 end if;
2547
dc06abec 2548 Mark_Coextensions (N, E);
996ae0b0
RK
2549 Analyze (E);
2550
9bc856dd 2551 -- In case of errors detected in the analysis of the expression,
f29b857f 2552 -- decorate it with the expected type to avoid cascaded errors
9bc856dd 2553
57193e09 2554 if No (Etype (E)) then
9bc856dd
AC
2555 Set_Etype (E, T);
2556 end if;
2557
fbf5a39b
AC
2558 -- If an initialization expression is present, then we set the
2559 -- Is_True_Constant flag. It will be reset if this is a variable
2560 -- and it is indeed modified.
2561
2562 Set_Is_True_Constant (Id, True);
2563
ce4a6e84
RD
2564 -- If we are analyzing a constant declaration, set its completion
2565 -- flag after analyzing and resolving the expression.
2566
2567 if Constant_Present (N) then
2568 Set_Has_Completion (Id);
2569 end if;
2570
2571 -- Set type and resolve (type may be overridden later on)
2572
2573 Set_Etype (Id, T);
2574 Resolve (E, T);
2575
45a9255f
RD
2576 -- If E is null and has been replaced by an N_Raise_Constraint_Error
2577 -- node (which was marked already-analyzed), we need to set the type
2578 -- to something other than Any_Access in order to keep gigi happy.
c775e4a1
AC
2579
2580 if Etype (E) = Any_Access then
2581 Set_Etype (E, T);
2582 end if;
2583
f29b857f
ES
2584 -- If the object is an access to variable, the initialization
2585 -- expression cannot be an access to constant.
17cf3985 2586
f29b857f 2587 if Is_Access_Type (T)
17cf3985 2588 and then not Is_Access_Constant (T)
f29b857f
ES
2589 and then Is_Access_Type (Etype (E))
2590 and then Is_Access_Constant (Etype (E))
17cf3985 2591 then
f29b857f 2592 Error_Msg_N
49d8b802 2593 ("access to variable cannot be initialized " &
f29b857f 2594 "with an access-to-constant expression", E);
17cf3985
AC
2595 end if;
2596
996ae0b0
RK
2597 if not Assignment_OK (N) then
2598 Check_Initialization (T, E);
2599 end if;
2b73cf68 2600
fbf5a39b 2601 Check_Unset_Reference (E);
996ae0b0 2602
88b32fc3
BD
2603 -- If this is a variable, then set current value
2604
2605 if not Constant_Present (N) then
2606 if Compile_Time_Known_Value (E) then
2607 Set_Current_Value (Id, E);
2608 end if;
996ae0b0
RK
2609 end if;
2610
dc06abec
RD
2611 -- Deal with setting of null flags
2612
2613 if Is_Access_Type (T) then
2614 if Known_Non_Null (E) then
2615 Set_Is_Known_Non_Null (Id, True);
2616 elsif Known_Null (E)
2617 and then not Can_Never_Be_Null (Id)
2618 then
2619 Set_Is_Known_Null (Id, True);
2620 end if;
2621 end if;
2622
4755cce9
JM
2623 -- Check incorrect use of dynamically tagged expressions.
2624
2625 if Is_Tagged_Type (T) then
2626 Check_Dynamically_Tagged_Expression
2627 (Expr => E,
2628 Typ => T,
2629 Related_Nod => N);
996ae0b0
RK
2630 end if;
2631
2632 Apply_Scalar_Range_Check (E, T);
2633 Apply_Static_Length_Check (E, T);
2634 end if;
2635
ffe9aba8
AC
2636 -- If the No_Streams restriction is set, check that the type of the
2637 -- object is not, and does not contain, any subtype derived from
2638 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
2639 -- Has_Stream just for efficiency reasons. There is no point in
2640 -- spending time on a Has_Stream check if the restriction is not set.
2641
2642 if Restrictions.Set (No_Streams) then
2643 if Has_Stream (T) then
2644 Check_Restriction (No_Streams, N);
2645 end if;
2646 end if;
2647
996ae0b0
RK
2648 -- Case of unconstrained type
2649
6823270c 2650 if Is_Indefinite_Subtype (T) then
996ae0b0
RK
2651
2652 -- Nothing to do in deferred constant case
2653
2654 if Constant_Present (N) and then No (E) then
2655 null;
2656
2657 -- Case of no initialization present
2658
2659 elsif No (E) then
2660 if No_Initialization (N) then
2661 null;
2662
2663 elsif Is_Class_Wide_Type (T) then
2664 Error_Msg_N
2665 ("initialization required in class-wide declaration ", N);
2666
2667 else
2668 Error_Msg_N
2669 ("unconstrained subtype not allowed (need initialization)",
2670 Object_Definition (N));
ce4a6e84
RD
2671
2672 if Is_Record_Type (T) and then Has_Discriminants (T) then
2673 Error_Msg_N
2674 ("\provide initial value or explicit discriminant values",
2675 Object_Definition (N));
2676
2677 Error_Msg_NE
2678 ("\or give default discriminant values for type&",
2679 Object_Definition (N), T);
2680
2681 elsif Is_Array_Type (T) then
2682 Error_Msg_N
2683 ("\provide initial value or explicit array bounds",
2684 Object_Definition (N));
2685 end if;
996ae0b0
RK
2686 end if;
2687
2688 -- Case of initialization present but in error. Set initial
2689 -- expression as absent (but do not make above complaints)
2690
2691 elsif E = Error then
2692 Set_Expression (N, Empty);
2693 E := Empty;
2694
2695 -- Case of initialization present
2696
2697 else
2698 -- Not allowed in Ada 83
2699
2700 if not Constant_Present (N) then
0ab80019 2701 if Ada_Version = Ada_83
996ae0b0
RK
2702 and then Comes_From_Source (Object_Definition (N))
2703 then
2704 Error_Msg_N
2705 ("(Ada 83) unconstrained variable not allowed",
2706 Object_Definition (N));
2707 end if;
2708 end if;
2709
2710 -- Now we constrain the variable from the initializing expression
2711
2712 -- If the expression is an aggregate, it has been expanded into
2713 -- individual assignments. Retrieve the actual type from the
2714 -- expanded construct.
2715
2716 if Is_Array_Type (T)
2717 and then No_Initialization (N)
2718 and then Nkind (Original_Node (E)) = N_Aggregate
2719 then
2720 Act_T := Etype (E);
2721
0e41a941
AC
2722 -- In case of class-wide interface object declarations we delay
2723 -- the generation of the equivalent record type declarations until
2724 -- its expansion because there are cases in they are not required.
2725
2726 elsif Is_Interface (T) then
2727 null;
2728
996ae0b0
RK
2729 else
2730 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
2731 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
2732 end if;
2733
2734 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
2735
2736 if Aliased_Present (N) then
2737 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2738 end if;
2739
2740 Freeze_Before (N, Act_T);
2741 Freeze_Before (N, T);
2742 end if;
2743
2744 elsif Is_Array_Type (T)
2745 and then No_Initialization (N)
2746 and then Nkind (Original_Node (E)) = N_Aggregate
2747 then
2748 if not Is_Entity_Name (Object_Definition (N)) then
2749 Act_T := Etype (E);
fbf5a39b 2750 Check_Compile_Time_Size (Act_T);
996ae0b0
RK
2751
2752 if Aliased_Present (N) then
2753 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2754 end if;
2755 end if;
2756
2757 -- When the given object definition and the aggregate are specified
2758 -- independently, and their lengths might differ do a length check.
2759 -- This cannot happen if the aggregate is of the form (others =>...)
2760
2761 if not Is_Constrained (T) then
2762 null;
2763
2514b839 2764 elsif Nkind (E) = N_Raise_Constraint_Error then
449d2be3 2765
ffe9aba8 2766 -- Aggregate is statically illegal. Place back in declaration
449d2be3 2767
2514b839
ES
2768 Set_Expression (N, E);
2769 Set_No_Initialization (N, False);
2770
996ae0b0
RK
2771 elsif T = Etype (E) then
2772 null;
2773
2774 elsif Nkind (E) = N_Aggregate
2775 and then Present (Component_Associations (E))
2776 and then Present (Choices (First (Component_Associations (E))))
2777 and then Nkind (First
2778 (Choices (First (Component_Associations (E))))) = N_Others_Choice
2779 then
2780 null;
2781
2782 else
2783 Apply_Length_Check (E, T);
2784 end if;
2785
88b32fc3
BD
2786 -- If the type is limited unconstrained with defaulted discriminants
2787 -- and there is no expression, then the object is constrained by the
2788 -- defaults, so it is worthwhile building the corresponding subtype.
2789
996ae0b0
RK
2790 elsif (Is_Limited_Record (T)
2791 or else Is_Concurrent_Type (T))
2792 and then not Is_Constrained (T)
2793 and then Has_Discriminants (T)
2794 then
030d25f4 2795 if No (E) then
88b32fc3 2796 Act_T := Build_Default_Subtype (T, N);
030d25f4
JM
2797 else
2798 -- Ada 2005: a limited object may be initialized by means of an
2799 -- aggregate. If the type has default discriminants it has an
2800 -- unconstrained nominal type, Its actual subtype will be obtained
2801 -- from the aggregate, and not from the default discriminants.
2802
2803 Act_T := Etype (E);
2804 end if;
2805
996ae0b0
RK
2806 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
2807
9dfd2ff8
CC
2808 elsif Present (Underlying_Type (T))
2809 and then not Is_Constrained (Underlying_Type (T))
2810 and then Has_Discriminants (Underlying_Type (T))
996ae0b0 2811 and then Nkind (E) = N_Function_Call
9dfd2ff8 2812 and then Constant_Present (N)
996ae0b0
RK
2813 then
2814 -- The back-end has problems with constants of a discriminated type
2815 -- with defaults, if the initial value is a function call. We
2816 -- generate an intermediate temporary for the result of the call.
2817 -- It is unclear why this should make it acceptable to gcc. ???
2818
2819 Remove_Side_Effects (E);
2820 end if;
2821
ce4a6e84
RD
2822 -- Check No_Wide_Characters restriction
2823
2824 if T = Standard_Wide_Character
2825 or else T = Standard_Wide_Wide_Character
996ae0b0 2826 or else Root_Type (T) = Standard_Wide_String
82c80734 2827 or else Root_Type (T) = Standard_Wide_Wide_String
996ae0b0
RK
2828 then
2829 Check_Restriction (No_Wide_Characters, Object_Definition (N));
2830 end if;
2831
dc06abec
RD
2832 -- Indicate this is not set in source. Certainly true for constants,
2833 -- and true for variables so far (will be reset for a variable if and
2834 -- when we encounter a modification in the source).
2835
2836 Set_Never_Set_In_Source (Id, True);
2837
996ae0b0
RK
2838 -- Now establish the proper kind and type of the object
2839
2840 if Constant_Present (N) then
dc06abec
RD
2841 Set_Ekind (Id, E_Constant);
2842 Set_Is_True_Constant (Id, True);
996ae0b0
RK
2843
2844 else
2845 Set_Ekind (Id, E_Variable);
2846
2847 -- A variable is set as shared passive if it appears in a shared
2848 -- passive package, and is at the outer level. This is not done
2849 -- for entities generated during expansion, because those are
2850 -- always manipulated locally.
2851
2852 if Is_Shared_Passive (Current_Scope)
2853 and then Is_Library_Level_Entity (Id)
2854 and then Comes_From_Source (Id)
2855 then
2856 Set_Is_Shared_Passive (Id);
2857 Check_Shared_Var (Id, T, N);
2858 end if;
2859
dc06abec 2860 -- Set Has_Initial_Value if initializing expression present. Note
f3d57416 2861 -- that if there is no initializing expression, we leave the state
dc06abec
RD
2862 -- of this flag unchanged (usually it will be False, but notably in
2863 -- the case of exception choice variables, it will already be true).
fbf5a39b 2864
dc06abec
RD
2865 if Present (E) then
2866 Set_Has_Initial_Value (Id, True);
996ae0b0
RK
2867 end if;
2868 end if;
2869
ce4a6e84 2870 -- Initialize alignment and size and capture alignment setting
dc06abec 2871
ce4a6e84
RD
2872 Init_Alignment (Id);
2873 Init_Esize (Id);
2874 Set_Optimize_Alignment_Flags (Id);
996ae0b0 2875
dc06abec
RD
2876 -- Deal with aliased case
2877
996ae0b0
RK
2878 if Aliased_Present (N) then
2879 Set_Is_Aliased (Id);
2880
88b32fc3
BD
2881 -- If the object is aliased and the type is unconstrained with
2882 -- defaulted discriminants and there is no expression, then the
2883 -- object is constrained by the defaults, so it is worthwhile
2884 -- building the corresponding subtype.
2885
2886 -- Ada 2005 (AI-363): If the aliased object is discriminated and
2887 -- unconstrained, then only establish an actual subtype if the
2888 -- nominal subtype is indefinite. In definite cases the object is
2889 -- unconstrained in Ada 2005.
2890
996ae0b0
RK
2891 if No (E)
2892 and then Is_Record_Type (T)
2893 and then not Is_Constrained (T)
2894 and then Has_Discriminants (T)
88b32fc3 2895 and then (Ada_Version < Ada_05 or else Is_Indefinite_Subtype (T))
996ae0b0 2896 then
88b32fc3 2897 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
996ae0b0
RK
2898 end if;
2899 end if;
2900
dc06abec
RD
2901 -- Now we can set the type of the object
2902
996ae0b0
RK
2903 Set_Etype (Id, Act_T);
2904
dc06abec
RD
2905 -- Deal with controlled types
2906
996ae0b0
RK
2907 if Has_Controlled_Component (Etype (Id))
2908 or else Is_Controlled (Etype (Id))
2909 then
2910 if not Is_Library_Level_Entity (Id) then
2911 Check_Restriction (No_Nested_Finalization, N);
996ae0b0
RK
2912 else
2913 Validate_Controlled_Object (Id);
2914 end if;
2915
9dfd2ff8
CC
2916 -- Generate a warning when an initialization causes an obvious ABE
2917 -- violation. If the init expression is a simple aggregate there
2918 -- shouldn't be any initialize/adjust call generated. This will be
2919 -- true as soon as aggregates are built in place when possible.
2920
2921 -- ??? at the moment we do not generate warnings for temporaries
2922 -- created for those aggregates although Program_Error might be
2923 -- generated if compiled with -gnato.
996ae0b0
RK
2924
2925 if Is_Controlled (Etype (Id))
2926 and then Comes_From_Source (Id)
2927 then
2928 declare
fbf5a39b
AC
2929 BT : constant Entity_Id := Base_Type (Etype (Id));
2930
996ae0b0 2931 Implicit_Call : Entity_Id;
fbf5a39b 2932 pragma Warnings (Off, Implicit_Call);
9dfd2ff8 2933 -- ??? what is this for (never referenced!)
996ae0b0
RK
2934
2935 function Is_Aggr (N : Node_Id) return Boolean;
2936 -- Check that N is an aggregate
2937
fbf5a39b
AC
2938 -------------
2939 -- Is_Aggr --
2940 -------------
2941
996ae0b0
RK
2942 function Is_Aggr (N : Node_Id) return Boolean is
2943 begin
2944 case Nkind (Original_Node (N)) is
2945 when N_Aggregate | N_Extension_Aggregate =>
2946 return True;
2947
2948 when N_Qualified_Expression |
2949 N_Type_Conversion |
2950 N_Unchecked_Type_Conversion =>
2951 return Is_Aggr (Expression (Original_Node (N)));
2952
2953 when others =>
2954 return False;
2955 end case;
2956 end Is_Aggr;
2957
2958 begin
9dfd2ff8
CC
2959 -- If no underlying type, we already are in an error situation.
2960 -- Do not try to add a warning since we do not have access to
996ae0b0
RK
2961 -- prim-op list.
2962
2963 if No (Underlying_Type (BT)) then
2964 Implicit_Call := Empty;
2965
2966 -- A generic type does not have usable primitive operators.
2967 -- Initialization calls are built for instances.
2968
2969 elsif Is_Generic_Type (BT) then
2970 Implicit_Call := Empty;
2971
9dfd2ff8
CC
2972 -- If the init expression is not an aggregate, an adjust call
2973 -- will be generated
996ae0b0
RK
2974
2975 elsif Present (E) and then not Is_Aggr (E) then
2976 Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2977
9dfd2ff8 2978 -- If no init expression and we are not in the deferred
996ae0b0
RK
2979 -- constant case, an Initialize call will be generated
2980
2981 elsif No (E) and then not Constant_Present (N) then
2982 Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2983
2984 else
2985 Implicit_Call := Empty;
2986 end if;
2987 end;
2988 end if;
2989 end if;
2990
2991 if Has_Task (Etype (Id)) then
6e937c1c 2992 Check_Restriction (No_Tasking, N);
fbf5a39b 2993
ce4a6e84
RD
2994 -- Deal with counting max tasks
2995
2996 -- Nothing to do if inside a generic
2997
2998 if Inside_A_Generic then
2999 null;
3000
3001 -- If library level entity, then count tasks
3002
3003 elsif Is_Library_Level_Entity (Id) then
6e937c1c 3004 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
ce4a6e84
RD
3005
3006 -- If not library level entity, then indicate we don't know max
3007 -- tasks and also check task hierarchy restriction and blocking
3008 -- operation (since starting a task is definitely blocking!)
3009
6e937c1c
AC
3010 else
3011 Check_Restriction (Max_Tasks, N);
996ae0b0
RK
3012 Check_Restriction (No_Task_Hierarchy, N);
3013 Check_Potentially_Blocking_Operation (N);
3014 end if;
07fc65c4
GB
3015
3016 -- A rather specialized test. If we see two tasks being declared
3017 -- of the same type in the same object declaration, and the task
3018 -- has an entry with an address clause, we know that program error
3019 -- will be raised at run-time since we can't have two tasks with
3020 -- entries at the same address.
3021
71d9e9f2 3022 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
07fc65c4
GB
3023 declare
3024 E : Entity_Id;
3025
3026 begin
3027 E := First_Entity (Etype (Id));
3028 while Present (E) loop
3029 if Ekind (E) = E_Entry
3030 and then Present (Get_Attribute_Definition_Clause
3031 (E, Attribute_Address))
3032 then
3033 Error_Msg_N
3034 ("?more than one task with same entry address", N);
3035 Error_Msg_N
3036 ("\?Program_Error will be raised at run time", N);
3037 Insert_Action (N,
3038 Make_Raise_Program_Error (Loc,
3039 Reason => PE_Duplicated_Entry_Address));
3040 exit;
3041 end if;
3042
3043 Next_Entity (E);
3044 end loop;
3045 end;
3046 end if;
996ae0b0
RK
3047 end if;
3048
3049 -- Some simple constant-propagation: if the expression is a constant
3050 -- string initialized with a literal, share the literal. This avoids
3051 -- a run-time copy.
3052
3053 if Present (E)
3054 and then Is_Entity_Name (E)
3055 and then Ekind (Entity (E)) = E_Constant
3056 and then Base_Type (Etype (E)) = Standard_String
3057 then
3058 declare
3059 Val : constant Node_Id := Constant_Value (Entity (E));
996ae0b0
RK
3060 begin
3061 if Present (Val)
3062 and then Nkind (Val) = N_String_Literal
3063 then
3064 Rewrite (E, New_Copy (Val));
3065 end if;
3066 end;
3067 end if;
3068
3069 -- Another optimization: if the nominal subtype is unconstrained and
fbf5a39b
AC
3070 -- the expression is a function call that returns an unconstrained
3071 -- type, rewrite the declaration as a renaming of the result of the
996ae0b0
RK
3072 -- call. The exceptions below are cases where the copy is expected,
3073 -- either by the back end (Aliased case) or by the semantics, as for
3074 -- initializing controlled types or copying tags for classwide types.
3075
3076 if Present (E)
3077 and then Nkind (E) = N_Explicit_Dereference
3078 and then Nkind (Original_Node (E)) = N_Function_Call
3079 and then not Is_Library_Level_Entity (Id)
9dfd2ff8 3080 and then not Is_Constrained (Underlying_Type (T))
996ae0b0
RK
3081 and then not Is_Aliased (Id)
3082 and then not Is_Class_Wide_Type (T)
3083 and then not Is_Controlled (T)
3084 and then not Has_Controlled_Component (Base_Type (T))
3085 and then Expander_Active
3086 then
3087 Rewrite (N,
3088 Make_Object_Renaming_Declaration (Loc,
3089 Defining_Identifier => Id,
6e937c1c 3090 Access_Definition => Empty,
996ae0b0
RK
3091 Subtype_Mark => New_Occurrence_Of
3092 (Base_Type (Etype (Id)), Loc),
3093 Name => E));
3094
3095 Set_Renamed_Object (Id, E);
fbf5a39b 3096
9dfd2ff8
CC
3097 -- Force generation of debugging information for the constant and for
3098 -- the renamed function call.
fbf5a39b 3099
df89ab66
ES
3100 Set_Debug_Info_Needed (Id);
3101 Set_Debug_Info_Needed (Entity (Prefix (E)));
996ae0b0
RK
3102 end if;
3103
3104 if Present (Prev_Entity)
3105 and then Is_Frozen (Prev_Entity)
3106 and then not Error_Posted (Id)
3107 then
3108 Error_Msg_N ("full constant declaration appears too late", N);
3109 end if;
3110
3111 Check_Eliminated (Id);
88b32fc3
BD
3112
3113 -- Deal with setting In_Private_Part flag if in private part
3114
3115 if Ekind (Scope (Id)) = E_Package
3116 and then In_Private_Part (Scope (Id))
3117 then
3118 Set_In_Private_Part (Id);
3119 end if;
5b2217f8
RD
3120
3121 -- Check for violation of No_Local_Timing_Events
3122
3123 if Is_RTE (Etype (Id), RE_Timing_Event)
3124 and then not Is_Library_Level_Entity (Id)
3125 then
3126 Check_Restriction (No_Local_Timing_Events, N);
3127 end if;
996ae0b0
RK
3128 end Analyze_Object_Declaration;
3129
3130 ---------------------------
3131 -- Analyze_Others_Choice --
3132 ---------------------------
3133
3134 -- Nothing to do for the others choice node itself, the semantic analysis
3135 -- of the others choice will occur as part of the processing of the parent
3136
3137 procedure Analyze_Others_Choice (N : Node_Id) is
07fc65c4 3138 pragma Warnings (Off, N);
996ae0b0
RK
3139 begin
3140 null;
3141 end Analyze_Others_Choice;
3142
3143 -------------------------------------------
3144 -- Analyze_Private_Extension_Declaration --
3145 -------------------------------------------
3146
3147 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
fbf5a39b
AC
3148 T : constant Entity_Id := Defining_Identifier (N);
3149 Indic : constant Node_Id := Subtype_Indication (N);
996ae0b0
RK
3150 Parent_Type : Entity_Id;
3151 Parent_Base : Entity_Id;
3152
3153 begin
9dfd2ff8 3154 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
758c442c
GD
3155
3156 if Is_Non_Empty_List (Interface_List (N)) then
3157 declare
9dfd2ff8
CC
3158 Intf : Node_Id;
3159 T : Entity_Id;
3160
758c442c 3161 begin
9dfd2ff8
CC
3162 Intf := First (Interface_List (N));
3163 while Present (Intf) loop
3164 T := Find_Type_Of_Subtype_Indic (Intf);
758c442c 3165
6765b310 3166 Diagnose_Interface (Intf, T);
9dfd2ff8 3167 Next (Intf);
758c442c
GD
3168 end loop;
3169 end;
3170 end if;
3171
996ae0b0
RK
3172 Generate_Definition (T);
3173 Enter_Name (T);
3174
3175 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3176 Parent_Base := Base_Type (Parent_Type);
3177
3178 if Parent_Type = Any_Type
3179 or else Etype (Parent_Type) = Any_Type
3180 then
3181 Set_Ekind (T, Ekind (Parent_Type));
3182 Set_Etype (T, Any_Type);
3183 return;
3184
3185 elsif not Is_Tagged_Type (Parent_Type) then
3186 Error_Msg_N
3187 ("parent of type extension must be a tagged type ", Indic);
3188 return;
3189
3190 elsif Ekind (Parent_Type) = E_Void
3191 or else Ekind (Parent_Type) = E_Incomplete_Type
3192 then
3193 Error_Msg_N ("premature derivation of incomplete type", Indic);
3194 return;
dc06abec
RD
3195
3196 elsif Is_Concurrent_Type (Parent_Type) then
3197 Error_Msg_N
3198 ("parent type of a private extension cannot be "
3199 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3200
3201 Set_Etype (T, Any_Type);
3202 Set_Ekind (T, E_Limited_Private_Type);
3203 Set_Private_Dependents (T, New_Elmt_List);
3204 Set_Error_Posted (T);
3205 return;
996ae0b0
RK
3206 end if;
3207
3208 -- Perhaps the parent type should be changed to the class-wide type's
3209 -- specific type in this case to prevent cascading errors ???
3210
3211 if Is_Class_Wide_Type (Parent_Type) then
3212 Error_Msg_N
3213 ("parent of type extension must not be a class-wide type", Indic);
3214 return;
3215 end if;
3216
950d3e7d 3217 if (not Is_Package_Or_Generic_Package (Current_Scope)
996ae0b0
RK
3218 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3219 or else In_Private_Part (Current_Scope)
3220
3221 then
3222 Error_Msg_N ("invalid context for private extension", N);
3223 end if;
3224
3225 -- Set common attributes
3226
3227 Set_Is_Pure (T, Is_Pure (Current_Scope));
3228 Set_Scope (T, Current_Scope);
3229 Set_Ekind (T, E_Record_Type_With_Private);
3230 Init_Size_Align (T);
3231
3232 Set_Etype (T, Parent_Base);
3233 Set_Has_Task (T, Has_Task (Parent_Base));
3234
3235 Set_Convention (T, Convention (Parent_Type));
3236 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3237 Set_Is_First_Subtype (T);
3238 Make_Class_Wide_Type (T);
3239
e6f69614
AC
3240 if Unknown_Discriminants_Present (N) then
3241 Set_Discriminant_Constraint (T, No_Elist);
3242 end if;
3243
996ae0b0 3244 Build_Derived_Record_Type (N, Parent_Type, T);
653da906 3245
88b32fc3
BD
3246 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3247 -- synchronized formal derived type.
3248
3249 if Ada_Version >= Ada_05
3250 and then Synchronized_Present (N)
3251 then
3252 Set_Is_Limited_Record (T);
3253
3254 -- Formal derived type case
3255
3256 if Is_Generic_Type (T) then
3257
3258 -- The parent must be a tagged limited type or a synchronized
3259 -- interface.
3260
3261 if (not Is_Tagged_Type (Parent_Type)
3262 or else not Is_Limited_Type (Parent_Type))
3263 and then
3264 (not Is_Interface (Parent_Type)
3265 or else not Is_Synchronized_Interface (Parent_Type))
3266 then
3267 Error_Msg_NE ("parent type of & must be tagged limited " &
3268 "or synchronized", N, T);
3269 end if;
3270
3271 -- The progenitors (if any) must be limited or synchronized
3272 -- interfaces.
3273
ce2b6ba5 3274 if Present (Interfaces (T)) then
88b32fc3
BD
3275 declare
3276 Iface : Entity_Id;
3277 Iface_Elmt : Elmt_Id;
3278
3279 begin
ce2b6ba5 3280 Iface_Elmt := First_Elmt (Interfaces (T));
88b32fc3
BD
3281 while Present (Iface_Elmt) loop
3282 Iface := Node (Iface_Elmt);
3283
3284 if not Is_Limited_Interface (Iface)
3285 and then not Is_Synchronized_Interface (Iface)
3286 then
3287 Error_Msg_NE ("progenitor & must be limited " &
3288 "or synchronized", N, Iface);
3289 end if;
3290
3291 Next_Elmt (Iface_Elmt);
3292 end loop;
3293 end;
3294 end if;
3295
3296 -- Regular derived extension, the parent must be a limited or
3297 -- synchronized interface.
3298
3299 else
3300 if not Is_Interface (Parent_Type)
3301 or else (not Is_Limited_Interface (Parent_Type)
3302 and then
3303 not Is_Synchronized_Interface (Parent_Type))
3304 then
3305 Error_Msg_NE
3306 ("parent type of & must be limited interface", N, T);
3307 end if;
3308 end if;
3309
443614e3
AC
3310 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
3311 -- extension with a synchronized parent must be explicitly declared
3312 -- synchronized, because the full view will be a synchronized type.
3313 -- This must be checked before the check for limited types below,
2794f022 3314 -- to ensure that types declared limited are not allowed to extend
443614e3
AC
3315 -- synchronized interfaces.
3316
3317 elsif Is_Interface (Parent_Type)
3318 and then Is_Synchronized_Interface (Parent_Type)
3319 and then not Synchronized_Present (N)
3320 then
3321 Error_Msg_NE
3322 ("private extension of& must be explicitly synchronized",
3323 N, Parent_Type);
3324
88b32fc3 3325 elsif Limited_Present (N) then
653da906
RD
3326 Set_Is_Limited_Record (T);
3327
57193e09
TQ
3328 if not Is_Limited_Type (Parent_Type)
3329 and then
3330 (not Is_Interface (Parent_Type)
88b32fc3 3331 or else not Is_Limited_Interface (Parent_Type))
57193e09 3332 then
653da906
RD
3333 Error_Msg_NE ("parent type& of limited extension must be limited",
3334 N, Parent_Type);
3335 end if;
3336 end if;
996ae0b0
RK
3337 end Analyze_Private_Extension_Declaration;
3338
3339 ---------------------------------
3340 -- Analyze_Subtype_Declaration --
3341 ---------------------------------
3342
88b32fc3
BD
3343 procedure Analyze_Subtype_Declaration
3344 (N : Node_Id;
3345 Skip : Boolean := False)
3346 is
996ae0b0
RK
3347 Id : constant Entity_Id := Defining_Identifier (N);
3348 T : Entity_Id;
3349 R_Checks : Check_Result;
3350
3351 begin
3352 Generate_Definition (Id);
3353 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3354 Init_Size_Align (Id);
3355
9dfd2ff8
CC
3356 -- The following guard condition on Enter_Name is to handle cases where
3357 -- the defining identifier has already been entered into the scope but
3358 -- the declaration as a whole needs to be analyzed.
996ae0b0 3359
a5b62485 3360 -- This case in particular happens for derived enumeration types. The
9dfd2ff8
CC
3361 -- derived enumeration type is processed as an inserted enumeration type
3362 -- declaration followed by a rewritten subtype declaration. The defining
3363 -- identifier, however, is entered into the name scope very early in the
3364 -- processing of the original type declaration and therefore needs to be
3365 -- avoided here, when the created subtype declaration is analyzed. (See
3366 -- Build_Derived_Types)
996ae0b0 3367
a5b62485
AC
3368 -- This also happens when the full view of a private type is derived
3369 -- type with constraints. In this case the entity has been introduced
3370 -- in the private declaration.
996ae0b0 3371
88b32fc3
BD
3372 if Skip
3373 or else (Present (Etype (Id))
3374 and then (Is_Private_Type (Etype (Id))
3375 or else Is_Task_Type (Etype (Id))
3376 or else Is_Rewrite_Substitution (N)))
996ae0b0
RK
3377 then
3378 null;
3379
3380 else
3381 Enter_Name (Id);
3382 end if;
3383
3384 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3385
3386 -- Inherit common attributes
3387
fbf5a39b
AC
3388 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
3389 Set_Is_Volatile (Id, Is_Volatile (T));
3390 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3391 Set_Is_Atomic (Id, Is_Atomic (T));
88b32fc3 3392 Set_Is_Ada_2005_Only (Id, Is_Ada_2005_Only (T));
df89ab66 3393 Set_Convention (Id, Convention (T));
996ae0b0
RK
3394
3395 -- In the case where there is no constraint given in the subtype
9dfd2ff8
CC
3396 -- indication, Process_Subtype just returns the Subtype_Mark, so its
3397 -- semantic attributes must be established here.
996ae0b0
RK
3398
3399 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
3400 Set_Etype (Id, Base_Type (T));
3401
3402 case Ekind (T) is
3403 when Array_Kind =>
adc04486
AC
3404 Set_Ekind (Id, E_Array_Subtype);
3405 Copy_Array_Subtype_Attributes (Id, T);
996ae0b0
RK
3406
3407 when Decimal_Fixed_Point_Kind =>
3408 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
3409 Set_Digits_Value (Id, Digits_Value (T));
3410 Set_Delta_Value (Id, Delta_Value (T));
3411 Set_Scale_Value (Id, Scale_Value (T));
3412 Set_Small_Value (Id, Small_Value (T));
3413 Set_Scalar_Range (Id, Scalar_Range (T));
3414 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
3415 Set_Is_Constrained (Id, Is_Constrained (T));
8dc2ddaf 3416 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
996ae0b0
RK
3417 Set_RM_Size (Id, RM_Size (T));
3418
3419 when Enumeration_Kind =>
3420 Set_Ekind (Id, E_Enumeration_Subtype);
3421 Set_First_Literal (Id, First_Literal (Base_Type (T)));
3422 Set_Scalar_Range (Id, Scalar_Range (T));
3423 Set_Is_Character_Type (Id, Is_Character_Type (T));
3424 Set_Is_Constrained (Id, Is_Constrained (T));
8dc2ddaf 3425 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
996ae0b0
RK
3426 Set_RM_Size (Id, RM_Size (T));
3427
3428 when Ordinary_Fixed_Point_Kind =>
3429 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
3430 Set_Scalar_Range (Id, Scalar_Range (T));
3431 Set_Small_Value (Id, Small_Value (T));
3432 Set_Delta_Value (Id, Delta_Value (T));
3433 Set_Is_Constrained (Id, Is_Constrained (T));
8dc2ddaf 3434 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
996ae0b0
RK
3435 Set_RM_Size (Id, RM_Size (T));
3436
3437 when Float_Kind =>
3438 Set_Ekind (Id, E_Floating_Point_Subtype);
3439 Set_Scalar_Range (Id, Scalar_Range (T));
3440 Set_Digits_Value (Id, Digits_Value (T));
3441 Set_Is_Constrained (Id, Is_Constrained (T));
3442
3443 when Signed_Integer_Kind =>
3444 Set_Ekind (Id, E_Signed_Integer_Subtype);
3445 Set_Scalar_Range (Id, Scalar_Range (T));
3446 Set_Is_Constrained (Id, Is_Constrained (T));
8dc2ddaf 3447 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
996ae0b0
RK
3448 Set_RM_Size (Id, RM_Size (T));
3449
3450 when Modular_Integer_Kind =>
3451 Set_Ekind (Id, E_Modular_Integer_Subtype);
3452 Set_Scalar_Range (Id, Scalar_Range (T));
3453 Set_Is_Constrained (Id, Is_Constrained (T));
8dc2ddaf 3454 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
996ae0b0
RK
3455 Set_RM_Size (Id, RM_Size (T));
3456
3457 when Class_Wide_Kind =>
3458 Set_Ekind (Id, E_Class_Wide_Subtype);
3459 Set_First_Entity (Id, First_Entity (T));
3460 Set_Last_Entity (Id, Last_Entity (T));
3461 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3462 Set_Cloned_Subtype (Id, T);
3463 Set_Is_Tagged_Type (Id, True);
3464 Set_Has_Unknown_Discriminants
3465 (Id, True);
3466
3467 if Ekind (T) = E_Class_Wide_Subtype then
3468 Set_Equivalent_Type (Id, Equivalent_Type (T));
3469 end if;
3470
3471 when E_Record_Type | E_Record_Subtype =>
3472 Set_Ekind (Id, E_Record_Subtype);
3473
3474 if Ekind (T) = E_Record_Subtype
3475 and then Present (Cloned_Subtype (T))
3476 then
3477 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
3478 else
3479 Set_Cloned_Subtype (Id, T);
3480 end if;
3481
3482 Set_First_Entity (Id, First_Entity (T));
3483 Set_Last_Entity (Id, Last_Entity (T));
3484 Set_Has_Discriminants (Id, Has_Discriminants (T));
3485 Set_Is_Constrained (Id, Is_Constrained (T));
3486 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
3487 Set_Has_Unknown_Discriminants
3488 (Id, Has_Unknown_Discriminants (T));
3489
3490 if Has_Discriminants (T) then
3491 Set_Discriminant_Constraint
3492 (Id, Discriminant_Constraint (T));
fbf5a39b 3493 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
996ae0b0
RK
3494
3495 elsif Has_Unknown_Discriminants (Id) then
3496 Set_Discriminant_Constraint (Id, No_Elist);
3497 end if;
3498
3499 if Is_Tagged_Type (T) then
3500 Set_Is_Tagged_Type (Id);
fea9e956 3501 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
996ae0b0
RK
3502 Set_Primitive_Operations
3503 (Id, Primitive_Operations (T));
3504 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
2b73cf68
JM
3505
3506 if Is_Interface (T) then
3507 Set_Is_Interface (Id);
3508 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
3509 end if;
996ae0b0
RK
3510 end if;
3511
3512 when Private_Kind =>
3513 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
3514 Set_Has_Discriminants (Id, Has_Discriminants (T));
3515 Set_Is_Constrained (Id, Is_Constrained (T));
3516 Set_First_Entity (Id, First_Entity (T));
3517 Set_Last_Entity (Id, Last_Entity (T));
3518 Set_Private_Dependents (Id, New_Elmt_List);
3519 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
3520 Set_Has_Unknown_Discriminants
3521 (Id, Has_Unknown_Discriminants (T));
7d7af38a
JM
3522 Set_Known_To_Have_Preelab_Init
3523 (Id, Known_To_Have_Preelab_Init (T));
996ae0b0
RK
3524
3525 if Is_Tagged_Type (T) then
fea9e956
ES
3526 Set_Is_Tagged_Type (Id);
3527 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
3528 Set_Primitive_Operations (Id, Primitive_Operations (T));
3529 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
996ae0b0
RK
3530 end if;
3531
9dfd2ff8
CC
3532 -- In general the attributes of the subtype of a private type
3533 -- are the attributes of the partial view of parent. However,
3534 -- the full view may be a discriminated type, and the subtype
3535 -- must share the discriminant constraint to generate correct
3536 -- calls to initialization procedures.
996ae0b0
RK
3537
3538 if Has_Discriminants (T) then
3539 Set_Discriminant_Constraint
3540 (Id, Discriminant_Constraint (T));
fbf5a39b 3541 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
996ae0b0
RK
3542
3543 elsif Present (Full_View (T))
3544 and then Has_Discriminants (Full_View (T))
3545 then
3546 Set_Discriminant_Constraint
3547 (Id, Discriminant_Constraint (Full_View (T)));
fbf5a39b 3548 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
996ae0b0
RK
3549
3550 -- This would seem semantically correct, but apparently
ce4a6e84
RD
3551 -- confuses the back-end. To be explained and checked with
3552 -- current version ???
996ae0b0
RK
3553
3554 -- Set_Has_Discriminants (Id);
3555 end if;
3556
3557 Prepare_Private_Subtype_Completion (Id, N);
3558
3559 when Access_Kind =>
3560 Set_Ekind (Id, E_Access_Subtype);
3561 Set_Is_Constrained (Id, Is_Constrained (T));
3562 Set_Is_Access_Constant
3563 (Id, Is_Access_Constant (T));
3564 Set_Directly_Designated_Type
3565 (Id, Designated_Type (T));
9dfd2ff8 3566 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
2820d220 3567
996ae0b0
RK
3568 -- A Pure library_item must not contain the declaration of a
3569 -- named access type, except within a subprogram, generic
03f050b6
AC
3570 -- subprogram, task unit, or protected unit, or if it has
3571 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
996ae0b0
RK
3572
3573 if Comes_From_Source (Id)
3574 and then In_Pure_Unit
3575 and then not In_Subprogram_Task_Protected_Unit
03f050b6 3576 and then not No_Pool_Assigned (Id)
996ae0b0
RK
3577 then
3578 Error_Msg_N
3579 ("named access types not allowed in pure unit", N);
3580 end if;
3581
3582 when Concurrent_Kind =>
996ae0b0
RK
3583 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
3584 Set_Corresponding_Record_Type (Id,
3585 Corresponding_Record_Type (T));
3586 Set_First_Entity (Id, First_Entity (T));
3587 Set_First_Private_Entity (Id, First_Private_Entity (T));
3588 Set_Has_Discriminants (Id, Has_Discriminants (T));
3589 Set_Is_Constrained (Id, Is_Constrained (T));
2b73cf68 3590 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
996ae0b0
RK
3591 Set_Last_Entity (Id, Last_Entity (T));
3592
3593 if Has_Discriminants (T) then
3594 Set_Discriminant_Constraint (Id,
3595 Discriminant_Constraint (T));
fbf5a39b 3596 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
996ae0b0
RK
3597 end if;
3598
996ae0b0 3599 when E_Incomplete_Type =>
88b32fc3
BD
3600 if Ada_Version >= Ada_05 then
3601 Set_Ekind (Id, E_Incomplete_Subtype);
3602
3603 -- Ada 2005 (AI-412): Decorate an incomplete subtype
3604 -- of an incomplete type visible through a limited
3605 -- with clause.
3606
3607 if From_With_Type (T)
3608 and then Present (Non_Limited_View (T))
3609 then
3610 Set_From_With_Type (Id);
3611 Set_Non_Limited_View (Id, Non_Limited_View (T));
3612
3613 -- Ada 2005 (AI-412): Add the regular incomplete subtype
3614 -- to the private dependents of the original incomplete
3615 -- type for future transformation.
3616
3617 else
3618 Append_Elmt (Id, Private_Dependents (T));
3619 end if;
3620
3621 -- If the subtype name denotes an incomplete type an error
3622 -- was already reported by Process_Subtype.
3623
3624 else
3625 Set_Etype (Id, Any_Type);
3626 end if;
996ae0b0
RK
3627
3628 when others =>
3629 raise Program_Error;
3630 end case;
3631 end if;
3632
3633 if Etype (Id) = Any_Type then
3634 return;
3635 end if;
3636
3637 -- Some common processing on all types
3638
3639 Set_Size_Info (Id, T);
3640 Set_First_Rep_Item (Id, First_Rep_Item (T));
3641
3642 T := Etype (Id);
3643
9c510803
ES
3644 Set_Is_Immediately_Visible (Id, True);
3645 Set_Depends_On_Private (Id, Has_Private_Component (T));
3646 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
996ae0b0 3647
2b73cf68
JM
3648 if Is_Interface (T) then
3649 Set_Is_Interface (Id);
3650 end if;
3651
996ae0b0
RK
3652 if Present (Generic_Parent_Type (N))
3653 and then
3654 (Nkind
3655 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
3656 or else Nkind
3657 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
3658 /= N_Formal_Private_Type_Definition)
3659 then
3660 if Is_Tagged_Type (Id) then
2b73cf68
JM
3661
3662 -- If this is a generic actual subtype for a synchronized type,
3663 -- the primitive operations are those of the corresponding record
3664 -- for which there is a separate subtype declaration.
3665
3666 if Is_Concurrent_Type (Id) then
3667 null;
3668 elsif Is_Class_Wide_Type (Id) then
996ae0b0
RK
3669 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
3670 else
3671 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
3672 end if;
3673
3674 elsif Scope (Etype (Id)) /= Standard_Standard then
3675 Derive_Subprograms (Generic_Parent_Type (N), Id);
3676 end if;
3677 end if;
3678
3679 if Is_Private_Type (T)
3680 and then Present (Full_View (T))
3681 then
3682 Conditional_Delay (Id, Full_View (T));
3683
3684 -- The subtypes of components or subcomponents of protected types
3685 -- do not need freeze nodes, which would otherwise appear in the
3686 -- wrong scope (before the freeze node for the protected type). The
3687 -- proper subtypes are those of the subcomponents of the corresponding
3688 -- record.
3689
3690 elsif Ekind (Scope (Id)) /= E_Protected_Type
3691 and then Present (Scope (Scope (Id))) -- error defense!
3692 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
3693 then
3694 Conditional_Delay (Id, T);
3695 end if;
3696
3697 -- Check that constraint_error is raised for a scalar subtype
3698 -- indication when the lower or upper bound of a non-null range
3699 -- lies outside the range of the type mark.
3700
3701 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
3702 if Is_Scalar_Type (Etype (Id))
3703 and then Scalar_Range (Id) /=
3704 Scalar_Range (Etype (Subtype_Mark
3705 (Subtype_Indication (N))))
3706 then
3707 Apply_Range_Check
3708 (Scalar_Range (Id),
3709 Etype (Subtype_Mark (Subtype_Indication (N))));
3710
3711 elsif Is_Array_Type (Etype (Id))
3712 and then Present (First_Index (Id))
3713 then
3714 -- This really should be a subprogram that finds the indications
3715 -- to check???
3716
3717 if ((Nkind (First_Index (Id)) = N_Identifier
3718 and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
3719 or else Nkind (First_Index (Id)) = N_Subtype_Indication)
3720 and then
3721 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
3722 then
3723 declare
fbf5a39b
AC
3724 Target_Typ : constant Entity_Id :=
3725 Etype
3726 (First_Index (Etype
3727 (Subtype_Mark (Subtype_Indication (N)))));
996ae0b0
RK
3728 begin
3729 R_Checks :=
dc06abec 3730 Get_Range_Checks
996ae0b0
RK
3731 (Scalar_Range (Etype (First_Index (Id))),
3732 Target_Typ,
3733 Etype (First_Index (Id)),
3734 Defining_Identifier (N));
3735
3736 Insert_Range_Checks
3737 (R_Checks,
3738 N,
3739 Target_Typ,
3740 Sloc (Defining_Identifier (N)));
3741 end;
3742 end if;
3743 end if;
3744 end if;
3745
ce4a6e84 3746 Set_Optimize_Alignment_Flags (Id);
996ae0b0
RK
3747 Check_Eliminated (Id);
3748 end Analyze_Subtype_Declaration;
3749
3750 --------------------------------
3751 -- Analyze_Subtype_Indication --
3752 --------------------------------
3753
3754 procedure Analyze_Subtype_Indication (N : Node_Id) is
3755 T : constant Entity_Id := Subtype_Mark (N);
3756 R : constant Node_Id := Range_Expression (Constraint (N));
3757
3758 begin
3759 Analyze (T);
ce9e9122
RD
3760
3761 if R /= Error then
3762 Analyze (R);
3763 Set_Etype (N, Etype (R));
fea9e956 3764 Resolve (R, Entity (T));
ce9e9122
RD
3765 else
3766 Set_Error_Posted (R);
3767 Set_Error_Posted (T);
3768 end if;
996ae0b0
RK
3769 end Analyze_Subtype_Indication;
3770
3771 ------------------------------
3772 -- Analyze_Type_Declaration --
3773 ------------------------------
3774
3775 procedure Analyze_Type_Declaration (N : Node_Id) is
3776 Def : constant Node_Id := Type_Definition (N);
3777 Def_Id : constant Entity_Id := Defining_Identifier (N);
3778 T : Entity_Id;
3779 Prev : Entity_Id;
3780
fbf5a39b
AC
3781 Is_Remote : constant Boolean :=
3782 (Is_Remote_Types (Current_Scope)
fea9e956
ES
3783 or else Is_Remote_Call_Interface (Current_Scope))
3784 and then not (In_Private_Part (Current_Scope)
3785 or else In_Package_Body (Current_Scope));
fbf5a39b 3786
950d3e7d
ES
3787 procedure Check_Ops_From_Incomplete_Type;
3788 -- If there is a tagged incomplete partial view of the type, transfer
3789 -- its operations to the full view, and indicate that the type of the
3790 -- controlling parameter (s) is this full view.
3791
3792 ------------------------------------
3793 -- Check_Ops_From_Incomplete_Type --
3794 ------------------------------------
3795
3796 procedure Check_Ops_From_Incomplete_Type is
3797 Elmt : Elmt_Id;
3798 Formal : Entity_Id;
3799 Op : Entity_Id;
3800
3801 begin
3802 if Prev /= T
3803 and then Ekind (Prev) = E_Incomplete_Type
3804 and then Is_Tagged_Type (Prev)
3805 and then Is_Tagged_Type (T)
3806 then
3807 Elmt := First_Elmt (Primitive_Operations (Prev));
3808 while Present (Elmt) loop
3809 Op := Node (Elmt);
3810 Prepend_Elmt (Op, Primitive_Operations (T));
3811
3812 Formal := First_Formal (Op);
3813 while Present (Formal) loop
3814 if Etype (Formal) = Prev then
3815 Set_Etype (Formal, T);
3816 end if;
3817
3818 Next_Formal (Formal);
3819 end loop;
3820
3821 if Etype (Op) = Prev then
3822 Set_Etype (Op, T);
3823 end if;
3824
3825 Next_Elmt (Elmt);
3826 end loop;
3827 end if;
3828 end Check_Ops_From_Incomplete_Type;
3829
3830 -- Start of processing for Analyze_Type_Declaration
3831
996ae0b0
RK
3832 begin
3833 Prev := Find_Type_Name (N);
3834
19f0526a
AC
3835 -- The full view, if present, now points to the current type
3836
0ab80019
AC
3837 -- Ada 2005 (AI-50217): If the type was previously decorated when
3838 -- imported through a LIMITED WITH clause, it appears as incomplete
3839 -- but has no full view.
fea9e956
ES
3840 -- If the incomplete view is tagged, a class_wide type has been
3841 -- created already. Use it for the full view as well, to prevent
3842 -- multiple incompatible class-wide types that may be created for
3843 -- self-referential anonymous access components.
fbf5a39b
AC
3844
3845 if Ekind (Prev) = E_Incomplete_Type
3846 and then Present (Full_View (Prev))
3847 then
996ae0b0 3848 T := Full_View (Prev);
fea9e956
ES
3849
3850 if Is_Tagged_Type (Prev)
3851 and then Present (Class_Wide_Type (Prev))
3852 then
3853 Set_Ekind (T, Ekind (Prev)); -- will be reset later
3854 Set_Class_Wide_Type (T, Class_Wide_Type (Prev));
3855 Set_Etype (Class_Wide_Type (T), T);
3856 end if;
3857
996ae0b0
RK
3858 else
3859 T := Prev;
3860 end if;
3861
3862 Set_Is_Pure (T, Is_Pure (Current_Scope));
3863
3864 -- We set the flag Is_First_Subtype here. It is needed to set the
3865 -- corresponding flag for the Implicit class-wide-type created
3866 -- during tagged types processing.
3867
3868 Set_Is_First_Subtype (T, True);
3869
3870 -- Only composite types other than array types are allowed to have
3871 -- discriminants.
3872
3873 case Nkind (Def) is
3874
3875 -- For derived types, the rule will be checked once we've figured
3876 -- out the parent type.
3877
3878 when N_Derived_Type_Definition =>
3879 null;
3880
ffe9aba8 3881 -- For record types, discriminants are allowed
996ae0b0
RK
3882
3883 when N_Record_Definition =>
3884 null;
3885
3886 when others =>
3887 if Present (Discriminant_Specifications (N)) then
3888 Error_Msg_N
3889 ("elementary or array type cannot have discriminants",
3890 Defining_Identifier
3891 (First (Discriminant_Specifications (N))));
3892 end if;
3893 end case;
3894
3895 -- Elaborate the type definition according to kind, and generate
88b32fc3
BD
3896 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3897 -- already done (this happens during the reanalysis that follows a call
3898 -- to the high level optimizer).
996ae0b0
RK
3899
3900 if not Analyzed (T) then
3901 Set_Analyzed (T);
3902
3903 case Nkind (Def) is
3904
3905 when N_Access_To_Subprogram_Definition =>
3906 Access_Subprogram_Declaration (T, Def);
3907
88b32fc3
BD
3908 -- If this is a remote access to subprogram, we must create the
3909 -- equivalent fat pointer type, and related subprograms.
996ae0b0 3910
fbf5a39b 3911 if Is_Remote then
996ae0b0
RK
3912 Process_Remote_AST_Declaration (N);
3913 end if;
3914
3915 -- Validate categorization rule against access type declaration
3916 -- usually a violation in Pure unit, Shared_Passive unit.
3917
3918 Validate_Access_Type_Declaration (T, N);
3919
3920 when N_Access_To_Object_Definition =>
3921 Access_Type_Declaration (T, Def);
3922
3923 -- Validate categorization rule against access type declaration
3924 -- usually a violation in Pure unit, Shared_Passive unit.
3925
3926 Validate_Access_Type_Declaration (T, N);
3927
33931112
JM
3928 -- If we are in a Remote_Call_Interface package and define a
3929 -- RACW, then calling stubs and specific stream attributes
3930 -- must be added.
996ae0b0 3931
fbf5a39b 3932 if Is_Remote
996ae0b0
RK
3933 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3934 then
3935 Add_RACW_Features (Def_Id);
3936 end if;
3937
8a6a52dc
AC
3938 -- Set no strict aliasing flag if config pragma seen
3939
3940 if Opt.No_Strict_Aliasing then
3941 Set_No_Strict_Aliasing (Base_Type (Def_Id));
3942 end if;
3943
996ae0b0
RK
3944 when N_Array_Type_Definition =>
3945 Array_Type_Declaration (T, Def);
3946
3947 when N_Derived_Type_Definition =>
3948 Derived_Type_Declaration (T, N, T /= Def_Id);
3949
3950 when N_Enumeration_Type_Definition =>
3951 Enumeration_Type_Declaration (T, Def);
3952
3953 when N_Floating_Point_Definition =>
3954 Floating_Point_Type_Declaration (T, Def);
3955
3956 when N_Decimal_Fixed_Point_Definition =>
3957 Decimal_Fixed_Point_Type_Declaration (T, Def);
3958
3959 when N_Ordinary_Fixed_Point_Definition =>
3960 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3961
3962 when N_Signed_Integer_Type_Definition =>
3963 Signed_Integer_Type_Declaration (T, Def);
3964
3965 when N_Modular_Type_Definition =>
3966 Modular_Type_Declaration (T, Def);
3967
3968 when N_Record_Definition =>
fbf5a39b 3969 Record_Type_Declaration (T, N, Prev);
996ae0b0
RK
3970
3971 when others =>
3972 raise Program_Error;
3973
3974 end case;
3975 end if;
3976
3977 if Etype (T) = Any_Type then
3978 return;
3979 end if;
3980
3981 -- Some common processing for all types
3982
3983 Set_Depends_On_Private (T, Has_Private_Component (T));
950d3e7d 3984 Check_Ops_From_Incomplete_Type;
996ae0b0
RK
3985
3986 -- Both the declared entity, and its anonymous base type if one
3987 -- was created, need freeze nodes allocated.
3988
3989 declare
3990 B : constant Entity_Id := Base_Type (T);
3991
3992 begin
33931112
JM
3993 -- In the case where the base type differs from the first subtype, we
3994 -- pre-allocate a freeze node, and set the proper link to the first
3995 -- subtype. Freeze_Entity will use this preallocated freeze node when
3996 -- it freezes the entity.
03b64787 3997
054275e4
ES
3998 -- This does not apply if the base type is a generic type, whose
3999 -- declaration is independent of the current derived definition.
996ae0b0 4000
8737a29a 4001 if B /= T and then not Is_Generic_Type (B) then
996ae0b0
RK
4002 Ensure_Freeze_Node (B);
4003 Set_First_Subtype_Link (Freeze_Node (B), T);
4004 end if;
4005
28450a75 4006 -- A type that is imported through a limited_with clause cannot
8737a29a
AC
4007 -- generate any code, and thus need not be frozen. However, an access
4008 -- type with an imported designated type needs a finalization list,
4009 -- which may be referenced in some other package that has non-limited
4010 -- visibility on the designated type. Thus we must create the
4011 -- finalization list at the point the access type is frozen, to
4012 -- prevent unsatisfied references at link time.
4013
4014 if not From_With_Type (T) or else Is_Access_Type (T) then
996ae0b0
RK
4015 Set_Has_Delayed_Freeze (T);
4016 end if;
4017 end;
4018
8737a29a 4019 -- Case where T is the full declaration of some private type which has
996ae0b0
RK
4020 -- been swapped in Defining_Identifier (N).
4021
4022 if T /= Def_Id and then Is_Private_Type (Def_Id) then
4023 Process_Full_View (N, T, Def_Id);
4024
33931112
JM
4025 -- Record the reference. The form of this is a little strange, since
4026 -- the full declaration has been swapped in. So the first parameter
4027 -- here represents the entity to which a reference is made which is
4028 -- the "real" entity, i.e. the one swapped in, and the second
4029 -- parameter provides the reference location.
996ae0b0 4030
fea9e956
ES
4031 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
4032 -- since we don't want a complaint about the full type being an
4033 -- unwanted reference to the private type
4034
4035 declare
4036 B : constant Boolean := Has_Pragma_Unreferenced (T);
4037 begin
4038 Set_Has_Pragma_Unreferenced (T, False);
4039 Generate_Reference (T, T, 'c');
4040 Set_Has_Pragma_Unreferenced (T, B);
4041 end;
4042
07fc65c4 4043 Set_Completion_Referenced (Def_Id);
996ae0b0
RK
4044
4045 -- For completion of incomplete type, process incomplete dependents
4046 -- and always mark the full type as referenced (it is the incomplete
4047 -- type that we get for any real reference).
4048
4049 elsif Ekind (Prev) = E_Incomplete_Type then
4050 Process_Incomplete_Dependents (N, T, Prev);
4051 Generate_Reference (Prev, Def_Id, 'c');
07fc65c4 4052 Set_Completion_Referenced (Def_Id);
996ae0b0
RK
4053
4054 -- If not private type or incomplete type completion, this is a real
4055 -- definition of a new entity, so record it.
4056
4057 else
4058 Generate_Definition (Def_Id);
4059 end if;
4060
8737a29a 4061 if Chars (Scope (Def_Id)) = Name_System
9c510803
ES
4062 and then Chars (Def_Id) = Name_Address
4063 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
4064 then
4065 Set_Is_Descendent_Of_Address (Def_Id);
4066 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
4067 Set_Is_Descendent_Of_Address (Prev);
4068 end if;
4069
ce4a6e84 4070 Set_Optimize_Alignment_Flags (Def_Id);
996ae0b0
RK
4071 Check_Eliminated (Def_Id);
4072 end Analyze_Type_Declaration;
4073
4074 --------------------------
4075 -- Analyze_Variant_Part --
4076 --------------------------
4077
4078 procedure Analyze_Variant_Part (N : Node_Id) is
4079
4080 procedure Non_Static_Choice_Error (Choice : Node_Id);
33931112
JM
4081 -- Error routine invoked by the generic instantiation below when the
4082 -- variant part has a non static choice.
996ae0b0
RK
4083
4084 procedure Process_Declarations (Variant : Node_Id);
33931112
JM
4085 -- Analyzes all the declarations associated with a Variant. Needed by
4086 -- the generic instantiation below.
996ae0b0
RK
4087
4088 package Variant_Choices_Processing is new
4089 Generic_Choices_Processing
4090 (Get_Alternatives => Variants,
4091 Get_Choices => Discrete_Choices,
4092 Process_Empty_Choice => No_OP,
4093 Process_Non_Static_Choice => Non_Static_Choice_Error,
4094 Process_Associated_Node => Process_Declarations);
4095 use Variant_Choices_Processing;
ffe9aba8 4096 -- Instantiation of the generic choice processing package
996ae0b0
RK
4097
4098 -----------------------------
4099 -- Non_Static_Choice_Error --
4100 -----------------------------
4101
4102 procedure Non_Static_Choice_Error (Choice : Node_Id) is
4103 begin
fbf5a39b
AC
4104 Flag_Non_Static_Expr
4105 ("choice given in variant part is not static!", Choice);
996ae0b0
RK
4106 end Non_Static_Choice_Error;
4107
4108 --------------------------
4109 -- Process_Declarations --
4110 --------------------------
4111
4112 procedure Process_Declarations (Variant : Node_Id) is
4113 begin
4114 if not Null_Present (Component_List (Variant)) then
4115 Analyze_Declarations (Component_Items (Component_List (Variant)));
4116
4117 if Present (Variant_Part (Component_List (Variant))) then
4118 Analyze (Variant_Part (Component_List (Variant)));
4119 end if;
4120 end if;
4121 end Process_Declarations;
4122
ce4a6e84 4123 -- Local Variables
996ae0b0 4124
996ae0b0
RK
4125 Discr_Name : Node_Id;
4126 Discr_Type : Entity_Id;
4127
4128 Case_Table : Choice_Table_Type (1 .. Number_Of_Choices (N));
4129 Last_Choice : Nat;
4130 Dont_Care : Boolean;
4131 Others_Present : Boolean := False;
4132
0501956d
GD
4133 pragma Warnings (Off, Case_Table);
4134 pragma Warnings (Off, Last_Choice);
4135 pragma Warnings (Off, Dont_Care);
4136 pragma Warnings (Off, Others_Present);
4137 -- We don't care about the assigned values of any of these
4138
996ae0b0
RK
4139 -- Start of processing for Analyze_Variant_Part
4140
4141 begin
4142 Discr_Name := Name (N);
4143 Analyze (Discr_Name);
4144
ce4a6e84 4145 -- If Discr_Name bad, get out (prevent cascaded errors)
2b73cf68 4146
ce4a6e84 4147 if Etype (Discr_Name) = Any_Type then
2b73cf68 4148 return;
ce4a6e84 4149 end if;
2b73cf68 4150
ce4a6e84
RD
4151 -- Check invalid discriminant in variant part
4152
4153 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
996ae0b0
RK
4154 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4155 end if;
4156
4157 Discr_Type := Etype (Entity (Discr_Name));
4158
855ff2e1
GB
4159 if not Is_Discrete_Type (Discr_Type) then
4160 Error_Msg_N
4161 ("discriminant in a variant part must be of a discrete type",
4162 Name (N));
4163 return;
4164 end if;
4165
996ae0b0
RK
4166 -- Call the instantiated Analyze_Choices which does the rest of the work
4167
4168 Analyze_Choices
4169 (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
996ae0b0
RK
4170 end Analyze_Variant_Part;
4171
4172 ----------------------------
4173 -- Array_Type_Declaration --
4174 ----------------------------
4175
4176 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
a397db96 4177 Component_Def : constant Node_Id := Component_Definition (Def);
996ae0b0
RK
4178 Element_Type : Entity_Id;
4179 Implicit_Base : Entity_Id;
4180 Index : Node_Id;
4181 Related_Id : Entity_Id := Empty;
4182 Nb_Index : Nat;
4183 P : constant Node_Id := Parent (Def);
4184 Priv : Entity_Id;
4185
4186 begin
4187 if Nkind (Def) = N_Constrained_Array_Definition then
996ae0b0 4188 Index := First (Discrete_Subtype_Definitions (Def));
6e937c1c
AC
4189 else
4190 Index := First (Subtype_Marks (Def));
4191 end if;
996ae0b0 4192
33931112
JM
4193 -- Find proper names for the implicit types which may be public. In case
4194 -- of anonymous arrays we use the name of the first object of that type
4195 -- as prefix.
996ae0b0 4196
6e937c1c
AC
4197 if No (T) then
4198 Related_Id := Defining_Identifier (P);
996ae0b0 4199 else
6e937c1c 4200 Related_Id := T;
996ae0b0
RK
4201 end if;
4202
4203 Nb_Index := 1;
996ae0b0
RK
4204 while Present (Index) loop
4205 Analyze (Index);
88b32fc3
BD
4206
4207 -- Add a subtype declaration for each index of private array type
4208 -- declaration whose etype is also private. For example:
4209
4210 -- package Pkg is
4211 -- type Index is private;
4212 -- private
4213 -- type Table is array (Index) of ...
4214 -- end;
4215
33931112
JM
4216 -- This is currently required by the expander for the internally
4217 -- generated equality subprogram of records with variant parts in
4218 -- which the etype of some component is such private type.
88b32fc3
BD
4219
4220 if Ekind (Current_Scope) = E_Package
4221 and then In_Private_Part (Current_Scope)
4222 and then Has_Private_Declaration (Etype (Index))
4223 then
4224 declare
4225 Loc : constant Source_Ptr := Sloc (Def);
4226 New_E : Entity_Id;
4227 Decl : Entity_Id;
4228
4229 begin
4230 New_E :=
4231 Make_Defining_Identifier (Loc,
4232 Chars => New_Internal_Name ('T'));
4233 Set_Is_Internal (New_E);
4234
4235 Decl :=
4236 Make_Subtype_Declaration (Loc,
4237 Defining_Identifier => New_E,
4238 Subtype_Indication =>
4239 New_Occurrence_Of (Etype (Index), Loc));
4240
4241 Insert_Before (Parent (Def), Decl);
4242 Analyze (Decl);
4243 Set_Etype (Index, New_E);
4244
4245 -- If the index is a range the Entity attribute is not
4246 -- available. Example:
4247
4248 -- package Pkg is
4249 -- type T is private;
4250 -- private
4251 -- type T is new Natural;
4252 -- Table : array (T(1) .. T(10)) of Boolean;
4253 -- end Pkg;
4254
4255 if Nkind (Index) /= N_Range then
4256 Set_Entity (Index, New_E);
4257 end if;
4258 end;
4259 end if;
4260
996ae0b0
RK
4261 Make_Index (Index, P, Related_Id, Nb_Index);
4262 Next_Index (Index);
4263 Nb_Index := Nb_Index + 1;
4264 end loop;
4265
88b32fc3
BD
4266 -- Process subtype indication if one is present
4267
6e937c1c 4268 if Present (Subtype_Indication (Component_Def)) then
88b32fc3
BD
4269 Element_Type :=
4270 Process_Subtype
4271 (Subtype_Indication (Component_Def), P, Related_Id, 'C');
6e937c1c 4272
0ab80019 4273 -- Ada 2005 (AI-230): Access Definition case
6e937c1c 4274
9bc856dd 4275 else pragma Assert (Present (Access_Definition (Component_Def)));
fea9e956
ES
4276
4277 -- Indicate that the anonymous access type is created by the
4278 -- array type declaration.
4279
6e937c1c 4280 Element_Type := Access_Definition
fea9e956 4281 (Related_Nod => P,
6e937c1c 4282 N => Access_Definition (Component_Def));
758c442c 4283 Set_Is_Local_Anonymous_Access (Element_Type);
6e937c1c 4284
fea9e956
ES
4285 -- Propagate the parent. This field is needed if we have to generate
4286 -- the master_id associated with an anonymous access to task type
4287 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4288
4289 Set_Parent (Element_Type, Parent (T));
4290
33931112
JM
4291 -- Ada 2005 (AI-230): In case of components that are anonymous access
4292 -- types the level of accessibility depends on the enclosing type
4293 -- declaration
35b7fa6a 4294
0ab80019 4295 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
35b7fa6a 4296
0ab80019 4297 -- Ada 2005 (AI-254)
7324bf49 4298
af4b9434
AC
4299 declare
4300 CD : constant Node_Id :=
4301 Access_To_Subprogram_Definition
4302 (Access_Definition (Component_Def));
4303 begin
4304 if Present (CD) and then Protected_Present (CD) then
4305 Element_Type :=
fea9e956 4306 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
af4b9434
AC
4307 end if;
4308 end;
6e937c1c 4309 end if;
996ae0b0
RK
4310
4311 -- Constrained array case
4312
4313 if No (T) then
4314 T := Create_Itype (E_Void, P, Related_Id, 'T');
4315 end if;
4316
4317 if Nkind (Def) = N_Constrained_Array_Definition then
4318
4319 -- Establish Implicit_Base as unconstrained base type
4320
4321 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4322
996ae0b0
RK
4323 Set_Etype (Implicit_Base, Implicit_Base);
4324 Set_Scope (Implicit_Base, Current_Scope);
4325 Set_Has_Delayed_Freeze (Implicit_Base);
4326
4327 -- The constrained array type is a subtype of the unconstrained one
4328
4329 Set_Ekind (T, E_Array_Subtype);
4330 Init_Size_Align (T);
4331 Set_Etype (T, Implicit_Base);
4332 Set_Scope (T, Current_Scope);
4333 Set_Is_Constrained (T, True);
4334 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4335 Set_Has_Delayed_Freeze (T);
4336
4337 -- Complete setup of implicit base type
4338
fea9e956
ES
4339 Set_First_Index (Implicit_Base, First_Index (T));
4340 Set_Component_Type (Implicit_Base, Element_Type);
4341 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4342 Set_Component_Size (Implicit_Base, Uint_0);
4343 Set_Packed_Array_Type (Implicit_Base, Empty);
07fc65c4 4344 Set_Has_Controlled_Component
fea9e956
ES
4345 (Implicit_Base, Has_Controlled_Component
4346 (Element_Type)
4347 or else Is_Controlled
4348 (Element_Type));
07fc65c4 4349 Set_Finalize_Storage_Only
fea9e956
ES
4350 (Implicit_Base, Finalize_Storage_Only
4351 (Element_Type));
996ae0b0
RK
4352
4353 -- Unconstrained array case
4354
4355 else
4356 Set_Ekind (T, E_Array_Type);
4357 Init_Size_Align (T);
4358 Set_Etype (T, T);
4359 Set_Scope (T, Current_Scope);
4360 Set_Component_Size (T, Uint_0);
4361 Set_Is_Constrained (T, False);
4362 Set_First_Index (T, First (Subtype_Marks (Def)));
4363 Set_Has_Delayed_Freeze (T, True);
07fc65c4
GB
4364 Set_Has_Task (T, Has_Task (Element_Type));
4365 Set_Has_Controlled_Component (T, Has_Controlled_Component
4366 (Element_Type)
4367 or else
4368 Is_Controlled (Element_Type));
4369 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4370 (Element_Type));
996ae0b0
RK
4371 end if;
4372
fea9e956
ES
4373 -- Common attributes for both cases
4374
07fc65c4 4375 Set_Component_Type (Base_Type (T), Element_Type);
fea9e956 4376 Set_Packed_Array_Type (T, Empty);
996ae0b0 4377
a397db96 4378 if Aliased_Present (Component_Definition (Def)) then
996ae0b0
RK
4379 Set_Has_Aliased_Components (Etype (T));
4380 end if;
4381
0ab80019 4382 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
9dfd2ff8 4383 -- array type to ensure that objects of this type are initialized.
2820d220 4384
0ab80019 4385 if Ada_Version >= Ada_05
9dfd2ff8 4386 and then Can_Never_Be_Null (Element_Type)
2820d220
AC
4387 then
4388 Set_Can_Never_Be_Null (T);
4389
4390 if Null_Exclusion_Present (Component_Definition (Def))
9dfd2ff8 4391
33931112
JM
4392 -- No need to check itypes because in their case this check was
4393 -- done at their point of creation
9dfd2ff8
CC
4394
4395 and then not Is_Itype (Element_Type)
2820d220
AC
4396 then
4397 Error_Msg_N
2b73cf68 4398 ("`NOT NULL` not allowed (null already excluded)",
2820d220
AC
4399 Subtype_Indication (Component_Definition (Def)));
4400 end if;
4401 end if;
4402
996ae0b0
RK
4403 Priv := Private_Component (Element_Type);
4404
4405 if Present (Priv) then
07fc65c4
GB
4406
4407 -- Check for circular definitions
996ae0b0
RK
4408
4409 if Priv = Any_Type then
996ae0b0
RK
4410 Set_Component_Type (Etype (T), Any_Type);
4411
fbf5a39b 4412 -- There is a gap in the visibility of operations on the composite
996ae0b0
RK
4413 -- type only if the component type is defined in a different scope.
4414
4415 elsif Scope (Priv) = Current_Scope then
4416 null;
4417
4418 elsif Is_Limited_Type (Priv) then
4419 Set_Is_Limited_Composite (Etype (T));
4420 Set_Is_Limited_Composite (T);
4421 else
4422 Set_Is_Private_Composite (Etype (T));
4423 Set_Is_Private_Composite (T);
4424 end if;
4425 end if;
4426
33931112
JM
4427 -- A syntax error in the declaration itself may lead to an empty index
4428 -- list, in which case do a minimal patch.
2b73cf68
JM
4429
4430 if No (First_Index (T)) then
4431 Error_Msg_N ("missing index definition in array type declaration", T);
4432
4433 declare
4434 Indices : constant List_Id :=
dc06abec 4435 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
2b73cf68
JM
4436 begin
4437 Set_Discrete_Subtype_Definitions (Def, Indices);
4438 Set_First_Index (T, First (Indices));
4439 return;
4440 end;
4441 end if;
4442
c6fe3827
GD
4443 -- Create a concatenation operator for the new type. Internal array
4444 -- types created for packed entities do not need such, they are
4445 -- compatible with the user-defined type.
996ae0b0
RK
4446
4447 if Number_Dimensions (T) = 1
4448 and then not Is_Packed_Array_Type (T)
4449 then
6c1e24d3 4450 New_Concatenation_Op (T);
996ae0b0
RK
4451 end if;
4452
c6fe3827
GD
4453 -- In the case of an unconstrained array the parser has already verified
4454 -- that all the indices are unconstrained but we still need to make sure
4455 -- that the element type is constrained.
996ae0b0
RK
4456
4457 if Is_Indefinite_Subtype (Element_Type) then
4458 Error_Msg_N
a397db96
AC
4459 ("unconstrained element type in array declaration",
4460 Subtype_Indication (Component_Def));
996ae0b0 4461
fea9e956 4462 elsif Is_Abstract_Type (Element_Type) then
a397db96 4463 Error_Msg_N
758c442c 4464 ("the type of a component cannot be abstract",
a397db96 4465 Subtype_Indication (Component_Def));
996ae0b0 4466 end if;
996ae0b0
RK
4467 end Array_Type_Declaration;
4468
7324bf49
AC
4469 ------------------------------------------------------
4470 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4471 ------------------------------------------------------
4472
4473 function Replace_Anonymous_Access_To_Protected_Subprogram
c6fe3827 4474 (N : Node_Id) return Entity_Id
7324bf49
AC
4475 is
4476 Loc : constant Source_Ptr := Sloc (N);
4477
4478 Curr_Scope : constant Scope_Stack_Entry :=
4479 Scope_Stack.Table (Scope_Stack.Last);
4480
4481 Anon : constant Entity_Id :=
4482 Make_Defining_Identifier (Loc,
4483 Chars => New_Internal_Name ('S'));
4484
4485 Acc : Node_Id;
4486 Comp : Node_Id;
4487 Decl : Node_Id;
9dfd2ff8 4488 P : Node_Id;
7324bf49
AC
4489
4490 begin
4491 Set_Is_Internal (Anon);
4492
4493 case Nkind (N) is
4494 when N_Component_Declaration |
4495 N_Unconstrained_Array_Definition |
4496 N_Constrained_Array_Definition =>
4497 Comp := Component_Definition (N);
fea9e956 4498 Acc := Access_Definition (Comp);
7324bf49
AC
4499
4500 when N_Discriminant_Specification =>
4501 Comp := Discriminant_Type (N);
fea9e956 4502 Acc := Comp;
7324bf49
AC
4503
4504 when N_Parameter_Specification =>
4505 Comp := Parameter_Type (N);
fea9e956
ES
4506 Acc := Comp;
4507
2b73cf68
JM
4508 when N_Access_Function_Definition =>
4509 Comp := Result_Definition (N);
4510 Acc := Comp;
4511
fea9e956
ES
4512 when N_Object_Declaration =>
4513 Comp := Object_Definition (N);
4514 Acc := Comp;
7324bf49 4515
b1c11e0e
JM
4516 when N_Function_Specification =>
4517 Comp := Result_Definition (N);
4518 Acc := Comp;
4519
7324bf49 4520 when others =>
9bc856dd 4521 raise Program_Error;
7324bf49
AC
4522 end case;
4523
4524 Decl := Make_Full_Type_Declaration (Loc,
4525 Defining_Identifier => Anon,
4526 Type_Definition =>
af4b9434 4527 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
7324bf49
AC
4528
4529 Mark_Rewrite_Insertion (Decl);
4530
cd1c668b
ES
4531 -- Insert the new declaration in the nearest enclosing scope. If the
4532 -- node is a body and N is its return type, the declaration belongs in
4533 -- the enclosing scope.
7324bf49 4534
9dfd2ff8 4535 P := Parent (N);
92298782 4536
cd1c668b
ES
4537 if Nkind (P) = N_Subprogram_Body
4538 and then Nkind (N) = N_Function_Specification
4539 then
4540 P := Parent (P);
4541 end if;
4542
af4b9434 4543 while Present (P) and then not Has_Declarations (P) loop
7324bf49
AC
4544 P := Parent (P);
4545 end loop;
4546
af4b9434
AC
4547 pragma Assert (Present (P));
4548
4549 if Nkind (P) = N_Package_Specification then
4550 Prepend (Decl, Visible_Declarations (P));
4551 else
4552 Prepend (Decl, Declarations (P));
4553 end if;
7324bf49
AC
4554
4555 -- Replace the anonymous type with an occurrence of the new declaration.
9dfd2ff8 4556 -- In all cases the rewritten node does not have the null-exclusion
7324bf49
AC
4557 -- attribute because (if present) it was already inherited by the
4558 -- anonymous entity (Anon). Thus, in case of components we do not
4559 -- inherit this attribute.
4560
4561 if Nkind (N) = N_Parameter_Specification then
4562 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4563 Set_Etype (Defining_Identifier (N), Anon);
4564 Set_Null_Exclusion_Present (N, False);
fea9e956
ES
4565
4566 elsif Nkind (N) = N_Object_Declaration then
4567 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4568 Set_Etype (Defining_Identifier (N), Anon);
4569
2b73cf68
JM
4570 elsif Nkind (N) = N_Access_Function_Definition then
4571 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4572
b1c11e0e
JM
4573 elsif Nkind (N) = N_Function_Specification then
4574 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4575 Set_Etype (Defining_Unit_Name (N), Anon);
4576
7324bf49
AC
4577 else
4578 Rewrite (Comp,
4579 Make_Component_Definition (Loc,
4580 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4581 end if;
4582
4583 Mark_Rewrite_Insertion (Comp);
4584
7d7af38a 4585 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
fea9e956 4586 Analyze (Decl);
2b73cf68 4587
fea9e956 4588 else
cd1c668b
ES
4589 -- Temporarily remove the current scope (record or subprogram) from
4590 -- the stack to add the new declarations to the enclosing scope.
4591
2b73cf68 4592 Scope_Stack.Decrement_Last;
fea9e956 4593 Analyze (Decl);
2b73cf68
JM
4594 Set_Is_Itype (Anon);
4595 Scope_Stack.Append (Curr_Scope);
fea9e956 4596 end if;
7324bf49 4597
fea9e956 4598 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
7d7af38a 4599 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
7324bf49
AC
4600 return Anon;
4601 end Replace_Anonymous_Access_To_Protected_Subprogram;
4602
996ae0b0
RK
4603 -------------------------------
4604 -- Build_Derived_Access_Type --
4605 -------------------------------
4606
4607 procedure Build_Derived_Access_Type
4608 (N : Node_Id;
4609 Parent_Type : Entity_Id;
4610 Derived_Type : Entity_Id)
4611 is
4612 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4613
4614 Desig_Type : Entity_Id;
4615 Discr : Entity_Id;
4616 Discr_Con_Elist : Elist_Id;
4617 Discr_Con_El : Elmt_Id;
6e937c1c 4618 Subt : Entity_Id;
996ae0b0
RK
4619
4620 begin
c6fe3827
GD
4621 -- Set the designated type so it is available in case this is an access
4622 -- to a self-referential type, e.g. a standard list type with a next
4623 -- pointer. Will be reset after subtype is built.
996ae0b0 4624
a397db96
AC
4625 Set_Directly_Designated_Type
4626 (Derived_Type, Designated_Type (Parent_Type));
996ae0b0
RK
4627
4628 Subt := Process_Subtype (S, N);
4629
4630 if Nkind (S) /= N_Subtype_Indication
4631 and then Subt /= Base_Type (Subt)
4632 then
4633 Set_Ekind (Derived_Type, E_Access_Subtype);
4634 end if;
4635
4636 if Ekind (Derived_Type) = E_Access_Subtype then
4637 declare
4638 Pbase : constant Entity_Id := Base_Type (Parent_Type);
4639 Ibase : constant Entity_Id :=
4640 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
4641 Svg_Chars : constant Name_Id := Chars (Ibase);
4642 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
4643
4644 begin
4645 Copy_Node (Pbase, Ibase);
4646
07fc65c4
GB
4647 Set_Chars (Ibase, Svg_Chars);
4648 Set_Next_Entity (Ibase, Svg_Next_E);
4649 Set_Sloc (Ibase, Sloc (Derived_Type));
4650 Set_Scope (Ibase, Scope (Derived_Type));
4651 Set_Freeze_Node (Ibase, Empty);
4652 Set_Is_Frozen (Ibase, False);
4653 Set_Comes_From_Source (Ibase, False);
4654 Set_Is_First_Subtype (Ibase, False);
996ae0b0
RK
4655
4656 Set_Etype (Ibase, Pbase);
4657 Set_Etype (Derived_Type, Ibase);
4658 end;
4659 end if;
4660
4661 Set_Directly_Designated_Type
4662 (Derived_Type, Designated_Type (Subt));
4663
4664 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
4665 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
4666 Set_Size_Info (Derived_Type, Parent_Type);
4667 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
4668 Set_Depends_On_Private (Derived_Type,
4669 Has_Private_Component (Derived_Type));
4670 Conditional_Delay (Derived_Type, Subt);
4671
7bd98753 4672 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
fa961f76 4673 -- that it is not redundant.
2820d220 4674
fa961f76
ES
4675 if Null_Exclusion_Present (Type_Definition (N)) then
4676 Set_Can_Never_Be_Null (Derived_Type);
4677
4678 if Can_Never_Be_Null (Parent_Type)
4679 and then False
4680 then
4681 Error_Msg_NE
4682 ("`NOT NULL` not allowed (& already excludes null)",
4683 N, Parent_Type);
4684 end if;
4685
4686 elsif Can_Never_Be_Null (Parent_Type) then
2820d220
AC
4687 Set_Can_Never_Be_Null (Derived_Type);
4688 end if;
4689
c6fe3827
GD
4690 -- Note: we do not copy the Storage_Size_Variable, since we always go to
4691 -- the root type for this information.
996ae0b0
RK
4692
4693 -- Apply range checks to discriminants for derived record case
4694 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
4695
4696 Desig_Type := Designated_Type (Derived_Type);
4697 if Is_Composite_Type (Desig_Type)
4698 and then (not Is_Array_Type (Desig_Type))
4699 and then Has_Discriminants (Desig_Type)
4700 and then Base_Type (Desig_Type) /= Desig_Type
4701 then
4702 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
4703 Discr_Con_El := First_Elmt (Discr_Con_Elist);
4704
4705 Discr := First_Discriminant (Base_Type (Desig_Type));
4706 while Present (Discr_Con_El) loop
4707 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
4708 Next_Elmt (Discr_Con_El);
4709 Next_Discriminant (Discr);
4710 end loop;
4711 end if;
4712 end Build_Derived_Access_Type;
4713
4714 ------------------------------
4715 -- Build_Derived_Array_Type --
4716 ------------------------------
4717
4718 procedure Build_Derived_Array_Type
4719 (N : Node_Id;
4720 Parent_Type : Entity_Id;
4721 Derived_Type : Entity_Id)
4722 is
4723 Loc : constant Source_Ptr := Sloc (N);
4724 Tdef : constant Node_Id := Type_Definition (N);
4725 Indic : constant Node_Id := Subtype_Indication (Tdef);
4726 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
4727 Implicit_Base : Entity_Id;
4728 New_Indic : Node_Id;
4729
4730 procedure Make_Implicit_Base;
c6fe3827
GD
4731 -- If the parent subtype is constrained, the derived type is a subtype
4732 -- of an implicit base type derived from the parent base.
996ae0b0
RK
4733
4734 ------------------------
4735 -- Make_Implicit_Base --
4736 ------------------------
4737
4738 procedure Make_Implicit_Base is
4739 begin
4740 Implicit_Base :=
4741 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4742
4743 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
4744 Set_Etype (Implicit_Base, Parent_Base);
4745
4746 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
4747 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
4748
4749 Set_Has_Delayed_Freeze (Implicit_Base, True);
4750 end Make_Implicit_Base;
4751
4752 -- Start of processing for Build_Derived_Array_Type
4753
4754 begin
4755 if not Is_Constrained (Parent_Type) then
4756 if Nkind (Indic) /= N_Subtype_Indication then
4757 Set_Ekind (Derived_Type, E_Array_Type);
4758
4759 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4760 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
4761
4762 Set_Has_Delayed_Freeze (Derived_Type, True);
4763
4764 else
4765 Make_Implicit_Base;
4766 Set_Etype (Derived_Type, Implicit_Base);
4767
4768 New_Indic :=
4769 Make_Subtype_Declaration (Loc,
4770 Defining_Identifier => Derived_Type,
4771 Subtype_Indication =>
4772 Make_Subtype_Indication (Loc,
4773 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
4774 Constraint => Constraint (Indic)));
4775
4776 Rewrite (N, New_Indic);
4777 Analyze (N);
4778 end if;
4779
4780 else
4781 if Nkind (Indic) /= N_Subtype_Indication then
4782 Make_Implicit_Base;
4783
4784 Set_Ekind (Derived_Type, Ekind (Parent_Type));
4785 Set_Etype (Derived_Type, Implicit_Base);
4786 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4787
4788 else
4789 Error_Msg_N ("illegal constraint on constrained type", Indic);
4790 end if;
4791 end if;
4792
9dfd2ff8
CC
4793 -- If parent type is not a derived type itself, and is declared in
4794 -- closed scope (e.g. a subprogram), then we must explicitly introduce
4795 -- the new type's concatenation operator since Derive_Subprograms
4796 -- will not inherit the parent's operator. If the parent type is
4797 -- unconstrained, the operator is of the unconstrained base type.
996ae0b0
RK
4798
4799 if Number_Dimensions (Parent_Type) = 1
4800 and then not Is_Limited_Type (Parent_Type)
4801 and then not Is_Derived_Type (Parent_Type)
950d3e7d
ES
4802 and then not Is_Package_Or_Generic_Package
4803 (Scope (Base_Type (Parent_Type)))
996ae0b0 4804 then
81a5b587
AC
4805 if not Is_Constrained (Parent_Type)
4806 and then Is_Constrained (Derived_Type)
4807 then
4808 New_Concatenation_Op (Implicit_Base);
4809 else
4810 New_Concatenation_Op (Derived_Type);
4811 end if;
996ae0b0
RK
4812 end if;
4813 end Build_Derived_Array_Type;
4814
4815 -----------------------------------
4816 -- Build_Derived_Concurrent_Type --
4817 -----------------------------------
4818
4819 procedure Build_Derived_Concurrent_Type
4820 (N : Node_Id;
4821 Parent_Type : Entity_Id;
4822 Derived_Type : Entity_Id)
4823 is
4824 D_Constraint : Node_Id;
4825 Disc_Spec : Node_Id;
4826 Old_Disc : Entity_Id;
4827 New_Disc : Entity_Id;
7ae0dcd8 4828
996ae0b0 4829 Constraint_Present : constant Boolean :=
7ae0dcd8
ES
4830 Nkind (Subtype_Indication (Type_Definition (N)))
4831 = N_Subtype_Indication;
996ae0b0
RK
4832
4833 begin
fbf5a39b 4834 Set_Stored_Constraint (Derived_Type, No_Elist);
996ae0b0 4835
ce4a6e84
RD
4836 -- Copy Storage_Size and Relative_Deadline variables if task case
4837
996ae0b0
RK
4838 if Is_Task_Type (Parent_Type) then
4839 Set_Storage_Size_Variable (Derived_Type,
4840 Storage_Size_Variable (Parent_Type));
ce4a6e84
RD
4841 Set_Relative_Deadline_Variable (Derived_Type,
4842 Relative_Deadline_Variable (Parent_Type));
996ae0b0
RK
4843 end if;
4844
4845 if Present (Discriminant_Specifications (N)) then
2b73cf68 4846 Push_Scope (Derived_Type);
996ae0b0
RK
4847 Check_Or_Process_Discriminants (N, Derived_Type);
4848 End_Scope;
7ae0dcd8
ES
4849
4850 elsif Constraint_Present then
4851
4852 -- Build constrained subtype and derive from it
4853
4854 declare
4855 Loc : constant Source_Ptr := Sloc (N);
fbf5a39b 4856 Anon : constant Entity_Id :=
7ae0dcd8
ES
4857 Make_Defining_Identifier (Loc,
4858 New_External_Name (Chars (Derived_Type), 'T'));
4859 Decl : Node_Id;
4860
4861 begin
4862 Decl :=
4863 Make_Subtype_Declaration (Loc,
4864 Defining_Identifier => Anon,
4865 Subtype_Indication =>
88b32fc3 4866 Subtype_Indication (Type_Definition (N)));
7ae0dcd8 4867 Insert_Before (N, Decl);
88b32fc3
BD
4868 Analyze (Decl);
4869
7ae0dcd8
ES
4870 Rewrite (Subtype_Indication (Type_Definition (N)),
4871 New_Occurrence_Of (Anon, Loc));
7ae0dcd8
ES
4872 Set_Analyzed (Derived_Type, False);
4873 Analyze (N);
4874 return;
4875 end;
996ae0b0
RK
4876 end if;
4877
4878 -- All attributes are inherited from parent. In particular,
4879 -- entries and the corresponding record type are the same.
4880 -- Discriminants may be renamed, and must be treated separately.
4881
4882 Set_Has_Discriminants
7ae0dcd8 4883 (Derived_Type, Has_Discriminants (Parent_Type));
996ae0b0 4884 Set_Corresponding_Record_Type
7ae0dcd8 4885 (Derived_Type, Corresponding_Record_Type (Parent_Type));
996ae0b0 4886
0501956d
GD
4887 -- Is_Constrained is set according the parent subtype, but is set to
4888 -- False if the derived type is declared with new discriminants.
4889
4890 Set_Is_Constrained
4891 (Derived_Type,
4892 (Is_Constrained (Parent_Type) or else Constraint_Present)
4893 and then not Present (Discriminant_Specifications (N)));
4894
996ae0b0 4895 if Constraint_Present then
996ae0b0
RK
4896 if not Has_Discriminants (Parent_Type) then
4897 Error_Msg_N ("untagged parent must have discriminants", N);
4898
4899 elsif Present (Discriminant_Specifications (N)) then
4900
9dfd2ff8 4901 -- Verify that new discriminants are used to constrain old ones
996ae0b0 4902
996ae0b0 4903 D_Constraint :=
7ae0dcd8
ES
4904 First
4905 (Constraints
4906 (Constraint (Subtype_Indication (Type_Definition (N)))));
996ae0b0 4907
9dfd2ff8
CC
4908 Old_Disc := First_Discriminant (Parent_Type);
4909 New_Disc := First_Discriminant (Derived_Type);
4910 Disc_Spec := First (Discriminant_Specifications (N));
996ae0b0 4911 while Present (Old_Disc) and then Present (Disc_Spec) loop
996ae0b0 4912 if Nkind (Discriminant_Type (Disc_Spec)) /=
7ae0dcd8 4913 N_Access_Definition
996ae0b0
RK
4914 then
4915 Analyze (Discriminant_Type (Disc_Spec));
7ae0dcd8 4916
996ae0b0
RK
4917 if not Subtypes_Statically_Compatible (
4918 Etype (Discriminant_Type (Disc_Spec)),
4919 Etype (Old_Disc))
4920 then
4921 Error_Msg_N
4922 ("not statically compatible with parent discriminant",
4923 Discriminant_Type (Disc_Spec));
4924 end if;
4925 end if;
4926
4927 if Nkind (D_Constraint) = N_Identifier
4928 and then Chars (D_Constraint) /=
7d7af38a 4929 Chars (Defining_Identifier (Disc_Spec))
996ae0b0
RK
4930 then
4931 Error_Msg_N ("new discriminants must constrain old ones",
4932 D_Constraint);
4933 else
4934 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
4935 end if;
4936
4937 Next_Discriminant (Old_Disc);
4938 Next_Discriminant (New_Disc);
4939 Next (Disc_Spec);
4940 end loop;
4941
4942 if Present (Old_Disc) or else Present (Disc_Spec) then
4943 Error_Msg_N ("discriminant mismatch in derivation", N);
4944 end if;
4945
4946 end if;
4947
4948 elsif Present (Discriminant_Specifications (N)) then
4949 Error_Msg_N
4950 ("missing discriminant constraint in untagged derivation",
4951 N);
4952 end if;
4953
4954 if Present (Discriminant_Specifications (N)) then
996ae0b0 4955 Old_Disc := First_Discriminant (Parent_Type);
996ae0b0
RK
4956 while Present (Old_Disc) loop
4957
4958 if No (Next_Entity (Old_Disc))
4959 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
4960 then
4961 Set_Next_Entity (Last_Entity (Derived_Type),
4962 Next_Entity (Old_Disc));
4963 exit;
4964 end if;
4965
4966 Next_Discriminant (Old_Disc);
4967 end loop;
4968
4969 else
4970 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7ae0dcd8 4971 if Has_Discriminants (Parent_Type) then
7324bf49 4972 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7ae0dcd8
ES
4973 Set_Discriminant_Constraint (
4974 Derived_Type, Discriminant_Constraint (Parent_Type));
4975 end if;
996ae0b0
RK
4976 end if;
4977
4978 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
4979
4980 Set_Has_Completion (Derived_Type);
4981 end Build_Derived_Concurrent_Type;
4982
4983 ------------------------------------
4984 -- Build_Derived_Enumeration_Type --
4985 ------------------------------------
4986
4987 procedure Build_Derived_Enumeration_Type
4988 (N : Node_Id;
4989 Parent_Type : Entity_Id;
4990 Derived_Type : Entity_Id)
4991 is
4992 Loc : constant Source_Ptr := Sloc (N);
4993 Def : constant Node_Id := Type_Definition (N);
4994 Indic : constant Node_Id := Subtype_Indication (Def);
4995 Implicit_Base : Entity_Id;
4996 Literal : Entity_Id;
4997 New_Lit : Entity_Id;
4998 Literals_List : List_Id;
4999 Type_Decl : Node_Id;
5000 Hi, Lo : Node_Id;
5001 Rang_Expr : Node_Id;
5002
5003 begin
94fd3dc6 5004 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
996ae0b0
RK
5005 -- not have explicit literals lists we need to process types derived
5006 -- from them specially. This is handled by Derived_Standard_Character.
5007 -- If the parent type is a generic type, there are no literals either,
5008 -- and we construct the same skeletal representation as for the generic
5009 -- parent type.
5010
ce4a6e84 5011 if Is_Standard_Character_Type (Parent_Type) then
996ae0b0
RK
5012 Derived_Standard_Character (N, Parent_Type, Derived_Type);
5013
5014 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
5015 declare
5016 Lo : Node_Id;
5017 Hi : Node_Id;
5018
5019 begin
054275e4
ES
5020 if Nkind (Indic) /= N_Subtype_Indication then
5021 Lo :=
5022 Make_Attribute_Reference (Loc,
5023 Attribute_Name => Name_First,
03b64787 5024 Prefix => New_Reference_To (Derived_Type, Loc));
054275e4
ES
5025 Set_Etype (Lo, Derived_Type);
5026
5027 Hi :=
5028 Make_Attribute_Reference (Loc,
5029 Attribute_Name => Name_Last,
03b64787 5030 Prefix => New_Reference_To (Derived_Type, Loc));
054275e4
ES
5031 Set_Etype (Hi, Derived_Type);
5032
5033 Set_Scalar_Range (Derived_Type,
5034 Make_Range (Loc,
03b64787 5035 Low_Bound => Lo,
054275e4
ES
5036 High_Bound => Hi));
5037 else
5038
5039 -- Analyze subtype indication and verify compatibility
5040 -- with parent type.
5041
03b64787
AC
5042 if Base_Type (Process_Subtype (Indic, N)) /=
5043 Base_Type (Parent_Type)
054275e4
ES
5044 then
5045 Error_Msg_N
5046 ("illegal constraint for formal discrete type", N);
5047 end if;
5048 end if;
996ae0b0
RK
5049 end;
5050
5051 else
5052 -- If a constraint is present, analyze the bounds to catch
5053 -- premature usage of the derived literals.
5054
5055 if Nkind (Indic) = N_Subtype_Indication
5056 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5057 then
5058 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
5059 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5060 end if;
5061
c6fe3827
GD
5062 -- Introduce an implicit base type for the derived type even if there
5063 -- is no constraint attached to it, since this seems closer to the
5064 -- Ada semantics. Build a full type declaration tree for the derived
5065 -- type using the implicit base type as the defining identifier. The
5066 -- build a subtype declaration tree which applies the constraint (if
5067 -- any) have it replace the derived type declaration.
996ae0b0
RK
5068
5069 Literal := First_Literal (Parent_Type);
5070 Literals_List := New_List;
996ae0b0
RK
5071 while Present (Literal)
5072 and then Ekind (Literal) = E_Enumeration_Literal
5073 loop
5074 -- Literals of the derived type have the same representation as
5075 -- those of the parent type, but this representation can be
5076 -- overridden by an explicit representation clause. Indicate
5077 -- that there is no explicit representation given yet. These
5078 -- derived literals are implicit operations of the new type,
9dfd2ff8 5079 -- and can be overridden by explicit ones.
996ae0b0
RK
5080
5081 if Nkind (Literal) = N_Defining_Character_Literal then
5082 New_Lit :=
5083 Make_Defining_Character_Literal (Loc, Chars (Literal));
5084 else
5085 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5086 end if;
5087
5088 Set_Ekind (New_Lit, E_Enumeration_Literal);
5089 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
5090 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
5091 Set_Enumeration_Rep_Expr (New_Lit, Empty);
5092 Set_Alias (New_Lit, Literal);
5093 Set_Is_Known_Valid (New_Lit, True);
5094
5095 Append (New_Lit, Literals_List);
5096 Next_Literal (Literal);
5097 end loop;
5098
5099 Implicit_Base :=
5100 Make_Defining_Identifier (Sloc (Derived_Type),
5101 New_External_Name (Chars (Derived_Type), 'B'));
5102
c6fe3827
GD
5103 -- Indicate the proper nature of the derived type. This must be done
5104 -- before analysis of the literals, to recognize cases when a literal
5105 -- may be hidden by a previous explicit function definition (cf.
5106 -- c83031a).
996ae0b0
RK
5107
5108 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5109 Set_Etype (Derived_Type, Implicit_Base);
5110
5111 Type_Decl :=
5112 Make_Full_Type_Declaration (Loc,
5113 Defining_Identifier => Implicit_Base,
5114 Discriminant_Specifications => No_List,
5115 Type_Definition =>
5116 Make_Enumeration_Type_Definition (Loc, Literals_List));
5117
5118 Mark_Rewrite_Insertion (Type_Decl);
5119 Insert_Before (N, Type_Decl);
5120 Analyze (Type_Decl);
5121
a5b62485
AC
5122 -- After the implicit base is analyzed its Etype needs to be changed
5123 -- to reflect the fact that it is derived from the parent type which
5124 -- was ignored during analysis. We also set the size at this point.
996ae0b0
RK
5125
5126 Set_Etype (Implicit_Base, Parent_Type);
5127
5128 Set_Size_Info (Implicit_Base, Parent_Type);
5129 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5130 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5131
5132 Set_Has_Non_Standard_Rep
5133 (Implicit_Base, Has_Non_Standard_Rep
5134 (Parent_Type));
5135 Set_Has_Delayed_Freeze (Implicit_Base);
5136
c6fe3827
GD
5137 -- Process the subtype indication including a validation check on the
5138 -- constraint, if any. If a constraint is given, its bounds must be
5139 -- implicitly converted to the new type.
996ae0b0
RK
5140
5141 if Nkind (Indic) = N_Subtype_Indication then
996ae0b0 5142 declare
71d9e9f2
ES
5143 R : constant Node_Id :=
5144 Range_Expression (Constraint (Indic));
996ae0b0
RK
5145
5146 begin
5147 if Nkind (R) = N_Range then
5148 Hi := Build_Scalar_Bound
07fc65c4 5149 (High_Bound (R), Parent_Type, Implicit_Base);
996ae0b0 5150 Lo := Build_Scalar_Bound
07fc65c4 5151 (Low_Bound (R), Parent_Type, Implicit_Base);
996ae0b0
RK
5152
5153 else
c6fe3827
GD
5154 -- Constraint is a Range attribute. Replace with explicit
5155 -- mention of the bounds of the prefix, which must be a
5156 -- subtype.
996ae0b0
RK
5157
5158 Analyze (Prefix (R));
5159 Hi :=
5160 Convert_To (Implicit_Base,
5161 Make_Attribute_Reference (Loc,
5162 Attribute_Name => Name_Last,
5163 Prefix =>
5164 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5165
5166 Lo :=
5167 Convert_To (Implicit_Base,
5168 Make_Attribute_Reference (Loc,
5169 Attribute_Name => Name_First,
5170 Prefix =>
5171 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5172 end if;
996ae0b0
RK
5173 end;
5174
5175 else
5176 Hi :=
5177 Build_Scalar_Bound
5178 (Type_High_Bound (Parent_Type),
07fc65c4 5179 Parent_Type, Implicit_Base);
996ae0b0
RK
5180 Lo :=
5181 Build_Scalar_Bound
5182 (Type_Low_Bound (Parent_Type),
07fc65c4 5183 Parent_Type, Implicit_Base);
996ae0b0
RK
5184 end if;
5185
5186 Rang_Expr :=
5187 Make_Range (Loc,
5188 Low_Bound => Lo,
5189 High_Bound => Hi);
5190
5191 -- If we constructed a default range for the case where no range
5192 -- was given, then the expressions in the range must not freeze
5193 -- since they do not correspond to expressions in the source.
5194
5195 if Nkind (Indic) /= N_Subtype_Indication then
5196 Set_Must_Not_Freeze (Lo);
5197 Set_Must_Not_Freeze (Hi);
5198 Set_Must_Not_Freeze (Rang_Expr);
5199 end if;
5200
5201 Rewrite (N,
5202 Make_Subtype_Declaration (Loc,
5203 Defining_Identifier => Derived_Type,
5204 Subtype_Indication =>
5205 Make_Subtype_Indication (Loc,
5206 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5207 Constraint =>
5208 Make_Range_Constraint (Loc,
5209 Range_Expression => Rang_Expr))));
5210
5211 Analyze (N);
5212
c6fe3827
GD
5213 -- If pragma Discard_Names applies on the first subtype of the parent
5214 -- type, then it must be applied on this subtype as well.
996ae0b0
RK
5215
5216 if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5217 Set_Discard_Names (Derived_Type);
5218 end if;
5219
a5b62485
AC
5220 -- Apply a range check. Since this range expression doesn't have an
5221 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5222 -- this right???
996ae0b0
RK
5223
5224 if Nkind (Indic) = N_Subtype_Indication then
5225 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5226 Parent_Type,
5227 Source_Typ => Entity (Subtype_Mark (Indic)));
5228 end if;
5229 end if;
996ae0b0
RK
5230 end Build_Derived_Enumeration_Type;
5231
5232 --------------------------------
5233 -- Build_Derived_Numeric_Type --
5234 --------------------------------
5235
5236 procedure Build_Derived_Numeric_Type
5237 (N : Node_Id;
5238 Parent_Type : Entity_Id;
5239 Derived_Type : Entity_Id)
5240 is
5241 Loc : constant Source_Ptr := Sloc (N);
5242 Tdef : constant Node_Id := Type_Definition (N);
5243 Indic : constant Node_Id := Subtype_Indication (Tdef);
5244 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5245 No_Constraint : constant Boolean := Nkind (Indic) /=
5246 N_Subtype_Indication;
71d9e9f2 5247 Implicit_Base : Entity_Id;
996ae0b0
RK
5248
5249 Lo : Node_Id;
5250 Hi : Node_Id;
996ae0b0
RK
5251
5252 begin
5253 -- Process the subtype indication including a validation check on
5254 -- the constraint if any.
5255
fbf5a39b 5256 Discard_Node (Process_Subtype (Indic, N));
996ae0b0 5257
a5b62485
AC
5258 -- Introduce an implicit base type for the derived type even if there
5259 -- is no constraint attached to it, since this seems closer to the Ada
5260 -- semantics.
996ae0b0
RK
5261
5262 Implicit_Base :=
5263 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5264
5265 Set_Etype (Implicit_Base, Parent_Base);
5266 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5267 Set_Size_Info (Implicit_Base, Parent_Base);
996ae0b0
RK
5268 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5269 Set_Parent (Implicit_Base, Parent (Derived_Type));
8dc2ddaf 5270 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
996ae0b0 5271
7d7af38a
JM
5272 -- Set RM Size for discrete type or decimal fixed-point type
5273 -- Ordinary fixed-point is excluded, why???
5274
5275 if Is_Discrete_Type (Parent_Base)
5276 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7bde4677 5277 then
996ae0b0
RK
5278 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5279 end if;
5280
5281 Set_Has_Delayed_Freeze (Implicit_Base);
5282
5283 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5284 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5285
5286 Set_Scalar_Range (Implicit_Base,
5287 Make_Range (Loc,
5288 Low_Bound => Lo,
5289 High_Bound => Hi));
5290
5291 if Has_Infinities (Parent_Base) then
5292 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5293 end if;
5294
a5b62485
AC
5295 -- The Derived_Type, which is the entity of the declaration, is a
5296 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5297 -- absence of an explicit constraint.
996ae0b0
RK
5298
5299 Set_Etype (Derived_Type, Implicit_Base);
5300
5301 -- If we did not have a constraint, then the Ekind is set from the
5302 -- parent type (otherwise Process_Subtype has set the bounds)
5303
5304 if No_Constraint then
5305 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5306 end if;
5307
a5b62485
AC
5308 -- If we did not have a range constraint, then set the range from the
5309 -- parent type. Otherwise, the call to Process_Subtype has set the
5310 -- bounds.
996ae0b0
RK
5311
5312 if No_Constraint
5313 or else not Has_Range_Constraint (Indic)
5314 then
5315 Set_Scalar_Range (Derived_Type,
5316 Make_Range (Loc,
5317 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
5318 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5319 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5320
5321 if Has_Infinities (Parent_Type) then
5322 Set_Includes_Infinities (Scalar_Range (Derived_Type));
5323 end if;
8dc2ddaf
RD
5324
5325 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
996ae0b0
RK
5326 end if;
5327
9c510803
ES
5328 Set_Is_Descendent_Of_Address (Derived_Type,
5329 Is_Descendent_Of_Address (Parent_Type));
5330 Set_Is_Descendent_Of_Address (Implicit_Base,
5331 Is_Descendent_Of_Address (Parent_Type));
5332
996ae0b0
RK
5333 -- Set remaining type-specific fields, depending on numeric type
5334
5335 if Is_Modular_Integer_Type (Parent_Type) then
5336 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5337
5338 Set_Non_Binary_Modulus
5339 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5340
8dc2ddaf
RD
5341 Set_Is_Known_Valid
5342 (Implicit_Base, Is_Known_Valid (Parent_Base));
5343
996ae0b0
RK
5344 elsif Is_Floating_Point_Type (Parent_Type) then
5345
5346 -- Digits of base type is always copied from the digits value of
5347 -- the parent base type, but the digits of the derived type will
5348 -- already have been set if there was a constraint present.
5349
5350 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5351 Set_Vax_Float (Implicit_Base, Vax_Float (Parent_Base));
5352
5353 if No_Constraint then
5354 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5355 end if;
5356
5357 elsif Is_Fixed_Point_Type (Parent_Type) then
5358
a5b62485
AC
5359 -- Small of base type and derived type are always copied from the
5360 -- parent base type, since smalls never change. The delta of the
5361 -- base type is also copied from the parent base type. However the
5362 -- delta of the derived type will have been set already if a
5363 -- constraint was present.
996ae0b0
RK
5364
5365 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
5366 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5367 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5368
5369 if No_Constraint then
5370 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
5371 end if;
5372
5373 -- The scale and machine radix in the decimal case are always
5374 -- copied from the parent base type.
5375
5376 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5377 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
5378 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5379
5380 Set_Machine_Radix_10
5381 (Derived_Type, Machine_Radix_10 (Parent_Base));
5382 Set_Machine_Radix_10
5383 (Implicit_Base, Machine_Radix_10 (Parent_Base));
5384
5385 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5386
5387 if No_Constraint then
5388 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5389
5390 else
5391 -- the analysis of the subtype_indication sets the
5392 -- digits value of the derived type.
5393
5394 null;
5395 end if;
5396 end if;
5397 end if;
5398
5399 -- The type of the bounds is that of the parent type, and they
5400 -- must be converted to the derived type.
5401
5402 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5403
5404 -- The implicit_base should be frozen when the derived type is frozen,
a5b62485
AC
5405 -- but note that it is used in the conversions of the bounds. For fixed
5406 -- types we delay the determination of the bounds until the proper
996ae0b0
RK
5407 -- freezing point. For other numeric types this is rejected by GCC, for
5408 -- reasons that are currently unclear (???), so we choose to freeze the
5409 -- implicit base now. In the case of integers and floating point types
5410 -- this is harmless because subsequent representation clauses cannot
5411 -- affect anything, but it is still baffling that we cannot use the
5412 -- same mechanism for all derived numeric types.
5413
88b32fc3
BD
5414 -- There is a further complication: actually *some* representation
5415 -- clauses can affect the implicit base type. Namely, attribute
5416 -- definition clauses for stream-oriented attributes need to set the
5417 -- corresponding TSS entries on the base type, and this normally cannot
5418 -- be done after the base type is frozen, so the circuitry in
5419 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5420 -- not use Set_TSS in this case.
5421
996ae0b0
RK
5422 if Is_Fixed_Point_Type (Parent_Type) then
5423 Conditional_Delay (Implicit_Base, Parent_Type);
5424 else
5425 Freeze_Before (N, Implicit_Base);
5426 end if;
996ae0b0
RK
5427 end Build_Derived_Numeric_Type;
5428
5429 --------------------------------
5430 -- Build_Derived_Private_Type --
5431 --------------------------------
5432
5433 procedure Build_Derived_Private_Type
07fc65c4
GB
5434 (N : Node_Id;
5435 Parent_Type : Entity_Id;
5436 Derived_Type : Entity_Id;
996ae0b0
RK
5437 Is_Completion : Boolean;
5438 Derive_Subps : Boolean := True)
5439 is
39f346aa 5440 Loc : constant Source_Ptr := Sloc (N);
996ae0b0
RK
5441 Der_Base : Entity_Id;
5442 Discr : Entity_Id;
5443 Full_Decl : Node_Id := Empty;
5444 Full_Der : Entity_Id;
5445 Full_P : Entity_Id;
5446 Last_Discr : Entity_Id;
5447 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
5448 Swapped : Boolean := False;
5449
5450 procedure Copy_And_Build;
5451 -- Copy derived type declaration, replace parent with its full view,
5452 -- and analyze new declaration.
5453
07fc65c4
GB
5454 --------------------
5455 -- Copy_And_Build --
5456 --------------------
5457
996ae0b0 5458 procedure Copy_And_Build is
71d9e9f2 5459 Full_N : Node_Id;
996ae0b0
RK
5460
5461 begin
5462 if Ekind (Parent_Type) in Record_Kind
82c80734
RD
5463 or else
5464 (Ekind (Parent_Type) in Enumeration_Kind
ce4a6e84 5465 and then not Is_Standard_Character_Type (Parent_Type)
82c80734 5466 and then not Is_Generic_Type (Root_Type (Parent_Type)))
996ae0b0
RK
5467 then
5468 Full_N := New_Copy_Tree (N);
5469 Insert_After (N, Full_N);
5470 Build_Derived_Type (
5471 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5472
5473 else
5474 Build_Derived_Type (
5475 N, Parent_Type, Full_Der, True, Derive_Subps => False);
5476 end if;
5477 end Copy_And_Build;
5478
5479 -- Start of processing for Build_Derived_Private_Type
5480
5481 begin
5482 if Is_Tagged_Type (Parent_Type) then
9013065b 5483 Full_P := Full_View (Parent_Type);
39f346aa
ES
5484
5485 -- A type extension of a type with unknown discriminants is an
5486 -- indefinite type that the back-end cannot handle directly.
5487 -- We treat it as a private type, and build a completion that is
5488 -- derived from the full view of the parent, and hopefully has
9013065b
AC
5489 -- known discriminants.
5490
c206e8fd
AC
5491 -- If the full view of the parent type has an underlying record view,
5492 -- use it to generate the underlying record view of this derived type
5493 -- (required for chains of derivations with unknown discriminants).
9013065b 5494
c206e8fd 5495 -- Minor optimization: we avoid the generation of useless underlying
9013065b 5496 -- record view entities if the private type declaration has unknown
c206e8fd
AC
5497 -- discriminants but its corresponding full view has no
5498 -- discriminants.
39f346aa
ES
5499
5500 if Has_Unknown_Discriminants (Parent_Type)
9013065b
AC
5501 and then Present (Full_P)
5502 and then (Has_Discriminants (Full_P)
5503 or else Present (Underlying_Record_View (Full_P)))
39f346aa 5504 and then not In_Open_Scopes (Par_Scope)
39f346aa
ES
5505 and then Expander_Active
5506 then
5507 declare
5508 Full_Der : constant Entity_Id :=
c5d91669
AC
5509 Make_Defining_Identifier (Loc,
5510 Chars => New_Internal_Name ('T'));
c5d91669
AC
5511 New_Ext : constant Node_Id :=
5512 Copy_Separate_Tree
5513 (Record_Extension_Part (Type_Definition (N)));
9013065b 5514 Decl : Node_Id;
39f346aa
ES
5515
5516 begin
5517 Build_Derived_Record_Type
5518 (N, Parent_Type, Derived_Type, Derive_Subps);
5519
5520 -- Build anonymous completion, as a derivation from the full
bf06d37f
AC
5521 -- view of the parent. This is not a completion in the usual
5522 -- sense, because the current type is not private.
39f346aa
ES
5523
5524 Decl :=
5525 Make_Full_Type_Declaration (Loc,
5526 Defining_Identifier => Full_Der,
5527 Type_Definition =>
5528 Make_Derived_Type_Definition (Loc,
5529 Subtype_Indication =>
5530 New_Copy_Tree
5531 (Subtype_Indication (Type_Definition (N))),
5532 Record_Extension_Part => New_Ext));
9013065b 5533
c206e8fd
AC
5534 -- If the parent type has an underlying record view, use it
5535 -- here to build the new underlying record view.
9013065b
AC
5536
5537 if Present (Underlying_Record_View (Full_P)) then
5538 pragma Assert
5539 (Nkind (Subtype_Indication (Type_Definition (Decl)))
5540 = N_Identifier);
5541 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
5542 Underlying_Record_View (Full_P));
5543 end if;
5544
39f346aa
ES
5545 Install_Private_Declarations (Par_Scope);
5546 Install_Visible_Declarations (Par_Scope);
bddd6058 5547 Insert_Before (N, Decl);
9013065b 5548
c206e8fd
AC
5549 -- Mark entity as an underlying record view before analysis,
5550 -- to avoid generating the list of its primitive operations
5551 -- (which is not really required for this entity) and thus
5552 -- prevent spurious errors associated with missing overriding
5553 -- of abstract primitives (overridden only for Derived_Type).
9013065b
AC
5554
5555 Set_Ekind (Full_Der, E_Record_Type);
5556 Set_Is_Underlying_Record_View (Full_Der);
5557
39f346aa 5558 Analyze (Decl);
9013065b
AC
5559
5560 pragma Assert (Has_Discriminants (Full_Der)
5561 and then not Has_Unknown_Discriminants (Full_Der));
5562
39f346aa
ES
5563 Uninstall_Declarations (Par_Scope);
5564
c206e8fd
AC
5565 -- Freeze the underlying record view, to prevent generation of
5566 -- useless dispatching information, which is simply shared with
5567 -- the real derived type.
39f346aa
ES
5568
5569 Set_Is_Frozen (Full_Der);
9013065b 5570
c206e8fd 5571 -- Set up links between real entity and underlying record view
9013065b
AC
5572
5573 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
5574 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
39f346aa
ES
5575 end;
5576
c206e8fd 5577 -- If discriminants are known, build derived record
39f346aa
ES
5578
5579 else
5580 Build_Derived_Record_Type
5581 (N, Parent_Type, Derived_Type, Derive_Subps);
5582 end if;
5583
996ae0b0
RK
5584 return;
5585
5586 elsif Has_Discriminants (Parent_Type) then
996ae0b0
RK
5587 if Present (Full_View (Parent_Type)) then
5588 if not Is_Completion then
5589
a5b62485
AC
5590 -- Copy declaration for subsequent analysis, to provide a
5591 -- completion for what is a private declaration. Indicate that
5592 -- the full type is internally generated.
996ae0b0
RK
5593
5594 Full_Decl := New_Copy_Tree (N);
5595 Full_Der := New_Copy (Derived_Type);
7324bf49 5596 Set_Comes_From_Source (Full_Decl, False);
950d3e7d 5597 Set_Comes_From_Source (Full_Der, False);
fbf5a39b 5598
996ae0b0
RK
5599 Insert_After (N, Full_Decl);
5600
5601 else
c206e8fd
AC
5602 -- If this is a completion, the full view being built is itself
5603 -- private. We build a subtype of the parent with the same
5604 -- constraints as this full view, to convey to the back end the
5605 -- constrained components and the size of this subtype. If the
5606 -- parent is constrained, its full view can serve as the
5607 -- underlying full view of the derived type.
996ae0b0
RK
5608
5609 if No (Discriminant_Specifications (N)) then
71d9e9f2
ES
5610 if Nkind (Subtype_Indication (Type_Definition (N))) =
5611 N_Subtype_Indication
996ae0b0
RK
5612 then
5613 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
5614
5615 elsif Is_Constrained (Full_View (Parent_Type)) then
c5d91669
AC
5616 Set_Underlying_Full_View
5617 (Derived_Type, Full_View (Parent_Type));
996ae0b0
RK
5618 end if;
5619
5620 else
5621 -- If there are new discriminants, the parent subtype is
5622 -- constrained by them, but it is not clear how to build
c206e8fd 5623 -- the Underlying_Full_View in this case???
996ae0b0
RK
5624
5625 null;
5626 end if;
5627 end if;
5628 end if;
5629
ffe9aba8 5630 -- Build partial view of derived type from partial view of parent
fbf5a39b 5631
996ae0b0
RK
5632 Build_Derived_Record_Type
5633 (N, Parent_Type, Derived_Type, Derive_Subps);
5634
c206e8fd 5635 if Present (Full_View (Parent_Type)) and then not Is_Completion then
996ae0b0
RK
5636 if not In_Open_Scopes (Par_Scope)
5637 or else not In_Same_Source_Unit (N, Parent_Type)
5638 then
5639 -- Swap partial and full views temporarily
5640
5641 Install_Private_Declarations (Par_Scope);
5642 Install_Visible_Declarations (Par_Scope);
5643 Swapped := True;
5644 end if;
5645
a5b62485
AC
5646 -- Build full view of derived type from full view of parent which
5647 -- is now installed. Subprograms have been derived on the partial
5648 -- view, the completion does not derive them anew.
996ae0b0 5649
fbf5a39b 5650 if not Is_Tagged_Type (Parent_Type) then
950d3e7d
ES
5651
5652 -- If the parent is itself derived from another private type,
5653 -- installing the private declarations has not affected its
5654 -- privacy status, so use its own full view explicitly.
5655
5656 if Is_Private_Type (Parent_Type) then
5657 Build_Derived_Record_Type
5658 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
5659 else
5660 Build_Derived_Record_Type
5661 (Full_Decl, Parent_Type, Full_Der, False);
5662 end if;
fbf5a39b 5663
71d9e9f2 5664 else
c206e8fd
AC
5665 -- If full view of parent is tagged, the completion inherits
5666 -- the proper primitive operations.
fbf5a39b
AC
5667
5668 Set_Defining_Identifier (Full_Decl, Full_Der);
5669 Build_Derived_Record_Type
5670 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
5671 Set_Analyzed (Full_Decl);
5672 end if;
996ae0b0
RK
5673
5674 if Swapped then
5675 Uninstall_Declarations (Par_Scope);
5676
5677 if In_Open_Scopes (Par_Scope) then
5678 Install_Visible_Declarations (Par_Scope);
5679 end if;
5680 end if;
5681
5682 Der_Base := Base_Type (Derived_Type);
5683 Set_Full_View (Derived_Type, Full_Der);
5684 Set_Full_View (Der_Base, Base_Type (Full_Der));
5685
a5b62485 5686 -- Copy the discriminant list from full view to the partial views
c206e8fd
AC
5687 -- (base type and its subtype). Gigi requires that the partial and
5688 -- full views have the same discriminants.
a5b62485
AC
5689
5690 -- Note that since the partial view is pointing to discriminants
5691 -- in the full view, their scope will be that of the full view.
c206e8fd 5692 -- This might cause some front end problems and need adjustment???
996ae0b0
RK
5693
5694 Discr := First_Discriminant (Base_Type (Full_Der));
5695 Set_First_Entity (Der_Base, Discr);
5696
5697 loop
5698 Last_Discr := Discr;
5699 Next_Discriminant (Discr);
5700 exit when No (Discr);
5701 end loop;
5702
5703 Set_Last_Entity (Der_Base, Last_Discr);
5704
5705 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
5706 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
30c20106 5707 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
996ae0b0
RK
5708
5709 else
c206e8fd
AC
5710 -- If this is a completion, the derived type stays private and
5711 -- there is no need to create a further full view, except in the
5712 -- unusual case when the derivation is nested within a child unit,
5713 -- see below.
996ae0b0
RK
5714
5715 null;
5716 end if;
5717
5718 elsif Present (Full_View (Parent_Type))
5719 and then Has_Discriminants (Full_View (Parent_Type))
5720 then
5721 if Has_Unknown_Discriminants (Parent_Type)
7d7af38a
JM
5722 and then Nkind (Subtype_Indication (Type_Definition (N))) =
5723 N_Subtype_Indication
996ae0b0
RK
5724 then
5725 Error_Msg_N
5726 ("cannot constrain type with unknown discriminants",
5727 Subtype_Indication (Type_Definition (N)));
5728 return;
5729 end if;
5730
c206e8fd
AC
5731 -- If full view of parent is a record type, build full view as a
5732 -- derivation from the parent's full view. Partial view remains
5733 -- private. For code generation and linking, the full view must have
5734 -- the same public status as the partial one. This full view is only
5735 -- needed if the parent type is in an enclosing scope, so that the
5736 -- full view may actually become visible, e.g. in a child unit. This
5737 -- is both more efficient, and avoids order of freezing problems with
5738 -- the added entities.
fbf5a39b
AC
5739
5740 if not Is_Private_Type (Full_View (Parent_Type))
5741 and then (In_Open_Scopes (Scope (Parent_Type)))
5742 then
07fc65c4
GB
5743 Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
5744 Chars (Derived_Type));
5745 Set_Is_Itype (Full_Der);
5746 Set_Has_Private_Declaration (Full_Der);
5747 Set_Has_Private_Declaration (Derived_Type);
5748 Set_Associated_Node_For_Itype (Full_Der, N);
5749 Set_Parent (Full_Der, Parent (Derived_Type));
5750 Set_Full_View (Derived_Type, Full_Der);
fbf5a39b 5751 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
07fc65c4
GB
5752 Full_P := Full_View (Parent_Type);
5753 Exchange_Declarations (Parent_Type);
5754 Copy_And_Build;
5755 Exchange_Declarations (Full_P);
996ae0b0 5756
07fc65c4
GB
5757 else
5758 Build_Derived_Record_Type
5759 (N, Full_View (Parent_Type), Derived_Type,
5760 Derive_Subps => False);
5761 end if;
996ae0b0 5762
c206e8fd
AC
5763 -- In any case, the primitive operations are inherited from the
5764 -- parent type, not from the internal full view.
996ae0b0 5765
996ae0b0
RK
5766 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
5767
5768 if Derive_Subps then
5769 Derive_Subprograms (Parent_Type, Derived_Type);
5770 end if;
5771
5772 else
07fc65c4 5773 -- Untagged type, No discriminants on either view
996ae0b0 5774
71d9e9f2
ES
5775 if Nkind (Subtype_Indication (Type_Definition (N))) =
5776 N_Subtype_Indication
996ae0b0
RK
5777 then
5778 Error_Msg_N
5779 ("illegal constraint on type without discriminants", N);
5780 end if;
5781
5782 if Present (Discriminant_Specifications (N))
5783 and then Present (Full_View (Parent_Type))
5784 and then not Is_Tagged_Type (Full_View (Parent_Type))
5785 then
c206e8fd 5786 Error_Msg_N ("cannot add discriminants to untagged type", N);
996ae0b0
RK
5787 end if;
5788
fbf5a39b 5789 Set_Stored_Constraint (Derived_Type, No_Elist);
07fc65c4
GB
5790 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5791 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
5792 Set_Has_Controlled_Component
5793 (Derived_Type, Has_Controlled_Component
5794 (Parent_Type));
996ae0b0 5795
07fc65c4 5796 -- Direct controlled types do not inherit Finalize_Storage_Only flag
996ae0b0
RK
5797
5798 if not Is_Controlled (Parent_Type) then
07fc65c4
GB
5799 Set_Finalize_Storage_Only
5800 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
996ae0b0
RK
5801 end if;
5802
c206e8fd
AC
5803 -- Construct the implicit full view by deriving from full view of the
5804 -- parent type. In order to get proper visibility, we install the
5805 -- parent scope and its declarations.
996ae0b0 5806
c206e8fd
AC
5807 -- ??? If the parent is untagged private and its completion is
5808 -- tagged, this mechanism will not work because we cannot derive from
5809 -- the tagged full view unless we have an extension.
996ae0b0
RK
5810
5811 if Present (Full_View (Parent_Type))
5812 and then not Is_Tagged_Type (Full_View (Parent_Type))
5813 and then not Is_Completion
5814 then
71d9e9f2
ES
5815 Full_Der :=
5816 Make_Defining_Identifier (Sloc (Derived_Type),
5817 Chars => Chars (Derived_Type));
996ae0b0
RK
5818 Set_Is_Itype (Full_Der);
5819 Set_Has_Private_Declaration (Full_Der);
5820 Set_Has_Private_Declaration (Derived_Type);
5821 Set_Associated_Node_For_Itype (Full_Der, N);
5822 Set_Parent (Full_Der, Parent (Derived_Type));
5823 Set_Full_View (Derived_Type, Full_Der);
5824
5825 if not In_Open_Scopes (Par_Scope) then
5826 Install_Private_Declarations (Par_Scope);
5827 Install_Visible_Declarations (Par_Scope);
5828 Copy_And_Build;
5829 Uninstall_Declarations (Par_Scope);
5830
a5b62485
AC
5831 -- If parent scope is open and in another unit, and parent has a
5832 -- completion, then the derivation is taking place in the visible
5833 -- part of a child unit. In that case retrieve the full view of
5834 -- the parent momentarily.
996ae0b0
RK
5835
5836 elsif not In_Same_Source_Unit (N, Parent_Type) then
5837 Full_P := Full_View (Parent_Type);
5838 Exchange_Declarations (Parent_Type);
5839 Copy_And_Build;
5840 Exchange_Declarations (Full_P);
5841
ffe9aba8 5842 -- Otherwise it is a local derivation
996ae0b0
RK
5843
5844 else
5845 Copy_And_Build;
5846 end if;
5847
5848 Set_Scope (Full_Der, Current_Scope);
5849 Set_Is_First_Subtype (Full_Der,
5850 Is_First_Subtype (Derived_Type));
5851 Set_Has_Size_Clause (Full_Der, False);
5852 Set_Has_Alignment_Clause (Full_Der, False);
5853 Set_Next_Entity (Full_Der, Empty);
5854 Set_Has_Delayed_Freeze (Full_Der);
5855 Set_Is_Frozen (Full_Der, False);
5856 Set_Freeze_Node (Full_Der, Empty);
5857 Set_Depends_On_Private (Full_Der,
c206e8fd 5858 Has_Private_Component (Full_Der));
f91b40db 5859 Set_Public_Status (Full_Der);
996ae0b0
RK
5860 end if;
5861 end if;
5862
5863 Set_Has_Unknown_Discriminants (Derived_Type,
5864 Has_Unknown_Discriminants (Parent_Type));
5865
5866 if Is_Private_Type (Derived_Type) then
5867 Set_Private_Dependents (Derived_Type, New_Elmt_List);
5868 end if;
5869
5870 if Is_Private_Type (Parent_Type)
5871 and then Base_Type (Parent_Type) = Parent_Type
5872 and then In_Open_Scopes (Scope (Parent_Type))
5873 then
5874 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
5875
5876 if Is_Child_Unit (Scope (Current_Scope))
5877 and then Is_Completion
5878 and then In_Private_Part (Current_Scope)
3a77b68d 5879 and then Scope (Parent_Type) /= Current_Scope
996ae0b0
RK
5880 then
5881 -- This is the unusual case where a type completed by a private
c206e8fd
AC
5882 -- derivation occurs within a package nested in a child unit, and
5883 -- the parent is declared in an ancestor. In this case, the full
e34ca162
AC
5884 -- view of the parent type will become visible in the body of
5885 -- the enclosing child, and only then will the current type be
5886 -- possibly non-private. We build a underlying full view that
996ae0b0
RK
5887 -- will be installed when the enclosing child body is compiled.
5888
fea9e956
ES
5889 Full_Der :=
5890 Make_Defining_Identifier (Sloc (Derived_Type),
5891 Chars => Chars (Derived_Type));
5892 Set_Is_Itype (Full_Der);
5893 Build_Itype_Reference (Full_Der, N);
996ae0b0 5894
fea9e956
ES
5895 -- The full view will be used to swap entities on entry/exit to
5896 -- the body, and must appear in the entity list for the package.
5897
5898 Append_Entity (Full_Der, Scope (Derived_Type));
5899 Set_Has_Private_Declaration (Full_Der);
5900 Set_Has_Private_Declaration (Derived_Type);
5901 Set_Associated_Node_For_Itype (Full_Der, N);
5902 Set_Parent (Full_Der, Parent (Derived_Type));
5903 Full_P := Full_View (Parent_Type);
5904 Exchange_Declarations (Parent_Type);
5905 Copy_And_Build;
5906 Exchange_Declarations (Full_P);
5907 Set_Underlying_Full_View (Derived_Type, Full_Der);
996ae0b0
RK
5908 end if;
5909 end if;
5910 end Build_Derived_Private_Type;
5911
5912 -------------------------------
5913 -- Build_Derived_Record_Type --
5914 -------------------------------
5915
71d9e9f2 5916 -- 1. INTRODUCTION
996ae0b0
RK
5917
5918 -- Ideally we would like to use the same model of type derivation for
5919 -- tagged and untagged record types. Unfortunately this is not quite
5920 -- possible because the semantics of representation clauses is different
5921 -- for tagged and untagged records under inheritance. Consider the
5922 -- following:
5923
5924 -- type R (...) is [tagged] record ... end record;
5925 -- type T (...) is new R (...) [with ...];
5926
fea9e956
ES
5927 -- The representation clauses for T can specify a completely different
5928 -- record layout from R's. Hence the same component can be placed in two
fdac1f80
AC
5929 -- very different positions in objects of type T and R. If R and T are
5930 -- tagged types, representation clauses for T can only specify the layout
5931 -- of non inherited components, thus components that are common in R and T
5932 -- have the same position in objects of type R and T.
996ae0b0
RK
5933
5934 -- This has two implications. The first is that the entire tree for R's
a5b62485
AC
5935 -- declaration needs to be copied for T in the untagged case, so that T
5936 -- can be viewed as a record type of its own with its own representation
996ae0b0
RK
5937 -- clauses. The second implication is the way we handle discriminants.
5938 -- Specifically, in the untagged case we need a way to communicate to Gigi
5939 -- what are the real discriminants in the record, while for the semantics
5940 -- we need to consider those introduced by the user to rename the
5941 -- discriminants in the parent type. This is handled by introducing the
fbf5a39b 5942 -- notion of stored discriminants. See below for more.
996ae0b0
RK
5943
5944 -- Fortunately the way regular components are inherited can be handled in
5945 -- the same way in tagged and untagged types.
5946
5947 -- To complicate things a bit more the private view of a private extension
5948 -- cannot be handled in the same way as the full view (for one thing the
5949 -- semantic rules are somewhat different). We will explain what differs
5950 -- below.
5951
71d9e9f2 5952 -- 2. DISCRIMINANTS UNDER INHERITANCE
996ae0b0
RK
5953
5954 -- The semantic rules governing the discriminants of derived types are
5955 -- quite subtle.
5956
5957 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
88b32fc3 5958 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
996ae0b0
RK
5959
5960 -- If parent type has discriminants, then the discriminants that are
5961 -- declared in the derived type are [3.4 (11)]:
5962
5963 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
5964 -- there is one;
5965
a5b62485
AC
5966 -- o Otherwise, each discriminant of the parent type (implicitly declared
5967 -- in the same order with the same specifications). In this case, the
5968 -- discriminants are said to be "inherited", or if unknown in the parent
5969 -- are also unknown in the derived type.
996ae0b0
RK
5970
5971 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
5972
5973 -- o The parent subtype shall be constrained;
5974
5975 -- o If the parent type is not a tagged type, then each discriminant of
5976 -- the derived type shall be used in the constraint defining a parent
88b32fc3
BD
5977 -- subtype. [Implementation note: This ensures that the new discriminant
5978 -- can share storage with an existing discriminant.]
996ae0b0
RK
5979
5980 -- For the derived type each discriminant of the parent type is either
5981 -- inherited, constrained to equal some new discriminant of the derived
5982 -- type, or constrained to the value of an expression.
5983
5984 -- When inherited or constrained to equal some new discriminant, the
5985 -- parent discriminant and the discriminant of the derived type are said
5986 -- to "correspond".
5987
5988 -- If a discriminant of the parent type is constrained to a specific value
5989 -- in the derived type definition, then the discriminant is said to be
5990 -- "specified" by that derived type definition.
5991
ffe9aba8 5992 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
996ae0b0 5993
fbf5a39b
AC
5994 -- We have spoken about stored discriminants in point 1 (introduction)
5995 -- above. There are two sort of stored discriminants: implicit and
996ae0b0 5996 -- explicit. As long as the derived type inherits the same discriminants as
fbf5a39b 5997 -- the root record type, stored discriminants are the same as regular
996ae0b0
RK
5998 -- discriminants, and are said to be implicit. However, if any discriminant
5999 -- in the root type was renamed in the derived type, then the derived
fbf5a39b 6000 -- type will contain explicit stored discriminants. Explicit stored
996ae0b0 6001 -- discriminants are discriminants in addition to the semantically visible
fbf5a39b 6002 -- discriminants defined for the derived type. Stored discriminants are
996ae0b0
RK
6003 -- used by Gigi to figure out what are the physical discriminants in
6004 -- objects of the derived type (see precise definition in einfo.ads).
6005 -- As an example, consider the following:
6006
6007 -- type R (D1, D2, D3 : Int) is record ... end record;
6008 -- type T1 is new R;
6009 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
6010 -- type T3 is new T2;
6011 -- type T4 (Y : Int) is new T3 (Y, 99);
6012
fbf5a39b 6013 -- The following table summarizes the discriminants and stored
996ae0b0
RK
6014 -- discriminants in R and T1 through T4.
6015
fbf5a39b 6016 -- Type Discrim Stored Discrim Comment
30c20106
AC
6017 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
6018 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
6019 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
6020 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
6021 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
6022
6023 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
6024 -- find the corresponding discriminant in the parent type, while
996ae0b0
RK
6025 -- Original_Record_Component (abbreviated ORC below), the actual physical
6026 -- component that is renamed. Finally the field Is_Completely_Hidden
fbf5a39b 6027 -- (abbreviated ICH below) is set for all explicit stored discriminants
996ae0b0
RK
6028 -- (see einfo.ads for more info). For the above example this gives:
6029
6030 -- Discrim CD ORC ICH
6031 -- ^^^^^^^ ^^ ^^^ ^^^
6032 -- D1 in R empty itself no
6033 -- D2 in R empty itself no
6034 -- D3 in R empty itself no
6035
6036 -- D1 in T1 D1 in R itself no
6037 -- D2 in T1 D2 in R itself no
6038 -- D3 in T1 D3 in R itself no
6039
6040 -- X1 in T2 D3 in T1 D3 in T2 no
6041 -- X2 in T2 D1 in T1 D1 in T2 no
6042 -- D1 in T2 empty itself yes
6043 -- D2 in T2 empty itself yes
6044 -- D3 in T2 empty itself yes
6045
6046 -- X1 in T3 X1 in T2 D3 in T3 no
6047 -- X2 in T3 X2 in T2 D1 in T3 no
6048 -- D1 in T3 empty itself yes
6049 -- D2 in T3 empty itself yes
6050 -- D3 in T3 empty itself yes
6051
6052 -- Y in T4 X1 in T3 D3 in T3 no
6053 -- D1 in T3 empty itself yes
6054 -- D2 in T3 empty itself yes
6055 -- D3 in T3 empty itself yes
6056
71d9e9f2 6057 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
996ae0b0 6058
88b32fc3 6059 -- Type derivation for tagged types is fairly straightforward. If no
996ae0b0 6060 -- discriminants are specified by the derived type, these are inherited
fbf5a39b 6061 -- from the parent. No explicit stored discriminants are ever necessary.
996ae0b0
RK
6062 -- The only manipulation that is done to the tree is that of adding a
6063 -- _parent field with parent type and constrained to the same constraint
6064 -- specified for the parent in the derived type definition. For instance:
6065
6066 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
6067 -- type T1 is new R with null record;
6068 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
6069
71d9e9f2 6070 -- are changed into:
996ae0b0
RK
6071
6072 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
6073 -- _parent : R (D1, D2, D3);
6074 -- end record;
6075
6076 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
6077 -- _parent : T1 (X2, 88, X1);
6078 -- end record;
6079
6080 -- The discriminants actually present in R, T1 and T2 as well as their CD,
6081 -- ORC and ICH fields are:
6082
6083 -- Discrim CD ORC ICH
6084 -- ^^^^^^^ ^^ ^^^ ^^^
6085 -- D1 in R empty itself no
6086 -- D2 in R empty itself no
6087 -- D3 in R empty itself no
6088
6089 -- D1 in T1 D1 in R D1 in R no
6090 -- D2 in T1 D2 in R D2 in R no
6091 -- D3 in T1 D3 in R D3 in R no
6092
6093 -- X1 in T2 D3 in T1 D3 in R no
6094 -- X2 in T2 D1 in T1 D1 in R no
6095
71d9e9f2 6096 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
996ae0b0
RK
6097 --
6098 -- Regardless of whether we dealing with a tagged or untagged type
6099 -- we will transform all derived type declarations of the form
6100 --
6101 -- type T is new R (...) [with ...];
6102 -- or
6103 -- subtype S is R (...);
6104 -- type T is new S [with ...];
6105 -- into
6106 -- type BT is new R [with ...];
6107 -- subtype T is BT (...);
6108 --
6109 -- That is, the base derived type is constrained only if it has no
6110 -- discriminants. The reason for doing this is that GNAT's semantic model
6111 -- assumes that a base type with discriminants is unconstrained.
6112 --
6113 -- Note that, strictly speaking, the above transformation is not always
fbf5a39b 6114 -- correct. Consider for instance the following excerpt from ACVC b34011a:
996ae0b0
RK
6115 --
6116 -- procedure B34011A is
6117 -- type REC (D : integer := 0) is record
6118 -- I : Integer;
6119 -- end record;
6120
6121 -- package P is
6122 -- type T6 is new Rec;
6123 -- function F return T6;
6124 -- end P;
6125
6126 -- use P;
6127 -- package Q6 is
6128 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
6129 -- end Q6;
6130 --
6131 -- The definition of Q6.U is illegal. However transforming Q6.U into
6132
6133 -- type BaseU is new T6;
6134 -- subtype U is BaseU (Q6.F.I)
6135
6136 -- turns U into a legal subtype, which is incorrect. To avoid this problem
6137 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
6138 -- the transformation described above.
6139
6140 -- There is another instance where the above transformation is incorrect.
6141 -- Consider:
6142
6143 -- package Pack is
6144 -- type Base (D : Integer) is tagged null record;
6145 -- procedure P (X : Base);
6146
6147 -- type Der is new Base (2) with null record;
6148 -- procedure P (X : Der);
6149 -- end Pack;
6150
6151 -- Then the above transformation turns this into
6152
6153 -- type Der_Base is new Base with null record;
44d6a706 6154 -- -- procedure P (X : Base) is implicitly inherited here
996ae0b0
RK
6155 -- -- as procedure P (X : Der_Base).
6156
6157 -- subtype Der is Der_Base (2);
6158 -- procedure P (X : Der);
6159 -- -- The overriding of P (X : Der_Base) is illegal since we
6160 -- -- have a parameter conformance problem.
6161
6162 -- To get around this problem, after having semantically processed Der_Base
6163 -- and the rewritten subtype declaration for Der, we copy Der_Base field
6164 -- Discriminant_Constraint from Der so that when parameter conformance is
fbf5a39b 6165 -- checked when P is overridden, no semantic errors are flagged.
996ae0b0 6166
ffe9aba8 6167 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
996ae0b0 6168
fbf5a39b 6169 -- Regardless of whether we are dealing with a tagged or untagged type
996ae0b0
RK
6170 -- we will transform all derived type declarations of the form
6171
6172 -- type R (D1, .., Dn : ...) is [tagged] record ...;
6173 -- type T is new R [with ...];
6174 -- into
6175 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6176
6177 -- The reason for such transformation is that it allows us to implement a
6178 -- very clean form of component inheritance as explained below.
6179
6180 -- Note that this transformation is not achieved by direct tree rewriting
6181 -- and manipulation, but rather by redoing the semantic actions that the
6182 -- above transformation will entail. This is done directly in routine
6183 -- Inherit_Components.
6184
71d9e9f2 6185 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
996ae0b0
RK
6186
6187 -- In both tagged and untagged derived types, regular non discriminant
6188 -- components are inherited in the derived type from the parent type. In
6189 -- the absence of discriminants component, inheritance is straightforward
6190 -- as components can simply be copied from the parent.
a5b62485 6191
996ae0b0
RK
6192 -- If the parent has discriminants, inheriting components constrained with
6193 -- these discriminants requires caution. Consider the following example:
6194
6195 -- type R (D1, D2 : Positive) is [tagged] record
6196 -- S : String (D1 .. D2);
6197 -- end record;
6198
6199 -- type T1 is new R [with null record];
6200 -- type T2 (X : positive) is new R (1, X) [with null record];
6201
6202 -- As explained in 6. above, T1 is rewritten as
996ae0b0 6203 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
996ae0b0
RK
6204 -- which makes the treatment for T1 and T2 identical.
6205
6206 -- What we want when inheriting S, is that references to D1 and D2 in R are
f3d57416 6207 -- replaced with references to their correct constraints, i.e. D1 and D2 in
996ae0b0
RK
6208 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
6209 -- with either discriminant references in the derived type or expressions.
fbf5a39b 6210 -- This replacement is achieved as follows: before inheriting R's
996ae0b0
RK
6211 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6212 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
6213 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6214 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
6215 -- by String (1 .. X).
6216
71d9e9f2 6217 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
996ae0b0
RK
6218
6219 -- We explain here the rules governing private type extensions relevant to
6220 -- type derivation. These rules are explained on the following example:
6221
6222 -- type D [(...)] is new A [(...)] with private; <-- partial view
6223 -- type D [(...)] is new P [(...)] with null record; <-- full view
6224
6225 -- Type A is called the ancestor subtype of the private extension.
6226 -- Type P is the parent type of the full view of the private extension. It
6227 -- must be A or a type derived from A.
6228
6229 -- The rules concerning the discriminants of private type extensions are
6230 -- [7.3(10-13)]:
6231
6232 -- o If a private extension inherits known discriminants from the ancestor
6233 -- subtype, then the full view shall also inherit its discriminants from
6234 -- the ancestor subtype and the parent subtype of the full view shall be
6235 -- constrained if and only if the ancestor subtype is constrained.
6236
6237 -- o If a partial view has unknown discriminants, then the full view may
6238 -- define a definite or an indefinite subtype, with or without
6239 -- discriminants.
6240
6241 -- o If a partial view has neither known nor unknown discriminants, then
6242 -- the full view shall define a definite subtype.
6243
6244 -- o If the ancestor subtype of a private extension has constrained
fbf5a39b 6245 -- discriminants, then the parent subtype of the full view shall impose a
996ae0b0
RK
6246 -- statically matching constraint on those discriminants.
6247
6248 -- This means that only the following forms of private extensions are
6249 -- allowed:
6250
6251 -- type D is new A with private; <-- partial view
6252 -- type D is new P with null record; <-- full view
6253
6254 -- If A has no discriminants than P has no discriminants, otherwise P must
6255 -- inherit A's discriminants.
6256
6257 -- type D is new A (...) with private; <-- partial view
6258 -- type D is new P (:::) with null record; <-- full view
6259
6260 -- P must inherit A's discriminants and (...) and (:::) must statically
6261 -- match.
6262
6263 -- subtype A is R (...);
6264 -- type D is new A with private; <-- partial view
6265 -- type D is new P with null record; <-- full view
6266
6267 -- P must have inherited R's discriminants and must be derived from A or
6268 -- any of its subtypes.
6269
6270 -- type D (..) is new A with private; <-- partial view
6271 -- type D (..) is new P [(:::)] with null record; <-- full view
6272
6273 -- No specific constraints on P's discriminants or constraint (:::).
6274 -- Note that A can be unconstrained, but the parent subtype P must either
6275 -- be constrained or (:::) must be present.
6276
6277 -- type D (..) is new A [(...)] with private; <-- partial view
6278 -- type D (..) is new P [(:::)] with null record; <-- full view
6279
6280 -- P's constraints on A's discriminants must statically match those
6281 -- imposed by (...).
6282
71d9e9f2 6283 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
996ae0b0
RK
6284
6285 -- The full view of a private extension is handled exactly as described
a5b62485 6286 -- above. The model chose for the private view of a private extension is
f3d57416 6287 -- the same for what concerns discriminants (i.e. they receive the same
996ae0b0
RK
6288 -- treatment as in the tagged case). However, the private view of the
6289 -- private extension always inherits the components of the parent base,
a5b62485
AC
6290 -- without replacing any discriminant reference. Strictly speaking this is
6291 -- incorrect. However, Gigi never uses this view to generate code so this
6292 -- is a purely semantic issue. In theory, a set of transformations similar
6293 -- to those given in 5. and 6. above could be applied to private views of
6294 -- private extensions to have the same model of component inheritance as
6295 -- for non private extensions. However, this is not done because it would
6296 -- further complicate private type processing. Semantically speaking, this
6297 -- leaves us in an uncomfortable situation. As an example consider:
996ae0b0
RK
6298
6299 -- package Pack is
6300 -- type R (D : integer) is tagged record
6301 -- S : String (1 .. D);
6302 -- end record;
6303 -- procedure P (X : R);
6304 -- type T is new R (1) with private;
6305 -- private
6306 -- type T is new R (1) with null record;
6307 -- end;
6308
6309 -- This is transformed into:
6310
6311 -- package Pack is
6312 -- type R (D : integer) is tagged record
6313 -- S : String (1 .. D);
6314 -- end record;
6315 -- procedure P (X : R);
6316 -- type T is new R (1) with private;
6317 -- private
6318 -- type BaseT is new R with null record;
6319 -- subtype T is BaseT (1);
6320 -- end;
6321
ffe9aba8 6322 -- (strictly speaking the above is incorrect Ada)
996ae0b0
RK
6323
6324 -- From the semantic standpoint the private view of private extension T
6325 -- should be flagged as constrained since one can clearly have
6326 --
6327 -- Obj : T;
6328 --
6329 -- in a unit withing Pack. However, when deriving subprograms for the
6330 -- private view of private extension T, T must be seen as unconstrained
6331 -- since T has discriminants (this is a constraint of the current
6332 -- subprogram derivation model). Thus, when processing the private view of
6333 -- a private extension such as T, we first mark T as unconstrained, we
6334 -- process it, we perform program derivation and just before returning from
6335 -- Build_Derived_Record_Type we mark T as constrained.
a5b62485 6336
fbf5a39b 6337 -- ??? Are there are other uncomfortable cases that we will have to
996ae0b0
RK
6338 -- deal with.
6339
71d9e9f2 6340 -- 10. RECORD_TYPE_WITH_PRIVATE complications
996ae0b0
RK
6341
6342 -- Types that are derived from a visible record type and have a private
6343 -- extension present other peculiarities. They behave mostly like private
6344 -- types, but if they have primitive operations defined, these will not
6345 -- have the proper signatures for further inheritance, because other
6346 -- primitive operations will use the implicit base that we define for
6347 -- private derivations below. This affect subprogram inheritance (see
6348 -- Derive_Subprograms for details). We also derive the implicit base from
6349 -- the base type of the full view, so that the implicit base is a record
6350 -- type and not another private type, This avoids infinite loops.
6351
6352 procedure Build_Derived_Record_Type
6353 (N : Node_Id;
6354 Parent_Type : Entity_Id;
6355 Derived_Type : Entity_Id;
6356 Derive_Subps : Boolean := True)
6357 is
6358 Loc : constant Source_Ptr := Sloc (N);
6359 Parent_Base : Entity_Id;
996ae0b0
RK
6360 Type_Def : Node_Id;
6361 Indic : Node_Id;
996ae0b0
RK
6362 Discrim : Entity_Id;
6363 Last_Discrim : Entity_Id;
6364 Constrs : Elist_Id;
71d9e9f2 6365
c6fe3827 6366 Discs : Elist_Id := New_Elmt_List;
996ae0b0
RK
6367 -- An empty Discs list means that there were no constraints in the
6368 -- subtype indication or that there was an error processing it.
6369
c6fe3827
GD
6370 Assoc_List : Elist_Id;
6371 New_Discrs : Elist_Id;
6372 New_Base : Entity_Id;
6373 New_Decl : Node_Id;
6374 New_Indic : Node_Id;
996ae0b0
RK
6375
6376 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
07fc65c4
GB
6377 Discriminant_Specs : constant Boolean :=
6378 Present (Discriminant_Specifications (N));
6379 Private_Extension : constant Boolean :=
7d7af38a 6380 Nkind (N) = N_Private_Extension_Declaration;
996ae0b0 6381
c6fe3827
GD
6382 Constraint_Present : Boolean;
6383 Inherit_Discrims : Boolean := False;
6384 Save_Etype : Entity_Id;
6385 Save_Discr_Constr : Elist_Id;
6386 Save_Next_Entity : Entity_Id;
996ae0b0
RK
6387
6388 begin
6389 if Ekind (Parent_Type) = E_Record_Type_With_Private
6390 and then Present (Full_View (Parent_Type))
6391 and then Has_Discriminants (Parent_Type)
6392 then
6393 Parent_Base := Base_Type (Full_View (Parent_Type));
6394 else
6395 Parent_Base := Base_Type (Parent_Type);
6396 end if;
6397
6398 -- Before we start the previously documented transformations, here is
fea9e956
ES
6399 -- little fix for size and alignment of tagged types. Normally when we
6400 -- derive type D from type P, we copy the size and alignment of P as the
6401 -- default for D, and in the absence of explicit representation clauses
6402 -- for D, the size and alignment are indeed the same as the parent.
6403
6404 -- But this is wrong for tagged types, since fields may be added, and
6405 -- the default size may need to be larger, and the default alignment may
6406 -- need to be larger.
996ae0b0 6407
fea9e956
ES
6408 -- We therefore reset the size and alignment fields in the tagged case.
6409 -- Note that the size and alignment will in any case be at least as
6410 -- large as the parent type (since the derived type has a copy of the
6411 -- parent type in the _parent field)
996ae0b0 6412
fea9e956
ES
6413 -- The type is also marked as being tagged here, which is needed when
6414 -- processing components with a self-referential anonymous access type
6415 -- in the call to Check_Anonymous_Access_Components below. Note that
6416 -- this flag is also set later on for completeness.
996ae0b0
RK
6417
6418 if Is_Tagged then
fea9e956
ES
6419 Set_Is_Tagged_Type (Derived_Type);
6420 Init_Size_Align (Derived_Type);
996ae0b0
RK
6421 end if;
6422
71d9e9f2 6423 -- STEP 0a: figure out what kind of derived type declaration we have
996ae0b0
RK
6424
6425 if Private_Extension then
6426 Type_Def := N;
6427 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6428
6429 else
6430 Type_Def := Type_Definition (N);
6431
c6fe3827 6432 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
996ae0b0
RK
6433 -- Parent_Base can be a private type or private extension. However,
6434 -- for tagged types with an extension the newly added fields are
6435 -- visible and hence the Derived_Type is always an E_Record_Type.
6436 -- (except that the parent may have its own private fields).
6437 -- For untagged types we preserve the Ekind of the Parent_Base.
6438
6439 if Present (Record_Extension_Part (Type_Def)) then
6440 Set_Ekind (Derived_Type, E_Record_Type);
fea9e956
ES
6441
6442 -- Create internal access types for components with anonymous
6443 -- access types.
6444
6445 if Ada_Version >= Ada_05 then
6446 Check_Anonymous_Access_Components
6447 (N, Derived_Type, Derived_Type,
6448 Component_List (Record_Extension_Part (Type_Def)));
6449 end if;
6450
996ae0b0
RK
6451 else
6452 Set_Ekind (Derived_Type, Ekind (Parent_Base));
6453 end if;
6454 end if;
6455
6456 -- Indic can either be an N_Identifier if the subtype indication
6457 -- contains no constraint or an N_Subtype_Indication if the subtype
6458 -- indication has a constraint.
6459
6460 Indic := Subtype_Indication (Type_Def);
6461 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6462
8a6a52dc
AC
6463 -- Check that the type has visible discriminants. The type may be
6464 -- a private type with unknown discriminants whose full view has
6465 -- discriminants which are invisible.
6466
996ae0b0 6467 if Constraint_Present then
8a6a52dc
AC
6468 if not Has_Discriminants (Parent_Base)
6469 or else
6470 (Has_Unknown_Discriminants (Parent_Base)
6471 and then Is_Private_Type (Parent_Base))
6472 then
996ae0b0
RK
6473 Error_Msg_N
6474 ("invalid constraint: type has no discriminant",
6475 Constraint (Indic));
6476
6477 Constraint_Present := False;
6478 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6479
6480 elsif Is_Constrained (Parent_Type) then
6481 Error_Msg_N
6482 ("invalid constraint: parent type is already constrained",
6483 Constraint (Indic));
6484
6485 Constraint_Present := False;
6486 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6487 end if;
6488 end if;
6489
71d9e9f2 6490 -- STEP 0b: If needed, apply transformation given in point 5. above
996ae0b0
RK
6491
6492 if not Private_Extension
6493 and then Has_Discriminants (Parent_Type)
6494 and then not Discriminant_Specs
6495 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
6496 then
ffe9aba8 6497 -- First, we must analyze the constraint (see comment in point 5.)
996ae0b0
RK
6498
6499 if Constraint_Present then
6500 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
6501
6502 if Has_Discriminants (Derived_Type)
6503 and then Has_Private_Declaration (Derived_Type)
6504 and then Present (Discriminant_Constraint (Derived_Type))
6505 then
ea0a7f39
ST
6506 -- Verify that constraints of the full view statically match
6507 -- those given in the partial view.
996ae0b0
RK
6508
6509 declare
6510 C1, C2 : Elmt_Id;
6511
6512 begin
6513 C1 := First_Elmt (New_Discrs);
6514 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
996ae0b0 6515 while Present (C1) and then Present (C2) loop
ea0a7f39
ST
6516 if Fully_Conformant_Expressions (Node (C1), Node (C2))
6517 or else
10619438
AC
6518 (Is_OK_Static_Expression (Node (C1))
6519 and then
6520 Is_OK_Static_Expression (Node (C2))
6521 and then
6522 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
996ae0b0 6523 then
ea0a7f39
ST
6524 null;
6525
6526 else
996ae0b0
RK
6527 Error_Msg_N (
6528 "constraint not conformant to previous declaration",
6529 Node (C1));
6530 end if;
9dfd2ff8 6531
996ae0b0
RK
6532 Next_Elmt (C1);
6533 Next_Elmt (C2);
6534 end loop;
6535 end;
6536 end if;
6537 end if;
6538
6539 -- Insert and analyze the declaration for the unconstrained base type
6540
6541 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6542
6543 New_Decl :=
6544 Make_Full_Type_Declaration (Loc,
6545 Defining_Identifier => New_Base,
6546 Type_Definition =>
6547 Make_Derived_Type_Definition (Loc,
6548 Abstract_Present => Abstract_Present (Type_Def),
fdac1f80 6549 Limited_Present => Limited_Present (Type_Def),
996ae0b0
RK
6550 Subtype_Indication =>
6551 New_Occurrence_Of (Parent_Base, Loc),
6552 Record_Extension_Part =>
fdac1f80
AC
6553 Relocate_Node (Record_Extension_Part (Type_Def)),
6554 Interface_List => Interface_List (Type_Def)));
996ae0b0
RK
6555
6556 Set_Parent (New_Decl, Parent (N));
6557 Mark_Rewrite_Insertion (New_Decl);
6558 Insert_Before (N, New_Decl);
6559
a5b62485
AC
6560 -- Note that this call passes False for the Derive_Subps parameter
6561 -- because subprogram derivation is deferred until after creating
6562 -- the subtype (see below).
996ae0b0
RK
6563
6564 Build_Derived_Type
6565 (New_Decl, Parent_Base, New_Base,
6566 Is_Completion => True, Derive_Subps => False);
6567
6568 -- ??? This needs re-examination to determine whether the
6569 -- above call can simply be replaced by a call to Analyze.
6570
6571 Set_Analyzed (New_Decl);
6572
6573 -- Insert and analyze the declaration for the constrained subtype
6574
6575 if Constraint_Present then
6576 New_Indic :=
6577 Make_Subtype_Indication (Loc,
6578 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6579 Constraint => Relocate_Node (Constraint (Indic)));
6580
6581 else
6582 declare
fbf5a39b 6583 Constr_List : constant List_Id := New_List;
996ae0b0 6584 C : Elmt_Id;
fbf5a39b 6585 Expr : Node_Id;
996ae0b0
RK
6586
6587 begin
6588 C := First_Elmt (Discriminant_Constraint (Parent_Type));
6589 while Present (C) loop
6590 Expr := Node (C);
6591
6592 -- It is safe here to call New_Copy_Tree since
6593 -- Force_Evaluation was called on each constraint in
6594 -- Build_Discriminant_Constraints.
6595
6596 Append (New_Copy_Tree (Expr), To => Constr_List);
6597
6598 Next_Elmt (C);
6599 end loop;
6600
6601 New_Indic :=
6602 Make_Subtype_Indication (Loc,
6603 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6604 Constraint =>
6605 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
6606 end;
6607 end if;
6608
6609 Rewrite (N,
6610 Make_Subtype_Declaration (Loc,
6611 Defining_Identifier => Derived_Type,
6612 Subtype_Indication => New_Indic));
6613
6614 Analyze (N);
6615
71d9e9f2
ES
6616 -- Derivation of subprograms must be delayed until the full subtype
6617 -- has been established to ensure proper overriding of subprograms
6618 -- inherited by full types. If the derivations occurred as part of
6619 -- the call to Build_Derived_Type above, then the check for type
6620 -- conformance would fail because earlier primitive subprograms
6621 -- could still refer to the full type prior the change to the new
6622 -- subtype and hence would not match the new base type created here.
996ae0b0 6623
59262ebb 6624 Derive_Subprograms (Parent_Type, Derived_Type);
996ae0b0
RK
6625
6626 -- For tagged types the Discriminant_Constraint of the new base itype
6627 -- is inherited from the first subtype so that no subtype conformance
6628 -- problem arise when the first subtype overrides primitive
6629 -- operations inherited by the implicit base type.
6630
6631 if Is_Tagged then
6632 Set_Discriminant_Constraint
6633 (New_Base, Discriminant_Constraint (Derived_Type));
6634 end if;
6635
6636 return;
6637 end if;
6638
6639 -- If we get here Derived_Type will have no discriminants or it will be
6640 -- a discriminated unconstrained base type.
6641
6642 -- STEP 1a: perform preliminary actions/checks for derived tagged types
6643
6644 if Is_Tagged then
71d9e9f2 6645
996ae0b0 6646 -- The parent type is frozen for non-private extensions (RM 13.14(7))
88b32fc3
BD
6647 -- The declaration of a specific descendant of an interface type
6648 -- freezes the interface type (RM 13.14).
996ae0b0 6649
88b32fc3
BD
6650 if not Private_Extension
6651 or else Is_Interface (Parent_Base)
6652 then
996ae0b0
RK
6653 Freeze_Before (N, Parent_Type);
6654 end if;
6655
758c442c
GD
6656 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
6657 -- cannot be declared at a deeper level than its parent type is
6658 -- removed. The check on derivation within a generic body is also
6659 -- relaxed, but there's a restriction that a derived tagged type
6660 -- cannot be declared in a generic body if it's derived directly
6661 -- or indirectly from a formal type of that generic.
6662
6663 if Ada_Version >= Ada_05 then
6664 if Present (Enclosing_Generic_Body (Derived_Type)) then
6665 declare
9dfd2ff8 6666 Ancestor_Type : Entity_Id;
758c442c
GD
6667
6668 begin
6669 -- Check to see if any ancestor of the derived type is a
6670 -- formal type.
6671
9dfd2ff8 6672 Ancestor_Type := Parent_Type;
758c442c
GD
6673 while not Is_Generic_Type (Ancestor_Type)
6674 and then Etype (Ancestor_Type) /= Ancestor_Type
6675 loop
6676 Ancestor_Type := Etype (Ancestor_Type);
6677 end loop;
6678
6679 -- If the derived type does have a formal type as an
6680 -- ancestor, then it's an error if the derived type is
6681 -- declared within the body of the generic unit that
6682 -- declares the formal type in its generic formal part. It's
6683 -- sufficient to check whether the ancestor type is declared
6684 -- inside the same generic body as the derived type (such as
6685 -- within a nested generic spec), in which case the
6686 -- derivation is legal. If the formal type is declared
6687 -- outside of that generic body, then it's guaranteed that
6688 -- the derived type is declared within the generic body of
6689 -- the generic unit declaring the formal type.
6690
6691 if Is_Generic_Type (Ancestor_Type)
6692 and then Enclosing_Generic_Body (Ancestor_Type) /=
6693 Enclosing_Generic_Body (Derived_Type)
6694 then
6695 Error_Msg_NE
6696 ("parent type of& must not be descendant of formal type"
6697 & " of an enclosing generic body",
6698 Indic, Derived_Type);
6699 end if;
6700 end;
6701 end if;
6702
6703 elsif Type_Access_Level (Derived_Type) /=
6704 Type_Access_Level (Parent_Type)
996ae0b0
RK
6705 and then not Is_Generic_Type (Derived_Type)
6706 then
6707 if Is_Controlled (Parent_Type) then
6708 Error_Msg_N
6709 ("controlled type must be declared at the library level",
6710 Indic);
6711 else
6712 Error_Msg_N
6713 ("type extension at deeper accessibility level than parent",
6714 Indic);
6715 end if;
6716
6717 else
6718 declare
6719 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
6720
6721 begin
6722 if Present (GB)
6723 and then GB /= Enclosing_Generic_Body (Parent_Base)
6724 then
fbf5a39b
AC
6725 Error_Msg_NE
6726 ("parent type of& must not be outside generic body"
dc06abec 6727 & " (RM 3.9.1(4))",
fbf5a39b 6728 Indic, Derived_Type);
996ae0b0
RK
6729 end if;
6730 end;
6731 end if;
6732 end if;
6733
758c442c
GD
6734 -- Ada 2005 (AI-251)
6735
6736 if Ada_Version = Ada_05
6737 and then Is_Tagged
6738 then
758c442c
GD
6739 -- "The declaration of a specific descendant of an interface type
6740 -- freezes the interface type" (RM 13.14).
6741
6742 declare
6743 Iface : Node_Id;
6744 begin
6745 if Is_Non_Empty_List (Interface_List (Type_Def)) then
6746 Iface := First (Interface_List (Type_Def));
758c442c
GD
6747 while Present (Iface) loop
6748 Freeze_Before (N, Etype (Iface));
6749 Next (Iface);
6750 end loop;
6751 end if;
6752 end;
6753 end if;
6754
996ae0b0
RK
6755 -- STEP 1b : preliminary cleanup of the full view of private types
6756
6757 -- If the type is already marked as having discriminants, then it's the
6758 -- completion of a private type or private extension and we need to
6759 -- retain the discriminants from the partial view if the current
6760 -- declaration has Discriminant_Specifications so that we can verify
6761 -- conformance. However, we must remove any existing components that
fbf5a39b 6762 -- were inherited from the parent (and attached in Copy_And_Swap)
996ae0b0 6763 -- because the full type inherits all appropriate components anyway, and
71d9e9f2 6764 -- we do not want the partial view's components interfering.
996ae0b0
RK
6765
6766 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
6767 Discrim := First_Discriminant (Derived_Type);
6768 loop
6769 Last_Discrim := Discrim;
6770 Next_Discriminant (Discrim);
6771 exit when No (Discrim);
6772 end loop;
6773
6774 Set_Last_Entity (Derived_Type, Last_Discrim);
6775
6776 -- In all other cases wipe out the list of inherited components (even
6777 -- inherited discriminants), it will be properly rebuilt here.
6778
6779 else
6780 Set_First_Entity (Derived_Type, Empty);
6781 Set_Last_Entity (Derived_Type, Empty);
6782 end if;
6783
6784 -- STEP 1c: Initialize some flags for the Derived_Type
6785
6786 -- The following flags must be initialized here so that
88b32fc3
BD
6787 -- Process_Discriminants can check that discriminants of tagged types do
6788 -- not have a default initial value and that access discriminants are
6789 -- only specified for limited records. For completeness, these flags are
6790 -- also initialized along with all the other flags below.
996ae0b0 6791
88b32fc3
BD
6792 -- AI-419: Limitedness is not inherited from an interface parent, so to
6793 -- be limited in that case the type must be explicitly declared as
dc06abec 6794 -- limited. However, task and protected interfaces are always limited.
653da906 6795
dc06abec
RD
6796 if Limited_Present (Type_Def) then
6797 Set_Is_Limited_Record (Derived_Type);
6798
ce4a6e84
RD
6799 elsif Is_Limited_Record (Parent_Type)
6800 or else (Present (Full_View (Parent_Type))
6801 and then Is_Limited_Record (Full_View (Parent_Type)))
6802 then
dc06abec
RD
6803 if not Is_Interface (Parent_Type)
6804 or else Is_Synchronized_Interface (Parent_Type)
6805 or else Is_Protected_Interface (Parent_Type)
6806 or else Is_Task_Interface (Parent_Type)
6807 then
6808 Set_Is_Limited_Record (Derived_Type);
6809 end if;
6810 end if;
996ae0b0 6811
71d9e9f2 6812 -- STEP 2a: process discriminants of derived type if any
996ae0b0 6813
2b73cf68 6814 Push_Scope (Derived_Type);
996ae0b0
RK
6815
6816 if Discriminant_Specs then
6817 Set_Has_Unknown_Discriminants (Derived_Type, False);
6818
6819 -- The following call initializes fields Has_Discriminants and
6820 -- Discriminant_Constraint, unless we are processing the completion
6821 -- of a private type declaration.
6822
6823 Check_Or_Process_Discriminants (N, Derived_Type);
6824
6825 -- For non-tagged types the constraint on the Parent_Type must be
6826 -- present and is used to rename the discriminants.
6827
6828 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
6829 Error_Msg_N ("untagged parent must have discriminants", Indic);
6830
6831 elsif not Is_Tagged and then not Constraint_Present then
6832 Error_Msg_N
6833 ("discriminant constraint needed for derived untagged records",
6834 Indic);
6835
6836 -- Otherwise the parent subtype must be constrained unless we have a
6837 -- private extension.
6838
6839 elsif not Constraint_Present
6840 and then not Private_Extension
6841 and then not Is_Constrained (Parent_Type)
6842 then
6843 Error_Msg_N
6844 ("unconstrained type not allowed in this context", Indic);
6845
6846 elsif Constraint_Present then
6847 -- The following call sets the field Corresponding_Discriminant
6848 -- for the discriminants in the Derived_Type.
6849
6850 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
6851
6852 -- For untagged types all new discriminants must rename
6853 -- discriminants in the parent. For private extensions new
6854 -- discriminants cannot rename old ones (implied by [7.3(13)]).
6855
6856 Discrim := First_Discriminant (Derived_Type);
996ae0b0
RK
6857 while Present (Discrim) loop
6858 if not Is_Tagged
57193e09 6859 and then No (Corresponding_Discriminant (Discrim))
996ae0b0
RK
6860 then
6861 Error_Msg_N
6862 ("new discriminants must constrain old ones", Discrim);
6863
6864 elsif Private_Extension
6865 and then Present (Corresponding_Discriminant (Discrim))
6866 then
6867 Error_Msg_N
fbf5a39b 6868 ("only static constraints allowed for parent"
996ae0b0 6869 & " discriminants in the partial view", Indic);
996ae0b0
RK
6870 exit;
6871 end if;
6872
a5b62485
AC
6873 -- If a new discriminant is used in the constraint, then its
6874 -- subtype must be statically compatible with the parent
6875 -- discriminant's subtype (3.7(15)).
996ae0b0
RK
6876
6877 if Present (Corresponding_Discriminant (Discrim))
6878 and then
6879 not Subtypes_Statically_Compatible
6880 (Etype (Discrim),
6881 Etype (Corresponding_Discriminant (Discrim)))
6882 then
6883 Error_Msg_N
6884 ("subtype must be compatible with parent discriminant",
6885 Discrim);
6886 end if;
6887
6888 Next_Discriminant (Discrim);
6889 end loop;
0da2c8ac
AC
6890
6891 -- Check whether the constraints of the full view statically
6892 -- match those imposed by the parent subtype [7.3(13)].
6893
6894 if Present (Stored_Constraint (Derived_Type)) then
6895 declare
6896 C1, C2 : Elmt_Id;
6897
6898 begin
6899 C1 := First_Elmt (Discs);
6900 C2 := First_Elmt (Stored_Constraint (Derived_Type));
6901 while Present (C1) and then Present (C2) loop
6902 if not
6903 Fully_Conformant_Expressions (Node (C1), Node (C2))
6904 then
88b32fc3
BD
6905 Error_Msg_N
6906 ("not conformant with previous declaration",
6907 Node (C1));
0da2c8ac
AC
6908 end if;
6909
6910 Next_Elmt (C1);
6911 Next_Elmt (C2);
6912 end loop;
6913 end;
6914 end if;
996ae0b0
RK
6915 end if;
6916
6917 -- STEP 2b: No new discriminants, inherit discriminants if any
6918
6919 else
6920 if Private_Extension then
6921 Set_Has_Unknown_Discriminants
0da2c8ac
AC
6922 (Derived_Type,
6923 Has_Unknown_Discriminants (Parent_Type)
6924 or else Unknown_Discriminants_Present (N));
8a6a52dc
AC
6925
6926 -- The partial view of the parent may have unknown discriminants,
6927 -- but if the full view has discriminants and the parent type is
6928 -- in scope they must be inherited.
6929
6930 elsif Has_Unknown_Discriminants (Parent_Type)
6931 and then
6932 (not Has_Discriminants (Parent_Type)
6933 or else not In_Open_Scopes (Scope (Parent_Type)))
6934 then
6935 Set_Has_Unknown_Discriminants (Derived_Type);
996ae0b0
RK
6936 end if;
6937
6938 if not Has_Unknown_Discriminants (Derived_Type)
ffe9aba8 6939 and then not Has_Unknown_Discriminants (Parent_Base)
996ae0b0
RK
6940 and then Has_Discriminants (Parent_Type)
6941 then
6942 Inherit_Discrims := True;
6943 Set_Has_Discriminants
6944 (Derived_Type, True);
6945 Set_Discriminant_Constraint
6946 (Derived_Type, Discriminant_Constraint (Parent_Base));
6947 end if;
6948
6949 -- The following test is true for private types (remember
6950 -- transformation 5. is not applied to those) and in an error
6951 -- situation.
6952
6953 if Constraint_Present then
6954 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
6955 end if;
6956
fbf5a39b 6957 -- For now mark a new derived type as constrained only if it has no
996ae0b0
RK
6958 -- discriminants. At the end of Build_Derived_Record_Type we properly
6959 -- set this flag in the case of private extensions. See comments in
6960 -- point 9. just before body of Build_Derived_Record_Type.
6961
6962 Set_Is_Constrained
6963 (Derived_Type,
6964 not (Inherit_Discrims
71d9e9f2 6965 or else Has_Unknown_Discriminants (Derived_Type)));
996ae0b0
RK
6966 end if;
6967
ffe9aba8 6968 -- STEP 3: initialize fields of derived type
996ae0b0
RK
6969
6970 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
fbf5a39b 6971 Set_Stored_Constraint (Derived_Type, No_Elist);
996ae0b0 6972
758c442c
GD
6973 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
6974 -- but cannot be interfaces
6975
6976 if not Private_Extension
6977 and then Ekind (Derived_Type) /= E_Private_Type
6978 and then Ekind (Derived_Type) /= E_Limited_Private_Type
6979 then
fea9e956
ES
6980 if Interface_Present (Type_Def) then
6981 Analyze_Interface_Declaration (Derived_Type, Type_Def);
6982 end if;
6983
ce2b6ba5 6984 Set_Interfaces (Derived_Type, No_Elist);
758c442c
GD
6985 end if;
6986
996ae0b0
RK
6987 -- Fields inherited from the Parent_Type
6988
6989 Set_Discard_Names
b603e37b 6990 (Derived_Type, Einfo.Discard_Names (Parent_Type));
996ae0b0 6991 Set_Has_Specified_Layout
b603e37b 6992 (Derived_Type, Has_Specified_Layout (Parent_Type));
996ae0b0 6993 Set_Is_Limited_Composite
b603e37b 6994 (Derived_Type, Is_Limited_Composite (Parent_Type));
996ae0b0 6995 Set_Is_Private_Composite
b603e37b 6996 (Derived_Type, Is_Private_Composite (Parent_Type));
996ae0b0
RK
6997
6998 -- Fields inherited from the Parent_Base
6999
7000 Set_Has_Controlled_Component
7001 (Derived_Type, Has_Controlled_Component (Parent_Base));
7002 Set_Has_Non_Standard_Rep
7003 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7004 Set_Has_Primitive_Operations
7005 (Derived_Type, Has_Primitive_Operations (Parent_Base));
7006
df89ab66 7007 -- Fields inherited from the Parent_Base in the non-private case
c6fe3827
GD
7008
7009 if Ekind (Derived_Type) = E_Record_Type then
7010 Set_Has_Complex_Representation
7011 (Derived_Type, Has_Complex_Representation (Parent_Base));
7012 end if;
7013
df89ab66
ES
7014 -- Fields inherited from the Parent_Base for record types
7015
7016 if Is_Record_Type (Derived_Type) then
b603e37b
AC
7017
7018 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7019 -- Parent_Base can be a private type or private extension.
7020
7021 if Present (Full_View (Parent_Base)) then
7022 Set_OK_To_Reorder_Components
7023 (Derived_Type,
7024 OK_To_Reorder_Components (Full_View (Parent_Base)));
7025 Set_Reverse_Bit_Order
7026 (Derived_Type, Reverse_Bit_Order (Full_View (Parent_Base)));
7027 else
7028 Set_OK_To_Reorder_Components
7029 (Derived_Type, OK_To_Reorder_Components (Parent_Base));
7030 Set_Reverse_Bit_Order
7031 (Derived_Type, Reverse_Bit_Order (Parent_Base));
7032 end if;
df89ab66
ES
7033 end if;
7034
07fc65c4 7035 -- Direct controlled types do not inherit Finalize_Storage_Only flag
996ae0b0 7036
c6fe3827 7037 if not Is_Controlled (Parent_Type) then
07fc65c4
GB
7038 Set_Finalize_Storage_Only
7039 (Derived_Type, Finalize_Storage_Only (Parent_Type));
996ae0b0
RK
7040 end if;
7041
ffe9aba8 7042 -- Set fields for private derived types
996ae0b0
RK
7043
7044 if Is_Private_Type (Derived_Type) then
7045 Set_Depends_On_Private (Derived_Type, True);
7046 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7047
7048 -- Inherit fields from non private record types. If this is the
7049 -- completion of a derivation from a private type, the parent itself
7050 -- is private, and the attributes come from its full view, which must
7051 -- be present.
7052
7053 else
7054 if Is_Private_Type (Parent_Base)
7055 and then not Is_Record_Type (Parent_Base)
7056 then
7057 Set_Component_Alignment
7058 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
7059 Set_C_Pass_By_Copy
7060 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
7061 else
7062 Set_Component_Alignment
7063 (Derived_Type, Component_Alignment (Parent_Base));
996ae0b0
RK
7064 Set_C_Pass_By_Copy
7065 (Derived_Type, C_Pass_By_Copy (Parent_Base));
7066 end if;
7067 end if;
7068
fbf5a39b 7069 -- Set fields for tagged types
996ae0b0
RK
7070
7071 if Is_Tagged then
7072 Set_Primitive_Operations (Derived_Type, New_Elmt_List);
7073
7074 -- All tagged types defined in Ada.Finalization are controlled
7075
7076 if Chars (Scope (Derived_Type)) = Name_Finalization
7077 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
7078 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
7079 then
7080 Set_Is_Controlled (Derived_Type);
7081 else
7082 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
7083 end if;
7084
c206e8fd
AC
7085 -- Minor optimization: there is no need to generate the class-wide
7086 -- entity associated with an underlying record view.
9013065b
AC
7087
7088 if not Is_Underlying_Record_View (Derived_Type) then
7089 Make_Class_Wide_Type (Derived_Type);
7090 end if;
7091
fea9e956 7092 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
996ae0b0
RK
7093
7094 if Has_Discriminants (Derived_Type)
7095 and then Constraint_Present
7096 then
fbf5a39b
AC
7097 Set_Stored_Constraint
7098 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
996ae0b0
RK
7099 end if;
7100
758c442c 7101 if Ada_Version >= Ada_05 then
88b32fc3
BD
7102 declare
7103 Ifaces_List : Elist_Id;
c6fe3827 7104
88b32fc3 7105 begin
c6fe3827
GD
7106 -- Checks rules 3.9.4 (13/2 and 14/2)
7107
7108 if Comes_From_Source (Derived_Type)
7109 and then not Is_Private_Type (Derived_Type)
7110 and then Is_Interface (Parent_Type)
7111 and then not Is_Interface (Derived_Type)
7112 then
7113 if Is_Task_Interface (Parent_Type) then
7114 Error_Msg_N
7115 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
7116 Derived_Type);
7117
7118 elsif Is_Protected_Interface (Parent_Type) then
7119 Error_Msg_N
7120 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
7121 Derived_Type);
7122 end if;
7123 end if;
7124
fea9e956
ES
7125 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
7126
ce2b6ba5 7127 Check_Interfaces (N, Type_Def);
fea9e956
ES
7128
7129 -- Ada 2005 (AI-251): Collect the list of progenitors that are
7130 -- not already in the parents.
7131
ce2b6ba5
JM
7132 Collect_Interfaces
7133 (T => Derived_Type,
7134 Ifaces_List => Ifaces_List,
7135 Exclude_Parents => True);
7136
7137 Set_Interfaces (Derived_Type, Ifaces_List);
88b32fc3 7138 end;
758c442c
GD
7139 end if;
7140
996ae0b0
RK
7141 else
7142 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
7143 Set_Has_Non_Standard_Rep
7144 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7145 end if;
7146
7147 -- STEP 4: Inherit components from the parent base and constrain them.
7148 -- Apply the second transformation described in point 6. above.
7149
7150 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
7151 or else not Has_Discriminants (Parent_Type)
7152 or else not Is_Constrained (Parent_Type)
7153 then
7154 Constrs := Discs;
7155 else
7156 Constrs := Discriminant_Constraint (Parent_Type);
7157 end if;
7158
57193e09
TQ
7159 Assoc_List :=
7160 Inherit_Components
7161 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
996ae0b0
RK
7162
7163 -- STEP 5a: Copy the parent record declaration for untagged types
7164
7165 if not Is_Tagged then
7166
7167 -- Discriminant_Constraint (Derived_Type) has been properly
71d9e9f2
ES
7168 -- constructed. Save it and temporarily set it to Empty because we
7169 -- do not want the call to New_Copy_Tree below to mess this list.
996ae0b0
RK
7170
7171 if Has_Discriminants (Derived_Type) then
7172 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7173 Set_Discriminant_Constraint (Derived_Type, No_Elist);
7174 else
7175 Save_Discr_Constr := No_Elist;
7176 end if;
7177
71d9e9f2
ES
7178 -- Save the Etype field of Derived_Type. It is correctly set now,
7179 -- but the call to New_Copy tree may remap it to point to itself,
7180 -- which is not what we want. Ditto for the Next_Entity field.
996ae0b0
RK
7181
7182 Save_Etype := Etype (Derived_Type);
7183 Save_Next_Entity := Next_Entity (Derived_Type);
7184
fbf5a39b
AC
7185 -- Assoc_List maps all stored discriminants in the Parent_Base to
7186 -- stored discriminants in the Derived_Type. It is fundamental that
7187 -- no types or itypes with discriminants other than the stored
996ae0b0 7188 -- discriminants appear in the entities declared inside
71d9e9f2 7189 -- Derived_Type, since the back end cannot deal with it.
996ae0b0
RK
7190
7191 New_Decl :=
7192 New_Copy_Tree
7193 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7194
7195 -- Restore the fields saved prior to the New_Copy_Tree call
fbf5a39b 7196 -- and compute the stored constraint.
996ae0b0
RK
7197
7198 Set_Etype (Derived_Type, Save_Etype);
7199 Set_Next_Entity (Derived_Type, Save_Next_Entity);
7200
7201 if Has_Discriminants (Derived_Type) then
7202 Set_Discriminant_Constraint
7203 (Derived_Type, Save_Discr_Constr);
fbf5a39b 7204 Set_Stored_Constraint
30c20106 7205 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
07fc65c4 7206 Replace_Components (Derived_Type, New_Decl);
996ae0b0
RK
7207 end if;
7208
7209 -- Insert the new derived type declaration
7210
7211 Rewrite (N, New_Decl);
7212
7213 -- STEP 5b: Complete the processing for record extensions in generics
7214
7215 -- There is no completion for record extensions declared in the
7216 -- parameter part of a generic, so we need to complete processing for
fbf5a39b
AC
7217 -- these generic record extensions here. The Record_Type_Definition call
7218 -- will change the Ekind of the components from E_Void to E_Component.
996ae0b0
RK
7219
7220 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
7221 Record_Type_Definition (Empty, Derived_Type);
7222
c885d7a1 7223 -- STEP 5c: Process the record extension for non private tagged types
996ae0b0
RK
7224
7225 elsif not Private_Extension then
996ae0b0 7226
c885d7a1
AC
7227 -- Add the _parent field in the derived type
7228
7229 Expand_Record_Extension (Derived_Type, Type_Def);
996ae0b0 7230
758c442c
GD
7231 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
7232 -- implemented interfaces if we are in expansion mode
7233
fea9e956 7234 if Expander_Active
ce2b6ba5 7235 and then Has_Interfaces (Derived_Type)
fea9e956 7236 then
758c442c
GD
7237 Add_Interface_Tag_Components (N, Derived_Type);
7238 end if;
7239
996ae0b0
RK
7240 -- Analyze the record extension
7241
7242 Record_Type_Definition
7243 (Record_Extension_Part (Type_Def), Derived_Type);
7244 end if;
7245
7246 End_Scope;
7247
88b32fc3
BD
7248 -- Nothing else to do if there is an error in the derivation.
7249 -- An unusual case: the full view may be derived from a type in an
7250 -- instance, when the partial view was used illegally as an actual
7251 -- in that instance, leading to a circular definition.
7252
7253 if Etype (Derived_Type) = Any_Type
7254 or else Etype (Parent_Type) = Derived_Type
7255 then
996ae0b0
RK
7256 return;
7257 end if;
7258
7259 -- Set delayed freeze and then derive subprograms, we need to do
7260 -- this in this order so that derived subprograms inherit the
7261 -- derived freeze if necessary.
7262
7263 Set_Has_Delayed_Freeze (Derived_Type);
758c442c 7264
996ae0b0 7265 if Derive_Subps then
88b32fc3
BD
7266 Derive_Subprograms (Parent_Type, Derived_Type);
7267 end if;
758c442c 7268
88b32fc3
BD
7269 -- If we have a private extension which defines a constrained derived
7270 -- type mark as constrained here after we have derived subprograms. See
7271 -- comment on point 9. just above the body of Build_Derived_Record_Type.
758c442c 7272
88b32fc3
BD
7273 if Private_Extension and then Inherit_Discrims then
7274 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7275 Set_Is_Constrained (Derived_Type, True);
7276 Set_Discriminant_Constraint (Derived_Type, Discs);
758c442c 7277
88b32fc3
BD
7278 elsif Is_Constrained (Parent_Type) then
7279 Set_Is_Constrained
7280 (Derived_Type, True);
7281 Set_Discriminant_Constraint
7282 (Derived_Type, Discriminant_Constraint (Parent_Type));
7283 end if;
7284 end if;
950d3e7d 7285
c206e8fd
AC
7286 -- Update the class-wide type, which shares the now-completed entity
7287 -- list with its specific type. In case of underlying record views,
9013065b 7288 -- we do not generate the corresponding class wide entity.
950d3e7d 7289
9013065b
AC
7290 if Is_Tagged
7291 and then not Is_Underlying_Record_View (Derived_Type)
7292 then
88b32fc3
BD
7293 Set_First_Entity
7294 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7295 Set_Last_Entity
7296 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7297 end if;
758c442c 7298
2b73cf68
JM
7299 -- Update the scope of anonymous access types of discriminants and other
7300 -- components, to prevent scope anomalies in gigi, when the derivation
7301 -- appears in a scope nested within that of the parent.
7302
7303 declare
7304 D : Entity_Id;
7305
7306 begin
7307 D := First_Entity (Derived_Type);
7308 while Present (D) loop
7309 if Ekind (D) = E_Discriminant
7310 or else Ekind (D) = E_Component
7311 then
7312 if Is_Itype (Etype (D))
7313 and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7314 then
7315 Set_Scope (Etype (D), Current_Scope);
7316 end if;
7317 end if;
7318
7319 Next_Entity (D);
7320 end loop;
7321 end;
88b32fc3 7322 end Build_Derived_Record_Type;
996ae0b0
RK
7323
7324 ------------------------
7325 -- Build_Derived_Type --
7326 ------------------------
7327
7328 procedure Build_Derived_Type
7329 (N : Node_Id;
7330 Parent_Type : Entity_Id;
7331 Derived_Type : Entity_Id;
7332 Is_Completion : Boolean;
7333 Derive_Subps : Boolean := True)
7334 is
7335 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7336
7337 begin
7338 -- Set common attributes
7339
c6fe3827 7340 Set_Scope (Derived_Type, Current_Scope);
996ae0b0 7341
c6fe3827
GD
7342 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7343 Set_Etype (Derived_Type, Parent_Base);
7344 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
996ae0b0 7345
93bcda23
AC
7346 Set_Size_Info (Derived_Type, Parent_Type);
7347 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
7348 Set_Convention (Derived_Type, Convention (Parent_Type));
7349 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7350 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
fbf5a39b
AC
7351
7352 -- The derived type inherits the representation clauses of the parent.
7353 -- However, for a private type that is completed by a derivation, there
7354 -- may be operation attributes that have been specified already (stream
7355 -- attributes and External_Tag) and those must be provided. Finally,
7356 -- if the partial view is a private extension, the representation items
7357 -- of the parent have been inherited already, and should not be chained
7358 -- twice to the derived type.
7359
7360 if Is_Tagged_Type (Parent_Type)
7361 and then Present (First_Rep_Item (Derived_Type))
7362 then
7363 -- The existing items are either operational items or items inherited
7364 -- from a private extension declaration.
7365
7366 declare
dc06abec
RD
7367 Rep : Node_Id;
7368 -- Used to iterate over representation items of the derived type
7369
7370 Last_Rep : Node_Id;
7371 -- Last representation item of the (non-empty) representation
7372 -- item list of the derived type.
7373
fbf5a39b
AC
7374 Found : Boolean := False;
7375
7376 begin
dc06abec
RD
7377 Rep := First_Rep_Item (Derived_Type);
7378 Last_Rep := Rep;
fbf5a39b
AC
7379 while Present (Rep) loop
7380 if Rep = First_Rep_Item (Parent_Type) then
7381 Found := True;
7382 exit;
dc06abec 7383
fbf5a39b
AC
7384 else
7385 Rep := Next_Rep_Item (Rep);
dc06abec
RD
7386
7387 if Present (Rep) then
7388 Last_Rep := Rep;
7389 end if;
fbf5a39b
AC
7390 end if;
7391 end loop;
7392
dc06abec
RD
7393 -- Here if we either encountered the parent type's first rep
7394 -- item on the derived type's rep item list (in which case
7395 -- Found is True, and we have nothing else to do), or if we
7396 -- reached the last rep item of the derived type, which is
7397 -- Last_Rep, in which case we further chain the parent type's
7398 -- rep items to those of the derived type.
7399
fbf5a39b 7400 if not Found then
dc06abec 7401 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
fbf5a39b
AC
7402 end if;
7403 end;
7404
7405 else
7406 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7407 end if;
996ae0b0
RK
7408
7409 case Ekind (Parent_Type) is
7410 when Numeric_Kind =>
7411 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7412
7413 when Array_Kind =>
7414 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
7415
7416 when E_Record_Type
7417 | E_Record_Subtype
7418 | Class_Wide_Kind =>
7419 Build_Derived_Record_Type
7420 (N, Parent_Type, Derived_Type, Derive_Subps);
7421 return;
7422
7423 when Enumeration_Kind =>
7424 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7425
7426 when Access_Kind =>
7427 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7428
7429 when Incomplete_Or_Private_Kind =>
7430 Build_Derived_Private_Type
7431 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
7432
7433 -- For discriminated types, the derivation includes deriving
7434 -- primitive operations. For others it is done below.
7435
7436 if Is_Tagged_Type (Parent_Type)
7437 or else Has_Discriminants (Parent_Type)
7438 or else (Present (Full_View (Parent_Type))
7439 and then Has_Discriminants (Full_View (Parent_Type)))
7440 then
7441 return;
7442 end if;
7443
7444 when Concurrent_Kind =>
7445 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
7446
7447 when others =>
7448 raise Program_Error;
7449 end case;
7450
7451 if Etype (Derived_Type) = Any_Type then
7452 return;
7453 end if;
7454
a5b62485
AC
7455 -- Set delayed freeze and then derive subprograms, we need to do this
7456 -- in this order so that derived subprograms inherit the derived freeze
7457 -- if necessary.
996ae0b0
RK
7458
7459 Set_Has_Delayed_Freeze (Derived_Type);
7460 if Derive_Subps then
7461 Derive_Subprograms (Parent_Type, Derived_Type);
7462 end if;
7463
7464 Set_Has_Primitive_Operations
7465 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
7466 end Build_Derived_Type;
7467
7468 -----------------------
7469 -- Build_Discriminal --
7470 -----------------------
7471
7472 procedure Build_Discriminal (Discrim : Entity_Id) is
7473 D_Minal : Entity_Id;
7474 CR_Disc : Entity_Id;
7475
7476 begin
71d9e9f2 7477 -- A discriminal has the same name as the discriminant
996ae0b0 7478
71d9e9f2
ES
7479 D_Minal :=
7480 Make_Defining_Identifier (Sloc (Discrim),
7481 Chars => Chars (Discrim));
996ae0b0
RK
7482
7483 Set_Ekind (D_Minal, E_In_Parameter);
7484 Set_Mechanism (D_Minal, Default_Mechanism);
7485 Set_Etype (D_Minal, Etype (Discrim));
7486
7487 Set_Discriminal (Discrim, D_Minal);
7488 Set_Discriminal_Link (D_Minal, Discrim);
7489
7490 -- For task types, build at once the discriminants of the corresponding
7491 -- record, which are needed if discriminants are used in entry defaults
7492 -- and in family bounds.
7493
7494 if Is_Concurrent_Type (Current_Scope)
7495 or else Is_Limited_Type (Current_Scope)
7496 then
7497 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
7498
950d3e7d
ES
7499 Set_Ekind (CR_Disc, E_In_Parameter);
7500 Set_Mechanism (CR_Disc, Default_Mechanism);
7501 Set_Etype (CR_Disc, Etype (Discrim));
7502 Set_Discriminal_Link (CR_Disc, Discrim);
7503 Set_CR_Discriminant (Discrim, CR_Disc);
996ae0b0
RK
7504 end if;
7505 end Build_Discriminal;
7506
7507 ------------------------------------
7508 -- Build_Discriminant_Constraints --
7509 ------------------------------------
7510
7511 function Build_Discriminant_Constraints
7512 (T : Entity_Id;
7513 Def : Node_Id;
b0f26df5 7514 Derived_Def : Boolean := False) return Elist_Id
996ae0b0 7515 is
71d9e9f2
ES
7516 C : constant Node_Id := Constraint (Def);
7517 Nb_Discr : constant Nat := Number_Discriminants (T);
7518
996ae0b0 7519 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
71d9e9f2 7520 -- Saves the expression corresponding to a given discriminant in T
996ae0b0
RK
7521
7522 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
7523 -- Return the Position number within array Discr_Expr of a discriminant
7524 -- D within the discriminant list of the discriminated type T.
7525
7526 ------------------
7527 -- Pos_Of_Discr --
7528 ------------------
7529
7530 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
7531 Disc : Entity_Id;
7532
7533 begin
7534 Disc := First_Discriminant (T);
7535 for J in Discr_Expr'Range loop
7536 if Disc = D then
7537 return J;
7538 end if;
7539
7540 Next_Discriminant (Disc);
7541 end loop;
7542
7543 -- Note: Since this function is called on discriminants that are
7544 -- known to belong to the discriminated type, falling through the
7545 -- loop with no match signals an internal compiler error.
7546
7547 raise Program_Error;
7548 end Pos_Of_Discr;
7549
fbf5a39b 7550 -- Declarations local to Build_Discriminant_Constraints
996ae0b0
RK
7551
7552 Discr : Entity_Id;
7553 E : Entity_Id;
fbf5a39b 7554 Elist : constant Elist_Id := New_Elmt_List;
996ae0b0 7555
71d9e9f2
ES
7556 Constr : Node_Id;
7557 Expr : Node_Id;
7558 Id : Node_Id;
7559 Position : Nat;
7560 Found : Boolean;
996ae0b0
RK
7561
7562 Discrim_Present : Boolean := False;
7563
7564 -- Start of processing for Build_Discriminant_Constraints
7565
7566 begin
7567 -- The following loop will process positional associations only.
7568 -- For a positional association, the (single) discriminant is
7569 -- implicitly specified by position, in textual order (RM 3.7.2).
7570
7571 Discr := First_Discriminant (T);
7572 Constr := First (Constraints (C));
996ae0b0
RK
7573 for D in Discr_Expr'Range loop
7574 exit when Nkind (Constr) = N_Discriminant_Association;
7575
7576 if No (Constr) then
7577 Error_Msg_N ("too few discriminants given in constraint", C);
7578 return New_Elmt_List;
7579
7580 elsif Nkind (Constr) = N_Range
7581 or else (Nkind (Constr) = N_Attribute_Reference
7582 and then
7583 Attribute_Name (Constr) = Name_Range)
7584 then
7585 Error_Msg_N
7586 ("a range is not a valid discriminant constraint", Constr);
7587 Discr_Expr (D) := Error;
7588
7589 else
7590 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
7591 Discr_Expr (D) := Constr;
7592 end if;
7593
7594 Next_Discriminant (Discr);
7595 Next (Constr);
7596 end loop;
7597
7598 if No (Discr) and then Present (Constr) then
7599 Error_Msg_N ("too many discriminants given in constraint", Constr);
7600 return New_Elmt_List;
7601 end if;
7602
7603 -- Named associations can be given in any order, but if both positional
7604 -- and named associations are used in the same discriminant constraint,
7605 -- then positional associations must occur first, at their normal
7606 -- position. Hence once a named association is used, the rest of the
7607 -- discriminant constraint must use only named associations.
7608
7609 while Present (Constr) loop
7610
ffe9aba8 7611 -- Positional association forbidden after a named association
996ae0b0
RK
7612
7613 if Nkind (Constr) /= N_Discriminant_Association then
7614 Error_Msg_N ("positional association follows named one", Constr);
7615 return New_Elmt_List;
7616
7617 -- Otherwise it is a named association
7618
7619 else
7620 -- E records the type of the discriminants in the named
7621 -- association. All the discriminants specified in the same name
7622 -- association must have the same type.
7623
7624 E := Empty;
7625
7626 -- Search the list of discriminants in T to see if the simple name
7627 -- given in the constraint matches any of them.
7628
7629 Id := First (Selector_Names (Constr));
7630 while Present (Id) loop
7631 Found := False;
7632
7633 -- If Original_Discriminant is present, we are processing a
7634 -- generic instantiation and this is an instance node. We need
7635 -- to find the name of the corresponding discriminant in the
7636 -- actual record type T and not the name of the discriminant in
7637 -- the generic formal. Example:
88b32fc3 7638
996ae0b0
RK
7639 -- generic
7640 -- type G (D : int) is private;
7641 -- package P is
7642 -- subtype W is G (D => 1);
7643 -- end package;
7644 -- type Rec (X : int) is record ... end record;
7645 -- package Q is new P (G => Rec);
88b32fc3 7646
996ae0b0
RK
7647 -- At the point of the instantiation, formal type G is Rec
7648 -- and therefore when reanalyzing "subtype W is G (D => 1);"
7649 -- which really looks like "subtype W is Rec (D => 1);" at
7650 -- the point of instantiation, we want to find the discriminant
f3d57416 7651 -- that corresponds to D in Rec, i.e. X.
996ae0b0
RK
7652
7653 if Present (Original_Discriminant (Id)) then
7654 Discr := Find_Corresponding_Discriminant (Id, T);
7655 Found := True;
7656
7657 else
7658 Discr := First_Discriminant (T);
7659 while Present (Discr) loop
7660 if Chars (Discr) = Chars (Id) then
7661 Found := True;
7662 exit;
7663 end if;
7664
7665 Next_Discriminant (Discr);
7666 end loop;
7667
7668 if not Found then
7669 Error_Msg_N ("& does not match any discriminant", Id);
7670 return New_Elmt_List;
7671
7672 -- The following is only useful for the benefit of generic
7673 -- instances but it does not interfere with other
638e383e 7674 -- processing for the non-generic case so we do it in all
996ae0b0
RK
7675 -- cases (for generics this statement is executed when
7676 -- processing the generic definition, see comment at the
fbf5a39b 7677 -- beginning of this if statement).
996ae0b0
RK
7678
7679 else
7680 Set_Original_Discriminant (Id, Discr);
7681 end if;
7682 end if;
7683
7684 Position := Pos_Of_Discr (T, Discr);
7685
7686 if Present (Discr_Expr (Position)) then
7687 Error_Msg_N ("duplicate constraint for discriminant&", Id);
7688
7689 else
7690 -- Each discriminant specified in the same named association
7691 -- must be associated with a separate copy of the
7692 -- corresponding expression.
7693
7694 if Present (Next (Id)) then
7695 Expr := New_Copy_Tree (Expression (Constr));
7696 Set_Parent (Expr, Parent (Expression (Constr)));
7697 else
7698 Expr := Expression (Constr);
7699 end if;
7700
7701 Discr_Expr (Position) := Expr;
7702 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
7703 end if;
7704
7705 -- A discriminant association with more than one discriminant
7706 -- name is only allowed if the named discriminants are all of
7707 -- the same type (RM 3.7.1(8)).
7708
7709 if E = Empty then
7710 E := Base_Type (Etype (Discr));
7711
7712 elsif Base_Type (Etype (Discr)) /= E then
7713 Error_Msg_N
7714 ("all discriminants in an association " &
7715 "must have the same type", Id);
7716 end if;
7717
7718 Next (Id);
7719 end loop;
7720 end if;
7721
7722 Next (Constr);
7723 end loop;
7724
7725 -- A discriminant constraint must provide exactly one value for each
7726 -- discriminant of the type (RM 3.7.1(8)).
7727
7728 for J in Discr_Expr'Range loop
7729 if No (Discr_Expr (J)) then
7730 Error_Msg_N ("too few discriminants given in constraint", C);
7731 return New_Elmt_List;
7732 end if;
7733 end loop;
7734
ffe9aba8 7735 -- Determine if there are discriminant expressions in the constraint
996ae0b0
RK
7736
7737 for J in Discr_Expr'Range loop
88b32fc3
BD
7738 if Denotes_Discriminant
7739 (Discr_Expr (J), Check_Concurrent => True)
7740 then
996ae0b0
RK
7741 Discrim_Present := True;
7742 end if;
7743 end loop;
7744
7745 -- Build an element list consisting of the expressions given in the
2820d220
AC
7746 -- discriminant constraint and apply the appropriate checks. The list
7747 -- is constructed after resolving any named discriminant associations
7748 -- and therefore the expressions appear in the textual order of the
7749 -- discriminants.
996ae0b0
RK
7750
7751 Discr := First_Discriminant (T);
7752 for J in Discr_Expr'Range loop
7753 if Discr_Expr (J) /= Error then
996ae0b0
RK
7754 Append_Elmt (Discr_Expr (J), Elist);
7755
7756 -- If any of the discriminant constraints is given by a
7757 -- discriminant and we are in a derived type declaration we
7758 -- have a discriminant renaming. Establish link between new
7759 -- and old discriminant.
7760
7761 if Denotes_Discriminant (Discr_Expr (J)) then
7762 if Derived_Def then
7763 Set_Corresponding_Discriminant
7764 (Entity (Discr_Expr (J)), Discr);
7765 end if;
7766
7767 -- Force the evaluation of non-discriminant expressions.
7768 -- If we have found a discriminant in the constraint 3.4(26)
7769 -- and 3.8(18) demand that no range checks are performed are
fbf5a39b
AC
7770 -- after evaluation. If the constraint is for a component
7771 -- definition that has a per-object constraint, expressions are
7772 -- evaluated but not checked either. In all other cases perform
7773 -- a range check.
996ae0b0
RK
7774
7775 else
fbf5a39b
AC
7776 if Discrim_Present then
7777 null;
7778
a397db96 7779 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
fbf5a39b
AC
7780 and then
7781 Has_Per_Object_Constraint
a397db96 7782 (Defining_Identifier (Parent (Parent (Def))))
fbf5a39b
AC
7783 then
7784 null;
7785
2820d220
AC
7786 elsif Is_Access_Type (Etype (Discr)) then
7787 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
7788
fbf5a39b 7789 else
996ae0b0
RK
7790 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
7791 end if;
7792
7793 Force_Evaluation (Discr_Expr (J));
7794 end if;
7795
88b32fc3
BD
7796 -- Check that the designated type of an access discriminant's
7797 -- expression is not a class-wide type unless the discriminant's
7798 -- designated type is also class-wide.
996ae0b0
RK
7799
7800 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
7801 and then not Is_Class_Wide_Type
7802 (Designated_Type (Etype (Discr)))
7803 and then Etype (Discr_Expr (J)) /= Any_Type
7804 and then Is_Class_Wide_Type
7805 (Designated_Type (Etype (Discr_Expr (J))))
7806 then
7807 Wrong_Type (Discr_Expr (J), Etype (Discr));
49d8b802
ES
7808
7809 elsif Is_Access_Type (Etype (Discr))
7810 and then not Is_Access_Constant (Etype (Discr))
7811 and then Is_Access_Type (Etype (Discr_Expr (J)))
7812 and then Is_Access_Constant (Etype (Discr_Expr (J)))
7813 then
7814 Error_Msg_NE
7815 ("constraint for discriminant& must be access to variable",
7816 Def, Discr);
996ae0b0
RK
7817 end if;
7818 end if;
7819
7820 Next_Discriminant (Discr);
7821 end loop;
7822
7823 return Elist;
7824 end Build_Discriminant_Constraints;
7825
7826 ---------------------------------
7827 -- Build_Discriminated_Subtype --
7828 ---------------------------------
7829
7830 procedure Build_Discriminated_Subtype
7831 (T : Entity_Id;
7832 Def_Id : Entity_Id;
7833 Elist : Elist_Id;
7834 Related_Nod : Node_Id;
7835 For_Access : Boolean := False)
7836 is
7837 Has_Discrs : constant Boolean := Has_Discriminants (T);
88b32fc3
BD
7838 Constrained : constant Boolean :=
7839 (Has_Discrs
7840 and then not Is_Empty_Elmt_List (Elist)
7841 and then not Is_Class_Wide_Type (T))
7842 or else Is_Constrained (T);
996ae0b0
RK
7843
7844 begin
7845 if Ekind (T) = E_Record_Type then
7846 if For_Access then
7847 Set_Ekind (Def_Id, E_Private_Subtype);
7848 Set_Is_For_Access_Subtype (Def_Id, True);
7849 else
7850 Set_Ekind (Def_Id, E_Record_Subtype);
7851 end if;
7852
7d7af38a
JM
7853 -- Inherit preelaboration flag from base, for types for which it
7854 -- may have been set: records, private types, protected types.
7855
7856 Set_Known_To_Have_Preelab_Init
7857 (Def_Id, Known_To_Have_Preelab_Init (T));
7858
996ae0b0
RK
7859 elsif Ekind (T) = E_Task_Type then
7860 Set_Ekind (Def_Id, E_Task_Subtype);
7861
7862 elsif Ekind (T) = E_Protected_Type then
7863 Set_Ekind (Def_Id, E_Protected_Subtype);
7d7af38a
JM
7864 Set_Known_To_Have_Preelab_Init
7865 (Def_Id, Known_To_Have_Preelab_Init (T));
996ae0b0
RK
7866
7867 elsif Is_Private_Type (T) then
7868 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
7d7af38a
JM
7869 Set_Known_To_Have_Preelab_Init
7870 (Def_Id, Known_To_Have_Preelab_Init (T));
996ae0b0
RK
7871
7872 elsif Is_Class_Wide_Type (T) then
7873 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
7874
7875 else
88b32fc3 7876 -- Incomplete type. Attach subtype to list of dependents, to be
35ae2ed8
AC
7877 -- completed with full view of parent type, unless is it the
7878 -- designated subtype of a record component within an init_proc.
7879 -- This last case arises for a component of an access type whose
7880 -- designated type is incomplete (e.g. a Taft Amendment type).
7881 -- The designated subtype is within an inner scope, and needs no
7882 -- elaboration, because only the access type is needed in the
7883 -- initialization procedure.
996ae0b0
RK
7884
7885 Set_Ekind (Def_Id, Ekind (T));
35ae2ed8
AC
7886
7887 if For_Access and then Within_Init_Proc then
7888 null;
7889 else
7890 Append_Elmt (Def_Id, Private_Dependents (T));
7891 end if;
996ae0b0
RK
7892 end if;
7893
7894 Set_Etype (Def_Id, T);
7895 Init_Size_Align (Def_Id);
7896 Set_Has_Discriminants (Def_Id, Has_Discrs);
7897 Set_Is_Constrained (Def_Id, Constrained);
7898
7899 Set_First_Entity (Def_Id, First_Entity (T));
7900 Set_Last_Entity (Def_Id, Last_Entity (T));
33931112
JM
7901
7902 -- If the subtype is the completion of a private declaration, there may
7903 -- have been representation clauses for the partial view, and they must
7904 -- be preserved. Build_Derived_Type chains the inherited clauses with
7905 -- the ones appearing on the extension. If this comes from a subtype
7906 -- declaration, all clauses are inherited.
7907
7908 if No (First_Rep_Item (Def_Id)) then
23c4ff9b 7909 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
33931112 7910 end if;
996ae0b0
RK
7911
7912 if Is_Tagged_Type (T) then
7913 Set_Is_Tagged_Type (Def_Id);
7914 Make_Class_Wide_Type (Def_Id);
7915 end if;
7916
fbf5a39b 7917 Set_Stored_Constraint (Def_Id, No_Elist);
996ae0b0
RK
7918
7919 if Has_Discrs then
7920 Set_Discriminant_Constraint (Def_Id, Elist);
fbf5a39b 7921 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
996ae0b0
RK
7922 end if;
7923
7924 if Is_Tagged_Type (T) then
030d25f4
JM
7925
7926 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
7927 -- concurrent record type (which has the list of primitive
7928 -- operations).
7929
7930 if Ada_Version >= Ada_05
7931 and then Is_Concurrent_Type (T)
7932 then
7933 Set_Corresponding_Record_Type (Def_Id,
7934 Corresponding_Record_Type (T));
7935 else
7936 Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
7937 end if;
7938
fea9e956 7939 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
996ae0b0
RK
7940 end if;
7941
7942 -- Subtypes introduced by component declarations do not need to be
7943 -- marked as delayed, and do not get freeze nodes, because the semantics
7944 -- verifies that the parents of the subtypes are frozen before the
7945 -- enclosing record is frozen.
7946
7947 if not Is_Type (Scope (Def_Id)) then
7948 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
7949
7950 if Is_Private_Type (T)
7951 and then Present (Full_View (T))
7952 then
7953 Conditional_Delay (Def_Id, Full_View (T));
7954 else
7955 Conditional_Delay (Def_Id, T);
7956 end if;
7957 end if;
7958
7959 if Is_Record_Type (T) then
7960 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
7961
7962 if Has_Discrs
7963 and then not Is_Empty_Elmt_List (Elist)
7964 and then not For_Access
7965 then
7966 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
7967 elsif not For_Access then
7968 Set_Cloned_Subtype (Def_Id, T);
7969 end if;
7970 end if;
996ae0b0
RK
7971 end Build_Discriminated_Subtype;
7972
fea9e956
ES
7973 ---------------------------
7974 -- Build_Itype_Reference --
7975 ---------------------------
7976
7977 procedure Build_Itype_Reference
7978 (Ityp : Entity_Id;
7979 Nod : Node_Id)
7980 is
7981 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
7982 begin
7983 Set_Itype (IR, Ityp);
7984 Insert_After (Nod, IR);
7985 end Build_Itype_Reference;
7986
996ae0b0
RK
7987 ------------------------
7988 -- Build_Scalar_Bound --
7989 ------------------------
7990
7991 function Build_Scalar_Bound
7992 (Bound : Node_Id;
7993 Par_T : Entity_Id;
b0f26df5 7994 Der_T : Entity_Id) return Node_Id
996ae0b0
RK
7995 is
7996 New_Bound : Entity_Id;
7997
7998 begin
7999 -- Note: not clear why this is needed, how can the original bound
8000 -- be unanalyzed at this point? and if it is, what business do we
8001 -- have messing around with it? and why is the base type of the
8002 -- parent type the right type for the resolution. It probably is
8003 -- not! It is OK for the new bound we are creating, but not for
8004 -- the old one??? Still if it never happens, no problem!
8005
8006 Analyze_And_Resolve (Bound, Base_Type (Par_T));
8007
7d7af38a 8008 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
996ae0b0
RK
8009 New_Bound := New_Copy (Bound);
8010 Set_Etype (New_Bound, Der_T);
8011 Set_Analyzed (New_Bound);
8012
8013 elsif Is_Entity_Name (Bound) then
8014 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
8015
8016 -- The following is almost certainly wrong. What business do we have
8017 -- relocating a node (Bound) that is presumably still attached to
8018 -- the tree elsewhere???
8019
8020 else
8021 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
8022 end if;
8023
8024 Set_Etype (New_Bound, Der_T);
8025 return New_Bound;
8026 end Build_Scalar_Bound;
8027
8028 --------------------------------
8029 -- Build_Underlying_Full_View --
8030 --------------------------------
8031
8032 procedure Build_Underlying_Full_View
8033 (N : Node_Id;
8034 Typ : Entity_Id;
8035 Par : Entity_Id)
8036 is
8037 Loc : constant Source_Ptr := Sloc (N);
8038 Subt : constant Entity_Id :=
8039 Make_Defining_Identifier
8040 (Loc, New_External_Name (Chars (Typ), 'S'));
8041
8042 Constr : Node_Id;
8043 Indic : Node_Id;
8044 C : Node_Id;
8045 Id : Node_Id;
8046
244e5a2c
AC
8047 procedure Set_Discriminant_Name (Id : Node_Id);
8048 -- If the derived type has discriminants, they may rename discriminants
8049 -- of the parent. When building the full view of the parent, we need to
8050 -- recover the names of the original discriminants if the constraint is
8051 -- given by named associations.
8052
8053 ---------------------------
8054 -- Set_Discriminant_Name --
8055 ---------------------------
8056
8057 procedure Set_Discriminant_Name (Id : Node_Id) is
8058 Disc : Entity_Id;
8059
8060 begin
8061 Set_Original_Discriminant (Id, Empty);
8062
8063 if Has_Discriminants (Typ) then
8064 Disc := First_Discriminant (Typ);
244e5a2c
AC
8065 while Present (Disc) loop
8066 if Chars (Disc) = Chars (Id)
8067 and then Present (Corresponding_Discriminant (Disc))
8068 then
8069 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
8070 end if;
8071 Next_Discriminant (Disc);
8072 end loop;
8073 end if;
8074 end Set_Discriminant_Name;
8075
8076 -- Start of processing for Build_Underlying_Full_View
8077
996ae0b0
RK
8078 begin
8079 if Nkind (N) = N_Full_Type_Declaration then
8080 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
8081
244e5a2c 8082 elsif Nkind (N) = N_Subtype_Declaration then
996ae0b0 8083 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
996ae0b0 8084
244e5a2c
AC
8085 elsif Nkind (N) = N_Component_Declaration then
8086 Constr :=
8087 New_Copy_Tree
8088 (Constraint (Subtype_Indication (Component_Definition (N))));
996ae0b0 8089
244e5a2c
AC
8090 else
8091 raise Program_Error;
8092 end if;
996ae0b0 8093
244e5a2c 8094 C := First (Constraints (Constr));
996ae0b0 8095 while Present (C) loop
996ae0b0
RK
8096 if Nkind (C) = N_Discriminant_Association then
8097 Id := First (Selector_Names (C));
996ae0b0 8098 while Present (Id) loop
244e5a2c 8099 Set_Discriminant_Name (Id);
996ae0b0
RK
8100 Next (Id);
8101 end loop;
8102 end if;
8103
8104 Next (C);
8105 end loop;
8106
244e5a2c
AC
8107 Indic :=
8108 Make_Subtype_Declaration (Loc,
8109 Defining_Identifier => Subt,
8110 Subtype_Indication =>
8111 Make_Subtype_Indication (Loc,
8112 Subtype_Mark => New_Reference_To (Par, Loc),
8113 Constraint => New_Copy_Tree (Constr)));
996ae0b0 8114
615cbd95
AC
8115 -- If this is a component subtype for an outer itype, it is not
8116 -- a list member, so simply set the parent link for analysis: if
8117 -- the enclosing type does not need to be in a declarative list,
8118 -- neither do the components.
8119
244e5a2c
AC
8120 if Is_List_Member (N)
8121 and then Nkind (N) /= N_Component_Declaration
8122 then
615cbd95
AC
8123 Insert_Before (N, Indic);
8124 else
8125 Set_Parent (Indic, Parent (N));
8126 end if;
8127
996ae0b0
RK
8128 Analyze (Indic);
8129 Set_Underlying_Full_View (Typ, Full_View (Subt));
8130 end Build_Underlying_Full_View;
8131
8132 -------------------------------
8133 -- Check_Abstract_Overriding --
8134 -------------------------------
8135
8136 procedure Check_Abstract_Overriding (T : Entity_Id) is
88b32fc3 8137 Alias_Subp : Entity_Id;
57193e09 8138 Elmt : Elmt_Id;
88b32fc3 8139 Op_List : Elist_Id;
57193e09 8140 Subp : Entity_Id;
57193e09 8141 Type_Def : Node_Id;
996ae0b0
RK
8142
8143 begin
8144 Op_List := Primitive_Operations (T);
8145
8146 -- Loop to check primitive operations
8147
8148 Elmt := First_Elmt (Op_List);
8149 while Present (Elmt) loop
8150 Subp := Node (Elmt);
57193e09
TQ
8151 Alias_Subp := Alias (Subp);
8152
8153 -- Inherited subprograms are identified by the fact that they do not
8154 -- come from source, and the associated source location is the
8155 -- location of the first subtype of the derived type.
996ae0b0 8156
fea9e956
ES
8157 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
8158 -- subprograms that "require overriding".
8159
a5b62485 8160 -- Special exception, do not complain about failure to override the
9dfd2ff8
CC
8161 -- stream routines _Input and _Output, as well as the primitive
8162 -- operations used in dispatching selects since we always provide
996ae0b0
RK
8163 -- automatic overridings for these subprograms.
8164
2b73cf68
JM
8165 -- Also ignore this rule for convention CIL since .NET libraries
8166 -- do bizarre things with interfaces???
8167
8168 -- The partial view of T may have been a private extension, for
8169 -- which inherited functions dispatching on result are abstract.
8170 -- If the full view is a null extension, there is no need for
8171 -- overriding in Ada2005, but wrappers need to be built for them
8172 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
8173
8174 if Is_Null_Extension (T)
8175 and then Has_Controlling_Result (Subp)
8176 and then Ada_Version >= Ada_05
ce2b6ba5 8177 and then Present (Alias_Subp)
2b73cf68 8178 and then not Comes_From_Source (Subp)
ce2b6ba5 8179 and then not Is_Abstract_Subprogram (Alias_Subp)
ce4a6e84 8180 and then not Is_Access_Type (Etype (Subp))
2b73cf68 8181 then
7d7af38a 8182 null;
2b73cf68 8183
ce2b6ba5
JM
8184 -- Ada 2005 (AI-251): Internal entities of interfaces need no
8185 -- processing because this check is done with the aliased
8186 -- entity
8187
8188 elsif Present (Interface_Alias (Subp)) then
8189 null;
8190
7d7af38a 8191 elsif (Is_Abstract_Subprogram (Subp)
ce4a6e84
RD
8192 or else Requires_Overriding (Subp)
8193 or else
8194 (Has_Controlling_Result (Subp)
8195 and then Present (Alias_Subp)
8196 and then not Comes_From_Source (Subp)
8197 and then Sloc (Subp) = Sloc (First_Subtype (T))))
fbf5a39b
AC
8198 and then not Is_TSS (Subp, TSS_Stream_Input)
8199 and then not Is_TSS (Subp, TSS_Stream_Output)
fea9e956 8200 and then not Is_Abstract_Type (T)
2b73cf68 8201 and then Convention (T) /= Convention_CIL
ce2b6ba5 8202 and then not Is_Predefined_Interface_Primitive (Subp)
88b32fc3
BD
8203
8204 -- Ada 2005 (AI-251): Do not consider hidden entities associated
8205 -- with abstract interface types because the check will be done
8206 -- with the aliased entity (otherwise we generate a duplicated
8207 -- error message).
8208
ce2b6ba5 8209 and then not Present (Interface_Alias (Subp))
996ae0b0 8210 then
57193e09
TQ
8211 if Present (Alias_Subp) then
8212
8213 -- Only perform the check for a derived subprogram when the
f3d0f304 8214 -- type has an explicit record extension. This avoids incorrect
ce4a6e84
RD
8215 -- flagging of abstract subprograms for the case of a type
8216 -- without an extension that is derived from a formal type
8217 -- with a tagged actual (can occur within a private part).
57193e09
TQ
8218
8219 -- Ada 2005 (AI-391): In the case of an inherited function with
8220 -- a controlling result of the type, the rule does not apply if
8221 -- the type is a null extension (unless the parent function
8222 -- itself is abstract, in which case the function must still be
8223 -- be overridden). The expander will generate an overriding
8224 -- wrapper function calling the parent subprogram (see
8225 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
996ae0b0
RK
8226
8227 Type_Def := Type_Definition (Parent (T));
7d7af38a 8228
996ae0b0
RK
8229 if Nkind (Type_Def) = N_Derived_Type_Definition
8230 and then Present (Record_Extension_Part (Type_Def))
57193e09
TQ
8231 and then
8232 (Ada_Version < Ada_05
8233 or else not Is_Null_Extension (T)
8234 or else Ekind (Subp) = E_Procedure
8235 or else not Has_Controlling_Result (Subp)
fea9e956
ES
8236 or else Is_Abstract_Subprogram (Alias_Subp)
8237 or else Requires_Overriding (Subp)
57193e09 8238 or else Is_Access_Type (Etype (Subp)))
996ae0b0 8239 then
ce2b6ba5
JM
8240 -- Avoid reporting error in case of abstract predefined
8241 -- primitive inherited from interface type because the
8242 -- body of internally generated predefined primitives
8243 -- of tagged types are generated later by Freeze_Type
8244
8245 if Is_Interface (Root_Type (T))
8246 and then Is_Abstract_Subprogram (Subp)
8247 and then Is_Predefined_Dispatching_Operation (Subp)
8248 and then not Comes_From_Source (Ultimate_Alias (Subp))
7d7af38a
JM
8249 then
8250 null;
9dfd2ff8 8251
7d7af38a
JM
8252 else
8253 Error_Msg_NE
8254 ("type must be declared abstract or & overridden",
8255 T, Subp);
9dfd2ff8 8256
7d7af38a
JM
8257 -- Traverse the whole chain of aliased subprograms to
8258 -- complete the error notification. This is especially
8259 -- useful for traceability of the chain of entities when
8260 -- the subprogram corresponds with an interface
8261 -- subprogram (which may be defined in another package).
8262
8263 if Present (Alias_Subp) then
8264 declare
8265 E : Entity_Id;
8266
8267 begin
8268 E := Subp;
8269 while Present (Alias (E)) loop
8270 Error_Msg_Sloc := Sloc (E);
8271 Error_Msg_NE
8272 ("\& has been inherited #", T, Subp);
8273 E := Alias (E);
8274 end loop;
9dfd2ff8 8275
7d7af38a
JM
8276 Error_Msg_Sloc := Sloc (E);
8277 Error_Msg_NE
8278 ("\& has been inherited from subprogram #",
8279 T, Subp);
8280 end;
8281 end if;
9dfd2ff8
CC
8282 end if;
8283
758c442c 8284 -- Ada 2005 (AI-345): Protected or task type implementing
9dfd2ff8 8285 -- abstract interfaces.
758c442c
GD
8286
8287 elsif Is_Concurrent_Record_Type (T)
ce2b6ba5 8288 and then Present (Interfaces (T))
758c442c 8289 then
88b32fc3
BD
8290 -- The controlling formal of Subp must be of mode "out",
8291 -- "in out" or an access-to-variable to be overridden.
8292
dc06abec
RD
8293 -- Error message below needs rewording (remember comma
8294 -- in -gnatj mode) ???
8295
8f983e64
ES
8296 if Ekind (First_Formal (Subp)) = E_In_Parameter
8297 and then Ekind (Subp) /= E_Function
8298 then
ce2b6ba5
JM
8299 if not Is_Predefined_Dispatching_Operation (Subp) then
8300 Error_Msg_NE
8301 ("first formal of & must be of mode `OUT`, " &
8302 "`IN OUT` or access-to-variable", T, Subp);
8303 Error_Msg_N
8304 ("\to be overridden by protected procedure or " &
8305 "entry (RM 9.4(11.9/2))", T);
8306 end if;
88b32fc3
BD
8307
8308 -- Some other kind of overriding failure
8309
8310 else
8311 Error_Msg_NE
8312 ("interface subprogram & must be overridden",
8313 T, Subp);
8f983e64
ES
8314
8315 -- Examine primitive operations of synchronized type,
8316 -- to find homonyms that have the wrong profile.
8317
8318 declare
8319 Prim : Entity_Id;
8320
8321 begin
8322 Prim :=
8323 First_Entity (Corresponding_Concurrent_Type (T));
8324 while Present (Prim) loop
8325 if Chars (Prim) = Chars (Subp) then
8326 Error_Msg_NE
8327 ("profile is not type conformant with "
8328 & "prefixed view profile of "
8329 & "inherited operation&", Prim, Subp);
8330 end if;
8331
8332 Next_Entity (Prim);
8333 end loop;
8334 end;
88b32fc3 8335 end if;
996ae0b0 8336 end if;
88b32fc3 8337
996ae0b0 8338 else
fea9e956
ES
8339 Error_Msg_Node_2 := T;
8340 Error_Msg_N
8341 ("abstract subprogram& not allowed for type&", Subp);
8342
8343 -- Also post unconditional warning on the type (unconditional
8344 -- so that if there are more than one of these cases, we get
8345 -- them all, and not just the first one).
8346
8347 Error_Msg_Node_2 := Subp;
8348 Error_Msg_N
8349 ("nonabstract type& has abstract subprogram&!", T);
996ae0b0
RK
8350 end if;
8351 end if;
8352
7d7af38a
JM
8353 -- Ada 2005 (AI05-0030): Inspect hidden subprograms which provide
8354 -- the mapping between interface and implementing type primitives.
8355 -- If the interface alias is marked as Implemented_By_Entry, the
8356 -- alias must be an entry wrapper.
8357
8358 if Ada_Version >= Ada_05
8359 and then Is_Hidden (Subp)
ce2b6ba5
JM
8360 and then Present (Interface_Alias (Subp))
8361 and then Implemented_By_Entry (Interface_Alias (Subp))
7d7af38a
JM
8362 and then Present (Alias_Subp)
8363 and then
8364 (not Is_Primitive_Wrapper (Alias_Subp)
8365 or else Ekind (Wrapped_Entity (Alias_Subp)) /= E_Entry)
8366 then
8367 declare
8368 Error_Ent : Entity_Id := T;
8369
8370 begin
8371 if Is_Concurrent_Record_Type (Error_Ent) then
8372 Error_Ent := Corresponding_Concurrent_Type (Error_Ent);
8373 end if;
8374
ce2b6ba5 8375 Error_Msg_Node_2 := Interface_Alias (Subp);
7d7af38a
JM
8376 Error_Msg_NE
8377 ("type & must implement abstract subprogram & with an entry",
8378 Error_Ent, Error_Ent);
8379 end;
8380 end if;
8381
8382 Next_Elmt (Elmt);
996ae0b0
RK
8383 end loop;
8384 end Check_Abstract_Overriding;
8385
8386 ------------------------------------------------
8387 -- Check_Access_Discriminant_Requires_Limited --
8388 ------------------------------------------------
8389
8390 procedure Check_Access_Discriminant_Requires_Limited
8391 (D : Node_Id;
8392 Loc : Node_Id)
8393 is
8394 begin
9dfd2ff8
CC
8395 -- A discriminant_specification for an access discriminant shall appear
8396 -- only in the declaration for a task or protected type, or for a type
8397 -- with the reserved word 'limited' in its definition or in one of its
8398 -- ancestors. (RM 3.7(10))
996ae0b0
RK
8399
8400 if Nkind (Discriminant_Type (D)) = N_Access_Definition
8401 and then not Is_Concurrent_Type (Current_Scope)
8402 and then not Is_Concurrent_Record_Type (Current_Scope)
8403 and then not Is_Limited_Record (Current_Scope)
8404 and then Ekind (Current_Scope) /= E_Limited_Private_Type
8405 then
8406 Error_Msg_N
8407 ("access discriminants allowed only for limited types", Loc);
8408 end if;
8409 end Check_Access_Discriminant_Requires_Limited;
8410
8411 -----------------------------------
8412 -- Check_Aliased_Component_Types --
8413 -----------------------------------
8414
8415 procedure Check_Aliased_Component_Types (T : Entity_Id) is
8416 C : Entity_Id;
8417
8418 begin
a5b62485
AC
8419 -- ??? Also need to check components of record extensions, but not
8420 -- components of protected types (which are always limited).
996ae0b0 8421
9dfd2ff8
CC
8422 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
8423 -- types to be unconstrained. This is safe because it is illegal to
8424 -- create access subtypes to such types with explicit discriminant
8425 -- constraints.
758c442c 8426
996ae0b0
RK
8427 if not Is_Limited_Type (T) then
8428 if Ekind (T) = E_Record_Type then
8429 C := First_Component (T);
8430 while Present (C) loop
8431 if Is_Aliased (C)
8432 and then Has_Discriminants (Etype (C))
8433 and then not Is_Constrained (Etype (C))
950d3e7d 8434 and then not In_Instance_Body
758c442c 8435 and then Ada_Version < Ada_05
996ae0b0
RK
8436 then
8437 Error_Msg_N
dc06abec 8438 ("aliased component must be constrained (RM 3.6(11))",
996ae0b0
RK
8439 C);
8440 end if;
8441
8442 Next_Component (C);
8443 end loop;
8444
8445 elsif Ekind (T) = E_Array_Type then
8446 if Has_Aliased_Components (T)
8447 and then Has_Discriminants (Component_Type (T))
8448 and then not Is_Constrained (Component_Type (T))
950d3e7d
ES
8449 and then not In_Instance_Body
8450 and then Ada_Version < Ada_05
996ae0b0
RK
8451 then
8452 Error_Msg_N
dc06abec 8453 ("aliased component type must be constrained (RM 3.6(11))",
996ae0b0
RK
8454 T);
8455 end if;
8456 end if;
8457 end if;
8458 end Check_Aliased_Component_Types;
8459
8460 ----------------------
8461 -- Check_Completion --
8462 ----------------------
8463
8464 procedure Check_Completion (Body_Id : Node_Id := Empty) is
8465 E : Entity_Id;
8466
8467 procedure Post_Error;
8468 -- Post error message for lack of completion for entity E
8469
fbf5a39b
AC
8470 ----------------
8471 -- Post_Error --
8472 ----------------
8473
996ae0b0 8474 procedure Post_Error is
b568955d
AC
8475
8476 procedure Missing_Body;
8477 -- Output missing body message
8478
8479 ------------------
8480 -- Missing_Body --
8481 ------------------
8482
8483 procedure Missing_Body is
8484 begin
8485 -- Spec is in same unit, so we can post on spec
8486
8487 if In_Same_Source_Unit (Body_Id, E) then
8488 Error_Msg_N ("missing body for &", E);
8489
8490 -- Spec is in a separate unit, so we have to post on the body
8491
8492 else
8493 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
8494 end if;
8495 end Missing_Body;
8496
8497 -- Start of processing for Post_Error
8498
996ae0b0
RK
8499 begin
8500 if not Comes_From_Source (E) then
8501
fbf5a39b
AC
8502 if Ekind (E) = E_Task_Type
8503 or else Ekind (E) = E_Protected_Type
996ae0b0
RK
8504 then
8505 -- It may be an anonymous protected type created for a
8506 -- single variable. Post error on variable, if present.
8507
8508 declare
8509 Var : Entity_Id;
8510
8511 begin
8512 Var := First_Entity (Current_Scope);
996ae0b0
RK
8513 while Present (Var) loop
8514 exit when Etype (Var) = E
8515 and then Comes_From_Source (Var);
8516
8517 Next_Entity (Var);
8518 end loop;
8519
8520 if Present (Var) then
8521 E := Var;
8522 end if;
8523 end;
8524 end if;
8525 end if;
8526
8527 -- If a generated entity has no completion, then either previous
a5b62485 8528 -- semantic errors have disabled the expansion phase, or else we had
fea9e956 8529 -- missing subunits, or else we are compiling without expansion,
a5b62485 8530 -- or else something is very wrong.
996ae0b0
RK
8531
8532 if not Comes_From_Source (E) then
8533 pragma Assert
07fc65c4 8534 (Serious_Errors_Detected > 0
fbf5a39b 8535 or else Configurable_Run_Time_Violations > 0
996ae0b0
RK
8536 or else Subunits_Missing
8537 or else not Expander_Active);
8538 return;
8539
8540 -- Here for source entity
8541
8542 else
8543 -- Here if no body to post the error message, so we post the error
8544 -- on the declaration that has no completion. This is not really
8545 -- the right place to post it, think about this later ???
8546
8547 if No (Body_Id) then
8548 if Is_Type (E) then
8549 Error_Msg_NE
8550 ("missing full declaration for }", Parent (E), E);
8551 else
8552 Error_Msg_NE
8553 ("missing body for &", Parent (E), E);
8554 end if;
8555
8556 -- Package body has no completion for a declaration that appears
8557 -- in the corresponding spec. Post error on the body, with a
8558 -- reference to the non-completed declaration.
8559
8560 else
8561 Error_Msg_Sloc := Sloc (E);
8562
8563 if Is_Type (E) then
8564 Error_Msg_NE
8565 ("missing full declaration for }!", Body_Id, E);
8566
8567 elsif Is_Overloadable (E)
8568 and then Current_Entity_In_Scope (E) /= E
8569 then
7d7af38a
JM
8570 -- It may be that the completion is mistyped and appears as
8571 -- a distinct overloading of the entity.
996ae0b0
RK
8572
8573 declare
fbf5a39b
AC
8574 Candidate : constant Entity_Id :=
8575 Current_Entity_In_Scope (E);
8576 Decl : constant Node_Id :=
8577 Unit_Declaration_Node (Candidate);
996ae0b0
RK
8578
8579 begin
8580 if Is_Overloadable (Candidate)
8581 and then Ekind (Candidate) = Ekind (E)
8582 and then Nkind (Decl) = N_Subprogram_Body
8583 and then Acts_As_Spec (Decl)
8584 then
8585 Check_Type_Conformant (Candidate, E);
8586
8587 else
b568955d 8588 Missing_Body;
996ae0b0
RK
8589 end if;
8590 end;
b568955d 8591
996ae0b0 8592 else
b568955d 8593 Missing_Body;
996ae0b0
RK
8594 end if;
8595 end if;
8596 end if;
8597 end Post_Error;
8598
d8221f45 8599 -- Start of processing for Check_Completion
996ae0b0
RK
8600
8601 begin
8602 E := First_Entity (Current_Scope);
8603 while Present (E) loop
8604 if Is_Intrinsic_Subprogram (E) then
8605 null;
8606
7d7af38a
JM
8607 -- The following situation requires special handling: a child unit
8608 -- that appears in the context clause of the body of its parent:
996ae0b0
RK
8609
8610 -- procedure Parent.Child (...);
a5b62485 8611
996ae0b0
RK
8612 -- with Parent.Child;
8613 -- package body Parent is
8614
7d7af38a
JM
8615 -- Here Parent.Child appears as a local entity, but should not be
8616 -- flagged as requiring completion, because it is a compilation
8617 -- unit.
996ae0b0 8618
fea9e956
ES
8619 -- Ignore missing completion for a subprogram that does not come from
8620 -- source (including the _Call primitive operation of RAS types,
8621 -- which has to have the flag Comes_From_Source for other purposes):
8622 -- we assume that the expander will provide the missing completion.
e1f3cb58 8623 -- In case of previous errors, other expansion actions that provide
d6533e74 8624 -- bodies for null procedures with not be invoked, so inhibit message
e1f3cb58 8625 -- in those cases.
d6533e74
RD
8626 -- Note that E_Operator is not in the list that follows, because
8627 -- this kind is reserved for predefined operators, that are
8628 -- intrinsic and do not need completion.
fea9e956 8629
996ae0b0
RK
8630 elsif Ekind (E) = E_Function
8631 or else Ekind (E) = E_Procedure
8632 or else Ekind (E) = E_Generic_Function
8633 or else Ekind (E) = E_Generic_Procedure
8634 then
e1f3cb58
AC
8635 if Has_Completion (E) then
8636 null;
8637
8638 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
8639 null;
8640
8641 elsif Is_Subprogram (E)
8642 and then (not Comes_From_Source (E)
8643 or else Chars (E) = Name_uCall)
8644 then
8645 null;
8646
8647 elsif
8648 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
8649 then
8650 null;
8651
8652 elsif Nkind (Parent (E)) = N_Procedure_Specification
8653 and then Null_Present (Parent (E))
8654 and then Serious_Errors_Detected > 0
996ae0b0 8655 then
e1f3cb58
AC
8656 null;
8657
8658 else
996ae0b0
RK
8659 Post_Error;
8660 end if;
8661
8662 elsif Is_Entry (E) then
8663 if not Has_Completion (E) and then
8664 (Ekind (Scope (E)) = E_Protected_Object
8665 or else Ekind (Scope (E)) = E_Protected_Type)
8666 then
8667 Post_Error;
8668 end if;
8669
950d3e7d 8670 elsif Is_Package_Or_Generic_Package (E) then
996ae0b0
RK
8671 if Unit_Requires_Body (E) then
8672 if not Has_Completion (E)
8673 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8674 N_Compilation_Unit
8675 then
8676 Post_Error;
8677 end if;
8678
8679 elsif not Is_Child_Unit (E) then
8680 May_Need_Implicit_Body (E);
8681 end if;
8682
8683 elsif Ekind (E) = E_Incomplete_Type
8684 and then No (Underlying_Type (E))
8685 then
8686 Post_Error;
8687
8688 elsif (Ekind (E) = E_Task_Type or else
8689 Ekind (E) = E_Protected_Type)
8690 and then not Has_Completion (E)
8691 then
8692 Post_Error;
8693
a5b62485
AC
8694 -- A single task declared in the current scope is a constant, verify
8695 -- that the body of its anonymous type is in the same scope. If the
8696 -- task is defined elsewhere, this may be a renaming declaration for
fbf5a39b
AC
8697 -- which no completion is needed.
8698
996ae0b0
RK
8699 elsif Ekind (E) = E_Constant
8700 and then Ekind (Etype (E)) = E_Task_Type
8701 and then not Has_Completion (Etype (E))
fbf5a39b 8702 and then Scope (Etype (E)) = Current_Scope
996ae0b0
RK
8703 then
8704 Post_Error;
8705
8706 elsif Ekind (E) = E_Protected_Object
8707 and then not Has_Completion (Etype (E))
8708 then
8709 Post_Error;
8710
8711 elsif Ekind (E) = E_Record_Type then
8712 if Is_Tagged_Type (E) then
8713 Check_Abstract_Overriding (E);
88b32fc3 8714 Check_Conventions (E);
996ae0b0
RK
8715 end if;
8716
8717 Check_Aliased_Component_Types (E);
8718
8719 elsif Ekind (E) = E_Array_Type then
8720 Check_Aliased_Component_Types (E);
8721
8722 end if;
8723
8724 Next_Entity (E);
8725 end loop;
8726 end Check_Completion;
8727
8728 ----------------------------
8729 -- Check_Delta_Expression --
8730 ----------------------------
8731
8732 procedure Check_Delta_Expression (E : Node_Id) is
8733 begin
8734 if not (Is_Real_Type (Etype (E))) then
8735 Wrong_Type (E, Any_Real);
8736
8737 elsif not Is_OK_Static_Expression (E) then
fbf5a39b
AC
8738 Flag_Non_Static_Expr
8739 ("non-static expression used for delta value!", E);
996ae0b0
RK
8740
8741 elsif not UR_Is_Positive (Expr_Value_R (E)) then
8742 Error_Msg_N ("delta expression must be positive", E);
8743
8744 else
8745 return;
8746 end if;
8747
8748 -- If any of above errors occurred, then replace the incorrect
8749 -- expression by the real 0.1, which should prevent further errors.
8750
8751 Rewrite (E,
8752 Make_Real_Literal (Sloc (E), Ureal_Tenth));
8753 Analyze_And_Resolve (E, Standard_Float);
996ae0b0
RK
8754 end Check_Delta_Expression;
8755
8756 -----------------------------
8757 -- Check_Digits_Expression --
8758 -----------------------------
8759
8760 procedure Check_Digits_Expression (E : Node_Id) is
8761 begin
8762 if not (Is_Integer_Type (Etype (E))) then
8763 Wrong_Type (E, Any_Integer);
8764
8765 elsif not Is_OK_Static_Expression (E) then
fbf5a39b
AC
8766 Flag_Non_Static_Expr
8767 ("non-static expression used for digits value!", E);
996ae0b0
RK
8768
8769 elsif Expr_Value (E) <= 0 then
8770 Error_Msg_N ("digits value must be greater than zero", E);
8771
8772 else
8773 return;
8774 end if;
8775
8776 -- If any of above errors occurred, then replace the incorrect
8777 -- expression by the integer 1, which should prevent further errors.
8778
8779 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
8780 Analyze_And_Resolve (E, Standard_Integer);
8781
8782 end Check_Digits_Expression;
8783
996ae0b0
RK
8784 --------------------------
8785 -- Check_Initialization --
8786 --------------------------
8787
8788 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
8789 begin
88b32fc3 8790 if Is_Limited_Type (T)
996ae0b0 8791 and then not In_Instance
c45b6ae0 8792 and then not In_Inlined_Body
996ae0b0 8793 then
2a31c32b 8794 if not OK_For_Limited_Init (T, Exp) then
71f62180
ES
8795
8796 -- In GNAT mode, this is just a warning, to allow it to be evilly
8797 -- turned off. Otherwise it is a real error.
65356e64 8798
88b32fc3
BD
8799 if GNAT_Mode then
8800 Error_Msg_N
71f62180
ES
8801 ("?cannot initialize entities of limited type!", Exp);
8802
8803 elsif Ada_Version < Ada_05 then
88b32fc3
BD
8804 Error_Msg_N
8805 ("cannot initialize entities of limited type", Exp);
8806 Explain_Limited_Type (T, Exp);
71f62180
ES
8807
8808 else
8809 -- Specialize error message according to kind of illegal
8810 -- initial expression.
8811
8812 if Nkind (Exp) = N_Type_Conversion
8813 and then Nkind (Expression (Exp)) = N_Function_Call
8814 then
8815 Error_Msg_N
8816 ("illegal context for call"
8817 & " to function with limited result", Exp);
8818
8819 else
8820 Error_Msg_N
7d7af38a 8821 ("initialization of limited object requires aggregate "
71f62180
ES
8822 & "or function call", Exp);
8823 end if;
88b32fc3 8824 end if;
65356e64 8825 end if;
996ae0b0
RK
8826 end if;
8827 end Check_Initialization;
8828
ce2b6ba5
JM
8829 ----------------------
8830 -- Check_Interfaces --
8831 ----------------------
8832
8833 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
8834 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
8835
8836 Iface : Node_Id;
8837 Iface_Def : Node_Id;
8838 Iface_Typ : Entity_Id;
8839 Parent_Node : Node_Id;
8840
8841 Is_Task : Boolean := False;
8842 -- Set True if parent type or any progenitor is a task interface
8843
8844 Is_Protected : Boolean := False;
8845 -- Set True if parent type or any progenitor is a protected interface
8846
8847 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
8848 -- Check that a progenitor is compatible with declaration.
8849 -- Error is posted on Error_Node.
8850
8851 ------------------
8852 -- Check_Ifaces --
8853 ------------------
8854
8855 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
8856 Iface_Id : constant Entity_Id :=
8857 Defining_Identifier (Parent (Iface_Def));
8858 Type_Def : Node_Id;
8859
8860 begin
8861 if Nkind (N) = N_Private_Extension_Declaration then
8862 Type_Def := N;
8863 else
8864 Type_Def := Type_Definition (N);
8865 end if;
8866
8867 if Is_Task_Interface (Iface_Id) then
8868 Is_Task := True;
8869
8870 elsif Is_Protected_Interface (Iface_Id) then
8871 Is_Protected := True;
8872 end if;
8873
443614e3
AC
8874 if Is_Synchronized_Interface (Iface_Id) then
8875
8876 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
8877 -- extension derived from a synchronized interface must explicitly
8878 -- be declared synchronized, because the full view will be a
8879 -- synchronized type.
8880
8881 if Nkind (N) = N_Private_Extension_Declaration then
8882 if not Synchronized_Present (N) then
8883 Error_Msg_NE
8884 ("private extension of& must be explicitly synchronized",
8885 N, Iface_Id);
8886 end if;
8887
8888 -- However, by 3.9.4(16/2), a full type that is a record extension
8889 -- is never allowed to derive from a synchronized interface (note
8890 -- that interfaces must be excluded from this check, because those
8891 -- are represented by derived type definitions in some cases).
8892
8893 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
8894 and then not Interface_Present (Type_Definition (N))
8895 then
8896 Error_Msg_N ("record extension cannot derive from synchronized"
8897 & " interface", Error_Node);
8898 end if;
8899 end if;
8900
ce2b6ba5
JM
8901 -- Check that the characteristics of the progenitor are compatible
8902 -- with the explicit qualifier in the declaration.
8903 -- The check only applies to qualifiers that come from source.
8904 -- Limited_Present also appears in the declaration of corresponding
8905 -- records, and the check does not apply to them.
8906
8907 if Limited_Present (Type_Def)
8908 and then not
8909 Is_Concurrent_Record_Type (Defining_Identifier (N))
8910 then
8911 if Is_Limited_Interface (Parent_Type)
8912 and then not Is_Limited_Interface (Iface_Id)
8913 then
8914 Error_Msg_NE
8915 ("progenitor& must be limited interface",
8916 Error_Node, Iface_Id);
8917
8918 elsif
8919 (Task_Present (Iface_Def)
8920 or else Protected_Present (Iface_Def)
8921 or else Synchronized_Present (Iface_Def))
8922 and then Nkind (N) /= N_Private_Extension_Declaration
e358346d 8923 and then not Error_Posted (N)
ce2b6ba5
JM
8924 then
8925 Error_Msg_NE
8926 ("progenitor& must be limited interface",
8927 Error_Node, Iface_Id);
8928 end if;
8929
8930 -- Protected interfaces can only inherit from limited, synchronized
8931 -- or protected interfaces.
8932
8933 elsif Nkind (N) = N_Full_Type_Declaration
8934 and then Protected_Present (Type_Def)
8935 then
8936 if Limited_Present (Iface_Def)
8937 or else Synchronized_Present (Iface_Def)
8938 or else Protected_Present (Iface_Def)
8939 then
8940 null;
8941
8942 elsif Task_Present (Iface_Def) then
8943 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8944 & " from task interface", Error_Node);
8945
8946 else
8947 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8948 & " from non-limited interface", Error_Node);
8949 end if;
8950
8951 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
8952 -- limited and synchronized.
8953
8954 elsif Synchronized_Present (Type_Def) then
8955 if Limited_Present (Iface_Def)
8956 or else Synchronized_Present (Iface_Def)
8957 then
8958 null;
8959
8960 elsif Protected_Present (Iface_Def)
8961 and then Nkind (N) /= N_Private_Extension_Declaration
8962 then
8963 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8964 & " from protected interface", Error_Node);
8965
8966 elsif Task_Present (Iface_Def)
8967 and then Nkind (N) /= N_Private_Extension_Declaration
8968 then
8969 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8970 & " from task interface", Error_Node);
8971
8972 elsif not Is_Limited_Interface (Iface_Id) then
8973 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8974 & " from non-limited interface", Error_Node);
8975 end if;
8976
8977 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
8978 -- synchronized or task interfaces.
8979
8980 elsif Nkind (N) = N_Full_Type_Declaration
8981 and then Task_Present (Type_Def)
8982 then
8983 if Limited_Present (Iface_Def)
8984 or else Synchronized_Present (Iface_Def)
8985 or else Task_Present (Iface_Def)
8986 then
8987 null;
8988
8989 elsif Protected_Present (Iface_Def) then
8990 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8991 & " protected interface", Error_Node);
8992
8993 else
8994 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8995 & " non-limited interface", Error_Node);
8996 end if;
8997 end if;
8998 end Check_Ifaces;
8999
9000 -- Start of processing for Check_Interfaces
9001
9002 begin
9003 if Is_Interface (Parent_Type) then
9004 if Is_Task_Interface (Parent_Type) then
9005 Is_Task := True;
9006
9007 elsif Is_Protected_Interface (Parent_Type) then
9008 Is_Protected := True;
9009 end if;
9010 end if;
9011
9012 if Nkind (N) = N_Private_Extension_Declaration then
9013
9014 -- Check that progenitors are compatible with declaration
9015
9016 Iface := First (Interface_List (Def));
9017 while Present (Iface) loop
9018 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9019
9020 Parent_Node := Parent (Base_Type (Iface_Typ));
9021 Iface_Def := Type_Definition (Parent_Node);
9022
9023 if not Is_Interface (Iface_Typ) then
6765b310 9024 Diagnose_Interface (Iface, Iface_Typ);
ce2b6ba5
JM
9025
9026 else
9027 Check_Ifaces (Iface_Def, Iface);
9028 end if;
9029
9030 Next (Iface);
9031 end loop;
9032
9033 if Is_Task and Is_Protected then
9034 Error_Msg_N
9035 ("type cannot derive from task and protected interface", N);
9036 end if;
9037
9038 return;
9039 end if;
9040
9041 -- Full type declaration of derived type.
9042 -- Check compatibility with parent if it is interface type
9043
9044 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9045 and then Is_Interface (Parent_Type)
9046 then
9047 Parent_Node := Parent (Parent_Type);
9048
9049 -- More detailed checks for interface varieties
9050
9051 Check_Ifaces
9052 (Iface_Def => Type_Definition (Parent_Node),
9053 Error_Node => Subtype_Indication (Type_Definition (N)));
9054 end if;
9055
9056 Iface := First (Interface_List (Def));
9057 while Present (Iface) loop
9058 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9059
9060 Parent_Node := Parent (Base_Type (Iface_Typ));
9061 Iface_Def := Type_Definition (Parent_Node);
9062
9063 if not Is_Interface (Iface_Typ) then
6765b310 9064 Diagnose_Interface (Iface, Iface_Typ);
ce2b6ba5
JM
9065
9066 else
9067 -- "The declaration of a specific descendant of an interface
9068 -- type freezes the interface type" RM 13.14
9069
9070 Freeze_Before (N, Iface_Typ);
9071 Check_Ifaces (Iface_Def, Error_Node => Iface);
9072 end if;
9073
9074 Next (Iface);
9075 end loop;
9076
9077 if Is_Task and Is_Protected then
9078 Error_Msg_N
9079 ("type cannot derive from task and protected interface", N);
9080 end if;
9081 end Check_Interfaces;
9082
996ae0b0
RK
9083 ------------------------------------
9084 -- Check_Or_Process_Discriminants --
9085 ------------------------------------
9086
9dfd2ff8
CC
9087 -- If an incomplete or private type declaration was already given for the
9088 -- type, the discriminants may have already been processed if they were
9089 -- present on the incomplete declaration. In this case a full conformance
9090 -- check is performed otherwise just process them.
996ae0b0 9091
fbf5a39b
AC
9092 procedure Check_Or_Process_Discriminants
9093 (N : Node_Id;
9094 T : Entity_Id;
9095 Prev : Entity_Id := Empty)
9096 is
996ae0b0
RK
9097 begin
9098 if Has_Discriminants (T) then
9099
ffe9aba8 9100 -- Make the discriminants visible to component declarations
996ae0b0
RK
9101
9102 declare
9dfd2ff8 9103 D : Entity_Id;
996ae0b0
RK
9104 Prev : Entity_Id;
9105
9106 begin
9dfd2ff8 9107 D := First_Discriminant (T);
996ae0b0
RK
9108 while Present (D) loop
9109 Prev := Current_Entity (D);
9110 Set_Current_Entity (D);
9111 Set_Is_Immediately_Visible (D);
9112 Set_Homonym (D, Prev);
9113
0ab80019
AC
9114 -- Ada 2005 (AI-230): Access discriminant allowed in
9115 -- non-limited record types.
996ae0b0 9116
0ab80019 9117 if Ada_Version < Ada_05 then
6e937c1c 9118
9dfd2ff8
CC
9119 -- This restriction gets applied to the full type here. It
9120 -- has already been applied earlier to the partial view.
6e937c1c
AC
9121
9122 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
9123 end if;
996ae0b0
RK
9124
9125 Next_Discriminant (D);
9126 end loop;
9127 end;
9128
9129 elsif Present (Discriminant_Specifications (N)) then
fbf5a39b 9130 Process_Discriminants (N, Prev);
996ae0b0
RK
9131 end if;
9132 end Check_Or_Process_Discriminants;
9133
9134 ----------------------
9135 -- Check_Real_Bound --
9136 ----------------------
9137
9138 procedure Check_Real_Bound (Bound : Node_Id) is
9139 begin
9140 if not Is_Real_Type (Etype (Bound)) then
9141 Error_Msg_N
9142 ("bound in real type definition must be of real type", Bound);
9143
9144 elsif not Is_OK_Static_Expression (Bound) then
fbf5a39b
AC
9145 Flag_Non_Static_Expr
9146 ("non-static expression used for real type bound!", Bound);
996ae0b0
RK
9147
9148 else
9149 return;
9150 end if;
9151
9152 Rewrite
9153 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
9154 Analyze (Bound);
9155 Resolve (Bound, Standard_Float);
9156 end Check_Real_Bound;
9157
758c442c
GD
9158 ------------------------------
9159 -- Complete_Private_Subtype --
9160 ------------------------------
9161
9162 procedure Complete_Private_Subtype
9163 (Priv : Entity_Id;
9164 Full : Entity_Id;
9165 Full_Base : Entity_Id;
9166 Related_Nod : Node_Id)
9167 is
9168 Save_Next_Entity : Entity_Id;
9169 Save_Homonym : Entity_Id;
9170
9171 begin
9172 -- Set semantic attributes for (implicit) private subtype completion.
9173 -- If the full type has no discriminants, then it is a copy of the full
9174 -- view of the base. Otherwise, it is a subtype of the base with a
9175 -- possible discriminant constraint. Save and restore the original
9176 -- Next_Entity field of full to ensure that the calls to Copy_Node
9177 -- do not corrupt the entity chain.
9178
9dfd2ff8
CC
9179 -- Note that the type of the full view is the same entity as the type of
9180 -- the partial view. In this fashion, the subtype has access to the
9181 -- correct view of the parent.
996ae0b0
RK
9182
9183 Save_Next_Entity := Next_Entity (Full);
9184 Save_Homonym := Homonym (Priv);
9185
9186 case Ekind (Full_Base) is
996ae0b0
RK
9187 when E_Record_Type |
9188 E_Record_Subtype |
9189 Class_Wide_Kind |
9190 Private_Kind |
9191 Task_Kind |
9192 Protected_Kind =>
9193 Copy_Node (Priv, Full);
9194
9195 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
9196 Set_First_Entity (Full, First_Entity (Full_Base));
9197 Set_Last_Entity (Full, Last_Entity (Full_Base));
9198
9199 when others =>
9200 Copy_Node (Full_Base, Full);
9201 Set_Chars (Full, Chars (Priv));
9202 Conditional_Delay (Full, Priv);
9203 Set_Sloc (Full, Sloc (Priv));
996ae0b0
RK
9204 end case;
9205
9206 Set_Next_Entity (Full, Save_Next_Entity);
9207 Set_Homonym (Full, Save_Homonym);
9208 Set_Associated_Node_For_Itype (Full, Related_Nod);
9209
71d9e9f2 9210 -- Set common attributes for all subtypes
996ae0b0
RK
9211
9212 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
9213
9214 -- The Etype of the full view is inconsistent. Gigi needs to see the
9215 -- structural full view, which is what the current scheme gives:
9216 -- the Etype of the full view is the etype of the full base. However,
9217 -- if the full base is a derived type, the full view then looks like
9218 -- a subtype of the parent, not a subtype of the full base. If instead
9219 -- we write:
9220
9221 -- Set_Etype (Full, Full_Base);
9222
9223 -- then we get inconsistencies in the front-end (confusion between
71d9e9f2 9224 -- views). Several outstanding bugs are related to this ???
996ae0b0
RK
9225
9226 Set_Is_First_Subtype (Full, False);
9227 Set_Scope (Full, Scope (Priv));
9228 Set_Size_Info (Full, Full_Base);
9229 Set_RM_Size (Full, RM_Size (Full_Base));
9230 Set_Is_Itype (Full);
9231
9232 -- A subtype of a private-type-without-discriminants, whose full-view
9233 -- has discriminants with default expressions, is not constrained!
9234
9235 if not Has_Discriminants (Priv) then
9236 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
fbf5a39b
AC
9237
9238 if Has_Discriminants (Full_Base) then
9239 Set_Discriminant_Constraint
9240 (Full, Discriminant_Constraint (Full_Base));
35ae2ed8
AC
9241
9242 -- The partial view may have been indefinite, the full view
9243 -- might not be.
9244
9245 Set_Has_Unknown_Discriminants
9246 (Full, Has_Unknown_Discriminants (Full_Base));
fbf5a39b 9247 end if;
996ae0b0
RK
9248 end if;
9249
9250 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
9251 Set_Depends_On_Private (Full, Has_Private_Component (Full));
9252
a5b62485
AC
9253 -- Freeze the private subtype entity if its parent is delayed, and not
9254 -- already frozen. We skip this processing if the type is an anonymous
9255 -- subtype of a record component, or is the corresponding record of a
9256 -- protected type, since ???
996ae0b0
RK
9257
9258 if not Is_Type (Scope (Full)) then
9259 Set_Has_Delayed_Freeze (Full,
9260 Has_Delayed_Freeze (Full_Base)
71d9e9f2 9261 and then (not Is_Frozen (Full_Base)));
996ae0b0
RK
9262 end if;
9263
9264 Set_Freeze_Node (Full, Empty);
9265 Set_Is_Frozen (Full, False);
9266 Set_Full_View (Priv, Full);
9267
9268 if Has_Discriminants (Full) then
fbf5a39b
AC
9269 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
9270 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
71d9e9f2 9271
996ae0b0
RK
9272 if Has_Unknown_Discriminants (Full) then
9273 Set_Discriminant_Constraint (Full, No_Elist);
9274 end if;
9275 end if;
9276
9277 if Ekind (Full_Base) = E_Record_Type
9278 and then Has_Discriminants (Full_Base)
9279 and then Has_Discriminants (Priv) -- might not, if errors
e6f69614 9280 and then not Has_Unknown_Discriminants (Priv)
996ae0b0
RK
9281 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
9282 then
9283 Create_Constrained_Components
9284 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
9285
9286 -- If the full base is itself derived from private, build a congruent
244e5a2c
AC
9287 -- subtype of its underlying type, for use by the back end. For a
9288 -- constrained record component, the declaration cannot be placed on
9dfd2ff8
CC
9289 -- the component list, but it must nevertheless be built an analyzed, to
9290 -- supply enough information for Gigi to compute the size of component.
996ae0b0
RK
9291
9292 elsif Ekind (Full_Base) in Private_Kind
9293 and then Is_Derived_Type (Full_Base)
9294 and then Has_Discriminants (Full_Base)
24105bab 9295 and then (Ekind (Current_Scope) /= E_Record_Subtype)
996ae0b0 9296 then
244e5a2c
AC
9297 if not Is_Itype (Priv)
9298 and then
9299 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
9300 then
9301 Build_Underlying_Full_View
9302 (Parent (Priv), Full, Etype (Full_Base));
9303
9304 elsif Nkind (Related_Nod) = N_Component_Declaration then
9305 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
9306 end if;
996ae0b0
RK
9307
9308 elsif Is_Record_Type (Full_Base) then
9309
71d9e9f2 9310 -- Show Full is simply a renaming of Full_Base
996ae0b0
RK
9311
9312 Set_Cloned_Subtype (Full, Full_Base);
9313 end if;
9314
a5b62485
AC
9315 -- It is unsafe to share to bounds of a scalar type, because the Itype
9316 -- is elaborated on demand, and if a bound is non-static then different
9317 -- orders of elaboration in different units will lead to different
9318 -- external symbols.
996ae0b0
RK
9319
9320 if Is_Scalar_Type (Full_Base) then
9321 Set_Scalar_Range (Full,
9322 Make_Range (Sloc (Related_Nod),
fbf5a39b
AC
9323 Low_Bound =>
9324 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
9325 High_Bound =>
9326 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
9327
9328 -- This completion inherits the bounds of the full parent, but if
9329 -- the parent is an unconstrained floating point type, so is the
9330 -- completion.
9331
9332 if Is_Floating_Point_Type (Full_Base) then
9333 Set_Includes_Infinities
9334 (Scalar_Range (Full), Has_Infinities (Full_Base));
9335 end if;
996ae0b0
RK
9336 end if;
9337
a5b62485
AC
9338 -- ??? It seems that a lot of fields are missing that should be copied
9339 -- from Full_Base to Full. Here are some that are introduced in a
9340 -- non-disruptive way but a cleanup is necessary.
996ae0b0
RK
9341
9342 if Is_Tagged_Type (Full_Base) then
9343 Set_Is_Tagged_Type (Full);
9344 Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
fbf5a39b 9345 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
996ae0b0 9346
fc4039b9
ES
9347 -- If this is a subtype of a protected or task type, constrain its
9348 -- corresponding record, unless this is a subtype without constraints,
9349 -- i.e. a simple renaming as with an actual subtype in an instance.
9350
996ae0b0 9351 elsif Is_Concurrent_Type (Full_Base) then
996ae0b0
RK
9352 if Has_Discriminants (Full)
9353 and then Present (Corresponding_Record_Type (Full_Base))
fc4039b9
ES
9354 and then
9355 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
996ae0b0
RK
9356 then
9357 Set_Corresponding_Record_Type (Full,
9358 Constrain_Corresponding_Record
9359 (Full, Corresponding_Record_Type (Full_Base),
9360 Related_Nod, Full_Base));
9361
9362 else
9363 Set_Corresponding_Record_Type (Full,
9364 Corresponding_Record_Type (Full_Base));
9365 end if;
9366 end if;
996ae0b0
RK
9367 end Complete_Private_Subtype;
9368
9369 ----------------------------
9370 -- Constant_Redeclaration --
9371 ----------------------------
9372
9373 procedure Constant_Redeclaration
9374 (Id : Entity_Id;
9375 N : Node_Id;
9376 T : out Entity_Id)
9377 is
9378 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
9379 Obj_Def : constant Node_Id := Object_Definition (N);
9380 New_T : Entity_Id;
9381
57193e09
TQ
9382 procedure Check_Possible_Deferred_Completion
9383 (Prev_Id : Entity_Id;
9384 Prev_Obj_Def : Node_Id;
9385 Curr_Obj_Def : Node_Id);
9386 -- Determine whether the two object definitions describe the partial
9387 -- and the full view of a constrained deferred constant. Generate
9388 -- a subtype for the full view and verify that it statically matches
9389 -- the subtype of the partial view.
9390
07fc65c4 9391 procedure Check_Recursive_Declaration (Typ : Entity_Id);
9dfd2ff8
CC
9392 -- If deferred constant is an access type initialized with an allocator,
9393 -- check whether there is an illegal recursion in the definition,
9394 -- through a default value of some record subcomponent. This is normally
9395 -- detected when generating init procs, but requires this additional
9396 -- mechanism when expansion is disabled.
07fc65c4 9397
57193e09
TQ
9398 ----------------------------------------
9399 -- Check_Possible_Deferred_Completion --
9400 ----------------------------------------
9401
9402 procedure Check_Possible_Deferred_Completion
9403 (Prev_Id : Entity_Id;
9404 Prev_Obj_Def : Node_Id;
9405 Curr_Obj_Def : Node_Id)
9406 is
9407 begin
9408 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
9409 and then Present (Constraint (Prev_Obj_Def))
9410 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
9411 and then Present (Constraint (Curr_Obj_Def))
9412 then
9413 declare
9414 Loc : constant Source_Ptr := Sloc (N);
9415 Def_Id : constant Entity_Id :=
9416 Make_Defining_Identifier (Loc,
9417 New_Internal_Name ('S'));
9418 Decl : constant Node_Id :=
9419 Make_Subtype_Declaration (Loc,
9420 Defining_Identifier =>
9421 Def_Id,
9422 Subtype_Indication =>
9423 Relocate_Node (Curr_Obj_Def));
9424
9425 begin
9426 Insert_Before_And_Analyze (N, Decl);
9427 Set_Etype (Id, Def_Id);
9428
9429 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
9430 Error_Msg_Sloc := Sloc (Prev_Id);
9431 Error_Msg_N ("subtype does not statically match deferred " &
9432 "declaration#", N);
9433 end if;
9434 end;
9435 end if;
9436 end Check_Possible_Deferred_Completion;
9437
fbf5a39b
AC
9438 ---------------------------------
9439 -- Check_Recursive_Declaration --
9440 ---------------------------------
9441
07fc65c4
GB
9442 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
9443 Comp : Entity_Id;
9444
9445 begin
9446 if Is_Record_Type (Typ) then
9447 Comp := First_Component (Typ);
07fc65c4
GB
9448 while Present (Comp) loop
9449 if Comes_From_Source (Comp) then
9450 if Present (Expression (Parent (Comp)))
9451 and then Is_Entity_Name (Expression (Parent (Comp)))
9452 and then Entity (Expression (Parent (Comp))) = Prev
9453 then
9454 Error_Msg_Sloc := Sloc (Parent (Comp));
9455 Error_Msg_NE
9456 ("illegal circularity with declaration for&#",
9457 N, Comp);
9458 return;
9459
9460 elsif Is_Record_Type (Etype (Comp)) then
9461 Check_Recursive_Declaration (Etype (Comp));
9462 end if;
9463 end if;
9464
9465 Next_Component (Comp);
9466 end loop;
9467 end if;
9468 end Check_Recursive_Declaration;
9469
9470 -- Start of processing for Constant_Redeclaration
9471
996ae0b0
RK
9472 begin
9473 if Nkind (Parent (Prev)) = N_Object_Declaration then
9474 if Nkind (Object_Definition
9475 (Parent (Prev))) = N_Subtype_Indication
9476 then
9477 -- Find type of new declaration. The constraints of the two
9478 -- views must match statically, but there is no point in
9479 -- creating an itype for the full view.
9480
9481 if Nkind (Obj_Def) = N_Subtype_Indication then
9482 Find_Type (Subtype_Mark (Obj_Def));
9483 New_T := Entity (Subtype_Mark (Obj_Def));
9484
9485 else
9486 Find_Type (Obj_Def);
9487 New_T := Entity (Obj_Def);
9488 end if;
9489
9490 T := Etype (Prev);
9491
9492 else
9493 -- The full view may impose a constraint, even if the partial
9494 -- view does not, so construct the subtype.
9495
9496 New_T := Find_Type_Of_Object (Obj_Def, N);
9497 T := New_T;
9498 end if;
9499
9500 else
71d9e9f2 9501 -- Current declaration is illegal, diagnosed below in Enter_Name
996ae0b0
RK
9502
9503 T := Empty;
9504 New_T := Any_Type;
9505 end if;
9506
9507 -- If previous full declaration exists, or if a homograph is present,
9508 -- let Enter_Name handle it, either with an error, or with the removal
9509 -- of an overridden implicit subprogram.
9510
9511 if Ekind (Prev) /= E_Constant
9512 or else Present (Expression (Parent (Prev)))
07fc65c4 9513 or else Present (Full_View (Prev))
996ae0b0
RK
9514 then
9515 Enter_Name (Id);
9516
758c442c
GD
9517 -- Verify that types of both declarations match, or else that both types
9518 -- are anonymous access types whose designated subtypes statically match
9519 -- (as allowed in Ada 2005 by AI-385).
996ae0b0 9520
758c442c
GD
9521 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
9522 and then
9523 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
9524 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
ce4a6e84
RD
9525 or else Is_Access_Constant (Etype (New_T)) /=
9526 Is_Access_Constant (Etype (Prev))
9527 or else Can_Never_Be_Null (Etype (New_T)) /=
9528 Can_Never_Be_Null (Etype (Prev))
9529 or else Null_Exclusion_Present (Parent (Prev)) /=
9530 Null_Exclusion_Present (Parent (Id))
758c442c
GD
9531 or else not Subtypes_Statically_Match
9532 (Designated_Type (Etype (Prev)),
9533 Designated_Type (Etype (New_T))))
9534 then
996ae0b0
RK
9535 Error_Msg_Sloc := Sloc (Prev);
9536 Error_Msg_N ("type does not match declaration#", N);
9537 Set_Full_View (Prev, Id);
9538 Set_Etype (Id, Any_Type);
9539
ce4a6e84
RD
9540 elsif
9541 Null_Exclusion_Present (Parent (Prev))
9542 and then not Null_Exclusion_Present (N)
9543 then
9544 Error_Msg_Sloc := Sloc (Prev);
9545 Error_Msg_N ("null-exclusion does not match declaration#", N);
9546 Set_Full_View (Prev, Id);
9547 Set_Etype (Id, Any_Type);
9548
996ae0b0
RK
9549 -- If so, process the full constant declaration
9550
9551 else
57193e09
TQ
9552 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
9553 -- the deferred declaration is constrained, then the subtype defined
9554 -- by the subtype_indication in the full declaration shall match it
9555 -- statically.
9556
9557 Check_Possible_Deferred_Completion
9558 (Prev_Id => Prev,
9559 Prev_Obj_Def => Object_Definition (Parent (Prev)),
9560 Curr_Obj_Def => Obj_Def);
9561
996ae0b0
RK
9562 Set_Full_View (Prev, Id);
9563 Set_Is_Public (Id, Is_Public (Prev));
9564 Set_Is_Internal (Id);
9565 Append_Entity (Id, Current_Scope);
9566
9567 -- Check ALIASED present if present before (RM 7.4(7))
9568
9569 if Is_Aliased (Prev)
9570 and then not Aliased_Present (N)
9571 then
9572 Error_Msg_Sloc := Sloc (Prev);
9573 Error_Msg_N ("ALIASED required (see declaration#)", N);
9574 end if;
9575
07fc65c4
GB
9576 -- Check that placement is in private part and that the incomplete
9577 -- declaration appeared in the visible part.
996ae0b0 9578
b16d9747 9579 if Ekind (Current_Scope) = E_Package
996ae0b0
RK
9580 and then not In_Private_Part (Current_Scope)
9581 then
9582 Error_Msg_Sloc := Sloc (Prev);
9583 Error_Msg_N ("full constant for declaration#"
9584 & " must be in private part", N);
07fc65c4
GB
9585
9586 elsif Ekind (Current_Scope) = E_Package
9587 and then List_Containing (Parent (Prev))
9588 /= Visible_Declarations
9589 (Specification (Unit_Declaration_Node (Current_Scope)))
9590 then
9591 Error_Msg_N
9592 ("deferred constant must be declared in visible part",
9593 Parent (Prev));
9594 end if;
9595
9596 if Is_Access_Type (T)
9597 and then Nkind (Expression (N)) = N_Allocator
9598 then
9599 Check_Recursive_Declaration (Designated_Type (T));
996ae0b0
RK
9600 end if;
9601 end if;
9602 end Constant_Redeclaration;
9603
9604 ----------------------
9605 -- Constrain_Access --
9606 ----------------------
9607
9608 procedure Constrain_Access
9609 (Def_Id : in out Entity_Id;
9610 S : Node_Id;
9611 Related_Nod : Node_Id)
9612 is
9613 T : constant Entity_Id := Entity (Subtype_Mark (S));
9614 Desig_Type : constant Entity_Id := Designated_Type (T);
9615 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
9616 Constraint_OK : Boolean := True;
9617
758c442c
GD
9618 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
9619 -- Simple predicate to test for defaulted discriminants
9620 -- Shouldn't this be in sem_util???
9621
9622 ---------------------------------
9623 -- Has_Defaulted_Discriminants --
9624 ---------------------------------
9625
9626 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
9627 begin
9628 return Has_Discriminants (Typ)
9629 and then Present (First_Discriminant (Typ))
9630 and then Present
9631 (Discriminant_Default_Value (First_Discriminant (Typ)));
9632 end Has_Defaulted_Discriminants;
9633
9634 -- Start of processing for Constrain_Access
9635
996ae0b0
RK
9636 begin
9637 if Is_Array_Type (Desig_Type) then
9638 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
9639
9640 elsif (Is_Record_Type (Desig_Type)
9641 or else Is_Incomplete_Or_Private_Type (Desig_Type))
9642 and then not Is_Constrained (Desig_Type)
9643 then
71d9e9f2
ES
9644 -- ??? The following code is a temporary kludge to ignore a
9645 -- discriminant constraint on access type if it is constraining
9646 -- the current record. Avoid creating the implicit subtype of the
9647 -- record we are currently compiling since right now, we cannot
9648 -- handle these. For now, just return the access type itself.
996ae0b0
RK
9649
9650 if Desig_Type = Current_Scope
9651 and then No (Def_Id)
9652 then
9653 Set_Ekind (Desig_Subtype, E_Record_Subtype);
9654 Def_Id := Entity (Subtype_Mark (S));
9655
71d9e9f2
ES
9656 -- This call added to ensure that the constraint is analyzed
9657 -- (needed for a B test). Note that we still return early from
9658 -- this procedure to avoid recursive processing. ???
996ae0b0
RK
9659
9660 Constrain_Discriminated_Type
9661 (Desig_Subtype, S, Related_Nod, For_Access => True);
996ae0b0
RK
9662 return;
9663 end if;
9664
f29b857f
ES
9665 if (Ekind (T) = E_General_Access_Type
9666 or else Ada_Version >= Ada_05)
07fc65c4
GB
9667 and then Has_Private_Declaration (Desig_Type)
9668 and then In_Open_Scopes (Scope (Desig_Type))
f29b857f 9669 and then Has_Discriminants (Desig_Type)
07fc65c4
GB
9670 then
9671 -- Enforce rule that the constraint is illegal if there is
9672 -- an unconstrained view of the designated type. This means
9673 -- that the partial view (either a private type declaration or
9674 -- a derivation from a private type) has no discriminants.
9675 -- (Defect Report 8652/0008, Technical Corrigendum 1, checked
9676 -- by ACATS B371001).
fea9e956 9677
758c442c
GD
9678 -- Rule updated for Ada 2005: the private type is said to have
9679 -- a constrained partial view, given that objects of the type
f29b857f
ES
9680 -- can be declared. Furthermore, the rule applies to all access
9681 -- types, unlike the rule concerning default discriminants.
07fc65c4
GB
9682
9683 declare
fbf5a39b
AC
9684 Pack : constant Node_Id :=
9685 Unit_Declaration_Node (Scope (Desig_Type));
07fc65c4
GB
9686 Decls : List_Id;
9687 Decl : Node_Id;
9688
9689 begin
9690 if Nkind (Pack) = N_Package_Declaration then
9691 Decls := Visible_Declarations (Specification (Pack));
9692 Decl := First (Decls);
07fc65c4
GB
9693 while Present (Decl) loop
9694 if (Nkind (Decl) = N_Private_Type_Declaration
9695 and then
9696 Chars (Defining_Identifier (Decl)) =
9697 Chars (Desig_Type))
9698
9699 or else
9700 (Nkind (Decl) = N_Full_Type_Declaration
9701 and then
9702 Chars (Defining_Identifier (Decl)) =
9703 Chars (Desig_Type)
9704 and then Is_Derived_Type (Desig_Type)
9705 and then
9706 Has_Private_Declaration (Etype (Desig_Type)))
9707 then
9708 if No (Discriminant_Specifications (Decl)) then
9709 Error_Msg_N
758c442c
GD
9710 ("cannot constrain general access type if " &
9711 "designated type has constrained partial view",
9712 S);
07fc65c4
GB
9713 end if;
9714
9715 exit;
9716 end if;
9717
9718 Next (Decl);
9719 end loop;
9720 end if;
9721 end;
9722 end if;
9723
996ae0b0
RK
9724 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
9725 For_Access => True);
9726
9727 elsif (Is_Task_Type (Desig_Type)
9728 or else Is_Protected_Type (Desig_Type))
9729 and then not Is_Constrained (Desig_Type)
9730 then
9731 Constrain_Concurrent
9732 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
9733
9734 else
9735 Error_Msg_N ("invalid constraint on access type", S);
9736 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
9737 Constraint_OK := False;
9738 end if;
9739
9740 if No (Def_Id) then
9741 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
9742 else
9743 Set_Ekind (Def_Id, E_Access_Subtype);
9744 end if;
9745
9746 if Constraint_OK then
9747 Set_Etype (Def_Id, Base_Type (T));
9748
9749 if Is_Private_Type (Desig_Type) then
9750 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
9751 end if;
9752 else
9753 Set_Etype (Def_Id, Any_Type);
9754 end if;
9755
9756 Set_Size_Info (Def_Id, T);
9757 Set_Is_Constrained (Def_Id, Constraint_OK);
9758 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
9759 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9760 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
9761
c6823a20 9762 Conditional_Delay (Def_Id, T);
758c442c 9763
9dfd2ff8
CC
9764 -- AI-363 : Subtypes of general access types whose designated types have
9765 -- default discriminants are disallowed. In instances, the rule has to
9766 -- be checked against the actual, of which T is the subtype. In a
9767 -- generic body, the rule is checked assuming that the actual type has
9768 -- defaulted discriminants.
758c442c 9769
fea9e956 9770 if Ada_Version >= Ada_05 or else Warn_On_Ada_2005_Compatibility then
758c442c
GD
9771 if Ekind (Base_Type (T)) = E_General_Access_Type
9772 and then Has_Defaulted_Discriminants (Desig_Type)
9773 then
fea9e956
ES
9774 if Ada_Version < Ada_05 then
9775 Error_Msg_N
9776 ("access subtype of general access type would not " &
9777 "be allowed in Ada 2005?", S);
9778 else
9779 Error_Msg_N
9780 ("access subype of general access type not allowed", S);
9781 end if;
9782
88b32fc3 9783 Error_Msg_N ("\discriminants have defaults", S);
758c442c
GD
9784
9785 elsif Is_Access_Type (T)
9786 and then Is_Generic_Type (Desig_Type)
9787 and then Has_Discriminants (Desig_Type)
9788 and then In_Package_Body (Current_Scope)
9789 then
fea9e956
ES
9790 if Ada_Version < Ada_05 then
9791 Error_Msg_N
9792 ("access subtype would not be allowed in generic body " &
9793 "in Ada 2005?", S);
9794 else
9795 Error_Msg_N
9796 ("access subtype not allowed in generic body", S);
9797 end if;
9798
758c442c 9799 Error_Msg_N
88b32fc3 9800 ("\designated type is a discriminated formal", S);
758c442c
GD
9801 end if;
9802 end if;
996ae0b0
RK
9803 end Constrain_Access;
9804
9805 ---------------------
9806 -- Constrain_Array --
9807 ---------------------
9808
9809 procedure Constrain_Array
9810 (Def_Id : in out Entity_Id;
9811 SI : Node_Id;
9812 Related_Nod : Node_Id;
9813 Related_Id : Entity_Id;
9814 Suffix : Character)
9815 is
9816 C : constant Node_Id := Constraint (SI);
9817 Number_Of_Constraints : Nat := 0;
9818 Index : Node_Id;
9819 S, T : Entity_Id;
9820 Constraint_OK : Boolean := True;
9821
9822 begin
9823 T := Entity (Subtype_Mark (SI));
9824
9825 if Ekind (T) in Access_Kind then
9826 T := Designated_Type (T);
9827 end if;
9828
9829 -- If an index constraint follows a subtype mark in a subtype indication
9830 -- then the type or subtype denoted by the subtype mark must not already
9831 -- impose an index constraint. The subtype mark must denote either an
9832 -- unconstrained array type or an access type whose designated type
9833 -- is such an array type... (RM 3.6.1)
9834
9835 if Is_Constrained (T) then
9836 Error_Msg_N
9837 ("array type is already constrained", Subtype_Mark (SI));
9838 Constraint_OK := False;
9839
9840 else
9841 S := First (Constraints (C));
996ae0b0
RK
9842 while Present (S) loop
9843 Number_Of_Constraints := Number_Of_Constraints + 1;
9844 Next (S);
9845 end loop;
9846
9847 -- In either case, the index constraint must provide a discrete
9848 -- range for each index of the array type and the type of each
9849 -- discrete range must be the same as that of the corresponding
9850 -- index. (RM 3.6.1)
9851
9852 if Number_Of_Constraints /= Number_Dimensions (T) then
9853 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
9854 Constraint_OK := False;
9855
9856 else
9857 S := First (Constraints (C));
9858 Index := First_Index (T);
9859 Analyze (Index);
9860
9861 -- Apply constraints to each index type
9862
9863 for J in 1 .. Number_Of_Constraints loop
9864 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
9865 Next (Index);
9866 Next (S);
9867 end loop;
9868
9869 end if;
9870 end if;
9871
9872 if No (Def_Id) then
9873 Def_Id :=
9874 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
fbf5a39b
AC
9875 Set_Parent (Def_Id, Related_Nod);
9876
996ae0b0
RK
9877 else
9878 Set_Ekind (Def_Id, E_Array_Subtype);
9879 end if;
9880
9881 Set_Size_Info (Def_Id, (T));
9882 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9883 Set_Etype (Def_Id, Base_Type (T));
9884
9885 if Constraint_OK then
9886 Set_First_Index (Def_Id, First (Constraints (C)));
758c442c
GD
9887 else
9888 Set_First_Index (Def_Id, First_Index (T));
996ae0b0
RK
9889 end if;
9890
996ae0b0
RK
9891 Set_Is_Constrained (Def_Id, True);
9892 Set_Is_Aliased (Def_Id, Is_Aliased (T));
9893 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9894
9895 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
9896 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
9897
2b73cf68
JM
9898 -- A subtype does not inherit the packed_array_type of is parent. We
9899 -- need to initialize the attribute because if Def_Id is previously
9900 -- analyzed through a limited_with clause, it will have the attributes
9901 -- of an incomplete type, one of which is an Elist that overlaps the
9902 -- Packed_Array_Type field.
9903
9904 Set_Packed_Array_Type (Def_Id, Empty);
9905
9906 -- Build a freeze node if parent still needs one. Also make sure that
9907 -- the Depends_On_Private status is set because the subtype will need
9908 -- reprocessing at the time the base type does, and also we must set a
9909 -- conditional delay.
996ae0b0 9910
c6823a20
EB
9911 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9912 Conditional_Delay (Def_Id, T);
996ae0b0
RK
9913 end Constrain_Array;
9914
9915 ------------------------------
9916 -- Constrain_Component_Type --
9917 ------------------------------
9918
9919 function Constrain_Component_Type
c6823a20 9920 (Comp : Entity_Id;
996ae0b0
RK
9921 Constrained_Typ : Entity_Id;
9922 Related_Node : Node_Id;
9923 Typ : Entity_Id;
b0f26df5 9924 Constraints : Elist_Id) return Entity_Id
996ae0b0 9925 is
c6823a20
EB
9926 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
9927 Compon_Type : constant Entity_Id := Etype (Comp);
996ae0b0
RK
9928
9929 function Build_Constrained_Array_Type
b0f26df5 9930 (Old_Type : Entity_Id) return Entity_Id;
a5b62485
AC
9931 -- If Old_Type is an array type, one of whose indices is constrained
9932 -- by a discriminant, build an Itype whose constraint replaces the
9933 -- discriminant with its value in the constraint.
996ae0b0
RK
9934
9935 function Build_Constrained_Discriminated_Type
b0f26df5 9936 (Old_Type : Entity_Id) return Entity_Id;
71d9e9f2 9937 -- Ditto for record components
996ae0b0
RK
9938
9939 function Build_Constrained_Access_Type
b0f26df5 9940 (Old_Type : Entity_Id) return Entity_Id;
996ae0b0
RK
9941 -- Ditto for access types. Makes use of previous two functions, to
9942 -- constrain designated type.
9943
9944 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
9945 -- T is an array or discriminated type, C is a list of constraints
9946 -- that apply to T. This routine builds the constrained subtype.
9947
9948 function Is_Discriminant (Expr : Node_Id) return Boolean;
71d9e9f2 9949 -- Returns True if Expr is a discriminant
996ae0b0 9950
07fc65c4 9951 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
71d9e9f2 9952 -- Find the value of discriminant Discrim in Constraint
996ae0b0
RK
9953
9954 -----------------------------------
9955 -- Build_Constrained_Access_Type --
9956 -----------------------------------
9957
9958 function Build_Constrained_Access_Type
b0f26df5 9959 (Old_Type : Entity_Id) return Entity_Id
996ae0b0
RK
9960 is
9961 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
9962 Itype : Entity_Id;
9963 Desig_Subtype : Entity_Id;
9964 Scop : Entity_Id;
9965
9966 begin
9967 -- if the original access type was not embedded in the enclosing
9968 -- type definition, there is no need to produce a new access
9969 -- subtype. In fact every access type with an explicit constraint
9970 -- generates an itype whose scope is the enclosing record.
9971
9972 if not Is_Type (Scope (Old_Type)) then
9973 return Old_Type;
9974
9975 elsif Is_Array_Type (Desig_Type) then
9976 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
9977
9978 elsif Has_Discriminants (Desig_Type) then
9979
9980 -- This may be an access type to an enclosing record type for
9981 -- which we are constructing the constrained components. Return
9982 -- the enclosing record subtype. This is not always correct,
9983 -- but avoids infinite recursion. ???
9984
9985 Desig_Subtype := Any_Type;
9986
9987 for J in reverse 0 .. Scope_Stack.Last loop
9988 Scop := Scope_Stack.Table (J).Entity;
9989
9990 if Is_Type (Scop)
9991 and then Base_Type (Scop) = Base_Type (Desig_Type)
9992 then
9993 Desig_Subtype := Scop;
9994 end if;
9995
9996 exit when not Is_Type (Scop);
9997 end loop;
9998
9999 if Desig_Subtype = Any_Type then
10000 Desig_Subtype :=
10001 Build_Constrained_Discriminated_Type (Desig_Type);
10002 end if;
10003
10004 else
10005 return Old_Type;
10006 end if;
10007
10008 if Desig_Subtype /= Desig_Type then
71d9e9f2 10009
996ae0b0
RK
10010 -- The Related_Node better be here or else we won't be able
10011 -- to attach new itypes to a node in the tree.
10012
10013 pragma Assert (Present (Related_Node));
10014
10015 Itype := Create_Itype (E_Access_Subtype, Related_Node);
10016
10017 Set_Etype (Itype, Base_Type (Old_Type));
10018 Set_Size_Info (Itype, (Old_Type));
10019 Set_Directly_Designated_Type (Itype, Desig_Subtype);
10020 Set_Depends_On_Private (Itype, Has_Private_Component
10021 (Old_Type));
10022 Set_Is_Access_Constant (Itype, Is_Access_Constant
10023 (Old_Type));
10024
10025 -- The new itype needs freezing when it depends on a not frozen
10026 -- type and the enclosing subtype needs freezing.
10027
10028 if Has_Delayed_Freeze (Constrained_Typ)
10029 and then not Is_Frozen (Constrained_Typ)
10030 then
10031 Conditional_Delay (Itype, Base_Type (Old_Type));
10032 end if;
10033
10034 return Itype;
10035
10036 else
10037 return Old_Type;
10038 end if;
10039 end Build_Constrained_Access_Type;
10040
10041 ----------------------------------
10042 -- Build_Constrained_Array_Type --
10043 ----------------------------------
10044
10045 function Build_Constrained_Array_Type
b0f26df5 10046 (Old_Type : Entity_Id) return Entity_Id
996ae0b0
RK
10047 is
10048 Lo_Expr : Node_Id;
10049 Hi_Expr : Node_Id;
10050 Old_Index : Node_Id;
10051 Range_Node : Node_Id;
10052 Constr_List : List_Id;
10053
10054 Need_To_Create_Itype : Boolean := False;
10055
10056 begin
10057 Old_Index := First_Index (Old_Type);
10058 while Present (Old_Index) loop
10059 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10060
10061 if Is_Discriminant (Lo_Expr)
10062 or else Is_Discriminant (Hi_Expr)
10063 then
10064 Need_To_Create_Itype := True;
10065 end if;
10066
10067 Next_Index (Old_Index);
10068 end loop;
10069
10070 if Need_To_Create_Itype then
10071 Constr_List := New_List;
10072
10073 Old_Index := First_Index (Old_Type);
10074 while Present (Old_Index) loop
10075 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10076
10077 if Is_Discriminant (Lo_Expr) then
07fc65c4 10078 Lo_Expr := Get_Discr_Value (Lo_Expr);
996ae0b0
RK
10079 end if;
10080
10081 if Is_Discriminant (Hi_Expr) then
07fc65c4 10082 Hi_Expr := Get_Discr_Value (Hi_Expr);
996ae0b0
RK
10083 end if;
10084
10085 Range_Node :=
10086 Make_Range
10087 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
10088
10089 Append (Range_Node, To => Constr_List);
10090
10091 Next_Index (Old_Index);
10092 end loop;
10093
10094 return Build_Subtype (Old_Type, Constr_List);
10095
10096 else
10097 return Old_Type;
10098 end if;
10099 end Build_Constrained_Array_Type;
10100
10101 ------------------------------------------
10102 -- Build_Constrained_Discriminated_Type --
10103 ------------------------------------------
10104
10105 function Build_Constrained_Discriminated_Type
b0f26df5 10106 (Old_Type : Entity_Id) return Entity_Id
996ae0b0
RK
10107 is
10108 Expr : Node_Id;
10109 Constr_List : List_Id;
10110 Old_Constraint : Elmt_Id;
10111
10112 Need_To_Create_Itype : Boolean := False;
10113
10114 begin
10115 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10116 while Present (Old_Constraint) loop
10117 Expr := Node (Old_Constraint);
10118
10119 if Is_Discriminant (Expr) then
10120 Need_To_Create_Itype := True;
10121 end if;
10122
10123 Next_Elmt (Old_Constraint);
10124 end loop;
10125
10126 if Need_To_Create_Itype then
10127 Constr_List := New_List;
10128
10129 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10130 while Present (Old_Constraint) loop
10131 Expr := Node (Old_Constraint);
10132
10133 if Is_Discriminant (Expr) then
07fc65c4 10134 Expr := Get_Discr_Value (Expr);
996ae0b0
RK
10135 end if;
10136
10137 Append (New_Copy_Tree (Expr), To => Constr_List);
10138
10139 Next_Elmt (Old_Constraint);
10140 end loop;
10141
10142 return Build_Subtype (Old_Type, Constr_List);
10143
10144 else
10145 return Old_Type;
10146 end if;
10147 end Build_Constrained_Discriminated_Type;
10148
10149 -------------------
10150 -- Build_Subtype --
10151 -------------------
10152
10153 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
10154 Indic : Node_Id;
10155 Subtyp_Decl : Node_Id;
10156 Def_Id : Entity_Id;
10157 Btyp : Entity_Id := Base_Type (T);
10158
10159 begin
a5b62485
AC
10160 -- The Related_Node better be here or else we won't be able to
10161 -- attach new itypes to a node in the tree.
996ae0b0
RK
10162
10163 pragma Assert (Present (Related_Node));
10164
10165 -- If the view of the component's type is incomplete or private
10166 -- with unknown discriminants, then the constraint must be applied
10167 -- to the full type.
10168
10169 if Has_Unknown_Discriminants (Btyp)
10170 and then Present (Underlying_Type (Btyp))
10171 then
10172 Btyp := Underlying_Type (Btyp);
10173 end if;
10174
10175 Indic :=
10176 Make_Subtype_Indication (Loc,
10177 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
10178 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
10179
10180 Def_Id := Create_Itype (Ekind (T), Related_Node);
10181
10182 Subtyp_Decl :=
10183 Make_Subtype_Declaration (Loc,
10184 Defining_Identifier => Def_Id,
10185 Subtype_Indication => Indic);
24105bab 10186
996ae0b0
RK
10187 Set_Parent (Subtyp_Decl, Parent (Related_Node));
10188
ffe9aba8 10189 -- Itypes must be analyzed with checks off (see package Itypes)
996ae0b0
RK
10190
10191 Analyze (Subtyp_Decl, Suppress => All_Checks);
10192
10193 return Def_Id;
10194 end Build_Subtype;
10195
07fc65c4
GB
10196 ---------------------
10197 -- Get_Discr_Value --
10198 ---------------------
996ae0b0 10199
07fc65c4 10200 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
9dfd2ff8
CC
10201 D : Entity_Id;
10202 E : Elmt_Id;
996ae0b0
RK
10203
10204 begin
07fc65c4
GB
10205 -- The discriminant may be declared for the type, in which case we
10206 -- find it by iterating over the list of discriminants. If the
10207 -- discriminant is inherited from a parent type, it appears as the
10208 -- corresponding discriminant of the current type. This will be the
10209 -- case when constraining an inherited component whose constraint is
10210 -- given by a discriminant of the parent.
996ae0b0 10211
9dfd2ff8
CC
10212 D := First_Discriminant (Typ);
10213 E := First_Elmt (Constraints);
88b32fc3 10214
07fc65c4 10215 while Present (D) loop
996ae0b0 10216 if D = Entity (Discrim)
88b32fc3 10217 or else D = CR_Discriminant (Entity (Discrim))
996ae0b0
RK
10218 or else Corresponding_Discriminant (D) = Entity (Discrim)
10219 then
10220 return Node (E);
10221 end if;
10222
10223 Next_Discriminant (D);
10224 Next_Elmt (E);
10225 end loop;
10226
07fc65c4
GB
10227 -- The corresponding_Discriminant mechanism is incomplete, because
10228 -- the correspondence between new and old discriminants is not one
a5b62485
AC
10229 -- to one: one new discriminant can constrain several old ones. In
10230 -- that case, scan sequentially the stored_constraint, the list of
10231 -- discriminants of the parents, and the constraints.
ab8bfb64
ES
10232 -- Previous code checked for the present of the Stored_Constraint
10233 -- list for the derived type, but did not use it at all. Should it
10234 -- be present when the component is a discriminated task type?
07fc65c4
GB
10235
10236 if Is_Derived_Type (Typ)
07fc65c4
GB
10237 and then Scope (Entity (Discrim)) = Etype (Typ)
10238 then
10239 D := First_Discriminant (Etype (Typ));
10240 E := First_Elmt (Constraints);
07fc65c4
GB
10241 while Present (D) loop
10242 if D = Entity (Discrim) then
10243 return Node (E);
10244 end if;
10245
10246 Next_Discriminant (D);
10247 Next_Elmt (E);
07fc65c4
GB
10248 end loop;
10249 end if;
10250
996ae0b0
RK
10251 -- Something is wrong if we did not find the value
10252
10253 raise Program_Error;
07fc65c4 10254 end Get_Discr_Value;
996ae0b0
RK
10255
10256 ---------------------
10257 -- Is_Discriminant --
10258 ---------------------
10259
10260 function Is_Discriminant (Expr : Node_Id) return Boolean is
10261 Discrim_Scope : Entity_Id;
10262
10263 begin
10264 if Denotes_Discriminant (Expr) then
10265 Discrim_Scope := Scope (Entity (Expr));
10266
10267 -- Either we have a reference to one of Typ's discriminants,
10268
10269 pragma Assert (Discrim_Scope = Typ
10270
10271 -- or to the discriminants of the parent type, in the case
10272 -- of a derivation of a tagged type with variants.
10273
10274 or else Discrim_Scope = Etype (Typ)
10275 or else Full_View (Discrim_Scope) = Etype (Typ)
10276
10277 -- or same as above for the case where the discriminants
10278 -- were declared in Typ's private view.
10279
10280 or else (Is_Private_Type (Discrim_Scope)
10281 and then Chars (Discrim_Scope) = Chars (Typ))
10282
10283 -- or else we are deriving from the full view and the
10284 -- discriminant is declared in the private entity.
10285
10286 or else (Is_Private_Type (Typ)
33931112 10287 and then Chars (Discrim_Scope) = Chars (Typ))
996ae0b0 10288
88b32fc3
BD
10289 -- Or we are constrained the corresponding record of a
10290 -- synchronized type that completes a private declaration.
10291
10292 or else (Is_Concurrent_Record_Type (Typ)
10293 and then
10294 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
10295
996ae0b0
RK
10296 -- or we have a class-wide type, in which case make sure the
10297 -- discriminant found belongs to the root type.
10298
10299 or else (Is_Class_Wide_Type (Typ)
33931112 10300 and then Etype (Typ) = Discrim_Scope));
996ae0b0
RK
10301
10302 return True;
10303 end if;
10304
ffe9aba8 10305 -- In all other cases we have something wrong
996ae0b0
RK
10306
10307 return False;
10308 end Is_Discriminant;
10309
10310 -- Start of processing for Constrain_Component_Type
10311
10312 begin
c6823a20
EB
10313 if Nkind (Parent (Comp)) = N_Component_Declaration
10314 and then Comes_From_Source (Parent (Comp))
10315 and then Comes_From_Source
10316 (Subtype_Indication (Component_Definition (Parent (Comp))))
10317 and then
10318 Is_Entity_Name
10319 (Subtype_Indication (Component_Definition (Parent (Comp))))
10320 then
10321 return Compon_Type;
10322
10323 elsif Is_Array_Type (Compon_Type) then
996ae0b0
RK
10324 return Build_Constrained_Array_Type (Compon_Type);
10325
10326 elsif Has_Discriminants (Compon_Type) then
10327 return Build_Constrained_Discriminated_Type (Compon_Type);
10328
10329 elsif Is_Access_Type (Compon_Type) then
10330 return Build_Constrained_Access_Type (Compon_Type);
996ae0b0 10331
c6823a20
EB
10332 else
10333 return Compon_Type;
10334 end if;
996ae0b0
RK
10335 end Constrain_Component_Type;
10336
10337 --------------------------
10338 -- Constrain_Concurrent --
10339 --------------------------
10340
10341 -- For concurrent types, the associated record value type carries the same
10342 -- discriminants, so when we constrain a concurrent type, we must constrain
950d3e7d 10343 -- the corresponding record type as well.
996ae0b0
RK
10344
10345 procedure Constrain_Concurrent
10346 (Def_Id : in out Entity_Id;
10347 SI : Node_Id;
10348 Related_Nod : Node_Id;
10349 Related_Id : Entity_Id;
10350 Suffix : Character)
10351 is
10352 T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
10353 T_Val : Entity_Id;
10354
10355 begin
10356 if Ekind (T_Ent) in Access_Kind then
10357 T_Ent := Designated_Type (T_Ent);
10358 end if;
10359
10360 T_Val := Corresponding_Record_Type (T_Ent);
10361
10362 if Present (T_Val) then
10363
10364 if No (Def_Id) then
10365 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10366 end if;
10367
10368 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10369
10370 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10371 Set_Corresponding_Record_Type (Def_Id,
10372 Constrain_Corresponding_Record
10373 (Def_Id, T_Val, Related_Nod, Related_Id));
10374
10375 else
10376 -- If there is no associated record, expansion is disabled and this
10377 -- is a generic context. Create a subtype in any case, so that
10378 -- semantic analysis can proceed.
10379
10380 if No (Def_Id) then
10381 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10382 end if;
10383
10384 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10385 end if;
10386 end Constrain_Concurrent;
10387
10388 ------------------------------------
10389 -- Constrain_Corresponding_Record --
10390 ------------------------------------
10391
10392 function Constrain_Corresponding_Record
10393 (Prot_Subt : Entity_Id;
10394 Corr_Rec : Entity_Id;
10395 Related_Nod : Node_Id;
b0f26df5 10396 Related_Id : Entity_Id) return Entity_Id
996ae0b0 10397 is
71d9e9f2
ES
10398 T_Sub : constant Entity_Id :=
10399 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
996ae0b0
RK
10400
10401 begin
71d9e9f2 10402 Set_Etype (T_Sub, Corr_Rec);
71d9e9f2
ES
10403 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
10404 Set_Is_Constrained (T_Sub, True);
10405 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
10406 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
996ae0b0 10407
88b32fc3
BD
10408 -- As elsewhere, we do not want to create a freeze node for this itype
10409 -- if it is created for a constrained component of an enclosing record
10410 -- because references to outer discriminants will appear out of scope.
10411
10412 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
10413 Conditional_Delay (T_Sub, Corr_Rec);
10414 else
10415 Set_Is_Frozen (T_Sub);
10416 end if;
996ae0b0
RK
10417
10418 if Has_Discriminants (Prot_Subt) then -- False only if errors.
71d9e9f2
ES
10419 Set_Discriminant_Constraint
10420 (T_Sub, Discriminant_Constraint (Prot_Subt));
fbf5a39b 10421 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
71d9e9f2
ES
10422 Create_Constrained_Components
10423 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
996ae0b0
RK
10424 end if;
10425
10426 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
10427
10428 return T_Sub;
10429 end Constrain_Corresponding_Record;
10430
10431 -----------------------
10432 -- Constrain_Decimal --
10433 -----------------------
10434
07fc65c4 10435 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
10436 T : constant Entity_Id := Entity (Subtype_Mark (S));
10437 C : constant Node_Id := Constraint (S);
10438 Loc : constant Source_Ptr := Sloc (C);
10439 Range_Expr : Node_Id;
10440 Digits_Expr : Node_Id;
10441 Digits_Val : Uint;
10442 Bound_Val : Ureal;
10443
10444 begin
10445 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
10446
10447 if Nkind (C) = N_Range_Constraint then
10448 Range_Expr := Range_Expression (C);
10449 Digits_Val := Digits_Value (T);
10450
10451 else
10452 pragma Assert (Nkind (C) = N_Digits_Constraint);
10453 Digits_Expr := Digits_Expression (C);
10454 Analyze_And_Resolve (Digits_Expr, Any_Integer);
10455
10456 Check_Digits_Expression (Digits_Expr);
10457 Digits_Val := Expr_Value (Digits_Expr);
10458
10459 if Digits_Val > Digits_Value (T) then
10460 Error_Msg_N
10461 ("digits expression is incompatible with subtype", C);
10462 Digits_Val := Digits_Value (T);
10463 end if;
10464
10465 if Present (Range_Constraint (C)) then
10466 Range_Expr := Range_Expression (Range_Constraint (C));
10467 else
10468 Range_Expr := Empty;
10469 end if;
10470 end if;
10471
10472 Set_Etype (Def_Id, Base_Type (T));
10473 Set_Size_Info (Def_Id, (T));
10474 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10475 Set_Delta_Value (Def_Id, Delta_Value (T));
10476 Set_Scale_Value (Def_Id, Scale_Value (T));
10477 Set_Small_Value (Def_Id, Small_Value (T));
10478 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
10479 Set_Digits_Value (Def_Id, Digits_Val);
10480
10481 -- Manufacture range from given digits value if no range present
10482
10483 if No (Range_Expr) then
10484 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
10485 Range_Expr :=
71d9e9f2
ES
10486 Make_Range (Loc,
10487 Low_Bound =>
10488 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
10489 High_Bound =>
10490 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
996ae0b0
RK
10491 end if;
10492
07fc65c4 10493 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
996ae0b0
RK
10494 Set_Discrete_RM_Size (Def_Id);
10495
10496 -- Unconditionally delay the freeze, since we cannot set size
10497 -- information in all cases correctly until the freeze point.
10498
10499 Set_Has_Delayed_Freeze (Def_Id);
10500 end Constrain_Decimal;
10501
10502 ----------------------------------
10503 -- Constrain_Discriminated_Type --
10504 ----------------------------------
10505
10506 procedure Constrain_Discriminated_Type
10507 (Def_Id : Entity_Id;
10508 S : Node_Id;
10509 Related_Nod : Node_Id;
10510 For_Access : Boolean := False)
10511 is
07fc65c4 10512 E : constant Entity_Id := Entity (Subtype_Mark (S));
996ae0b0
RK
10513 T : Entity_Id;
10514 C : Node_Id;
10515 Elist : Elist_Id := New_Elmt_List;
10516
10517 procedure Fixup_Bad_Constraint;
10518 -- This is called after finding a bad constraint, and after having
10519 -- posted an appropriate error message. The mission is to leave the
10520 -- entity T in as reasonable state as possible!
10521
fbf5a39b
AC
10522 --------------------------
10523 -- Fixup_Bad_Constraint --
10524 --------------------------
10525
996ae0b0
RK
10526 procedure Fixup_Bad_Constraint is
10527 begin
10528 -- Set a reasonable Ekind for the entity. For an incomplete type,
10529 -- we can't do much, but for other types, we can set the proper
10530 -- corresponding subtype kind.
10531
10532 if Ekind (T) = E_Incomplete_Type then
10533 Set_Ekind (Def_Id, Ekind (T));
10534 else
10535 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10536 end if;
10537
491016e5
GD
10538 -- Set Etype to the known type, to reduce chances of cascaded errors
10539
10540 Set_Etype (Def_Id, E);
996ae0b0
RK
10541 Set_Error_Posted (Def_Id);
10542 end Fixup_Bad_Constraint;
10543
10544 -- Start of processing for Constrain_Discriminated_Type
10545
10546 begin
10547 C := Constraint (S);
10548
10549 -- A discriminant constraint is only allowed in a subtype indication,
10550 -- after a subtype mark. This subtype mark must denote either a type
10551 -- with discriminants, or an access type whose designated type is a
10552 -- type with discriminants. A discriminant constraint specifies the
10553 -- values of these discriminants (RM 3.7.2(5)).
10554
10555 T := Base_Type (Entity (Subtype_Mark (S)));
10556
10557 if Ekind (T) in Access_Kind then
10558 T := Designated_Type (T);
10559 end if;
10560
88b32fc3
BD
10561 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
10562 -- Avoid generating an error for access-to-incomplete subtypes.
10563
10564 if Ada_Version >= Ada_05
10565 and then Ekind (T) = E_Incomplete_Type
10566 and then Nkind (Parent (S)) = N_Subtype_Declaration
10567 and then not Is_Itype (Def_Id)
10568 then
10569 -- A little sanity check, emit an error message if the type
10570 -- has discriminants to begin with. Type T may be a regular
10571 -- incomplete type or imported via a limited with clause.
10572
10573 if Has_Discriminants (T)
10574 or else
10575 (From_With_Type (T)
10576 and then Present (Non_Limited_View (T))
10577 and then Nkind (Parent (Non_Limited_View (T))) =
10578 N_Full_Type_Declaration
10579 and then Present (Discriminant_Specifications
10580 (Parent (Non_Limited_View (T)))))
10581 then
10582 Error_Msg_N
10583 ("(Ada 2005) incomplete subtype may not be constrained", C);
10584 else
10585 Error_Msg_N
10586 ("invalid constraint: type has no discriminant", C);
10587 end if;
10588
10589 Fixup_Bad_Constraint;
10590 return;
10591
8a6a52dc
AC
10592 -- Check that the type has visible discriminants. The type may be
10593 -- a private type with unknown discriminants whose full view has
10594 -- discriminants which are invisible.
10595
88b32fc3 10596 elsif not Has_Discriminants (T)
8a6a52dc
AC
10597 or else
10598 (Has_Unknown_Discriminants (T)
10599 and then Is_Private_Type (T))
10600 then
996ae0b0
RK
10601 Error_Msg_N ("invalid constraint: type has no discriminant", C);
10602 Fixup_Bad_Constraint;
10603 return;
10604
07fc65c4
GB
10605 elsif Is_Constrained (E)
10606 or else (Ekind (E) = E_Class_Wide_Subtype
10607 and then Present (Discriminant_Constraint (E)))
10608 then
996ae0b0
RK
10609 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
10610 Fixup_Bad_Constraint;
10611 return;
10612 end if;
10613
10614 -- T may be an unconstrained subtype (e.g. a generic actual).
10615 -- Constraint applies to the base type.
10616
10617 T := Base_Type (T);
10618
10619 Elist := Build_Discriminant_Constraints (T, S);
10620
10621 -- If the list returned was empty we had an error in building the
10622 -- discriminant constraint. We have also already signalled an error
10623 -- in the incomplete type case
10624
10625 if Is_Empty_Elmt_List (Elist) then
10626 Fixup_Bad_Constraint;
10627 return;
10628 end if;
10629
10630 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
10631 end Constrain_Discriminated_Type;
10632
10633 ---------------------------
10634 -- Constrain_Enumeration --
10635 ---------------------------
10636
07fc65c4 10637 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
10638 T : constant Entity_Id := Entity (Subtype_Mark (S));
10639 C : constant Node_Id := Constraint (S);
10640
10641 begin
10642 Set_Ekind (Def_Id, E_Enumeration_Subtype);
10643
10644 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
10645
10646 Set_Etype (Def_Id, Base_Type (T));
10647 Set_Size_Info (Def_Id, (T));
10648 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10649 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10650
07fc65c4 10651 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
996ae0b0
RK
10652
10653 Set_Discrete_RM_Size (Def_Id);
996ae0b0
RK
10654 end Constrain_Enumeration;
10655
10656 ----------------------
10657 -- Constrain_Float --
10658 ----------------------
10659
07fc65c4 10660 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
10661 T : constant Entity_Id := Entity (Subtype_Mark (S));
10662 C : Node_Id;
10663 D : Node_Id;
10664 Rais : Node_Id;
10665
10666 begin
10667 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
10668
10669 Set_Etype (Def_Id, Base_Type (T));
10670 Set_Size_Info (Def_Id, (T));
10671 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10672
10673 -- Process the constraint
10674
10675 C := Constraint (S);
10676
10677 -- Digits constraint present
10678
10679 if Nkind (C) = N_Digits_Constraint then
5f3ab6fb
AC
10680 Check_Restriction (No_Obsolescent_Features, C);
10681
fbf5a39b
AC
10682 if Warn_On_Obsolescent_Feature then
10683 Error_Msg_N
10684 ("subtype digits constraint is an " &
dc06abec 10685 "obsolescent feature (RM J.3(8))?", C);
fbf5a39b
AC
10686 end if;
10687
996ae0b0
RK
10688 D := Digits_Expression (C);
10689 Analyze_And_Resolve (D, Any_Integer);
10690 Check_Digits_Expression (D);
10691 Set_Digits_Value (Def_Id, Expr_Value (D));
10692
10693 -- Check that digits value is in range. Obviously we can do this
10694 -- at compile time, but it is strictly a runtime check, and of
10695 -- course there is an ACVC test that checks this!
10696
10697 if Digits_Value (Def_Id) > Digits_Value (T) then
10698 Error_Msg_Uint_1 := Digits_Value (T);
10699 Error_Msg_N ("?digits value is too large, maximum is ^", D);
07fc65c4
GB
10700 Rais :=
10701 Make_Raise_Constraint_Error (Sloc (D),
10702 Reason => CE_Range_Check_Failed);
996ae0b0
RK
10703 Insert_Action (Declaration_Node (Def_Id), Rais);
10704 end if;
10705
10706 C := Range_Constraint (C);
10707
10708 -- No digits constraint present
10709
10710 else
10711 Set_Digits_Value (Def_Id, Digits_Value (T));
10712 end if;
10713
10714 -- Range constraint present
10715
10716 if Nkind (C) = N_Range_Constraint then
07fc65c4 10717 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
996ae0b0
RK
10718
10719 -- No range constraint present
10720
10721 else
10722 pragma Assert (No (C));
10723 Set_Scalar_Range (Def_Id, Scalar_Range (T));
10724 end if;
10725
10726 Set_Is_Constrained (Def_Id);
10727 end Constrain_Float;
10728
10729 ---------------------
10730 -- Constrain_Index --
10731 ---------------------
10732
10733 procedure Constrain_Index
10734 (Index : Node_Id;
10735 S : Node_Id;
10736 Related_Nod : Node_Id;
10737 Related_Id : Entity_Id;
10738 Suffix : Character;
10739 Suffix_Index : Nat)
10740 is
7324bf49
AC
10741 Def_Id : Entity_Id;
10742 R : Node_Id := Empty;
10743 T : constant Entity_Id := Etype (Index);
996ae0b0
RK
10744
10745 begin
10746 if Nkind (S) = N_Range
fbf5a39b
AC
10747 or else
10748 (Nkind (S) = N_Attribute_Reference
10749 and then Attribute_Name (S) = Name_Range)
996ae0b0 10750 then
ffe9aba8 10751 -- A Range attribute will transformed into N_Range by Resolve
996ae0b0
RK
10752
10753 Analyze (S);
10754 Set_Etype (S, T);
10755 R := S;
10756
7324bf49 10757 Process_Range_Expr_In_Decl (R, T, Empty_List);
996ae0b0
RK
10758
10759 if not Error_Posted (S)
10760 and then
10761 (Nkind (S) /= N_Range
891a6e79
AC
10762 or else not Covers (T, (Etype (Low_Bound (S))))
10763 or else not Covers (T, (Etype (High_Bound (S)))))
996ae0b0
RK
10764 then
10765 if Base_Type (T) /= Any_Type
10766 and then Etype (Low_Bound (S)) /= Any_Type
10767 and then Etype (High_Bound (S)) /= Any_Type
10768 then
10769 Error_Msg_N ("range expected", S);
10770 end if;
10771 end if;
10772
10773 elsif Nkind (S) = N_Subtype_Indication then
71d9e9f2
ES
10774
10775 -- The parser has verified that this is a discrete indication
996ae0b0
RK
10776
10777 Resolve_Discrete_Subtype_Indication (S, T);
10778 R := Range_Expression (Constraint (S));
10779
10780 elsif Nkind (S) = N_Discriminant_Association then
10781
71d9e9f2 10782 -- Syntactically valid in subtype indication
996ae0b0
RK
10783
10784 Error_Msg_N ("invalid index constraint", S);
10785 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10786 return;
10787
10788 -- Subtype_Mark case, no anonymous subtypes to construct
10789
10790 else
10791 Analyze (S);
10792
10793 if Is_Entity_Name (S) then
996ae0b0
RK
10794 if not Is_Type (Entity (S)) then
10795 Error_Msg_N ("expect subtype mark for index constraint", S);
10796
10797 elsif Base_Type (Entity (S)) /= Base_Type (T) then
10798 Wrong_Type (S, Base_Type (T));
10799 end if;
10800
10801 return;
10802
10803 else
10804 Error_Msg_N ("invalid index constraint", S);
10805 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10806 return;
10807 end if;
10808 end if;
10809
10810 Def_Id :=
10811 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
10812
10813 Set_Etype (Def_Id, Base_Type (T));
10814
10815 if Is_Modular_Integer_Type (T) then
10816 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10817
10818 elsif Is_Integer_Type (T) then
10819 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10820
10821 else
10822 Set_Ekind (Def_Id, E_Enumeration_Subtype);
10823 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10824 end if;
10825
10826 Set_Size_Info (Def_Id, (T));
10827 Set_RM_Size (Def_Id, RM_Size (T));
10828 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10829
996ae0b0
RK
10830 Set_Scalar_Range (Def_Id, R);
10831
10832 Set_Etype (S, Def_Id);
10833 Set_Discrete_RM_Size (Def_Id);
10834 end Constrain_Index;
10835
10836 -----------------------
10837 -- Constrain_Integer --
10838 -----------------------
10839
07fc65c4 10840 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
10841 T : constant Entity_Id := Entity (Subtype_Mark (S));
10842 C : constant Node_Id := Constraint (S);
10843
10844 begin
07fc65c4 10845 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
996ae0b0
RK
10846
10847 if Is_Modular_Integer_Type (T) then
10848 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10849 else
10850 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10851 end if;
10852
10853 Set_Etype (Def_Id, Base_Type (T));
10854 Set_Size_Info (Def_Id, (T));
10855 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10856 Set_Discrete_RM_Size (Def_Id);
996ae0b0
RK
10857 end Constrain_Integer;
10858
10859 ------------------------------
10860 -- Constrain_Ordinary_Fixed --
10861 ------------------------------
10862
07fc65c4 10863 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
10864 T : constant Entity_Id := Entity (Subtype_Mark (S));
10865 C : Node_Id;
10866 D : Node_Id;
10867 Rais : Node_Id;
10868
10869 begin
10870 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
10871 Set_Etype (Def_Id, Base_Type (T));
10872 Set_Size_Info (Def_Id, (T));
10873 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10874 Set_Small_Value (Def_Id, Small_Value (T));
10875
10876 -- Process the constraint
10877
10878 C := Constraint (S);
10879
10880 -- Delta constraint present
10881
10882 if Nkind (C) = N_Delta_Constraint then
5f3ab6fb
AC
10883 Check_Restriction (No_Obsolescent_Features, C);
10884
fbf5a39b
AC
10885 if Warn_On_Obsolescent_Feature then
10886 Error_Msg_S
10887 ("subtype delta constraint is an " &
dc06abec 10888 "obsolescent feature (RM J.3(7))?");
fbf5a39b
AC
10889 end if;
10890
996ae0b0
RK
10891 D := Delta_Expression (C);
10892 Analyze_And_Resolve (D, Any_Real);
10893 Check_Delta_Expression (D);
10894 Set_Delta_Value (Def_Id, Expr_Value_R (D));
10895
10896 -- Check that delta value is in range. Obviously we can do this
10897 -- at compile time, but it is strictly a runtime check, and of
10898 -- course there is an ACVC test that checks this!
10899
10900 if Delta_Value (Def_Id) < Delta_Value (T) then
10901 Error_Msg_N ("?delta value is too small", D);
07fc65c4
GB
10902 Rais :=
10903 Make_Raise_Constraint_Error (Sloc (D),
10904 Reason => CE_Range_Check_Failed);
996ae0b0
RK
10905 Insert_Action (Declaration_Node (Def_Id), Rais);
10906 end if;
10907
10908 C := Range_Constraint (C);
10909
10910 -- No delta constraint present
10911
10912 else
10913 Set_Delta_Value (Def_Id, Delta_Value (T));
10914 end if;
10915
10916 -- Range constraint present
10917
10918 if Nkind (C) = N_Range_Constraint then
07fc65c4 10919 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
996ae0b0
RK
10920
10921 -- No range constraint present
10922
10923 else
10924 pragma Assert (No (C));
10925 Set_Scalar_Range (Def_Id, Scalar_Range (T));
10926
10927 end if;
10928
10929 Set_Discrete_RM_Size (Def_Id);
10930
10931 -- Unconditionally delay the freeze, since we cannot set size
10932 -- information in all cases correctly until the freeze point.
10933
10934 Set_Has_Delayed_Freeze (Def_Id);
10935 end Constrain_Ordinary_Fixed;
10936
dc06abec
RD
10937 -----------------------
10938 -- Contain_Interface --
10939 -----------------------
10940
10941 function Contain_Interface
10942 (Iface : Entity_Id;
10943 Ifaces : Elist_Id) return Boolean
10944 is
10945 Iface_Elmt : Elmt_Id;
10946
10947 begin
10948 if Present (Ifaces) then
10949 Iface_Elmt := First_Elmt (Ifaces);
10950 while Present (Iface_Elmt) loop
10951 if Node (Iface_Elmt) = Iface then
10952 return True;
10953 end if;
10954
10955 Next_Elmt (Iface_Elmt);
10956 end loop;
10957 end if;
10958
10959 return False;
10960 end Contain_Interface;
10961
996ae0b0
RK
10962 ---------------------------
10963 -- Convert_Scalar_Bounds --
10964 ---------------------------
10965
10966 procedure Convert_Scalar_Bounds
10967 (N : Node_Id;
10968 Parent_Type : Entity_Id;
10969 Derived_Type : Entity_Id;
10970 Loc : Source_Ptr)
10971 is
10972 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
10973
10974 Lo : Node_Id;
10975 Hi : Node_Id;
10976 Rng : Node_Id;
10977
10978 begin
10979 Lo := Build_Scalar_Bound
10980 (Type_Low_Bound (Derived_Type),
07fc65c4 10981 Parent_Type, Implicit_Base);
996ae0b0
RK
10982
10983 Hi := Build_Scalar_Bound
10984 (Type_High_Bound (Derived_Type),
07fc65c4 10985 Parent_Type, Implicit_Base);
996ae0b0
RK
10986
10987 Rng :=
10988 Make_Range (Loc,
10989 Low_Bound => Lo,
10990 High_Bound => Hi);
10991
10992 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
10993
10994 Set_Parent (Rng, N);
10995 Set_Scalar_Range (Derived_Type, Rng);
10996
10997 -- Analyze the bounds
10998
10999 Analyze_And_Resolve (Lo, Implicit_Base);
11000 Analyze_And_Resolve (Hi, Implicit_Base);
11001
11002 -- Analyze the range itself, except that we do not analyze it if
11003 -- the bounds are real literals, and we have a fixed-point type.
11004 -- The reason for this is that we delay setting the bounds in this
11005 -- case till we know the final Small and Size values (see circuit
11006 -- in Freeze.Freeze_Fixed_Point_Type for further details).
11007
11008 if Is_Fixed_Point_Type (Parent_Type)
11009 and then Nkind (Lo) = N_Real_Literal
11010 and then Nkind (Hi) = N_Real_Literal
11011 then
11012 return;
11013
ffe9aba8 11014 -- Here we do the analysis of the range
996ae0b0
RK
11015
11016 -- Note: we do this manually, since if we do a normal Analyze and
11017 -- Resolve call, there are problems with the conversions used for
11018 -- the derived type range.
11019
11020 else
11021 Set_Etype (Rng, Implicit_Base);
11022 Set_Analyzed (Rng, True);
11023 end if;
11024 end Convert_Scalar_Bounds;
11025
11026 -------------------
11027 -- Copy_And_Swap --
11028 -------------------
11029
fbf5a39b 11030 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
996ae0b0
RK
11031 begin
11032 -- Initialize new full declaration entity by copying the pertinent
11033 -- fields of the corresponding private declaration entity.
11034
996ae0b0
RK
11035 -- We temporarily set Ekind to a value appropriate for a type to
11036 -- avoid assert failures in Einfo from checking for setting type
11037 -- attributes on something that is not a type. Ekind (Priv) is an
11038 -- appropriate choice, since it allowed the attributes to be set
11039 -- in the first place. This Ekind value will be modified later.
11040
11041 Set_Ekind (Full, Ekind (Priv));
11042
11043 -- Also set Etype temporarily to Any_Type, again, in the absence
11044 -- of errors, it will be properly reset, and if there are errors,
11045 -- then we want a value of Any_Type to remain.
11046
11047 Set_Etype (Full, Any_Type);
11048
11049 -- Now start copying attributes
11050
11051 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
11052
11053 if Has_Discriminants (Full) then
11054 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
fbf5a39b 11055 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
996ae0b0
RK
11056 end if;
11057
fbf5a39b 11058 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
996ae0b0
RK
11059 Set_Homonym (Full, Homonym (Priv));
11060 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
11061 Set_Is_Public (Full, Is_Public (Priv));
11062 Set_Is_Pure (Full, Is_Pure (Priv));
11063 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
fea9e956
ES
11064 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
11065 Set_Has_Pragma_Unreferenced_Objects
11066 (Full, Has_Pragma_Unreferenced_Objects
11067 (Priv));
996ae0b0
RK
11068
11069 Conditional_Delay (Full, Priv);
11070
11071 if Is_Tagged_Type (Full) then
11072 Set_Primitive_Operations (Full, Primitive_Operations (Priv));
11073
11074 if Priv = Base_Type (Priv) then
11075 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
11076 end if;
11077 end if;
11078
11079 Set_Is_Volatile (Full, Is_Volatile (Priv));
fbf5a39b 11080 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
996ae0b0
RK
11081 Set_Scope (Full, Scope (Priv));
11082 Set_Next_Entity (Full, Next_Entity (Priv));
11083 Set_First_Entity (Full, First_Entity (Priv));
11084 Set_Last_Entity (Full, Last_Entity (Priv));
11085
a5b62485
AC
11086 -- If access types have been recorded for later handling, keep them in
11087 -- the full view so that they get handled when the full view freeze
11088 -- node is expanded.
996ae0b0
RK
11089
11090 if Present (Freeze_Node (Priv))
11091 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
11092 then
11093 Ensure_Freeze_Node (Full);
fbf5a39b
AC
11094 Set_Access_Types_To_Process
11095 (Freeze_Node (Full),
11096 Access_Types_To_Process (Freeze_Node (Priv)));
996ae0b0 11097 end if;
996ae0b0 11098
fbf5a39b
AC
11099 -- Swap the two entities. Now Privat is the full type entity and
11100 -- Full is the private one. They will be swapped back at the end
11101 -- of the private part. This swapping ensures that the entity that
11102 -- is visible in the private part is the full declaration.
996ae0b0 11103
fbf5a39b
AC
11104 Exchange_Entities (Priv, Full);
11105 Append_Entity (Full, Scope (Full));
11106 end Copy_And_Swap;
996ae0b0 11107
fbf5a39b
AC
11108 -------------------------------------
11109 -- Copy_Array_Base_Type_Attributes --
11110 -------------------------------------
996ae0b0 11111
fbf5a39b
AC
11112 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
11113 begin
11114 Set_Component_Alignment (T1, Component_Alignment (T2));
11115 Set_Component_Type (T1, Component_Type (T2));
11116 Set_Component_Size (T1, Component_Size (T2));
11117 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
11118 Set_Finalize_Storage_Only (T1, Finalize_Storage_Only (T2));
11119 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
11120 Set_Has_Task (T1, Has_Task (T2));
11121 Set_Is_Packed (T1, Is_Packed (T2));
11122 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
11123 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
11124 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
11125 end Copy_Array_Base_Type_Attributes;
11126
11127 -----------------------------------
11128 -- Copy_Array_Subtype_Attributes --
11129 -----------------------------------
11130
11131 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
11132 begin
11133 Set_Size_Info (T1, T2);
11134
11135 Set_First_Index (T1, First_Index (T2));
11136 Set_Is_Aliased (T1, Is_Aliased (T2));
11137 Set_Is_Atomic (T1, Is_Atomic (T2));
11138 Set_Is_Volatile (T1, Is_Volatile (T2));
11139 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
11140 Set_Is_Constrained (T1, Is_Constrained (T2));
11141 Set_Depends_On_Private (T1, Has_Private_Component (T2));
11142 Set_First_Rep_Item (T1, First_Rep_Item (T2));
11143 Set_Convention (T1, Convention (T2));
11144 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
11145 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
ff7139c3 11146 Set_Packed_Array_Type (T1, Packed_Array_Type (T2));
fbf5a39b
AC
11147 end Copy_Array_Subtype_Attributes;
11148
11149 -----------------------------------
11150 -- Create_Constrained_Components --
11151 -----------------------------------
11152
11153 procedure Create_Constrained_Components
11154 (Subt : Entity_Id;
11155 Decl_Node : Node_Id;
11156 Typ : Entity_Id;
11157 Constraints : Elist_Id)
11158 is
11159 Loc : constant Source_Ptr := Sloc (Subt);
11160 Comp_List : constant Elist_Id := New_Elmt_List;
11161 Parent_Type : constant Entity_Id := Etype (Typ);
11162 Assoc_List : constant List_Id := New_List;
11163 Discr_Val : Elmt_Id;
11164 Errors : Boolean;
11165 New_C : Entity_Id;
11166 Old_C : Entity_Id;
11167 Is_Static : Boolean := True;
11168
11169 procedure Collect_Fixed_Components (Typ : Entity_Id);
0da2c8ac 11170 -- Collect parent type components that do not appear in a variant part
fbf5a39b
AC
11171
11172 procedure Create_All_Components;
ffe9aba8 11173 -- Iterate over Comp_List to create the components of the subtype
fbf5a39b
AC
11174
11175 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
11176 -- Creates a new component from Old_Compon, copying all the fields from
11177 -- it, including its Etype, inserts the new component in the Subt entity
11178 -- chain and returns the new component.
11179
11180 function Is_Variant_Record (T : Entity_Id) return Boolean;
11181 -- If true, and discriminants are static, collect only components from
11182 -- variants selected by discriminant values.
11183
11184 ------------------------------
996ae0b0
RK
11185 -- Collect_Fixed_Components --
11186 ------------------------------
11187
11188 procedure Collect_Fixed_Components (Typ : Entity_Id) is
11189 begin
a5b62485
AC
11190 -- Build association list for discriminants, and find components of the
11191 -- variant part selected by the values of the discriminants.
996ae0b0
RK
11192
11193 Old_C := First_Discriminant (Typ);
11194 Discr_Val := First_Elmt (Constraints);
996ae0b0
RK
11195 while Present (Old_C) loop
11196 Append_To (Assoc_List,
11197 Make_Component_Association (Loc,
11198 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
11199 Expression => New_Copy (Node (Discr_Val))));
11200
11201 Next_Elmt (Discr_Val);
11202 Next_Discriminant (Old_C);
11203 end loop;
11204
11205 -- The tag, and the possible parent and controller components
11206 -- are unconditionally in the subtype.
11207
11208 if Is_Tagged_Type (Typ)
11209 or else Has_Controlled_Component (Typ)
11210 then
11211 Old_C := First_Component (Typ);
996ae0b0
RK
11212 while Present (Old_C) loop
11213 if Chars ((Old_C)) = Name_uTag
11214 or else Chars ((Old_C)) = Name_uParent
11215 or else Chars ((Old_C)) = Name_uController
11216 then
11217 Append_Elmt (Old_C, Comp_List);
11218 end if;
11219
11220 Next_Component (Old_C);
11221 end loop;
11222 end if;
11223 end Collect_Fixed_Components;
11224
11225 ---------------------------
11226 -- Create_All_Components --
11227 ---------------------------
11228
11229 procedure Create_All_Components is
11230 Comp : Elmt_Id;
11231
11232 begin
11233 Comp := First_Elmt (Comp_List);
996ae0b0
RK
11234 while Present (Comp) loop
11235 Old_C := Node (Comp);
11236 New_C := Create_Component (Old_C);
11237
11238 Set_Etype
11239 (New_C,
11240 Constrain_Component_Type
c6823a20 11241 (Old_C, Subt, Decl_Node, Typ, Constraints));
996ae0b0
RK
11242 Set_Is_Public (New_C, Is_Public (Subt));
11243
11244 Next_Elmt (Comp);
11245 end loop;
11246 end Create_All_Components;
11247
11248 ----------------------
11249 -- Create_Component --
11250 ----------------------
11251
11252 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
fbf5a39b 11253 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
996ae0b0
RK
11254
11255 begin
c0bca7e1
EB
11256 if Ekind (Old_Compon) = E_Discriminant
11257 and then Is_Completely_Hidden (Old_Compon)
11258 then
c0bca7e1 11259 -- This is a shadow discriminant created for a discriminant of
c9e7bd8e
AC
11260 -- the parent type, which needs to be present in the subtype.
11261 -- Give the shadow discriminant an internal name that cannot
11262 -- conflict with that of visible components.
c0bca7e1
EB
11263
11264 Set_Chars (New_Compon, New_Internal_Name ('C'));
11265 end if;
11266
653da906
RD
11267 -- Set the parent so we have a proper link for freezing etc. This is
11268 -- not a real parent pointer, since of course our parent does not own
11269 -- up to us and reference us, we are an illegitimate child of the
11270 -- original parent!
996ae0b0
RK
11271
11272 Set_Parent (New_Compon, Parent (Old_Compon));
11273
653da906
RD
11274 -- If the old component's Esize was already determined and is a
11275 -- static value, then the new component simply inherits it. Otherwise
11276 -- the old component's size may require run-time determination, but
11277 -- the new component's size still might be statically determinable
11278 -- (if, for example it has a static constraint). In that case we want
11279 -- Layout_Type to recompute the component's size, so we reset its
11280 -- size and positional fields.
11281
11282 if Frontend_Layout_On_Target
11283 and then not Known_Static_Esize (Old_Compon)
11284 then
11285 Set_Esize (New_Compon, Uint_0);
11286 Init_Normalized_First_Bit (New_Compon);
11287 Init_Normalized_Position (New_Compon);
11288 Init_Normalized_Position_Max (New_Compon);
11289 end if;
11290
996ae0b0 11291 -- We do not want this node marked as Comes_From_Source, since
653da906
RD
11292 -- otherwise it would get first class status and a separate cross-
11293 -- reference line would be generated. Illegitimate children do not
11294 -- rate such recognition.
996ae0b0
RK
11295
11296 Set_Comes_From_Source (New_Compon, False);
11297
653da906
RD
11298 -- But it is a real entity, and a birth certificate must be properly
11299 -- registered by entering it into the entity list.
996ae0b0
RK
11300
11301 Enter_Name (New_Compon);
653da906 11302
996ae0b0
RK
11303 return New_Compon;
11304 end Create_Component;
11305
11306 -----------------------
11307 -- Is_Variant_Record --
11308 -----------------------
11309
11310 function Is_Variant_Record (T : Entity_Id) return Boolean is
11311 begin
11312 return Nkind (Parent (T)) = N_Full_Type_Declaration
11313 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
11314 and then Present (Component_List (Type_Definition (Parent (T))))
2b73cf68
JM
11315 and then
11316 Present
11317 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
996ae0b0
RK
11318 end Is_Variant_Record;
11319
11320 -- Start of processing for Create_Constrained_Components
11321
11322 begin
11323 pragma Assert (Subt /= Base_Type (Subt));
11324 pragma Assert (Typ = Base_Type (Typ));
11325
11326 Set_First_Entity (Subt, Empty);
11327 Set_Last_Entity (Subt, Empty);
11328
11329 -- Check whether constraint is fully static, in which case we can
11330 -- optimize the list of components.
11331
11332 Discr_Val := First_Elmt (Constraints);
996ae0b0 11333 while Present (Discr_Val) loop
996ae0b0
RK
11334 if not Is_OK_Static_Expression (Node (Discr_Val)) then
11335 Is_Static := False;
11336 exit;
11337 end if;
11338
11339 Next_Elmt (Discr_Val);
11340 end loop;
11341
88b32fc3
BD
11342 Set_Has_Static_Discriminants (Subt, Is_Static);
11343
2b73cf68 11344 Push_Scope (Subt);
996ae0b0 11345
71d9e9f2 11346 -- Inherit the discriminants of the parent type
996ae0b0 11347
c0bca7e1
EB
11348 Add_Discriminants : declare
11349 Num_Disc : Int;
11350 Num_Gird : Int;
11351
11352 begin
11353 Num_Disc := 0;
11354 Old_C := First_Discriminant (Typ);
11355
11356 while Present (Old_C) loop
11357 Num_Disc := Num_Disc + 1;
11358 New_C := Create_Component (Old_C);
11359 Set_Is_Public (New_C, Is_Public (Subt));
11360 Next_Discriminant (Old_C);
11361 end loop;
11362
11363 -- For an untagged derived subtype, the number of discriminants may
11364 -- be smaller than the number of inherited discriminants, because
c9e7bd8e
AC
11365 -- several of them may be renamed by a single new discriminant or
11366 -- constrained. In this case, add the hidden discriminants back into
11367 -- the subtype, because they need to be present if the optimizer of
11368 -- the GCC 4.x back-end decides to break apart assignments between
11369 -- objects using the parent view into member-wise assignments.
c0bca7e1
EB
11370
11371 Num_Gird := 0;
11372
11373 if Is_Derived_Type (Typ)
11374 and then not Is_Tagged_Type (Typ)
11375 then
11376 Old_C := First_Stored_Discriminant (Typ);
11377
11378 while Present (Old_C) loop
11379 Num_Gird := Num_Gird + 1;
11380 Next_Stored_Discriminant (Old_C);
11381 end loop;
11382 end if;
11383
11384 if Num_Gird > Num_Disc then
11385
11386 -- Find out multiple uses of new discriminants, and add hidden
11387 -- components for the extra renamed discriminants. We recognize
11388 -- multiple uses through the Corresponding_Discriminant of a
11389 -- new discriminant: if it constrains several old discriminants,
11390 -- this field points to the last one in the parent type. The
11391 -- stored discriminants of the derived type have the same name
11392 -- as those of the parent.
11393
11394 declare
11395 Constr : Elmt_Id;
11396 New_Discr : Entity_Id;
11397 Old_Discr : Entity_Id;
11398
11399 begin
11400 Constr := First_Elmt (Stored_Constraint (Typ));
11401 Old_Discr := First_Stored_Discriminant (Typ);
c0bca7e1
EB
11402 while Present (Constr) loop
11403 if Is_Entity_Name (Node (Constr))
11404 and then Ekind (Entity (Node (Constr))) = E_Discriminant
11405 then
11406 New_Discr := Entity (Node (Constr));
11407
dc06abec
RD
11408 if Chars (Corresponding_Discriminant (New_Discr)) /=
11409 Chars (Old_Discr)
c0bca7e1 11410 then
dc06abec
RD
11411 -- The new discriminant has been used to rename a
11412 -- subsequent old discriminant. Introduce a shadow
c0bca7e1
EB
11413 -- component for the current old discriminant.
11414
11415 New_C := Create_Component (Old_Discr);
c9e7bd8e 11416 Set_Original_Record_Component (New_C, Old_Discr);
c0bca7e1 11417 end if;
c9e7bd8e
AC
11418
11419 else
11420 -- The constraint has eliminated the old discriminant.
11421 -- Introduce a shadow component.
11422
11423 New_C := Create_Component (Old_Discr);
11424 Set_Original_Record_Component (New_C, Old_Discr);
c0bca7e1
EB
11425 end if;
11426
11427 Next_Elmt (Constr);
11428 Next_Stored_Discriminant (Old_Discr);
11429 end loop;
11430 end;
11431 end if;
11432 end Add_Discriminants;
996ae0b0
RK
11433
11434 if Is_Static
11435 and then Is_Variant_Record (Typ)
11436 then
11437 Collect_Fixed_Components (Typ);
11438
11439 Gather_Components (
11440 Typ,
11441 Component_List (Type_Definition (Parent (Typ))),
11442 Governed_By => Assoc_List,
11443 Into => Comp_List,
11444 Report_Errors => Errors);
11445 pragma Assert (not Errors);
11446
11447 Create_All_Components;
11448
11449 -- If the subtype declaration is created for a tagged type derivation
11450 -- with constraints, we retrieve the record definition of the parent
11451 -- type to select the components of the proper variant.
11452
11453 elsif Is_Static
11454 and then Is_Tagged_Type (Typ)
11455 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11456 and then
11457 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
11458 and then Is_Variant_Record (Parent_Type)
11459 then
11460 Collect_Fixed_Components (Typ);
11461
11462 Gather_Components (
11463 Typ,
11464 Component_List (Type_Definition (Parent (Parent_Type))),
11465 Governed_By => Assoc_List,
11466 Into => Comp_List,
11467 Report_Errors => Errors);
11468 pragma Assert (not Errors);
11469
11470 -- If the tagged derivation has a type extension, collect all the
11471 -- new components therein.
11472
0da2c8ac
AC
11473 if Present
11474 (Record_Extension_Part (Type_Definition (Parent (Typ))))
996ae0b0
RK
11475 then
11476 Old_C := First_Component (Typ);
996ae0b0
RK
11477 while Present (Old_C) loop
11478 if Original_Record_Component (Old_C) = Old_C
11479 and then Chars (Old_C) /= Name_uTag
11480 and then Chars (Old_C) /= Name_uParent
11481 and then Chars (Old_C) /= Name_uController
11482 then
11483 Append_Elmt (Old_C, Comp_List);
11484 end if;
11485
11486 Next_Component (Old_C);
11487 end loop;
11488 end if;
11489
11490 Create_All_Components;
11491
11492 else
9dfd2ff8
CC
11493 -- If discriminants are not static, or if this is a multi-level type
11494 -- extension, we have to include all components of the parent type.
996ae0b0
RK
11495
11496 Old_C := First_Component (Typ);
996ae0b0
RK
11497 while Present (Old_C) loop
11498 New_C := Create_Component (Old_C);
11499
11500 Set_Etype
11501 (New_C,
11502 Constrain_Component_Type
c6823a20 11503 (Old_C, Subt, Decl_Node, Typ, Constraints));
996ae0b0
RK
11504 Set_Is_Public (New_C, Is_Public (Subt));
11505
11506 Next_Component (Old_C);
11507 end loop;
11508 end if;
11509
11510 End_Scope;
11511 end Create_Constrained_Components;
11512
11513 ------------------------------------------
11514 -- Decimal_Fixed_Point_Type_Declaration --
11515 ------------------------------------------
11516
11517 procedure Decimal_Fixed_Point_Type_Declaration
11518 (T : Entity_Id;
11519 Def : Node_Id)
11520 is
11521 Loc : constant Source_Ptr := Sloc (Def);
11522 Digs_Expr : constant Node_Id := Digits_Expression (Def);
11523 Delta_Expr : constant Node_Id := Delta_Expression (Def);
11524 Implicit_Base : Entity_Id;
11525 Digs_Val : Uint;
11526 Delta_Val : Ureal;
11527 Scale_Val : Uint;
11528 Bound_Val : Ureal;
11529
996ae0b0
RK
11530 begin
11531 Check_Restriction (No_Fixed_Point, Def);
11532
11533 -- Create implicit base type
11534
11535 Implicit_Base :=
11536 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
11537 Set_Etype (Implicit_Base, Implicit_Base);
11538
11539 -- Analyze and process delta expression
11540
11541 Analyze_And_Resolve (Delta_Expr, Universal_Real);
11542
11543 Check_Delta_Expression (Delta_Expr);
11544 Delta_Val := Expr_Value_R (Delta_Expr);
11545
11546 -- Check delta is power of 10, and determine scale value from it
11547
11548 declare
9dfd2ff8 11549 Val : Ureal;
996ae0b0
RK
11550
11551 begin
11552 Scale_Val := Uint_0;
9dfd2ff8 11553 Val := Delta_Val;
996ae0b0
RK
11554
11555 if Val < Ureal_1 then
11556 while Val < Ureal_1 loop
11557 Val := Val * Ureal_10;
11558 Scale_Val := Scale_Val + 1;
11559 end loop;
11560
11561 if Scale_Val > 18 then
11562 Error_Msg_N ("scale exceeds maximum value of 18", Def);
11563 Scale_Val := UI_From_Int (+18);
11564 end if;
11565
11566 else
11567 while Val > Ureal_1 loop
11568 Val := Val / Ureal_10;
11569 Scale_Val := Scale_Val - 1;
11570 end loop;
11571
11572 if Scale_Val < -18 then
11573 Error_Msg_N ("scale is less than minimum value of -18", Def);
11574 Scale_Val := UI_From_Int (-18);
11575 end if;
11576 end if;
11577
11578 if Val /= Ureal_1 then
11579 Error_Msg_N ("delta expression must be a power of 10", Def);
11580 Delta_Val := Ureal_10 ** (-Scale_Val);
11581 end if;
11582 end;
11583
11584 -- Set delta, scale and small (small = delta for decimal type)
11585
11586 Set_Delta_Value (Implicit_Base, Delta_Val);
11587 Set_Scale_Value (Implicit_Base, Scale_Val);
11588 Set_Small_Value (Implicit_Base, Delta_Val);
11589
11590 -- Analyze and process digits expression
11591
11592 Analyze_And_Resolve (Digs_Expr, Any_Integer);
11593 Check_Digits_Expression (Digs_Expr);
11594 Digs_Val := Expr_Value (Digs_Expr);
11595
11596 if Digs_Val > 18 then
11597 Digs_Val := UI_From_Int (+18);
11598 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
11599 end if;
11600
11601 Set_Digits_Value (Implicit_Base, Digs_Val);
11602 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
11603
11604 -- Set range of base type from digits value for now. This will be
11605 -- expanded to represent the true underlying base range by Freeze.
11606
11607 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
11608
ce4a6e84
RD
11609 -- Note: We leave size as zero for now, size will be set at freeze
11610 -- time. We have to do this for ordinary fixed-point, because the size
11611 -- depends on the specified small, and we might as well do the same for
11612 -- decimal fixed-point.
996ae0b0 11613
ce4a6e84 11614 pragma Assert (Esize (Implicit_Base) = Uint_0);
996ae0b0 11615
996ae0b0
RK
11616 -- If there are bounds given in the declaration use them as the
11617 -- bounds of the first named subtype.
11618
11619 if Present (Real_Range_Specification (Def)) then
11620 declare
11621 RRS : constant Node_Id := Real_Range_Specification (Def);
11622 Low : constant Node_Id := Low_Bound (RRS);
11623 High : constant Node_Id := High_Bound (RRS);
11624 Low_Val : Ureal;
11625 High_Val : Ureal;
11626
11627 begin
11628 Analyze_And_Resolve (Low, Any_Real);
11629 Analyze_And_Resolve (High, Any_Real);
11630 Check_Real_Bound (Low);
11631 Check_Real_Bound (High);
11632 Low_Val := Expr_Value_R (Low);
11633 High_Val := Expr_Value_R (High);
11634
11635 if Low_Val < (-Bound_Val) then
11636 Error_Msg_N
11637 ("range low bound too small for digits value", Low);
11638 Low_Val := -Bound_Val;
11639 end if;
11640
11641 if High_Val > Bound_Val then
11642 Error_Msg_N
11643 ("range high bound too large for digits value", High);
11644 High_Val := Bound_Val;
11645 end if;
11646
11647 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
11648 end;
11649
11650 -- If no explicit range, use range that corresponds to given
11651 -- digits value. This will end up as the final range for the
11652 -- first subtype.
11653
11654 else
11655 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
11656 end if;
11657
c45b6ae0
AC
11658 -- Complete entity for first subtype
11659
11660 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
11661 Set_Etype (T, Implicit_Base);
11662 Set_Size_Info (T, Implicit_Base);
11663 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
11664 Set_Digits_Value (T, Digs_Val);
11665 Set_Delta_Value (T, Delta_Val);
11666 Set_Small_Value (T, Delta_Val);
11667 Set_Scale_Value (T, Scale_Val);
11668 Set_Is_Constrained (T);
996ae0b0
RK
11669 end Decimal_Fixed_Point_Type_Declaration;
11670
ce2b6ba5
JM
11671 -----------------------------------
11672 -- Derive_Progenitor_Subprograms --
11673 -----------------------------------
758c442c 11674
ce2b6ba5 11675 procedure Derive_Progenitor_Subprograms
88b32fc3 11676 (Parent_Type : Entity_Id;
ce2b6ba5 11677 Tagged_Type : Entity_Id)
88b32fc3 11678 is
ce2b6ba5
JM
11679 E : Entity_Id;
11680 Elmt : Elmt_Id;
11681 Iface : Entity_Id;
11682 Iface_Elmt : Elmt_Id;
11683 Iface_Subp : Entity_Id;
11684 New_Subp : Entity_Id := Empty;
11685 Prim_Elmt : Elmt_Id;
11686 Subp : Entity_Id;
11687 Typ : Entity_Id;
758c442c 11688
ce2b6ba5
JM
11689 begin
11690 pragma Assert (Ada_Version >= Ada_05
11691 and then Is_Record_Type (Tagged_Type)
11692 and then Is_Tagged_Type (Tagged_Type)
11693 and then Has_Interfaces (Tagged_Type));
11694
30783513 11695 -- Step 1: Transfer to the full-view primitives associated with the
ce2b6ba5
JM
11696 -- partial-view that cover interface primitives. Conceptually this
11697 -- work should be done later by Process_Full_View; done here to
11698 -- simplify its implementation at later stages. It can be safely
11699 -- done here because interfaces must be visible in the partial and
11700 -- private view (RM 7.3(7.3/2)).
11701
11702 -- Small optimization: This work is only required if the parent is
11703 -- abstract. If the tagged type is not abstract, it cannot have
11704 -- abstract primitives (the only entities in the list of primitives of
11705 -- non-abstract tagged types that can reference abstract primitives
11706 -- through its Alias attribute are the internal entities that have
11707 -- attribute Interface_Alias, and these entities are generated later
11708 -- by Freeze_Record_Type).
88b32fc3 11709
ce2b6ba5
JM
11710 if In_Private_Part (Current_Scope)
11711 and then Is_Abstract_Type (Parent_Type)
11712 then
11713 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
11714 while Present (Elmt) loop
11715 Subp := Node (Elmt);
88b32fc3 11716
ce2b6ba5
JM
11717 -- At this stage it is not possible to have entities in the list
11718 -- of primitives that have attribute Interface_Alias
758c442c 11719
ce2b6ba5 11720 pragma Assert (No (Interface_Alias (Subp)));
758c442c 11721
ce2b6ba5 11722 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
88b32fc3 11723
ce2b6ba5
JM
11724 if Is_Interface (Typ) then
11725 E := Find_Primitive_Covering_Interface
11726 (Tagged_Type => Tagged_Type,
11727 Iface_Prim => Subp);
88b32fc3 11728
ce2b6ba5
JM
11729 if Present (E)
11730 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
11731 then
11732 Replace_Elmt (Elmt, E);
11733 Remove_Homonym (Subp);
950d3e7d 11734 end if;
88b32fc3
BD
11735 end if;
11736
11737 Next_Elmt (Elmt);
11738 end loop;
88b32fc3
BD
11739 end if;
11740
ce2b6ba5
JM
11741 -- Step 2: Add primitives of progenitors that are not implemented by
11742 -- parents of Tagged_Type
88b32fc3 11743
59262ebb
AC
11744 if Present (Interfaces (Base_Type (Tagged_Type))) then
11745 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
ce2b6ba5
JM
11746 while Present (Iface_Elmt) loop
11747 Iface := Node (Iface_Elmt);
88b32fc3 11748
ce2b6ba5
JM
11749 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
11750 while Present (Prim_Elmt) loop
11751 Iface_Subp := Node (Prim_Elmt);
fea9e956 11752
9800ef59
JM
11753 -- Exclude derivation of predefined primitives except those
11754 -- that come from source. Required to catch declarations of
11755 -- equality operators of interfaces. For example:
11756
11757 -- type Iface is interface;
11758 -- function "=" (Left, Right : Iface) return Boolean;
11759
8c3dd7a8
JM
11760 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
11761 or else Comes_From_Source (Iface_Subp)
11762 then
ce2b6ba5
JM
11763 E := Find_Primitive_Covering_Interface
11764 (Tagged_Type => Tagged_Type,
11765 Iface_Prim => Iface_Subp);
88b32fc3 11766
ce2b6ba5
JM
11767 -- If not found we derive a new primitive leaving its alias
11768 -- attribute referencing the interface primitive
88b32fc3 11769
ce2b6ba5
JM
11770 if No (E) then
11771 Derive_Subprogram
11772 (New_Subp, Iface_Subp, Tagged_Type, Iface);
88b32fc3 11773
ce2b6ba5
JM
11774 -- Propagate to the full view interface entities associated
11775 -- with the partial view
88b32fc3 11776
ce2b6ba5
JM
11777 elsif In_Private_Part (Current_Scope)
11778 and then Present (Alias (E))
11779 and then Alias (E) = Iface_Subp
11780 and then
11781 List_Containing (Parent (E)) /=
11782 Private_Declarations
11783 (Specification
11784 (Unit_Declaration_Node (Current_Scope)))
11785 then
11786 Append_Elmt (E, Primitive_Operations (Tagged_Type));
11787 end if;
88b32fc3
BD
11788 end if;
11789
ce2b6ba5 11790 Next_Elmt (Prim_Elmt);
88b32fc3
BD
11791 end loop;
11792
ce2b6ba5 11793 Next_Elmt (Iface_Elmt);
88b32fc3
BD
11794 end loop;
11795 end if;
ce2b6ba5 11796 end Derive_Progenitor_Subprograms;
758c442c 11797
996ae0b0
RK
11798 -----------------------
11799 -- Derive_Subprogram --
11800 -----------------------
11801
11802 procedure Derive_Subprogram
11803 (New_Subp : in out Entity_Id;
11804 Parent_Subp : Entity_Id;
11805 Derived_Type : Entity_Id;
11806 Parent_Type : Entity_Id;
11807 Actual_Subp : Entity_Id := Empty)
11808 is
ce4a6e84
RD
11809 Formal : Entity_Id;
11810 -- Formal parameter of parent primitive operation
11811
11812 Formal_Of_Actual : Entity_Id;
11813 -- Formal parameter of actual operation, when the derivation is to
11814 -- create a renaming for a primitive operation of an actual in an
11815 -- instantiation.
11816
11817 New_Formal : Entity_Id;
11818 -- Formal of inherited operation
11819
fbf5a39b 11820 Visible_Subp : Entity_Id := Parent_Subp;
996ae0b0
RK
11821
11822 function Is_Private_Overriding return Boolean;
ce4a6e84
RD
11823 -- If Subp is a private overriding of a visible operation, the inherited
11824 -- operation derives from the overridden op (even though its body is the
11825 -- overriding one) and the inherited operation is visible now. See
11826 -- sem_disp to see the full details of the handling of the overridden
11827 -- subprogram, which is removed from the list of primitive operations of
11828 -- the type. The overridden subprogram is saved locally in Visible_Subp,
11829 -- and used to diagnose abstract operations that need overriding in the
11830 -- derived type.
996ae0b0
RK
11831
11832 procedure Replace_Type (Id, New_Id : Entity_Id);
11833 -- When the type is an anonymous access type, create a new access type
11834 -- designating the derived type.
11835
fbf5a39b
AC
11836 procedure Set_Derived_Name;
11837 -- This procedure sets the appropriate Chars name for New_Subp. This
11838 -- is normally just a copy of the parent name. An exception arises for
11839 -- type support subprograms, where the name is changed to reflect the
11840 -- name of the derived type, e.g. if type foo is derived from type bar,
11841 -- then a procedure barDA is derived with a name fooDA.
11842
996ae0b0
RK
11843 ---------------------------
11844 -- Is_Private_Overriding --
11845 ---------------------------
11846
11847 function Is_Private_Overriding return Boolean is
11848 Prev : Entity_Id;
11849
11850 begin
88b32fc3
BD
11851 -- If the parent is not a dispatching operation there is no
11852 -- need to investigate overridings
11853
11854 if not Is_Dispatching_Operation (Parent_Subp) then
11855 return False;
11856 end if;
11857
9dfd2ff8 11858 -- The visible operation that is overridden is a homonym of the
a5b62485
AC
11859 -- parent subprogram. We scan the homonym chain to find the one
11860 -- whose alias is the subprogram we are deriving.
996ae0b0 11861
9dfd2ff8 11862 Prev := Current_Entity (Parent_Subp);
996ae0b0 11863 while Present (Prev) loop
88b32fc3 11864 if Ekind (Prev) = Ekind (Parent_Subp)
996ae0b0
RK
11865 and then Alias (Prev) = Parent_Subp
11866 and then Scope (Parent_Subp) = Scope (Prev)
88b32fc3 11867 and then not Is_Hidden (Prev)
996ae0b0 11868 then
fbf5a39b 11869 Visible_Subp := Prev;
996ae0b0
RK
11870 return True;
11871 end if;
11872
11873 Prev := Homonym (Prev);
11874 end loop;
11875
11876 return False;
11877 end Is_Private_Overriding;
11878
11879 ------------------
11880 -- Replace_Type --
11881 ------------------
11882
11883 procedure Replace_Type (Id, New_Id : Entity_Id) is
11884 Acc_Type : Entity_Id;
0da2c8ac 11885 Par : constant Node_Id := Parent (Derived_Type);
996ae0b0
RK
11886
11887 begin
11888 -- When the type is an anonymous access type, create a new access
11889 -- type designating the derived type. This itype must be elaborated
11890 -- at the point of the derivation, not on subsequent calls that may
11891 -- be out of the proper scope for Gigi, so we insert a reference to
11892 -- it after the derivation.
11893
11894 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
11895 declare
11896 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
11897
11898 begin
11899 if Ekind (Desig_Typ) = E_Record_Type_With_Private
11900 and then Present (Full_View (Desig_Typ))
11901 and then not Is_Private_Type (Parent_Type)
11902 then
11903 Desig_Typ := Full_View (Desig_Typ);
11904 end if;
11905
88b32fc3
BD
11906 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
11907
11908 -- Ada 2005 (AI-251): Handle also derivations of abstract
11909 -- interface primitives.
11910
11911 or else (Is_Interface (Desig_Typ)
11912 and then not Is_Class_Wide_Type (Desig_Typ))
11913 then
996ae0b0
RK
11914 Acc_Type := New_Copy (Etype (Id));
11915 Set_Etype (Acc_Type, Acc_Type);
11916 Set_Scope (Acc_Type, New_Subp);
11917
71d9e9f2 11918 -- Compute size of anonymous access type
996ae0b0
RK
11919
11920 if Is_Array_Type (Desig_Typ)
11921 and then not Is_Constrained (Desig_Typ)
11922 then
11923 Init_Size (Acc_Type, 2 * System_Address_Size);
11924 else
11925 Init_Size (Acc_Type, System_Address_Size);
11926 end if;
11927
11928 Init_Alignment (Acc_Type);
996ae0b0
RK
11929 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
11930
11931 Set_Etype (New_Id, Acc_Type);
11932 Set_Scope (New_Id, New_Subp);
11933
0da2c8ac 11934 -- Create a reference to it
fea9e956 11935 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
996ae0b0
RK
11936
11937 else
11938 Set_Etype (New_Id, Etype (Id));
11939 end if;
11940 end;
0da2c8ac 11941
996ae0b0
RK
11942 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
11943 or else
11944 (Ekind (Etype (Id)) = E_Record_Type_With_Private
11945 and then Present (Full_View (Etype (Id)))
0da2c8ac
AC
11946 and then
11947 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
996ae0b0 11948 then
996ae0b0
RK
11949 -- Constraint checks on formals are generated during expansion,
11950 -- based on the signature of the original subprogram. The bounds
11951 -- of the derived type are not relevant, and thus we can use
11952 -- the base type for the formals. However, the return type may be
11953 -- used in a context that requires that the proper static bounds
11954 -- be used (a case statement, for example) and for those cases
11955 -- we must use the derived type (first subtype), not its base.
11956
0da2c8ac
AC
11957 -- If the derived_type_definition has no constraints, we know that
11958 -- the derived type has the same constraints as the first subtype
11959 -- of the parent, and we can also use it rather than its base,
11960 -- which can lead to more efficient code.
11961
11962 if Etype (Id) = Parent_Type then
11963 if Is_Scalar_Type (Parent_Type)
11964 and then
11965 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
11966 then
11967 Set_Etype (New_Id, Derived_Type);
11968
11969 elsif Nkind (Par) = N_Full_Type_Declaration
11970 and then
11971 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
11972 and then
11973 Is_Entity_Name
11974 (Subtype_Indication (Type_Definition (Par)))
11975 then
11976 Set_Etype (New_Id, Derived_Type);
11977
11978 else
11979 Set_Etype (New_Id, Base_Type (Derived_Type));
11980 end if;
11981
996ae0b0
RK
11982 else
11983 Set_Etype (New_Id, Base_Type (Derived_Type));
11984 end if;
11985
88b32fc3
BD
11986 -- Ada 2005 (AI-251): Handle derivations of abstract interface
11987 -- primitives.
11988
11989 elsif Is_Interface (Etype (Id))
11990 and then not Is_Class_Wide_Type (Etype (Id))
ce4a6e84 11991 and then Is_Progenitor (Etype (Id), Derived_Type)
88b32fc3
BD
11992 then
11993 Set_Etype (New_Id, Derived_Type);
11994
996ae0b0
RK
11995 else
11996 Set_Etype (New_Id, Etype (Id));
11997 end if;
11998 end Replace_Type;
11999
fbf5a39b
AC
12000 ----------------------
12001 -- Set_Derived_Name --
12002 ----------------------
12003
12004 procedure Set_Derived_Name is
12005 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
12006 begin
12007 if Nm = TSS_Null then
12008 Set_Chars (New_Subp, Chars (Parent_Subp));
12009 else
12010 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
12011 end if;
12012 end Set_Derived_Name;
12013
ce2b6ba5
JM
12014 -- Local variables
12015
12016 Parent_Overrides_Interface_Primitive : Boolean := False;
12017
996ae0b0
RK
12018 -- Start of processing for Derive_Subprogram
12019
12020 begin
12021 New_Subp :=
12022 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
12023 Set_Ekind (New_Subp, Ekind (Parent_Subp));
12024
ce2b6ba5
JM
12025 -- Check whether the parent overrides an interface primitive
12026
12027 if Is_Overriding_Operation (Parent_Subp) then
12028 declare
12029 E : Entity_Id := Parent_Subp;
12030 begin
12031 while Present (Overridden_Operation (E)) loop
12032 E := Ultimate_Alias (Overridden_Operation (E));
12033 end loop;
12034
12035 Parent_Overrides_Interface_Primitive :=
12036 Is_Dispatching_Operation (E)
12037 and then Present (Find_Dispatching_Type (E))
12038 and then Is_Interface (Find_Dispatching_Type (E));
12039 end;
12040 end if;
12041
996ae0b0
RK
12042 -- Check whether the inherited subprogram is a private operation that
12043 -- should be inherited but not yet made visible. Such subprograms can
12044 -- become visible at a later point (e.g., the private part of a public
12045 -- child unit) via Declare_Inherited_Private_Subprograms. If the
12046 -- following predicate is true, then this is not such a private
12047 -- operation and the subprogram simply inherits the name of the parent
12048 -- subprogram. Note the special check for the names of controlled
12049 -- operations, which are currently exempted from being inherited with
12050 -- a hidden name because they must be findable for generation of
12051 -- implicit run-time calls.
12052
12053 if not Is_Hidden (Parent_Subp)
12054 or else Is_Internal (Parent_Subp)
12055 or else Is_Private_Overriding
12056 or else Is_Internal_Name (Chars (Parent_Subp))
12057 or else Chars (Parent_Subp) = Name_Initialize
12058 or else Chars (Parent_Subp) = Name_Adjust
12059 or else Chars (Parent_Subp) = Name_Finalize
12060 then
fbf5a39b 12061 Set_Derived_Name;
996ae0b0 12062
af268547
ES
12063 -- An inherited dispatching equality will be overridden by an internally
12064 -- generated one, or by an explicit one, so preserve its name and thus
12065 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
12066 -- private operation it may become invisible if the full view has
12067 -- progenitors, and the dispatch table will be malformed.
12068 -- We check that the type is limited to handle the anomalous declaration
12069 -- of Limited_Controlled, which is derived from a non-limited type, and
12070 -- which is handled specially elsewhere as well.
12071
12072 elsif Chars (Parent_Subp) = Name_Op_Eq
12073 and then Is_Dispatching_Operation (Parent_Subp)
12074 and then Etype (Parent_Subp) = Standard_Boolean
c0985d4e 12075 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
af268547 12076 and then
c0985d4e
HK
12077 Etype (First_Formal (Parent_Subp)) =
12078 Etype (Next_Formal (First_Formal (Parent_Subp)))
af268547
ES
12079 then
12080 Set_Derived_Name;
12081
996ae0b0
RK
12082 -- If parent is hidden, this can be a regular derivation if the
12083 -- parent is immediately visible in a non-instantiating context,
12084 -- or if we are in the private part of an instance. This test
12085 -- should still be refined ???
12086
a5b62485
AC
12087 -- The test for In_Instance_Not_Visible avoids inheriting the derived
12088 -- operation as a non-visible operation in cases where the parent
12089 -- subprogram might not be visible now, but was visible within the
12090 -- original generic, so it would be wrong to make the inherited
12091 -- subprogram non-visible now. (Not clear if this test is fully
12092 -- correct; are there any cases where we should declare the inherited
12093 -- operation as not visible to avoid it being overridden, e.g., when
12094 -- the parent type is a generic actual with private primitives ???)
996ae0b0
RK
12095
12096 -- (they should be treated the same as other private inherited
12097 -- subprograms, but it's not clear how to do this cleanly). ???
12098
12099 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
12100 and then Is_Immediately_Visible (Parent_Subp)
12101 and then not In_Instance)
12102 or else In_Instance_Not_Visible
12103 then
fbf5a39b 12104 Set_Derived_Name;
996ae0b0 12105
ce2b6ba5
JM
12106 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
12107 -- overrides an interface primitive because interface primitives
12108 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
88b32fc3 12109
ce2b6ba5 12110 elsif Parent_Overrides_Interface_Primitive then
88b32fc3
BD
12111 Set_Derived_Name;
12112
af268547 12113 -- Otherwise, the type is inheriting a private operation, so enter
996ae0b0
RK
12114 -- it with a special name so it can't be overridden.
12115
12116 else
12117 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
12118 end if;
12119
12120 Set_Parent (New_Subp, Parent (Derived_Type));
ce4a6e84
RD
12121
12122 if Present (Actual_Subp) then
12123 Replace_Type (Actual_Subp, New_Subp);
12124 else
12125 Replace_Type (Parent_Subp, New_Subp);
12126 end if;
12127
996ae0b0
RK
12128 Conditional_Delay (New_Subp, Parent_Subp);
12129
ce4a6e84
RD
12130 -- If we are creating a renaming for a primitive operation of an
12131 -- actual of a generic derived type, we must examine the signature
f3d0f304 12132 -- of the actual primitive, not that of the generic formal, which for
ce4a6e84
RD
12133 -- example may be an interface. However the name and initial value
12134 -- of the inherited operation are those of the formal primitive.
12135
996ae0b0 12136 Formal := First_Formal (Parent_Subp);
ce4a6e84
RD
12137
12138 if Present (Actual_Subp) then
12139 Formal_Of_Actual := First_Formal (Actual_Subp);
12140 else
12141 Formal_Of_Actual := Empty;
12142 end if;
12143
996ae0b0
RK
12144 while Present (Formal) loop
12145 New_Formal := New_Copy (Formal);
12146
12147 -- Normally we do not go copying parents, but in the case of
a5b62485
AC
12148 -- formals, we need to link up to the declaration (which is the
12149 -- parameter specification), and it is fine to link up to the
12150 -- original formal's parameter specification in this case.
996ae0b0
RK
12151
12152 Set_Parent (New_Formal, Parent (Formal));
996ae0b0
RK
12153 Append_Entity (New_Formal, New_Subp);
12154
ce4a6e84
RD
12155 if Present (Formal_Of_Actual) then
12156 Replace_Type (Formal_Of_Actual, New_Formal);
12157 Next_Formal (Formal_Of_Actual);
12158 else
12159 Replace_Type (Formal, New_Formal);
12160 end if;
12161
996ae0b0
RK
12162 Next_Formal (Formal);
12163 end loop;
12164
12165 -- If this derivation corresponds to a tagged generic actual, then
12166 -- primitive operations rename those of the actual. Otherwise the
ce4a6e84
RD
12167 -- primitive operations rename those of the parent type, If the parent
12168 -- renames an intrinsic operator, so does the new subprogram. We except
12169 -- concatenation, which is always properly typed, and does not get
12170 -- expanded as other intrinsic operations.
996ae0b0
RK
12171
12172 if No (Actual_Subp) then
fbf5a39b
AC
12173 if Is_Intrinsic_Subprogram (Parent_Subp) then
12174 Set_Is_Intrinsic_Subprogram (New_Subp);
12175
12176 if Present (Alias (Parent_Subp))
12177 and then Chars (Parent_Subp) /= Name_Op_Concat
12178 then
12179 Set_Alias (New_Subp, Alias (Parent_Subp));
12180 else
12181 Set_Alias (New_Subp, Parent_Subp);
12182 end if;
12183
12184 else
12185 Set_Alias (New_Subp, Parent_Subp);
12186 end if;
996ae0b0
RK
12187
12188 else
12189 Set_Alias (New_Subp, Actual_Subp);
12190 end if;
12191
12192 -- Derived subprograms of a tagged type must inherit the convention
12193 -- of the parent subprogram (a requirement of AI-117). Derived
12194 -- subprograms of untagged types simply get convention Ada by default.
12195
12196 if Is_Tagged_Type (Derived_Type) then
88b32fc3 12197 Set_Convention (New_Subp, Convention (Parent_Subp));
996ae0b0
RK
12198 end if;
12199
12200 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
12201 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
12202
12203 if Ekind (Parent_Subp) = E_Procedure then
12204 Set_Is_Valued_Procedure
12205 (New_Subp, Is_Valued_Procedure (Parent_Subp));
12206 end if;
12207
57193e09
TQ
12208 -- No_Return must be inherited properly. If this is overridden in the
12209 -- case of a dispatching operation, then a check is made in Sem_Disp
12210 -- that the overriding operation is also No_Return (no such check is
12211 -- required for the case of non-dispatching operation.
12212
12213 Set_No_Return (New_Subp, No_Return (Parent_Subp));
12214
a5b62485
AC
12215 -- A derived function with a controlling result is abstract. If the
12216 -- Derived_Type is a nonabstract formal generic derived type, then
12217 -- inherited operations are not abstract: the required check is done at
12218 -- instantiation time. If the derivation is for a generic actual, the
12219 -- function is not abstract unless the actual is.
fbf5a39b
AC
12220
12221 if Is_Generic_Type (Derived_Type)
fea9e956 12222 and then not Is_Abstract_Type (Derived_Type)
fbf5a39b
AC
12223 then
12224 null;
12225
fea9e956
ES
12226 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
12227 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
12228
12229 elsif Ada_Version >= Ada_05
12230 and then (Is_Abstract_Subprogram (Alias (New_Subp))
12231 or else (Is_Tagged_Type (Derived_Type)
12232 and then Etype (New_Subp) = Derived_Type
12233 and then not Is_Null_Extension (Derived_Type))
12234 or else (Is_Tagged_Type (Derived_Type)
12235 and then Ekind (Etype (New_Subp)) =
12236 E_Anonymous_Access_Type
12237 and then Designated_Type (Etype (New_Subp)) =
12238 Derived_Type
12239 and then not Is_Null_Extension (Derived_Type)))
12240 and then No (Actual_Subp)
12241 then
12242 if not Is_Tagged_Type (Derived_Type)
12243 or else Is_Abstract_Type (Derived_Type)
12244 or else Is_Abstract_Subprogram (Alias (New_Subp))
12245 then
12246 Set_Is_Abstract_Subprogram (New_Subp);
12247 else
12248 Set_Requires_Overriding (New_Subp);
12249 end if;
12250
12251 elsif Ada_Version < Ada_05
12252 and then (Is_Abstract_Subprogram (Alias (New_Subp))
12253 or else (Is_Tagged_Type (Derived_Type)
12254 and then Etype (New_Subp) = Derived_Type
12255 and then No (Actual_Subp)))
fbf5a39b 12256 then
fea9e956 12257 Set_Is_Abstract_Subprogram (New_Subp);
fbf5a39b 12258
2b73cf68 12259 -- Finally, if the parent type is abstract we must verify that all
ce4a6e84
RD
12260 -- inherited operations are either non-abstract or overridden, or that
12261 -- the derived type itself is abstract (this check is performed at the
12262 -- end of a package declaration, in Check_Abstract_Overriding). A
12263 -- private overriding in the parent type will not be visible in the
fbf5a39b
AC
12264 -- derivation if we are not in an inner package or in a child unit of
12265 -- the parent type, in which case the abstractness of the inherited
12266 -- operation is carried to the new subprogram.
12267
fea9e956 12268 elsif Is_Abstract_Type (Parent_Type)
fbf5a39b
AC
12269 and then not In_Open_Scopes (Scope (Parent_Type))
12270 and then Is_Private_Overriding
fea9e956 12271 and then Is_Abstract_Subprogram (Visible_Subp)
fbf5a39b 12272 then
2b73cf68
JM
12273 if No (Actual_Subp) then
12274 Set_Alias (New_Subp, Visible_Subp);
12275 Set_Is_Abstract_Subprogram
12276 (New_Subp, True);
12277 else
12278 -- If this is a derivation for an instance of a formal derived
12279 -- type, abstractness comes from the primitive operation of the
12280 -- actual, not from the operation inherited from the ancestor.
12281
12282 Set_Is_Abstract_Subprogram
12283 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
12284 end if;
fbf5a39b
AC
12285 end if;
12286
996ae0b0
RK
12287 New_Overloaded_Entity (New_Subp, Derived_Type);
12288
a5b62485
AC
12289 -- Check for case of a derived subprogram for the instantiation of a
12290 -- formal derived tagged type, if so mark the subprogram as dispatching
12291 -- and inherit the dispatching attributes of the parent subprogram. The
12292 -- derived subprogram is effectively renaming of the actual subprogram,
12293 -- so it needs to have the same attributes as the actual.
996ae0b0
RK
12294
12295 if Present (Actual_Subp)
12296 and then Is_Dispatching_Operation (Parent_Subp)
12297 then
12298 Set_Is_Dispatching_Operation (New_Subp);
88b32fc3 12299
996ae0b0
RK
12300 if Present (DTC_Entity (Parent_Subp)) then
12301 Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
12302 Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
12303 end if;
12304 end if;
12305
a5b62485
AC
12306 -- Indicate that a derived subprogram does not require a body and that
12307 -- it does not require processing of default expressions.
996ae0b0
RK
12308
12309 Set_Has_Completion (New_Subp);
12310 Set_Default_Expressions_Processed (New_Subp);
12311
996ae0b0
RK
12312 if Ekind (New_Subp) = E_Function then
12313 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
12314 end if;
12315 end Derive_Subprogram;
12316
12317 ------------------------
12318 -- Derive_Subprograms --
12319 ------------------------
12320
12321 procedure Derive_Subprograms
7d7af38a
JM
12322 (Parent_Type : Entity_Id;
12323 Derived_Type : Entity_Id;
12324 Generic_Actual : Entity_Id := Empty)
996ae0b0 12325 is
ce2b6ba5
JM
12326 Op_List : constant Elist_Id :=
12327 Collect_Primitive_Operations (Parent_Type);
12328
12329 function Check_Derived_Type return Boolean;
12330 -- Check that all primitive inherited from Parent_Type are found in
12331 -- the list of primitives of Derived_Type exactly in the same order.
12332
12333 function Check_Derived_Type return Boolean is
12334 E : Entity_Id;
12335 Elmt : Elmt_Id;
12336 List : Elist_Id;
12337 New_Subp : Entity_Id;
12338 Op_Elmt : Elmt_Id;
12339 Subp : Entity_Id;
12340
12341 begin
12342 -- Traverse list of entities in the current scope searching for
12343 -- an incomplete type whose full-view is derived type
12344
12345 E := First_Entity (Scope (Derived_Type));
12346 while Present (E)
12347 and then E /= Derived_Type
12348 loop
12349 if Ekind (E) = E_Incomplete_Type
12350 and then Present (Full_View (E))
12351 and then Full_View (E) = Derived_Type
12352 then
12353 -- Disable this test if Derived_Type completes an incomplete
12354 -- type because in such case more primitives can be added
12355 -- later to the list of primitives of Derived_Type by routine
12356 -- Process_Incomplete_Dependents
12357
12358 return True;
12359 end if;
12360
12361 E := Next_Entity (E);
12362 end loop;
12363
12364 List := Collect_Primitive_Operations (Derived_Type);
12365 Elmt := First_Elmt (List);
12366
12367 Op_Elmt := First_Elmt (Op_List);
12368 while Present (Op_Elmt) loop
12369 Subp := Node (Op_Elmt);
12370 New_Subp := Node (Elmt);
12371
12372 -- At this early stage Derived_Type has no entities with attribute
12373 -- Interface_Alias. In addition, such primitives are always
12374 -- located at the end of the list of primitives of Parent_Type.
12375 -- Therefore, if found we can safely stop processing pending
12376 -- entities.
12377
12378 exit when Present (Interface_Alias (Subp));
12379
12380 -- Handle hidden entities
12381
12382 if not Is_Predefined_Dispatching_Operation (Subp)
12383 and then Is_Hidden (Subp)
12384 then
12385 if Present (New_Subp)
12386 and then Primitive_Names_Match (Subp, New_Subp)
12387 then
12388 Next_Elmt (Elmt);
12389 end if;
12390
12391 else
12392 if not Present (New_Subp)
12393 or else Ekind (Subp) /= Ekind (New_Subp)
12394 or else not Primitive_Names_Match (Subp, New_Subp)
12395 then
12396 return False;
12397 end if;
12398
12399 Next_Elmt (Elmt);
12400 end if;
12401
12402 Next_Elmt (Op_Elmt);
12403 end loop;
12404
12405 return True;
12406 end Check_Derived_Type;
12407
12408 -- Local variables
12409
12410 Alias_Subp : Entity_Id;
88b32fc3 12411 Act_List : Elist_Id;
ce2b6ba5
JM
12412 Act_Elmt : Elmt_Id := No_Elmt;
12413 Act_Subp : Entity_Id := Empty;
88b32fc3 12414 Elmt : Elmt_Id;
ce2b6ba5 12415 Need_Search : Boolean := False;
88b32fc3
BD
12416 New_Subp : Entity_Id := Empty;
12417 Parent_Base : Entity_Id;
12418 Subp : Entity_Id;
996ae0b0 12419
ce2b6ba5
JM
12420 -- Start of processing for Derive_Subprograms
12421
996ae0b0
RK
12422 begin
12423 if Ekind (Parent_Type) = E_Record_Type_With_Private
12424 and then Has_Discriminants (Parent_Type)
12425 and then Present (Full_View (Parent_Type))
12426 then
12427 Parent_Base := Full_View (Parent_Type);
12428 else
12429 Parent_Base := Parent_Type;
12430 end if;
12431
996ae0b0
RK
12432 if Present (Generic_Actual) then
12433 Act_List := Collect_Primitive_Operations (Generic_Actual);
12434 Act_Elmt := First_Elmt (Act_List);
996ae0b0
RK
12435 end if;
12436
ce2b6ba5
JM
12437 -- Derive primitives inherited from the parent. Note that if the generic
12438 -- actual is present, this is not really a type derivation, it is a
12439 -- completion within an instance.
996ae0b0 12440
ce2b6ba5
JM
12441 -- Case 1: Derived_Type does not implement interfaces
12442
12443 if not Is_Tagged_Type (Derived_Type)
12444 or else (not Has_Interfaces (Derived_Type)
12445 and then not (Present (Generic_Actual)
12446 and then
12447 Has_Interfaces (Generic_Actual)))
12448 then
12449 Elmt := First_Elmt (Op_List);
12450 while Present (Elmt) loop
12451 Subp := Node (Elmt);
996ae0b0 12452
ce2b6ba5
JM
12453 -- Literals are derived earlier in the process of building the
12454 -- derived type, and are skipped here.
950d3e7d 12455
ce2b6ba5 12456 if Ekind (Subp) = E_Enumeration_Literal then
9dfd2ff8 12457 null;
758c442c 12458
ce2b6ba5
JM
12459 -- The actual is a direct descendant and the common primitive
12460 -- operations appear in the same order.
7d7af38a 12461
ce2b6ba5
JM
12462 -- If the generic parent type is present, the derived type is an
12463 -- instance of a formal derived type, and within the instance its
12464 -- operations are those of the actual. We derive from the formal
12465 -- type but make the inherited operations aliases of the
12466 -- corresponding operations of the actual.
7d7af38a 12467
ce2b6ba5
JM
12468 else
12469 Derive_Subprogram
12470 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
88b32fc3 12471
ce2b6ba5
JM
12472 if Present (Act_Elmt) then
12473 Next_Elmt (Act_Elmt);
12474 end if;
12475 end if;
88b32fc3 12476
ce2b6ba5
JM
12477 Next_Elmt (Elmt);
12478 end loop;
12479
12480 -- Case 2: Derived_Type implements interfaces
12481
12482 else
12483 -- If the parent type has no predefined primitives we remove
12484 -- predefined primitives from the list of primitives of generic
12485 -- actual to simplify the complexity of this algorithm.
12486
12487 if Present (Generic_Actual) then
12488 declare
12489 Has_Predefined_Primitives : Boolean := False;
12490
12491 begin
12492 -- Check if the parent type has predefined primitives
12493
12494 Elmt := First_Elmt (Op_List);
12495 while Present (Elmt) loop
12496 Subp := Node (Elmt);
12497
12498 if Is_Predefined_Dispatching_Operation (Subp)
12499 and then not Comes_From_Source (Ultimate_Alias (Subp))
12500 then
12501 Has_Predefined_Primitives := True;
12502 exit;
12503 end if;
12504
12505 Next_Elmt (Elmt);
12506 end loop;
12507
12508 -- Remove predefined primitives of Generic_Actual. We must use
12509 -- an auxiliary list because in case of tagged types the value
12510 -- returned by Collect_Primitive_Operations is the value stored
12511 -- in its Primitive_Operations attribute (and we don't want to
12512 -- modify its current contents).
12513
12514 if not Has_Predefined_Primitives then
12515 declare
12516 Aux_List : constant Elist_Id := New_Elmt_List;
12517
12518 begin
12519 Elmt := First_Elmt (Act_List);
12520 while Present (Elmt) loop
12521 Subp := Node (Elmt);
12522
12523 if not Is_Predefined_Dispatching_Operation (Subp)
12524 or else Comes_From_Source (Subp)
12525 then
12526 Append_Elmt (Subp, Aux_List);
12527 end if;
12528
12529 Next_Elmt (Elmt);
12530 end loop;
12531
12532 Act_List := Aux_List;
12533 end;
88b32fc3 12534 end if;
996ae0b0 12535
ce2b6ba5
JM
12536 Act_Elmt := First_Elmt (Act_List);
12537 Act_Subp := Node (Act_Elmt);
12538 end;
12539 end if;
12540
12541 -- Stage 1: If the generic actual is not present we derive the
12542 -- primitives inherited from the parent type. If the generic parent
12543 -- type is present, the derived type is an instance of a formal
12544 -- derived type, and within the instance its operations are those of
12545 -- the actual. We derive from the formal type but make the inherited
12546 -- operations aliases of the corresponding operations of the actual.
12547
12548 Elmt := First_Elmt (Op_List);
12549 while Present (Elmt) loop
12550 Subp := Node (Elmt);
12551 Alias_Subp := Ultimate_Alias (Subp);
12552
12553 -- At this early stage Derived_Type has no entities with attribute
12554 -- Interface_Alias. In addition, such primitives are always
12555 -- located at the end of the list of primitives of Parent_Type.
12556 -- Therefore, if found we can safely stop processing pending
12557 -- entities.
12558
12559 exit when Present (Interface_Alias (Subp));
12560
12561 -- If the generic actual is present find the corresponding
12562 -- operation in the generic actual. If the parent type is a
12563 -- direct ancestor of the derived type then, even if it is an
12564 -- interface, the operations are inherited from the primary
12565 -- dispatch table and are in the proper order. If we detect here
12566 -- that primitives are not in the same order we traverse the list
12567 -- of primitive operations of the actual to find the one that
12568 -- implements the interface primitive.
12569
12570 if Need_Search
12571 or else
12572 (Present (Generic_Actual)
12573 and then Present (Act_Subp)
12574 and then not Primitive_Names_Match (Subp, Act_Subp))
12575 then
12576 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual));
12577 pragma Assert (Is_Interface (Parent_Base));
12578
12579 -- Remember that we need searching for all the pending
12580 -- primitives
12581
12582 Need_Search := True;
12583
12584 -- Handle entities associated with interface primitives
12585
12586 if Present (Alias (Subp))
12587 and then Is_Interface (Find_Dispatching_Type (Alias (Subp)))
12588 and then not Is_Predefined_Dispatching_Operation (Subp)
71f62180 12589 then
ce2b6ba5
JM
12590 Act_Subp :=
12591 Find_Primitive_Covering_Interface
12592 (Tagged_Type => Generic_Actual,
12593 Iface_Prim => Subp);
2b73cf68 12594
ce2b6ba5
JM
12595 -- Handle predefined primitives plus the rest of user-defined
12596 -- primitives
12597
12598 else
71f62180 12599 Act_Elmt := First_Elmt (Act_List);
2b73cf68 12600 while Present (Act_Elmt) loop
ce2b6ba5
JM
12601 Act_Subp := Node (Act_Elmt);
12602
12603 exit when Primitive_Names_Match (Subp, Act_Subp)
12604 and then Type_Conformant (Subp, Act_Subp,
12605 Skip_Controlling_Formals => True)
12606 and then No (Interface_Alias (Act_Subp));
12607
2b73cf68
JM
12608 Next_Elmt (Act_Elmt);
12609 end loop;
12610 end if;
ce2b6ba5 12611 end if;
2b73cf68 12612
ce2b6ba5
JM
12613 -- Case 1: If the parent is a limited interface then it has the
12614 -- predefined primitives of synchronized interfaces. However, the
12615 -- actual type may be a non-limited type and hence it does not
12616 -- have such primitives.
2b73cf68 12617
ce2b6ba5
JM
12618 if Present (Generic_Actual)
12619 and then not Present (Act_Subp)
12620 and then Is_Limited_Interface (Parent_Base)
12621 and then Is_Predefined_Interface_Primitive (Subp)
12622 then
12623 null;
2b73cf68 12624
ce2b6ba5
JM
12625 -- Case 2: Inherit entities associated with interfaces that
12626 -- were not covered by the parent type. We exclude here null
12627 -- interface primitives because they do not need special
12628 -- management.
12629
12630 elsif Present (Alias (Subp))
12631 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
12632 and then not
12633 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
12634 and then Null_Present (Parent (Alias_Subp)))
12635 then
12636 Derive_Subprogram
12637 (New_Subp => New_Subp,
12638 Parent_Subp => Alias_Subp,
12639 Derived_Type => Derived_Type,
12640 Parent_Type => Find_Dispatching_Type (Alias_Subp),
12641 Actual_Subp => Act_Subp);
12642
12643 if No (Generic_Actual) then
12644 Set_Alias (New_Subp, Subp);
2b73cf68 12645 end if;
996ae0b0 12646
ce2b6ba5 12647 -- Case 3: Common derivation
88b32fc3 12648
ce2b6ba5
JM
12649 else
12650 Derive_Subprogram
12651 (New_Subp => New_Subp,
12652 Parent_Subp => Subp,
12653 Derived_Type => Derived_Type,
12654 Parent_Type => Parent_Base,
12655 Actual_Subp => Act_Subp);
12656 end if;
2b73cf68 12657
ce2b6ba5
JM
12658 -- No need to update Act_Elm if we must search for the
12659 -- corresponding operation in the generic actual
7d7af38a 12660
ce2b6ba5
JM
12661 if not Need_Search
12662 and then Present (Act_Elmt)
12663 then
12664 Next_Elmt (Act_Elmt);
12665 Act_Subp := Node (Act_Elmt);
12666 end if;
7d7af38a 12667
7d7af38a
JM
12668 Next_Elmt (Elmt);
12669 end loop;
ce2b6ba5
JM
12670
12671 -- Inherit additional operations from progenitors. If the derived
12672 -- type is a generic actual, there are not new primitive operations
12673 -- for the type because it has those of the actual, and therefore
12674 -- nothing needs to be done. The renamings generated above are not
12675 -- primitive operations, and their purpose is simply to make the
12676 -- proper operations visible within an instantiation.
12677
12678 if No (Generic_Actual) then
12679 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
12680 end if;
7d7af38a 12681 end if;
ce2b6ba5
JM
12682
12683 -- Final check: Direct descendants must have their primitives in the
12684 -- same order. We exclude from this test non-tagged types and instances
12685 -- of formal derived types. We skip this test if we have already
12686 -- reported serious errors in the sources.
12687
12688 pragma Assert (not Is_Tagged_Type (Derived_Type)
12689 or else Present (Generic_Actual)
12690 or else Serious_Errors_Detected > 0
12691 or else Check_Derived_Type);
996ae0b0
RK
12692 end Derive_Subprograms;
12693
12694 --------------------------------
12695 -- Derived_Standard_Character --
12696 --------------------------------
12697
12698 procedure Derived_Standard_Character
71f62180
ES
12699 (N : Node_Id;
12700 Parent_Type : Entity_Id;
12701 Derived_Type : Entity_Id)
996ae0b0
RK
12702 is
12703 Loc : constant Source_Ptr := Sloc (N);
12704 Def : constant Node_Id := Type_Definition (N);
12705 Indic : constant Node_Id := Subtype_Indication (Def);
12706 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
12707 Implicit_Base : constant Entity_Id :=
12708 Create_Itype
12709 (E_Enumeration_Type, N, Derived_Type, 'B');
12710
12711 Lo : Node_Id;
12712 Hi : Node_Id;
996ae0b0
RK
12713
12714 begin
fbf5a39b 12715 Discard_Node (Process_Subtype (Indic, N));
996ae0b0
RK
12716
12717 Set_Etype (Implicit_Base, Parent_Base);
12718 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
12719 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
12720
12721 Set_Is_Character_Type (Implicit_Base, True);
12722 Set_Has_Delayed_Freeze (Implicit_Base);
12723
fbf5a39b
AC
12724 -- The bounds of the implicit base are the bounds of the parent base.
12725 -- Note that their type is the parent base.
12726
12727 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
12728 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
996ae0b0
RK
12729
12730 Set_Scalar_Range (Implicit_Base,
12731 Make_Range (Loc,
12732 Low_Bound => Lo,
12733 High_Bound => Hi));
12734
12735 Conditional_Delay (Derived_Type, Parent_Type);
12736
12737 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
12738 Set_Etype (Derived_Type, Implicit_Base);
12739 Set_Size_Info (Derived_Type, Parent_Type);
12740
12741 if Unknown_RM_Size (Derived_Type) then
12742 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
12743 end if;
12744
12745 Set_Is_Character_Type (Derived_Type, True);
12746
12747 if Nkind (Indic) /= N_Subtype_Indication then
fbf5a39b
AC
12748
12749 -- If no explicit constraint, the bounds are those
12750 -- of the parent type.
12751
12752 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
12753 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
12754 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
996ae0b0
RK
12755 end if;
12756
12757 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
12758
7d7af38a
JM
12759 -- Because the implicit base is used in the conversion of the bounds, we
12760 -- have to freeze it now. This is similar to what is done for numeric
12761 -- types, and it equally suspicious, but otherwise a non-static bound
12762 -- will have a reference to an unfrozen type, which is rejected by Gigi
12763 -- (???). This requires specific care for definition of stream
12764 -- attributes. For details, see comments at the end of
88b32fc3 12765 -- Build_Derived_Numeric_Type.
996ae0b0
RK
12766
12767 Freeze_Before (N, Implicit_Base);
996ae0b0
RK
12768 end Derived_Standard_Character;
12769
12770 ------------------------------
12771 -- Derived_Type_Declaration --
12772 ------------------------------
12773
12774 procedure Derived_Type_Declaration
12775 (T : Entity_Id;
12776 N : Node_Id;
12777 Is_Completion : Boolean)
12778 is
996ae0b0 12779 Parent_Type : Entity_Id;
996ae0b0 12780
c6823a20
EB
12781 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
12782 -- Check whether the parent type is a generic formal, or derives
12783 -- directly or indirectly from one.
12784
12785 ------------------------
12786 -- Comes_From_Generic --
12787 ------------------------
12788
12789 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
12790 begin
12791 if Is_Generic_Type (Typ) then
12792 return True;
12793
12794 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
12795 return True;
12796
12797 elsif Is_Private_Type (Typ)
12798 and then Present (Full_View (Typ))
12799 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
12800 then
12801 return True;
12802
12803 elsif Is_Generic_Actual_Type (Typ) then
12804 return True;
12805
12806 else
12807 return False;
12808 end if;
12809 end Comes_From_Generic;
12810
2b73cf68
JM
12811 -- Local variables
12812
12813 Def : constant Node_Id := Type_Definition (N);
12814 Iface_Def : Node_Id;
12815 Indic : constant Node_Id := Subtype_Indication (Def);
12816 Extension : constant Node_Id := Record_Extension_Part (Def);
12817 Parent_Node : Node_Id;
12818 Parent_Scope : Entity_Id;
12819 Taggd : Boolean;
12820
fa7c4d23
AC
12821 -- Start of processing for Derived_Type_Declaration
12822
996ae0b0
RK
12823 begin
12824 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
12825
758c442c
GD
12826 -- Ada 2005 (AI-251): In case of interface derivation check that the
12827 -- parent is also an interface.
12828
12829 if Interface_Present (Def) then
12830 if not Is_Interface (Parent_Type) then
6765b310 12831 Diagnose_Interface (Indic, Parent_Type);
758c442c
GD
12832
12833 else
2b73cf68
JM
12834 Parent_Node := Parent (Base_Type (Parent_Type));
12835 Iface_Def := Type_Definition (Parent_Node);
758c442c
GD
12836
12837 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
12838 -- other limited interfaces.
12839
12840 if Limited_Present (Def) then
12841 if Limited_Present (Iface_Def) then
12842 null;
12843
12844 elsif Protected_Present (Iface_Def) then
e358346d
AC
12845 Error_Msg_NE
12846 ("descendant of& must be declared"
12847 & " as a protected interface",
12848 N, Parent_Type);
758c442c
GD
12849
12850 elsif Synchronized_Present (Iface_Def) then
e358346d
AC
12851 Error_Msg_NE
12852 ("descendant of& must be declared"
12853 & " as a synchronized interface",
12854 N, Parent_Type);
758c442c
GD
12855
12856 elsif Task_Present (Iface_Def) then
e358346d
AC
12857 Error_Msg_NE
12858 ("descendant of& must be declared as a task interface",
12859 N, Parent_Type);
758c442c
GD
12860
12861 else
dc06abec
RD
12862 Error_Msg_N
12863 ("(Ada 2005) limited interface cannot "
12864 & "inherit from non-limited interface", Indic);
758c442c
GD
12865 end if;
12866
12867 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
12868 -- from non-limited or limited interfaces.
12869
12870 elsif not Protected_Present (Def)
12871 and then not Synchronized_Present (Def)
12872 and then not Task_Present (Def)
12873 then
12874 if Limited_Present (Iface_Def) then
12875 null;
12876
12877 elsif Protected_Present (Iface_Def) then
e358346d
AC
12878 Error_Msg_NE
12879 ("descendant of& must be declared"
12880 & " as a protected interface",
12881 N, Parent_Type);
758c442c
GD
12882
12883 elsif Synchronized_Present (Iface_Def) then
e358346d
AC
12884 Error_Msg_NE
12885 ("descendant of& must be declared"
12886 & " as a synchronized interface",
12887 N, Parent_Type);
758c442c
GD
12888
12889 elsif Task_Present (Iface_Def) then
e358346d
AC
12890 Error_Msg_NE
12891 ("descendant of& must be declared as a task interface",
12892 N, Parent_Type);
758c442c
GD
12893 else
12894 null;
12895 end if;
12896 end if;
12897 end if;
12898 end if;
12899
fea9e956
ES
12900 if Is_Tagged_Type (Parent_Type)
12901 and then Is_Concurrent_Type (Parent_Type)
12902 and then not Is_Interface (Parent_Type)
fea9e956 12903 then
dc06abec
RD
12904 Error_Msg_N
12905 ("parent type of a record extension cannot be "
12906 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
12907 Set_Etype (T, Any_Type);
fea9e956
ES
12908 return;
12909 end if;
12910
758c442c
GD
12911 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
12912 -- interfaces
12913
12914 if Is_Tagged_Type (Parent_Type)
12915 and then Is_Non_Empty_List (Interface_List (Def))
12916 then
12917 declare
9dfd2ff8
CC
12918 Intf : Node_Id;
12919 T : Entity_Id;
12920
758c442c 12921 begin
9dfd2ff8
CC
12922 Intf := First (Interface_List (Def));
12923 while Present (Intf) loop
12924 T := Find_Type_Of_Subtype_Indic (Intf);
758c442c
GD
12925
12926 if not Is_Interface (T) then
6765b310 12927 Diagnose_Interface (Intf, T);
653da906 12928
2b73cf68
JM
12929 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
12930 -- a limited type from having a nonlimited progenitor.
12931
12932 elsif (Limited_Present (Def)
12933 or else (not Is_Interface (Parent_Type)
12934 and then Is_Limited_Type (Parent_Type)))
653da906
RD
12935 and then not Is_Limited_Interface (T)
12936 then
12937 Error_Msg_NE
12938 ("progenitor interface& of limited type must be limited",
12939 N, T);
758c442c
GD
12940 end if;
12941
9dfd2ff8 12942 Next (Intf);
758c442c
GD
12943 end loop;
12944 end;
12945 end if;
12946
996ae0b0
RK
12947 if Parent_Type = Any_Type
12948 or else Etype (Parent_Type) = Any_Type
12949 or else (Is_Class_Wide_Type (Parent_Type)
fa7c4d23 12950 and then Etype (Parent_Type) = T)
996ae0b0 12951 then
a5b62485
AC
12952 -- If Parent_Type is undefined or illegal, make new type into a
12953 -- subtype of Any_Type, and set a few attributes to prevent cascaded
12954 -- errors. If this is a self-definition, emit error now.
996ae0b0
RK
12955
12956 if T = Parent_Type
12957 or else T = Etype (Parent_Type)
12958 then
12959 Error_Msg_N ("type cannot be used in its own definition", Indic);
12960 end if;
12961
12962 Set_Ekind (T, Ekind (Parent_Type));
12963 Set_Etype (T, Any_Type);
12964 Set_Scalar_Range (T, Scalar_Range (Any_Type));
12965
12966 if Is_Tagged_Type (T) then
12967 Set_Primitive_Operations (T, New_Elmt_List);
12968 end if;
07fc65c4 12969
996ae0b0 12970 return;
996ae0b0
RK
12971 end if;
12972
653da906
RD
12973 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
12974 -- an interface is special because the list of interfaces in the full
12975 -- view can be given in any order. For example:
12976
12977 -- type A is interface;
12978 -- type B is interface and A;
12979 -- type D is new B with private;
12980 -- private
12981 -- type D is new A and B with null record; -- 1 --
12982
12983 -- In this case we perform the following transformation of -1-:
12984
12985 -- type D is new B and A with null record;
12986
12987 -- If the parent of the full-view covers the parent of the partial-view
12988 -- we have two possible cases:
12989
12990 -- 1) They have the same parent
12991 -- 2) The parent of the full-view implements some further interfaces
12992
12993 -- In both cases we do not need to perform the transformation. In the
12994 -- first case the source program is correct and the transformation is
12995 -- not needed; in the second case the source program does not fulfill
12996 -- the no-hidden interfaces rule (AI-396) and the error will be reported
12997 -- later.
12998
12999 -- This transformation not only simplifies the rest of the analysis of
13000 -- this type declaration but also simplifies the correct generation of
13001 -- the object layout to the expander.
13002
13003 if In_Private_Part (Current_Scope)
13004 and then Is_Interface (Parent_Type)
13005 then
13006 declare
13007 Iface : Node_Id;
13008 Partial_View : Entity_Id;
13009 Partial_View_Parent : Entity_Id;
13010 New_Iface : Node_Id;
13011
13012 begin
13013 -- Look for the associated private type declaration
13014
13015 Partial_View := First_Entity (Current_Scope);
13016 loop
57193e09 13017 exit when No (Partial_View)
653da906
RD
13018 or else (Has_Private_Declaration (Partial_View)
13019 and then Full_View (Partial_View) = T);
13020
13021 Next_Entity (Partial_View);
13022 end loop;
13023
13024 -- If the partial view was not found then the source code has
13025 -- errors and the transformation is not needed.
13026
13027 if Present (Partial_View) then
13028 Partial_View_Parent := Etype (Partial_View);
13029
13030 -- If the parent of the full-view covers the parent of the
13031 -- partial-view we have nothing else to do.
13032
13033 if Interface_Present_In_Ancestor
13034 (Parent_Type, Partial_View_Parent)
13035 then
13036 null;
13037
13038 -- Traverse the list of interfaces of the full-view to look
13039 -- for the parent of the partial-view and perform the tree
13040 -- transformation.
13041
13042 else
13043 Iface := First (Interface_List (Def));
13044 while Present (Iface) loop
13045 if Etype (Iface) = Etype (Partial_View) then
13046 Rewrite (Subtype_Indication (Def),
13047 New_Copy (Subtype_Indication
13048 (Parent (Partial_View))));
13049
13050 New_Iface := Make_Identifier (Sloc (N),
13051 Chars (Parent_Type));
13052 Append (New_Iface, Interface_List (Def));
13053
13054 -- Analyze the transformed code
13055
13056 Derived_Type_Declaration (T, N, Is_Completion);
13057 return;
13058 end if;
13059
13060 Next (Iface);
13061 end loop;
13062 end if;
13063 end if;
13064 end;
13065 end if;
13066
996ae0b0
RK
13067 -- Only composite types other than array types are allowed to have
13068 -- discriminants.
13069
13070 if Present (Discriminant_Specifications (N))
13071 and then (Is_Elementary_Type (Parent_Type)
13072 or else Is_Array_Type (Parent_Type))
13073 and then not Error_Posted (N)
13074 then
13075 Error_Msg_N
13076 ("elementary or array type cannot have discriminants",
13077 Defining_Identifier (First (Discriminant_Specifications (N))));
13078 Set_Has_Discriminants (T, False);
13079 end if;
13080
13081 -- In Ada 83, a derived type defined in a package specification cannot
13082 -- be used for further derivation until the end of its visible part.
13083 -- Note that derivation in the private part of the package is allowed.
13084
0ab80019 13085 if Ada_Version = Ada_83
996ae0b0
RK
13086 and then Is_Derived_Type (Parent_Type)
13087 and then In_Visible_Part (Scope (Parent_Type))
13088 then
0ab80019 13089 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
996ae0b0
RK
13090 Error_Msg_N
13091 ("(Ada 83): premature use of type for derivation", Indic);
13092 end if;
13093 end if;
13094
13095 -- Check for early use of incomplete or private type
13096
13097 if Ekind (Parent_Type) = E_Void
13098 or else Ekind (Parent_Type) = E_Incomplete_Type
13099 then
13100 Error_Msg_N ("premature derivation of incomplete type", Indic);
13101 return;
13102
13103 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
c6823a20 13104 and then not Comes_From_Generic (Parent_Type))
996ae0b0
RK
13105 or else Has_Private_Component (Parent_Type)
13106 then
13107 -- The ancestor type of a formal type can be incomplete, in which
13108 -- case only the operations of the partial view are available in
13109 -- the generic. Subsequent checks may be required when the full
13110 -- view is analyzed, to verify that derivation from a tagged type
13111 -- has an extension.
13112
13113 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
13114 null;
13115
13116 elsif No (Underlying_Type (Parent_Type))
13117 or else Has_Private_Component (Parent_Type)
13118 then
13119 Error_Msg_N
13120 ("premature derivation of derived or private type", Indic);
13121
13122 -- Flag the type itself as being in error, this prevents some
c6823a20 13123 -- nasty problems with subsequent uses of the malformed type.
996ae0b0
RK
13124
13125 Set_Error_Posted (T);
13126
13127 -- Check that within the immediate scope of an untagged partial
13128 -- view it's illegal to derive from the partial view if the
13129 -- full view is tagged. (7.3(7))
13130
13131 -- We verify that the Parent_Type is a partial view by checking
13132 -- that it is not a Full_Type_Declaration (i.e. a private type or
13133 -- private extension declaration), to distinguish a partial view
13134 -- from a derivation from a private type which also appears as
13135 -- E_Private_Type.
13136
13137 elsif Present (Full_View (Parent_Type))
13138 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
13139 and then not Is_Tagged_Type (Parent_Type)
13140 and then Is_Tagged_Type (Full_View (Parent_Type))
13141 then
13142 Parent_Scope := Scope (T);
13143 while Present (Parent_Scope)
13144 and then Parent_Scope /= Standard_Standard
13145 loop
13146 if Parent_Scope = Scope (Parent_Type) then
13147 Error_Msg_N
13148 ("premature derivation from type with tagged full view",
13149 Indic);
13150 end if;
13151
13152 Parent_Scope := Scope (Parent_Scope);
13153 end loop;
13154 end if;
13155 end if;
13156
13157 -- Check that form of derivation is appropriate
13158
13159 Taggd := Is_Tagged_Type (Parent_Type);
13160
13161 -- Perhaps the parent type should be changed to the class-wide type's
13162 -- specific type in this case to prevent cascading errors ???
13163
13164 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
13165 Error_Msg_N ("parent type must not be a class-wide type", Indic);
13166 return;
13167 end if;
13168
13169 if Present (Extension) and then not Taggd then
13170 Error_Msg_N
13171 ("type derived from untagged type cannot have extension", Indic);
13172
13173 elsif No (Extension) and then Taggd then
71d9e9f2 13174
a5b62485
AC
13175 -- If this declaration is within a private part (or body) of a
13176 -- generic instantiation then the derivation is allowed (the parent
13177 -- type can only appear tagged in this case if it's a generic actual
13178 -- type, since it would otherwise have been rejected in the analysis
13179 -- of the generic template).
996ae0b0
RK
13180
13181 if not Is_Generic_Actual_Type (Parent_Type)
13182 or else In_Visible_Part (Scope (Parent_Type))
13183 then
13184 Error_Msg_N
13185 ("type derived from tagged type must have extension", Indic);
13186 end if;
13187 end if;
13188
88b32fc3
BD
13189 -- AI-443: Synchronized formal derived types require a private
13190 -- extension. There is no point in checking the ancestor type or
13191 -- the progenitors since the construct is wrong to begin with.
13192
13193 if Ada_Version >= Ada_05
13194 and then Is_Generic_Type (T)
13195 and then Present (Original_Node (N))
13196 then
13197 declare
13198 Decl : constant Node_Id := Original_Node (N);
13199
13200 begin
13201 if Nkind (Decl) = N_Formal_Type_Declaration
13202 and then Nkind (Formal_Type_Definition (Decl)) =
13203 N_Formal_Derived_Type_Definition
13204 and then Synchronized_Present (Formal_Type_Definition (Decl))
13205 and then No (Extension)
13206
13207 -- Avoid emitting a duplicate error message
13208
13209 and then not Error_Posted (Indic)
13210 then
13211 Error_Msg_N
13212 ("synchronized derived type must have extension", N);
13213 end if;
13214 end;
13215 end if;
13216
fa961f76
ES
13217 if Null_Exclusion_Present (Def)
13218 and then not Is_Access_Type (Parent_Type)
13219 then
13220 Error_Msg_N ("null exclusion can only apply to an access type", N);
13221 end if;
13222
c206e8fd 13223 -- Avoid deriving parent primitives of underlying record views
9013065b
AC
13224
13225 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
13226 Derive_Subps => not Is_Underlying_Record_View (T));
653da906 13227
88b32fc3 13228 -- AI-419: The parent type of an explicitly limited derived type must
57193e09 13229 -- be a limited type or a limited interface.
653da906
RD
13230
13231 if Limited_Present (Def) then
13232 Set_Is_Limited_Record (T);
13233
030d25f4
JM
13234 if Is_Interface (T) then
13235 Set_Is_Limited_Interface (T);
13236 end if;
13237
653da906 13238 if not Is_Limited_Type (Parent_Type)
57193e09
TQ
13239 and then
13240 (not Is_Interface (Parent_Type)
13241 or else not Is_Limited_Interface (Parent_Type))
653da906
RD
13242 then
13243 Error_Msg_NE ("parent type& of limited type must be limited",
13244 N, Parent_Type);
13245 end if;
13246 end if;
996ae0b0
RK
13247 end Derived_Type_Declaration;
13248
6765b310
ES
13249 ------------------------
13250 -- Diagnose_Interface --
13251 ------------------------
13252
13253 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
13254 begin
13255 if not Is_Interface (E)
13256 and then E /= Any_Type
13257 then
13258 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
13259 end if;
13260 end Diagnose_Interface;
13261
996ae0b0
RK
13262 ----------------------------------
13263 -- Enumeration_Type_Declaration --
13264 ----------------------------------
13265
13266 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13267 Ev : Uint;
13268 L : Node_Id;
13269 R_Node : Node_Id;
13270 B_Node : Node_Id;
13271
13272 begin
13273 -- Create identifier node representing lower bound
13274
13275 B_Node := New_Node (N_Identifier, Sloc (Def));
13276 L := First (Literals (Def));
13277 Set_Chars (B_Node, Chars (L));
13278 Set_Entity (B_Node, L);
13279 Set_Etype (B_Node, T);
13280 Set_Is_Static_Expression (B_Node, True);
13281
13282 R_Node := New_Node (N_Range, Sloc (Def));
13283 Set_Low_Bound (R_Node, B_Node);
13284
13285 Set_Ekind (T, E_Enumeration_Type);
13286 Set_First_Literal (T, L);
13287 Set_Etype (T, T);
13288 Set_Is_Constrained (T);
13289
13290 Ev := Uint_0;
13291
13292 -- Loop through literals of enumeration type setting pos and rep values
00838d9a
AC
13293 -- except that if the Ekind is already set, then it means the literal
13294 -- was already constructed (case of a derived type declaration and we
13295 -- should not disturb the Pos and Rep values.
996ae0b0
RK
13296
13297 while Present (L) loop
13298 if Ekind (L) /= E_Enumeration_Literal then
13299 Set_Ekind (L, E_Enumeration_Literal);
13300 Set_Enumeration_Pos (L, Ev);
13301 Set_Enumeration_Rep (L, Ev);
13302 Set_Is_Known_Valid (L, True);
13303 end if;
13304
13305 Set_Etype (L, T);
13306 New_Overloaded_Entity (L);
13307 Generate_Definition (L);
13308 Set_Convention (L, Convention_Intrinsic);
13309
13310 if Nkind (L) = N_Defining_Character_Literal then
13311 Set_Is_Character_Type (T, True);
13312 end if;
13313
13314 Ev := Ev + 1;
13315 Next (L);
13316 end loop;
13317
13318 -- Now create a node representing upper bound
13319
13320 B_Node := New_Node (N_Identifier, Sloc (Def));
13321 Set_Chars (B_Node, Chars (Last (Literals (Def))));
13322 Set_Entity (B_Node, Last (Literals (Def)));
13323 Set_Etype (B_Node, T);
13324 Set_Is_Static_Expression (B_Node, True);
13325
13326 Set_High_Bound (R_Node, B_Node);
2b73cf68
JM
13327
13328 -- Initialize various fields of the type. Some of this information
13329 -- may be overwritten later through rep.clauses.
13330
13331 Set_Scalar_Range (T, R_Node);
13332 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
13333 Set_Enum_Esize (T);
13334 Set_Enum_Pos_To_Rep (T, Empty);
996ae0b0 13335
fbf5a39b 13336 -- Set Discard_Names if configuration pragma set, or if there is
996ae0b0
RK
13337 -- a parameterless pragma in the current declarative region
13338
13339 if Global_Discard_Names
13340 or else Discard_Names (Scope (T))
13341 then
13342 Set_Discard_Names (T);
13343 end if;
07fc65c4
GB
13344
13345 -- Process end label if there is one
13346
13347 if Present (Def) then
13348 Process_End_Label (Def, 'e', T);
13349 end if;
996ae0b0
RK
13350 end Enumeration_Type_Declaration;
13351
996ae0b0 13352 ---------------------------------
fbf5a39b 13353 -- Expand_To_Stored_Constraint --
996ae0b0
RK
13354 ---------------------------------
13355
fbf5a39b 13356 function Expand_To_Stored_Constraint
996ae0b0 13357 (Typ : Entity_Id;
b0f26df5 13358 Constraint : Elist_Id) return Elist_Id
996ae0b0
RK
13359 is
13360 Explicitly_Discriminated_Type : Entity_Id;
13361 Expansion : Elist_Id;
13362 Discriminant : Entity_Id;
13363
13364 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
ffe9aba8 13365 -- Find the nearest type that actually specifies discriminants
996ae0b0
RK
13366
13367 ---------------------------------
13368 -- Type_With_Explicit_Discrims --
13369 ---------------------------------
13370
13371 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
13372 Typ : constant E := Base_Type (Id);
13373
13374 begin
13375 if Ekind (Typ) in Incomplete_Or_Private_Kind then
13376 if Present (Full_View (Typ)) then
13377 return Type_With_Explicit_Discrims (Full_View (Typ));
13378 end if;
13379
13380 else
13381 if Has_Discriminants (Typ) then
13382 return Typ;
13383 end if;
13384 end if;
13385
13386 if Etype (Typ) = Typ then
13387 return Empty;
13388 elsif Has_Discriminants (Typ) then
13389 return Typ;
13390 else
13391 return Type_With_Explicit_Discrims (Etype (Typ));
13392 end if;
13393
13394 end Type_With_Explicit_Discrims;
13395
fbf5a39b 13396 -- Start of processing for Expand_To_Stored_Constraint
996ae0b0
RK
13397
13398 begin
13399 if No (Constraint)
13400 or else Is_Empty_Elmt_List (Constraint)
13401 then
13402 return No_Elist;
13403 end if;
13404
13405 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
13406
13407 if No (Explicitly_Discriminated_Type) then
13408 return No_Elist;
13409 end if;
13410
13411 Expansion := New_Elmt_List;
13412
13413 Discriminant :=
fbf5a39b 13414 First_Stored_Discriminant (Explicitly_Discriminated_Type);
996ae0b0 13415 while Present (Discriminant) loop
996ae0b0
RK
13416 Append_Elmt (
13417 Get_Discriminant_Value (
13418 Discriminant, Explicitly_Discriminated_Type, Constraint),
13419 Expansion);
fbf5a39b 13420 Next_Stored_Discriminant (Discriminant);
996ae0b0
RK
13421 end loop;
13422
13423 return Expansion;
fbf5a39b 13424 end Expand_To_Stored_Constraint;
996ae0b0 13425
dc06abec
RD
13426 ---------------------------
13427 -- Find_Hidden_Interface --
13428 ---------------------------
13429
13430 function Find_Hidden_Interface
13431 (Src : Elist_Id;
13432 Dest : Elist_Id) return Entity_Id
13433 is
13434 Iface : Entity_Id;
13435 Iface_Elmt : Elmt_Id;
13436
13437 begin
13438 if Present (Src) and then Present (Dest) then
13439 Iface_Elmt := First_Elmt (Src);
13440 while Present (Iface_Elmt) loop
13441 Iface := Node (Iface_Elmt);
13442
13443 if Is_Interface (Iface)
13444 and then not Contain_Interface (Iface, Dest)
13445 then
13446 return Iface;
13447 end if;
13448
13449 Next_Elmt (Iface_Elmt);
13450 end loop;
13451 end if;
13452
13453 return Empty;
13454 end Find_Hidden_Interface;
13455
996ae0b0
RK
13456 --------------------
13457 -- Find_Type_Name --
13458 --------------------
13459
13460 function Find_Type_Name (N : Node_Id) return Entity_Id is
13461 Id : constant Entity_Id := Defining_Identifier (N);
13462 Prev : Entity_Id;
13463 New_Id : Entity_Id;
13464 Prev_Par : Node_Id;
13465
33931112 13466 procedure Tag_Mismatch;
abed5dc6 13467 -- Diagnose a tagged partial view whose full view is untagged.
33931112
JM
13468 -- We post the message on the full view, with a reference to
13469 -- the previous partial view. The partial view can be private
13470 -- or incomplete, and these are handled in a different manner,
13471 -- so we determine the position of the error message from the
13472 -- respective slocs of both.
13473
13474 ------------------
13475 -- Tag_Mismatch --
13476 ------------------
13477
13478 procedure Tag_Mismatch is
13479 begin
13480 if Sloc (Prev) < Sloc (Id) then
13481 Error_Msg_NE
13482 ("full declaration of } must be a tagged type ", Id, Prev);
13483 else
13484 Error_Msg_NE
13485 ("full declaration of } must be a tagged type ", Prev, Id);
13486 end if;
13487 end Tag_Mismatch;
13488
d8221f45 13489 -- Start of processing for Find_Type_Name
33931112 13490
996ae0b0 13491 begin
71d9e9f2 13492 -- Find incomplete declaration, if one was given
996ae0b0
RK
13493
13494 Prev := Current_Entity_In_Scope (Id);
13495
13496 if Present (Prev) then
13497
13498 -- Previous declaration exists. Error if not incomplete/private case
13499 -- except if previous declaration is implicit, etc. Enter_Name will
13500 -- emit error if appropriate.
13501
13502 Prev_Par := Parent (Prev);
13503
13504 if not Is_Incomplete_Or_Private_Type (Prev) then
13505 Enter_Name (Id);
13506 New_Id := Id;
13507
7d7af38a
JM
13508 elsif not Nkind_In (N, N_Full_Type_Declaration,
13509 N_Task_Type_Declaration,
13510 N_Protected_Type_Declaration)
996ae0b0
RK
13511 then
13512 -- Completion must be a full type declarations (RM 7.3(4))
13513
13514 Error_Msg_Sloc := Sloc (Prev);
13515 Error_Msg_NE ("invalid completion of }", Id, Prev);
13516
13517 -- Set scope of Id to avoid cascaded errors. Entity is never
13518 -- examined again, except when saving globals in generics.
13519
13520 Set_Scope (Id, Current_Scope);
13521 New_Id := Id;
13522
d4429d51
ES
13523 -- If this is a repeated incomplete declaration, no further
13524 -- checks are possible.
13525
13526 if Nkind (N) = N_Incomplete_Type_Declaration then
13527 return Prev;
13528 end if;
13529
996ae0b0
RK
13530 -- Case of full declaration of incomplete type
13531
13532 elsif Ekind (Prev) = E_Incomplete_Type then
13533
a5b62485
AC
13534 -- Indicate that the incomplete declaration has a matching full
13535 -- declaration. The defining occurrence of the incomplete
996ae0b0
RK
13536 -- declaration remains the visible one, and the procedure
13537 -- Get_Full_View dereferences it whenever the type is used.
13538
13539 if Present (Full_View (Prev)) then
13540 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13541 end if;
13542
13543 Set_Full_View (Prev, Id);
13544 Append_Entity (Id, Current_Scope);
13545 Set_Is_Public (Id, Is_Public (Prev));
13546 Set_Is_Internal (Id);
13547 New_Id := Prev;
13548
13549 -- Case of full declaration of private type
13550
13551 else
13552 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
13553 if Etype (Prev) /= Prev then
13554
13555 -- Prev is a private subtype or a derived type, and needs
13556 -- no completion.
13557
13558 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13559 New_Id := Id;
13560
13561 elsif Ekind (Prev) = E_Private_Type
7d7af38a
JM
13562 and then Nkind_In (N, N_Task_Type_Declaration,
13563 N_Protected_Type_Declaration)
996ae0b0
RK
13564 then
13565 Error_Msg_N
13566 ("completion of nonlimited type cannot be limited", N);
57193e09
TQ
13567
13568 elsif Ekind (Prev) = E_Record_Type_With_Private
7d7af38a
JM
13569 and then Nkind_In (N, N_Task_Type_Declaration,
13570 N_Protected_Type_Declaration)
57193e09
TQ
13571 then
13572 if not Is_Limited_Record (Prev) then
13573 Error_Msg_N
13574 ("completion of nonlimited type cannot be limited", N);
13575
13576 elsif No (Interface_List (N)) then
13577 Error_Msg_N
13578 ("completion of tagged private type must be tagged",
ff2e7c1e 13579 N);
57193e09 13580 end if;
26a43556
AC
13581
13582 elsif Nkind (N) = N_Full_Type_Declaration
13583 and then
13584 Nkind (Type_Definition (N)) = N_Record_Definition
13585 and then Interface_Present (Type_Definition (N))
13586 then
13587 Error_Msg_N
ff2e7c1e 13588 ("completion of private type cannot be an interface", N);
996ae0b0
RK
13589 end if;
13590
dc06abec
RD
13591 -- Ada 2005 (AI-251): Private extension declaration of a task
13592 -- type or a protected type. This case arises when covering
13593 -- interface types.
758c442c 13594
7d7af38a
JM
13595 elsif Nkind_In (N, N_Task_Type_Declaration,
13596 N_Protected_Type_Declaration)
758c442c
GD
13597 then
13598 null;
13599
996ae0b0
RK
13600 elsif Nkind (N) /= N_Full_Type_Declaration
13601 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
13602 then
71d9e9f2
ES
13603 Error_Msg_N
13604 ("full view of private extension must be an extension", N);
996ae0b0
RK
13605
13606 elsif not (Abstract_Present (Parent (Prev)))
13607 and then Abstract_Present (Type_Definition (N))
13608 then
71d9e9f2
ES
13609 Error_Msg_N
13610 ("full view of non-abstract extension cannot be abstract", N);
996ae0b0
RK
13611 end if;
13612
13613 if not In_Private_Part (Current_Scope) then
13614 Error_Msg_N
71d9e9f2 13615 ("declaration of full view must appear in private part", N);
996ae0b0
RK
13616 end if;
13617
13618 Copy_And_Swap (Prev, Id);
996ae0b0
RK
13619 Set_Has_Private_Declaration (Prev);
13620 Set_Has_Private_Declaration (Id);
07fc65c4
GB
13621
13622 -- If no error, propagate freeze_node from private to full view.
13623 -- It may have been generated for an early operational item.
13624
13625 if Present (Freeze_Node (Id))
13626 and then Serious_Errors_Detected = 0
13627 and then No (Full_View (Id))
13628 then
13629 Set_Freeze_Node (Prev, Freeze_Node (Id));
13630 Set_Freeze_Node (Id, Empty);
13631 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
13632 end if;
13633
13634 Set_Full_View (Id, Prev);
996ae0b0
RK
13635 New_Id := Prev;
13636 end if;
13637
33931112 13638 -- Verify that full declaration conforms to partial one
996ae0b0
RK
13639
13640 if Is_Incomplete_Or_Private_Type (Prev)
13641 and then Present (Discriminant_Specifications (Prev_Par))
13642 then
13643 if Present (Discriminant_Specifications (N)) then
13644 if Ekind (Prev) = E_Incomplete_Type then
13645 Check_Discriminant_Conformance (N, Prev, Prev);
13646 else
13647 Check_Discriminant_Conformance (N, Prev, Id);
13648 end if;
13649
13650 else
13651 Error_Msg_N
13652 ("missing discriminants in full type declaration", N);
13653
13654 -- To avoid cascaded errors on subsequent use, share the
13655 -- discriminants of the partial view.
13656
13657 Set_Discriminant_Specifications (N,
13658 Discriminant_Specifications (Prev_Par));
13659 end if;
13660 end if;
13661
33931112 13662 -- A prior untagged partial view can have an associated class-wide
abed5dc6
AC
13663 -- type due to use of the class attribute, and in this case the full
13664 -- type must also be tagged. This Ada 95 usage is deprecated in favor
13665 -- of incomplete tagged declarations, but we check for it.
996ae0b0
RK
13666
13667 if Is_Type (Prev)
13668 and then (Is_Tagged_Type (Prev)
13669 or else Present (Class_Wide_Type (Prev)))
13670 then
af4133b2
ST
13671 -- The full declaration is either a tagged type (including
13672 -- a synchronized type that implements interfaces) or a
13673 -- type extension, otherwise this is an error.
13674
13675 if Nkind_In (N, N_Task_Type_Declaration,
82d2af1b 13676 N_Protected_Type_Declaration)
af4133b2
ST
13677 then
13678 if No (Interface_List (N))
13679 and then not Error_Posted (N)
13680 then
33931112 13681 Tag_Mismatch;
af4133b2
ST
13682 end if;
13683
13684 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
13685
13686 -- Indicate that the previous declaration (tagged incomplete
13687 -- or private declaration) requires the same on the full one.
996ae0b0 13688
996ae0b0 13689 if not Tagged_Present (Type_Definition (N)) then
33931112 13690 Tag_Mismatch;
996ae0b0
RK
13691 Set_Is_Tagged_Type (Id);
13692 Set_Primitive_Operations (Id, New_Elmt_List);
13693 end if;
13694
13695 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
13696 if No (Record_Extension_Part (Type_Definition (N))) then
13697 Error_Msg_NE (
13698 "full declaration of } must be a record extension",
13699 Prev, Id);
93bcda23 13700
03b64787 13701 -- Set some attributes to produce a usable full view
93bcda23 13702
996ae0b0
RK
13703 Set_Is_Tagged_Type (Id);
13704 Set_Primitive_Operations (Id, New_Elmt_List);
13705 end if;
13706
13707 else
33931112 13708 Tag_Mismatch;
996ae0b0
RK
13709 end if;
13710 end if;
13711
13712 return New_Id;
13713
13714 else
13715 -- New type declaration
13716
13717 Enter_Name (Id);
13718 return Id;
13719 end if;
13720 end Find_Type_Name;
13721
13722 -------------------------
13723 -- Find_Type_Of_Object --
13724 -------------------------
13725
13726 function Find_Type_Of_Object
13727 (Obj_Def : Node_Id;
b0f26df5 13728 Related_Nod : Node_Id) return Entity_Id
996ae0b0
RK
13729 is
13730 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
a397db96 13731 P : Node_Id := Parent (Obj_Def);
996ae0b0
RK
13732 T : Entity_Id;
13733 Nam : Name_Id;
13734
13735 begin
a397db96
AC
13736 -- If the parent is a component_definition node we climb to the
13737 -- component_declaration node
13738
13739 if Nkind (P) = N_Component_Definition then
13740 P := Parent (P);
13741 end if;
13742
996ae0b0
RK
13743 -- Case of an anonymous array subtype
13744
7d7af38a
JM
13745 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
13746 N_Unconstrained_Array_Definition)
996ae0b0
RK
13747 then
13748 T := Empty;
13749 Array_Type_Declaration (T, Obj_Def);
13750
ffe9aba8 13751 -- Create an explicit subtype whenever possible
996ae0b0
RK
13752
13753 elsif Nkind (P) /= N_Component_Declaration
13754 and then Def_Kind = N_Subtype_Indication
13755 then
13756 -- Base name of subtype on object name, which will be unique in
13757 -- the current scope.
13758
13759 -- If this is a duplicate declaration, return base type, to avoid
13760 -- generating duplicate anonymous types.
13761
13762 if Error_Posted (P) then
13763 Analyze (Subtype_Mark (Obj_Def));
13764 return Entity (Subtype_Mark (Obj_Def));
13765 end if;
13766
13767 Nam :=
13768 New_External_Name
13769 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
13770
13771 T := Make_Defining_Identifier (Sloc (P), Nam);
13772
13773 Insert_Action (Obj_Def,
13774 Make_Subtype_Declaration (Sloc (P),
13775 Defining_Identifier => T,
13776 Subtype_Indication => Relocate_Node (Obj_Def)));
13777
aa720a54 13778 -- This subtype may need freezing, and this will not be done
a5b62485
AC
13779 -- automatically if the object declaration is not in declarative
13780 -- part. Since this is an object declaration, the type cannot always
13781 -- be frozen here. Deferred constants do not freeze their type
13782 -- (which often enough will be private).
996ae0b0
RK
13783
13784 if Nkind (P) = N_Object_Declaration
13785 and then Constant_Present (P)
13786 and then No (Expression (P))
13787 then
13788 null;
996ae0b0
RK
13789 else
13790 Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
13791 end if;
13792
758c442c
GD
13793 -- Ada 2005 AI-406: the object definition in an object declaration
13794 -- can be an access definition.
13795
13796 elsif Def_Kind = N_Access_Definition then
13797 T := Access_Definition (Related_Nod, Obj_Def);
2b73cf68 13798 Set_Is_Local_Anonymous_Access (T);
88b32fc3
BD
13799
13800 -- Otherwise, the object definition is just a subtype_mark
758c442c 13801
996ae0b0
RK
13802 else
13803 T := Process_Subtype (Obj_Def, Related_Nod);
13804 end if;
13805
13806 return T;
13807 end Find_Type_Of_Object;
13808
13809 --------------------------------
13810 -- Find_Type_Of_Subtype_Indic --
13811 --------------------------------
13812
13813 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
13814 Typ : Entity_Id;
13815
13816 begin
13817 -- Case of subtype mark with a constraint
13818
13819 if Nkind (S) = N_Subtype_Indication then
13820 Find_Type (Subtype_Mark (S));
13821 Typ := Entity (Subtype_Mark (S));
13822
13823 if not
13824 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
13825 then
13826 Error_Msg_N
13827 ("incorrect constraint for this kind of type", Constraint (S));
13828 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
13829 end if;
13830
13831 -- Otherwise we have a subtype mark without a constraint
13832
dd5875a6
ES
13833 elsif Error_Posted (S) then
13834 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
13835 return Any_Type;
13836
996ae0b0
RK
13837 else
13838 Find_Type (S);
13839 Typ := Entity (S);
13840 end if;
13841
ce4a6e84
RD
13842 -- Check No_Wide_Characters restriction
13843
996ae0b0 13844 if Typ = Standard_Wide_Character
82c80734 13845 or else Typ = Standard_Wide_Wide_Character
996ae0b0 13846 or else Typ = Standard_Wide_String
82c80734 13847 or else Typ = Standard_Wide_Wide_String
996ae0b0
RK
13848 then
13849 Check_Restriction (No_Wide_Characters, S);
13850 end if;
13851
13852 return Typ;
13853 end Find_Type_Of_Subtype_Indic;
13854
13855 -------------------------------------
13856 -- Floating_Point_Type_Declaration --
13857 -------------------------------------
13858
13859 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13860 Digs : constant Node_Id := Digits_Expression (Def);
13861 Digs_Val : Uint;
13862 Base_Typ : Entity_Id;
13863 Implicit_Base : Entity_Id;
13864 Bound : Node_Id;
13865
13866 function Can_Derive_From (E : Entity_Id) return Boolean;
13867 -- Find if given digits value allows derivation from specified type
13868
fbf5a39b
AC
13869 ---------------------
13870 -- Can_Derive_From --
13871 ---------------------
13872
996ae0b0
RK
13873 function Can_Derive_From (E : Entity_Id) return Boolean is
13874 Spec : constant Entity_Id := Real_Range_Specification (Def);
13875
13876 begin
13877 if Digs_Val > Digits_Value (E) then
13878 return False;
13879 end if;
13880
13881 if Present (Spec) then
13882 if Expr_Value_R (Type_Low_Bound (E)) >
13883 Expr_Value_R (Low_Bound (Spec))
13884 then
13885 return False;
13886 end if;
13887
13888 if Expr_Value_R (Type_High_Bound (E)) <
13889 Expr_Value_R (High_Bound (Spec))
13890 then
13891 return False;
13892 end if;
13893 end if;
13894
13895 return True;
13896 end Can_Derive_From;
13897
13898 -- Start of processing for Floating_Point_Type_Declaration
13899
13900 begin
13901 Check_Restriction (No_Floating_Point, Def);
13902
13903 -- Create an implicit base type
13904
13905 Implicit_Base :=
13906 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
13907
13908 -- Analyze and verify digits value
13909
13910 Analyze_And_Resolve (Digs, Any_Integer);
13911 Check_Digits_Expression (Digs);
13912 Digs_Val := Expr_Value (Digs);
13913
13914 -- Process possible range spec and find correct type to derive from
13915
13916 Process_Real_Range_Specification (Def);
13917
13918 if Can_Derive_From (Standard_Short_Float) then
13919 Base_Typ := Standard_Short_Float;
13920 elsif Can_Derive_From (Standard_Float) then
13921 Base_Typ := Standard_Float;
13922 elsif Can_Derive_From (Standard_Long_Float) then
13923 Base_Typ := Standard_Long_Float;
13924 elsif Can_Derive_From (Standard_Long_Long_Float) then
13925 Base_Typ := Standard_Long_Long_Float;
13926
aa720a54 13927 -- If we can't derive from any existing type, use long_long_float
996ae0b0
RK
13928 -- and give appropriate message explaining the problem.
13929
13930 else
13931 Base_Typ := Standard_Long_Long_Float;
13932
13933 if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
13934 Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
13935 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
13936
13937 else
13938 Error_Msg_N
13939 ("range too large for any predefined type",
13940 Real_Range_Specification (Def));
13941 end if;
13942 end if;
13943
13944 -- If there are bounds given in the declaration use them as the bounds
13945 -- of the type, otherwise use the bounds of the predefined base type
13946 -- that was chosen based on the Digits value.
13947
13948 if Present (Real_Range_Specification (Def)) then
13949 Set_Scalar_Range (T, Real_Range_Specification (Def));
13950 Set_Is_Constrained (T);
13951
13952 -- The bounds of this range must be converted to machine numbers
13953 -- in accordance with RM 4.9(38).
13954
13955 Bound := Type_Low_Bound (T);
13956
13957 if Nkind (Bound) = N_Real_Literal then
fbf5a39b
AC
13958 Set_Realval
13959 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
996ae0b0
RK
13960 Set_Is_Machine_Number (Bound);
13961 end if;
13962
13963 Bound := Type_High_Bound (T);
13964
13965 if Nkind (Bound) = N_Real_Literal then
fbf5a39b
AC
13966 Set_Realval
13967 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
996ae0b0
RK
13968 Set_Is_Machine_Number (Bound);
13969 end if;
13970
13971 else
13972 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
13973 end if;
13974
13975 -- Complete definition of implicit base and declared first subtype
13976
13977 Set_Etype (Implicit_Base, Base_Typ);
13978
13979 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
13980 Set_Size_Info (Implicit_Base, (Base_Typ));
13981 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
13982 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
13983 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
13984 Set_Vax_Float (Implicit_Base, Vax_Float (Base_Typ));
13985
13986 Set_Ekind (T, E_Floating_Point_Subtype);
13987 Set_Etype (T, Implicit_Base);
13988
13989 Set_Size_Info (T, (Implicit_Base));
13990 Set_RM_Size (T, RM_Size (Implicit_Base));
13991 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13992 Set_Digits_Value (T, Digs_Val);
996ae0b0
RK
13993 end Floating_Point_Type_Declaration;
13994
13995 ----------------------------
13996 -- Get_Discriminant_Value --
13997 ----------------------------
13998
ffe9aba8 13999 -- This is the situation:
996ae0b0
RK
14000
14001 -- There is a non-derived type
14002
14003 -- type T0 (Dx, Dy, Dz...)
14004
a5b62485
AC
14005 -- There are zero or more levels of derivation, with each derivation
14006 -- either purely inheriting the discriminants, or defining its own.
996ae0b0
RK
14007
14008 -- type Ti is new Ti-1
14009 -- or
14010 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
14011 -- or
14012 -- subtype Ti is ...
14013
a5b62485
AC
14014 -- The subtype issue is avoided by the use of Original_Record_Component,
14015 -- and the fact that derived subtypes also derive the constraints.
996ae0b0
RK
14016
14017 -- This chain leads back from
14018
14019 -- Typ_For_Constraint
14020
14021 -- Typ_For_Constraint has discriminants, and the value for each
14022 -- discriminant is given by its corresponding Elmt of Constraints.
14023
71d9e9f2 14024 -- Discriminant is some discriminant in this hierarchy
996ae0b0 14025
71d9e9f2 14026 -- We need to return its value
996ae0b0
RK
14027
14028 -- We do this by recursively searching each level, and looking for
14029 -- Discriminant. Once we get to the bottom, we start backing up
14030 -- returning the value for it which may in turn be a discriminant
14031 -- further up, so on the backup we continue the substitution.
14032
14033 function Get_Discriminant_Value
14034 (Discriminant : Entity_Id;
14035 Typ_For_Constraint : Entity_Id;
b0f26df5 14036 Constraint : Elist_Id) return Node_Id
996ae0b0 14037 is
fbf5a39b 14038 function Search_Derivation_Levels
996ae0b0
RK
14039 (Ti : Entity_Id;
14040 Discrim_Values : Elist_Id;
b0f26df5 14041 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
996ae0b0
RK
14042 -- This is the routine that performs the recursive search of levels
14043 -- as described above.
14044
fbf5a39b
AC
14045 ------------------------------
14046 -- Search_Derivation_Levels --
14047 ------------------------------
14048
14049 function Search_Derivation_Levels
996ae0b0
RK
14050 (Ti : Entity_Id;
14051 Discrim_Values : Elist_Id;
b0f26df5 14052 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
996ae0b0
RK
14053 is
14054 Assoc : Elmt_Id;
14055 Disc : Entity_Id;
14056 Result : Node_Or_Entity_Id;
14057 Result_Entity : Node_Id;
14058
14059 begin
14060 -- If inappropriate type, return Error, this happens only in
14061 -- cascaded error situations, and we want to avoid a blow up.
14062
14063 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
14064 return Error;
14065 end if;
14066
fbf5a39b 14067 -- Look deeper if possible. Use Stored_Constraints only for
996ae0b0
RK
14068 -- untagged types. For tagged types use the given constraint.
14069 -- This asymmetry needs explanation???
14070
fbf5a39b
AC
14071 if not Stored_Discrim_Values
14072 and then Present (Stored_Constraint (Ti))
996ae0b0
RK
14073 and then not Is_Tagged_Type (Ti)
14074 then
fbf5a39b
AC
14075 Result :=
14076 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
996ae0b0
RK
14077 else
14078 declare
fbf5a39b 14079 Td : constant Entity_Id := Etype (Ti);
996ae0b0 14080
fbf5a39b 14081 begin
996ae0b0
RK
14082 if Td = Ti then
14083 Result := Discriminant;
14084
14085 else
fbf5a39b 14086 if Present (Stored_Constraint (Ti)) then
996ae0b0 14087 Result :=
fbf5a39b
AC
14088 Search_Derivation_Levels
14089 (Td, Stored_Constraint (Ti), True);
996ae0b0
RK
14090 else
14091 Result :=
fbf5a39b
AC
14092 Search_Derivation_Levels
14093 (Td, Discrim_Values, Stored_Discrim_Values);
996ae0b0
RK
14094 end if;
14095 end if;
14096 end;
14097 end if;
14098
14099 -- Extra underlying places to search, if not found above. For
14100 -- concurrent types, the relevant discriminant appears in the
14101 -- corresponding record. For a type derived from a private type
14102 -- without discriminant, the full view inherits the discriminants
14103 -- of the full view of the parent.
14104
14105 if Result = Discriminant then
14106 if Is_Concurrent_Type (Ti)
14107 and then Present (Corresponding_Record_Type (Ti))
14108 then
14109 Result :=
fbf5a39b 14110 Search_Derivation_Levels (
996ae0b0
RK
14111 Corresponding_Record_Type (Ti),
14112 Discrim_Values,
fbf5a39b 14113 Stored_Discrim_Values);
996ae0b0
RK
14114
14115 elsif Is_Private_Type (Ti)
14116 and then not Has_Discriminants (Ti)
14117 and then Present (Full_View (Ti))
14118 and then Etype (Full_View (Ti)) /= Ti
14119 then
14120 Result :=
fbf5a39b 14121 Search_Derivation_Levels (
996ae0b0
RK
14122 Full_View (Ti),
14123 Discrim_Values,
fbf5a39b 14124 Stored_Discrim_Values);
996ae0b0
RK
14125 end if;
14126 end if;
14127
71d9e9f2
ES
14128 -- If Result is not a (reference to a) discriminant, return it,
14129 -- otherwise set Result_Entity to the discriminant.
996ae0b0
RK
14130
14131 if Nkind (Result) = N_Defining_Identifier then
996ae0b0 14132 pragma Assert (Result = Discriminant);
996ae0b0
RK
14133 Result_Entity := Result;
14134
14135 else
14136 if not Denotes_Discriminant (Result) then
14137 return Result;
14138 end if;
14139
14140 Result_Entity := Entity (Result);
14141 end if;
14142
14143 -- See if this level of derivation actually has discriminants
14144 -- because tagged derivations can add them, hence the lower
14145 -- levels need not have any.
14146
14147 if not Has_Discriminants (Ti) then
14148 return Result;
14149 end if;
14150
14151 -- Scan Ti's discriminants for Result_Entity,
14152 -- and return its corresponding value, if any.
14153
14154 Result_Entity := Original_Record_Component (Result_Entity);
14155
14156 Assoc := First_Elmt (Discrim_Values);
14157
fbf5a39b
AC
14158 if Stored_Discrim_Values then
14159 Disc := First_Stored_Discriminant (Ti);
996ae0b0
RK
14160 else
14161 Disc := First_Discriminant (Ti);
14162 end if;
14163
14164 while Present (Disc) loop
996ae0b0
RK
14165 pragma Assert (Present (Assoc));
14166
14167 if Original_Record_Component (Disc) = Result_Entity then
14168 return Node (Assoc);
14169 end if;
14170
14171 Next_Elmt (Assoc);
14172
fbf5a39b
AC
14173 if Stored_Discrim_Values then
14174 Next_Stored_Discriminant (Disc);
996ae0b0
RK
14175 else
14176 Next_Discriminant (Disc);
14177 end if;
14178 end loop;
14179
14180 -- Could not find it
14181 --
14182 return Result;
fbf5a39b 14183 end Search_Derivation_Levels;
996ae0b0 14184
ce4a6e84
RD
14185 -- Local Variables
14186
996ae0b0
RK
14187 Result : Node_Or_Entity_Id;
14188
14189 -- Start of processing for Get_Discriminant_Value
14190
14191 begin
71d9e9f2
ES
14192 -- ??? This routine is a gigantic mess and will be deleted. For the
14193 -- time being just test for the trivial case before calling recurse.
996ae0b0
RK
14194
14195 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
14196 declare
9dfd2ff8
CC
14197 D : Entity_Id;
14198 E : Elmt_Id;
71d9e9f2 14199
996ae0b0 14200 begin
9dfd2ff8
CC
14201 D := First_Discriminant (Typ_For_Constraint);
14202 E := First_Elmt (Constraint);
996ae0b0
RK
14203 while Present (D) loop
14204 if Chars (D) = Chars (Discriminant) then
14205 return Node (E);
14206 end if;
14207
14208 Next_Discriminant (D);
14209 Next_Elmt (E);
14210 end loop;
14211 end;
14212 end if;
14213
fbf5a39b
AC
14214 Result := Search_Derivation_Levels
14215 (Typ_For_Constraint, Constraint, False);
996ae0b0
RK
14216
14217 -- ??? hack to disappear when this routine is gone
14218
14219 if Nkind (Result) = N_Defining_Identifier then
14220 declare
9dfd2ff8
CC
14221 D : Entity_Id;
14222 E : Elmt_Id;
fbf5a39b 14223
996ae0b0 14224 begin
9dfd2ff8
CC
14225 D := First_Discriminant (Typ_For_Constraint);
14226 E := First_Elmt (Constraint);
996ae0b0
RK
14227 while Present (D) loop
14228 if Corresponding_Discriminant (D) = Discriminant then
14229 return Node (E);
14230 end if;
14231
14232 Next_Discriminant (D);
14233 Next_Elmt (E);
14234 end loop;
14235 end;
14236 end if;
14237
14238 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
14239 return Result;
14240 end Get_Discriminant_Value;
14241
14242 --------------------------
14243 -- Has_Range_Constraint --
14244 --------------------------
14245
14246 function Has_Range_Constraint (N : Node_Id) return Boolean is
14247 C : constant Node_Id := Constraint (N);
14248
14249 begin
14250 if Nkind (C) = N_Range_Constraint then
14251 return True;
14252
14253 elsif Nkind (C) = N_Digits_Constraint then
14254 return
14255 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
14256 or else
14257 Present (Range_Constraint (C));
14258
14259 elsif Nkind (C) = N_Delta_Constraint then
14260 return Present (Range_Constraint (C));
14261
14262 else
14263 return False;
14264 end if;
14265 end Has_Range_Constraint;
14266
14267 ------------------------
14268 -- Inherit_Components --
14269 ------------------------
14270
14271 function Inherit_Components
14272 (N : Node_Id;
14273 Parent_Base : Entity_Id;
14274 Derived_Base : Entity_Id;
14275 Is_Tagged : Boolean;
14276 Inherit_Discr : Boolean;
b0f26df5 14277 Discs : Elist_Id) return Elist_Id
996ae0b0 14278 is
fbf5a39b 14279 Assoc_List : constant Elist_Id := New_Elmt_List;
996ae0b0
RK
14280
14281 procedure Inherit_Component
14282 (Old_C : Entity_Id;
14283 Plain_Discrim : Boolean := False;
fbf5a39b 14284 Stored_Discrim : Boolean := False);
a5b62485
AC
14285 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
14286 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
14287 -- True, Old_C is a stored discriminant. If they are both false then
14288 -- Old_C is a regular component.
996ae0b0
RK
14289
14290 -----------------------
14291 -- Inherit_Component --
14292 -----------------------
14293
14294 procedure Inherit_Component
14295 (Old_C : Entity_Id;
14296 Plain_Discrim : Boolean := False;
fbf5a39b 14297 Stored_Discrim : Boolean := False)
996ae0b0 14298 is
fbf5a39b 14299 New_C : constant Entity_Id := New_Copy (Old_C);
996ae0b0
RK
14300
14301 Discrim : Entity_Id;
14302 Corr_Discrim : Entity_Id;
14303
14304 begin
fbf5a39b 14305 pragma Assert (not Is_Tagged or else not Stored_Discrim);
996ae0b0
RK
14306
14307 Set_Parent (New_C, Parent (Old_C));
14308
88b32fc3
BD
14309 -- Regular discriminants and components must be inserted in the scope
14310 -- of the Derived_Base. Do it here.
996ae0b0 14311
fbf5a39b 14312 if not Stored_Discrim then
996ae0b0
RK
14313 Enter_Name (New_C);
14314 end if;
14315
14316 -- For tagged types the Original_Record_Component must point to
14317 -- whatever this field was pointing to in the parent type. This has
14318 -- already been achieved by the call to New_Copy above.
14319
14320 if not Is_Tagged then
14321 Set_Original_Record_Component (New_C, New_C);
14322 end if;
14323
14324 -- If we have inherited a component then see if its Etype contains
14325 -- references to Parent_Base discriminants. In this case, replace
14326 -- these references with the constraints given in Discs. We do not
14327 -- do this for the partial view of private types because this is
14328 -- not needed (only the components of the full view will be used
14329 -- for code generation) and cause problem. We also avoid this
14330 -- transformation in some error situations.
14331
14332 if Ekind (New_C) = E_Component then
14333 if (Is_Private_Type (Derived_Base)
88b32fc3 14334 and then not Is_Generic_Type (Derived_Base))
996ae0b0 14335 or else (Is_Empty_Elmt_List (Discs)
88b32fc3 14336 and then not Expander_Active)
996ae0b0
RK
14337 then
14338 Set_Etype (New_C, Etype (Old_C));
88b32fc3 14339
996ae0b0 14340 else
88b32fc3
BD
14341 -- The current component introduces a circularity of the
14342 -- following kind:
14343
14344 -- limited with Pack_2;
14345 -- package Pack_1 is
14346 -- type T_1 is tagged record
14347 -- Comp : access Pack_2.T_2;
14348 -- ...
14349 -- end record;
14350 -- end Pack_1;
14351
14352 -- with Pack_1;
14353 -- package Pack_2 is
14354 -- type T_2 is new Pack_1.T_1 with ...;
14355 -- end Pack_2;
14356
2b73cf68
JM
14357 Set_Etype
14358 (New_C,
14359 Constrain_Component_Type
14360 (Old_C, Derived_Base, N, Parent_Base, Discs));
996ae0b0
RK
14361 end if;
14362 end if;
14363
14364 -- In derived tagged types it is illegal to reference a non
14365 -- discriminant component in the parent type. To catch this, mark
14366 -- these components with an Ekind of E_Void. This will be reset in
14367 -- Record_Type_Definition after processing the record extension of
14368 -- the derived type.
14369
2b73cf68
JM
14370 -- If the declaration is a private extension, there is no further
14371 -- record extension to process, and the components retain their
14372 -- current kind, because they are visible at this point.
14373
14374 if Is_Tagged and then Ekind (New_C) = E_Component
14375 and then Nkind (N) /= N_Private_Extension_Declaration
14376 then
996ae0b0
RK
14377 Set_Ekind (New_C, E_Void);
14378 end if;
14379
14380 if Plain_Discrim then
14381 Set_Corresponding_Discriminant (New_C, Old_C);
14382 Build_Discriminal (New_C);
14383
fbf5a39b 14384 -- If we are explicitly inheriting a stored discriminant it will be
996ae0b0
RK
14385 -- completely hidden.
14386
fbf5a39b 14387 elsif Stored_Discrim then
996ae0b0
RK
14388 Set_Corresponding_Discriminant (New_C, Empty);
14389 Set_Discriminal (New_C, Empty);
14390 Set_Is_Completely_Hidden (New_C);
14391
14392 -- Set the Original_Record_Component of each discriminant in the
fbf5a39b 14393 -- derived base to point to the corresponding stored that we just
996ae0b0
RK
14394 -- created.
14395
14396 Discrim := First_Discriminant (Derived_Base);
14397 while Present (Discrim) loop
14398 Corr_Discrim := Corresponding_Discriminant (Discrim);
14399
9dfd2ff8 14400 -- Corr_Discrim could be missing in an error situation
996ae0b0
RK
14401
14402 if Present (Corr_Discrim)
14403 and then Original_Record_Component (Corr_Discrim) = Old_C
14404 then
14405 Set_Original_Record_Component (Discrim, New_C);
14406 end if;
14407
14408 Next_Discriminant (Discrim);
14409 end loop;
14410
14411 Append_Entity (New_C, Derived_Base);
14412 end if;
14413
14414 if not Is_Tagged then
14415 Append_Elmt (Old_C, Assoc_List);
14416 Append_Elmt (New_C, Assoc_List);
14417 end if;
14418 end Inherit_Component;
14419
71d9e9f2 14420 -- Variables local to Inherit_Component
996ae0b0
RK
14421
14422 Loc : constant Source_Ptr := Sloc (N);
14423
14424 Parent_Discrim : Entity_Id;
fbf5a39b 14425 Stored_Discrim : Entity_Id;
996ae0b0 14426 D : Entity_Id;
71d9e9f2 14427 Component : Entity_Id;
996ae0b0
RK
14428
14429 -- Start of processing for Inherit_Components
14430
14431 begin
14432 if not Is_Tagged then
14433 Append_Elmt (Parent_Base, Assoc_List);
14434 Append_Elmt (Derived_Base, Assoc_List);
14435 end if;
14436
ffe9aba8 14437 -- Inherit parent discriminants if needed
996ae0b0
RK
14438
14439 if Inherit_Discr then
14440 Parent_Discrim := First_Discriminant (Parent_Base);
14441 while Present (Parent_Discrim) loop
14442 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
14443 Next_Discriminant (Parent_Discrim);
14444 end loop;
14445 end if;
14446
ffe9aba8 14447 -- Create explicit stored discrims for untagged types when necessary
996ae0b0
RK
14448
14449 if not Has_Unknown_Discriminants (Derived_Base)
14450 and then Has_Discriminants (Parent_Base)
14451 and then not Is_Tagged
14452 and then
14453 (not Inherit_Discr
71d9e9f2
ES
14454 or else First_Discriminant (Parent_Base) /=
14455 First_Stored_Discriminant (Parent_Base))
996ae0b0 14456 then
fbf5a39b
AC
14457 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
14458 while Present (Stored_Discrim) loop
14459 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
14460 Next_Stored_Discriminant (Stored_Discrim);
996ae0b0
RK
14461 end loop;
14462 end if;
14463
14464 -- See if we can apply the second transformation for derived types, as
14465 -- explained in point 6. in the comments above Build_Derived_Record_Type
a5b62485
AC
14466 -- This is achieved by appending Derived_Base discriminants into Discs,
14467 -- which has the side effect of returning a non empty Discs list to the
14468 -- caller of Inherit_Components, which is what we want. This must be
14469 -- done for private derived types if there are explicit stored
14470 -- discriminants, to ensure that we can retrieve the values of the
14471 -- constraints provided in the ancestors.
996ae0b0
RK
14472
14473 if Inherit_Discr
14474 and then Is_Empty_Elmt_List (Discs)
30c20106
AC
14475 and then Present (First_Discriminant (Derived_Base))
14476 and then
14477 (not Is_Private_Type (Derived_Base)
71d9e9f2
ES
14478 or else Is_Completely_Hidden
14479 (First_Stored_Discriminant (Derived_Base))
14480 or else Is_Generic_Type (Derived_Base))
996ae0b0
RK
14481 then
14482 D := First_Discriminant (Derived_Base);
14483 while Present (D) loop
14484 Append_Elmt (New_Reference_To (D, Loc), Discs);
14485 Next_Discriminant (D);
14486 end loop;
14487 end if;
14488
14489 -- Finally, inherit non-discriminant components unless they are not
14490 -- visible because defined or inherited from the full view of the
14491 -- parent. Don't inherit the _parent field of the parent type.
14492
14493 Component := First_Entity (Parent_Base);
14494 while Present (Component) loop
758c442c 14495
2b73cf68
JM
14496 -- Ada 2005 (AI-251): Do not inherit components associated with
14497 -- secondary tags of the parent.
758c442c
GD
14498
14499 if Ekind (Component) = E_Component
7d7af38a 14500 and then Present (Related_Type (Component))
758c442c
GD
14501 then
14502 null;
14503
14504 elsif Ekind (Component) /= E_Component
996ae0b0
RK
14505 or else Chars (Component) = Name_uParent
14506 then
14507 null;
14508
14509 -- If the derived type is within the parent type's declarative
14510 -- region, then the components can still be inherited even though
14511 -- they aren't visible at this point. This can occur for cases
14512 -- such as within public child units where the components must
14513 -- become visible upon entering the child unit's private part.
14514
14515 elsif not Is_Visible_Component (Component)
14516 and then not In_Open_Scopes (Scope (Parent_Base))
14517 then
14518 null;
14519
14520 elsif Ekind (Derived_Base) = E_Private_Type
14521 or else Ekind (Derived_Base) = E_Limited_Private_Type
14522 then
14523 null;
14524
14525 else
14526 Inherit_Component (Component);
14527 end if;
14528
14529 Next_Entity (Component);
14530 end loop;
14531
14532 -- For tagged derived types, inherited discriminants cannot be used in
14533 -- component declarations of the record extension part. To achieve this
14534 -- we mark the inherited discriminants as not visible.
14535
14536 if Is_Tagged and then Inherit_Discr then
14537 D := First_Discriminant (Derived_Base);
14538 while Present (D) loop
14539 Set_Is_Immediately_Visible (D, False);
14540 Next_Discriminant (D);
14541 end loop;
14542 end if;
14543
14544 return Assoc_List;
14545 end Inherit_Components;
14546
57193e09
TQ
14547 -----------------------
14548 -- Is_Null_Extension --
14549 -----------------------
14550
14551 function Is_Null_Extension (T : Entity_Id) return Boolean is
2b73cf68
JM
14552 Type_Decl : constant Node_Id := Parent (T);
14553 Comp_List : Node_Id;
14554 Comp : Node_Id;
57193e09
TQ
14555
14556 begin
fea9e956
ES
14557 if Nkind (Type_Decl) /= N_Full_Type_Declaration
14558 or else not Is_Tagged_Type (T)
14559 or else Nkind (Type_Definition (Type_Decl)) /=
14560 N_Derived_Type_Definition
14561 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
57193e09
TQ
14562 then
14563 return False;
14564 end if;
14565
fea9e956
ES
14566 Comp_List :=
14567 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
57193e09 14568
fea9e956 14569 if Present (Discriminant_Specifications (Type_Decl)) then
57193e09
TQ
14570 return False;
14571
14572 elsif Present (Comp_List)
14573 and then Is_Non_Empty_List (Component_Items (Comp_List))
14574 then
2b73cf68
JM
14575 Comp := First (Component_Items (Comp_List));
14576
14577 -- Only user-defined components are relevant. The component list
14578 -- may also contain a parent component and internal components
14579 -- corresponding to secondary tags, but these do not determine
14580 -- whether this is a null extension.
14581
14582 while Present (Comp) loop
14583 if Comes_From_Source (Comp) then
14584 return False;
14585 end if;
57193e09 14586
2b73cf68
JM
14587 Next (Comp);
14588 end loop;
57193e09 14589
2b73cf68 14590 return True;
57193e09
TQ
14591 else
14592 return True;
14593 end if;
14594 end Is_Null_Extension;
14595
ce4a6e84
RD
14596 --------------------
14597 -- Is_Progenitor --
14598 --------------------
14599
14600 function Is_Progenitor
14601 (Iface : Entity_Id;
14602 Typ : Entity_Id) return Boolean
14603 is
ce4a6e84 14604 begin
ce2b6ba5
JM
14605 return Implements_Interface (Typ, Iface,
14606 Exclude_Parents => True);
ce4a6e84
RD
14607 end Is_Progenitor;
14608
996ae0b0
RK
14609 ------------------------------
14610 -- Is_Valid_Constraint_Kind --
14611 ------------------------------
14612
14613 function Is_Valid_Constraint_Kind
14614 (T_Kind : Type_Kind;
b0f26df5 14615 Constraint_Kind : Node_Kind) return Boolean
996ae0b0
RK
14616 is
14617 begin
14618 case T_Kind is
996ae0b0
RK
14619 when Enumeration_Kind |
14620 Integer_Kind =>
14621 return Constraint_Kind = N_Range_Constraint;
14622
14623 when Decimal_Fixed_Point_Kind =>
7d7af38a
JM
14624 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14625 N_Range_Constraint);
996ae0b0
RK
14626
14627 when Ordinary_Fixed_Point_Kind =>
7d7af38a
JM
14628 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
14629 N_Range_Constraint);
996ae0b0
RK
14630
14631 when Float_Kind =>
7d7af38a
JM
14632 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14633 N_Range_Constraint);
996ae0b0
RK
14634
14635 when Access_Kind |
14636 Array_Kind |
14637 E_Record_Type |
14638 E_Record_Subtype |
14639 Class_Wide_Kind |
14640 E_Incomplete_Type |
14641 Private_Kind |
14642 Concurrent_Kind =>
14643 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
14644
14645 when others =>
71d9e9f2 14646 return True; -- Error will be detected later
996ae0b0 14647 end case;
996ae0b0
RK
14648 end Is_Valid_Constraint_Kind;
14649
14650 --------------------------
14651 -- Is_Visible_Component --
14652 --------------------------
14653
14654 function Is_Visible_Component (C : Entity_Id) return Boolean is
fbf5a39b 14655 Original_Comp : Entity_Id := Empty;
996ae0b0 14656 Original_Scope : Entity_Id;
fbf5a39b
AC
14657 Type_Scope : Entity_Id;
14658
14659 function Is_Local_Type (Typ : Entity_Id) return Boolean;
a5b62485
AC
14660 -- Check whether parent type of inherited component is declared locally,
14661 -- possibly within a nested package or instance. The current scope is
14662 -- the derived record itself.
fbf5a39b
AC
14663
14664 -------------------
14665 -- Is_Local_Type --
14666 -------------------
14667
14668 function Is_Local_Type (Typ : Entity_Id) return Boolean is
9dfd2ff8 14669 Scop : Entity_Id;
fbf5a39b
AC
14670
14671 begin
9dfd2ff8 14672 Scop := Scope (Typ);
fbf5a39b
AC
14673 while Present (Scop)
14674 and then Scop /= Standard_Standard
14675 loop
14676 if Scop = Scope (Current_Scope) then
14677 return True;
14678 end if;
14679
14680 Scop := Scope (Scop);
14681 end loop;
71d9e9f2 14682
fbf5a39b
AC
14683 return False;
14684 end Is_Local_Type;
14685
14686 -- Start of processing for Is_Visible_Component
996ae0b0
RK
14687
14688 begin
fbf5a39b
AC
14689 if Ekind (C) = E_Component
14690 or else Ekind (C) = E_Discriminant
14691 then
14692 Original_Comp := Original_Record_Component (C);
14693 end if;
14694
996ae0b0
RK
14695 if No (Original_Comp) then
14696
14697 -- Premature usage, or previous error
14698
14699 return False;
14700
14701 else
14702 Original_Scope := Scope (Original_Comp);
fbf5a39b 14703 Type_Scope := Scope (Base_Type (Scope (C)));
996ae0b0
RK
14704 end if;
14705
fbf5a39b 14706 -- This test only concerns tagged types
996ae0b0
RK
14707
14708 if not Is_Tagged_Type (Original_Scope) then
14709 return True;
14710
fbf5a39b 14711 -- If it is _Parent or _Tag, there is no visibility issue
996ae0b0
RK
14712
14713 elsif not Comes_From_Source (Original_Comp) then
14714 return True;
14715
a5b62485
AC
14716 -- If we are in the body of an instantiation, the component is visible
14717 -- even when the parent type (possibly defined in an enclosing unit or
14718 -- in a parent unit) might not.
996ae0b0
RK
14719
14720 elsif In_Instance_Body then
14721 return True;
14722
71d9e9f2 14723 -- Discriminants are always visible
996ae0b0
RK
14724
14725 elsif Ekind (Original_Comp) = E_Discriminant
14726 and then not Has_Unknown_Discriminants (Original_Scope)
14727 then
14728 return True;
14729
71d9e9f2
ES
14730 -- If the component has been declared in an ancestor which is currently
14731 -- a private type, then it is not visible. The same applies if the
14732 -- component's containing type is not in an open scope and the original
dc06abec 14733 -- component's enclosing type is a visible full view of a private type
71d9e9f2
ES
14734 -- (which can occur in cases where an attempt is being made to reference
14735 -- a component in a sibling package that is inherited from a visible
14736 -- component of a type in an ancestor package; the component in the
14737 -- sibling package should not be visible even though the component it
14738 -- inherited from is visible). This does not apply however in the case
14739 -- where the scope of the type is a private child unit, or when the
14740 -- parent comes from a local package in which the ancestor is currently
14741 -- visible. The latter suppression of visibility is needed for cases
14742 -- that are tested in B730006.
fbf5a39b
AC
14743
14744 elsif Is_Private_Type (Original_Scope)
14745 or else
14746 (not Is_Private_Descendant (Type_Scope)
14747 and then not In_Open_Scopes (Type_Scope)
14748 and then Has_Private_Declaration (Original_Scope))
996ae0b0 14749 then
fbf5a39b
AC
14750 -- If the type derives from an entity in a formal package, there
14751 -- are no additional visible components.
14752
14753 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
14754 N_Formal_Package_Declaration
14755 then
14756 return False;
14757
14758 -- if we are not in the private part of the current package, there
14759 -- are no additional visible components.
14760
14761 elsif Ekind (Scope (Current_Scope)) = E_Package
14762 and then not In_Private_Part (Scope (Current_Scope))
14763 then
14764 return False;
14765 else
14766 return
14767 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
dc06abec 14768 and then In_Open_Scopes (Scope (Original_Scope))
fbf5a39b
AC
14769 and then Is_Local_Type (Type_Scope);
14770 end if;
996ae0b0
RK
14771
14772 -- There is another weird way in which a component may be invisible
14773 -- when the private and the full view are not derived from the same
14774 -- ancestor. Here is an example :
14775
14776 -- type A1 is tagged record F1 : integer; end record;
14777 -- type A2 is new A1 with record F2 : integer; end record;
14778 -- type T is new A1 with private;
14779 -- private
fbf5a39b 14780 -- type T is new A2 with null record;
996ae0b0 14781
a5b62485
AC
14782 -- In this case, the full view of T inherits F1 and F2 but the private
14783 -- view inherits only F1
996ae0b0
RK
14784
14785 else
14786 declare
14787 Ancestor : Entity_Id := Scope (C);
14788
14789 begin
14790 loop
14791 if Ancestor = Original_Scope then
14792 return True;
14793 elsif Ancestor = Etype (Ancestor) then
14794 return False;
14795 end if;
14796
14797 Ancestor := Etype (Ancestor);
14798 end loop;
996ae0b0
RK
14799 end;
14800 end if;
14801 end Is_Visible_Component;
14802
14803 --------------------------
14804 -- Make_Class_Wide_Type --
14805 --------------------------
14806
14807 procedure Make_Class_Wide_Type (T : Entity_Id) is
14808 CW_Type : Entity_Id;
14809 CW_Name : Name_Id;
14810 Next_E : Entity_Id;
14811
14812 begin
88b32fc3
BD
14813 -- The class wide type can have been defined by the partial view, in
14814 -- which case everything is already done.
996ae0b0
RK
14815
14816 if Present (Class_Wide_Type (T)) then
14817 return;
14818 end if;
14819
14820 CW_Type :=
14821 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
14822
14823 -- Inherit root type characteristics
14824
14825 CW_Name := Chars (CW_Type);
14826 Next_E := Next_Entity (CW_Type);
14827 Copy_Node (T, CW_Type);
14828 Set_Comes_From_Source (CW_Type, False);
14829 Set_Chars (CW_Type, CW_Name);
14830 Set_Parent (CW_Type, Parent (T));
14831 Set_Next_Entity (CW_Type, Next_E);
88b32fc3
BD
14832
14833 -- Ensure we have a new freeze node for the class-wide type. The partial
14834 -- view may have freeze action of its own, requiring a proper freeze
14835 -- node, and the same freeze node cannot be shared between the two
14836 -- types.
14837
996ae0b0 14838 Set_Has_Delayed_Freeze (CW_Type);
88b32fc3 14839 Set_Freeze_Node (CW_Type, Empty);
996ae0b0
RK
14840
14841 -- Customize the class-wide type: It has no prim. op., it cannot be
07fc65c4 14842 -- abstract and its Etype points back to the specific root type.
996ae0b0
RK
14843
14844 Set_Ekind (CW_Type, E_Class_Wide_Type);
14845 Set_Is_Tagged_Type (CW_Type, True);
14846 Set_Primitive_Operations (CW_Type, New_Elmt_List);
fea9e956 14847 Set_Is_Abstract_Type (CW_Type, False);
996ae0b0
RK
14848 Set_Is_Constrained (CW_Type, False);
14849 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
996ae0b0 14850
07fc65c4
GB
14851 if Ekind (T) = E_Class_Wide_Subtype then
14852 Set_Etype (CW_Type, Etype (Base_Type (T)));
14853 else
14854 Set_Etype (CW_Type, T);
14855 end if;
14856
996ae0b0
RK
14857 -- If this is the class_wide type of a constrained subtype, it does
14858 -- not have discriminants.
14859
14860 Set_Has_Discriminants (CW_Type,
14861 Has_Discriminants (T) and then not Is_Constrained (T));
14862
14863 Set_Has_Unknown_Discriminants (CW_Type, True);
14864 Set_Class_Wide_Type (T, CW_Type);
14865 Set_Equivalent_Type (CW_Type, Empty);
14866
14867 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
14868
14869 Set_Class_Wide_Type (CW_Type, CW_Type);
996ae0b0
RK
14870 end Make_Class_Wide_Type;
14871
14872 ----------------
14873 -- Make_Index --
14874 ----------------
14875
14876 procedure Make_Index
14877 (I : Node_Id;
14878 Related_Nod : Node_Id;
14879 Related_Id : Entity_Id := Empty;
14880 Suffix_Index : Nat := 1)
14881 is
14882 R : Node_Id;
14883 T : Entity_Id;
14884 Def_Id : Entity_Id := Empty;
14885 Found : Boolean := False;
14886
14887 begin
14888 -- For a discrete range used in a constrained array definition and
14889 -- defined by a range, an implicit conversion to the predefined type
14890 -- INTEGER is assumed if each bound is either a numeric literal, a named
14891 -- number, or an attribute, and the type of both bounds (prior to the
14892 -- implicit conversion) is the type universal_integer. Otherwise, both
14893 -- bounds must be of the same discrete type, other than universal
14894 -- integer; this type must be determinable independently of the
14895 -- context, but using the fact that the type must be discrete and that
14896 -- both bounds must have the same type.
14897
14898 -- Character literals also have a universal type in the absence of
14899 -- of additional context, and are resolved to Standard_Character.
14900
14901 if Nkind (I) = N_Range then
14902
14903 -- The index is given by a range constraint. The bounds are known
14904 -- to be of a consistent type.
14905
14906 if not Is_Overloaded (I) then
14907 T := Etype (I);
14908
2b73cf68 14909 -- For universal bounds, choose the specific predefined type
996ae0b0
RK
14910
14911 if T = Universal_Integer then
14912 T := Standard_Integer;
14913
14914 elsif T = Any_Character then
2b73cf68 14915 Ambiguous_Character (Low_Bound (I));
996ae0b0
RK
14916
14917 T := Standard_Character;
14918 end if;
14919
df89ab66
ES
14920 -- The node may be overloaded because some user-defined operators
14921 -- are available, but if a universal interpretation exists it is
14922 -- also the selected one.
14923
14924 elsif Universal_Interpretation (I) = Universal_Integer then
14925 T := Standard_Integer;
14926
996ae0b0
RK
14927 else
14928 T := Any_Type;
14929
14930 declare
14931 Ind : Interp_Index;
14932 It : Interp;
14933
14934 begin
14935 Get_First_Interp (I, Ind, It);
996ae0b0
RK
14936 while Present (It.Typ) loop
14937 if Is_Discrete_Type (It.Typ) then
14938
14939 if Found
14940 and then not Covers (It.Typ, T)
14941 and then not Covers (T, It.Typ)
14942 then
14943 Error_Msg_N ("ambiguous bounds in discrete range", I);
14944 exit;
14945 else
14946 T := It.Typ;
14947 Found := True;
14948 end if;
14949 end if;
14950
14951 Get_Next_Interp (Ind, It);
14952 end loop;
14953
14954 if T = Any_Type then
14955 Error_Msg_N ("discrete type required for range", I);
14956 Set_Etype (I, Any_Type);
14957 return;
14958
14959 elsif T = Universal_Integer then
14960 T := Standard_Integer;
14961 end if;
14962 end;
14963 end if;
14964
14965 if not Is_Discrete_Type (T) then
14966 Error_Msg_N ("discrete type required for range", I);
14967 Set_Etype (I, Any_Type);
14968 return;
14969 end if;
14970
fbf5a39b
AC
14971 if Nkind (Low_Bound (I)) = N_Attribute_Reference
14972 and then Attribute_Name (Low_Bound (I)) = Name_First
14973 and then Is_Entity_Name (Prefix (Low_Bound (I)))
14974 and then Is_Type (Entity (Prefix (Low_Bound (I))))
14975 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
14976 then
a5b62485
AC
14977 -- The type of the index will be the type of the prefix, as long
14978 -- as the upper bound is 'Last of the same type.
fbf5a39b
AC
14979
14980 Def_Id := Entity (Prefix (Low_Bound (I)));
14981
14982 if Nkind (High_Bound (I)) /= N_Attribute_Reference
14983 or else Attribute_Name (High_Bound (I)) /= Name_Last
14984 or else not Is_Entity_Name (Prefix (High_Bound (I)))
14985 or else Entity (Prefix (High_Bound (I))) /= Def_Id
14986 then
14987 Def_Id := Empty;
14988 end if;
14989 end if;
14990
996ae0b0 14991 R := I;
07fc65c4 14992 Process_Range_Expr_In_Decl (R, T);
996ae0b0
RK
14993
14994 elsif Nkind (I) = N_Subtype_Indication then
14995
71d9e9f2 14996 -- The index is given by a subtype with a range constraint
996ae0b0
RK
14997
14998 T := Base_Type (Entity (Subtype_Mark (I)));
14999
15000 if not Is_Discrete_Type (T) then
15001 Error_Msg_N ("discrete type required for range", I);
15002 Set_Etype (I, Any_Type);
15003 return;
15004 end if;
15005
15006 R := Range_Expression (Constraint (I));
15007
15008 Resolve (R, T);
07fc65c4 15009 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
996ae0b0
RK
15010
15011 elsif Nkind (I) = N_Attribute_Reference then
15012
15013 -- The parser guarantees that the attribute is a RANGE attribute
15014
fbf5a39b
AC
15015 -- If the node denotes the range of a type mark, that is also the
15016 -- resulting type, and we do no need to create an Itype for it.
15017
15018 if Is_Entity_Name (Prefix (I))
15019 and then Comes_From_Source (I)
15020 and then Is_Type (Entity (Prefix (I)))
15021 and then Is_Discrete_Type (Entity (Prefix (I)))
15022 then
15023 Def_Id := Entity (Prefix (I));
15024 end if;
15025
d087cd96 15026 Analyze_And_Resolve (I);
996ae0b0 15027 T := Etype (I);
996ae0b0
RK
15028 R := I;
15029
15030 -- If none of the above, must be a subtype. We convert this to a
15031 -- range attribute reference because in the case of declared first
15032 -- named subtypes, the types in the range reference can be different
15033 -- from the type of the entity. A range attribute normalizes the
15034 -- reference and obtains the correct types for the bounds.
15035
15036 -- This transformation is in the nature of an expansion, is only
15037 -- done if expansion is active. In particular, it is not done on
15038 -- formal generic types, because we need to retain the name of the
15039 -- original index for instantiation purposes.
15040
15041 else
15042 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
15043 Error_Msg_N ("invalid subtype mark in discrete range ", I);
15044 Set_Etype (I, Any_Integer);
15045 return;
71d9e9f2 15046
996ae0b0
RK
15047 else
15048 -- The type mark may be that of an incomplete type. It is only
15049 -- now that we can get the full view, previous analysis does
15050 -- not look specifically for a type mark.
15051
15052 Set_Entity (I, Get_Full_View (Entity (I)));
15053 Set_Etype (I, Entity (I));
15054 Def_Id := Entity (I);
15055
15056 if not Is_Discrete_Type (Def_Id) then
15057 Error_Msg_N ("discrete type required for index", I);
15058 Set_Etype (I, Any_Type);
15059 return;
15060 end if;
15061 end if;
15062
15063 if Expander_Active then
15064 Rewrite (I,
15065 Make_Attribute_Reference (Sloc (I),
15066 Attribute_Name => Name_Range,
15067 Prefix => Relocate_Node (I)));
15068
15069 -- The original was a subtype mark that does not freeze. This
15070 -- means that the rewritten version must not freeze either.
15071
15072 Set_Must_Not_Freeze (I);
15073 Set_Must_Not_Freeze (Prefix (I));
15074
15075 -- Is order critical??? if so, document why, if not
15076 -- use Analyze_And_Resolve
15077
88b32fc3 15078 Analyze_And_Resolve (I);
996ae0b0 15079 T := Etype (I);
996ae0b0
RK
15080 R := I;
15081
fbf5a39b
AC
15082 -- If expander is inactive, type is legal, nothing else to construct
15083
996ae0b0 15084 else
996ae0b0
RK
15085 return;
15086 end if;
15087 end if;
15088
15089 if not Is_Discrete_Type (T) then
15090 Error_Msg_N ("discrete type required for range", I);
15091 Set_Etype (I, Any_Type);
15092 return;
15093
15094 elsif T = Any_Type then
15095 Set_Etype (I, Any_Type);
15096 return;
15097 end if;
15098
a5b62485
AC
15099 -- We will now create the appropriate Itype to describe the range, but
15100 -- first a check. If we originally had a subtype, then we just label
15101 -- the range with this subtype. Not only is there no need to construct
15102 -- a new subtype, but it is wrong to do so for two reasons:
996ae0b0 15103
a5b62485
AC
15104 -- 1. A legality concern, if we have a subtype, it must not freeze,
15105 -- and the Itype would cause freezing incorrectly
996ae0b0 15106
a5b62485
AC
15107 -- 2. An efficiency concern, if we created an Itype, it would not be
15108 -- recognized as the same type for the purposes of eliminating
15109 -- checks in some circumstances.
996ae0b0 15110
71d9e9f2 15111 -- We signal this case by setting the subtype entity in Def_Id
996ae0b0 15112
996ae0b0 15113 if No (Def_Id) then
996ae0b0
RK
15114 Def_Id :=
15115 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
15116 Set_Etype (Def_Id, Base_Type (T));
15117
15118 if Is_Signed_Integer_Type (T) then
15119 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
15120
15121 elsif Is_Modular_Integer_Type (T) then
15122 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
15123
15124 else
15125 Set_Ekind (Def_Id, E_Enumeration_Subtype);
15126 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
fbf5a39b 15127 Set_First_Literal (Def_Id, First_Literal (T));
996ae0b0
RK
15128 end if;
15129
15130 Set_Size_Info (Def_Id, (T));
15131 Set_RM_Size (Def_Id, RM_Size (T));
15132 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
15133
15134 Set_Scalar_Range (Def_Id, R);
15135 Conditional_Delay (Def_Id, T);
15136
15137 -- In the subtype indication case, if the immediate parent of the
15138 -- new subtype is non-static, then the subtype we create is non-
15139 -- static, even if its bounds are static.
15140
15141 if Nkind (I) = N_Subtype_Indication
15142 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
15143 then
15144 Set_Is_Non_Static_Subtype (Def_Id);
15145 end if;
15146 end if;
15147
15148 -- Final step is to label the index with this constructed type
15149
15150 Set_Etype (I, Def_Id);
15151 end Make_Index;
15152
15153 ------------------------------
15154 -- Modular_Type_Declaration --
15155 ------------------------------
15156
15157 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15158 Mod_Expr : constant Node_Id := Expression (Def);
15159 M_Val : Uint;
15160
15161 procedure Set_Modular_Size (Bits : Int);
15162 -- Sets RM_Size to Bits, and Esize to normal word size above this
15163
fbf5a39b
AC
15164 ----------------------
15165 -- Set_Modular_Size --
15166 ----------------------
15167
996ae0b0
RK
15168 procedure Set_Modular_Size (Bits : Int) is
15169 begin
15170 Set_RM_Size (T, UI_From_Int (Bits));
15171
15172 if Bits <= 8 then
15173 Init_Esize (T, 8);
15174
15175 elsif Bits <= 16 then
15176 Init_Esize (T, 16);
15177
15178 elsif Bits <= 32 then
15179 Init_Esize (T, 32);
15180
15181 else
15182 Init_Esize (T, System_Max_Binary_Modulus_Power);
15183 end if;
8dc2ddaf
RD
15184
15185 if not Non_Binary_Modulus (T)
15186 and then Esize (T) = RM_Size (T)
15187 then
15188 Set_Is_Known_Valid (T);
15189 end if;
996ae0b0
RK
15190 end Set_Modular_Size;
15191
15192 -- Start of processing for Modular_Type_Declaration
15193
15194 begin
15195 Analyze_And_Resolve (Mod_Expr, Any_Integer);
15196 Set_Etype (T, T);
15197 Set_Ekind (T, E_Modular_Integer_Type);
15198 Init_Alignment (T);
15199 Set_Is_Constrained (T);
15200
15201 if not Is_OK_Static_Expression (Mod_Expr) then
fbf5a39b
AC
15202 Flag_Non_Static_Expr
15203 ("non-static expression used for modular type bound!", Mod_Expr);
996ae0b0
RK
15204 M_Val := 2 ** System_Max_Binary_Modulus_Power;
15205 else
15206 M_Val := Expr_Value (Mod_Expr);
15207 end if;
15208
15209 if M_Val < 1 then
15210 Error_Msg_N ("modulus value must be positive", Mod_Expr);
15211 M_Val := 2 ** System_Max_Binary_Modulus_Power;
15212 end if;
15213
15214 Set_Modulus (T, M_Val);
15215
15216 -- Create bounds for the modular type based on the modulus given in
15217 -- the type declaration and then analyze and resolve those bounds.
15218
15219 Set_Scalar_Range (T,
15220 Make_Range (Sloc (Mod_Expr),
15221 Low_Bound =>
15222 Make_Integer_Literal (Sloc (Mod_Expr), 0),
15223 High_Bound =>
15224 Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
15225
15226 -- Properly analyze the literals for the range. We do this manually
15227 -- because we can't go calling Resolve, since we are resolving these
15228 -- bounds with the type, and this type is certainly not complete yet!
15229
15230 Set_Etype (Low_Bound (Scalar_Range (T)), T);
15231 Set_Etype (High_Bound (Scalar_Range (T)), T);
15232 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
15233 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
15234
15235 -- Loop through powers of two to find number of bits required
15236
15237 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
15238
15239 -- Binary case
15240
15241 if M_Val = 2 ** Bits then
15242 Set_Modular_Size (Bits);
15243 return;
15244
15245 -- Non-binary case
15246
15247 elsif M_Val < 2 ** Bits then
15248 Set_Non_Binary_Modulus (T);
15249
15250 if Bits > System_Max_Nonbinary_Modulus_Power then
15251 Error_Msg_Uint_1 :=
15252 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
2b73cf68 15253 Error_Msg_F
996ae0b0
RK
15254 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
15255 Set_Modular_Size (System_Max_Binary_Modulus_Power);
15256 return;
15257
15258 else
71d9e9f2 15259 -- In the non-binary case, set size as per RM 13.3(55)
996ae0b0
RK
15260
15261 Set_Modular_Size (Bits);
15262 return;
15263 end if;
15264 end if;
15265
15266 end loop;
15267
15268 -- If we fall through, then the size exceed System.Max_Binary_Modulus
15269 -- so we just signal an error and set the maximum size.
15270
15271 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
2b73cf68 15272 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
996ae0b0
RK
15273
15274 Set_Modular_Size (System_Max_Binary_Modulus_Power);
15275 Init_Alignment (T);
dc06abec 15276
996ae0b0
RK
15277 end Modular_Type_Declaration;
15278
6c1e24d3
AC
15279 --------------------------
15280 -- New_Concatenation_Op --
15281 --------------------------
996ae0b0 15282
6c1e24d3 15283 procedure New_Concatenation_Op (Typ : Entity_Id) is
996ae0b0
RK
15284 Loc : constant Source_Ptr := Sloc (Typ);
15285 Op : Entity_Id;
15286
15287 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
15288 -- Create abbreviated declaration for the formal of a predefined
15289 -- Operator 'Op' of type 'Typ'
15290
15291 --------------------
15292 -- Make_Op_Formal --
15293 --------------------
15294
15295 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
15296 Formal : Entity_Id;
996ae0b0
RK
15297 begin
15298 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
15299 Set_Etype (Formal, Typ);
15300 Set_Mechanism (Formal, Default_Mechanism);
15301 return Formal;
15302 end Make_Op_Formal;
15303
6c1e24d3 15304 -- Start of processing for New_Concatenation_Op
996ae0b0
RK
15305
15306 begin
6c1e24d3 15307 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
996ae0b0
RK
15308
15309 Set_Ekind (Op, E_Operator);
15310 Set_Scope (Op, Current_Scope);
15311 Set_Etype (Op, Typ);
6c1e24d3 15312 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
996ae0b0
RK
15313 Set_Is_Immediately_Visible (Op);
15314 Set_Is_Intrinsic_Subprogram (Op);
15315 Set_Has_Completion (Op);
15316 Append_Entity (Op, Current_Scope);
15317
6c1e24d3 15318 Set_Name_Entity_Id (Name_Op_Concat, Op);
996ae0b0
RK
15319
15320 Append_Entity (Make_Op_Formal (Typ, Op), Op);
15321 Append_Entity (Make_Op_Formal (Typ, Op), Op);
6c1e24d3 15322 end New_Concatenation_Op;
996ae0b0 15323
88b32fc3
BD
15324 -------------------------
15325 -- OK_For_Limited_Init --
15326 -------------------------
15327
15328 -- ???Check all calls of this, and compare the conditions under which it's
15329 -- called.
15330
2a31c32b
AC
15331 function OK_For_Limited_Init
15332 (Typ : Entity_Id;
15333 Exp : Node_Id) return Boolean
15334 is
88b32fc3 15335 begin
236fecbf
JM
15336 return Is_CPP_Constructor_Call (Exp)
15337 or else (Ada_Version >= Ada_05
15338 and then not Debug_Flag_Dot_L
2a31c32b 15339 and then OK_For_Limited_Init_In_05 (Typ, Exp));
88b32fc3
BD
15340 end OK_For_Limited_Init;
15341
15342 -------------------------------
15343 -- OK_For_Limited_Init_In_05 --
15344 -------------------------------
15345
2a31c32b
AC
15346 function OK_For_Limited_Init_In_05
15347 (Typ : Entity_Id;
15348 Exp : Node_Id) return Boolean
15349 is
88b32fc3 15350 begin
2a31c32b
AC
15351 -- An object of a limited interface type can be initialized with any
15352 -- expression of a nonlimited descendant type.
15353
15354 if Is_Class_Wide_Type (Typ)
15355 and then Is_Limited_Interface (Typ)
15356 and then not Is_Limited_Type (Etype (Exp))
15357 then
15358 return True;
15359 end if;
15360
c6fe3827
GD
15361 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
15362 -- case of limited aggregates (including extension aggregates), and
15363 -- function calls. The function call may have been give in prefixed
2b73cf68 15364 -- notation, in which case the original node is an indexed component.
88b32fc3
BD
15365
15366 case Nkind (Original_Node (Exp)) is
2b73cf68 15367 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
88b32fc3
BD
15368 return True;
15369
71f62180
ES
15370 when N_Qualified_Expression =>
15371 return
2a31c32b
AC
15372 OK_For_Limited_Init_In_05
15373 (Typ, Expression (Original_Node (Exp)));
71f62180 15374
2b73cf68 15375 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
c6fe3827 15376 -- with a function call, the expander has rewritten the call into an
2b73cf68
JM
15377 -- N_Type_Conversion node to force displacement of the pointer to
15378 -- reference the component containing the secondary dispatch table.
71f62180 15379 -- Otherwise a type conversion is not a legal context.
e80d72ea
ES
15380 -- A return statement for a build-in-place function returning a
15381 -- synchronized type also introduces an unchecked conversion.
2b73cf68 15382
e80d72ea 15383 when N_Type_Conversion | N_Unchecked_Type_Conversion =>
71f62180
ES
15384 return not Comes_From_Source (Exp)
15385 and then
2a31c32b
AC
15386 OK_For_Limited_Init_In_05
15387 (Typ, Expression (Original_Node (Exp)));
88b32fc3 15388
dc06abec 15389 when N_Indexed_Component | N_Selected_Component =>
2b73cf68
JM
15390 return Nkind (Exp) = N_Function_Call;
15391
c6fe3827
GD
15392 -- A use of 'Input is a function call, hence allowed. Normally the
15393 -- attribute will be changed to a call, but the attribute by itself
15394 -- can occur with -gnatc.
15395
15396 when N_Attribute_Reference =>
15397 return Attribute_Name (Original_Node (Exp)) = Name_Input;
15398
88b32fc3
BD
15399 when others =>
15400 return False;
15401 end case;
15402 end OK_For_Limited_Init_In_05;
15403
996ae0b0
RK
15404 -------------------------------------------
15405 -- Ordinary_Fixed_Point_Type_Declaration --
15406 -------------------------------------------
15407
15408 procedure Ordinary_Fixed_Point_Type_Declaration
15409 (T : Entity_Id;
15410 Def : Node_Id)
15411 is
15412 Loc : constant Source_Ptr := Sloc (Def);
15413 Delta_Expr : constant Node_Id := Delta_Expression (Def);
15414 RRS : constant Node_Id := Real_Range_Specification (Def);
15415 Implicit_Base : Entity_Id;
15416 Delta_Val : Ureal;
15417 Small_Val : Ureal;
15418 Low_Val : Ureal;
15419 High_Val : Ureal;
15420
15421 begin
15422 Check_Restriction (No_Fixed_Point, Def);
15423
15424 -- Create implicit base type
15425
15426 Implicit_Base :=
15427 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
15428 Set_Etype (Implicit_Base, Implicit_Base);
15429
15430 -- Analyze and process delta expression
15431
15432 Analyze_And_Resolve (Delta_Expr, Any_Real);
15433
15434 Check_Delta_Expression (Delta_Expr);
15435 Delta_Val := Expr_Value_R (Delta_Expr);
15436
15437 Set_Delta_Value (Implicit_Base, Delta_Val);
15438
a5b62485
AC
15439 -- Compute default small from given delta, which is the largest power
15440 -- of two that does not exceed the given delta value.
996ae0b0
RK
15441
15442 declare
9dfd2ff8
CC
15443 Tmp : Ureal;
15444 Scale : Int;
996ae0b0
RK
15445
15446 begin
9dfd2ff8
CC
15447 Tmp := Ureal_1;
15448 Scale := 0;
15449
996ae0b0
RK
15450 if Delta_Val < Ureal_1 then
15451 while Delta_Val < Tmp loop
15452 Tmp := Tmp / Ureal_2;
15453 Scale := Scale + 1;
15454 end loop;
15455
15456 else
15457 loop
15458 Tmp := Tmp * Ureal_2;
15459 exit when Tmp > Delta_Val;
15460 Scale := Scale - 1;
15461 end loop;
15462 end if;
15463
15464 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
15465 end;
15466
15467 Set_Small_Value (Implicit_Base, Small_Val);
15468
15469 -- If no range was given, set a dummy range
15470
15471 if RRS <= Empty_Or_Error then
15472 Low_Val := -Small_Val;
15473 High_Val := Small_Val;
15474
15475 -- Otherwise analyze and process given range
15476
15477 else
15478 declare
15479 Low : constant Node_Id := Low_Bound (RRS);
15480 High : constant Node_Id := High_Bound (RRS);
15481
15482 begin
15483 Analyze_And_Resolve (Low, Any_Real);
15484 Analyze_And_Resolve (High, Any_Real);
15485 Check_Real_Bound (Low);
15486 Check_Real_Bound (High);
15487
15488 -- Obtain and set the range
15489
15490 Low_Val := Expr_Value_R (Low);
15491 High_Val := Expr_Value_R (High);
15492
15493 if Low_Val > High_Val then
15494 Error_Msg_NE ("?fixed point type& has null range", Def, T);
15495 end if;
15496 end;
15497 end if;
15498
a5b62485
AC
15499 -- The range for both the implicit base and the declared first subtype
15500 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
15501 -- set a temporary range in place. Note that the bounds of the base
15502 -- type will be widened to be symmetrical and to fill the available
15503 -- bits when the type is frozen.
996ae0b0
RK
15504
15505 -- We could do this with all discrete types, and probably should, but
15506 -- we absolutely have to do it for fixed-point, since the end-points
15507 -- of the range and the size are determined by the small value, which
15508 -- could be reset before the freeze point.
15509
15510 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
15511 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15512
996ae0b0
RK
15513 -- Complete definition of first subtype
15514
15515 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
15516 Set_Etype (T, Implicit_Base);
15517 Init_Size_Align (T);
15518 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15519 Set_Small_Value (T, Small_Val);
15520 Set_Delta_Value (T, Delta_Val);
15521 Set_Is_Constrained (T);
15522
15523 end Ordinary_Fixed_Point_Type_Declaration;
15524
15525 ----------------------------------------
15526 -- Prepare_Private_Subtype_Completion --
15527 ----------------------------------------
15528
15529 procedure Prepare_Private_Subtype_Completion
15530 (Id : Entity_Id;
15531 Related_Nod : Node_Id)
15532 is
15533 Id_B : constant Entity_Id := Base_Type (Id);
15534 Full_B : constant Entity_Id := Full_View (Id_B);
15535 Full : Entity_Id;
15536
15537 begin
15538 if Present (Full_B) then
15539
a5b62485
AC
15540 -- The Base_Type is already completed, we can complete the subtype
15541 -- now. We have to create a new entity with the same name, Thus we
15542 -- can't use Create_Itype.
15543
996ae0b0
RK
15544 -- This is messy, should be fixed ???
15545
15546 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
15547 Set_Is_Itype (Full);
15548 Set_Associated_Node_For_Itype (Full, Related_Nod);
15549 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
15550 end if;
15551
15552 -- The parent subtype may be private, but the base might not, in some
15553 -- nested instances. In that case, the subtype does not need to be
15554 -- exchanged. It would still be nice to make private subtypes and their
15555 -- bases consistent at all times ???
15556
15557 if Is_Private_Type (Id_B) then
15558 Append_Elmt (Id, Private_Dependents (Id_B));
15559 end if;
15560
15561 end Prepare_Private_Subtype_Completion;
15562
15563 ---------------------------
15564 -- Process_Discriminants --
15565 ---------------------------
15566
fbf5a39b
AC
15567 procedure Process_Discriminants
15568 (N : Node_Id;
15569 Prev : Entity_Id := Empty)
15570 is
15571 Elist : constant Elist_Id := New_Elmt_List;
996ae0b0
RK
15572 Id : Node_Id;
15573 Discr : Node_Id;
15574 Discr_Number : Uint;
15575 Discr_Type : Entity_Id;
15576 Default_Present : Boolean := False;
15577 Default_Not_Present : Boolean := False;
996ae0b0
RK
15578
15579 begin
15580 -- A composite type other than an array type can have discriminants.
996ae0b0
RK
15581 -- On entry, the current scope is the composite type.
15582
15583 -- The discriminants are initially entered into the scope of the type
15584 -- via Enter_Name with the default Ekind of E_Void to prevent premature
15585 -- use, as explained at the end of this procedure.
15586
15587 Discr := First (Discriminant_Specifications (N));
15588 while Present (Discr) loop
15589 Enter_Name (Defining_Identifier (Discr));
15590
fbf5a39b
AC
15591 -- For navigation purposes we add a reference to the discriminant
15592 -- in the entity for the type. If the current declaration is a
15593 -- completion, place references on the partial view. Otherwise the
15594 -- type is the current scope.
15595
15596 if Present (Prev) then
15597
15598 -- The references go on the partial view, if present. If the
15599 -- partial view has discriminants, the references have been
15600 -- generated already.
15601
15602 if not Has_Discriminants (Prev) then
15603 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
15604 end if;
15605 else
15606 Generate_Reference
15607 (Current_Scope, Defining_Identifier (Discr), 'd');
15608 end if;
15609
996ae0b0 15610 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
57193e09 15611 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
996ae0b0 15612
0ab80019 15613 -- Ada 2005 (AI-254)
7324bf49
AC
15614
15615 if Present (Access_To_Subprogram_Definition
15616 (Discriminant_Type (Discr)))
15617 and then Protected_Present (Access_To_Subprogram_Definition
15618 (Discriminant_Type (Discr)))
15619 then
15620 Discr_Type :=
fea9e956 15621 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
7324bf49
AC
15622 end if;
15623
996ae0b0
RK
15624 else
15625 Find_Type (Discriminant_Type (Discr));
15626 Discr_Type := Etype (Discriminant_Type (Discr));
15627
15628 if Error_Posted (Discriminant_Type (Discr)) then
15629 Discr_Type := Any_Type;
15630 end if;
15631 end if;
15632
15633 if Is_Access_Type (Discr_Type) then
6e937c1c 15634
0ab80019 15635 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
6e937c1c
AC
15636 -- record types
15637
0ab80019 15638 if Ada_Version < Ada_05 then
6e937c1c
AC
15639 Check_Access_Discriminant_Requires_Limited
15640 (Discr, Discriminant_Type (Discr));
15641 end if;
996ae0b0 15642
0ab80019 15643 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
996ae0b0
RK
15644 Error_Msg_N
15645 ("(Ada 83) access discriminant not allowed", Discr);
15646 end if;
15647
15648 elsif not Is_Discrete_Type (Discr_Type) then
15649 Error_Msg_N ("discriminants must have a discrete or access type",
15650 Discriminant_Type (Discr));
15651 end if;
15652
15653 Set_Etype (Defining_Identifier (Discr), Discr_Type);
15654
15655 -- If a discriminant specification includes the assignment compound
15656 -- delimiter followed by an expression, the expression is the default
15657 -- expression of the discriminant; the default expression must be of
15658 -- the type of the discriminant. (RM 3.7.1) Since this expression is
15659 -- a default expression, we do the special preanalysis, since this
fbf5a39b
AC
15660 -- expression does not freeze (see "Handling of Default and Per-
15661 -- Object Expressions" in spec of package Sem).
996ae0b0
RK
15662
15663 if Present (Expression (Discr)) then
ce4a6e84 15664 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
996ae0b0
RK
15665
15666 if Nkind (N) = N_Formal_Type_Declaration then
15667 Error_Msg_N
15668 ("discriminant defaults not allowed for formal type",
15669 Expression (Discr));
15670
7324bf49
AC
15671 -- Tagged types cannot have defaulted discriminants, but a
15672 -- non-tagged private type with defaulted discriminants
15673 -- can have a tagged completion.
15674
15675 elsif Is_Tagged_Type (Current_Scope)
15676 and then Comes_From_Source (N)
15677 then
996ae0b0
RK
15678 Error_Msg_N
15679 ("discriminants of tagged type cannot have defaults",
15680 Expression (Discr));
15681
15682 else
15683 Default_Present := True;
15684 Append_Elmt (Expression (Discr), Elist);
15685
15686 -- Tag the defining identifiers for the discriminants with
15687 -- their corresponding default expressions from the tree.
15688
15689 Set_Discriminant_Default_Value
15690 (Defining_Identifier (Discr), Expression (Discr));
15691 end if;
15692
15693 else
15694 Default_Not_Present := True;
15695 end if;
15696
9dfd2ff8
CC
15697 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
15698 -- Discr_Type but with the null-exclusion attribute
15699
15700 if Ada_Version >= Ada_05 then
15701
15702 -- Ada 2005 (AI-231): Static checks
15703
15704 if Can_Never_Be_Null (Discr_Type) then
15705 Null_Exclusion_Static_Checks (Discr);
15706
15707 elsif Is_Access_Type (Discr_Type)
15708 and then Null_Exclusion_Present (Discr)
15709
15710 -- No need to check itypes because in their case this check
15711 -- was done at their point of creation
15712
15713 and then not Is_Itype (Discr_Type)
15714 then
15715 if Can_Never_Be_Null (Discr_Type) then
2b73cf68
JM
15716 Error_Msg_NE
15717 ("`NOT NULL` not allowed (& already excludes null)",
15718 Discr,
15719 Discr_Type);
9dfd2ff8
CC
15720 end if;
15721
15722 Set_Etype (Defining_Identifier (Discr),
15723 Create_Null_Excluding_Itype
15724 (T => Discr_Type,
15725 Related_Nod => Discr));
fa961f76
ES
15726
15727 -- Check for improper null exclusion if the type is otherwise
15728 -- legal for a discriminant.
15729
15730 elsif Null_Exclusion_Present (Discr)
15731 and then Is_Discrete_Type (Discr_Type)
15732 then
15733 Error_Msg_N
15734 ("null exclusion can only apply to an access type", Discr);
9dfd2ff8 15735 end if;
2820d220 15736
88b32fc3 15737 -- Ada 2005 (AI-402): access discriminants of nonlimited types
ce4a6e84
RD
15738 -- can't have defaults. Synchronized types, or types that are
15739 -- explicitly limited are fine, but special tests apply to derived
15740 -- types in generics: in a generic body we have to assume the
15741 -- worst, and therefore defaults are not allowed if the parent is
15742 -- a generic formal private type (see ACATS B370001).
88b32fc3
BD
15743
15744 if Is_Access_Type (Discr_Type) then
15745 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
15746 or else not Default_Present
15747 or else Is_Limited_Record (Current_Scope)
15748 or else Is_Concurrent_Type (Current_Scope)
15749 or else Is_Concurrent_Record_Type (Current_Scope)
15750 or else Ekind (Current_Scope) = E_Limited_Private_Type
15751 then
ce4a6e84
RD
15752 if not Is_Derived_Type (Current_Scope)
15753 or else not Is_Generic_Type (Etype (Current_Scope))
15754 or else not In_Package_Body (Scope (Etype (Current_Scope)))
15755 or else Limited_Present
15756 (Type_Definition (Parent (Current_Scope)))
15757 then
15758 null;
15759
15760 else
15761 Error_Msg_N ("access discriminants of nonlimited types",
15762 Expression (Discr));
15763 Error_Msg_N ("\cannot have defaults", Expression (Discr));
15764 end if;
dc06abec
RD
15765
15766 elsif Present (Expression (Discr)) then
88b32fc3
BD
15767 Error_Msg_N
15768 ("(Ada 2005) access discriminants of nonlimited types",
15769 Expression (Discr));
15770 Error_Msg_N ("\cannot have defaults", Expression (Discr));
15771 end if;
15772 end if;
2820d220
AC
15773 end if;
15774
996ae0b0
RK
15775 Next (Discr);
15776 end loop;
15777
15778 -- An element list consisting of the default expressions of the
15779 -- discriminants is constructed in the above loop and used to set
15780 -- the Discriminant_Constraint attribute for the type. If an object
15781 -- is declared of this (record or task) type without any explicit
15782 -- discriminant constraint given, this element list will form the
15783 -- actual parameters for the corresponding initialization procedure
15784 -- for the type.
15785
15786 Set_Discriminant_Constraint (Current_Scope, Elist);
fbf5a39b 15787 Set_Stored_Constraint (Current_Scope, No_Elist);
996ae0b0
RK
15788
15789 -- Default expressions must be provided either for all or for none
15790 -- of the discriminants of a discriminant part. (RM 3.7.1)
15791
15792 if Default_Present and then Default_Not_Present then
15793 Error_Msg_N
15794 ("incomplete specification of defaults for discriminants", N);
15795 end if;
15796
15797 -- The use of the name of a discriminant is not allowed in default
15798 -- expressions of a discriminant part if the specification of the
15799 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
15800
15801 -- To detect this, the discriminant names are entered initially with an
15802 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
15803 -- attempt to use a void entity (for example in an expression that is
15804 -- type-checked) produces the error message: premature usage. Now after
15805 -- completing the semantic analysis of the discriminant part, we can set
15806 -- the Ekind of all the discriminants appropriately.
15807
15808 Discr := First (Discriminant_Specifications (N));
15809 Discr_Number := Uint_1;
996ae0b0
RK
15810 while Present (Discr) loop
15811 Id := Defining_Identifier (Discr);
15812 Set_Ekind (Id, E_Discriminant);
15813 Init_Component_Location (Id);
15814 Init_Esize (Id);
15815 Set_Discriminant_Number (Id, Discr_Number);
15816
15817 -- Make sure this is always set, even in illegal programs
15818
15819 Set_Corresponding_Discriminant (Id, Empty);
15820
15821 -- Initialize the Original_Record_Component to the entity itself.
15822 -- Inherit_Components will propagate the right value to
15823 -- discriminants in derived record types.
15824
15825 Set_Original_Record_Component (Id, Id);
15826
ffe9aba8 15827 -- Create the discriminal for the discriminant
996ae0b0
RK
15828
15829 Build_Discriminal (Id);
15830
15831 Next (Discr);
15832 Discr_Number := Discr_Number + 1;
15833 end loop;
15834
15835 Set_Has_Discriminants (Current_Scope);
15836 end Process_Discriminants;
15837
15838 -----------------------
15839 -- Process_Full_View --
15840 -----------------------
15841
15842 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
15843 Priv_Parent : Entity_Id;
15844 Full_Parent : Entity_Id;
15845 Full_Indic : Node_Id;
15846
653da906
RD
15847 procedure Collect_Implemented_Interfaces
15848 (Typ : Entity_Id;
15849 Ifaces : Elist_Id);
15850 -- Ada 2005: Gather all the interfaces that Typ directly or
15851 -- inherently implements. Duplicate entries are not added to
15852 -- the list Ifaces.
15853
653da906
RD
15854 ------------------------------------
15855 -- Collect_Implemented_Interfaces --
15856 ------------------------------------
758c442c 15857
653da906
RD
15858 procedure Collect_Implemented_Interfaces
15859 (Typ : Entity_Id;
15860 Ifaces : Elist_Id)
758c442c 15861 is
653da906
RD
15862 Iface : Entity_Id;
15863 Iface_Elmt : Elmt_Id;
758c442c
GD
15864
15865 begin
57193e09
TQ
15866 -- Abstract interfaces are only associated with tagged record types
15867
15868 if not Is_Tagged_Type (Typ)
15869 or else not Is_Record_Type (Typ)
15870 then
15871 return;
15872 end if;
15873
88b32fc3
BD
15874 -- Recursively climb to the ancestors
15875
15876 if Etype (Typ) /= Typ
15877
15878 -- Protect the frontend against wrong cyclic declarations like:
758c442c 15879
88b32fc3
BD
15880 -- type B is new A with private;
15881 -- type C is new A with private;
15882 -- private
15883 -- type B is new C with null record;
15884 -- type C is new B with null record;
15885
15886 and then Etype (Typ) /= Priv_T
15887 and then Etype (Typ) /= Full_T
653da906 15888 then
88b32fc3
BD
15889 -- Keep separate the management of private type declarations
15890
15891 if Ekind (Typ) = E_Record_Type_With_Private then
15892
15893 -- Handle the following erronous case:
15894 -- type Private_Type is tagged private;
15895 -- private
15896 -- type Private_Type is new Type_Implementing_Iface;
15897
15898 if Present (Full_View (Typ))
15899 and then Etype (Typ) /= Full_View (Typ)
15900 then
dc06abec
RD
15901 if Is_Interface (Etype (Typ)) then
15902 Append_Unique_Elmt (Etype (Typ), Ifaces);
88b32fc3
BD
15903 end if;
15904
15905 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15906 end if;
15907
15908 -- Non-private types
15909
15910 else
dc06abec
RD
15911 if Is_Interface (Etype (Typ)) then
15912 Append_Unique_Elmt (Etype (Typ), Ifaces);
88b32fc3
BD
15913 end if;
15914
15915 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15916 end if;
653da906 15917 end if;
9dfd2ff8 15918
88b32fc3 15919 -- Handle entities in the list of abstract interfaces
9dfd2ff8 15920
ce2b6ba5
JM
15921 if Present (Interfaces (Typ)) then
15922 Iface_Elmt := First_Elmt (Interfaces (Typ));
653da906
RD
15923 while Present (Iface_Elmt) loop
15924 Iface := Node (Iface_Elmt);
15925
57193e09
TQ
15926 pragma Assert (Is_Interface (Iface));
15927
15928 if not Contain_Interface (Iface, Ifaces) then
653da906 15929 Append_Elmt (Iface, Ifaces);
57193e09 15930 Collect_Implemented_Interfaces (Iface, Ifaces);
653da906
RD
15931 end if;
15932
15933 Next_Elmt (Iface_Elmt);
15934 end loop;
15935 end if;
653da906
RD
15936 end Collect_Implemented_Interfaces;
15937
758c442c
GD
15938 -- Start of processing for Process_Full_View
15939
996ae0b0
RK
15940 begin
15941 -- First some sanity checks that must be done after semantic
15942 -- decoration of the full view and thus cannot be placed with other
15943 -- similar checks in Find_Type_Name
15944
15945 if not Is_Limited_Type (Priv_T)
15946 and then (Is_Limited_Type (Full_T)
15947 or else Is_Limited_Composite (Full_T))
15948 then
15949 Error_Msg_N
15950 ("completion of nonlimited type cannot be limited", Full_T);
fbf5a39b 15951 Explain_Limited_Type (Full_T, Full_T);
996ae0b0 15952
fea9e956
ES
15953 elsif Is_Abstract_Type (Full_T)
15954 and then not Is_Abstract_Type (Priv_T)
15955 then
996ae0b0
RK
15956 Error_Msg_N
15957 ("completion of nonabstract type cannot be abstract", Full_T);
15958
15959 elsif Is_Tagged_Type (Priv_T)
15960 and then Is_Limited_Type (Priv_T)
15961 and then not Is_Limited_Type (Full_T)
15962 then
dc06abec
RD
15963 -- If pragma CPP_Class was applied to the private declaration
15964 -- propagate the limitedness to the full-view
15965
15966 if Is_CPP_Class (Priv_T) then
15967 Set_Is_Limited_Record (Full_T);
15968
996ae0b0
RK
15969 -- GNAT allow its own definition of Limited_Controlled to disobey
15970 -- this rule in order in ease the implementation. The next test is
15971 -- safe because Root_Controlled is defined in a private system child
15972
dc06abec 15973 elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
996ae0b0
RK
15974 Set_Is_Limited_Composite (Full_T);
15975 else
15976 Error_Msg_N
15977 ("completion of limited tagged type must be limited", Full_T);
15978 end if;
15979
15980 elsif Is_Generic_Type (Priv_T) then
15981 Error_Msg_N ("generic type cannot have a completion", Full_T);
15982 end if;
15983
88b32fc3
BD
15984 -- Check that ancestor interfaces of private and full views are
15985 -- consistent. We omit this check for synchronized types because
fea9e956 15986 -- they are performed on the corresponding record type when frozen.
88b32fc3 15987
758c442c 15988 if Ada_Version >= Ada_05
653da906 15989 and then Is_Tagged_Type (Priv_T)
758c442c 15990 and then Is_Tagged_Type (Full_T)
fea9e956 15991 and then not Is_Concurrent_Type (Full_T)
758c442c
GD
15992 then
15993 declare
653da906
RD
15994 Iface : Entity_Id;
15995 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
15996 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
758c442c
GD
15997
15998 begin
653da906
RD
15999 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
16000 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
758c442c 16001
57193e09
TQ
16002 -- Ada 2005 (AI-251): The partial view shall be a descendant of
16003 -- an interface type if and only if the full type is descendant
16004 -- of the interface type (AARM 7.3 (7.3/2).
16005
16006 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
16007
16008 if Present (Iface) then
16009 Error_Msg_NE ("interface & not implemented by full type " &
dc06abec 16010 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
57193e09 16011 end if;
758c442c 16012
653da906 16013 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
758c442c 16014
653da906
RD
16015 if Present (Iface) then
16016 Error_Msg_NE ("interface & not implemented by partial view " &
dc06abec 16017 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
758c442c
GD
16018 end if;
16019 end;
16020 end if;
16021
996ae0b0
RK
16022 if Is_Tagged_Type (Priv_T)
16023 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
16024 and then Is_Derived_Type (Full_T)
16025 then
16026 Priv_Parent := Etype (Priv_T);
16027
16028 -- The full view of a private extension may have been transformed
16029 -- into an unconstrained derived type declaration and a subtype
16030 -- declaration (see build_derived_record_type for details).
16031
16032 if Nkind (N) = N_Subtype_Declaration then
16033 Full_Indic := Subtype_Indication (N);
16034 Full_Parent := Etype (Base_Type (Full_T));
16035 else
16036 Full_Indic := Subtype_Indication (Type_Definition (N));
16037 Full_Parent := Etype (Full_T);
16038 end if;
16039
16040 -- Check that the parent type of the full type is a descendant of
16041 -- the ancestor subtype given in the private extension. If either
16042 -- entity has an Etype equal to Any_Type then we had some previous
16043 -- error situation [7.3(8)].
16044
16045 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
16046 return;
16047
653da906
RD
16048 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
16049 -- any order. Therefore we don't have to check that its parent must
16050 -- be a descendant of the parent of the private type declaration.
16051
16052 elsif Is_Interface (Priv_Parent)
16053 and then Is_Interface (Full_Parent)
16054 then
16055 null;
16056
57193e09
TQ
16057 -- Ada 2005 (AI-251): If the parent of the private type declaration
16058 -- is an interface there is no need to check that it is an ancestor
16059 -- of the associated full type declaration. The required tests for
16b05213 16060 -- this case are performed by Build_Derived_Record_Type.
57193e09
TQ
16061
16062 elsif not Is_Interface (Base_Type (Priv_Parent))
16063 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
16064 then
950d3e7d
ES
16065 Error_Msg_N
16066 ("parent of full type must descend from parent"
16067 & " of private extension", Full_Indic);
996ae0b0
RK
16068
16069 -- Check the rules of 7.3(10): if the private extension inherits
16070 -- known discriminants, then the full type must also inherit those
16071 -- discriminants from the same (ancestor) type, and the parent
16072 -- subtype of the full type must be constrained if and only if
16073 -- the ancestor subtype of the private extension is constrained.
16074
57193e09 16075 elsif No (Discriminant_Specifications (Parent (Priv_T)))
996ae0b0
RK
16076 and then not Has_Unknown_Discriminants (Priv_T)
16077 and then Has_Discriminants (Base_Type (Priv_Parent))
16078 then
16079 declare
16080 Priv_Indic : constant Node_Id :=
16081 Subtype_Indication (Parent (Priv_T));
16082
16083 Priv_Constr : constant Boolean :=
16084 Is_Constrained (Priv_Parent)
16085 or else
16086 Nkind (Priv_Indic) = N_Subtype_Indication
16087 or else Is_Constrained (Entity (Priv_Indic));
16088
16089 Full_Constr : constant Boolean :=
16090 Is_Constrained (Full_Parent)
16091 or else
16092 Nkind (Full_Indic) = N_Subtype_Indication
16093 or else Is_Constrained (Entity (Full_Indic));
16094
16095 Priv_Discr : Entity_Id;
16096 Full_Discr : Entity_Id;
16097
16098 begin
16099 Priv_Discr := First_Discriminant (Priv_Parent);
16100 Full_Discr := First_Discriminant (Full_Parent);
996ae0b0
RK
16101 while Present (Priv_Discr) and then Present (Full_Discr) loop
16102 if Original_Record_Component (Priv_Discr) =
16103 Original_Record_Component (Full_Discr)
16104 or else
16105 Corresponding_Discriminant (Priv_Discr) =
16106 Corresponding_Discriminant (Full_Discr)
16107 then
16108 null;
16109 else
16110 exit;
16111 end if;
16112
16113 Next_Discriminant (Priv_Discr);
16114 Next_Discriminant (Full_Discr);
16115 end loop;
16116
16117 if Present (Priv_Discr) or else Present (Full_Discr) then
16118 Error_Msg_N
16119 ("full view must inherit discriminants of the parent type"
16120 & " used in the private extension", Full_Indic);
16121
16122 elsif Priv_Constr and then not Full_Constr then
16123 Error_Msg_N
16124 ("parent subtype of full type must be constrained",
16125 Full_Indic);
16126
16127 elsif Full_Constr and then not Priv_Constr then
16128 Error_Msg_N
16129 ("parent subtype of full type must be unconstrained",
16130 Full_Indic);
16131 end if;
16132 end;
16133
16134 -- Check the rules of 7.3(12): if a partial view has neither known
16135 -- or unknown discriminants, then the full type declaration shall
16136 -- define a definite subtype.
16137
16138 elsif not Has_Unknown_Discriminants (Priv_T)
16139 and then not Has_Discriminants (Priv_T)
16140 and then not Is_Constrained (Full_T)
16141 then
16142 Error_Msg_N
16143 ("full view must define a constrained type if partial view"
758c442c 16144 & " has no discriminants", Full_T);
996ae0b0
RK
16145 end if;
16146
16147 -- ??????? Do we implement the following properly ?????
16148 -- If the ancestor subtype of a private extension has constrained
16149 -- discriminants, then the parent subtype of the full view shall
16150 -- impose a statically matching constraint on those discriminants
16151 -- [7.3(13)].
16152
16153 else
16154 -- For untagged types, verify that a type without discriminants
16155 -- is not completed with an unconstrained type.
16156
16157 if not Is_Indefinite_Subtype (Priv_T)
16158 and then Is_Indefinite_Subtype (Full_T)
16159 then
16160 Error_Msg_N ("full view of type must be definite subtype", Full_T);
16161 end if;
16162 end if;
16163
653da906
RD
16164 -- AI-419: verify that the use of "limited" is consistent
16165
16166 declare
16167 Orig_Decl : constant Node_Id := Original_Node (N);
88b32fc3 16168
653da906
RD
16169 begin
16170 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
16171 and then not Limited_Present (Parent (Priv_T))
88b32fc3 16172 and then not Synchronized_Present (Parent (Priv_T))
653da906
RD
16173 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
16174 and then Nkind
16175 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
16176 and then Limited_Present (Type_Definition (Orig_Decl))
16177 then
16178 Error_Msg_N
16179 ("full view of non-limited extension cannot be limited", N);
16180 end if;
16181 end;
16182
88b32fc3
BD
16183 -- Ada 2005 (AI-443): A synchronized private extension must be
16184 -- completed by a task or protected type.
16185
16186 if Ada_Version >= Ada_05
16187 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
16188 and then Synchronized_Present (Parent (Priv_T))
fea9e956 16189 and then not Is_Concurrent_Type (Full_T)
88b32fc3
BD
16190 then
16191 Error_Msg_N ("full view of synchronized extension must " &
16192 "be synchronized type", N);
16193 end if;
16194
758c442c
GD
16195 -- Ada 2005 AI-363: if the full view has discriminants with
16196 -- defaults, it is illegal to declare constrained access subtypes
16197 -- whose designated type is the current type. This allows objects
16198 -- of the type that are declared in the heap to be unconstrained.
16199
16200 if not Has_Unknown_Discriminants (Priv_T)
16201 and then not Has_Discriminants (Priv_T)
16202 and then Has_Discriminants (Full_T)
16203 and then
88b32fc3 16204 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
758c442c
GD
16205 then
16206 Set_Has_Constrained_Partial_View (Full_T);
16207 Set_Has_Constrained_Partial_View (Priv_T);
16208 end if;
16209
996ae0b0 16210 -- Create a full declaration for all its subtypes recorded in
a5b62485
AC
16211 -- Private_Dependents and swap them similarly to the base type. These
16212 -- are subtypes that have been define before the full declaration of
16213 -- the private type. We also swap the entry in Private_Dependents list
16214 -- so we can properly restore the private view on exit from the scope.
996ae0b0
RK
16215
16216 declare
16217 Priv_Elmt : Elmt_Id;
16218 Priv : Entity_Id;
16219 Full : Entity_Id;
16220
16221 begin
16222 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
16223 while Present (Priv_Elmt) loop
16224 Priv := Node (Priv_Elmt);
16225
16226 if Ekind (Priv) = E_Private_Subtype
16227 or else Ekind (Priv) = E_Limited_Private_Subtype
16228 or else Ekind (Priv) = E_Record_Subtype_With_Private
16229 then
16230 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
16231 Set_Is_Itype (Full);
16232 Set_Parent (Full, Parent (Priv));
16233 Set_Associated_Node_For_Itype (Full, N);
16234
16235 -- Now we need to complete the private subtype, but since the
16236 -- base type has already been swapped, we must also swap the
16237 -- subtypes (and thus, reverse the arguments in the call to
16238 -- Complete_Private_Subtype).
16239
16240 Copy_And_Swap (Priv, Full);
16241 Complete_Private_Subtype (Full, Priv, Full_T, N);
16242 Replace_Elmt (Priv_Elmt, Full);
16243 end if;
16244
16245 Next_Elmt (Priv_Elmt);
16246 end loop;
16247 end;
16248
2b73cf68
JM
16249 -- If the private view was tagged, copy the new primitive operations
16250 -- from the private view to the full view.
996ae0b0 16251
d44202ba 16252 if Is_Tagged_Type (Full_T) then
996ae0b0 16253 declare
d44202ba
HK
16254 Disp_Typ : Entity_Id;
16255 Full_List : Elist_Id;
996ae0b0 16256 Prim : Entity_Id;
d44202ba
HK
16257 Prim_Elmt : Elmt_Id;
16258 Priv_List : Elist_Id;
16259
16260 function Contains
16261 (E : Entity_Id;
16262 L : Elist_Id) return Boolean;
16263 -- Determine whether list L contains element E
16264
16265 --------------
16266 -- Contains --
16267 --------------
16268
16269 function Contains
16270 (E : Entity_Id;
16271 L : Elist_Id) return Boolean
16272 is
16273 List_Elmt : Elmt_Id;
16274
16275 begin
16276 List_Elmt := First_Elmt (L);
16277 while Present (List_Elmt) loop
16278 if Node (List_Elmt) = E then
16279 return True;
16280 end if;
16281
16282 Next_Elmt (List_Elmt);
16283 end loop;
16284
16285 return False;
16286 end Contains;
16287
16288 -- Start of processing
996ae0b0
RK
16289
16290 begin
16291 if Is_Tagged_Type (Priv_T) then
16292 Priv_List := Primitive_Operations (Priv_T);
d44202ba
HK
16293 Prim_Elmt := First_Elmt (Priv_List);
16294
16295 -- In the case of a concurrent type completing a private tagged
16b05213 16296 -- type, primitives may have been declared in between the two
d44202ba
HK
16297 -- views. These subprograms need to be wrapped the same way
16298 -- entries and protected procedures are handled because they
16299 -- cannot be directly shared by the two views.
16300
16301 if Is_Concurrent_Type (Full_T) then
16302 declare
16303 Conc_Typ : constant Entity_Id :=
16304 Corresponding_Record_Type (Full_T);
d44202ba
HK
16305 Curr_Nod : Node_Id := Parent (Conc_Typ);
16306 Wrap_Spec : Node_Id;
996ae0b0 16307
d44202ba
HK
16308 begin
16309 while Present (Prim_Elmt) loop
16310 Prim := Node (Prim_Elmt);
996ae0b0 16311
d44202ba
HK
16312 if Comes_From_Source (Prim)
16313 and then not Is_Abstract_Subprogram (Prim)
16314 then
16315 Wrap_Spec :=
eb9cb0fc 16316 Make_Subprogram_Declaration (Sloc (Prim),
d44202ba 16317 Specification =>
eb9cb0fc
ES
16318 Build_Wrapper_Spec
16319 (Subp_Id => Prim,
16320 Obj_Typ => Conc_Typ,
16321 Formals =>
16322 Parameter_Specifications (
16323 Parent (Prim))));
d44202ba
HK
16324
16325 Insert_After (Curr_Nod, Wrap_Spec);
16326 Curr_Nod := Wrap_Spec;
16327
16328 Analyze (Wrap_Spec);
16329 end if;
996ae0b0 16330
d44202ba 16331 Next_Elmt (Prim_Elmt);
996ae0b0
RK
16332 end loop;
16333
d44202ba
HK
16334 return;
16335 end;
16336
16337 -- For non-concurrent types, transfer explicit primitives, but
16338 -- omit those inherited from the parent of the private view
16339 -- since they will be re-inherited later on.
16340
16341 else
16342 Full_List := Primitive_Operations (Full_T);
16343
16344 while Present (Prim_Elmt) loop
16345 Prim := Node (Prim_Elmt);
996ae0b0 16346
d44202ba
HK
16347 if Comes_From_Source (Prim)
16348 and then not Contains (Prim, Full_List)
16349 then
996ae0b0
RK
16350 Append_Elmt (Prim, Full_List);
16351 end if;
996ae0b0 16352
d44202ba
HK
16353 Next_Elmt (Prim_Elmt);
16354 end loop;
16355 end if;
16356
16357 -- Untagged private view
996ae0b0
RK
16358
16359 else
d44202ba
HK
16360 Full_List := Primitive_Operations (Full_T);
16361
88b32fc3
BD
16362 -- In this case the partial view is untagged, so here we locate
16363 -- all of the earlier primitives that need to be treated as
16364 -- dispatching (those that appear between the two views). Note
16365 -- that these additional operations must all be new operations
16366 -- (any earlier operations that override inherited operations
16367 -- of the full view will already have been inserted in the
16368 -- primitives list, marked by Check_Operation_From_Private_View
16369 -- as dispatching. Note that implicit "/=" operators are
16370 -- excluded from being added to the primitives list since they
16371 -- shouldn't be treated as dispatching (tagged "/=" is handled
16372 -- specially).
996ae0b0
RK
16373
16374 Prim := Next_Entity (Full_T);
16375 while Present (Prim) and then Prim /= Priv_T loop
fbf5a39b
AC
16376 if Ekind (Prim) = E_Procedure
16377 or else
16378 Ekind (Prim) = E_Function
996ae0b0 16379 then
d44202ba 16380 Disp_Typ := Find_Dispatching_Type (Prim);
996ae0b0 16381
d44202ba 16382 if Disp_Typ = Full_T
996ae0b0
RK
16383 and then (Chars (Prim) /= Name_Op_Ne
16384 or else Comes_From_Source (Prim))
16385 then
16386 Check_Controlling_Formals (Full_T, Prim);
16387
16388 if not Is_Dispatching_Operation (Prim) then
16389 Append_Elmt (Prim, Full_List);
16390 Set_Is_Dispatching_Operation (Prim, True);
16391 Set_DT_Position (Prim, No_Uint);
16392 end if;
16393
16394 elsif Is_Dispatching_Operation (Prim)
d44202ba 16395 and then Disp_Typ /= Full_T
996ae0b0
RK
16396 then
16397
88b32fc3
BD
16398 -- Verify that it is not otherwise controlled by a
16399 -- formal or a return value of type T.
996ae0b0 16400
d44202ba 16401 Check_Controlling_Formals (Disp_Typ, Prim);
996ae0b0
RK
16402 end if;
16403 end if;
16404
16405 Next_Entity (Prim);
16406 end loop;
16407 end if;
16408
16409 -- For the tagged case, the two views can share the same
16410 -- Primitive Operation list and the same class wide type.
16411 -- Update attributes of the class-wide type which depend on
16412 -- the full declaration.
16413
16414 if Is_Tagged_Type (Priv_T) then
16415 Set_Primitive_Operations (Priv_T, Full_List);
16416 Set_Class_Wide_Type
16417 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
16418
996ae0b0 16419 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
996ae0b0
RK
16420 end if;
16421 end;
16422 end if;
88b32fc3
BD
16423
16424 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
16425
16426 if Known_To_Have_Preelab_Init (Priv_T) then
16427
16428 -- Case where there is a pragma Preelaborable_Initialization. We
16429 -- always allow this in predefined units, which is a bit of a kludge,
16430 -- but it means we don't have to struggle to meet the requirements in
16431 -- the RM for having Preelaborable Initialization. Otherwise we
16432 -- require that the type meets the RM rules. But we can't check that
16433 -- yet, because of the rule about overriding Ininitialize, so we
16434 -- simply set a flag that will be checked at freeze time.
16435
16436 if not In_Predefined_Unit (Full_T) then
16437 Set_Must_Have_Preelab_Init (Full_T);
16438 end if;
16439 end if;
2b73cf68
JM
16440
16441 -- If pragma CPP_Class was applied to the private type declaration,
16442 -- propagate it now to the full type declaration.
16443
16444 if Is_CPP_Class (Priv_T) then
16445 Set_Is_CPP_Class (Full_T);
16446 Set_Convention (Full_T, Convention_CPP);
16447 end if;
23c4ff9b
AC
16448
16449 -- If the private view has user specified stream attributes, then so has
16450 -- the full view.
16451
16452 if Has_Specified_Stream_Read (Priv_T) then
16453 Set_Has_Specified_Stream_Read (Full_T);
16454 end if;
16455 if Has_Specified_Stream_Write (Priv_T) then
16456 Set_Has_Specified_Stream_Write (Full_T);
16457 end if;
16458 if Has_Specified_Stream_Input (Priv_T) then
16459 Set_Has_Specified_Stream_Input (Full_T);
16460 end if;
16461 if Has_Specified_Stream_Output (Priv_T) then
16462 Set_Has_Specified_Stream_Output (Full_T);
16463 end if;
996ae0b0
RK
16464 end Process_Full_View;
16465
16466 -----------------------------------
16467 -- Process_Incomplete_Dependents --
16468 -----------------------------------
16469
16470 procedure Process_Incomplete_Dependents
16471 (N : Node_Id;
16472 Full_T : Entity_Id;
16473 Inc_T : Entity_Id)
16474 is
16475 Inc_Elmt : Elmt_Id;
16476 Priv_Dep : Entity_Id;
16477 New_Subt : Entity_Id;
16478
16479 Disc_Constraint : Elist_Id;
16480
16481 begin
16482 if No (Private_Dependents (Inc_T)) then
16483 return;
996ae0b0
RK
16484 end if;
16485
9dfd2ff8
CC
16486 -- Itypes that may be generated by the completion of an incomplete
16487 -- subtype are not used by the back-end and not attached to the tree.
16488 -- They are created only for constraint-checking purposes.
16489
16490 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
996ae0b0
RK
16491 while Present (Inc_Elmt) loop
16492 Priv_Dep := Node (Inc_Elmt);
16493
16494 if Ekind (Priv_Dep) = E_Subprogram_Type then
16495
16496 -- An Access_To_Subprogram type may have a return type or a
16497 -- parameter type that is incomplete. Replace with the full view.
16498
16499 if Etype (Priv_Dep) = Inc_T then
16500 Set_Etype (Priv_Dep, Full_T);
16501 end if;
16502
16503 declare
16504 Formal : Entity_Id;
16505
16506 begin
16507 Formal := First_Formal (Priv_Dep);
996ae0b0 16508 while Present (Formal) loop
996ae0b0
RK
16509 if Etype (Formal) = Inc_T then
16510 Set_Etype (Formal, Full_T);
16511 end if;
16512
16513 Next_Formal (Formal);
16514 end loop;
16515 end;
16516
9dfd2ff8 16517 elsif Is_Overloadable (Priv_Dep) then
996ae0b0 16518
9dfd2ff8
CC
16519 -- A protected operation is never dispatching: only its
16520 -- wrapper operation (which has convention Ada) is.
16521
16522 if Is_Tagged_Type (Full_T)
16523 and then Convention (Priv_Dep) /= Convention_Protected
16524 then
996ae0b0
RK
16525
16526 -- Subprogram has an access parameter whose designated type
16527 -- was incomplete. Reexamine declaration now, because it may
16528 -- be a primitive operation of the full type.
16529
16530 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
16531 Set_Is_Dispatching_Operation (Priv_Dep);
16532 Check_Controlling_Formals (Full_T, Priv_Dep);
16533 end if;
16534
16535 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
16536
16537 -- Can happen during processing of a body before the completion
16538 -- of a TA type. Ignore, because spec is also on dependent list.
16539
16540 return;
16541
88b32fc3
BD
16542 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
16543 -- corresponding subtype of the full view.
16544
16545 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
16546 Set_Subtype_Indication
16547 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
16548 Set_Etype (Priv_Dep, Full_T);
16549 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
16550 Set_Analyzed (Parent (Priv_Dep), False);
16551
16552 -- Reanalyze the declaration, suppressing the call to
16553 -- Enter_Name to avoid duplicate names.
16554
16555 Analyze_Subtype_Declaration
16556 (N => Parent (Priv_Dep),
16557 Skip => True);
16558
996ae0b0
RK
16559 -- Dependent is a subtype
16560
16561 else
16562 -- We build a new subtype indication using the full view of the
16563 -- incomplete parent. The discriminant constraints have been
16564 -- elaborated already at the point of the subtype declaration.
16565
16566 New_Subt := Create_Itype (E_Void, N);
16567
16568 if Has_Discriminants (Full_T) then
16569 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
16570 else
16571 Disc_Constraint := No_Elist;
16572 end if;
16573
16574 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
16575 Set_Full_View (Priv_Dep, New_Subt);
16576 end if;
16577
16578 Next_Elmt (Inc_Elmt);
16579 end loop;
996ae0b0
RK
16580 end Process_Incomplete_Dependents;
16581
16582 --------------------------------
16583 -- Process_Range_Expr_In_Decl --
16584 --------------------------------
16585
16586 procedure Process_Range_Expr_In_Decl
16587 (R : Node_Id;
16588 T : Entity_Id;
996ae0b0
RK
16589 Check_List : List_Id := Empty_List;
16590 R_Check_Off : Boolean := False)
16591 is
16592 Lo, Hi : Node_Id;
16593 R_Checks : Check_Result;
16594 Type_Decl : Node_Id;
16595 Def_Id : Entity_Id;
16596
16597 begin
16598 Analyze_And_Resolve (R, Base_Type (T));
16599
16600 if Nkind (R) = N_Range then
16601 Lo := Low_Bound (R);
16602 Hi := High_Bound (R);
16603
88b32fc3
BD
16604 -- We need to ensure validity of the bounds here, because if we
16605 -- go ahead and do the expansion, then the expanded code will get
16606 -- analyzed with range checks suppressed and we miss the check.
16607
16608 Validity_Check_Range (R);
16609
996ae0b0
RK
16610 -- If there were errors in the declaration, try and patch up some
16611 -- common mistakes in the bounds. The cases handled are literals
16612 -- which are Integer where the expected type is Real and vice versa.
16613 -- These corrections allow the compilation process to proceed further
16614 -- along since some basic assumptions of the format of the bounds
16615 -- are guaranteed.
16616
16617 if Etype (R) = Any_Type then
16618
16619 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
16620 Rewrite (Lo,
16621 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
16622
16623 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
16624 Rewrite (Hi,
16625 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
16626
16627 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
16628 Rewrite (Lo,
16629 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
16630
16631 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
16632 Rewrite (Hi,
16633 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
16634 end if;
16635
16636 Set_Etype (Lo, T);
16637 Set_Etype (Hi, T);
16638 end if;
16639
a5b62485
AC
16640 -- If the bounds of the range have been mistakenly given as string
16641 -- literals (perhaps in place of character literals), then an error
16642 -- has already been reported, but we rewrite the string literal as a
16643 -- bound of the range's type to avoid blowups in later processing
16644 -- that looks at static values.
996ae0b0
RK
16645
16646 if Nkind (Lo) = N_String_Literal then
16647 Rewrite (Lo,
16648 Make_Attribute_Reference (Sloc (Lo),
16649 Attribute_Name => Name_First,
16650 Prefix => New_Reference_To (T, Sloc (Lo))));
16651 Analyze_And_Resolve (Lo);
16652 end if;
16653
16654 if Nkind (Hi) = N_String_Literal then
16655 Rewrite (Hi,
16656 Make_Attribute_Reference (Sloc (Hi),
16657 Attribute_Name => Name_First,
16658 Prefix => New_Reference_To (T, Sloc (Hi))));
16659 Analyze_And_Resolve (Hi);
16660 end if;
16661
16662 -- If bounds aren't scalar at this point then exit, avoiding
16663 -- problems with further processing of the range in this procedure.
16664
16665 if not Is_Scalar_Type (Etype (Lo)) then
16666 return;
16667 end if;
16668
16669 -- Resolve (actually Sem_Eval) has checked that the bounds are in
16670 -- then range of the base type. Here we check whether the bounds
16671 -- are in the range of the subtype itself. Note that if the bounds
16672 -- represent the null range the Constraint_Error exception should
16673 -- not be raised.
16674
16675 -- ??? The following code should be cleaned up as follows
a5b62485 16676
fbf5a39b 16677 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
996ae0b0 16678 -- is done in the call to Range_Check (R, T); below
a5b62485 16679
996ae0b0
RK
16680 -- 2. The use of R_Check_Off should be investigated and possibly
16681 -- removed, this would clean up things a bit.
16682
16683 if Is_Null_Range (Lo, Hi) then
16684 null;
16685
16686 else
fbf5a39b
AC
16687 -- Capture values of bounds and generate temporaries for them
16688 -- if needed, before applying checks, since checks may cause
16689 -- duplication of the expression without forcing evaluation.
16690
16691 if Expander_Active then
16692 Force_Evaluation (Lo);
16693 Force_Evaluation (Hi);
16694 end if;
16695
996ae0b0 16696 -- We use a flag here instead of suppressing checks on the
fbf5a39b
AC
16697 -- type because the type we check against isn't necessarily
16698 -- the place where we put the check.
996ae0b0
RK
16699
16700 if not R_Check_Off then
dc06abec 16701 R_Checks := Get_Range_Checks (R, T);
996ae0b0
RK
16702
16703 -- Look up tree to find an appropriate insertion point.
16704 -- This seems really junk code, and very brittle, couldn't
16705 -- we just use an insert actions call of some kind ???
16706
9dfd2ff8 16707 Type_Decl := Parent (R);
996ae0b0 16708 while Present (Type_Decl) and then not
7d7af38a
JM
16709 (Nkind_In (Type_Decl, N_Full_Type_Declaration,
16710 N_Subtype_Declaration,
16711 N_Loop_Statement,
16712 N_Task_Type_Declaration)
996ae0b0 16713 or else
7d7af38a
JM
16714 Nkind_In (Type_Decl, N_Single_Task_Declaration,
16715 N_Protected_Type_Declaration,
16716 N_Single_Protected_Declaration))
996ae0b0
RK
16717 loop
16718 Type_Decl := Parent (Type_Decl);
16719 end loop;
16720
16721 -- Why would Type_Decl not be present??? Without this test,
16722 -- short regression tests fail.
16723
16724 if Present (Type_Decl) then
fbf5a39b
AC
16725
16726 -- Case of loop statement (more comments ???)
16727
996ae0b0
RK
16728 if Nkind (Type_Decl) = N_Loop_Statement then
16729 declare
9dfd2ff8 16730 Indic : Node_Id;
fbf5a39b 16731
996ae0b0 16732 begin
9dfd2ff8 16733 Indic := Parent (R);
7d7af38a
JM
16734 while Present (Indic)
16735 and then Nkind (Indic) /= N_Subtype_Indication
996ae0b0
RK
16736 loop
16737 Indic := Parent (Indic);
16738 end loop;
16739
16740 if Present (Indic) then
16741 Def_Id := Etype (Subtype_Mark (Indic));
16742
16743 Insert_Range_Checks
16744 (R_Checks,
16745 Type_Decl,
16746 Def_Id,
16747 Sloc (Type_Decl),
16748 R,
16749 Do_Before => True);
16750 end if;
16751 end;
fbf5a39b
AC
16752
16753 -- All other cases (more comments ???)
16754
996ae0b0
RK
16755 else
16756 Def_Id := Defining_Identifier (Type_Decl);
16757
16758 if (Ekind (Def_Id) = E_Record_Type
16759 and then Depends_On_Discriminant (R))
16760 or else
16761 (Ekind (Def_Id) = E_Protected_Type
16762 and then Has_Discriminants (Def_Id))
16763 then
16764 Append_Range_Checks
16765 (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
16766
16767 else
16768 Insert_Range_Checks
16769 (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
16770
16771 end if;
16772 end if;
16773 end if;
16774 end if;
16775 end if;
996ae0b0 16776
fbf5a39b
AC
16777 elsif Expander_Active then
16778 Get_Index_Bounds (R, Lo, Hi);
996ae0b0
RK
16779 Force_Evaluation (Lo);
16780 Force_Evaluation (Hi);
16781 end if;
996ae0b0
RK
16782 end Process_Range_Expr_In_Decl;
16783
16784 --------------------------------------
16785 -- Process_Real_Range_Specification --
16786 --------------------------------------
16787
16788 procedure Process_Real_Range_Specification (Def : Node_Id) is
16789 Spec : constant Node_Id := Real_Range_Specification (Def);
16790 Lo : Node_Id;
16791 Hi : Node_Id;
16792 Err : Boolean := False;
16793
16794 procedure Analyze_Bound (N : Node_Id);
16795 -- Analyze and check one bound
16796
fbf5a39b
AC
16797 -------------------
16798 -- Analyze_Bound --
16799 -------------------
16800
996ae0b0
RK
16801 procedure Analyze_Bound (N : Node_Id) is
16802 begin
16803 Analyze_And_Resolve (N, Any_Real);
16804
16805 if not Is_OK_Static_Expression (N) then
fbf5a39b
AC
16806 Flag_Non_Static_Expr
16807 ("bound in real type definition is not static!", N);
996ae0b0
RK
16808 Err := True;
16809 end if;
16810 end Analyze_Bound;
16811
fbf5a39b
AC
16812 -- Start of processing for Process_Real_Range_Specification
16813
996ae0b0
RK
16814 begin
16815 if Present (Spec) then
16816 Lo := Low_Bound (Spec);
16817 Hi := High_Bound (Spec);
16818 Analyze_Bound (Lo);
16819 Analyze_Bound (Hi);
16820
16821 -- If error, clear away junk range specification
16822
16823 if Err then
16824 Set_Real_Range_Specification (Def, Empty);
16825 end if;
16826 end if;
16827 end Process_Real_Range_Specification;
16828
16829 ---------------------
16830 -- Process_Subtype --
16831 ---------------------
16832
16833 function Process_Subtype
16834 (S : Node_Id;
16835 Related_Nod : Node_Id;
16836 Related_Id : Entity_Id := Empty;
b0f26df5 16837 Suffix : Character := ' ') return Entity_Id
996ae0b0
RK
16838 is
16839 P : Node_Id;
16840 Def_Id : Entity_Id;
9dfd2ff8 16841 Error_Node : Node_Id;
996ae0b0
RK
16842 Full_View_Id : Entity_Id;
16843 Subtype_Mark_Id : Entity_Id;
fbf5a39b 16844
9dfd2ff8
CC
16845 May_Have_Null_Exclusion : Boolean;
16846
fbf5a39b
AC
16847 procedure Check_Incomplete (T : Entity_Id);
16848 -- Called to verify that an incomplete type is not used prematurely
16849
16850 ----------------------
16851 -- Check_Incomplete --
16852 ----------------------
16853
16854 procedure Check_Incomplete (T : Entity_Id) is
16855 begin
88b32fc3
BD
16856 -- Ada 2005 (AI-412): Incomplete subtypes are legal
16857
16858 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
16859 and then
16860 not (Ada_Version >= Ada_05
16861 and then
16862 (Nkind (Parent (T)) = N_Subtype_Declaration
16863 or else
16864 (Nkind (Parent (T)) = N_Subtype_Indication
16865 and then Nkind (Parent (Parent (T))) =
16866 N_Subtype_Declaration)))
16867 then
fbf5a39b
AC
16868 Error_Msg_N ("invalid use of type before its full declaration", T);
16869 end if;
16870 end Check_Incomplete;
16871
16872 -- Start of processing for Process_Subtype
996ae0b0
RK
16873
16874 begin
fbf5a39b
AC
16875 -- Case of no constraints present
16876
16877 if Nkind (S) /= N_Subtype_Indication then
fbf5a39b
AC
16878 Find_Type (S);
16879 Check_Incomplete (S);
9dfd2ff8 16880 P := Parent (S);
2820d220 16881
0ab80019 16882 -- Ada 2005 (AI-231): Static check
2820d220 16883
0ab80019 16884 if Ada_Version >= Ada_05
9dfd2ff8
CC
16885 and then Present (P)
16886 and then Null_Exclusion_Present (P)
16887 and then Nkind (P) /= N_Access_To_Object_Definition
2820d220
AC
16888 and then not Is_Access_Type (Entity (S))
16889 then
2b73cf68 16890 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
9dfd2ff8
CC
16891 end if;
16892
7d7af38a
JM
16893 -- The following is ugly, can't we have a range or even a flag???
16894
9dfd2ff8 16895 May_Have_Null_Exclusion :=
7d7af38a
JM
16896 Nkind_In (P, N_Access_Definition,
16897 N_Access_Function_Definition,
16898 N_Access_Procedure_Definition,
16899 N_Access_To_Object_Definition,
16900 N_Allocator,
16901 N_Component_Definition)
16902 or else
16903 Nkind_In (P, N_Derived_Type_Definition,
16904 N_Discriminant_Specification,
aecf0203 16905 N_Formal_Object_Declaration,
7d7af38a 16906 N_Object_Declaration,
aecf0203 16907 N_Object_Renaming_Declaration,
7d7af38a
JM
16908 N_Parameter_Specification,
16909 N_Subtype_Declaration);
9dfd2ff8
CC
16910
16911 -- Create an Itype that is a duplicate of Entity (S) but with the
16912 -- null-exclusion attribute
16913
16914 if May_Have_Null_Exclusion
16915 and then Is_Access_Type (Entity (S))
16916 and then Null_Exclusion_Present (P)
16917
16918 -- No need to check the case of an access to object definition.
16919 -- It is correct to define double not-null pointers.
88b32fc3 16920
9dfd2ff8
CC
16921 -- Example:
16922 -- type Not_Null_Int_Ptr is not null access Integer;
16923 -- type Acc is not null access Not_Null_Int_Ptr;
16924
16925 and then Nkind (P) /= N_Access_To_Object_Definition
16926 then
16927 if Can_Never_Be_Null (Entity (S)) then
16928 case Nkind (Related_Nod) is
16929 when N_Full_Type_Declaration =>
16930 if Nkind (Type_Definition (Related_Nod))
16931 in N_Array_Type_Definition
16932 then
16933 Error_Node :=
16934 Subtype_Indication
16935 (Component_Definition
16936 (Type_Definition (Related_Nod)));
16937 else
16938 Error_Node :=
16939 Subtype_Indication (Type_Definition (Related_Nod));
16940 end if;
16941
16942 when N_Subtype_Declaration =>
16943 Error_Node := Subtype_Indication (Related_Nod);
16944
16945 when N_Object_Declaration =>
16946 Error_Node := Object_Definition (Related_Nod);
16947
16948 when N_Component_Declaration =>
16949 Error_Node :=
16950 Subtype_Indication (Component_Definition (Related_Nod));
16951
aecf0203
AC
16952 when N_Allocator =>
16953 Error_Node := Expression (Related_Nod);
16954
9dfd2ff8
CC
16955 when others =>
16956 pragma Assert (False);
16957 Error_Node := Related_Nod;
16958 end case;
16959
2b73cf68
JM
16960 Error_Msg_NE
16961 ("`NOT NULL` not allowed (& already excludes null)",
16962 Error_Node,
16963 Entity (S));
9dfd2ff8
CC
16964 end if;
16965
16966 Set_Etype (S,
16967 Create_Null_Excluding_Itype
16968 (T => Entity (S),
16969 Related_Nod => P));
16970 Set_Entity (S, Etype (S));
2820d220 16971 end if;
9dfd2ff8 16972
fbf5a39b
AC
16973 return Entity (S);
16974
996ae0b0
RK
16975 -- Case of constraint present, so that we have an N_Subtype_Indication
16976 -- node (this node is created only if constraints are present).
16977
fbf5a39b 16978 else
996ae0b0
RK
16979 Find_Type (Subtype_Mark (S));
16980
16981 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
16982 and then not
16983 (Nkind (Parent (S)) = N_Subtype_Declaration
653da906 16984 and then Is_Itype (Defining_Identifier (Parent (S))))
996ae0b0
RK
16985 then
16986 Check_Incomplete (Subtype_Mark (S));
16987 end if;
16988
16989 P := Parent (S);
16990 Subtype_Mark_Id := Entity (Subtype_Mark (S));
16991
996ae0b0
RK
16992 -- Explicit subtype declaration case
16993
16994 if Nkind (P) = N_Subtype_Declaration then
16995 Def_Id := Defining_Identifier (P);
16996
16997 -- Explicit derived type definition case
16998
16999 elsif Nkind (P) = N_Derived_Type_Definition then
17000 Def_Id := Defining_Identifier (Parent (P));
17001
17002 -- Implicit case, the Def_Id must be created as an implicit type.
a5b62485
AC
17003 -- The one exception arises in the case of concurrent types, array
17004 -- and access types, where other subsidiary implicit types may be
17005 -- created and must appear before the main implicit type. In these
17006 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
17007 -- has not yet been called to create Def_Id.
996ae0b0
RK
17008
17009 else
17010 if Is_Array_Type (Subtype_Mark_Id)
17011 or else Is_Concurrent_Type (Subtype_Mark_Id)
17012 or else Is_Access_Type (Subtype_Mark_Id)
17013 then
17014 Def_Id := Empty;
17015
17016 -- For the other cases, we create a new unattached Itype,
17017 -- and set the indication to ensure it gets attached later.
17018
17019 else
17020 Def_Id :=
17021 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
17022 end if;
996ae0b0
RK
17023 end if;
17024
17025 -- If the kind of constraint is invalid for this kind of type,
17026 -- then give an error, and then pretend no constraint was given.
17027
17028 if not Is_Valid_Constraint_Kind
17029 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
17030 then
17031 Error_Msg_N
17032 ("incorrect constraint for this kind of type", Constraint (S));
17033
17034 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17035
758c442c 17036 -- Set Ekind of orphan itype, to prevent cascaded errors
82c80734
RD
17037
17038 if Present (Def_Id) then
17039 Set_Ekind (Def_Id, Ekind (Any_Type));
17040 end if;
17041
996ae0b0
RK
17042 -- Make recursive call, having got rid of the bogus constraint
17043
17044 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
17045 end if;
17046
17047 -- Remaining processing depends on type
17048
17049 case Ekind (Subtype_Mark_Id) is
996ae0b0
RK
17050 when Access_Kind =>
17051 Constrain_Access (Def_Id, S, Related_Nod);
17052
fea9e956
ES
17053 if Expander_Active
17054 and then Is_Itype (Designated_Type (Def_Id))
17055 and then Nkind (Related_Nod) = N_Subtype_Declaration
17056 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
17057 then
17058 Build_Itype_Reference
17059 (Designated_Type (Def_Id), Related_Nod);
17060 end if;
17061
996ae0b0
RK
17062 when Array_Kind =>
17063 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
17064
17065 when Decimal_Fixed_Point_Kind =>
07fc65c4 17066 Constrain_Decimal (Def_Id, S);
996ae0b0
RK
17067
17068 when Enumeration_Kind =>
07fc65c4 17069 Constrain_Enumeration (Def_Id, S);
996ae0b0
RK
17070
17071 when Ordinary_Fixed_Point_Kind =>
07fc65c4 17072 Constrain_Ordinary_Fixed (Def_Id, S);
996ae0b0
RK
17073
17074 when Float_Kind =>
07fc65c4 17075 Constrain_Float (Def_Id, S);
996ae0b0
RK
17076
17077 when Integer_Kind =>
07fc65c4 17078 Constrain_Integer (Def_Id, S);
996ae0b0
RK
17079
17080 when E_Record_Type |
17081 E_Record_Subtype |
17082 Class_Wide_Kind |
17083 E_Incomplete_Type =>
17084 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
17085
93bcda23
AC
17086 if Ekind (Def_Id) = E_Incomplete_Type then
17087 Set_Private_Dependents (Def_Id, New_Elmt_List);
17088 end if;
17089
996ae0b0
RK
17090 when Private_Kind =>
17091 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
17092 Set_Private_Dependents (Def_Id, New_Elmt_List);
17093
17094 -- In case of an invalid constraint prevent further processing
17095 -- since the type constructed is missing expected fields.
17096
17097 if Etype (Def_Id) = Any_Type then
17098 return Def_Id;
17099 end if;
17100
17101 -- If the full view is that of a task with discriminants,
17102 -- we must constrain both the concurrent type and its
17103 -- corresponding record type. Otherwise we will just propagate
17104 -- the constraint to the full view, if available.
17105
17106 if Present (Full_View (Subtype_Mark_Id))
17107 and then Has_Discriminants (Subtype_Mark_Id)
17108 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
17109 then
17110 Full_View_Id :=
17111 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
17112
17113 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
17114 Constrain_Concurrent (Full_View_Id, S,
17115 Related_Nod, Related_Id, Suffix);
17116 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
17117 Set_Full_View (Def_Id, Full_View_Id);
17118
88b32fc3
BD
17119 -- Introduce an explicit reference to the private subtype,
17120 -- to prevent scope anomalies in gigi if first use appears
17121 -- in a nested context, e.g. a later function body.
17122 -- Should this be generated in other contexts than a full
17123 -- type declaration?
17124
17125 if Is_Itype (Def_Id)
17126 and then
17127 Nkind (Parent (P)) = N_Full_Type_Declaration
17128 then
fea9e956 17129 Build_Itype_Reference (Def_Id, Parent (P));
88b32fc3
BD
17130 end if;
17131
996ae0b0
RK
17132 else
17133 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
17134 end if;
17135
17136 when Concurrent_Kind =>
17137 Constrain_Concurrent (Def_Id, S,
17138 Related_Nod, Related_Id, Suffix);
17139
17140 when others =>
17141 Error_Msg_N ("invalid subtype mark in subtype indication", S);
17142 end case;
17143
17144 -- Size and Convention are always inherited from the base type
17145
17146 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
17147 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
17148
17149 return Def_Id;
996ae0b0
RK
17150 end if;
17151 end Process_Subtype;
17152
fea9e956
ES
17153 ---------------------------------------
17154 -- Check_Anonymous_Access_Components --
17155 ---------------------------------------
996ae0b0 17156
fea9e956
ES
17157 procedure Check_Anonymous_Access_Components
17158 (Typ_Decl : Node_Id;
17159 Typ : Entity_Id;
17160 Prev : Entity_Id;
17161 Comp_List : Node_Id)
fbf5a39b 17162 is
fea9e956
ES
17163 Loc : constant Source_Ptr := Sloc (Typ_Decl);
17164 Anon_Access : Entity_Id;
17165 Acc_Def : Node_Id;
17166 Comp : Node_Id;
17167 Comp_Def : Node_Id;
17168 Decl : Node_Id;
17169 Type_Def : Node_Id;
17170
17171 procedure Build_Incomplete_Type_Declaration;
758c442c 17172 -- If the record type contains components that include an access to the
fea9e956
ES
17173 -- current record, then create an incomplete type declaration for the
17174 -- record, to be used as the designated type of the anonymous access.
17175 -- This is done only once, and only if there is no previous partial
17176 -- view of the type.
17177
5320014a 17178 function Designates_T (Subt : Node_Id) return Boolean;
d33fb1e6
BD
17179 -- Check whether a node designates the enclosing record type, or 'Class
17180 -- of that type
5320014a 17181
fea9e956
ES
17182 function Mentions_T (Acc_Def : Node_Id) return Boolean;
17183 -- Check whether an access definition includes a reference to
5320014a
ST
17184 -- the enclosing record type. The reference can be a subtype mark
17185 -- in the access definition itself, a 'Class attribute reference, or
17186 -- recursively a reference appearing in a parameter specification
17187 -- or result definition of an access_to_subprogram definition.
996ae0b0 17188
fea9e956
ES
17189 --------------------------------------
17190 -- Build_Incomplete_Type_Declaration --
17191 --------------------------------------
996ae0b0 17192
fea9e956
ES
17193 procedure Build_Incomplete_Type_Declaration is
17194 Decl : Node_Id;
17195 Inc_T : Entity_Id;
17196 H : Entity_Id;
996ae0b0 17197
d33fb1e6
BD
17198 -- Is_Tagged indicates whether the type is tagged. It is tagged if
17199 -- it's "is new ... with record" or else "is tagged record ...".
17200
17201 Is_Tagged : constant Boolean :=
17202 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
17203 and then
17204 Present
17205 (Record_Extension_Part (Type_Definition (Typ_Decl))))
17206 or else
17207 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
17208 and then Tagged_Present (Type_Definition (Typ_Decl)));
17209
fea9e956
ES
17210 begin
17211 -- If there is a previous partial view, no need to create a new one
17212 -- If the partial view, given by Prev, is incomplete, If Prev is
17213 -- a private declaration, full declaration is flagged accordingly.
758c442c 17214
fea9e956 17215 if Prev /= Typ then
d33fb1e6 17216 if Is_Tagged then
fea9e956
ES
17217 Make_Class_Wide_Type (Prev);
17218 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
17219 Set_Etype (Class_Wide_Type (Typ), Typ);
17220 end if;
758c442c 17221
fea9e956 17222 return;
758c442c 17223
fea9e956 17224 elsif Has_Private_Declaration (Typ) then
d33fb1e6
BD
17225
17226 -- If we refer to T'Class inside T, and T is the completion of a
17227 -- private type, then we need to make sure the class-wide type
17228 -- exists.
17229
17230 if Is_Tagged then
17231 Make_Class_Wide_Type (Typ);
17232 end if;
17233
fea9e956 17234 return;
57193e09 17235
fea9e956
ES
17236 -- If there was a previous anonymous access type, the incomplete
17237 -- type declaration will have been created already.
57193e09 17238
fea9e956
ES
17239 elsif Present (Current_Entity (Typ))
17240 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
17241 and then Full_View (Current_Entity (Typ)) = Typ
17242 then
17243 return;
758c442c 17244
fea9e956 17245 else
ce4a6e84
RD
17246 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
17247 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
758c442c 17248
fea9e956
ES
17249 -- Type has already been inserted into the current scope.
17250 -- Remove it, and add incomplete declaration for type, so
17251 -- that subsequent anonymous access types can use it.
17252 -- The entity is unchained from the homonym list and from
17253 -- immediate visibility. After analysis, the entity in the
17254 -- incomplete declaration becomes immediately visible in the
17255 -- record declaration that follows.
758c442c 17256
fea9e956 17257 H := Current_Entity (Typ);
758c442c 17258
fea9e956
ES
17259 if H = Typ then
17260 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
17261 else
17262 while Present (H)
17263 and then Homonym (H) /= Typ
17264 loop
17265 H := Homonym (Typ);
17266 end loop;
758c442c 17267
fea9e956 17268 Set_Homonym (H, Homonym (Typ));
758c442c 17269 end if;
758c442c 17270
fea9e956
ES
17271 Insert_Before (Typ_Decl, Decl);
17272 Analyze (Decl);
17273 Set_Full_View (Inc_T, Typ);
758c442c 17274
d33fb1e6 17275 if Is_Tagged then
fea9e956 17276 -- Create a common class-wide type for both views, and set
d33fb1e6 17277 -- the Etype of the class-wide type to the full view.
758c442c 17278
fea9e956
ES
17279 Make_Class_Wide_Type (Inc_T);
17280 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
17281 Set_Etype (Class_Wide_Type (Typ), Typ);
17282 end if;
17283 end if;
17284 end Build_Incomplete_Type_Declaration;
758c442c 17285
5320014a
ST
17286 ------------------
17287 -- Designates_T --
17288 ------------------
17289
17290 function Designates_T (Subt : Node_Id) return Boolean is
fea9e956 17291 Type_Id : constant Name_Id := Chars (Typ);
758c442c 17292
2b73cf68 17293 function Names_T (Nam : Node_Id) return Boolean;
2b73cf68
JM
17294 -- The record type has not been introduced in the current scope
17295 -- yet, so we must examine the name of the type itself, either
17296 -- an identifier T, or an expanded name of the form P.T, where
17297 -- P denotes the current scope.
17298
dc06abec
RD
17299 -------------
17300 -- Names_T --
17301 -------------
17302
2b73cf68
JM
17303 function Names_T (Nam : Node_Id) return Boolean is
17304 begin
17305 if Nkind (Nam) = N_Identifier then
17306 return Chars (Nam) = Type_Id;
17307
17308 elsif Nkind (Nam) = N_Selected_Component then
17309 if Chars (Selector_Name (Nam)) = Type_Id then
17310 if Nkind (Prefix (Nam)) = N_Identifier then
17311 return Chars (Prefix (Nam)) = Chars (Current_Scope);
17312
17313 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
dc06abec
RD
17314 return Chars (Selector_Name (Prefix (Nam))) =
17315 Chars (Current_Scope);
2b73cf68
JM
17316 else
17317 return False;
17318 end if;
7d7af38a 17319
2b73cf68
JM
17320 else
17321 return False;
17322 end if;
7d7af38a 17323
2b73cf68
JM
17324 else
17325 return False;
17326 end if;
17327 end Names_T;
17328
5320014a 17329 -- Start of processing for Designates_T
dc06abec 17330
fea9e956 17331 begin
5320014a
ST
17332 if Nkind (Subt) = N_Identifier then
17333 return Chars (Subt) = Type_Id;
88b32fc3 17334
fea9e956 17335 -- Reference can be through an expanded name which has not been
2b73cf68 17336 -- analyzed yet, and which designates enclosing scopes.
88b32fc3 17337
5320014a
ST
17338 elsif Nkind (Subt) = N_Selected_Component then
17339 if Names_T (Subt) then
17340 return True;
88b32fc3 17341
5320014a
ST
17342 -- Otherwise it must denote an entity that is already visible.
17343 -- The access definition may name a subtype of the enclosing
17344 -- type, if there is a previous incomplete declaration for it.
fea9e956 17345
fea9e956 17346 else
5320014a
ST
17347 Find_Selected_Component (Subt);
17348 return
17349 Is_Entity_Name (Subt)
17350 and then Scope (Entity (Subt)) = Current_Scope
17351 and then
17352 (Chars (Base_Type (Entity (Subt))) = Type_Id
17353 or else
17354 (Is_Class_Wide_Type (Entity (Subt))
17355 and then
7d7af38a
JM
17356 Chars (Etype (Base_Type (Entity (Subt)))) =
17357 Type_Id));
758c442c
GD
17358 end if;
17359
5320014a
ST
17360 -- A reference to the current type may appear as the prefix of
17361 -- a 'Class attribute.
17362
17363 elsif Nkind (Subt) = N_Attribute_Reference
17364 and then Attribute_Name (Subt) = Name_Class
17365 then
17366 return Names_T (Prefix (Subt));
17367
fea9e956 17368 else
5320014a
ST
17369 return False;
17370 end if;
17371 end Designates_T;
758c442c 17372
5320014a
ST
17373 ----------------
17374 -- Mentions_T --
17375 ----------------
fea9e956 17376
5320014a
ST
17377 function Mentions_T (Acc_Def : Node_Id) return Boolean is
17378 Param_Spec : Node_Id;
fea9e956 17379
5320014a 17380 Acc_Subprg : constant Node_Id :=
7d7af38a 17381 Access_To_Subprogram_Definition (Acc_Def);
fea9e956 17382
5320014a
ST
17383 begin
17384 if No (Acc_Subprg) then
17385 return Designates_T (Subtype_Mark (Acc_Def));
758c442c 17386 end if;
5320014a
ST
17387
17388 -- Component is an access_to_subprogram: examine its formals,
17389 -- and result definition in the case of an access_to_function.
17390
17391 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
17392 while Present (Param_Spec) loop
17393 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
17394 and then Mentions_T (Parameter_Type (Param_Spec))
17395 then
17396 return True;
17397
17398 elsif Designates_T (Parameter_Type (Param_Spec)) then
17399 return True;
17400 end if;
17401
17402 Next (Param_Spec);
17403 end loop;
17404
17405 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
17406 if Nkind (Result_Definition (Acc_Subprg)) =
17407 N_Access_Definition
17408 then
17409 return Mentions_T (Result_Definition (Acc_Subprg));
17410 else
17411 return Designates_T (Result_Definition (Acc_Subprg));
17412 end if;
17413 end if;
17414
17415 return False;
fea9e956 17416 end Mentions_T;
996ae0b0 17417
fea9e956 17418 -- Start of processing for Check_Anonymous_Access_Components
758c442c 17419
fea9e956
ES
17420 begin
17421 if No (Comp_List) then
17422 return;
17423 end if;
758c442c 17424
fea9e956
ES
17425 Comp := First (Component_Items (Comp_List));
17426 while Present (Comp) loop
17427 if Nkind (Comp) = N_Component_Declaration
17428 and then Present
17429 (Access_Definition (Component_Definition (Comp)))
17430 and then
17431 Mentions_T (Access_Definition (Component_Definition (Comp)))
57193e09 17432 then
fea9e956
ES
17433 Comp_Def := Component_Definition (Comp);
17434 Acc_Def :=
17435 Access_To_Subprogram_Definition
17436 (Access_Definition (Comp_Def));
758c442c 17437
fea9e956
ES
17438 Build_Incomplete_Type_Declaration;
17439 Anon_Access :=
17440 Make_Defining_Identifier (Loc,
17441 Chars => New_Internal_Name ('S'));
758c442c 17442
fea9e956
ES
17443 -- Create a declaration for the anonymous access type: either
17444 -- an access_to_object or an access_to_subprogram.
758c442c 17445
fea9e956
ES
17446 if Present (Acc_Def) then
17447 if Nkind (Acc_Def) = N_Access_Function_Definition then
17448 Type_Def :=
17449 Make_Access_Function_Definition (Loc,
17450 Parameter_Specifications =>
17451 Parameter_Specifications (Acc_Def),
17452 Result_Definition => Result_Definition (Acc_Def));
17453 else
17454 Type_Def :=
17455 Make_Access_Procedure_Definition (Loc,
17456 Parameter_Specifications =>
17457 Parameter_Specifications (Acc_Def));
17458 end if;
758c442c 17459
758c442c 17460 else
fea9e956
ES
17461 Type_Def :=
17462 Make_Access_To_Object_Definition (Loc,
17463 Subtype_Indication =>
17464 Relocate_Node
17465 (Subtype_Mark
17466 (Access_Definition (Comp_Def))));
2b73cf68
JM
17467
17468 Set_Constant_Present
17469 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
17470 Set_All_Present
17471 (Type_Def, All_Present (Access_Definition (Comp_Def)));
758c442c
GD
17472 end if;
17473
2b73cf68
JM
17474 Set_Null_Exclusion_Present
17475 (Type_Def,
17476 Null_Exclusion_Present (Access_Definition (Comp_Def)));
17477
17478 Decl :=
17479 Make_Full_Type_Declaration (Loc,
17480 Defining_Identifier => Anon_Access,
17481 Type_Definition => Type_Def);
fea9e956
ES
17482
17483 Insert_Before (Typ_Decl, Decl);
758c442c 17484 Analyze (Decl);
758c442c 17485
fea9e956
ES
17486 -- If an access to object, Preserve entity of designated type,
17487 -- for ASIS use, before rewriting the component definition.
17488
17489 if No (Acc_Def) then
17490 declare
17491 Desig : Entity_Id;
17492
17493 begin
17494 Desig := Entity (Subtype_Indication (Type_Def));
17495
17496 -- If the access definition is to the current record,
17497 -- the visible entity at this point is an incomplete
17498 -- type. Retrieve the full view to simplify ASIS queries
17499
17500 if Ekind (Desig) = E_Incomplete_Type then
17501 Desig := Full_View (Desig);
17502 end if;
17503
17504 Set_Entity
17505 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
17506 end;
758c442c 17507 end if;
fea9e956
ES
17508
17509 Rewrite (Comp_Def,
17510 Make_Component_Definition (Loc,
17511 Subtype_Indication =>
17512 New_Occurrence_Of (Anon_Access, Loc)));
5320014a
ST
17513
17514 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
17515 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
17516 else
17517 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
17518 end if;
17519
fea9e956 17520 Set_Is_Local_Anonymous_Access (Anon_Access);
758c442c 17521 end if;
758c442c 17522
fea9e956
ES
17523 Next (Comp);
17524 end loop;
17525
17526 if Present (Variant_Part (Comp_List)) then
17527 declare
17528 V : Node_Id;
17529 begin
17530 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
17531 while Present (V) loop
17532 Check_Anonymous_Access_Components
17533 (Typ_Decl, Typ, Prev, Component_List (V));
17534 Next_Non_Pragma (V);
17535 end loop;
17536 end;
17537 end if;
17538 end Check_Anonymous_Access_Components;
17539
ce4a6e84
RD
17540 --------------------------------
17541 -- Preanalyze_Spec_Expression --
17542 --------------------------------
17543
17544 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
17545 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
17546 begin
17547 In_Spec_Expression := True;
17548 Preanalyze_And_Resolve (N, T);
17549 In_Spec_Expression := Save_In_Spec_Expression;
17550 end Preanalyze_Spec_Expression;
17551
fea9e956
ES
17552 -----------------------------
17553 -- Record_Type_Declaration --
17554 -----------------------------
17555
17556 procedure Record_Type_Declaration
17557 (T : Entity_Id;
17558 N : Node_Id;
17559 Prev : Entity_Id)
17560 is
17561 Def : constant Node_Id := Type_Definition (N);
17562 Is_Tagged : Boolean;
17563 Tag_Comp : Entity_Id;
758c442c
GD
17564
17565 begin
996ae0b0
RK
17566 -- These flags must be initialized before calling Process_Discriminants
17567 -- because this routine makes use of them.
17568
ce2b6ba5
JM
17569 Set_Ekind (T, E_Record_Type);
17570 Set_Etype (T, T);
17571 Init_Size_Align (T);
17572 Set_Interfaces (T, No_Elist);
17573 Set_Stored_Constraint (T, No_Elist);
996ae0b0 17574
758c442c 17575 -- Normal case
996ae0b0 17576
758c442c
GD
17577 if Ada_Version < Ada_05
17578 or else not Interface_Present (Def)
17579 then
17580 -- The flag Is_Tagged_Type might have already been set by
17581 -- Find_Type_Name if it detected an error for declaration T. This
17582 -- arises in the case of private tagged types where the full view
17583 -- omits the word tagged.
996ae0b0 17584
758c442c
GD
17585 Is_Tagged :=
17586 Tagged_Present (Def)
17587 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
996ae0b0 17588
758c442c
GD
17589 Set_Is_Tagged_Type (T, Is_Tagged);
17590 Set_Is_Limited_Record (T, Limited_Present (Def));
17591
17592 -- Type is abstract if full declaration carries keyword, or if
17593 -- previous partial view did.
17594
fea9e956 17595 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
758c442c
GD
17596 or else Abstract_Present (Def));
17597
17598 else
17599 Is_Tagged := True;
950d3e7d 17600 Analyze_Interface_Declaration (T, Def);
88b32fc3
BD
17601
17602 if Present (Discriminant_Specifications (N)) then
17603 Error_Msg_N
17604 ("interface types cannot have discriminants",
17605 Defining_Identifier
17606 (First (Discriminant_Specifications (N))));
17607 end if;
758c442c
GD
17608 end if;
17609
17610 -- First pass: if there are self-referential access components,
17611 -- create the required anonymous access type declarations, and if
17612 -- need be an incomplete type declaration for T itself.
17613
fea9e956 17614 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
758c442c 17615
758c442c
GD
17616 if Ada_Version >= Ada_05
17617 and then Present (Interface_List (Def))
17618 then
ce2b6ba5 17619 Check_Interfaces (N, Def);
fea9e956 17620
758c442c 17621 declare
88b32fc3 17622 Ifaces_List : Elist_Id;
950d3e7d 17623
758c442c 17624 begin
88b32fc3
BD
17625 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
17626 -- already in the parents.
17627
ce2b6ba5
JM
17628 Collect_Interfaces
17629 (T => T,
17630 Ifaces_List => Ifaces_List,
17631 Exclude_Parents => True);
88b32fc3 17632
ce2b6ba5 17633 Set_Interfaces (T, Ifaces_List);
758c442c
GD
17634 end;
17635 end if;
17636
17637 -- Records constitute a scope for the component declarations within.
17638 -- The scope is created prior to the processing of these declarations.
17639 -- Discriminants are processed first, so that they are visible when
17640 -- processing the other components. The Ekind of the record type itself
17641 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
17642
17643 -- Enter record scope
17644
2b73cf68 17645 Push_Scope (T);
996ae0b0
RK
17646
17647 -- If an incomplete or private type declaration was already given for
17648 -- the type, then this scope already exists, and the discriminants have
17649 -- been declared within. We must verify that the full declaration
17650 -- matches the incomplete one.
17651
fbf5a39b 17652 Check_Or_Process_Discriminants (N, T, Prev);
996ae0b0
RK
17653
17654 Set_Is_Constrained (T, not Has_Discriminants (T));
17655 Set_Has_Delayed_Freeze (T, True);
17656
17657 -- For tagged types add a manually analyzed component corresponding
17658 -- to the component _tag, the corresponding piece of tree will be
17659 -- expanded as part of the freezing actions if it is not a CPP_Class.
17660
17661 if Is_Tagged then
ffe9aba8
AC
17662
17663 -- Do not add the tag unless we are in expansion mode
996ae0b0
RK
17664
17665 if Expander_Active then
17666 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
17667 Enter_Name (Tag_Comp);
17668
7d7af38a 17669 Set_Ekind (Tag_Comp, E_Component);
996ae0b0 17670 Set_Is_Tag (Tag_Comp);
758c442c 17671 Set_Is_Aliased (Tag_Comp);
996ae0b0
RK
17672 Set_Etype (Tag_Comp, RTE (RE_Tag));
17673 Set_DT_Entry_Count (Tag_Comp, No_Uint);
17674 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
17675 Init_Component_Location (Tag_Comp);
758c442c
GD
17676
17677 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
fea9e956 17678 -- implemented interfaces.
758c442c 17679
ce2b6ba5 17680 if Has_Interfaces (T) then
fea9e956
ES
17681 Add_Interface_Tag_Components (N, T);
17682 end if;
996ae0b0
RK
17683 end if;
17684
17685 Make_Class_Wide_Type (T);
17686 Set_Primitive_Operations (T, New_Elmt_List);
17687 end if;
17688
17689 -- We must suppress range checks when processing the components
17690 -- of a record in the presence of discriminants, since we don't
17691 -- want spurious checks to be generated during their analysis, but
fbf5a39b 17692 -- must reset the Suppress_Range_Checks flags after having processed
996ae0b0
RK
17693 -- the record definition.
17694
88b32fc3
BD
17695 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
17696 -- couldn't we just use the normal range check suppression method here.
17697 -- That would seem cleaner ???
17698
fbf5a39b
AC
17699 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
17700 Set_Kill_Range_Checks (T, True);
17701 Record_Type_Definition (Def, Prev);
17702 Set_Kill_Range_Checks (T, False);
17703 else
17704 Record_Type_Definition (Def, Prev);
996ae0b0
RK
17705 end if;
17706
17707 -- Exit from record scope
17708
17709 End_Scope;
758c442c 17710
88b32fc3
BD
17711 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
17712 -- the implemented interfaces and associate them an aliased entity.
17713
17714 if Is_Tagged
758c442c
GD
17715 and then not Is_Empty_List (Interface_List (Def))
17716 then
ce2b6ba5 17717 Derive_Progenitor_Subprograms (T, T);
758c442c 17718 end if;
996ae0b0
RK
17719 end Record_Type_Declaration;
17720
17721 ----------------------------
17722 -- Record_Type_Definition --
17723 ----------------------------
17724
fbf5a39b 17725 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
996ae0b0
RK
17726 Component : Entity_Id;
17727 Ctrl_Components : Boolean := False;
fbf5a39b
AC
17728 Final_Storage_Only : Boolean;
17729 T : Entity_Id;
996ae0b0
RK
17730
17731 begin
fbf5a39b
AC
17732 if Ekind (Prev_T) = E_Incomplete_Type then
17733 T := Full_View (Prev_T);
17734 else
17735 T := Prev_T;
17736 end if;
17737
17738 Final_Storage_Only := not Is_Controlled (T);
17739
57193e09
TQ
17740 -- Ada 2005: check whether an explicit Limited is present in a derived
17741 -- type declaration.
17742
17743 if Nkind (Parent (Def)) = N_Derived_Type_Definition
17744 and then Limited_Present (Parent (Def))
17745 then
17746 Set_Is_Limited_Record (T);
17747 end if;
17748
996ae0b0
RK
17749 -- If the component list of a record type is defined by the reserved
17750 -- word null and there is no discriminant part, then the record type has
17751 -- no components and all records of the type are null records (RM 3.7)
17752 -- This procedure is also called to process the extension part of a
17753 -- record extension, in which case the current scope may have inherited
17754 -- components.
17755
17756 if No (Def)
17757 or else No (Component_List (Def))
17758 or else Null_Present (Component_List (Def))
17759 then
17760 null;
17761
17762 else
17763 Analyze_Declarations (Component_Items (Component_List (Def)));
17764
17765 if Present (Variant_Part (Component_List (Def))) then
17766 Analyze (Variant_Part (Component_List (Def)));
17767 end if;
17768 end if;
17769
17770 -- After completing the semantic analysis of the record definition,
fea9e956 17771 -- record components, both new and inherited, are accessible. Set their
2b73cf68
JM
17772 -- kind accordingly. Exclude malformed itypes from illegal declarations,
17773 -- whose Ekind may be void.
996ae0b0
RK
17774
17775 Component := First_Entity (Current_Scope);
17776 while Present (Component) loop
2b73cf68
JM
17777 if Ekind (Component) = E_Void
17778 and then not Is_Itype (Component)
17779 then
996ae0b0
RK
17780 Set_Ekind (Component, E_Component);
17781 Init_Component_Location (Component);
17782 end if;
17783
17784 if Has_Task (Etype (Component)) then
17785 Set_Has_Task (T);
17786 end if;
17787
17788 if Ekind (Component) /= E_Component then
17789 null;
17790
17791 elsif Has_Controlled_Component (Etype (Component))
17792 or else (Chars (Component) /= Name_uParent
33931112 17793 and then Is_Controlled (Etype (Component)))
996ae0b0
RK
17794 then
17795 Set_Has_Controlled_Component (T, True);
33931112
JM
17796 Final_Storage_Only :=
17797 Final_Storage_Only
17798 and then Finalize_Storage_Only (Etype (Component));
996ae0b0
RK
17799 Ctrl_Components := True;
17800 end if;
17801
17802 Next_Entity (Component);
17803 end loop;
17804
fea9e956
ES
17805 -- A Type is Finalize_Storage_Only only if all its controlled components
17806 -- are also.
996ae0b0
RK
17807
17808 if Ctrl_Components then
17809 Set_Finalize_Storage_Only (T, Final_Storage_Only);
17810 end if;
17811
fbf5a39b
AC
17812 -- Place reference to end record on the proper entity, which may
17813 -- be a partial view.
17814
996ae0b0 17815 if Present (Def) then
fbf5a39b 17816 Process_End_Label (Def, 'e', Prev_T);
996ae0b0
RK
17817 end if;
17818 end Record_Type_Definition;
17819
07fc65c4
GB
17820 ------------------------
17821 -- Replace_Components --
17822 ------------------------
17823
17824 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
17825 function Process (N : Node_Id) return Traverse_Result;
17826
17827 -------------
17828 -- Process --
17829 -------------
17830
17831 function Process (N : Node_Id) return Traverse_Result is
17832 Comp : Entity_Id;
17833
17834 begin
17835 if Nkind (N) = N_Discriminant_Specification then
17836 Comp := First_Discriminant (Typ);
07fc65c4
GB
17837 while Present (Comp) loop
17838 if Chars (Comp) = Chars (Defining_Identifier (N)) then
17839 Set_Defining_Identifier (N, Comp);
17840 exit;
17841 end if;
17842
17843 Next_Discriminant (Comp);
17844 end loop;
17845
17846 elsif Nkind (N) = N_Component_Declaration then
17847 Comp := First_Component (Typ);
07fc65c4
GB
17848 while Present (Comp) loop
17849 if Chars (Comp) = Chars (Defining_Identifier (N)) then
17850 Set_Defining_Identifier (N, Comp);
17851 exit;
17852 end if;
17853
17854 Next_Component (Comp);
17855 end loop;
17856 end if;
17857
17858 return OK;
17859 end Process;
17860
17861 procedure Replace is new Traverse_Proc (Process);
17862
17863 -- Start of processing for Replace_Components
17864
17865 begin
17866 Replace (Decl);
17867 end Replace_Components;
17868
17869 -------------------------------
17870 -- Set_Completion_Referenced --
17871 -------------------------------
17872
17873 procedure Set_Completion_Referenced (E : Entity_Id) is
17874 begin
17875 -- If in main unit, mark entity that is a completion as referenced,
17876 -- warnings go on the partial view when needed.
17877
17878 if In_Extended_Main_Source_Unit (E) then
17879 Set_Referenced (E);
17880 end if;
17881 end Set_Completion_Referenced;
17882
996ae0b0
RK
17883 ---------------------
17884 -- Set_Fixed_Range --
17885 ---------------------
17886
17887 -- The range for fixed-point types is complicated by the fact that we
17888 -- do not know the exact end points at the time of the declaration. This
17889 -- is true for three reasons:
17890
17891 -- A size clause may affect the fudging of the end-points
17892 -- A small clause may affect the values of the end-points
17893 -- We try to include the end-points if it does not affect the size
17894
a5b62485
AC
17895 -- This means that the actual end-points must be established at the point
17896 -- when the type is frozen. Meanwhile, we first narrow the range as
17897 -- permitted (so that it will fit if necessary in a small specified size),
17898 -- and then build a range subtree with these narrowed bounds.
996ae0b0 17899
a5b62485
AC
17900 -- Set_Fixed_Range constructs the range from real literal values, and sets
17901 -- the range as the Scalar_Range of the given fixed-point type entity.
996ae0b0 17902
a5b62485
AC
17903 -- The parent of this range is set to point to the entity so that it is
17904 -- properly hooked into the tree (unlike normal Scalar_Range entries for
17905 -- other scalar types, which are just pointers to the range in the
996ae0b0
RK
17906 -- original tree, this would otherwise be an orphan).
17907
17908 -- The tree is left unanalyzed. When the type is frozen, the processing
17909 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
17910 -- analyzed, and uses this as an indication that it should complete
17911 -- work on the range (it will know the final small and size values).
17912
17913 procedure Set_Fixed_Range
17914 (E : Entity_Id;
17915 Loc : Source_Ptr;
17916 Lo : Ureal;
17917 Hi : Ureal)
17918 is
17919 S : constant Node_Id :=
17920 Make_Range (Loc,
17921 Low_Bound => Make_Real_Literal (Loc, Lo),
17922 High_Bound => Make_Real_Literal (Loc, Hi));
996ae0b0
RK
17923 begin
17924 Set_Scalar_Range (E, S);
17925 Set_Parent (S, E);
17926 end Set_Fixed_Range;
17927
996ae0b0
RK
17928 ----------------------------------
17929 -- Set_Scalar_Range_For_Subtype --
17930 ----------------------------------
17931
17932 procedure Set_Scalar_Range_For_Subtype
07fc65c4
GB
17933 (Def_Id : Entity_Id;
17934 R : Node_Id;
17935 Subt : Entity_Id)
996ae0b0
RK
17936 is
17937 Kind : constant Entity_Kind := Ekind (Def_Id);
71d9e9f2 17938
996ae0b0
RK
17939 begin
17940 Set_Scalar_Range (Def_Id, R);
17941
17942 -- We need to link the range into the tree before resolving it so
17943 -- that types that are referenced, including importantly the subtype
17944 -- itself, are properly frozen (Freeze_Expression requires that the
17945 -- expression be properly linked into the tree). Of course if it is
17946 -- already linked in, then we do not disturb the current link.
17947
17948 if No (Parent (R)) then
17949 Set_Parent (R, Def_Id);
17950 end if;
17951
17952 -- Reset the kind of the subtype during analysis of the range, to
17953 -- catch possible premature use in the bounds themselves.
17954
17955 Set_Ekind (Def_Id, E_Void);
07fc65c4 17956 Process_Range_Expr_In_Decl (R, Subt);
996ae0b0 17957 Set_Ekind (Def_Id, Kind);
996ae0b0
RK
17958 end Set_Scalar_Range_For_Subtype;
17959
fbf5a39b
AC
17960 --------------------------------------------------------
17961 -- Set_Stored_Constraint_From_Discriminant_Constraint --
17962 --------------------------------------------------------
17963
17964 procedure Set_Stored_Constraint_From_Discriminant_Constraint
17965 (E : Entity_Id)
17966 is
17967 begin
71d9e9f2 17968 -- Make sure set if encountered during Expand_To_Stored_Constraint
fbf5a39b
AC
17969
17970 Set_Stored_Constraint (E, No_Elist);
17971
17972 -- Give it the right value
17973
17974 if Is_Constrained (E) and then Has_Discriminants (E) then
17975 Set_Stored_Constraint (E,
17976 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
17977 end if;
fbf5a39b
AC
17978 end Set_Stored_Constraint_From_Discriminant_Constraint;
17979
996ae0b0
RK
17980 -------------------------------------
17981 -- Signed_Integer_Type_Declaration --
17982 -------------------------------------
17983
17984 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17985 Implicit_Base : Entity_Id;
17986 Base_Typ : Entity_Id;
17987 Lo_Val : Uint;
17988 Hi_Val : Uint;
17989 Errs : Boolean := False;
17990 Lo : Node_Id;
17991 Hi : Node_Id;
17992
17993 function Can_Derive_From (E : Entity_Id) return Boolean;
17994 -- Determine whether given bounds allow derivation from specified type
17995
17996 procedure Check_Bound (Expr : Node_Id);
17997 -- Check bound to make sure it is integral and static. If not, post
17998 -- appropriate error message and set Errs flag
17999
fbf5a39b
AC
18000 ---------------------
18001 -- Can_Derive_From --
18002 ---------------------
18003
71d9e9f2
ES
18004 -- Note we check both bounds against both end values, to deal with
18005 -- strange types like ones with a range of 0 .. -12341234.
18006
996ae0b0
RK
18007 function Can_Derive_From (E : Entity_Id) return Boolean is
18008 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
18009 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
996ae0b0 18010 begin
996ae0b0
RK
18011 return Lo <= Lo_Val and then Lo_Val <= Hi
18012 and then
18013 Lo <= Hi_Val and then Hi_Val <= Hi;
18014 end Can_Derive_From;
18015
fbf5a39b
AC
18016 -----------------
18017 -- Check_Bound --
18018 -----------------
18019
996ae0b0
RK
18020 procedure Check_Bound (Expr : Node_Id) is
18021 begin
18022 -- If a range constraint is used as an integer type definition, each
18023 -- bound of the range must be defined by a static expression of some
18024 -- integer type, but the two bounds need not have the same integer
18025 -- type (Negative bounds are allowed.) (RM 3.5.4)
18026
18027 if not Is_Integer_Type (Etype (Expr)) then
18028 Error_Msg_N
18029 ("integer type definition bounds must be of integer type", Expr);
18030 Errs := True;
18031
18032 elsif not Is_OK_Static_Expression (Expr) then
fbf5a39b
AC
18033 Flag_Non_Static_Expr
18034 ("non-static expression used for integer type bound!", Expr);
996ae0b0
RK
18035 Errs := True;
18036
18037 -- The bounds are folded into literals, and we set their type to be
18038 -- universal, to avoid typing difficulties: we cannot set the type
18039 -- of the literal to the new type, because this would be a forward
18040 -- reference for the back end, and if the original type is user-
18041 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
18042
18043 else
18044 if Is_Entity_Name (Expr) then
fbf5a39b 18045 Fold_Uint (Expr, Expr_Value (Expr), True);
996ae0b0
RK
18046 end if;
18047
18048 Set_Etype (Expr, Universal_Integer);
18049 end if;
18050 end Check_Bound;
18051
18052 -- Start of processing for Signed_Integer_Type_Declaration
18053
18054 begin
18055 -- Create an anonymous base type
18056
18057 Implicit_Base :=
18058 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
18059
18060 -- Analyze and check the bounds, they can be of any integer type
18061
18062 Lo := Low_Bound (Def);
18063 Hi := High_Bound (Def);
996ae0b0 18064
ce9e9122 18065 -- Arbitrarily use Integer as the type if either bound had an error
996ae0b0 18066
ce9e9122
RD
18067 if Hi = Error or else Lo = Error then
18068 Base_Typ := Any_Integer;
18069 Set_Error_Posted (T, True);
996ae0b0 18070
ce9e9122 18071 -- Here both bounds are OK expressions
996ae0b0 18072
ce9e9122
RD
18073 else
18074 Analyze_And_Resolve (Lo, Any_Integer);
18075 Analyze_And_Resolve (Hi, Any_Integer);
996ae0b0 18076
ce9e9122
RD
18077 Check_Bound (Lo);
18078 Check_Bound (Hi);
996ae0b0 18079
ce9e9122
RD
18080 if Errs then
18081 Hi := Type_High_Bound (Standard_Long_Long_Integer);
18082 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
18083 end if;
996ae0b0 18084
ce9e9122 18085 -- Find type to derive from
996ae0b0 18086
ce9e9122
RD
18087 Lo_Val := Expr_Value (Lo);
18088 Hi_Val := Expr_Value (Hi);
996ae0b0 18089
ce9e9122
RD
18090 if Can_Derive_From (Standard_Short_Short_Integer) then
18091 Base_Typ := Base_Type (Standard_Short_Short_Integer);
996ae0b0 18092
ce9e9122
RD
18093 elsif Can_Derive_From (Standard_Short_Integer) then
18094 Base_Typ := Base_Type (Standard_Short_Integer);
18095
18096 elsif Can_Derive_From (Standard_Integer) then
18097 Base_Typ := Base_Type (Standard_Integer);
18098
18099 elsif Can_Derive_From (Standard_Long_Integer) then
18100 Base_Typ := Base_Type (Standard_Long_Integer);
18101
18102 elsif Can_Derive_From (Standard_Long_Long_Integer) then
18103 Base_Typ := Base_Type (Standard_Long_Long_Integer);
18104
18105 else
18106 Base_Typ := Base_Type (Standard_Long_Long_Integer);
18107 Error_Msg_N ("integer type definition bounds out of range", Def);
18108 Hi := Type_High_Bound (Standard_Long_Long_Integer);
18109 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
18110 end if;
996ae0b0
RK
18111 end if;
18112
18113 -- Complete both implicit base and declared first subtype entities
18114
18115 Set_Etype (Implicit_Base, Base_Typ);
18116 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18117 Set_Size_Info (Implicit_Base, (Base_Typ));
18118 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18119 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18120
18121 Set_Ekind (T, E_Signed_Integer_Subtype);
18122 Set_Etype (T, Implicit_Base);
18123
18124 Set_Size_Info (T, (Implicit_Base));
18125 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
18126 Set_Scalar_Range (T, Def);
18127 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
18128 Set_Is_Constrained (T);
996ae0b0
RK
18129 end Signed_Integer_Type_Declaration;
18130
18131end Sem_Ch3;
This page took 4.420667 seconds and 5 git commands to generate.