]> gcc.gnu.org Git - gcc.git/blame - gcc/ada/sem_ch3.adb
sem_ch12.adb, [...]: New calling sequence for Analyze_Aspect_Specifications
[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-- --
ed2233dc 9-- Copyright (C) 1992-2010, 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;
1fb00064 64with Sem_Prag; use Sem_Prag;
996ae0b0
RK
65with Sem_Res; use Sem_Res;
66with Sem_Smem; use Sem_Smem;
67with Sem_Type; use Sem_Type;
68with Sem_Util; use Sem_Util;
fbf5a39b 69with Sem_Warn; use Sem_Warn;
996ae0b0
RK
70with Stand; use Stand;
71with Sinfo; use Sinfo;
b4d7b435 72with Sinput; use Sinput;
996ae0b0 73with Snames; use Snames;
653da906 74with Targparm; use Targparm;
996ae0b0
RK
75with Tbuild; use Tbuild;
76with Ttypes; use Ttypes;
77with Uintp; use Uintp;
78with Urealp; use Urealp;
79
80package body Sem_Ch3 is
81
82 -----------------------
83 -- Local Subprograms --
84 -----------------------
85
88b32fc3 86 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
758c442c
GD
87 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
88 -- abstract interface types implemented by a record type or a derived
89 -- record type.
90
996ae0b0
RK
91 procedure Build_Derived_Type
92 (N : Node_Id;
93 Parent_Type : Entity_Id;
94 Derived_Type : Entity_Id;
95 Is_Completion : Boolean;
96 Derive_Subps : Boolean := True);
9dfd2ff8
CC
97 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
98 -- the N_Full_Type_Declaration node containing the derived type definition.
99 -- Parent_Type is the entity for the parent type in the derived type
100 -- definition and Derived_Type the actual derived type. Is_Completion must
101 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
f3d57416 102 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
9dfd2ff8
CC
103 -- completion of a private type declaration. If Is_Completion is set to
104 -- True, N is the completion of a private type declaration and Derived_Type
105 -- is different from the defining identifier inside N (i.e. Derived_Type /=
106 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
107 -- subprograms should be derived. The only case where this parameter is
108 -- False is when Build_Derived_Type is recursively called to process an
109 -- implicit derived full type for a type derived from a private type (in
110 -- that case the subprograms must only be derived for the private view of
111 -- the type).
fea9e956 112 --
44d6a706 113 -- ??? These flags need a bit of re-examination and re-documentation:
996ae0b0
RK
114 -- ??? are they both necessary (both seem related to the recursion)?
115
116 procedure Build_Derived_Access_Type
117 (N : Node_Id;
118 Parent_Type : Entity_Id;
119 Derived_Type : Entity_Id);
120 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
121 -- create an implicit base if the parent type is constrained or if the
122 -- subtype indication has a constraint.
123
124 procedure Build_Derived_Array_Type
125 (N : Node_Id;
126 Parent_Type : Entity_Id;
127 Derived_Type : Entity_Id);
128 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
129 -- create an implicit base if the parent type is constrained or if the
130 -- subtype indication has a constraint.
131
132 procedure Build_Derived_Concurrent_Type
133 (N : Node_Id;
134 Parent_Type : Entity_Id;
135 Derived_Type : Entity_Id);
88b32fc3
BD
136 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
137 -- protected type, inherit entries and protected subprograms, check
138 -- legality of discriminant constraints if any.
996ae0b0
RK
139
140 procedure Build_Derived_Enumeration_Type
141 (N : Node_Id;
142 Parent_Type : Entity_Id;
143 Derived_Type : Entity_Id);
144 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
145 -- type, we must create a new list of literals. Types derived from
94fd3dc6 146 -- Character and [Wide_]Wide_Character are special-cased.
996ae0b0
RK
147
148 procedure Build_Derived_Numeric_Type
149 (N : Node_Id;
150 Parent_Type : Entity_Id;
151 Derived_Type : Entity_Id);
152 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
153 -- an anonymous base type, and propagate constraint to subtype if needed.
154
155 procedure Build_Derived_Private_Type
71d9e9f2
ES
156 (N : Node_Id;
157 Parent_Type : Entity_Id;
158 Derived_Type : Entity_Id;
996ae0b0
RK
159 Is_Completion : Boolean;
160 Derive_Subps : Boolean := True);
fbf5a39b 161 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
996ae0b0
RK
162 -- because the parent may or may not have a completion, and the derivation
163 -- may itself be a completion.
164
165 procedure Build_Derived_Record_Type
166 (N : Node_Id;
167 Parent_Type : Entity_Id;
168 Derived_Type : Entity_Id;
169 Derive_Subps : Boolean := True);
9dfd2ff8 170 -- Subsidiary procedure for Build_Derived_Type and
996ae0b0
RK
171 -- Analyze_Private_Extension_Declaration used for tagged and untagged
172 -- record types. All parameters are as in Build_Derived_Type except that
173 -- N, in addition to being an N_Full_Type_Declaration node, can also be an
174 -- N_Private_Extension_Declaration node. See the definition of this routine
175 -- for much more info. Derive_Subps indicates whether subprograms should
176 -- be derived from the parent type. The only case where Derive_Subps is
177 -- False is for an implicit derived full type for a type derived from a
178 -- private type (see Build_Derived_Type).
179
996ae0b0
RK
180 procedure Build_Discriminal (Discrim : Entity_Id);
181 -- Create the discriminal corresponding to discriminant Discrim, that is
182 -- the parameter corresponding to Discrim to be used in initialization
183 -- procedures for the type where Discrim is a discriminant. Discriminals
184 -- are not used during semantic analysis, and are not fully defined
185 -- entities until expansion. Thus they are not given a scope until
44d6a706 186 -- initialization procedures are built.
996ae0b0
RK
187
188 function Build_Discriminant_Constraints
189 (T : Entity_Id;
190 Def : Node_Id;
b0f26df5 191 Derived_Def : Boolean := False) return Elist_Id;
2b73cf68
JM
192 -- Validate discriminant constraints and return the list of the constraints
193 -- in order of discriminant declarations, where T is the discriminated
194 -- unconstrained type. Def is the N_Subtype_Indication node where the
195 -- discriminants constraints for T are specified. Derived_Def is True
196 -- when building the discriminant constraints in a derived type definition
197 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
198 -- type and Def is the constraint "(xxx)" on T and this routine sets the
199 -- Corresponding_Discriminant field of the discriminants in the derived
200 -- type D to point to the corresponding discriminants in the parent type T.
996ae0b0
RK
201
202 procedure Build_Discriminated_Subtype
203 (T : Entity_Id;
204 Def_Id : Entity_Id;
205 Elist : Elist_Id;
206 Related_Nod : Node_Id;
207 For_Access : Boolean := False);
208 -- Subsidiary procedure to Constrain_Discriminated_Type and to
209 -- Process_Incomplete_Dependents. Given
210 --
211 -- T (a possibly discriminated base type)
212 -- Def_Id (a very partially built subtype for T),
213 --
214 -- the call completes Def_Id to be the appropriate E_*_Subtype.
215 --
dc06abec
RD
216 -- The Elist is the list of discriminant constraints if any (it is set
217 -- to No_Elist if T is not a discriminated type, and to an empty list if
996ae0b0
RK
218 -- T has discriminants but there are no discriminant constraints). The
219 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
220 -- The For_Access says whether or not this subtype is really constraining
221 -- an access type. That is its sole purpose is the designated type of an
222 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
223 -- is built to avoid freezing T when the access subtype is frozen.
224
225 function Build_Scalar_Bound
226 (Bound : Node_Id;
227 Par_T : Entity_Id;
b0f26df5 228 Der_T : Entity_Id) return Node_Id;
996ae0b0
RK
229 -- The bounds of a derived scalar type are conversions of the bounds of
230 -- the parent type. Optimize the representation if the bounds are literals.
231 -- Needs a more complete spec--what are the parameters exactly, and what
232 -- exactly is the returned value, and how is Bound affected???
233
234 procedure Build_Underlying_Full_View
235 (N : Node_Id;
236 Typ : Entity_Id;
237 Par : Entity_Id);
238 -- If the completion of a private type is itself derived from a private
239 -- type, or if the full view of a private subtype is itself private, the
240 -- back-end has no way to compute the actual size of this type. We build
241 -- an internal subtype declaration of the proper parent type to convey
242 -- this information. This extra mechanism is needed because a full
243 -- view cannot itself have a full view (it would get clobbered during
244 -- view exchanges).
245
246 procedure Check_Access_Discriminant_Requires_Limited
247 (D : Node_Id;
248 Loc : Node_Id);
249 -- Check the restriction that the type to which an access discriminant
250 -- belongs must be a concurrent type or a descendant of a type with
251 -- the reserved word 'limited' in its declaration.
252
fea9e956
ES
253 procedure Check_Anonymous_Access_Components
254 (Typ_Decl : Node_Id;
255 Typ : Entity_Id;
256 Prev : Entity_Id;
257 Comp_List : Node_Id);
258 -- Ada 2005 AI-382: an access component in a record definition can refer to
259 -- the enclosing record, in which case it denotes the type itself, and not
260 -- the current instance of the type. We create an anonymous access type for
261 -- the component, and flag it as an access to a component, so accessibility
262 -- checks are properly performed on it. The declaration of the access type
263 -- is placed ahead of that of the record to prevent order-of-elaboration
264 -- circularity issues in Gigi. We create an incomplete type for the record
265 -- declaration, which is the designated type of the anonymous access.
266
996ae0b0 267 procedure Check_Delta_Expression (E : Node_Id);
fea9e956
ES
268 -- Check that the expression represented by E is suitable for use as a
269 -- delta expression, i.e. it is of real type and is static.
996ae0b0
RK
270
271 procedure Check_Digits_Expression (E : Node_Id);
fea9e956
ES
272 -- Check that the expression represented by E is suitable for use as a
273 -- digits expression, i.e. it is of integer type, positive and static.
996ae0b0 274
996ae0b0 275 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
fea9e956
ES
276 -- Validate the initialization of an object declaration. T is the required
277 -- type, and Exp is the initialization expression.
996ae0b0 278
ce2b6ba5
JM
279 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
280 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
281
fbf5a39b
AC
282 procedure Check_Or_Process_Discriminants
283 (N : Node_Id;
284 T : Entity_Id;
285 Prev : Entity_Id := Empty);
8e4dac80
TQ
286 -- If N is the full declaration of the completion T of an incomplete or
287 -- private type, check its discriminants (which are already known to be
288 -- conformant with those of the partial view, see Find_Type_Name),
289 -- otherwise process them. Prev is the entity of the partial declaration,
290 -- if any.
996ae0b0
RK
291
292 procedure Check_Real_Bound (Bound : Node_Id);
293 -- Check given bound for being of real type and static. If not, post an
294 -- appropriate message, and rewrite the bound with the real literal zero.
295
296 procedure Constant_Redeclaration
297 (Id : Entity_Id;
298 N : Node_Id;
299 T : out Entity_Id);
300 -- Various checks on legality of full declaration of deferred constant.
301 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
302 -- node. The caller has not yet set any attributes of this entity.
303
dc06abec
RD
304 function Contain_Interface
305 (Iface : Entity_Id;
306 Ifaces : Elist_Id) return Boolean;
307 -- Ada 2005: Determine whether Iface is present in the list Ifaces
308
996ae0b0
RK
309 procedure Convert_Scalar_Bounds
310 (N : Node_Id;
311 Parent_Type : Entity_Id;
312 Derived_Type : Entity_Id;
313 Loc : Source_Ptr);
fea9e956
ES
314 -- For derived scalar types, convert the bounds in the type definition to
315 -- the derived type, and complete their analysis. Given a constraint of the
316 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
317 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
318 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
319 -- subtype are conversions of those bounds to the derived_type, so that
320 -- their typing is consistent.
996ae0b0
RK
321
322 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
fea9e956
ES
323 -- Copies attributes from array base type T2 to array base type T1. Copies
324 -- only attributes that apply to base types, but not subtypes.
996ae0b0
RK
325
326 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
327 -- Copies attributes from array subtype T2 to array subtype T1. Copies
328 -- attributes that apply to both subtypes and base types.
329
330 procedure Create_Constrained_Components
331 (Subt : Entity_Id;
332 Decl_Node : Node_Id;
333 Typ : Entity_Id;
334 Constraints : Elist_Id);
335 -- Build the list of entities for a constrained discriminated record
336 -- subtype. If a component depends on a discriminant, replace its subtype
ce4a6e84
RD
337 -- using the discriminant values in the discriminant constraint. Subt
338 -- is the defining identifier for the subtype whose list of constrained
339 -- entities we will create. Decl_Node is the type declaration node where
340 -- we will attach all the itypes created. Typ is the base discriminated
341 -- type for the subtype Subt. Constraints is the list of discriminant
fea9e956 342 -- constraints for Typ.
996ae0b0
RK
343
344 function Constrain_Component_Type
c6823a20 345 (Comp : Entity_Id;
996ae0b0
RK
346 Constrained_Typ : Entity_Id;
347 Related_Node : Node_Id;
348 Typ : Entity_Id;
b0f26df5 349 Constraints : Elist_Id) return Entity_Id;
996ae0b0 350 -- Given a discriminated base type Typ, a list of discriminant constraint
c6823a20 351 -- Constraints for Typ and a component of Typ, with type Compon_Type,
996ae0b0 352 -- create and return the type corresponding to Compon_type where all
fea9e956
ES
353 -- discriminant references are replaced with the corresponding constraint.
354 -- If no discriminant references occur in Compon_Typ then return it as is.
355 -- Constrained_Typ is the final constrained subtype to which the
356 -- constrained Compon_Type belongs. Related_Node is the node where we will
357 -- attach all the itypes created.
ce4a6e84 358 --
fea9e956 359 -- Above description is confused, what is Compon_Type???
996ae0b0
RK
360
361 procedure Constrain_Access
362 (Def_Id : in out Entity_Id;
363 S : Node_Id;
364 Related_Nod : Node_Id);
9dfd2ff8
CC
365 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
366 -- an anonymous type created for a subtype indication. In that case it is
367 -- created in the procedure and attached to Related_Nod.
996ae0b0
RK
368
369 procedure Constrain_Array
370 (Def_Id : in out Entity_Id;
371 SI : Node_Id;
372 Related_Nod : Node_Id;
373 Related_Id : Entity_Id;
374 Suffix : Character);
375 -- Apply a list of index constraints to an unconstrained array type. The
376 -- first parameter is the entity for the resulting subtype. A value of
377 -- Empty for Def_Id indicates that an implicit type must be created, but
378 -- creation is delayed (and must be done by this procedure) because other
379 -- subsidiary implicit types must be created first (which is why Def_Id
07fc65c4
GB
380 -- is an in/out parameter). The second parameter is a subtype indication
381 -- node for the constrained array to be created (e.g. something of the
382 -- form string (1 .. 10)). Related_Nod gives the place where this type
383 -- has to be inserted in the tree. The Related_Id and Suffix parameters
384 -- are used to build the associated Implicit type name.
996ae0b0
RK
385
386 procedure Constrain_Concurrent
387 (Def_Id : in out Entity_Id;
388 SI : Node_Id;
389 Related_Nod : Node_Id;
390 Related_Id : Entity_Id;
391 Suffix : Character);
392 -- Apply list of discriminant constraints to an unconstrained concurrent
393 -- type.
394 --
395 -- SI is the N_Subtype_Indication node containing the constraint and
396 -- the unconstrained type to constrain.
397 --
a5b62485
AC
398 -- Def_Id is the entity for the resulting constrained subtype. A value
399 -- of Empty for Def_Id indicates that an implicit type must be created,
400 -- but creation is delayed (and must be done by this procedure) because
401 -- other subsidiary implicit types must be created first (which is why
402 -- Def_Id is an in/out parameter).
996ae0b0
RK
403 --
404 -- Related_Nod gives the place where this type has to be inserted
405 -- in the tree
406 --
407 -- The last two arguments are used to create its external name if needed.
408
409 function Constrain_Corresponding_Record
410 (Prot_Subt : Entity_Id;
411 Corr_Rec : Entity_Id;
412 Related_Nod : Node_Id;
b0f26df5 413 Related_Id : Entity_Id) return Entity_Id;
996ae0b0
RK
414 -- When constraining a protected type or task type with discriminants,
415 -- constrain the corresponding record with the same discriminant values.
416
07fc65c4 417 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
996ae0b0
RK
418 -- Constrain a decimal fixed point type with a digits constraint and/or a
419 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
420
421 procedure Constrain_Discriminated_Type
422 (Def_Id : Entity_Id;
423 S : Node_Id;
424 Related_Nod : Node_Id;
425 For_Access : Boolean := False);
426 -- Process discriminant constraints of composite type. Verify that values
427 -- have been provided for all discriminants, that the original type is
428 -- unconstrained, and that the types of the supplied expressions match
429 -- the discriminant types. The first three parameters are like in routine
fbf5a39b 430 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
996ae0b0
RK
431 -- of For_Access.
432
07fc65c4 433 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
9dfd2ff8
CC
434 -- Constrain an enumeration type with a range constraint. This is identical
435 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
996ae0b0 436
07fc65c4 437 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
996ae0b0
RK
438 -- Constrain a floating point type with either a digits constraint
439 -- and/or a range constraint, building a E_Floating_Point_Subtype.
440
441 procedure Constrain_Index
442 (Index : Node_Id;
443 S : Node_Id;
444 Related_Nod : Node_Id;
445 Related_Id : Entity_Id;
446 Suffix : Character;
447 Suffix_Index : Nat);
ea034236 448 -- Process an index constraint S in a constrained array declaration. The
fea9e956
ES
449 -- constraint can be a subtype name, or a range with or without an explicit
450 -- subtype mark. The index is the corresponding index of the unconstrained
451 -- array. The Related_Id and Suffix parameters are used to build the
452 -- associated Implicit type name.
996ae0b0 453
07fc65c4 454 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
ffe9aba8 455 -- Build subtype of a signed or modular integer type
996ae0b0 456
07fc65c4 457 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
996ae0b0
RK
458 -- Constrain an ordinary fixed point type with a range constraint, and
459 -- build an E_Ordinary_Fixed_Point_Subtype entity.
460
fbf5a39b 461 procedure Copy_And_Swap (Priv, Full : Entity_Id);
fea9e956
ES
462 -- Copy the Priv entity into the entity of its full declaration then swap
463 -- the two entities in such a manner that the former private type is now
464 -- seen as a full type.
996ae0b0 465
996ae0b0
RK
466 procedure Decimal_Fixed_Point_Type_Declaration
467 (T : Entity_Id;
468 Def : Node_Id);
469 -- Create a new decimal fixed point type, and apply the constraint to
470 -- obtain a subtype of this new type.
471
472 procedure Complete_Private_Subtype
473 (Priv : Entity_Id;
474 Full : Entity_Id;
475 Full_Base : Entity_Id;
476 Related_Nod : Node_Id);
9dfd2ff8
CC
477 -- Complete the implicit full view of a private subtype by setting the
478 -- appropriate semantic fields. If the full view of the parent is a record
479 -- type, build constrained components of subtype.
996ae0b0 480
ce2b6ba5 481 procedure Derive_Progenitor_Subprograms
88b32fc3 482 (Parent_Type : Entity_Id;
ce2b6ba5
JM
483 Tagged_Type : Entity_Id);
484 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
485 -- operations of progenitors of Tagged_Type, and replace the subsidiary
486 -- subtypes with Tagged_Type, to build the specs of the inherited interface
487 -- primitives. The derived primitives are aliased to those of the
4818e7b9
RD
488 -- interface. This routine takes care also of transferring to the full view
489 -- subprograms associated with the partial view of Tagged_Type that cover
ce2b6ba5 490 -- interface primitives.
758c442c 491
996ae0b0
RK
492 procedure Derived_Standard_Character
493 (N : Node_Id;
494 Parent_Type : Entity_Id;
495 Derived_Type : Entity_Id);
496 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
497 -- derivations from types Standard.Character and Standard.Wide_Character.
498
499 procedure Derived_Type_Declaration
500 (T : Entity_Id;
501 N : Node_Id;
502 Is_Completion : Boolean);
ce4a6e84
RD
503 -- Process a derived type declaration. Build_Derived_Type is invoked
504 -- to process the actual derived type definition. Parameters N and
505 -- Is_Completion have the same meaning as in Build_Derived_Type.
506 -- T is the N_Defining_Identifier for the entity defined in the
507 -- N_Full_Type_Declaration node N, that is T is the derived type.
996ae0b0 508
996ae0b0 509 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
9dfd2ff8
CC
510 -- Insert each literal in symbol table, as an overloadable identifier. Each
511 -- enumeration type is mapped into a sequence of integers, and each literal
512 -- is defined as a constant with integer value. If any of the literals are
513 -- character literals, the type is a character type, which means that
514 -- strings are legal aggregates for arrays of components of the type.
996ae0b0 515
fbf5a39b
AC
516 function Expand_To_Stored_Constraint
517 (Typ : Entity_Id;
b0f26df5 518 Constraint : Elist_Id) return Elist_Id;
ce4a6e84 519 -- Given a constraint (i.e. a list of expressions) on the discriminants of
9dfd2ff8
CC
520 -- Typ, expand it into a constraint on the stored discriminants and return
521 -- the new list of expressions constraining the stored discriminants.
996ae0b0
RK
522
523 function Find_Type_Of_Object
524 (Obj_Def : Node_Id;
b0f26df5 525 Related_Nod : Node_Id) return Entity_Id;
996ae0b0
RK
526 -- Get type entity for object referenced by Obj_Def, attaching the
527 -- implicit types generated to Related_Nod
528
529 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
ce4a6e84 530 -- Create a new float and apply the constraint to obtain subtype of it
996ae0b0
RK
531
532 function Has_Range_Constraint (N : Node_Id) return Boolean;
533 -- Given an N_Subtype_Indication node N, return True if a range constraint
534 -- is present, either directly, or as part of a digits or delta constraint.
535 -- In addition, a digits constraint in the decimal case returns True, since
536 -- it establishes a default range if no explicit range is present.
537
88b32fc3
BD
538 function Inherit_Components
539 (N : Node_Id;
540 Parent_Base : Entity_Id;
541 Derived_Base : Entity_Id;
542 Is_Tagged : Boolean;
543 Inherit_Discr : Boolean;
544 Discs : Elist_Id) return Elist_Id;
545 -- Called from Build_Derived_Record_Type to inherit the components of
546 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
547 -- For more information on derived types and component inheritance please
548 -- consult the comment above the body of Build_Derived_Record_Type.
549 --
550 -- N is the original derived type declaration
551 --
552 -- Is_Tagged is set if we are dealing with tagged types
553 --
fea9e956
ES
554 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
555 -- Parent_Base, otherwise no discriminants are inherited.
88b32fc3
BD
556 --
557 -- Discs gives the list of constraints that apply to Parent_Base in the
558 -- derived type declaration. If Discs is set to No_Elist, then we have
559 -- the following situation:
560 --
561 -- type Parent (D1..Dn : ..) is [tagged] record ...;
562 -- type Derived is new Parent [with ...];
563 --
564 -- which gets treated as
565 --
566 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
567 --
568 -- For untagged types the returned value is an association list. The list
569 -- starts from the association (Parent_Base => Derived_Base), and then it
570 -- contains a sequence of the associations of the form
571 --
572 -- (Old_Component => New_Component),
573 --
fea9e956
ES
574 -- where Old_Component is the Entity_Id of a component in Parent_Base and
575 -- New_Component is the Entity_Id of the corresponding component in
88b32fc3
BD
576 -- Derived_Base. For untagged records, this association list is needed when
577 -- copying the record declaration for the derived base. In the tagged case
578 -- the value returned is irrelevant.
579
996ae0b0
RK
580 function Is_Valid_Constraint_Kind
581 (T_Kind : Type_Kind;
b0f26df5 582 Constraint_Kind : Node_Kind) return Boolean;
9dfd2ff8
CC
583 -- Returns True if it is legal to apply the given kind of constraint to the
584 -- given kind of type (index constraint to an array type, for example).
996ae0b0
RK
585
586 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
8dc2ddaf 587 -- Create new modular type. Verify that modulus is in bounds and is
996ae0b0
RK
588 -- a power of two (implementation restriction).
589
6c1e24d3 590 procedure New_Concatenation_Op (Typ : Entity_Id);
996ae0b0 591 -- Create an abbreviated declaration for an operator in order to
6c1e24d3 592 -- materialize concatenation on array types.
996ae0b0
RK
593
594 procedure Ordinary_Fixed_Point_Type_Declaration
595 (T : Entity_Id;
596 Def : Node_Id);
9dfd2ff8
CC
597 -- Create a new ordinary fixed point type, and apply the constraint to
598 -- obtain subtype of it.
996ae0b0
RK
599
600 procedure Prepare_Private_Subtype_Completion
601 (Id : Entity_Id;
602 Related_Nod : Node_Id);
603 -- Id is a subtype of some private type. Creates the full declaration
604 -- associated with Id whenever possible, i.e. when the full declaration
605 -- of the base type is already known. Records each subtype into
606 -- Private_Dependents of the base type.
607
608 procedure Process_Incomplete_Dependents
609 (N : Node_Id;
610 Full_T : Entity_Id;
611 Inc_T : Entity_Id);
612 -- Process all entities that depend on an incomplete type. There include
613 -- subtypes, subprogram types that mention the incomplete type in their
614 -- profiles, and subprogram with access parameters that designate the
615 -- incomplete type.
616
617 -- Inc_T is the defining identifier of an incomplete type declaration, its
618 -- Ekind is E_Incomplete_Type.
619 --
620 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
621 --
622 -- Full_T is N's defining identifier.
623 --
624 -- Subtypes of incomplete types with discriminants are completed when the
625 -- parent type is. This is simpler than private subtypes, because they can
626 -- only appear in the same scope, and there is no need to exchange views.
627 -- Similarly, access_to_subprogram types may have a parameter or a return
628 -- type that is an incomplete type, and that must be replaced with the
629 -- full type.
ce4a6e84 630 --
996ae0b0
RK
631 -- If the full type is tagged, subprogram with access parameters that
632 -- designated the incomplete may be primitive operations of the full type,
633 -- and have to be processed accordingly.
634
635 procedure Process_Real_Range_Specification (Def : Node_Id);
ce4a6e84
RD
636 -- Given the type definition for a real type, this procedure processes and
637 -- checks the real range specification of this type definition if one is
638 -- present. If errors are found, error messages are posted, and the
639 -- Real_Range_Specification of Def is reset to Empty.
996ae0b0 640
fbf5a39b
AC
641 procedure Record_Type_Declaration
642 (T : Entity_Id;
643 N : Node_Id;
644 Prev : Entity_Id);
996ae0b0
RK
645 -- Process a record type declaration (for both untagged and tagged
646 -- records). Parameters T and N are exactly like in procedure
9dfd2ff8
CC
647 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
648 -- for this routine. If this is the completion of an incomplete type
649 -- declaration, Prev is the entity of the incomplete declaration, used for
650 -- cross-referencing. Otherwise Prev = T.
996ae0b0 651
fbf5a39b 652 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
ce4a6e84
RD
653 -- This routine is used to process the actual record type definition (both
654 -- for untagged and tagged records). Def is a record type definition node.
655 -- This procedure analyzes the components in this record type definition.
656 -- Prev_T is the entity for the enclosing record type. It is provided so
657 -- that its Has_Task flag can be set if any of the component have Has_Task
658 -- set. If the declaration is the completion of an incomplete type
659 -- declaration, Prev_T is the original incomplete type, whose full view is
660 -- the record type.
996ae0b0 661
07fc65c4
GB
662 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
663 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
664 -- build a copy of the declaration tree of the parent, and we create
665 -- independently the list of components for the derived type. Semantic
666 -- information uses the component entities, but record representation
667 -- clauses are validated on the declaration tree. This procedure replaces
668 -- discriminants and components in the declaration with those that have
669 -- been created by Inherit_Components.
670
996ae0b0
RK
671 procedure Set_Fixed_Range
672 (E : Entity_Id;
673 Loc : Source_Ptr;
674 Lo : Ureal;
675 Hi : Ureal);
676 -- Build a range node with the given bounds and set it as the Scalar_Range
677 -- of the given fixed-point type entity. Loc is the source location used
678 -- for the constructed range. See body for further details.
679
680 procedure Set_Scalar_Range_For_Subtype
07fc65c4
GB
681 (Def_Id : Entity_Id;
682 R : Node_Id;
683 Subt : Entity_Id);
57193e09
TQ
684 -- This routine is used to set the scalar range field for a subtype given
685 -- Def_Id, the entity for the subtype, and R, the range expression for the
686 -- scalar range. Subt provides the parent subtype to be used to analyze,
687 -- resolve, and check the given range.
996ae0b0
RK
688
689 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
690 -- Create a new signed integer entity, and apply the constraint to obtain
691 -- the required first named subtype of this type.
692
fbf5a39b
AC
693 procedure Set_Stored_Constraint_From_Discriminant_Constraint
694 (E : Entity_Id);
695 -- E is some record type. This routine computes E's Stored_Constraint
696 -- from its Discriminant_Constraint.
697
6765b310
ES
698 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
699 -- Check that an entity in a list of progenitors is an interface,
700 -- emit error otherwise.
701
996ae0b0
RK
702 -----------------------
703 -- Access_Definition --
704 -----------------------
705
706 function Access_Definition
707 (Related_Nod : Node_Id;
b0f26df5 708 N : Node_Id) return Entity_Id
996ae0b0 709 is
550f4135
AC
710 Loc : constant Source_Ptr := Sloc (Related_Nod);
711 Anon_Type : Entity_Id;
712 Anon_Scope : Entity_Id;
713 Desig_Type : Entity_Id;
714 Decl : Entity_Id;
715 Enclosing_Prot_Type : Entity_Id := Empty;
996ae0b0
RK
716
717 begin
7ff2d234
AC
718 -- Access type is not allowed in SPARK or ALFA
719
720 if Formal_Verification_Mode
721 and then Comes_From_Source (N)
722 then
723 Error_Msg_F ("|~~access type is not allowed", N);
724 end if;
725
726 -- Proceed with analysis
727
996ae0b0
RK
728 if Is_Entry (Current_Scope)
729 and then Is_Task_Type (Etype (Scope (Current_Scope)))
730 then
731 Error_Msg_N ("task entries cannot have access parameters", N);
fea9e956 732 return Empty;
996ae0b0
RK
733 end if;
734
57193e09
TQ
735 -- Ada 2005: for an object declaration the corresponding anonymous
736 -- type is declared in the current scope.
758c442c 737
88b32fc3
BD
738 -- If the access definition is the return type of another access to
739 -- function, scope is the current one, because it is the one of the
740 -- current type declaration.
741
7d7af38a
JM
742 if Nkind_In (Related_Nod, N_Object_Declaration,
743 N_Access_Function_Definition)
88b32fc3 744 then
2b73cf68 745 Anon_Scope := Current_Scope;
9dfd2ff8 746
fea9e956
ES
747 -- For the anonymous function result case, retrieve the scope of the
748 -- function specification's associated entity rather than using the
749 -- current scope. The current scope will be the function itself if the
750 -- formal part is currently being analyzed, but will be the parent scope
751 -- in the case of a parameterless function, and we always want to use
752 -- the function's parent scope. Finally, if the function is a child
f3d57416 753 -- unit, we must traverse the tree to retrieve the proper entity.
9dfd2ff8
CC
754
755 elsif Nkind (Related_Nod) = N_Function_Specification
7d7af38a 756 and then Nkind (Parent (N)) /= N_Parameter_Specification
9dfd2ff8 757 then
2b73cf68
JM
758 -- If the current scope is a protected type, the anonymous access
759 -- is associated with one of the protected operations, and must
760 -- be available in the scope that encloses the protected declaration.
16b05213 761 -- Otherwise the type is in the scope enclosing the subprogram.
16c5f1c6 762
550f4135
AC
763 -- If the function has formals, The return type of a subprogram
764 -- declaration is analyzed in the scope of the subprogram (see
765 -- Process_Formals) and thus the protected type, if present, is
766 -- the scope of the current function scope.
2b73cf68
JM
767
768 if Ekind (Current_Scope) = E_Protected_Type then
550f4135
AC
769 Enclosing_Prot_Type := Current_Scope;
770
771 elsif Ekind (Current_Scope) = E_Function
772 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
773 then
774 Enclosing_Prot_Type := Scope (Current_Scope);
775 end if;
776
777 if Present (Enclosing_Prot_Type) then
778 Anon_Scope := Scope (Enclosing_Prot_Type);
779
2b73cf68
JM
780 else
781 Anon_Scope := Scope (Defining_Entity (Related_Nod));
782 end if;
57193e09
TQ
783
784 else
fea9e956
ES
785 -- For access formals, access components, and access discriminants,
786 -- the scope is that of the enclosing declaration,
57193e09 787
2b73cf68 788 Anon_Scope := Scope (Current_Scope);
758c442c
GD
789 end if;
790
2b73cf68
JM
791 Anon_Type :=
792 Create_Itype
b87971f3 793 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
2b73cf68 794
758c442c 795 if All_Present (N)
0791fbe9 796 and then Ada_Version >= Ada_2005
758c442c
GD
797 then
798 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
799 end if;
800
fea9e956
ES
801 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
802 -- the corresponding semantic routine
7324bf49
AC
803
804 if Present (Access_To_Subprogram_Definition (N)) then
805 Access_Subprogram_Declaration
806 (T_Name => Anon_Type,
807 T_Def => Access_To_Subprogram_Definition (N));
af4b9434
AC
808
809 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
810 Set_Ekind
811 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
812 else
813 Set_Ekind
814 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
815 end if;
816
7d7af38a
JM
817 Set_Can_Use_Internal_Rep
818 (Anon_Type, not Always_Compatible_Rep_On_Target);
819
2b73cf68
JM
820 -- If the anonymous access is associated with a protected operation
821 -- create a reference to it after the enclosing protected definition
822 -- because the itype will be used in the subsequent bodies.
823
824 if Ekind (Current_Scope) = E_Protected_Type then
825 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
826 end if;
827
7324bf49
AC
828 return Anon_Type;
829 end if;
830
996ae0b0
RK
831 Find_Type (Subtype_Mark (N));
832 Desig_Type := Entity (Subtype_Mark (N));
833
b87971f3 834 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
c0985d4e 835 Set_Etype (Anon_Type, Anon_Type);
ce4a6e84
RD
836
837 -- Make sure the anonymous access type has size and alignment fields
838 -- set, as required by gigi. This is necessary in the case of the
839 -- Task_Body_Procedure.
840
841 if not Has_Private_Component (Desig_Type) then
842 Layout_Type (Anon_Type);
843 end if;
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
0791fbe9 849 if Ada_Version >= Ada_2005 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)
44bf8eb0 900 and then not Restriction_Active (No_Task_Hierarchy)
88b32fc3
BD
901 then
902 if not Has_Master_Entity (Current_Scope) then
903 Decl :=
904 Make_Object_Declaration (Loc,
905 Defining_Identifier =>
906 Make_Defining_Identifier (Loc, Name_uMaster),
907 Constant_Present => True,
908 Object_Definition =>
909 New_Reference_To (RTE (RE_Master_Id), Loc),
910 Expression =>
911 Make_Explicit_Dereference (Loc,
912 New_Reference_To (RTE (RE_Current_Master), Loc)));
913
914 Insert_Before (Related_Nod, Decl);
915 Analyze (Decl);
916
917 Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
918 Set_Has_Master_Entity (Current_Scope);
919 else
920 Build_Master_Renaming (Related_Nod, Anon_Type);
921 end if;
922 end if;
950d3e7d
ES
923 end if;
924
fea9e956
ES
925 -- For a private component of a protected type, it is imperative that
926 -- the back-end elaborate the type immediately after the protected
927 -- declaration, because this type will be used in the declarations
928 -- created for the component within each protected body, so we must
929 -- create an itype reference for it now.
930
931 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
932 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
df89ab66
ES
933
934 -- Similarly, if the access definition is the return result of a
88eb6e62
AC
935 -- function, create an itype reference for it because it will be used
936 -- within the function body. For a regular function that is not a
937 -- compilation unit, insert reference after the declaration. For a
938 -- protected operation, insert it after the enclosing protected type
939 -- declaration. In either case, do not create a reference for a type
940 -- obtained through a limited_with clause, because this would introduce
941 -- semantic dependencies.
942
89c273b4
AC
943 -- Similarly, do not create a reference if the designated type is a
944 -- generic formal, because no use of it will reach the backend.
df89ab66
ES
945
946 elsif Nkind (Related_Nod) = N_Function_Specification
c0985d4e 947 and then not From_With_Type (Desig_Type)
89c273b4 948 and then not Is_Generic_Type (Desig_Type)
df89ab66 949 then
550f4135
AC
950 if Present (Enclosing_Prot_Type) then
951 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
0f5177ad
ES
952
953 elsif Is_List_Member (Parent (Related_Nod))
954 and then Nkind (Parent (N)) /= N_Parameter_Specification
955 then
956 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
957 end if;
df89ab66 958
88eb6e62
AC
959 -- Finally, create an itype reference for an object declaration of an
960 -- anonymous access type. This is strictly necessary only for deferred
961 -- constants, but in any case will avoid out-of-scope problems in the
962 -- back-end.
df89ab66
ES
963
964 elsif Nkind (Related_Nod) = N_Object_Declaration then
965 Build_Itype_Reference (Anon_Type, Related_Nod);
fea9e956
ES
966 end if;
967
996ae0b0
RK
968 return Anon_Type;
969 end Access_Definition;
970
971 -----------------------------------
972 -- Access_Subprogram_Declaration --
973 -----------------------------------
974
975 procedure Access_Subprogram_Declaration
976 (T_Name : Entity_Id;
977 T_Def : Node_Id)
978 is
b0f26df5 979
f29b857f 980 procedure Check_For_Premature_Usage (Def : Node_Id);
8dbf3473
AC
981 -- Check that type T_Name is not used, directly or recursively, as a
982 -- parameter or a return type in Def. Def is either a subtype, an
983 -- access_definition, or an access_to_subprogram_definition.
f29b857f
ES
984
985 -------------------------------
986 -- Check_For_Premature_Usage --
987 -------------------------------
988
989 procedure Check_For_Premature_Usage (Def : Node_Id) is
990 Param : Node_Id;
991
992 begin
993 -- Check for a subtype mark
994
995 if Nkind (Def) in N_Has_Etype then
996 if Etype (Def) = T_Name then
997 Error_Msg_N
998 ("type& cannot be used before end of its declaration", Def);
999 end if;
1000
1001 -- If this is not a subtype, then this is an access_definition
1002
1003 elsif Nkind (Def) = N_Access_Definition then
1004 if Present (Access_To_Subprogram_Definition (Def)) then
1005 Check_For_Premature_Usage
1006 (Access_To_Subprogram_Definition (Def));
1007 else
1008 Check_For_Premature_Usage (Subtype_Mark (Def));
1009 end if;
1010
1011 -- The only cases left are N_Access_Function_Definition and
1012 -- N_Access_Procedure_Definition.
1013
1014 else
1015 if Present (Parameter_Specifications (Def)) then
1016 Param := First (Parameter_Specifications (Def));
1017 while Present (Param) loop
1018 Check_For_Premature_Usage (Parameter_Type (Param));
1019 Param := Next (Param);
1020 end loop;
1021 end if;
1022
1023 if Nkind (Def) = N_Access_Function_Definition then
1024 Check_For_Premature_Usage (Result_Definition (Def));
1025 end if;
1026 end if;
1027 end Check_For_Premature_Usage;
1028
1029 -- Local variables
1030
1031 Formals : constant List_Id := Parameter_Specifications (T_Def);
1032 Formal : Entity_Id;
1033 D_Ityp : Node_Id;
996ae0b0 1034 Desig_Type : constant Entity_Id :=
0da2c8ac 1035 Create_Itype (E_Subprogram_Type, Parent (T_Def));
996ae0b0 1036
f29b857f
ES
1037 -- Start of processing for Access_Subprogram_Declaration
1038
996ae0b0 1039 begin
7ff2d234
AC
1040 -- Access type is not allowed in SPARK or ALFA
1041
1042 if Formal_Verification_Mode
1043 and then Comes_From_Source (T_Def)
1044 then
1045 Error_Msg_F ("|~~access type is not allowed", T_Def);
1046 end if;
1047
fea9e956 1048 -- Associate the Itype node with the inner full-type declaration or
e86a3a7e
AC
1049 -- subprogram spec or entry body. This is required to handle nested
1050 -- anonymous declarations. For example:
758c442c
GD
1051
1052 -- procedure P
1053 -- (X : access procedure
1054 -- (Y : access procedure
1055 -- (Z : access T)))
1056
9dfd2ff8 1057 D_Ityp := Associated_Node_For_Itype (Desig_Type);
7d7af38a
JM
1058 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1059 N_Private_Type_Declaration,
1060 N_Private_Extension_Declaration,
1061 N_Procedure_Specification,
e86a3a7e
AC
1062 N_Function_Specification,
1063 N_Entry_Body)
1064
7d7af38a
JM
1065 or else
1066 Nkind_In (D_Ityp, N_Object_Declaration,
1067 N_Object_Renaming_Declaration,
53cf4600 1068 N_Formal_Object_Declaration,
7d7af38a
JM
1069 N_Formal_Type_Declaration,
1070 N_Task_Type_Declaration,
1071 N_Protected_Type_Declaration))
758c442c
GD
1072 loop
1073 D_Ityp := Parent (D_Ityp);
1074 pragma Assert (D_Ityp /= Empty);
1075 end loop;
1076
1077 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1078
7d7af38a
JM
1079 if Nkind_In (D_Ityp, N_Procedure_Specification,
1080 N_Function_Specification)
758c442c 1081 then
88b32fc3 1082 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
758c442c 1083
7d7af38a
JM
1084 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1085 N_Object_Declaration,
1086 N_Object_Renaming_Declaration,
1087 N_Formal_Type_Declaration)
758c442c
GD
1088 then
1089 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1090 end if;
1091
996ae0b0 1092 if Nkind (T_Def) = N_Access_Function_Definition then
9dfd2ff8 1093 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
2b73cf68
JM
1094 declare
1095 Acc : constant Node_Id := Result_Definition (T_Def);
1096
1097 begin
1098 if Present (Access_To_Subprogram_Definition (Acc))
1099 and then
1100 Protected_Present (Access_To_Subprogram_Definition (Acc))
1101 then
1102 Set_Etype
1103 (Desig_Type,
1104 Replace_Anonymous_Access_To_Protected_Subprogram
1105 (T_Def));
1106
1107 else
1108 Set_Etype
1109 (Desig_Type,
1110 Access_Definition (T_Def, Result_Definition (T_Def)));
1111 end if;
1112 end;
1113
9dfd2ff8
CC
1114 else
1115 Analyze (Result_Definition (T_Def));
b66c3ff4
AC
1116
1117 declare
1118 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1119
1120 begin
1121 -- If a null exclusion is imposed on the result type, then
1122 -- create a null-excluding itype (an access subtype) and use
1123 -- it as the function's Etype.
1124
1125 if Is_Access_Type (Typ)
1126 and then Null_Exclusion_In_Return_Present (T_Def)
1127 then
1128 Set_Etype (Desig_Type,
1129 Create_Null_Excluding_Itype
1130 (T => Typ,
1131 Related_Nod => T_Def,
1132 Scope_Id => Current_Scope));
cec29135 1133
b66c3ff4 1134 else
cec29135 1135 if From_With_Type (Typ) then
dd386db0 1136
0f1a6a0b 1137 -- AI05-151: Incomplete types are allowed in all basic
dd386db0
AC
1138 -- declarations, including access to subprograms.
1139
1140 if Ada_Version >= Ada_2012 then
1141 null;
1142
1143 else
1144 Error_Msg_NE
1145 ("illegal use of incomplete type&",
1146 Result_Definition (T_Def), Typ);
1147 end if;
cec29135
ES
1148
1149 elsif Ekind (Current_Scope) = E_Package
1150 and then In_Private_Part (Current_Scope)
1151 then
1152 if Ekind (Typ) = E_Incomplete_Type then
1153 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1154
1155 elsif Is_Class_Wide_Type (Typ)
1156 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1157 then
1158 Append_Elmt
1159 (Desig_Type, Private_Dependents (Etype (Typ)));
1160 end if;
1161 end if;
1162
b66c3ff4
AC
1163 Set_Etype (Desig_Type, Typ);
1164 end if;
1165 end;
9dfd2ff8 1166 end if;
0c644933
AC
1167
1168 if not (Is_Type (Etype (Desig_Type))) then
1169 Error_Msg_N
9dfd2ff8
CC
1170 ("expect type in function specification",
1171 Result_Definition (T_Def));
0c644933 1172 end if;
b0f26df5 1173
996ae0b0
RK
1174 else
1175 Set_Etype (Desig_Type, Standard_Void_Type);
1176 end if;
1177
1178 if Present (Formals) then
2b73cf68 1179 Push_Scope (Desig_Type);
b1c11e0e
JM
1180
1181 -- A bit of a kludge here. These kludges will be removed when Itypes
1182 -- have proper parent pointers to their declarations???
1183
16b05213 1184 -- Kludge 1) Link defining_identifier of formals. Required by
b1c11e0e
JM
1185 -- First_Formal to provide its functionality.
1186
1187 declare
1188 F : Node_Id;
1189
1190 begin
1191 F := First (Formals);
0bb9276c
AC
1192
1193 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1194 -- when it is part of an unconstrained type and subtype expansion
d8b3ccb9 1195 -- is disabled. To avoid back-end problems with shared profiles,
0bb9276c
AC
1196 -- use previous subprogram type as the designated type.
1197
1198 if ASIS_Mode
1199 and then Present (Scope (Defining_Identifier (F)))
1200 then
1201 Set_Etype (T_Name, T_Name);
1202 Init_Size_Align (T_Name);
1203 Set_Directly_Designated_Type (T_Name,
1204 Scope (Defining_Identifier (F)));
1205 return;
1206 end if;
1207
b1c11e0e
JM
1208 while Present (F) loop
1209 if No (Parent (Defining_Identifier (F))) then
1210 Set_Parent (Defining_Identifier (F), F);
1211 end if;
1212
1213 Next (F);
1214 end loop;
1215 end;
1216
07fc65c4 1217 Process_Formals (Formals, Parent (T_Def));
996ae0b0 1218
b1c11e0e
JM
1219 -- Kludge 2) End_Scope requires that the parent pointer be set to
1220 -- something reasonable, but Itypes don't have parent pointers. So
1221 -- we set it and then unset it ???
996ae0b0
RK
1222
1223 Set_Parent (Desig_Type, T_Name);
1224 End_Scope;
1225 Set_Parent (Desig_Type, Empty);
1226 end if;
1227
f29b857f
ES
1228 -- Check for premature usage of the type being defined
1229
1230 Check_For_Premature_Usage (T_Def);
1231
996ae0b0
RK
1232 -- The return type and/or any parameter type may be incomplete. Mark
1233 -- the subprogram_type as depending on the incomplete type, so that
2b73cf68
JM
1234 -- it can be updated when the full type declaration is seen. This
1235 -- only applies to incomplete types declared in some enclosing scope,
1236 -- not to limited views from other packages.
996ae0b0
RK
1237
1238 if Present (Formals) then
1239 Formal := First_Formal (Desig_Type);
996ae0b0 1240 while Present (Formal) loop
996ae0b0
RK
1241 if Ekind (Formal) /= E_In_Parameter
1242 and then Nkind (T_Def) = N_Access_Function_Definition
1243 then
1244 Error_Msg_N ("functions can only have IN parameters", Formal);
1245 end if;
1246
2b73cf68
JM
1247 if Ekind (Etype (Formal)) = E_Incomplete_Type
1248 and then In_Open_Scopes (Scope (Etype (Formal)))
1249 then
996ae0b0
RK
1250 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1251 Set_Has_Delayed_Freeze (Desig_Type);
1252 end if;
1253
1254 Next_Formal (Formal);
1255 end loop;
1256 end if;
1257
49d8b802
ES
1258 -- If the return type is incomplete, this is legal as long as the
1259 -- type is declared in the current scope and will be completed in
1260 -- it (rather than being part of limited view).
1261
996ae0b0
RK
1262 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1263 and then not Has_Delayed_Freeze (Desig_Type)
49d8b802 1264 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
996ae0b0
RK
1265 then
1266 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1267 Set_Has_Delayed_Freeze (Desig_Type);
1268 end if;
1269
1270 Check_Delayed_Subprogram (Desig_Type);
1271
1272 if Protected_Present (T_Def) then
1273 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1274 Set_Convention (Desig_Type, Convention_Protected);
1275 else
1276 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1277 end if;
1278
7d7af38a
JM
1279 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1280
996ae0b0
RK
1281 Set_Etype (T_Name, T_Name);
1282 Init_Size_Align (T_Name);
1283 Set_Directly_Designated_Type (T_Name, Desig_Type);
1284
0ab80019 1285 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
2820d220
AC
1286
1287 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1288
996ae0b0
RK
1289 Check_Restriction (No_Access_Subprograms, T_Def);
1290 end Access_Subprogram_Declaration;
1291
1292 ----------------------------
1293 -- Access_Type_Declaration --
1294 ----------------------------
1295
1296 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1297 S : constant Node_Id := Subtype_Indication (Def);
1298 P : constant Node_Id := Parent (Def);
996ae0b0 1299 begin
7ff2d234
AC
1300 -- Access type is not allowed in SPARK or ALFA
1301
1302 if Formal_Verification_Mode
1303 and then Comes_From_Source (Def)
1304 then
1305 Error_Msg_F ("|~~access type is not allowed", Def);
1306 end if;
1307
996ae0b0
RK
1308 -- Check for permissible use of incomplete type
1309
1310 if Nkind (S) /= N_Subtype_Indication then
1311 Analyze (S);
1312
1313 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1314 Set_Directly_Designated_Type (T, Entity (S));
1315 else
1316 Set_Directly_Designated_Type (T,
1317 Process_Subtype (S, P, T, 'P'));
1318 end if;
1319
1320 else
1321 Set_Directly_Designated_Type (T,
1322 Process_Subtype (S, P, T, 'P'));
1323 end if;
1324
1325 if All_Present (Def) or Constant_Present (Def) then
1326 Set_Ekind (T, E_General_Access_Type);
1327 else
1328 Set_Ekind (T, E_Access_Type);
1329 end if;
1330
1331 if Base_Type (Designated_Type (T)) = T then
1332 Error_Msg_N ("access type cannot designate itself", S);
9dfd2ff8
CC
1333
1334 -- In Ada 2005, the type may have a limited view through some unit
1335 -- in its own context, allowing the following circularity that cannot
1336 -- be detected earlier
1337
1338 elsif Is_Class_Wide_Type (Designated_Type (T))
1339 and then Etype (Designated_Type (T)) = T
1340 then
1341 Error_Msg_N
1342 ("access type cannot designate its own classwide type", S);
950d3e7d
ES
1343
1344 -- Clean up indication of tagged status to prevent cascaded errors
1345
1346 Set_Is_Tagged_Type (T, False);
996ae0b0
RK
1347 end if;
1348
fbf5a39b 1349 Set_Etype (T, T);
996ae0b0
RK
1350
1351 -- If the type has appeared already in a with_type clause, it is
1352 -- frozen and the pointer size is already set. Else, initialize.
1353
1354 if not From_With_Type (T) then
1355 Init_Size_Align (T);
1356 end if;
1357
996ae0b0
RK
1358 -- Note that Has_Task is always false, since the access type itself
1359 -- is not a task type. See Einfo for more description on this point.
1360 -- Exactly the same consideration applies to Has_Controlled_Component.
1361
1362 Set_Has_Task (T, False);
1363 Set_Has_Controlled_Component (T, False);
2820d220 1364
ce4a6e84
RD
1365 -- Initialize Associated_Final_Chain explicitly to Empty, to avoid
1366 -- problems where an incomplete view of this entity has been previously
1367 -- established by a limited with and an overlaid version of this field
1368 -- (Stored_Constraint) was initialized for the incomplete view.
1369
1370 Set_Associated_Final_Chain (T, Empty);
1371
0ab80019 1372 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
2820d220
AC
1373 -- attributes
1374
1375 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1376 Set_Is_Access_Constant (T, Constant_Present (Def));
996ae0b0
RK
1377 end Access_Type_Declaration;
1378
758c442c
GD
1379 ----------------------------------
1380 -- Add_Interface_Tag_Components --
1381 ----------------------------------
1382
88b32fc3 1383 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
758c442c 1384 Loc : constant Source_Ptr := Sloc (N);
758c442c
GD
1385 L : List_Id;
1386 Last_Tag : Node_Id;
fea9e956 1387
758c442c 1388 procedure Add_Tag (Iface : Entity_Id);
88b32fc3 1389 -- Add tag for one of the progenitor interfaces
758c442c
GD
1390
1391 -------------
1392 -- Add_Tag --
1393 -------------
1394
1395 procedure Add_Tag (Iface : Entity_Id) is
57193e09
TQ
1396 Decl : Node_Id;
1397 Def : Node_Id;
1398 Tag : Entity_Id;
1399 Offset : Entity_Id;
758c442c
GD
1400
1401 begin
1402 pragma Assert (Is_Tagged_Type (Iface)
1403 and then Is_Interface (Iface));
1404
4818e7b9
RD
1405 -- This is a reasonable place to propagate predicates
1406
1407 if Has_Predicates (Iface) then
1408 Set_Has_Predicates (Typ);
1409 end if;
1410
758c442c
GD
1411 Def :=
1412 Make_Component_Definition (Loc,
1413 Aliased_Present => True,
1414 Subtype_Indication =>
1415 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1416
092ef350 1417 Tag := Make_Temporary (Loc, 'V');
758c442c
GD
1418
1419 Decl :=
1420 Make_Component_Declaration (Loc,
1421 Defining_Identifier => Tag,
1422 Component_Definition => Def);
1423
1424 Analyze_Component_Declaration (Decl);
1425
1426 Set_Analyzed (Decl);
1427 Set_Ekind (Tag, E_Component);
758c442c 1428 Set_Is_Tag (Tag);
2b73cf68 1429 Set_Is_Aliased (Tag);
7d7af38a 1430 Set_Related_Type (Tag, Iface);
758c442c
GD
1431 Init_Component_Location (Tag);
1432
1433 pragma Assert (Is_Frozen (Iface));
1434
1435 Set_DT_Entry_Count (Tag,
1436 DT_Entry_Count (First_Entity (Iface)));
1437
57193e09 1438 if No (Last_Tag) then
758c442c
GD
1439 Prepend (Decl, L);
1440 else
1441 Insert_After (Last_Tag, Decl);
1442 end if;
1443
1444 Last_Tag := Decl;
57193e09
TQ
1445
1446 -- If the ancestor has discriminants we need to give special support
1447 -- to store the offset_to_top value of the secondary dispatch tables.
1448 -- For this purpose we add a supplementary component just after the
1449 -- field that contains the tag associated with each secondary DT.
1450
1451 if Typ /= Etype (Typ)
1452 and then Has_Discriminants (Etype (Typ))
1453 then
1454 Def :=
1455 Make_Component_Definition (Loc,
1456 Subtype_Indication =>
1457 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1458
092ef350 1459 Offset := Make_Temporary (Loc, 'V');
57193e09
TQ
1460
1461 Decl :=
1462 Make_Component_Declaration (Loc,
1463 Defining_Identifier => Offset,
1464 Component_Definition => Def);
1465
1466 Analyze_Component_Declaration (Decl);
1467
1468 Set_Analyzed (Decl);
1469 Set_Ekind (Offset, E_Component);
2b73cf68 1470 Set_Is_Aliased (Offset);
7d7af38a 1471 Set_Related_Type (Offset, Iface);
57193e09
TQ
1472 Init_Component_Location (Offset);
1473 Insert_After (Last_Tag, Decl);
1474 Last_Tag := Decl;
1475 end if;
758c442c
GD
1476 end Add_Tag;
1477
fea9e956
ES
1478 -- Local variables
1479
ce2b6ba5
JM
1480 Elmt : Elmt_Id;
1481 Ext : Node_Id;
1482 Comp : Node_Id;
fea9e956 1483
9dfd2ff8 1484 -- Start of processing for Add_Interface_Tag_Components
758c442c
GD
1485
1486 begin
2b73cf68
JM
1487 if not RTE_Available (RE_Interface_Tag) then
1488 Error_Msg
1489 ("(Ada 2005) interface types not supported by this run-time!",
1490 Sloc (N));
1491 return;
1492 end if;
1493
758c442c 1494 if Ekind (Typ) /= E_Record_Type
fea9e956
ES
1495 or else (Is_Concurrent_Record_Type (Typ)
1496 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1497 or else (not Is_Concurrent_Record_Type (Typ)
ce2b6ba5
JM
1498 and then No (Interfaces (Typ))
1499 and then Is_Empty_Elmt_List (Interfaces (Typ)))
758c442c
GD
1500 then
1501 return;
1502 end if;
1503
fea9e956
ES
1504 -- Find the current last tag
1505
1506 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1507 Ext := Record_Extension_Part (Type_Definition (N));
1508 else
1509 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1510 Ext := Type_Definition (N);
1511 end if;
758c442c 1512
fea9e956 1513 Last_Tag := Empty;
758c442c 1514
fea9e956
ES
1515 if not (Present (Component_List (Ext))) then
1516 Set_Null_Present (Ext, False);
1517 L := New_List;
1518 Set_Component_List (Ext,
1519 Make_Component_List (Loc,
1520 Component_Items => L,
1521 Null_Present => False));
1522 else
758c442c 1523 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
fea9e956
ES
1524 L := Component_Items
1525 (Component_List
1526 (Record_Extension_Part
1527 (Type_Definition (N))));
758c442c 1528 else
fea9e956
ES
1529 L := Component_Items
1530 (Component_List
1531 (Type_Definition (N)));
758c442c
GD
1532 end if;
1533
fea9e956 1534 -- Find the last tag component
758c442c 1535
fea9e956
ES
1536 Comp := First (L);
1537 while Present (Comp) loop
2b73cf68
JM
1538 if Nkind (Comp) = N_Component_Declaration
1539 and then Is_Tag (Defining_Identifier (Comp))
1540 then
fea9e956 1541 Last_Tag := Comp;
758c442c
GD
1542 end if;
1543
fea9e956
ES
1544 Next (Comp);
1545 end loop;
1546 end if;
758c442c 1547
fea9e956
ES
1548 -- At this point L references the list of components and Last_Tag
1549 -- references the current last tag (if any). Now we add the tag
1550 -- corresponding with all the interfaces that are not implemented
1551 -- by the parent.
758c442c 1552
ce2b6ba5
JM
1553 if Present (Interfaces (Typ)) then
1554 Elmt := First_Elmt (Interfaces (Typ));
758c442c
GD
1555 while Present (Elmt) loop
1556 Add_Tag (Node (Elmt));
1557 Next_Elmt (Elmt);
1558 end loop;
1559 end if;
1560 end Add_Interface_Tag_Components;
1561
3ff38f33
JM
1562 -------------------------------------
1563 -- Add_Internal_Interface_Entities --
1564 -------------------------------------
1565
1566 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
74853971
AC
1567 Elmt : Elmt_Id;
1568 Iface : Entity_Id;
1569 Iface_Elmt : Elmt_Id;
1570 Iface_Prim : Entity_Id;
1571 Ifaces_List : Elist_Id;
1572 New_Subp : Entity_Id := Empty;
1573 Prim : Entity_Id;
1574 Restore_Scope : Boolean := False;
3ff38f33
JM
1575
1576 begin
0791fbe9 1577 pragma Assert (Ada_Version >= Ada_2005
3ff38f33
JM
1578 and then Is_Record_Type (Tagged_Type)
1579 and then Is_Tagged_Type (Tagged_Type)
1580 and then Has_Interfaces (Tagged_Type)
1581 and then not Is_Interface (Tagged_Type));
1582
74853971
AC
1583 -- Ensure that the internal entities are added to the scope of the type
1584
1585 if Scope (Tagged_Type) /= Current_Scope then
1586 Push_Scope (Scope (Tagged_Type));
1587 Restore_Scope := True;
1588 end if;
1589
3ff38f33
JM
1590 Collect_Interfaces (Tagged_Type, Ifaces_List);
1591
1592 Iface_Elmt := First_Elmt (Ifaces_List);
1593 while Present (Iface_Elmt) loop
1594 Iface := Node (Iface_Elmt);
1595
b4d7b435
AC
1596 -- Originally we excluded here from this processing interfaces that
1597 -- are parents of Tagged_Type because their primitives are located
1598 -- in the primary dispatch table (and hence no auxiliary internal
1599 -- entities are required to handle secondary dispatch tables in such
1600 -- case). However, these auxiliary entities are also required to
1601 -- handle derivations of interfaces in formals of generics (see
1602 -- Derive_Subprograms).
3ff38f33 1603
b4d7b435
AC
1604 Elmt := First_Elmt (Primitive_Operations (Iface));
1605 while Present (Elmt) loop
1606 Iface_Prim := Node (Elmt);
3ff38f33 1607
b4d7b435
AC
1608 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1609 Prim :=
1610 Find_Primitive_Covering_Interface
1611 (Tagged_Type => Tagged_Type,
1612 Iface_Prim => Iface_Prim);
3ff38f33 1613
947430d5 1614 pragma Assert (Present (Prim));
3ff38f33 1615
ce09f8b3
AC
1616 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1617 -- differs from the name of the interface primitive then it is
1618 -- a private primitive inherited from a parent type. In such
1619 -- case, given that Tagged_Type covers the interface, the
1620 -- inherited private primitive becomes visible. For such
1621 -- purpose we add a new entity that renames the inherited
1622 -- private primitive.
1623
1624 if Chars (Prim) /= Chars (Iface_Prim) then
1625 pragma Assert (Has_Suffix (Prim, 'P'));
1626 Derive_Subprogram
1627 (New_Subp => New_Subp,
1628 Parent_Subp => Iface_Prim,
1629 Derived_Type => Tagged_Type,
1630 Parent_Type => Iface);
1631 Set_Alias (New_Subp, Prim);
878f708a
RD
1632 Set_Is_Abstract_Subprogram
1633 (New_Subp, Is_Abstract_Subprogram (Prim));
ce09f8b3
AC
1634 end if;
1635
b4d7b435
AC
1636 Derive_Subprogram
1637 (New_Subp => New_Subp,
1638 Parent_Subp => Iface_Prim,
1639 Derived_Type => Tagged_Type,
1640 Parent_Type => Iface);
1641
1642 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1643 -- associated with interface types. These entities are
1644 -- only registered in the list of primitives of its
1645 -- corresponding tagged type because they are only used
1646 -- to fill the contents of the secondary dispatch tables.
1647 -- Therefore they are removed from the homonym chains.
1648
1649 Set_Is_Hidden (New_Subp);
1650 Set_Is_Internal (New_Subp);
1651 Set_Alias (New_Subp, Prim);
1652 Set_Is_Abstract_Subprogram
1653 (New_Subp, Is_Abstract_Subprogram (Prim));
1654 Set_Interface_Alias (New_Subp, Iface_Prim);
1655
1656 -- Internal entities associated with interface types are
1657 -- only registered in the list of primitives of the tagged
1658 -- type. They are only used to fill the contents of the
1659 -- secondary dispatch tables. Therefore they are not needed
1660 -- in the homonym chains.
1661
1662 Remove_Homonym (New_Subp);
1663
1664 -- Hidden entities associated with interfaces must have set
1665 -- the Has_Delay_Freeze attribute to ensure that, in case of
1666 -- locally defined tagged types (or compiling with static
1667 -- dispatch tables generation disabled) the corresponding
1668 -- entry of the secondary dispatch table is filled when
1669 -- such an entity is frozen.
1670
1671 Set_Has_Delayed_Freeze (New_Subp);
1672 end if;
1673
1674 Next_Elmt (Elmt);
1675 end loop;
3ff38f33
JM
1676
1677 Next_Elmt (Iface_Elmt);
1678 end loop;
74853971
AC
1679
1680 if Restore_Scope then
1681 Pop_Scope;
1682 end if;
3ff38f33
JM
1683 end Add_Internal_Interface_Entities;
1684
996ae0b0
RK
1685 -----------------------------------
1686 -- Analyze_Component_Declaration --
1687 -----------------------------------
1688
1689 procedure Analyze_Component_Declaration (N : Node_Id) is
1690 Id : constant Entity_Id := Defining_Identifier (N);
2b73cf68 1691 E : constant Node_Id := Expression (N);
996ae0b0
RK
1692 T : Entity_Id;
1693 P : Entity_Id;
1694
5d09245e
AC
1695 function Contains_POC (Constr : Node_Id) return Boolean;
1696 -- Determines whether a constraint uses the discriminant of a record
1697 -- type thus becoming a per-object constraint (POC).
1698
57193e09 1699 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
88b32fc3
BD
1700 -- Typ is the type of the current component, check whether this type is
1701 -- a limited type. Used to validate declaration against that of
1702 -- enclosing record.
57193e09 1703
5d09245e
AC
1704 ------------------
1705 -- Contains_POC --
1706 ------------------
1707
1708 function Contains_POC (Constr : Node_Id) return Boolean is
1709 begin
dc06abec 1710 -- Prevent cascaded errors
2b73cf68
JM
1711
1712 if Error_Posted (Constr) then
1713 return False;
1714 end if;
1715
5d09245e 1716 case Nkind (Constr) is
5d09245e 1717 when N_Attribute_Reference =>
2b73cf68
JM
1718 return
1719 Attribute_Name (Constr) = Name_Access
1720 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
5d09245e
AC
1721
1722 when N_Discriminant_Association =>
1723 return Denotes_Discriminant (Expression (Constr));
1724
1725 when N_Identifier =>
1726 return Denotes_Discriminant (Constr);
1727
1728 when N_Index_Or_Discriminant_Constraint =>
1729 declare
9dfd2ff8 1730 IDC : Node_Id;
71d9e9f2 1731
5d09245e 1732 begin
9dfd2ff8 1733 IDC := First (Constraints (Constr));
5d09245e
AC
1734 while Present (IDC) loop
1735
9dfd2ff8 1736 -- One per-object constraint is sufficient
5d09245e
AC
1737
1738 if Contains_POC (IDC) then
1739 return True;
1740 end if;
1741
1742 Next (IDC);
1743 end loop;
1744
1745 return False;
1746 end;
1747
1748 when N_Range =>
1749 return Denotes_Discriminant (Low_Bound (Constr))
71d9e9f2 1750 or else
5d09245e
AC
1751 Denotes_Discriminant (High_Bound (Constr));
1752
1753 when N_Range_Constraint =>
1754 return Denotes_Discriminant (Range_Expression (Constr));
1755
1756 when others =>
1757 return False;
1758
1759 end case;
1760 end Contains_POC;
1761
57193e09
TQ
1762 ----------------------
1763 -- Is_Known_Limited --
1764 ----------------------
1765
1766 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1767 P : constant Entity_Id := Etype (Typ);
1768 R : constant Entity_Id := Root_Type (Typ);
1769
1770 begin
1771 if Is_Limited_Record (Typ) then
1772 return True;
1773
1774 -- If the root type is limited (and not a limited interface)
1775 -- so is the current type
1776
1777 elsif Is_Limited_Record (R)
1778 and then
1779 (not Is_Interface (R)
1780 or else not Is_Limited_Interface (R))
1781 then
1782 return True;
1783
1784 -- Else the type may have a limited interface progenitor, but a
1785 -- limited record parent.
1786
1787 elsif R /= P
1788 and then Is_Limited_Record (P)
1789 then
1790 return True;
1791
1792 else
1793 return False;
1794 end if;
1795 end Is_Known_Limited;
1796
5d09245e
AC
1797 -- Start of processing for Analyze_Component_Declaration
1798
996ae0b0
RK
1799 begin
1800 Generate_Definition (Id);
1801 Enter_Name (Id);
6e937c1c
AC
1802
1803 if Present (Subtype_Indication (Component_Definition (N))) then
1804 T := Find_Type_Of_Object
1805 (Subtype_Indication (Component_Definition (N)), N);
1806
0ab80019 1807 -- Ada 2005 (AI-230): Access Definition case
6e937c1c 1808
9bc856dd
AC
1809 else
1810 pragma Assert (Present
1811 (Access_Definition (Component_Definition (N))));
1812
6e937c1c
AC
1813 T := Access_Definition
1814 (Related_Nod => N,
1815 N => Access_Definition (Component_Definition (N)));
758c442c 1816 Set_Is_Local_Anonymous_Access (T);
35b7fa6a 1817
0ab80019 1818 -- Ada 2005 (AI-254)
7324bf49
AC
1819
1820 if Present (Access_To_Subprogram_Definition
1821 (Access_Definition (Component_Definition (N))))
1822 and then Protected_Present (Access_To_Subprogram_Definition
1823 (Access_Definition
1824 (Component_Definition (N))))
1825 then
fea9e956 1826 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
7324bf49 1827 end if;
6e937c1c 1828 end if;
996ae0b0 1829
fbf5a39b 1830 -- If the subtype is a constrained subtype of the enclosing record,
9dfd2ff8
CC
1831 -- (which must have a partial view) the back-end does not properly
1832 -- handle the recursion. Rewrite the component declaration with an
758c442c
GD
1833 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1834 -- the tree directly because side effects have already been removed from
1835 -- discriminant constraints.
fbf5a39b
AC
1836
1837 if Ekind (T) = E_Access_Subtype
a397db96 1838 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
fbf5a39b
AC
1839 and then Comes_From_Source (T)
1840 and then Nkind (Parent (T)) = N_Subtype_Declaration
1841 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1842 then
1843 Rewrite
a397db96 1844 (Subtype_Indication (Component_Definition (N)),
fbf5a39b 1845 New_Copy_Tree (Subtype_Indication (Parent (T))));
a397db96
AC
1846 T := Find_Type_Of_Object
1847 (Subtype_Indication (Component_Definition (N)), N);
fbf5a39b
AC
1848 end if;
1849
996ae0b0
RK
1850 -- If the component declaration includes a default expression, then we
1851 -- check that the component is not of a limited type (RM 3.7(5)),
1852 -- and do the special preanalysis of the expression (see section on
fbf5a39b
AC
1853 -- "Handling of Default and Per-Object Expressions" in the spec of
1854 -- package Sem).
996ae0b0 1855
2b73cf68 1856 if Present (E) then
ce4a6e84 1857 Preanalyze_Spec_Expression (E, T);
2b73cf68 1858 Check_Initialization (T, E);
57193e09 1859
0791fbe9 1860 if Ada_Version >= Ada_2005
57193e09 1861 and then Ekind (T) = E_Anonymous_Access_Type
3c829e3c 1862 and then Etype (E) /= Any_Type
57193e09
TQ
1863 then
1864 -- Check RM 3.9.2(9): "if the expected type for an expression is
1865 -- an anonymous access-to-specific tagged type, then the object
1866 -- designated by the expression shall not be dynamically tagged
1867 -- unless it is a controlling operand in a call on a dispatching
1868 -- operation"
1869
1870 if Is_Tagged_Type (Directly_Designated_Type (T))
1871 and then
1872 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1873 and then
2b73cf68
JM
1874 Ekind (Directly_Designated_Type (Etype (E))) =
1875 E_Class_Wide_Type
57193e09
TQ
1876 then
1877 Error_Msg_N
dc06abec 1878 ("access to specific tagged type required (RM 3.9.2(9))", E);
57193e09
TQ
1879 end if;
1880
1881 -- (Ada 2005: AI-230): Accessibility check for anonymous
1882 -- components
1883
2b73cf68
JM
1884 if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1885 Error_Msg_N
1886 ("expression has deeper access level than component " &
dc06abec 1887 "(RM 3.10.2 (12.2))", E);
2b73cf68
JM
1888 end if;
1889
1890 -- The initialization expression is a reference to an access
1891 -- discriminant. The type of the discriminant is always deeper
1892 -- than any access type.
88b32fc3 1893
2b73cf68
JM
1894 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1895 and then Is_Entity_Name (E)
1896 and then Ekind (Entity (E)) = E_In_Parameter
1897 and then Present (Discriminal_Link (Entity (E)))
57193e09
TQ
1898 then
1899 Error_Msg_N
2b73cf68
JM
1900 ("discriminant has deeper accessibility level than target",
1901 E);
57193e09
TQ
1902 end if;
1903 end if;
996ae0b0
RK
1904 end if;
1905
1906 -- The parent type may be a private view with unknown discriminants,
1907 -- and thus unconstrained. Regular components must be constrained.
1908
1909 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
8a6a52dc
AC
1910 if Is_Class_Wide_Type (T) then
1911 Error_Msg_N
1912 ("class-wide subtype with unknown discriminants" &
1913 " in component declaration",
1914 Subtype_Indication (Component_Definition (N)));
1915 else
1916 Error_Msg_N
1917 ("unconstrained subtype in component declaration",
1918 Subtype_Indication (Component_Definition (N)));
1919 end if;
996ae0b0
RK
1920
1921 -- Components cannot be abstract, except for the special case of
1922 -- the _Parent field (case of extending an abstract tagged type)
1923
fea9e956 1924 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
996ae0b0
RK
1925 Error_Msg_N ("type of a component cannot be abstract", N);
1926 end if;
1927
1928 Set_Etype (Id, T);
a397db96 1929 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
996ae0b0 1930
a5b62485
AC
1931 -- The component declaration may have a per-object constraint, set
1932 -- the appropriate flag in the defining identifier of the subtype.
5d09245e
AC
1933
1934 if Present (Subtype_Indication (Component_Definition (N))) then
1935 declare
1936 Sindic : constant Node_Id :=
71d9e9f2 1937 Subtype_Indication (Component_Definition (N));
5d09245e
AC
1938 begin
1939 if Nkind (Sindic) = N_Subtype_Indication
1940 and then Present (Constraint (Sindic))
1941 and then Contains_POC (Constraint (Sindic))
1942 then
1943 Set_Has_Per_Object_Constraint (Id);
1944 end if;
1945 end;
1946 end if;
1947
0ab80019 1948 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
71d9e9f2 1949 -- out some static checks.
2820d220 1950
0791fbe9 1951 if Ada_Version >= Ada_2005
9dfd2ff8 1952 and then Can_Never_Be_Null (T)
2820d220 1953 then
2820d220
AC
1954 Null_Exclusion_Static_Checks (N);
1955 end if;
1956
758c442c
GD
1957 -- If this component is private (or depends on a private type), flag the
1958 -- record type to indicate that some operations are not available.
996ae0b0
RK
1959
1960 P := Private_Component (T);
1961
1962 if Present (P) then
030d25f4 1963
71d9e9f2 1964 -- Check for circular definitions
996ae0b0
RK
1965
1966 if P = Any_Type then
1967 Set_Etype (Id, Any_Type);
1968
1969 -- There is a gap in the visibility of operations only if the
1970 -- component type is not defined in the scope of the record type.
1971
1972 elsif Scope (P) = Scope (Current_Scope) then
1973 null;
1974
1975 elsif Is_Limited_Type (P) then
1976 Set_Is_Limited_Composite (Current_Scope);
1977
1978 else
1979 Set_Is_Private_Composite (Current_Scope);
1980 end if;
1981 end if;
1982
1983 if P /= Any_Type
1984 and then Is_Limited_Type (T)
1985 and then Chars (Id) /= Name_uParent
1986 and then Is_Tagged_Type (Current_Scope)
1987 then
1988 if Is_Derived_Type (Current_Scope)
57193e09 1989 and then not Is_Known_Limited (Current_Scope)
996ae0b0
RK
1990 then
1991 Error_Msg_N
1992 ("extension of nonlimited type cannot have limited components",
1993 N);
57193e09
TQ
1994
1995 if Is_Interface (Root_Type (Current_Scope)) then
1996 Error_Msg_N
1997 ("\limitedness is not inherited from limited interface", N);
ed2233dc 1998 Error_Msg_N ("\add LIMITED to type indication", N);
57193e09
TQ
1999 end if;
2000
fbf5a39b 2001 Explain_Limited_Type (T, N);
996ae0b0
RK
2002 Set_Etype (Id, Any_Type);
2003 Set_Is_Limited_Composite (Current_Scope, False);
2004
2005 elsif not Is_Derived_Type (Current_Scope)
2006 and then not Is_Limited_Record (Current_Scope)
653da906 2007 and then not Is_Concurrent_Type (Current_Scope)
996ae0b0 2008 then
fbf5a39b
AC
2009 Error_Msg_N
2010 ("nonlimited tagged type cannot have limited components", N);
2011 Explain_Limited_Type (T, N);
996ae0b0
RK
2012 Set_Etype (Id, Any_Type);
2013 Set_Is_Limited_Composite (Current_Scope, False);
2014 end if;
2015 end if;
2016
2017 Set_Original_Record_Component (Id, Id);
eaba57fb
RD
2018
2019 if Has_Aspects (N) then
2020 Analyze_Aspect_Specifications (N, Id);
2021 end if;
996ae0b0
RK
2022 end Analyze_Component_Declaration;
2023
2024 --------------------------
2025 -- Analyze_Declarations --
2026 --------------------------
2027
2028 procedure Analyze_Declarations (L : List_Id) is
2029 D : Node_Id;
996ae0b0 2030 Freeze_From : Entity_Id := Empty;
88b32fc3 2031 Next_Node : Node_Id;
996ae0b0
RK
2032
2033 procedure Adjust_D;
2034 -- Adjust D not to include implicit label declarations, since these
2035 -- have strange Sloc values that result in elaboration check problems.
fbf5a39b
AC
2036 -- (They have the sloc of the label as found in the source, and that
2037 -- is ahead of the current declarative part).
2038
2039 --------------
2040 -- Adjust_D --
2041 --------------
996ae0b0
RK
2042
2043 procedure Adjust_D is
2044 begin
2045 while Present (Prev (D))
2046 and then Nkind (D) = N_Implicit_Label_Declaration
2047 loop
2048 Prev (D);
2049 end loop;
2050 end Adjust_D;
2051
2052 -- Start of processing for Analyze_Declarations
2053
2054 begin
2055 D := First (L);
2056 while Present (D) loop
2057
8ed68165 2058 -- Package specification cannot contain a package declaration in
8d606a78 2059 -- SPARK or ALFA.
8ed68165
AC
2060
2061 if Formal_Verification_Mode
2062 and then Nkind (D) = N_Package_Declaration
2063 and then Nkind (Parent (L)) = N_Package_Specification
2064 then
53beff22
YM
2065 Error_Msg_F ("|~~package specification cannot contain "
2066 & "a package declaration", D);
8ed68165
AC
2067 end if;
2068
996ae0b0
RK
2069 -- Complete analysis of declaration
2070
2071 Analyze (D);
2072 Next_Node := Next (D);
2073
2074 if No (Freeze_From) then
2075 Freeze_From := First_Entity (Current_Scope);
2076 end if;
2077
2078 -- At the end of a declarative part, freeze remaining entities
a5b62485
AC
2079 -- declared in it. The end of the visible declarations of package
2080 -- specification is not the end of a declarative part if private
2081 -- declarations are present. The end of a package declaration is a
2082 -- freezing point only if it a library package. A task definition or
2083 -- protected type definition is not a freeze point either. Finally,
2084 -- we do not freeze entities in generic scopes, because there is no
2085 -- code generated for them and freeze nodes will be generated for
2086 -- the instance.
996ae0b0
RK
2087
2088 -- The end of a package instantiation is not a freeze point, but
2089 -- for now we make it one, because the generic body is inserted
2090 -- (currently) immediately after. Generic instantiations will not
2091 -- be a freeze point once delayed freezing of bodies is implemented.
2092 -- (This is needed in any case for early instantiations ???).
2093
2094 if No (Next_Node) then
7d7af38a
JM
2095 if Nkind_In (Parent (L), N_Component_List,
2096 N_Task_Definition,
2097 N_Protected_Definition)
996ae0b0
RK
2098 then
2099 null;
2100
2101 elsif Nkind (Parent (L)) /= N_Package_Specification then
996ae0b0
RK
2102 if Nkind (Parent (L)) = N_Package_Body then
2103 Freeze_From := First_Entity (Current_Scope);
2104 end if;
2105
2106 Adjust_D;
2107 Freeze_All (Freeze_From, D);
2108 Freeze_From := Last_Entity (Current_Scope);
2109
2110 elsif Scope (Current_Scope) /= Standard_Standard
2111 and then not Is_Child_Unit (Current_Scope)
2112 and then No (Generic_Parent (Parent (L)))
2113 then
2114 null;
2115
2116 elsif L /= Visible_Declarations (Parent (L))
2117 or else No (Private_Declarations (Parent (L)))
2118 or else Is_Empty_List (Private_Declarations (Parent (L)))
2119 then
2120 Adjust_D;
2121 Freeze_All (Freeze_From, D);
2122 Freeze_From := Last_Entity (Current_Scope);
2123 end if;
2124
2125 -- If next node is a body then freeze all types before the body.
fea9e956
ES
2126 -- An exception occurs for some expander-generated bodies. If these
2127 -- are generated at places where in general language rules would not
2128 -- allow a freeze point, then we assume that the expander has
2129 -- explicitly checked that all required types are properly frozen,
2130 -- and we do not cause general freezing here. This special circuit
2131 -- is used when the encountered body is marked as having already
2132 -- been analyzed.
2133
2134 -- In all other cases (bodies that come from source, and expander
2135 -- generated bodies that have not been analyzed yet), freeze all
2136 -- types now. Note that in the latter case, the expander must take
2137 -- care to attach the bodies at a proper place in the tree so as to
2138 -- not cause unwanted freezing at that point.
996ae0b0
RK
2139
2140 elsif not Analyzed (Next_Node)
7d7af38a
JM
2141 and then (Nkind_In (Next_Node, N_Subprogram_Body,
2142 N_Entry_Body,
2143 N_Package_Body,
2144 N_Protected_Body,
2145 N_Task_Body)
2146 or else
2147 Nkind (Next_Node) in N_Body_Stub)
996ae0b0
RK
2148 then
2149 Adjust_D;
2150 Freeze_All (Freeze_From, D);
2151 Freeze_From := Last_Entity (Current_Scope);
2152 end if;
2153
2154 D := Next_Node;
2155 end loop;
1fb00064
AC
2156
2157 -- One more thing to do, we need to scan the declarations to check
2158 -- for any precondition/postcondition pragmas (Pre/Post aspects have
2159 -- by this stage been converted into corresponding pragmas). It is
2160 -- at this point that we analyze the expressions in such pragmas,
2161 -- to implement the delayed visibility requirement.
2162
2163 declare
2164 Decl : Node_Id;
2165 Spec : Node_Id;
2166 Sent : Entity_Id;
2167 Prag : Node_Id;
2168
2169 begin
2170 Decl := First (L);
2171 while Present (Decl) loop
2172 if Nkind (Original_Node (Decl)) = N_Subprogram_Declaration then
2173 Spec := Specification (Original_Node (Decl));
2174 Sent := Defining_Unit_Name (Spec);
2175 Prag := Spec_PPC_List (Sent);
2176 while Present (Prag) loop
2177 Analyze_PPC_In_Decl_Part (Prag, Sent);
2178 Prag := Next_Pragma (Prag);
2179 end loop;
2180 end if;
2181
2182 Next (Decl);
2183 end loop;
2184 end;
996ae0b0
RK
2185 end Analyze_Declarations;
2186
0f1a6a0b
AC
2187 -----------------------------------
2188 -- Analyze_Full_Type_Declaration --
2189 -----------------------------------
996ae0b0 2190
0f1a6a0b
AC
2191 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2192 Def : constant Node_Id := Type_Definition (N);
2193 Def_Id : constant Entity_Id := Defining_Identifier (N);
0f1a6a0b
AC
2194 T : Entity_Id;
2195 Prev : Entity_Id;
996ae0b0 2196
0f1a6a0b
AC
2197 Is_Remote : constant Boolean :=
2198 (Is_Remote_Types (Current_Scope)
2199 or else Is_Remote_Call_Interface (Current_Scope))
2200 and then not (In_Private_Part (Current_Scope)
2201 or else In_Package_Body (Current_Scope));
996ae0b0 2202
0f1a6a0b
AC
2203 procedure Check_Ops_From_Incomplete_Type;
2204 -- If there is a tagged incomplete partial view of the type, transfer
2205 -- its operations to the full view, and indicate that the type of the
2206 -- controlling parameter (s) is this full view.
996ae0b0 2207
0f1a6a0b
AC
2208 ------------------------------------
2209 -- Check_Ops_From_Incomplete_Type --
2210 ------------------------------------
996ae0b0 2211
0f1a6a0b
AC
2212 procedure Check_Ops_From_Incomplete_Type is
2213 Elmt : Elmt_Id;
2214 Formal : Entity_Id;
2215 Op : Entity_Id;
996ae0b0 2216
0f1a6a0b
AC
2217 begin
2218 if Prev /= T
2219 and then Ekind (Prev) = E_Incomplete_Type
2220 and then Is_Tagged_Type (Prev)
2221 and then Is_Tagged_Type (T)
2222 then
2223 Elmt := First_Elmt (Primitive_Operations (Prev));
2224 while Present (Elmt) loop
2225 Op := Node (Elmt);
2226 Prepend_Elmt (Op, Primitive_Operations (T));
d8db0bca 2227
0f1a6a0b
AC
2228 Formal := First_Formal (Op);
2229 while Present (Formal) loop
2230 if Etype (Formal) = Prev then
2231 Set_Etype (Formal, T);
2232 end if;
d8db0bca 2233
0f1a6a0b
AC
2234 Next_Formal (Formal);
2235 end loop;
d8db0bca 2236
0f1a6a0b
AC
2237 if Etype (Op) = Prev then
2238 Set_Etype (Op, T);
2239 end if;
996ae0b0 2240
0f1a6a0b
AC
2241 Next_Elmt (Elmt);
2242 end loop;
2243 end if;
2244 end Check_Ops_From_Incomplete_Type;
996ae0b0 2245
0f1a6a0b 2246 -- Start of processing for Analyze_Full_Type_Declaration
996ae0b0 2247
0f1a6a0b
AC
2248 begin
2249 Prev := Find_Type_Name (N);
996ae0b0 2250
0f1a6a0b 2251 -- The full view, if present, now points to the current type
996ae0b0 2252
0f1a6a0b
AC
2253 -- Ada 2005 (AI-50217): If the type was previously decorated when
2254 -- imported through a LIMITED WITH clause, it appears as incomplete
2255 -- but has no full view.
996ae0b0 2256
0f1a6a0b
AC
2257 if Ekind (Prev) = E_Incomplete_Type
2258 and then Present (Full_View (Prev))
2259 then
2260 T := Full_View (Prev);
0f1a6a0b
AC
2261 else
2262 T := Prev;
2263 end if;
950d3e7d 2264
0f1a6a0b 2265 Set_Is_Pure (T, Is_Pure (Current_Scope));
950d3e7d 2266
0f1a6a0b
AC
2267 -- We set the flag Is_First_Subtype here. It is needed to set the
2268 -- corresponding flag for the Implicit class-wide-type created
2269 -- during tagged types processing.
950d3e7d 2270
0f1a6a0b 2271 Set_Is_First_Subtype (T, True);
2b73cf68 2272
0f1a6a0b
AC
2273 -- Only composite types other than array types are allowed to have
2274 -- discriminants.
2b73cf68 2275
0f1a6a0b 2276 case Nkind (Def) is
2b73cf68 2277
0f1a6a0b
AC
2278 -- For derived types, the rule will be checked once we've figured
2279 -- out the parent type.
2b73cf68 2280
0f1a6a0b
AC
2281 when N_Derived_Type_Definition =>
2282 null;
ce4a6e84 2283
0f1a6a0b 2284 -- For record types, discriminants are allowed
ce4a6e84 2285
0f1a6a0b
AC
2286 when N_Record_Definition =>
2287 null;
950d3e7d 2288
0f1a6a0b
AC
2289 when others =>
2290 if Present (Discriminant_Specifications (N)) then
2291 Error_Msg_N
2292 ("elementary or array type cannot have discriminants",
2293 Defining_Identifier
2294 (First (Discriminant_Specifications (N))));
2295 end if;
2296 end case;
996ae0b0 2297
0f1a6a0b
AC
2298 -- Elaborate the type definition according to kind, and generate
2299 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2300 -- already done (this happens during the reanalysis that follows a call
2301 -- to the high level optimizer).
996ae0b0 2302
0f1a6a0b
AC
2303 if not Analyzed (T) then
2304 Set_Analyzed (T);
996ae0b0 2305
0f1a6a0b 2306 case Nkind (Def) is
996ae0b0 2307
0f1a6a0b
AC
2308 when N_Access_To_Subprogram_Definition =>
2309 Access_Subprogram_Declaration (T, Def);
996ae0b0 2310
0f1a6a0b
AC
2311 -- If this is a remote access to subprogram, we must create the
2312 -- equivalent fat pointer type, and related subprograms.
996ae0b0 2313
0f1a6a0b
AC
2314 if Is_Remote then
2315 Process_Remote_AST_Declaration (N);
2316 end if;
996ae0b0 2317
0f1a6a0b
AC
2318 -- Validate categorization rule against access type declaration
2319 -- usually a violation in Pure unit, Shared_Passive unit.
996ae0b0 2320
0f1a6a0b 2321 Validate_Access_Type_Declaration (T, N);
996ae0b0 2322
0f1a6a0b
AC
2323 when N_Access_To_Object_Definition =>
2324 Access_Type_Declaration (T, Def);
996ae0b0 2325
0f1a6a0b
AC
2326 -- Validate categorization rule against access type declaration
2327 -- usually a violation in Pure unit, Shared_Passive unit.
ce9e9122 2328
0f1a6a0b 2329 Validate_Access_Type_Declaration (T, N);
ce9e9122 2330
0f1a6a0b
AC
2331 -- If we are in a Remote_Call_Interface package and define a
2332 -- RACW, then calling stubs and specific stream attributes
2333 -- must be added.
ce9e9122 2334
0f1a6a0b
AC
2335 if Is_Remote
2336 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2337 then
2338 Add_RACW_Features (Def_Id);
2339 end if;
996ae0b0 2340
0f1a6a0b 2341 -- Set no strict aliasing flag if config pragma seen
996ae0b0 2342
0f1a6a0b
AC
2343 if Opt.No_Strict_Aliasing then
2344 Set_No_Strict_Aliasing (Base_Type (Def_Id));
2345 end if;
996ae0b0 2346
0f1a6a0b
AC
2347 when N_Array_Type_Definition =>
2348 Array_Type_Declaration (T, Def);
996ae0b0 2349
0f1a6a0b
AC
2350 when N_Derived_Type_Definition =>
2351 Derived_Type_Declaration (T, N, T /= Def_Id);
996ae0b0 2352
0f1a6a0b
AC
2353 when N_Enumeration_Type_Definition =>
2354 Enumeration_Type_Declaration (T, Def);
996ae0b0 2355
0f1a6a0b
AC
2356 when N_Floating_Point_Definition =>
2357 Floating_Point_Type_Declaration (T, Def);
996ae0b0 2358
0f1a6a0b
AC
2359 when N_Decimal_Fixed_Point_Definition =>
2360 Decimal_Fixed_Point_Type_Declaration (T, Def);
996ae0b0 2361
0f1a6a0b
AC
2362 when N_Ordinary_Fixed_Point_Definition =>
2363 Ordinary_Fixed_Point_Type_Declaration (T, Def);
996ae0b0 2364
0f1a6a0b
AC
2365 when N_Signed_Integer_Type_Definition =>
2366 Signed_Integer_Type_Declaration (T, Def);
996ae0b0 2367
0f1a6a0b
AC
2368 when N_Modular_Type_Definition =>
2369 Modular_Type_Declaration (T, Def);
996ae0b0 2370
0f1a6a0b
AC
2371 when N_Record_Definition =>
2372 Record_Type_Declaration (T, N, Prev);
996ae0b0 2373
0f1a6a0b 2374 -- If declaration has a parse error, nothing to elaborate.
996ae0b0 2375
0f1a6a0b
AC
2376 when N_Error =>
2377 null;
996ae0b0 2378
0f1a6a0b
AC
2379 when others =>
2380 raise Program_Error;
fbf5a39b 2381
0f1a6a0b 2382 end case;
996ae0b0
RK
2383 end if;
2384
0f1a6a0b 2385 if Etype (T) = Any_Type then
4818e7b9 2386 return;
996ae0b0
RK
2387 end if;
2388
8ed68165
AC
2389 if Formal_Verification_Mode then
2390
2391 -- Controlled type is not allowed in SPARK and ALFA
2392
2393 if Is_Visibly_Controlled (T) then
53beff22 2394 Error_Msg_F ("|~~controlled type is not allowed", N);
8ed68165
AC
2395 end if;
2396
2397 -- Discriminant type is not allowed in SPARK and ALFA
2398
2399 if Present (Discriminant_Specifications (N)) then
53beff22 2400 Error_Msg_F ("|~~discriminant type is not allowed", N);
8ed68165
AC
2401 end if;
2402 end if;
2403
0f1a6a0b 2404 -- Some common processing for all types
996ae0b0 2405
0f1a6a0b
AC
2406 Set_Depends_On_Private (T, Has_Private_Component (T));
2407 Check_Ops_From_Incomplete_Type;
996ae0b0 2408
0f1a6a0b
AC
2409 -- Both the declared entity, and its anonymous base type if one
2410 -- was created, need freeze nodes allocated.
996ae0b0 2411
0f1a6a0b
AC
2412 declare
2413 B : constant Entity_Id := Base_Type (T);
996ae0b0 2414
0f1a6a0b
AC
2415 begin
2416 -- In the case where the base type differs from the first subtype, we
2417 -- pre-allocate a freeze node, and set the proper link to the first
2418 -- subtype. Freeze_Entity will use this preallocated freeze node when
2419 -- it freezes the entity.
996ae0b0 2420
0f1a6a0b
AC
2421 -- This does not apply if the base type is a generic type, whose
2422 -- declaration is independent of the current derived definition.
6e937c1c 2423
0f1a6a0b
AC
2424 if B /= T and then not Is_Generic_Type (B) then
2425 Ensure_Freeze_Node (B);
2426 Set_First_Subtype_Link (Freeze_Node (B), T);
2427 end if;
6e937c1c 2428
0f1a6a0b
AC
2429 -- A type that is imported through a limited_with clause cannot
2430 -- generate any code, and thus need not be frozen. However, an access
2431 -- type with an imported designated type needs a finalization list,
2432 -- which may be referenced in some other package that has non-limited
2433 -- visibility on the designated type. Thus we must create the
2434 -- finalization list at the point the access type is frozen, to
2435 -- prevent unsatisfied references at link time.
6e937c1c 2436
0f1a6a0b
AC
2437 if not From_With_Type (T) or else Is_Access_Type (T) then
2438 Set_Has_Delayed_Freeze (T);
2439 end if;
2440 end;
6e937c1c 2441
0f1a6a0b
AC
2442 -- Case where T is the full declaration of some private type which has
2443 -- been swapped in Defining_Identifier (N).
6e937c1c 2444
0f1a6a0b
AC
2445 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2446 Process_Full_View (N, T, Def_Id);
6e937c1c 2447
0f1a6a0b
AC
2448 -- Record the reference. The form of this is a little strange, since
2449 -- the full declaration has been swapped in. So the first parameter
2450 -- here represents the entity to which a reference is made which is
2451 -- the "real" entity, i.e. the one swapped in, and the second
2452 -- parameter provides the reference location.
6e937c1c 2453
0f1a6a0b
AC
2454 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2455 -- since we don't want a complaint about the full type being an
2456 -- unwanted reference to the private type
6e937c1c 2457
0f1a6a0b
AC
2458 declare
2459 B : constant Boolean := Has_Pragma_Unreferenced (T);
2460 begin
2461 Set_Has_Pragma_Unreferenced (T, False);
2462 Generate_Reference (T, T, 'c');
2463 Set_Has_Pragma_Unreferenced (T, B);
2464 end;
6e937c1c 2465
0f1a6a0b 2466 Set_Completion_Referenced (Def_Id);
6e937c1c 2467
0f1a6a0b
AC
2468 -- For completion of incomplete type, process incomplete dependents
2469 -- and always mark the full type as referenced (it is the incomplete
2470 -- type that we get for any real reference).
6e937c1c 2471
0f1a6a0b
AC
2472 elsif Ekind (Prev) = E_Incomplete_Type then
2473 Process_Incomplete_Dependents (N, T, Prev);
2474 Generate_Reference (Prev, Def_Id, 'c');
2475 Set_Completion_Referenced (Def_Id);
6e937c1c 2476
0f1a6a0b
AC
2477 -- If not private type or incomplete type completion, this is a real
2478 -- definition of a new entity, so record it.
996ae0b0 2479
0f1a6a0b
AC
2480 else
2481 Generate_Definition (Def_Id);
2482 end if;
996ae0b0 2483
0f1a6a0b
AC
2484 if Chars (Scope (Def_Id)) = Name_System
2485 and then Chars (Def_Id) = Name_Address
2486 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2487 then
2488 Set_Is_Descendent_Of_Address (Def_Id);
2489 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2490 Set_Is_Descendent_Of_Address (Prev);
2491 end if;
996ae0b0 2492
0f1a6a0b
AC
2493 Set_Optimize_Alignment_Flags (Def_Id);
2494 Check_Eliminated (Def_Id);
996ae0b0 2495
eaba57fb
RD
2496 if Has_Aspects (N) then
2497 Analyze_Aspect_Specifications (N, Def_Id);
2498 end if;
0f1a6a0b 2499 end Analyze_Full_Type_Declaration;
996ae0b0 2500
0f1a6a0b
AC
2501 ----------------------------------
2502 -- Analyze_Incomplete_Type_Decl --
2503 ----------------------------------
996ae0b0 2504
0f1a6a0b
AC
2505 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2506 F : constant Boolean := Is_Pure (Current_Scope);
2507 T : Entity_Id;
996ae0b0 2508
0f1a6a0b 2509 begin
7ff2d234
AC
2510 -- Incomplete type is not allowed in SPARK or ALFA
2511
2512 if Formal_Verification_Mode
2513 and then Comes_From_Source (Original_Node (N))
2514 then
2515 Error_Msg_F ("|~~incomplete type is not allowed", N);
2516 end if;
2517
2518 -- Proceed with analysis
2519
0f1a6a0b 2520 Generate_Definition (Defining_Identifier (N));
5a989c6b 2521
0f1a6a0b
AC
2522 -- Process an incomplete declaration. The identifier must not have been
2523 -- declared already in the scope. However, an incomplete declaration may
2524 -- appear in the private part of a package, for a private type that has
2525 -- already been declared.
ce4a6e84 2526
0f1a6a0b 2527 -- In this case, the discriminants (if any) must match
ce4a6e84 2528
0f1a6a0b 2529 T := Find_Type_Name (N);
5a989c6b 2530
0f1a6a0b
AC
2531 Set_Ekind (T, E_Incomplete_Type);
2532 Init_Size_Align (T);
2533 Set_Is_First_Subtype (T, True);
2534 Set_Etype (T, T);
5a989c6b 2535
0f1a6a0b
AC
2536 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2537 -- incomplete types.
2538
2539 if Tagged_Present (N) then
2540 Set_Is_Tagged_Type (T);
2541 Make_Class_Wide_Type (T);
ef2a63ba 2542 Set_Direct_Primitive_Operations (T, New_Elmt_List);
996ae0b0
RK
2543 end if;
2544
0f1a6a0b 2545 Push_Scope (T);
996ae0b0 2546
0f1a6a0b 2547 Set_Stored_Constraint (T, No_Elist);
996ae0b0 2548
0f1a6a0b
AC
2549 if Present (Discriminant_Specifications (N)) then
2550 Process_Discriminants (N);
2551 end if;
71d9e9f2 2552
0f1a6a0b 2553 End_Scope;
996ae0b0 2554
0f1a6a0b
AC
2555 -- If the type has discriminants, non-trivial subtypes may be
2556 -- declared before the full view of the type. The full views of those
2557 -- subtypes will be built after the full view of the type.
996ae0b0 2558
0f1a6a0b
AC
2559 Set_Private_Dependents (T, New_Elmt_List);
2560 Set_Is_Pure (T, F);
2561 end Analyze_Incomplete_Type_Decl;
996ae0b0 2562
0f1a6a0b
AC
2563 -----------------------------------
2564 -- Analyze_Interface_Declaration --
2565 -----------------------------------
996ae0b0 2566
0f1a6a0b
AC
2567 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2568 CW : constant Entity_Id := Class_Wide_Type (T);
dc06abec 2569
0f1a6a0b
AC
2570 begin
2571 Set_Is_Tagged_Type (T);
996ae0b0 2572
0f1a6a0b
AC
2573 Set_Is_Limited_Record (T, Limited_Present (Def)
2574 or else Task_Present (Def)
2575 or else Protected_Present (Def)
2576 or else Synchronized_Present (Def));
fea9e956 2577
0f1a6a0b
AC
2578 -- Type is abstract if full declaration carries keyword, or if previous
2579 -- partial view did.
996ae0b0 2580
0f1a6a0b
AC
2581 Set_Is_Abstract_Type (T);
2582 Set_Is_Interface (T);
2820d220 2583
0f1a6a0b
AC
2584 -- Type is a limited interface if it includes the keyword limited, task,
2585 -- protected, or synchronized.
9dfd2ff8 2586
0f1a6a0b
AC
2587 Set_Is_Limited_Interface
2588 (T, Limited_Present (Def)
2589 or else Protected_Present (Def)
2590 or else Synchronized_Present (Def)
2591 or else Task_Present (Def));
9dfd2ff8 2592
0f1a6a0b 2593 Set_Interfaces (T, New_Elmt_List);
ef2a63ba 2594 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2820d220 2595
0f1a6a0b
AC
2596 -- Complete the decoration of the class-wide entity if it was already
2597 -- built (i.e. during the creation of the limited view)
996ae0b0 2598
0f1a6a0b
AC
2599 if Present (CW) then
2600 Set_Is_Interface (CW);
2601 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2602 end if;
996ae0b0 2603
0f1a6a0b
AC
2604 -- Check runtime support for synchronized interfaces
2605
2606 if VM_Target = No_VM
2607 and then (Is_Task_Interface (T)
2608 or else Is_Protected_Interface (T)
2609 or else Is_Synchronized_Interface (T))
2610 and then not RTE_Available (RE_Select_Specific_Data)
996ae0b0 2611 then
0f1a6a0b
AC
2612 Error_Msg_CRT ("synchronized interfaces", T);
2613 end if;
2614 end Analyze_Interface_Declaration;
33931112 2615
0f1a6a0b
AC
2616 -----------------------------
2617 -- Analyze_Itype_Reference --
2618 -----------------------------
33931112 2619
0f1a6a0b
AC
2620 -- Nothing to do. This node is placed in the tree only for the benefit of
2621 -- back end processing, and has no effect on the semantic processing.
33931112 2622
0f1a6a0b
AC
2623 procedure Analyze_Itype_Reference (N : Node_Id) is
2624 begin
2625 pragma Assert (Is_Itype (Itype (N)));
2626 null;
2627 end Analyze_Itype_Reference;
996ae0b0 2628
0f1a6a0b
AC
2629 --------------------------------
2630 -- Analyze_Number_Declaration --
2631 --------------------------------
996ae0b0 2632
0f1a6a0b
AC
2633 procedure Analyze_Number_Declaration (N : Node_Id) is
2634 Id : constant Entity_Id := Defining_Identifier (N);
2635 E : constant Node_Id := Expression (N);
2636 T : Entity_Id;
2637 Index : Interp_Index;
2638 It : Interp;
996ae0b0 2639
0f1a6a0b
AC
2640 begin
2641 Generate_Definition (Id);
2642 Enter_Name (Id);
996ae0b0 2643
0f1a6a0b 2644 -- This is an optimization of a common case of an integer literal
996ae0b0 2645
0f1a6a0b
AC
2646 if Nkind (E) = N_Integer_Literal then
2647 Set_Is_Static_Expression (E, True);
2648 Set_Etype (E, Universal_Integer);
996ae0b0 2649
0f1a6a0b
AC
2650 Set_Etype (Id, Universal_Integer);
2651 Set_Ekind (Id, E_Named_Integer);
2652 Set_Is_Frozen (Id, True);
2653 return;
996ae0b0
RK
2654 end if;
2655
0f1a6a0b 2656 Set_Is_Pure (Id, Is_Pure (Current_Scope));
996ae0b0 2657
0f1a6a0b
AC
2658 -- Process expression, replacing error by integer zero, to avoid
2659 -- cascaded errors or aborts further along in the processing
996ae0b0 2660
0f1a6a0b
AC
2661 -- Replace Error by integer zero, which seems least likely to
2662 -- cause cascaded errors.
758c442c 2663
0f1a6a0b
AC
2664 if E = Error then
2665 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2666 Set_Error_Posted (E);
996ae0b0
RK
2667 end if;
2668
0f1a6a0b 2669 Analyze (E);
996ae0b0 2670
0f1a6a0b
AC
2671 -- Verify that the expression is static and numeric. If
2672 -- the expression is overloaded, we apply the preference
2673 -- rule that favors root numeric types.
996ae0b0 2674
0f1a6a0b
AC
2675 if not Is_Overloaded (E) then
2676 T := Etype (E);
ce4a6e84 2677
0f1a6a0b
AC
2678 else
2679 T := Any_Type;
ce4a6e84 2680
0f1a6a0b
AC
2681 Get_First_Interp (E, Index, It);
2682 while Present (It.Typ) loop
2683 if (Is_Integer_Type (It.Typ)
2684 or else Is_Real_Type (It.Typ))
2685 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2686 then
2687 if T = Any_Type then
2688 T := It.Typ;
ce4a6e84 2689
0f1a6a0b
AC
2690 elsif It.Typ = Universal_Real
2691 or else It.Typ = Universal_Integer
2692 then
2693 -- Choose universal interpretation over any other
996ae0b0 2694
0f1a6a0b
AC
2695 T := It.Typ;
2696 exit;
2697 end if;
2698 end if;
9bc856dd 2699
0f1a6a0b
AC
2700 Get_Next_Interp (Index, It);
2701 end loop;
2702 end if;
9bc856dd 2703
0f1a6a0b
AC
2704 if Is_Integer_Type (T) then
2705 Resolve (E, T);
2706 Set_Etype (Id, Universal_Integer);
2707 Set_Ekind (Id, E_Named_Integer);
fbf5a39b 2708
0f1a6a0b 2709 elsif Is_Real_Type (T) then
fbf5a39b 2710
0f1a6a0b
AC
2711 -- Because the real value is converted to universal_real, this is a
2712 -- legal context for a universal fixed expression.
ce4a6e84 2713
0f1a6a0b
AC
2714 if T = Universal_Fixed then
2715 declare
2716 Loc : constant Source_Ptr := Sloc (N);
2717 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2718 Subtype_Mark =>
2719 New_Occurrence_Of (Universal_Real, Loc),
2720 Expression => Relocate_Node (E));
ce4a6e84 2721
0f1a6a0b
AC
2722 begin
2723 Rewrite (E, Conv);
2724 Analyze (E);
2725 end;
ce4a6e84 2726
0f1a6a0b
AC
2727 elsif T = Any_Fixed then
2728 Error_Msg_N ("illegal context for mixed mode operation", E);
ce4a6e84 2729
0f1a6a0b
AC
2730 -- Expression is of the form : universal_fixed * integer. Try to
2731 -- resolve as universal_real.
c775e4a1 2732
0f1a6a0b 2733 T := Universal_Real;
c775e4a1
AC
2734 Set_Etype (E, T);
2735 end if;
2736
0f1a6a0b
AC
2737 Resolve (E, T);
2738 Set_Etype (Id, Universal_Real);
2739 Set_Ekind (Id, E_Named_Real);
17cf3985 2740
0f1a6a0b
AC
2741 else
2742 Wrong_Type (E, Any_Numeric);
2743 Resolve (E, T);
17cf3985 2744
0f1a6a0b
AC
2745 Set_Etype (Id, T);
2746 Set_Ekind (Id, E_Constant);
2747 Set_Never_Set_In_Source (Id, True);
2748 Set_Is_True_Constant (Id, True);
2749 return;
2750 end if;
2b73cf68 2751
0f1a6a0b
AC
2752 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2753 Set_Etype (E, Etype (Id));
2754 end if;
996ae0b0 2755
0f1a6a0b
AC
2756 if not Is_OK_Static_Expression (E) then
2757 Flag_Non_Static_Expr
2758 ("non-static expression used in number declaration!", E);
2759 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2760 Set_Etype (E, Any_Type);
2761 end if;
2762 end Analyze_Number_Declaration;
88b32fc3 2763
0f1a6a0b
AC
2764 --------------------------------
2765 -- Analyze_Object_Declaration --
2766 --------------------------------
e27b834b 2767
0f1a6a0b
AC
2768 procedure Analyze_Object_Declaration (N : Node_Id) is
2769 Loc : constant Source_Ptr := Sloc (N);
2770 Id : constant Entity_Id := Defining_Identifier (N);
0f1a6a0b
AC
2771 T : Entity_Id;
2772 Act_T : Entity_Id;
996ae0b0 2773
0f1a6a0b
AC
2774 E : Node_Id := Expression (N);
2775 -- E is set to Expression (N) throughout this routine. When
2776 -- Expression (N) is modified, E is changed accordingly.
dc06abec 2777
0f1a6a0b 2778 Prev_Entity : Entity_Id := Empty;
dc06abec 2779
0f1a6a0b
AC
2780 function Count_Tasks (T : Entity_Id) return Uint;
2781 -- This function is called when a non-generic library level object of a
2782 -- task type is declared. Its function is to count the static number of
2783 -- tasks declared within the type (it is only called if Has_Tasks is set
2784 -- for T). As a side effect, if an array of tasks with non-static bounds
2785 -- or a variant record type is encountered, Check_Restrictions is called
2786 -- indicating the count is unknown.
4755cce9 2787
0f1a6a0b
AC
2788 -----------------
2789 -- Count_Tasks --
2790 -----------------
996ae0b0 2791
0f1a6a0b
AC
2792 function Count_Tasks (T : Entity_Id) return Uint is
2793 C : Entity_Id;
2794 X : Node_Id;
2795 V : Uint;
996ae0b0 2796
0f1a6a0b
AC
2797 begin
2798 if Is_Task_Type (T) then
2799 return Uint_1;
ffe9aba8 2800
0f1a6a0b
AC
2801 elsif Is_Record_Type (T) then
2802 if Has_Discriminants (T) then
2803 Check_Restriction (Max_Tasks, N);
2804 return Uint_0;
ffe9aba8 2805
0f1a6a0b
AC
2806 else
2807 V := Uint_0;
2808 C := First_Component (T);
2809 while Present (C) loop
2810 V := V + Count_Tasks (Etype (C));
2811 Next_Component (C);
2812 end loop;
996ae0b0 2813
0f1a6a0b
AC
2814 return V;
2815 end if;
996ae0b0 2816
0f1a6a0b
AC
2817 elsif Is_Array_Type (T) then
2818 X := First_Index (T);
2819 V := Count_Tasks (Component_Type (T));
2820 while Present (X) loop
2821 C := Etype (X);
996ae0b0 2822
0f1a6a0b
AC
2823 if not Is_Static_Subtype (C) then
2824 Check_Restriction (Max_Tasks, N);
2825 return Uint_0;
2826 else
2827 V := V * (UI_Max (Uint_0,
2828 Expr_Value (Type_High_Bound (C)) -
2829 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2830 end if;
996ae0b0 2831
0f1a6a0b
AC
2832 Next_Index (X);
2833 end loop;
996ae0b0 2834
0f1a6a0b 2835 return V;
996ae0b0 2836
0f1a6a0b
AC
2837 else
2838 return Uint_0;
2839 end if;
2840 end Count_Tasks;
996ae0b0 2841
0f1a6a0b 2842 -- Start of processing for Analyze_Object_Declaration
ce4a6e84 2843
0f1a6a0b
AC
2844 begin
2845 -- There are three kinds of implicit types generated by an
2846 -- object declaration:
ce4a6e84 2847
7ff2d234 2848 -- 1. Those generated by the original Object Definition
ce4a6e84 2849
0f1a6a0b 2850 -- 2. Those generated by the Expression
996ae0b0 2851
0f1a6a0b
AC
2852 -- 3. Those used to constrained the Object Definition with the
2853 -- expression constraints when it is unconstrained
996ae0b0 2854
0f1a6a0b
AC
2855 -- They must be generated in this order to avoid order of elaboration
2856 -- issues. Thus the first step (after entering the name) is to analyze
2857 -- the object definition.
996ae0b0 2858
0f1a6a0b
AC
2859 if Constant_Present (N) then
2860 Prev_Entity := Current_Entity_In_Scope (Id);
996ae0b0 2861
0f1a6a0b
AC
2862 if Present (Prev_Entity)
2863 and then
2864 -- If the homograph is an implicit subprogram, it is overridden
2865 -- by the current declaration.
996ae0b0 2866
0f1a6a0b
AC
2867 ((Is_Overloadable (Prev_Entity)
2868 and then Is_Inherited_Operation (Prev_Entity))
996ae0b0 2869
0f1a6a0b
AC
2870 -- The current object is a discriminal generated for an entry
2871 -- family index. Even though the index is a constant, in this
2872 -- particular context there is no true constant redeclaration.
2873 -- Enter_Name will handle the visibility.
996ae0b0 2874
0f1a6a0b
AC
2875 or else
2876 (Is_Discriminal (Id)
2877 and then Ekind (Discriminal_Link (Id)) =
2878 E_Entry_Index_Parameter)
996ae0b0 2879
0f1a6a0b
AC
2880 -- The current object is the renaming for a generic declared
2881 -- within the instance.
996ae0b0 2882
0f1a6a0b
AC
2883 or else
2884 (Ekind (Prev_Entity) = E_Package
2885 and then Nkind (Parent (Prev_Entity)) =
2886 N_Package_Renaming_Declaration
2887 and then not Comes_From_Source (Prev_Entity)
2888 and then Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
2889 then
2890 Prev_Entity := Empty;
2891 end if;
2892 end if;
0e41a941 2893
0f1a6a0b
AC
2894 if Present (Prev_Entity) then
2895 Constant_Redeclaration (Id, N, T);
0e41a941 2896
0f1a6a0b
AC
2897 Generate_Reference (Prev_Entity, Id, 'c');
2898 Set_Completion_Referenced (Id);
996ae0b0 2899
0f1a6a0b 2900 if Error_Posted (N) then
996ae0b0 2901
0f1a6a0b
AC
2902 -- Type mismatch or illegal redeclaration, Do not analyze
2903 -- expression to avoid cascaded errors.
996ae0b0 2904
0f1a6a0b
AC
2905 T := Find_Type_Of_Object (Object_Definition (N), N);
2906 Set_Etype (Id, T);
2907 Set_Ekind (Id, E_Variable);
2908 goto Leave;
996ae0b0
RK
2909 end if;
2910
0f1a6a0b
AC
2911 -- In the normal case, enter identifier at the start to catch premature
2912 -- usage in the initialization expression.
996ae0b0 2913
0f1a6a0b
AC
2914 else
2915 Generate_Definition (Id);
2916 Enter_Name (Id);
996ae0b0 2917
0f1a6a0b 2918 Mark_Coextensions (N, Object_Definition (N));
996ae0b0 2919
0f1a6a0b 2920 T := Find_Type_Of_Object (Object_Definition (N), N);
996ae0b0 2921
0f1a6a0b
AC
2922 if Nkind (Object_Definition (N)) = N_Access_Definition
2923 and then Present
2924 (Access_To_Subprogram_Definition (Object_Definition (N)))
2925 and then Protected_Present
2926 (Access_To_Subprogram_Definition (Object_Definition (N)))
2927 then
2928 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2929 end if;
449d2be3 2930
0f1a6a0b
AC
2931 if Error_Posted (Id) then
2932 Set_Etype (Id, T);
2933 Set_Ekind (Id, E_Variable);
2934 goto Leave;
2935 end if;
2936 end if;
449d2be3 2937
0f1a6a0b
AC
2938 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2939 -- out some static checks
2514b839 2940
0f1a6a0b
AC
2941 if Ada_Version >= Ada_2005
2942 and then Can_Never_Be_Null (T)
2943 then
2944 -- In case of aggregates we must also take care of the correct
2945 -- initialization of nested aggregates bug this is done at the
2946 -- point of the analysis of the aggregate (see sem_aggr.adb)
996ae0b0 2947
0f1a6a0b
AC
2948 if Present (Expression (N))
2949 and then Nkind (Expression (N)) = N_Aggregate
996ae0b0
RK
2950 then
2951 null;
2952
2953 else
0f1a6a0b
AC
2954 declare
2955 Save_Typ : constant Entity_Id := Etype (Id);
2956 begin
2957 Set_Etype (Id, T); -- Temp. decoration for static checks
2958 Null_Exclusion_Static_Checks (N);
2959 Set_Etype (Id, Save_Typ);
2960 end;
996ae0b0 2961 end if;
0f1a6a0b 2962 end if;
996ae0b0 2963
0f1a6a0b 2964 Set_Is_Pure (Id, Is_Pure (Current_Scope));
88b32fc3 2965
0f1a6a0b
AC
2966 -- If deferred constant, make sure context is appropriate. We detect
2967 -- a deferred constant as a constant declaration with no expression.
2968 -- A deferred constant can appear in a package body if its completion
2969 -- is by means of an interface pragma.
2970
2971 if Constant_Present (N)
2972 and then No (E)
996ae0b0 2973 then
0f1a6a0b
AC
2974 -- A deferred constant may appear in the declarative part of the
2975 -- following constructs:
030d25f4 2976
0f1a6a0b
AC
2977 -- blocks
2978 -- entry bodies
2979 -- extended return statements
2980 -- package specs
2981 -- package bodies
2982 -- subprogram bodies
2983 -- task bodies
030d25f4 2984
0f1a6a0b
AC
2985 -- When declared inside a package spec, a deferred constant must be
2986 -- completed by a full constant declaration or pragma Import. In all
2987 -- other cases, the only proper completion is pragma Import. Extended
2988 -- return statements are flagged as invalid contexts because they do
2989 -- not have a declarative part and so cannot accommodate the pragma.
996ae0b0 2990
0f1a6a0b
AC
2991 if Ekind (Current_Scope) = E_Return_Statement then
2992 Error_Msg_N
2993 ("invalid context for deferred constant declaration (RM 7.4)",
2994 N);
2995 Error_Msg_N
2996 ("\declaration requires an initialization expression",
2997 N);
2998 Set_Constant_Present (N, False);
996ae0b0 2999
0f1a6a0b 3000 -- In Ada 83, deferred constant must be of private type
996ae0b0 3001
0f1a6a0b
AC
3002 elsif not Is_Private_Type (T) then
3003 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3004 Error_Msg_N
3005 ("(Ada 83) deferred constant must be private type", N);
3006 end if;
3007 end if;
ce4a6e84 3008
0f1a6a0b 3009 -- If not a deferred constant, then object declaration freezes its type
996ae0b0 3010
0f1a6a0b
AC
3011 else
3012 Check_Fully_Declared (T, N);
3013 Freeze_Before (N, T);
3014 end if;
dc06abec 3015
0f1a6a0b
AC
3016 -- If the object was created by a constrained array definition, then
3017 -- set the link in both the anonymous base type and anonymous subtype
3018 -- that are built to represent the array type to point to the object.
dc06abec 3019
0f1a6a0b
AC
3020 if Nkind (Object_Definition (Declaration_Node (Id))) =
3021 N_Constrained_Array_Definition
3022 then
3023 Set_Related_Array_Object (T, Id);
3024 Set_Related_Array_Object (Base_Type (T), Id);
3025 end if;
996ae0b0 3026
0f1a6a0b 3027 -- Special checks for protected objects not at library level
996ae0b0 3028
0f1a6a0b
AC
3029 if Is_Protected_Type (T)
3030 and then not Is_Library_Level_Entity (Id)
3031 then
3032 Check_Restriction (No_Local_Protected_Objects, Id);
996ae0b0 3033
0f1a6a0b 3034 -- Protected objects with interrupt handlers must be at library level
996ae0b0 3035
0f1a6a0b
AC
3036 -- Ada 2005: this test is not needed (and the corresponding clause
3037 -- in the RM is removed) because accessibility checks are sufficient
3038 -- to make handlers not at the library level illegal.
996ae0b0 3039
0f1a6a0b
AC
3040 if Has_Interrupt_Handler (T)
3041 and then Ada_Version < Ada_2005
3042 then
3043 Error_Msg_N
3044 ("interrupt object can only be declared at library level", Id);
996ae0b0
RK
3045 end if;
3046 end if;
3047
0f1a6a0b
AC
3048 -- The actual subtype of the object is the nominal subtype, unless
3049 -- the nominal one is unconstrained and obtained from the expression.
996ae0b0 3050
0f1a6a0b 3051 Act_T := T;
dc06abec 3052
7ff2d234
AC
3053 -- These checks should be performed before the initialization expression
3054 -- is considered, so that the Object_Definition node is still the same
3055 -- as in source code.
3056
3057 if Formal_Verification_Mode
3058 and then Comes_From_Source (Original_Node (N))
3059 then
3060 -- In SPARK or ALFA, the nominal subtype shall be given by a subtype
3061 -- mark and shall not be unconstrained. (The only exception to this
3062 -- is the admission of declarations of constants of type String.)
3063
3064 if not Nkind_In (Object_Definition (N),
3065 N_Identifier,
3066 N_Expanded_Name)
3067 then
3068 Error_Msg_F ("|~~subtype mark expected", Object_Definition (N));
3069 elsif Is_Array_Type (T)
3070 and then not Is_Constrained (T)
3071 and then T /= Standard_String
3072 then
3073 Error_Msg_F ("|~~subtype mark of constrained type expected",
3074 Object_Definition (N));
3075 else
3076 null;
3077 end if;
3078
3079 -- There are no aliased objects in SPARK or ALFA
3080
3081 if Aliased_Present (N) then
3082 Error_Msg_F ("|~~aliased object is not allowed", N);
3083 end if;
3084 end if;
3085
0f1a6a0b 3086 -- Process initialization expression if present and not in error
996ae0b0 3087
0f1a6a0b 3088 if Present (E) and then E /= Error then
88b32fc3 3089
0f1a6a0b
AC
3090 -- Generate an error in case of CPP class-wide object initialization.
3091 -- Required because otherwise the expansion of the class-wide
3092 -- assignment would try to use 'size to initialize the object
3093 -- (primitive that is not available in CPP tagged types).
88b32fc3 3094
0f1a6a0b
AC
3095 if Is_Class_Wide_Type (Act_T)
3096 and then
3097 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3098 or else
3099 (Present (Full_View (Root_Type (Etype (Act_T))))
3100 and then
3101 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
996ae0b0 3102 then
0f1a6a0b
AC
3103 Error_Msg_N
3104 ("predefined assignment not available for 'C'P'P tagged types",
3105 E);
996ae0b0 3106 end if;
996ae0b0 3107
0f1a6a0b
AC
3108 Mark_Coextensions (N, E);
3109 Analyze (E);
dc06abec 3110
0f1a6a0b
AC
3111 -- In case of errors detected in the analysis of the expression,
3112 -- decorate it with the expected type to avoid cascaded errors
996ae0b0 3113
0f1a6a0b
AC
3114 if No (Etype (E)) then
3115 Set_Etype (E, T);
3116 end if;
dc06abec 3117
0f1a6a0b
AC
3118 -- If an initialization expression is present, then we set the
3119 -- Is_True_Constant flag. It will be reset if this is a variable
3120 -- and it is indeed modified.
3121
3122 Set_Is_True_Constant (Id, True);
3123
3124 -- If we are analyzing a constant declaration, set its completion
3125 -- flag after analyzing and resolving the expression.
3126
3127 if Constant_Present (N) then
3128 Set_Has_Completion (Id);
996ae0b0
RK
3129 end if;
3130
0f1a6a0b 3131 -- Set type and resolve (type may be overridden later on)
9dfd2ff8 3132
0f1a6a0b
AC
3133 Set_Etype (Id, T);
3134 Resolve (E, T);
996ae0b0 3135
0f1a6a0b
AC
3136 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3137 -- node (which was marked already-analyzed), we need to set the type
3138 -- to something other than Any_Access in order to keep gigi happy.
fbf5a39b 3139
0f1a6a0b
AC
3140 if Etype (E) = Any_Access then
3141 Set_Etype (E, T);
3142 end if;
996ae0b0 3143
0f1a6a0b
AC
3144 -- If the object is an access to variable, the initialization
3145 -- expression cannot be an access to constant.
996ae0b0 3146
0f1a6a0b
AC
3147 if Is_Access_Type (T)
3148 and then not Is_Access_Constant (T)
3149 and then Is_Access_Type (Etype (E))
3150 and then Is_Access_Constant (Etype (E))
3151 then
3152 Error_Msg_N
3153 ("access to variable cannot be initialized "
3154 & "with an access-to-constant expression", E);
3155 end if;
fbf5a39b 3156
0f1a6a0b
AC
3157 if not Assignment_OK (N) then
3158 Check_Initialization (T, E);
3159 end if;
996ae0b0 3160
0f1a6a0b 3161 Check_Unset_Reference (E);
996ae0b0 3162
0f1a6a0b
AC
3163 -- If this is a variable, then set current value. If this is a
3164 -- declared constant of a scalar type with a static expression,
3165 -- indicate that it is always valid.
996ae0b0 3166
0f1a6a0b
AC
3167 if not Constant_Present (N) then
3168 if Compile_Time_Known_Value (E) then
3169 Set_Current_Value (Id, E);
3170 end if;
996ae0b0 3171
0f1a6a0b
AC
3172 elsif Is_Scalar_Type (T)
3173 and then Is_OK_Static_Expression (E)
3174 then
3175 Set_Is_Known_Valid (Id);
3176 end if;
996ae0b0 3177
0f1a6a0b 3178 -- Deal with setting of null flags
996ae0b0 3179
0f1a6a0b
AC
3180 if Is_Access_Type (T) then
3181 if Known_Non_Null (E) then
3182 Set_Is_Known_Non_Null (Id, True);
3183 elsif Known_Null (E)
3184 and then not Can_Never_Be_Null (Id)
3185 then
3186 Set_Is_Known_Null (Id, True);
3187 end if;
3188 end if;
996ae0b0 3189
0f1a6a0b 3190 -- Check incorrect use of dynamically tagged expressions.
996ae0b0 3191
0f1a6a0b
AC
3192 if Is_Tagged_Type (T) then
3193 Check_Dynamically_Tagged_Expression
3194 (Expr => E,
3195 Typ => T,
3196 Related_Nod => N);
3197 end if;
996ae0b0 3198
0f1a6a0b
AC
3199 Apply_Scalar_Range_Check (E, T);
3200 Apply_Static_Length_Check (E, T);
3201 end if;
996ae0b0 3202
0f1a6a0b
AC
3203 -- If the No_Streams restriction is set, check that the type of the
3204 -- object is not, and does not contain, any subtype derived from
3205 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3206 -- Has_Stream just for efficiency reasons. There is no point in
3207 -- spending time on a Has_Stream check if the restriction is not set.
996ae0b0 3208
0f1a6a0b
AC
3209 if Restriction_Check_Required (No_Streams) then
3210 if Has_Stream (T) then
3211 Check_Restriction (No_Streams, N);
996ae0b0
RK
3212 end if;
3213 end if;
3214
f2acf80c
AC
3215 -- Deal with predicate check before we start to do major rewriting.
3216 -- it is OK to initialize and then check the initialized value, since
3217 -- the object goes out of scope if we get a predicate failure. Note
3218 -- that we do this in the analyzer and not the expander because the
3219 -- analyzer does some substantial rewriting in some cases.
3220
3221 -- We need a predicate check if the type has predicates, and if either
3222 -- there is an initializing expression, or for default initialization
3223 -- when we have at least one case of an explicit default initial value.
3224
3225 if not Suppress_Assignment_Checks (N)
3226 and then Present (Predicate_Function (T))
3227 and then
3228 (Present (E)
3229 or else
3230 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3231 then
3232 Insert_After (N,
3233 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3234 end if;
3235
0f1a6a0b 3236 -- Case of unconstrained type
fbf5a39b 3237
0f1a6a0b 3238 if Is_Indefinite_Subtype (T) then
ce4a6e84 3239
0f1a6a0b 3240 -- Nothing to do in deferred constant case
ce4a6e84 3241
0f1a6a0b 3242 if Constant_Present (N) and then No (E) then
ce4a6e84
RD
3243 null;
3244
0f1a6a0b 3245 -- Case of no initialization present
ce4a6e84 3246
0f1a6a0b
AC
3247 elsif No (E) then
3248 if No_Initialization (N) then
3249 null;
ce4a6e84 3250
0f1a6a0b
AC
3251 elsif Is_Class_Wide_Type (T) then
3252 Error_Msg_N
3253 ("initialization required in class-wide declaration ", N);
ce4a6e84 3254
0f1a6a0b
AC
3255 else
3256 Error_Msg_N
3257 ("unconstrained subtype not allowed (need initialization)",
3258 Object_Definition (N));
07fc65c4 3259
0f1a6a0b
AC
3260 if Is_Record_Type (T) and then Has_Discriminants (T) then
3261 Error_Msg_N
3262 ("\provide initial value or explicit discriminant values",
3263 Object_Definition (N));
07fc65c4 3264
0f1a6a0b
AC
3265 Error_Msg_NE
3266 ("\or give default discriminant values for type&",
3267 Object_Definition (N), T);
07fc65c4 3268
0f1a6a0b
AC
3269 elsif Is_Array_Type (T) then
3270 Error_Msg_N
3271 ("\provide initial value or explicit array bounds",
3272 Object_Definition (N));
3273 end if;
3274 end if;
07fc65c4 3275
0f1a6a0b
AC
3276 -- Case of initialization present but in error. Set initial
3277 -- expression as absent (but do not make above complaints)
996ae0b0 3278
0f1a6a0b
AC
3279 elsif E = Error then
3280 Set_Expression (N, Empty);
3281 E := Empty;
996ae0b0 3282
0f1a6a0b 3283 -- Case of initialization present
996ae0b0 3284
0f1a6a0b
AC
3285 else
3286 -- Not allowed in Ada 83
996ae0b0 3287
0f1a6a0b
AC
3288 if not Constant_Present (N) then
3289 if Ada_Version = Ada_83
3290 and then Comes_From_Source (Object_Definition (N))
3291 then
3292 Error_Msg_N
3293 ("(Ada 83) unconstrained variable not allowed",
3294 Object_Definition (N));
3295 end if;
3296 end if;
996ae0b0 3297
0f1a6a0b 3298 -- Now we constrain the variable from the initializing expression
fbf5a39b 3299
0f1a6a0b
AC
3300 -- If the expression is an aggregate, it has been expanded into
3301 -- individual assignments. Retrieve the actual type from the
3302 -- expanded construct.
fbf5a39b 3303
0f1a6a0b
AC
3304 if Is_Array_Type (T)
3305 and then No_Initialization (N)
3306 and then Nkind (Original_Node (E)) = N_Aggregate
3307 then
3308 Act_T := Etype (E);
996ae0b0 3309
0f1a6a0b
AC
3310 -- In case of class-wide interface object declarations we delay
3311 -- the generation of the equivalent record type declarations until
3312 -- its expansion because there are cases in they are not required.
996ae0b0 3313
0f1a6a0b
AC
3314 elsif Is_Interface (T) then
3315 null;
88b32fc3 3316
0f1a6a0b
AC
3317 else
3318 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3319 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3320 end if;
88b32fc3 3321
0f1a6a0b 3322 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
5b2217f8 3323
0f1a6a0b
AC
3324 if Aliased_Present (N) then
3325 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3326 end if;
5b2217f8 3327
0f1a6a0b
AC
3328 Freeze_Before (N, Act_T);
3329 Freeze_Before (N, T);
3330 end if;
996ae0b0 3331
0f1a6a0b
AC
3332 elsif Is_Array_Type (T)
3333 and then No_Initialization (N)
3334 and then Nkind (Original_Node (E)) = N_Aggregate
3335 then
3336 if not Is_Entity_Name (Object_Definition (N)) then
3337 Act_T := Etype (E);
3338 Check_Compile_Time_Size (Act_T);
996ae0b0 3339
0f1a6a0b
AC
3340 if Aliased_Present (N) then
3341 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3342 end if;
3343 end if;
996ae0b0 3344
0f1a6a0b
AC
3345 -- When the given object definition and the aggregate are specified
3346 -- independently, and their lengths might differ do a length check.
3347 -- This cannot happen if the aggregate is of the form (others =>...)
996ae0b0 3348
0f1a6a0b
AC
3349 if not Is_Constrained (T) then
3350 null;
996ae0b0 3351
0f1a6a0b 3352 elsif Nkind (E) = N_Raise_Constraint_Error then
996ae0b0 3353
0f1a6a0b 3354 -- Aggregate is statically illegal. Place back in declaration
758c442c 3355
0f1a6a0b
AC
3356 Set_Expression (N, E);
3357 Set_No_Initialization (N, False);
9dfd2ff8 3358
0f1a6a0b
AC
3359 elsif T = Etype (E) then
3360 null;
758c442c 3361
0f1a6a0b
AC
3362 elsif Nkind (E) = N_Aggregate
3363 and then Present (Component_Associations (E))
3364 and then Present (Choices (First (Component_Associations (E))))
3365 and then Nkind (First
3366 (Choices (First (Component_Associations (E))))) = N_Others_Choice
3367 then
3368 null;
758c442c 3369
0f1a6a0b
AC
3370 else
3371 Apply_Length_Check (E, T);
3372 end if;
996ae0b0 3373
0f1a6a0b
AC
3374 -- If the type is limited unconstrained with defaulted discriminants and
3375 -- there is no expression, then the object is constrained by the
3376 -- defaults, so it is worthwhile building the corresponding subtype.
996ae0b0 3377
0f1a6a0b
AC
3378 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
3379 and then not Is_Constrained (T)
3380 and then Has_Discriminants (T)
996ae0b0 3381 then
0f1a6a0b
AC
3382 if No (E) then
3383 Act_T := Build_Default_Subtype (T, N);
3384 else
3385 -- Ada 2005: a limited object may be initialized by means of an
3386 -- aggregate. If the type has default discriminants it has an
3387 -- unconstrained nominal type, Its actual subtype will be obtained
3388 -- from the aggregate, and not from the default discriminants.
996ae0b0 3389
0f1a6a0b
AC
3390 Act_T := Etype (E);
3391 end if;
996ae0b0 3392
0f1a6a0b 3393 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
dc06abec 3394
0f1a6a0b
AC
3395 elsif Present (Underlying_Type (T))
3396 and then not Is_Constrained (Underlying_Type (T))
3397 and then Has_Discriminants (Underlying_Type (T))
3398 and then Nkind (E) = N_Function_Call
3399 and then Constant_Present (N)
3400 then
3401 -- The back-end has problems with constants of a discriminated type
3402 -- with defaults, if the initial value is a function call. We
3403 -- generate an intermediate temporary for the result of the call.
3404 -- It is unclear why this should make it acceptable to gcc. ???
dc06abec 3405
0f1a6a0b 3406 Remove_Side_Effects (E);
996ae0b0
RK
3407 end if;
3408
0f1a6a0b 3409 -- Check No_Wide_Characters restriction
996ae0b0 3410
0f1a6a0b 3411 Check_Wide_Character_Restriction (T, Object_Definition (N));
996ae0b0 3412
0f1a6a0b
AC
3413 -- Indicate this is not set in source. Certainly true for constants,
3414 -- and true for variables so far (will be reset for a variable if and
3415 -- when we encounter a modification in the source).
996ae0b0 3416
0f1a6a0b 3417 Set_Never_Set_In_Source (Id, True);
996ae0b0 3418
0f1a6a0b 3419 -- Now establish the proper kind and type of the object
996ae0b0 3420
0f1a6a0b
AC
3421 if Constant_Present (N) then
3422 Set_Ekind (Id, E_Constant);
3423 Set_Is_True_Constant (Id, True);
996ae0b0 3424
0f1a6a0b
AC
3425 else
3426 Set_Ekind (Id, E_Variable);
996ae0b0 3427
0f1a6a0b
AC
3428 -- A variable is set as shared passive if it appears in a shared
3429 -- passive package, and is at the outer level. This is not done
3430 -- for entities generated during expansion, because those are
3431 -- always manipulated locally.
e6f69614 3432
0f1a6a0b
AC
3433 if Is_Shared_Passive (Current_Scope)
3434 and then Is_Library_Level_Entity (Id)
3435 and then Comes_From_Source (Id)
3436 then
3437 Set_Is_Shared_Passive (Id);
3438 Check_Shared_Var (Id, T, N);
3439 end if;
653da906 3440
0f1a6a0b
AC
3441 -- Set Has_Initial_Value if initializing expression present. Note
3442 -- that if there is no initializing expression, we leave the state
3443 -- of this flag unchanged (usually it will be False, but notably in
3444 -- the case of exception choice variables, it will already be true).
88b32fc3 3445
0f1a6a0b
AC
3446 if Present (E) then
3447 Set_Has_Initial_Value (Id, True);
3448 end if;
3449 end if;
88b32fc3 3450
0f1a6a0b 3451 -- Initialize alignment and size and capture alignment setting
88b32fc3 3452
0f1a6a0b
AC
3453 Init_Alignment (Id);
3454 Init_Esize (Id);
3455 Set_Optimize_Alignment_Flags (Id);
88b32fc3 3456
0f1a6a0b 3457 -- Deal with aliased case
88b32fc3 3458
0f1a6a0b
AC
3459 if Aliased_Present (N) then
3460 Set_Is_Aliased (Id);
88b32fc3 3461
0f1a6a0b
AC
3462 -- If the object is aliased and the type is unconstrained with
3463 -- defaulted discriminants and there is no expression, then the
3464 -- object is constrained by the defaults, so it is worthwhile
3465 -- building the corresponding subtype.
88b32fc3 3466
0f1a6a0b
AC
3467 -- Ada 2005 (AI-363): If the aliased object is discriminated and
3468 -- unconstrained, then only establish an actual subtype if the
3469 -- nominal subtype is indefinite. In definite cases the object is
3470 -- unconstrained in Ada 2005.
88b32fc3 3471
0f1a6a0b
AC
3472 if No (E)
3473 and then Is_Record_Type (T)
3474 and then not Is_Constrained (T)
3475 and then Has_Discriminants (T)
3476 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
3477 then
3478 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
3479 end if;
3480 end if;
88b32fc3 3481
0f1a6a0b 3482 -- Now we can set the type of the object
88b32fc3 3483
0f1a6a0b 3484 Set_Etype (Id, Act_T);
88b32fc3 3485
0f1a6a0b 3486 -- Deal with controlled types
88b32fc3 3487
0f1a6a0b
AC
3488 if Has_Controlled_Component (Etype (Id))
3489 or else Is_Controlled (Etype (Id))
3490 then
3491 if not Is_Library_Level_Entity (Id) then
3492 Check_Restriction (No_Nested_Finalization, N);
88b32fc3 3493 else
0f1a6a0b 3494 Validate_Controlled_Object (Id);
88b32fc3
BD
3495 end if;
3496
0f1a6a0b
AC
3497 -- Generate a warning when an initialization causes an obvious ABE
3498 -- violation. If the init expression is a simple aggregate there
3499 -- shouldn't be any initialize/adjust call generated. This will be
3500 -- true as soon as aggregates are built in place when possible.
443614e3 3501
0f1a6a0b
AC
3502 -- ??? at the moment we do not generate warnings for temporaries
3503 -- created for those aggregates although Program_Error might be
3504 -- generated if compiled with -gnato.
653da906 3505
0f1a6a0b
AC
3506 if Is_Controlled (Etype (Id))
3507 and then Comes_From_Source (Id)
57193e09 3508 then
0f1a6a0b
AC
3509 declare
3510 BT : constant Entity_Id := Base_Type (Etype (Id));
996ae0b0 3511
0f1a6a0b
AC
3512 Implicit_Call : Entity_Id;
3513 pragma Warnings (Off, Implicit_Call);
3514 -- ??? what is this for (never referenced!)
996ae0b0 3515
0f1a6a0b
AC
3516 function Is_Aggr (N : Node_Id) return Boolean;
3517 -- Check that N is an aggregate
996ae0b0 3518
0f1a6a0b
AC
3519 -------------
3520 -- Is_Aggr --
3521 -------------
996ae0b0 3522
0f1a6a0b
AC
3523 function Is_Aggr (N : Node_Id) return Boolean is
3524 begin
3525 case Nkind (Original_Node (N)) is
3526 when N_Aggregate | N_Extension_Aggregate =>
3527 return True;
996ae0b0 3528
0f1a6a0b
AC
3529 when N_Qualified_Expression |
3530 N_Type_Conversion |
3531 N_Unchecked_Type_Conversion =>
3532 return Is_Aggr (Expression (Original_Node (N)));
996ae0b0 3533
0f1a6a0b
AC
3534 when others =>
3535 return False;
3536 end case;
3537 end Is_Aggr;
996ae0b0 3538
0f1a6a0b
AC
3539 begin
3540 -- If no underlying type, we already are in an error situation.
3541 -- Do not try to add a warning since we do not have access to
3542 -- prim-op list.
996ae0b0 3543
0f1a6a0b
AC
3544 if No (Underlying_Type (BT)) then
3545 Implicit_Call := Empty;
996ae0b0 3546
0f1a6a0b
AC
3547 -- A generic type does not have usable primitive operators.
3548 -- Initialization calls are built for instances.
996ae0b0 3549
0f1a6a0b
AC
3550 elsif Is_Generic_Type (BT) then
3551 Implicit_Call := Empty;
996ae0b0 3552
0f1a6a0b
AC
3553 -- If the init expression is not an aggregate, an adjust call
3554 -- will be generated
996ae0b0 3555
0f1a6a0b
AC
3556 elsif Present (E) and then not Is_Aggr (E) then
3557 Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
996ae0b0 3558
0f1a6a0b
AC
3559 -- If no init expression and we are not in the deferred
3560 -- constant case, an Initialize call will be generated
996ae0b0 3561
0f1a6a0b
AC
3562 elsif No (E) and then not Constant_Present (N) then
3563 Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
996ae0b0 3564
0f1a6a0b
AC
3565 else
3566 Implicit_Call := Empty;
3567 end if;
3568 end;
3569 end if;
3570 end if;
996ae0b0 3571
0f1a6a0b
AC
3572 if Has_Task (Etype (Id)) then
3573 Check_Restriction (No_Tasking, N);
996ae0b0 3574
0f1a6a0b 3575 -- Deal with counting max tasks
996ae0b0 3576
0f1a6a0b 3577 -- Nothing to do if inside a generic
996ae0b0 3578
0f1a6a0b
AC
3579 if Inside_A_Generic then
3580 null;
996ae0b0 3581
0f1a6a0b 3582 -- If library level entity, then count tasks
996ae0b0 3583
0f1a6a0b
AC
3584 elsif Is_Library_Level_Entity (Id) then
3585 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
996ae0b0 3586
0f1a6a0b
AC
3587 -- If not library level entity, then indicate we don't know max
3588 -- tasks and also check task hierarchy restriction and blocking
3589 -- operation (since starting a task is definitely blocking!)
996ae0b0 3590
0f1a6a0b
AC
3591 else
3592 Check_Restriction (Max_Tasks, N);
3593 Check_Restriction (No_Task_Hierarchy, N);
3594 Check_Potentially_Blocking_Operation (N);
3595 end if;
996ae0b0 3596
0f1a6a0b
AC
3597 -- A rather specialized test. If we see two tasks being declared
3598 -- of the same type in the same object declaration, and the task
3599 -- has an entry with an address clause, we know that program error
3600 -- will be raised at run time since we can't have two tasks with
3601 -- entries at the same address.
996ae0b0 3602
0f1a6a0b
AC
3603 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3604 declare
3605 E : Entity_Id;
996ae0b0 3606
0f1a6a0b
AC
3607 begin
3608 E := First_Entity (Etype (Id));
3609 while Present (E) loop
3610 if Ekind (E) = E_Entry
3611 and then Present (Get_Attribute_Definition_Clause
3612 (E, Attribute_Address))
3613 then
3614 Error_Msg_N
3615 ("?more than one task with same entry address", N);
3616 Error_Msg_N
3617 ("\?Program_Error will be raised at run time", N);
3618 Insert_Action (N,
3619 Make_Raise_Program_Error (Loc,
3620 Reason => PE_Duplicated_Entry_Address));
3621 exit;
3622 end if;
996ae0b0 3623
0f1a6a0b
AC
3624 Next_Entity (E);
3625 end loop;
3626 end;
3627 end if;
3628 end if;
996ae0b0 3629
0f1a6a0b
AC
3630 -- Some simple constant-propagation: if the expression is a constant
3631 -- string initialized with a literal, share the literal. This avoids
3632 -- a run-time copy.
2b73cf68 3633
0f1a6a0b
AC
3634 if Present (E)
3635 and then Is_Entity_Name (E)
3636 and then Ekind (Entity (E)) = E_Constant
3637 and then Base_Type (Etype (E)) = Standard_String
3638 then
3639 declare
3640 Val : constant Node_Id := Constant_Value (Entity (E));
3641 begin
3642 if Present (Val)
3643 and then Nkind (Val) = N_String_Literal
3644 then
3645 Rewrite (E, New_Copy (Val));
3646 end if;
3647 end;
3648 end if;
996ae0b0 3649
0f1a6a0b
AC
3650 -- Another optimization: if the nominal subtype is unconstrained and
3651 -- the expression is a function call that returns an unconstrained
3652 -- type, rewrite the declaration as a renaming of the result of the
3653 -- call. The exceptions below are cases where the copy is expected,
3654 -- either by the back end (Aliased case) or by the semantics, as for
3655 -- initializing controlled types or copying tags for classwide types.
996ae0b0 3656
0f1a6a0b
AC
3657 if Present (E)
3658 and then Nkind (E) = N_Explicit_Dereference
3659 and then Nkind (Original_Node (E)) = N_Function_Call
3660 and then not Is_Library_Level_Entity (Id)
3661 and then not Is_Constrained (Underlying_Type (T))
3662 and then not Is_Aliased (Id)
3663 and then not Is_Class_Wide_Type (T)
3664 and then not Is_Controlled (T)
3665 and then not Has_Controlled_Component (Base_Type (T))
3666 and then Expander_Active
3667 then
3668 Rewrite (N,
3669 Make_Object_Renaming_Declaration (Loc,
3670 Defining_Identifier => Id,
3671 Access_Definition => Empty,
3672 Subtype_Mark => New_Occurrence_Of
3673 (Base_Type (Etype (Id)), Loc),
3674 Name => E));
996ae0b0 3675
0f1a6a0b 3676 Set_Renamed_Object (Id, E);
996ae0b0 3677
0f1a6a0b
AC
3678 -- Force generation of debugging information for the constant and for
3679 -- the renamed function call.
996ae0b0 3680
0f1a6a0b
AC
3681 Set_Debug_Info_Needed (Id);
3682 Set_Debug_Info_Needed (Entity (Prefix (E)));
3683 end if;
996ae0b0 3684
0f1a6a0b
AC
3685 if Present (Prev_Entity)
3686 and then Is_Frozen (Prev_Entity)
3687 and then not Error_Posted (Id)
3688 then
3689 Error_Msg_N ("full constant declaration appears too late", N);
3690 end if;
996ae0b0 3691
0f1a6a0b 3692 Check_Eliminated (Id);
996ae0b0 3693
0f1a6a0b 3694 -- Deal with setting In_Private_Part flag if in private part
996ae0b0 3695
0f1a6a0b
AC
3696 if Ekind (Scope (Id)) = E_Package
3697 and then In_Private_Part (Scope (Id))
3698 then
3699 Set_In_Private_Part (Id);
3700 end if;
2820d220 3701
0f1a6a0b 3702 -- Check for violation of No_Local_Timing_Events
996ae0b0 3703
0f1a6a0b
AC
3704 if Is_RTE (Etype (Id), RE_Timing_Event)
3705 and then not Is_Library_Level_Entity (Id)
3706 then
3707 Check_Restriction (No_Local_Timing_Events, N);
3708 end if;
996ae0b0 3709
c7f0d2c0 3710 <<Leave>>
eaba57fb
RD
3711 if Has_Aspects (N) then
3712 Analyze_Aspect_Specifications (N, Id);
3713 end if;
0f1a6a0b 3714 end Analyze_Object_Declaration;
996ae0b0 3715
0f1a6a0b
AC
3716 ---------------------------
3717 -- Analyze_Others_Choice --
3718 ---------------------------
996ae0b0 3719
0f1a6a0b
AC
3720 -- Nothing to do for the others choice node itself, the semantic analysis
3721 -- of the others choice will occur as part of the processing of the parent
88b32fc3 3722
0f1a6a0b
AC
3723 procedure Analyze_Others_Choice (N : Node_Id) is
3724 pragma Warnings (Off, N);
3725 begin
3726 null;
3727 end Analyze_Others_Choice;
88b32fc3 3728
0f1a6a0b
AC
3729 -------------------------------------------
3730 -- Analyze_Private_Extension_Declaration --
3731 -------------------------------------------
88b32fc3 3732
0f1a6a0b
AC
3733 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3734 T : constant Entity_Id := Defining_Identifier (N);
3735 Indic : constant Node_Id := Subtype_Indication (N);
0f1a6a0b
AC
3736 Parent_Type : Entity_Id;
3737 Parent_Base : Entity_Id;
88b32fc3 3738
0f1a6a0b
AC
3739 begin
3740 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
88b32fc3 3741
0f1a6a0b
AC
3742 if Is_Non_Empty_List (Interface_List (N)) then
3743 declare
3744 Intf : Node_Id;
3745 T : Entity_Id;
88b32fc3 3746
0f1a6a0b
AC
3747 begin
3748 Intf := First (Interface_List (N));
3749 while Present (Intf) loop
3750 T := Find_Type_Of_Subtype_Indic (Intf);
996ae0b0 3751
0f1a6a0b
AC
3752 Diagnose_Interface (Intf, T);
3753 Next (Intf);
3754 end loop;
3755 end;
996ae0b0
RK
3756 end if;
3757
0f1a6a0b 3758 Generate_Definition (T);
6191e212 3759
e606088a
AC
3760 -- For other than Ada 2012, just enter the name in the current scope
3761
6191e212
AC
3762 if Ada_Version < Ada_2012 then
3763 Enter_Name (T);
3764
3765 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
3766 -- case of private type that completes an incomplete type.
3767
3768 else
3769 declare
3770 Prev : Entity_Id;
3771
3772 begin
3773 Prev := Find_Type_Name (N);
3774
3775 pragma Assert (Prev = T
3776 or else (Ekind (Prev) = E_Incomplete_Type
3777 and then Present (Full_View (Prev))
3778 and then Full_View (Prev) = T));
3779 end;
3780 end if;
996ae0b0 3781
0f1a6a0b
AC
3782 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3783 Parent_Base := Base_Type (Parent_Type);
996ae0b0 3784
0f1a6a0b
AC
3785 if Parent_Type = Any_Type
3786 or else Etype (Parent_Type) = Any_Type
3787 then
3788 Set_Ekind (T, Ekind (Parent_Type));
3789 Set_Etype (T, Any_Type);
3790 goto Leave;
996ae0b0 3791
0f1a6a0b
AC
3792 elsif not Is_Tagged_Type (Parent_Type) then
3793 Error_Msg_N
3794 ("parent of type extension must be a tagged type ", Indic);
3795 goto Leave;
996ae0b0 3796
0f1a6a0b
AC
3797 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
3798 Error_Msg_N ("premature derivation of incomplete type", Indic);
3799 goto Leave;
3800
3801 elsif Is_Concurrent_Type (Parent_Type) then
3802 Error_Msg_N
3803 ("parent type of a private extension cannot be "
3804 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3805
3806 Set_Etype (T, Any_Type);
3807 Set_Ekind (T, E_Limited_Private_Type);
3808 Set_Private_Dependents (T, New_Elmt_List);
3809 Set_Error_Posted (T);
3810 goto Leave;
3811 end if;
3812
3813 -- Perhaps the parent type should be changed to the class-wide type's
3814 -- specific type in this case to prevent cascading errors ???
996ae0b0 3815
0f1a6a0b
AC
3816 if Is_Class_Wide_Type (Parent_Type) then
3817 Error_Msg_N
3818 ("parent of type extension must not be a class-wide type", Indic);
3819 goto Leave;
2b73cf68
JM
3820 end if;
3821
0f1a6a0b
AC
3822 if (not Is_Package_Or_Generic_Package (Current_Scope)
3823 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3824 or else In_Private_Part (Current_Scope)
3825
996ae0b0 3826 then
0f1a6a0b
AC
3827 Error_Msg_N ("invalid context for private extension", N);
3828 end if;
2b73cf68 3829
0f1a6a0b 3830 -- Set common attributes
2b73cf68 3831
0f1a6a0b
AC
3832 Set_Is_Pure (T, Is_Pure (Current_Scope));
3833 Set_Scope (T, Current_Scope);
3834 Set_Ekind (T, E_Record_Type_With_Private);
3835 Init_Size_Align (T);
996ae0b0 3836
0f1a6a0b
AC
3837 Set_Etype (T, Parent_Base);
3838 Set_Has_Task (T, Has_Task (Parent_Base));
3839
3840 Set_Convention (T, Convention (Parent_Type));
3841 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3842 Set_Is_First_Subtype (T);
3843 Make_Class_Wide_Type (T);
3844
3845 if Unknown_Discriminants_Present (N) then
3846 Set_Discriminant_Constraint (T, No_Elist);
996ae0b0
RK
3847 end if;
3848
0f1a6a0b 3849 Build_Derived_Record_Type (N, Parent_Type, T);
996ae0b0 3850
f2264ac2
RD
3851 -- Propagate inherited invariant information. The new type has
3852 -- invariants, if the parent type has inheritable invariants,
3853 -- and these invariants can in turn be inherited.
3854
3855 if Has_Inheritable_Invariants (Parent_Type) then
3856 Set_Has_Inheritable_Invariants (T);
3857 Set_Has_Invariants (T);
3858 end if;
3859
0f1a6a0b
AC
3860 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3861 -- synchronized formal derived type.
996ae0b0 3862
0f1a6a0b
AC
3863 if Ada_Version >= Ada_2005
3864 and then Synchronized_Present (N)
996ae0b0 3865 then
0f1a6a0b 3866 Set_Is_Limited_Record (T);
996ae0b0 3867
0f1a6a0b 3868 -- Formal derived type case
996ae0b0 3869
0f1a6a0b 3870 if Is_Generic_Type (T) then
996ae0b0 3871
0f1a6a0b
AC
3872 -- The parent must be a tagged limited type or a synchronized
3873 -- interface.
996ae0b0 3874
0f1a6a0b
AC
3875 if (not Is_Tagged_Type (Parent_Type)
3876 or else not Is_Limited_Type (Parent_Type))
996ae0b0 3877 and then
0f1a6a0b
AC
3878 (not Is_Interface (Parent_Type)
3879 or else not Is_Synchronized_Interface (Parent_Type))
996ae0b0 3880 then
0f1a6a0b
AC
3881 Error_Msg_NE ("parent type of & must be tagged limited " &
3882 "or synchronized", N, T);
3883 end if;
3884
3885 -- The progenitors (if any) must be limited or synchronized
3886 -- interfaces.
3887
3888 if Present (Interfaces (T)) then
996ae0b0 3889 declare
0f1a6a0b
AC
3890 Iface : Entity_Id;
3891 Iface_Elmt : Elmt_Id;
3892
996ae0b0 3893 begin
0f1a6a0b
AC
3894 Iface_Elmt := First_Elmt (Interfaces (T));
3895 while Present (Iface_Elmt) loop
3896 Iface := Node (Iface_Elmt);
996ae0b0 3897
0f1a6a0b
AC
3898 if not Is_Limited_Interface (Iface)
3899 and then not Is_Synchronized_Interface (Iface)
3900 then
3901 Error_Msg_NE ("progenitor & must be limited " &
3902 "or synchronized", N, Iface);
3903 end if;
3904
3905 Next_Elmt (Iface_Elmt);
3906 end loop;
996ae0b0
RK
3907 end;
3908 end if;
996ae0b0 3909
0f1a6a0b
AC
3910 -- Regular derived extension, the parent must be a limited or
3911 -- synchronized interface.
996ae0b0 3912
0f1a6a0b
AC
3913 else
3914 if not Is_Interface (Parent_Type)
3915 or else (not Is_Limited_Interface (Parent_Type)
3916 and then
3917 not Is_Synchronized_Interface (Parent_Type))
3918 then
3919 Error_Msg_NE
3920 ("parent type of & must be limited interface", N, T);
3921 end if;
3922 end if;
ce9e9122 3923
0f1a6a0b
AC
3924 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
3925 -- extension with a synchronized parent must be explicitly declared
3926 -- synchronized, because the full view will be a synchronized type.
3927 -- This must be checked before the check for limited types below,
3928 -- to ensure that types declared limited are not allowed to extend
3929 -- synchronized interfaces.
996ae0b0 3930
0f1a6a0b
AC
3931 elsif Is_Interface (Parent_Type)
3932 and then Is_Synchronized_Interface (Parent_Type)
3933 and then not Synchronized_Present (N)
3934 then
3935 Error_Msg_NE
3936 ("private extension of& must be explicitly synchronized",
3937 N, Parent_Type);
996ae0b0 3938
0f1a6a0b
AC
3939 elsif Limited_Present (N) then
3940 Set_Is_Limited_Record (T);
996ae0b0 3941
0f1a6a0b
AC
3942 if not Is_Limited_Type (Parent_Type)
3943 and then
3944 (not Is_Interface (Parent_Type)
3945 or else not Is_Limited_Interface (Parent_Type))
3946 then
3947 Error_Msg_NE ("parent type& of limited extension must be limited",
3948 N, Parent_Type);
3949 end if;
3950 end if;
fbf5a39b 3951
eaba57fb
RD
3952 <<Leave>>
3953 if Has_Aspects (N) then
3954 Analyze_Aspect_Specifications (N, T);
3955 end if;
0f1a6a0b 3956 end Analyze_Private_Extension_Declaration;
950d3e7d 3957
0f1a6a0b
AC
3958 ---------------------------------
3959 -- Analyze_Subtype_Declaration --
3960 ---------------------------------
950d3e7d 3961
0f1a6a0b
AC
3962 procedure Analyze_Subtype_Declaration
3963 (N : Node_Id;
3964 Skip : Boolean := False)
3965 is
3966 Id : constant Entity_Id := Defining_Identifier (N);
0f1a6a0b
AC
3967 T : Entity_Id;
3968 R_Checks : Check_Result;
950d3e7d 3969
0f1a6a0b
AC
3970 begin
3971 Generate_Definition (Id);
3972 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3973 Init_Size_Align (Id);
950d3e7d 3974
0f1a6a0b
AC
3975 -- The following guard condition on Enter_Name is to handle cases where
3976 -- the defining identifier has already been entered into the scope but
3977 -- the declaration as a whole needs to be analyzed.
950d3e7d 3978
0f1a6a0b
AC
3979 -- This case in particular happens for derived enumeration types. The
3980 -- derived enumeration type is processed as an inserted enumeration type
3981 -- declaration followed by a rewritten subtype declaration. The defining
3982 -- identifier, however, is entered into the name scope very early in the
3983 -- processing of the original type declaration and therefore needs to be
3984 -- avoided here, when the created subtype declaration is analyzed. (See
3985 -- Build_Derived_Types)
950d3e7d 3986
0f1a6a0b
AC
3987 -- This also happens when the full view of a private type is derived
3988 -- type with constraints. In this case the entity has been introduced
3989 -- in the private declaration.
950d3e7d 3990
0f1a6a0b
AC
3991 if Skip
3992 or else (Present (Etype (Id))
3993 and then (Is_Private_Type (Etype (Id))
3994 or else Is_Task_Type (Etype (Id))
3995 or else Is_Rewrite_Substitution (N)))
3996 then
3997 null;
950d3e7d 3998
0f1a6a0b
AC
3999 else
4000 Enter_Name (Id);
4001 end if;
950d3e7d 4002
0f1a6a0b 4003 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
996ae0b0 4004
0f1a6a0b 4005 -- Inherit common attributes
19f0526a 4006
0f1a6a0b
AC
4007 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4008 Set_Is_Volatile (Id, Is_Volatile (T));
4009 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4010 Set_Is_Atomic (Id, Is_Atomic (T));
4011 Set_Is_Ada_2005_Only (Id, Is_Ada_2005_Only (T));
4012 Set_Is_Ada_2012_Only (Id, Is_Ada_2012_Only (T));
4013 Set_Convention (Id, Convention (T));
86200f66
RD
4014
4015 -- If ancestor has predicates then so does the subtype, and in addition
4016 -- we must delay the freeze to properly arrange predicate inheritance.
4017
f2acf80c
AC
4018 -- The Ancestor_Type test is a big kludge, there seem to be cases in
4019 -- which T = ID, so the above tests and assignments do nothing???
4020
4021 if Has_Predicates (T)
4022 or else (Present (Ancestor_Subtype (T))
4023 and then Has_Predicates (Ancestor_Subtype (T)))
4024 then
86200f66
RD
4025 Set_Has_Predicates (Id);
4026 Set_Has_Delayed_Freeze (Id);
4027 end if;
fbf5a39b 4028
7ff2d234
AC
4029 -- Subtype of Boolean is not allowed to have a constraint in SPARK or
4030 -- ALFA.
4031
4032 if Formal_Verification_Mode
4033 and then Comes_From_Source (Original_Node (N))
4034 and then Is_Boolean_Type (T)
4035 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4036 then
4037 Error_Msg_F ("|~~subtype of Boolean cannot have constraint", N);
4038 end if;
4039
0f1a6a0b
AC
4040 -- In the case where there is no constraint given in the subtype
4041 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4042 -- semantic attributes must be established here.
fea9e956 4043
0f1a6a0b
AC
4044 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4045 Set_Etype (Id, Base_Type (T));
fea9e956 4046
7ff2d234
AC
4047 -- Subtype of unconstrained array without constraint is not allowed
4048 -- in SPARK or ALFA.
4049
4050 if Formal_Verification_Mode
4051 and then Comes_From_Source (Original_Node (N))
4052 and then Is_Array_Type (T)
4053 and then not Is_Constrained (T)
4054 then
4055 Error_Msg_F
4056 ("|~~subtype of unconstrained array must have constraint", N);
4057 end if;
4058
4059 -- Proceed with analysis
4060
0f1a6a0b
AC
4061 case Ekind (T) is
4062 when Array_Kind =>
4063 Set_Ekind (Id, E_Array_Subtype);
4064 Copy_Array_Subtype_Attributes (Id, T);
996ae0b0 4065
0f1a6a0b
AC
4066 when Decimal_Fixed_Point_Kind =>
4067 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4068 Set_Digits_Value (Id, Digits_Value (T));
4069 Set_Delta_Value (Id, Delta_Value (T));
4070 Set_Scale_Value (Id, Scale_Value (T));
4071 Set_Small_Value (Id, Small_Value (T));
4072 Set_Scalar_Range (Id, Scalar_Range (T));
4073 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4074 Set_Is_Constrained (Id, Is_Constrained (T));
4075 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4076 Set_RM_Size (Id, RM_Size (T));
996ae0b0 4077
0f1a6a0b
AC
4078 when Enumeration_Kind =>
4079 Set_Ekind (Id, E_Enumeration_Subtype);
4080 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4081 Set_Scalar_Range (Id, Scalar_Range (T));
4082 Set_Is_Character_Type (Id, Is_Character_Type (T));
4083 Set_Is_Constrained (Id, Is_Constrained (T));
4084 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4085 Set_RM_Size (Id, RM_Size (T));
996ae0b0 4086
0f1a6a0b
AC
4087 when Ordinary_Fixed_Point_Kind =>
4088 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4089 Set_Scalar_Range (Id, Scalar_Range (T));
4090 Set_Small_Value (Id, Small_Value (T));
4091 Set_Delta_Value (Id, Delta_Value (T));
4092 Set_Is_Constrained (Id, Is_Constrained (T));
4093 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4094 Set_RM_Size (Id, RM_Size (T));
996ae0b0 4095
0f1a6a0b
AC
4096 when Float_Kind =>
4097 Set_Ekind (Id, E_Floating_Point_Subtype);
4098 Set_Scalar_Range (Id, Scalar_Range (T));
4099 Set_Digits_Value (Id, Digits_Value (T));
4100 Set_Is_Constrained (Id, Is_Constrained (T));
996ae0b0 4101
0f1a6a0b
AC
4102 when Signed_Integer_Kind =>
4103 Set_Ekind (Id, E_Signed_Integer_Subtype);
4104 Set_Scalar_Range (Id, Scalar_Range (T));
4105 Set_Is_Constrained (Id, Is_Constrained (T));
4106 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4107 Set_RM_Size (Id, RM_Size (T));
996ae0b0 4108
0f1a6a0b
AC
4109 when Modular_Integer_Kind =>
4110 Set_Ekind (Id, E_Modular_Integer_Subtype);
4111 Set_Scalar_Range (Id, Scalar_Range (T));
4112 Set_Is_Constrained (Id, Is_Constrained (T));
4113 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4114 Set_RM_Size (Id, RM_Size (T));
996ae0b0 4115
0f1a6a0b
AC
4116 when Class_Wide_Kind =>
4117 Set_Ekind (Id, E_Class_Wide_Subtype);
4118 Set_First_Entity (Id, First_Entity (T));
4119 Set_Last_Entity (Id, Last_Entity (T));
4120 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4121 Set_Cloned_Subtype (Id, T);
4122 Set_Is_Tagged_Type (Id, True);
4123 Set_Has_Unknown_Discriminants
4124 (Id, True);
996ae0b0 4125
0f1a6a0b
AC
4126 if Ekind (T) = E_Class_Wide_Subtype then
4127 Set_Equivalent_Type (Id, Equivalent_Type (T));
4128 end if;
996ae0b0 4129
0f1a6a0b
AC
4130 when E_Record_Type | E_Record_Subtype =>
4131 Set_Ekind (Id, E_Record_Subtype);
996ae0b0 4132
0f1a6a0b
AC
4133 if Ekind (T) = E_Record_Subtype
4134 and then Present (Cloned_Subtype (T))
4135 then
4136 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4137 else
4138 Set_Cloned_Subtype (Id, T);
4139 end if;
996ae0b0 4140
0f1a6a0b
AC
4141 Set_First_Entity (Id, First_Entity (T));
4142 Set_Last_Entity (Id, Last_Entity (T));
4143 Set_Has_Discriminants (Id, Has_Discriminants (T));
4144 Set_Is_Constrained (Id, Is_Constrained (T));
4145 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4146 Set_Has_Unknown_Discriminants
4147 (Id, Has_Unknown_Discriminants (T));
996ae0b0 4148
0f1a6a0b
AC
4149 if Has_Discriminants (T) then
4150 Set_Discriminant_Constraint
4151 (Id, Discriminant_Constraint (T));
4152 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
996ae0b0 4153
0f1a6a0b
AC
4154 elsif Has_Unknown_Discriminants (Id) then
4155 Set_Discriminant_Constraint (Id, No_Elist);
4156 end if;
996ae0b0 4157
0f1a6a0b
AC
4158 if Is_Tagged_Type (T) then
4159 Set_Is_Tagged_Type (Id);
4160 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
ef2a63ba
JM
4161 Set_Direct_Primitive_Operations
4162 (Id, Direct_Primitive_Operations (T));
0f1a6a0b 4163 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
996ae0b0 4164
0f1a6a0b
AC
4165 if Is_Interface (T) then
4166 Set_Is_Interface (Id);
4167 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4168 end if;
4169 end if;
996ae0b0 4170
0f1a6a0b
AC
4171 when Private_Kind =>
4172 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4173 Set_Has_Discriminants (Id, Has_Discriminants (T));
4174 Set_Is_Constrained (Id, Is_Constrained (T));
4175 Set_First_Entity (Id, First_Entity (T));
4176 Set_Last_Entity (Id, Last_Entity (T));
4177 Set_Private_Dependents (Id, New_Elmt_List);
4178 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4179 Set_Has_Unknown_Discriminants
4180 (Id, Has_Unknown_Discriminants (T));
4181 Set_Known_To_Have_Preelab_Init
4182 (Id, Known_To_Have_Preelab_Init (T));
4183
4184 if Is_Tagged_Type (T) then
ef2a63ba
JM
4185 Set_Is_Tagged_Type (Id);
4186 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4187 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4188 Set_Direct_Primitive_Operations (Id,
4189 Direct_Primitive_Operations (T));
996ae0b0
RK
4190 end if;
4191
0f1a6a0b
AC
4192 -- In general the attributes of the subtype of a private type
4193 -- are the attributes of the partial view of parent. However,
4194 -- the full view may be a discriminated type, and the subtype
4195 -- must share the discriminant constraint to generate correct
4196 -- calls to initialization procedures.
996ae0b0 4197
0f1a6a0b
AC
4198 if Has_Discriminants (T) then
4199 Set_Discriminant_Constraint
4200 (Id, Discriminant_Constraint (T));
4201 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
996ae0b0 4202
0f1a6a0b
AC
4203 elsif Present (Full_View (T))
4204 and then Has_Discriminants (Full_View (T))
4205 then
4206 Set_Discriminant_Constraint
4207 (Id, Discriminant_Constraint (Full_View (T)));
4208 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
996ae0b0 4209
0f1a6a0b
AC
4210 -- This would seem semantically correct, but apparently
4211 -- confuses the back-end. To be explained and checked with
4212 -- current version ???
996ae0b0 4213
0f1a6a0b
AC
4214 -- Set_Has_Discriminants (Id);
4215 end if;
996ae0b0 4216
0f1a6a0b 4217 Prepare_Private_Subtype_Completion (Id, N);
996ae0b0 4218
0f1a6a0b
AC
4219 when Access_Kind =>
4220 Set_Ekind (Id, E_Access_Subtype);
4221 Set_Is_Constrained (Id, Is_Constrained (T));
4222 Set_Is_Access_Constant
4223 (Id, Is_Access_Constant (T));
4224 Set_Directly_Designated_Type
4225 (Id, Designated_Type (T));
4226 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4227
4228 -- A Pure library_item must not contain the declaration of a
4229 -- named access type, except within a subprogram, generic
4230 -- subprogram, task unit, or protected unit, or if it has
4231 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4232
4233 if Comes_From_Source (Id)
4234 and then In_Pure_Unit
4235 and then not In_Subprogram_Task_Protected_Unit
4236 and then not No_Pool_Assigned (Id)
996ae0b0 4237 then
0f1a6a0b
AC
4238 Error_Msg_N
4239 ("named access types not allowed in pure unit", N);
996ae0b0
RK
4240 end if;
4241
0f1a6a0b
AC
4242 when Concurrent_Kind =>
4243 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4244 Set_Corresponding_Record_Type (Id,
4245 Corresponding_Record_Type (T));
4246 Set_First_Entity (Id, First_Entity (T));
4247 Set_First_Private_Entity (Id, First_Private_Entity (T));
4248 Set_Has_Discriminants (Id, Has_Discriminants (T));
4249 Set_Is_Constrained (Id, Is_Constrained (T));
4250 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4251 Set_Last_Entity (Id, Last_Entity (T));
8a6a52dc 4252
0f1a6a0b
AC
4253 if Has_Discriminants (T) then
4254 Set_Discriminant_Constraint (Id,
4255 Discriminant_Constraint (T));
4256 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
8a6a52dc
AC
4257 end if;
4258
0f1a6a0b
AC
4259 when E_Incomplete_Type =>
4260 if Ada_Version >= Ada_2005 then
4261 Set_Ekind (Id, E_Incomplete_Subtype);
996ae0b0 4262
0f1a6a0b
AC
4263 -- Ada 2005 (AI-412): Decorate an incomplete subtype
4264 -- of an incomplete type visible through a limited
4265 -- with clause.
996ae0b0 4266
0f1a6a0b
AC
4267 if From_With_Type (T)
4268 and then Present (Non_Limited_View (T))
4269 then
4270 Set_From_With_Type (Id);
4271 Set_Non_Limited_View (Id, Non_Limited_View (T));
996ae0b0 4272
0f1a6a0b
AC
4273 -- Ada 2005 (AI-412): Add the regular incomplete subtype
4274 -- to the private dependents of the original incomplete
4275 -- type for future transformation.
996ae0b0 4276
0f1a6a0b
AC
4277 else
4278 Append_Elmt (Id, Private_Dependents (T));
4279 end if;
996ae0b0 4280
0f1a6a0b
AC
4281 -- If the subtype name denotes an incomplete type an error
4282 -- was already reported by Process_Subtype.
1c218ac3 4283
0f1a6a0b
AC
4284 else
4285 Set_Etype (Id, Any_Type);
4286 end if;
1c218ac3 4287
996ae0b0
RK
4288 when others =>
4289 raise Program_Error;
996ae0b0
RK
4290 end case;
4291 end if;
4292
0f1a6a0b
AC
4293 if Etype (Id) = Any_Type then
4294 goto Leave;
996ae0b0
RK
4295 end if;
4296
0f1a6a0b 4297 -- Some common processing on all types
996ae0b0 4298
0f1a6a0b
AC
4299 Set_Size_Info (Id, T);
4300 Set_First_Rep_Item (Id, First_Rep_Item (T));
996ae0b0 4301
0f1a6a0b 4302 T := Etype (Id);
996ae0b0 4303
0f1a6a0b
AC
4304 Set_Is_Immediately_Visible (Id, True);
4305 Set_Depends_On_Private (Id, Has_Private_Component (T));
4306 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
996ae0b0 4307
0f1a6a0b
AC
4308 if Is_Interface (T) then
4309 Set_Is_Interface (Id);
4310 end if;
03b64787 4311
0f1a6a0b
AC
4312 if Present (Generic_Parent_Type (N))
4313 and then
4314 (Nkind
4315 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
4316 or else Nkind
4317 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
4318 /= N_Formal_Private_Type_Definition)
4319 then
4320 if Is_Tagged_Type (Id) then
996ae0b0 4321
0f1a6a0b
AC
4322 -- If this is a generic actual subtype for a synchronized type,
4323 -- the primitive operations are those of the corresponding record
4324 -- for which there is a separate subtype declaration.
996ae0b0 4325
0f1a6a0b
AC
4326 if Is_Concurrent_Type (Id) then
4327 null;
4328 elsif Is_Class_Wide_Type (Id) then
4329 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
4330 else
4331 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
4332 end if;
8737a29a 4333
0f1a6a0b
AC
4334 elsif Scope (Etype (Id)) /= Standard_Standard then
4335 Derive_Subprograms (Generic_Parent_Type (N), Id);
996ae0b0 4336 end if;
0f1a6a0b 4337 end if;
996ae0b0 4338
0f1a6a0b
AC
4339 if Is_Private_Type (T)
4340 and then Present (Full_View (T))
4341 then
4342 Conditional_Delay (Id, Full_View (T));
996ae0b0 4343
0f1a6a0b
AC
4344 -- The subtypes of components or subcomponents of protected types
4345 -- do not need freeze nodes, which would otherwise appear in the
4346 -- wrong scope (before the freeze node for the protected type). The
4347 -- proper subtypes are those of the subcomponents of the corresponding
4348 -- record.
996ae0b0 4349
0f1a6a0b
AC
4350 elsif Ekind (Scope (Id)) /= E_Protected_Type
4351 and then Present (Scope (Scope (Id))) -- error defense!
4352 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
4353 then
4354 Conditional_Delay (Id, T);
4355 end if;
996ae0b0 4356
0f1a6a0b
AC
4357 -- Check that constraint_error is raised for a scalar subtype
4358 -- indication when the lower or upper bound of a non-null range
4359 -- lies outside the range of the type mark.
fea9e956 4360
0f1a6a0b
AC
4361 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4362 if Is_Scalar_Type (Etype (Id))
4363 and then Scalar_Range (Id) /=
4364 Scalar_Range (Etype (Subtype_Mark
4365 (Subtype_Indication (N))))
4366 then
4367 Apply_Range_Check
4368 (Scalar_Range (Id),
4369 Etype (Subtype_Mark (Subtype_Indication (N))));
fea9e956 4370
0f1a6a0b
AC
4371 elsif Is_Array_Type (Etype (Id))
4372 and then Present (First_Index (Id))
4373 then
4374 -- This really should be a subprogram that finds the indications
4375 -- to check???
996ae0b0 4376
0f1a6a0b
AC
4377 if ((Nkind (First_Index (Id)) = N_Identifier
4378 and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
4379 or else Nkind (First_Index (Id)) = N_Subtype_Indication)
4380 and then
4381 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
4382 then
4383 declare
4384 Target_Typ : constant Entity_Id :=
4385 Etype
4386 (First_Index (Etype
4387 (Subtype_Mark (Subtype_Indication (N)))));
4388 begin
4389 R_Checks :=
4390 Get_Range_Checks
4391 (Scalar_Range (Etype (First_Index (Id))),
4392 Target_Typ,
4393 Etype (First_Index (Id)),
4394 Defining_Identifier (N));
996ae0b0 4395
0f1a6a0b
AC
4396 Insert_Range_Checks
4397 (R_Checks,
4398 N,
4399 Target_Typ,
4400 Sloc (Defining_Identifier (N)));
4401 end;
4402 end if;
4403 end if;
4404 end if;
996ae0b0 4405
6b958cec 4406 -- Make sure that generic actual types are properly frozen. The subtype
718deaf1
AC
4407 -- is marked as a generic actual type when the enclosing instance is
4408 -- analyzed, so here we identify the subtype from the tree structure.
c159409f
AC
4409
4410 if Expander_Active
4411 and then Is_Generic_Actual_Type (Id)
718deaf1
AC
4412 and then In_Instance
4413 and then not Comes_From_Source (N)
4414 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
4415 and then Is_Frozen (T)
c159409f 4416 then
6b958cec 4417 Freeze_Before (N, Id);
c159409f
AC
4418 end if;
4419
0f1a6a0b
AC
4420 Set_Optimize_Alignment_Flags (Id);
4421 Check_Eliminated (Id);
996ae0b0 4422
2d4e0553 4423 <<Leave>>
eaba57fb
RD
4424 if Has_Aspects (N) then
4425 Analyze_Aspect_Specifications (N, Id);
4426 end if;
0f1a6a0b 4427 end Analyze_Subtype_Declaration;
996ae0b0 4428
0f1a6a0b
AC
4429 --------------------------------
4430 -- Analyze_Subtype_Indication --
4431 --------------------------------
9c510803 4432
0f1a6a0b
AC
4433 procedure Analyze_Subtype_Indication (N : Node_Id) is
4434 T : constant Entity_Id := Subtype_Mark (N);
4435 R : constant Node_Id := Range_Expression (Constraint (N));
4436
4437 begin
4438 Analyze (T);
4439
4440 if R /= Error then
4441 Analyze (R);
4442 Set_Etype (N, Etype (R));
4443 Resolve (R, Entity (T));
4444 else
4445 Set_Error_Posted (R);
4446 Set_Error_Posted (T);
4447 end if;
4448 end Analyze_Subtype_Indication;
996ae0b0
RK
4449
4450 --------------------------
4451 -- Analyze_Variant_Part --
4452 --------------------------
4453
4454 procedure Analyze_Variant_Part (N : Node_Id) is
4455
4456 procedure Non_Static_Choice_Error (Choice : Node_Id);
33931112
JM
4457 -- Error routine invoked by the generic instantiation below when the
4458 -- variant part has a non static choice.
996ae0b0
RK
4459
4460 procedure Process_Declarations (Variant : Node_Id);
33931112
JM
4461 -- Analyzes all the declarations associated with a Variant. Needed by
4462 -- the generic instantiation below.
996ae0b0
RK
4463
4464 package Variant_Choices_Processing is new
4465 Generic_Choices_Processing
4466 (Get_Alternatives => Variants,
4467 Get_Choices => Discrete_Choices,
4468 Process_Empty_Choice => No_OP,
4469 Process_Non_Static_Choice => Non_Static_Choice_Error,
4470 Process_Associated_Node => Process_Declarations);
4471 use Variant_Choices_Processing;
ffe9aba8 4472 -- Instantiation of the generic choice processing package
996ae0b0
RK
4473
4474 -----------------------------
4475 -- Non_Static_Choice_Error --
4476 -----------------------------
4477
4478 procedure Non_Static_Choice_Error (Choice : Node_Id) is
4479 begin
fbf5a39b
AC
4480 Flag_Non_Static_Expr
4481 ("choice given in variant part is not static!", Choice);
996ae0b0
RK
4482 end Non_Static_Choice_Error;
4483
4484 --------------------------
4485 -- Process_Declarations --
4486 --------------------------
4487
4488 procedure Process_Declarations (Variant : Node_Id) is
4489 begin
4490 if not Null_Present (Component_List (Variant)) then
4491 Analyze_Declarations (Component_Items (Component_List (Variant)));
4492
4493 if Present (Variant_Part (Component_List (Variant))) then
4494 Analyze (Variant_Part (Component_List (Variant)));
4495 end if;
4496 end if;
4497 end Process_Declarations;
4498
ce4a6e84 4499 -- Local Variables
996ae0b0 4500
996ae0b0
RK
4501 Discr_Name : Node_Id;
4502 Discr_Type : Entity_Id;
4503
996ae0b0
RK
4504 Dont_Care : Boolean;
4505 Others_Present : Boolean := False;
4506
0501956d
GD
4507 pragma Warnings (Off, Dont_Care);
4508 pragma Warnings (Off, Others_Present);
4509 -- We don't care about the assigned values of any of these
4510
996ae0b0
RK
4511 -- Start of processing for Analyze_Variant_Part
4512
4513 begin
4514 Discr_Name := Name (N);
4515 Analyze (Discr_Name);
4516
ce4a6e84 4517 -- If Discr_Name bad, get out (prevent cascaded errors)
2b73cf68 4518
ce4a6e84 4519 if Etype (Discr_Name) = Any_Type then
2b73cf68 4520 return;
ce4a6e84 4521 end if;
2b73cf68 4522
ce4a6e84
RD
4523 -- Check invalid discriminant in variant part
4524
4525 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
996ae0b0
RK
4526 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4527 end if;
4528
4529 Discr_Type := Etype (Entity (Discr_Name));
4530
855ff2e1
GB
4531 if not Is_Discrete_Type (Discr_Type) then
4532 Error_Msg_N
4533 ("discriminant in a variant part must be of a discrete type",
4534 Name (N));
4535 return;
4536 end if;
4537
996ae0b0
RK
4538 -- Call the instantiated Analyze_Choices which does the rest of the work
4539
86200f66 4540 Analyze_Choices (N, Discr_Type, Dont_Care, Others_Present);
996ae0b0
RK
4541 end Analyze_Variant_Part;
4542
4543 ----------------------------
4544 -- Array_Type_Declaration --
4545 ----------------------------
4546
4547 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
a397db96 4548 Component_Def : constant Node_Id := Component_Definition (Def);
996ae0b0
RK
4549 Element_Type : Entity_Id;
4550 Implicit_Base : Entity_Id;
4551 Index : Node_Id;
4552 Related_Id : Entity_Id := Empty;
4553 Nb_Index : Nat;
4554 P : constant Node_Id := Parent (Def);
4555 Priv : Entity_Id;
4556
4557 begin
4558 if Nkind (Def) = N_Constrained_Array_Definition then
996ae0b0 4559 Index := First (Discrete_Subtype_Definitions (Def));
6e937c1c
AC
4560 else
4561 Index := First (Subtype_Marks (Def));
4562 end if;
996ae0b0 4563
33931112
JM
4564 -- Find proper names for the implicit types which may be public. In case
4565 -- of anonymous arrays we use the name of the first object of that type
4566 -- as prefix.
996ae0b0 4567
6e937c1c
AC
4568 if No (T) then
4569 Related_Id := Defining_Identifier (P);
996ae0b0 4570 else
6e937c1c 4571 Related_Id := T;
996ae0b0
RK
4572 end if;
4573
4574 Nb_Index := 1;
996ae0b0
RK
4575 while Present (Index) loop
4576 Analyze (Index);
88b32fc3
BD
4577
4578 -- Add a subtype declaration for each index of private array type
4579 -- declaration whose etype is also private. For example:
4580
4581 -- package Pkg is
4582 -- type Index is private;
4583 -- private
4584 -- type Table is array (Index) of ...
4585 -- end;
4586
33931112
JM
4587 -- This is currently required by the expander for the internally
4588 -- generated equality subprogram of records with variant parts in
4589 -- which the etype of some component is such private type.
88b32fc3
BD
4590
4591 if Ekind (Current_Scope) = E_Package
4592 and then In_Private_Part (Current_Scope)
4593 and then Has_Private_Declaration (Etype (Index))
4594 then
4595 declare
4596 Loc : constant Source_Ptr := Sloc (Def);
4597 New_E : Entity_Id;
4598 Decl : Entity_Id;
4599
4600 begin
092ef350 4601 New_E := Make_Temporary (Loc, 'T');
88b32fc3
BD
4602 Set_Is_Internal (New_E);
4603
4604 Decl :=
4605 Make_Subtype_Declaration (Loc,
4606 Defining_Identifier => New_E,
4607 Subtype_Indication =>
4608 New_Occurrence_Of (Etype (Index), Loc));
4609
4610 Insert_Before (Parent (Def), Decl);
4611 Analyze (Decl);
4612 Set_Etype (Index, New_E);
4613
4614 -- If the index is a range the Entity attribute is not
4615 -- available. Example:
4616
4617 -- package Pkg is
4618 -- type T is private;
4619 -- private
4620 -- type T is new Natural;
4621 -- Table : array (T(1) .. T(10)) of Boolean;
4622 -- end Pkg;
4623
4624 if Nkind (Index) /= N_Range then
4625 Set_Entity (Index, New_E);
4626 end if;
4627 end;
4628 end if;
4629
996ae0b0 4630 Make_Index (Index, P, Related_Id, Nb_Index);
ea034236
AC
4631
4632 -- Check error of subtype with predicate for index type
4633
ed00f472
RD
4634 Bad_Predicated_Subtype_Use
4635 ("subtype& has predicate, not allowed as index subtype",
4636 Index, Etype (Index));
ea034236
AC
4637
4638 -- Move to next index
4639
996ae0b0
RK
4640 Next_Index (Index);
4641 Nb_Index := Nb_Index + 1;
4642 end loop;
4643
88b32fc3
BD
4644 -- Process subtype indication if one is present
4645
6e937c1c 4646 if Present (Subtype_Indication (Component_Def)) then
88b32fc3
BD
4647 Element_Type :=
4648 Process_Subtype
4649 (Subtype_Indication (Component_Def), P, Related_Id, 'C');
6e937c1c 4650
0ab80019 4651 -- Ada 2005 (AI-230): Access Definition case
6e937c1c 4652
9bc856dd 4653 else pragma Assert (Present (Access_Definition (Component_Def)));
fea9e956
ES
4654
4655 -- Indicate that the anonymous access type is created by the
4656 -- array type declaration.
4657
6e937c1c 4658 Element_Type := Access_Definition
fea9e956 4659 (Related_Nod => P,
6e937c1c 4660 N => Access_Definition (Component_Def));
758c442c 4661 Set_Is_Local_Anonymous_Access (Element_Type);
6e937c1c 4662
fea9e956
ES
4663 -- Propagate the parent. This field is needed if we have to generate
4664 -- the master_id associated with an anonymous access to task type
4665 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4666
4667 Set_Parent (Element_Type, Parent (T));
4668
33931112
JM
4669 -- Ada 2005 (AI-230): In case of components that are anonymous access
4670 -- types the level of accessibility depends on the enclosing type
4671 -- declaration
35b7fa6a 4672
0ab80019 4673 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
35b7fa6a 4674
0ab80019 4675 -- Ada 2005 (AI-254)
7324bf49 4676
af4b9434
AC
4677 declare
4678 CD : constant Node_Id :=
4679 Access_To_Subprogram_Definition
4680 (Access_Definition (Component_Def));
4681 begin
4682 if Present (CD) and then Protected_Present (CD) then
4683 Element_Type :=
fea9e956 4684 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
af4b9434
AC
4685 end if;
4686 end;
6e937c1c 4687 end if;
996ae0b0
RK
4688
4689 -- Constrained array case
4690
4691 if No (T) then
4692 T := Create_Itype (E_Void, P, Related_Id, 'T');
4693 end if;
4694
4695 if Nkind (Def) = N_Constrained_Array_Definition then
4696
4697 -- Establish Implicit_Base as unconstrained base type
4698
4699 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4700
996ae0b0
RK
4701 Set_Etype (Implicit_Base, Implicit_Base);
4702 Set_Scope (Implicit_Base, Current_Scope);
4703 Set_Has_Delayed_Freeze (Implicit_Base);
4704
4705 -- The constrained array type is a subtype of the unconstrained one
4706
4707 Set_Ekind (T, E_Array_Subtype);
4708 Init_Size_Align (T);
4709 Set_Etype (T, Implicit_Base);
4710 Set_Scope (T, Current_Scope);
4711 Set_Is_Constrained (T, True);
4712 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4713 Set_Has_Delayed_Freeze (T);
4714
4715 -- Complete setup of implicit base type
4716
fea9e956
ES
4717 Set_First_Index (Implicit_Base, First_Index (T));
4718 Set_Component_Type (Implicit_Base, Element_Type);
4719 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4720 Set_Component_Size (Implicit_Base, Uint_0);
4721 Set_Packed_Array_Type (Implicit_Base, Empty);
07fc65c4 4722 Set_Has_Controlled_Component
fea9e956
ES
4723 (Implicit_Base, Has_Controlled_Component
4724 (Element_Type)
4725 or else Is_Controlled
4726 (Element_Type));
07fc65c4 4727 Set_Finalize_Storage_Only
fea9e956
ES
4728 (Implicit_Base, Finalize_Storage_Only
4729 (Element_Type));
996ae0b0
RK
4730
4731 -- Unconstrained array case
4732
4733 else
4734 Set_Ekind (T, E_Array_Type);
4735 Init_Size_Align (T);
4736 Set_Etype (T, T);
4737 Set_Scope (T, Current_Scope);
4738 Set_Component_Size (T, Uint_0);
4739 Set_Is_Constrained (T, False);
4740 Set_First_Index (T, First (Subtype_Marks (Def)));
4741 Set_Has_Delayed_Freeze (T, True);
07fc65c4
GB
4742 Set_Has_Task (T, Has_Task (Element_Type));
4743 Set_Has_Controlled_Component (T, Has_Controlled_Component
4744 (Element_Type)
4745 or else
4746 Is_Controlled (Element_Type));
4747 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4748 (Element_Type));
996ae0b0
RK
4749 end if;
4750
fea9e956
ES
4751 -- Common attributes for both cases
4752
07fc65c4 4753 Set_Component_Type (Base_Type (T), Element_Type);
fea9e956 4754 Set_Packed_Array_Type (T, Empty);
996ae0b0 4755
a397db96 4756 if Aliased_Present (Component_Definition (Def)) then
996ae0b0
RK
4757 Set_Has_Aliased_Components (Etype (T));
4758 end if;
4759
0ab80019 4760 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
9dfd2ff8 4761 -- array type to ensure that objects of this type are initialized.
2820d220 4762
0791fbe9 4763 if Ada_Version >= Ada_2005
9dfd2ff8 4764 and then Can_Never_Be_Null (Element_Type)
2820d220
AC
4765 then
4766 Set_Can_Never_Be_Null (T);
4767
4768 if Null_Exclusion_Present (Component_Definition (Def))
9dfd2ff8 4769
33931112
JM
4770 -- No need to check itypes because in their case this check was
4771 -- done at their point of creation
9dfd2ff8
CC
4772
4773 and then not Is_Itype (Element_Type)
2820d220
AC
4774 then
4775 Error_Msg_N
2b73cf68 4776 ("`NOT NULL` not allowed (null already excluded)",
2820d220
AC
4777 Subtype_Indication (Component_Definition (Def)));
4778 end if;
4779 end if;
4780
996ae0b0
RK
4781 Priv := Private_Component (Element_Type);
4782
4783 if Present (Priv) then
07fc65c4
GB
4784
4785 -- Check for circular definitions
996ae0b0
RK
4786
4787 if Priv = Any_Type then
996ae0b0
RK
4788 Set_Component_Type (Etype (T), Any_Type);
4789
fbf5a39b 4790 -- There is a gap in the visibility of operations on the composite
996ae0b0
RK
4791 -- type only if the component type is defined in a different scope.
4792
4793 elsif Scope (Priv) = Current_Scope then
4794 null;
4795
4796 elsif Is_Limited_Type (Priv) then
4797 Set_Is_Limited_Composite (Etype (T));
4798 Set_Is_Limited_Composite (T);
4799 else
4800 Set_Is_Private_Composite (Etype (T));
4801 Set_Is_Private_Composite (T);
4802 end if;
4803 end if;
4804
33931112
JM
4805 -- A syntax error in the declaration itself may lead to an empty index
4806 -- list, in which case do a minimal patch.
2b73cf68
JM
4807
4808 if No (First_Index (T)) then
4809 Error_Msg_N ("missing index definition in array type declaration", T);
4810
4811 declare
3b42c566 4812 Indexes : constant List_Id :=
dc06abec 4813 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
2b73cf68 4814 begin
3b42c566
RD
4815 Set_Discrete_Subtype_Definitions (Def, Indexes);
4816 Set_First_Index (T, First (Indexes));
2b73cf68
JM
4817 return;
4818 end;
4819 end if;
4820
c6fe3827
GD
4821 -- Create a concatenation operator for the new type. Internal array
4822 -- types created for packed entities do not need such, they are
4823 -- compatible with the user-defined type.
996ae0b0
RK
4824
4825 if Number_Dimensions (T) = 1
4826 and then not Is_Packed_Array_Type (T)
4827 then
6c1e24d3 4828 New_Concatenation_Op (T);
996ae0b0
RK
4829 end if;
4830
c6fe3827 4831 -- In the case of an unconstrained array the parser has already verified
3b42c566 4832 -- that all the indexes are unconstrained but we still need to make sure
c6fe3827 4833 -- that the element type is constrained.
996ae0b0
RK
4834
4835 if Is_Indefinite_Subtype (Element_Type) then
4836 Error_Msg_N
a397db96
AC
4837 ("unconstrained element type in array declaration",
4838 Subtype_Indication (Component_Def));
996ae0b0 4839
fea9e956 4840 elsif Is_Abstract_Type (Element_Type) then
a397db96 4841 Error_Msg_N
758c442c 4842 ("the type of a component cannot be abstract",
a397db96 4843 Subtype_Indication (Component_Def));
996ae0b0 4844 end if;
996ae0b0
RK
4845 end Array_Type_Declaration;
4846
7324bf49
AC
4847 ------------------------------------------------------
4848 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4849 ------------------------------------------------------
4850
4851 function Replace_Anonymous_Access_To_Protected_Subprogram
c6fe3827 4852 (N : Node_Id) return Entity_Id
7324bf49
AC
4853 is
4854 Loc : constant Source_Ptr := Sloc (N);
4855
4856 Curr_Scope : constant Scope_Stack_Entry :=
4857 Scope_Stack.Table (Scope_Stack.Last);
4858
092ef350 4859 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
7324bf49
AC
4860 Acc : Node_Id;
4861 Comp : Node_Id;
4862 Decl : Node_Id;
9dfd2ff8 4863 P : Node_Id;
7324bf49
AC
4864
4865 begin
4866 Set_Is_Internal (Anon);
4867
4868 case Nkind (N) is
4869 when N_Component_Declaration |
4870 N_Unconstrained_Array_Definition |
4871 N_Constrained_Array_Definition =>
4872 Comp := Component_Definition (N);
fea9e956 4873 Acc := Access_Definition (Comp);
7324bf49
AC
4874
4875 when N_Discriminant_Specification =>
4876 Comp := Discriminant_Type (N);
fea9e956 4877 Acc := Comp;
7324bf49
AC
4878
4879 when N_Parameter_Specification =>
4880 Comp := Parameter_Type (N);
fea9e956
ES
4881 Acc := Comp;
4882
2b73cf68
JM
4883 when N_Access_Function_Definition =>
4884 Comp := Result_Definition (N);
4885 Acc := Comp;
4886
fea9e956
ES
4887 when N_Object_Declaration =>
4888 Comp := Object_Definition (N);
4889 Acc := Comp;
7324bf49 4890
b1c11e0e
JM
4891 when N_Function_Specification =>
4892 Comp := Result_Definition (N);
4893 Acc := Comp;
4894
7324bf49 4895 when others =>
9bc856dd 4896 raise Program_Error;
7324bf49
AC
4897 end case;
4898
4899 Decl := Make_Full_Type_Declaration (Loc,
4900 Defining_Identifier => Anon,
4901 Type_Definition =>
af4b9434 4902 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
7324bf49
AC
4903
4904 Mark_Rewrite_Insertion (Decl);
4905
cd1c668b
ES
4906 -- Insert the new declaration in the nearest enclosing scope. If the
4907 -- node is a body and N is its return type, the declaration belongs in
4908 -- the enclosing scope.
7324bf49 4909
9dfd2ff8 4910 P := Parent (N);
92298782 4911
cd1c668b
ES
4912 if Nkind (P) = N_Subprogram_Body
4913 and then Nkind (N) = N_Function_Specification
4914 then
4915 P := Parent (P);
4916 end if;
4917
af4b9434 4918 while Present (P) and then not Has_Declarations (P) loop
7324bf49
AC
4919 P := Parent (P);
4920 end loop;
4921
af4b9434
AC
4922 pragma Assert (Present (P));
4923
4924 if Nkind (P) = N_Package_Specification then
4925 Prepend (Decl, Visible_Declarations (P));
4926 else
4927 Prepend (Decl, Declarations (P));
4928 end if;
7324bf49
AC
4929
4930 -- Replace the anonymous type with an occurrence of the new declaration.
9dfd2ff8 4931 -- In all cases the rewritten node does not have the null-exclusion
7324bf49
AC
4932 -- attribute because (if present) it was already inherited by the
4933 -- anonymous entity (Anon). Thus, in case of components we do not
4934 -- inherit this attribute.
4935
4936 if Nkind (N) = N_Parameter_Specification then
4937 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4938 Set_Etype (Defining_Identifier (N), Anon);
4939 Set_Null_Exclusion_Present (N, False);
fea9e956
ES
4940
4941 elsif Nkind (N) = N_Object_Declaration then
4942 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4943 Set_Etype (Defining_Identifier (N), Anon);
4944
2b73cf68
JM
4945 elsif Nkind (N) = N_Access_Function_Definition then
4946 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4947
b1c11e0e
JM
4948 elsif Nkind (N) = N_Function_Specification then
4949 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4950 Set_Etype (Defining_Unit_Name (N), Anon);
4951
7324bf49
AC
4952 else
4953 Rewrite (Comp,
4954 Make_Component_Definition (Loc,
4955 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4956 end if;
4957
4958 Mark_Rewrite_Insertion (Comp);
4959
7d7af38a 4960 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
fea9e956 4961 Analyze (Decl);
2b73cf68 4962
fea9e956 4963 else
cd1c668b
ES
4964 -- Temporarily remove the current scope (record or subprogram) from
4965 -- the stack to add the new declarations to the enclosing scope.
4966
2b73cf68 4967 Scope_Stack.Decrement_Last;
fea9e956 4968 Analyze (Decl);
2b73cf68
JM
4969 Set_Is_Itype (Anon);
4970 Scope_Stack.Append (Curr_Scope);
fea9e956 4971 end if;
7324bf49 4972
fea9e956 4973 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
7d7af38a 4974 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
7324bf49
AC
4975 return Anon;
4976 end Replace_Anonymous_Access_To_Protected_Subprogram;
4977
996ae0b0
RK
4978 -------------------------------
4979 -- Build_Derived_Access_Type --
4980 -------------------------------
4981
4982 procedure Build_Derived_Access_Type
4983 (N : Node_Id;
4984 Parent_Type : Entity_Id;
4985 Derived_Type : Entity_Id)
4986 is
4987 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4988
4989 Desig_Type : Entity_Id;
4990 Discr : Entity_Id;
4991 Discr_Con_Elist : Elist_Id;
4992 Discr_Con_El : Elmt_Id;
6e937c1c 4993 Subt : Entity_Id;
996ae0b0
RK
4994
4995 begin
c6fe3827
GD
4996 -- Set the designated type so it is available in case this is an access
4997 -- to a self-referential type, e.g. a standard list type with a next
4998 -- pointer. Will be reset after subtype is built.
996ae0b0 4999
a397db96
AC
5000 Set_Directly_Designated_Type
5001 (Derived_Type, Designated_Type (Parent_Type));
996ae0b0
RK
5002
5003 Subt := Process_Subtype (S, N);
5004
5005 if Nkind (S) /= N_Subtype_Indication
5006 and then Subt /= Base_Type (Subt)
5007 then
5008 Set_Ekind (Derived_Type, E_Access_Subtype);
5009 end if;
5010
5011 if Ekind (Derived_Type) = E_Access_Subtype then
5012 declare
5013 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5014 Ibase : constant Entity_Id :=
5015 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5016 Svg_Chars : constant Name_Id := Chars (Ibase);
5017 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5018
5019 begin
5020 Copy_Node (Pbase, Ibase);
5021
07fc65c4
GB
5022 Set_Chars (Ibase, Svg_Chars);
5023 Set_Next_Entity (Ibase, Svg_Next_E);
5024 Set_Sloc (Ibase, Sloc (Derived_Type));
5025 Set_Scope (Ibase, Scope (Derived_Type));
5026 Set_Freeze_Node (Ibase, Empty);
5027 Set_Is_Frozen (Ibase, False);
5028 Set_Comes_From_Source (Ibase, False);
5029 Set_Is_First_Subtype (Ibase, False);
996ae0b0
RK
5030
5031 Set_Etype (Ibase, Pbase);
5032 Set_Etype (Derived_Type, Ibase);
5033 end;
5034 end if;
5035
5036 Set_Directly_Designated_Type
5037 (Derived_Type, Designated_Type (Subt));
5038
5039 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5040 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5041 Set_Size_Info (Derived_Type, Parent_Type);
5042 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5043 Set_Depends_On_Private (Derived_Type,
5044 Has_Private_Component (Derived_Type));
5045 Conditional_Delay (Derived_Type, Subt);
5046
7bd98753 5047 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
fa961f76 5048 -- that it is not redundant.
2820d220 5049
fa961f76
ES
5050 if Null_Exclusion_Present (Type_Definition (N)) then
5051 Set_Can_Never_Be_Null (Derived_Type);
5052
5053 if Can_Never_Be_Null (Parent_Type)
5054 and then False
5055 then
5056 Error_Msg_NE
5057 ("`NOT NULL` not allowed (& already excludes null)",
5058 N, Parent_Type);
5059 end if;
5060
5061 elsif Can_Never_Be_Null (Parent_Type) then
2820d220
AC
5062 Set_Can_Never_Be_Null (Derived_Type);
5063 end if;
5064
c6fe3827
GD
5065 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5066 -- the root type for this information.
996ae0b0
RK
5067
5068 -- Apply range checks to discriminants for derived record case
5069 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
5070
5071 Desig_Type := Designated_Type (Derived_Type);
5072 if Is_Composite_Type (Desig_Type)
5073 and then (not Is_Array_Type (Desig_Type))
5074 and then Has_Discriminants (Desig_Type)
5075 and then Base_Type (Desig_Type) /= Desig_Type
5076 then
5077 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
5078 Discr_Con_El := First_Elmt (Discr_Con_Elist);
5079
5080 Discr := First_Discriminant (Base_Type (Desig_Type));
5081 while Present (Discr_Con_El) loop
5082 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
5083 Next_Elmt (Discr_Con_El);
5084 Next_Discriminant (Discr);
5085 end loop;
5086 end if;
5087 end Build_Derived_Access_Type;
5088
5089 ------------------------------
5090 -- Build_Derived_Array_Type --
5091 ------------------------------
5092
5093 procedure Build_Derived_Array_Type
5094 (N : Node_Id;
5095 Parent_Type : Entity_Id;
5096 Derived_Type : Entity_Id)
5097 is
5098 Loc : constant Source_Ptr := Sloc (N);
5099 Tdef : constant Node_Id := Type_Definition (N);
5100 Indic : constant Node_Id := Subtype_Indication (Tdef);
5101 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5102 Implicit_Base : Entity_Id;
5103 New_Indic : Node_Id;
5104
5105 procedure Make_Implicit_Base;
c6fe3827
GD
5106 -- If the parent subtype is constrained, the derived type is a subtype
5107 -- of an implicit base type derived from the parent base.
996ae0b0
RK
5108
5109 ------------------------
5110 -- Make_Implicit_Base --
5111 ------------------------
5112
5113 procedure Make_Implicit_Base is
5114 begin
5115 Implicit_Base :=
5116 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5117
5118 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5119 Set_Etype (Implicit_Base, Parent_Base);
5120
5121 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
5122 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
5123
5124 Set_Has_Delayed_Freeze (Implicit_Base, True);
5125 end Make_Implicit_Base;
5126
5127 -- Start of processing for Build_Derived_Array_Type
5128
5129 begin
5130 if not Is_Constrained (Parent_Type) then
5131 if Nkind (Indic) /= N_Subtype_Indication then
5132 Set_Ekind (Derived_Type, E_Array_Type);
5133
5134 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5135 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
5136
5137 Set_Has_Delayed_Freeze (Derived_Type, True);
5138
5139 else
5140 Make_Implicit_Base;
5141 Set_Etype (Derived_Type, Implicit_Base);
5142
5143 New_Indic :=
5144 Make_Subtype_Declaration (Loc,
5145 Defining_Identifier => Derived_Type,
5146 Subtype_Indication =>
5147 Make_Subtype_Indication (Loc,
5148 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
5149 Constraint => Constraint (Indic)));
5150
5151 Rewrite (N, New_Indic);
5152 Analyze (N);
5153 end if;
5154
5155 else
5156 if Nkind (Indic) /= N_Subtype_Indication then
5157 Make_Implicit_Base;
5158
5159 Set_Ekind (Derived_Type, Ekind (Parent_Type));
5160 Set_Etype (Derived_Type, Implicit_Base);
5161 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5162
5163 else
5164 Error_Msg_N ("illegal constraint on constrained type", Indic);
5165 end if;
5166 end if;
5167
9dfd2ff8
CC
5168 -- If parent type is not a derived type itself, and is declared in
5169 -- closed scope (e.g. a subprogram), then we must explicitly introduce
5170 -- the new type's concatenation operator since Derive_Subprograms
5171 -- will not inherit the parent's operator. If the parent type is
5172 -- unconstrained, the operator is of the unconstrained base type.
996ae0b0
RK
5173
5174 if Number_Dimensions (Parent_Type) = 1
5175 and then not Is_Limited_Type (Parent_Type)
5176 and then not Is_Derived_Type (Parent_Type)
950d3e7d
ES
5177 and then not Is_Package_Or_Generic_Package
5178 (Scope (Base_Type (Parent_Type)))
996ae0b0 5179 then
81a5b587
AC
5180 if not Is_Constrained (Parent_Type)
5181 and then Is_Constrained (Derived_Type)
5182 then
5183 New_Concatenation_Op (Implicit_Base);
5184 else
5185 New_Concatenation_Op (Derived_Type);
5186 end if;
996ae0b0
RK
5187 end if;
5188 end Build_Derived_Array_Type;
5189
5190 -----------------------------------
5191 -- Build_Derived_Concurrent_Type --
5192 -----------------------------------
5193
5194 procedure Build_Derived_Concurrent_Type
5195 (N : Node_Id;
5196 Parent_Type : Entity_Id;
5197 Derived_Type : Entity_Id)
5198 is
8d12c865
RD
5199 Loc : constant Source_Ptr := Sloc (N);
5200
092ef350 5201 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
f7e71125
AC
5202 Corr_Decl : Node_Id;
5203 Corr_Decl_Needed : Boolean;
8d12c865
RD
5204 -- If the derived type has fewer discriminants than its parent, the
5205 -- corresponding record is also a derived type, in order to account for
5206 -- the bound discriminants. We create a full type declaration for it in
5207 -- this case.
f7e71125 5208
8d12c865
RD
5209 Constraint_Present : constant Boolean :=
5210 Nkind (Subtype_Indication (Type_Definition (N))) =
5211 N_Subtype_Indication;
f7e71125
AC
5212
5213 D_Constraint : Node_Id;
5214 New_Constraint : Elist_Id;
5215 Old_Disc : Entity_Id;
5216 New_Disc : Entity_Id;
5217 New_N : Node_Id;
996ae0b0
RK
5218
5219 begin
fbf5a39b 5220 Set_Stored_Constraint (Derived_Type, No_Elist);
f7e71125
AC
5221 Corr_Decl_Needed := False;
5222 Old_Disc := Empty;
5223
5224 if Present (Discriminant_Specifications (N))
5225 and then Constraint_Present
5226 then
5227 Old_Disc := First_Discriminant (Parent_Type);
5228 New_Disc := First (Discriminant_Specifications (N));
5229 while Present (New_Disc) and then Present (Old_Disc) loop
5230 Next_Discriminant (Old_Disc);
5231 Next (New_Disc);
5232 end loop;
5233 end if;
5234
f915704f 5235 if Present (Old_Disc) and then Expander_Active then
f7e71125
AC
5236
5237 -- The new type has fewer discriminants, so we need to create a new
5238 -- corresponding record, which is derived from the corresponding
8d12c865 5239 -- record of the parent, and has a stored constraint that captures
9fc2854d
AC
5240 -- the values of the discriminant constraints. The corresponding
5241 -- record is needed only if expander is active and code generation is
5242 -- enabled.
8d12c865 5243
f915704f
AC
5244 -- The type declaration for the derived corresponding record has the
5245 -- same discriminant part and constraints as the current declaration.
5246 -- Copy the unanalyzed tree to build declaration.
f7e71125
AC
5247
5248 Corr_Decl_Needed := True;
5249 New_N := Copy_Separate_Tree (N);
5250
5251 Corr_Decl :=
5252 Make_Full_Type_Declaration (Loc,
f915704f 5253 Defining_Identifier => Corr_Record,
f7e71125
AC
5254 Discriminant_Specifications =>
5255 Discriminant_Specifications (New_N),
f915704f 5256 Type_Definition =>
f7e71125
AC
5257 Make_Derived_Type_Definition (Loc,
5258 Subtype_Indication =>
5259 Make_Subtype_Indication (Loc,
5260 Subtype_Mark =>
5261 New_Occurrence_Of
5262 (Corresponding_Record_Type (Parent_Type), Loc),
f915704f 5263 Constraint =>
f7e71125
AC
5264 Constraint
5265 (Subtype_Indication (Type_Definition (New_N))))));
5266 end if;
996ae0b0 5267
ce4a6e84
RD
5268 -- Copy Storage_Size and Relative_Deadline variables if task case
5269
996ae0b0
RK
5270 if Is_Task_Type (Parent_Type) then
5271 Set_Storage_Size_Variable (Derived_Type,
5272 Storage_Size_Variable (Parent_Type));
ce4a6e84
RD
5273 Set_Relative_Deadline_Variable (Derived_Type,
5274 Relative_Deadline_Variable (Parent_Type));
996ae0b0
RK
5275 end if;
5276
5277 if Present (Discriminant_Specifications (N)) then
2b73cf68 5278 Push_Scope (Derived_Type);
996ae0b0 5279 Check_Or_Process_Discriminants (N, Derived_Type);
f7e71125
AC
5280
5281 if Constraint_Present then
5282 New_Constraint :=
5283 Expand_To_Stored_Constraint
5284 (Parent_Type,
5285 Build_Discriminant_Constraints
5286 (Parent_Type,
5287 Subtype_Indication (Type_Definition (N)), True));
5288 end if;
5289
996ae0b0 5290 End_Scope;
7ae0dcd8
ES
5291
5292 elsif Constraint_Present then
5293
5294 -- Build constrained subtype and derive from it
5295
5296 declare
5297 Loc : constant Source_Ptr := Sloc (N);
fbf5a39b 5298 Anon : constant Entity_Id :=
7ae0dcd8 5299 Make_Defining_Identifier (Loc,
7675ad4f 5300 Chars => New_External_Name (Chars (Derived_Type), 'T'));
7ae0dcd8
ES
5301 Decl : Node_Id;
5302
5303 begin
5304 Decl :=
5305 Make_Subtype_Declaration (Loc,
5306 Defining_Identifier => Anon,
5307 Subtype_Indication =>
88b32fc3 5308 Subtype_Indication (Type_Definition (N)));
7ae0dcd8 5309 Insert_Before (N, Decl);
88b32fc3
BD
5310 Analyze (Decl);
5311
7ae0dcd8
ES
5312 Rewrite (Subtype_Indication (Type_Definition (N)),
5313 New_Occurrence_Of (Anon, Loc));
7ae0dcd8
ES
5314 Set_Analyzed (Derived_Type, False);
5315 Analyze (N);
5316 return;
5317 end;
996ae0b0
RK
5318 end if;
5319
f7e71125
AC
5320 -- By default, operations and private data are inherited from parent.
5321 -- However, in the presence of bound discriminants, a new corresponding
5322 -- record will be created, see below.
996ae0b0
RK
5323
5324 Set_Has_Discriminants
7ae0dcd8 5325 (Derived_Type, Has_Discriminants (Parent_Type));
996ae0b0 5326 Set_Corresponding_Record_Type
7ae0dcd8 5327 (Derived_Type, Corresponding_Record_Type (Parent_Type));
996ae0b0 5328
0501956d
GD
5329 -- Is_Constrained is set according the parent subtype, but is set to
5330 -- False if the derived type is declared with new discriminants.
5331
5332 Set_Is_Constrained
5333 (Derived_Type,
5334 (Is_Constrained (Parent_Type) or else Constraint_Present)
5335 and then not Present (Discriminant_Specifications (N)));
5336
996ae0b0 5337 if Constraint_Present then
996ae0b0
RK
5338 if not Has_Discriminants (Parent_Type) then
5339 Error_Msg_N ("untagged parent must have discriminants", N);
5340
5341 elsif Present (Discriminant_Specifications (N)) then
5342
9dfd2ff8 5343 -- Verify that new discriminants are used to constrain old ones
996ae0b0 5344
996ae0b0 5345 D_Constraint :=
7ae0dcd8
ES
5346 First
5347 (Constraints
5348 (Constraint (Subtype_Indication (Type_Definition (N)))));
996ae0b0 5349
f7e71125 5350 Old_Disc := First_Discriminant (Parent_Type);
7ae0dcd8 5351
f7e71125
AC
5352 while Present (D_Constraint) loop
5353 if Nkind (D_Constraint) /= N_Discriminant_Association then
5354
8d12c865
RD
5355 -- Positional constraint. If it is a reference to a new
5356 -- discriminant, it constrains the corresponding old one.
f7e71125
AC
5357
5358 if Nkind (D_Constraint) = N_Identifier then
5359 New_Disc := First_Discriminant (Derived_Type);
5360 while Present (New_Disc) loop
8d12c865 5361 exit when Chars (New_Disc) = Chars (D_Constraint);
f7e71125
AC
5362 Next_Discriminant (New_Disc);
5363 end loop;
5364
5365 if Present (New_Disc) then
5366 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
5367 end if;
5368 end if;
5369
5370 Next_Discriminant (Old_Disc);
5371
8d12c865
RD
5372 -- if this is a named constraint, search by name for the old
5373 -- discriminants constrained by the new one.
f7e71125
AC
5374
5375 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
5376
8d12c865 5377 -- Find new discriminant with that name
f7e71125
AC
5378
5379 New_Disc := First_Discriminant (Derived_Type);
5380 while Present (New_Disc) loop
5381 exit when
5382 Chars (New_Disc) = Chars (Expression (D_Constraint));
5383 Next_Discriminant (New_Disc);
5384 end loop;
5385
5386 if Present (New_Disc) then
5387
8d12c865
RD
5388 -- Verify that new discriminant renames some discriminant
5389 -- of the parent type, and associate the new discriminant
5390 -- with one or more old ones that it renames.
f7e71125
AC
5391
5392 declare
5393 Selector : Node_Id;
5394
5395 begin
5396 Selector := First (Selector_Names (D_Constraint));
f7e71125
AC
5397 while Present (Selector) loop
5398 Old_Disc := First_Discriminant (Parent_Type);
f7e71125
AC
5399 while Present (Old_Disc) loop
5400 exit when Chars (Old_Disc) = Chars (Selector);
5401 Next_Discriminant (Old_Disc);
5402 end loop;
5403
5404 if Present (Old_Disc) then
5405 Set_Corresponding_Discriminant
5406 (New_Disc, Old_Disc);
f7e71125
AC
5407 end if;
5408
5409 Next (Selector);
5410 end loop;
5411 end;
996ae0b0
RK
5412 end if;
5413 end if;
5414
f7e71125
AC
5415 Next (D_Constraint);
5416 end loop;
5417
8d12c865 5418 New_Disc := First_Discriminant (Derived_Type);
f7e71125
AC
5419 while Present (New_Disc) loop
5420 if No (Corresponding_Discriminant (New_Disc)) then
5421 Error_Msg_NE
8d12c865
RD
5422 ("new discriminant& must constrain old one", N, New_Disc);
5423
f7e71125 5424 elsif not
8d12c865
RD
5425 Subtypes_Statically_Compatible
5426 (Etype (New_Disc),
5427 Etype (Corresponding_Discriminant (New_Disc)))
996ae0b0 5428 then
f7e71125
AC
5429 Error_Msg_NE
5430 ("& not statically compatible with parent discriminant",
5431 N, New_Disc);
996ae0b0
RK
5432 end if;
5433
996ae0b0 5434 Next_Discriminant (New_Disc);
996ae0b0 5435 end loop;
996ae0b0
RK
5436 end if;
5437
5438 elsif Present (Discriminant_Specifications (N)) then
5439 Error_Msg_N
8d12c865 5440 ("missing discriminant constraint in untagged derivation", N);
996ae0b0
RK
5441 end if;
5442
8d12c865
RD
5443 -- The entity chain of the derived type includes the new discriminants
5444 -- but shares operations with the parent.
f7e71125 5445
996ae0b0 5446 if Present (Discriminant_Specifications (N)) then
996ae0b0 5447 Old_Disc := First_Discriminant (Parent_Type);
996ae0b0 5448 while Present (Old_Disc) loop
996ae0b0
RK
5449 if No (Next_Entity (Old_Disc))
5450 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
5451 then
8d12c865
RD
5452 Set_Next_Entity
5453 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
996ae0b0
RK
5454 exit;
5455 end if;
5456
5457 Next_Discriminant (Old_Disc);
5458 end loop;
5459
5460 else
5461 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7ae0dcd8 5462 if Has_Discriminants (Parent_Type) then
7324bf49 5463 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7ae0dcd8
ES
5464 Set_Discriminant_Constraint (
5465 Derived_Type, Discriminant_Constraint (Parent_Type));
5466 end if;
996ae0b0
RK
5467 end if;
5468
5469 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
5470
5471 Set_Has_Completion (Derived_Type);
f7e71125
AC
5472
5473 if Corr_Decl_Needed then
5474 Set_Stored_Constraint (Derived_Type, New_Constraint);
5475 Insert_After (N, Corr_Decl);
5476 Analyze (Corr_Decl);
5477 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
5478 end if;
996ae0b0
RK
5479 end Build_Derived_Concurrent_Type;
5480
5481 ------------------------------------
5482 -- Build_Derived_Enumeration_Type --
5483 ------------------------------------
5484
5485 procedure Build_Derived_Enumeration_Type
5486 (N : Node_Id;
5487 Parent_Type : Entity_Id;
5488 Derived_Type : Entity_Id)
5489 is
5490 Loc : constant Source_Ptr := Sloc (N);
5491 Def : constant Node_Id := Type_Definition (N);
5492 Indic : constant Node_Id := Subtype_Indication (Def);
5493 Implicit_Base : Entity_Id;
5494 Literal : Entity_Id;
5495 New_Lit : Entity_Id;
5496 Literals_List : List_Id;
5497 Type_Decl : Node_Id;
5498 Hi, Lo : Node_Id;
5499 Rang_Expr : Node_Id;
5500
5501 begin
94fd3dc6 5502 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
996ae0b0
RK
5503 -- not have explicit literals lists we need to process types derived
5504 -- from them specially. This is handled by Derived_Standard_Character.
5505 -- If the parent type is a generic type, there are no literals either,
5506 -- and we construct the same skeletal representation as for the generic
5507 -- parent type.
5508
ce4a6e84 5509 if Is_Standard_Character_Type (Parent_Type) then
996ae0b0
RK
5510 Derived_Standard_Character (N, Parent_Type, Derived_Type);
5511
5512 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
5513 declare
5514 Lo : Node_Id;
5515 Hi : Node_Id;
5516
5517 begin
054275e4
ES
5518 if Nkind (Indic) /= N_Subtype_Indication then
5519 Lo :=
5520 Make_Attribute_Reference (Loc,
5521 Attribute_Name => Name_First,
03b64787 5522 Prefix => New_Reference_To (Derived_Type, Loc));
054275e4
ES
5523 Set_Etype (Lo, Derived_Type);
5524
5525 Hi :=
5526 Make_Attribute_Reference (Loc,
5527 Attribute_Name => Name_Last,
03b64787 5528 Prefix => New_Reference_To (Derived_Type, Loc));
054275e4
ES
5529 Set_Etype (Hi, Derived_Type);
5530
5531 Set_Scalar_Range (Derived_Type,
5532 Make_Range (Loc,
03b64787 5533 Low_Bound => Lo,
054275e4
ES
5534 High_Bound => Hi));
5535 else
5536
5537 -- Analyze subtype indication and verify compatibility
5538 -- with parent type.
5539
03b64787
AC
5540 if Base_Type (Process_Subtype (Indic, N)) /=
5541 Base_Type (Parent_Type)
054275e4
ES
5542 then
5543 Error_Msg_N
5544 ("illegal constraint for formal discrete type", N);
5545 end if;
5546 end if;
996ae0b0
RK
5547 end;
5548
5549 else
5550 -- If a constraint is present, analyze the bounds to catch
5551 -- premature usage of the derived literals.
5552
5553 if Nkind (Indic) = N_Subtype_Indication
5554 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5555 then
5556 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
5557 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5558 end if;
5559
c6fe3827
GD
5560 -- Introduce an implicit base type for the derived type even if there
5561 -- is no constraint attached to it, since this seems closer to the
5562 -- Ada semantics. Build a full type declaration tree for the derived
5563 -- type using the implicit base type as the defining identifier. The
5564 -- build a subtype declaration tree which applies the constraint (if
5565 -- any) have it replace the derived type declaration.
996ae0b0
RK
5566
5567 Literal := First_Literal (Parent_Type);
5568 Literals_List := New_List;
996ae0b0
RK
5569 while Present (Literal)
5570 and then Ekind (Literal) = E_Enumeration_Literal
5571 loop
5572 -- Literals of the derived type have the same representation as
5573 -- those of the parent type, but this representation can be
5574 -- overridden by an explicit representation clause. Indicate
5575 -- that there is no explicit representation given yet. These
5576 -- derived literals are implicit operations of the new type,
9dfd2ff8 5577 -- and can be overridden by explicit ones.
996ae0b0
RK
5578
5579 if Nkind (Literal) = N_Defining_Character_Literal then
5580 New_Lit :=
5581 Make_Defining_Character_Literal (Loc, Chars (Literal));
5582 else
5583 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5584 end if;
5585
5586 Set_Ekind (New_Lit, E_Enumeration_Literal);
5587 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
5588 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
5589 Set_Enumeration_Rep_Expr (New_Lit, Empty);
5590 Set_Alias (New_Lit, Literal);
5591 Set_Is_Known_Valid (New_Lit, True);
5592
5593 Append (New_Lit, Literals_List);
5594 Next_Literal (Literal);
5595 end loop;
5596
5597 Implicit_Base :=
5598 Make_Defining_Identifier (Sloc (Derived_Type),
7675ad4f 5599 Chars => New_External_Name (Chars (Derived_Type), 'B'));
996ae0b0 5600
c6fe3827
GD
5601 -- Indicate the proper nature of the derived type. This must be done
5602 -- before analysis of the literals, to recognize cases when a literal
5603 -- may be hidden by a previous explicit function definition (cf.
5604 -- c83031a).
996ae0b0
RK
5605
5606 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5607 Set_Etype (Derived_Type, Implicit_Base);
5608
5609 Type_Decl :=
5610 Make_Full_Type_Declaration (Loc,
5611 Defining_Identifier => Implicit_Base,
5612 Discriminant_Specifications => No_List,
5613 Type_Definition =>
5614 Make_Enumeration_Type_Definition (Loc, Literals_List));
5615
5616 Mark_Rewrite_Insertion (Type_Decl);
5617 Insert_Before (N, Type_Decl);
5618 Analyze (Type_Decl);
5619
a5b62485
AC
5620 -- After the implicit base is analyzed its Etype needs to be changed
5621 -- to reflect the fact that it is derived from the parent type which
5622 -- was ignored during analysis. We also set the size at this point.
996ae0b0
RK
5623
5624 Set_Etype (Implicit_Base, Parent_Type);
5625
5626 Set_Size_Info (Implicit_Base, Parent_Type);
5627 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5628 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5629
bd29d519
AC
5630 -- Copy other flags from parent type
5631
996ae0b0
RK
5632 Set_Has_Non_Standard_Rep
5633 (Implicit_Base, Has_Non_Standard_Rep
5634 (Parent_Type));
bd29d519
AC
5635 Set_Has_Pragma_Ordered
5636 (Implicit_Base, Has_Pragma_Ordered
5637 (Parent_Type));
996ae0b0
RK
5638 Set_Has_Delayed_Freeze (Implicit_Base);
5639
c6fe3827
GD
5640 -- Process the subtype indication including a validation check on the
5641 -- constraint, if any. If a constraint is given, its bounds must be
5642 -- implicitly converted to the new type.
996ae0b0
RK
5643
5644 if Nkind (Indic) = N_Subtype_Indication then
996ae0b0 5645 declare
71d9e9f2
ES
5646 R : constant Node_Id :=
5647 Range_Expression (Constraint (Indic));
996ae0b0
RK
5648
5649 begin
5650 if Nkind (R) = N_Range then
5651 Hi := Build_Scalar_Bound
07fc65c4 5652 (High_Bound (R), Parent_Type, Implicit_Base);
996ae0b0 5653 Lo := Build_Scalar_Bound
07fc65c4 5654 (Low_Bound (R), Parent_Type, Implicit_Base);
996ae0b0
RK
5655
5656 else
c6fe3827
GD
5657 -- Constraint is a Range attribute. Replace with explicit
5658 -- mention of the bounds of the prefix, which must be a
5659 -- subtype.
996ae0b0
RK
5660
5661 Analyze (Prefix (R));
5662 Hi :=
5663 Convert_To (Implicit_Base,
5664 Make_Attribute_Reference (Loc,
5665 Attribute_Name => Name_Last,
5666 Prefix =>
5667 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5668
5669 Lo :=
5670 Convert_To (Implicit_Base,
5671 Make_Attribute_Reference (Loc,
5672 Attribute_Name => Name_First,
5673 Prefix =>
5674 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5675 end if;
996ae0b0
RK
5676 end;
5677
5678 else
5679 Hi :=
5680 Build_Scalar_Bound
5681 (Type_High_Bound (Parent_Type),
07fc65c4 5682 Parent_Type, Implicit_Base);
996ae0b0
RK
5683 Lo :=
5684 Build_Scalar_Bound
5685 (Type_Low_Bound (Parent_Type),
07fc65c4 5686 Parent_Type, Implicit_Base);
996ae0b0
RK
5687 end if;
5688
5689 Rang_Expr :=
5690 Make_Range (Loc,
5691 Low_Bound => Lo,
5692 High_Bound => Hi);
5693
5694 -- If we constructed a default range for the case where no range
5695 -- was given, then the expressions in the range must not freeze
5696 -- since they do not correspond to expressions in the source.
5697
5698 if Nkind (Indic) /= N_Subtype_Indication then
5699 Set_Must_Not_Freeze (Lo);
5700 Set_Must_Not_Freeze (Hi);
5701 Set_Must_Not_Freeze (Rang_Expr);
5702 end if;
5703
5704 Rewrite (N,
5705 Make_Subtype_Declaration (Loc,
5706 Defining_Identifier => Derived_Type,
5707 Subtype_Indication =>
5708 Make_Subtype_Indication (Loc,
5709 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5710 Constraint =>
5711 Make_Range_Constraint (Loc,
5712 Range_Expression => Rang_Expr))));
5713
5714 Analyze (N);
5715
c6fe3827
GD
5716 -- If pragma Discard_Names applies on the first subtype of the parent
5717 -- type, then it must be applied on this subtype as well.
996ae0b0
RK
5718
5719 if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5720 Set_Discard_Names (Derived_Type);
5721 end if;
5722
a5b62485
AC
5723 -- Apply a range check. Since this range expression doesn't have an
5724 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5725 -- this right???
996ae0b0
RK
5726
5727 if Nkind (Indic) = N_Subtype_Indication then
5728 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5729 Parent_Type,
5730 Source_Typ => Entity (Subtype_Mark (Indic)));
5731 end if;
5732 end if;
996ae0b0
RK
5733 end Build_Derived_Enumeration_Type;
5734
5735 --------------------------------
5736 -- Build_Derived_Numeric_Type --
5737 --------------------------------
5738
5739 procedure Build_Derived_Numeric_Type
5740 (N : Node_Id;
5741 Parent_Type : Entity_Id;
5742 Derived_Type : Entity_Id)
5743 is
5744 Loc : constant Source_Ptr := Sloc (N);
5745 Tdef : constant Node_Id := Type_Definition (N);
5746 Indic : constant Node_Id := Subtype_Indication (Tdef);
5747 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5748 No_Constraint : constant Boolean := Nkind (Indic) /=
5749 N_Subtype_Indication;
71d9e9f2 5750 Implicit_Base : Entity_Id;
996ae0b0
RK
5751
5752 Lo : Node_Id;
5753 Hi : Node_Id;
996ae0b0
RK
5754
5755 begin
5756 -- Process the subtype indication including a validation check on
5757 -- the constraint if any.
5758
fbf5a39b 5759 Discard_Node (Process_Subtype (Indic, N));
996ae0b0 5760
a5b62485
AC
5761 -- Introduce an implicit base type for the derived type even if there
5762 -- is no constraint attached to it, since this seems closer to the Ada
5763 -- semantics.
996ae0b0
RK
5764
5765 Implicit_Base :=
5766 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5767
5768 Set_Etype (Implicit_Base, Parent_Base);
5769 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5770 Set_Size_Info (Implicit_Base, Parent_Base);
996ae0b0
RK
5771 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5772 Set_Parent (Implicit_Base, Parent (Derived_Type));
8dc2ddaf 5773 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
996ae0b0 5774
7d7af38a
JM
5775 -- Set RM Size for discrete type or decimal fixed-point type
5776 -- Ordinary fixed-point is excluded, why???
5777
5778 if Is_Discrete_Type (Parent_Base)
5779 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7bde4677 5780 then
996ae0b0
RK
5781 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5782 end if;
5783
5784 Set_Has_Delayed_Freeze (Implicit_Base);
5785
5786 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5787 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5788
5789 Set_Scalar_Range (Implicit_Base,
5790 Make_Range (Loc,
5791 Low_Bound => Lo,
5792 High_Bound => Hi));
5793
5794 if Has_Infinities (Parent_Base) then
5795 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5796 end if;
5797
a5b62485
AC
5798 -- The Derived_Type, which is the entity of the declaration, is a
5799 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5800 -- absence of an explicit constraint.
996ae0b0
RK
5801
5802 Set_Etype (Derived_Type, Implicit_Base);
5803
5804 -- If we did not have a constraint, then the Ekind is set from the
5805 -- parent type (otherwise Process_Subtype has set the bounds)
5806
5807 if No_Constraint then
5808 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5809 end if;
5810
a5b62485 5811 -- If we did not have a range constraint, then set the range from the
498d1b80 5812 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
996ae0b0
RK
5813
5814 if No_Constraint
5815 or else not Has_Range_Constraint (Indic)
5816 then
5817 Set_Scalar_Range (Derived_Type,
5818 Make_Range (Loc,
5819 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
5820 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5821 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5822
5823 if Has_Infinities (Parent_Type) then
5824 Set_Includes_Infinities (Scalar_Range (Derived_Type));
5825 end if;
8dc2ddaf
RD
5826
5827 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
996ae0b0
RK
5828 end if;
5829
9c510803
ES
5830 Set_Is_Descendent_Of_Address (Derived_Type,
5831 Is_Descendent_Of_Address (Parent_Type));
5832 Set_Is_Descendent_Of_Address (Implicit_Base,
5833 Is_Descendent_Of_Address (Parent_Type));
5834
996ae0b0
RK
5835 -- Set remaining type-specific fields, depending on numeric type
5836
5837 if Is_Modular_Integer_Type (Parent_Type) then
5838 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5839
5840 Set_Non_Binary_Modulus
5841 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5842
8dc2ddaf
RD
5843 Set_Is_Known_Valid
5844 (Implicit_Base, Is_Known_Valid (Parent_Base));
5845
996ae0b0
RK
5846 elsif Is_Floating_Point_Type (Parent_Type) then
5847
5848 -- Digits of base type is always copied from the digits value of
5849 -- the parent base type, but the digits of the derived type will
5850 -- already have been set if there was a constraint present.
5851
5852 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
23c799b1 5853 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
996ae0b0
RK
5854
5855 if No_Constraint then
5856 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5857 end if;
5858
5859 elsif Is_Fixed_Point_Type (Parent_Type) then
5860
a5b62485
AC
5861 -- Small of base type and derived type are always copied from the
5862 -- parent base type, since smalls never change. The delta of the
5863 -- base type is also copied from the parent base type. However the
5864 -- delta of the derived type will have been set already if a
5865 -- constraint was present.
996ae0b0
RK
5866
5867 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
5868 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5869 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5870
5871 if No_Constraint then
5872 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
5873 end if;
5874
5875 -- The scale and machine radix in the decimal case are always
5876 -- copied from the parent base type.
5877
5878 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5879 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
5880 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5881
5882 Set_Machine_Radix_10
5883 (Derived_Type, Machine_Radix_10 (Parent_Base));
5884 Set_Machine_Radix_10
5885 (Implicit_Base, Machine_Radix_10 (Parent_Base));
5886
5887 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5888
5889 if No_Constraint then
5890 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5891
5892 else
5893 -- the analysis of the subtype_indication sets the
5894 -- digits value of the derived type.
5895
5896 null;
5897 end if;
5898 end if;
5899 end if;
5900
5901 -- The type of the bounds is that of the parent type, and they
5902 -- must be converted to the derived type.
5903
5904 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5905
5906 -- The implicit_base should be frozen when the derived type is frozen,
a5b62485
AC
5907 -- but note that it is used in the conversions of the bounds. For fixed
5908 -- types we delay the determination of the bounds until the proper
996ae0b0
RK
5909 -- freezing point. For other numeric types this is rejected by GCC, for
5910 -- reasons that are currently unclear (???), so we choose to freeze the
5911 -- implicit base now. In the case of integers and floating point types
5912 -- this is harmless because subsequent representation clauses cannot
5913 -- affect anything, but it is still baffling that we cannot use the
5914 -- same mechanism for all derived numeric types.
5915
88b32fc3
BD
5916 -- There is a further complication: actually *some* representation
5917 -- clauses can affect the implicit base type. Namely, attribute
5918 -- definition clauses for stream-oriented attributes need to set the
5919 -- corresponding TSS entries on the base type, and this normally cannot
5920 -- be done after the base type is frozen, so the circuitry in
5921 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5922 -- not use Set_TSS in this case.
5923
996ae0b0
RK
5924 if Is_Fixed_Point_Type (Parent_Type) then
5925 Conditional_Delay (Implicit_Base, Parent_Type);
5926 else
5927 Freeze_Before (N, Implicit_Base);
5928 end if;
996ae0b0
RK
5929 end Build_Derived_Numeric_Type;
5930
5931 --------------------------------
5932 -- Build_Derived_Private_Type --
5933 --------------------------------
5934
5935 procedure Build_Derived_Private_Type
07fc65c4
GB
5936 (N : Node_Id;
5937 Parent_Type : Entity_Id;
5938 Derived_Type : Entity_Id;
996ae0b0
RK
5939 Is_Completion : Boolean;
5940 Derive_Subps : Boolean := True)
5941 is
39f346aa 5942 Loc : constant Source_Ptr := Sloc (N);
996ae0b0
RK
5943 Der_Base : Entity_Id;
5944 Discr : Entity_Id;
5945 Full_Decl : Node_Id := Empty;
5946 Full_Der : Entity_Id;
5947 Full_P : Entity_Id;
5948 Last_Discr : Entity_Id;
5949 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
5950 Swapped : Boolean := False;
5951
5952 procedure Copy_And_Build;
5953 -- Copy derived type declaration, replace parent with its full view,
5954 -- and analyze new declaration.
5955
07fc65c4
GB
5956 --------------------
5957 -- Copy_And_Build --
5958 --------------------
5959
996ae0b0 5960 procedure Copy_And_Build is
71d9e9f2 5961 Full_N : Node_Id;
996ae0b0
RK
5962
5963 begin
5964 if Ekind (Parent_Type) in Record_Kind
82c80734
RD
5965 or else
5966 (Ekind (Parent_Type) in Enumeration_Kind
ce4a6e84 5967 and then not Is_Standard_Character_Type (Parent_Type)
82c80734 5968 and then not Is_Generic_Type (Root_Type (Parent_Type)))
996ae0b0
RK
5969 then
5970 Full_N := New_Copy_Tree (N);
5971 Insert_After (N, Full_N);
5972 Build_Derived_Type (
5973 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5974
5975 else
5976 Build_Derived_Type (
5977 N, Parent_Type, Full_Der, True, Derive_Subps => False);
5978 end if;
5979 end Copy_And_Build;
5980
5981 -- Start of processing for Build_Derived_Private_Type
5982
5983 begin
5984 if Is_Tagged_Type (Parent_Type) then
9013065b 5985 Full_P := Full_View (Parent_Type);
39f346aa
ES
5986
5987 -- A type extension of a type with unknown discriminants is an
5988 -- indefinite type that the back-end cannot handle directly.
5989 -- We treat it as a private type, and build a completion that is
5990 -- derived from the full view of the parent, and hopefully has
9013065b
AC
5991 -- known discriminants.
5992
c206e8fd
AC
5993 -- If the full view of the parent type has an underlying record view,
5994 -- use it to generate the underlying record view of this derived type
5995 -- (required for chains of derivations with unknown discriminants).
9013065b 5996
c206e8fd 5997 -- Minor optimization: we avoid the generation of useless underlying
9013065b 5998 -- record view entities if the private type declaration has unknown
c206e8fd
AC
5999 -- discriminants but its corresponding full view has no
6000 -- discriminants.
39f346aa
ES
6001
6002 if Has_Unknown_Discriminants (Parent_Type)
9013065b
AC
6003 and then Present (Full_P)
6004 and then (Has_Discriminants (Full_P)
6005 or else Present (Underlying_Record_View (Full_P)))
39f346aa 6006 and then not In_Open_Scopes (Par_Scope)
39f346aa
ES
6007 and then Expander_Active
6008 then
6009 declare
092ef350 6010 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
c5d91669
AC
6011 New_Ext : constant Node_Id :=
6012 Copy_Separate_Tree
6013 (Record_Extension_Part (Type_Definition (N)));
9013065b 6014 Decl : Node_Id;
39f346aa
ES
6015
6016 begin
6017 Build_Derived_Record_Type
6018 (N, Parent_Type, Derived_Type, Derive_Subps);
6019
6020 -- Build anonymous completion, as a derivation from the full
bf06d37f
AC
6021 -- view of the parent. This is not a completion in the usual
6022 -- sense, because the current type is not private.
39f346aa
ES
6023
6024 Decl :=
6025 Make_Full_Type_Declaration (Loc,
6026 Defining_Identifier => Full_Der,
6027 Type_Definition =>
6028 Make_Derived_Type_Definition (Loc,
6029 Subtype_Indication =>
6030 New_Copy_Tree
6031 (Subtype_Indication (Type_Definition (N))),
6032 Record_Extension_Part => New_Ext));
9013065b 6033
c206e8fd
AC
6034 -- If the parent type has an underlying record view, use it
6035 -- here to build the new underlying record view.
9013065b
AC
6036
6037 if Present (Underlying_Record_View (Full_P)) then
6038 pragma Assert
6039 (Nkind (Subtype_Indication (Type_Definition (Decl)))
6040 = N_Identifier);
6041 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
6042 Underlying_Record_View (Full_P));
6043 end if;
6044
39f346aa
ES
6045 Install_Private_Declarations (Par_Scope);
6046 Install_Visible_Declarations (Par_Scope);
bddd6058 6047 Insert_Before (N, Decl);
9013065b 6048
c206e8fd
AC
6049 -- Mark entity as an underlying record view before analysis,
6050 -- to avoid generating the list of its primitive operations
6051 -- (which is not really required for this entity) and thus
6052 -- prevent spurious errors associated with missing overriding
6053 -- of abstract primitives (overridden only for Derived_Type).
9013065b
AC
6054
6055 Set_Ekind (Full_Der, E_Record_Type);
6056 Set_Is_Underlying_Record_View (Full_Der);
6057
39f346aa 6058 Analyze (Decl);
9013065b
AC
6059
6060 pragma Assert (Has_Discriminants (Full_Der)
6061 and then not Has_Unknown_Discriminants (Full_Der));
6062
39f346aa
ES
6063 Uninstall_Declarations (Par_Scope);
6064
c206e8fd
AC
6065 -- Freeze the underlying record view, to prevent generation of
6066 -- useless dispatching information, which is simply shared with
6067 -- the real derived type.
39f346aa
ES
6068
6069 Set_Is_Frozen (Full_Der);
9013065b 6070
c206e8fd 6071 -- Set up links between real entity and underlying record view
9013065b
AC
6072
6073 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
6074 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
39f346aa
ES
6075 end;
6076
c206e8fd 6077 -- If discriminants are known, build derived record
39f346aa
ES
6078
6079 else
6080 Build_Derived_Record_Type
6081 (N, Parent_Type, Derived_Type, Derive_Subps);
6082 end if;
6083
996ae0b0
RK
6084 return;
6085
6086 elsif Has_Discriminants (Parent_Type) then
996ae0b0
RK
6087 if Present (Full_View (Parent_Type)) then
6088 if not Is_Completion then
6089
a5b62485
AC
6090 -- Copy declaration for subsequent analysis, to provide a
6091 -- completion for what is a private declaration. Indicate that
6092 -- the full type is internally generated.
996ae0b0
RK
6093
6094 Full_Decl := New_Copy_Tree (N);
6095 Full_Der := New_Copy (Derived_Type);
7324bf49 6096 Set_Comes_From_Source (Full_Decl, False);
950d3e7d 6097 Set_Comes_From_Source (Full_Der, False);
f4b049db 6098 Set_Parent (Full_Der, Full_Decl);
fbf5a39b 6099
996ae0b0
RK
6100 Insert_After (N, Full_Decl);
6101
6102 else
c206e8fd
AC
6103 -- If this is a completion, the full view being built is itself
6104 -- private. We build a subtype of the parent with the same
6105 -- constraints as this full view, to convey to the back end the
6106 -- constrained components and the size of this subtype. If the
6107 -- parent is constrained, its full view can serve as the
6108 -- underlying full view of the derived type.
996ae0b0
RK
6109
6110 if No (Discriminant_Specifications (N)) then
71d9e9f2
ES
6111 if Nkind (Subtype_Indication (Type_Definition (N))) =
6112 N_Subtype_Indication
996ae0b0
RK
6113 then
6114 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
6115
6116 elsif Is_Constrained (Full_View (Parent_Type)) then
c5d91669
AC
6117 Set_Underlying_Full_View
6118 (Derived_Type, Full_View (Parent_Type));
996ae0b0
RK
6119 end if;
6120
6121 else
6122 -- If there are new discriminants, the parent subtype is
6123 -- constrained by them, but it is not clear how to build
c206e8fd 6124 -- the Underlying_Full_View in this case???
996ae0b0
RK
6125
6126 null;
6127 end if;
6128 end if;
6129 end if;
6130
ffe9aba8 6131 -- Build partial view of derived type from partial view of parent
fbf5a39b 6132
996ae0b0
RK
6133 Build_Derived_Record_Type
6134 (N, Parent_Type, Derived_Type, Derive_Subps);
6135
c206e8fd 6136 if Present (Full_View (Parent_Type)) and then not Is_Completion then
996ae0b0
RK
6137 if not In_Open_Scopes (Par_Scope)
6138 or else not In_Same_Source_Unit (N, Parent_Type)
6139 then
6140 -- Swap partial and full views temporarily
6141
6142 Install_Private_Declarations (Par_Scope);
6143 Install_Visible_Declarations (Par_Scope);
6144 Swapped := True;
6145 end if;
6146
a5b62485
AC
6147 -- Build full view of derived type from full view of parent which
6148 -- is now installed. Subprograms have been derived on the partial
6149 -- view, the completion does not derive them anew.
996ae0b0 6150
fbf5a39b 6151 if not Is_Tagged_Type (Parent_Type) then
950d3e7d
ES
6152
6153 -- If the parent is itself derived from another private type,
6154 -- installing the private declarations has not affected its
6155 -- privacy status, so use its own full view explicitly.
6156
6157 if Is_Private_Type (Parent_Type) then
6158 Build_Derived_Record_Type
6159 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
6160 else
6161 Build_Derived_Record_Type
6162 (Full_Decl, Parent_Type, Full_Der, False);
6163 end if;
fbf5a39b 6164
71d9e9f2 6165 else
c206e8fd
AC
6166 -- If full view of parent is tagged, the completion inherits
6167 -- the proper primitive operations.
fbf5a39b
AC
6168
6169 Set_Defining_Identifier (Full_Decl, Full_Der);
6170 Build_Derived_Record_Type
6171 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
fbf5a39b 6172 end if;
996ae0b0 6173
f4b049db
AC
6174 -- The full declaration has been introduced into the tree and
6175 -- processed in the step above. It should not be analyzed again
6176 -- (when encountered later in the current list of declarations)
6177 -- to prevent spurious name conflicts. The full entity remains
6178 -- invisible.
6179
6180 Set_Analyzed (Full_Decl);
6181
996ae0b0
RK
6182 if Swapped then
6183 Uninstall_Declarations (Par_Scope);
6184
6185 if In_Open_Scopes (Par_Scope) then
6186 Install_Visible_Declarations (Par_Scope);
6187 end if;
6188 end if;
6189
6190 Der_Base := Base_Type (Derived_Type);
6191 Set_Full_View (Derived_Type, Full_Der);
6192 Set_Full_View (Der_Base, Base_Type (Full_Der));
6193
a5b62485 6194 -- Copy the discriminant list from full view to the partial views
c206e8fd
AC
6195 -- (base type and its subtype). Gigi requires that the partial and
6196 -- full views have the same discriminants.
a5b62485
AC
6197
6198 -- Note that since the partial view is pointing to discriminants
6199 -- in the full view, their scope will be that of the full view.
c206e8fd 6200 -- This might cause some front end problems and need adjustment???
996ae0b0
RK
6201
6202 Discr := First_Discriminant (Base_Type (Full_Der));
6203 Set_First_Entity (Der_Base, Discr);
6204
6205 loop
6206 Last_Discr := Discr;
6207 Next_Discriminant (Discr);
6208 exit when No (Discr);
6209 end loop;
6210
6211 Set_Last_Entity (Der_Base, Last_Discr);
6212
6213 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
6214 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
30c20106 6215 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
996ae0b0
RK
6216
6217 else
c206e8fd
AC
6218 -- If this is a completion, the derived type stays private and
6219 -- there is no need to create a further full view, except in the
6220 -- unusual case when the derivation is nested within a child unit,
6221 -- see below.
996ae0b0
RK
6222
6223 null;
6224 end if;
6225
6226 elsif Present (Full_View (Parent_Type))
6227 and then Has_Discriminants (Full_View (Parent_Type))
6228 then
6229 if Has_Unknown_Discriminants (Parent_Type)
7d7af38a
JM
6230 and then Nkind (Subtype_Indication (Type_Definition (N))) =
6231 N_Subtype_Indication
996ae0b0
RK
6232 then
6233 Error_Msg_N
6234 ("cannot constrain type with unknown discriminants",
6235 Subtype_Indication (Type_Definition (N)));
6236 return;
6237 end if;
6238
c206e8fd
AC
6239 -- If full view of parent is a record type, build full view as a
6240 -- derivation from the parent's full view. Partial view remains
6241 -- private. For code generation and linking, the full view must have
6242 -- the same public status as the partial one. This full view is only
6243 -- needed if the parent type is in an enclosing scope, so that the
6244 -- full view may actually become visible, e.g. in a child unit. This
6245 -- is both more efficient, and avoids order of freezing problems with
6246 -- the added entities.
fbf5a39b
AC
6247
6248 if not Is_Private_Type (Full_View (Parent_Type))
6249 and then (In_Open_Scopes (Scope (Parent_Type)))
6250 then
7675ad4f
AC
6251 Full_Der :=
6252 Make_Defining_Identifier
6253 (Sloc (Derived_Type), Chars (Derived_Type));
07fc65c4
GB
6254 Set_Is_Itype (Full_Der);
6255 Set_Has_Private_Declaration (Full_Der);
6256 Set_Has_Private_Declaration (Derived_Type);
6257 Set_Associated_Node_For_Itype (Full_Der, N);
6258 Set_Parent (Full_Der, Parent (Derived_Type));
6259 Set_Full_View (Derived_Type, Full_Der);
fbf5a39b 6260 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
07fc65c4
GB
6261 Full_P := Full_View (Parent_Type);
6262 Exchange_Declarations (Parent_Type);
6263 Copy_And_Build;
6264 Exchange_Declarations (Full_P);
996ae0b0 6265
07fc65c4
GB
6266 else
6267 Build_Derived_Record_Type
6268 (N, Full_View (Parent_Type), Derived_Type,
6269 Derive_Subps => False);
6270 end if;
996ae0b0 6271
c206e8fd
AC
6272 -- In any case, the primitive operations are inherited from the
6273 -- parent type, not from the internal full view.
996ae0b0 6274
996ae0b0
RK
6275 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
6276
6277 if Derive_Subps then
6278 Derive_Subprograms (Parent_Type, Derived_Type);
6279 end if;
6280
6281 else
07fc65c4 6282 -- Untagged type, No discriminants on either view
996ae0b0 6283
71d9e9f2
ES
6284 if Nkind (Subtype_Indication (Type_Definition (N))) =
6285 N_Subtype_Indication
996ae0b0
RK
6286 then
6287 Error_Msg_N
6288 ("illegal constraint on type without discriminants", N);
6289 end if;
6290
6291 if Present (Discriminant_Specifications (N))
6292 and then Present (Full_View (Parent_Type))
6293 and then not Is_Tagged_Type (Full_View (Parent_Type))
6294 then
c206e8fd 6295 Error_Msg_N ("cannot add discriminants to untagged type", N);
996ae0b0
RK
6296 end if;
6297
fbf5a39b 6298 Set_Stored_Constraint (Derived_Type, No_Elist);
07fc65c4
GB
6299 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6300 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
6301 Set_Has_Controlled_Component
6302 (Derived_Type, Has_Controlled_Component
6303 (Parent_Type));
996ae0b0 6304
07fc65c4 6305 -- Direct controlled types do not inherit Finalize_Storage_Only flag
996ae0b0
RK
6306
6307 if not Is_Controlled (Parent_Type) then
07fc65c4
GB
6308 Set_Finalize_Storage_Only
6309 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
996ae0b0
RK
6310 end if;
6311
c206e8fd
AC
6312 -- Construct the implicit full view by deriving from full view of the
6313 -- parent type. In order to get proper visibility, we install the
6314 -- parent scope and its declarations.
996ae0b0 6315
c206e8fd
AC
6316 -- ??? If the parent is untagged private and its completion is
6317 -- tagged, this mechanism will not work because we cannot derive from
6318 -- the tagged full view unless we have an extension.
996ae0b0
RK
6319
6320 if Present (Full_View (Parent_Type))
6321 and then not Is_Tagged_Type (Full_View (Parent_Type))
6322 and then not Is_Completion
6323 then
71d9e9f2 6324 Full_Der :=
7675ad4f
AC
6325 Make_Defining_Identifier
6326 (Sloc (Derived_Type), Chars (Derived_Type));
996ae0b0
RK
6327 Set_Is_Itype (Full_Der);
6328 Set_Has_Private_Declaration (Full_Der);
6329 Set_Has_Private_Declaration (Derived_Type);
6330 Set_Associated_Node_For_Itype (Full_Der, N);
6331 Set_Parent (Full_Der, Parent (Derived_Type));
6332 Set_Full_View (Derived_Type, Full_Der);
6333
6334 if not In_Open_Scopes (Par_Scope) then
6335 Install_Private_Declarations (Par_Scope);
6336 Install_Visible_Declarations (Par_Scope);
6337 Copy_And_Build;
6338 Uninstall_Declarations (Par_Scope);
6339
a5b62485
AC
6340 -- If parent scope is open and in another unit, and parent has a
6341 -- completion, then the derivation is taking place in the visible
6342 -- part of a child unit. In that case retrieve the full view of
6343 -- the parent momentarily.
996ae0b0
RK
6344
6345 elsif not In_Same_Source_Unit (N, Parent_Type) then
6346 Full_P := Full_View (Parent_Type);
6347 Exchange_Declarations (Parent_Type);
6348 Copy_And_Build;
6349 Exchange_Declarations (Full_P);
6350
ffe9aba8 6351 -- Otherwise it is a local derivation
996ae0b0
RK
6352
6353 else
6354 Copy_And_Build;
6355 end if;
6356
6357 Set_Scope (Full_Der, Current_Scope);
6358 Set_Is_First_Subtype (Full_Der,
6359 Is_First_Subtype (Derived_Type));
6360 Set_Has_Size_Clause (Full_Der, False);
6361 Set_Has_Alignment_Clause (Full_Der, False);
6362 Set_Next_Entity (Full_Der, Empty);
6363 Set_Has_Delayed_Freeze (Full_Der);
6364 Set_Is_Frozen (Full_Der, False);
6365 Set_Freeze_Node (Full_Der, Empty);
6366 Set_Depends_On_Private (Full_Der,
c206e8fd 6367 Has_Private_Component (Full_Der));
f91b40db 6368 Set_Public_Status (Full_Der);
996ae0b0
RK
6369 end if;
6370 end if;
6371
6372 Set_Has_Unknown_Discriminants (Derived_Type,
6373 Has_Unknown_Discriminants (Parent_Type));
6374
6375 if Is_Private_Type (Derived_Type) then
6376 Set_Private_Dependents (Derived_Type, New_Elmt_List);
6377 end if;
6378
6379 if Is_Private_Type (Parent_Type)
6380 and then Base_Type (Parent_Type) = Parent_Type
6381 and then In_Open_Scopes (Scope (Parent_Type))
6382 then
6383 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
6384
6385 if Is_Child_Unit (Scope (Current_Scope))
6386 and then Is_Completion
6387 and then In_Private_Part (Current_Scope)
3a77b68d 6388 and then Scope (Parent_Type) /= Current_Scope
996ae0b0
RK
6389 then
6390 -- This is the unusual case where a type completed by a private
c206e8fd
AC
6391 -- derivation occurs within a package nested in a child unit, and
6392 -- the parent is declared in an ancestor. In this case, the full
e34ca162
AC
6393 -- view of the parent type will become visible in the body of
6394 -- the enclosing child, and only then will the current type be
6395 -- possibly non-private. We build a underlying full view that
996ae0b0
RK
6396 -- will be installed when the enclosing child body is compiled.
6397
fea9e956 6398 Full_Der :=
7675ad4f
AC
6399 Make_Defining_Identifier
6400 (Sloc (Derived_Type), Chars (Derived_Type));
fea9e956
ES
6401 Set_Is_Itype (Full_Der);
6402 Build_Itype_Reference (Full_Der, N);
996ae0b0 6403
fea9e956
ES
6404 -- The full view will be used to swap entities on entry/exit to
6405 -- the body, and must appear in the entity list for the package.
6406
6407 Append_Entity (Full_Der, Scope (Derived_Type));
6408 Set_Has_Private_Declaration (Full_Der);
6409 Set_Has_Private_Declaration (Derived_Type);
6410 Set_Associated_Node_For_Itype (Full_Der, N);
6411 Set_Parent (Full_Der, Parent (Derived_Type));
6412 Full_P := Full_View (Parent_Type);
6413 Exchange_Declarations (Parent_Type);
6414 Copy_And_Build;
6415 Exchange_Declarations (Full_P);
6416 Set_Underlying_Full_View (Derived_Type, Full_Der);
996ae0b0
RK
6417 end if;
6418 end if;
6419 end Build_Derived_Private_Type;
6420
6421 -------------------------------
6422 -- Build_Derived_Record_Type --
6423 -------------------------------
6424
71d9e9f2 6425 -- 1. INTRODUCTION
996ae0b0
RK
6426
6427 -- Ideally we would like to use the same model of type derivation for
6428 -- tagged and untagged record types. Unfortunately this is not quite
6429 -- possible because the semantics of representation clauses is different
6430 -- for tagged and untagged records under inheritance. Consider the
6431 -- following:
6432
6433 -- type R (...) is [tagged] record ... end record;
6434 -- type T (...) is new R (...) [with ...];
6435
fea9e956
ES
6436 -- The representation clauses for T can specify a completely different
6437 -- record layout from R's. Hence the same component can be placed in two
fdac1f80
AC
6438 -- very different positions in objects of type T and R. If R and T are
6439 -- tagged types, representation clauses for T can only specify the layout
6440 -- of non inherited components, thus components that are common in R and T
6441 -- have the same position in objects of type R and T.
996ae0b0
RK
6442
6443 -- This has two implications. The first is that the entire tree for R's
a5b62485
AC
6444 -- declaration needs to be copied for T in the untagged case, so that T
6445 -- can be viewed as a record type of its own with its own representation
996ae0b0
RK
6446 -- clauses. The second implication is the way we handle discriminants.
6447 -- Specifically, in the untagged case we need a way to communicate to Gigi
6448 -- what are the real discriminants in the record, while for the semantics
6449 -- we need to consider those introduced by the user to rename the
6450 -- discriminants in the parent type. This is handled by introducing the
fbf5a39b 6451 -- notion of stored discriminants. See below for more.
996ae0b0
RK
6452
6453 -- Fortunately the way regular components are inherited can be handled in
6454 -- the same way in tagged and untagged types.
6455
6456 -- To complicate things a bit more the private view of a private extension
6457 -- cannot be handled in the same way as the full view (for one thing the
6458 -- semantic rules are somewhat different). We will explain what differs
6459 -- below.
6460
71d9e9f2 6461 -- 2. DISCRIMINANTS UNDER INHERITANCE
996ae0b0
RK
6462
6463 -- The semantic rules governing the discriminants of derived types are
6464 -- quite subtle.
6465
6466 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
88b32fc3 6467 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
996ae0b0
RK
6468
6469 -- If parent type has discriminants, then the discriminants that are
6470 -- declared in the derived type are [3.4 (11)]:
6471
6472 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
6473 -- there is one;
6474
a5b62485
AC
6475 -- o Otherwise, each discriminant of the parent type (implicitly declared
6476 -- in the same order with the same specifications). In this case, the
6477 -- discriminants are said to be "inherited", or if unknown in the parent
6478 -- are also unknown in the derived type.
996ae0b0
RK
6479
6480 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
6481
6482 -- o The parent subtype shall be constrained;
6483
6484 -- o If the parent type is not a tagged type, then each discriminant of
6485 -- the derived type shall be used in the constraint defining a parent
88b32fc3
BD
6486 -- subtype. [Implementation note: This ensures that the new discriminant
6487 -- can share storage with an existing discriminant.]
996ae0b0
RK
6488
6489 -- For the derived type each discriminant of the parent type is either
6490 -- inherited, constrained to equal some new discriminant of the derived
6491 -- type, or constrained to the value of an expression.
6492
6493 -- When inherited or constrained to equal some new discriminant, the
6494 -- parent discriminant and the discriminant of the derived type are said
6495 -- to "correspond".
6496
6497 -- If a discriminant of the parent type is constrained to a specific value
6498 -- in the derived type definition, then the discriminant is said to be
6499 -- "specified" by that derived type definition.
6500
ffe9aba8 6501 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
996ae0b0 6502
fbf5a39b
AC
6503 -- We have spoken about stored discriminants in point 1 (introduction)
6504 -- above. There are two sort of stored discriminants: implicit and
996ae0b0 6505 -- explicit. As long as the derived type inherits the same discriminants as
fbf5a39b 6506 -- the root record type, stored discriminants are the same as regular
996ae0b0
RK
6507 -- discriminants, and are said to be implicit. However, if any discriminant
6508 -- in the root type was renamed in the derived type, then the derived
fbf5a39b 6509 -- type will contain explicit stored discriminants. Explicit stored
996ae0b0 6510 -- discriminants are discriminants in addition to the semantically visible
fbf5a39b 6511 -- discriminants defined for the derived type. Stored discriminants are
996ae0b0
RK
6512 -- used by Gigi to figure out what are the physical discriminants in
6513 -- objects of the derived type (see precise definition in einfo.ads).
6514 -- As an example, consider the following:
6515
6516 -- type R (D1, D2, D3 : Int) is record ... end record;
6517 -- type T1 is new R;
6518 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
6519 -- type T3 is new T2;
6520 -- type T4 (Y : Int) is new T3 (Y, 99);
6521
fbf5a39b 6522 -- The following table summarizes the discriminants and stored
996ae0b0
RK
6523 -- discriminants in R and T1 through T4.
6524
fbf5a39b 6525 -- Type Discrim Stored Discrim Comment
30c20106
AC
6526 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
6527 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
6528 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
6529 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
6530 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
6531
6532 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
6533 -- find the corresponding discriminant in the parent type, while
996ae0b0
RK
6534 -- Original_Record_Component (abbreviated ORC below), the actual physical
6535 -- component that is renamed. Finally the field Is_Completely_Hidden
fbf5a39b 6536 -- (abbreviated ICH below) is set for all explicit stored discriminants
996ae0b0
RK
6537 -- (see einfo.ads for more info). For the above example this gives:
6538
6539 -- Discrim CD ORC ICH
6540 -- ^^^^^^^ ^^ ^^^ ^^^
6541 -- D1 in R empty itself no
6542 -- D2 in R empty itself no
6543 -- D3 in R empty itself no
6544
6545 -- D1 in T1 D1 in R itself no
6546 -- D2 in T1 D2 in R itself no
6547 -- D3 in T1 D3 in R itself no
6548
6549 -- X1 in T2 D3 in T1 D3 in T2 no
6550 -- X2 in T2 D1 in T1 D1 in T2 no
6551 -- D1 in T2 empty itself yes
6552 -- D2 in T2 empty itself yes
6553 -- D3 in T2 empty itself yes
6554
6555 -- X1 in T3 X1 in T2 D3 in T3 no
6556 -- X2 in T3 X2 in T2 D1 in T3 no
6557 -- D1 in T3 empty itself yes
6558 -- D2 in T3 empty itself yes
6559 -- D3 in T3 empty itself yes
6560
6561 -- Y in T4 X1 in T3 D3 in T3 no
6562 -- D1 in T3 empty itself yes
6563 -- D2 in T3 empty itself yes
6564 -- D3 in T3 empty itself yes
6565
71d9e9f2 6566 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
996ae0b0 6567
88b32fc3 6568 -- Type derivation for tagged types is fairly straightforward. If no
996ae0b0 6569 -- discriminants are specified by the derived type, these are inherited
fbf5a39b 6570 -- from the parent. No explicit stored discriminants are ever necessary.
996ae0b0
RK
6571 -- The only manipulation that is done to the tree is that of adding a
6572 -- _parent field with parent type and constrained to the same constraint
6573 -- specified for the parent in the derived type definition. For instance:
6574
6575 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
6576 -- type T1 is new R with null record;
6577 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
6578
71d9e9f2 6579 -- are changed into:
996ae0b0
RK
6580
6581 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
6582 -- _parent : R (D1, D2, D3);
6583 -- end record;
6584
6585 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
6586 -- _parent : T1 (X2, 88, X1);
6587 -- end record;
6588
6589 -- The discriminants actually present in R, T1 and T2 as well as their CD,
6590 -- ORC and ICH fields are:
6591
6592 -- Discrim CD ORC ICH
6593 -- ^^^^^^^ ^^ ^^^ ^^^
6594 -- D1 in R empty itself no
6595 -- D2 in R empty itself no
6596 -- D3 in R empty itself no
6597
6598 -- D1 in T1 D1 in R D1 in R no
6599 -- D2 in T1 D2 in R D2 in R no
6600 -- D3 in T1 D3 in R D3 in R no
6601
6602 -- X1 in T2 D3 in T1 D3 in R no
6603 -- X2 in T2 D1 in T1 D1 in R no
6604
71d9e9f2 6605 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
996ae0b0
RK
6606 --
6607 -- Regardless of whether we dealing with a tagged or untagged type
6608 -- we will transform all derived type declarations of the form
6609 --
6610 -- type T is new R (...) [with ...];
6611 -- or
6612 -- subtype S is R (...);
6613 -- type T is new S [with ...];
6614 -- into
6615 -- type BT is new R [with ...];
6616 -- subtype T is BT (...);
6617 --
6618 -- That is, the base derived type is constrained only if it has no
6619 -- discriminants. The reason for doing this is that GNAT's semantic model
6620 -- assumes that a base type with discriminants is unconstrained.
6621 --
6622 -- Note that, strictly speaking, the above transformation is not always
fbf5a39b 6623 -- correct. Consider for instance the following excerpt from ACVC b34011a:
996ae0b0
RK
6624 --
6625 -- procedure B34011A is
6626 -- type REC (D : integer := 0) is record
6627 -- I : Integer;
6628 -- end record;
6629
6630 -- package P is
6631 -- type T6 is new Rec;
6632 -- function F return T6;
6633 -- end P;
6634
6635 -- use P;
6636 -- package Q6 is
6637 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
6638 -- end Q6;
6639 --
6640 -- The definition of Q6.U is illegal. However transforming Q6.U into
6641
6642 -- type BaseU is new T6;
6643 -- subtype U is BaseU (Q6.F.I)
6644
6645 -- turns U into a legal subtype, which is incorrect. To avoid this problem
6646 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
6647 -- the transformation described above.
6648
6649 -- There is another instance where the above transformation is incorrect.
6650 -- Consider:
6651
6652 -- package Pack is
6653 -- type Base (D : Integer) is tagged null record;
6654 -- procedure P (X : Base);
6655
6656 -- type Der is new Base (2) with null record;
6657 -- procedure P (X : Der);
6658 -- end Pack;
6659
6660 -- Then the above transformation turns this into
6661
6662 -- type Der_Base is new Base with null record;
44d6a706 6663 -- -- procedure P (X : Base) is implicitly inherited here
996ae0b0
RK
6664 -- -- as procedure P (X : Der_Base).
6665
6666 -- subtype Der is Der_Base (2);
6667 -- procedure P (X : Der);
6668 -- -- The overriding of P (X : Der_Base) is illegal since we
6669 -- -- have a parameter conformance problem.
6670
6671 -- To get around this problem, after having semantically processed Der_Base
6672 -- and the rewritten subtype declaration for Der, we copy Der_Base field
6673 -- Discriminant_Constraint from Der so that when parameter conformance is
fbf5a39b 6674 -- checked when P is overridden, no semantic errors are flagged.
996ae0b0 6675
ffe9aba8 6676 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
996ae0b0 6677
fbf5a39b 6678 -- Regardless of whether we are dealing with a tagged or untagged type
996ae0b0
RK
6679 -- we will transform all derived type declarations of the form
6680
6681 -- type R (D1, .., Dn : ...) is [tagged] record ...;
6682 -- type T is new R [with ...];
6683 -- into
6684 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6685
6686 -- The reason for such transformation is that it allows us to implement a
6687 -- very clean form of component inheritance as explained below.
6688
6689 -- Note that this transformation is not achieved by direct tree rewriting
6690 -- and manipulation, but rather by redoing the semantic actions that the
6691 -- above transformation will entail. This is done directly in routine
6692 -- Inherit_Components.
6693
71d9e9f2 6694 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
996ae0b0
RK
6695
6696 -- In both tagged and untagged derived types, regular non discriminant
6697 -- components are inherited in the derived type from the parent type. In
6698 -- the absence of discriminants component, inheritance is straightforward
6699 -- as components can simply be copied from the parent.
a5b62485 6700
996ae0b0
RK
6701 -- If the parent has discriminants, inheriting components constrained with
6702 -- these discriminants requires caution. Consider the following example:
6703
6704 -- type R (D1, D2 : Positive) is [tagged] record
6705 -- S : String (D1 .. D2);
6706 -- end record;
6707
6708 -- type T1 is new R [with null record];
6709 -- type T2 (X : positive) is new R (1, X) [with null record];
6710
6711 -- As explained in 6. above, T1 is rewritten as
996ae0b0 6712 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
996ae0b0
RK
6713 -- which makes the treatment for T1 and T2 identical.
6714
6715 -- What we want when inheriting S, is that references to D1 and D2 in R are
f3d57416 6716 -- replaced with references to their correct constraints, i.e. D1 and D2 in
996ae0b0
RK
6717 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
6718 -- with either discriminant references in the derived type or expressions.
fbf5a39b 6719 -- This replacement is achieved as follows: before inheriting R's
996ae0b0
RK
6720 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6721 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
6722 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6723 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
6724 -- by String (1 .. X).
6725
71d9e9f2 6726 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
996ae0b0
RK
6727
6728 -- We explain here the rules governing private type extensions relevant to
6729 -- type derivation. These rules are explained on the following example:
6730
6731 -- type D [(...)] is new A [(...)] with private; <-- partial view
6732 -- type D [(...)] is new P [(...)] with null record; <-- full view
6733
6734 -- Type A is called the ancestor subtype of the private extension.
6735 -- Type P is the parent type of the full view of the private extension. It
6736 -- must be A or a type derived from A.
6737
6738 -- The rules concerning the discriminants of private type extensions are
6739 -- [7.3(10-13)]:
6740
6741 -- o If a private extension inherits known discriminants from the ancestor
6742 -- subtype, then the full view shall also inherit its discriminants from
6743 -- the ancestor subtype and the parent subtype of the full view shall be
6744 -- constrained if and only if the ancestor subtype is constrained.
6745
6746 -- o If a partial view has unknown discriminants, then the full view may
6747 -- define a definite or an indefinite subtype, with or without
6748 -- discriminants.
6749
6750 -- o If a partial view has neither known nor unknown discriminants, then
6751 -- the full view shall define a definite subtype.
6752
6753 -- o If the ancestor subtype of a private extension has constrained
fbf5a39b 6754 -- discriminants, then the parent subtype of the full view shall impose a
996ae0b0
RK
6755 -- statically matching constraint on those discriminants.
6756
6757 -- This means that only the following forms of private extensions are
6758 -- allowed:
6759
6760 -- type D is new A with private; <-- partial view
6761 -- type D is new P with null record; <-- full view
6762
6763 -- If A has no discriminants than P has no discriminants, otherwise P must
6764 -- inherit A's discriminants.
6765
6766 -- type D is new A (...) with private; <-- partial view
6767 -- type D is new P (:::) with null record; <-- full view
6768
6769 -- P must inherit A's discriminants and (...) and (:::) must statically
6770 -- match.
6771
6772 -- subtype A is R (...);
6773 -- type D is new A with private; <-- partial view
6774 -- type D is new P with null record; <-- full view
6775
6776 -- P must have inherited R's discriminants and must be derived from A or
6777 -- any of its subtypes.
6778
6779 -- type D (..) is new A with private; <-- partial view
6780 -- type D (..) is new P [(:::)] with null record; <-- full view
6781
6782 -- No specific constraints on P's discriminants or constraint (:::).
6783 -- Note that A can be unconstrained, but the parent subtype P must either
6784 -- be constrained or (:::) must be present.
6785
6786 -- type D (..) is new A [(...)] with private; <-- partial view
6787 -- type D (..) is new P [(:::)] with null record; <-- full view
6788
6789 -- P's constraints on A's discriminants must statically match those
6790 -- imposed by (...).
6791
71d9e9f2 6792 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
996ae0b0
RK
6793
6794 -- The full view of a private extension is handled exactly as described
a5b62485 6795 -- above. The model chose for the private view of a private extension is
f3d57416 6796 -- the same for what concerns discriminants (i.e. they receive the same
996ae0b0
RK
6797 -- treatment as in the tagged case). However, the private view of the
6798 -- private extension always inherits the components of the parent base,
a5b62485
AC
6799 -- without replacing any discriminant reference. Strictly speaking this is
6800 -- incorrect. However, Gigi never uses this view to generate code so this
6801 -- is a purely semantic issue. In theory, a set of transformations similar
6802 -- to those given in 5. and 6. above could be applied to private views of
6803 -- private extensions to have the same model of component inheritance as
6804 -- for non private extensions. However, this is not done because it would
6805 -- further complicate private type processing. Semantically speaking, this
6806 -- leaves us in an uncomfortable situation. As an example consider:
996ae0b0
RK
6807
6808 -- package Pack is
6809 -- type R (D : integer) is tagged record
6810 -- S : String (1 .. D);
6811 -- end record;
6812 -- procedure P (X : R);
6813 -- type T is new R (1) with private;
6814 -- private
6815 -- type T is new R (1) with null record;
6816 -- end;
6817
6818 -- This is transformed into:
6819
6820 -- package Pack is
6821 -- type R (D : integer) is tagged record
6822 -- S : String (1 .. D);
6823 -- end record;
6824 -- procedure P (X : R);
6825 -- type T is new R (1) with private;
6826 -- private
6827 -- type BaseT is new R with null record;
6828 -- subtype T is BaseT (1);
6829 -- end;
6830
ffe9aba8 6831 -- (strictly speaking the above is incorrect Ada)
996ae0b0
RK
6832
6833 -- From the semantic standpoint the private view of private extension T
6834 -- should be flagged as constrained since one can clearly have
6835 --
6836 -- Obj : T;
6837 --
6838 -- in a unit withing Pack. However, when deriving subprograms for the
6839 -- private view of private extension T, T must be seen as unconstrained
6840 -- since T has discriminants (this is a constraint of the current
6841 -- subprogram derivation model). Thus, when processing the private view of
6842 -- a private extension such as T, we first mark T as unconstrained, we
6843 -- process it, we perform program derivation and just before returning from
6844 -- Build_Derived_Record_Type we mark T as constrained.
a5b62485 6845
fbf5a39b 6846 -- ??? Are there are other uncomfortable cases that we will have to
996ae0b0
RK
6847 -- deal with.
6848
71d9e9f2 6849 -- 10. RECORD_TYPE_WITH_PRIVATE complications
996ae0b0
RK
6850
6851 -- Types that are derived from a visible record type and have a private
6852 -- extension present other peculiarities. They behave mostly like private
6853 -- types, but if they have primitive operations defined, these will not
6854 -- have the proper signatures for further inheritance, because other
6855 -- primitive operations will use the implicit base that we define for
6856 -- private derivations below. This affect subprogram inheritance (see
6857 -- Derive_Subprograms for details). We also derive the implicit base from
6858 -- the base type of the full view, so that the implicit base is a record
6859 -- type and not another private type, This avoids infinite loops.
6860
6861 procedure Build_Derived_Record_Type
6862 (N : Node_Id;
6863 Parent_Type : Entity_Id;
6864 Derived_Type : Entity_Id;
6865 Derive_Subps : Boolean := True)
6866 is
6867 Loc : constant Source_Ptr := Sloc (N);
6868 Parent_Base : Entity_Id;
996ae0b0
RK
6869 Type_Def : Node_Id;
6870 Indic : Node_Id;
996ae0b0
RK
6871 Discrim : Entity_Id;
6872 Last_Discrim : Entity_Id;
6873 Constrs : Elist_Id;
71d9e9f2 6874
c6fe3827 6875 Discs : Elist_Id := New_Elmt_List;
996ae0b0
RK
6876 -- An empty Discs list means that there were no constraints in the
6877 -- subtype indication or that there was an error processing it.
6878
c6fe3827
GD
6879 Assoc_List : Elist_Id;
6880 New_Discrs : Elist_Id;
6881 New_Base : Entity_Id;
6882 New_Decl : Node_Id;
6883 New_Indic : Node_Id;
996ae0b0
RK
6884
6885 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
07fc65c4
GB
6886 Discriminant_Specs : constant Boolean :=
6887 Present (Discriminant_Specifications (N));
6888 Private_Extension : constant Boolean :=
7d7af38a 6889 Nkind (N) = N_Private_Extension_Declaration;
996ae0b0 6890
c6fe3827
GD
6891 Constraint_Present : Boolean;
6892 Inherit_Discrims : Boolean := False;
6893 Save_Etype : Entity_Id;
6894 Save_Discr_Constr : Elist_Id;
6895 Save_Next_Entity : Entity_Id;
996ae0b0
RK
6896
6897 begin
6898 if Ekind (Parent_Type) = E_Record_Type_With_Private
6899 and then Present (Full_View (Parent_Type))
6900 and then Has_Discriminants (Parent_Type)
6901 then
6902 Parent_Base := Base_Type (Full_View (Parent_Type));
6903 else
6904 Parent_Base := Base_Type (Parent_Type);
6905 end if;
6906
6907 -- Before we start the previously documented transformations, here is
fea9e956
ES
6908 -- little fix for size and alignment of tagged types. Normally when we
6909 -- derive type D from type P, we copy the size and alignment of P as the
6910 -- default for D, and in the absence of explicit representation clauses
6911 -- for D, the size and alignment are indeed the same as the parent.
6912
6913 -- But this is wrong for tagged types, since fields may be added, and
6914 -- the default size may need to be larger, and the default alignment may
6915 -- need to be larger.
996ae0b0 6916
fea9e956
ES
6917 -- We therefore reset the size and alignment fields in the tagged case.
6918 -- Note that the size and alignment will in any case be at least as
6919 -- large as the parent type (since the derived type has a copy of the
6920 -- parent type in the _parent field)
996ae0b0 6921
fea9e956
ES
6922 -- The type is also marked as being tagged here, which is needed when
6923 -- processing components with a self-referential anonymous access type
6924 -- in the call to Check_Anonymous_Access_Components below. Note that
6925 -- this flag is also set later on for completeness.
996ae0b0
RK
6926
6927 if Is_Tagged then
fea9e956
ES
6928 Set_Is_Tagged_Type (Derived_Type);
6929 Init_Size_Align (Derived_Type);
996ae0b0
RK
6930 end if;
6931
71d9e9f2 6932 -- STEP 0a: figure out what kind of derived type declaration we have
996ae0b0
RK
6933
6934 if Private_Extension then
6935 Type_Def := N;
6936 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6937
6938 else
6939 Type_Def := Type_Definition (N);
6940
c6fe3827 6941 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
996ae0b0
RK
6942 -- Parent_Base can be a private type or private extension. However,
6943 -- for tagged types with an extension the newly added fields are
6944 -- visible and hence the Derived_Type is always an E_Record_Type.
6945 -- (except that the parent may have its own private fields).
6946 -- For untagged types we preserve the Ekind of the Parent_Base.
6947
6948 if Present (Record_Extension_Part (Type_Def)) then
6949 Set_Ekind (Derived_Type, E_Record_Type);
fea9e956
ES
6950
6951 -- Create internal access types for components with anonymous
6952 -- access types.
6953
0791fbe9 6954 if Ada_Version >= Ada_2005 then
fea9e956
ES
6955 Check_Anonymous_Access_Components
6956 (N, Derived_Type, Derived_Type,
6957 Component_List (Record_Extension_Part (Type_Def)));
6958 end if;
6959
996ae0b0
RK
6960 else
6961 Set_Ekind (Derived_Type, Ekind (Parent_Base));
6962 end if;
6963 end if;
6964
6965 -- Indic can either be an N_Identifier if the subtype indication
6966 -- contains no constraint or an N_Subtype_Indication if the subtype
6967 -- indication has a constraint.
6968
6969 Indic := Subtype_Indication (Type_Def);
6970 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6971
8a6a52dc
AC
6972 -- Check that the type has visible discriminants. The type may be
6973 -- a private type with unknown discriminants whose full view has
6974 -- discriminants which are invisible.
6975
996ae0b0 6976 if Constraint_Present then
8a6a52dc
AC
6977 if not Has_Discriminants (Parent_Base)
6978 or else
6979 (Has_Unknown_Discriminants (Parent_Base)
6980 and then Is_Private_Type (Parent_Base))
6981 then
996ae0b0
RK
6982 Error_Msg_N
6983 ("invalid constraint: type has no discriminant",
6984 Constraint (Indic));
6985
6986 Constraint_Present := False;
6987 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6988
6989 elsif Is_Constrained (Parent_Type) then
6990 Error_Msg_N
6991 ("invalid constraint: parent type is already constrained",
6992 Constraint (Indic));
6993
6994 Constraint_Present := False;
6995 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6996 end if;
6997 end if;
6998
71d9e9f2 6999 -- STEP 0b: If needed, apply transformation given in point 5. above
996ae0b0
RK
7000
7001 if not Private_Extension
7002 and then Has_Discriminants (Parent_Type)
7003 and then not Discriminant_Specs
7004 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7005 then
ffe9aba8 7006 -- First, we must analyze the constraint (see comment in point 5.)
996ae0b0
RK
7007
7008 if Constraint_Present then
7009 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
7010
7011 if Has_Discriminants (Derived_Type)
7012 and then Has_Private_Declaration (Derived_Type)
7013 and then Present (Discriminant_Constraint (Derived_Type))
7014 then
ea0a7f39
ST
7015 -- Verify that constraints of the full view statically match
7016 -- those given in the partial view.
996ae0b0
RK
7017
7018 declare
7019 C1, C2 : Elmt_Id;
7020
7021 begin
7022 C1 := First_Elmt (New_Discrs);
7023 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
996ae0b0 7024 while Present (C1) and then Present (C2) loop
ea0a7f39
ST
7025 if Fully_Conformant_Expressions (Node (C1), Node (C2))
7026 or else
10619438
AC
7027 (Is_OK_Static_Expression (Node (C1))
7028 and then
7029 Is_OK_Static_Expression (Node (C2))
7030 and then
7031 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
996ae0b0 7032 then
ea0a7f39
ST
7033 null;
7034
7035 else
996ae0b0
RK
7036 Error_Msg_N (
7037 "constraint not conformant to previous declaration",
7038 Node (C1));
7039 end if;
9dfd2ff8 7040
996ae0b0
RK
7041 Next_Elmt (C1);
7042 Next_Elmt (C2);
7043 end loop;
7044 end;
7045 end if;
7046 end if;
7047
7048 -- Insert and analyze the declaration for the unconstrained base type
7049
7050 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7051
7052 New_Decl :=
7053 Make_Full_Type_Declaration (Loc,
7054 Defining_Identifier => New_Base,
7055 Type_Definition =>
7056 Make_Derived_Type_Definition (Loc,
7057 Abstract_Present => Abstract_Present (Type_Def),
fdac1f80 7058 Limited_Present => Limited_Present (Type_Def),
996ae0b0
RK
7059 Subtype_Indication =>
7060 New_Occurrence_Of (Parent_Base, Loc),
7061 Record_Extension_Part =>
fdac1f80
AC
7062 Relocate_Node (Record_Extension_Part (Type_Def)),
7063 Interface_List => Interface_List (Type_Def)));
996ae0b0
RK
7064
7065 Set_Parent (New_Decl, Parent (N));
7066 Mark_Rewrite_Insertion (New_Decl);
7067 Insert_Before (N, New_Decl);
7068
61441c18 7069 -- In the extension case, make sure ancestor is frozen appropriately
47d3b920
AC
7070 -- (see also non-discriminated case below).
7071
61441c18 7072 if Present (Record_Extension_Part (Type_Def))
c42bfef2 7073 or else Is_Interface (Parent_Base)
61441c18 7074 then
47d3b920
AC
7075 Freeze_Before (New_Decl, Parent_Type);
7076 end if;
7077
a5b62485
AC
7078 -- Note that this call passes False for the Derive_Subps parameter
7079 -- because subprogram derivation is deferred until after creating
7080 -- the subtype (see below).
996ae0b0
RK
7081
7082 Build_Derived_Type
7083 (New_Decl, Parent_Base, New_Base,
7084 Is_Completion => True, Derive_Subps => False);
7085
7086 -- ??? This needs re-examination to determine whether the
7087 -- above call can simply be replaced by a call to Analyze.
7088
7089 Set_Analyzed (New_Decl);
7090
7091 -- Insert and analyze the declaration for the constrained subtype
7092
7093 if Constraint_Present then
7094 New_Indic :=
7095 Make_Subtype_Indication (Loc,
7096 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7097 Constraint => Relocate_Node (Constraint (Indic)));
7098
7099 else
7100 declare
fbf5a39b 7101 Constr_List : constant List_Id := New_List;
996ae0b0 7102 C : Elmt_Id;
fbf5a39b 7103 Expr : Node_Id;
996ae0b0
RK
7104
7105 begin
7106 C := First_Elmt (Discriminant_Constraint (Parent_Type));
7107 while Present (C) loop
7108 Expr := Node (C);
7109
7110 -- It is safe here to call New_Copy_Tree since
7111 -- Force_Evaluation was called on each constraint in
7112 -- Build_Discriminant_Constraints.
7113
7114 Append (New_Copy_Tree (Expr), To => Constr_List);
7115
7116 Next_Elmt (C);
7117 end loop;
7118
7119 New_Indic :=
7120 Make_Subtype_Indication (Loc,
7121 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7122 Constraint =>
7123 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
7124 end;
7125 end if;
7126
7127 Rewrite (N,
7128 Make_Subtype_Declaration (Loc,
7129 Defining_Identifier => Derived_Type,
7130 Subtype_Indication => New_Indic));
7131
7132 Analyze (N);
7133
71d9e9f2
ES
7134 -- Derivation of subprograms must be delayed until the full subtype
7135 -- has been established to ensure proper overriding of subprograms
7136 -- inherited by full types. If the derivations occurred as part of
7137 -- the call to Build_Derived_Type above, then the check for type
7138 -- conformance would fail because earlier primitive subprograms
7139 -- could still refer to the full type prior the change to the new
7140 -- subtype and hence would not match the new base type created here.
996ae0b0 7141
59262ebb 7142 Derive_Subprograms (Parent_Type, Derived_Type);
996ae0b0
RK
7143
7144 -- For tagged types the Discriminant_Constraint of the new base itype
7145 -- is inherited from the first subtype so that no subtype conformance
7146 -- problem arise when the first subtype overrides primitive
7147 -- operations inherited by the implicit base type.
7148
7149 if Is_Tagged then
7150 Set_Discriminant_Constraint
7151 (New_Base, Discriminant_Constraint (Derived_Type));
7152 end if;
7153
7154 return;
7155 end if;
7156
7157 -- If we get here Derived_Type will have no discriminants or it will be
7158 -- a discriminated unconstrained base type.
7159
7160 -- STEP 1a: perform preliminary actions/checks for derived tagged types
7161
7162 if Is_Tagged then
71d9e9f2 7163
996ae0b0 7164 -- The parent type is frozen for non-private extensions (RM 13.14(7))
88b32fc3
BD
7165 -- The declaration of a specific descendant of an interface type
7166 -- freezes the interface type (RM 13.14).
996ae0b0 7167
47d3b920 7168 if not Private_Extension or else Is_Interface (Parent_Base) then
996ae0b0
RK
7169 Freeze_Before (N, Parent_Type);
7170 end if;
7171
758c442c
GD
7172 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
7173 -- cannot be declared at a deeper level than its parent type is
7174 -- removed. The check on derivation within a generic body is also
7175 -- relaxed, but there's a restriction that a derived tagged type
7176 -- cannot be declared in a generic body if it's derived directly
7177 -- or indirectly from a formal type of that generic.
7178
0791fbe9 7179 if Ada_Version >= Ada_2005 then
758c442c
GD
7180 if Present (Enclosing_Generic_Body (Derived_Type)) then
7181 declare
9dfd2ff8 7182 Ancestor_Type : Entity_Id;
758c442c
GD
7183
7184 begin
7185 -- Check to see if any ancestor of the derived type is a
7186 -- formal type.
7187
9dfd2ff8 7188 Ancestor_Type := Parent_Type;
758c442c
GD
7189 while not Is_Generic_Type (Ancestor_Type)
7190 and then Etype (Ancestor_Type) /= Ancestor_Type
7191 loop
7192 Ancestor_Type := Etype (Ancestor_Type);
7193 end loop;
7194
7195 -- If the derived type does have a formal type as an
7196 -- ancestor, then it's an error if the derived type is
7197 -- declared within the body of the generic unit that
7198 -- declares the formal type in its generic formal part. It's
7199 -- sufficient to check whether the ancestor type is declared
7200 -- inside the same generic body as the derived type (such as
7201 -- within a nested generic spec), in which case the
7202 -- derivation is legal. If the formal type is declared
7203 -- outside of that generic body, then it's guaranteed that
7204 -- the derived type is declared within the generic body of
7205 -- the generic unit declaring the formal type.
7206
7207 if Is_Generic_Type (Ancestor_Type)
7208 and then Enclosing_Generic_Body (Ancestor_Type) /=
7209 Enclosing_Generic_Body (Derived_Type)
7210 then
7211 Error_Msg_NE
7212 ("parent type of& must not be descendant of formal type"
7213 & " of an enclosing generic body",
7214 Indic, Derived_Type);
7215 end if;
7216 end;
7217 end if;
7218
7219 elsif Type_Access_Level (Derived_Type) /=
7220 Type_Access_Level (Parent_Type)
996ae0b0
RK
7221 and then not Is_Generic_Type (Derived_Type)
7222 then
7223 if Is_Controlled (Parent_Type) then
7224 Error_Msg_N
7225 ("controlled type must be declared at the library level",
7226 Indic);
7227 else
7228 Error_Msg_N
7229 ("type extension at deeper accessibility level than parent",
7230 Indic);
7231 end if;
7232
7233 else
7234 declare
7235 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
7236
7237 begin
7238 if Present (GB)
7239 and then GB /= Enclosing_Generic_Body (Parent_Base)
7240 then
fbf5a39b
AC
7241 Error_Msg_NE
7242 ("parent type of& must not be outside generic body"
dc06abec 7243 & " (RM 3.9.1(4))",
fbf5a39b 7244 Indic, Derived_Type);
996ae0b0
RK
7245 end if;
7246 end;
7247 end if;
7248 end if;
7249
758c442c
GD
7250 -- Ada 2005 (AI-251)
7251
0791fbe9 7252 if Ada_Version >= Ada_2005 and then Is_Tagged then
946db1e2 7253
758c442c
GD
7254 -- "The declaration of a specific descendant of an interface type
7255 -- freezes the interface type" (RM 13.14).
7256
7257 declare
7258 Iface : Node_Id;
7259 begin
7260 if Is_Non_Empty_List (Interface_List (Type_Def)) then
7261 Iface := First (Interface_List (Type_Def));
758c442c
GD
7262 while Present (Iface) loop
7263 Freeze_Before (N, Etype (Iface));
7264 Next (Iface);
7265 end loop;
7266 end if;
7267 end;
7268 end if;
7269
996ae0b0
RK
7270 -- STEP 1b : preliminary cleanup of the full view of private types
7271
7272 -- If the type is already marked as having discriminants, then it's the
7273 -- completion of a private type or private extension and we need to
7274 -- retain the discriminants from the partial view if the current
7275 -- declaration has Discriminant_Specifications so that we can verify
7276 -- conformance. However, we must remove any existing components that
fbf5a39b 7277 -- were inherited from the parent (and attached in Copy_And_Swap)
996ae0b0 7278 -- because the full type inherits all appropriate components anyway, and
71d9e9f2 7279 -- we do not want the partial view's components interfering.
996ae0b0
RK
7280
7281 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
7282 Discrim := First_Discriminant (Derived_Type);
7283 loop
7284 Last_Discrim := Discrim;
7285 Next_Discriminant (Discrim);
7286 exit when No (Discrim);
7287 end loop;
7288
7289 Set_Last_Entity (Derived_Type, Last_Discrim);
7290
7291 -- In all other cases wipe out the list of inherited components (even
7292 -- inherited discriminants), it will be properly rebuilt here.
7293
7294 else
7295 Set_First_Entity (Derived_Type, Empty);
7296 Set_Last_Entity (Derived_Type, Empty);
7297 end if;
7298
7299 -- STEP 1c: Initialize some flags for the Derived_Type
7300
7301 -- The following flags must be initialized here so that
88b32fc3
BD
7302 -- Process_Discriminants can check that discriminants of tagged types do
7303 -- not have a default initial value and that access discriminants are
7304 -- only specified for limited records. For completeness, these flags are
7305 -- also initialized along with all the other flags below.
996ae0b0 7306
88b32fc3
BD
7307 -- AI-419: Limitedness is not inherited from an interface parent, so to
7308 -- be limited in that case the type must be explicitly declared as
dc06abec 7309 -- limited. However, task and protected interfaces are always limited.
653da906 7310
dc06abec
RD
7311 if Limited_Present (Type_Def) then
7312 Set_Is_Limited_Record (Derived_Type);
7313
ce4a6e84
RD
7314 elsif Is_Limited_Record (Parent_Type)
7315 or else (Present (Full_View (Parent_Type))
7316 and then Is_Limited_Record (Full_View (Parent_Type)))
7317 then
dc06abec
RD
7318 if not Is_Interface (Parent_Type)
7319 or else Is_Synchronized_Interface (Parent_Type)
7320 or else Is_Protected_Interface (Parent_Type)
7321 or else Is_Task_Interface (Parent_Type)
7322 then
7323 Set_Is_Limited_Record (Derived_Type);
7324 end if;
7325 end if;
996ae0b0 7326
71d9e9f2 7327 -- STEP 2a: process discriminants of derived type if any
996ae0b0 7328
2b73cf68 7329 Push_Scope (Derived_Type);
996ae0b0
RK
7330
7331 if Discriminant_Specs then
7332 Set_Has_Unknown_Discriminants (Derived_Type, False);
7333
7334 -- The following call initializes fields Has_Discriminants and
7335 -- Discriminant_Constraint, unless we are processing the completion
7336 -- of a private type declaration.
7337
7338 Check_Or_Process_Discriminants (N, Derived_Type);
7339
dd386db0 7340 -- For untagged types, the constraint on the Parent_Type must be
996ae0b0
RK
7341 -- present and is used to rename the discriminants.
7342
7343 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
7344 Error_Msg_N ("untagged parent must have discriminants", Indic);
7345
7346 elsif not Is_Tagged and then not Constraint_Present then
7347 Error_Msg_N
7348 ("discriminant constraint needed for derived untagged records",
7349 Indic);
7350
7351 -- Otherwise the parent subtype must be constrained unless we have a
7352 -- private extension.
7353
7354 elsif not Constraint_Present
7355 and then not Private_Extension
7356 and then not Is_Constrained (Parent_Type)
7357 then
7358 Error_Msg_N
7359 ("unconstrained type not allowed in this context", Indic);
7360
7361 elsif Constraint_Present then
7362 -- The following call sets the field Corresponding_Discriminant
7363 -- for the discriminants in the Derived_Type.
7364
7365 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
7366
7367 -- For untagged types all new discriminants must rename
7368 -- discriminants in the parent. For private extensions new
7369 -- discriminants cannot rename old ones (implied by [7.3(13)]).
7370
7371 Discrim := First_Discriminant (Derived_Type);
996ae0b0
RK
7372 while Present (Discrim) loop
7373 if not Is_Tagged
57193e09 7374 and then No (Corresponding_Discriminant (Discrim))
996ae0b0
RK
7375 then
7376 Error_Msg_N
7377 ("new discriminants must constrain old ones", Discrim);
7378
7379 elsif Private_Extension
7380 and then Present (Corresponding_Discriminant (Discrim))
7381 then
7382 Error_Msg_N
fbf5a39b 7383 ("only static constraints allowed for parent"
996ae0b0 7384 & " discriminants in the partial view", Indic);
996ae0b0
RK
7385 exit;
7386 end if;
7387
a5b62485
AC
7388 -- If a new discriminant is used in the constraint, then its
7389 -- subtype must be statically compatible with the parent
7390 -- discriminant's subtype (3.7(15)).
996ae0b0
RK
7391
7392 if Present (Corresponding_Discriminant (Discrim))
7393 and then
7394 not Subtypes_Statically_Compatible
7395 (Etype (Discrim),
7396 Etype (Corresponding_Discriminant (Discrim)))
7397 then
7398 Error_Msg_N
7399 ("subtype must be compatible with parent discriminant",
7400 Discrim);
7401 end if;
7402
7403 Next_Discriminant (Discrim);
7404 end loop;
0da2c8ac
AC
7405
7406 -- Check whether the constraints of the full view statically
7407 -- match those imposed by the parent subtype [7.3(13)].
7408
7409 if Present (Stored_Constraint (Derived_Type)) then
7410 declare
7411 C1, C2 : Elmt_Id;
7412
7413 begin
7414 C1 := First_Elmt (Discs);
7415 C2 := First_Elmt (Stored_Constraint (Derived_Type));
7416 while Present (C1) and then Present (C2) loop
7417 if not
7418 Fully_Conformant_Expressions (Node (C1), Node (C2))
7419 then
88b32fc3
BD
7420 Error_Msg_N
7421 ("not conformant with previous declaration",
7422 Node (C1));
0da2c8ac
AC
7423 end if;
7424
7425 Next_Elmt (C1);
7426 Next_Elmt (C2);
7427 end loop;
7428 end;
7429 end if;
996ae0b0
RK
7430 end if;
7431
7432 -- STEP 2b: No new discriminants, inherit discriminants if any
7433
7434 else
7435 if Private_Extension then
7436 Set_Has_Unknown_Discriminants
0da2c8ac
AC
7437 (Derived_Type,
7438 Has_Unknown_Discriminants (Parent_Type)
7439 or else Unknown_Discriminants_Present (N));
8a6a52dc
AC
7440
7441 -- The partial view of the parent may have unknown discriminants,
7442 -- but if the full view has discriminants and the parent type is
7443 -- in scope they must be inherited.
7444
7445 elsif Has_Unknown_Discriminants (Parent_Type)
7446 and then
7447 (not Has_Discriminants (Parent_Type)
7448 or else not In_Open_Scopes (Scope (Parent_Type)))
7449 then
7450 Set_Has_Unknown_Discriminants (Derived_Type);
996ae0b0
RK
7451 end if;
7452
7453 if not Has_Unknown_Discriminants (Derived_Type)
ffe9aba8 7454 and then not Has_Unknown_Discriminants (Parent_Base)
996ae0b0
RK
7455 and then Has_Discriminants (Parent_Type)
7456 then
7457 Inherit_Discrims := True;
7458 Set_Has_Discriminants
7459 (Derived_Type, True);
7460 Set_Discriminant_Constraint
7461 (Derived_Type, Discriminant_Constraint (Parent_Base));
7462 end if;
7463
7464 -- The following test is true for private types (remember
7465 -- transformation 5. is not applied to those) and in an error
7466 -- situation.
7467
7468 if Constraint_Present then
7469 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
7470 end if;
7471
fbf5a39b 7472 -- For now mark a new derived type as constrained only if it has no
996ae0b0
RK
7473 -- discriminants. At the end of Build_Derived_Record_Type we properly
7474 -- set this flag in the case of private extensions. See comments in
7475 -- point 9. just before body of Build_Derived_Record_Type.
7476
7477 Set_Is_Constrained
7478 (Derived_Type,
7479 not (Inherit_Discrims
71d9e9f2 7480 or else Has_Unknown_Discriminants (Derived_Type)));
996ae0b0
RK
7481 end if;
7482
ffe9aba8 7483 -- STEP 3: initialize fields of derived type
996ae0b0
RK
7484
7485 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
fbf5a39b 7486 Set_Stored_Constraint (Derived_Type, No_Elist);
996ae0b0 7487
758c442c
GD
7488 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
7489 -- but cannot be interfaces
7490
7491 if not Private_Extension
7492 and then Ekind (Derived_Type) /= E_Private_Type
7493 and then Ekind (Derived_Type) /= E_Limited_Private_Type
7494 then
fea9e956
ES
7495 if Interface_Present (Type_Def) then
7496 Analyze_Interface_Declaration (Derived_Type, Type_Def);
7497 end if;
7498
ce2b6ba5 7499 Set_Interfaces (Derived_Type, No_Elist);
758c442c
GD
7500 end if;
7501
996ae0b0
RK
7502 -- Fields inherited from the Parent_Type
7503
7504 Set_Discard_Names
b603e37b 7505 (Derived_Type, Einfo.Discard_Names (Parent_Type));
996ae0b0 7506 Set_Has_Specified_Layout
b603e37b 7507 (Derived_Type, Has_Specified_Layout (Parent_Type));
996ae0b0 7508 Set_Is_Limited_Composite
b603e37b 7509 (Derived_Type, Is_Limited_Composite (Parent_Type));
996ae0b0 7510 Set_Is_Private_Composite
b603e37b 7511 (Derived_Type, Is_Private_Composite (Parent_Type));
996ae0b0
RK
7512
7513 -- Fields inherited from the Parent_Base
7514
7515 Set_Has_Controlled_Component
7516 (Derived_Type, Has_Controlled_Component (Parent_Base));
7517 Set_Has_Non_Standard_Rep
7518 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7519 Set_Has_Primitive_Operations
7520 (Derived_Type, Has_Primitive_Operations (Parent_Base));
7521
df89ab66 7522 -- Fields inherited from the Parent_Base in the non-private case
c6fe3827
GD
7523
7524 if Ekind (Derived_Type) = E_Record_Type then
7525 Set_Has_Complex_Representation
7526 (Derived_Type, Has_Complex_Representation (Parent_Base));
7527 end if;
7528
df89ab66
ES
7529 -- Fields inherited from the Parent_Base for record types
7530
7531 if Is_Record_Type (Derived_Type) then
b603e37b
AC
7532
7533 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7534 -- Parent_Base can be a private type or private extension.
7535
7536 if Present (Full_View (Parent_Base)) then
7537 Set_OK_To_Reorder_Components
7538 (Derived_Type,
7539 OK_To_Reorder_Components (Full_View (Parent_Base)));
7540 Set_Reverse_Bit_Order
7541 (Derived_Type, Reverse_Bit_Order (Full_View (Parent_Base)));
7542 else
7543 Set_OK_To_Reorder_Components
7544 (Derived_Type, OK_To_Reorder_Components (Parent_Base));
7545 Set_Reverse_Bit_Order
7546 (Derived_Type, Reverse_Bit_Order (Parent_Base));
7547 end if;
df89ab66
ES
7548 end if;
7549
07fc65c4 7550 -- Direct controlled types do not inherit Finalize_Storage_Only flag
996ae0b0 7551
c6fe3827 7552 if not Is_Controlled (Parent_Type) then
07fc65c4
GB
7553 Set_Finalize_Storage_Only
7554 (Derived_Type, Finalize_Storage_Only (Parent_Type));
996ae0b0
RK
7555 end if;
7556
ffe9aba8 7557 -- Set fields for private derived types
996ae0b0
RK
7558
7559 if Is_Private_Type (Derived_Type) then
7560 Set_Depends_On_Private (Derived_Type, True);
7561 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7562
7563 -- Inherit fields from non private record types. If this is the
7564 -- completion of a derivation from a private type, the parent itself
7565 -- is private, and the attributes come from its full view, which must
7566 -- be present.
7567
7568 else
7569 if Is_Private_Type (Parent_Base)
7570 and then not Is_Record_Type (Parent_Base)
7571 then
7572 Set_Component_Alignment
7573 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
7574 Set_C_Pass_By_Copy
7575 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
7576 else
7577 Set_Component_Alignment
7578 (Derived_Type, Component_Alignment (Parent_Base));
996ae0b0
RK
7579 Set_C_Pass_By_Copy
7580 (Derived_Type, C_Pass_By_Copy (Parent_Base));
7581 end if;
7582 end if;
7583
fbf5a39b 7584 -- Set fields for tagged types
996ae0b0
RK
7585
7586 if Is_Tagged then
ef2a63ba 7587 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
996ae0b0
RK
7588
7589 -- All tagged types defined in Ada.Finalization are controlled
7590
7591 if Chars (Scope (Derived_Type)) = Name_Finalization
7592 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
7593 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
7594 then
7595 Set_Is_Controlled (Derived_Type);
7596 else
7597 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
7598 end if;
7599
c206e8fd
AC
7600 -- Minor optimization: there is no need to generate the class-wide
7601 -- entity associated with an underlying record view.
9013065b
AC
7602
7603 if not Is_Underlying_Record_View (Derived_Type) then
7604 Make_Class_Wide_Type (Derived_Type);
7605 end if;
7606
fea9e956 7607 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
996ae0b0
RK
7608
7609 if Has_Discriminants (Derived_Type)
7610 and then Constraint_Present
7611 then
fbf5a39b
AC
7612 Set_Stored_Constraint
7613 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
996ae0b0
RK
7614 end if;
7615
0791fbe9 7616 if Ada_Version >= Ada_2005 then
88b32fc3
BD
7617 declare
7618 Ifaces_List : Elist_Id;
c6fe3827 7619
88b32fc3 7620 begin
c6fe3827
GD
7621 -- Checks rules 3.9.4 (13/2 and 14/2)
7622
7623 if Comes_From_Source (Derived_Type)
7624 and then not Is_Private_Type (Derived_Type)
7625 and then Is_Interface (Parent_Type)
7626 and then not Is_Interface (Derived_Type)
7627 then
7628 if Is_Task_Interface (Parent_Type) then
7629 Error_Msg_N
7630 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
7631 Derived_Type);
7632
7633 elsif Is_Protected_Interface (Parent_Type) then
7634 Error_Msg_N
7635 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
7636 Derived_Type);
7637 end if;
7638 end if;
7639
fea9e956
ES
7640 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
7641
ce2b6ba5 7642 Check_Interfaces (N, Type_Def);
fea9e956
ES
7643
7644 -- Ada 2005 (AI-251): Collect the list of progenitors that are
7645 -- not already in the parents.
7646
ce2b6ba5
JM
7647 Collect_Interfaces
7648 (T => Derived_Type,
7649 Ifaces_List => Ifaces_List,
7650 Exclude_Parents => True);
7651
7652 Set_Interfaces (Derived_Type, Ifaces_List);
7cec010e
ES
7653
7654 -- If the derived type is the anonymous type created for
7655 -- a declaration whose parent has a constraint, propagate
7656 -- the interface list to the source type. This must be done
7657 -- prior to the completion of the analysis of the source type
7658 -- because the components in the extension may contain current
7659 -- instances whose legality depends on some ancestor.
7660
7661 if Is_Itype (Derived_Type) then
7662 declare
7663 Def : constant Node_Id :=
7664 Associated_Node_For_Itype (Derived_Type);
7665 begin
7666 if Present (Def)
7667 and then Nkind (Def) = N_Full_Type_Declaration
7668 then
7669 Set_Interfaces
7670 (Defining_Identifier (Def), Ifaces_List);
7671 end if;
7672 end;
7673 end if;
88b32fc3 7674 end;
758c442c
GD
7675 end if;
7676
996ae0b0
RK
7677 else
7678 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
7679 Set_Has_Non_Standard_Rep
7680 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7681 end if;
7682
7683 -- STEP 4: Inherit components from the parent base and constrain them.
7684 -- Apply the second transformation described in point 6. above.
7685
7686 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
7687 or else not Has_Discriminants (Parent_Type)
7688 or else not Is_Constrained (Parent_Type)
7689 then
7690 Constrs := Discs;
7691 else
7692 Constrs := Discriminant_Constraint (Parent_Type);
7693 end if;
7694
57193e09
TQ
7695 Assoc_List :=
7696 Inherit_Components
7697 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
996ae0b0
RK
7698
7699 -- STEP 5a: Copy the parent record declaration for untagged types
7700
7701 if not Is_Tagged then
7702
7703 -- Discriminant_Constraint (Derived_Type) has been properly
71d9e9f2
ES
7704 -- constructed. Save it and temporarily set it to Empty because we
7705 -- do not want the call to New_Copy_Tree below to mess this list.
996ae0b0
RK
7706
7707 if Has_Discriminants (Derived_Type) then
7708 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7709 Set_Discriminant_Constraint (Derived_Type, No_Elist);
7710 else
7711 Save_Discr_Constr := No_Elist;
7712 end if;
7713
71d9e9f2
ES
7714 -- Save the Etype field of Derived_Type. It is correctly set now,
7715 -- but the call to New_Copy tree may remap it to point to itself,
7716 -- which is not what we want. Ditto for the Next_Entity field.
996ae0b0
RK
7717
7718 Save_Etype := Etype (Derived_Type);
7719 Save_Next_Entity := Next_Entity (Derived_Type);
7720
fbf5a39b
AC
7721 -- Assoc_List maps all stored discriminants in the Parent_Base to
7722 -- stored discriminants in the Derived_Type. It is fundamental that
7723 -- no types or itypes with discriminants other than the stored
996ae0b0 7724 -- discriminants appear in the entities declared inside
71d9e9f2 7725 -- Derived_Type, since the back end cannot deal with it.
996ae0b0
RK
7726
7727 New_Decl :=
7728 New_Copy_Tree
7729 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7730
7731 -- Restore the fields saved prior to the New_Copy_Tree call
fbf5a39b 7732 -- and compute the stored constraint.
996ae0b0
RK
7733
7734 Set_Etype (Derived_Type, Save_Etype);
7735 Set_Next_Entity (Derived_Type, Save_Next_Entity);
7736
7737 if Has_Discriminants (Derived_Type) then
7738 Set_Discriminant_Constraint
7739 (Derived_Type, Save_Discr_Constr);
fbf5a39b 7740 Set_Stored_Constraint
30c20106 7741 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
07fc65c4 7742 Replace_Components (Derived_Type, New_Decl);
996ae0b0
RK
7743 end if;
7744
7745 -- Insert the new derived type declaration
7746
7747 Rewrite (N, New_Decl);
7748
7749 -- STEP 5b: Complete the processing for record extensions in generics
7750
7751 -- There is no completion for record extensions declared in the
7752 -- parameter part of a generic, so we need to complete processing for
fbf5a39b
AC
7753 -- these generic record extensions here. The Record_Type_Definition call
7754 -- will change the Ekind of the components from E_Void to E_Component.
996ae0b0
RK
7755
7756 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
7757 Record_Type_Definition (Empty, Derived_Type);
7758
c885d7a1 7759 -- STEP 5c: Process the record extension for non private tagged types
996ae0b0
RK
7760
7761 elsif not Private_Extension then
996ae0b0 7762
c885d7a1
AC
7763 -- Add the _parent field in the derived type
7764
7765 Expand_Record_Extension (Derived_Type, Type_Def);
996ae0b0 7766
758c442c
GD
7767 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
7768 -- implemented interfaces if we are in expansion mode
7769
fea9e956 7770 if Expander_Active
ce2b6ba5 7771 and then Has_Interfaces (Derived_Type)
fea9e956 7772 then
758c442c
GD
7773 Add_Interface_Tag_Components (N, Derived_Type);
7774 end if;
7775
996ae0b0
RK
7776 -- Analyze the record extension
7777
7778 Record_Type_Definition
7779 (Record_Extension_Part (Type_Def), Derived_Type);
7780 end if;
7781
7782 End_Scope;
7783
88b32fc3
BD
7784 -- Nothing else to do if there is an error in the derivation.
7785 -- An unusual case: the full view may be derived from a type in an
7786 -- instance, when the partial view was used illegally as an actual
7787 -- in that instance, leading to a circular definition.
7788
7789 if Etype (Derived_Type) = Any_Type
7790 or else Etype (Parent_Type) = Derived_Type
7791 then
996ae0b0
RK
7792 return;
7793 end if;
7794
7795 -- Set delayed freeze and then derive subprograms, we need to do
7796 -- this in this order so that derived subprograms inherit the
7797 -- derived freeze if necessary.
7798
7799 Set_Has_Delayed_Freeze (Derived_Type);
758c442c 7800
996ae0b0 7801 if Derive_Subps then
88b32fc3
BD
7802 Derive_Subprograms (Parent_Type, Derived_Type);
7803 end if;
758c442c 7804
88b32fc3
BD
7805 -- If we have a private extension which defines a constrained derived
7806 -- type mark as constrained here after we have derived subprograms. See
7807 -- comment on point 9. just above the body of Build_Derived_Record_Type.
758c442c 7808
88b32fc3
BD
7809 if Private_Extension and then Inherit_Discrims then
7810 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7811 Set_Is_Constrained (Derived_Type, True);
7812 Set_Discriminant_Constraint (Derived_Type, Discs);
758c442c 7813
88b32fc3
BD
7814 elsif Is_Constrained (Parent_Type) then
7815 Set_Is_Constrained
7816 (Derived_Type, True);
7817 Set_Discriminant_Constraint
7818 (Derived_Type, Discriminant_Constraint (Parent_Type));
7819 end if;
7820 end if;
950d3e7d 7821
c206e8fd
AC
7822 -- Update the class-wide type, which shares the now-completed entity
7823 -- list with its specific type. In case of underlying record views,
9013065b 7824 -- we do not generate the corresponding class wide entity.
950d3e7d 7825
9013065b
AC
7826 if Is_Tagged
7827 and then not Is_Underlying_Record_View (Derived_Type)
7828 then
88b32fc3
BD
7829 Set_First_Entity
7830 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7831 Set_Last_Entity
7832 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7833 end if;
758c442c 7834
2b73cf68
JM
7835 -- Update the scope of anonymous access types of discriminants and other
7836 -- components, to prevent scope anomalies in gigi, when the derivation
7837 -- appears in a scope nested within that of the parent.
7838
7839 declare
7840 D : Entity_Id;
7841
7842 begin
7843 D := First_Entity (Derived_Type);
7844 while Present (D) loop
bce79204 7845 if Ekind_In (D, E_Discriminant, E_Component) then
2b73cf68
JM
7846 if Is_Itype (Etype (D))
7847 and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7848 then
7849 Set_Scope (Etype (D), Current_Scope);
7850 end if;
7851 end if;
7852
7853 Next_Entity (D);
7854 end loop;
7855 end;
88b32fc3 7856 end Build_Derived_Record_Type;
996ae0b0
RK
7857
7858 ------------------------
7859 -- Build_Derived_Type --
7860 ------------------------
7861
7862 procedure Build_Derived_Type
7863 (N : Node_Id;
7864 Parent_Type : Entity_Id;
7865 Derived_Type : Entity_Id;
7866 Is_Completion : Boolean;
7867 Derive_Subps : Boolean := True)
7868 is
7869 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7870
7871 begin
7872 -- Set common attributes
7873
c6fe3827 7874 Set_Scope (Derived_Type, Current_Scope);
996ae0b0 7875
c6fe3827
GD
7876 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7877 Set_Etype (Derived_Type, Parent_Base);
7878 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
996ae0b0 7879
93bcda23
AC
7880 Set_Size_Info (Derived_Type, Parent_Type);
7881 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
7882 Set_Convention (Derived_Type, Convention (Parent_Type));
7883 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7884 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
fbf5a39b 7885
e606088a
AC
7886 -- Propagate invariant information. The new type has invariants if
7887 -- they are inherited from the parent type, and these invariants can
7888 -- be further inherited, so both flags are set.
7889
7890 if Has_Inheritable_Invariants (Parent_Type) then
7891 Set_Has_Inheritable_Invariants (Derived_Type);
7892 Set_Has_Invariants (Derived_Type);
7893 end if;
7894
4818e7b9
RD
7895 -- We similarly inherit predicates
7896
7897 if Has_Predicates (Parent_Type) then
7898 Set_Has_Predicates (Derived_Type);
7899 end if;
7900
fbf5a39b
AC
7901 -- The derived type inherits the representation clauses of the parent.
7902 -- However, for a private type that is completed by a derivation, there
7903 -- may be operation attributes that have been specified already (stream
7904 -- attributes and External_Tag) and those must be provided. Finally,
7905 -- if the partial view is a private extension, the representation items
7906 -- of the parent have been inherited already, and should not be chained
7907 -- twice to the derived type.
7908
7909 if Is_Tagged_Type (Parent_Type)
7910 and then Present (First_Rep_Item (Derived_Type))
7911 then
7912 -- The existing items are either operational items or items inherited
7913 -- from a private extension declaration.
7914
7915 declare
dc06abec
RD
7916 Rep : Node_Id;
7917 -- Used to iterate over representation items of the derived type
7918
7919 Last_Rep : Node_Id;
7920 -- Last representation item of the (non-empty) representation
7921 -- item list of the derived type.
7922
fbf5a39b
AC
7923 Found : Boolean := False;
7924
7925 begin
dc06abec
RD
7926 Rep := First_Rep_Item (Derived_Type);
7927 Last_Rep := Rep;
fbf5a39b
AC
7928 while Present (Rep) loop
7929 if Rep = First_Rep_Item (Parent_Type) then
7930 Found := True;
7931 exit;
dc06abec 7932
fbf5a39b
AC
7933 else
7934 Rep := Next_Rep_Item (Rep);
dc06abec
RD
7935
7936 if Present (Rep) then
7937 Last_Rep := Rep;
7938 end if;
fbf5a39b
AC
7939 end if;
7940 end loop;
7941
dc06abec
RD
7942 -- Here if we either encountered the parent type's first rep
7943 -- item on the derived type's rep item list (in which case
7944 -- Found is True, and we have nothing else to do), or if we
7945 -- reached the last rep item of the derived type, which is
7946 -- Last_Rep, in which case we further chain the parent type's
7947 -- rep items to those of the derived type.
7948
fbf5a39b 7949 if not Found then
dc06abec 7950 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
fbf5a39b
AC
7951 end if;
7952 end;
7953
7954 else
7955 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7956 end if;
996ae0b0
RK
7957
7958 case Ekind (Parent_Type) is
7959 when Numeric_Kind =>
7960 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7961
7962 when Array_Kind =>
7963 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
7964
7965 when E_Record_Type
7966 | E_Record_Subtype
7967 | Class_Wide_Kind =>
7968 Build_Derived_Record_Type
7969 (N, Parent_Type, Derived_Type, Derive_Subps);
7970 return;
7971
7972 when Enumeration_Kind =>
7973 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7974
7975 when Access_Kind =>
7976 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7977
7978 when Incomplete_Or_Private_Kind =>
7979 Build_Derived_Private_Type
7980 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
7981
7982 -- For discriminated types, the derivation includes deriving
7983 -- primitive operations. For others it is done below.
7984
7985 if Is_Tagged_Type (Parent_Type)
7986 or else Has_Discriminants (Parent_Type)
7987 or else (Present (Full_View (Parent_Type))
7988 and then Has_Discriminants (Full_View (Parent_Type)))
7989 then
7990 return;
7991 end if;
7992
7993 when Concurrent_Kind =>
7994 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
7995
7996 when others =>
7997 raise Program_Error;
7998 end case;
7999
8000 if Etype (Derived_Type) = Any_Type then
8001 return;
8002 end if;
8003
a5b62485
AC
8004 -- Set delayed freeze and then derive subprograms, we need to do this
8005 -- in this order so that derived subprograms inherit the derived freeze
8006 -- if necessary.
996ae0b0
RK
8007
8008 Set_Has_Delayed_Freeze (Derived_Type);
8009 if Derive_Subps then
8010 Derive_Subprograms (Parent_Type, Derived_Type);
8011 end if;
8012
8013 Set_Has_Primitive_Operations
8014 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
8015 end Build_Derived_Type;
8016
8017 -----------------------
8018 -- Build_Discriminal --
8019 -----------------------
8020
8021 procedure Build_Discriminal (Discrim : Entity_Id) is
8022 D_Minal : Entity_Id;
8023 CR_Disc : Entity_Id;
8024
8025 begin
71d9e9f2 8026 -- A discriminal has the same name as the discriminant
996ae0b0 8027
7675ad4f 8028 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
996ae0b0
RK
8029
8030 Set_Ekind (D_Minal, E_In_Parameter);
8031 Set_Mechanism (D_Minal, Default_Mechanism);
8032 Set_Etype (D_Minal, Etype (Discrim));
f0d10385 8033 Set_Scope (D_Minal, Current_Scope);
996ae0b0
RK
8034
8035 Set_Discriminal (Discrim, D_Minal);
8036 Set_Discriminal_Link (D_Minal, Discrim);
8037
8038 -- For task types, build at once the discriminants of the corresponding
8039 -- record, which are needed if discriminants are used in entry defaults
8040 -- and in family bounds.
8041
8042 if Is_Concurrent_Type (Current_Scope)
8043 or else Is_Limited_Type (Current_Scope)
8044 then
8045 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8046
950d3e7d
ES
8047 Set_Ekind (CR_Disc, E_In_Parameter);
8048 Set_Mechanism (CR_Disc, Default_Mechanism);
8049 Set_Etype (CR_Disc, Etype (Discrim));
f0d10385 8050 Set_Scope (CR_Disc, Current_Scope);
950d3e7d
ES
8051 Set_Discriminal_Link (CR_Disc, Discrim);
8052 Set_CR_Discriminant (Discrim, CR_Disc);
996ae0b0
RK
8053 end if;
8054 end Build_Discriminal;
8055
8056 ------------------------------------
8057 -- Build_Discriminant_Constraints --
8058 ------------------------------------
8059
8060 function Build_Discriminant_Constraints
8061 (T : Entity_Id;
8062 Def : Node_Id;
b0f26df5 8063 Derived_Def : Boolean := False) return Elist_Id
996ae0b0 8064 is
71d9e9f2
ES
8065 C : constant Node_Id := Constraint (Def);
8066 Nb_Discr : constant Nat := Number_Discriminants (T);
8067
996ae0b0 8068 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
71d9e9f2 8069 -- Saves the expression corresponding to a given discriminant in T
996ae0b0
RK
8070
8071 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
8072 -- Return the Position number within array Discr_Expr of a discriminant
8073 -- D within the discriminant list of the discriminated type T.
8074
8075 ------------------
8076 -- Pos_Of_Discr --
8077 ------------------
8078
8079 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
8080 Disc : Entity_Id;
8081
8082 begin
8083 Disc := First_Discriminant (T);
8084 for J in Discr_Expr'Range loop
8085 if Disc = D then
8086 return J;
8087 end if;
8088
8089 Next_Discriminant (Disc);
8090 end loop;
8091
8092 -- Note: Since this function is called on discriminants that are
8093 -- known to belong to the discriminated type, falling through the
8094 -- loop with no match signals an internal compiler error.
8095
8096 raise Program_Error;
8097 end Pos_Of_Discr;
8098
fbf5a39b 8099 -- Declarations local to Build_Discriminant_Constraints
996ae0b0
RK
8100
8101 Discr : Entity_Id;
8102 E : Entity_Id;
fbf5a39b 8103 Elist : constant Elist_Id := New_Elmt_List;
996ae0b0 8104
71d9e9f2
ES
8105 Constr : Node_Id;
8106 Expr : Node_Id;
8107 Id : Node_Id;
8108 Position : Nat;
8109 Found : Boolean;
996ae0b0
RK
8110
8111 Discrim_Present : Boolean := False;
8112
8113 -- Start of processing for Build_Discriminant_Constraints
8114
8115 begin
8116 -- The following loop will process positional associations only.
8117 -- For a positional association, the (single) discriminant is
8118 -- implicitly specified by position, in textual order (RM 3.7.2).
8119
8120 Discr := First_Discriminant (T);
8121 Constr := First (Constraints (C));
996ae0b0
RK
8122 for D in Discr_Expr'Range loop
8123 exit when Nkind (Constr) = N_Discriminant_Association;
8124
8125 if No (Constr) then
8126 Error_Msg_N ("too few discriminants given in constraint", C);
8127 return New_Elmt_List;
8128
8129 elsif Nkind (Constr) = N_Range
8130 or else (Nkind (Constr) = N_Attribute_Reference
8131 and then
8132 Attribute_Name (Constr) = Name_Range)
8133 then
8134 Error_Msg_N
8135 ("a range is not a valid discriminant constraint", Constr);
8136 Discr_Expr (D) := Error;
8137
8138 else
8139 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
8140 Discr_Expr (D) := Constr;
8141 end if;
8142
8143 Next_Discriminant (Discr);
8144 Next (Constr);
8145 end loop;
8146
8147 if No (Discr) and then Present (Constr) then
8148 Error_Msg_N ("too many discriminants given in constraint", Constr);
8149 return New_Elmt_List;
8150 end if;
8151
8152 -- Named associations can be given in any order, but if both positional
8153 -- and named associations are used in the same discriminant constraint,
8154 -- then positional associations must occur first, at their normal
8155 -- position. Hence once a named association is used, the rest of the
8156 -- discriminant constraint must use only named associations.
8157
8158 while Present (Constr) loop
8159
ffe9aba8 8160 -- Positional association forbidden after a named association
996ae0b0
RK
8161
8162 if Nkind (Constr) /= N_Discriminant_Association then
8163 Error_Msg_N ("positional association follows named one", Constr);
8164 return New_Elmt_List;
8165
8166 -- Otherwise it is a named association
8167
8168 else
8169 -- E records the type of the discriminants in the named
8170 -- association. All the discriminants specified in the same name
8171 -- association must have the same type.
8172
8173 E := Empty;
8174
8175 -- Search the list of discriminants in T to see if the simple name
8176 -- given in the constraint matches any of them.
8177
8178 Id := First (Selector_Names (Constr));
8179 while Present (Id) loop
8180 Found := False;
8181
8182 -- If Original_Discriminant is present, we are processing a
8183 -- generic instantiation and this is an instance node. We need
8184 -- to find the name of the corresponding discriminant in the
8185 -- actual record type T and not the name of the discriminant in
8186 -- the generic formal. Example:
88b32fc3 8187
996ae0b0
RK
8188 -- generic
8189 -- type G (D : int) is private;
8190 -- package P is
8191 -- subtype W is G (D => 1);
8192 -- end package;
8193 -- type Rec (X : int) is record ... end record;
8194 -- package Q is new P (G => Rec);
88b32fc3 8195
996ae0b0
RK
8196 -- At the point of the instantiation, formal type G is Rec
8197 -- and therefore when reanalyzing "subtype W is G (D => 1);"
8198 -- which really looks like "subtype W is Rec (D => 1);" at
8199 -- the point of instantiation, we want to find the discriminant
f3d57416 8200 -- that corresponds to D in Rec, i.e. X.
996ae0b0
RK
8201
8202 if Present (Original_Discriminant (Id)) then
8203 Discr := Find_Corresponding_Discriminant (Id, T);
8204 Found := True;
8205
8206 else
8207 Discr := First_Discriminant (T);
8208 while Present (Discr) loop
8209 if Chars (Discr) = Chars (Id) then
8210 Found := True;
8211 exit;
8212 end if;
8213
8214 Next_Discriminant (Discr);
8215 end loop;
8216
8217 if not Found then
8218 Error_Msg_N ("& does not match any discriminant", Id);
8219 return New_Elmt_List;
8220
8221 -- The following is only useful for the benefit of generic
8222 -- instances but it does not interfere with other
638e383e 8223 -- processing for the non-generic case so we do it in all
996ae0b0
RK
8224 -- cases (for generics this statement is executed when
8225 -- processing the generic definition, see comment at the
fbf5a39b 8226 -- beginning of this if statement).
996ae0b0
RK
8227
8228 else
8229 Set_Original_Discriminant (Id, Discr);
8230 end if;
8231 end if;
8232
8233 Position := Pos_Of_Discr (T, Discr);
8234
8235 if Present (Discr_Expr (Position)) then
8236 Error_Msg_N ("duplicate constraint for discriminant&", Id);
8237
8238 else
8239 -- Each discriminant specified in the same named association
8240 -- must be associated with a separate copy of the
8241 -- corresponding expression.
8242
8243 if Present (Next (Id)) then
8244 Expr := New_Copy_Tree (Expression (Constr));
8245 Set_Parent (Expr, Parent (Expression (Constr)));
8246 else
8247 Expr := Expression (Constr);
8248 end if;
8249
8250 Discr_Expr (Position) := Expr;
8251 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
8252 end if;
8253
8254 -- A discriminant association with more than one discriminant
8255 -- name is only allowed if the named discriminants are all of
8256 -- the same type (RM 3.7.1(8)).
8257
8258 if E = Empty then
8259 E := Base_Type (Etype (Discr));
8260
8261 elsif Base_Type (Etype (Discr)) /= E then
8262 Error_Msg_N
8263 ("all discriminants in an association " &
8264 "must have the same type", Id);
8265 end if;
8266
8267 Next (Id);
8268 end loop;
8269 end if;
8270
8271 Next (Constr);
8272 end loop;
8273
8274 -- A discriminant constraint must provide exactly one value for each
8275 -- discriminant of the type (RM 3.7.1(8)).
8276
8277 for J in Discr_Expr'Range loop
8278 if No (Discr_Expr (J)) then
8279 Error_Msg_N ("too few discriminants given in constraint", C);
8280 return New_Elmt_List;
8281 end if;
8282 end loop;
8283
ffe9aba8 8284 -- Determine if there are discriminant expressions in the constraint
996ae0b0
RK
8285
8286 for J in Discr_Expr'Range loop
88b32fc3
BD
8287 if Denotes_Discriminant
8288 (Discr_Expr (J), Check_Concurrent => True)
8289 then
996ae0b0
RK
8290 Discrim_Present := True;
8291 end if;
8292 end loop;
8293
8294 -- Build an element list consisting of the expressions given in the
2820d220
AC
8295 -- discriminant constraint and apply the appropriate checks. The list
8296 -- is constructed after resolving any named discriminant associations
8297 -- and therefore the expressions appear in the textual order of the
8298 -- discriminants.
996ae0b0
RK
8299
8300 Discr := First_Discriminant (T);
8301 for J in Discr_Expr'Range loop
8302 if Discr_Expr (J) /= Error then
996ae0b0
RK
8303 Append_Elmt (Discr_Expr (J), Elist);
8304
8305 -- If any of the discriminant constraints is given by a
8306 -- discriminant and we are in a derived type declaration we
8307 -- have a discriminant renaming. Establish link between new
8308 -- and old discriminant.
8309
8310 if Denotes_Discriminant (Discr_Expr (J)) then
8311 if Derived_Def then
8312 Set_Corresponding_Discriminant
8313 (Entity (Discr_Expr (J)), Discr);
8314 end if;
8315
8316 -- Force the evaluation of non-discriminant expressions.
8317 -- If we have found a discriminant in the constraint 3.4(26)
8318 -- and 3.8(18) demand that no range checks are performed are
fbf5a39b
AC
8319 -- after evaluation. If the constraint is for a component
8320 -- definition that has a per-object constraint, expressions are
8321 -- evaluated but not checked either. In all other cases perform
8322 -- a range check.
996ae0b0
RK
8323
8324 else
fbf5a39b
AC
8325 if Discrim_Present then
8326 null;
8327
a397db96 8328 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
fbf5a39b
AC
8329 and then
8330 Has_Per_Object_Constraint
a397db96 8331 (Defining_Identifier (Parent (Parent (Def))))
fbf5a39b
AC
8332 then
8333 null;
8334
2820d220
AC
8335 elsif Is_Access_Type (Etype (Discr)) then
8336 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
8337
fbf5a39b 8338 else
996ae0b0
RK
8339 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
8340 end if;
8341
8342 Force_Evaluation (Discr_Expr (J));
8343 end if;
8344
88b32fc3
BD
8345 -- Check that the designated type of an access discriminant's
8346 -- expression is not a class-wide type unless the discriminant's
8347 -- designated type is also class-wide.
996ae0b0
RK
8348
8349 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
8350 and then not Is_Class_Wide_Type
8351 (Designated_Type (Etype (Discr)))
8352 and then Etype (Discr_Expr (J)) /= Any_Type
8353 and then Is_Class_Wide_Type
8354 (Designated_Type (Etype (Discr_Expr (J))))
8355 then
8356 Wrong_Type (Discr_Expr (J), Etype (Discr));
49d8b802
ES
8357
8358 elsif Is_Access_Type (Etype (Discr))
8359 and then not Is_Access_Constant (Etype (Discr))
8360 and then Is_Access_Type (Etype (Discr_Expr (J)))
8361 and then Is_Access_Constant (Etype (Discr_Expr (J)))
8362 then
8363 Error_Msg_NE
8364 ("constraint for discriminant& must be access to variable",
8365 Def, Discr);
996ae0b0
RK
8366 end if;
8367 end if;
8368
8369 Next_Discriminant (Discr);
8370 end loop;
8371
8372 return Elist;
8373 end Build_Discriminant_Constraints;
8374
8375 ---------------------------------
8376 -- Build_Discriminated_Subtype --
8377 ---------------------------------
8378
8379 procedure Build_Discriminated_Subtype
8380 (T : Entity_Id;
8381 Def_Id : Entity_Id;
8382 Elist : Elist_Id;
8383 Related_Nod : Node_Id;
8384 For_Access : Boolean := False)
8385 is
8386 Has_Discrs : constant Boolean := Has_Discriminants (T);
88b32fc3
BD
8387 Constrained : constant Boolean :=
8388 (Has_Discrs
8389 and then not Is_Empty_Elmt_List (Elist)
8390 and then not Is_Class_Wide_Type (T))
8391 or else Is_Constrained (T);
996ae0b0
RK
8392
8393 begin
8394 if Ekind (T) = E_Record_Type then
8395 if For_Access then
8396 Set_Ekind (Def_Id, E_Private_Subtype);
8397 Set_Is_For_Access_Subtype (Def_Id, True);
8398 else
8399 Set_Ekind (Def_Id, E_Record_Subtype);
8400 end if;
8401
7d7af38a
JM
8402 -- Inherit preelaboration flag from base, for types for which it
8403 -- may have been set: records, private types, protected types.
8404
8405 Set_Known_To_Have_Preelab_Init
8406 (Def_Id, Known_To_Have_Preelab_Init (T));
8407
996ae0b0
RK
8408 elsif Ekind (T) = E_Task_Type then
8409 Set_Ekind (Def_Id, E_Task_Subtype);
8410
8411 elsif Ekind (T) = E_Protected_Type then
8412 Set_Ekind (Def_Id, E_Protected_Subtype);
7d7af38a
JM
8413 Set_Known_To_Have_Preelab_Init
8414 (Def_Id, Known_To_Have_Preelab_Init (T));
996ae0b0
RK
8415
8416 elsif Is_Private_Type (T) then
8417 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
7d7af38a
JM
8418 Set_Known_To_Have_Preelab_Init
8419 (Def_Id, Known_To_Have_Preelab_Init (T));
996ae0b0
RK
8420
8421 elsif Is_Class_Wide_Type (T) then
8422 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
8423
8424 else
88b32fc3 8425 -- Incomplete type. Attach subtype to list of dependents, to be
35ae2ed8
AC
8426 -- completed with full view of parent type, unless is it the
8427 -- designated subtype of a record component within an init_proc.
8428 -- This last case arises for a component of an access type whose
8429 -- designated type is incomplete (e.g. a Taft Amendment type).
8430 -- The designated subtype is within an inner scope, and needs no
8431 -- elaboration, because only the access type is needed in the
8432 -- initialization procedure.
996ae0b0
RK
8433
8434 Set_Ekind (Def_Id, Ekind (T));
35ae2ed8
AC
8435
8436 if For_Access and then Within_Init_Proc then
8437 null;
8438 else
8439 Append_Elmt (Def_Id, Private_Dependents (T));
8440 end if;
996ae0b0
RK
8441 end if;
8442
8443 Set_Etype (Def_Id, T);
8444 Init_Size_Align (Def_Id);
8445 Set_Has_Discriminants (Def_Id, Has_Discrs);
8446 Set_Is_Constrained (Def_Id, Constrained);
8447
8448 Set_First_Entity (Def_Id, First_Entity (T));
8449 Set_Last_Entity (Def_Id, Last_Entity (T));
33931112
JM
8450
8451 -- If the subtype is the completion of a private declaration, there may
8452 -- have been representation clauses for the partial view, and they must
8453 -- be preserved. Build_Derived_Type chains the inherited clauses with
8454 -- the ones appearing on the extension. If this comes from a subtype
8455 -- declaration, all clauses are inherited.
8456
8457 if No (First_Rep_Item (Def_Id)) then
23c4ff9b 8458 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
33931112 8459 end if;
996ae0b0
RK
8460
8461 if Is_Tagged_Type (T) then
8462 Set_Is_Tagged_Type (Def_Id);
8463 Make_Class_Wide_Type (Def_Id);
8464 end if;
8465
fbf5a39b 8466 Set_Stored_Constraint (Def_Id, No_Elist);
996ae0b0
RK
8467
8468 if Has_Discrs then
8469 Set_Discriminant_Constraint (Def_Id, Elist);
fbf5a39b 8470 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
996ae0b0
RK
8471 end if;
8472
8473 if Is_Tagged_Type (T) then
030d25f4
JM
8474
8475 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
8476 -- concurrent record type (which has the list of primitive
8477 -- operations).
8478
0791fbe9 8479 if Ada_Version >= Ada_2005
030d25f4
JM
8480 and then Is_Concurrent_Type (T)
8481 then
8482 Set_Corresponding_Record_Type (Def_Id,
8483 Corresponding_Record_Type (T));
8484 else
ef2a63ba
JM
8485 Set_Direct_Primitive_Operations (Def_Id,
8486 Direct_Primitive_Operations (T));
030d25f4
JM
8487 end if;
8488
fea9e956 8489 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
996ae0b0
RK
8490 end if;
8491
8492 -- Subtypes introduced by component declarations do not need to be
8493 -- marked as delayed, and do not get freeze nodes, because the semantics
8494 -- verifies that the parents of the subtypes are frozen before the
8495 -- enclosing record is frozen.
8496
8497 if not Is_Type (Scope (Def_Id)) then
8498 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
8499
8500 if Is_Private_Type (T)
8501 and then Present (Full_View (T))
8502 then
8503 Conditional_Delay (Def_Id, Full_View (T));
8504 else
8505 Conditional_Delay (Def_Id, T);
8506 end if;
8507 end if;
8508
8509 if Is_Record_Type (T) then
8510 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
8511
8512 if Has_Discrs
8513 and then not Is_Empty_Elmt_List (Elist)
8514 and then not For_Access
8515 then
8516 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
8517 elsif not For_Access then
8518 Set_Cloned_Subtype (Def_Id, T);
8519 end if;
8520 end if;
996ae0b0
RK
8521 end Build_Discriminated_Subtype;
8522
fea9e956
ES
8523 ---------------------------
8524 -- Build_Itype_Reference --
8525 ---------------------------
8526
8527 procedure Build_Itype_Reference
8528 (Ityp : Entity_Id;
8529 Nod : Node_Id)
8530 is
8531 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
8532 begin
8533 Set_Itype (IR, Ityp);
8534 Insert_After (Nod, IR);
8535 end Build_Itype_Reference;
8536
996ae0b0
RK
8537 ------------------------
8538 -- Build_Scalar_Bound --
8539 ------------------------
8540
8541 function Build_Scalar_Bound
8542 (Bound : Node_Id;
8543 Par_T : Entity_Id;
b0f26df5 8544 Der_T : Entity_Id) return Node_Id
996ae0b0
RK
8545 is
8546 New_Bound : Entity_Id;
8547
8548 begin
8549 -- Note: not clear why this is needed, how can the original bound
8550 -- be unanalyzed at this point? and if it is, what business do we
8551 -- have messing around with it? and why is the base type of the
8552 -- parent type the right type for the resolution. It probably is
8553 -- not! It is OK for the new bound we are creating, but not for
8554 -- the old one??? Still if it never happens, no problem!
8555
8556 Analyze_And_Resolve (Bound, Base_Type (Par_T));
8557
7d7af38a 8558 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
996ae0b0
RK
8559 New_Bound := New_Copy (Bound);
8560 Set_Etype (New_Bound, Der_T);
8561 Set_Analyzed (New_Bound);
8562
8563 elsif Is_Entity_Name (Bound) then
8564 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
8565
8566 -- The following is almost certainly wrong. What business do we have
8567 -- relocating a node (Bound) that is presumably still attached to
8568 -- the tree elsewhere???
8569
8570 else
8571 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
8572 end if;
8573
8574 Set_Etype (New_Bound, Der_T);
8575 return New_Bound;
8576 end Build_Scalar_Bound;
8577
8578 --------------------------------
8579 -- Build_Underlying_Full_View --
8580 --------------------------------
8581
8582 procedure Build_Underlying_Full_View
8583 (N : Node_Id;
8584 Typ : Entity_Id;
8585 Par : Entity_Id)
8586 is
8587 Loc : constant Source_Ptr := Sloc (N);
8588 Subt : constant Entity_Id :=
8589 Make_Defining_Identifier
8590 (Loc, New_External_Name (Chars (Typ), 'S'));
8591
8592 Constr : Node_Id;
8593 Indic : Node_Id;
8594 C : Node_Id;
8595 Id : Node_Id;
8596
244e5a2c
AC
8597 procedure Set_Discriminant_Name (Id : Node_Id);
8598 -- If the derived type has discriminants, they may rename discriminants
8599 -- of the parent. When building the full view of the parent, we need to
8600 -- recover the names of the original discriminants if the constraint is
8601 -- given by named associations.
8602
8603 ---------------------------
8604 -- Set_Discriminant_Name --
8605 ---------------------------
8606
8607 procedure Set_Discriminant_Name (Id : Node_Id) is
8608 Disc : Entity_Id;
8609
8610 begin
8611 Set_Original_Discriminant (Id, Empty);
8612
8613 if Has_Discriminants (Typ) then
8614 Disc := First_Discriminant (Typ);
244e5a2c
AC
8615 while Present (Disc) loop
8616 if Chars (Disc) = Chars (Id)
8617 and then Present (Corresponding_Discriminant (Disc))
8618 then
8619 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
8620 end if;
8621 Next_Discriminant (Disc);
8622 end loop;
8623 end if;
8624 end Set_Discriminant_Name;
8625
8626 -- Start of processing for Build_Underlying_Full_View
8627
996ae0b0
RK
8628 begin
8629 if Nkind (N) = N_Full_Type_Declaration then
8630 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
8631
244e5a2c 8632 elsif Nkind (N) = N_Subtype_Declaration then
996ae0b0 8633 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
996ae0b0 8634
244e5a2c
AC
8635 elsif Nkind (N) = N_Component_Declaration then
8636 Constr :=
8637 New_Copy_Tree
8638 (Constraint (Subtype_Indication (Component_Definition (N))));
996ae0b0 8639
244e5a2c
AC
8640 else
8641 raise Program_Error;
8642 end if;
996ae0b0 8643
244e5a2c 8644 C := First (Constraints (Constr));
996ae0b0 8645 while Present (C) loop
996ae0b0
RK
8646 if Nkind (C) = N_Discriminant_Association then
8647 Id := First (Selector_Names (C));
996ae0b0 8648 while Present (Id) loop
244e5a2c 8649 Set_Discriminant_Name (Id);
996ae0b0
RK
8650 Next (Id);
8651 end loop;
8652 end if;
8653
8654 Next (C);
8655 end loop;
8656
244e5a2c
AC
8657 Indic :=
8658 Make_Subtype_Declaration (Loc,
8659 Defining_Identifier => Subt,
8660 Subtype_Indication =>
8661 Make_Subtype_Indication (Loc,
8662 Subtype_Mark => New_Reference_To (Par, Loc),
8663 Constraint => New_Copy_Tree (Constr)));
996ae0b0 8664
615cbd95
AC
8665 -- If this is a component subtype for an outer itype, it is not
8666 -- a list member, so simply set the parent link for analysis: if
8667 -- the enclosing type does not need to be in a declarative list,
8668 -- neither do the components.
8669
244e5a2c
AC
8670 if Is_List_Member (N)
8671 and then Nkind (N) /= N_Component_Declaration
8672 then
615cbd95
AC
8673 Insert_Before (N, Indic);
8674 else
8675 Set_Parent (Indic, Parent (N));
8676 end if;
8677
996ae0b0
RK
8678 Analyze (Indic);
8679 Set_Underlying_Full_View (Typ, Full_View (Subt));
8680 end Build_Underlying_Full_View;
8681
8682 -------------------------------
8683 -- Check_Abstract_Overriding --
8684 -------------------------------
8685
8686 procedure Check_Abstract_Overriding (T : Entity_Id) is
88b32fc3 8687 Alias_Subp : Entity_Id;
57193e09 8688 Elmt : Elmt_Id;
88b32fc3 8689 Op_List : Elist_Id;
57193e09 8690 Subp : Entity_Id;
57193e09 8691 Type_Def : Node_Id;
996ae0b0 8692
bfae1846
AC
8693 procedure Check_Pragma_Implemented (Subp : Entity_Id);
8694 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
8695 -- which has pragma Implemented already set. Check whether Subp's entity
8696 -- kind conforms to the implementation kind of the overridden routine.
8697
8698 procedure Check_Pragma_Implemented
8699 (Subp : Entity_Id;
8700 Iface_Subp : Entity_Id);
8701 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
8702 -- Iface_Subp and both entities have pragma Implemented already set on
8703 -- them. Check whether the two implementation kinds are conforming.
8704
8705 procedure Inherit_Pragma_Implemented
8706 (Subp : Entity_Id;
8707 Iface_Subp : Entity_Id);
8708 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
8709 -- subprogram Iface_Subp which has been marked by pragma Implemented.
8710 -- Propagate the implementation kind of Iface_Subp to Subp.
8711
8712 ------------------------------
8713 -- Check_Pragma_Implemented --
8714 ------------------------------
8715
8716 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
8717 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
8718 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
8719 Contr_Typ : Entity_Id;
8720
8721 begin
8722 -- Subp must have an alias since it is a hidden entity used to link
8723 -- an interface subprogram to its overriding counterpart.
8724
8725 pragma Assert (Present (Alias (Subp)));
8726
8727 -- Extract the type of the controlling formal
8728
8729 Contr_Typ := Etype (First_Formal (Alias (Subp)));
8730
8731 if Is_Concurrent_Record_Type (Contr_Typ) then
8732 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
8733 end if;
8734
8735 -- An interface subprogram whose implementation kind is By_Entry must
8736 -- be implemented by an entry.
8737
8738 if Impl_Kind = Name_By_Entry
8739 and then Ekind (Wrapped_Entity (Alias (Subp))) /= E_Entry
8740 then
8741 Error_Msg_Node_2 := Iface_Alias;
8742 Error_Msg_NE
8743 ("type & must implement abstract subprogram & with an entry",
8744 Alias (Subp), Contr_Typ);
8745
8746 elsif Impl_Kind = Name_By_Protected_Procedure then
8747
8748 -- An interface subprogram whose implementation kind is By_
8749 -- Protected_Procedure cannot be implemented by a primitive
8750 -- procedure of a task type.
8751
8752 if Ekind (Contr_Typ) /= E_Protected_Type then
8753 Error_Msg_Node_2 := Contr_Typ;
8754 Error_Msg_NE
8755 ("interface subprogram & cannot be implemented by a " &
8756 "primitive procedure of task type &", Alias (Subp),
8757 Iface_Alias);
8758
8759 -- An interface subprogram whose implementation kind is By_
8760 -- Protected_Procedure must be implemented by a procedure.
8761
8762 elsif Is_Primitive_Wrapper (Alias (Subp))
8763 and then Ekind (Wrapped_Entity (Alias (Subp))) /= E_Procedure
8764 then
8765 Error_Msg_Node_2 := Iface_Alias;
8766 Error_Msg_NE
8767 ("type & must implement abstract subprogram & with a " &
8768 "procedure", Alias (Subp), Contr_Typ);
8769 end if;
8770 end if;
8771 end Check_Pragma_Implemented;
8772
8773 ------------------------------
8774 -- Check_Pragma_Implemented --
8775 ------------------------------
8776
8777 procedure Check_Pragma_Implemented
8778 (Subp : Entity_Id;
8779 Iface_Subp : Entity_Id)
8780 is
8781 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8782 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
8783
8784 begin
8785 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
8786 -- and overriding subprogram are different. In general this is an
8787 -- error except when the implementation kind of the overridden
8788 -- subprograms is By_Any.
8789
8790 if Iface_Kind /= Subp_Kind
8791 and then Iface_Kind /= Name_By_Any
8792 then
8793 if Iface_Kind = Name_By_Entry then
8794 Error_Msg_N
8795 ("incompatible implementation kind, overridden subprogram " &
8796 "is marked By_Entry", Subp);
8797 else
8798 Error_Msg_N
8799 ("incompatible implementation kind, overridden subprogram " &
8800 "is marked By_Protected_Procedure", Subp);
8801 end if;
8802 end if;
8803 end Check_Pragma_Implemented;
8804
8805 --------------------------------
8806 -- Inherit_Pragma_Implemented --
8807 --------------------------------
8808
8809 procedure Inherit_Pragma_Implemented
8810 (Subp : Entity_Id;
8811 Iface_Subp : Entity_Id)
8812 is
8813 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8814 Loc : constant Source_Ptr := Sloc (Subp);
8815 Impl_Prag : Node_Id;
8816
8817 begin
8818 -- Since the implementation kind is stored as a representation item
8819 -- rather than a flag, create a pragma node.
8820
8821 Impl_Prag :=
8822 Make_Pragma (Loc,
8823 Chars => Name_Implemented,
8824 Pragma_Argument_Associations => New_List (
8825 Make_Pragma_Argument_Association (Loc,
8826 Expression =>
8827 New_Reference_To (Subp, Loc)),
8828
8829 Make_Pragma_Argument_Association (Loc,
7675ad4f 8830 Expression => Make_Identifier (Loc, Iface_Kind))));
bfae1846 8831
308e6f3a 8832 -- The pragma doesn't need to be analyzed because it is internally
bfae1846
AC
8833 -- build. It is safe to directly register it as a rep item since we
8834 -- are only interested in the characters of the implementation kind.
8835
8836 Record_Rep_Item (Subp, Impl_Prag);
8837 end Inherit_Pragma_Implemented;
8838
8839 -- Start of processing for Check_Abstract_Overriding
8840
996ae0b0
RK
8841 begin
8842 Op_List := Primitive_Operations (T);
8843
8844 -- Loop to check primitive operations
8845
8846 Elmt := First_Elmt (Op_List);
8847 while Present (Elmt) loop
8848 Subp := Node (Elmt);
57193e09
TQ
8849 Alias_Subp := Alias (Subp);
8850
8851 -- Inherited subprograms are identified by the fact that they do not
8852 -- come from source, and the associated source location is the
8853 -- location of the first subtype of the derived type.
996ae0b0 8854
fea9e956
ES
8855 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
8856 -- subprograms that "require overriding".
8857
a5b62485 8858 -- Special exception, do not complain about failure to override the
9dfd2ff8
CC
8859 -- stream routines _Input and _Output, as well as the primitive
8860 -- operations used in dispatching selects since we always provide
996ae0b0
RK
8861 -- automatic overridings for these subprograms.
8862
2b73cf68
JM
8863 -- Also ignore this rule for convention CIL since .NET libraries
8864 -- do bizarre things with interfaces???
8865
8866 -- The partial view of T may have been a private extension, for
8867 -- which inherited functions dispatching on result are abstract.
8868 -- If the full view is a null extension, there is no need for
8869 -- overriding in Ada2005, but wrappers need to be built for them
8870 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
8871
8872 if Is_Null_Extension (T)
8873 and then Has_Controlling_Result (Subp)
0791fbe9 8874 and then Ada_Version >= Ada_2005
ce2b6ba5 8875 and then Present (Alias_Subp)
2b73cf68 8876 and then not Comes_From_Source (Subp)
ce2b6ba5 8877 and then not Is_Abstract_Subprogram (Alias_Subp)
ce4a6e84 8878 and then not Is_Access_Type (Etype (Subp))
2b73cf68 8879 then
7d7af38a 8880 null;
2b73cf68 8881
ce2b6ba5
JM
8882 -- Ada 2005 (AI-251): Internal entities of interfaces need no
8883 -- processing because this check is done with the aliased
8884 -- entity
8885
8886 elsif Present (Interface_Alias (Subp)) then
8887 null;
8888
7d7af38a 8889 elsif (Is_Abstract_Subprogram (Subp)
ce4a6e84
RD
8890 or else Requires_Overriding (Subp)
8891 or else
8892 (Has_Controlling_Result (Subp)
8893 and then Present (Alias_Subp)
8894 and then not Comes_From_Source (Subp)
8895 and then Sloc (Subp) = Sloc (First_Subtype (T))))
fbf5a39b
AC
8896 and then not Is_TSS (Subp, TSS_Stream_Input)
8897 and then not Is_TSS (Subp, TSS_Stream_Output)
fea9e956 8898 and then not Is_Abstract_Type (T)
2b73cf68 8899 and then Convention (T) /= Convention_CIL
ce2b6ba5 8900 and then not Is_Predefined_Interface_Primitive (Subp)
88b32fc3
BD
8901
8902 -- Ada 2005 (AI-251): Do not consider hidden entities associated
8903 -- with abstract interface types because the check will be done
8904 -- with the aliased entity (otherwise we generate a duplicated
8905 -- error message).
8906
ce2b6ba5 8907 and then not Present (Interface_Alias (Subp))
996ae0b0 8908 then
57193e09
TQ
8909 if Present (Alias_Subp) then
8910
8911 -- Only perform the check for a derived subprogram when the
f3d0f304 8912 -- type has an explicit record extension. This avoids incorrect
ce4a6e84
RD
8913 -- flagging of abstract subprograms for the case of a type
8914 -- without an extension that is derived from a formal type
8915 -- with a tagged actual (can occur within a private part).
57193e09
TQ
8916
8917 -- Ada 2005 (AI-391): In the case of an inherited function with
8918 -- a controlling result of the type, the rule does not apply if
8919 -- the type is a null extension (unless the parent function
8920 -- itself is abstract, in which case the function must still be
8921 -- be overridden). The expander will generate an overriding
8922 -- wrapper function calling the parent subprogram (see
8923 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
996ae0b0
RK
8924
8925 Type_Def := Type_Definition (Parent (T));
7d7af38a 8926
996ae0b0
RK
8927 if Nkind (Type_Def) = N_Derived_Type_Definition
8928 and then Present (Record_Extension_Part (Type_Def))
57193e09 8929 and then
0791fbe9 8930 (Ada_Version < Ada_2005
57193e09
TQ
8931 or else not Is_Null_Extension (T)
8932 or else Ekind (Subp) = E_Procedure
8933 or else not Has_Controlling_Result (Subp)
fea9e956
ES
8934 or else Is_Abstract_Subprogram (Alias_Subp)
8935 or else Requires_Overriding (Subp)
57193e09 8936 or else Is_Access_Type (Etype (Subp)))
996ae0b0 8937 then
ce2b6ba5
JM
8938 -- Avoid reporting error in case of abstract predefined
8939 -- primitive inherited from interface type because the
8940 -- body of internally generated predefined primitives
8941 -- of tagged types are generated later by Freeze_Type
8942
8943 if Is_Interface (Root_Type (T))
8944 and then Is_Abstract_Subprogram (Subp)
8945 and then Is_Predefined_Dispatching_Operation (Subp)
8946 and then not Comes_From_Source (Ultimate_Alias (Subp))
7d7af38a
JM
8947 then
8948 null;
9dfd2ff8 8949
7d7af38a
JM
8950 else
8951 Error_Msg_NE
8952 ("type must be declared abstract or & overridden",
8953 T, Subp);
9dfd2ff8 8954
7d7af38a
JM
8955 -- Traverse the whole chain of aliased subprograms to
8956 -- complete the error notification. This is especially
8957 -- useful for traceability of the chain of entities when
8958 -- the subprogram corresponds with an interface
8959 -- subprogram (which may be defined in another package).
8960
8961 if Present (Alias_Subp) then
8962 declare
8963 E : Entity_Id;
8964
8965 begin
8966 E := Subp;
8967 while Present (Alias (E)) loop
8968 Error_Msg_Sloc := Sloc (E);
8969 Error_Msg_NE
8970 ("\& has been inherited #", T, Subp);
8971 E := Alias (E);
8972 end loop;
9dfd2ff8 8973
7d7af38a
JM
8974 Error_Msg_Sloc := Sloc (E);
8975 Error_Msg_NE
8976 ("\& has been inherited from subprogram #",
8977 T, Subp);
8978 end;
8979 end if;
9dfd2ff8
CC
8980 end if;
8981
758c442c 8982 -- Ada 2005 (AI-345): Protected or task type implementing
9dfd2ff8 8983 -- abstract interfaces.
758c442c
GD
8984
8985 elsif Is_Concurrent_Record_Type (T)
ce2b6ba5 8986 and then Present (Interfaces (T))
758c442c 8987 then
88b32fc3
BD
8988 -- The controlling formal of Subp must be of mode "out",
8989 -- "in out" or an access-to-variable to be overridden.
8990
dc06abec
RD
8991 -- Error message below needs rewording (remember comma
8992 -- in -gnatj mode) ???
8993
8f983e64
ES
8994 if Ekind (First_Formal (Subp)) = E_In_Parameter
8995 and then Ekind (Subp) /= E_Function
8996 then
ce2b6ba5
JM
8997 if not Is_Predefined_Dispatching_Operation (Subp) then
8998 Error_Msg_NE
8999 ("first formal of & must be of mode `OUT`, " &
9000 "`IN OUT` or access-to-variable", T, Subp);
9001 Error_Msg_N
9002 ("\to be overridden by protected procedure or " &
9003 "entry (RM 9.4(11.9/2))", T);
9004 end if;
88b32fc3
BD
9005
9006 -- Some other kind of overriding failure
9007
9008 else
9009 Error_Msg_NE
9010 ("interface subprogram & must be overridden",
9011 T, Subp);
8f983e64
ES
9012
9013 -- Examine primitive operations of synchronized type,
9014 -- to find homonyms that have the wrong profile.
9015
9016 declare
9017 Prim : Entity_Id;
9018
9019 begin
9020 Prim :=
9021 First_Entity (Corresponding_Concurrent_Type (T));
9022 while Present (Prim) loop
9023 if Chars (Prim) = Chars (Subp) then
9024 Error_Msg_NE
9025 ("profile is not type conformant with "
9026 & "prefixed view profile of "
9027 & "inherited operation&", Prim, Subp);
9028 end if;
9029
9030 Next_Entity (Prim);
9031 end loop;
9032 end;
88b32fc3 9033 end if;
996ae0b0 9034 end if;
88b32fc3 9035
996ae0b0 9036 else
fea9e956
ES
9037 Error_Msg_Node_2 := T;
9038 Error_Msg_N
9039 ("abstract subprogram& not allowed for type&", Subp);
9040
9041 -- Also post unconditional warning on the type (unconditional
9042 -- so that if there are more than one of these cases, we get
9043 -- them all, and not just the first one).
9044
9045 Error_Msg_Node_2 := Subp;
ed2233dc 9046 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
996ae0b0
RK
9047 end if;
9048 end if;
9049
bfae1846
AC
9050 -- Ada 2012 (AI05-0030): Perform some checks related to pragma
9051 -- Implemented
7d7af38a 9052
bfae1846
AC
9053 -- Subp is an expander-generated procedure which maps an interface
9054 -- alias to a protected wrapper. The interface alias is flagged by
9055 -- pragma Implemented. Ensure that Subp is a procedure when the
9056 -- implementation kind is By_Protected_Procedure or an entry when
9057 -- By_Entry.
9058
9059 if Ada_Version >= Ada_2012
7d7af38a 9060 and then Is_Hidden (Subp)
ce2b6ba5 9061 and then Present (Interface_Alias (Subp))
bfae1846 9062 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
7d7af38a 9063 then
bfae1846
AC
9064 Check_Pragma_Implemented (Subp);
9065 end if;
7d7af38a 9066
bfae1846
AC
9067 -- Subp is an interface primitive which overrides another interface
9068 -- primitive marked with pragma Implemented.
7d7af38a 9069
bfae1846 9070 if Ada_Version >= Ada_2012
bfae1846
AC
9071 and then Present (Overridden_Operation (Subp))
9072 and then Has_Rep_Pragma
9073 (Overridden_Operation (Subp), Name_Implemented)
9074 then
9075 -- If the overriding routine is also marked by Implemented, check
9076 -- that the two implementation kinds are conforming.
9077
9078 if Has_Rep_Pragma (Subp, Name_Implemented) then
9079 Check_Pragma_Implemented
9080 (Subp => Subp,
9081 Iface_Subp => Overridden_Operation (Subp));
9082
9083 -- Otherwise the overriding routine inherits the implementation
9084 -- kind from the overridden subprogram.
9085
9086 else
9087 Inherit_Pragma_Implemented
9088 (Subp => Subp,
9089 Iface_Subp => Overridden_Operation (Subp));
9090 end if;
7d7af38a
JM
9091 end if;
9092
9093 Next_Elmt (Elmt);
996ae0b0
RK
9094 end loop;
9095 end Check_Abstract_Overriding;
9096
9097 ------------------------------------------------
9098 -- Check_Access_Discriminant_Requires_Limited --
9099 ------------------------------------------------
9100
9101 procedure Check_Access_Discriminant_Requires_Limited
9102 (D : Node_Id;
9103 Loc : Node_Id)
9104 is
9105 begin
9dfd2ff8
CC
9106 -- A discriminant_specification for an access discriminant shall appear
9107 -- only in the declaration for a task or protected type, or for a type
9108 -- with the reserved word 'limited' in its definition or in one of its
0144fd18
RD
9109 -- ancestors (RM 3.7(10)).
9110
9111 -- AI-0063: The proper condition is that type must be immutably limited,
9112 -- or else be a partial view.
996ae0b0 9113
e0ae93e2
RD
9114 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
9115 if Is_Immutably_Limited_Type (Current_Scope)
9116 or else
0144fd18 9117 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
e0ae93e2
RD
9118 and then Limited_Present (Parent (Current_Scope)))
9119 then
9120 null;
9121
9122 else
9123 Error_Msg_N
9124 ("access discriminants allowed only for limited types", Loc);
9125 end if;
996ae0b0
RK
9126 end if;
9127 end Check_Access_Discriminant_Requires_Limited;
9128
9129 -----------------------------------
9130 -- Check_Aliased_Component_Types --
9131 -----------------------------------
9132
9133 procedure Check_Aliased_Component_Types (T : Entity_Id) is
9134 C : Entity_Id;
9135
9136 begin
a5b62485
AC
9137 -- ??? Also need to check components of record extensions, but not
9138 -- components of protected types (which are always limited).
996ae0b0 9139
9dfd2ff8
CC
9140 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
9141 -- types to be unconstrained. This is safe because it is illegal to
9142 -- create access subtypes to such types with explicit discriminant
9143 -- constraints.
758c442c 9144
996ae0b0
RK
9145 if not Is_Limited_Type (T) then
9146 if Ekind (T) = E_Record_Type then
9147 C := First_Component (T);
9148 while Present (C) loop
9149 if Is_Aliased (C)
9150 and then Has_Discriminants (Etype (C))
9151 and then not Is_Constrained (Etype (C))
950d3e7d 9152 and then not In_Instance_Body
0791fbe9 9153 and then Ada_Version < Ada_2005
996ae0b0
RK
9154 then
9155 Error_Msg_N
dc06abec 9156 ("aliased component must be constrained (RM 3.6(11))",
996ae0b0
RK
9157 C);
9158 end if;
9159
9160 Next_Component (C);
9161 end loop;
9162
9163 elsif Ekind (T) = E_Array_Type then
9164 if Has_Aliased_Components (T)
9165 and then Has_Discriminants (Component_Type (T))
9166 and then not Is_Constrained (Component_Type (T))
950d3e7d 9167 and then not In_Instance_Body
0791fbe9 9168 and then Ada_Version < Ada_2005
996ae0b0
RK
9169 then
9170 Error_Msg_N
dc06abec 9171 ("aliased component type must be constrained (RM 3.6(11))",
996ae0b0
RK
9172 T);
9173 end if;
9174 end if;
9175 end if;
9176 end Check_Aliased_Component_Types;
9177
9178 ----------------------
9179 -- Check_Completion --
9180 ----------------------
9181
9182 procedure Check_Completion (Body_Id : Node_Id := Empty) is
9183 E : Entity_Id;
9184
9185 procedure Post_Error;
9186 -- Post error message for lack of completion for entity E
9187
fbf5a39b
AC
9188 ----------------
9189 -- Post_Error --
9190 ----------------
9191
996ae0b0 9192 procedure Post_Error is
b568955d
AC
9193
9194 procedure Missing_Body;
9195 -- Output missing body message
9196
9197 ------------------
9198 -- Missing_Body --
9199 ------------------
9200
9201 procedure Missing_Body is
9202 begin
9203 -- Spec is in same unit, so we can post on spec
9204
9205 if In_Same_Source_Unit (Body_Id, E) then
9206 Error_Msg_N ("missing body for &", E);
9207
9208 -- Spec is in a separate unit, so we have to post on the body
9209
9210 else
9211 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
9212 end if;
9213 end Missing_Body;
9214
9215 -- Start of processing for Post_Error
9216
996ae0b0
RK
9217 begin
9218 if not Comes_From_Source (E) then
9219
bce79204 9220 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
996ae0b0
RK
9221 -- It may be an anonymous protected type created for a
9222 -- single variable. Post error on variable, if present.
9223
9224 declare
9225 Var : Entity_Id;
9226
9227 begin
9228 Var := First_Entity (Current_Scope);
996ae0b0
RK
9229 while Present (Var) loop
9230 exit when Etype (Var) = E
9231 and then Comes_From_Source (Var);
9232
9233 Next_Entity (Var);
9234 end loop;
9235
9236 if Present (Var) then
9237 E := Var;
9238 end if;
9239 end;
9240 end if;
9241 end if;
9242
9243 -- If a generated entity has no completion, then either previous
a5b62485 9244 -- semantic errors have disabled the expansion phase, or else we had
fea9e956 9245 -- missing subunits, or else we are compiling without expansion,
a5b62485 9246 -- or else something is very wrong.
996ae0b0
RK
9247
9248 if not Comes_From_Source (E) then
9249 pragma Assert
07fc65c4 9250 (Serious_Errors_Detected > 0
fbf5a39b 9251 or else Configurable_Run_Time_Violations > 0
996ae0b0
RK
9252 or else Subunits_Missing
9253 or else not Expander_Active);
9254 return;
9255
9256 -- Here for source entity
9257
9258 else
9259 -- Here if no body to post the error message, so we post the error
9260 -- on the declaration that has no completion. This is not really
9261 -- the right place to post it, think about this later ???
9262
9263 if No (Body_Id) then
9264 if Is_Type (E) then
9265 Error_Msg_NE
9266 ("missing full declaration for }", Parent (E), E);
9267 else
ed2233dc 9268 Error_Msg_NE ("missing body for &", Parent (E), E);
996ae0b0
RK
9269 end if;
9270
9271 -- Package body has no completion for a declaration that appears
9272 -- in the corresponding spec. Post error on the body, with a
9273 -- reference to the non-completed declaration.
9274
9275 else
9276 Error_Msg_Sloc := Sloc (E);
9277
9278 if Is_Type (E) then
ed2233dc 9279 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
996ae0b0
RK
9280
9281 elsif Is_Overloadable (E)
9282 and then Current_Entity_In_Scope (E) /= E
9283 then
7d7af38a
JM
9284 -- It may be that the completion is mistyped and appears as
9285 -- a distinct overloading of the entity.
996ae0b0
RK
9286
9287 declare
fbf5a39b
AC
9288 Candidate : constant Entity_Id :=
9289 Current_Entity_In_Scope (E);
9290 Decl : constant Node_Id :=
9291 Unit_Declaration_Node (Candidate);
996ae0b0
RK
9292
9293 begin
9294 if Is_Overloadable (Candidate)
9295 and then Ekind (Candidate) = Ekind (E)
9296 and then Nkind (Decl) = N_Subprogram_Body
9297 and then Acts_As_Spec (Decl)
9298 then
9299 Check_Type_Conformant (Candidate, E);
9300
9301 else
b568955d 9302 Missing_Body;
996ae0b0
RK
9303 end if;
9304 end;
b568955d 9305
996ae0b0 9306 else
b568955d 9307 Missing_Body;
996ae0b0
RK
9308 end if;
9309 end if;
9310 end if;
9311 end Post_Error;
9312
d8221f45 9313 -- Start of processing for Check_Completion
996ae0b0
RK
9314
9315 begin
9316 E := First_Entity (Current_Scope);
9317 while Present (E) loop
9318 if Is_Intrinsic_Subprogram (E) then
9319 null;
9320
7d7af38a
JM
9321 -- The following situation requires special handling: a child unit
9322 -- that appears in the context clause of the body of its parent:
996ae0b0
RK
9323
9324 -- procedure Parent.Child (...);
a5b62485 9325
996ae0b0
RK
9326 -- with Parent.Child;
9327 -- package body Parent is
9328
7d7af38a
JM
9329 -- Here Parent.Child appears as a local entity, but should not be
9330 -- flagged as requiring completion, because it is a compilation
9331 -- unit.
996ae0b0 9332
fea9e956
ES
9333 -- Ignore missing completion for a subprogram that does not come from
9334 -- source (including the _Call primitive operation of RAS types,
9335 -- which has to have the flag Comes_From_Source for other purposes):
9336 -- we assume that the expander will provide the missing completion.
e1f3cb58 9337 -- In case of previous errors, other expansion actions that provide
d6533e74 9338 -- bodies for null procedures with not be invoked, so inhibit message
e1f3cb58 9339 -- in those cases.
d6533e74
RD
9340 -- Note that E_Operator is not in the list that follows, because
9341 -- this kind is reserved for predefined operators, that are
9342 -- intrinsic and do not need completion.
fea9e956 9343
996ae0b0
RK
9344 elsif Ekind (E) = E_Function
9345 or else Ekind (E) = E_Procedure
9346 or else Ekind (E) = E_Generic_Function
9347 or else Ekind (E) = E_Generic_Procedure
9348 then
e1f3cb58
AC
9349 if Has_Completion (E) then
9350 null;
9351
9352 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
9353 null;
9354
9355 elsif Is_Subprogram (E)
9356 and then (not Comes_From_Source (E)
9357 or else Chars (E) = Name_uCall)
9358 then
9359 null;
9360
9361 elsif
9362 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9363 then
9364 null;
9365
9366 elsif Nkind (Parent (E)) = N_Procedure_Specification
9367 and then Null_Present (Parent (E))
9368 and then Serious_Errors_Detected > 0
996ae0b0 9369 then
e1f3cb58
AC
9370 null;
9371
9372 else
996ae0b0
RK
9373 Post_Error;
9374 end if;
9375
9376 elsif Is_Entry (E) then
9377 if not Has_Completion (E) and then
9378 (Ekind (Scope (E)) = E_Protected_Object
9379 or else Ekind (Scope (E)) = E_Protected_Type)
9380 then
9381 Post_Error;
9382 end if;
9383
950d3e7d 9384 elsif Is_Package_Or_Generic_Package (E) then
996ae0b0
RK
9385 if Unit_Requires_Body (E) then
9386 if not Has_Completion (E)
9387 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
9388 N_Compilation_Unit
9389 then
9390 Post_Error;
9391 end if;
9392
9393 elsif not Is_Child_Unit (E) then
9394 May_Need_Implicit_Body (E);
9395 end if;
9396
9397 elsif Ekind (E) = E_Incomplete_Type
9398 and then No (Underlying_Type (E))
9399 then
9400 Post_Error;
9401
9402 elsif (Ekind (E) = E_Task_Type or else
9403 Ekind (E) = E_Protected_Type)
9404 and then not Has_Completion (E)
9405 then
9406 Post_Error;
9407
a5b62485
AC
9408 -- A single task declared in the current scope is a constant, verify
9409 -- that the body of its anonymous type is in the same scope. If the
9410 -- task is defined elsewhere, this may be a renaming declaration for
fbf5a39b
AC
9411 -- which no completion is needed.
9412
996ae0b0
RK
9413 elsif Ekind (E) = E_Constant
9414 and then Ekind (Etype (E)) = E_Task_Type
9415 and then not Has_Completion (Etype (E))
fbf5a39b 9416 and then Scope (Etype (E)) = Current_Scope
996ae0b0
RK
9417 then
9418 Post_Error;
9419
9420 elsif Ekind (E) = E_Protected_Object
9421 and then not Has_Completion (Etype (E))
9422 then
9423 Post_Error;
9424
9425 elsif Ekind (E) = E_Record_Type then
9426 if Is_Tagged_Type (E) then
9427 Check_Abstract_Overriding (E);
88b32fc3 9428 Check_Conventions (E);
996ae0b0
RK
9429 end if;
9430
9431 Check_Aliased_Component_Types (E);
9432
9433 elsif Ekind (E) = E_Array_Type then
9434 Check_Aliased_Component_Types (E);
9435
9436 end if;
9437
9438 Next_Entity (E);
9439 end loop;
9440 end Check_Completion;
9441
9442 ----------------------------
9443 -- Check_Delta_Expression --
9444 ----------------------------
9445
9446 procedure Check_Delta_Expression (E : Node_Id) is
9447 begin
9448 if not (Is_Real_Type (Etype (E))) then
9449 Wrong_Type (E, Any_Real);
9450
9451 elsif not Is_OK_Static_Expression (E) then
fbf5a39b
AC
9452 Flag_Non_Static_Expr
9453 ("non-static expression used for delta value!", E);
996ae0b0
RK
9454
9455 elsif not UR_Is_Positive (Expr_Value_R (E)) then
9456 Error_Msg_N ("delta expression must be positive", E);
9457
9458 else
9459 return;
9460 end if;
9461
9462 -- If any of above errors occurred, then replace the incorrect
9463 -- expression by the real 0.1, which should prevent further errors.
9464
9465 Rewrite (E,
9466 Make_Real_Literal (Sloc (E), Ureal_Tenth));
9467 Analyze_And_Resolve (E, Standard_Float);
996ae0b0
RK
9468 end Check_Delta_Expression;
9469
9470 -----------------------------
9471 -- Check_Digits_Expression --
9472 -----------------------------
9473
9474 procedure Check_Digits_Expression (E : Node_Id) is
9475 begin
9476 if not (Is_Integer_Type (Etype (E))) then
9477 Wrong_Type (E, Any_Integer);
9478
9479 elsif not Is_OK_Static_Expression (E) then
fbf5a39b
AC
9480 Flag_Non_Static_Expr
9481 ("non-static expression used for digits value!", E);
996ae0b0
RK
9482
9483 elsif Expr_Value (E) <= 0 then
9484 Error_Msg_N ("digits value must be greater than zero", E);
9485
9486 else
9487 return;
9488 end if;
9489
9490 -- If any of above errors occurred, then replace the incorrect
9491 -- expression by the integer 1, which should prevent further errors.
9492
9493 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
9494 Analyze_And_Resolve (E, Standard_Integer);
9495
9496 end Check_Digits_Expression;
9497
996ae0b0
RK
9498 --------------------------
9499 -- Check_Initialization --
9500 --------------------------
9501
9502 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
9503 begin
88b32fc3 9504 if Is_Limited_Type (T)
996ae0b0 9505 and then not In_Instance
c45b6ae0 9506 and then not In_Inlined_Body
996ae0b0 9507 then
2a31c32b 9508 if not OK_For_Limited_Init (T, Exp) then
71f62180
ES
9509
9510 -- In GNAT mode, this is just a warning, to allow it to be evilly
9511 -- turned off. Otherwise it is a real error.
65356e64 9512
88b32fc3
BD
9513 if GNAT_Mode then
9514 Error_Msg_N
71f62180
ES
9515 ("?cannot initialize entities of limited type!", Exp);
9516
0791fbe9 9517 elsif Ada_Version < Ada_2005 then
88b32fc3
BD
9518 Error_Msg_N
9519 ("cannot initialize entities of limited type", Exp);
9520 Explain_Limited_Type (T, Exp);
71f62180
ES
9521
9522 else
9523 -- Specialize error message according to kind of illegal
9524 -- initial expression.
9525
9526 if Nkind (Exp) = N_Type_Conversion
9527 and then Nkind (Expression (Exp)) = N_Function_Call
9528 then
9529 Error_Msg_N
9530 ("illegal context for call"
9531 & " to function with limited result", Exp);
9532
9533 else
9534 Error_Msg_N
7d7af38a 9535 ("initialization of limited object requires aggregate "
71f62180
ES
9536 & "or function call", Exp);
9537 end if;
88b32fc3 9538 end if;
65356e64 9539 end if;
996ae0b0
RK
9540 end if;
9541 end Check_Initialization;
9542
ce2b6ba5
JM
9543 ----------------------
9544 -- Check_Interfaces --
9545 ----------------------
9546
9547 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
9548 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
9549
9550 Iface : Node_Id;
9551 Iface_Def : Node_Id;
9552 Iface_Typ : Entity_Id;
9553 Parent_Node : Node_Id;
9554
9555 Is_Task : Boolean := False;
9556 -- Set True if parent type or any progenitor is a task interface
9557
9558 Is_Protected : Boolean := False;
9559 -- Set True if parent type or any progenitor is a protected interface
9560
9561 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
9562 -- Check that a progenitor is compatible with declaration.
9563 -- Error is posted on Error_Node.
9564
9565 ------------------
9566 -- Check_Ifaces --
9567 ------------------
9568
9569 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
9570 Iface_Id : constant Entity_Id :=
9571 Defining_Identifier (Parent (Iface_Def));
9572 Type_Def : Node_Id;
9573
9574 begin
9575 if Nkind (N) = N_Private_Extension_Declaration then
9576 Type_Def := N;
9577 else
9578 Type_Def := Type_Definition (N);
9579 end if;
9580
9581 if Is_Task_Interface (Iface_Id) then
9582 Is_Task := True;
9583
9584 elsif Is_Protected_Interface (Iface_Id) then
9585 Is_Protected := True;
9586 end if;
9587
443614e3
AC
9588 if Is_Synchronized_Interface (Iface_Id) then
9589
9590 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
9591 -- extension derived from a synchronized interface must explicitly
9592 -- be declared synchronized, because the full view will be a
9593 -- synchronized type.
9594
9595 if Nkind (N) = N_Private_Extension_Declaration then
9596 if not Synchronized_Present (N) then
9597 Error_Msg_NE
9598 ("private extension of& must be explicitly synchronized",
9599 N, Iface_Id);
9600 end if;
9601
9602 -- However, by 3.9.4(16/2), a full type that is a record extension
9603 -- is never allowed to derive from a synchronized interface (note
9604 -- that interfaces must be excluded from this check, because those
9605 -- are represented by derived type definitions in some cases).
9606
9607 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9608 and then not Interface_Present (Type_Definition (N))
9609 then
9610 Error_Msg_N ("record extension cannot derive from synchronized"
9611 & " interface", Error_Node);
9612 end if;
9613 end if;
9614
ce2b6ba5
JM
9615 -- Check that the characteristics of the progenitor are compatible
9616 -- with the explicit qualifier in the declaration.
9617 -- The check only applies to qualifiers that come from source.
9618 -- Limited_Present also appears in the declaration of corresponding
9619 -- records, and the check does not apply to them.
9620
9621 if Limited_Present (Type_Def)
9622 and then not
9623 Is_Concurrent_Record_Type (Defining_Identifier (N))
9624 then
9625 if Is_Limited_Interface (Parent_Type)
9626 and then not Is_Limited_Interface (Iface_Id)
9627 then
9628 Error_Msg_NE
9629 ("progenitor& must be limited interface",
9630 Error_Node, Iface_Id);
9631
9632 elsif
9633 (Task_Present (Iface_Def)
9634 or else Protected_Present (Iface_Def)
9635 or else Synchronized_Present (Iface_Def))
9636 and then Nkind (N) /= N_Private_Extension_Declaration
e358346d 9637 and then not Error_Posted (N)
ce2b6ba5
JM
9638 then
9639 Error_Msg_NE
9640 ("progenitor& must be limited interface",
9641 Error_Node, Iface_Id);
9642 end if;
9643
9644 -- Protected interfaces can only inherit from limited, synchronized
9645 -- or protected interfaces.
9646
9647 elsif Nkind (N) = N_Full_Type_Declaration
9648 and then Protected_Present (Type_Def)
9649 then
9650 if Limited_Present (Iface_Def)
9651 or else Synchronized_Present (Iface_Def)
9652 or else Protected_Present (Iface_Def)
9653 then
9654 null;
9655
9656 elsif Task_Present (Iface_Def) then
9657 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9658 & " from task interface", Error_Node);
9659
9660 else
9661 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9662 & " from non-limited interface", Error_Node);
9663 end if;
9664
9665 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
9666 -- limited and synchronized.
9667
9668 elsif Synchronized_Present (Type_Def) then
9669 if Limited_Present (Iface_Def)
9670 or else Synchronized_Present (Iface_Def)
9671 then
9672 null;
9673
9674 elsif Protected_Present (Iface_Def)
9675 and then Nkind (N) /= N_Private_Extension_Declaration
9676 then
9677 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9678 & " from protected interface", Error_Node);
9679
9680 elsif Task_Present (Iface_Def)
9681 and then Nkind (N) /= N_Private_Extension_Declaration
9682 then
9683 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9684 & " from task interface", Error_Node);
9685
9686 elsif not Is_Limited_Interface (Iface_Id) then
9687 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9688 & " from non-limited interface", Error_Node);
9689 end if;
9690
9691 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
9692 -- synchronized or task interfaces.
9693
9694 elsif Nkind (N) = N_Full_Type_Declaration
9695 and then Task_Present (Type_Def)
9696 then
9697 if Limited_Present (Iface_Def)
9698 or else Synchronized_Present (Iface_Def)
9699 or else Task_Present (Iface_Def)
9700 then
9701 null;
9702
9703 elsif Protected_Present (Iface_Def) then
9704 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9705 & " protected interface", Error_Node);
9706
9707 else
9708 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9709 & " non-limited interface", Error_Node);
9710 end if;
9711 end if;
9712 end Check_Ifaces;
9713
9714 -- Start of processing for Check_Interfaces
9715
9716 begin
9717 if Is_Interface (Parent_Type) then
9718 if Is_Task_Interface (Parent_Type) then
9719 Is_Task := True;
9720
9721 elsif Is_Protected_Interface (Parent_Type) then
9722 Is_Protected := True;
9723 end if;
9724 end if;
9725
9726 if Nkind (N) = N_Private_Extension_Declaration then
9727
9728 -- Check that progenitors are compatible with declaration
9729
9730 Iface := First (Interface_List (Def));
9731 while Present (Iface) loop
9732 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9733
9734 Parent_Node := Parent (Base_Type (Iface_Typ));
9735 Iface_Def := Type_Definition (Parent_Node);
9736
9737 if not Is_Interface (Iface_Typ) then
6765b310 9738 Diagnose_Interface (Iface, Iface_Typ);
ce2b6ba5
JM
9739
9740 else
9741 Check_Ifaces (Iface_Def, Iface);
9742 end if;
9743
9744 Next (Iface);
9745 end loop;
9746
9747 if Is_Task and Is_Protected then
9748 Error_Msg_N
9749 ("type cannot derive from task and protected interface", N);
9750 end if;
9751
9752 return;
9753 end if;
9754
9755 -- Full type declaration of derived type.
9756 -- Check compatibility with parent if it is interface type
9757
9758 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9759 and then Is_Interface (Parent_Type)
9760 then
9761 Parent_Node := Parent (Parent_Type);
9762
9763 -- More detailed checks for interface varieties
9764
9765 Check_Ifaces
9766 (Iface_Def => Type_Definition (Parent_Node),
9767 Error_Node => Subtype_Indication (Type_Definition (N)));
9768 end if;
9769
9770 Iface := First (Interface_List (Def));
9771 while Present (Iface) loop
9772 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9773
9774 Parent_Node := Parent (Base_Type (Iface_Typ));
9775 Iface_Def := Type_Definition (Parent_Node);
9776
9777 if not Is_Interface (Iface_Typ) then
6765b310 9778 Diagnose_Interface (Iface, Iface_Typ);
ce2b6ba5
JM
9779
9780 else
9781 -- "The declaration of a specific descendant of an interface
9782 -- type freezes the interface type" RM 13.14
9783
9784 Freeze_Before (N, Iface_Typ);
9785 Check_Ifaces (Iface_Def, Error_Node => Iface);
9786 end if;
9787
9788 Next (Iface);
9789 end loop;
9790
9791 if Is_Task and Is_Protected then
9792 Error_Msg_N
9793 ("type cannot derive from task and protected interface", N);
9794 end if;
9795 end Check_Interfaces;
9796
996ae0b0
RK
9797 ------------------------------------
9798 -- Check_Or_Process_Discriminants --
9799 ------------------------------------
9800
9dfd2ff8
CC
9801 -- If an incomplete or private type declaration was already given for the
9802 -- type, the discriminants may have already been processed if they were
9803 -- present on the incomplete declaration. In this case a full conformance
8e4dac80
TQ
9804 -- check has been performed in Find_Type_Name, and we then recheck here
9805 -- some properties that can't be checked on the partial view alone.
9806 -- Otherwise we call Process_Discriminants.
996ae0b0 9807
fbf5a39b
AC
9808 procedure Check_Or_Process_Discriminants
9809 (N : Node_Id;
9810 T : Entity_Id;
9811 Prev : Entity_Id := Empty)
9812 is
996ae0b0
RK
9813 begin
9814 if Has_Discriminants (T) then
9815
8e4dac80
TQ
9816 -- Discriminants are already set on T if they were already present
9817 -- on the partial view. Make them visible to component declarations.
996ae0b0
RK
9818
9819 declare
027dbed8
AC
9820 D : Entity_Id;
9821 -- Discriminant on T (full view) referencing expr on partial view
8e4dac80
TQ
9822
9823 Prev_D : Entity_Id;
9824 -- Entity of corresponding discriminant on partial view
996ae0b0 9825
8e4dac80
TQ
9826 New_D : Node_Id;
9827 -- Discriminant specification for full view, expression is the
9828 -- syntactic copy on full view (which has been checked for
9829 -- conformance with partial view), only used here to post error
9830 -- message.
027dbed8 9831
996ae0b0 9832 begin
027dbed8 9833 D := First_Discriminant (T);
8e4dac80 9834 New_D := First (Discriminant_Specifications (N));
996ae0b0 9835 while Present (D) loop
8e4dac80 9836 Prev_D := Current_Entity (D);
996ae0b0
RK
9837 Set_Current_Entity (D);
9838 Set_Is_Immediately_Visible (D);
8e4dac80
TQ
9839 Set_Homonym (D, Prev_D);
9840
9841 -- Handle the case where there is an untagged partial view and
9842 -- the full view is tagged: must disallow discriminants with
5e5db3b4
GD
9843 -- defaults, unless compiling for Ada 2012, which allows a
9844 -- limited tagged type to have defaulted discriminants (see
9845 -- AI05-0214). However, suppress the error here if it was
9846 -- already reported on the default expression of the partial
9847 -- view.
8e4dac80
TQ
9848
9849 if Is_Tagged_Type (T)
9850 and then Present (Expression (Parent (D)))
5e5db3b4
GD
9851 and then (not Is_Limited_Type (Current_Scope)
9852 or else Ada_Version < Ada_2012)
8e4dac80
TQ
9853 and then not Error_Posted (Expression (Parent (D)))
9854 then
5e5db3b4
GD
9855 if Ada_Version >= Ada_2012 then
9856 Error_Msg_N
9857 ("discriminants of nonlimited tagged type cannot have"
9858 & " defaults",
9859 Expression (New_D));
9860 else
9861 Error_Msg_N
9862 ("discriminants of tagged type cannot have defaults",
9863 Expression (New_D));
9864 end if;
8e4dac80 9865 end if;
996ae0b0 9866
0ab80019
AC
9867 -- Ada 2005 (AI-230): Access discriminant allowed in
9868 -- non-limited record types.
996ae0b0 9869
0791fbe9 9870 if Ada_Version < Ada_2005 then
6e937c1c 9871
9dfd2ff8
CC
9872 -- This restriction gets applied to the full type here. It
9873 -- has already been applied earlier to the partial view.
6e937c1c
AC
9874
9875 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
9876 end if;
996ae0b0
RK
9877
9878 Next_Discriminant (D);
8e4dac80 9879 Next (New_D);
996ae0b0
RK
9880 end loop;
9881 end;
9882
9883 elsif Present (Discriminant_Specifications (N)) then
fbf5a39b 9884 Process_Discriminants (N, Prev);
996ae0b0
RK
9885 end if;
9886 end Check_Or_Process_Discriminants;
9887
9888 ----------------------
9889 -- Check_Real_Bound --
9890 ----------------------
9891
9892 procedure Check_Real_Bound (Bound : Node_Id) is
9893 begin
9894 if not Is_Real_Type (Etype (Bound)) then
9895 Error_Msg_N
9896 ("bound in real type definition must be of real type", Bound);
9897
9898 elsif not Is_OK_Static_Expression (Bound) then
fbf5a39b
AC
9899 Flag_Non_Static_Expr
9900 ("non-static expression used for real type bound!", Bound);
996ae0b0
RK
9901
9902 else
9903 return;
9904 end if;
9905
9906 Rewrite
9907 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
9908 Analyze (Bound);
9909 Resolve (Bound, Standard_Float);
9910 end Check_Real_Bound;
9911
758c442c
GD
9912 ------------------------------
9913 -- Complete_Private_Subtype --
9914 ------------------------------
9915
9916 procedure Complete_Private_Subtype
9917 (Priv : Entity_Id;
9918 Full : Entity_Id;
9919 Full_Base : Entity_Id;
9920 Related_Nod : Node_Id)
9921 is
9922 Save_Next_Entity : Entity_Id;
9923 Save_Homonym : Entity_Id;
9924
9925 begin
9926 -- Set semantic attributes for (implicit) private subtype completion.
9927 -- If the full type has no discriminants, then it is a copy of the full
9928 -- view of the base. Otherwise, it is a subtype of the base with a
9929 -- possible discriminant constraint. Save and restore the original
9930 -- Next_Entity field of full to ensure that the calls to Copy_Node
9931 -- do not corrupt the entity chain.
9932
9dfd2ff8
CC
9933 -- Note that the type of the full view is the same entity as the type of
9934 -- the partial view. In this fashion, the subtype has access to the
9935 -- correct view of the parent.
996ae0b0
RK
9936
9937 Save_Next_Entity := Next_Entity (Full);
9938 Save_Homonym := Homonym (Priv);
9939
9940 case Ekind (Full_Base) is
996ae0b0
RK
9941 when E_Record_Type |
9942 E_Record_Subtype |
9943 Class_Wide_Kind |
9944 Private_Kind |
9945 Task_Kind |
9946 Protected_Kind =>
9947 Copy_Node (Priv, Full);
9948
9949 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
9950 Set_First_Entity (Full, First_Entity (Full_Base));
9951 Set_Last_Entity (Full, Last_Entity (Full_Base));
9952
9953 when others =>
9954 Copy_Node (Full_Base, Full);
9955 Set_Chars (Full, Chars (Priv));
9956 Conditional_Delay (Full, Priv);
9957 Set_Sloc (Full, Sloc (Priv));
996ae0b0
RK
9958 end case;
9959
9960 Set_Next_Entity (Full, Save_Next_Entity);
9961 Set_Homonym (Full, Save_Homonym);
9962 Set_Associated_Node_For_Itype (Full, Related_Nod);
9963
71d9e9f2 9964 -- Set common attributes for all subtypes
996ae0b0
RK
9965
9966 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
9967
9968 -- The Etype of the full view is inconsistent. Gigi needs to see the
9969 -- structural full view, which is what the current scheme gives:
9970 -- the Etype of the full view is the etype of the full base. However,
9971 -- if the full base is a derived type, the full view then looks like
9972 -- a subtype of the parent, not a subtype of the full base. If instead
9973 -- we write:
9974
9975 -- Set_Etype (Full, Full_Base);
9976
9977 -- then we get inconsistencies in the front-end (confusion between
71d9e9f2 9978 -- views). Several outstanding bugs are related to this ???
996ae0b0
RK
9979
9980 Set_Is_First_Subtype (Full, False);
9981 Set_Scope (Full, Scope (Priv));
9982 Set_Size_Info (Full, Full_Base);
9983 Set_RM_Size (Full, RM_Size (Full_Base));
9984 Set_Is_Itype (Full);
9985
9986 -- A subtype of a private-type-without-discriminants, whose full-view
9987 -- has discriminants with default expressions, is not constrained!
9988
9989 if not Has_Discriminants (Priv) then
9990 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
fbf5a39b
AC
9991
9992 if Has_Discriminants (Full_Base) then
9993 Set_Discriminant_Constraint
9994 (Full, Discriminant_Constraint (Full_Base));
35ae2ed8
AC
9995
9996 -- The partial view may have been indefinite, the full view
9997 -- might not be.
9998
9999 Set_Has_Unknown_Discriminants
10000 (Full, Has_Unknown_Discriminants (Full_Base));
fbf5a39b 10001 end if;
996ae0b0
RK
10002 end if;
10003
10004 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
10005 Set_Depends_On_Private (Full, Has_Private_Component (Full));
10006
a5b62485
AC
10007 -- Freeze the private subtype entity if its parent is delayed, and not
10008 -- already frozen. We skip this processing if the type is an anonymous
10009 -- subtype of a record component, or is the corresponding record of a
10010 -- protected type, since ???
996ae0b0
RK
10011
10012 if not Is_Type (Scope (Full)) then
10013 Set_Has_Delayed_Freeze (Full,
10014 Has_Delayed_Freeze (Full_Base)
71d9e9f2 10015 and then (not Is_Frozen (Full_Base)));
996ae0b0
RK
10016 end if;
10017
10018 Set_Freeze_Node (Full, Empty);
10019 Set_Is_Frozen (Full, False);
10020 Set_Full_View (Priv, Full);
10021
10022 if Has_Discriminants (Full) then
fbf5a39b
AC
10023 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
10024 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
71d9e9f2 10025
996ae0b0
RK
10026 if Has_Unknown_Discriminants (Full) then
10027 Set_Discriminant_Constraint (Full, No_Elist);
10028 end if;
10029 end if;
10030
10031 if Ekind (Full_Base) = E_Record_Type
10032 and then Has_Discriminants (Full_Base)
10033 and then Has_Discriminants (Priv) -- might not, if errors
e6f69614 10034 and then not Has_Unknown_Discriminants (Priv)
996ae0b0
RK
10035 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
10036 then
10037 Create_Constrained_Components
10038 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
10039
10040 -- If the full base is itself derived from private, build a congruent
244e5a2c
AC
10041 -- subtype of its underlying type, for use by the back end. For a
10042 -- constrained record component, the declaration cannot be placed on
9dfd2ff8
CC
10043 -- the component list, but it must nevertheless be built an analyzed, to
10044 -- supply enough information for Gigi to compute the size of component.
996ae0b0
RK
10045
10046 elsif Ekind (Full_Base) in Private_Kind
10047 and then Is_Derived_Type (Full_Base)
10048 and then Has_Discriminants (Full_Base)
24105bab 10049 and then (Ekind (Current_Scope) /= E_Record_Subtype)
996ae0b0 10050 then
244e5a2c
AC
10051 if not Is_Itype (Priv)
10052 and then
10053 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
10054 then
10055 Build_Underlying_Full_View
10056 (Parent (Priv), Full, Etype (Full_Base));
10057
10058 elsif Nkind (Related_Nod) = N_Component_Declaration then
10059 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
10060 end if;
996ae0b0
RK
10061
10062 elsif Is_Record_Type (Full_Base) then
10063
71d9e9f2 10064 -- Show Full is simply a renaming of Full_Base
996ae0b0
RK
10065
10066 Set_Cloned_Subtype (Full, Full_Base);
10067 end if;
10068
a5b62485
AC
10069 -- It is unsafe to share to bounds of a scalar type, because the Itype
10070 -- is elaborated on demand, and if a bound is non-static then different
10071 -- orders of elaboration in different units will lead to different
10072 -- external symbols.
996ae0b0
RK
10073
10074 if Is_Scalar_Type (Full_Base) then
10075 Set_Scalar_Range (Full,
10076 Make_Range (Sloc (Related_Nod),
fbf5a39b
AC
10077 Low_Bound =>
10078 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
10079 High_Bound =>
10080 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
10081
10082 -- This completion inherits the bounds of the full parent, but if
10083 -- the parent is an unconstrained floating point type, so is the
10084 -- completion.
10085
10086 if Is_Floating_Point_Type (Full_Base) then
10087 Set_Includes_Infinities
10088 (Scalar_Range (Full), Has_Infinities (Full_Base));
10089 end if;
996ae0b0
RK
10090 end if;
10091
a5b62485
AC
10092 -- ??? It seems that a lot of fields are missing that should be copied
10093 -- from Full_Base to Full. Here are some that are introduced in a
10094 -- non-disruptive way but a cleanup is necessary.
996ae0b0
RK
10095
10096 if Is_Tagged_Type (Full_Base) then
10097 Set_Is_Tagged_Type (Full);
ef2a63ba
JM
10098 Set_Direct_Primitive_Operations (Full,
10099 Direct_Primitive_Operations (Full_Base));
0fb31b5f
AC
10100
10101 -- Inherit class_wide type of full_base in case the partial view was
10102 -- not tagged. Otherwise it has already been created when the private
10103 -- subtype was analyzed.
10104
10105 if No (Class_Wide_Type (Full)) then
10106 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
10107 end if;
996ae0b0 10108
fc4039b9
ES
10109 -- If this is a subtype of a protected or task type, constrain its
10110 -- corresponding record, unless this is a subtype without constraints,
10111 -- i.e. a simple renaming as with an actual subtype in an instance.
10112
996ae0b0 10113 elsif Is_Concurrent_Type (Full_Base) then
996ae0b0
RK
10114 if Has_Discriminants (Full)
10115 and then Present (Corresponding_Record_Type (Full_Base))
fc4039b9
ES
10116 and then
10117 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
996ae0b0
RK
10118 then
10119 Set_Corresponding_Record_Type (Full,
10120 Constrain_Corresponding_Record
10121 (Full, Corresponding_Record_Type (Full_Base),
10122 Related_Nod, Full_Base));
10123
10124 else
10125 Set_Corresponding_Record_Type (Full,
10126 Corresponding_Record_Type (Full_Base));
10127 end if;
10128 end if;
b4ca2d2c 10129
a043e735
AC
10130 -- Link rep item chain, and also setting of Has_Predicates from private
10131 -- subtype to full subtype, since we will need these on the full subtype
10132 -- to create the predicate function. Note that the full subtype may
10133 -- already have rep items, inherited from the full view of the base
10134 -- type, so we must be sure not to overwrite these entries.
b4ca2d2c 10135
a043e735
AC
10136 declare
10137 Item : Node_Id;
10138 Next_Item : Node_Id;
10139
10140 begin
10141 Item := First_Rep_Item (Full);
10142
10143 -- If no existing rep items on full type, we can just link directly
10144 -- to the list of items on the private type.
10145
10146 if No (Item) then
10147 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
10148
10149 -- Else search to end of items currently linked to the full subtype
10150
10151 else
10152 loop
10153 Next_Item := Next_Rep_Item (Item);
10154 exit when No (Next_Item);
10155 Item := Next_Item;
10156 end loop;
10157
10158 -- And link the private type items at the end of the chain
10159
10160 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
10161 end if;
10162 end;
10163
10164 -- Make sure Has_Predicates is set on full type if it is set on the
10165 -- private type. Note that it may already be set on the full type and
10166 -- if so, we don't want to unset it.
10167
10168 if Has_Predicates (Priv) then
10169 Set_Has_Predicates (Full);
10170 end if;
996ae0b0
RK
10171 end Complete_Private_Subtype;
10172
10173 ----------------------------
10174 -- Constant_Redeclaration --
10175 ----------------------------
10176
10177 procedure Constant_Redeclaration
10178 (Id : Entity_Id;
10179 N : Node_Id;
10180 T : out Entity_Id)
10181 is
10182 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
10183 Obj_Def : constant Node_Id := Object_Definition (N);
10184 New_T : Entity_Id;
10185
57193e09
TQ
10186 procedure Check_Possible_Deferred_Completion
10187 (Prev_Id : Entity_Id;
10188 Prev_Obj_Def : Node_Id;
10189 Curr_Obj_Def : Node_Id);
10190 -- Determine whether the two object definitions describe the partial
10191 -- and the full view of a constrained deferred constant. Generate
10192 -- a subtype for the full view and verify that it statically matches
10193 -- the subtype of the partial view.
10194
07fc65c4 10195 procedure Check_Recursive_Declaration (Typ : Entity_Id);
9dfd2ff8
CC
10196 -- If deferred constant is an access type initialized with an allocator,
10197 -- check whether there is an illegal recursion in the definition,
10198 -- through a default value of some record subcomponent. This is normally
10199 -- detected when generating init procs, but requires this additional
10200 -- mechanism when expansion is disabled.
07fc65c4 10201
57193e09
TQ
10202 ----------------------------------------
10203 -- Check_Possible_Deferred_Completion --
10204 ----------------------------------------
10205
10206 procedure Check_Possible_Deferred_Completion
10207 (Prev_Id : Entity_Id;
10208 Prev_Obj_Def : Node_Id;
10209 Curr_Obj_Def : Node_Id)
10210 is
10211 begin
10212 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
10213 and then Present (Constraint (Prev_Obj_Def))
10214 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
10215 and then Present (Constraint (Curr_Obj_Def))
10216 then
10217 declare
10218 Loc : constant Source_Ptr := Sloc (N);
092ef350
RD
10219 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
10220 Decl : constant Node_Id :=
57193e09 10221 Make_Subtype_Declaration (Loc,
092ef350
RD
10222 Defining_Identifier => Def_Id,
10223 Subtype_Indication =>
57193e09
TQ
10224 Relocate_Node (Curr_Obj_Def));
10225
10226 begin
10227 Insert_Before_And_Analyze (N, Decl);
10228 Set_Etype (Id, Def_Id);
10229
10230 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
10231 Error_Msg_Sloc := Sloc (Prev_Id);
10232 Error_Msg_N ("subtype does not statically match deferred " &
10233 "declaration#", N);
10234 end if;
10235 end;
10236 end if;
10237 end Check_Possible_Deferred_Completion;
10238
fbf5a39b
AC
10239 ---------------------------------
10240 -- Check_Recursive_Declaration --
10241 ---------------------------------
10242
07fc65c4
GB
10243 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
10244 Comp : Entity_Id;
10245
10246 begin
10247 if Is_Record_Type (Typ) then
10248 Comp := First_Component (Typ);
07fc65c4
GB
10249 while Present (Comp) loop
10250 if Comes_From_Source (Comp) then
10251 if Present (Expression (Parent (Comp)))
10252 and then Is_Entity_Name (Expression (Parent (Comp)))
10253 and then Entity (Expression (Parent (Comp))) = Prev
10254 then
10255 Error_Msg_Sloc := Sloc (Parent (Comp));
10256 Error_Msg_NE
10257 ("illegal circularity with declaration for&#",
10258 N, Comp);
10259 return;
10260
10261 elsif Is_Record_Type (Etype (Comp)) then
10262 Check_Recursive_Declaration (Etype (Comp));
10263 end if;
10264 end if;
10265
10266 Next_Component (Comp);
10267 end loop;
10268 end if;
10269 end Check_Recursive_Declaration;
10270
10271 -- Start of processing for Constant_Redeclaration
10272
996ae0b0
RK
10273 begin
10274 if Nkind (Parent (Prev)) = N_Object_Declaration then
10275 if Nkind (Object_Definition
10276 (Parent (Prev))) = N_Subtype_Indication
10277 then
10278 -- Find type of new declaration. The constraints of the two
10279 -- views must match statically, but there is no point in
10280 -- creating an itype for the full view.
10281
10282 if Nkind (Obj_Def) = N_Subtype_Indication then
10283 Find_Type (Subtype_Mark (Obj_Def));
10284 New_T := Entity (Subtype_Mark (Obj_Def));
10285
10286 else
10287 Find_Type (Obj_Def);
10288 New_T := Entity (Obj_Def);
10289 end if;
10290
10291 T := Etype (Prev);
10292
10293 else
10294 -- The full view may impose a constraint, even if the partial
10295 -- view does not, so construct the subtype.
10296
10297 New_T := Find_Type_Of_Object (Obj_Def, N);
10298 T := New_T;
10299 end if;
10300
10301 else
71d9e9f2 10302 -- Current declaration is illegal, diagnosed below in Enter_Name
996ae0b0
RK
10303
10304 T := Empty;
10305 New_T := Any_Type;
10306 end if;
10307
4f08579c
AC
10308 -- If previous full declaration or a renaming declaration exists, or if
10309 -- a homograph is present, let Enter_Name handle it, either with an
10310 -- error or with the removal of an overridden implicit subprogram.
996ae0b0
RK
10311
10312 if Ekind (Prev) /= E_Constant
4f08579c 10313 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
996ae0b0 10314 or else Present (Expression (Parent (Prev)))
07fc65c4 10315 or else Present (Full_View (Prev))
996ae0b0
RK
10316 then
10317 Enter_Name (Id);
10318
758c442c
GD
10319 -- Verify that types of both declarations match, or else that both types
10320 -- are anonymous access types whose designated subtypes statically match
10321 -- (as allowed in Ada 2005 by AI-385).
996ae0b0 10322
758c442c
GD
10323 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
10324 and then
10325 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
10326 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
ce4a6e84
RD
10327 or else Is_Access_Constant (Etype (New_T)) /=
10328 Is_Access_Constant (Etype (Prev))
10329 or else Can_Never_Be_Null (Etype (New_T)) /=
10330 Can_Never_Be_Null (Etype (Prev))
10331 or else Null_Exclusion_Present (Parent (Prev)) /=
10332 Null_Exclusion_Present (Parent (Id))
758c442c
GD
10333 or else not Subtypes_Statically_Match
10334 (Designated_Type (Etype (Prev)),
10335 Designated_Type (Etype (New_T))))
10336 then
996ae0b0
RK
10337 Error_Msg_Sloc := Sloc (Prev);
10338 Error_Msg_N ("type does not match declaration#", N);
10339 Set_Full_View (Prev, Id);
10340 Set_Etype (Id, Any_Type);
10341
ce4a6e84
RD
10342 elsif
10343 Null_Exclusion_Present (Parent (Prev))
10344 and then not Null_Exclusion_Present (N)
10345 then
10346 Error_Msg_Sloc := Sloc (Prev);
10347 Error_Msg_N ("null-exclusion does not match declaration#", N);
10348 Set_Full_View (Prev, Id);
10349 Set_Etype (Id, Any_Type);
10350
996ae0b0
RK
10351 -- If so, process the full constant declaration
10352
10353 else
57193e09
TQ
10354 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
10355 -- the deferred declaration is constrained, then the subtype defined
10356 -- by the subtype_indication in the full declaration shall match it
10357 -- statically.
10358
10359 Check_Possible_Deferred_Completion
10360 (Prev_Id => Prev,
10361 Prev_Obj_Def => Object_Definition (Parent (Prev)),
10362 Curr_Obj_Def => Obj_Def);
10363
996ae0b0
RK
10364 Set_Full_View (Prev, Id);
10365 Set_Is_Public (Id, Is_Public (Prev));
10366 Set_Is_Internal (Id);
10367 Append_Entity (Id, Current_Scope);
10368
10369 -- Check ALIASED present if present before (RM 7.4(7))
10370
10371 if Is_Aliased (Prev)
10372 and then not Aliased_Present (N)
10373 then
10374 Error_Msg_Sloc := Sloc (Prev);
10375 Error_Msg_N ("ALIASED required (see declaration#)", N);
10376 end if;
10377
07fc65c4
GB
10378 -- Check that placement is in private part and that the incomplete
10379 -- declaration appeared in the visible part.
996ae0b0 10380
b16d9747 10381 if Ekind (Current_Scope) = E_Package
996ae0b0
RK
10382 and then not In_Private_Part (Current_Scope)
10383 then
10384 Error_Msg_Sloc := Sloc (Prev);
ed2233dc
AC
10385 Error_Msg_N
10386 ("full constant for declaration#"
10387 & " must be in private part", N);
07fc65c4
GB
10388
10389 elsif Ekind (Current_Scope) = E_Package
bce79204
AC
10390 and then
10391 List_Containing (Parent (Prev)) /=
10392 Visible_Declarations
10393 (Specification (Unit_Declaration_Node (Current_Scope)))
07fc65c4
GB
10394 then
10395 Error_Msg_N
10396 ("deferred constant must be declared in visible part",
10397 Parent (Prev));
10398 end if;
10399
10400 if Is_Access_Type (T)
10401 and then Nkind (Expression (N)) = N_Allocator
10402 then
10403 Check_Recursive_Declaration (Designated_Type (T));
996ae0b0
RK
10404 end if;
10405 end if;
10406 end Constant_Redeclaration;
10407
10408 ----------------------
10409 -- Constrain_Access --
10410 ----------------------
10411
10412 procedure Constrain_Access
10413 (Def_Id : in out Entity_Id;
10414 S : Node_Id;
10415 Related_Nod : Node_Id)
10416 is
10417 T : constant Entity_Id := Entity (Subtype_Mark (S));
10418 Desig_Type : constant Entity_Id := Designated_Type (T);
10419 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
10420 Constraint_OK : Boolean := True;
10421
758c442c
GD
10422 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
10423 -- Simple predicate to test for defaulted discriminants
10424 -- Shouldn't this be in sem_util???
10425
10426 ---------------------------------
10427 -- Has_Defaulted_Discriminants --
10428 ---------------------------------
10429
10430 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
10431 begin
10432 return Has_Discriminants (Typ)
10433 and then Present (First_Discriminant (Typ))
10434 and then Present
10435 (Discriminant_Default_Value (First_Discriminant (Typ)));
10436 end Has_Defaulted_Discriminants;
10437
10438 -- Start of processing for Constrain_Access
10439
996ae0b0
RK
10440 begin
10441 if Is_Array_Type (Desig_Type) then
10442 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
10443
10444 elsif (Is_Record_Type (Desig_Type)
10445 or else Is_Incomplete_Or_Private_Type (Desig_Type))
10446 and then not Is_Constrained (Desig_Type)
10447 then
71d9e9f2
ES
10448 -- ??? The following code is a temporary kludge to ignore a
10449 -- discriminant constraint on access type if it is constraining
10450 -- the current record. Avoid creating the implicit subtype of the
10451 -- record we are currently compiling since right now, we cannot
10452 -- handle these. For now, just return the access type itself.
996ae0b0
RK
10453
10454 if Desig_Type = Current_Scope
10455 and then No (Def_Id)
10456 then
10457 Set_Ekind (Desig_Subtype, E_Record_Subtype);
10458 Def_Id := Entity (Subtype_Mark (S));
10459
71d9e9f2
ES
10460 -- This call added to ensure that the constraint is analyzed
10461 -- (needed for a B test). Note that we still return early from
10462 -- this procedure to avoid recursive processing. ???
996ae0b0
RK
10463
10464 Constrain_Discriminated_Type
10465 (Desig_Subtype, S, Related_Nod, For_Access => True);
996ae0b0
RK
10466 return;
10467 end if;
10468
f29b857f 10469 if (Ekind (T) = E_General_Access_Type
0791fbe9 10470 or else Ada_Version >= Ada_2005)
07fc65c4
GB
10471 and then Has_Private_Declaration (Desig_Type)
10472 and then In_Open_Scopes (Scope (Desig_Type))
f29b857f 10473 and then Has_Discriminants (Desig_Type)
07fc65c4
GB
10474 then
10475 -- Enforce rule that the constraint is illegal if there is
10476 -- an unconstrained view of the designated type. This means
10477 -- that the partial view (either a private type declaration or
10478 -- a derivation from a private type) has no discriminants.
10479 -- (Defect Report 8652/0008, Technical Corrigendum 1, checked
10480 -- by ACATS B371001).
fea9e956 10481
758c442c
GD
10482 -- Rule updated for Ada 2005: the private type is said to have
10483 -- a constrained partial view, given that objects of the type
f29b857f
ES
10484 -- can be declared. Furthermore, the rule applies to all access
10485 -- types, unlike the rule concerning default discriminants.
07fc65c4
GB
10486
10487 declare
fbf5a39b
AC
10488 Pack : constant Node_Id :=
10489 Unit_Declaration_Node (Scope (Desig_Type));
07fc65c4
GB
10490 Decls : List_Id;
10491 Decl : Node_Id;
10492
10493 begin
10494 if Nkind (Pack) = N_Package_Declaration then
10495 Decls := Visible_Declarations (Specification (Pack));
10496 Decl := First (Decls);
07fc65c4
GB
10497 while Present (Decl) loop
10498 if (Nkind (Decl) = N_Private_Type_Declaration
10499 and then
10500 Chars (Defining_Identifier (Decl)) =
10501 Chars (Desig_Type))
10502
10503 or else
10504 (Nkind (Decl) = N_Full_Type_Declaration
10505 and then
10506 Chars (Defining_Identifier (Decl)) =
10507 Chars (Desig_Type)
10508 and then Is_Derived_Type (Desig_Type)
10509 and then
10510 Has_Private_Declaration (Etype (Desig_Type)))
10511 then
10512 if No (Discriminant_Specifications (Decl)) then
10513 Error_Msg_N
758c442c
GD
10514 ("cannot constrain general access type if " &
10515 "designated type has constrained partial view",
10516 S);
07fc65c4
GB
10517 end if;
10518
10519 exit;
10520 end if;
10521
10522 Next (Decl);
10523 end loop;
10524 end if;
10525 end;
10526 end if;
10527
996ae0b0
RK
10528 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
10529 For_Access => True);
10530
10531 elsif (Is_Task_Type (Desig_Type)
10532 or else Is_Protected_Type (Desig_Type))
10533 and then not Is_Constrained (Desig_Type)
10534 then
10535 Constrain_Concurrent
10536 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
10537
10538 else
10539 Error_Msg_N ("invalid constraint on access type", S);
10540 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
10541 Constraint_OK := False;
10542 end if;
10543
10544 if No (Def_Id) then
10545 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
10546 else
10547 Set_Ekind (Def_Id, E_Access_Subtype);
10548 end if;
10549
10550 if Constraint_OK then
10551 Set_Etype (Def_Id, Base_Type (T));
10552
10553 if Is_Private_Type (Desig_Type) then
10554 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
10555 end if;
10556 else
10557 Set_Etype (Def_Id, Any_Type);
10558 end if;
10559
10560 Set_Size_Info (Def_Id, T);
10561 Set_Is_Constrained (Def_Id, Constraint_OK);
10562 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
10563 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10564 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
10565
c6823a20 10566 Conditional_Delay (Def_Id, T);
758c442c 10567
9dfd2ff8
CC
10568 -- AI-363 : Subtypes of general access types whose designated types have
10569 -- default discriminants are disallowed. In instances, the rule has to
10570 -- be checked against the actual, of which T is the subtype. In a
10571 -- generic body, the rule is checked assuming that the actual type has
10572 -- defaulted discriminants.
758c442c 10573
0791fbe9 10574 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
758c442c
GD
10575 if Ekind (Base_Type (T)) = E_General_Access_Type
10576 and then Has_Defaulted_Discriminants (Desig_Type)
10577 then
0791fbe9 10578 if Ada_Version < Ada_2005 then
fea9e956
ES
10579 Error_Msg_N
10580 ("access subtype of general access type would not " &
10581 "be allowed in Ada 2005?", S);
10582 else
10583 Error_Msg_N
308e6f3a 10584 ("access subtype of general access type not allowed", S);
fea9e956
ES
10585 end if;
10586
88b32fc3 10587 Error_Msg_N ("\discriminants have defaults", S);
758c442c
GD
10588
10589 elsif Is_Access_Type (T)
10590 and then Is_Generic_Type (Desig_Type)
10591 and then Has_Discriminants (Desig_Type)
10592 and then In_Package_Body (Current_Scope)
10593 then
0791fbe9 10594 if Ada_Version < Ada_2005 then
fea9e956
ES
10595 Error_Msg_N
10596 ("access subtype would not be allowed in generic body " &
10597 "in Ada 2005?", S);
10598 else
10599 Error_Msg_N
10600 ("access subtype not allowed in generic body", S);
10601 end if;
10602
758c442c 10603 Error_Msg_N
88b32fc3 10604 ("\designated type is a discriminated formal", S);
758c442c
GD
10605 end if;
10606 end if;
996ae0b0
RK
10607 end Constrain_Access;
10608
10609 ---------------------
10610 -- Constrain_Array --
10611 ---------------------
10612
10613 procedure Constrain_Array
10614 (Def_Id : in out Entity_Id;
10615 SI : Node_Id;
10616 Related_Nod : Node_Id;
10617 Related_Id : Entity_Id;
10618 Suffix : Character)
10619 is
10620 C : constant Node_Id := Constraint (SI);
10621 Number_Of_Constraints : Nat := 0;
10622 Index : Node_Id;
10623 S, T : Entity_Id;
10624 Constraint_OK : Boolean := True;
10625
10626 begin
10627 T := Entity (Subtype_Mark (SI));
10628
10629 if Ekind (T) in Access_Kind then
10630 T := Designated_Type (T);
10631 end if;
10632
10633 -- If an index constraint follows a subtype mark in a subtype indication
10634 -- then the type or subtype denoted by the subtype mark must not already
10635 -- impose an index constraint. The subtype mark must denote either an
10636 -- unconstrained array type or an access type whose designated type
10637 -- is such an array type... (RM 3.6.1)
10638
10639 if Is_Constrained (T) then
ed2233dc 10640 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
996ae0b0
RK
10641 Constraint_OK := False;
10642
10643 else
10644 S := First (Constraints (C));
996ae0b0
RK
10645 while Present (S) loop
10646 Number_Of_Constraints := Number_Of_Constraints + 1;
10647 Next (S);
10648 end loop;
10649
10650 -- In either case, the index constraint must provide a discrete
10651 -- range for each index of the array type and the type of each
10652 -- discrete range must be the same as that of the corresponding
10653 -- index. (RM 3.6.1)
10654
10655 if Number_Of_Constraints /= Number_Dimensions (T) then
10656 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
10657 Constraint_OK := False;
10658
10659 else
10660 S := First (Constraints (C));
10661 Index := First_Index (T);
10662 Analyze (Index);
10663
10664 -- Apply constraints to each index type
10665
10666 for J in 1 .. Number_Of_Constraints loop
10667 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
10668 Next (Index);
10669 Next (S);
10670 end loop;
10671
10672 end if;
10673 end if;
10674
10675 if No (Def_Id) then
10676 Def_Id :=
10677 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
fbf5a39b
AC
10678 Set_Parent (Def_Id, Related_Nod);
10679
996ae0b0
RK
10680 else
10681 Set_Ekind (Def_Id, E_Array_Subtype);
10682 end if;
10683
10684 Set_Size_Info (Def_Id, (T));
10685 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10686 Set_Etype (Def_Id, Base_Type (T));
10687
10688 if Constraint_OK then
10689 Set_First_Index (Def_Id, First (Constraints (C)));
758c442c
GD
10690 else
10691 Set_First_Index (Def_Id, First_Index (T));
996ae0b0
RK
10692 end if;
10693
996ae0b0
RK
10694 Set_Is_Constrained (Def_Id, True);
10695 Set_Is_Aliased (Def_Id, Is_Aliased (T));
10696 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10697
10698 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
10699 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
10700
2b73cf68
JM
10701 -- A subtype does not inherit the packed_array_type of is parent. We
10702 -- need to initialize the attribute because if Def_Id is previously
10703 -- analyzed through a limited_with clause, it will have the attributes
10704 -- of an incomplete type, one of which is an Elist that overlaps the
10705 -- Packed_Array_Type field.
10706
10707 Set_Packed_Array_Type (Def_Id, Empty);
10708
10709 -- Build a freeze node if parent still needs one. Also make sure that
10710 -- the Depends_On_Private status is set because the subtype will need
10711 -- reprocessing at the time the base type does, and also we must set a
10712 -- conditional delay.
996ae0b0 10713
c6823a20
EB
10714 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10715 Conditional_Delay (Def_Id, T);
996ae0b0
RK
10716 end Constrain_Array;
10717
10718 ------------------------------
10719 -- Constrain_Component_Type --
10720 ------------------------------
10721
10722 function Constrain_Component_Type
c6823a20 10723 (Comp : Entity_Id;
996ae0b0
RK
10724 Constrained_Typ : Entity_Id;
10725 Related_Node : Node_Id;
10726 Typ : Entity_Id;
b0f26df5 10727 Constraints : Elist_Id) return Entity_Id
996ae0b0 10728 is
c6823a20
EB
10729 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
10730 Compon_Type : constant Entity_Id := Etype (Comp);
996ae0b0
RK
10731
10732 function Build_Constrained_Array_Type
b0f26df5 10733 (Old_Type : Entity_Id) return Entity_Id;
3b42c566 10734 -- If Old_Type is an array type, one of whose indexes is constrained
a5b62485
AC
10735 -- by a discriminant, build an Itype whose constraint replaces the
10736 -- discriminant with its value in the constraint.
996ae0b0
RK
10737
10738 function Build_Constrained_Discriminated_Type
b0f26df5 10739 (Old_Type : Entity_Id) return Entity_Id;
71d9e9f2 10740 -- Ditto for record components
996ae0b0
RK
10741
10742 function Build_Constrained_Access_Type
b0f26df5 10743 (Old_Type : Entity_Id) return Entity_Id;
996ae0b0
RK
10744 -- Ditto for access types. Makes use of previous two functions, to
10745 -- constrain designated type.
10746
10747 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
10748 -- T is an array or discriminated type, C is a list of constraints
10749 -- that apply to T. This routine builds the constrained subtype.
10750
10751 function Is_Discriminant (Expr : Node_Id) return Boolean;
71d9e9f2 10752 -- Returns True if Expr is a discriminant
996ae0b0 10753
07fc65c4 10754 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
71d9e9f2 10755 -- Find the value of discriminant Discrim in Constraint
996ae0b0
RK
10756
10757 -----------------------------------
10758 -- Build_Constrained_Access_Type --
10759 -----------------------------------
10760
10761 function Build_Constrained_Access_Type
b0f26df5 10762 (Old_Type : Entity_Id) return Entity_Id
996ae0b0
RK
10763 is
10764 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
10765 Itype : Entity_Id;
10766 Desig_Subtype : Entity_Id;
10767 Scop : Entity_Id;
10768
10769 begin
10770 -- if the original access type was not embedded in the enclosing
10771 -- type definition, there is no need to produce a new access
10772 -- subtype. In fact every access type with an explicit constraint
10773 -- generates an itype whose scope is the enclosing record.
10774
10775 if not Is_Type (Scope (Old_Type)) then
10776 return Old_Type;
10777
10778 elsif Is_Array_Type (Desig_Type) then
10779 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
10780
10781 elsif Has_Discriminants (Desig_Type) then
10782
10783 -- This may be an access type to an enclosing record type for
10784 -- which we are constructing the constrained components. Return
10785 -- the enclosing record subtype. This is not always correct,
10786 -- but avoids infinite recursion. ???
10787
10788 Desig_Subtype := Any_Type;
10789
10790 for J in reverse 0 .. Scope_Stack.Last loop
10791 Scop := Scope_Stack.Table (J).Entity;
10792
10793 if Is_Type (Scop)
10794 and then Base_Type (Scop) = Base_Type (Desig_Type)
10795 then
10796 Desig_Subtype := Scop;
10797 end if;
10798
10799 exit when not Is_Type (Scop);
10800 end loop;
10801
10802 if Desig_Subtype = Any_Type then
10803 Desig_Subtype :=
10804 Build_Constrained_Discriminated_Type (Desig_Type);
10805 end if;
10806
10807 else
10808 return Old_Type;
10809 end if;
10810
10811 if Desig_Subtype /= Desig_Type then
71d9e9f2 10812
996ae0b0
RK
10813 -- The Related_Node better be here or else we won't be able
10814 -- to attach new itypes to a node in the tree.
10815
10816 pragma Assert (Present (Related_Node));
10817
10818 Itype := Create_Itype (E_Access_Subtype, Related_Node);
10819
10820 Set_Etype (Itype, Base_Type (Old_Type));
10821 Set_Size_Info (Itype, (Old_Type));
10822 Set_Directly_Designated_Type (Itype, Desig_Subtype);
10823 Set_Depends_On_Private (Itype, Has_Private_Component
10824 (Old_Type));
10825 Set_Is_Access_Constant (Itype, Is_Access_Constant
10826 (Old_Type));
10827
10828 -- The new itype needs freezing when it depends on a not frozen
10829 -- type and the enclosing subtype needs freezing.
10830
10831 if Has_Delayed_Freeze (Constrained_Typ)
10832 and then not Is_Frozen (Constrained_Typ)
10833 then
10834 Conditional_Delay (Itype, Base_Type (Old_Type));
10835 end if;
10836
10837 return Itype;
10838
10839 else
10840 return Old_Type;
10841 end if;
10842 end Build_Constrained_Access_Type;
10843
10844 ----------------------------------
10845 -- Build_Constrained_Array_Type --
10846 ----------------------------------
10847
10848 function Build_Constrained_Array_Type
b0f26df5 10849 (Old_Type : Entity_Id) return Entity_Id
996ae0b0
RK
10850 is
10851 Lo_Expr : Node_Id;
10852 Hi_Expr : Node_Id;
10853 Old_Index : Node_Id;
10854 Range_Node : Node_Id;
10855 Constr_List : List_Id;
10856
10857 Need_To_Create_Itype : Boolean := False;
10858
10859 begin
10860 Old_Index := First_Index (Old_Type);
10861 while Present (Old_Index) loop
10862 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10863
10864 if Is_Discriminant (Lo_Expr)
10865 or else Is_Discriminant (Hi_Expr)
10866 then
10867 Need_To_Create_Itype := True;
10868 end if;
10869
10870 Next_Index (Old_Index);
10871 end loop;
10872
10873 if Need_To_Create_Itype then
10874 Constr_List := New_List;
10875
10876 Old_Index := First_Index (Old_Type);
10877 while Present (Old_Index) loop
10878 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10879
10880 if Is_Discriminant (Lo_Expr) then
07fc65c4 10881 Lo_Expr := Get_Discr_Value (Lo_Expr);
996ae0b0
RK
10882 end if;
10883
10884 if Is_Discriminant (Hi_Expr) then
07fc65c4 10885 Hi_Expr := Get_Discr_Value (Hi_Expr);
996ae0b0
RK
10886 end if;
10887
10888 Range_Node :=
10889 Make_Range
10890 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
10891
10892 Append (Range_Node, To => Constr_List);
10893
10894 Next_Index (Old_Index);
10895 end loop;
10896
10897 return Build_Subtype (Old_Type, Constr_List);
10898
10899 else
10900 return Old_Type;
10901 end if;
10902 end Build_Constrained_Array_Type;
10903
10904 ------------------------------------------
10905 -- Build_Constrained_Discriminated_Type --
10906 ------------------------------------------
10907
10908 function Build_Constrained_Discriminated_Type
b0f26df5 10909 (Old_Type : Entity_Id) return Entity_Id
996ae0b0
RK
10910 is
10911 Expr : Node_Id;
10912 Constr_List : List_Id;
10913 Old_Constraint : Elmt_Id;
10914
10915 Need_To_Create_Itype : Boolean := False;
10916
10917 begin
10918 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10919 while Present (Old_Constraint) loop
10920 Expr := Node (Old_Constraint);
10921
10922 if Is_Discriminant (Expr) then
10923 Need_To_Create_Itype := True;
10924 end if;
10925
10926 Next_Elmt (Old_Constraint);
10927 end loop;
10928
10929 if Need_To_Create_Itype then
10930 Constr_List := New_List;
10931
10932 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10933 while Present (Old_Constraint) loop
10934 Expr := Node (Old_Constraint);
10935
10936 if Is_Discriminant (Expr) then
07fc65c4 10937 Expr := Get_Discr_Value (Expr);
996ae0b0
RK
10938 end if;
10939
10940 Append (New_Copy_Tree (Expr), To => Constr_List);
10941
10942 Next_Elmt (Old_Constraint);
10943 end loop;
10944
10945 return Build_Subtype (Old_Type, Constr_List);
10946
10947 else
10948 return Old_Type;
10949 end if;
10950 end Build_Constrained_Discriminated_Type;
10951
10952 -------------------
10953 -- Build_Subtype --
10954 -------------------
10955
10956 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
10957 Indic : Node_Id;
10958 Subtyp_Decl : Node_Id;
10959 Def_Id : Entity_Id;
10960 Btyp : Entity_Id := Base_Type (T);
10961
10962 begin
a5b62485
AC
10963 -- The Related_Node better be here or else we won't be able to
10964 -- attach new itypes to a node in the tree.
996ae0b0
RK
10965
10966 pragma Assert (Present (Related_Node));
10967
10968 -- If the view of the component's type is incomplete or private
10969 -- with unknown discriminants, then the constraint must be applied
10970 -- to the full type.
10971
10972 if Has_Unknown_Discriminants (Btyp)
10973 and then Present (Underlying_Type (Btyp))
10974 then
10975 Btyp := Underlying_Type (Btyp);
10976 end if;
10977
10978 Indic :=
10979 Make_Subtype_Indication (Loc,
10980 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
10981 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
10982
10983 Def_Id := Create_Itype (Ekind (T), Related_Node);
10984
10985 Subtyp_Decl :=
10986 Make_Subtype_Declaration (Loc,
10987 Defining_Identifier => Def_Id,
10988 Subtype_Indication => Indic);
24105bab 10989
996ae0b0
RK
10990 Set_Parent (Subtyp_Decl, Parent (Related_Node));
10991
ffe9aba8 10992 -- Itypes must be analyzed with checks off (see package Itypes)
996ae0b0
RK
10993
10994 Analyze (Subtyp_Decl, Suppress => All_Checks);
10995
10996 return Def_Id;
10997 end Build_Subtype;
10998
07fc65c4
GB
10999 ---------------------
11000 -- Get_Discr_Value --
11001 ---------------------
996ae0b0 11002
07fc65c4 11003 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
9dfd2ff8
CC
11004 D : Entity_Id;
11005 E : Elmt_Id;
996ae0b0
RK
11006
11007 begin
07fc65c4
GB
11008 -- The discriminant may be declared for the type, in which case we
11009 -- find it by iterating over the list of discriminants. If the
11010 -- discriminant is inherited from a parent type, it appears as the
11011 -- corresponding discriminant of the current type. This will be the
11012 -- case when constraining an inherited component whose constraint is
11013 -- given by a discriminant of the parent.
996ae0b0 11014
9dfd2ff8
CC
11015 D := First_Discriminant (Typ);
11016 E := First_Elmt (Constraints);
88b32fc3 11017
07fc65c4 11018 while Present (D) loop
996ae0b0 11019 if D = Entity (Discrim)
88b32fc3 11020 or else D = CR_Discriminant (Entity (Discrim))
996ae0b0
RK
11021 or else Corresponding_Discriminant (D) = Entity (Discrim)
11022 then
11023 return Node (E);
11024 end if;
11025
11026 Next_Discriminant (D);
11027 Next_Elmt (E);
11028 end loop;
11029
027dbed8 11030 -- The Corresponding_Discriminant mechanism is incomplete, because
07fc65c4 11031 -- the correspondence between new and old discriminants is not one
a5b62485
AC
11032 -- to one: one new discriminant can constrain several old ones. In
11033 -- that case, scan sequentially the stored_constraint, the list of
11034 -- discriminants of the parents, and the constraints.
ab8bfb64
ES
11035 -- Previous code checked for the present of the Stored_Constraint
11036 -- list for the derived type, but did not use it at all. Should it
11037 -- be present when the component is a discriminated task type?
07fc65c4
GB
11038
11039 if Is_Derived_Type (Typ)
07fc65c4
GB
11040 and then Scope (Entity (Discrim)) = Etype (Typ)
11041 then
11042 D := First_Discriminant (Etype (Typ));
11043 E := First_Elmt (Constraints);
07fc65c4
GB
11044 while Present (D) loop
11045 if D = Entity (Discrim) then
11046 return Node (E);
11047 end if;
11048
11049 Next_Discriminant (D);
11050 Next_Elmt (E);
07fc65c4
GB
11051 end loop;
11052 end if;
11053
996ae0b0
RK
11054 -- Something is wrong if we did not find the value
11055
11056 raise Program_Error;
07fc65c4 11057 end Get_Discr_Value;
996ae0b0
RK
11058
11059 ---------------------
11060 -- Is_Discriminant --
11061 ---------------------
11062
11063 function Is_Discriminant (Expr : Node_Id) return Boolean is
11064 Discrim_Scope : Entity_Id;
11065
11066 begin
11067 if Denotes_Discriminant (Expr) then
11068 Discrim_Scope := Scope (Entity (Expr));
11069
11070 -- Either we have a reference to one of Typ's discriminants,
11071
11072 pragma Assert (Discrim_Scope = Typ
11073
11074 -- or to the discriminants of the parent type, in the case
11075 -- of a derivation of a tagged type with variants.
11076
11077 or else Discrim_Scope = Etype (Typ)
11078 or else Full_View (Discrim_Scope) = Etype (Typ)
11079
11080 -- or same as above for the case where the discriminants
11081 -- were declared in Typ's private view.
11082
11083 or else (Is_Private_Type (Discrim_Scope)
11084 and then Chars (Discrim_Scope) = Chars (Typ))
11085
11086 -- or else we are deriving from the full view and the
11087 -- discriminant is declared in the private entity.
11088
11089 or else (Is_Private_Type (Typ)
33931112 11090 and then Chars (Discrim_Scope) = Chars (Typ))
996ae0b0 11091
88b32fc3
BD
11092 -- Or we are constrained the corresponding record of a
11093 -- synchronized type that completes a private declaration.
11094
11095 or else (Is_Concurrent_Record_Type (Typ)
11096 and then
11097 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
11098
996ae0b0
RK
11099 -- or we have a class-wide type, in which case make sure the
11100 -- discriminant found belongs to the root type.
11101
11102 or else (Is_Class_Wide_Type (Typ)
33931112 11103 and then Etype (Typ) = Discrim_Scope));
996ae0b0
RK
11104
11105 return True;
11106 end if;
11107
ffe9aba8 11108 -- In all other cases we have something wrong
996ae0b0
RK
11109
11110 return False;
11111 end Is_Discriminant;
11112
11113 -- Start of processing for Constrain_Component_Type
11114
11115 begin
c6823a20
EB
11116 if Nkind (Parent (Comp)) = N_Component_Declaration
11117 and then Comes_From_Source (Parent (Comp))
11118 and then Comes_From_Source
11119 (Subtype_Indication (Component_Definition (Parent (Comp))))
11120 and then
11121 Is_Entity_Name
11122 (Subtype_Indication (Component_Definition (Parent (Comp))))
11123 then
11124 return Compon_Type;
11125
11126 elsif Is_Array_Type (Compon_Type) then
996ae0b0
RK
11127 return Build_Constrained_Array_Type (Compon_Type);
11128
11129 elsif Has_Discriminants (Compon_Type) then
11130 return Build_Constrained_Discriminated_Type (Compon_Type);
11131
11132 elsif Is_Access_Type (Compon_Type) then
11133 return Build_Constrained_Access_Type (Compon_Type);
996ae0b0 11134
c6823a20
EB
11135 else
11136 return Compon_Type;
11137 end if;
996ae0b0
RK
11138 end Constrain_Component_Type;
11139
11140 --------------------------
11141 -- Constrain_Concurrent --
11142 --------------------------
11143
11144 -- For concurrent types, the associated record value type carries the same
11145 -- discriminants, so when we constrain a concurrent type, we must constrain
950d3e7d 11146 -- the corresponding record type as well.
996ae0b0
RK
11147
11148 procedure Constrain_Concurrent
11149 (Def_Id : in out Entity_Id;
11150 SI : Node_Id;
11151 Related_Nod : Node_Id;
11152 Related_Id : Entity_Id;
11153 Suffix : Character)
11154 is
11155 T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
11156 T_Val : Entity_Id;
11157
11158 begin
11159 if Ekind (T_Ent) in Access_Kind then
11160 T_Ent := Designated_Type (T_Ent);
11161 end if;
11162
11163 T_Val := Corresponding_Record_Type (T_Ent);
11164
11165 if Present (T_Val) then
11166
11167 if No (Def_Id) then
11168 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11169 end if;
11170
11171 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11172
11173 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11174 Set_Corresponding_Record_Type (Def_Id,
11175 Constrain_Corresponding_Record
11176 (Def_Id, T_Val, Related_Nod, Related_Id));
11177
11178 else
11179 -- If there is no associated record, expansion is disabled and this
11180 -- is a generic context. Create a subtype in any case, so that
11181 -- semantic analysis can proceed.
11182
11183 if No (Def_Id) then
11184 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11185 end if;
11186
11187 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11188 end if;
11189 end Constrain_Concurrent;
11190
11191 ------------------------------------
11192 -- Constrain_Corresponding_Record --
11193 ------------------------------------
11194
11195 function Constrain_Corresponding_Record
11196 (Prot_Subt : Entity_Id;
11197 Corr_Rec : Entity_Id;
11198 Related_Nod : Node_Id;
b0f26df5 11199 Related_Id : Entity_Id) return Entity_Id
996ae0b0 11200 is
71d9e9f2
ES
11201 T_Sub : constant Entity_Id :=
11202 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
996ae0b0
RK
11203
11204 begin
71d9e9f2 11205 Set_Etype (T_Sub, Corr_Rec);
71d9e9f2
ES
11206 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
11207 Set_Is_Constrained (T_Sub, True);
11208 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
11209 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
996ae0b0 11210
88b32fc3
BD
11211 -- As elsewhere, we do not want to create a freeze node for this itype
11212 -- if it is created for a constrained component of an enclosing record
11213 -- because references to outer discriminants will appear out of scope.
11214
11215 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
11216 Conditional_Delay (T_Sub, Corr_Rec);
11217 else
11218 Set_Is_Frozen (T_Sub);
11219 end if;
996ae0b0
RK
11220
11221 if Has_Discriminants (Prot_Subt) then -- False only if errors.
71d9e9f2
ES
11222 Set_Discriminant_Constraint
11223 (T_Sub, Discriminant_Constraint (Prot_Subt));
fbf5a39b 11224 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
71d9e9f2
ES
11225 Create_Constrained_Components
11226 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
996ae0b0
RK
11227 end if;
11228
11229 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
11230
11231 return T_Sub;
11232 end Constrain_Corresponding_Record;
11233
11234 -----------------------
11235 -- Constrain_Decimal --
11236 -----------------------
11237
07fc65c4 11238 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
11239 T : constant Entity_Id := Entity (Subtype_Mark (S));
11240 C : constant Node_Id := Constraint (S);
11241 Loc : constant Source_Ptr := Sloc (C);
11242 Range_Expr : Node_Id;
11243 Digits_Expr : Node_Id;
11244 Digits_Val : Uint;
11245 Bound_Val : Ureal;
11246
11247 begin
11248 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
11249
11250 if Nkind (C) = N_Range_Constraint then
11251 Range_Expr := Range_Expression (C);
11252 Digits_Val := Digits_Value (T);
11253
11254 else
11255 pragma Assert (Nkind (C) = N_Digits_Constraint);
7ff2d234
AC
11256
11257 -- Digits constraint is not allowed in SPARK or ALFA
11258
11259 if Formal_Verification_Mode
11260 and then Comes_From_Source (Original_Node (S))
11261 then
11262 Error_Msg_F ("|~~digits constraint is not allowed", S);
11263 end if;
11264
11265 -- Proceed with analysis
11266
996ae0b0
RK
11267 Digits_Expr := Digits_Expression (C);
11268 Analyze_And_Resolve (Digits_Expr, Any_Integer);
11269
11270 Check_Digits_Expression (Digits_Expr);
11271 Digits_Val := Expr_Value (Digits_Expr);
11272
11273 if Digits_Val > Digits_Value (T) then
11274 Error_Msg_N
11275 ("digits expression is incompatible with subtype", C);
11276 Digits_Val := Digits_Value (T);
11277 end if;
11278
11279 if Present (Range_Constraint (C)) then
11280 Range_Expr := Range_Expression (Range_Constraint (C));
11281 else
11282 Range_Expr := Empty;
11283 end if;
11284 end if;
11285
11286 Set_Etype (Def_Id, Base_Type (T));
11287 Set_Size_Info (Def_Id, (T));
11288 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11289 Set_Delta_Value (Def_Id, Delta_Value (T));
11290 Set_Scale_Value (Def_Id, Scale_Value (T));
11291 Set_Small_Value (Def_Id, Small_Value (T));
11292 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
11293 Set_Digits_Value (Def_Id, Digits_Val);
11294
11295 -- Manufacture range from given digits value if no range present
11296
11297 if No (Range_Expr) then
11298 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
11299 Range_Expr :=
71d9e9f2
ES
11300 Make_Range (Loc,
11301 Low_Bound =>
11302 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
11303 High_Bound =>
11304 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
996ae0b0
RK
11305 end if;
11306
07fc65c4 11307 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
996ae0b0
RK
11308 Set_Discrete_RM_Size (Def_Id);
11309
11310 -- Unconditionally delay the freeze, since we cannot set size
11311 -- information in all cases correctly until the freeze point.
11312
11313 Set_Has_Delayed_Freeze (Def_Id);
11314 end Constrain_Decimal;
11315
11316 ----------------------------------
11317 -- Constrain_Discriminated_Type --
11318 ----------------------------------
11319
11320 procedure Constrain_Discriminated_Type
11321 (Def_Id : Entity_Id;
11322 S : Node_Id;
11323 Related_Nod : Node_Id;
11324 For_Access : Boolean := False)
11325 is
07fc65c4 11326 E : constant Entity_Id := Entity (Subtype_Mark (S));
996ae0b0
RK
11327 T : Entity_Id;
11328 C : Node_Id;
11329 Elist : Elist_Id := New_Elmt_List;
11330
11331 procedure Fixup_Bad_Constraint;
11332 -- This is called after finding a bad constraint, and after having
11333 -- posted an appropriate error message. The mission is to leave the
11334 -- entity T in as reasonable state as possible!
11335
fbf5a39b
AC
11336 --------------------------
11337 -- Fixup_Bad_Constraint --
11338 --------------------------
11339
996ae0b0
RK
11340 procedure Fixup_Bad_Constraint is
11341 begin
11342 -- Set a reasonable Ekind for the entity. For an incomplete type,
11343 -- we can't do much, but for other types, we can set the proper
11344 -- corresponding subtype kind.
11345
11346 if Ekind (T) = E_Incomplete_Type then
11347 Set_Ekind (Def_Id, Ekind (T));
11348 else
11349 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
11350 end if;
11351
491016e5
GD
11352 -- Set Etype to the known type, to reduce chances of cascaded errors
11353
11354 Set_Etype (Def_Id, E);
996ae0b0
RK
11355 Set_Error_Posted (Def_Id);
11356 end Fixup_Bad_Constraint;
11357
11358 -- Start of processing for Constrain_Discriminated_Type
11359
11360 begin
11361 C := Constraint (S);
11362
11363 -- A discriminant constraint is only allowed in a subtype indication,
11364 -- after a subtype mark. This subtype mark must denote either a type
11365 -- with discriminants, or an access type whose designated type is a
11366 -- type with discriminants. A discriminant constraint specifies the
11367 -- values of these discriminants (RM 3.7.2(5)).
11368
11369 T := Base_Type (Entity (Subtype_Mark (S)));
11370
11371 if Ekind (T) in Access_Kind then
11372 T := Designated_Type (T);
11373 end if;
11374
88b32fc3
BD
11375 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
11376 -- Avoid generating an error for access-to-incomplete subtypes.
11377
0791fbe9 11378 if Ada_Version >= Ada_2005
88b32fc3
BD
11379 and then Ekind (T) = E_Incomplete_Type
11380 and then Nkind (Parent (S)) = N_Subtype_Declaration
11381 and then not Is_Itype (Def_Id)
11382 then
11383 -- A little sanity check, emit an error message if the type
11384 -- has discriminants to begin with. Type T may be a regular
11385 -- incomplete type or imported via a limited with clause.
11386
11387 if Has_Discriminants (T)
11388 or else
11389 (From_With_Type (T)
11390 and then Present (Non_Limited_View (T))
11391 and then Nkind (Parent (Non_Limited_View (T))) =
11392 N_Full_Type_Declaration
11393 and then Present (Discriminant_Specifications
11394 (Parent (Non_Limited_View (T)))))
11395 then
11396 Error_Msg_N
11397 ("(Ada 2005) incomplete subtype may not be constrained", C);
11398 else
ed2233dc 11399 Error_Msg_N ("invalid constraint: type has no discriminant", C);
88b32fc3
BD
11400 end if;
11401
11402 Fixup_Bad_Constraint;
11403 return;
11404
8a6a52dc
AC
11405 -- Check that the type has visible discriminants. The type may be
11406 -- a private type with unknown discriminants whose full view has
11407 -- discriminants which are invisible.
11408
88b32fc3 11409 elsif not Has_Discriminants (T)
8a6a52dc
AC
11410 or else
11411 (Has_Unknown_Discriminants (T)
11412 and then Is_Private_Type (T))
11413 then
996ae0b0
RK
11414 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11415 Fixup_Bad_Constraint;
11416 return;
11417
07fc65c4
GB
11418 elsif Is_Constrained (E)
11419 or else (Ekind (E) = E_Class_Wide_Subtype
11420 and then Present (Discriminant_Constraint (E)))
11421 then
996ae0b0
RK
11422 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
11423 Fixup_Bad_Constraint;
11424 return;
11425 end if;
11426
11427 -- T may be an unconstrained subtype (e.g. a generic actual).
11428 -- Constraint applies to the base type.
11429
11430 T := Base_Type (T);
11431
11432 Elist := Build_Discriminant_Constraints (T, S);
11433
11434 -- If the list returned was empty we had an error in building the
11435 -- discriminant constraint. We have also already signalled an error
11436 -- in the incomplete type case
11437
11438 if Is_Empty_Elmt_List (Elist) then
11439 Fixup_Bad_Constraint;
11440 return;
11441 end if;
11442
11443 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
11444 end Constrain_Discriminated_Type;
11445
11446 ---------------------------
11447 -- Constrain_Enumeration --
11448 ---------------------------
11449
07fc65c4 11450 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
11451 T : constant Entity_Id := Entity (Subtype_Mark (S));
11452 C : constant Node_Id := Constraint (S);
11453
11454 begin
11455 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11456
11457 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
11458
11459 Set_Etype (Def_Id, Base_Type (T));
11460 Set_Size_Info (Def_Id, (T));
11461 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11462 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11463
07fc65c4 11464 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
996ae0b0
RK
11465
11466 Set_Discrete_RM_Size (Def_Id);
996ae0b0
RK
11467 end Constrain_Enumeration;
11468
11469 ----------------------
11470 -- Constrain_Float --
11471 ----------------------
11472
07fc65c4 11473 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
11474 T : constant Entity_Id := Entity (Subtype_Mark (S));
11475 C : Node_Id;
11476 D : Node_Id;
11477 Rais : Node_Id;
11478
11479 begin
11480 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
11481
11482 Set_Etype (Def_Id, Base_Type (T));
11483 Set_Size_Info (Def_Id, (T));
11484 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11485
11486 -- Process the constraint
11487
11488 C := Constraint (S);
11489
11490 -- Digits constraint present
11491
11492 if Nkind (C) = N_Digits_Constraint then
7ff2d234
AC
11493
11494 -- Digits constraint is not allowed in SPARK or ALFA
11495
11496 if Formal_Verification_Mode
11497 and then Comes_From_Source (Original_Node (S))
11498 then
11499 Error_Msg_F ("|~~digits constraint is not allowed", S);
11500 end if;
11501
11502 -- Proceed with analysis
11503
5f3ab6fb
AC
11504 Check_Restriction (No_Obsolescent_Features, C);
11505
fbf5a39b
AC
11506 if Warn_On_Obsolescent_Feature then
11507 Error_Msg_N
11508 ("subtype digits constraint is an " &
dc06abec 11509 "obsolescent feature (RM J.3(8))?", C);
fbf5a39b
AC
11510 end if;
11511
996ae0b0
RK
11512 D := Digits_Expression (C);
11513 Analyze_And_Resolve (D, Any_Integer);
11514 Check_Digits_Expression (D);
11515 Set_Digits_Value (Def_Id, Expr_Value (D));
11516
11517 -- Check that digits value is in range. Obviously we can do this
11518 -- at compile time, but it is strictly a runtime check, and of
11519 -- course there is an ACVC test that checks this!
11520
11521 if Digits_Value (Def_Id) > Digits_Value (T) then
11522 Error_Msg_Uint_1 := Digits_Value (T);
11523 Error_Msg_N ("?digits value is too large, maximum is ^", D);
07fc65c4
GB
11524 Rais :=
11525 Make_Raise_Constraint_Error (Sloc (D),
11526 Reason => CE_Range_Check_Failed);
996ae0b0
RK
11527 Insert_Action (Declaration_Node (Def_Id), Rais);
11528 end if;
11529
11530 C := Range_Constraint (C);
11531
11532 -- No digits constraint present
11533
11534 else
11535 Set_Digits_Value (Def_Id, Digits_Value (T));
11536 end if;
11537
11538 -- Range constraint present
11539
11540 if Nkind (C) = N_Range_Constraint then
07fc65c4 11541 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
996ae0b0
RK
11542
11543 -- No range constraint present
11544
11545 else
11546 pragma Assert (No (C));
11547 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11548 end if;
11549
11550 Set_Is_Constrained (Def_Id);
11551 end Constrain_Float;
11552
11553 ---------------------
11554 -- Constrain_Index --
11555 ---------------------
11556
11557 procedure Constrain_Index
11558 (Index : Node_Id;
11559 S : Node_Id;
11560 Related_Nod : Node_Id;
11561 Related_Id : Entity_Id;
11562 Suffix : Character;
11563 Suffix_Index : Nat)
11564 is
7324bf49
AC
11565 Def_Id : Entity_Id;
11566 R : Node_Id := Empty;
11567 T : constant Entity_Id := Etype (Index);
996ae0b0
RK
11568
11569 begin
11570 if Nkind (S) = N_Range
fbf5a39b
AC
11571 or else
11572 (Nkind (S) = N_Attribute_Reference
11573 and then Attribute_Name (S) = Name_Range)
996ae0b0 11574 then
ffe9aba8 11575 -- A Range attribute will transformed into N_Range by Resolve
996ae0b0
RK
11576
11577 Analyze (S);
11578 Set_Etype (S, T);
11579 R := S;
11580
7324bf49 11581 Process_Range_Expr_In_Decl (R, T, Empty_List);
996ae0b0
RK
11582
11583 if not Error_Posted (S)
11584 and then
11585 (Nkind (S) /= N_Range
891a6e79
AC
11586 or else not Covers (T, (Etype (Low_Bound (S))))
11587 or else not Covers (T, (Etype (High_Bound (S)))))
996ae0b0
RK
11588 then
11589 if Base_Type (T) /= Any_Type
11590 and then Etype (Low_Bound (S)) /= Any_Type
11591 and then Etype (High_Bound (S)) /= Any_Type
11592 then
11593 Error_Msg_N ("range expected", S);
11594 end if;
11595 end if;
11596
11597 elsif Nkind (S) = N_Subtype_Indication then
71d9e9f2
ES
11598
11599 -- The parser has verified that this is a discrete indication
996ae0b0
RK
11600
11601 Resolve_Discrete_Subtype_Indication (S, T);
11602 R := Range_Expression (Constraint (S));
11603
4230bdb7
AC
11604 -- Capture values of bounds and generate temporaries for them if
11605 -- needed, since checks may cause duplication of the expressions
11606 -- which must not be reevaluated.
11607
11608 if Expander_Active then
11609 Force_Evaluation (Low_Bound (R));
11610 Force_Evaluation (High_Bound (R));
11611 end if;
11612
996ae0b0
RK
11613 elsif Nkind (S) = N_Discriminant_Association then
11614
71d9e9f2 11615 -- Syntactically valid in subtype indication
996ae0b0
RK
11616
11617 Error_Msg_N ("invalid index constraint", S);
11618 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11619 return;
11620
11621 -- Subtype_Mark case, no anonymous subtypes to construct
11622
11623 else
11624 Analyze (S);
11625
11626 if Is_Entity_Name (S) then
996ae0b0
RK
11627 if not Is_Type (Entity (S)) then
11628 Error_Msg_N ("expect subtype mark for index constraint", S);
11629
11630 elsif Base_Type (Entity (S)) /= Base_Type (T) then
11631 Wrong_Type (S, Base_Type (T));
ea034236
AC
11632
11633 -- Check error of subtype with predicate in index constraint
11634
ed00f472
RD
11635 else
11636 Bad_Predicated_Subtype_Use
11637 ("subtype& has predicate, not allowed in index constraint",
ea034236 11638 S, Entity (S));
996ae0b0
RK
11639 end if;
11640
11641 return;
11642
11643 else
11644 Error_Msg_N ("invalid index constraint", S);
11645 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11646 return;
11647 end if;
11648 end if;
11649
11650 Def_Id :=
11651 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
11652
11653 Set_Etype (Def_Id, Base_Type (T));
11654
11655 if Is_Modular_Integer_Type (T) then
11656 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11657
11658 elsif Is_Integer_Type (T) then
11659 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11660
11661 else
11662 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11663 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
3428cb9f 11664 Set_First_Literal (Def_Id, First_Literal (T));
996ae0b0
RK
11665 end if;
11666
11667 Set_Size_Info (Def_Id, (T));
11668 Set_RM_Size (Def_Id, RM_Size (T));
11669 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11670
996ae0b0
RK
11671 Set_Scalar_Range (Def_Id, R);
11672
11673 Set_Etype (S, Def_Id);
11674 Set_Discrete_RM_Size (Def_Id);
11675 end Constrain_Index;
11676
11677 -----------------------
11678 -- Constrain_Integer --
11679 -----------------------
11680
07fc65c4 11681 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
11682 T : constant Entity_Id := Entity (Subtype_Mark (S));
11683 C : constant Node_Id := Constraint (S);
11684
11685 begin
07fc65c4 11686 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
996ae0b0
RK
11687
11688 if Is_Modular_Integer_Type (T) then
11689 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11690 else
11691 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11692 end if;
11693
11694 Set_Etype (Def_Id, Base_Type (T));
11695 Set_Size_Info (Def_Id, (T));
11696 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11697 Set_Discrete_RM_Size (Def_Id);
996ae0b0
RK
11698 end Constrain_Integer;
11699
11700 ------------------------------
11701 -- Constrain_Ordinary_Fixed --
11702 ------------------------------
11703
07fc65c4 11704 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
11705 T : constant Entity_Id := Entity (Subtype_Mark (S));
11706 C : Node_Id;
11707 D : Node_Id;
11708 Rais : Node_Id;
11709
11710 begin
11711 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
11712 Set_Etype (Def_Id, Base_Type (T));
11713 Set_Size_Info (Def_Id, (T));
11714 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11715 Set_Small_Value (Def_Id, Small_Value (T));
11716
11717 -- Process the constraint
11718
11719 C := Constraint (S);
11720
11721 -- Delta constraint present
11722
11723 if Nkind (C) = N_Delta_Constraint then
7ff2d234
AC
11724 -- Delta constraint is not allowed in SPARK or ALFA
11725
11726 if Formal_Verification_Mode
11727 and then Comes_From_Source (Original_Node (S))
11728 then
11729 Error_Msg_F ("|~~delta constraint is not allowed", S);
11730 end if;
11731
11732 -- Proceed with analysis
11733
5f3ab6fb
AC
11734 Check_Restriction (No_Obsolescent_Features, C);
11735
fbf5a39b
AC
11736 if Warn_On_Obsolescent_Feature then
11737 Error_Msg_S
11738 ("subtype delta constraint is an " &
dc06abec 11739 "obsolescent feature (RM J.3(7))?");
fbf5a39b
AC
11740 end if;
11741
996ae0b0
RK
11742 D := Delta_Expression (C);
11743 Analyze_And_Resolve (D, Any_Real);
11744 Check_Delta_Expression (D);
11745 Set_Delta_Value (Def_Id, Expr_Value_R (D));
11746
11747 -- Check that delta value is in range. Obviously we can do this
11748 -- at compile time, but it is strictly a runtime check, and of
11749 -- course there is an ACVC test that checks this!
11750
11751 if Delta_Value (Def_Id) < Delta_Value (T) then
11752 Error_Msg_N ("?delta value is too small", D);
07fc65c4
GB
11753 Rais :=
11754 Make_Raise_Constraint_Error (Sloc (D),
11755 Reason => CE_Range_Check_Failed);
996ae0b0
RK
11756 Insert_Action (Declaration_Node (Def_Id), Rais);
11757 end if;
11758
11759 C := Range_Constraint (C);
11760
11761 -- No delta constraint present
11762
11763 else
11764 Set_Delta_Value (Def_Id, Delta_Value (T));
11765 end if;
11766
11767 -- Range constraint present
11768
11769 if Nkind (C) = N_Range_Constraint then
07fc65c4 11770 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
996ae0b0
RK
11771
11772 -- No range constraint present
11773
11774 else
11775 pragma Assert (No (C));
11776 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11777
11778 end if;
11779
11780 Set_Discrete_RM_Size (Def_Id);
11781
11782 -- Unconditionally delay the freeze, since we cannot set size
11783 -- information in all cases correctly until the freeze point.
11784
11785 Set_Has_Delayed_Freeze (Def_Id);
11786 end Constrain_Ordinary_Fixed;
11787
dc06abec
RD
11788 -----------------------
11789 -- Contain_Interface --
11790 -----------------------
11791
11792 function Contain_Interface
11793 (Iface : Entity_Id;
11794 Ifaces : Elist_Id) return Boolean
11795 is
11796 Iface_Elmt : Elmt_Id;
11797
11798 begin
11799 if Present (Ifaces) then
11800 Iface_Elmt := First_Elmt (Ifaces);
11801 while Present (Iface_Elmt) loop
11802 if Node (Iface_Elmt) = Iface then
11803 return True;
11804 end if;
11805
11806 Next_Elmt (Iface_Elmt);
11807 end loop;
11808 end if;
11809
11810 return False;
11811 end Contain_Interface;
11812
996ae0b0
RK
11813 ---------------------------
11814 -- Convert_Scalar_Bounds --
11815 ---------------------------
11816
11817 procedure Convert_Scalar_Bounds
11818 (N : Node_Id;
11819 Parent_Type : Entity_Id;
11820 Derived_Type : Entity_Id;
11821 Loc : Source_Ptr)
11822 is
11823 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
11824
11825 Lo : Node_Id;
11826 Hi : Node_Id;
11827 Rng : Node_Id;
11828
11829 begin
199c6a10
AC
11830 -- Defend against previous errors
11831
11832 if No (Scalar_Range (Derived_Type)) then
11833 return;
11834 end if;
11835
996ae0b0
RK
11836 Lo := Build_Scalar_Bound
11837 (Type_Low_Bound (Derived_Type),
07fc65c4 11838 Parent_Type, Implicit_Base);
996ae0b0
RK
11839
11840 Hi := Build_Scalar_Bound
11841 (Type_High_Bound (Derived_Type),
07fc65c4 11842 Parent_Type, Implicit_Base);
996ae0b0
RK
11843
11844 Rng :=
11845 Make_Range (Loc,
11846 Low_Bound => Lo,
11847 High_Bound => Hi);
11848
11849 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
11850
11851 Set_Parent (Rng, N);
11852 Set_Scalar_Range (Derived_Type, Rng);
11853
11854 -- Analyze the bounds
11855
11856 Analyze_And_Resolve (Lo, Implicit_Base);
11857 Analyze_And_Resolve (Hi, Implicit_Base);
11858
11859 -- Analyze the range itself, except that we do not analyze it if
11860 -- the bounds are real literals, and we have a fixed-point type.
11861 -- The reason for this is that we delay setting the bounds in this
11862 -- case till we know the final Small and Size values (see circuit
11863 -- in Freeze.Freeze_Fixed_Point_Type for further details).
11864
11865 if Is_Fixed_Point_Type (Parent_Type)
11866 and then Nkind (Lo) = N_Real_Literal
11867 and then Nkind (Hi) = N_Real_Literal
11868 then
11869 return;
11870
ffe9aba8 11871 -- Here we do the analysis of the range
996ae0b0
RK
11872
11873 -- Note: we do this manually, since if we do a normal Analyze and
11874 -- Resolve call, there are problems with the conversions used for
11875 -- the derived type range.
11876
11877 else
11878 Set_Etype (Rng, Implicit_Base);
11879 Set_Analyzed (Rng, True);
11880 end if;
11881 end Convert_Scalar_Bounds;
11882
11883 -------------------
11884 -- Copy_And_Swap --
11885 -------------------
11886
fbf5a39b 11887 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
996ae0b0
RK
11888 begin
11889 -- Initialize new full declaration entity by copying the pertinent
11890 -- fields of the corresponding private declaration entity.
11891
996ae0b0
RK
11892 -- We temporarily set Ekind to a value appropriate for a type to
11893 -- avoid assert failures in Einfo from checking for setting type
11894 -- attributes on something that is not a type. Ekind (Priv) is an
11895 -- appropriate choice, since it allowed the attributes to be set
11896 -- in the first place. This Ekind value will be modified later.
11897
11898 Set_Ekind (Full, Ekind (Priv));
11899
11900 -- Also set Etype temporarily to Any_Type, again, in the absence
11901 -- of errors, it will be properly reset, and if there are errors,
11902 -- then we want a value of Any_Type to remain.
11903
11904 Set_Etype (Full, Any_Type);
11905
11906 -- Now start copying attributes
11907
11908 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
11909
11910 if Has_Discriminants (Full) then
11911 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
fbf5a39b 11912 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
996ae0b0
RK
11913 end if;
11914
fbf5a39b 11915 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
996ae0b0
RK
11916 Set_Homonym (Full, Homonym (Priv));
11917 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
11918 Set_Is_Public (Full, Is_Public (Priv));
11919 Set_Is_Pure (Full, Is_Pure (Priv));
11920 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
4a214958 11921 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
fea9e956
ES
11922 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
11923 Set_Has_Pragma_Unreferenced_Objects
11924 (Full, Has_Pragma_Unreferenced_Objects
11925 (Priv));
996ae0b0
RK
11926
11927 Conditional_Delay (Full, Priv);
11928
11929 if Is_Tagged_Type (Full) then
ef2a63ba
JM
11930 Set_Direct_Primitive_Operations (Full,
11931 Direct_Primitive_Operations (Priv));
996ae0b0 11932
d347f572 11933 if Is_Base_Type (Priv) then
996ae0b0
RK
11934 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
11935 end if;
11936 end if;
11937
11938 Set_Is_Volatile (Full, Is_Volatile (Priv));
fbf5a39b 11939 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
996ae0b0
RK
11940 Set_Scope (Full, Scope (Priv));
11941 Set_Next_Entity (Full, Next_Entity (Priv));
11942 Set_First_Entity (Full, First_Entity (Priv));
11943 Set_Last_Entity (Full, Last_Entity (Priv));
11944
a5b62485
AC
11945 -- If access types have been recorded for later handling, keep them in
11946 -- the full view so that they get handled when the full view freeze
11947 -- node is expanded.
996ae0b0
RK
11948
11949 if Present (Freeze_Node (Priv))
11950 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
11951 then
11952 Ensure_Freeze_Node (Full);
fbf5a39b
AC
11953 Set_Access_Types_To_Process
11954 (Freeze_Node (Full),
11955 Access_Types_To_Process (Freeze_Node (Priv)));
996ae0b0 11956 end if;
996ae0b0 11957
308e6f3a
RW
11958 -- Swap the two entities. Now Private is the full type entity and Full
11959 -- is the private one. They will be swapped back at the end of the
11960 -- private part. This swapping ensures that the entity that is visible
11961 -- in the private part is the full declaration.
996ae0b0 11962
fbf5a39b
AC
11963 Exchange_Entities (Priv, Full);
11964 Append_Entity (Full, Scope (Full));
11965 end Copy_And_Swap;
996ae0b0 11966
fbf5a39b
AC
11967 -------------------------------------
11968 -- Copy_Array_Base_Type_Attributes --
11969 -------------------------------------
996ae0b0 11970
fbf5a39b
AC
11971 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
11972 begin
11973 Set_Component_Alignment (T1, Component_Alignment (T2));
11974 Set_Component_Type (T1, Component_Type (T2));
11975 Set_Component_Size (T1, Component_Size (T2));
11976 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
11977 Set_Finalize_Storage_Only (T1, Finalize_Storage_Only (T2));
11978 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
11979 Set_Has_Task (T1, Has_Task (T2));
11980 Set_Is_Packed (T1, Is_Packed (T2));
11981 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
11982 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
11983 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
11984 end Copy_Array_Base_Type_Attributes;
11985
11986 -----------------------------------
11987 -- Copy_Array_Subtype_Attributes --
11988 -----------------------------------
11989
11990 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
11991 begin
11992 Set_Size_Info (T1, T2);
11993
11994 Set_First_Index (T1, First_Index (T2));
11995 Set_Is_Aliased (T1, Is_Aliased (T2));
11996 Set_Is_Atomic (T1, Is_Atomic (T2));
11997 Set_Is_Volatile (T1, Is_Volatile (T2));
11998 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
11999 Set_Is_Constrained (T1, Is_Constrained (T2));
12000 Set_Depends_On_Private (T1, Has_Private_Component (T2));
12001 Set_First_Rep_Item (T1, First_Rep_Item (T2));
12002 Set_Convention (T1, Convention (T2));
12003 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
12004 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
ff7139c3 12005 Set_Packed_Array_Type (T1, Packed_Array_Type (T2));
fbf5a39b
AC
12006 end Copy_Array_Subtype_Attributes;
12007
12008 -----------------------------------
12009 -- Create_Constrained_Components --
12010 -----------------------------------
12011
12012 procedure Create_Constrained_Components
12013 (Subt : Entity_Id;
12014 Decl_Node : Node_Id;
12015 Typ : Entity_Id;
12016 Constraints : Elist_Id)
12017 is
12018 Loc : constant Source_Ptr := Sloc (Subt);
12019 Comp_List : constant Elist_Id := New_Elmt_List;
12020 Parent_Type : constant Entity_Id := Etype (Typ);
12021 Assoc_List : constant List_Id := New_List;
12022 Discr_Val : Elmt_Id;
12023 Errors : Boolean;
12024 New_C : Entity_Id;
12025 Old_C : Entity_Id;
12026 Is_Static : Boolean := True;
12027
12028 procedure Collect_Fixed_Components (Typ : Entity_Id);
0da2c8ac 12029 -- Collect parent type components that do not appear in a variant part
fbf5a39b
AC
12030
12031 procedure Create_All_Components;
ffe9aba8 12032 -- Iterate over Comp_List to create the components of the subtype
fbf5a39b
AC
12033
12034 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
12035 -- Creates a new component from Old_Compon, copying all the fields from
12036 -- it, including its Etype, inserts the new component in the Subt entity
12037 -- chain and returns the new component.
12038
12039 function Is_Variant_Record (T : Entity_Id) return Boolean;
12040 -- If true, and discriminants are static, collect only components from
12041 -- variants selected by discriminant values.
12042
12043 ------------------------------
996ae0b0
RK
12044 -- Collect_Fixed_Components --
12045 ------------------------------
12046
12047 procedure Collect_Fixed_Components (Typ : Entity_Id) is
12048 begin
a5b62485
AC
12049 -- Build association list for discriminants, and find components of the
12050 -- variant part selected by the values of the discriminants.
996ae0b0
RK
12051
12052 Old_C := First_Discriminant (Typ);
12053 Discr_Val := First_Elmt (Constraints);
996ae0b0
RK
12054 while Present (Old_C) loop
12055 Append_To (Assoc_List,
12056 Make_Component_Association (Loc,
12057 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
12058 Expression => New_Copy (Node (Discr_Val))));
12059
12060 Next_Elmt (Discr_Val);
12061 Next_Discriminant (Old_C);
12062 end loop;
12063
12064 -- The tag, and the possible parent and controller components
12065 -- are unconditionally in the subtype.
12066
12067 if Is_Tagged_Type (Typ)
12068 or else Has_Controlled_Component (Typ)
12069 then
12070 Old_C := First_Component (Typ);
996ae0b0
RK
12071 while Present (Old_C) loop
12072 if Chars ((Old_C)) = Name_uTag
12073 or else Chars ((Old_C)) = Name_uParent
12074 or else Chars ((Old_C)) = Name_uController
12075 then
12076 Append_Elmt (Old_C, Comp_List);
12077 end if;
12078
12079 Next_Component (Old_C);
12080 end loop;
12081 end if;
12082 end Collect_Fixed_Components;
12083
12084 ---------------------------
12085 -- Create_All_Components --
12086 ---------------------------
12087
12088 procedure Create_All_Components is
12089 Comp : Elmt_Id;
12090
12091 begin
12092 Comp := First_Elmt (Comp_List);
996ae0b0
RK
12093 while Present (Comp) loop
12094 Old_C := Node (Comp);
12095 New_C := Create_Component (Old_C);
12096
12097 Set_Etype
12098 (New_C,
12099 Constrain_Component_Type
c6823a20 12100 (Old_C, Subt, Decl_Node, Typ, Constraints));
996ae0b0
RK
12101 Set_Is_Public (New_C, Is_Public (Subt));
12102
12103 Next_Elmt (Comp);
12104 end loop;
12105 end Create_All_Components;
12106
12107 ----------------------
12108 -- Create_Component --
12109 ----------------------
12110
12111 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
fbf5a39b 12112 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
996ae0b0
RK
12113
12114 begin
c0bca7e1
EB
12115 if Ekind (Old_Compon) = E_Discriminant
12116 and then Is_Completely_Hidden (Old_Compon)
12117 then
c0bca7e1 12118 -- This is a shadow discriminant created for a discriminant of
c9e7bd8e
AC
12119 -- the parent type, which needs to be present in the subtype.
12120 -- Give the shadow discriminant an internal name that cannot
12121 -- conflict with that of visible components.
c0bca7e1
EB
12122
12123 Set_Chars (New_Compon, New_Internal_Name ('C'));
12124 end if;
12125
653da906
RD
12126 -- Set the parent so we have a proper link for freezing etc. This is
12127 -- not a real parent pointer, since of course our parent does not own
12128 -- up to us and reference us, we are an illegitimate child of the
12129 -- original parent!
996ae0b0
RK
12130
12131 Set_Parent (New_Compon, Parent (Old_Compon));
12132
653da906
RD
12133 -- If the old component's Esize was already determined and is a
12134 -- static value, then the new component simply inherits it. Otherwise
12135 -- the old component's size may require run-time determination, but
12136 -- the new component's size still might be statically determinable
12137 -- (if, for example it has a static constraint). In that case we want
12138 -- Layout_Type to recompute the component's size, so we reset its
12139 -- size and positional fields.
12140
12141 if Frontend_Layout_On_Target
12142 and then not Known_Static_Esize (Old_Compon)
12143 then
12144 Set_Esize (New_Compon, Uint_0);
12145 Init_Normalized_First_Bit (New_Compon);
12146 Init_Normalized_Position (New_Compon);
12147 Init_Normalized_Position_Max (New_Compon);
12148 end if;
12149
996ae0b0 12150 -- We do not want this node marked as Comes_From_Source, since
653da906
RD
12151 -- otherwise it would get first class status and a separate cross-
12152 -- reference line would be generated. Illegitimate children do not
12153 -- rate such recognition.
996ae0b0
RK
12154
12155 Set_Comes_From_Source (New_Compon, False);
12156
653da906
RD
12157 -- But it is a real entity, and a birth certificate must be properly
12158 -- registered by entering it into the entity list.
996ae0b0
RK
12159
12160 Enter_Name (New_Compon);
653da906 12161
996ae0b0
RK
12162 return New_Compon;
12163 end Create_Component;
12164
12165 -----------------------
12166 -- Is_Variant_Record --
12167 -----------------------
12168
12169 function Is_Variant_Record (T : Entity_Id) return Boolean is
12170 begin
12171 return Nkind (Parent (T)) = N_Full_Type_Declaration
12172 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
12173 and then Present (Component_List (Type_Definition (Parent (T))))
2b73cf68
JM
12174 and then
12175 Present
12176 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
996ae0b0
RK
12177 end Is_Variant_Record;
12178
12179 -- Start of processing for Create_Constrained_Components
12180
12181 begin
12182 pragma Assert (Subt /= Base_Type (Subt));
12183 pragma Assert (Typ = Base_Type (Typ));
12184
12185 Set_First_Entity (Subt, Empty);
12186 Set_Last_Entity (Subt, Empty);
12187
12188 -- Check whether constraint is fully static, in which case we can
12189 -- optimize the list of components.
12190
12191 Discr_Val := First_Elmt (Constraints);
996ae0b0 12192 while Present (Discr_Val) loop
996ae0b0
RK
12193 if not Is_OK_Static_Expression (Node (Discr_Val)) then
12194 Is_Static := False;
12195 exit;
12196 end if;
12197
12198 Next_Elmt (Discr_Val);
12199 end loop;
12200
88b32fc3
BD
12201 Set_Has_Static_Discriminants (Subt, Is_Static);
12202
2b73cf68 12203 Push_Scope (Subt);
996ae0b0 12204
71d9e9f2 12205 -- Inherit the discriminants of the parent type
996ae0b0 12206
c0bca7e1
EB
12207 Add_Discriminants : declare
12208 Num_Disc : Int;
12209 Num_Gird : Int;
12210
12211 begin
12212 Num_Disc := 0;
12213 Old_C := First_Discriminant (Typ);
12214
12215 while Present (Old_C) loop
12216 Num_Disc := Num_Disc + 1;
12217 New_C := Create_Component (Old_C);
12218 Set_Is_Public (New_C, Is_Public (Subt));
12219 Next_Discriminant (Old_C);
12220 end loop;
12221
12222 -- For an untagged derived subtype, the number of discriminants may
12223 -- be smaller than the number of inherited discriminants, because
c9e7bd8e
AC
12224 -- several of them may be renamed by a single new discriminant or
12225 -- constrained. In this case, add the hidden discriminants back into
12226 -- the subtype, because they need to be present if the optimizer of
12227 -- the GCC 4.x back-end decides to break apart assignments between
12228 -- objects using the parent view into member-wise assignments.
c0bca7e1
EB
12229
12230 Num_Gird := 0;
12231
12232 if Is_Derived_Type (Typ)
12233 and then not Is_Tagged_Type (Typ)
12234 then
12235 Old_C := First_Stored_Discriminant (Typ);
12236
12237 while Present (Old_C) loop
12238 Num_Gird := Num_Gird + 1;
12239 Next_Stored_Discriminant (Old_C);
12240 end loop;
12241 end if;
12242
12243 if Num_Gird > Num_Disc then
12244
12245 -- Find out multiple uses of new discriminants, and add hidden
12246 -- components for the extra renamed discriminants. We recognize
12247 -- multiple uses through the Corresponding_Discriminant of a
12248 -- new discriminant: if it constrains several old discriminants,
12249 -- this field points to the last one in the parent type. The
12250 -- stored discriminants of the derived type have the same name
12251 -- as those of the parent.
12252
12253 declare
12254 Constr : Elmt_Id;
12255 New_Discr : Entity_Id;
12256 Old_Discr : Entity_Id;
12257
12258 begin
12259 Constr := First_Elmt (Stored_Constraint (Typ));
12260 Old_Discr := First_Stored_Discriminant (Typ);
c0bca7e1
EB
12261 while Present (Constr) loop
12262 if Is_Entity_Name (Node (Constr))
12263 and then Ekind (Entity (Node (Constr))) = E_Discriminant
12264 then
12265 New_Discr := Entity (Node (Constr));
12266
dc06abec
RD
12267 if Chars (Corresponding_Discriminant (New_Discr)) /=
12268 Chars (Old_Discr)
c0bca7e1 12269 then
dc06abec
RD
12270 -- The new discriminant has been used to rename a
12271 -- subsequent old discriminant. Introduce a shadow
c0bca7e1
EB
12272 -- component for the current old discriminant.
12273
12274 New_C := Create_Component (Old_Discr);
c9e7bd8e 12275 Set_Original_Record_Component (New_C, Old_Discr);
c0bca7e1 12276 end if;
c9e7bd8e
AC
12277
12278 else
12279 -- The constraint has eliminated the old discriminant.
12280 -- Introduce a shadow component.
12281
12282 New_C := Create_Component (Old_Discr);
12283 Set_Original_Record_Component (New_C, Old_Discr);
c0bca7e1
EB
12284 end if;
12285
12286 Next_Elmt (Constr);
12287 Next_Stored_Discriminant (Old_Discr);
12288 end loop;
12289 end;
12290 end if;
12291 end Add_Discriminants;
996ae0b0
RK
12292
12293 if Is_Static
12294 and then Is_Variant_Record (Typ)
12295 then
12296 Collect_Fixed_Components (Typ);
12297
12298 Gather_Components (
12299 Typ,
12300 Component_List (Type_Definition (Parent (Typ))),
12301 Governed_By => Assoc_List,
12302 Into => Comp_List,
12303 Report_Errors => Errors);
12304 pragma Assert (not Errors);
12305
12306 Create_All_Components;
12307
12308 -- If the subtype declaration is created for a tagged type derivation
12309 -- with constraints, we retrieve the record definition of the parent
12310 -- type to select the components of the proper variant.
12311
12312 elsif Is_Static
12313 and then Is_Tagged_Type (Typ)
12314 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
12315 and then
12316 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
12317 and then Is_Variant_Record (Parent_Type)
12318 then
12319 Collect_Fixed_Components (Typ);
12320
12321 Gather_Components (
12322 Typ,
12323 Component_List (Type_Definition (Parent (Parent_Type))),
12324 Governed_By => Assoc_List,
12325 Into => Comp_List,
12326 Report_Errors => Errors);
12327 pragma Assert (not Errors);
12328
12329 -- If the tagged derivation has a type extension, collect all the
12330 -- new components therein.
12331
0da2c8ac
AC
12332 if Present
12333 (Record_Extension_Part (Type_Definition (Parent (Typ))))
996ae0b0
RK
12334 then
12335 Old_C := First_Component (Typ);
996ae0b0
RK
12336 while Present (Old_C) loop
12337 if Original_Record_Component (Old_C) = Old_C
12338 and then Chars (Old_C) /= Name_uTag
12339 and then Chars (Old_C) /= Name_uParent
12340 and then Chars (Old_C) /= Name_uController
12341 then
12342 Append_Elmt (Old_C, Comp_List);
12343 end if;
12344
12345 Next_Component (Old_C);
12346 end loop;
12347 end if;
12348
12349 Create_All_Components;
12350
12351 else
9dfd2ff8
CC
12352 -- If discriminants are not static, or if this is a multi-level type
12353 -- extension, we have to include all components of the parent type.
996ae0b0
RK
12354
12355 Old_C := First_Component (Typ);
996ae0b0
RK
12356 while Present (Old_C) loop
12357 New_C := Create_Component (Old_C);
12358
12359 Set_Etype
12360 (New_C,
12361 Constrain_Component_Type
c6823a20 12362 (Old_C, Subt, Decl_Node, Typ, Constraints));
996ae0b0
RK
12363 Set_Is_Public (New_C, Is_Public (Subt));
12364
12365 Next_Component (Old_C);
12366 end loop;
12367 end if;
12368
12369 End_Scope;
12370 end Create_Constrained_Components;
12371
12372 ------------------------------------------
12373 -- Decimal_Fixed_Point_Type_Declaration --
12374 ------------------------------------------
12375
12376 procedure Decimal_Fixed_Point_Type_Declaration
12377 (T : Entity_Id;
12378 Def : Node_Id)
12379 is
12380 Loc : constant Source_Ptr := Sloc (Def);
12381 Digs_Expr : constant Node_Id := Digits_Expression (Def);
12382 Delta_Expr : constant Node_Id := Delta_Expression (Def);
12383 Implicit_Base : Entity_Id;
12384 Digs_Val : Uint;
12385 Delta_Val : Ureal;
12386 Scale_Val : Uint;
12387 Bound_Val : Ureal;
12388
996ae0b0 12389 begin
7ff2d234
AC
12390 -- Decimal fixed point type is not allowed in SPARK or ALFA
12391
12392 if Formal_Verification_Mode
12393 and then Comes_From_Source (Original_Node (Def))
12394 then
12395 Error_Msg_F
12396 ("|~~decimal fixed point type is not allowed", Def);
12397 end if;
12398
12399 -- Proceed with analysis
12400
996ae0b0
RK
12401 Check_Restriction (No_Fixed_Point, Def);
12402
12403 -- Create implicit base type
12404
12405 Implicit_Base :=
12406 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
12407 Set_Etype (Implicit_Base, Implicit_Base);
12408
12409 -- Analyze and process delta expression
12410
12411 Analyze_And_Resolve (Delta_Expr, Universal_Real);
12412
12413 Check_Delta_Expression (Delta_Expr);
12414 Delta_Val := Expr_Value_R (Delta_Expr);
12415
12416 -- Check delta is power of 10, and determine scale value from it
12417
12418 declare
9dfd2ff8 12419 Val : Ureal;
996ae0b0
RK
12420
12421 begin
12422 Scale_Val := Uint_0;
9dfd2ff8 12423 Val := Delta_Val;
996ae0b0
RK
12424
12425 if Val < Ureal_1 then
12426 while Val < Ureal_1 loop
12427 Val := Val * Ureal_10;
12428 Scale_Val := Scale_Val + 1;
12429 end loop;
12430
12431 if Scale_Val > 18 then
12432 Error_Msg_N ("scale exceeds maximum value of 18", Def);
12433 Scale_Val := UI_From_Int (+18);
12434 end if;
12435
12436 else
12437 while Val > Ureal_1 loop
12438 Val := Val / Ureal_10;
12439 Scale_Val := Scale_Val - 1;
12440 end loop;
12441
12442 if Scale_Val < -18 then
12443 Error_Msg_N ("scale is less than minimum value of -18", Def);
12444 Scale_Val := UI_From_Int (-18);
12445 end if;
12446 end if;
12447
12448 if Val /= Ureal_1 then
12449 Error_Msg_N ("delta expression must be a power of 10", Def);
12450 Delta_Val := Ureal_10 ** (-Scale_Val);
12451 end if;
12452 end;
12453
12454 -- Set delta, scale and small (small = delta for decimal type)
12455
12456 Set_Delta_Value (Implicit_Base, Delta_Val);
12457 Set_Scale_Value (Implicit_Base, Scale_Val);
12458 Set_Small_Value (Implicit_Base, Delta_Val);
12459
12460 -- Analyze and process digits expression
12461
12462 Analyze_And_Resolve (Digs_Expr, Any_Integer);
12463 Check_Digits_Expression (Digs_Expr);
12464 Digs_Val := Expr_Value (Digs_Expr);
12465
12466 if Digs_Val > 18 then
12467 Digs_Val := UI_From_Int (+18);
12468 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
12469 end if;
12470
12471 Set_Digits_Value (Implicit_Base, Digs_Val);
12472 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
12473
12474 -- Set range of base type from digits value for now. This will be
12475 -- expanded to represent the true underlying base range by Freeze.
12476
12477 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
12478
ce4a6e84
RD
12479 -- Note: We leave size as zero for now, size will be set at freeze
12480 -- time. We have to do this for ordinary fixed-point, because the size
12481 -- depends on the specified small, and we might as well do the same for
12482 -- decimal fixed-point.
996ae0b0 12483
ce4a6e84 12484 pragma Assert (Esize (Implicit_Base) = Uint_0);
996ae0b0 12485
996ae0b0
RK
12486 -- If there are bounds given in the declaration use them as the
12487 -- bounds of the first named subtype.
12488
12489 if Present (Real_Range_Specification (Def)) then
12490 declare
12491 RRS : constant Node_Id := Real_Range_Specification (Def);
12492 Low : constant Node_Id := Low_Bound (RRS);
12493 High : constant Node_Id := High_Bound (RRS);
12494 Low_Val : Ureal;
12495 High_Val : Ureal;
12496
12497 begin
12498 Analyze_And_Resolve (Low, Any_Real);
12499 Analyze_And_Resolve (High, Any_Real);
12500 Check_Real_Bound (Low);
12501 Check_Real_Bound (High);
12502 Low_Val := Expr_Value_R (Low);
12503 High_Val := Expr_Value_R (High);
12504
12505 if Low_Val < (-Bound_Val) then
12506 Error_Msg_N
12507 ("range low bound too small for digits value", Low);
12508 Low_Val := -Bound_Val;
12509 end if;
12510
12511 if High_Val > Bound_Val then
12512 Error_Msg_N
12513 ("range high bound too large for digits value", High);
12514 High_Val := Bound_Val;
12515 end if;
12516
12517 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
12518 end;
12519
12520 -- If no explicit range, use range that corresponds to given
12521 -- digits value. This will end up as the final range for the
12522 -- first subtype.
12523
12524 else
12525 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
12526 end if;
12527
c45b6ae0
AC
12528 -- Complete entity for first subtype
12529
12530 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
12531 Set_Etype (T, Implicit_Base);
12532 Set_Size_Info (T, Implicit_Base);
12533 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
12534 Set_Digits_Value (T, Digs_Val);
12535 Set_Delta_Value (T, Delta_Val);
12536 Set_Small_Value (T, Delta_Val);
12537 Set_Scale_Value (T, Scale_Val);
12538 Set_Is_Constrained (T);
996ae0b0
RK
12539 end Decimal_Fixed_Point_Type_Declaration;
12540
ce2b6ba5
JM
12541 -----------------------------------
12542 -- Derive_Progenitor_Subprograms --
12543 -----------------------------------
758c442c 12544
ce2b6ba5 12545 procedure Derive_Progenitor_Subprograms
88b32fc3 12546 (Parent_Type : Entity_Id;
ce2b6ba5 12547 Tagged_Type : Entity_Id)
88b32fc3 12548 is
ce2b6ba5
JM
12549 E : Entity_Id;
12550 Elmt : Elmt_Id;
12551 Iface : Entity_Id;
12552 Iface_Elmt : Elmt_Id;
12553 Iface_Subp : Entity_Id;
12554 New_Subp : Entity_Id := Empty;
12555 Prim_Elmt : Elmt_Id;
12556 Subp : Entity_Id;
12557 Typ : Entity_Id;
758c442c 12558
ce2b6ba5 12559 begin
0791fbe9 12560 pragma Assert (Ada_Version >= Ada_2005
ce2b6ba5
JM
12561 and then Is_Record_Type (Tagged_Type)
12562 and then Is_Tagged_Type (Tagged_Type)
12563 and then Has_Interfaces (Tagged_Type));
12564
30783513 12565 -- Step 1: Transfer to the full-view primitives associated with the
ce2b6ba5
JM
12566 -- partial-view that cover interface primitives. Conceptually this
12567 -- work should be done later by Process_Full_View; done here to
12568 -- simplify its implementation at later stages. It can be safely
12569 -- done here because interfaces must be visible in the partial and
12570 -- private view (RM 7.3(7.3/2)).
12571
12572 -- Small optimization: This work is only required if the parent is
12573 -- abstract. If the tagged type is not abstract, it cannot have
12574 -- abstract primitives (the only entities in the list of primitives of
12575 -- non-abstract tagged types that can reference abstract primitives
12576 -- through its Alias attribute are the internal entities that have
12577 -- attribute Interface_Alias, and these entities are generated later
b4d7b435 12578 -- by Add_Internal_Interface_Entities).
88b32fc3 12579
ce2b6ba5
JM
12580 if In_Private_Part (Current_Scope)
12581 and then Is_Abstract_Type (Parent_Type)
12582 then
12583 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
12584 while Present (Elmt) loop
12585 Subp := Node (Elmt);
88b32fc3 12586
ce2b6ba5
JM
12587 -- At this stage it is not possible to have entities in the list
12588 -- of primitives that have attribute Interface_Alias
758c442c 12589
ce2b6ba5 12590 pragma Assert (No (Interface_Alias (Subp)));
758c442c 12591
ce2b6ba5 12592 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
88b32fc3 12593
ce2b6ba5
JM
12594 if Is_Interface (Typ) then
12595 E := Find_Primitive_Covering_Interface
12596 (Tagged_Type => Tagged_Type,
12597 Iface_Prim => Subp);
88b32fc3 12598
ce2b6ba5
JM
12599 if Present (E)
12600 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
12601 then
12602 Replace_Elmt (Elmt, E);
12603 Remove_Homonym (Subp);
950d3e7d 12604 end if;
88b32fc3
BD
12605 end if;
12606
12607 Next_Elmt (Elmt);
12608 end loop;
88b32fc3
BD
12609 end if;
12610
ce2b6ba5
JM
12611 -- Step 2: Add primitives of progenitors that are not implemented by
12612 -- parents of Tagged_Type
88b32fc3 12613
59262ebb
AC
12614 if Present (Interfaces (Base_Type (Tagged_Type))) then
12615 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
ce2b6ba5
JM
12616 while Present (Iface_Elmt) loop
12617 Iface := Node (Iface_Elmt);
88b32fc3 12618
ce2b6ba5
JM
12619 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
12620 while Present (Prim_Elmt) loop
12621 Iface_Subp := Node (Prim_Elmt);
fea9e956 12622
9800ef59
JM
12623 -- Exclude derivation of predefined primitives except those
12624 -- that come from source. Required to catch declarations of
12625 -- equality operators of interfaces. For example:
12626
12627 -- type Iface is interface;
12628 -- function "=" (Left, Right : Iface) return Boolean;
12629
8c3dd7a8
JM
12630 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
12631 or else Comes_From_Source (Iface_Subp)
12632 then
ce2b6ba5
JM
12633 E := Find_Primitive_Covering_Interface
12634 (Tagged_Type => Tagged_Type,
12635 Iface_Prim => Iface_Subp);
88b32fc3 12636
ce2b6ba5
JM
12637 -- If not found we derive a new primitive leaving its alias
12638 -- attribute referencing the interface primitive
88b32fc3 12639
ce2b6ba5
JM
12640 if No (E) then
12641 Derive_Subprogram
12642 (New_Subp, Iface_Subp, Tagged_Type, Iface);
88b32fc3 12643
ce09f8b3
AC
12644 -- Ada 2012 (AI05-0197): If the covering primitive's name
12645 -- differs from the name of the interface primitive then it
12646 -- is a private primitive inherited from a parent type. In
12647 -- such case, given that Tagged_Type covers the interface,
12648 -- the inherited private primitive becomes visible. For such
12649 -- purpose we add a new entity that renames the inherited
12650 -- private primitive.
12651
12652 elsif Chars (E) /= Chars (Iface_Subp) then
12653 pragma Assert (Has_Suffix (E, 'P'));
12654 Derive_Subprogram
12655 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12656 Set_Alias (New_Subp, E);
12657 Set_Is_Abstract_Subprogram (New_Subp,
12658 Is_Abstract_Subprogram (E));
12659
ce2b6ba5
JM
12660 -- Propagate to the full view interface entities associated
12661 -- with the partial view
88b32fc3 12662
ce2b6ba5
JM
12663 elsif In_Private_Part (Current_Scope)
12664 and then Present (Alias (E))
12665 and then Alias (E) = Iface_Subp
12666 and then
12667 List_Containing (Parent (E)) /=
12668 Private_Declarations
12669 (Specification
12670 (Unit_Declaration_Node (Current_Scope)))
12671 then
12672 Append_Elmt (E, Primitive_Operations (Tagged_Type));
12673 end if;
88b32fc3
BD
12674 end if;
12675
ce2b6ba5 12676 Next_Elmt (Prim_Elmt);
88b32fc3
BD
12677 end loop;
12678
ce2b6ba5 12679 Next_Elmt (Iface_Elmt);
88b32fc3
BD
12680 end loop;
12681 end if;
ce2b6ba5 12682 end Derive_Progenitor_Subprograms;
758c442c 12683
996ae0b0
RK
12684 -----------------------
12685 -- Derive_Subprogram --
12686 -----------------------
12687
12688 procedure Derive_Subprogram
12689 (New_Subp : in out Entity_Id;
12690 Parent_Subp : Entity_Id;
12691 Derived_Type : Entity_Id;
12692 Parent_Type : Entity_Id;
12693 Actual_Subp : Entity_Id := Empty)
12694 is
ce4a6e84
RD
12695 Formal : Entity_Id;
12696 -- Formal parameter of parent primitive operation
12697
12698 Formal_Of_Actual : Entity_Id;
12699 -- Formal parameter of actual operation, when the derivation is to
12700 -- create a renaming for a primitive operation of an actual in an
12701 -- instantiation.
12702
12703 New_Formal : Entity_Id;
12704 -- Formal of inherited operation
12705
fbf5a39b 12706 Visible_Subp : Entity_Id := Parent_Subp;
996ae0b0
RK
12707
12708 function Is_Private_Overriding return Boolean;
ce4a6e84
RD
12709 -- If Subp is a private overriding of a visible operation, the inherited
12710 -- operation derives from the overridden op (even though its body is the
12711 -- overriding one) and the inherited operation is visible now. See
12712 -- sem_disp to see the full details of the handling of the overridden
12713 -- subprogram, which is removed from the list of primitive operations of
12714 -- the type. The overridden subprogram is saved locally in Visible_Subp,
12715 -- and used to diagnose abstract operations that need overriding in the
12716 -- derived type.
996ae0b0
RK
12717
12718 procedure Replace_Type (Id, New_Id : Entity_Id);
12719 -- When the type is an anonymous access type, create a new access type
12720 -- designating the derived type.
12721
fbf5a39b
AC
12722 procedure Set_Derived_Name;
12723 -- This procedure sets the appropriate Chars name for New_Subp. This
12724 -- is normally just a copy of the parent name. An exception arises for
12725 -- type support subprograms, where the name is changed to reflect the
12726 -- name of the derived type, e.g. if type foo is derived from type bar,
12727 -- then a procedure barDA is derived with a name fooDA.
12728
996ae0b0
RK
12729 ---------------------------
12730 -- Is_Private_Overriding --
12731 ---------------------------
12732
12733 function Is_Private_Overriding return Boolean is
12734 Prev : Entity_Id;
12735
12736 begin
88b32fc3
BD
12737 -- If the parent is not a dispatching operation there is no
12738 -- need to investigate overridings
12739
12740 if not Is_Dispatching_Operation (Parent_Subp) then
12741 return False;
12742 end if;
12743
9dfd2ff8 12744 -- The visible operation that is overridden is a homonym of the
a5b62485
AC
12745 -- parent subprogram. We scan the homonym chain to find the one
12746 -- whose alias is the subprogram we are deriving.
996ae0b0 12747
9dfd2ff8 12748 Prev := Current_Entity (Parent_Subp);
996ae0b0 12749 while Present (Prev) loop
88b32fc3 12750 if Ekind (Prev) = Ekind (Parent_Subp)
996ae0b0
RK
12751 and then Alias (Prev) = Parent_Subp
12752 and then Scope (Parent_Subp) = Scope (Prev)
88b32fc3 12753 and then not Is_Hidden (Prev)
996ae0b0 12754 then
fbf5a39b 12755 Visible_Subp := Prev;
996ae0b0
RK
12756 return True;
12757 end if;
12758
12759 Prev := Homonym (Prev);
12760 end loop;
12761
12762 return False;
12763 end Is_Private_Overriding;
12764
12765 ------------------
12766 -- Replace_Type --
12767 ------------------
12768
12769 procedure Replace_Type (Id, New_Id : Entity_Id) is
12770 Acc_Type : Entity_Id;
0da2c8ac 12771 Par : constant Node_Id := Parent (Derived_Type);
996ae0b0
RK
12772
12773 begin
12774 -- When the type is an anonymous access type, create a new access
12775 -- type designating the derived type. This itype must be elaborated
12776 -- at the point of the derivation, not on subsequent calls that may
12777 -- be out of the proper scope for Gigi, so we insert a reference to
12778 -- it after the derivation.
12779
12780 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
12781 declare
12782 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
12783
12784 begin
12785 if Ekind (Desig_Typ) = E_Record_Type_With_Private
12786 and then Present (Full_View (Desig_Typ))
12787 and then not Is_Private_Type (Parent_Type)
12788 then
12789 Desig_Typ := Full_View (Desig_Typ);
12790 end if;
12791
88b32fc3
BD
12792 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
12793
12794 -- Ada 2005 (AI-251): Handle also derivations of abstract
12795 -- interface primitives.
12796
12797 or else (Is_Interface (Desig_Typ)
12798 and then not Is_Class_Wide_Type (Desig_Typ))
12799 then
996ae0b0
RK
12800 Acc_Type := New_Copy (Etype (Id));
12801 Set_Etype (Acc_Type, Acc_Type);
12802 Set_Scope (Acc_Type, New_Subp);
12803
71d9e9f2 12804 -- Compute size of anonymous access type
996ae0b0
RK
12805
12806 if Is_Array_Type (Desig_Typ)
12807 and then not Is_Constrained (Desig_Typ)
12808 then
12809 Init_Size (Acc_Type, 2 * System_Address_Size);
12810 else
12811 Init_Size (Acc_Type, System_Address_Size);
12812 end if;
12813
12814 Init_Alignment (Acc_Type);
996ae0b0
RK
12815 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
12816
12817 Set_Etype (New_Id, Acc_Type);
12818 Set_Scope (New_Id, New_Subp);
12819
0da2c8ac 12820 -- Create a reference to it
fea9e956 12821 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
996ae0b0
RK
12822
12823 else
12824 Set_Etype (New_Id, Etype (Id));
12825 end if;
12826 end;
0da2c8ac 12827
996ae0b0
RK
12828 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
12829 or else
12830 (Ekind (Etype (Id)) = E_Record_Type_With_Private
12831 and then Present (Full_View (Etype (Id)))
0da2c8ac
AC
12832 and then
12833 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
996ae0b0 12834 then
996ae0b0
RK
12835 -- Constraint checks on formals are generated during expansion,
12836 -- based on the signature of the original subprogram. The bounds
12837 -- of the derived type are not relevant, and thus we can use
12838 -- the base type for the formals. However, the return type may be
12839 -- used in a context that requires that the proper static bounds
12840 -- be used (a case statement, for example) and for those cases
12841 -- we must use the derived type (first subtype), not its base.
12842
0da2c8ac
AC
12843 -- If the derived_type_definition has no constraints, we know that
12844 -- the derived type has the same constraints as the first subtype
12845 -- of the parent, and we can also use it rather than its base,
12846 -- which can lead to more efficient code.
12847
12848 if Etype (Id) = Parent_Type then
12849 if Is_Scalar_Type (Parent_Type)
12850 and then
12851 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
12852 then
12853 Set_Etype (New_Id, Derived_Type);
12854
12855 elsif Nkind (Par) = N_Full_Type_Declaration
12856 and then
12857 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
12858 and then
12859 Is_Entity_Name
12860 (Subtype_Indication (Type_Definition (Par)))
12861 then
12862 Set_Etype (New_Id, Derived_Type);
12863
12864 else
12865 Set_Etype (New_Id, Base_Type (Derived_Type));
12866 end if;
12867
996ae0b0
RK
12868 else
12869 Set_Etype (New_Id, Base_Type (Derived_Type));
12870 end if;
12871
12872 else
12873 Set_Etype (New_Id, Etype (Id));
12874 end if;
12875 end Replace_Type;
12876
fbf5a39b
AC
12877 ----------------------
12878 -- Set_Derived_Name --
12879 ----------------------
12880
12881 procedure Set_Derived_Name is
12882 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
12883 begin
12884 if Nm = TSS_Null then
12885 Set_Chars (New_Subp, Chars (Parent_Subp));
12886 else
12887 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
12888 end if;
12889 end Set_Derived_Name;
12890
996ae0b0
RK
12891 -- Start of processing for Derive_Subprogram
12892
12893 begin
12894 New_Subp :=
12895 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
12896 Set_Ekind (New_Subp, Ekind (Parent_Subp));
12897
12898 -- Check whether the inherited subprogram is a private operation that
12899 -- should be inherited but not yet made visible. Such subprograms can
12900 -- become visible at a later point (e.g., the private part of a public
12901 -- child unit) via Declare_Inherited_Private_Subprograms. If the
12902 -- following predicate is true, then this is not such a private
12903 -- operation and the subprogram simply inherits the name of the parent
12904 -- subprogram. Note the special check for the names of controlled
12905 -- operations, which are currently exempted from being inherited with
12906 -- a hidden name because they must be findable for generation of
12907 -- implicit run-time calls.
12908
12909 if not Is_Hidden (Parent_Subp)
12910 or else Is_Internal (Parent_Subp)
12911 or else Is_Private_Overriding
12912 or else Is_Internal_Name (Chars (Parent_Subp))
12913 or else Chars (Parent_Subp) = Name_Initialize
12914 or else Chars (Parent_Subp) = Name_Adjust
12915 or else Chars (Parent_Subp) = Name_Finalize
12916 then
fbf5a39b 12917 Set_Derived_Name;
996ae0b0 12918
af268547
ES
12919 -- An inherited dispatching equality will be overridden by an internally
12920 -- generated one, or by an explicit one, so preserve its name and thus
12921 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
12922 -- private operation it may become invisible if the full view has
12923 -- progenitors, and the dispatch table will be malformed.
12924 -- We check that the type is limited to handle the anomalous declaration
12925 -- of Limited_Controlled, which is derived from a non-limited type, and
12926 -- which is handled specially elsewhere as well.
12927
12928 elsif Chars (Parent_Subp) = Name_Op_Eq
12929 and then Is_Dispatching_Operation (Parent_Subp)
12930 and then Etype (Parent_Subp) = Standard_Boolean
c0985d4e 12931 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
af268547 12932 and then
c0985d4e
HK
12933 Etype (First_Formal (Parent_Subp)) =
12934 Etype (Next_Formal (First_Formal (Parent_Subp)))
af268547
ES
12935 then
12936 Set_Derived_Name;
12937
996ae0b0
RK
12938 -- If parent is hidden, this can be a regular derivation if the
12939 -- parent is immediately visible in a non-instantiating context,
12940 -- or if we are in the private part of an instance. This test
12941 -- should still be refined ???
12942
a5b62485
AC
12943 -- The test for In_Instance_Not_Visible avoids inheriting the derived
12944 -- operation as a non-visible operation in cases where the parent
12945 -- subprogram might not be visible now, but was visible within the
12946 -- original generic, so it would be wrong to make the inherited
12947 -- subprogram non-visible now. (Not clear if this test is fully
12948 -- correct; are there any cases where we should declare the inherited
12949 -- operation as not visible to avoid it being overridden, e.g., when
12950 -- the parent type is a generic actual with private primitives ???)
996ae0b0
RK
12951
12952 -- (they should be treated the same as other private inherited
12953 -- subprograms, but it's not clear how to do this cleanly). ???
12954
12955 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
12956 and then Is_Immediately_Visible (Parent_Subp)
12957 and then not In_Instance)
12958 or else In_Instance_Not_Visible
12959 then
fbf5a39b 12960 Set_Derived_Name;
996ae0b0 12961
ce2b6ba5
JM
12962 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
12963 -- overrides an interface primitive because interface primitives
12964 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
88b32fc3 12965
0791fbe9 12966 elsif Ada_Version >= Ada_2005
0052da20
JM
12967 and then Is_Dispatching_Operation (Parent_Subp)
12968 and then Covers_Some_Interface (Parent_Subp)
12969 then
88b32fc3
BD
12970 Set_Derived_Name;
12971
af268547 12972 -- Otherwise, the type is inheriting a private operation, so enter
996ae0b0
RK
12973 -- it with a special name so it can't be overridden.
12974
12975 else
12976 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
12977 end if;
12978
12979 Set_Parent (New_Subp, Parent (Derived_Type));
ce4a6e84
RD
12980
12981 if Present (Actual_Subp) then
12982 Replace_Type (Actual_Subp, New_Subp);
12983 else
12984 Replace_Type (Parent_Subp, New_Subp);
12985 end if;
12986
996ae0b0
RK
12987 Conditional_Delay (New_Subp, Parent_Subp);
12988
ce4a6e84
RD
12989 -- If we are creating a renaming for a primitive operation of an
12990 -- actual of a generic derived type, we must examine the signature
f3d0f304 12991 -- of the actual primitive, not that of the generic formal, which for
ce4a6e84
RD
12992 -- example may be an interface. However the name and initial value
12993 -- of the inherited operation are those of the formal primitive.
12994
996ae0b0 12995 Formal := First_Formal (Parent_Subp);
ce4a6e84
RD
12996
12997 if Present (Actual_Subp) then
12998 Formal_Of_Actual := First_Formal (Actual_Subp);
12999 else
13000 Formal_Of_Actual := Empty;
13001 end if;
13002
996ae0b0
RK
13003 while Present (Formal) loop
13004 New_Formal := New_Copy (Formal);
13005
13006 -- Normally we do not go copying parents, but in the case of
a5b62485
AC
13007 -- formals, we need to link up to the declaration (which is the
13008 -- parameter specification), and it is fine to link up to the
13009 -- original formal's parameter specification in this case.
996ae0b0
RK
13010
13011 Set_Parent (New_Formal, Parent (Formal));
996ae0b0
RK
13012 Append_Entity (New_Formal, New_Subp);
13013
ce4a6e84
RD
13014 if Present (Formal_Of_Actual) then
13015 Replace_Type (Formal_Of_Actual, New_Formal);
13016 Next_Formal (Formal_Of_Actual);
13017 else
13018 Replace_Type (Formal, New_Formal);
13019 end if;
13020
996ae0b0
RK
13021 Next_Formal (Formal);
13022 end loop;
13023
13024 -- If this derivation corresponds to a tagged generic actual, then
13025 -- primitive operations rename those of the actual. Otherwise the
ce4a6e84
RD
13026 -- primitive operations rename those of the parent type, If the parent
13027 -- renames an intrinsic operator, so does the new subprogram. We except
13028 -- concatenation, which is always properly typed, and does not get
13029 -- expanded as other intrinsic operations.
996ae0b0
RK
13030
13031 if No (Actual_Subp) then
fbf5a39b
AC
13032 if Is_Intrinsic_Subprogram (Parent_Subp) then
13033 Set_Is_Intrinsic_Subprogram (New_Subp);
13034
13035 if Present (Alias (Parent_Subp))
13036 and then Chars (Parent_Subp) /= Name_Op_Concat
13037 then
13038 Set_Alias (New_Subp, Alias (Parent_Subp));
13039 else
13040 Set_Alias (New_Subp, Parent_Subp);
13041 end if;
13042
13043 else
13044 Set_Alias (New_Subp, Parent_Subp);
13045 end if;
996ae0b0
RK
13046
13047 else
13048 Set_Alias (New_Subp, Actual_Subp);
13049 end if;
13050
13051 -- Derived subprograms of a tagged type must inherit the convention
13052 -- of the parent subprogram (a requirement of AI-117). Derived
13053 -- subprograms of untagged types simply get convention Ada by default.
13054
13055 if Is_Tagged_Type (Derived_Type) then
88b32fc3 13056 Set_Convention (New_Subp, Convention (Parent_Subp));
996ae0b0
RK
13057 end if;
13058
fd0d899b
AC
13059 -- Predefined controlled operations retain their name even if the parent
13060 -- is hidden (see above), but they are not primitive operations if the
13061 -- ancestor is not visible, for example if the parent is a private
13062 -- extension completed with a controlled extension. Note that a full
13063 -- type that is controlled can break privacy: the flag Is_Controlled is
13064 -- set on both views of the type.
13065
13066 if Is_Controlled (Parent_Type)
13067 and then
13068 (Chars (Parent_Subp) = Name_Initialize
13069 or else Chars (Parent_Subp) = Name_Adjust
13070 or else Chars (Parent_Subp) = Name_Finalize)
13071 and then Is_Hidden (Parent_Subp)
13072 and then not Is_Visibly_Controlled (Parent_Type)
13073 then
13074 Set_Is_Hidden (New_Subp);
13075 end if;
13076
996ae0b0
RK
13077 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
13078 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
13079
13080 if Ekind (Parent_Subp) = E_Procedure then
13081 Set_Is_Valued_Procedure
13082 (New_Subp, Is_Valued_Procedure (Parent_Subp));
c86ee18a
AC
13083 else
13084 Set_Has_Controlling_Result
13085 (New_Subp, Has_Controlling_Result (Parent_Subp));
996ae0b0
RK
13086 end if;
13087
57193e09
TQ
13088 -- No_Return must be inherited properly. If this is overridden in the
13089 -- case of a dispatching operation, then a check is made in Sem_Disp
13090 -- that the overriding operation is also No_Return (no such check is
13091 -- required for the case of non-dispatching operation.
13092
13093 Set_No_Return (New_Subp, No_Return (Parent_Subp));
13094
a5b62485
AC
13095 -- A derived function with a controlling result is abstract. If the
13096 -- Derived_Type is a nonabstract formal generic derived type, then
13097 -- inherited operations are not abstract: the required check is done at
13098 -- instantiation time. If the derivation is for a generic actual, the
13099 -- function is not abstract unless the actual is.
fbf5a39b
AC
13100
13101 if Is_Generic_Type (Derived_Type)
fea9e956 13102 and then not Is_Abstract_Type (Derived_Type)
fbf5a39b
AC
13103 then
13104 null;
13105
fea9e956
ES
13106 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
13107 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
13108
0791fbe9 13109 elsif Ada_Version >= Ada_2005
fea9e956
ES
13110 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13111 or else (Is_Tagged_Type (Derived_Type)
13112 and then Etype (New_Subp) = Derived_Type
13113 and then not Is_Null_Extension (Derived_Type))
13114 or else (Is_Tagged_Type (Derived_Type)
13115 and then Ekind (Etype (New_Subp)) =
13116 E_Anonymous_Access_Type
13117 and then Designated_Type (Etype (New_Subp)) =
13118 Derived_Type
13119 and then not Is_Null_Extension (Derived_Type)))
13120 and then No (Actual_Subp)
13121 then
13122 if not Is_Tagged_Type (Derived_Type)
13123 or else Is_Abstract_Type (Derived_Type)
13124 or else Is_Abstract_Subprogram (Alias (New_Subp))
13125 then
13126 Set_Is_Abstract_Subprogram (New_Subp);
13127 else
13128 Set_Requires_Overriding (New_Subp);
13129 end if;
13130
0791fbe9 13131 elsif Ada_Version < Ada_2005
fea9e956
ES
13132 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13133 or else (Is_Tagged_Type (Derived_Type)
13134 and then Etype (New_Subp) = Derived_Type
13135 and then No (Actual_Subp)))
fbf5a39b 13136 then
fea9e956 13137 Set_Is_Abstract_Subprogram (New_Subp);
fbf5a39b 13138
c86ee18a
AC
13139 -- AI05-0097 : an inherited operation that dispatches on result is
13140 -- abstract if the derived type is abstract, even if the parent type
13141 -- is concrete and the derived type is a null extension.
13142
13143 elsif Has_Controlling_Result (Alias (New_Subp))
13144 and then Is_Abstract_Type (Etype (New_Subp))
13145 then
13146 Set_Is_Abstract_Subprogram (New_Subp);
13147
2b73cf68 13148 -- Finally, if the parent type is abstract we must verify that all
ce4a6e84
RD
13149 -- inherited operations are either non-abstract or overridden, or that
13150 -- the derived type itself is abstract (this check is performed at the
13151 -- end of a package declaration, in Check_Abstract_Overriding). A
13152 -- private overriding in the parent type will not be visible in the
fbf5a39b
AC
13153 -- derivation if we are not in an inner package or in a child unit of
13154 -- the parent type, in which case the abstractness of the inherited
13155 -- operation is carried to the new subprogram.
13156
fea9e956 13157 elsif Is_Abstract_Type (Parent_Type)
fbf5a39b
AC
13158 and then not In_Open_Scopes (Scope (Parent_Type))
13159 and then Is_Private_Overriding
fea9e956 13160 and then Is_Abstract_Subprogram (Visible_Subp)
fbf5a39b 13161 then
2b73cf68
JM
13162 if No (Actual_Subp) then
13163 Set_Alias (New_Subp, Visible_Subp);
b8dfbe1e
AC
13164 Set_Is_Abstract_Subprogram (New_Subp, True);
13165
2b73cf68
JM
13166 else
13167 -- If this is a derivation for an instance of a formal derived
13168 -- type, abstractness comes from the primitive operation of the
13169 -- actual, not from the operation inherited from the ancestor.
13170
13171 Set_Is_Abstract_Subprogram
13172 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
13173 end if;
fbf5a39b
AC
13174 end if;
13175
996ae0b0
RK
13176 New_Overloaded_Entity (New_Subp, Derived_Type);
13177
a5b62485
AC
13178 -- Check for case of a derived subprogram for the instantiation of a
13179 -- formal derived tagged type, if so mark the subprogram as dispatching
13180 -- and inherit the dispatching attributes of the parent subprogram. The
13181 -- derived subprogram is effectively renaming of the actual subprogram,
13182 -- so it needs to have the same attributes as the actual.
996ae0b0
RK
13183
13184 if Present (Actual_Subp)
13185 and then Is_Dispatching_Operation (Parent_Subp)
13186 then
13187 Set_Is_Dispatching_Operation (New_Subp);
88b32fc3 13188
996ae0b0
RK
13189 if Present (DTC_Entity (Parent_Subp)) then
13190 Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
13191 Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
13192 end if;
13193 end if;
13194
a5b62485
AC
13195 -- Indicate that a derived subprogram does not require a body and that
13196 -- it does not require processing of default expressions.
996ae0b0
RK
13197
13198 Set_Has_Completion (New_Subp);
13199 Set_Default_Expressions_Processed (New_Subp);
13200
996ae0b0
RK
13201 if Ekind (New_Subp) = E_Function then
13202 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
13203 end if;
13204 end Derive_Subprogram;
13205
13206 ------------------------
13207 -- Derive_Subprograms --
13208 ------------------------
13209
13210 procedure Derive_Subprograms
7d7af38a
JM
13211 (Parent_Type : Entity_Id;
13212 Derived_Type : Entity_Id;
13213 Generic_Actual : Entity_Id := Empty)
996ae0b0 13214 is
ce2b6ba5
JM
13215 Op_List : constant Elist_Id :=
13216 Collect_Primitive_Operations (Parent_Type);
13217
13218 function Check_Derived_Type return Boolean;
ff2efe85 13219 -- Check that all the entities derived from Parent_Type are found in
ce2b6ba5
JM
13220 -- the list of primitives of Derived_Type exactly in the same order.
13221
ff2efe85
AC
13222 procedure Derive_Interface_Subprogram
13223 (New_Subp : in out Entity_Id;
13224 Subp : Entity_Id;
13225 Actual_Subp : Entity_Id);
13226 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
13227 -- (which is an interface primitive). If Generic_Actual is present then
13228 -- Actual_Subp is the actual subprogram corresponding with the generic
13229 -- subprogram Subp.
13230
ce2b6ba5
JM
13231 function Check_Derived_Type return Boolean is
13232 E : Entity_Id;
13233 Elmt : Elmt_Id;
13234 List : Elist_Id;
13235 New_Subp : Entity_Id;
13236 Op_Elmt : Elmt_Id;
13237 Subp : Entity_Id;
13238
13239 begin
13240 -- Traverse list of entities in the current scope searching for
13241 -- an incomplete type whose full-view is derived type
13242
13243 E := First_Entity (Scope (Derived_Type));
13244 while Present (E)
13245 and then E /= Derived_Type
13246 loop
13247 if Ekind (E) = E_Incomplete_Type
13248 and then Present (Full_View (E))
13249 and then Full_View (E) = Derived_Type
13250 then
13251 -- Disable this test if Derived_Type completes an incomplete
13252 -- type because in such case more primitives can be added
13253 -- later to the list of primitives of Derived_Type by routine
13254 -- Process_Incomplete_Dependents
13255
13256 return True;
13257 end if;
13258
13259 E := Next_Entity (E);
13260 end loop;
13261
13262 List := Collect_Primitive_Operations (Derived_Type);
13263 Elmt := First_Elmt (List);
13264
13265 Op_Elmt := First_Elmt (Op_List);
13266 while Present (Op_Elmt) loop
13267 Subp := Node (Op_Elmt);
13268 New_Subp := Node (Elmt);
13269
13270 -- At this early stage Derived_Type has no entities with attribute
13271 -- Interface_Alias. In addition, such primitives are always
13272 -- located at the end of the list of primitives of Parent_Type.
13273 -- Therefore, if found we can safely stop processing pending
13274 -- entities.
13275
13276 exit when Present (Interface_Alias (Subp));
13277
13278 -- Handle hidden entities
13279
13280 if not Is_Predefined_Dispatching_Operation (Subp)
13281 and then Is_Hidden (Subp)
13282 then
13283 if Present (New_Subp)
13284 and then Primitive_Names_Match (Subp, New_Subp)
13285 then
13286 Next_Elmt (Elmt);
13287 end if;
13288
13289 else
13290 if not Present (New_Subp)
13291 or else Ekind (Subp) /= Ekind (New_Subp)
13292 or else not Primitive_Names_Match (Subp, New_Subp)
13293 then
13294 return False;
13295 end if;
13296
13297 Next_Elmt (Elmt);
13298 end if;
13299
13300 Next_Elmt (Op_Elmt);
13301 end loop;
13302
13303 return True;
13304 end Check_Derived_Type;
13305
ff2efe85
AC
13306 ---------------------------------
13307 -- Derive_Interface_Subprogram --
13308 ---------------------------------
13309
13310 procedure Derive_Interface_Subprogram
13311 (New_Subp : in out Entity_Id;
13312 Subp : Entity_Id;
13313 Actual_Subp : Entity_Id)
13314 is
13315 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
13316 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
13317
13318 begin
13319 pragma Assert (Is_Interface (Iface_Type));
13320
13321 Derive_Subprogram
13322 (New_Subp => New_Subp,
13323 Parent_Subp => Iface_Subp,
13324 Derived_Type => Derived_Type,
13325 Parent_Type => Iface_Type,
13326 Actual_Subp => Actual_Subp);
13327
13328 -- Given that this new interface entity corresponds with a primitive
13329 -- of the parent that was not overridden we must leave it associated
13330 -- with its parent primitive to ensure that it will share the same
13331 -- dispatch table slot when overridden.
13332
13333 if No (Actual_Subp) then
13334 Set_Alias (New_Subp, Subp);
13335
13336 -- For instantiations this is not needed since the previous call to
13337 -- Derive_Subprogram leaves the entity well decorated.
13338
13339 else
13340 pragma Assert (Alias (New_Subp) = Actual_Subp);
13341 null;
13342 end if;
13343 end Derive_Interface_Subprogram;
13344
ce2b6ba5
JM
13345 -- Local variables
13346
13347 Alias_Subp : Entity_Id;
88b32fc3 13348 Act_List : Elist_Id;
ce2b6ba5
JM
13349 Act_Elmt : Elmt_Id := No_Elmt;
13350 Act_Subp : Entity_Id := Empty;
88b32fc3 13351 Elmt : Elmt_Id;
ce2b6ba5 13352 Need_Search : Boolean := False;
88b32fc3
BD
13353 New_Subp : Entity_Id := Empty;
13354 Parent_Base : Entity_Id;
13355 Subp : Entity_Id;
996ae0b0 13356
ce2b6ba5
JM
13357 -- Start of processing for Derive_Subprograms
13358
996ae0b0
RK
13359 begin
13360 if Ekind (Parent_Type) = E_Record_Type_With_Private
13361 and then Has_Discriminants (Parent_Type)
13362 and then Present (Full_View (Parent_Type))
13363 then
13364 Parent_Base := Full_View (Parent_Type);
13365 else
13366 Parent_Base := Parent_Type;
13367 end if;
13368
996ae0b0
RK
13369 if Present (Generic_Actual) then
13370 Act_List := Collect_Primitive_Operations (Generic_Actual);
13371 Act_Elmt := First_Elmt (Act_List);
996ae0b0
RK
13372 end if;
13373
ce2b6ba5
JM
13374 -- Derive primitives inherited from the parent. Note that if the generic
13375 -- actual is present, this is not really a type derivation, it is a
13376 -- completion within an instance.
996ae0b0 13377
ce2b6ba5
JM
13378 -- Case 1: Derived_Type does not implement interfaces
13379
13380 if not Is_Tagged_Type (Derived_Type)
13381 or else (not Has_Interfaces (Derived_Type)
13382 and then not (Present (Generic_Actual)
13383 and then
13384 Has_Interfaces (Generic_Actual)))
13385 then
13386 Elmt := First_Elmt (Op_List);
13387 while Present (Elmt) loop
13388 Subp := Node (Elmt);
996ae0b0 13389
ce2b6ba5
JM
13390 -- Literals are derived earlier in the process of building the
13391 -- derived type, and are skipped here.
950d3e7d 13392
ce2b6ba5 13393 if Ekind (Subp) = E_Enumeration_Literal then
9dfd2ff8 13394 null;
758c442c 13395
ce2b6ba5
JM
13396 -- The actual is a direct descendant and the common primitive
13397 -- operations appear in the same order.
7d7af38a 13398
ce2b6ba5
JM
13399 -- If the generic parent type is present, the derived type is an
13400 -- instance of a formal derived type, and within the instance its
13401 -- operations are those of the actual. We derive from the formal
13402 -- type but make the inherited operations aliases of the
13403 -- corresponding operations of the actual.
7d7af38a 13404
ce2b6ba5 13405 else
b4d7b435
AC
13406 pragma Assert (No (Node (Act_Elmt))
13407 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
13408 and then
13409 Type_Conformant (Subp, Node (Act_Elmt),
13410 Skip_Controlling_Formals => True)));
13411
ce2b6ba5
JM
13412 Derive_Subprogram
13413 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
88b32fc3 13414
ce2b6ba5
JM
13415 if Present (Act_Elmt) then
13416 Next_Elmt (Act_Elmt);
13417 end if;
13418 end if;
88b32fc3 13419
ce2b6ba5
JM
13420 Next_Elmt (Elmt);
13421 end loop;
13422
13423 -- Case 2: Derived_Type implements interfaces
13424
13425 else
13426 -- If the parent type has no predefined primitives we remove
13427 -- predefined primitives from the list of primitives of generic
13428 -- actual to simplify the complexity of this algorithm.
13429
13430 if Present (Generic_Actual) then
13431 declare
13432 Has_Predefined_Primitives : Boolean := False;
13433
13434 begin
13435 -- Check if the parent type has predefined primitives
13436
13437 Elmt := First_Elmt (Op_List);
13438 while Present (Elmt) loop
13439 Subp := Node (Elmt);
13440
13441 if Is_Predefined_Dispatching_Operation (Subp)
13442 and then not Comes_From_Source (Ultimate_Alias (Subp))
13443 then
13444 Has_Predefined_Primitives := True;
13445 exit;
13446 end if;
13447
13448 Next_Elmt (Elmt);
13449 end loop;
13450
13451 -- Remove predefined primitives of Generic_Actual. We must use
13452 -- an auxiliary list because in case of tagged types the value
13453 -- returned by Collect_Primitive_Operations is the value stored
13454 -- in its Primitive_Operations attribute (and we don't want to
13455 -- modify its current contents).
13456
13457 if not Has_Predefined_Primitives then
13458 declare
13459 Aux_List : constant Elist_Id := New_Elmt_List;
13460
13461 begin
13462 Elmt := First_Elmt (Act_List);
13463 while Present (Elmt) loop
13464 Subp := Node (Elmt);
13465
13466 if not Is_Predefined_Dispatching_Operation (Subp)
13467 or else Comes_From_Source (Subp)
13468 then
13469 Append_Elmt (Subp, Aux_List);
13470 end if;
13471
13472 Next_Elmt (Elmt);
13473 end loop;
13474
13475 Act_List := Aux_List;
13476 end;
88b32fc3 13477 end if;
996ae0b0 13478
ce2b6ba5
JM
13479 Act_Elmt := First_Elmt (Act_List);
13480 Act_Subp := Node (Act_Elmt);
13481 end;
13482 end if;
13483
13484 -- Stage 1: If the generic actual is not present we derive the
13485 -- primitives inherited from the parent type. If the generic parent
13486 -- type is present, the derived type is an instance of a formal
13487 -- derived type, and within the instance its operations are those of
13488 -- the actual. We derive from the formal type but make the inherited
13489 -- operations aliases of the corresponding operations of the actual.
13490
13491 Elmt := First_Elmt (Op_List);
13492 while Present (Elmt) loop
13493 Subp := Node (Elmt);
13494 Alias_Subp := Ultimate_Alias (Subp);
13495
74853971 13496 -- Do not derive internal entities of the parent that link
ff2efe85 13497 -- interface primitives with their covering primitive. These
74853971 13498 -- entities will be added to this type when frozen.
ce2b6ba5 13499
74853971
AC
13500 if Present (Interface_Alias (Subp)) then
13501 goto Continue;
13502 end if;
ce2b6ba5
JM
13503
13504 -- If the generic actual is present find the corresponding
13505 -- operation in the generic actual. If the parent type is a
13506 -- direct ancestor of the derived type then, even if it is an
13507 -- interface, the operations are inherited from the primary
13508 -- dispatch table and are in the proper order. If we detect here
13509 -- that primitives are not in the same order we traverse the list
13510 -- of primitive operations of the actual to find the one that
13511 -- implements the interface primitive.
13512
13513 if Need_Search
13514 or else
13515 (Present (Generic_Actual)
4a214958 13516 and then Present (Act_Subp)
b4d7b435
AC
13517 and then not
13518 (Primitive_Names_Match (Subp, Act_Subp)
13519 and then
13520 Type_Conformant (Subp, Act_Subp,
13521 Skip_Controlling_Formals => True)))
ce2b6ba5
JM
13522 then
13523 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual));
ce2b6ba5 13524
4a214958 13525 -- Remember that we need searching for all pending primitives
ce2b6ba5
JM
13526
13527 Need_Search := True;
13528
13529 -- Handle entities associated with interface primitives
13530
b4d7b435
AC
13531 if Present (Alias_Subp)
13532 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
ce2b6ba5 13533 and then not Is_Predefined_Dispatching_Operation (Subp)
71f62180 13534 then
b4d7b435
AC
13535 -- Search for the primitive in the homonym chain
13536
ce2b6ba5
JM
13537 Act_Subp :=
13538 Find_Primitive_Covering_Interface
13539 (Tagged_Type => Generic_Actual,
b4d7b435
AC
13540 Iface_Prim => Alias_Subp);
13541
13542 -- Previous search may not locate primitives covering
13543 -- interfaces defined in generics units or instantiations.
13544 -- (it fails if the covering primitive has formals whose
13545 -- type is also defined in generics or instantiations).
13546 -- In such case we search in the list of primitives of the
13547 -- generic actual for the internal entity that links the
13548 -- interface primitive and the covering primitive.
13549
13550 if No (Act_Subp)
13551 and then Is_Generic_Type (Parent_Type)
13552 then
13553 -- This code has been designed to handle only generic
13554 -- formals that implement interfaces that are defined
13555 -- in a generic unit or instantiation. If this code is
13556 -- needed for other cases we must review it because
13557 -- (given that it relies on Original_Location to locate
13558 -- the primitive of Generic_Actual that covers the
13559 -- interface) it could leave linked through attribute
13560 -- Alias entities of unrelated instantiations).
13561
13562 pragma Assert
13563 (Is_Generic_Unit
13564 (Scope (Find_Dispatching_Type (Alias_Subp)))
13565 or else
13566 Instantiation_Depth
13567 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
13568
13569 declare
13570 Iface_Prim_Loc : constant Source_Ptr :=
13571 Original_Location (Sloc (Alias_Subp));
13572 Elmt : Elmt_Id;
13573 Prim : Entity_Id;
13574 begin
13575 Elmt :=
13576 First_Elmt (Primitive_Operations (Generic_Actual));
13577
13578 Search : while Present (Elmt) loop
13579 Prim := Node (Elmt);
13580
13581 if Present (Interface_Alias (Prim))
13582 and then Original_Location
13583 (Sloc (Interface_Alias (Prim)))
13584 = Iface_Prim_Loc
13585 then
13586 Act_Subp := Alias (Prim);
13587 exit Search;
13588 end if;
13589
13590 Next_Elmt (Elmt);
13591 end loop Search;
13592 end;
13593 end if;
13594
13595 pragma Assert (Present (Act_Subp)
13596 or else Is_Abstract_Type (Generic_Actual)
13597 or else Serious_Errors_Detected > 0);
2b73cf68 13598
ce2b6ba5
JM
13599 -- Handle predefined primitives plus the rest of user-defined
13600 -- primitives
13601
13602 else
71f62180 13603 Act_Elmt := First_Elmt (Act_List);
2b73cf68 13604 while Present (Act_Elmt) loop
ce2b6ba5
JM
13605 Act_Subp := Node (Act_Elmt);
13606
13607 exit when Primitive_Names_Match (Subp, Act_Subp)
4a214958
AC
13608 and then Type_Conformant
13609 (Subp, Act_Subp,
13610 Skip_Controlling_Formals => True)
ce2b6ba5
JM
13611 and then No (Interface_Alias (Act_Subp));
13612
2b73cf68
JM
13613 Next_Elmt (Act_Elmt);
13614 end loop;
b4d7b435
AC
13615
13616 if No (Act_Elmt) then
13617 Act_Subp := Empty;
13618 end if;
2b73cf68 13619 end if;
ce2b6ba5 13620 end if;
2b73cf68 13621
ce2b6ba5
JM
13622 -- Case 1: If the parent is a limited interface then it has the
13623 -- predefined primitives of synchronized interfaces. However, the
13624 -- actual type may be a non-limited type and hence it does not
13625 -- have such primitives.
2b73cf68 13626
ce2b6ba5
JM
13627 if Present (Generic_Actual)
13628 and then not Present (Act_Subp)
13629 and then Is_Limited_Interface (Parent_Base)
13630 and then Is_Predefined_Interface_Primitive (Subp)
13631 then
13632 null;
2b73cf68 13633
4120ada7
RD
13634 -- Case 2: Inherit entities associated with interfaces that were
13635 -- not covered by the parent type. We exclude here null interface
13636 -- primitives because they do not need special management.
13637
13638 -- We also exclude interface operations that are renamings. If the
13639 -- subprogram is an explicit renaming of an interface primitive,
13640 -- it is a regular primitive operation, and the presence of its
13641 -- alias is not relevant: it has to be derived like any other
13642 -- primitive.
ce2b6ba5
JM
13643
13644 elsif Present (Alias (Subp))
4120ada7
RD
13645 and then Nkind (Unit_Declaration_Node (Subp)) /=
13646 N_Subprogram_Renaming_Declaration
ce2b6ba5
JM
13647 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13648 and then not
13649 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
4a214958 13650 and then Null_Present (Parent (Alias_Subp)))
ce2b6ba5 13651 then
ff2efe85
AC
13652 -- If this is an abstract private type then we transfer the
13653 -- derivation of the interface primitive from the partial view
13654 -- to the full view. This is safe because all the interfaces
13655 -- must be visible in the partial view. Done to avoid adding
13656 -- a new interface derivation to the private part of the
13657 -- enclosing package; otherwise this new derivation would be
13658 -- decorated as hidden when the analysis of the enclosing
13659 -- package completes.
13660
13661 if Is_Abstract_Type (Derived_Type)
13662 and then In_Private_Part (Current_Scope)
13663 and then Has_Private_Declaration (Derived_Type)
13664 then
13665 declare
13666 Partial_View : Entity_Id;
13667 Elmt : Elmt_Id;
13668 Ent : Entity_Id;
13669
13670 begin
13671 Partial_View := First_Entity (Current_Scope);
13672 loop
13673 exit when No (Partial_View)
13674 or else (Has_Private_Declaration (Partial_View)
13675 and then
13676 Full_View (Partial_View) = Derived_Type);
13677
13678 Next_Entity (Partial_View);
13679 end loop;
13680
13681 -- If the partial view was not found then the source code
13682 -- has errors and the derivation is not needed.
ce2b6ba5 13683
ff2efe85
AC
13684 if Present (Partial_View) then
13685 Elmt :=
13686 First_Elmt (Primitive_Operations (Partial_View));
13687 while Present (Elmt) loop
13688 Ent := Node (Elmt);
13689
13690 if Present (Alias (Ent))
13691 and then Ultimate_Alias (Ent) = Alias (Subp)
13692 then
13693 Append_Elmt
13694 (Ent, Primitive_Operations (Derived_Type));
13695 exit;
13696 end if;
13697
13698 Next_Elmt (Elmt);
13699 end loop;
13700
13701 -- If the interface primitive was not found in the
13702 -- partial view then this interface primitive was
13703 -- overridden. We add a derivation to activate in
13704 -- Derive_Progenitor_Subprograms the machinery to
13705 -- search for it.
13706
13707 if No (Elmt) then
13708 Derive_Interface_Subprogram
13709 (New_Subp => New_Subp,
13710 Subp => Subp,
13711 Actual_Subp => Act_Subp);
13712 end if;
13713 end if;
13714 end;
13715 else
13716 Derive_Interface_Subprogram
13717 (New_Subp => New_Subp,
13718 Subp => Subp,
13719 Actual_Subp => Act_Subp);
2b73cf68 13720 end if;
996ae0b0 13721
ce2b6ba5 13722 -- Case 3: Common derivation
88b32fc3 13723
ce2b6ba5
JM
13724 else
13725 Derive_Subprogram
13726 (New_Subp => New_Subp,
13727 Parent_Subp => Subp,
13728 Derived_Type => Derived_Type,
13729 Parent_Type => Parent_Base,
13730 Actual_Subp => Act_Subp);
13731 end if;
2b73cf68 13732
ce2b6ba5
JM
13733 -- No need to update Act_Elm if we must search for the
13734 -- corresponding operation in the generic actual
7d7af38a 13735
ce2b6ba5
JM
13736 if not Need_Search
13737 and then Present (Act_Elmt)
13738 then
13739 Next_Elmt (Act_Elmt);
13740 Act_Subp := Node (Act_Elmt);
13741 end if;
7d7af38a 13742
74853971 13743 <<Continue>>
7d7af38a
JM
13744 Next_Elmt (Elmt);
13745 end loop;
ce2b6ba5
JM
13746
13747 -- Inherit additional operations from progenitors. If the derived
13748 -- type is a generic actual, there are not new primitive operations
13749 -- for the type because it has those of the actual, and therefore
13750 -- nothing needs to be done. The renamings generated above are not
13751 -- primitive operations, and their purpose is simply to make the
13752 -- proper operations visible within an instantiation.
13753
13754 if No (Generic_Actual) then
13755 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
13756 end if;
7d7af38a 13757 end if;
ce2b6ba5
JM
13758
13759 -- Final check: Direct descendants must have their primitives in the
dd386db0 13760 -- same order. We exclude from this test untagged types and instances
ce2b6ba5
JM
13761 -- of formal derived types. We skip this test if we have already
13762 -- reported serious errors in the sources.
13763
13764 pragma Assert (not Is_Tagged_Type (Derived_Type)
13765 or else Present (Generic_Actual)
13766 or else Serious_Errors_Detected > 0
13767 or else Check_Derived_Type);
996ae0b0
RK
13768 end Derive_Subprograms;
13769
13770 --------------------------------
13771 -- Derived_Standard_Character --
13772 --------------------------------
13773
13774 procedure Derived_Standard_Character
71f62180
ES
13775 (N : Node_Id;
13776 Parent_Type : Entity_Id;
13777 Derived_Type : Entity_Id)
996ae0b0
RK
13778 is
13779 Loc : constant Source_Ptr := Sloc (N);
13780 Def : constant Node_Id := Type_Definition (N);
13781 Indic : constant Node_Id := Subtype_Indication (Def);
13782 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
13783 Implicit_Base : constant Entity_Id :=
13784 Create_Itype
13785 (E_Enumeration_Type, N, Derived_Type, 'B');
13786
13787 Lo : Node_Id;
13788 Hi : Node_Id;
996ae0b0
RK
13789
13790 begin
fbf5a39b 13791 Discard_Node (Process_Subtype (Indic, N));
996ae0b0
RK
13792
13793 Set_Etype (Implicit_Base, Parent_Base);
13794 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
13795 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
13796
13797 Set_Is_Character_Type (Implicit_Base, True);
13798 Set_Has_Delayed_Freeze (Implicit_Base);
13799
fbf5a39b
AC
13800 -- The bounds of the implicit base are the bounds of the parent base.
13801 -- Note that their type is the parent base.
13802
13803 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
13804 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
996ae0b0
RK
13805
13806 Set_Scalar_Range (Implicit_Base,
13807 Make_Range (Loc,
13808 Low_Bound => Lo,
13809 High_Bound => Hi));
13810
13811 Conditional_Delay (Derived_Type, Parent_Type);
13812
13813 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
13814 Set_Etype (Derived_Type, Implicit_Base);
13815 Set_Size_Info (Derived_Type, Parent_Type);
13816
13817 if Unknown_RM_Size (Derived_Type) then
13818 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
13819 end if;
13820
13821 Set_Is_Character_Type (Derived_Type, True);
13822
13823 if Nkind (Indic) /= N_Subtype_Indication then
fbf5a39b
AC
13824
13825 -- If no explicit constraint, the bounds are those
13826 -- of the parent type.
13827
13828 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
13829 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
13830 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
996ae0b0
RK
13831 end if;
13832
13833 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
13834
7d7af38a
JM
13835 -- Because the implicit base is used in the conversion of the bounds, we
13836 -- have to freeze it now. This is similar to what is done for numeric
13837 -- types, and it equally suspicious, but otherwise a non-static bound
13838 -- will have a reference to an unfrozen type, which is rejected by Gigi
13839 -- (???). This requires specific care for definition of stream
13840 -- attributes. For details, see comments at the end of
88b32fc3 13841 -- Build_Derived_Numeric_Type.
996ae0b0
RK
13842
13843 Freeze_Before (N, Implicit_Base);
996ae0b0
RK
13844 end Derived_Standard_Character;
13845
13846 ------------------------------
13847 -- Derived_Type_Declaration --
13848 ------------------------------
13849
13850 procedure Derived_Type_Declaration
13851 (T : Entity_Id;
13852 N : Node_Id;
13853 Is_Completion : Boolean)
13854 is
996ae0b0 13855 Parent_Type : Entity_Id;
996ae0b0 13856
c6823a20
EB
13857 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
13858 -- Check whether the parent type is a generic formal, or derives
13859 -- directly or indirectly from one.
13860
13861 ------------------------
13862 -- Comes_From_Generic --
13863 ------------------------
13864
13865 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
13866 begin
13867 if Is_Generic_Type (Typ) then
13868 return True;
13869
13870 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
13871 return True;
13872
13873 elsif Is_Private_Type (Typ)
13874 and then Present (Full_View (Typ))
13875 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
13876 then
13877 return True;
13878
13879 elsif Is_Generic_Actual_Type (Typ) then
13880 return True;
13881
13882 else
13883 return False;
13884 end if;
13885 end Comes_From_Generic;
13886
2b73cf68
JM
13887 -- Local variables
13888
13889 Def : constant Node_Id := Type_Definition (N);
13890 Iface_Def : Node_Id;
13891 Indic : constant Node_Id := Subtype_Indication (Def);
13892 Extension : constant Node_Id := Record_Extension_Part (Def);
13893 Parent_Node : Node_Id;
13894 Parent_Scope : Entity_Id;
13895 Taggd : Boolean;
13896
fa7c4d23
AC
13897 -- Start of processing for Derived_Type_Declaration
13898
996ae0b0
RK
13899 begin
13900 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
13901
758c442c
GD
13902 -- Ada 2005 (AI-251): In case of interface derivation check that the
13903 -- parent is also an interface.
13904
13905 if Interface_Present (Def) then
13906 if not Is_Interface (Parent_Type) then
6765b310 13907 Diagnose_Interface (Indic, Parent_Type);
758c442c
GD
13908
13909 else
2b73cf68
JM
13910 Parent_Node := Parent (Base_Type (Parent_Type));
13911 Iface_Def := Type_Definition (Parent_Node);
758c442c
GD
13912
13913 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
13914 -- other limited interfaces.
13915
13916 if Limited_Present (Def) then
13917 if Limited_Present (Iface_Def) then
13918 null;
13919
13920 elsif Protected_Present (Iface_Def) then
e358346d
AC
13921 Error_Msg_NE
13922 ("descendant of& must be declared"
13923 & " as a protected interface",
13924 N, Parent_Type);
758c442c
GD
13925
13926 elsif Synchronized_Present (Iface_Def) then
e358346d
AC
13927 Error_Msg_NE
13928 ("descendant of& must be declared"
13929 & " as a synchronized interface",
13930 N, Parent_Type);
758c442c
GD
13931
13932 elsif Task_Present (Iface_Def) then
e358346d
AC
13933 Error_Msg_NE
13934 ("descendant of& must be declared as a task interface",
13935 N, Parent_Type);
758c442c
GD
13936
13937 else
dc06abec
RD
13938 Error_Msg_N
13939 ("(Ada 2005) limited interface cannot "
13940 & "inherit from non-limited interface", Indic);
758c442c
GD
13941 end if;
13942
13943 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
13944 -- from non-limited or limited interfaces.
13945
13946 elsif not Protected_Present (Def)
13947 and then not Synchronized_Present (Def)
13948 and then not Task_Present (Def)
13949 then
13950 if Limited_Present (Iface_Def) then
13951 null;
13952
13953 elsif Protected_Present (Iface_Def) then
e358346d
AC
13954 Error_Msg_NE
13955 ("descendant of& must be declared"
13956 & " as a protected interface",
13957 N, Parent_Type);
758c442c
GD
13958
13959 elsif Synchronized_Present (Iface_Def) then
e358346d
AC
13960 Error_Msg_NE
13961 ("descendant of& must be declared"
13962 & " as a synchronized interface",
13963 N, Parent_Type);
758c442c
GD
13964
13965 elsif Task_Present (Iface_Def) then
e358346d
AC
13966 Error_Msg_NE
13967 ("descendant of& must be declared as a task interface",
13968 N, Parent_Type);
758c442c
GD
13969 else
13970 null;
13971 end if;
13972 end if;
13973 end if;
13974 end if;
13975
fea9e956
ES
13976 if Is_Tagged_Type (Parent_Type)
13977 and then Is_Concurrent_Type (Parent_Type)
13978 and then not Is_Interface (Parent_Type)
fea9e956 13979 then
dc06abec
RD
13980 Error_Msg_N
13981 ("parent type of a record extension cannot be "
13982 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
13983 Set_Etype (T, Any_Type);
fea9e956
ES
13984 return;
13985 end if;
13986
758c442c
GD
13987 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
13988 -- interfaces
13989
13990 if Is_Tagged_Type (Parent_Type)
13991 and then Is_Non_Empty_List (Interface_List (Def))
13992 then
13993 declare
9dfd2ff8
CC
13994 Intf : Node_Id;
13995 T : Entity_Id;
13996
758c442c 13997 begin
9dfd2ff8
CC
13998 Intf := First (Interface_List (Def));
13999 while Present (Intf) loop
14000 T := Find_Type_Of_Subtype_Indic (Intf);
758c442c
GD
14001
14002 if not Is_Interface (T) then
6765b310 14003 Diagnose_Interface (Intf, T);
653da906 14004
2b73cf68
JM
14005 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
14006 -- a limited type from having a nonlimited progenitor.
14007
14008 elsif (Limited_Present (Def)
14009 or else (not Is_Interface (Parent_Type)
14010 and then Is_Limited_Type (Parent_Type)))
653da906
RD
14011 and then not Is_Limited_Interface (T)
14012 then
14013 Error_Msg_NE
14014 ("progenitor interface& of limited type must be limited",
14015 N, T);
758c442c
GD
14016 end if;
14017
9dfd2ff8 14018 Next (Intf);
758c442c
GD
14019 end loop;
14020 end;
14021 end if;
14022
996ae0b0
RK
14023 if Parent_Type = Any_Type
14024 or else Etype (Parent_Type) = Any_Type
14025 or else (Is_Class_Wide_Type (Parent_Type)
fa7c4d23 14026 and then Etype (Parent_Type) = T)
996ae0b0 14027 then
a5b62485
AC
14028 -- If Parent_Type is undefined or illegal, make new type into a
14029 -- subtype of Any_Type, and set a few attributes to prevent cascaded
14030 -- errors. If this is a self-definition, emit error now.
996ae0b0
RK
14031
14032 if T = Parent_Type
14033 or else T = Etype (Parent_Type)
14034 then
14035 Error_Msg_N ("type cannot be used in its own definition", Indic);
14036 end if;
14037
14038 Set_Ekind (T, Ekind (Parent_Type));
14039 Set_Etype (T, Any_Type);
14040 Set_Scalar_Range (T, Scalar_Range (Any_Type));
14041
ef2a63ba
JM
14042 if Is_Tagged_Type (T)
14043 and then Is_Record_Type (T)
14044 then
14045 Set_Direct_Primitive_Operations (T, New_Elmt_List);
996ae0b0 14046 end if;
07fc65c4 14047
996ae0b0 14048 return;
996ae0b0
RK
14049 end if;
14050
653da906
RD
14051 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
14052 -- an interface is special because the list of interfaces in the full
14053 -- view can be given in any order. For example:
14054
14055 -- type A is interface;
14056 -- type B is interface and A;
14057 -- type D is new B with private;
14058 -- private
14059 -- type D is new A and B with null record; -- 1 --
14060
14061 -- In this case we perform the following transformation of -1-:
14062
14063 -- type D is new B and A with null record;
14064
14065 -- If the parent of the full-view covers the parent of the partial-view
14066 -- we have two possible cases:
14067
14068 -- 1) They have the same parent
14069 -- 2) The parent of the full-view implements some further interfaces
14070
14071 -- In both cases we do not need to perform the transformation. In the
14072 -- first case the source program is correct and the transformation is
14073 -- not needed; in the second case the source program does not fulfill
14074 -- the no-hidden interfaces rule (AI-396) and the error will be reported
14075 -- later.
14076
14077 -- This transformation not only simplifies the rest of the analysis of
14078 -- this type declaration but also simplifies the correct generation of
14079 -- the object layout to the expander.
14080
14081 if In_Private_Part (Current_Scope)
14082 and then Is_Interface (Parent_Type)
14083 then
14084 declare
14085 Iface : Node_Id;
14086 Partial_View : Entity_Id;
14087 Partial_View_Parent : Entity_Id;
14088 New_Iface : Node_Id;
14089
14090 begin
14091 -- Look for the associated private type declaration
14092
14093 Partial_View := First_Entity (Current_Scope);
14094 loop
57193e09 14095 exit when No (Partial_View)
653da906
RD
14096 or else (Has_Private_Declaration (Partial_View)
14097 and then Full_View (Partial_View) = T);
14098
14099 Next_Entity (Partial_View);
14100 end loop;
14101
14102 -- If the partial view was not found then the source code has
14103 -- errors and the transformation is not needed.
14104
14105 if Present (Partial_View) then
14106 Partial_View_Parent := Etype (Partial_View);
14107
14108 -- If the parent of the full-view covers the parent of the
14109 -- partial-view we have nothing else to do.
14110
14111 if Interface_Present_In_Ancestor
14112 (Parent_Type, Partial_View_Parent)
14113 then
14114 null;
14115
14116 -- Traverse the list of interfaces of the full-view to look
14117 -- for the parent of the partial-view and perform the tree
14118 -- transformation.
14119
14120 else
14121 Iface := First (Interface_List (Def));
14122 while Present (Iface) loop
14123 if Etype (Iface) = Etype (Partial_View) then
14124 Rewrite (Subtype_Indication (Def),
14125 New_Copy (Subtype_Indication
14126 (Parent (Partial_View))));
14127
7675ad4f
AC
14128 New_Iface :=
14129 Make_Identifier (Sloc (N), Chars (Parent_Type));
653da906
RD
14130 Append (New_Iface, Interface_List (Def));
14131
14132 -- Analyze the transformed code
14133
14134 Derived_Type_Declaration (T, N, Is_Completion);
14135 return;
14136 end if;
14137
14138 Next (Iface);
14139 end loop;
14140 end if;
14141 end if;
14142 end;
14143 end if;
14144
996ae0b0
RK
14145 -- Only composite types other than array types are allowed to have
14146 -- discriminants.
14147
14148 if Present (Discriminant_Specifications (N))
14149 and then (Is_Elementary_Type (Parent_Type)
14150 or else Is_Array_Type (Parent_Type))
14151 and then not Error_Posted (N)
14152 then
14153 Error_Msg_N
14154 ("elementary or array type cannot have discriminants",
14155 Defining_Identifier (First (Discriminant_Specifications (N))));
14156 Set_Has_Discriminants (T, False);
14157 end if;
14158
14159 -- In Ada 83, a derived type defined in a package specification cannot
14160 -- be used for further derivation until the end of its visible part.
14161 -- Note that derivation in the private part of the package is allowed.
14162
0ab80019 14163 if Ada_Version = Ada_83
996ae0b0
RK
14164 and then Is_Derived_Type (Parent_Type)
14165 and then In_Visible_Part (Scope (Parent_Type))
14166 then
0ab80019 14167 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
996ae0b0
RK
14168 Error_Msg_N
14169 ("(Ada 83): premature use of type for derivation", Indic);
14170 end if;
14171 end if;
14172
14173 -- Check for early use of incomplete or private type
14174
bce79204 14175 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
996ae0b0
RK
14176 Error_Msg_N ("premature derivation of incomplete type", Indic);
14177 return;
14178
14179 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
c6823a20 14180 and then not Comes_From_Generic (Parent_Type))
996ae0b0
RK
14181 or else Has_Private_Component (Parent_Type)
14182 then
14183 -- The ancestor type of a formal type can be incomplete, in which
14184 -- case only the operations of the partial view are available in
14185 -- the generic. Subsequent checks may be required when the full
14186 -- view is analyzed, to verify that derivation from a tagged type
14187 -- has an extension.
14188
14189 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
14190 null;
14191
14192 elsif No (Underlying_Type (Parent_Type))
14193 or else Has_Private_Component (Parent_Type)
14194 then
14195 Error_Msg_N
14196 ("premature derivation of derived or private type", Indic);
14197
14198 -- Flag the type itself as being in error, this prevents some
c6823a20 14199 -- nasty problems with subsequent uses of the malformed type.
996ae0b0
RK
14200
14201 Set_Error_Posted (T);
14202
14203 -- Check that within the immediate scope of an untagged partial
14204 -- view it's illegal to derive from the partial view if the
14205 -- full view is tagged. (7.3(7))
14206
14207 -- We verify that the Parent_Type is a partial view by checking
14208 -- that it is not a Full_Type_Declaration (i.e. a private type or
14209 -- private extension declaration), to distinguish a partial view
14210 -- from a derivation from a private type which also appears as
14211 -- E_Private_Type.
14212
14213 elsif Present (Full_View (Parent_Type))
14214 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
14215 and then not Is_Tagged_Type (Parent_Type)
14216 and then Is_Tagged_Type (Full_View (Parent_Type))
14217 then
14218 Parent_Scope := Scope (T);
14219 while Present (Parent_Scope)
14220 and then Parent_Scope /= Standard_Standard
14221 loop
14222 if Parent_Scope = Scope (Parent_Type) then
14223 Error_Msg_N
14224 ("premature derivation from type with tagged full view",
14225 Indic);
14226 end if;
14227
14228 Parent_Scope := Scope (Parent_Scope);
14229 end loop;
14230 end if;
14231 end if;
14232
14233 -- Check that form of derivation is appropriate
14234
14235 Taggd := Is_Tagged_Type (Parent_Type);
14236
14237 -- Perhaps the parent type should be changed to the class-wide type's
14238 -- specific type in this case to prevent cascading errors ???
14239
14240 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
14241 Error_Msg_N ("parent type must not be a class-wide type", Indic);
14242 return;
14243 end if;
14244
14245 if Present (Extension) and then not Taggd then
14246 Error_Msg_N
14247 ("type derived from untagged type cannot have extension", Indic);
14248
14249 elsif No (Extension) and then Taggd then
71d9e9f2 14250
a5b62485
AC
14251 -- If this declaration is within a private part (or body) of a
14252 -- generic instantiation then the derivation is allowed (the parent
14253 -- type can only appear tagged in this case if it's a generic actual
14254 -- type, since it would otherwise have been rejected in the analysis
14255 -- of the generic template).
996ae0b0
RK
14256
14257 if not Is_Generic_Actual_Type (Parent_Type)
14258 or else In_Visible_Part (Scope (Parent_Type))
14259 then
46256d9d
AC
14260 if Is_Class_Wide_Type (Parent_Type) then
14261 Error_Msg_N
14262 ("parent type must not be a class-wide type", Indic);
14263
14264 -- Use specific type to prevent cascaded errors.
14265
14266 Parent_Type := Etype (Parent_Type);
14267
14268 else
14269 Error_Msg_N
14270 ("type derived from tagged type must have extension", Indic);
14271 end if;
996ae0b0
RK
14272 end if;
14273 end if;
14274
88b32fc3
BD
14275 -- AI-443: Synchronized formal derived types require a private
14276 -- extension. There is no point in checking the ancestor type or
14277 -- the progenitors since the construct is wrong to begin with.
14278
0791fbe9 14279 if Ada_Version >= Ada_2005
88b32fc3
BD
14280 and then Is_Generic_Type (T)
14281 and then Present (Original_Node (N))
14282 then
14283 declare
14284 Decl : constant Node_Id := Original_Node (N);
14285
14286 begin
14287 if Nkind (Decl) = N_Formal_Type_Declaration
14288 and then Nkind (Formal_Type_Definition (Decl)) =
14289 N_Formal_Derived_Type_Definition
14290 and then Synchronized_Present (Formal_Type_Definition (Decl))
14291 and then No (Extension)
14292
14293 -- Avoid emitting a duplicate error message
14294
14295 and then not Error_Posted (Indic)
14296 then
14297 Error_Msg_N
14298 ("synchronized derived type must have extension", N);
14299 end if;
14300 end;
14301 end if;
14302
fa961f76
ES
14303 if Null_Exclusion_Present (Def)
14304 and then not Is_Access_Type (Parent_Type)
14305 then
14306 Error_Msg_N ("null exclusion can only apply to an access type", N);
14307 end if;
14308
c206e8fd 14309 -- Avoid deriving parent primitives of underlying record views
9013065b
AC
14310
14311 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
14312 Derive_Subps => not Is_Underlying_Record_View (T));
653da906 14313
88b32fc3 14314 -- AI-419: The parent type of an explicitly limited derived type must
57193e09 14315 -- be a limited type or a limited interface.
653da906
RD
14316
14317 if Limited_Present (Def) then
14318 Set_Is_Limited_Record (T);
14319
030d25f4
JM
14320 if Is_Interface (T) then
14321 Set_Is_Limited_Interface (T);
14322 end if;
14323
653da906 14324 if not Is_Limited_Type (Parent_Type)
57193e09
TQ
14325 and then
14326 (not Is_Interface (Parent_Type)
14327 or else not Is_Limited_Interface (Parent_Type))
653da906 14328 then
ef237104 14329 -- AI05-0096: a derivation in the private part of an instance is
2604ec03
AC
14330 -- legal if the generic formal is untagged limited, and the actual
14331 -- is non-limited.
14332
14333 if Is_Generic_Actual_Type (Parent_Type)
14334 and then In_Private_Part (Current_Scope)
14335 and then
14336 not Is_Tagged_Type
ef237104 14337 (Generic_Parent_Type (Parent (Parent_Type)))
2604ec03
AC
14338 then
14339 null;
14340
14341 else
14342 Error_Msg_NE
14343 ("parent type& of limited type must be limited",
14344 N, Parent_Type);
14345 end if;
653da906
RD
14346 end if;
14347 end if;
7ff2d234
AC
14348
14349 -- In SPARK or ALFA, there are no derived type definitions other than
14350 -- type extensions of tagged record types.
14351
14352 if Formal_Verification_Mode
14353 and then No (Extension)
14354 then
14355 Error_Msg_F ("|~~derived type is not allowed", N);
14356 end if;
996ae0b0
RK
14357 end Derived_Type_Declaration;
14358
6765b310
ES
14359 ------------------------
14360 -- Diagnose_Interface --
14361 ------------------------
14362
14363 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
14364 begin
14365 if not Is_Interface (E)
14366 and then E /= Any_Type
14367 then
14368 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
14369 end if;
14370 end Diagnose_Interface;
14371
996ae0b0
RK
14372 ----------------------------------
14373 -- Enumeration_Type_Declaration --
14374 ----------------------------------
14375
14376 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14377 Ev : Uint;
14378 L : Node_Id;
14379 R_Node : Node_Id;
14380 B_Node : Node_Id;
14381
14382 begin
14383 -- Create identifier node representing lower bound
14384
14385 B_Node := New_Node (N_Identifier, Sloc (Def));
14386 L := First (Literals (Def));
14387 Set_Chars (B_Node, Chars (L));
14388 Set_Entity (B_Node, L);
14389 Set_Etype (B_Node, T);
14390 Set_Is_Static_Expression (B_Node, True);
14391
14392 R_Node := New_Node (N_Range, Sloc (Def));
14393 Set_Low_Bound (R_Node, B_Node);
14394
14395 Set_Ekind (T, E_Enumeration_Type);
14396 Set_First_Literal (T, L);
14397 Set_Etype (T, T);
14398 Set_Is_Constrained (T);
14399
14400 Ev := Uint_0;
14401
14402 -- Loop through literals of enumeration type setting pos and rep values
00838d9a
AC
14403 -- except that if the Ekind is already set, then it means the literal
14404 -- was already constructed (case of a derived type declaration and we
14405 -- should not disturb the Pos and Rep values.
996ae0b0
RK
14406
14407 while Present (L) loop
14408 if Ekind (L) /= E_Enumeration_Literal then
14409 Set_Ekind (L, E_Enumeration_Literal);
14410 Set_Enumeration_Pos (L, Ev);
14411 Set_Enumeration_Rep (L, Ev);
14412 Set_Is_Known_Valid (L, True);
14413 end if;
14414
14415 Set_Etype (L, T);
14416 New_Overloaded_Entity (L);
14417 Generate_Definition (L);
14418 Set_Convention (L, Convention_Intrinsic);
14419
30196a76
RD
14420 -- Case of character literal
14421
996ae0b0
RK
14422 if Nkind (L) = N_Defining_Character_Literal then
14423 Set_Is_Character_Type (T, True);
30196a76
RD
14424
14425 -- Check violation of No_Wide_Characters
14426
7a963087 14427 if Restriction_Check_Required (No_Wide_Characters) then
30196a76
RD
14428 Get_Name_String (Chars (L));
14429
14430 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
14431 Check_Restriction (No_Wide_Characters, L);
14432 end if;
14433 end if;
996ae0b0
RK
14434 end if;
14435
14436 Ev := Ev + 1;
14437 Next (L);
14438 end loop;
14439
14440 -- Now create a node representing upper bound
14441
14442 B_Node := New_Node (N_Identifier, Sloc (Def));
14443 Set_Chars (B_Node, Chars (Last (Literals (Def))));
14444 Set_Entity (B_Node, Last (Literals (Def)));
14445 Set_Etype (B_Node, T);
14446 Set_Is_Static_Expression (B_Node, True);
14447
14448 Set_High_Bound (R_Node, B_Node);
2b73cf68
JM
14449
14450 -- Initialize various fields of the type. Some of this information
14451 -- may be overwritten later through rep.clauses.
14452
14453 Set_Scalar_Range (T, R_Node);
14454 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
14455 Set_Enum_Esize (T);
14456 Set_Enum_Pos_To_Rep (T, Empty);
996ae0b0 14457
fbf5a39b 14458 -- Set Discard_Names if configuration pragma set, or if there is
996ae0b0
RK
14459 -- a parameterless pragma in the current declarative region
14460
14461 if Global_Discard_Names
14462 or else Discard_Names (Scope (T))
14463 then
14464 Set_Discard_Names (T);
14465 end if;
07fc65c4
GB
14466
14467 -- Process end label if there is one
14468
14469 if Present (Def) then
14470 Process_End_Label (Def, 'e', T);
14471 end if;
996ae0b0
RK
14472 end Enumeration_Type_Declaration;
14473
996ae0b0 14474 ---------------------------------
fbf5a39b 14475 -- Expand_To_Stored_Constraint --
996ae0b0
RK
14476 ---------------------------------
14477
fbf5a39b 14478 function Expand_To_Stored_Constraint
996ae0b0 14479 (Typ : Entity_Id;
b0f26df5 14480 Constraint : Elist_Id) return Elist_Id
996ae0b0
RK
14481 is
14482 Explicitly_Discriminated_Type : Entity_Id;
14483 Expansion : Elist_Id;
14484 Discriminant : Entity_Id;
14485
14486 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
ffe9aba8 14487 -- Find the nearest type that actually specifies discriminants
996ae0b0
RK
14488
14489 ---------------------------------
14490 -- Type_With_Explicit_Discrims --
14491 ---------------------------------
14492
14493 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
14494 Typ : constant E := Base_Type (Id);
14495
14496 begin
14497 if Ekind (Typ) in Incomplete_Or_Private_Kind then
14498 if Present (Full_View (Typ)) then
14499 return Type_With_Explicit_Discrims (Full_View (Typ));
14500 end if;
14501
14502 else
14503 if Has_Discriminants (Typ) then
14504 return Typ;
14505 end if;
14506 end if;
14507
14508 if Etype (Typ) = Typ then
14509 return Empty;
14510 elsif Has_Discriminants (Typ) then
14511 return Typ;
14512 else
14513 return Type_With_Explicit_Discrims (Etype (Typ));
14514 end if;
14515
14516 end Type_With_Explicit_Discrims;
14517
fbf5a39b 14518 -- Start of processing for Expand_To_Stored_Constraint
996ae0b0
RK
14519
14520 begin
14521 if No (Constraint)
14522 or else Is_Empty_Elmt_List (Constraint)
14523 then
14524 return No_Elist;
14525 end if;
14526
14527 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
14528
14529 if No (Explicitly_Discriminated_Type) then
14530 return No_Elist;
14531 end if;
14532
14533 Expansion := New_Elmt_List;
14534
14535 Discriminant :=
fbf5a39b 14536 First_Stored_Discriminant (Explicitly_Discriminated_Type);
996ae0b0 14537 while Present (Discriminant) loop
996ae0b0
RK
14538 Append_Elmt (
14539 Get_Discriminant_Value (
14540 Discriminant, Explicitly_Discriminated_Type, Constraint),
14541 Expansion);
fbf5a39b 14542 Next_Stored_Discriminant (Discriminant);
996ae0b0
RK
14543 end loop;
14544
14545 return Expansion;
fbf5a39b 14546 end Expand_To_Stored_Constraint;
996ae0b0 14547
dc06abec
RD
14548 ---------------------------
14549 -- Find_Hidden_Interface --
14550 ---------------------------
14551
14552 function Find_Hidden_Interface
14553 (Src : Elist_Id;
14554 Dest : Elist_Id) return Entity_Id
14555 is
14556 Iface : Entity_Id;
14557 Iface_Elmt : Elmt_Id;
14558
14559 begin
14560 if Present (Src) and then Present (Dest) then
14561 Iface_Elmt := First_Elmt (Src);
14562 while Present (Iface_Elmt) loop
14563 Iface := Node (Iface_Elmt);
14564
14565 if Is_Interface (Iface)
14566 and then not Contain_Interface (Iface, Dest)
14567 then
14568 return Iface;
14569 end if;
14570
14571 Next_Elmt (Iface_Elmt);
14572 end loop;
14573 end if;
14574
14575 return Empty;
14576 end Find_Hidden_Interface;
14577
996ae0b0
RK
14578 --------------------
14579 -- Find_Type_Name --
14580 --------------------
14581
14582 function Find_Type_Name (N : Node_Id) return Entity_Id is
14583 Id : constant Entity_Id := Defining_Identifier (N);
14584 Prev : Entity_Id;
14585 New_Id : Entity_Id;
14586 Prev_Par : Node_Id;
14587
33931112 14588 procedure Tag_Mismatch;
abed5dc6 14589 -- Diagnose a tagged partial view whose full view is untagged.
33931112
JM
14590 -- We post the message on the full view, with a reference to
14591 -- the previous partial view. The partial view can be private
14592 -- or incomplete, and these are handled in a different manner,
14593 -- so we determine the position of the error message from the
14594 -- respective slocs of both.
14595
14596 ------------------
14597 -- Tag_Mismatch --
14598 ------------------
14599
14600 procedure Tag_Mismatch is
14601 begin
14602 if Sloc (Prev) < Sloc (Id) then
6191e212
AC
14603 if Ada_Version >= Ada_2012
14604 and then Nkind (N) = N_Private_Type_Declaration
14605 then
14606 Error_Msg_NE
14607 ("declaration of private } must be a tagged type ", Id, Prev);
14608 else
14609 Error_Msg_NE
14610 ("full declaration of } must be a tagged type ", Id, Prev);
14611 end if;
33931112 14612 else
6191e212
AC
14613 if Ada_Version >= Ada_2012
14614 and then Nkind (N) = N_Private_Type_Declaration
14615 then
14616 Error_Msg_NE
14617 ("declaration of private } must be a tagged type ", Prev, Id);
14618 else
14619 Error_Msg_NE
14620 ("full declaration of } must be a tagged type ", Prev, Id);
14621 end if;
33931112
JM
14622 end if;
14623 end Tag_Mismatch;
14624
d8221f45 14625 -- Start of processing for Find_Type_Name
33931112 14626
996ae0b0 14627 begin
71d9e9f2 14628 -- Find incomplete declaration, if one was given
996ae0b0
RK
14629
14630 Prev := Current_Entity_In_Scope (Id);
14631
6191e212
AC
14632 -- New type declaration
14633
14634 if No (Prev) then
14635 Enter_Name (Id);
14636 return Id;
996ae0b0 14637
6191e212 14638 -- Previous declaration exists
996ae0b0 14639
6191e212 14640 else
996ae0b0
RK
14641 Prev_Par := Parent (Prev);
14642
6191e212
AC
14643 -- Error if not incomplete/private case except if previous
14644 -- declaration is implicit, etc. Enter_Name will emit error if
14645 -- appropriate.
14646
996ae0b0
RK
14647 if not Is_Incomplete_Or_Private_Type (Prev) then
14648 Enter_Name (Id);
14649 New_Id := Id;
14650
6191e212
AC
14651 -- Check invalid completion of private or incomplete type
14652
7d7af38a
JM
14653 elsif not Nkind_In (N, N_Full_Type_Declaration,
14654 N_Task_Type_Declaration,
14655 N_Protected_Type_Declaration)
6191e212
AC
14656 and then
14657 (Ada_Version < Ada_2012
14658 or else not Is_Incomplete_Type (Prev)
14659 or else not Nkind_In (N, N_Private_Type_Declaration,
14660 N_Private_Extension_Declaration))
996ae0b0
RK
14661 then
14662 -- Completion must be a full type declarations (RM 7.3(4))
14663
14664 Error_Msg_Sloc := Sloc (Prev);
14665 Error_Msg_NE ("invalid completion of }", Id, Prev);
14666
14667 -- Set scope of Id to avoid cascaded errors. Entity is never
14668 -- examined again, except when saving globals in generics.
14669
14670 Set_Scope (Id, Current_Scope);
14671 New_Id := Id;
14672
d4429d51
ES
14673 -- If this is a repeated incomplete declaration, no further
14674 -- checks are possible.
14675
14676 if Nkind (N) = N_Incomplete_Type_Declaration then
14677 return Prev;
14678 end if;
14679
996ae0b0
RK
14680 -- Case of full declaration of incomplete type
14681
6191e212
AC
14682 elsif Ekind (Prev) = E_Incomplete_Type
14683 and then (Ada_Version < Ada_2012
e606088a
AC
14684 or else No (Full_View (Prev))
14685 or else not Is_Private_Type (Full_View (Prev)))
6191e212 14686 then
996ae0b0 14687
a5b62485
AC
14688 -- Indicate that the incomplete declaration has a matching full
14689 -- declaration. The defining occurrence of the incomplete
996ae0b0
RK
14690 -- declaration remains the visible one, and the procedure
14691 -- Get_Full_View dereferences it whenever the type is used.
14692
14693 if Present (Full_View (Prev)) then
14694 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14695 end if;
14696
14697 Set_Full_View (Prev, Id);
14698 Append_Entity (Id, Current_Scope);
14699 Set_Is_Public (Id, Is_Public (Prev));
14700 Set_Is_Internal (Id);
14701 New_Id := Prev;
14702
6191e212
AC
14703 -- If the incomplete view is tagged, a class_wide type has been
14704 -- created already. Use it for the private type as well, in order
14705 -- to prevent multiple incompatible class-wide types that may be
14706 -- created for self-referential anonymous access components.
14707
14708 if Is_Tagged_Type (Prev)
14709 and then Present (Class_Wide_Type (Prev))
14710 then
14711 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
14712 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
14713 Set_Etype (Class_Wide_Type (Id), Id);
14714 end if;
14715
996ae0b0
RK
14716 -- Case of full declaration of private type
14717
14718 else
6191e212
AC
14719 -- If the private type was a completion of an incomplete type then
14720 -- update Prev to reference the private type
14721
14722 if Ada_Version >= Ada_2012
14723 and then Ekind (Prev) = E_Incomplete_Type
14724 and then Present (Full_View (Prev))
14725 and then Is_Private_Type (Full_View (Prev))
14726 then
14727 Prev := Full_View (Prev);
14728 Prev_Par := Parent (Prev);
14729 end if;
14730
996ae0b0
RK
14731 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
14732 if Etype (Prev) /= Prev then
14733
14734 -- Prev is a private subtype or a derived type, and needs
14735 -- no completion.
14736
14737 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14738 New_Id := Id;
14739
14740 elsif Ekind (Prev) = E_Private_Type
7d7af38a
JM
14741 and then Nkind_In (N, N_Task_Type_Declaration,
14742 N_Protected_Type_Declaration)
996ae0b0
RK
14743 then
14744 Error_Msg_N
14745 ("completion of nonlimited type cannot be limited", N);
57193e09
TQ
14746
14747 elsif Ekind (Prev) = E_Record_Type_With_Private
7d7af38a
JM
14748 and then Nkind_In (N, N_Task_Type_Declaration,
14749 N_Protected_Type_Declaration)
57193e09
TQ
14750 then
14751 if not Is_Limited_Record (Prev) then
14752 Error_Msg_N
14753 ("completion of nonlimited type cannot be limited", N);
14754
14755 elsif No (Interface_List (N)) then
14756 Error_Msg_N
14757 ("completion of tagged private type must be tagged",
ff2e7c1e 14758 N);
57193e09 14759 end if;
26a43556
AC
14760
14761 elsif Nkind (N) = N_Full_Type_Declaration
14762 and then
14763 Nkind (Type_Definition (N)) = N_Record_Definition
14764 and then Interface_Present (Type_Definition (N))
14765 then
14766 Error_Msg_N
ff2e7c1e 14767 ("completion of private type cannot be an interface", N);
996ae0b0
RK
14768 end if;
14769
dc06abec
RD
14770 -- Ada 2005 (AI-251): Private extension declaration of a task
14771 -- type or a protected type. This case arises when covering
14772 -- interface types.
758c442c 14773
7d7af38a
JM
14774 elsif Nkind_In (N, N_Task_Type_Declaration,
14775 N_Protected_Type_Declaration)
758c442c
GD
14776 then
14777 null;
14778
996ae0b0
RK
14779 elsif Nkind (N) /= N_Full_Type_Declaration
14780 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
14781 then
71d9e9f2
ES
14782 Error_Msg_N
14783 ("full view of private extension must be an extension", N);
996ae0b0
RK
14784
14785 elsif not (Abstract_Present (Parent (Prev)))
14786 and then Abstract_Present (Type_Definition (N))
14787 then
71d9e9f2
ES
14788 Error_Msg_N
14789 ("full view of non-abstract extension cannot be abstract", N);
996ae0b0
RK
14790 end if;
14791
14792 if not In_Private_Part (Current_Scope) then
14793 Error_Msg_N
71d9e9f2 14794 ("declaration of full view must appear in private part", N);
996ae0b0
RK
14795 end if;
14796
14797 Copy_And_Swap (Prev, Id);
996ae0b0
RK
14798 Set_Has_Private_Declaration (Prev);
14799 Set_Has_Private_Declaration (Id);
07fc65c4
GB
14800
14801 -- If no error, propagate freeze_node from private to full view.
14802 -- It may have been generated for an early operational item.
14803
14804 if Present (Freeze_Node (Id))
14805 and then Serious_Errors_Detected = 0
14806 and then No (Full_View (Id))
14807 then
14808 Set_Freeze_Node (Prev, Freeze_Node (Id));
14809 Set_Freeze_Node (Id, Empty);
14810 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
14811 end if;
14812
14813 Set_Full_View (Id, Prev);
996ae0b0
RK
14814 New_Id := Prev;
14815 end if;
14816
33931112 14817 -- Verify that full declaration conforms to partial one
996ae0b0
RK
14818
14819 if Is_Incomplete_Or_Private_Type (Prev)
14820 and then Present (Discriminant_Specifications (Prev_Par))
14821 then
14822 if Present (Discriminant_Specifications (N)) then
14823 if Ekind (Prev) = E_Incomplete_Type then
14824 Check_Discriminant_Conformance (N, Prev, Prev);
14825 else
14826 Check_Discriminant_Conformance (N, Prev, Id);
14827 end if;
14828
14829 else
14830 Error_Msg_N
14831 ("missing discriminants in full type declaration", N);
14832
14833 -- To avoid cascaded errors on subsequent use, share the
14834 -- discriminants of the partial view.
14835
14836 Set_Discriminant_Specifications (N,
14837 Discriminant_Specifications (Prev_Par));
14838 end if;
14839 end if;
14840
33931112 14841 -- A prior untagged partial view can have an associated class-wide
abed5dc6
AC
14842 -- type due to use of the class attribute, and in this case the full
14843 -- type must also be tagged. This Ada 95 usage is deprecated in favor
14844 -- of incomplete tagged declarations, but we check for it.
996ae0b0
RK
14845
14846 if Is_Type (Prev)
14847 and then (Is_Tagged_Type (Prev)
6191e212 14848 or else Present (Class_Wide_Type (Prev)))
996ae0b0 14849 then
6191e212
AC
14850 -- Ada 2012 (AI05-0162): A private type may be the completion of
14851 -- an incomplete type
14852
14853 if Ada_Version >= Ada_2012
14854 and then Is_Incomplete_Type (Prev)
14855 and then Nkind_In (N, N_Private_Type_Declaration,
14856 N_Private_Extension_Declaration)
14857 then
14858 -- No need to check private extensions since they are tagged
14859
14860 if Nkind (N) = N_Private_Type_Declaration
14861 and then not Tagged_Present (N)
14862 then
14863 Tag_Mismatch;
14864 end if;
14865
af4133b2
ST
14866 -- The full declaration is either a tagged type (including
14867 -- a synchronized type that implements interfaces) or a
14868 -- type extension, otherwise this is an error.
14869
6191e212
AC
14870 elsif Nkind_In (N, N_Task_Type_Declaration,
14871 N_Protected_Type_Declaration)
af4133b2
ST
14872 then
14873 if No (Interface_List (N))
14874 and then not Error_Posted (N)
14875 then
33931112 14876 Tag_Mismatch;
af4133b2
ST
14877 end if;
14878
14879 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
14880
14881 -- Indicate that the previous declaration (tagged incomplete
14882 -- or private declaration) requires the same on the full one.
996ae0b0 14883
996ae0b0 14884 if not Tagged_Present (Type_Definition (N)) then
33931112 14885 Tag_Mismatch;
996ae0b0 14886 Set_Is_Tagged_Type (Id);
996ae0b0
RK
14887 end if;
14888
14889 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
14890 if No (Record_Extension_Part (Type_Definition (N))) then
ed2233dc
AC
14891 Error_Msg_NE
14892 ("full declaration of } must be a record extension",
14893 Prev, Id);
93bcda23 14894
03b64787 14895 -- Set some attributes to produce a usable full view
93bcda23 14896
996ae0b0 14897 Set_Is_Tagged_Type (Id);
996ae0b0
RK
14898 end if;
14899
14900 else
33931112 14901 Tag_Mismatch;
996ae0b0
RK
14902 end if;
14903 end if;
14904
14905 return New_Id;
996ae0b0
RK
14906 end if;
14907 end Find_Type_Name;
14908
14909 -------------------------
14910 -- Find_Type_Of_Object --
14911 -------------------------
14912
14913 function Find_Type_Of_Object
14914 (Obj_Def : Node_Id;
b0f26df5 14915 Related_Nod : Node_Id) return Entity_Id
996ae0b0
RK
14916 is
14917 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
a397db96 14918 P : Node_Id := Parent (Obj_Def);
996ae0b0
RK
14919 T : Entity_Id;
14920 Nam : Name_Id;
14921
14922 begin
a397db96
AC
14923 -- If the parent is a component_definition node we climb to the
14924 -- component_declaration node
14925
14926 if Nkind (P) = N_Component_Definition then
14927 P := Parent (P);
14928 end if;
14929
996ae0b0
RK
14930 -- Case of an anonymous array subtype
14931
7d7af38a
JM
14932 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
14933 N_Unconstrained_Array_Definition)
996ae0b0
RK
14934 then
14935 T := Empty;
14936 Array_Type_Declaration (T, Obj_Def);
14937
ffe9aba8 14938 -- Create an explicit subtype whenever possible
996ae0b0
RK
14939
14940 elsif Nkind (P) /= N_Component_Declaration
14941 and then Def_Kind = N_Subtype_Indication
14942 then
14943 -- Base name of subtype on object name, which will be unique in
14944 -- the current scope.
14945
14946 -- If this is a duplicate declaration, return base type, to avoid
14947 -- generating duplicate anonymous types.
14948
14949 if Error_Posted (P) then
14950 Analyze (Subtype_Mark (Obj_Def));
14951 return Entity (Subtype_Mark (Obj_Def));
14952 end if;
14953
14954 Nam :=
14955 New_External_Name
14956 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
14957
14958 T := Make_Defining_Identifier (Sloc (P), Nam);
14959
14960 Insert_Action (Obj_Def,
14961 Make_Subtype_Declaration (Sloc (P),
14962 Defining_Identifier => T,
14963 Subtype_Indication => Relocate_Node (Obj_Def)));
14964
aa720a54 14965 -- This subtype may need freezing, and this will not be done
a5b62485
AC
14966 -- automatically if the object declaration is not in declarative
14967 -- part. Since this is an object declaration, the type cannot always
14968 -- be frozen here. Deferred constants do not freeze their type
14969 -- (which often enough will be private).
996ae0b0
RK
14970
14971 if Nkind (P) = N_Object_Declaration
14972 and then Constant_Present (P)
14973 and then No (Expression (P))
14974 then
14975 null;
996ae0b0 14976 else
c159409f 14977 Insert_Actions (Obj_Def, Freeze_Entity (T, P));
996ae0b0
RK
14978 end if;
14979
758c442c
GD
14980 -- Ada 2005 AI-406: the object definition in an object declaration
14981 -- can be an access definition.
14982
14983 elsif Def_Kind = N_Access_Definition then
14984 T := Access_Definition (Related_Nod, Obj_Def);
2b73cf68 14985 Set_Is_Local_Anonymous_Access (T);
88b32fc3
BD
14986
14987 -- Otherwise, the object definition is just a subtype_mark
758c442c 14988
996ae0b0
RK
14989 else
14990 T := Process_Subtype (Obj_Def, Related_Nod);
14991 end if;
14992
14993 return T;
14994 end Find_Type_Of_Object;
14995
14996 --------------------------------
14997 -- Find_Type_Of_Subtype_Indic --
14998 --------------------------------
14999
15000 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
15001 Typ : Entity_Id;
15002
15003 begin
15004 -- Case of subtype mark with a constraint
15005
15006 if Nkind (S) = N_Subtype_Indication then
15007 Find_Type (Subtype_Mark (S));
15008 Typ := Entity (Subtype_Mark (S));
15009
15010 if not
15011 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
15012 then
15013 Error_Msg_N
15014 ("incorrect constraint for this kind of type", Constraint (S));
15015 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
15016 end if;
15017
15018 -- Otherwise we have a subtype mark without a constraint
15019
dd5875a6
ES
15020 elsif Error_Posted (S) then
15021 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
15022 return Any_Type;
15023
996ae0b0
RK
15024 else
15025 Find_Type (S);
15026 Typ := Entity (S);
15027 end if;
15028
ce4a6e84
RD
15029 -- Check No_Wide_Characters restriction
15030
30196a76 15031 Check_Wide_Character_Restriction (Typ, S);
996ae0b0
RK
15032
15033 return Typ;
15034 end Find_Type_Of_Subtype_Indic;
15035
15036 -------------------------------------
15037 -- Floating_Point_Type_Declaration --
15038 -------------------------------------
15039
15040 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15041 Digs : constant Node_Id := Digits_Expression (Def);
15042 Digs_Val : Uint;
15043 Base_Typ : Entity_Id;
15044 Implicit_Base : Entity_Id;
15045 Bound : Node_Id;
15046
15047 function Can_Derive_From (E : Entity_Id) return Boolean;
15048 -- Find if given digits value allows derivation from specified type
15049
fbf5a39b
AC
15050 ---------------------
15051 -- Can_Derive_From --
15052 ---------------------
15053
996ae0b0
RK
15054 function Can_Derive_From (E : Entity_Id) return Boolean is
15055 Spec : constant Entity_Id := Real_Range_Specification (Def);
15056
15057 begin
15058 if Digs_Val > Digits_Value (E) then
15059 return False;
15060 end if;
15061
15062 if Present (Spec) then
15063 if Expr_Value_R (Type_Low_Bound (E)) >
15064 Expr_Value_R (Low_Bound (Spec))
15065 then
15066 return False;
15067 end if;
15068
15069 if Expr_Value_R (Type_High_Bound (E)) <
15070 Expr_Value_R (High_Bound (Spec))
15071 then
15072 return False;
15073 end if;
15074 end if;
15075
15076 return True;
15077 end Can_Derive_From;
15078
15079 -- Start of processing for Floating_Point_Type_Declaration
15080
15081 begin
15082 Check_Restriction (No_Floating_Point, Def);
15083
15084 -- Create an implicit base type
15085
15086 Implicit_Base :=
15087 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
15088
15089 -- Analyze and verify digits value
15090
15091 Analyze_And_Resolve (Digs, Any_Integer);
15092 Check_Digits_Expression (Digs);
15093 Digs_Val := Expr_Value (Digs);
15094
15095 -- Process possible range spec and find correct type to derive from
15096
15097 Process_Real_Range_Specification (Def);
15098
15099 if Can_Derive_From (Standard_Short_Float) then
15100 Base_Typ := Standard_Short_Float;
15101 elsif Can_Derive_From (Standard_Float) then
15102 Base_Typ := Standard_Float;
15103 elsif Can_Derive_From (Standard_Long_Float) then
15104 Base_Typ := Standard_Long_Float;
15105 elsif Can_Derive_From (Standard_Long_Long_Float) then
15106 Base_Typ := Standard_Long_Long_Float;
15107
aa720a54 15108 -- If we can't derive from any existing type, use long_long_float
996ae0b0
RK
15109 -- and give appropriate message explaining the problem.
15110
15111 else
15112 Base_Typ := Standard_Long_Long_Float;
15113
15114 if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
15115 Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
15116 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
15117
15118 else
15119 Error_Msg_N
15120 ("range too large for any predefined type",
15121 Real_Range_Specification (Def));
15122 end if;
15123 end if;
15124
15125 -- If there are bounds given in the declaration use them as the bounds
15126 -- of the type, otherwise use the bounds of the predefined base type
15127 -- that was chosen based on the Digits value.
15128
15129 if Present (Real_Range_Specification (Def)) then
15130 Set_Scalar_Range (T, Real_Range_Specification (Def));
15131 Set_Is_Constrained (T);
15132
15133 -- The bounds of this range must be converted to machine numbers
15134 -- in accordance with RM 4.9(38).
15135
15136 Bound := Type_Low_Bound (T);
15137
15138 if Nkind (Bound) = N_Real_Literal then
fbf5a39b
AC
15139 Set_Realval
15140 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
996ae0b0
RK
15141 Set_Is_Machine_Number (Bound);
15142 end if;
15143
15144 Bound := Type_High_Bound (T);
15145
15146 if Nkind (Bound) = N_Real_Literal then
fbf5a39b
AC
15147 Set_Realval
15148 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
996ae0b0
RK
15149 Set_Is_Machine_Number (Bound);
15150 end if;
15151
15152 else
15153 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
15154 end if;
15155
15156 -- Complete definition of implicit base and declared first subtype
15157
15158 Set_Etype (Implicit_Base, Base_Typ);
15159
15160 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
15161 Set_Size_Info (Implicit_Base, (Base_Typ));
15162 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
15163 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
15164 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
23c799b1 15165 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
996ae0b0
RK
15166
15167 Set_Ekind (T, E_Floating_Point_Subtype);
15168 Set_Etype (T, Implicit_Base);
15169
15170 Set_Size_Info (T, (Implicit_Base));
15171 Set_RM_Size (T, RM_Size (Implicit_Base));
15172 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15173 Set_Digits_Value (T, Digs_Val);
996ae0b0
RK
15174 end Floating_Point_Type_Declaration;
15175
15176 ----------------------------
15177 -- Get_Discriminant_Value --
15178 ----------------------------
15179
ffe9aba8 15180 -- This is the situation:
996ae0b0
RK
15181
15182 -- There is a non-derived type
15183
15184 -- type T0 (Dx, Dy, Dz...)
15185
a5b62485
AC
15186 -- There are zero or more levels of derivation, with each derivation
15187 -- either purely inheriting the discriminants, or defining its own.
996ae0b0
RK
15188
15189 -- type Ti is new Ti-1
15190 -- or
15191 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
15192 -- or
15193 -- subtype Ti is ...
15194
a5b62485
AC
15195 -- The subtype issue is avoided by the use of Original_Record_Component,
15196 -- and the fact that derived subtypes also derive the constraints.
996ae0b0
RK
15197
15198 -- This chain leads back from
15199
15200 -- Typ_For_Constraint
15201
15202 -- Typ_For_Constraint has discriminants, and the value for each
15203 -- discriminant is given by its corresponding Elmt of Constraints.
15204
71d9e9f2 15205 -- Discriminant is some discriminant in this hierarchy
996ae0b0 15206
71d9e9f2 15207 -- We need to return its value
996ae0b0
RK
15208
15209 -- We do this by recursively searching each level, and looking for
15210 -- Discriminant. Once we get to the bottom, we start backing up
15211 -- returning the value for it which may in turn be a discriminant
15212 -- further up, so on the backup we continue the substitution.
15213
15214 function Get_Discriminant_Value
15215 (Discriminant : Entity_Id;
15216 Typ_For_Constraint : Entity_Id;
b0f26df5 15217 Constraint : Elist_Id) return Node_Id
996ae0b0 15218 is
fbf5a39b 15219 function Search_Derivation_Levels
996ae0b0
RK
15220 (Ti : Entity_Id;
15221 Discrim_Values : Elist_Id;
b0f26df5 15222 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
996ae0b0
RK
15223 -- This is the routine that performs the recursive search of levels
15224 -- as described above.
15225
fbf5a39b
AC
15226 ------------------------------
15227 -- Search_Derivation_Levels --
15228 ------------------------------
15229
15230 function Search_Derivation_Levels
996ae0b0
RK
15231 (Ti : Entity_Id;
15232 Discrim_Values : Elist_Id;
b0f26df5 15233 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
996ae0b0
RK
15234 is
15235 Assoc : Elmt_Id;
15236 Disc : Entity_Id;
15237 Result : Node_Or_Entity_Id;
15238 Result_Entity : Node_Id;
15239
15240 begin
15241 -- If inappropriate type, return Error, this happens only in
15242 -- cascaded error situations, and we want to avoid a blow up.
15243
15244 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
15245 return Error;
15246 end if;
15247
fbf5a39b 15248 -- Look deeper if possible. Use Stored_Constraints only for
996ae0b0
RK
15249 -- untagged types. For tagged types use the given constraint.
15250 -- This asymmetry needs explanation???
15251
fbf5a39b
AC
15252 if not Stored_Discrim_Values
15253 and then Present (Stored_Constraint (Ti))
996ae0b0
RK
15254 and then not Is_Tagged_Type (Ti)
15255 then
fbf5a39b
AC
15256 Result :=
15257 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
996ae0b0
RK
15258 else
15259 declare
fbf5a39b 15260 Td : constant Entity_Id := Etype (Ti);
996ae0b0 15261
fbf5a39b 15262 begin
996ae0b0
RK
15263 if Td = Ti then
15264 Result := Discriminant;
15265
15266 else
fbf5a39b 15267 if Present (Stored_Constraint (Ti)) then
996ae0b0 15268 Result :=
fbf5a39b
AC
15269 Search_Derivation_Levels
15270 (Td, Stored_Constraint (Ti), True);
996ae0b0
RK
15271 else
15272 Result :=
fbf5a39b
AC
15273 Search_Derivation_Levels
15274 (Td, Discrim_Values, Stored_Discrim_Values);
996ae0b0
RK
15275 end if;
15276 end if;
15277 end;
15278 end if;
15279
15280 -- Extra underlying places to search, if not found above. For
15281 -- concurrent types, the relevant discriminant appears in the
15282 -- corresponding record. For a type derived from a private type
15283 -- without discriminant, the full view inherits the discriminants
15284 -- of the full view of the parent.
15285
15286 if Result = Discriminant then
15287 if Is_Concurrent_Type (Ti)
15288 and then Present (Corresponding_Record_Type (Ti))
15289 then
15290 Result :=
fbf5a39b 15291 Search_Derivation_Levels (
996ae0b0
RK
15292 Corresponding_Record_Type (Ti),
15293 Discrim_Values,
fbf5a39b 15294 Stored_Discrim_Values);
996ae0b0
RK
15295
15296 elsif Is_Private_Type (Ti)
15297 and then not Has_Discriminants (Ti)
15298 and then Present (Full_View (Ti))
15299 and then Etype (Full_View (Ti)) /= Ti
15300 then
15301 Result :=
fbf5a39b 15302 Search_Derivation_Levels (
996ae0b0
RK
15303 Full_View (Ti),
15304 Discrim_Values,
fbf5a39b 15305 Stored_Discrim_Values);
996ae0b0
RK
15306 end if;
15307 end if;
15308
71d9e9f2
ES
15309 -- If Result is not a (reference to a) discriminant, return it,
15310 -- otherwise set Result_Entity to the discriminant.
996ae0b0
RK
15311
15312 if Nkind (Result) = N_Defining_Identifier then
996ae0b0 15313 pragma Assert (Result = Discriminant);
996ae0b0
RK
15314 Result_Entity := Result;
15315
15316 else
15317 if not Denotes_Discriminant (Result) then
15318 return Result;
15319 end if;
15320
15321 Result_Entity := Entity (Result);
15322 end if;
15323
15324 -- See if this level of derivation actually has discriminants
15325 -- because tagged derivations can add them, hence the lower
15326 -- levels need not have any.
15327
15328 if not Has_Discriminants (Ti) then
15329 return Result;
15330 end if;
15331
15332 -- Scan Ti's discriminants for Result_Entity,
15333 -- and return its corresponding value, if any.
15334
15335 Result_Entity := Original_Record_Component (Result_Entity);
15336
15337 Assoc := First_Elmt (Discrim_Values);
15338
fbf5a39b
AC
15339 if Stored_Discrim_Values then
15340 Disc := First_Stored_Discriminant (Ti);
996ae0b0
RK
15341 else
15342 Disc := First_Discriminant (Ti);
15343 end if;
15344
15345 while Present (Disc) loop
996ae0b0
RK
15346 pragma Assert (Present (Assoc));
15347
15348 if Original_Record_Component (Disc) = Result_Entity then
15349 return Node (Assoc);
15350 end if;
15351
15352 Next_Elmt (Assoc);
15353
fbf5a39b
AC
15354 if Stored_Discrim_Values then
15355 Next_Stored_Discriminant (Disc);
996ae0b0
RK
15356 else
15357 Next_Discriminant (Disc);
15358 end if;
15359 end loop;
15360
15361 -- Could not find it
15362 --
15363 return Result;
fbf5a39b 15364 end Search_Derivation_Levels;
996ae0b0 15365
ce4a6e84
RD
15366 -- Local Variables
15367
996ae0b0
RK
15368 Result : Node_Or_Entity_Id;
15369
15370 -- Start of processing for Get_Discriminant_Value
15371
15372 begin
71d9e9f2
ES
15373 -- ??? This routine is a gigantic mess and will be deleted. For the
15374 -- time being just test for the trivial case before calling recurse.
996ae0b0
RK
15375
15376 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
15377 declare
9dfd2ff8
CC
15378 D : Entity_Id;
15379 E : Elmt_Id;
71d9e9f2 15380
996ae0b0 15381 begin
9dfd2ff8
CC
15382 D := First_Discriminant (Typ_For_Constraint);
15383 E := First_Elmt (Constraint);
996ae0b0
RK
15384 while Present (D) loop
15385 if Chars (D) = Chars (Discriminant) then
15386 return Node (E);
15387 end if;
15388
15389 Next_Discriminant (D);
15390 Next_Elmt (E);
15391 end loop;
15392 end;
15393 end if;
15394
fbf5a39b
AC
15395 Result := Search_Derivation_Levels
15396 (Typ_For_Constraint, Constraint, False);
996ae0b0
RK
15397
15398 -- ??? hack to disappear when this routine is gone
15399
15400 if Nkind (Result) = N_Defining_Identifier then
15401 declare
9dfd2ff8
CC
15402 D : Entity_Id;
15403 E : Elmt_Id;
fbf5a39b 15404
996ae0b0 15405 begin
9dfd2ff8
CC
15406 D := First_Discriminant (Typ_For_Constraint);
15407 E := First_Elmt (Constraint);
996ae0b0
RK
15408 while Present (D) loop
15409 if Corresponding_Discriminant (D) = Discriminant then
15410 return Node (E);
15411 end if;
15412
15413 Next_Discriminant (D);
15414 Next_Elmt (E);
15415 end loop;
15416 end;
15417 end if;
15418
15419 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
15420 return Result;
15421 end Get_Discriminant_Value;
15422
15423 --------------------------
15424 -- Has_Range_Constraint --
15425 --------------------------
15426
15427 function Has_Range_Constraint (N : Node_Id) return Boolean is
15428 C : constant Node_Id := Constraint (N);
15429
15430 begin
15431 if Nkind (C) = N_Range_Constraint then
15432 return True;
15433
15434 elsif Nkind (C) = N_Digits_Constraint then
15435 return
15436 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
15437 or else
15438 Present (Range_Constraint (C));
15439
15440 elsif Nkind (C) = N_Delta_Constraint then
15441 return Present (Range_Constraint (C));
15442
15443 else
15444 return False;
15445 end if;
15446 end Has_Range_Constraint;
15447
15448 ------------------------
15449 -- Inherit_Components --
15450 ------------------------
15451
15452 function Inherit_Components
15453 (N : Node_Id;
15454 Parent_Base : Entity_Id;
15455 Derived_Base : Entity_Id;
15456 Is_Tagged : Boolean;
15457 Inherit_Discr : Boolean;
b0f26df5 15458 Discs : Elist_Id) return Elist_Id
996ae0b0 15459 is
fbf5a39b 15460 Assoc_List : constant Elist_Id := New_Elmt_List;
996ae0b0
RK
15461
15462 procedure Inherit_Component
15463 (Old_C : Entity_Id;
15464 Plain_Discrim : Boolean := False;
fbf5a39b 15465 Stored_Discrim : Boolean := False);
a5b62485
AC
15466 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
15467 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
15468 -- True, Old_C is a stored discriminant. If they are both false then
15469 -- Old_C is a regular component.
996ae0b0
RK
15470
15471 -----------------------
15472 -- Inherit_Component --
15473 -----------------------
15474
15475 procedure Inherit_Component
15476 (Old_C : Entity_Id;
15477 Plain_Discrim : Boolean := False;
fbf5a39b 15478 Stored_Discrim : Boolean := False)
996ae0b0 15479 is
fbf5a39b 15480 New_C : constant Entity_Id := New_Copy (Old_C);
996ae0b0
RK
15481
15482 Discrim : Entity_Id;
15483 Corr_Discrim : Entity_Id;
15484
15485 begin
fbf5a39b 15486 pragma Assert (not Is_Tagged or else not Stored_Discrim);
996ae0b0
RK
15487
15488 Set_Parent (New_C, Parent (Old_C));
15489
88b32fc3
BD
15490 -- Regular discriminants and components must be inserted in the scope
15491 -- of the Derived_Base. Do it here.
996ae0b0 15492
fbf5a39b 15493 if not Stored_Discrim then
996ae0b0
RK
15494 Enter_Name (New_C);
15495 end if;
15496
15497 -- For tagged types the Original_Record_Component must point to
15498 -- whatever this field was pointing to in the parent type. This has
15499 -- already been achieved by the call to New_Copy above.
15500
15501 if not Is_Tagged then
15502 Set_Original_Record_Component (New_C, New_C);
15503 end if;
15504
15505 -- If we have inherited a component then see if its Etype contains
15506 -- references to Parent_Base discriminants. In this case, replace
15507 -- these references with the constraints given in Discs. We do not
15508 -- do this for the partial view of private types because this is
15509 -- not needed (only the components of the full view will be used
15510 -- for code generation) and cause problem. We also avoid this
15511 -- transformation in some error situations.
15512
15513 if Ekind (New_C) = E_Component then
15514 if (Is_Private_Type (Derived_Base)
88b32fc3 15515 and then not Is_Generic_Type (Derived_Base))
996ae0b0 15516 or else (Is_Empty_Elmt_List (Discs)
88b32fc3 15517 and then not Expander_Active)
996ae0b0
RK
15518 then
15519 Set_Etype (New_C, Etype (Old_C));
88b32fc3 15520
996ae0b0 15521 else
88b32fc3
BD
15522 -- The current component introduces a circularity of the
15523 -- following kind:
15524
15525 -- limited with Pack_2;
15526 -- package Pack_1 is
15527 -- type T_1 is tagged record
15528 -- Comp : access Pack_2.T_2;
15529 -- ...
15530 -- end record;
15531 -- end Pack_1;
15532
15533 -- with Pack_1;
15534 -- package Pack_2 is
15535 -- type T_2 is new Pack_1.T_1 with ...;
15536 -- end Pack_2;
15537
2b73cf68
JM
15538 Set_Etype
15539 (New_C,
15540 Constrain_Component_Type
15541 (Old_C, Derived_Base, N, Parent_Base, Discs));
996ae0b0
RK
15542 end if;
15543 end if;
15544
15545 -- In derived tagged types it is illegal to reference a non
15546 -- discriminant component in the parent type. To catch this, mark
15547 -- these components with an Ekind of E_Void. This will be reset in
15548 -- Record_Type_Definition after processing the record extension of
15549 -- the derived type.
15550
2b73cf68
JM
15551 -- If the declaration is a private extension, there is no further
15552 -- record extension to process, and the components retain their
15553 -- current kind, because they are visible at this point.
15554
15555 if Is_Tagged and then Ekind (New_C) = E_Component
15556 and then Nkind (N) /= N_Private_Extension_Declaration
15557 then
996ae0b0
RK
15558 Set_Ekind (New_C, E_Void);
15559 end if;
15560
15561 if Plain_Discrim then
15562 Set_Corresponding_Discriminant (New_C, Old_C);
15563 Build_Discriminal (New_C);
15564
fbf5a39b 15565 -- If we are explicitly inheriting a stored discriminant it will be
996ae0b0
RK
15566 -- completely hidden.
15567
fbf5a39b 15568 elsif Stored_Discrim then
996ae0b0
RK
15569 Set_Corresponding_Discriminant (New_C, Empty);
15570 Set_Discriminal (New_C, Empty);
15571 Set_Is_Completely_Hidden (New_C);
15572
15573 -- Set the Original_Record_Component of each discriminant in the
fbf5a39b 15574 -- derived base to point to the corresponding stored that we just
996ae0b0
RK
15575 -- created.
15576
15577 Discrim := First_Discriminant (Derived_Base);
15578 while Present (Discrim) loop
15579 Corr_Discrim := Corresponding_Discriminant (Discrim);
15580
9dfd2ff8 15581 -- Corr_Discrim could be missing in an error situation
996ae0b0
RK
15582
15583 if Present (Corr_Discrim)
15584 and then Original_Record_Component (Corr_Discrim) = Old_C
15585 then
15586 Set_Original_Record_Component (Discrim, New_C);
15587 end if;
15588
15589 Next_Discriminant (Discrim);
15590 end loop;
15591
15592 Append_Entity (New_C, Derived_Base);
15593 end if;
15594
15595 if not Is_Tagged then
15596 Append_Elmt (Old_C, Assoc_List);
15597 Append_Elmt (New_C, Assoc_List);
15598 end if;
15599 end Inherit_Component;
15600
71d9e9f2 15601 -- Variables local to Inherit_Component
996ae0b0
RK
15602
15603 Loc : constant Source_Ptr := Sloc (N);
15604
15605 Parent_Discrim : Entity_Id;
fbf5a39b 15606 Stored_Discrim : Entity_Id;
996ae0b0 15607 D : Entity_Id;
71d9e9f2 15608 Component : Entity_Id;
996ae0b0
RK
15609
15610 -- Start of processing for Inherit_Components
15611
15612 begin
15613 if not Is_Tagged then
15614 Append_Elmt (Parent_Base, Assoc_List);
15615 Append_Elmt (Derived_Base, Assoc_List);
15616 end if;
15617
ffe9aba8 15618 -- Inherit parent discriminants if needed
996ae0b0
RK
15619
15620 if Inherit_Discr then
15621 Parent_Discrim := First_Discriminant (Parent_Base);
15622 while Present (Parent_Discrim) loop
15623 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
15624 Next_Discriminant (Parent_Discrim);
15625 end loop;
15626 end if;
15627
ffe9aba8 15628 -- Create explicit stored discrims for untagged types when necessary
996ae0b0
RK
15629
15630 if not Has_Unknown_Discriminants (Derived_Base)
15631 and then Has_Discriminants (Parent_Base)
15632 and then not Is_Tagged
15633 and then
15634 (not Inherit_Discr
71d9e9f2
ES
15635 or else First_Discriminant (Parent_Base) /=
15636 First_Stored_Discriminant (Parent_Base))
996ae0b0 15637 then
fbf5a39b
AC
15638 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
15639 while Present (Stored_Discrim) loop
15640 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
15641 Next_Stored_Discriminant (Stored_Discrim);
996ae0b0
RK
15642 end loop;
15643 end if;
15644
15645 -- See if we can apply the second transformation for derived types, as
15646 -- explained in point 6. in the comments above Build_Derived_Record_Type
a5b62485
AC
15647 -- This is achieved by appending Derived_Base discriminants into Discs,
15648 -- which has the side effect of returning a non empty Discs list to the
15649 -- caller of Inherit_Components, which is what we want. This must be
15650 -- done for private derived types if there are explicit stored
15651 -- discriminants, to ensure that we can retrieve the values of the
15652 -- constraints provided in the ancestors.
996ae0b0
RK
15653
15654 if Inherit_Discr
15655 and then Is_Empty_Elmt_List (Discs)
30c20106
AC
15656 and then Present (First_Discriminant (Derived_Base))
15657 and then
15658 (not Is_Private_Type (Derived_Base)
71d9e9f2
ES
15659 or else Is_Completely_Hidden
15660 (First_Stored_Discriminant (Derived_Base))
15661 or else Is_Generic_Type (Derived_Base))
996ae0b0
RK
15662 then
15663 D := First_Discriminant (Derived_Base);
15664 while Present (D) loop
15665 Append_Elmt (New_Reference_To (D, Loc), Discs);
15666 Next_Discriminant (D);
15667 end loop;
15668 end if;
15669
15670 -- Finally, inherit non-discriminant components unless they are not
15671 -- visible because defined or inherited from the full view of the
15672 -- parent. Don't inherit the _parent field of the parent type.
15673
15674 Component := First_Entity (Parent_Base);
15675 while Present (Component) loop
758c442c 15676
2b73cf68
JM
15677 -- Ada 2005 (AI-251): Do not inherit components associated with
15678 -- secondary tags of the parent.
758c442c
GD
15679
15680 if Ekind (Component) = E_Component
7d7af38a 15681 and then Present (Related_Type (Component))
758c442c
GD
15682 then
15683 null;
15684
15685 elsif Ekind (Component) /= E_Component
996ae0b0
RK
15686 or else Chars (Component) = Name_uParent
15687 then
15688 null;
15689
15690 -- If the derived type is within the parent type's declarative
15691 -- region, then the components can still be inherited even though
15692 -- they aren't visible at this point. This can occur for cases
15693 -- such as within public child units where the components must
15694 -- become visible upon entering the child unit's private part.
15695
15696 elsif not Is_Visible_Component (Component)
15697 and then not In_Open_Scopes (Scope (Parent_Base))
15698 then
15699 null;
15700
bce79204
AC
15701 elsif Ekind_In (Derived_Base, E_Private_Type,
15702 E_Limited_Private_Type)
996ae0b0
RK
15703 then
15704 null;
15705
15706 else
15707 Inherit_Component (Component);
15708 end if;
15709
15710 Next_Entity (Component);
15711 end loop;
15712
15713 -- For tagged derived types, inherited discriminants cannot be used in
15714 -- component declarations of the record extension part. To achieve this
15715 -- we mark the inherited discriminants as not visible.
15716
15717 if Is_Tagged and then Inherit_Discr then
15718 D := First_Discriminant (Derived_Base);
15719 while Present (D) loop
15720 Set_Is_Immediately_Visible (D, False);
15721 Next_Discriminant (D);
15722 end loop;
15723 end if;
15724
15725 return Assoc_List;
15726 end Inherit_Components;
15727
4230bdb7
AC
15728 -----------------------
15729 -- Is_Constant_Bound --
15730 -----------------------
15731
15732 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
15733 begin
15734 if Compile_Time_Known_Value (Exp) then
15735 return True;
15736
15737 elsif Is_Entity_Name (Exp)
15738 and then Present (Entity (Exp))
15739 then
15740 return Is_Constant_Object (Entity (Exp))
15741 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
15742
15743 elsif Nkind (Exp) in N_Binary_Op then
15744 return Is_Constant_Bound (Left_Opnd (Exp))
15745 and then Is_Constant_Bound (Right_Opnd (Exp))
15746 and then Scope (Entity (Exp)) = Standard_Standard;
15747
15748 else
15749 return False;
15750 end if;
15751 end Is_Constant_Bound;
15752
57193e09
TQ
15753 -----------------------
15754 -- Is_Null_Extension --
15755 -----------------------
15756
15757 function Is_Null_Extension (T : Entity_Id) return Boolean is
1646c947 15758 Type_Decl : constant Node_Id := Parent (Base_Type (T));
2b73cf68
JM
15759 Comp_List : Node_Id;
15760 Comp : Node_Id;
57193e09
TQ
15761
15762 begin
fea9e956
ES
15763 if Nkind (Type_Decl) /= N_Full_Type_Declaration
15764 or else not Is_Tagged_Type (T)
15765 or else Nkind (Type_Definition (Type_Decl)) /=
15766 N_Derived_Type_Definition
15767 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
57193e09
TQ
15768 then
15769 return False;
15770 end if;
15771
fea9e956
ES
15772 Comp_List :=
15773 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
57193e09 15774
fea9e956 15775 if Present (Discriminant_Specifications (Type_Decl)) then
57193e09
TQ
15776 return False;
15777
15778 elsif Present (Comp_List)
15779 and then Is_Non_Empty_List (Component_Items (Comp_List))
15780 then
2b73cf68
JM
15781 Comp := First (Component_Items (Comp_List));
15782
15783 -- Only user-defined components are relevant. The component list
15784 -- may also contain a parent component and internal components
15785 -- corresponding to secondary tags, but these do not determine
15786 -- whether this is a null extension.
15787
15788 while Present (Comp) loop
15789 if Comes_From_Source (Comp) then
15790 return False;
15791 end if;
57193e09 15792
2b73cf68
JM
15793 Next (Comp);
15794 end loop;
57193e09 15795
2b73cf68 15796 return True;
57193e09
TQ
15797 else
15798 return True;
15799 end if;
15800 end Is_Null_Extension;
15801
996ae0b0
RK
15802 ------------------------------
15803 -- Is_Valid_Constraint_Kind --
15804 ------------------------------
15805
15806 function Is_Valid_Constraint_Kind
15807 (T_Kind : Type_Kind;
b0f26df5 15808 Constraint_Kind : Node_Kind) return Boolean
996ae0b0
RK
15809 is
15810 begin
15811 case T_Kind is
996ae0b0
RK
15812 when Enumeration_Kind |
15813 Integer_Kind =>
15814 return Constraint_Kind = N_Range_Constraint;
15815
15816 when Decimal_Fixed_Point_Kind =>
7d7af38a
JM
15817 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
15818 N_Range_Constraint);
996ae0b0
RK
15819
15820 when Ordinary_Fixed_Point_Kind =>
7d7af38a
JM
15821 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
15822 N_Range_Constraint);
996ae0b0
RK
15823
15824 when Float_Kind =>
7d7af38a
JM
15825 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
15826 N_Range_Constraint);
996ae0b0
RK
15827
15828 when Access_Kind |
15829 Array_Kind |
15830 E_Record_Type |
15831 E_Record_Subtype |
15832 Class_Wide_Kind |
15833 E_Incomplete_Type |
15834 Private_Kind |
15835 Concurrent_Kind =>
15836 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
15837
15838 when others =>
71d9e9f2 15839 return True; -- Error will be detected later
996ae0b0 15840 end case;
996ae0b0
RK
15841 end Is_Valid_Constraint_Kind;
15842
15843 --------------------------
15844 -- Is_Visible_Component --
15845 --------------------------
15846
15847 function Is_Visible_Component (C : Entity_Id) return Boolean is
fbf5a39b 15848 Original_Comp : Entity_Id := Empty;
996ae0b0 15849 Original_Scope : Entity_Id;
fbf5a39b
AC
15850 Type_Scope : Entity_Id;
15851
15852 function Is_Local_Type (Typ : Entity_Id) return Boolean;
a5b62485
AC
15853 -- Check whether parent type of inherited component is declared locally,
15854 -- possibly within a nested package or instance. The current scope is
15855 -- the derived record itself.
fbf5a39b
AC
15856
15857 -------------------
15858 -- Is_Local_Type --
15859 -------------------
15860
15861 function Is_Local_Type (Typ : Entity_Id) return Boolean is
9dfd2ff8 15862 Scop : Entity_Id;
fbf5a39b
AC
15863
15864 begin
9dfd2ff8 15865 Scop := Scope (Typ);
fbf5a39b
AC
15866 while Present (Scop)
15867 and then Scop /= Standard_Standard
15868 loop
15869 if Scop = Scope (Current_Scope) then
15870 return True;
15871 end if;
15872
15873 Scop := Scope (Scop);
15874 end loop;
71d9e9f2 15875
fbf5a39b
AC
15876 return False;
15877 end Is_Local_Type;
15878
15879 -- Start of processing for Is_Visible_Component
996ae0b0
RK
15880
15881 begin
bce79204 15882 if Ekind_In (C, E_Component, E_Discriminant) then
fbf5a39b
AC
15883 Original_Comp := Original_Record_Component (C);
15884 end if;
15885
996ae0b0
RK
15886 if No (Original_Comp) then
15887
15888 -- Premature usage, or previous error
15889
15890 return False;
15891
15892 else
15893 Original_Scope := Scope (Original_Comp);
fbf5a39b 15894 Type_Scope := Scope (Base_Type (Scope (C)));
996ae0b0
RK
15895 end if;
15896
fbf5a39b 15897 -- This test only concerns tagged types
996ae0b0
RK
15898
15899 if not Is_Tagged_Type (Original_Scope) then
15900 return True;
15901
fbf5a39b 15902 -- If it is _Parent or _Tag, there is no visibility issue
996ae0b0
RK
15903
15904 elsif not Comes_From_Source (Original_Comp) then
15905 return True;
15906
a5b62485
AC
15907 -- If we are in the body of an instantiation, the component is visible
15908 -- even when the parent type (possibly defined in an enclosing unit or
15909 -- in a parent unit) might not.
996ae0b0
RK
15910
15911 elsif In_Instance_Body then
15912 return True;
15913
71d9e9f2 15914 -- Discriminants are always visible
996ae0b0
RK
15915
15916 elsif Ekind (Original_Comp) = E_Discriminant
15917 and then not Has_Unknown_Discriminants (Original_Scope)
15918 then
15919 return True;
15920
71d9e9f2
ES
15921 -- If the component has been declared in an ancestor which is currently
15922 -- a private type, then it is not visible. The same applies if the
15923 -- component's containing type is not in an open scope and the original
dc06abec 15924 -- component's enclosing type is a visible full view of a private type
71d9e9f2
ES
15925 -- (which can occur in cases where an attempt is being made to reference
15926 -- a component in a sibling package that is inherited from a visible
15927 -- component of a type in an ancestor package; the component in the
15928 -- sibling package should not be visible even though the component it
15929 -- inherited from is visible). This does not apply however in the case
15930 -- where the scope of the type is a private child unit, or when the
15931 -- parent comes from a local package in which the ancestor is currently
15932 -- visible. The latter suppression of visibility is needed for cases
15933 -- that are tested in B730006.
fbf5a39b
AC
15934
15935 elsif Is_Private_Type (Original_Scope)
15936 or else
15937 (not Is_Private_Descendant (Type_Scope)
15938 and then not In_Open_Scopes (Type_Scope)
15939 and then Has_Private_Declaration (Original_Scope))
996ae0b0 15940 then
fbf5a39b
AC
15941 -- If the type derives from an entity in a formal package, there
15942 -- are no additional visible components.
15943
15944 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
15945 N_Formal_Package_Declaration
15946 then
15947 return False;
15948
15949 -- if we are not in the private part of the current package, there
15950 -- are no additional visible components.
15951
15952 elsif Ekind (Scope (Current_Scope)) = E_Package
15953 and then not In_Private_Part (Scope (Current_Scope))
15954 then
15955 return False;
15956 else
15957 return
15958 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
dc06abec 15959 and then In_Open_Scopes (Scope (Original_Scope))
fbf5a39b
AC
15960 and then Is_Local_Type (Type_Scope);
15961 end if;
996ae0b0
RK
15962
15963 -- There is another weird way in which a component may be invisible
15964 -- when the private and the full view are not derived from the same
15965 -- ancestor. Here is an example :
15966
15967 -- type A1 is tagged record F1 : integer; end record;
15968 -- type A2 is new A1 with record F2 : integer; end record;
15969 -- type T is new A1 with private;
15970 -- private
fbf5a39b 15971 -- type T is new A2 with null record;
996ae0b0 15972
a5b62485
AC
15973 -- In this case, the full view of T inherits F1 and F2 but the private
15974 -- view inherits only F1
996ae0b0
RK
15975
15976 else
15977 declare
15978 Ancestor : Entity_Id := Scope (C);
15979
15980 begin
15981 loop
15982 if Ancestor = Original_Scope then
15983 return True;
15984 elsif Ancestor = Etype (Ancestor) then
15985 return False;
15986 end if;
15987
15988 Ancestor := Etype (Ancestor);
15989 end loop;
996ae0b0
RK
15990 end;
15991 end if;
15992 end Is_Visible_Component;
15993
15994 --------------------------
15995 -- Make_Class_Wide_Type --
15996 --------------------------
15997
15998 procedure Make_Class_Wide_Type (T : Entity_Id) is
15999 CW_Type : Entity_Id;
16000 CW_Name : Name_Id;
16001 Next_E : Entity_Id;
16002
16003 begin
88b32fc3
BD
16004 -- The class wide type can have been defined by the partial view, in
16005 -- which case everything is already done.
996ae0b0
RK
16006
16007 if Present (Class_Wide_Type (T)) then
16008 return;
16009 end if;
16010
16011 CW_Type :=
16012 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
16013
16014 -- Inherit root type characteristics
16015
16016 CW_Name := Chars (CW_Type);
16017 Next_E := Next_Entity (CW_Type);
16018 Copy_Node (T, CW_Type);
16019 Set_Comes_From_Source (CW_Type, False);
16020 Set_Chars (CW_Type, CW_Name);
16021 Set_Parent (CW_Type, Parent (T));
16022 Set_Next_Entity (CW_Type, Next_E);
88b32fc3
BD
16023
16024 -- Ensure we have a new freeze node for the class-wide type. The partial
16025 -- view may have freeze action of its own, requiring a proper freeze
16026 -- node, and the same freeze node cannot be shared between the two
16027 -- types.
16028
996ae0b0 16029 Set_Has_Delayed_Freeze (CW_Type);
88b32fc3 16030 Set_Freeze_Node (CW_Type, Empty);
996ae0b0
RK
16031
16032 -- Customize the class-wide type: It has no prim. op., it cannot be
07fc65c4 16033 -- abstract and its Etype points back to the specific root type.
996ae0b0 16034
ef2a63ba
JM
16035 Set_Ekind (CW_Type, E_Class_Wide_Type);
16036 Set_Is_Tagged_Type (CW_Type, True);
16037 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
16038 Set_Is_Abstract_Type (CW_Type, False);
16039 Set_Is_Constrained (CW_Type, False);
16040 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
996ae0b0 16041
07fc65c4
GB
16042 if Ekind (T) = E_Class_Wide_Subtype then
16043 Set_Etype (CW_Type, Etype (Base_Type (T)));
16044 else
16045 Set_Etype (CW_Type, T);
16046 end if;
16047
996ae0b0
RK
16048 -- If this is the class_wide type of a constrained subtype, it does
16049 -- not have discriminants.
16050
16051 Set_Has_Discriminants (CW_Type,
16052 Has_Discriminants (T) and then not Is_Constrained (T));
16053
16054 Set_Has_Unknown_Discriminants (CW_Type, True);
16055 Set_Class_Wide_Type (T, CW_Type);
16056 Set_Equivalent_Type (CW_Type, Empty);
16057
16058 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
16059
16060 Set_Class_Wide_Type (CW_Type, CW_Type);
996ae0b0
RK
16061 end Make_Class_Wide_Type;
16062
16063 ----------------
16064 -- Make_Index --
16065 ----------------
16066
16067 procedure Make_Index
16068 (I : Node_Id;
16069 Related_Nod : Node_Id;
16070 Related_Id : Entity_Id := Empty;
16071 Suffix_Index : Nat := 1)
16072 is
16073 R : Node_Id;
16074 T : Entity_Id;
16075 Def_Id : Entity_Id := Empty;
16076 Found : Boolean := False;
16077
16078 begin
16079 -- For a discrete range used in a constrained array definition and
16080 -- defined by a range, an implicit conversion to the predefined type
16081 -- INTEGER is assumed if each bound is either a numeric literal, a named
16082 -- number, or an attribute, and the type of both bounds (prior to the
16083 -- implicit conversion) is the type universal_integer. Otherwise, both
16084 -- bounds must be of the same discrete type, other than universal
16085 -- integer; this type must be determinable independently of the
16086 -- context, but using the fact that the type must be discrete and that
16087 -- both bounds must have the same type.
16088
16089 -- Character literals also have a universal type in the absence of
16090 -- of additional context, and are resolved to Standard_Character.
16091
16092 if Nkind (I) = N_Range then
16093
16094 -- The index is given by a range constraint. The bounds are known
16095 -- to be of a consistent type.
16096
16097 if not Is_Overloaded (I) then
16098 T := Etype (I);
16099
2b73cf68 16100 -- For universal bounds, choose the specific predefined type
996ae0b0
RK
16101
16102 if T = Universal_Integer then
16103 T := Standard_Integer;
16104
16105 elsif T = Any_Character then
2b73cf68 16106 Ambiguous_Character (Low_Bound (I));
996ae0b0
RK
16107
16108 T := Standard_Character;
16109 end if;
16110
df89ab66
ES
16111 -- The node may be overloaded because some user-defined operators
16112 -- are available, but if a universal interpretation exists it is
16113 -- also the selected one.
16114
16115 elsif Universal_Interpretation (I) = Universal_Integer then
16116 T := Standard_Integer;
16117
996ae0b0
RK
16118 else
16119 T := Any_Type;
16120
16121 declare
16122 Ind : Interp_Index;
16123 It : Interp;
16124
16125 begin
16126 Get_First_Interp (I, Ind, It);
996ae0b0
RK
16127 while Present (It.Typ) loop
16128 if Is_Discrete_Type (It.Typ) then
16129
16130 if Found
16131 and then not Covers (It.Typ, T)
16132 and then not Covers (T, It.Typ)
16133 then
16134 Error_Msg_N ("ambiguous bounds in discrete range", I);
16135 exit;
16136 else
16137 T := It.Typ;
16138 Found := True;
16139 end if;
16140 end if;
16141
16142 Get_Next_Interp (Ind, It);
16143 end loop;
16144
16145 if T = Any_Type then
16146 Error_Msg_N ("discrete type required for range", I);
16147 Set_Etype (I, Any_Type);
16148 return;
16149
16150 elsif T = Universal_Integer then
16151 T := Standard_Integer;
16152 end if;
16153 end;
16154 end if;
16155
16156 if not Is_Discrete_Type (T) then
16157 Error_Msg_N ("discrete type required for range", I);
16158 Set_Etype (I, Any_Type);
16159 return;
16160 end if;
16161
fbf5a39b
AC
16162 if Nkind (Low_Bound (I)) = N_Attribute_Reference
16163 and then Attribute_Name (Low_Bound (I)) = Name_First
16164 and then Is_Entity_Name (Prefix (Low_Bound (I)))
16165 and then Is_Type (Entity (Prefix (Low_Bound (I))))
16166 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
16167 then
a5b62485
AC
16168 -- The type of the index will be the type of the prefix, as long
16169 -- as the upper bound is 'Last of the same type.
fbf5a39b
AC
16170
16171 Def_Id := Entity (Prefix (Low_Bound (I)));
16172
16173 if Nkind (High_Bound (I)) /= N_Attribute_Reference
16174 or else Attribute_Name (High_Bound (I)) /= Name_Last
16175 or else not Is_Entity_Name (Prefix (High_Bound (I)))
16176 or else Entity (Prefix (High_Bound (I))) /= Def_Id
16177 then
16178 Def_Id := Empty;
16179 end if;
16180 end if;
16181
996ae0b0 16182 R := I;
07fc65c4 16183 Process_Range_Expr_In_Decl (R, T);
996ae0b0
RK
16184
16185 elsif Nkind (I) = N_Subtype_Indication then
16186
71d9e9f2 16187 -- The index is given by a subtype with a range constraint
996ae0b0
RK
16188
16189 T := Base_Type (Entity (Subtype_Mark (I)));
16190
16191 if not Is_Discrete_Type (T) then
16192 Error_Msg_N ("discrete type required for range", I);
16193 Set_Etype (I, Any_Type);
16194 return;
16195 end if;
16196
16197 R := Range_Expression (Constraint (I));
16198
16199 Resolve (R, T);
07fc65c4 16200 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
996ae0b0
RK
16201
16202 elsif Nkind (I) = N_Attribute_Reference then
16203
16204 -- The parser guarantees that the attribute is a RANGE attribute
16205
fbf5a39b
AC
16206 -- If the node denotes the range of a type mark, that is also the
16207 -- resulting type, and we do no need to create an Itype for it.
16208
16209 if Is_Entity_Name (Prefix (I))
16210 and then Comes_From_Source (I)
16211 and then Is_Type (Entity (Prefix (I)))
16212 and then Is_Discrete_Type (Entity (Prefix (I)))
16213 then
16214 Def_Id := Entity (Prefix (I));
16215 end if;
16216
d087cd96 16217 Analyze_And_Resolve (I);
996ae0b0 16218 T := Etype (I);
996ae0b0
RK
16219 R := I;
16220
16221 -- If none of the above, must be a subtype. We convert this to a
16222 -- range attribute reference because in the case of declared first
16223 -- named subtypes, the types in the range reference can be different
16224 -- from the type of the entity. A range attribute normalizes the
16225 -- reference and obtains the correct types for the bounds.
16226
16227 -- This transformation is in the nature of an expansion, is only
16228 -- done if expansion is active. In particular, it is not done on
16229 -- formal generic types, because we need to retain the name of the
16230 -- original index for instantiation purposes.
16231
16232 else
16233 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
16234 Error_Msg_N ("invalid subtype mark in discrete range ", I);
16235 Set_Etype (I, Any_Integer);
16236 return;
71d9e9f2 16237
996ae0b0
RK
16238 else
16239 -- The type mark may be that of an incomplete type. It is only
16240 -- now that we can get the full view, previous analysis does
16241 -- not look specifically for a type mark.
16242
16243 Set_Entity (I, Get_Full_View (Entity (I)));
16244 Set_Etype (I, Entity (I));
16245 Def_Id := Entity (I);
16246
16247 if not Is_Discrete_Type (Def_Id) then
16248 Error_Msg_N ("discrete type required for index", I);
16249 Set_Etype (I, Any_Type);
16250 return;
16251 end if;
16252 end if;
16253
16254 if Expander_Active then
16255 Rewrite (I,
16256 Make_Attribute_Reference (Sloc (I),
16257 Attribute_Name => Name_Range,
16258 Prefix => Relocate_Node (I)));
16259
16260 -- The original was a subtype mark that does not freeze. This
16261 -- means that the rewritten version must not freeze either.
16262
16263 Set_Must_Not_Freeze (I);
16264 Set_Must_Not_Freeze (Prefix (I));
16265
16266 -- Is order critical??? if so, document why, if not
16267 -- use Analyze_And_Resolve
16268
88b32fc3 16269 Analyze_And_Resolve (I);
996ae0b0 16270 T := Etype (I);
996ae0b0
RK
16271 R := I;
16272
fbf5a39b
AC
16273 -- If expander is inactive, type is legal, nothing else to construct
16274
996ae0b0 16275 else
996ae0b0
RK
16276 return;
16277 end if;
16278 end if;
16279
16280 if not Is_Discrete_Type (T) then
16281 Error_Msg_N ("discrete type required for range", I);
16282 Set_Etype (I, Any_Type);
16283 return;
16284
16285 elsif T = Any_Type then
16286 Set_Etype (I, Any_Type);
16287 return;
16288 end if;
16289
a5b62485
AC
16290 -- We will now create the appropriate Itype to describe the range, but
16291 -- first a check. If we originally had a subtype, then we just label
16292 -- the range with this subtype. Not only is there no need to construct
16293 -- a new subtype, but it is wrong to do so for two reasons:
996ae0b0 16294
a5b62485
AC
16295 -- 1. A legality concern, if we have a subtype, it must not freeze,
16296 -- and the Itype would cause freezing incorrectly
996ae0b0 16297
a5b62485
AC
16298 -- 2. An efficiency concern, if we created an Itype, it would not be
16299 -- recognized as the same type for the purposes of eliminating
16300 -- checks in some circumstances.
996ae0b0 16301
71d9e9f2 16302 -- We signal this case by setting the subtype entity in Def_Id
996ae0b0 16303
996ae0b0 16304 if No (Def_Id) then
996ae0b0
RK
16305 Def_Id :=
16306 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
16307 Set_Etype (Def_Id, Base_Type (T));
16308
16309 if Is_Signed_Integer_Type (T) then
16310 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
16311
16312 elsif Is_Modular_Integer_Type (T) then
16313 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
16314
16315 else
16316 Set_Ekind (Def_Id, E_Enumeration_Subtype);
16317 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
fbf5a39b 16318 Set_First_Literal (Def_Id, First_Literal (T));
996ae0b0
RK
16319 end if;
16320
16321 Set_Size_Info (Def_Id, (T));
16322 Set_RM_Size (Def_Id, RM_Size (T));
16323 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
16324
16325 Set_Scalar_Range (Def_Id, R);
16326 Conditional_Delay (Def_Id, T);
16327
16328 -- In the subtype indication case, if the immediate parent of the
16329 -- new subtype is non-static, then the subtype we create is non-
16330 -- static, even if its bounds are static.
16331
16332 if Nkind (I) = N_Subtype_Indication
16333 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
16334 then
16335 Set_Is_Non_Static_Subtype (Def_Id);
16336 end if;
16337 end if;
16338
16339 -- Final step is to label the index with this constructed type
16340
16341 Set_Etype (I, Def_Id);
16342 end Make_Index;
16343
16344 ------------------------------
16345 -- Modular_Type_Declaration --
16346 ------------------------------
16347
16348 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16349 Mod_Expr : constant Node_Id := Expression (Def);
16350 M_Val : Uint;
16351
16352 procedure Set_Modular_Size (Bits : Int);
16353 -- Sets RM_Size to Bits, and Esize to normal word size above this
16354
fbf5a39b
AC
16355 ----------------------
16356 -- Set_Modular_Size --
16357 ----------------------
16358
996ae0b0
RK
16359 procedure Set_Modular_Size (Bits : Int) is
16360 begin
16361 Set_RM_Size (T, UI_From_Int (Bits));
16362
16363 if Bits <= 8 then
16364 Init_Esize (T, 8);
16365
16366 elsif Bits <= 16 then
16367 Init_Esize (T, 16);
16368
16369 elsif Bits <= 32 then
16370 Init_Esize (T, 32);
16371
16372 else
16373 Init_Esize (T, System_Max_Binary_Modulus_Power);
16374 end if;
8dc2ddaf
RD
16375
16376 if not Non_Binary_Modulus (T)
16377 and then Esize (T) = RM_Size (T)
16378 then
16379 Set_Is_Known_Valid (T);
16380 end if;
996ae0b0
RK
16381 end Set_Modular_Size;
16382
16383 -- Start of processing for Modular_Type_Declaration
16384
16385 begin
16386 Analyze_And_Resolve (Mod_Expr, Any_Integer);
16387 Set_Etype (T, T);
16388 Set_Ekind (T, E_Modular_Integer_Type);
16389 Init_Alignment (T);
16390 Set_Is_Constrained (T);
16391
16392 if not Is_OK_Static_Expression (Mod_Expr) then
fbf5a39b
AC
16393 Flag_Non_Static_Expr
16394 ("non-static expression used for modular type bound!", Mod_Expr);
996ae0b0
RK
16395 M_Val := 2 ** System_Max_Binary_Modulus_Power;
16396 else
16397 M_Val := Expr_Value (Mod_Expr);
16398 end if;
16399
16400 if M_Val < 1 then
16401 Error_Msg_N ("modulus value must be positive", Mod_Expr);
16402 M_Val := 2 ** System_Max_Binary_Modulus_Power;
16403 end if;
16404
16405 Set_Modulus (T, M_Val);
16406
16407 -- Create bounds for the modular type based on the modulus given in
16408 -- the type declaration and then analyze and resolve those bounds.
16409
16410 Set_Scalar_Range (T,
16411 Make_Range (Sloc (Mod_Expr),
7675ad4f
AC
16412 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
16413 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
996ae0b0
RK
16414
16415 -- Properly analyze the literals for the range. We do this manually
16416 -- because we can't go calling Resolve, since we are resolving these
16417 -- bounds with the type, and this type is certainly not complete yet!
16418
16419 Set_Etype (Low_Bound (Scalar_Range (T)), T);
16420 Set_Etype (High_Bound (Scalar_Range (T)), T);
16421 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
16422 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
16423
16424 -- Loop through powers of two to find number of bits required
16425
16426 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
16427
16428 -- Binary case
16429
16430 if M_Val = 2 ** Bits then
16431 Set_Modular_Size (Bits);
16432 return;
16433
16434 -- Non-binary case
16435
16436 elsif M_Val < 2 ** Bits then
16437 Set_Non_Binary_Modulus (T);
16438
16439 if Bits > System_Max_Nonbinary_Modulus_Power then
16440 Error_Msg_Uint_1 :=
16441 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
2b73cf68 16442 Error_Msg_F
996ae0b0
RK
16443 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
16444 Set_Modular_Size (System_Max_Binary_Modulus_Power);
16445 return;
16446
16447 else
71d9e9f2 16448 -- In the non-binary case, set size as per RM 13.3(55)
996ae0b0
RK
16449
16450 Set_Modular_Size (Bits);
16451 return;
16452 end if;
16453 end if;
16454
16455 end loop;
16456
16457 -- If we fall through, then the size exceed System.Max_Binary_Modulus
16458 -- so we just signal an error and set the maximum size.
16459
16460 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
2b73cf68 16461 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
996ae0b0
RK
16462
16463 Set_Modular_Size (System_Max_Binary_Modulus_Power);
16464 Init_Alignment (T);
dc06abec 16465
996ae0b0
RK
16466 end Modular_Type_Declaration;
16467
6c1e24d3
AC
16468 --------------------------
16469 -- New_Concatenation_Op --
16470 --------------------------
996ae0b0 16471
6c1e24d3 16472 procedure New_Concatenation_Op (Typ : Entity_Id) is
996ae0b0
RK
16473 Loc : constant Source_Ptr := Sloc (Typ);
16474 Op : Entity_Id;
16475
16476 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
16477 -- Create abbreviated declaration for the formal of a predefined
16478 -- Operator 'Op' of type 'Typ'
16479
16480 --------------------
16481 -- Make_Op_Formal --
16482 --------------------
16483
16484 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
16485 Formal : Entity_Id;
996ae0b0
RK
16486 begin
16487 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
16488 Set_Etype (Formal, Typ);
16489 Set_Mechanism (Formal, Default_Mechanism);
16490 return Formal;
16491 end Make_Op_Formal;
16492
6c1e24d3 16493 -- Start of processing for New_Concatenation_Op
996ae0b0
RK
16494
16495 begin
6c1e24d3 16496 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
996ae0b0
RK
16497
16498 Set_Ekind (Op, E_Operator);
16499 Set_Scope (Op, Current_Scope);
16500 Set_Etype (Op, Typ);
6c1e24d3 16501 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
996ae0b0
RK
16502 Set_Is_Immediately_Visible (Op);
16503 Set_Is_Intrinsic_Subprogram (Op);
16504 Set_Has_Completion (Op);
16505 Append_Entity (Op, Current_Scope);
16506
6c1e24d3 16507 Set_Name_Entity_Id (Name_Op_Concat, Op);
996ae0b0
RK
16508
16509 Append_Entity (Make_Op_Formal (Typ, Op), Op);
16510 Append_Entity (Make_Op_Formal (Typ, Op), Op);
6c1e24d3 16511 end New_Concatenation_Op;
996ae0b0 16512
88b32fc3
BD
16513 -------------------------
16514 -- OK_For_Limited_Init --
16515 -------------------------
16516
16517 -- ???Check all calls of this, and compare the conditions under which it's
16518 -- called.
16519
2a31c32b
AC
16520 function OK_For_Limited_Init
16521 (Typ : Entity_Id;
16522 Exp : Node_Id) return Boolean
16523 is
88b32fc3 16524 begin
236fecbf 16525 return Is_CPP_Constructor_Call (Exp)
0791fbe9 16526 or else (Ada_Version >= Ada_2005
236fecbf 16527 and then not Debug_Flag_Dot_L
2a31c32b 16528 and then OK_For_Limited_Init_In_05 (Typ, Exp));
88b32fc3
BD
16529 end OK_For_Limited_Init;
16530
16531 -------------------------------
16532 -- OK_For_Limited_Init_In_05 --
16533 -------------------------------
16534
2a31c32b
AC
16535 function OK_For_Limited_Init_In_05
16536 (Typ : Entity_Id;
16537 Exp : Node_Id) return Boolean
16538 is
88b32fc3 16539 begin
2a31c32b
AC
16540 -- An object of a limited interface type can be initialized with any
16541 -- expression of a nonlimited descendant type.
16542
16543 if Is_Class_Wide_Type (Typ)
16544 and then Is_Limited_Interface (Typ)
16545 and then not Is_Limited_Type (Etype (Exp))
16546 then
16547 return True;
16548 end if;
16549
c6fe3827
GD
16550 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
16551 -- case of limited aggregates (including extension aggregates), and
4adf3c50 16552 -- function calls. The function call may have been given in prefixed
2b73cf68 16553 -- notation, in which case the original node is an indexed component.
4adf3c50
AC
16554 -- If the function is parameterless, the original node was an explicit
16555 -- dereference.
88b32fc3
BD
16556
16557 case Nkind (Original_Node (Exp)) is
2b73cf68 16558 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
88b32fc3
BD
16559 return True;
16560
71f62180
ES
16561 when N_Qualified_Expression =>
16562 return
2a31c32b
AC
16563 OK_For_Limited_Init_In_05
16564 (Typ, Expression (Original_Node (Exp)));
71f62180 16565
2b73cf68 16566 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
c6fe3827 16567 -- with a function call, the expander has rewritten the call into an
2b73cf68
JM
16568 -- N_Type_Conversion node to force displacement of the pointer to
16569 -- reference the component containing the secondary dispatch table.
71f62180 16570 -- Otherwise a type conversion is not a legal context.
e80d72ea
ES
16571 -- A return statement for a build-in-place function returning a
16572 -- synchronized type also introduces an unchecked conversion.
2b73cf68 16573
e606088a
AC
16574 when N_Type_Conversion |
16575 N_Unchecked_Type_Conversion =>
71f62180
ES
16576 return not Comes_From_Source (Exp)
16577 and then
2a31c32b
AC
16578 OK_For_Limited_Init_In_05
16579 (Typ, Expression (Original_Node (Exp)));
88b32fc3 16580
e606088a
AC
16581 when N_Indexed_Component |
16582 N_Selected_Component |
16583 N_Explicit_Dereference =>
2b73cf68
JM
16584 return Nkind (Exp) = N_Function_Call;
16585
c6fe3827
GD
16586 -- A use of 'Input is a function call, hence allowed. Normally the
16587 -- attribute will be changed to a call, but the attribute by itself
16588 -- can occur with -gnatc.
16589
16590 when N_Attribute_Reference =>
16591 return Attribute_Name (Original_Node (Exp)) = Name_Input;
16592
88b32fc3
BD
16593 when others =>
16594 return False;
16595 end case;
16596 end OK_For_Limited_Init_In_05;
16597
996ae0b0
RK
16598 -------------------------------------------
16599 -- Ordinary_Fixed_Point_Type_Declaration --
16600 -------------------------------------------
16601
16602 procedure Ordinary_Fixed_Point_Type_Declaration
16603 (T : Entity_Id;
16604 Def : Node_Id)
16605 is
16606 Loc : constant Source_Ptr := Sloc (Def);
16607 Delta_Expr : constant Node_Id := Delta_Expression (Def);
16608 RRS : constant Node_Id := Real_Range_Specification (Def);
16609 Implicit_Base : Entity_Id;
16610 Delta_Val : Ureal;
16611 Small_Val : Ureal;
16612 Low_Val : Ureal;
16613 High_Val : Ureal;
16614
16615 begin
16616 Check_Restriction (No_Fixed_Point, Def);
16617
16618 -- Create implicit base type
16619
16620 Implicit_Base :=
16621 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
16622 Set_Etype (Implicit_Base, Implicit_Base);
16623
16624 -- Analyze and process delta expression
16625
16626 Analyze_And_Resolve (Delta_Expr, Any_Real);
16627
16628 Check_Delta_Expression (Delta_Expr);
16629 Delta_Val := Expr_Value_R (Delta_Expr);
16630
16631 Set_Delta_Value (Implicit_Base, Delta_Val);
16632
a5b62485
AC
16633 -- Compute default small from given delta, which is the largest power
16634 -- of two that does not exceed the given delta value.
996ae0b0
RK
16635
16636 declare
9dfd2ff8
CC
16637 Tmp : Ureal;
16638 Scale : Int;
996ae0b0
RK
16639
16640 begin
9dfd2ff8
CC
16641 Tmp := Ureal_1;
16642 Scale := 0;
16643
996ae0b0
RK
16644 if Delta_Val < Ureal_1 then
16645 while Delta_Val < Tmp loop
16646 Tmp := Tmp / Ureal_2;
16647 Scale := Scale + 1;
16648 end loop;
16649
16650 else
16651 loop
16652 Tmp := Tmp * Ureal_2;
16653 exit when Tmp > Delta_Val;
16654 Scale := Scale - 1;
16655 end loop;
16656 end if;
16657
16658 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
16659 end;
16660
16661 Set_Small_Value (Implicit_Base, Small_Val);
16662
16663 -- If no range was given, set a dummy range
16664
16665 if RRS <= Empty_Or_Error then
16666 Low_Val := -Small_Val;
16667 High_Val := Small_Val;
16668
16669 -- Otherwise analyze and process given range
16670
16671 else
16672 declare
16673 Low : constant Node_Id := Low_Bound (RRS);
16674 High : constant Node_Id := High_Bound (RRS);
16675
16676 begin
16677 Analyze_And_Resolve (Low, Any_Real);
16678 Analyze_And_Resolve (High, Any_Real);
16679 Check_Real_Bound (Low);
16680 Check_Real_Bound (High);
16681
16682 -- Obtain and set the range
16683
16684 Low_Val := Expr_Value_R (Low);
16685 High_Val := Expr_Value_R (High);
16686
16687 if Low_Val > High_Val then
16688 Error_Msg_NE ("?fixed point type& has null range", Def, T);
16689 end if;
16690 end;
16691 end if;
16692
a5b62485
AC
16693 -- The range for both the implicit base and the declared first subtype
16694 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
16695 -- set a temporary range in place. Note that the bounds of the base
16696 -- type will be widened to be symmetrical and to fill the available
16697 -- bits when the type is frozen.
996ae0b0
RK
16698
16699 -- We could do this with all discrete types, and probably should, but
16700 -- we absolutely have to do it for fixed-point, since the end-points
16701 -- of the range and the size are determined by the small value, which
16702 -- could be reset before the freeze point.
16703
16704 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
16705 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
16706
996ae0b0
RK
16707 -- Complete definition of first subtype
16708
16709 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
16710 Set_Etype (T, Implicit_Base);
16711 Init_Size_Align (T);
16712 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
16713 Set_Small_Value (T, Small_Val);
16714 Set_Delta_Value (T, Delta_Val);
16715 Set_Is_Constrained (T);
16716
16717 end Ordinary_Fixed_Point_Type_Declaration;
16718
16719 ----------------------------------------
16720 -- Prepare_Private_Subtype_Completion --
16721 ----------------------------------------
16722
16723 procedure Prepare_Private_Subtype_Completion
16724 (Id : Entity_Id;
16725 Related_Nod : Node_Id)
16726 is
16727 Id_B : constant Entity_Id := Base_Type (Id);
16728 Full_B : constant Entity_Id := Full_View (Id_B);
16729 Full : Entity_Id;
16730
16731 begin
16732 if Present (Full_B) then
16733
a5b62485
AC
16734 -- The Base_Type is already completed, we can complete the subtype
16735 -- now. We have to create a new entity with the same name, Thus we
16736 -- can't use Create_Itype.
16737
996ae0b0
RK
16738 -- This is messy, should be fixed ???
16739
16740 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
16741 Set_Is_Itype (Full);
16742 Set_Associated_Node_For_Itype (Full, Related_Nod);
16743 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
16744 end if;
16745
16746 -- The parent subtype may be private, but the base might not, in some
16747 -- nested instances. In that case, the subtype does not need to be
16748 -- exchanged. It would still be nice to make private subtypes and their
16749 -- bases consistent at all times ???
16750
16751 if Is_Private_Type (Id_B) then
16752 Append_Elmt (Id, Private_Dependents (Id_B));
16753 end if;
16754
16755 end Prepare_Private_Subtype_Completion;
16756
16757 ---------------------------
16758 -- Process_Discriminants --
16759 ---------------------------
16760
fbf5a39b
AC
16761 procedure Process_Discriminants
16762 (N : Node_Id;
16763 Prev : Entity_Id := Empty)
16764 is
16765 Elist : constant Elist_Id := New_Elmt_List;
996ae0b0
RK
16766 Id : Node_Id;
16767 Discr : Node_Id;
16768 Discr_Number : Uint;
16769 Discr_Type : Entity_Id;
16770 Default_Present : Boolean := False;
16771 Default_Not_Present : Boolean := False;
996ae0b0
RK
16772
16773 begin
16774 -- A composite type other than an array type can have discriminants.
996ae0b0
RK
16775 -- On entry, the current scope is the composite type.
16776
16777 -- The discriminants are initially entered into the scope of the type
16778 -- via Enter_Name with the default Ekind of E_Void to prevent premature
16779 -- use, as explained at the end of this procedure.
16780
16781 Discr := First (Discriminant_Specifications (N));
16782 while Present (Discr) loop
16783 Enter_Name (Defining_Identifier (Discr));
16784
fbf5a39b
AC
16785 -- For navigation purposes we add a reference to the discriminant
16786 -- in the entity for the type. If the current declaration is a
16787 -- completion, place references on the partial view. Otherwise the
16788 -- type is the current scope.
16789
16790 if Present (Prev) then
16791
16792 -- The references go on the partial view, if present. If the
16793 -- partial view has discriminants, the references have been
16794 -- generated already.
16795
16796 if not Has_Discriminants (Prev) then
16797 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
16798 end if;
16799 else
16800 Generate_Reference
16801 (Current_Scope, Defining_Identifier (Discr), 'd');
16802 end if;
16803
996ae0b0 16804 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
57193e09 16805 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
996ae0b0 16806
0ab80019 16807 -- Ada 2005 (AI-254)
7324bf49
AC
16808
16809 if Present (Access_To_Subprogram_Definition
16810 (Discriminant_Type (Discr)))
16811 and then Protected_Present (Access_To_Subprogram_Definition
16812 (Discriminant_Type (Discr)))
16813 then
16814 Discr_Type :=
fea9e956 16815 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
7324bf49
AC
16816 end if;
16817
996ae0b0
RK
16818 else
16819 Find_Type (Discriminant_Type (Discr));
16820 Discr_Type := Etype (Discriminant_Type (Discr));
16821
16822 if Error_Posted (Discriminant_Type (Discr)) then
16823 Discr_Type := Any_Type;
16824 end if;
16825 end if;
16826
16827 if Is_Access_Type (Discr_Type) then
6e937c1c 16828
0ab80019 16829 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
6e937c1c
AC
16830 -- record types
16831
0791fbe9 16832 if Ada_Version < Ada_2005 then
6e937c1c
AC
16833 Check_Access_Discriminant_Requires_Limited
16834 (Discr, Discriminant_Type (Discr));
16835 end if;
996ae0b0 16836
0ab80019 16837 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
996ae0b0
RK
16838 Error_Msg_N
16839 ("(Ada 83) access discriminant not allowed", Discr);
16840 end if;
16841
16842 elsif not Is_Discrete_Type (Discr_Type) then
16843 Error_Msg_N ("discriminants must have a discrete or access type",
16844 Discriminant_Type (Discr));
16845 end if;
16846
16847 Set_Etype (Defining_Identifier (Discr), Discr_Type);
16848
16849 -- If a discriminant specification includes the assignment compound
16850 -- delimiter followed by an expression, the expression is the default
16851 -- expression of the discriminant; the default expression must be of
16852 -- the type of the discriminant. (RM 3.7.1) Since this expression is
16853 -- a default expression, we do the special preanalysis, since this
fbf5a39b
AC
16854 -- expression does not freeze (see "Handling of Default and Per-
16855 -- Object Expressions" in spec of package Sem).
996ae0b0
RK
16856
16857 if Present (Expression (Discr)) then
ce4a6e84 16858 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
996ae0b0
RK
16859
16860 if Nkind (N) = N_Formal_Type_Declaration then
16861 Error_Msg_N
16862 ("discriminant defaults not allowed for formal type",
16863 Expression (Discr));
16864
5e5db3b4
GD
16865 -- Flag an error for a tagged type with defaulted discriminants,
16866 -- excluding limited tagged types when compiling for Ada 2012
16867 -- (see AI05-0214).
16868
7324bf49 16869 elsif Is_Tagged_Type (Current_Scope)
5e5db3b4
GD
16870 and then (not Is_Limited_Type (Current_Scope)
16871 or else Ada_Version < Ada_2012)
027dbed8 16872 and then Comes_From_Source (N)
7324bf49 16873 then
027dbed8
AC
16874 -- Note: see similar test in Check_Or_Process_Discriminants, to
16875 -- handle the (illegal) case of the completion of an untagged
16876 -- view with discriminants with defaults by a tagged full view.
5e5db3b4 16877 -- We skip the check if Discr does not come from source, to
027dbed8 16878 -- account for the case of an untagged derived type providing
5e5db3b4 16879 -- defaults for a renamed discriminant from a private untagged
027dbed8 16880 -- ancestor with a tagged full view (ACATS B460006).
8e4dac80 16881
5e5db3b4
GD
16882 if Ada_Version >= Ada_2012 then
16883 Error_Msg_N
16884 ("discriminants of nonlimited tagged type cannot have"
16885 & " defaults",
16886 Expression (Discr));
16887 else
16888 Error_Msg_N
16889 ("discriminants of tagged type cannot have defaults",
16890 Expression (Discr));
16891 end if;
996ae0b0
RK
16892
16893 else
16894 Default_Present := True;
16895 Append_Elmt (Expression (Discr), Elist);
16896
16897 -- Tag the defining identifiers for the discriminants with
16898 -- their corresponding default expressions from the tree.
16899
16900 Set_Discriminant_Default_Value
16901 (Defining_Identifier (Discr), Expression (Discr));
16902 end if;
16903
16904 else
16905 Default_Not_Present := True;
16906 end if;
16907
9dfd2ff8
CC
16908 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
16909 -- Discr_Type but with the null-exclusion attribute
16910
0791fbe9 16911 if Ada_Version >= Ada_2005 then
9dfd2ff8
CC
16912
16913 -- Ada 2005 (AI-231): Static checks
16914
16915 if Can_Never_Be_Null (Discr_Type) then
16916 Null_Exclusion_Static_Checks (Discr);
16917
16918 elsif Is_Access_Type (Discr_Type)
16919 and then Null_Exclusion_Present (Discr)
16920
16921 -- No need to check itypes because in their case this check
16922 -- was done at their point of creation
16923
16924 and then not Is_Itype (Discr_Type)
16925 then
16926 if Can_Never_Be_Null (Discr_Type) then
2b73cf68
JM
16927 Error_Msg_NE
16928 ("`NOT NULL` not allowed (& already excludes null)",
16929 Discr,
16930 Discr_Type);
9dfd2ff8
CC
16931 end if;
16932
16933 Set_Etype (Defining_Identifier (Discr),
16934 Create_Null_Excluding_Itype
16935 (T => Discr_Type,
16936 Related_Nod => Discr));
fa961f76
ES
16937
16938 -- Check for improper null exclusion if the type is otherwise
16939 -- legal for a discriminant.
16940
16941 elsif Null_Exclusion_Present (Discr)
16942 and then Is_Discrete_Type (Discr_Type)
16943 then
16944 Error_Msg_N
16945 ("null exclusion can only apply to an access type", Discr);
9dfd2ff8 16946 end if;
2820d220 16947
88b32fc3 16948 -- Ada 2005 (AI-402): access discriminants of nonlimited types
ce4a6e84
RD
16949 -- can't have defaults. Synchronized types, or types that are
16950 -- explicitly limited are fine, but special tests apply to derived
16951 -- types in generics: in a generic body we have to assume the
16952 -- worst, and therefore defaults are not allowed if the parent is
16953 -- a generic formal private type (see ACATS B370001).
88b32fc3
BD
16954
16955 if Is_Access_Type (Discr_Type) then
16956 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
16957 or else not Default_Present
16958 or else Is_Limited_Record (Current_Scope)
16959 or else Is_Concurrent_Type (Current_Scope)
16960 or else Is_Concurrent_Record_Type (Current_Scope)
16961 or else Ekind (Current_Scope) = E_Limited_Private_Type
16962 then
ce4a6e84
RD
16963 if not Is_Derived_Type (Current_Scope)
16964 or else not Is_Generic_Type (Etype (Current_Scope))
16965 or else not In_Package_Body (Scope (Etype (Current_Scope)))
16966 or else Limited_Present
16967 (Type_Definition (Parent (Current_Scope)))
16968 then
16969 null;
16970
16971 else
16972 Error_Msg_N ("access discriminants of nonlimited types",
16973 Expression (Discr));
16974 Error_Msg_N ("\cannot have defaults", Expression (Discr));
16975 end if;
dc06abec
RD
16976
16977 elsif Present (Expression (Discr)) then
88b32fc3
BD
16978 Error_Msg_N
16979 ("(Ada 2005) access discriminants of nonlimited types",
16980 Expression (Discr));
16981 Error_Msg_N ("\cannot have defaults", Expression (Discr));
16982 end if;
16983 end if;
2820d220
AC
16984 end if;
16985
996ae0b0
RK
16986 Next (Discr);
16987 end loop;
16988
16989 -- An element list consisting of the default expressions of the
16990 -- discriminants is constructed in the above loop and used to set
16991 -- the Discriminant_Constraint attribute for the type. If an object
16992 -- is declared of this (record or task) type without any explicit
16993 -- discriminant constraint given, this element list will form the
16994 -- actual parameters for the corresponding initialization procedure
16995 -- for the type.
16996
16997 Set_Discriminant_Constraint (Current_Scope, Elist);
fbf5a39b 16998 Set_Stored_Constraint (Current_Scope, No_Elist);
996ae0b0
RK
16999
17000 -- Default expressions must be provided either for all or for none
17001 -- of the discriminants of a discriminant part. (RM 3.7.1)
17002
17003 if Default_Present and then Default_Not_Present then
17004 Error_Msg_N
17005 ("incomplete specification of defaults for discriminants", N);
17006 end if;
17007
17008 -- The use of the name of a discriminant is not allowed in default
17009 -- expressions of a discriminant part if the specification of the
17010 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
17011
17012 -- To detect this, the discriminant names are entered initially with an
17013 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
17014 -- attempt to use a void entity (for example in an expression that is
17015 -- type-checked) produces the error message: premature usage. Now after
17016 -- completing the semantic analysis of the discriminant part, we can set
17017 -- the Ekind of all the discriminants appropriately.
17018
17019 Discr := First (Discriminant_Specifications (N));
17020 Discr_Number := Uint_1;
996ae0b0
RK
17021 while Present (Discr) loop
17022 Id := Defining_Identifier (Discr);
17023 Set_Ekind (Id, E_Discriminant);
17024 Init_Component_Location (Id);
17025 Init_Esize (Id);
17026 Set_Discriminant_Number (Id, Discr_Number);
17027
17028 -- Make sure this is always set, even in illegal programs
17029
17030 Set_Corresponding_Discriminant (Id, Empty);
17031
17032 -- Initialize the Original_Record_Component to the entity itself.
17033 -- Inherit_Components will propagate the right value to
17034 -- discriminants in derived record types.
17035
17036 Set_Original_Record_Component (Id, Id);
17037
ffe9aba8 17038 -- Create the discriminal for the discriminant
996ae0b0
RK
17039
17040 Build_Discriminal (Id);
17041
17042 Next (Discr);
17043 Discr_Number := Discr_Number + 1;
17044 end loop;
17045
17046 Set_Has_Discriminants (Current_Scope);
17047 end Process_Discriminants;
17048
17049 -----------------------
17050 -- Process_Full_View --
17051 -----------------------
17052
17053 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
17054 Priv_Parent : Entity_Id;
17055 Full_Parent : Entity_Id;
17056 Full_Indic : Node_Id;
17057
653da906
RD
17058 procedure Collect_Implemented_Interfaces
17059 (Typ : Entity_Id;
17060 Ifaces : Elist_Id);
17061 -- Ada 2005: Gather all the interfaces that Typ directly or
17062 -- inherently implements. Duplicate entries are not added to
17063 -- the list Ifaces.
17064
653da906
RD
17065 ------------------------------------
17066 -- Collect_Implemented_Interfaces --
17067 ------------------------------------
758c442c 17068
653da906
RD
17069 procedure Collect_Implemented_Interfaces
17070 (Typ : Entity_Id;
17071 Ifaces : Elist_Id)
758c442c 17072 is
653da906
RD
17073 Iface : Entity_Id;
17074 Iface_Elmt : Elmt_Id;
758c442c
GD
17075
17076 begin
57193e09
TQ
17077 -- Abstract interfaces are only associated with tagged record types
17078
17079 if not Is_Tagged_Type (Typ)
17080 or else not Is_Record_Type (Typ)
17081 then
17082 return;
17083 end if;
17084
88b32fc3
BD
17085 -- Recursively climb to the ancestors
17086
17087 if Etype (Typ) /= Typ
17088
17089 -- Protect the frontend against wrong cyclic declarations like:
758c442c 17090
88b32fc3
BD
17091 -- type B is new A with private;
17092 -- type C is new A with private;
17093 -- private
17094 -- type B is new C with null record;
17095 -- type C is new B with null record;
17096
17097 and then Etype (Typ) /= Priv_T
17098 and then Etype (Typ) /= Full_T
653da906 17099 then
88b32fc3
BD
17100 -- Keep separate the management of private type declarations
17101
17102 if Ekind (Typ) = E_Record_Type_With_Private then
17103
308e6f3a 17104 -- Handle the following erroneous case:
88b32fc3
BD
17105 -- type Private_Type is tagged private;
17106 -- private
17107 -- type Private_Type is new Type_Implementing_Iface;
17108
17109 if Present (Full_View (Typ))
17110 and then Etype (Typ) /= Full_View (Typ)
17111 then
dc06abec
RD
17112 if Is_Interface (Etype (Typ)) then
17113 Append_Unique_Elmt (Etype (Typ), Ifaces);
88b32fc3
BD
17114 end if;
17115
17116 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17117 end if;
17118
17119 -- Non-private types
17120
17121 else
dc06abec
RD
17122 if Is_Interface (Etype (Typ)) then
17123 Append_Unique_Elmt (Etype (Typ), Ifaces);
88b32fc3
BD
17124 end if;
17125
17126 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17127 end if;
653da906 17128 end if;
9dfd2ff8 17129
88b32fc3 17130 -- Handle entities in the list of abstract interfaces
9dfd2ff8 17131
ce2b6ba5
JM
17132 if Present (Interfaces (Typ)) then
17133 Iface_Elmt := First_Elmt (Interfaces (Typ));
653da906
RD
17134 while Present (Iface_Elmt) loop
17135 Iface := Node (Iface_Elmt);
17136
57193e09
TQ
17137 pragma Assert (Is_Interface (Iface));
17138
17139 if not Contain_Interface (Iface, Ifaces) then
653da906 17140 Append_Elmt (Iface, Ifaces);
57193e09 17141 Collect_Implemented_Interfaces (Iface, Ifaces);
653da906
RD
17142 end if;
17143
17144 Next_Elmt (Iface_Elmt);
17145 end loop;
17146 end if;
653da906
RD
17147 end Collect_Implemented_Interfaces;
17148
758c442c
GD
17149 -- Start of processing for Process_Full_View
17150
996ae0b0
RK
17151 begin
17152 -- First some sanity checks that must be done after semantic
17153 -- decoration of the full view and thus cannot be placed with other
17154 -- similar checks in Find_Type_Name
17155
17156 if not Is_Limited_Type (Priv_T)
17157 and then (Is_Limited_Type (Full_T)
17158 or else Is_Limited_Composite (Full_T))
17159 then
17160 Error_Msg_N
17161 ("completion of nonlimited type cannot be limited", Full_T);
fbf5a39b 17162 Explain_Limited_Type (Full_T, Full_T);
996ae0b0 17163
fea9e956
ES
17164 elsif Is_Abstract_Type (Full_T)
17165 and then not Is_Abstract_Type (Priv_T)
17166 then
996ae0b0
RK
17167 Error_Msg_N
17168 ("completion of nonabstract type cannot be abstract", Full_T);
17169
17170 elsif Is_Tagged_Type (Priv_T)
17171 and then Is_Limited_Type (Priv_T)
17172 and then not Is_Limited_Type (Full_T)
17173 then
dc06abec
RD
17174 -- If pragma CPP_Class was applied to the private declaration
17175 -- propagate the limitedness to the full-view
17176
17177 if Is_CPP_Class (Priv_T) then
17178 Set_Is_Limited_Record (Full_T);
17179
996ae0b0
RK
17180 -- GNAT allow its own definition of Limited_Controlled to disobey
17181 -- this rule in order in ease the implementation. The next test is
17182 -- safe because Root_Controlled is defined in a private system child
17183
dc06abec 17184 elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
996ae0b0
RK
17185 Set_Is_Limited_Composite (Full_T);
17186 else
17187 Error_Msg_N
17188 ("completion of limited tagged type must be limited", Full_T);
17189 end if;
17190
17191 elsif Is_Generic_Type (Priv_T) then
17192 Error_Msg_N ("generic type cannot have a completion", Full_T);
17193 end if;
17194
88b32fc3
BD
17195 -- Check that ancestor interfaces of private and full views are
17196 -- consistent. We omit this check for synchronized types because
fea9e956 17197 -- they are performed on the corresponding record type when frozen.
88b32fc3 17198
0791fbe9 17199 if Ada_Version >= Ada_2005
653da906 17200 and then Is_Tagged_Type (Priv_T)
758c442c 17201 and then Is_Tagged_Type (Full_T)
fea9e956 17202 and then not Is_Concurrent_Type (Full_T)
758c442c
GD
17203 then
17204 declare
653da906
RD
17205 Iface : Entity_Id;
17206 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
17207 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
758c442c
GD
17208
17209 begin
653da906
RD
17210 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
17211 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
758c442c 17212
57193e09
TQ
17213 -- Ada 2005 (AI-251): The partial view shall be a descendant of
17214 -- an interface type if and only if the full type is descendant
17215 -- of the interface type (AARM 7.3 (7.3/2).
17216
17217 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
17218
17219 if Present (Iface) then
ed2233dc
AC
17220 Error_Msg_NE
17221 ("interface & not implemented by full type " &
17222 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
57193e09 17223 end if;
758c442c 17224
653da906 17225 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
758c442c 17226
653da906 17227 if Present (Iface) then
ed2233dc
AC
17228 Error_Msg_NE
17229 ("interface & not implemented by partial view " &
17230 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
758c442c
GD
17231 end if;
17232 end;
17233 end if;
17234
996ae0b0
RK
17235 if Is_Tagged_Type (Priv_T)
17236 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17237 and then Is_Derived_Type (Full_T)
17238 then
17239 Priv_Parent := Etype (Priv_T);
17240
17241 -- The full view of a private extension may have been transformed
17242 -- into an unconstrained derived type declaration and a subtype
17243 -- declaration (see build_derived_record_type for details).
17244
17245 if Nkind (N) = N_Subtype_Declaration then
17246 Full_Indic := Subtype_Indication (N);
17247 Full_Parent := Etype (Base_Type (Full_T));
17248 else
17249 Full_Indic := Subtype_Indication (Type_Definition (N));
17250 Full_Parent := Etype (Full_T);
17251 end if;
17252
17253 -- Check that the parent type of the full type is a descendant of
17254 -- the ancestor subtype given in the private extension. If either
17255 -- entity has an Etype equal to Any_Type then we had some previous
17256 -- error situation [7.3(8)].
17257
17258 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
17259 return;
17260
653da906
RD
17261 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
17262 -- any order. Therefore we don't have to check that its parent must
17263 -- be a descendant of the parent of the private type declaration.
17264
17265 elsif Is_Interface (Priv_Parent)
17266 and then Is_Interface (Full_Parent)
17267 then
17268 null;
17269
57193e09
TQ
17270 -- Ada 2005 (AI-251): If the parent of the private type declaration
17271 -- is an interface there is no need to check that it is an ancestor
17272 -- of the associated full type declaration. The required tests for
16b05213 17273 -- this case are performed by Build_Derived_Record_Type.
57193e09
TQ
17274
17275 elsif not Is_Interface (Base_Type (Priv_Parent))
17276 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
17277 then
950d3e7d
ES
17278 Error_Msg_N
17279 ("parent of full type must descend from parent"
17280 & " of private extension", Full_Indic);
996ae0b0
RK
17281
17282 -- Check the rules of 7.3(10): if the private extension inherits
17283 -- known discriminants, then the full type must also inherit those
17284 -- discriminants from the same (ancestor) type, and the parent
17285 -- subtype of the full type must be constrained if and only if
17286 -- the ancestor subtype of the private extension is constrained.
17287
57193e09 17288 elsif No (Discriminant_Specifications (Parent (Priv_T)))
996ae0b0
RK
17289 and then not Has_Unknown_Discriminants (Priv_T)
17290 and then Has_Discriminants (Base_Type (Priv_Parent))
17291 then
17292 declare
17293 Priv_Indic : constant Node_Id :=
17294 Subtype_Indication (Parent (Priv_T));
17295
17296 Priv_Constr : constant Boolean :=
17297 Is_Constrained (Priv_Parent)
17298 or else
17299 Nkind (Priv_Indic) = N_Subtype_Indication
17300 or else Is_Constrained (Entity (Priv_Indic));
17301
17302 Full_Constr : constant Boolean :=
17303 Is_Constrained (Full_Parent)
17304 or else
17305 Nkind (Full_Indic) = N_Subtype_Indication
17306 or else Is_Constrained (Entity (Full_Indic));
17307
17308 Priv_Discr : Entity_Id;
17309 Full_Discr : Entity_Id;
17310
17311 begin
17312 Priv_Discr := First_Discriminant (Priv_Parent);
17313 Full_Discr := First_Discriminant (Full_Parent);
996ae0b0
RK
17314 while Present (Priv_Discr) and then Present (Full_Discr) loop
17315 if Original_Record_Component (Priv_Discr) =
17316 Original_Record_Component (Full_Discr)
17317 or else
17318 Corresponding_Discriminant (Priv_Discr) =
17319 Corresponding_Discriminant (Full_Discr)
17320 then
17321 null;
17322 else
17323 exit;
17324 end if;
17325
17326 Next_Discriminant (Priv_Discr);
17327 Next_Discriminant (Full_Discr);
17328 end loop;
17329
17330 if Present (Priv_Discr) or else Present (Full_Discr) then
17331 Error_Msg_N
17332 ("full view must inherit discriminants of the parent type"
17333 & " used in the private extension", Full_Indic);
17334
17335 elsif Priv_Constr and then not Full_Constr then
17336 Error_Msg_N
17337 ("parent subtype of full type must be constrained",
17338 Full_Indic);
17339
17340 elsif Full_Constr and then not Priv_Constr then
17341 Error_Msg_N
17342 ("parent subtype of full type must be unconstrained",
17343 Full_Indic);
17344 end if;
17345 end;
17346
17347 -- Check the rules of 7.3(12): if a partial view has neither known
17348 -- or unknown discriminants, then the full type declaration shall
17349 -- define a definite subtype.
17350
17351 elsif not Has_Unknown_Discriminants (Priv_T)
17352 and then not Has_Discriminants (Priv_T)
17353 and then not Is_Constrained (Full_T)
17354 then
17355 Error_Msg_N
17356 ("full view must define a constrained type if partial view"
758c442c 17357 & " has no discriminants", Full_T);
996ae0b0
RK
17358 end if;
17359
17360 -- ??????? Do we implement the following properly ?????
17361 -- If the ancestor subtype of a private extension has constrained
17362 -- discriminants, then the parent subtype of the full view shall
17363 -- impose a statically matching constraint on those discriminants
17364 -- [7.3(13)].
17365
17366 else
17367 -- For untagged types, verify that a type without discriminants
17368 -- is not completed with an unconstrained type.
17369
17370 if not Is_Indefinite_Subtype (Priv_T)
17371 and then Is_Indefinite_Subtype (Full_T)
17372 then
17373 Error_Msg_N ("full view of type must be definite subtype", Full_T);
17374 end if;
17375 end if;
17376
653da906
RD
17377 -- AI-419: verify that the use of "limited" is consistent
17378
17379 declare
17380 Orig_Decl : constant Node_Id := Original_Node (N);
88b32fc3 17381
653da906
RD
17382 begin
17383 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17384 and then not Limited_Present (Parent (Priv_T))
88b32fc3 17385 and then not Synchronized_Present (Parent (Priv_T))
653da906
RD
17386 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
17387 and then Nkind
17388 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
17389 and then Limited_Present (Type_Definition (Orig_Decl))
17390 then
17391 Error_Msg_N
17392 ("full view of non-limited extension cannot be limited", N);
17393 end if;
17394 end;
17395
88b32fc3
BD
17396 -- Ada 2005 (AI-443): A synchronized private extension must be
17397 -- completed by a task or protected type.
17398
0791fbe9 17399 if Ada_Version >= Ada_2005
88b32fc3
BD
17400 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17401 and then Synchronized_Present (Parent (Priv_T))
fea9e956 17402 and then not Is_Concurrent_Type (Full_T)
88b32fc3
BD
17403 then
17404 Error_Msg_N ("full view of synchronized extension must " &
17405 "be synchronized type", N);
17406 end if;
17407
758c442c
GD
17408 -- Ada 2005 AI-363: if the full view has discriminants with
17409 -- defaults, it is illegal to declare constrained access subtypes
17410 -- whose designated type is the current type. This allows objects
17411 -- of the type that are declared in the heap to be unconstrained.
17412
17413 if not Has_Unknown_Discriminants (Priv_T)
17414 and then not Has_Discriminants (Priv_T)
17415 and then Has_Discriminants (Full_T)
17416 and then
88b32fc3 17417 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
758c442c
GD
17418 then
17419 Set_Has_Constrained_Partial_View (Full_T);
17420 Set_Has_Constrained_Partial_View (Priv_T);
17421 end if;
17422
996ae0b0 17423 -- Create a full declaration for all its subtypes recorded in
a5b62485
AC
17424 -- Private_Dependents and swap them similarly to the base type. These
17425 -- are subtypes that have been define before the full declaration of
17426 -- the private type. We also swap the entry in Private_Dependents list
17427 -- so we can properly restore the private view on exit from the scope.
996ae0b0
RK
17428
17429 declare
17430 Priv_Elmt : Elmt_Id;
17431 Priv : Entity_Id;
17432 Full : Entity_Id;
17433
17434 begin
17435 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
17436 while Present (Priv_Elmt) loop
17437 Priv := Node (Priv_Elmt);
17438
bce79204
AC
17439 if Ekind_In (Priv, E_Private_Subtype,
17440 E_Limited_Private_Subtype,
17441 E_Record_Subtype_With_Private)
996ae0b0
RK
17442 then
17443 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
17444 Set_Is_Itype (Full);
17445 Set_Parent (Full, Parent (Priv));
17446 Set_Associated_Node_For_Itype (Full, N);
17447
17448 -- Now we need to complete the private subtype, but since the
17449 -- base type has already been swapped, we must also swap the
17450 -- subtypes (and thus, reverse the arguments in the call to
17451 -- Complete_Private_Subtype).
17452
17453 Copy_And_Swap (Priv, Full);
17454 Complete_Private_Subtype (Full, Priv, Full_T, N);
17455 Replace_Elmt (Priv_Elmt, Full);
17456 end if;
17457
17458 Next_Elmt (Priv_Elmt);
17459 end loop;
17460 end;
17461
2b73cf68
JM
17462 -- If the private view was tagged, copy the new primitive operations
17463 -- from the private view to the full view.
996ae0b0 17464
d44202ba 17465 if Is_Tagged_Type (Full_T) then
996ae0b0 17466 declare
d44202ba
HK
17467 Disp_Typ : Entity_Id;
17468 Full_List : Elist_Id;
996ae0b0 17469 Prim : Entity_Id;
d44202ba
HK
17470 Prim_Elmt : Elmt_Id;
17471 Priv_List : Elist_Id;
17472
17473 function Contains
17474 (E : Entity_Id;
17475 L : Elist_Id) return Boolean;
17476 -- Determine whether list L contains element E
17477
17478 --------------
17479 -- Contains --
17480 --------------
17481
17482 function Contains
17483 (E : Entity_Id;
17484 L : Elist_Id) return Boolean
17485 is
17486 List_Elmt : Elmt_Id;
17487
17488 begin
17489 List_Elmt := First_Elmt (L);
17490 while Present (List_Elmt) loop
17491 if Node (List_Elmt) = E then
17492 return True;
17493 end if;
17494
17495 Next_Elmt (List_Elmt);
17496 end loop;
17497
17498 return False;
17499 end Contains;
17500
17501 -- Start of processing
996ae0b0
RK
17502
17503 begin
17504 if Is_Tagged_Type (Priv_T) then
17505 Priv_List := Primitive_Operations (Priv_T);
d44202ba
HK
17506 Prim_Elmt := First_Elmt (Priv_List);
17507
17508 -- In the case of a concurrent type completing a private tagged
16b05213 17509 -- type, primitives may have been declared in between the two
d44202ba
HK
17510 -- views. These subprograms need to be wrapped the same way
17511 -- entries and protected procedures are handled because they
17512 -- cannot be directly shared by the two views.
17513
17514 if Is_Concurrent_Type (Full_T) then
17515 declare
17516 Conc_Typ : constant Entity_Id :=
17517 Corresponding_Record_Type (Full_T);
d44202ba
HK
17518 Curr_Nod : Node_Id := Parent (Conc_Typ);
17519 Wrap_Spec : Node_Id;
996ae0b0 17520
d44202ba
HK
17521 begin
17522 while Present (Prim_Elmt) loop
17523 Prim := Node (Prim_Elmt);
996ae0b0 17524
d44202ba
HK
17525 if Comes_From_Source (Prim)
17526 and then not Is_Abstract_Subprogram (Prim)
17527 then
17528 Wrap_Spec :=
eb9cb0fc 17529 Make_Subprogram_Declaration (Sloc (Prim),
d44202ba 17530 Specification =>
eb9cb0fc
ES
17531 Build_Wrapper_Spec
17532 (Subp_Id => Prim,
17533 Obj_Typ => Conc_Typ,
17534 Formals =>
17535 Parameter_Specifications (
17536 Parent (Prim))));
d44202ba
HK
17537
17538 Insert_After (Curr_Nod, Wrap_Spec);
17539 Curr_Nod := Wrap_Spec;
17540
17541 Analyze (Wrap_Spec);
17542 end if;
996ae0b0 17543
d44202ba 17544 Next_Elmt (Prim_Elmt);
996ae0b0
RK
17545 end loop;
17546
d44202ba
HK
17547 return;
17548 end;
17549
17550 -- For non-concurrent types, transfer explicit primitives, but
17551 -- omit those inherited from the parent of the private view
17552 -- since they will be re-inherited later on.
17553
17554 else
17555 Full_List := Primitive_Operations (Full_T);
17556
17557 while Present (Prim_Elmt) loop
17558 Prim := Node (Prim_Elmt);
996ae0b0 17559
d44202ba
HK
17560 if Comes_From_Source (Prim)
17561 and then not Contains (Prim, Full_List)
17562 then
996ae0b0
RK
17563 Append_Elmt (Prim, Full_List);
17564 end if;
996ae0b0 17565
d44202ba
HK
17566 Next_Elmt (Prim_Elmt);
17567 end loop;
17568 end if;
17569
17570 -- Untagged private view
996ae0b0
RK
17571
17572 else
d44202ba
HK
17573 Full_List := Primitive_Operations (Full_T);
17574
88b32fc3
BD
17575 -- In this case the partial view is untagged, so here we locate
17576 -- all of the earlier primitives that need to be treated as
17577 -- dispatching (those that appear between the two views). Note
17578 -- that these additional operations must all be new operations
17579 -- (any earlier operations that override inherited operations
17580 -- of the full view will already have been inserted in the
17581 -- primitives list, marked by Check_Operation_From_Private_View
17582 -- as dispatching. Note that implicit "/=" operators are
17583 -- excluded from being added to the primitives list since they
17584 -- shouldn't be treated as dispatching (tagged "/=" is handled
17585 -- specially).
996ae0b0
RK
17586
17587 Prim := Next_Entity (Full_T);
17588 while Present (Prim) and then Prim /= Priv_T loop
bce79204 17589 if Ekind_In (Prim, E_Procedure, E_Function) then
d44202ba 17590 Disp_Typ := Find_Dispatching_Type (Prim);
996ae0b0 17591
d44202ba 17592 if Disp_Typ = Full_T
996ae0b0
RK
17593 and then (Chars (Prim) /= Name_Op_Ne
17594 or else Comes_From_Source (Prim))
17595 then
17596 Check_Controlling_Formals (Full_T, Prim);
17597
17598 if not Is_Dispatching_Operation (Prim) then
17599 Append_Elmt (Prim, Full_List);
17600 Set_Is_Dispatching_Operation (Prim, True);
17601 Set_DT_Position (Prim, No_Uint);
17602 end if;
17603
17604 elsif Is_Dispatching_Operation (Prim)
d44202ba 17605 and then Disp_Typ /= Full_T
996ae0b0
RK
17606 then
17607
88b32fc3
BD
17608 -- Verify that it is not otherwise controlled by a
17609 -- formal or a return value of type T.
996ae0b0 17610
d44202ba 17611 Check_Controlling_Formals (Disp_Typ, Prim);
996ae0b0
RK
17612 end if;
17613 end if;
17614
17615 Next_Entity (Prim);
17616 end loop;
17617 end if;
17618
61441c18
TQ
17619 -- For the tagged case, the two views can share the same primitive
17620 -- operations list and the same class-wide type. Update attributes
17621 -- of the class-wide type which depend on the full declaration.
996ae0b0
RK
17622
17623 if Is_Tagged_Type (Priv_T) then
ef2a63ba 17624 Set_Direct_Primitive_Operations (Priv_T, Full_List);
996ae0b0
RK
17625 Set_Class_Wide_Type
17626 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
17627
996ae0b0 17628 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
996ae0b0
RK
17629 end if;
17630 end;
17631 end if;
88b32fc3
BD
17632
17633 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
17634
17635 if Known_To_Have_Preelab_Init (Priv_T) then
17636
17637 -- Case where there is a pragma Preelaborable_Initialization. We
17638 -- always allow this in predefined units, which is a bit of a kludge,
17639 -- but it means we don't have to struggle to meet the requirements in
17640 -- the RM for having Preelaborable Initialization. Otherwise we
17641 -- require that the type meets the RM rules. But we can't check that
308e6f3a
RW
17642 -- yet, because of the rule about overriding Initialize, so we simply
17643 -- set a flag that will be checked at freeze time.
88b32fc3
BD
17644
17645 if not In_Predefined_Unit (Full_T) then
17646 Set_Must_Have_Preelab_Init (Full_T);
17647 end if;
17648 end if;
2b73cf68
JM
17649
17650 -- If pragma CPP_Class was applied to the private type declaration,
17651 -- propagate it now to the full type declaration.
17652
17653 if Is_CPP_Class (Priv_T) then
17654 Set_Is_CPP_Class (Full_T);
17655 Set_Convention (Full_T, Convention_CPP);
17656 end if;
23c4ff9b
AC
17657
17658 -- If the private view has user specified stream attributes, then so has
17659 -- the full view.
17660
e606088a
AC
17661 -- Why the test, how could these flags be already set in Full_T ???
17662
23c4ff9b
AC
17663 if Has_Specified_Stream_Read (Priv_T) then
17664 Set_Has_Specified_Stream_Read (Full_T);
17665 end if;
e606088a 17666
23c4ff9b
AC
17667 if Has_Specified_Stream_Write (Priv_T) then
17668 Set_Has_Specified_Stream_Write (Full_T);
17669 end if;
e606088a 17670
23c4ff9b
AC
17671 if Has_Specified_Stream_Input (Priv_T) then
17672 Set_Has_Specified_Stream_Input (Full_T);
17673 end if;
e606088a 17674
23c4ff9b
AC
17675 if Has_Specified_Stream_Output (Priv_T) then
17676 Set_Has_Specified_Stream_Output (Full_T);
17677 end if;
e606088a 17678
f2264ac2 17679 -- Propagate invariants to full type
e606088a 17680
f2264ac2 17681 if Has_Invariants (Priv_T) then
e606088a 17682 Set_Has_Invariants (Full_T);
f2264ac2 17683 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
e606088a
AC
17684 end if;
17685
f2264ac2 17686 if Has_Inheritable_Invariants (Priv_T) then
e606088a 17687 Set_Has_Inheritable_Invariants (Full_T);
4818e7b9
RD
17688 end if;
17689
48f91b44 17690 -- Propagate predicates to full type
4818e7b9
RD
17691
17692 if Has_Predicates (Priv_T) then
48f91b44
RD
17693 Set_Predicate_Function (Priv_T, Predicate_Function (Full_T));
17694 Set_Has_Predicates (Priv_T);
e606088a 17695 end if;
996ae0b0
RK
17696 end Process_Full_View;
17697
17698 -----------------------------------
17699 -- Process_Incomplete_Dependents --
17700 -----------------------------------
17701
17702 procedure Process_Incomplete_Dependents
17703 (N : Node_Id;
17704 Full_T : Entity_Id;
17705 Inc_T : Entity_Id)
17706 is
17707 Inc_Elmt : Elmt_Id;
17708 Priv_Dep : Entity_Id;
17709 New_Subt : Entity_Id;
17710
17711 Disc_Constraint : Elist_Id;
17712
17713 begin
17714 if No (Private_Dependents (Inc_T)) then
17715 return;
996ae0b0
RK
17716 end if;
17717
9dfd2ff8
CC
17718 -- Itypes that may be generated by the completion of an incomplete
17719 -- subtype are not used by the back-end and not attached to the tree.
17720 -- They are created only for constraint-checking purposes.
17721
17722 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
996ae0b0
RK
17723 while Present (Inc_Elmt) loop
17724 Priv_Dep := Node (Inc_Elmt);
17725
17726 if Ekind (Priv_Dep) = E_Subprogram_Type then
17727
17728 -- An Access_To_Subprogram type may have a return type or a
17729 -- parameter type that is incomplete. Replace with the full view.
17730
17731 if Etype (Priv_Dep) = Inc_T then
17732 Set_Etype (Priv_Dep, Full_T);
17733 end if;
17734
17735 declare
17736 Formal : Entity_Id;
17737
17738 begin
17739 Formal := First_Formal (Priv_Dep);
996ae0b0 17740 while Present (Formal) loop
996ae0b0
RK
17741 if Etype (Formal) = Inc_T then
17742 Set_Etype (Formal, Full_T);
17743 end if;
17744
17745 Next_Formal (Formal);
17746 end loop;
17747 end;
17748
9dfd2ff8 17749 elsif Is_Overloadable (Priv_Dep) then
996ae0b0 17750
9dfd2ff8
CC
17751 -- A protected operation is never dispatching: only its
17752 -- wrapper operation (which has convention Ada) is.
17753
17754 if Is_Tagged_Type (Full_T)
17755 and then Convention (Priv_Dep) /= Convention_Protected
17756 then
996ae0b0
RK
17757
17758 -- Subprogram has an access parameter whose designated type
17759 -- was incomplete. Reexamine declaration now, because it may
17760 -- be a primitive operation of the full type.
17761
17762 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
17763 Set_Is_Dispatching_Operation (Priv_Dep);
17764 Check_Controlling_Formals (Full_T, Priv_Dep);
17765 end if;
17766
17767 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
17768
17769 -- Can happen during processing of a body before the completion
17770 -- of a TA type. Ignore, because spec is also on dependent list.
17771
17772 return;
17773
88b32fc3
BD
17774 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
17775 -- corresponding subtype of the full view.
17776
17777 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
17778 Set_Subtype_Indication
17779 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
17780 Set_Etype (Priv_Dep, Full_T);
17781 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
17782 Set_Analyzed (Parent (Priv_Dep), False);
17783
17784 -- Reanalyze the declaration, suppressing the call to
17785 -- Enter_Name to avoid duplicate names.
17786
17787 Analyze_Subtype_Declaration
17788 (N => Parent (Priv_Dep),
17789 Skip => True);
17790
996ae0b0
RK
17791 -- Dependent is a subtype
17792
17793 else
17794 -- We build a new subtype indication using the full view of the
17795 -- incomplete parent. The discriminant constraints have been
17796 -- elaborated already at the point of the subtype declaration.
17797
17798 New_Subt := Create_Itype (E_Void, N);
17799
17800 if Has_Discriminants (Full_T) then
17801 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
17802 else
17803 Disc_Constraint := No_Elist;
17804 end if;
17805
17806 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
17807 Set_Full_View (Priv_Dep, New_Subt);
17808 end if;
17809
17810 Next_Elmt (Inc_Elmt);
17811 end loop;
996ae0b0
RK
17812 end Process_Incomplete_Dependents;
17813
17814 --------------------------------
17815 -- Process_Range_Expr_In_Decl --
17816 --------------------------------
17817
17818 procedure Process_Range_Expr_In_Decl
17819 (R : Node_Id;
17820 T : Entity_Id;
996ae0b0
RK
17821 Check_List : List_Id := Empty_List;
17822 R_Check_Off : Boolean := False)
17823 is
0592046e
AC
17824 Lo, Hi : Node_Id;
17825 R_Checks : Check_Result;
17826 Insert_Node : Node_Id;
17827 Def_Id : Entity_Id;
996ae0b0
RK
17828
17829 begin
17830 Analyze_And_Resolve (R, Base_Type (T));
17831
17832 if Nkind (R) = N_Range then
17833 Lo := Low_Bound (R);
17834 Hi := High_Bound (R);
17835
88b32fc3
BD
17836 -- We need to ensure validity of the bounds here, because if we
17837 -- go ahead and do the expansion, then the expanded code will get
17838 -- analyzed with range checks suppressed and we miss the check.
17839
17840 Validity_Check_Range (R);
17841
996ae0b0
RK
17842 -- If there were errors in the declaration, try and patch up some
17843 -- common mistakes in the bounds. The cases handled are literals
17844 -- which are Integer where the expected type is Real and vice versa.
17845 -- These corrections allow the compilation process to proceed further
17846 -- along since some basic assumptions of the format of the bounds
17847 -- are guaranteed.
17848
17849 if Etype (R) = Any_Type then
17850
17851 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
17852 Rewrite (Lo,
17853 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
17854
17855 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
17856 Rewrite (Hi,
17857 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
17858
17859 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
17860 Rewrite (Lo,
17861 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
17862
17863 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
17864 Rewrite (Hi,
17865 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
17866 end if;
17867
17868 Set_Etype (Lo, T);
17869 Set_Etype (Hi, T);
17870 end if;
17871
a5b62485
AC
17872 -- If the bounds of the range have been mistakenly given as string
17873 -- literals (perhaps in place of character literals), then an error
17874 -- has already been reported, but we rewrite the string literal as a
17875 -- bound of the range's type to avoid blowups in later processing
17876 -- that looks at static values.
996ae0b0
RK
17877
17878 if Nkind (Lo) = N_String_Literal then
17879 Rewrite (Lo,
17880 Make_Attribute_Reference (Sloc (Lo),
17881 Attribute_Name => Name_First,
17882 Prefix => New_Reference_To (T, Sloc (Lo))));
17883 Analyze_And_Resolve (Lo);
17884 end if;
17885
17886 if Nkind (Hi) = N_String_Literal then
17887 Rewrite (Hi,
17888 Make_Attribute_Reference (Sloc (Hi),
17889 Attribute_Name => Name_First,
17890 Prefix => New_Reference_To (T, Sloc (Hi))));
17891 Analyze_And_Resolve (Hi);
17892 end if;
17893
17894 -- If bounds aren't scalar at this point then exit, avoiding
17895 -- problems with further processing of the range in this procedure.
17896
17897 if not Is_Scalar_Type (Etype (Lo)) then
17898 return;
17899 end if;
17900
17901 -- Resolve (actually Sem_Eval) has checked that the bounds are in
17902 -- then range of the base type. Here we check whether the bounds
17903 -- are in the range of the subtype itself. Note that if the bounds
17904 -- represent the null range the Constraint_Error exception should
17905 -- not be raised.
17906
17907 -- ??? The following code should be cleaned up as follows
a5b62485 17908
fbf5a39b 17909 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
996ae0b0 17910 -- is done in the call to Range_Check (R, T); below
a5b62485 17911
996ae0b0
RK
17912 -- 2. The use of R_Check_Off should be investigated and possibly
17913 -- removed, this would clean up things a bit.
17914
17915 if Is_Null_Range (Lo, Hi) then
17916 null;
17917
17918 else
fbf5a39b
AC
17919 -- Capture values of bounds and generate temporaries for them
17920 -- if needed, before applying checks, since checks may cause
17921 -- duplication of the expression without forcing evaluation.
17922
17923 if Expander_Active then
17924 Force_Evaluation (Lo);
17925 Force_Evaluation (Hi);
17926 end if;
17927
996ae0b0 17928 -- We use a flag here instead of suppressing checks on the
fbf5a39b
AC
17929 -- type because the type we check against isn't necessarily
17930 -- the place where we put the check.
996ae0b0
RK
17931
17932 if not R_Check_Off then
dc06abec 17933 R_Checks := Get_Range_Checks (R, T);
996ae0b0 17934
0592046e
AC
17935 -- Look up tree to find an appropriate insertion point. We
17936 -- can't just use insert_actions because later processing
17937 -- depends on the insertion node. Prior to Ada2012 the
17938 -- insertion point could only be a declaration or a loop, but
17939 -- quantified expressions can appear within any context in an
17940 -- expression, and the insertion point can be any statement,
17941 -- pragma, or declaration.
17942
17943 Insert_Node := Parent (R);
17944 while Present (Insert_Node) loop
17945 exit when
17946 Nkind (Insert_Node) in N_Declaration
17947 and then
17948 not Nkind_In
17949 (Insert_Node, N_Component_Declaration,
17950 N_Loop_Parameter_Specification,
17951 N_Function_Specification,
17952 N_Procedure_Specification);
17953
17954 exit when Nkind (Insert_Node) in N_Later_Decl_Item
17955 or else Nkind (Insert_Node) in
17956 N_Statement_Other_Than_Procedure_Call
17957 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
17958 N_Pragma);
17959
17960 Insert_Node := Parent (Insert_Node);
996ae0b0
RK
17961 end loop;
17962
17963 -- Why would Type_Decl not be present??? Without this test,
17964 -- short regression tests fail.
17965
0592046e 17966 if Present (Insert_Node) then
fbf5a39b 17967
0592046e
AC
17968 -- Case of loop statement. Verify that the range is part
17969 -- of the subtype indication of the iteration scheme.
fbf5a39b 17970
0592046e 17971 if Nkind (Insert_Node) = N_Loop_Statement then
996ae0b0 17972 declare
9dfd2ff8 17973 Indic : Node_Id;
fbf5a39b 17974
996ae0b0 17975 begin
9dfd2ff8 17976 Indic := Parent (R);
7d7af38a
JM
17977 while Present (Indic)
17978 and then Nkind (Indic) /= N_Subtype_Indication
996ae0b0
RK
17979 loop
17980 Indic := Parent (Indic);
17981 end loop;
17982
17983 if Present (Indic) then
17984 Def_Id := Etype (Subtype_Mark (Indic));
17985
17986 Insert_Range_Checks
17987 (R_Checks,
0592046e 17988 Insert_Node,
996ae0b0 17989 Def_Id,
0592046e 17990 Sloc (Insert_Node),
996ae0b0
RK
17991 R,
17992 Do_Before => True);
17993 end if;
17994 end;
fbf5a39b 17995
0592046e
AC
17996 -- Insertion before a declaration. If the declaration
17997 -- includes discriminants, the list of applicable checks
17998 -- is given by the caller.
fbf5a39b 17999
0592046e
AC
18000 elsif Nkind (Insert_Node) in N_Declaration then
18001 Def_Id := Defining_Identifier (Insert_Node);
996ae0b0
RK
18002
18003 if (Ekind (Def_Id) = E_Record_Type
18004 and then Depends_On_Discriminant (R))
18005 or else
18006 (Ekind (Def_Id) = E_Protected_Type
18007 and then Has_Discriminants (Def_Id))
18008 then
18009 Append_Range_Checks
0592046e
AC
18010 (R_Checks,
18011 Check_List, Def_Id, Sloc (Insert_Node), R);
996ae0b0
RK
18012
18013 else
18014 Insert_Range_Checks
0592046e
AC
18015 (R_Checks,
18016 Insert_Node, Def_Id, Sloc (Insert_Node), R);
996ae0b0
RK
18017
18018 end if;
0592046e
AC
18019
18020 -- Insertion before a statement. Range appears in the
18021 -- context of a quantified expression. Insertion will
18022 -- take place when expression is expanded.
18023
18024 else
18025 null;
996ae0b0
RK
18026 end if;
18027 end if;
18028 end if;
18029 end if;
996ae0b0 18030
0592046e
AC
18031 -- Case of other than an explicit N_Range node
18032
fbf5a39b
AC
18033 elsif Expander_Active then
18034 Get_Index_Bounds (R, Lo, Hi);
996ae0b0
RK
18035 Force_Evaluation (Lo);
18036 Force_Evaluation (Hi);
18037 end if;
996ae0b0
RK
18038 end Process_Range_Expr_In_Decl;
18039
18040 --------------------------------------
18041 -- Process_Real_Range_Specification --
18042 --------------------------------------
18043
18044 procedure Process_Real_Range_Specification (Def : Node_Id) is
18045 Spec : constant Node_Id := Real_Range_Specification (Def);
18046 Lo : Node_Id;
18047 Hi : Node_Id;
18048 Err : Boolean := False;
18049
18050 procedure Analyze_Bound (N : Node_Id);
18051 -- Analyze and check one bound
18052
fbf5a39b
AC
18053 -------------------
18054 -- Analyze_Bound --
18055 -------------------
18056
996ae0b0
RK
18057 procedure Analyze_Bound (N : Node_Id) is
18058 begin
18059 Analyze_And_Resolve (N, Any_Real);
18060
18061 if not Is_OK_Static_Expression (N) then
fbf5a39b
AC
18062 Flag_Non_Static_Expr
18063 ("bound in real type definition is not static!", N);
996ae0b0
RK
18064 Err := True;
18065 end if;
18066 end Analyze_Bound;
18067
fbf5a39b
AC
18068 -- Start of processing for Process_Real_Range_Specification
18069
996ae0b0
RK
18070 begin
18071 if Present (Spec) then
18072 Lo := Low_Bound (Spec);
18073 Hi := High_Bound (Spec);
18074 Analyze_Bound (Lo);
18075 Analyze_Bound (Hi);
18076
18077 -- If error, clear away junk range specification
18078
18079 if Err then
18080 Set_Real_Range_Specification (Def, Empty);
18081 end if;
18082 end if;
18083 end Process_Real_Range_Specification;
18084
18085 ---------------------
18086 -- Process_Subtype --
18087 ---------------------
18088
18089 function Process_Subtype
18090 (S : Node_Id;
18091 Related_Nod : Node_Id;
18092 Related_Id : Entity_Id := Empty;
b0f26df5 18093 Suffix : Character := ' ') return Entity_Id
996ae0b0
RK
18094 is
18095 P : Node_Id;
18096 Def_Id : Entity_Id;
9dfd2ff8 18097 Error_Node : Node_Id;
996ae0b0
RK
18098 Full_View_Id : Entity_Id;
18099 Subtype_Mark_Id : Entity_Id;
fbf5a39b 18100
9dfd2ff8
CC
18101 May_Have_Null_Exclusion : Boolean;
18102
fbf5a39b
AC
18103 procedure Check_Incomplete (T : Entity_Id);
18104 -- Called to verify that an incomplete type is not used prematurely
18105
18106 ----------------------
18107 -- Check_Incomplete --
18108 ----------------------
18109
18110 procedure Check_Incomplete (T : Entity_Id) is
18111 begin
88b32fc3
BD
18112 -- Ada 2005 (AI-412): Incomplete subtypes are legal
18113
18114 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
18115 and then
0791fbe9 18116 not (Ada_Version >= Ada_2005
88b32fc3
BD
18117 and then
18118 (Nkind (Parent (T)) = N_Subtype_Declaration
18119 or else
18120 (Nkind (Parent (T)) = N_Subtype_Indication
18121 and then Nkind (Parent (Parent (T))) =
18122 N_Subtype_Declaration)))
18123 then
fbf5a39b
AC
18124 Error_Msg_N ("invalid use of type before its full declaration", T);
18125 end if;
18126 end Check_Incomplete;
18127
18128 -- Start of processing for Process_Subtype
996ae0b0
RK
18129
18130 begin
fbf5a39b
AC
18131 -- Case of no constraints present
18132
18133 if Nkind (S) /= N_Subtype_Indication then
fbf5a39b
AC
18134 Find_Type (S);
18135 Check_Incomplete (S);
9dfd2ff8 18136 P := Parent (S);
2820d220 18137
0ab80019 18138 -- Ada 2005 (AI-231): Static check
2820d220 18139
0791fbe9 18140 if Ada_Version >= Ada_2005
9dfd2ff8
CC
18141 and then Present (P)
18142 and then Null_Exclusion_Present (P)
18143 and then Nkind (P) /= N_Access_To_Object_Definition
2820d220
AC
18144 and then not Is_Access_Type (Entity (S))
18145 then
2b73cf68 18146 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
9dfd2ff8
CC
18147 end if;
18148
7d7af38a
JM
18149 -- The following is ugly, can't we have a range or even a flag???
18150
9dfd2ff8 18151 May_Have_Null_Exclusion :=
7d7af38a
JM
18152 Nkind_In (P, N_Access_Definition,
18153 N_Access_Function_Definition,
18154 N_Access_Procedure_Definition,
18155 N_Access_To_Object_Definition,
18156 N_Allocator,
18157 N_Component_Definition)
18158 or else
18159 Nkind_In (P, N_Derived_Type_Definition,
18160 N_Discriminant_Specification,
aecf0203 18161 N_Formal_Object_Declaration,
7d7af38a 18162 N_Object_Declaration,
aecf0203 18163 N_Object_Renaming_Declaration,
7d7af38a
JM
18164 N_Parameter_Specification,
18165 N_Subtype_Declaration);
9dfd2ff8
CC
18166
18167 -- Create an Itype that is a duplicate of Entity (S) but with the
498d1b80 18168 -- null-exclusion attribute.
9dfd2ff8
CC
18169
18170 if May_Have_Null_Exclusion
18171 and then Is_Access_Type (Entity (S))
18172 and then Null_Exclusion_Present (P)
18173
18174 -- No need to check the case of an access to object definition.
18175 -- It is correct to define double not-null pointers.
88b32fc3 18176
9dfd2ff8
CC
18177 -- Example:
18178 -- type Not_Null_Int_Ptr is not null access Integer;
18179 -- type Acc is not null access Not_Null_Int_Ptr;
18180
18181 and then Nkind (P) /= N_Access_To_Object_Definition
18182 then
18183 if Can_Never_Be_Null (Entity (S)) then
18184 case Nkind (Related_Nod) is
18185 when N_Full_Type_Declaration =>
18186 if Nkind (Type_Definition (Related_Nod))
18187 in N_Array_Type_Definition
18188 then
18189 Error_Node :=
18190 Subtype_Indication
18191 (Component_Definition
18192 (Type_Definition (Related_Nod)));
18193 else
18194 Error_Node :=
18195 Subtype_Indication (Type_Definition (Related_Nod));
18196 end if;
18197
18198 when N_Subtype_Declaration =>
18199 Error_Node := Subtype_Indication (Related_Nod);
18200
18201 when N_Object_Declaration =>
18202 Error_Node := Object_Definition (Related_Nod);
18203
18204 when N_Component_Declaration =>
18205 Error_Node :=
18206 Subtype_Indication (Component_Definition (Related_Nod));
18207
aecf0203
AC
18208 when N_Allocator =>
18209 Error_Node := Expression (Related_Nod);
18210
9dfd2ff8
CC
18211 when others =>
18212 pragma Assert (False);
18213 Error_Node := Related_Nod;
18214 end case;
18215
2b73cf68
JM
18216 Error_Msg_NE
18217 ("`NOT NULL` not allowed (& already excludes null)",
18218 Error_Node,
18219 Entity (S));
9dfd2ff8
CC
18220 end if;
18221
18222 Set_Etype (S,
18223 Create_Null_Excluding_Itype
18224 (T => Entity (S),
18225 Related_Nod => P));
18226 Set_Entity (S, Etype (S));
2820d220 18227 end if;
9dfd2ff8 18228
fbf5a39b
AC
18229 return Entity (S);
18230
996ae0b0
RK
18231 -- Case of constraint present, so that we have an N_Subtype_Indication
18232 -- node (this node is created only if constraints are present).
18233
fbf5a39b 18234 else
996ae0b0
RK
18235 Find_Type (Subtype_Mark (S));
18236
18237 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
18238 and then not
18239 (Nkind (Parent (S)) = N_Subtype_Declaration
653da906 18240 and then Is_Itype (Defining_Identifier (Parent (S))))
996ae0b0
RK
18241 then
18242 Check_Incomplete (Subtype_Mark (S));
18243 end if;
18244
18245 P := Parent (S);
18246 Subtype_Mark_Id := Entity (Subtype_Mark (S));
18247
996ae0b0
RK
18248 -- Explicit subtype declaration case
18249
18250 if Nkind (P) = N_Subtype_Declaration then
18251 Def_Id := Defining_Identifier (P);
18252
18253 -- Explicit derived type definition case
18254
18255 elsif Nkind (P) = N_Derived_Type_Definition then
18256 Def_Id := Defining_Identifier (Parent (P));
18257
18258 -- Implicit case, the Def_Id must be created as an implicit type.
a5b62485
AC
18259 -- The one exception arises in the case of concurrent types, array
18260 -- and access types, where other subsidiary implicit types may be
18261 -- created and must appear before the main implicit type. In these
18262 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
18263 -- has not yet been called to create Def_Id.
996ae0b0
RK
18264
18265 else
18266 if Is_Array_Type (Subtype_Mark_Id)
18267 or else Is_Concurrent_Type (Subtype_Mark_Id)
18268 or else Is_Access_Type (Subtype_Mark_Id)
18269 then
18270 Def_Id := Empty;
18271
18272 -- For the other cases, we create a new unattached Itype,
18273 -- and set the indication to ensure it gets attached later.
18274
18275 else
18276 Def_Id :=
18277 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18278 end if;
996ae0b0
RK
18279 end if;
18280
18281 -- If the kind of constraint is invalid for this kind of type,
18282 -- then give an error, and then pretend no constraint was given.
18283
18284 if not Is_Valid_Constraint_Kind
18285 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
18286 then
18287 Error_Msg_N
18288 ("incorrect constraint for this kind of type", Constraint (S));
18289
18290 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18291
758c442c 18292 -- Set Ekind of orphan itype, to prevent cascaded errors
82c80734
RD
18293
18294 if Present (Def_Id) then
18295 Set_Ekind (Def_Id, Ekind (Any_Type));
18296 end if;
18297
996ae0b0
RK
18298 -- Make recursive call, having got rid of the bogus constraint
18299
18300 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
18301 end if;
18302
18303 -- Remaining processing depends on type
18304
18305 case Ekind (Subtype_Mark_Id) is
996ae0b0
RK
18306 when Access_Kind =>
18307 Constrain_Access (Def_Id, S, Related_Nod);
18308
fea9e956
ES
18309 if Expander_Active
18310 and then Is_Itype (Designated_Type (Def_Id))
18311 and then Nkind (Related_Nod) = N_Subtype_Declaration
18312 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
18313 then
18314 Build_Itype_Reference
18315 (Designated_Type (Def_Id), Related_Nod);
18316 end if;
18317
996ae0b0
RK
18318 when Array_Kind =>
18319 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
18320
18321 when Decimal_Fixed_Point_Kind =>
07fc65c4 18322 Constrain_Decimal (Def_Id, S);
996ae0b0
RK
18323
18324 when Enumeration_Kind =>
07fc65c4 18325 Constrain_Enumeration (Def_Id, S);
996ae0b0
RK
18326
18327 when Ordinary_Fixed_Point_Kind =>
07fc65c4 18328 Constrain_Ordinary_Fixed (Def_Id, S);
996ae0b0
RK
18329
18330 when Float_Kind =>
07fc65c4 18331 Constrain_Float (Def_Id, S);
996ae0b0
RK
18332
18333 when Integer_Kind =>
07fc65c4 18334 Constrain_Integer (Def_Id, S);
996ae0b0
RK
18335
18336 when E_Record_Type |
18337 E_Record_Subtype |
18338 Class_Wide_Kind |
18339 E_Incomplete_Type =>
18340 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
18341
93bcda23
AC
18342 if Ekind (Def_Id) = E_Incomplete_Type then
18343 Set_Private_Dependents (Def_Id, New_Elmt_List);
18344 end if;
18345
996ae0b0
RK
18346 when Private_Kind =>
18347 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
18348 Set_Private_Dependents (Def_Id, New_Elmt_List);
18349
18350 -- In case of an invalid constraint prevent further processing
18351 -- since the type constructed is missing expected fields.
18352
18353 if Etype (Def_Id) = Any_Type then
18354 return Def_Id;
18355 end if;
18356
18357 -- If the full view is that of a task with discriminants,
18358 -- we must constrain both the concurrent type and its
18359 -- corresponding record type. Otherwise we will just propagate
18360 -- the constraint to the full view, if available.
18361
18362 if Present (Full_View (Subtype_Mark_Id))
18363 and then Has_Discriminants (Subtype_Mark_Id)
18364 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
18365 then
18366 Full_View_Id :=
18367 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18368
18369 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
18370 Constrain_Concurrent (Full_View_Id, S,
18371 Related_Nod, Related_Id, Suffix);
18372 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
18373 Set_Full_View (Def_Id, Full_View_Id);
18374
88b32fc3
BD
18375 -- Introduce an explicit reference to the private subtype,
18376 -- to prevent scope anomalies in gigi if first use appears
18377 -- in a nested context, e.g. a later function body.
18378 -- Should this be generated in other contexts than a full
18379 -- type declaration?
18380
18381 if Is_Itype (Def_Id)
18382 and then
18383 Nkind (Parent (P)) = N_Full_Type_Declaration
18384 then
fea9e956 18385 Build_Itype_Reference (Def_Id, Parent (P));
88b32fc3
BD
18386 end if;
18387
996ae0b0
RK
18388 else
18389 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
18390 end if;
18391
18392 when Concurrent_Kind =>
18393 Constrain_Concurrent (Def_Id, S,
18394 Related_Nod, Related_Id, Suffix);
18395
18396 when others =>
18397 Error_Msg_N ("invalid subtype mark in subtype indication", S);
18398 end case;
18399
18400 -- Size and Convention are always inherited from the base type
18401
18402 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
18403 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
18404
18405 return Def_Id;
996ae0b0
RK
18406 end if;
18407 end Process_Subtype;
18408
fea9e956
ES
18409 ---------------------------------------
18410 -- Check_Anonymous_Access_Components --
18411 ---------------------------------------
996ae0b0 18412
fea9e956
ES
18413 procedure Check_Anonymous_Access_Components
18414 (Typ_Decl : Node_Id;
18415 Typ : Entity_Id;
18416 Prev : Entity_Id;
18417 Comp_List : Node_Id)
fbf5a39b 18418 is
fea9e956
ES
18419 Loc : constant Source_Ptr := Sloc (Typ_Decl);
18420 Anon_Access : Entity_Id;
18421 Acc_Def : Node_Id;
18422 Comp : Node_Id;
18423 Comp_Def : Node_Id;
18424 Decl : Node_Id;
18425 Type_Def : Node_Id;
18426
18427 procedure Build_Incomplete_Type_Declaration;
758c442c 18428 -- If the record type contains components that include an access to the
fea9e956
ES
18429 -- current record, then create an incomplete type declaration for the
18430 -- record, to be used as the designated type of the anonymous access.
18431 -- This is done only once, and only if there is no previous partial
18432 -- view of the type.
18433
5320014a 18434 function Designates_T (Subt : Node_Id) return Boolean;
d33fb1e6
BD
18435 -- Check whether a node designates the enclosing record type, or 'Class
18436 -- of that type
5320014a 18437
fea9e956
ES
18438 function Mentions_T (Acc_Def : Node_Id) return Boolean;
18439 -- Check whether an access definition includes a reference to
5320014a
ST
18440 -- the enclosing record type. The reference can be a subtype mark
18441 -- in the access definition itself, a 'Class attribute reference, or
18442 -- recursively a reference appearing in a parameter specification
18443 -- or result definition of an access_to_subprogram definition.
996ae0b0 18444
fea9e956
ES
18445 --------------------------------------
18446 -- Build_Incomplete_Type_Declaration --
18447 --------------------------------------
996ae0b0 18448
fea9e956
ES
18449 procedure Build_Incomplete_Type_Declaration is
18450 Decl : Node_Id;
18451 Inc_T : Entity_Id;
18452 H : Entity_Id;
996ae0b0 18453
d33fb1e6
BD
18454 -- Is_Tagged indicates whether the type is tagged. It is tagged if
18455 -- it's "is new ... with record" or else "is tagged record ...".
18456
18457 Is_Tagged : constant Boolean :=
18458 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
18459 and then
18460 Present
18461 (Record_Extension_Part (Type_Definition (Typ_Decl))))
18462 or else
18463 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
18464 and then Tagged_Present (Type_Definition (Typ_Decl)));
18465
fea9e956
ES
18466 begin
18467 -- If there is a previous partial view, no need to create a new one
18468 -- If the partial view, given by Prev, is incomplete, If Prev is
18469 -- a private declaration, full declaration is flagged accordingly.
758c442c 18470
fea9e956 18471 if Prev /= Typ then
d33fb1e6 18472 if Is_Tagged then
fea9e956
ES
18473 Make_Class_Wide_Type (Prev);
18474 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
18475 Set_Etype (Class_Wide_Type (Typ), Typ);
18476 end if;
758c442c 18477
fea9e956 18478 return;
758c442c 18479
fea9e956 18480 elsif Has_Private_Declaration (Typ) then
d33fb1e6
BD
18481
18482 -- If we refer to T'Class inside T, and T is the completion of a
18483 -- private type, then we need to make sure the class-wide type
18484 -- exists.
18485
18486 if Is_Tagged then
18487 Make_Class_Wide_Type (Typ);
18488 end if;
18489
fea9e956 18490 return;
57193e09 18491
fea9e956
ES
18492 -- If there was a previous anonymous access type, the incomplete
18493 -- type declaration will have been created already.
57193e09 18494
fea9e956
ES
18495 elsif Present (Current_Entity (Typ))
18496 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
18497 and then Full_View (Current_Entity (Typ)) = Typ
18498 then
b0760739
AC
18499 if Is_Tagged
18500 and then Comes_From_Source (Current_Entity (Typ))
18501 and then not Is_Tagged_Type (Current_Entity (Typ))
18502 then
18503 Make_Class_Wide_Type (Typ);
18504 Error_Msg_N
18505 ("incomplete view of tagged type should be declared tagged?",
2383acbd 18506 Parent (Current_Entity (Typ)));
b0760739 18507 end if;
fea9e956 18508 return;
758c442c 18509
fea9e956 18510 else
ce4a6e84
RD
18511 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
18512 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
758c442c 18513
2383acbd
AC
18514 -- Type has already been inserted into the current scope. Remove
18515 -- it, and add incomplete declaration for type, so that subsequent
18516 -- anonymous access types can use it. The entity is unchained from
18517 -- the homonym list and from immediate visibility. After analysis,
18518 -- the entity in the incomplete declaration becomes immediately
18519 -- visible in the record declaration that follows.
758c442c 18520
fea9e956 18521 H := Current_Entity (Typ);
758c442c 18522
fea9e956
ES
18523 if H = Typ then
18524 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
18525 else
18526 while Present (H)
18527 and then Homonym (H) /= Typ
18528 loop
18529 H := Homonym (Typ);
18530 end loop;
758c442c 18531
fea9e956 18532 Set_Homonym (H, Homonym (Typ));
758c442c 18533 end if;
758c442c 18534
fea9e956
ES
18535 Insert_Before (Typ_Decl, Decl);
18536 Analyze (Decl);
18537 Set_Full_View (Inc_T, Typ);
758c442c 18538
d33fb1e6 18539 if Is_Tagged then
2383acbd
AC
18540
18541 -- Create a common class-wide type for both views, and set the
18542 -- Etype of the class-wide type to the full view.
758c442c 18543
fea9e956
ES
18544 Make_Class_Wide_Type (Inc_T);
18545 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
18546 Set_Etype (Class_Wide_Type (Typ), Typ);
18547 end if;
18548 end if;
18549 end Build_Incomplete_Type_Declaration;
758c442c 18550
5320014a
ST
18551 ------------------
18552 -- Designates_T --
18553 ------------------
18554
18555 function Designates_T (Subt : Node_Id) return Boolean is
fea9e956 18556 Type_Id : constant Name_Id := Chars (Typ);
758c442c 18557
2b73cf68 18558 function Names_T (Nam : Node_Id) return Boolean;
2b73cf68
JM
18559 -- The record type has not been introduced in the current scope
18560 -- yet, so we must examine the name of the type itself, either
18561 -- an identifier T, or an expanded name of the form P.T, where
18562 -- P denotes the current scope.
18563
dc06abec
RD
18564 -------------
18565 -- Names_T --
18566 -------------
18567
2b73cf68
JM
18568 function Names_T (Nam : Node_Id) return Boolean is
18569 begin
18570 if Nkind (Nam) = N_Identifier then
18571 return Chars (Nam) = Type_Id;
18572
18573 elsif Nkind (Nam) = N_Selected_Component then
18574 if Chars (Selector_Name (Nam)) = Type_Id then
18575 if Nkind (Prefix (Nam)) = N_Identifier then
18576 return Chars (Prefix (Nam)) = Chars (Current_Scope);
18577
18578 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
dc06abec
RD
18579 return Chars (Selector_Name (Prefix (Nam))) =
18580 Chars (Current_Scope);
2b73cf68
JM
18581 else
18582 return False;
18583 end if;
7d7af38a 18584
2b73cf68
JM
18585 else
18586 return False;
18587 end if;
7d7af38a 18588
2b73cf68
JM
18589 else
18590 return False;
18591 end if;
18592 end Names_T;
18593
5320014a 18594 -- Start of processing for Designates_T
dc06abec 18595
fea9e956 18596 begin
5320014a
ST
18597 if Nkind (Subt) = N_Identifier then
18598 return Chars (Subt) = Type_Id;
88b32fc3 18599
fea9e956 18600 -- Reference can be through an expanded name which has not been
2b73cf68 18601 -- analyzed yet, and which designates enclosing scopes.
88b32fc3 18602
5320014a
ST
18603 elsif Nkind (Subt) = N_Selected_Component then
18604 if Names_T (Subt) then
18605 return True;
88b32fc3 18606
5320014a
ST
18607 -- Otherwise it must denote an entity that is already visible.
18608 -- The access definition may name a subtype of the enclosing
18609 -- type, if there is a previous incomplete declaration for it.
fea9e956 18610
fea9e956 18611 else
5320014a
ST
18612 Find_Selected_Component (Subt);
18613 return
18614 Is_Entity_Name (Subt)
18615 and then Scope (Entity (Subt)) = Current_Scope
18616 and then
18617 (Chars (Base_Type (Entity (Subt))) = Type_Id
18618 or else
18619 (Is_Class_Wide_Type (Entity (Subt))
18620 and then
7d7af38a
JM
18621 Chars (Etype (Base_Type (Entity (Subt)))) =
18622 Type_Id));
758c442c
GD
18623 end if;
18624
5320014a
ST
18625 -- A reference to the current type may appear as the prefix of
18626 -- a 'Class attribute.
18627
18628 elsif Nkind (Subt) = N_Attribute_Reference
18629 and then Attribute_Name (Subt) = Name_Class
18630 then
18631 return Names_T (Prefix (Subt));
18632
fea9e956 18633 else
5320014a
ST
18634 return False;
18635 end if;
18636 end Designates_T;
758c442c 18637
5320014a
ST
18638 ----------------
18639 -- Mentions_T --
18640 ----------------
fea9e956 18641
5320014a
ST
18642 function Mentions_T (Acc_Def : Node_Id) return Boolean is
18643 Param_Spec : Node_Id;
fea9e956 18644
5320014a 18645 Acc_Subprg : constant Node_Id :=
7d7af38a 18646 Access_To_Subprogram_Definition (Acc_Def);
fea9e956 18647
5320014a
ST
18648 begin
18649 if No (Acc_Subprg) then
18650 return Designates_T (Subtype_Mark (Acc_Def));
758c442c 18651 end if;
5320014a
ST
18652
18653 -- Component is an access_to_subprogram: examine its formals,
18654 -- and result definition in the case of an access_to_function.
18655
18656 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
18657 while Present (Param_Spec) loop
18658 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
18659 and then Mentions_T (Parameter_Type (Param_Spec))
18660 then
18661 return True;
18662
18663 elsif Designates_T (Parameter_Type (Param_Spec)) then
18664 return True;
18665 end if;
18666
18667 Next (Param_Spec);
18668 end loop;
18669
18670 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
18671 if Nkind (Result_Definition (Acc_Subprg)) =
18672 N_Access_Definition
18673 then
18674 return Mentions_T (Result_Definition (Acc_Subprg));
18675 else
18676 return Designates_T (Result_Definition (Acc_Subprg));
18677 end if;
18678 end if;
18679
18680 return False;
fea9e956 18681 end Mentions_T;
996ae0b0 18682
fea9e956 18683 -- Start of processing for Check_Anonymous_Access_Components
758c442c 18684
fea9e956
ES
18685 begin
18686 if No (Comp_List) then
18687 return;
18688 end if;
758c442c 18689
fea9e956
ES
18690 Comp := First (Component_Items (Comp_List));
18691 while Present (Comp) loop
18692 if Nkind (Comp) = N_Component_Declaration
18693 and then Present
18694 (Access_Definition (Component_Definition (Comp)))
18695 and then
18696 Mentions_T (Access_Definition (Component_Definition (Comp)))
57193e09 18697 then
fea9e956
ES
18698 Comp_Def := Component_Definition (Comp);
18699 Acc_Def :=
18700 Access_To_Subprogram_Definition
18701 (Access_Definition (Comp_Def));
758c442c 18702
fea9e956 18703 Build_Incomplete_Type_Declaration;
092ef350 18704 Anon_Access := Make_Temporary (Loc, 'S');
758c442c 18705
fea9e956
ES
18706 -- Create a declaration for the anonymous access type: either
18707 -- an access_to_object or an access_to_subprogram.
758c442c 18708
fea9e956
ES
18709 if Present (Acc_Def) then
18710 if Nkind (Acc_Def) = N_Access_Function_Definition then
18711 Type_Def :=
18712 Make_Access_Function_Definition (Loc,
18713 Parameter_Specifications =>
18714 Parameter_Specifications (Acc_Def),
18715 Result_Definition => Result_Definition (Acc_Def));
18716 else
18717 Type_Def :=
18718 Make_Access_Procedure_Definition (Loc,
18719 Parameter_Specifications =>
18720 Parameter_Specifications (Acc_Def));
18721 end if;
758c442c 18722
758c442c 18723 else
fea9e956
ES
18724 Type_Def :=
18725 Make_Access_To_Object_Definition (Loc,
18726 Subtype_Indication =>
18727 Relocate_Node
18728 (Subtype_Mark
18729 (Access_Definition (Comp_Def))));
2b73cf68
JM
18730
18731 Set_Constant_Present
18732 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
18733 Set_All_Present
18734 (Type_Def, All_Present (Access_Definition (Comp_Def)));
758c442c
GD
18735 end if;
18736
2b73cf68
JM
18737 Set_Null_Exclusion_Present
18738 (Type_Def,
18739 Null_Exclusion_Present (Access_Definition (Comp_Def)));
18740
18741 Decl :=
18742 Make_Full_Type_Declaration (Loc,
18743 Defining_Identifier => Anon_Access,
18744 Type_Definition => Type_Def);
fea9e956
ES
18745
18746 Insert_Before (Typ_Decl, Decl);
758c442c 18747 Analyze (Decl);
758c442c 18748
fea9e956
ES
18749 -- If an access to object, Preserve entity of designated type,
18750 -- for ASIS use, before rewriting the component definition.
18751
18752 if No (Acc_Def) then
18753 declare
18754 Desig : Entity_Id;
18755
18756 begin
18757 Desig := Entity (Subtype_Indication (Type_Def));
18758
18759 -- If the access definition is to the current record,
18760 -- the visible entity at this point is an incomplete
18761 -- type. Retrieve the full view to simplify ASIS queries
18762
18763 if Ekind (Desig) = E_Incomplete_Type then
18764 Desig := Full_View (Desig);
18765 end if;
18766
18767 Set_Entity
18768 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
18769 end;
758c442c 18770 end if;
fea9e956
ES
18771
18772 Rewrite (Comp_Def,
18773 Make_Component_Definition (Loc,
18774 Subtype_Indication =>
18775 New_Occurrence_Of (Anon_Access, Loc)));
5320014a
ST
18776
18777 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
18778 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
18779 else
18780 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
18781 end if;
18782
fea9e956 18783 Set_Is_Local_Anonymous_Access (Anon_Access);
758c442c 18784 end if;
758c442c 18785
fea9e956
ES
18786 Next (Comp);
18787 end loop;
18788
18789 if Present (Variant_Part (Comp_List)) then
18790 declare
18791 V : Node_Id;
18792 begin
18793 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
18794 while Present (V) loop
18795 Check_Anonymous_Access_Components
18796 (Typ_Decl, Typ, Prev, Component_List (V));
18797 Next_Non_Pragma (V);
18798 end loop;
18799 end;
18800 end if;
18801 end Check_Anonymous_Access_Components;
18802
ce4a6e84
RD
18803 --------------------------------
18804 -- Preanalyze_Spec_Expression --
18805 --------------------------------
18806
18807 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18808 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18809 begin
18810 In_Spec_Expression := True;
18811 Preanalyze_And_Resolve (N, T);
18812 In_Spec_Expression := Save_In_Spec_Expression;
18813 end Preanalyze_Spec_Expression;
18814
fea9e956
ES
18815 -----------------------------
18816 -- Record_Type_Declaration --
18817 -----------------------------
18818
18819 procedure Record_Type_Declaration
18820 (T : Entity_Id;
18821 N : Node_Id;
18822 Prev : Entity_Id)
18823 is
18824 Def : constant Node_Id := Type_Definition (N);
18825 Is_Tagged : Boolean;
18826 Tag_Comp : Entity_Id;
758c442c
GD
18827
18828 begin
996ae0b0
RK
18829 -- These flags must be initialized before calling Process_Discriminants
18830 -- because this routine makes use of them.
18831
ce2b6ba5
JM
18832 Set_Ekind (T, E_Record_Type);
18833 Set_Etype (T, T);
18834 Init_Size_Align (T);
18835 Set_Interfaces (T, No_Elist);
18836 Set_Stored_Constraint (T, No_Elist);
996ae0b0 18837
758c442c 18838 -- Normal case
996ae0b0 18839
0791fbe9 18840 if Ada_Version < Ada_2005
758c442c
GD
18841 or else not Interface_Present (Def)
18842 then
18843 -- The flag Is_Tagged_Type might have already been set by
18844 -- Find_Type_Name if it detected an error for declaration T. This
18845 -- arises in the case of private tagged types where the full view
18846 -- omits the word tagged.
996ae0b0 18847
758c442c
GD
18848 Is_Tagged :=
18849 Tagged_Present (Def)
18850 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
996ae0b0 18851
758c442c
GD
18852 Set_Is_Tagged_Type (T, Is_Tagged);
18853 Set_Is_Limited_Record (T, Limited_Present (Def));
18854
18855 -- Type is abstract if full declaration carries keyword, or if
18856 -- previous partial view did.
18857
fea9e956 18858 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
758c442c
GD
18859 or else Abstract_Present (Def));
18860
18861 else
18862 Is_Tagged := True;
950d3e7d 18863 Analyze_Interface_Declaration (T, Def);
88b32fc3
BD
18864
18865 if Present (Discriminant_Specifications (N)) then
18866 Error_Msg_N
18867 ("interface types cannot have discriminants",
18868 Defining_Identifier
18869 (First (Discriminant_Specifications (N))));
18870 end if;
758c442c
GD
18871 end if;
18872
18873 -- First pass: if there are self-referential access components,
18874 -- create the required anonymous access type declarations, and if
18875 -- need be an incomplete type declaration for T itself.
18876
fea9e956 18877 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
758c442c 18878
0791fbe9 18879 if Ada_Version >= Ada_2005
758c442c
GD
18880 and then Present (Interface_List (Def))
18881 then
ce2b6ba5 18882 Check_Interfaces (N, Def);
fea9e956 18883
758c442c 18884 declare
88b32fc3 18885 Ifaces_List : Elist_Id;
950d3e7d 18886
758c442c 18887 begin
88b32fc3
BD
18888 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
18889 -- already in the parents.
18890
ce2b6ba5
JM
18891 Collect_Interfaces
18892 (T => T,
18893 Ifaces_List => Ifaces_List,
18894 Exclude_Parents => True);
88b32fc3 18895
ce2b6ba5 18896 Set_Interfaces (T, Ifaces_List);
758c442c
GD
18897 end;
18898 end if;
18899
18900 -- Records constitute a scope for the component declarations within.
18901 -- The scope is created prior to the processing of these declarations.
18902 -- Discriminants are processed first, so that they are visible when
18903 -- processing the other components. The Ekind of the record type itself
18904 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
18905
18906 -- Enter record scope
18907
2b73cf68 18908 Push_Scope (T);
996ae0b0
RK
18909
18910 -- If an incomplete or private type declaration was already given for
18911 -- the type, then this scope already exists, and the discriminants have
18912 -- been declared within. We must verify that the full declaration
18913 -- matches the incomplete one.
18914
fbf5a39b 18915 Check_Or_Process_Discriminants (N, T, Prev);
996ae0b0
RK
18916
18917 Set_Is_Constrained (T, not Has_Discriminants (T));
18918 Set_Has_Delayed_Freeze (T, True);
18919
18920 -- For tagged types add a manually analyzed component corresponding
18921 -- to the component _tag, the corresponding piece of tree will be
18922 -- expanded as part of the freezing actions if it is not a CPP_Class.
18923
18924 if Is_Tagged then
ffe9aba8
AC
18925
18926 -- Do not add the tag unless we are in expansion mode
996ae0b0
RK
18927
18928 if Expander_Active then
18929 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
18930 Enter_Name (Tag_Comp);
18931
7d7af38a 18932 Set_Ekind (Tag_Comp, E_Component);
996ae0b0 18933 Set_Is_Tag (Tag_Comp);
758c442c 18934 Set_Is_Aliased (Tag_Comp);
996ae0b0
RK
18935 Set_Etype (Tag_Comp, RTE (RE_Tag));
18936 Set_DT_Entry_Count (Tag_Comp, No_Uint);
18937 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
18938 Init_Component_Location (Tag_Comp);
758c442c
GD
18939
18940 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
fea9e956 18941 -- implemented interfaces.
758c442c 18942
ce2b6ba5 18943 if Has_Interfaces (T) then
fea9e956
ES
18944 Add_Interface_Tag_Components (N, T);
18945 end if;
996ae0b0
RK
18946 end if;
18947
18948 Make_Class_Wide_Type (T);
ef2a63ba 18949 Set_Direct_Primitive_Operations (T, New_Elmt_List);
996ae0b0
RK
18950 end if;
18951
ef2a63ba
JM
18952 -- We must suppress range checks when processing record components in
18953 -- the presence of discriminants, since we don't want spurious checks to
18954 -- be generated during their analysis, but Suppress_Range_Checks flags
18955 -- must be reset the after processing the record definition.
996ae0b0 18956
88b32fc3
BD
18957 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
18958 -- couldn't we just use the normal range check suppression method here.
18959 -- That would seem cleaner ???
18960
fbf5a39b
AC
18961 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
18962 Set_Kill_Range_Checks (T, True);
18963 Record_Type_Definition (Def, Prev);
18964 Set_Kill_Range_Checks (T, False);
18965 else
18966 Record_Type_Definition (Def, Prev);
996ae0b0
RK
18967 end if;
18968
18969 -- Exit from record scope
18970
18971 End_Scope;
758c442c 18972
88b32fc3
BD
18973 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
18974 -- the implemented interfaces and associate them an aliased entity.
18975
18976 if Is_Tagged
758c442c
GD
18977 and then not Is_Empty_List (Interface_List (Def))
18978 then
ce2b6ba5 18979 Derive_Progenitor_Subprograms (T, T);
758c442c 18980 end if;
996ae0b0
RK
18981 end Record_Type_Declaration;
18982
18983 ----------------------------
18984 -- Record_Type_Definition --
18985 ----------------------------
18986
fbf5a39b 18987 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
996ae0b0
RK
18988 Component : Entity_Id;
18989 Ctrl_Components : Boolean := False;
fbf5a39b
AC
18990 Final_Storage_Only : Boolean;
18991 T : Entity_Id;
996ae0b0
RK
18992
18993 begin
fbf5a39b
AC
18994 if Ekind (Prev_T) = E_Incomplete_Type then
18995 T := Full_View (Prev_T);
18996 else
18997 T := Prev_T;
18998 end if;
18999
19000 Final_Storage_Only := not Is_Controlled (T);
19001
57193e09
TQ
19002 -- Ada 2005: check whether an explicit Limited is present in a derived
19003 -- type declaration.
19004
19005 if Nkind (Parent (Def)) = N_Derived_Type_Definition
19006 and then Limited_Present (Parent (Def))
19007 then
19008 Set_Is_Limited_Record (T);
19009 end if;
19010
996ae0b0
RK
19011 -- If the component list of a record type is defined by the reserved
19012 -- word null and there is no discriminant part, then the record type has
19013 -- no components and all records of the type are null records (RM 3.7)
19014 -- This procedure is also called to process the extension part of a
19015 -- record extension, in which case the current scope may have inherited
19016 -- components.
19017
19018 if No (Def)
19019 or else No (Component_List (Def))
19020 or else Null_Present (Component_List (Def))
19021 then
19022 null;
19023
19024 else
19025 Analyze_Declarations (Component_Items (Component_List (Def)));
19026
19027 if Present (Variant_Part (Component_List (Def))) then
19028 Analyze (Variant_Part (Component_List (Def)));
19029 end if;
19030 end if;
19031
19032 -- After completing the semantic analysis of the record definition,
fea9e956 19033 -- record components, both new and inherited, are accessible. Set their
2b73cf68
JM
19034 -- kind accordingly. Exclude malformed itypes from illegal declarations,
19035 -- whose Ekind may be void.
996ae0b0
RK
19036
19037 Component := First_Entity (Current_Scope);
19038 while Present (Component) loop
2b73cf68
JM
19039 if Ekind (Component) = E_Void
19040 and then not Is_Itype (Component)
19041 then
996ae0b0
RK
19042 Set_Ekind (Component, E_Component);
19043 Init_Component_Location (Component);
19044 end if;
19045
19046 if Has_Task (Etype (Component)) then
19047 Set_Has_Task (T);
19048 end if;
19049
19050 if Ekind (Component) /= E_Component then
19051 null;
19052
80fa4617
EB
19053 -- Do not set Has_Controlled_Component on a class-wide equivalent
19054 -- type. See Make_CW_Equivalent_Type.
19055
19056 elsif not Is_Class_Wide_Equivalent_Type (T)
19057 and then (Has_Controlled_Component (Etype (Component))
19058 or else (Chars (Component) /= Name_uParent
19059 and then Is_Controlled (Etype (Component))))
996ae0b0
RK
19060 then
19061 Set_Has_Controlled_Component (T, True);
33931112
JM
19062 Final_Storage_Only :=
19063 Final_Storage_Only
19064 and then Finalize_Storage_Only (Etype (Component));
996ae0b0
RK
19065 Ctrl_Components := True;
19066 end if;
19067
19068 Next_Entity (Component);
19069 end loop;
19070
fea9e956
ES
19071 -- A Type is Finalize_Storage_Only only if all its controlled components
19072 -- are also.
996ae0b0
RK
19073
19074 if Ctrl_Components then
19075 Set_Finalize_Storage_Only (T, Final_Storage_Only);
19076 end if;
19077
fbf5a39b
AC
19078 -- Place reference to end record on the proper entity, which may
19079 -- be a partial view.
19080
996ae0b0 19081 if Present (Def) then
fbf5a39b 19082 Process_End_Label (Def, 'e', Prev_T);
996ae0b0
RK
19083 end if;
19084 end Record_Type_Definition;
19085
07fc65c4
GB
19086 ------------------------
19087 -- Replace_Components --
19088 ------------------------
19089
19090 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
19091 function Process (N : Node_Id) return Traverse_Result;
19092
19093 -------------
19094 -- Process --
19095 -------------
19096
19097 function Process (N : Node_Id) return Traverse_Result is
19098 Comp : Entity_Id;
19099
19100 begin
19101 if Nkind (N) = N_Discriminant_Specification then
19102 Comp := First_Discriminant (Typ);
07fc65c4
GB
19103 while Present (Comp) loop
19104 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19105 Set_Defining_Identifier (N, Comp);
19106 exit;
19107 end if;
19108
19109 Next_Discriminant (Comp);
19110 end loop;
19111
19112 elsif Nkind (N) = N_Component_Declaration then
19113 Comp := First_Component (Typ);
07fc65c4
GB
19114 while Present (Comp) loop
19115 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19116 Set_Defining_Identifier (N, Comp);
19117 exit;
19118 end if;
19119
19120 Next_Component (Comp);
19121 end loop;
19122 end if;
19123
19124 return OK;
19125 end Process;
19126
19127 procedure Replace is new Traverse_Proc (Process);
19128
19129 -- Start of processing for Replace_Components
19130
19131 begin
19132 Replace (Decl);
19133 end Replace_Components;
19134
19135 -------------------------------
19136 -- Set_Completion_Referenced --
19137 -------------------------------
19138
19139 procedure Set_Completion_Referenced (E : Entity_Id) is
19140 begin
19141 -- If in main unit, mark entity that is a completion as referenced,
19142 -- warnings go on the partial view when needed.
19143
19144 if In_Extended_Main_Source_Unit (E) then
19145 Set_Referenced (E);
19146 end if;
19147 end Set_Completion_Referenced;
19148
996ae0b0
RK
19149 ---------------------
19150 -- Set_Fixed_Range --
19151 ---------------------
19152
19153 -- The range for fixed-point types is complicated by the fact that we
19154 -- do not know the exact end points at the time of the declaration. This
19155 -- is true for three reasons:
19156
19157 -- A size clause may affect the fudging of the end-points
19158 -- A small clause may affect the values of the end-points
19159 -- We try to include the end-points if it does not affect the size
19160
a5b62485
AC
19161 -- This means that the actual end-points must be established at the point
19162 -- when the type is frozen. Meanwhile, we first narrow the range as
19163 -- permitted (so that it will fit if necessary in a small specified size),
19164 -- and then build a range subtree with these narrowed bounds.
996ae0b0 19165
a5b62485
AC
19166 -- Set_Fixed_Range constructs the range from real literal values, and sets
19167 -- the range as the Scalar_Range of the given fixed-point type entity.
996ae0b0 19168
a5b62485
AC
19169 -- The parent of this range is set to point to the entity so that it is
19170 -- properly hooked into the tree (unlike normal Scalar_Range entries for
19171 -- other scalar types, which are just pointers to the range in the
996ae0b0
RK
19172 -- original tree, this would otherwise be an orphan).
19173
19174 -- The tree is left unanalyzed. When the type is frozen, the processing
19175 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
19176 -- analyzed, and uses this as an indication that it should complete
19177 -- work on the range (it will know the final small and size values).
19178
19179 procedure Set_Fixed_Range
19180 (E : Entity_Id;
19181 Loc : Source_Ptr;
19182 Lo : Ureal;
19183 Hi : Ureal)
19184 is
19185 S : constant Node_Id :=
19186 Make_Range (Loc,
19187 Low_Bound => Make_Real_Literal (Loc, Lo),
19188 High_Bound => Make_Real_Literal (Loc, Hi));
996ae0b0
RK
19189 begin
19190 Set_Scalar_Range (E, S);
19191 Set_Parent (S, E);
19192 end Set_Fixed_Range;
19193
996ae0b0
RK
19194 ----------------------------------
19195 -- Set_Scalar_Range_For_Subtype --
19196 ----------------------------------
19197
19198 procedure Set_Scalar_Range_For_Subtype
07fc65c4
GB
19199 (Def_Id : Entity_Id;
19200 R : Node_Id;
19201 Subt : Entity_Id)
996ae0b0
RK
19202 is
19203 Kind : constant Entity_Kind := Ekind (Def_Id);
71d9e9f2 19204
996ae0b0 19205 begin
199c6a10
AC
19206 -- Defend against previous error
19207
19208 if Nkind (R) = N_Error then
19209 return;
19210 end if;
19211
996ae0b0
RK
19212 Set_Scalar_Range (Def_Id, R);
19213
19214 -- We need to link the range into the tree before resolving it so
19215 -- that types that are referenced, including importantly the subtype
19216 -- itself, are properly frozen (Freeze_Expression requires that the
19217 -- expression be properly linked into the tree). Of course if it is
19218 -- already linked in, then we do not disturb the current link.
19219
19220 if No (Parent (R)) then
19221 Set_Parent (R, Def_Id);
19222 end if;
19223
19224 -- Reset the kind of the subtype during analysis of the range, to
19225 -- catch possible premature use in the bounds themselves.
19226
19227 Set_Ekind (Def_Id, E_Void);
07fc65c4 19228 Process_Range_Expr_In_Decl (R, Subt);
996ae0b0 19229 Set_Ekind (Def_Id, Kind);
996ae0b0
RK
19230 end Set_Scalar_Range_For_Subtype;
19231
fbf5a39b
AC
19232 --------------------------------------------------------
19233 -- Set_Stored_Constraint_From_Discriminant_Constraint --
19234 --------------------------------------------------------
19235
19236 procedure Set_Stored_Constraint_From_Discriminant_Constraint
19237 (E : Entity_Id)
19238 is
19239 begin
71d9e9f2 19240 -- Make sure set if encountered during Expand_To_Stored_Constraint
fbf5a39b
AC
19241
19242 Set_Stored_Constraint (E, No_Elist);
19243
19244 -- Give it the right value
19245
19246 if Is_Constrained (E) and then Has_Discriminants (E) then
19247 Set_Stored_Constraint (E,
19248 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
19249 end if;
fbf5a39b
AC
19250 end Set_Stored_Constraint_From_Discriminant_Constraint;
19251
996ae0b0
RK
19252 -------------------------------------
19253 -- Signed_Integer_Type_Declaration --
19254 -------------------------------------
19255
19256 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19257 Implicit_Base : Entity_Id;
19258 Base_Typ : Entity_Id;
19259 Lo_Val : Uint;
19260 Hi_Val : Uint;
19261 Errs : Boolean := False;
19262 Lo : Node_Id;
19263 Hi : Node_Id;
19264
19265 function Can_Derive_From (E : Entity_Id) return Boolean;
19266 -- Determine whether given bounds allow derivation from specified type
19267
19268 procedure Check_Bound (Expr : Node_Id);
19269 -- Check bound to make sure it is integral and static. If not, post
19270 -- appropriate error message and set Errs flag
19271
fbf5a39b
AC
19272 ---------------------
19273 -- Can_Derive_From --
19274 ---------------------
19275
71d9e9f2
ES
19276 -- Note we check both bounds against both end values, to deal with
19277 -- strange types like ones with a range of 0 .. -12341234.
19278
996ae0b0
RK
19279 function Can_Derive_From (E : Entity_Id) return Boolean is
19280 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
19281 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
996ae0b0 19282 begin
996ae0b0
RK
19283 return Lo <= Lo_Val and then Lo_Val <= Hi
19284 and then
19285 Lo <= Hi_Val and then Hi_Val <= Hi;
19286 end Can_Derive_From;
19287
fbf5a39b
AC
19288 -----------------
19289 -- Check_Bound --
19290 -----------------
19291
996ae0b0
RK
19292 procedure Check_Bound (Expr : Node_Id) is
19293 begin
19294 -- If a range constraint is used as an integer type definition, each
19295 -- bound of the range must be defined by a static expression of some
19296 -- integer type, but the two bounds need not have the same integer
19297 -- type (Negative bounds are allowed.) (RM 3.5.4)
19298
19299 if not Is_Integer_Type (Etype (Expr)) then
19300 Error_Msg_N
19301 ("integer type definition bounds must be of integer type", Expr);
19302 Errs := True;
19303
19304 elsif not Is_OK_Static_Expression (Expr) then
fbf5a39b
AC
19305 Flag_Non_Static_Expr
19306 ("non-static expression used for integer type bound!", Expr);
996ae0b0
RK
19307 Errs := True;
19308
19309 -- The bounds are folded into literals, and we set their type to be
19310 -- universal, to avoid typing difficulties: we cannot set the type
19311 -- of the literal to the new type, because this would be a forward
19312 -- reference for the back end, and if the original type is user-
19313 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
19314
19315 else
19316 if Is_Entity_Name (Expr) then
fbf5a39b 19317 Fold_Uint (Expr, Expr_Value (Expr), True);
996ae0b0
RK
19318 end if;
19319
19320 Set_Etype (Expr, Universal_Integer);
19321 end if;
19322 end Check_Bound;
19323
19324 -- Start of processing for Signed_Integer_Type_Declaration
19325
19326 begin
19327 -- Create an anonymous base type
19328
19329 Implicit_Base :=
19330 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
19331
19332 -- Analyze and check the bounds, they can be of any integer type
19333
19334 Lo := Low_Bound (Def);
19335 Hi := High_Bound (Def);
996ae0b0 19336
ce9e9122 19337 -- Arbitrarily use Integer as the type if either bound had an error
996ae0b0 19338
ce9e9122
RD
19339 if Hi = Error or else Lo = Error then
19340 Base_Typ := Any_Integer;
19341 Set_Error_Posted (T, True);
996ae0b0 19342
ce9e9122 19343 -- Here both bounds are OK expressions
996ae0b0 19344
ce9e9122
RD
19345 else
19346 Analyze_And_Resolve (Lo, Any_Integer);
19347 Analyze_And_Resolve (Hi, Any_Integer);
996ae0b0 19348
ce9e9122
RD
19349 Check_Bound (Lo);
19350 Check_Bound (Hi);
996ae0b0 19351
ce9e9122
RD
19352 if Errs then
19353 Hi := Type_High_Bound (Standard_Long_Long_Integer);
19354 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
19355 end if;
996ae0b0 19356
ce9e9122 19357 -- Find type to derive from
996ae0b0 19358
ce9e9122
RD
19359 Lo_Val := Expr_Value (Lo);
19360 Hi_Val := Expr_Value (Hi);
996ae0b0 19361
ce9e9122
RD
19362 if Can_Derive_From (Standard_Short_Short_Integer) then
19363 Base_Typ := Base_Type (Standard_Short_Short_Integer);
996ae0b0 19364
ce9e9122
RD
19365 elsif Can_Derive_From (Standard_Short_Integer) then
19366 Base_Typ := Base_Type (Standard_Short_Integer);
19367
19368 elsif Can_Derive_From (Standard_Integer) then
19369 Base_Typ := Base_Type (Standard_Integer);
19370
19371 elsif Can_Derive_From (Standard_Long_Integer) then
19372 Base_Typ := Base_Type (Standard_Long_Integer);
19373
19374 elsif Can_Derive_From (Standard_Long_Long_Integer) then
19375 Base_Typ := Base_Type (Standard_Long_Long_Integer);
19376
19377 else
19378 Base_Typ := Base_Type (Standard_Long_Long_Integer);
19379 Error_Msg_N ("integer type definition bounds out of range", Def);
19380 Hi := Type_High_Bound (Standard_Long_Long_Integer);
19381 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
19382 end if;
996ae0b0
RK
19383 end if;
19384
19385 -- Complete both implicit base and declared first subtype entities
19386
19387 Set_Etype (Implicit_Base, Base_Typ);
19388 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
19389 Set_Size_Info (Implicit_Base, (Base_Typ));
19390 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
19391 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
19392
19393 Set_Ekind (T, E_Signed_Integer_Subtype);
19394 Set_Etype (T, Implicit_Base);
19395
19396 Set_Size_Info (T, (Implicit_Base));
19397 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
19398 Set_Scalar_Range (T, Def);
19399 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
19400 Set_Is_Constrained (T);
996ae0b0
RK
19401 end Signed_Integer_Type_Declaration;
19402
19403end Sem_Ch3;
This page took 6.67129 seconds and 5 git commands to generate.