]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_ch3.adb
[multiple changes]
[thirdparty/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-- --
d3820795 9-- Copyright (C) 1992-2013, 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
9479ded4 26with Aspects; use Aspects;
996ae0b0
RK
27with Atree; use Atree;
28with Checks; use Checks;
88b32fc3 29with Debug; use Debug;
996ae0b0
RK
30with Elists; use Elists;
31with Einfo; use Einfo;
32with Errout; use Errout;
33with Eval_Fat; use Eval_Fat;
34with Exp_Ch3; use Exp_Ch3;
d44202ba 35with Exp_Ch9; use Exp_Ch9;
ce2b6ba5 36with Exp_Disp; use Exp_Disp;
996ae0b0 37with Exp_Dist; use Exp_Dist;
3b1d4d82 38with Exp_Pakd; use Exp_Pakd;
fbf5a39b 39with Exp_Tss; use Exp_Tss;
996ae0b0 40with Exp_Util; use Exp_Util;
9c510803 41with Fname; use Fname;
996ae0b0
RK
42with Freeze; use Freeze;
43with Itypes; use Itypes;
44with Layout; use Layout;
45with Lib; use Lib;
46with Lib.Xref; use Lib.Xref;
47with Namet; use Namet;
48with Nmake; use Nmake;
49with Opt; use Opt;
50with Restrict; use Restrict;
6e937c1c 51with Rident; use Rident;
996ae0b0
RK
52with Rtsfind; use Rtsfind;
53with Sem; use Sem;
a4100e55 54with Sem_Aux; use Sem_Aux;
996ae0b0
RK
55with Sem_Case; use Sem_Case;
56with Sem_Cat; use Sem_Cat;
57with Sem_Ch6; use Sem_Ch6;
58with Sem_Ch7; use Sem_Ch7;
59with Sem_Ch8; use Sem_Ch8;
60with Sem_Ch13; use Sem_Ch13;
dec6faf1 61with Sem_Dim; use Sem_Dim;
996ae0b0
RK
62with Sem_Disp; use Sem_Disp;
63with Sem_Dist; use Sem_Dist;
64with Sem_Elim; use Sem_Elim;
65with Sem_Eval; use Sem_Eval;
66with Sem_Mech; use Sem_Mech;
39af2bac 67with Sem_Prag; use Sem_Prag;
996ae0b0
RK
68with Sem_Res; use Sem_Res;
69with Sem_Smem; use Sem_Smem;
70with Sem_Type; use Sem_Type;
71with Sem_Util; use Sem_Util;
fbf5a39b 72with Sem_Warn; use Sem_Warn;
996ae0b0
RK
73with Stand; use Stand;
74with Sinfo; use Sinfo;
b4d7b435 75with Sinput; use Sinput;
996ae0b0 76with Snames; use Snames;
653da906 77with Targparm; use Targparm;
996ae0b0
RK
78with Tbuild; use Tbuild;
79with Ttypes; use Ttypes;
80with Uintp; use Uintp;
81with Urealp; use Urealp;
82
83package body Sem_Ch3 is
84
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
88
88b32fc3 89 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
758c442c
GD
90 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
91 -- abstract interface types implemented by a record type or a derived
92 -- record type.
93
f1bd0415
AC
94 procedure Analyze_Object_Contract (Obj_Id : Entity_Id);
95 -- Analyze all delayed aspects chained on the contract of object Obj_Id as
96 -- if they appeared at the end of the declarative region. The aspects to be
97 -- considered are:
6c3c671e
AC
98 -- Async_Readers
99 -- Async_Writers
100 -- Effective_Reads
101 -- Effective_Writes
d7af5ea5 102 -- Part_Of
6c3c671e 103
996ae0b0
RK
104 procedure Build_Derived_Type
105 (N : Node_Id;
106 Parent_Type : Entity_Id;
107 Derived_Type : Entity_Id;
108 Is_Completion : Boolean;
109 Derive_Subps : Boolean := True);
9dfd2ff8
CC
110 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
111 -- the N_Full_Type_Declaration node containing the derived type definition.
112 -- Parent_Type is the entity for the parent type in the derived type
113 -- definition and Derived_Type the actual derived type. Is_Completion must
114 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
f3d57416 115 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
9dfd2ff8
CC
116 -- completion of a private type declaration. If Is_Completion is set to
117 -- True, N is the completion of a private type declaration and Derived_Type
118 -- is different from the defining identifier inside N (i.e. Derived_Type /=
119 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
120 -- subprograms should be derived. The only case where this parameter is
121 -- False is when Build_Derived_Type is recursively called to process an
122 -- implicit derived full type for a type derived from a private type (in
123 -- that case the subprograms must only be derived for the private view of
124 -- the type).
fea9e956 125 --
44d6a706 126 -- ??? These flags need a bit of re-examination and re-documentation:
996ae0b0
RK
127 -- ??? are they both necessary (both seem related to the recursion)?
128
129 procedure Build_Derived_Access_Type
130 (N : Node_Id;
131 Parent_Type : Entity_Id;
132 Derived_Type : Entity_Id);
133 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
134 -- create an implicit base if the parent type is constrained or if the
135 -- subtype indication has a constraint.
136
137 procedure Build_Derived_Array_Type
138 (N : Node_Id;
139 Parent_Type : Entity_Id;
140 Derived_Type : Entity_Id);
141 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
142 -- create an implicit base if the parent type is constrained or if the
143 -- subtype indication has a constraint.
144
145 procedure Build_Derived_Concurrent_Type
146 (N : Node_Id;
147 Parent_Type : Entity_Id;
148 Derived_Type : Entity_Id);
88b32fc3
BD
149 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
150 -- protected type, inherit entries and protected subprograms, check
151 -- legality of discriminant constraints if any.
996ae0b0
RK
152
153 procedure Build_Derived_Enumeration_Type
154 (N : Node_Id;
155 Parent_Type : Entity_Id;
156 Derived_Type : Entity_Id);
157 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
158 -- type, we must create a new list of literals. Types derived from
94fd3dc6 159 -- Character and [Wide_]Wide_Character are special-cased.
996ae0b0
RK
160
161 procedure Build_Derived_Numeric_Type
162 (N : Node_Id;
163 Parent_Type : Entity_Id;
164 Derived_Type : Entity_Id);
165 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
166 -- an anonymous base type, and propagate constraint to subtype if needed.
167
168 procedure Build_Derived_Private_Type
71d9e9f2
ES
169 (N : Node_Id;
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id;
996ae0b0
RK
172 Is_Completion : Boolean;
173 Derive_Subps : Boolean := True);
fbf5a39b 174 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
996ae0b0
RK
175 -- because the parent may or may not have a completion, and the derivation
176 -- may itself be a completion.
177
178 procedure Build_Derived_Record_Type
179 (N : Node_Id;
180 Parent_Type : Entity_Id;
181 Derived_Type : Entity_Id;
182 Derive_Subps : Boolean := True);
15e934bf
AC
183 -- Subsidiary procedure used for tagged and untagged record types
184 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
185 -- All parameters are as in Build_Derived_Type except that N, in
186 -- addition to being an N_Full_Type_Declaration node, can also be an
996ae0b0 187 -- N_Private_Extension_Declaration node. See the definition of this routine
15e934bf
AC
188 -- for much more info. Derive_Subps indicates whether subprograms should be
189 -- derived from the parent type. The only case where Derive_Subps is False
190 -- is for an implicit derived full type for a type derived from a private
191 -- type (see Build_Derived_Type).
996ae0b0 192
996ae0b0
RK
193 procedure Build_Discriminal (Discrim : Entity_Id);
194 -- Create the discriminal corresponding to discriminant Discrim, that is
195 -- the parameter corresponding to Discrim to be used in initialization
196 -- procedures for the type where Discrim is a discriminant. Discriminals
197 -- are not used during semantic analysis, and are not fully defined
198 -- entities until expansion. Thus they are not given a scope until
44d6a706 199 -- initialization procedures are built.
996ae0b0
RK
200
201 function Build_Discriminant_Constraints
202 (T : Entity_Id;
203 Def : Node_Id;
b0f26df5 204 Derived_Def : Boolean := False) return Elist_Id;
2b73cf68
JM
205 -- Validate discriminant constraints and return the list of the constraints
206 -- in order of discriminant declarations, where T is the discriminated
207 -- unconstrained type. Def is the N_Subtype_Indication node where the
208 -- discriminants constraints for T are specified. Derived_Def is True
209 -- when building the discriminant constraints in a derived type definition
210 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
211 -- type and Def is the constraint "(xxx)" on T and this routine sets the
212 -- Corresponding_Discriminant field of the discriminants in the derived
213 -- type D to point to the corresponding discriminants in the parent type T.
996ae0b0
RK
214
215 procedure Build_Discriminated_Subtype
216 (T : Entity_Id;
217 Def_Id : Entity_Id;
218 Elist : Elist_Id;
219 Related_Nod : Node_Id;
220 For_Access : Boolean := False);
221 -- Subsidiary procedure to Constrain_Discriminated_Type and to
222 -- Process_Incomplete_Dependents. Given
223 --
224 -- T (a possibly discriminated base type)
225 -- Def_Id (a very partially built subtype for T),
226 --
227 -- the call completes Def_Id to be the appropriate E_*_Subtype.
228 --
dc06abec
RD
229 -- The Elist is the list of discriminant constraints if any (it is set
230 -- to No_Elist if T is not a discriminated type, and to an empty list if
996ae0b0
RK
231 -- T has discriminants but there are no discriminant constraints). The
232 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
233 -- The For_Access says whether or not this subtype is really constraining
234 -- an access type. That is its sole purpose is the designated type of an
235 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
236 -- is built to avoid freezing T when the access subtype is frozen.
237
238 function Build_Scalar_Bound
239 (Bound : Node_Id;
240 Par_T : Entity_Id;
b0f26df5 241 Der_T : Entity_Id) return Node_Id;
996ae0b0
RK
242 -- The bounds of a derived scalar type are conversions of the bounds of
243 -- the parent type. Optimize the representation if the bounds are literals.
244 -- Needs a more complete spec--what are the parameters exactly, and what
245 -- exactly is the returned value, and how is Bound affected???
246
247 procedure Build_Underlying_Full_View
248 (N : Node_Id;
249 Typ : Entity_Id;
250 Par : Entity_Id);
251 -- If the completion of a private type is itself derived from a private
252 -- type, or if the full view of a private subtype is itself private, the
253 -- back-end has no way to compute the actual size of this type. We build
254 -- an internal subtype declaration of the proper parent type to convey
255 -- this information. This extra mechanism is needed because a full
256 -- view cannot itself have a full view (it would get clobbered during
257 -- view exchanges).
258
259 procedure Check_Access_Discriminant_Requires_Limited
260 (D : Node_Id;
261 Loc : Node_Id);
262 -- Check the restriction that the type to which an access discriminant
263 -- belongs must be a concurrent type or a descendant of a type with
264 -- the reserved word 'limited' in its declaration.
265
fea9e956
ES
266 procedure Check_Anonymous_Access_Components
267 (Typ_Decl : Node_Id;
268 Typ : Entity_Id;
269 Prev : Entity_Id;
270 Comp_List : Node_Id);
271 -- Ada 2005 AI-382: an access component in a record definition can refer to
272 -- the enclosing record, in which case it denotes the type itself, and not
273 -- the current instance of the type. We create an anonymous access type for
274 -- the component, and flag it as an access to a component, so accessibility
275 -- checks are properly performed on it. The declaration of the access type
276 -- is placed ahead of that of the record to prevent order-of-elaboration
277 -- circularity issues in Gigi. We create an incomplete type for the record
278 -- declaration, which is the designated type of the anonymous access.
279
996ae0b0 280 procedure Check_Delta_Expression (E : Node_Id);
fea9e956
ES
281 -- Check that the expression represented by E is suitable for use as a
282 -- delta expression, i.e. it is of real type and is static.
996ae0b0
RK
283
284 procedure Check_Digits_Expression (E : Node_Id);
fea9e956
ES
285 -- Check that the expression represented by E is suitable for use as a
286 -- digits expression, i.e. it is of integer type, positive and static.
996ae0b0 287
996ae0b0 288 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
fea9e956
ES
289 -- Validate the initialization of an object declaration. T is the required
290 -- type, and Exp is the initialization expression.
996ae0b0 291
ce2b6ba5
JM
292 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
293 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
294
fbf5a39b
AC
295 procedure Check_Or_Process_Discriminants
296 (N : Node_Id;
297 T : Entity_Id;
298 Prev : Entity_Id := Empty);
8e4dac80
TQ
299 -- If N is the full declaration of the completion T of an incomplete or
300 -- private type, check its discriminants (which are already known to be
301 -- conformant with those of the partial view, see Find_Type_Name),
302 -- otherwise process them. Prev is the entity of the partial declaration,
303 -- if any.
996ae0b0
RK
304
305 procedure Check_Real_Bound (Bound : Node_Id);
306 -- Check given bound for being of real type and static. If not, post an
307 -- appropriate message, and rewrite the bound with the real literal zero.
308
309 procedure Constant_Redeclaration
310 (Id : Entity_Id;
311 N : Node_Id;
312 T : out Entity_Id);
313 -- Various checks on legality of full declaration of deferred constant.
314 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
315 -- node. The caller has not yet set any attributes of this entity.
316
dc06abec
RD
317 function Contain_Interface
318 (Iface : Entity_Id;
319 Ifaces : Elist_Id) return Boolean;
320 -- Ada 2005: Determine whether Iface is present in the list Ifaces
321
996ae0b0
RK
322 procedure Convert_Scalar_Bounds
323 (N : Node_Id;
324 Parent_Type : Entity_Id;
325 Derived_Type : Entity_Id;
326 Loc : Source_Ptr);
fea9e956
ES
327 -- For derived scalar types, convert the bounds in the type definition to
328 -- the derived type, and complete their analysis. Given a constraint of the
329 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
330 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
331 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
332 -- subtype are conversions of those bounds to the derived_type, so that
333 -- their typing is consistent.
996ae0b0
RK
334
335 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
fea9e956
ES
336 -- Copies attributes from array base type T2 to array base type T1. Copies
337 -- only attributes that apply to base types, but not subtypes.
996ae0b0
RK
338
339 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
340 -- Copies attributes from array subtype T2 to array subtype T1. Copies
341 -- attributes that apply to both subtypes and base types.
342
343 procedure Create_Constrained_Components
344 (Subt : Entity_Id;
345 Decl_Node : Node_Id;
346 Typ : Entity_Id;
347 Constraints : Elist_Id);
348 -- Build the list of entities for a constrained discriminated record
349 -- subtype. If a component depends on a discriminant, replace its subtype
ce4a6e84
RD
350 -- using the discriminant values in the discriminant constraint. Subt
351 -- is the defining identifier for the subtype whose list of constrained
352 -- entities we will create. Decl_Node is the type declaration node where
353 -- we will attach all the itypes created. Typ is the base discriminated
354 -- type for the subtype Subt. Constraints is the list of discriminant
fea9e956 355 -- constraints for Typ.
996ae0b0
RK
356
357 function Constrain_Component_Type
c6823a20 358 (Comp : Entity_Id;
996ae0b0
RK
359 Constrained_Typ : Entity_Id;
360 Related_Node : Node_Id;
361 Typ : Entity_Id;
b0f26df5 362 Constraints : Elist_Id) return Entity_Id;
996ae0b0 363 -- Given a discriminated base type Typ, a list of discriminant constraint
c6823a20 364 -- Constraints for Typ and a component of Typ, with type Compon_Type,
996ae0b0 365 -- create and return the type corresponding to Compon_type where all
fea9e956
ES
366 -- discriminant references are replaced with the corresponding constraint.
367 -- If no discriminant references occur in Compon_Typ then return it as is.
368 -- Constrained_Typ is the final constrained subtype to which the
369 -- constrained Compon_Type belongs. Related_Node is the node where we will
370 -- attach all the itypes created.
ce4a6e84 371 --
fea9e956 372 -- Above description is confused, what is Compon_Type???
996ae0b0
RK
373
374 procedure Constrain_Access
375 (Def_Id : in out Entity_Id;
376 S : Node_Id;
377 Related_Nod : Node_Id);
9dfd2ff8
CC
378 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
379 -- an anonymous type created for a subtype indication. In that case it is
380 -- created in the procedure and attached to Related_Nod.
996ae0b0
RK
381
382 procedure Constrain_Array
383 (Def_Id : in out Entity_Id;
384 SI : Node_Id;
385 Related_Nod : Node_Id;
386 Related_Id : Entity_Id;
387 Suffix : Character);
388 -- Apply a list of index constraints to an unconstrained array type. The
389 -- first parameter is the entity for the resulting subtype. A value of
390 -- Empty for Def_Id indicates that an implicit type must be created, but
391 -- creation is delayed (and must be done by this procedure) because other
392 -- subsidiary implicit types must be created first (which is why Def_Id
07fc65c4
GB
393 -- is an in/out parameter). The second parameter is a subtype indication
394 -- node for the constrained array to be created (e.g. something of the
395 -- form string (1 .. 10)). Related_Nod gives the place where this type
396 -- has to be inserted in the tree. The Related_Id and Suffix parameters
397 -- are used to build the associated Implicit type name.
996ae0b0
RK
398
399 procedure Constrain_Concurrent
400 (Def_Id : in out Entity_Id;
401 SI : Node_Id;
402 Related_Nod : Node_Id;
403 Related_Id : Entity_Id;
404 Suffix : Character);
405 -- Apply list of discriminant constraints to an unconstrained concurrent
406 -- type.
407 --
408 -- SI is the N_Subtype_Indication node containing the constraint and
409 -- the unconstrained type to constrain.
410 --
a5b62485
AC
411 -- Def_Id is the entity for the resulting constrained subtype. A value
412 -- of Empty for Def_Id indicates that an implicit type must be created,
413 -- but creation is delayed (and must be done by this procedure) because
414 -- other subsidiary implicit types must be created first (which is why
415 -- Def_Id is an in/out parameter).
996ae0b0
RK
416 --
417 -- Related_Nod gives the place where this type has to be inserted
418 -- in the tree
419 --
420 -- The last two arguments are used to create its external name if needed.
421
422 function Constrain_Corresponding_Record
423 (Prot_Subt : Entity_Id;
424 Corr_Rec : Entity_Id;
425 Related_Nod : Node_Id;
b0f26df5 426 Related_Id : Entity_Id) return Entity_Id;
996ae0b0
RK
427 -- When constraining a protected type or task type with discriminants,
428 -- constrain the corresponding record with the same discriminant values.
429
07fc65c4 430 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
996ae0b0
RK
431 -- Constrain a decimal fixed point type with a digits constraint and/or a
432 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
433
434 procedure Constrain_Discriminated_Type
435 (Def_Id : Entity_Id;
436 S : Node_Id;
437 Related_Nod : Node_Id;
438 For_Access : Boolean := False);
439 -- Process discriminant constraints of composite type. Verify that values
440 -- have been provided for all discriminants, that the original type is
441 -- unconstrained, and that the types of the supplied expressions match
442 -- the discriminant types. The first three parameters are like in routine
fbf5a39b 443 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
996ae0b0
RK
444 -- of For_Access.
445
07fc65c4 446 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
9dfd2ff8
CC
447 -- Constrain an enumeration type with a range constraint. This is identical
448 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
996ae0b0 449
07fc65c4 450 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
996ae0b0
RK
451 -- Constrain a floating point type with either a digits constraint
452 -- and/or a range constraint, building a E_Floating_Point_Subtype.
453
454 procedure Constrain_Index
455 (Index : Node_Id;
456 S : Node_Id;
457 Related_Nod : Node_Id;
458 Related_Id : Entity_Id;
459 Suffix : Character;
460 Suffix_Index : Nat);
ea034236 461 -- Process an index constraint S in a constrained array declaration. The
fea9e956
ES
462 -- constraint can be a subtype name, or a range with or without an explicit
463 -- subtype mark. The index is the corresponding index of the unconstrained
464 -- array. The Related_Id and Suffix parameters are used to build the
465 -- associated Implicit type name.
996ae0b0 466
07fc65c4 467 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
ffe9aba8 468 -- Build subtype of a signed or modular integer type
996ae0b0 469
07fc65c4 470 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
996ae0b0
RK
471 -- Constrain an ordinary fixed point type with a range constraint, and
472 -- build an E_Ordinary_Fixed_Point_Subtype entity.
473
fbf5a39b 474 procedure Copy_And_Swap (Priv, Full : Entity_Id);
fea9e956
ES
475 -- Copy the Priv entity into the entity of its full declaration then swap
476 -- the two entities in such a manner that the former private type is now
477 -- seen as a full type.
996ae0b0 478
996ae0b0
RK
479 procedure Decimal_Fixed_Point_Type_Declaration
480 (T : Entity_Id;
481 Def : Node_Id);
482 -- Create a new decimal fixed point type, and apply the constraint to
483 -- obtain a subtype of this new type.
484
485 procedure Complete_Private_Subtype
486 (Priv : Entity_Id;
487 Full : Entity_Id;
488 Full_Base : Entity_Id;
489 Related_Nod : Node_Id);
9dfd2ff8
CC
490 -- Complete the implicit full view of a private subtype by setting the
491 -- appropriate semantic fields. If the full view of the parent is a record
492 -- type, build constrained components of subtype.
996ae0b0 493
ce2b6ba5 494 procedure Derive_Progenitor_Subprograms
88b32fc3 495 (Parent_Type : Entity_Id;
ce2b6ba5
JM
496 Tagged_Type : Entity_Id);
497 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
498 -- operations of progenitors of Tagged_Type, and replace the subsidiary
499 -- subtypes with Tagged_Type, to build the specs of the inherited interface
500 -- primitives. The derived primitives are aliased to those of the
4818e7b9
RD
501 -- interface. This routine takes care also of transferring to the full view
502 -- subprograms associated with the partial view of Tagged_Type that cover
ce2b6ba5 503 -- interface primitives.
758c442c 504
996ae0b0
RK
505 procedure Derived_Standard_Character
506 (N : Node_Id;
507 Parent_Type : Entity_Id;
508 Derived_Type : Entity_Id);
509 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
510 -- derivations from types Standard.Character and Standard.Wide_Character.
511
512 procedure Derived_Type_Declaration
513 (T : Entity_Id;
514 N : Node_Id;
515 Is_Completion : Boolean);
ce4a6e84
RD
516 -- Process a derived type declaration. Build_Derived_Type is invoked
517 -- to process the actual derived type definition. Parameters N and
518 -- Is_Completion have the same meaning as in Build_Derived_Type.
519 -- T is the N_Defining_Identifier for the entity defined in the
520 -- N_Full_Type_Declaration node N, that is T is the derived type.
996ae0b0 521
996ae0b0 522 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
9dfd2ff8
CC
523 -- Insert each literal in symbol table, as an overloadable identifier. Each
524 -- enumeration type is mapped into a sequence of integers, and each literal
525 -- is defined as a constant with integer value. If any of the literals are
526 -- character literals, the type is a character type, which means that
527 -- strings are legal aggregates for arrays of components of the type.
996ae0b0 528
fbf5a39b
AC
529 function Expand_To_Stored_Constraint
530 (Typ : Entity_Id;
b0f26df5 531 Constraint : Elist_Id) return Elist_Id;
ce4a6e84 532 -- Given a constraint (i.e. a list of expressions) on the discriminants of
9dfd2ff8
CC
533 -- Typ, expand it into a constraint on the stored discriminants and return
534 -- the new list of expressions constraining the stored discriminants.
996ae0b0
RK
535
536 function Find_Type_Of_Object
537 (Obj_Def : Node_Id;
b0f26df5 538 Related_Nod : Node_Id) return Entity_Id;
996ae0b0
RK
539 -- Get type entity for object referenced by Obj_Def, attaching the
540 -- implicit types generated to Related_Nod
541
542 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
ce4a6e84 543 -- Create a new float and apply the constraint to obtain subtype of it
996ae0b0
RK
544
545 function Has_Range_Constraint (N : Node_Id) return Boolean;
546 -- Given an N_Subtype_Indication node N, return True if a range constraint
547 -- is present, either directly, or as part of a digits or delta constraint.
548 -- In addition, a digits constraint in the decimal case returns True, since
549 -- it establishes a default range if no explicit range is present.
550
88b32fc3
BD
551 function Inherit_Components
552 (N : Node_Id;
553 Parent_Base : Entity_Id;
554 Derived_Base : Entity_Id;
555 Is_Tagged : Boolean;
556 Inherit_Discr : Boolean;
557 Discs : Elist_Id) return Elist_Id;
558 -- Called from Build_Derived_Record_Type to inherit the components of
559 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
560 -- For more information on derived types and component inheritance please
561 -- consult the comment above the body of Build_Derived_Record_Type.
562 --
563 -- N is the original derived type declaration
564 --
565 -- Is_Tagged is set if we are dealing with tagged types
566 --
fea9e956
ES
567 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
568 -- Parent_Base, otherwise no discriminants are inherited.
88b32fc3
BD
569 --
570 -- Discs gives the list of constraints that apply to Parent_Base in the
571 -- derived type declaration. If Discs is set to No_Elist, then we have
572 -- the following situation:
573 --
574 -- type Parent (D1..Dn : ..) is [tagged] record ...;
575 -- type Derived is new Parent [with ...];
576 --
577 -- which gets treated as
578 --
579 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
580 --
581 -- For untagged types the returned value is an association list. The list
582 -- starts from the association (Parent_Base => Derived_Base), and then it
583 -- contains a sequence of the associations of the form
584 --
585 -- (Old_Component => New_Component),
586 --
fea9e956
ES
587 -- where Old_Component is the Entity_Id of a component in Parent_Base and
588 -- New_Component is the Entity_Id of the corresponding component in
88b32fc3
BD
589 -- Derived_Base. For untagged records, this association list is needed when
590 -- copying the record declaration for the derived base. In the tagged case
591 -- the value returned is irrelevant.
592
996ae0b0
RK
593 function Is_Valid_Constraint_Kind
594 (T_Kind : Type_Kind;
b0f26df5 595 Constraint_Kind : Node_Kind) return Boolean;
9dfd2ff8
CC
596 -- Returns True if it is legal to apply the given kind of constraint to the
597 -- given kind of type (index constraint to an array type, for example).
996ae0b0
RK
598
599 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
7a489a2b 600 -- Create new modular type. Verify that modulus is in bounds
996ae0b0 601
6c1e24d3 602 procedure New_Concatenation_Op (Typ : Entity_Id);
996ae0b0 603 -- Create an abbreviated declaration for an operator in order to
6c1e24d3 604 -- materialize concatenation on array types.
996ae0b0
RK
605
606 procedure Ordinary_Fixed_Point_Type_Declaration
607 (T : Entity_Id;
608 Def : Node_Id);
9dfd2ff8
CC
609 -- Create a new ordinary fixed point type, and apply the constraint to
610 -- obtain subtype of it.
996ae0b0
RK
611
612 procedure Prepare_Private_Subtype_Completion
613 (Id : Entity_Id;
614 Related_Nod : Node_Id);
615 -- Id is a subtype of some private type. Creates the full declaration
616 -- associated with Id whenever possible, i.e. when the full declaration
617 -- of the base type is already known. Records each subtype into
618 -- Private_Dependents of the base type.
619
620 procedure Process_Incomplete_Dependents
621 (N : Node_Id;
622 Full_T : Entity_Id;
623 Inc_T : Entity_Id);
624 -- Process all entities that depend on an incomplete type. There include
625 -- subtypes, subprogram types that mention the incomplete type in their
626 -- profiles, and subprogram with access parameters that designate the
627 -- incomplete type.
628
629 -- Inc_T is the defining identifier of an incomplete type declaration, its
630 -- Ekind is E_Incomplete_Type.
631 --
632 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
633 --
634 -- Full_T is N's defining identifier.
635 --
636 -- Subtypes of incomplete types with discriminants are completed when the
637 -- parent type is. This is simpler than private subtypes, because they can
638 -- only appear in the same scope, and there is no need to exchange views.
639 -- Similarly, access_to_subprogram types may have a parameter or a return
640 -- type that is an incomplete type, and that must be replaced with the
641 -- full type.
ce4a6e84 642 --
996ae0b0
RK
643 -- If the full type is tagged, subprogram with access parameters that
644 -- designated the incomplete may be primitive operations of the full type,
645 -- and have to be processed accordingly.
646
647 procedure Process_Real_Range_Specification (Def : Node_Id);
ce4a6e84
RD
648 -- Given the type definition for a real type, this procedure processes and
649 -- checks the real range specification of this type definition if one is
650 -- present. If errors are found, error messages are posted, and the
651 -- Real_Range_Specification of Def is reset to Empty.
996ae0b0 652
fbf5a39b
AC
653 procedure Record_Type_Declaration
654 (T : Entity_Id;
655 N : Node_Id;
656 Prev : Entity_Id);
996ae0b0
RK
657 -- Process a record type declaration (for both untagged and tagged
658 -- records). Parameters T and N are exactly like in procedure
9dfd2ff8
CC
659 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
660 -- for this routine. If this is the completion of an incomplete type
661 -- declaration, Prev is the entity of the incomplete declaration, used for
662 -- cross-referencing. Otherwise Prev = T.
996ae0b0 663
fbf5a39b 664 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
ce4a6e84
RD
665 -- This routine is used to process the actual record type definition (both
666 -- for untagged and tagged records). Def is a record type definition node.
667 -- This procedure analyzes the components in this record type definition.
668 -- Prev_T is the entity for the enclosing record type. It is provided so
669 -- that its Has_Task flag can be set if any of the component have Has_Task
670 -- set. If the declaration is the completion of an incomplete type
671 -- declaration, Prev_T is the original incomplete type, whose full view is
672 -- the record type.
996ae0b0 673
07fc65c4
GB
674 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
675 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
676 -- build a copy of the declaration tree of the parent, and we create
677 -- independently the list of components for the derived type. Semantic
678 -- information uses the component entities, but record representation
679 -- clauses are validated on the declaration tree. This procedure replaces
680 -- discriminants and components in the declaration with those that have
681 -- been created by Inherit_Components.
682
996ae0b0
RK
683 procedure Set_Fixed_Range
684 (E : Entity_Id;
685 Loc : Source_Ptr;
686 Lo : Ureal;
687 Hi : Ureal);
688 -- Build a range node with the given bounds and set it as the Scalar_Range
689 -- of the given fixed-point type entity. Loc is the source location used
690 -- for the constructed range. See body for further details.
691
692 procedure Set_Scalar_Range_For_Subtype
07fc65c4
GB
693 (Def_Id : Entity_Id;
694 R : Node_Id;
695 Subt : Entity_Id);
57193e09
TQ
696 -- This routine is used to set the scalar range field for a subtype given
697 -- Def_Id, the entity for the subtype, and R, the range expression for the
698 -- scalar range. Subt provides the parent subtype to be used to analyze,
699 -- resolve, and check the given range.
996ae0b0
RK
700
701 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
702 -- Create a new signed integer entity, and apply the constraint to obtain
703 -- the required first named subtype of this type.
704
fbf5a39b
AC
705 procedure Set_Stored_Constraint_From_Discriminant_Constraint
706 (E : Entity_Id);
707 -- E is some record type. This routine computes E's Stored_Constraint
708 -- from its Discriminant_Constraint.
709
6765b310
ES
710 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
711 -- Check that an entity in a list of progenitors is an interface,
712 -- emit error otherwise.
713
996ae0b0
RK
714 -----------------------
715 -- Access_Definition --
716 -----------------------
717
718 function Access_Definition
719 (Related_Nod : Node_Id;
b0f26df5 720 N : Node_Id) return Entity_Id
996ae0b0 721 is
550f4135
AC
722 Anon_Type : Entity_Id;
723 Anon_Scope : Entity_Id;
724 Desig_Type : Entity_Id;
550f4135 725 Enclosing_Prot_Type : Entity_Id := Empty;
996ae0b0
RK
726
727 begin
2ba431e5 728 Check_SPARK_Restriction ("access type is not allowed", N);
7ff2d234 729
996ae0b0
RK
730 if Is_Entry (Current_Scope)
731 and then Is_Task_Type (Etype (Scope (Current_Scope)))
732 then
733 Error_Msg_N ("task entries cannot have access parameters", N);
fea9e956 734 return Empty;
996ae0b0
RK
735 end if;
736
113a62d9 737 -- Ada 2005: For an object declaration the corresponding anonymous
57193e09 738 -- type is declared in the current scope.
758c442c 739
88b32fc3
BD
740 -- If the access definition is the return type of another access to
741 -- function, scope is the current one, because it is the one of the
be482a8c 742 -- current type declaration, except for the pathological case below.
88b32fc3 743
7d7af38a
JM
744 if Nkind_In (Related_Nod, N_Object_Declaration,
745 N_Access_Function_Definition)
88b32fc3 746 then
2b73cf68 747 Anon_Scope := Current_Scope;
9dfd2ff8 748
be482a8c 749 -- A pathological case: function returning access functions that
d673c5c5 750 -- return access functions, etc. Each anonymous access type created
be482a8c
AC
751 -- is in the enclosing scope of the outermost function.
752
753 declare
754 Par : Node_Id;
d673c5c5 755
be482a8c
AC
756 begin
757 Par := Related_Nod;
d673c5c5
RD
758 while Nkind_In (Par, N_Access_Function_Definition,
759 N_Access_Definition)
be482a8c
AC
760 loop
761 Par := Parent (Par);
762 end loop;
763
764 if Nkind (Par) = N_Function_Specification then
765 Anon_Scope := Scope (Defining_Entity (Par));
766 end if;
767 end;
768
fea9e956
ES
769 -- For the anonymous function result case, retrieve the scope of the
770 -- function specification's associated entity rather than using the
771 -- current scope. The current scope will be the function itself if the
772 -- formal part is currently being analyzed, but will be the parent scope
773 -- in the case of a parameterless function, and we always want to use
774 -- the function's parent scope. Finally, if the function is a child
f3d57416 775 -- unit, we must traverse the tree to retrieve the proper entity.
9dfd2ff8
CC
776
777 elsif Nkind (Related_Nod) = N_Function_Specification
7d7af38a 778 and then Nkind (Parent (N)) /= N_Parameter_Specification
9dfd2ff8 779 then
2b73cf68
JM
780 -- If the current scope is a protected type, the anonymous access
781 -- is associated with one of the protected operations, and must
782 -- be available in the scope that encloses the protected declaration.
16b05213 783 -- Otherwise the type is in the scope enclosing the subprogram.
16c5f1c6 784
550f4135
AC
785 -- If the function has formals, The return type of a subprogram
786 -- declaration is analyzed in the scope of the subprogram (see
787 -- Process_Formals) and thus the protected type, if present, is
788 -- the scope of the current function scope.
2b73cf68
JM
789
790 if Ekind (Current_Scope) = E_Protected_Type then
550f4135
AC
791 Enclosing_Prot_Type := Current_Scope;
792
793 elsif Ekind (Current_Scope) = E_Function
794 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
795 then
796 Enclosing_Prot_Type := Scope (Current_Scope);
797 end if;
798
799 if Present (Enclosing_Prot_Type) then
800 Anon_Scope := Scope (Enclosing_Prot_Type);
801
2b73cf68
JM
802 else
803 Anon_Scope := Scope (Defining_Entity (Related_Nod));
804 end if;
57193e09 805
8da1a312
AC
806 -- For an access type definition, if the current scope is a child
807 -- unit it is the scope of the type.
13a0b1e8
AC
808
809 elsif Is_Compilation_Unit (Current_Scope) then
810 Anon_Scope := Current_Scope;
57193e09 811
13a0b1e8
AC
812 -- For access formals, access components, and access discriminants, the
813 -- scope is that of the enclosing declaration,
814
815 else
2b73cf68 816 Anon_Scope := Scope (Current_Scope);
758c442c
GD
817 end if;
818
2b73cf68
JM
819 Anon_Type :=
820 Create_Itype
df3e68b1 821 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
2b73cf68 822
758c442c 823 if All_Present (N)
0791fbe9 824 and then Ada_Version >= Ada_2005
758c442c
GD
825 then
826 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
827 end if;
828
fea9e956
ES
829 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
830 -- the corresponding semantic routine
7324bf49
AC
831
832 if Present (Access_To_Subprogram_Definition (N)) then
8e293fbd
AC
833
834 -- Compiler runtime units are compiled in Ada 2005 mode when building
835 -- the runtime library but must also be compilable in Ada 95 mode
836 -- (when bootstrapping the compiler).
837
838 Check_Compiler_Unit (N);
839
7324bf49
AC
840 Access_Subprogram_Declaration
841 (T_Name => Anon_Type,
842 T_Def => Access_To_Subprogram_Definition (N));
af4b9434
AC
843
844 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
845 Set_Ekind
846 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
847 else
848 Set_Ekind
849 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
850 end if;
851
7d7af38a
JM
852 Set_Can_Use_Internal_Rep
853 (Anon_Type, not Always_Compatible_Rep_On_Target);
854
8da1a312 855 -- If the anonymous access is associated with a protected operation,
2b73cf68
JM
856 -- create a reference to it after the enclosing protected definition
857 -- because the itype will be used in the subsequent bodies.
858
859 if Ekind (Current_Scope) = E_Protected_Type then
860 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
861 end if;
862
7324bf49
AC
863 return Anon_Type;
864 end if;
865
996ae0b0
RK
866 Find_Type (Subtype_Mark (N));
867 Desig_Type := Entity (Subtype_Mark (N));
868
b87971f3 869 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
c0985d4e 870 Set_Etype (Anon_Type, Anon_Type);
ce4a6e84
RD
871
872 -- Make sure the anonymous access type has size and alignment fields
873 -- set, as required by gigi. This is necessary in the case of the
874 -- Task_Body_Procedure.
875
876 if not Has_Private_Component (Desig_Type) then
877 Layout_Type (Anon_Type);
878 end if;
879
0ab80019 880 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
fea9e956
ES
881 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
882 -- the null value is allowed. In Ada 95 the null value is never allowed.
2820d220 883
0791fbe9 884 if Ada_Version >= Ada_2005 then
6b6fcd3e 885 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
2820d220 886 else
6b6fcd3e 887 Set_Can_Never_Be_Null (Anon_Type, True);
2820d220
AC
888 end if;
889
996ae0b0
RK
890 -- The anonymous access type is as public as the discriminated type or
891 -- subprogram that defines it. It is imported (for back-end purposes)
892 -- if the designated type is.
893
6b6fcd3e 894 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
19f0526a 895
0ab80019 896 -- Ada 2005 (AI-231): Propagate the access-constant attribute
2820d220
AC
897
898 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
899
758c442c
GD
900 -- The context is either a subprogram declaration, object declaration,
901 -- or an access discriminant, in a private or a full type declaration.
902 -- In the case of a subprogram, if the designated type is incomplete,
903 -- the operation will be a primitive operation of the full type, to be
904 -- updated subsequently. If the type is imported through a limited_with
905 -- clause, the subprogram is not a primitive operation of the type
906 -- (which is declared elsewhere in some other scope).
996ae0b0
RK
907
908 if Ekind (Desig_Type) = E_Incomplete_Type
7b56a91b 909 and then not From_Limited_With (Desig_Type)
996ae0b0
RK
910 and then Is_Overloadable (Current_Scope)
911 then
912 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
913 Set_Has_Delayed_Freeze (Current_Scope);
914 end if;
915
113a62d9 916 -- Ada 2005: If the designated type is an interface that may contain
950d3e7d 917 -- tasks, create a Master entity for the declaration. This must be done
fea9e956
ES
918 -- before expansion of the full declaration, because the declaration may
919 -- include an expression that is an allocator, whose expansion needs the
920 -- proper Master for the created tasks.
950d3e7d
ES
921
922 if Nkind (Related_Nod) = N_Object_Declaration
1a36a0cd 923 and then Expander_Active
950d3e7d 924 then
88b32fc3
BD
925 if Is_Interface (Desig_Type)
926 and then Is_Limited_Record (Desig_Type)
927 then
928 Build_Class_Wide_Master (Anon_Type);
929
930 -- Similarly, if the type is an anonymous access that designates
931 -- tasks, create a master entity for it in the current context.
932
933 elsif Has_Task (Desig_Type)
934 and then Comes_From_Source (Related_Nod)
935 then
1a36a0cd
AC
936 Build_Master_Entity (Defining_Identifier (Related_Nod));
937 Build_Master_Renaming (Anon_Type);
88b32fc3 938 end if;
950d3e7d
ES
939 end if;
940
fea9e956
ES
941 -- For a private component of a protected type, it is imperative that
942 -- the back-end elaborate the type immediately after the protected
943 -- declaration, because this type will be used in the declarations
944 -- created for the component within each protected body, so we must
945 -- create an itype reference for it now.
946
947 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
948 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
df89ab66
ES
949
950 -- Similarly, if the access definition is the return result of a
88eb6e62
AC
951 -- function, create an itype reference for it because it will be used
952 -- within the function body. For a regular function that is not a
953 -- compilation unit, insert reference after the declaration. For a
954 -- protected operation, insert it after the enclosing protected type
955 -- declaration. In either case, do not create a reference for a type
956 -- obtained through a limited_with clause, because this would introduce
957 -- semantic dependencies.
958
89c273b4
AC
959 -- Similarly, do not create a reference if the designated type is a
960 -- generic formal, because no use of it will reach the backend.
df89ab66
ES
961
962 elsif Nkind (Related_Nod) = N_Function_Specification
7b56a91b 963 and then not From_Limited_With (Desig_Type)
89c273b4 964 and then not Is_Generic_Type (Desig_Type)
df89ab66 965 then
550f4135
AC
966 if Present (Enclosing_Prot_Type) then
967 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
0f5177ad
ES
968
969 elsif Is_List_Member (Parent (Related_Nod))
970 and then Nkind (Parent (N)) /= N_Parameter_Specification
971 then
972 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
973 end if;
df89ab66 974
88eb6e62
AC
975 -- Finally, create an itype reference for an object declaration of an
976 -- anonymous access type. This is strictly necessary only for deferred
977 -- constants, but in any case will avoid out-of-scope problems in the
978 -- back-end.
df89ab66
ES
979
980 elsif Nkind (Related_Nod) = N_Object_Declaration then
981 Build_Itype_Reference (Anon_Type, Related_Nod);
fea9e956
ES
982 end if;
983
996ae0b0
RK
984 return Anon_Type;
985 end Access_Definition;
986
987 -----------------------------------
988 -- Access_Subprogram_Declaration --
989 -----------------------------------
990
991 procedure Access_Subprogram_Declaration
992 (T_Name : Entity_Id;
993 T_Def : Node_Id)
994 is
f29b857f 995 procedure Check_For_Premature_Usage (Def : Node_Id);
8dbf3473
AC
996 -- Check that type T_Name is not used, directly or recursively, as a
997 -- parameter or a return type in Def. Def is either a subtype, an
998 -- access_definition, or an access_to_subprogram_definition.
f29b857f
ES
999
1000 -------------------------------
1001 -- Check_For_Premature_Usage --
1002 -------------------------------
1003
1004 procedure Check_For_Premature_Usage (Def : Node_Id) is
1005 Param : Node_Id;
1006
1007 begin
1008 -- Check for a subtype mark
1009
1010 if Nkind (Def) in N_Has_Etype then
1011 if Etype (Def) = T_Name then
1012 Error_Msg_N
808876a9 1013 ("type& cannot be used before end of its declaration", Def);
f29b857f
ES
1014 end if;
1015
1016 -- If this is not a subtype, then this is an access_definition
1017
1018 elsif Nkind (Def) = N_Access_Definition then
1019 if Present (Access_To_Subprogram_Definition (Def)) then
1020 Check_For_Premature_Usage
1021 (Access_To_Subprogram_Definition (Def));
1022 else
1023 Check_For_Premature_Usage (Subtype_Mark (Def));
1024 end if;
1025
1026 -- The only cases left are N_Access_Function_Definition and
1027 -- N_Access_Procedure_Definition.
1028
1029 else
1030 if Present (Parameter_Specifications (Def)) then
1031 Param := First (Parameter_Specifications (Def));
1032 while Present (Param) loop
1033 Check_For_Premature_Usage (Parameter_Type (Param));
1034 Param := Next (Param);
1035 end loop;
1036 end if;
1037
1038 if Nkind (Def) = N_Access_Function_Definition then
1039 Check_For_Premature_Usage (Result_Definition (Def));
1040 end if;
1041 end if;
1042 end Check_For_Premature_Usage;
1043
1044 -- Local variables
1045
1046 Formals : constant List_Id := Parameter_Specifications (T_Def);
1047 Formal : Entity_Id;
1048 D_Ityp : Node_Id;
996ae0b0 1049 Desig_Type : constant Entity_Id :=
0da2c8ac 1050 Create_Itype (E_Subprogram_Type, Parent (T_Def));
996ae0b0 1051
f29b857f
ES
1052 -- Start of processing for Access_Subprogram_Declaration
1053
996ae0b0 1054 begin
2ba431e5 1055 Check_SPARK_Restriction ("access type is not allowed", T_Def);
7ff2d234 1056
fea9e956 1057 -- Associate the Itype node with the inner full-type declaration or
e86a3a7e
AC
1058 -- subprogram spec or entry body. This is required to handle nested
1059 -- anonymous declarations. For example:
758c442c
GD
1060
1061 -- procedure P
1062 -- (X : access procedure
1063 -- (Y : access procedure
1064 -- (Z : access T)))
1065
9dfd2ff8 1066 D_Ityp := Associated_Node_For_Itype (Desig_Type);
7d7af38a
JM
1067 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1068 N_Private_Type_Declaration,
1069 N_Private_Extension_Declaration,
1070 N_Procedure_Specification,
e86a3a7e
AC
1071 N_Function_Specification,
1072 N_Entry_Body)
1073
7d7af38a
JM
1074 or else
1075 Nkind_In (D_Ityp, N_Object_Declaration,
1076 N_Object_Renaming_Declaration,
53cf4600 1077 N_Formal_Object_Declaration,
7d7af38a
JM
1078 N_Formal_Type_Declaration,
1079 N_Task_Type_Declaration,
1080 N_Protected_Type_Declaration))
758c442c
GD
1081 loop
1082 D_Ityp := Parent (D_Ityp);
1083 pragma Assert (D_Ityp /= Empty);
1084 end loop;
1085
1086 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1087
7d7af38a
JM
1088 if Nkind_In (D_Ityp, N_Procedure_Specification,
1089 N_Function_Specification)
758c442c 1090 then
88b32fc3 1091 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
758c442c 1092
7d7af38a
JM
1093 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1094 N_Object_Declaration,
1095 N_Object_Renaming_Declaration,
1096 N_Formal_Type_Declaration)
758c442c
GD
1097 then
1098 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1099 end if;
1100
996ae0b0 1101 if Nkind (T_Def) = N_Access_Function_Definition then
9dfd2ff8 1102 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
2b73cf68
JM
1103 declare
1104 Acc : constant Node_Id := Result_Definition (T_Def);
1105
1106 begin
1107 if Present (Access_To_Subprogram_Definition (Acc))
1108 and then
1109 Protected_Present (Access_To_Subprogram_Definition (Acc))
1110 then
1111 Set_Etype
1112 (Desig_Type,
1113 Replace_Anonymous_Access_To_Protected_Subprogram
1114 (T_Def));
1115
1116 else
1117 Set_Etype
1118 (Desig_Type,
1119 Access_Definition (T_Def, Result_Definition (T_Def)));
1120 end if;
1121 end;
1122
9dfd2ff8
CC
1123 else
1124 Analyze (Result_Definition (T_Def));
b66c3ff4
AC
1125
1126 declare
1127 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1128
1129 begin
1130 -- If a null exclusion is imposed on the result type, then
1131 -- create a null-excluding itype (an access subtype) and use
1132 -- it as the function's Etype.
1133
1134 if Is_Access_Type (Typ)
1135 and then Null_Exclusion_In_Return_Present (T_Def)
1136 then
1137 Set_Etype (Desig_Type,
1138 Create_Null_Excluding_Itype
1139 (T => Typ,
1140 Related_Nod => T_Def,
1141 Scope_Id => Current_Scope));
cec29135 1142
b66c3ff4 1143 else
7b56a91b 1144 if From_Limited_With (Typ) then
dd386db0 1145
0f1a6a0b 1146 -- AI05-151: Incomplete types are allowed in all basic
dd386db0
AC
1147 -- declarations, including access to subprograms.
1148
1149 if Ada_Version >= Ada_2012 then
1150 null;
1151
1152 else
1153 Error_Msg_NE
1154 ("illegal use of incomplete type&",
5ee96c9d 1155 Result_Definition (T_Def), Typ);
dd386db0 1156 end if;
cec29135
ES
1157
1158 elsif Ekind (Current_Scope) = E_Package
1159 and then In_Private_Part (Current_Scope)
1160 then
1161 if Ekind (Typ) = E_Incomplete_Type then
1162 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1163
1164 elsif Is_Class_Wide_Type (Typ)
1165 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1166 then
1167 Append_Elmt
1168 (Desig_Type, Private_Dependents (Etype (Typ)));
1169 end if;
1170 end if;
1171
b66c3ff4
AC
1172 Set_Etype (Desig_Type, Typ);
1173 end if;
1174 end;
9dfd2ff8 1175 end if;
0c644933
AC
1176
1177 if not (Is_Type (Etype (Desig_Type))) then
1178 Error_Msg_N
9dfd2ff8
CC
1179 ("expect type in function specification",
1180 Result_Definition (T_Def));
0c644933 1181 end if;
b0f26df5 1182
996ae0b0
RK
1183 else
1184 Set_Etype (Desig_Type, Standard_Void_Type);
1185 end if;
1186
1187 if Present (Formals) then
2b73cf68 1188 Push_Scope (Desig_Type);
b1c11e0e
JM
1189
1190 -- A bit of a kludge here. These kludges will be removed when Itypes
1191 -- have proper parent pointers to their declarations???
1192
16b05213 1193 -- Kludge 1) Link defining_identifier of formals. Required by
b1c11e0e
JM
1194 -- First_Formal to provide its functionality.
1195
1196 declare
1197 F : Node_Id;
1198
1199 begin
1200 F := First (Formals);
0bb9276c
AC
1201
1202 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1203 -- when it is part of an unconstrained type and subtype expansion
d8b3ccb9 1204 -- is disabled. To avoid back-end problems with shared profiles,
246ff1ae
AC
1205 -- use previous subprogram type as the designated type, and then
1206 -- remove scope added above.
0bb9276c
AC
1207
1208 if ASIS_Mode
1209 and then Present (Scope (Defining_Identifier (F)))
1210 then
1211 Set_Etype (T_Name, T_Name);
1212 Init_Size_Align (T_Name);
1213 Set_Directly_Designated_Type (T_Name,
1214 Scope (Defining_Identifier (F)));
246ff1ae 1215 End_Scope;
0bb9276c
AC
1216 return;
1217 end if;
1218
b1c11e0e
JM
1219 while Present (F) loop
1220 if No (Parent (Defining_Identifier (F))) then
1221 Set_Parent (Defining_Identifier (F), F);
1222 end if;
1223
1224 Next (F);
1225 end loop;
1226 end;
1227
07fc65c4 1228 Process_Formals (Formals, Parent (T_Def));
996ae0b0 1229
b1c11e0e
JM
1230 -- Kludge 2) End_Scope requires that the parent pointer be set to
1231 -- something reasonable, but Itypes don't have parent pointers. So
1232 -- we set it and then unset it ???
996ae0b0
RK
1233
1234 Set_Parent (Desig_Type, T_Name);
1235 End_Scope;
1236 Set_Parent (Desig_Type, Empty);
1237 end if;
1238
f29b857f
ES
1239 -- Check for premature usage of the type being defined
1240
1241 Check_For_Premature_Usage (T_Def);
1242
1355d373
AC
1243 -- The return type and/or any parameter type may be incomplete. Mark the
1244 -- subprogram_type as depending on the incomplete type, so that it can
1245 -- be updated when the full type declaration is seen. This only applies
1246 -- to incomplete types declared in some enclosing scope, not to limited
1247 -- views from other packages.
e917e3b8 1248
cf895a01 1249 -- Prior to Ada 2012, access to functions can only have in_parameters.
996ae0b0
RK
1250
1251 if Present (Formals) then
1252 Formal := First_Formal (Desig_Type);
996ae0b0 1253 while Present (Formal) loop
996ae0b0
RK
1254 if Ekind (Formal) /= E_In_Parameter
1255 and then Nkind (T_Def) = N_Access_Function_Definition
cf895a01 1256 and then Ada_Version < Ada_2012
996ae0b0
RK
1257 then
1258 Error_Msg_N ("functions can only have IN parameters", Formal);
1259 end if;
1260
2b73cf68
JM
1261 if Ekind (Etype (Formal)) = E_Incomplete_Type
1262 and then In_Open_Scopes (Scope (Etype (Formal)))
1263 then
996ae0b0
RK
1264 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1265 Set_Has_Delayed_Freeze (Desig_Type);
1266 end if;
1267
1268 Next_Formal (Formal);
1269 end loop;
1270 end if;
1271
4bb9c7b9
AC
1272 -- Check whether an indirect call without actuals may be possible. This
1273 -- is used when resolving calls whose result is then indexed.
1274
1275 May_Need_Actuals (Desig_Type);
1276
1355d373
AC
1277 -- If the return type is incomplete, this is legal as long as the type
1278 -- is declared in the current scope and will be completed in it (rather
1279 -- than being part of limited view).
49d8b802 1280
996ae0b0
RK
1281 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1282 and then not Has_Delayed_Freeze (Desig_Type)
49d8b802 1283 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
996ae0b0
RK
1284 then
1285 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1286 Set_Has_Delayed_Freeze (Desig_Type);
1287 end if;
1288
1289 Check_Delayed_Subprogram (Desig_Type);
1290
1291 if Protected_Present (T_Def) then
1292 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1293 Set_Convention (Desig_Type, Convention_Protected);
1294 else
1295 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1296 end if;
1297
7d7af38a
JM
1298 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1299
996ae0b0
RK
1300 Set_Etype (T_Name, T_Name);
1301 Init_Size_Align (T_Name);
1302 Set_Directly_Designated_Type (T_Name, Desig_Type);
1303
67a90476
AC
1304 Generate_Reference_To_Formals (T_Name);
1305
0ab80019 1306 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
2820d220
AC
1307
1308 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1309
996ae0b0
RK
1310 Check_Restriction (No_Access_Subprograms, T_Def);
1311 end Access_Subprogram_Declaration;
1312
1313 ----------------------------
1314 -- Access_Type_Declaration --
1315 ----------------------------
1316
1317 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
996ae0b0 1318 P : constant Node_Id := Parent (Def);
df3e68b1
HK
1319 S : constant Node_Id := Subtype_Indication (Def);
1320
1321 Full_Desig : Entity_Id;
1322
996ae0b0 1323 begin
2ba431e5 1324 Check_SPARK_Restriction ("access type is not allowed", Def);
7ff2d234 1325
996ae0b0
RK
1326 -- Check for permissible use of incomplete type
1327
1328 if Nkind (S) /= N_Subtype_Indication then
1329 Analyze (S);
1330
1331 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1332 Set_Directly_Designated_Type (T, Entity (S));
1333 else
1334 Set_Directly_Designated_Type (T,
1335 Process_Subtype (S, P, T, 'P'));
1336 end if;
1337
1338 else
1339 Set_Directly_Designated_Type (T,
1340 Process_Subtype (S, P, T, 'P'));
1341 end if;
1342
1343 if All_Present (Def) or Constant_Present (Def) then
1344 Set_Ekind (T, E_General_Access_Type);
1345 else
1346 Set_Ekind (T, E_Access_Type);
1347 end if;
1348
df3e68b1
HK
1349 Full_Desig := Designated_Type (T);
1350
1351 if Base_Type (Full_Desig) = T then
996ae0b0 1352 Error_Msg_N ("access type cannot designate itself", S);
9dfd2ff8 1353
1355d373
AC
1354 -- In Ada 2005, the type may have a limited view through some unit in
1355 -- its own context, allowing the following circularity that cannot be
1356 -- detected earlier
9dfd2ff8 1357
df3e68b1
HK
1358 elsif Is_Class_Wide_Type (Full_Desig)
1359 and then Etype (Full_Desig) = T
9dfd2ff8
CC
1360 then
1361 Error_Msg_N
1362 ("access type cannot designate its own classwide type", S);
950d3e7d
ES
1363
1364 -- Clean up indication of tagged status to prevent cascaded errors
1365
1366 Set_Is_Tagged_Type (T, False);
996ae0b0
RK
1367 end if;
1368
fbf5a39b 1369 Set_Etype (T, T);
996ae0b0 1370
1355d373
AC
1371 -- If the type has appeared already in a with_type clause, it is frozen
1372 -- and the pointer size is already set. Else, initialize.
996ae0b0 1373
7b56a91b 1374 if not From_Limited_With (T) then
996ae0b0
RK
1375 Init_Size_Align (T);
1376 end if;
1377
996ae0b0
RK
1378 -- Note that Has_Task is always false, since the access type itself
1379 -- is not a task type. See Einfo for more description on this point.
1380 -- Exactly the same consideration applies to Has_Controlled_Component.
1381
1382 Set_Has_Task (T, False);
1383 Set_Has_Controlled_Component (T, False);
2820d220 1384
d3f70b35 1385 -- Initialize field Finalization_Master explicitly to Empty, to avoid
ce4a6e84
RD
1386 -- problems where an incomplete view of this entity has been previously
1387 -- established by a limited with and an overlaid version of this field
1388 -- (Stored_Constraint) was initialized for the incomplete view.
1389
df3e68b1
HK
1390 -- This reset is performed in most cases except where the access type
1391 -- has been created for the purposes of allocating or deallocating a
1392 -- build-in-place object. Such access types have explicitly set pools
d3f70b35 1393 -- and finalization masters.
df3e68b1
HK
1394
1395 if No (Associated_Storage_Pool (T)) then
d3f70b35 1396 Set_Finalization_Master (T, Empty);
df3e68b1 1397 end if;
ce4a6e84 1398
0ab80019 1399 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
2820d220
AC
1400 -- attributes
1401
1402 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1403 Set_Is_Access_Constant (T, Constant_Present (Def));
996ae0b0
RK
1404 end Access_Type_Declaration;
1405
758c442c
GD
1406 ----------------------------------
1407 -- Add_Interface_Tag_Components --
1408 ----------------------------------
1409
88b32fc3 1410 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
758c442c 1411 Loc : constant Source_Ptr := Sloc (N);
758c442c
GD
1412 L : List_Id;
1413 Last_Tag : Node_Id;
fea9e956 1414
758c442c 1415 procedure Add_Tag (Iface : Entity_Id);
88b32fc3 1416 -- Add tag for one of the progenitor interfaces
758c442c
GD
1417
1418 -------------
1419 -- Add_Tag --
1420 -------------
1421
1422 procedure Add_Tag (Iface : Entity_Id) is
57193e09
TQ
1423 Decl : Node_Id;
1424 Def : Node_Id;
1425 Tag : Entity_Id;
1426 Offset : Entity_Id;
758c442c
GD
1427
1428 begin
5ee96c9d 1429 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
758c442c 1430
4818e7b9
RD
1431 -- This is a reasonable place to propagate predicates
1432
1433 if Has_Predicates (Iface) then
1434 Set_Has_Predicates (Typ);
1435 end if;
1436
758c442c
GD
1437 Def :=
1438 Make_Component_Definition (Loc,
1439 Aliased_Present => True,
1440 Subtype_Indication =>
1441 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1442
092ef350 1443 Tag := Make_Temporary (Loc, 'V');
758c442c
GD
1444
1445 Decl :=
1446 Make_Component_Declaration (Loc,
1447 Defining_Identifier => Tag,
1448 Component_Definition => Def);
1449
1450 Analyze_Component_Declaration (Decl);
1451
1452 Set_Analyzed (Decl);
1453 Set_Ekind (Tag, E_Component);
758c442c 1454 Set_Is_Tag (Tag);
2b73cf68 1455 Set_Is_Aliased (Tag);
7d7af38a 1456 Set_Related_Type (Tag, Iface);
758c442c
GD
1457 Init_Component_Location (Tag);
1458
1459 pragma Assert (Is_Frozen (Iface));
1460
1461 Set_DT_Entry_Count (Tag,
1462 DT_Entry_Count (First_Entity (Iface)));
1463
57193e09 1464 if No (Last_Tag) then
758c442c
GD
1465 Prepend (Decl, L);
1466 else
1467 Insert_After (Last_Tag, Decl);
1468 end if;
1469
1470 Last_Tag := Decl;
57193e09
TQ
1471
1472 -- If the ancestor has discriminants we need to give special support
1473 -- to store the offset_to_top value of the secondary dispatch tables.
1474 -- For this purpose we add a supplementary component just after the
1475 -- field that contains the tag associated with each secondary DT.
1476
5ee96c9d 1477 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
57193e09
TQ
1478 Def :=
1479 Make_Component_Definition (Loc,
1480 Subtype_Indication =>
1481 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1482
092ef350 1483 Offset := Make_Temporary (Loc, 'V');
57193e09
TQ
1484
1485 Decl :=
1486 Make_Component_Declaration (Loc,
1487 Defining_Identifier => Offset,
1488 Component_Definition => Def);
1489
1490 Analyze_Component_Declaration (Decl);
1491
1492 Set_Analyzed (Decl);
1493 Set_Ekind (Offset, E_Component);
2b73cf68 1494 Set_Is_Aliased (Offset);
7d7af38a 1495 Set_Related_Type (Offset, Iface);
57193e09
TQ
1496 Init_Component_Location (Offset);
1497 Insert_After (Last_Tag, Decl);
1498 Last_Tag := Decl;
1499 end if;
758c442c
GD
1500 end Add_Tag;
1501
fea9e956
ES
1502 -- Local variables
1503
ce2b6ba5
JM
1504 Elmt : Elmt_Id;
1505 Ext : Node_Id;
1506 Comp : Node_Id;
fea9e956 1507
9dfd2ff8 1508 -- Start of processing for Add_Interface_Tag_Components
758c442c
GD
1509
1510 begin
2b73cf68
JM
1511 if not RTE_Available (RE_Interface_Tag) then
1512 Error_Msg
1513 ("(Ada 2005) interface types not supported by this run-time!",
1514 Sloc (N));
1515 return;
1516 end if;
1517
758c442c 1518 if Ekind (Typ) /= E_Record_Type
fea9e956
ES
1519 or else (Is_Concurrent_Record_Type (Typ)
1520 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1521 or else (not Is_Concurrent_Record_Type (Typ)
ce2b6ba5
JM
1522 and then No (Interfaces (Typ))
1523 and then Is_Empty_Elmt_List (Interfaces (Typ)))
758c442c
GD
1524 then
1525 return;
1526 end if;
1527
fea9e956
ES
1528 -- Find the current last tag
1529
1530 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1531 Ext := Record_Extension_Part (Type_Definition (N));
1532 else
1533 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1534 Ext := Type_Definition (N);
1535 end if;
758c442c 1536
fea9e956 1537 Last_Tag := Empty;
758c442c 1538
fea9e956
ES
1539 if not (Present (Component_List (Ext))) then
1540 Set_Null_Present (Ext, False);
1541 L := New_List;
1542 Set_Component_List (Ext,
1543 Make_Component_List (Loc,
1544 Component_Items => L,
1545 Null_Present => False));
1546 else
758c442c 1547 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
fea9e956
ES
1548 L := Component_Items
1549 (Component_List
1550 (Record_Extension_Part
1551 (Type_Definition (N))));
758c442c 1552 else
fea9e956
ES
1553 L := Component_Items
1554 (Component_List
1555 (Type_Definition (N)));
758c442c
GD
1556 end if;
1557
fea9e956 1558 -- Find the last tag component
758c442c 1559
fea9e956
ES
1560 Comp := First (L);
1561 while Present (Comp) loop
2b73cf68
JM
1562 if Nkind (Comp) = N_Component_Declaration
1563 and then Is_Tag (Defining_Identifier (Comp))
1564 then
fea9e956 1565 Last_Tag := Comp;
758c442c
GD
1566 end if;
1567
fea9e956
ES
1568 Next (Comp);
1569 end loop;
1570 end if;
758c442c 1571
fea9e956
ES
1572 -- At this point L references the list of components and Last_Tag
1573 -- references the current last tag (if any). Now we add the tag
1574 -- corresponding with all the interfaces that are not implemented
1575 -- by the parent.
758c442c 1576
ce2b6ba5
JM
1577 if Present (Interfaces (Typ)) then
1578 Elmt := First_Elmt (Interfaces (Typ));
758c442c
GD
1579 while Present (Elmt) loop
1580 Add_Tag (Node (Elmt));
1581 Next_Elmt (Elmt);
1582 end loop;
1583 end if;
1584 end Add_Interface_Tag_Components;
1585
3ff38f33
JM
1586 -------------------------------------
1587 -- Add_Internal_Interface_Entities --
1588 -------------------------------------
1589
1590 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
74853971
AC
1591 Elmt : Elmt_Id;
1592 Iface : Entity_Id;
1593 Iface_Elmt : Elmt_Id;
1594 Iface_Prim : Entity_Id;
1595 Ifaces_List : Elist_Id;
1596 New_Subp : Entity_Id := Empty;
1597 Prim : Entity_Id;
1598 Restore_Scope : Boolean := False;
3ff38f33
JM
1599
1600 begin
0791fbe9 1601 pragma Assert (Ada_Version >= Ada_2005
3ff38f33
JM
1602 and then Is_Record_Type (Tagged_Type)
1603 and then Is_Tagged_Type (Tagged_Type)
1604 and then Has_Interfaces (Tagged_Type)
1605 and then not Is_Interface (Tagged_Type));
1606
74853971
AC
1607 -- Ensure that the internal entities are added to the scope of the type
1608
1609 if Scope (Tagged_Type) /= Current_Scope then
1610 Push_Scope (Scope (Tagged_Type));
1611 Restore_Scope := True;
1612 end if;
1613
3ff38f33
JM
1614 Collect_Interfaces (Tagged_Type, Ifaces_List);
1615
1616 Iface_Elmt := First_Elmt (Ifaces_List);
1617 while Present (Iface_Elmt) loop
1618 Iface := Node (Iface_Elmt);
1619
b4d7b435
AC
1620 -- Originally we excluded here from this processing interfaces that
1621 -- are parents of Tagged_Type because their primitives are located
1622 -- in the primary dispatch table (and hence no auxiliary internal
1623 -- entities are required to handle secondary dispatch tables in such
1624 -- case). However, these auxiliary entities are also required to
1625 -- handle derivations of interfaces in formals of generics (see
1626 -- Derive_Subprograms).
3ff38f33 1627
b4d7b435
AC
1628 Elmt := First_Elmt (Primitive_Operations (Iface));
1629 while Present (Elmt) loop
1630 Iface_Prim := Node (Elmt);
3ff38f33 1631
b4d7b435
AC
1632 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1633 Prim :=
1634 Find_Primitive_Covering_Interface
1635 (Tagged_Type => Tagged_Type,
1636 Iface_Prim => Iface_Prim);
3ff38f33 1637
c01817d2
AC
1638 if No (Prim) and then Serious_Errors_Detected > 0 then
1639 goto Continue;
1640 end if;
1641
947430d5 1642 pragma Assert (Present (Prim));
3ff38f33 1643
ce09f8b3
AC
1644 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1645 -- differs from the name of the interface primitive then it is
1646 -- a private primitive inherited from a parent type. In such
1647 -- case, given that Tagged_Type covers the interface, the
1648 -- inherited private primitive becomes visible. For such
1649 -- purpose we add a new entity that renames the inherited
1650 -- private primitive.
1651
1652 if Chars (Prim) /= Chars (Iface_Prim) then
1653 pragma Assert (Has_Suffix (Prim, 'P'));
1654 Derive_Subprogram
1655 (New_Subp => New_Subp,
1656 Parent_Subp => Iface_Prim,
1657 Derived_Type => Tagged_Type,
1658 Parent_Type => Iface);
1659 Set_Alias (New_Subp, Prim);
878f708a
RD
1660 Set_Is_Abstract_Subprogram
1661 (New_Subp, Is_Abstract_Subprogram (Prim));
ce09f8b3
AC
1662 end if;
1663
b4d7b435
AC
1664 Derive_Subprogram
1665 (New_Subp => New_Subp,
1666 Parent_Subp => Iface_Prim,
1667 Derived_Type => Tagged_Type,
1668 Parent_Type => Iface);
1669
1670 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1671 -- associated with interface types. These entities are
1672 -- only registered in the list of primitives of its
1673 -- corresponding tagged type because they are only used
1674 -- to fill the contents of the secondary dispatch tables.
1675 -- Therefore they are removed from the homonym chains.
1676
1677 Set_Is_Hidden (New_Subp);
1678 Set_Is_Internal (New_Subp);
1679 Set_Alias (New_Subp, Prim);
1680 Set_Is_Abstract_Subprogram
1681 (New_Subp, Is_Abstract_Subprogram (Prim));
1682 Set_Interface_Alias (New_Subp, Iface_Prim);
1683
f6f4d8d4
JM
1684 -- If the returned type is an interface then propagate it to
1685 -- the returned type. Needed by the thunk to generate the code
1686 -- which displaces "this" to reference the corresponding
1687 -- secondary dispatch table in the returned object.
1688
1689 if Is_Interface (Etype (Iface_Prim)) then
1690 Set_Etype (New_Subp, Etype (Iface_Prim));
1691 end if;
1692
b4d7b435
AC
1693 -- Internal entities associated with interface types are
1694 -- only registered in the list of primitives of the tagged
1695 -- type. They are only used to fill the contents of the
1696 -- secondary dispatch tables. Therefore they are not needed
1697 -- in the homonym chains.
1698
1699 Remove_Homonym (New_Subp);
1700
1701 -- Hidden entities associated with interfaces must have set
1702 -- the Has_Delay_Freeze attribute to ensure that, in case of
1703 -- locally defined tagged types (or compiling with static
1704 -- dispatch tables generation disabled) the corresponding
1705 -- entry of the secondary dispatch table is filled when
1706 -- such an entity is frozen.
1707
1708 Set_Has_Delayed_Freeze (New_Subp);
1709 end if;
1710
c01817d2 1711 <<Continue>>
b4d7b435
AC
1712 Next_Elmt (Elmt);
1713 end loop;
3ff38f33
JM
1714
1715 Next_Elmt (Iface_Elmt);
1716 end loop;
74853971
AC
1717
1718 if Restore_Scope then
1719 Pop_Scope;
1720 end if;
3ff38f33
JM
1721 end Add_Internal_Interface_Entities;
1722
996ae0b0
RK
1723 -----------------------------------
1724 -- Analyze_Component_Declaration --
1725 -----------------------------------
1726
1727 procedure Analyze_Component_Declaration (N : Node_Id) is
176dadf6
AC
1728 Id : constant Entity_Id := Defining_Identifier (N);
1729 E : constant Node_Id := Expression (N);
1730 Typ : constant Node_Id :=
1731 Subtype_Indication (Component_Definition (N));
1732 T : Entity_Id;
1733 P : Entity_Id;
996ae0b0 1734
5d09245e
AC
1735 function Contains_POC (Constr : Node_Id) return Boolean;
1736 -- Determines whether a constraint uses the discriminant of a record
1737 -- type thus becoming a per-object constraint (POC).
1738
57193e09 1739 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
88b32fc3
BD
1740 -- Typ is the type of the current component, check whether this type is
1741 -- a limited type. Used to validate declaration against that of
1742 -- enclosing record.
57193e09 1743
5d09245e
AC
1744 ------------------
1745 -- Contains_POC --
1746 ------------------
1747
1748 function Contains_POC (Constr : Node_Id) return Boolean is
1749 begin
dc06abec 1750 -- Prevent cascaded errors
2b73cf68
JM
1751
1752 if Error_Posted (Constr) then
1753 return False;
1754 end if;
1755
5d09245e 1756 case Nkind (Constr) is
5d09245e 1757 when N_Attribute_Reference =>
2b73cf68
JM
1758 return
1759 Attribute_Name (Constr) = Name_Access
1760 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
5d09245e
AC
1761
1762 when N_Discriminant_Association =>
1763 return Denotes_Discriminant (Expression (Constr));
1764
1765 when N_Identifier =>
1766 return Denotes_Discriminant (Constr);
1767
1768 when N_Index_Or_Discriminant_Constraint =>
1769 declare
9dfd2ff8 1770 IDC : Node_Id;
71d9e9f2 1771
5d09245e 1772 begin
9dfd2ff8 1773 IDC := First (Constraints (Constr));
5d09245e
AC
1774 while Present (IDC) loop
1775
9dfd2ff8 1776 -- One per-object constraint is sufficient
5d09245e
AC
1777
1778 if Contains_POC (IDC) then
1779 return True;
1780 end if;
1781
1782 Next (IDC);
1783 end loop;
1784
1785 return False;
1786 end;
1787
1788 when N_Range =>
1789 return Denotes_Discriminant (Low_Bound (Constr))
71d9e9f2 1790 or else
5d09245e
AC
1791 Denotes_Discriminant (High_Bound (Constr));
1792
1793 when N_Range_Constraint =>
1794 return Denotes_Discriminant (Range_Expression (Constr));
1795
1796 when others =>
1797 return False;
1798
1799 end case;
1800 end Contains_POC;
1801
57193e09
TQ
1802 ----------------------
1803 -- Is_Known_Limited --
1804 ----------------------
1805
1806 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1807 P : constant Entity_Id := Etype (Typ);
1808 R : constant Entity_Id := Root_Type (Typ);
1809
1810 begin
1811 if Is_Limited_Record (Typ) then
1812 return True;
1813
1814 -- If the root type is limited (and not a limited interface)
1815 -- so is the current type
1816
1817 elsif Is_Limited_Record (R)
5ee96c9d 1818 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
57193e09
TQ
1819 then
1820 return True;
1821
1822 -- Else the type may have a limited interface progenitor, but a
1823 -- limited record parent.
1824
5ee96c9d 1825 elsif R /= P and then Is_Limited_Record (P) then
57193e09
TQ
1826 return True;
1827
1828 else
1829 return False;
1830 end if;
1831 end Is_Known_Limited;
1832
5d09245e
AC
1833 -- Start of processing for Analyze_Component_Declaration
1834
996ae0b0
RK
1835 begin
1836 Generate_Definition (Id);
1837 Enter_Name (Id);
6e937c1c 1838
d8b962d8 1839 if Present (Typ) then
db72f10a
AC
1840 T := Find_Type_Of_Object
1841 (Subtype_Indication (Component_Definition (N)), N);
1842
23685ae6 1843 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
2ba431e5 1844 Check_SPARK_Restriction ("subtype mark required", Typ);
d8b962d8
AC
1845 end if;
1846
0ab80019 1847 -- Ada 2005 (AI-230): Access Definition case
6e937c1c 1848
9bc856dd
AC
1849 else
1850 pragma Assert (Present
1851 (Access_Definition (Component_Definition (N))));
1852
6e937c1c
AC
1853 T := Access_Definition
1854 (Related_Nod => N,
1855 N => Access_Definition (Component_Definition (N)));
758c442c 1856 Set_Is_Local_Anonymous_Access (T);
35b7fa6a 1857
0ab80019 1858 -- Ada 2005 (AI-254)
7324bf49
AC
1859
1860 if Present (Access_To_Subprogram_Definition
1861 (Access_Definition (Component_Definition (N))))
1862 and then Protected_Present (Access_To_Subprogram_Definition
1863 (Access_Definition
1864 (Component_Definition (N))))
1865 then
fea9e956 1866 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
7324bf49 1867 end if;
6e937c1c 1868 end if;
996ae0b0 1869
fbf5a39b 1870 -- If the subtype is a constrained subtype of the enclosing record,
9dfd2ff8
CC
1871 -- (which must have a partial view) the back-end does not properly
1872 -- handle the recursion. Rewrite the component declaration with an
758c442c
GD
1873 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1874 -- the tree directly because side effects have already been removed from
1875 -- discriminant constraints.
fbf5a39b
AC
1876
1877 if Ekind (T) = E_Access_Subtype
a397db96 1878 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
fbf5a39b
AC
1879 and then Comes_From_Source (T)
1880 and then Nkind (Parent (T)) = N_Subtype_Declaration
1881 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1882 then
1883 Rewrite
a397db96 1884 (Subtype_Indication (Component_Definition (N)),
fbf5a39b 1885 New_Copy_Tree (Subtype_Indication (Parent (T))));
a397db96
AC
1886 T := Find_Type_Of_Object
1887 (Subtype_Indication (Component_Definition (N)), N);
fbf5a39b
AC
1888 end if;
1889
996ae0b0
RK
1890 -- If the component declaration includes a default expression, then we
1891 -- check that the component is not of a limited type (RM 3.7(5)),
1892 -- and do the special preanalysis of the expression (see section on
fbf5a39b
AC
1893 -- "Handling of Default and Per-Object Expressions" in the spec of
1894 -- package Sem).
996ae0b0 1895
2b73cf68 1896 if Present (E) then
2ba431e5 1897 Check_SPARK_Restriction ("default expression is not allowed", E);
ce4a6e84 1898 Preanalyze_Spec_Expression (E, T);
2b73cf68 1899 Check_Initialization (T, E);
57193e09 1900
0791fbe9 1901 if Ada_Version >= Ada_2005
57193e09 1902 and then Ekind (T) = E_Anonymous_Access_Type
3c829e3c 1903 and then Etype (E) /= Any_Type
57193e09
TQ
1904 then
1905 -- Check RM 3.9.2(9): "if the expected type for an expression is
1906 -- an anonymous access-to-specific tagged type, then the object
1907 -- designated by the expression shall not be dynamically tagged
1908 -- unless it is a controlling operand in a call on a dispatching
1909 -- operation"
1910
1911 if Is_Tagged_Type (Directly_Designated_Type (T))
1912 and then
1913 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1914 and then
2b73cf68
JM
1915 Ekind (Directly_Designated_Type (Etype (E))) =
1916 E_Class_Wide_Type
57193e09
TQ
1917 then
1918 Error_Msg_N
dc06abec 1919 ("access to specific tagged type required (RM 3.9.2(9))", E);
57193e09
TQ
1920 end if;
1921
1922 -- (Ada 2005: AI-230): Accessibility check for anonymous
1923 -- components
1924
f460d8f3 1925 if Type_Access_Level (Etype (E)) >
83e5da69
AC
1926 Deepest_Type_Access_Level (T)
1927 then
2b73cf68
JM
1928 Error_Msg_N
1929 ("expression has deeper access level than component " &
dc06abec 1930 "(RM 3.10.2 (12.2))", E);
2b73cf68
JM
1931 end if;
1932
1933 -- The initialization expression is a reference to an access
1934 -- discriminant. The type of the discriminant is always deeper
1935 -- than any access type.
88b32fc3 1936
2b73cf68
JM
1937 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1938 and then Is_Entity_Name (E)
1939 and then Ekind (Entity (E)) = E_In_Parameter
1940 and then Present (Discriminal_Link (Entity (E)))
57193e09
TQ
1941 then
1942 Error_Msg_N
2b73cf68
JM
1943 ("discriminant has deeper accessibility level than target",
1944 E);
57193e09
TQ
1945 end if;
1946 end if;
996ae0b0
RK
1947 end if;
1948
1949 -- The parent type may be a private view with unknown discriminants,
1950 -- and thus unconstrained. Regular components must be constrained.
1951
1952 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
8a6a52dc
AC
1953 if Is_Class_Wide_Type (T) then
1954 Error_Msg_N
1955 ("class-wide subtype with unknown discriminants" &
1956 " in component declaration",
1957 Subtype_Indication (Component_Definition (N)));
1958 else
1959 Error_Msg_N
1960 ("unconstrained subtype in component declaration",
1961 Subtype_Indication (Component_Definition (N)));
1962 end if;
996ae0b0
RK
1963
1964 -- Components cannot be abstract, except for the special case of
1965 -- the _Parent field (case of extending an abstract tagged type)
1966
fea9e956 1967 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
996ae0b0
RK
1968 Error_Msg_N ("type of a component cannot be abstract", N);
1969 end if;
1970
1971 Set_Etype (Id, T);
a397db96 1972 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
996ae0b0 1973
a5b62485
AC
1974 -- The component declaration may have a per-object constraint, set
1975 -- the appropriate flag in the defining identifier of the subtype.
5d09245e
AC
1976
1977 if Present (Subtype_Indication (Component_Definition (N))) then
1978 declare
1979 Sindic : constant Node_Id :=
71d9e9f2 1980 Subtype_Indication (Component_Definition (N));
5d09245e
AC
1981 begin
1982 if Nkind (Sindic) = N_Subtype_Indication
1983 and then Present (Constraint (Sindic))
1984 and then Contains_POC (Constraint (Sindic))
1985 then
1986 Set_Has_Per_Object_Constraint (Id);
1987 end if;
1988 end;
1989 end if;
1990
0ab80019 1991 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
71d9e9f2 1992 -- out some static checks.
2820d220 1993
5ee96c9d 1994 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2820d220
AC
1995 Null_Exclusion_Static_Checks (N);
1996 end if;
1997
758c442c
GD
1998 -- If this component is private (or depends on a private type), flag the
1999 -- record type to indicate that some operations are not available.
996ae0b0
RK
2000
2001 P := Private_Component (T);
2002
2003 if Present (P) then
030d25f4 2004
71d9e9f2 2005 -- Check for circular definitions
996ae0b0
RK
2006
2007 if P = Any_Type then
2008 Set_Etype (Id, Any_Type);
2009
2010 -- There is a gap in the visibility of operations only if the
2011 -- component type is not defined in the scope of the record type.
2012
2013 elsif Scope (P) = Scope (Current_Scope) then
2014 null;
2015
2016 elsif Is_Limited_Type (P) then
2017 Set_Is_Limited_Composite (Current_Scope);
2018
2019 else
2020 Set_Is_Private_Composite (Current_Scope);
2021 end if;
2022 end if;
2023
2024 if P /= Any_Type
2025 and then Is_Limited_Type (T)
2026 and then Chars (Id) /= Name_uParent
2027 and then Is_Tagged_Type (Current_Scope)
2028 then
2029 if Is_Derived_Type (Current_Scope)
57193e09 2030 and then not Is_Known_Limited (Current_Scope)
996ae0b0
RK
2031 then
2032 Error_Msg_N
2033 ("extension of nonlimited type cannot have limited components",
2034 N);
57193e09
TQ
2035
2036 if Is_Interface (Root_Type (Current_Scope)) then
2037 Error_Msg_N
2038 ("\limitedness is not inherited from limited interface", N);
ed2233dc 2039 Error_Msg_N ("\add LIMITED to type indication", N);
57193e09
TQ
2040 end if;
2041
fbf5a39b 2042 Explain_Limited_Type (T, N);
996ae0b0
RK
2043 Set_Etype (Id, Any_Type);
2044 Set_Is_Limited_Composite (Current_Scope, False);
2045
2046 elsif not Is_Derived_Type (Current_Scope)
2047 and then not Is_Limited_Record (Current_Scope)
653da906 2048 and then not Is_Concurrent_Type (Current_Scope)
996ae0b0 2049 then
fbf5a39b
AC
2050 Error_Msg_N
2051 ("nonlimited tagged type cannot have limited components", N);
2052 Explain_Limited_Type (T, N);
996ae0b0
RK
2053 Set_Etype (Id, Any_Type);
2054 Set_Is_Limited_Composite (Current_Scope, False);
2055 end if;
2056 end if;
2057
2058 Set_Original_Record_Component (Id, Id);
eaba57fb
RD
2059
2060 if Has_Aspects (N) then
2061 Analyze_Aspect_Specifications (N, Id);
2062 end if;
54c04d6c 2063
dec6faf1 2064 Analyze_Dimension (N);
996ae0b0
RK
2065 end Analyze_Component_Declaration;
2066
2067 --------------------------
2068 -- Analyze_Declarations --
2069 --------------------------
2070
2071 procedure Analyze_Declarations (L : List_Id) is
ea3c0651 2072 Decl : Node_Id;
996ae0b0 2073
ea3c0651
AC
2074 procedure Adjust_Decl;
2075 -- Adjust Decl not to include implicit label declarations, since these
996ae0b0 2076 -- have strange Sloc values that result in elaboration check problems.
fbf5a39b
AC
2077 -- (They have the sloc of the label as found in the source, and that
2078 -- is ahead of the current declarative part).
2079
fba9ebfc
AC
2080 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2081 -- Determine whether Body_Decl denotes the body of a late controlled
2082 -- primitive (either Initialize, Adjust or Finalize). If this is the
2083 -- case, add a proper spec if the body lacks one. The spec is inserted
2084 -- before Body_Decl and immedately analyzed.
2085
c5c0ce68
HK
2086 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2087 -- Spec_Id is the entity of a package that may define abstract states.
2088 -- If the states have visible refinement, remove the visibility of each
2089 -- constituent at the end of the package body declarations.
2090
ea3c0651
AC
2091 -----------------
2092 -- Adjust_Decl --
2093 -----------------
996ae0b0 2094
ea3c0651 2095 procedure Adjust_Decl is
996ae0b0 2096 begin
ea3c0651
AC
2097 while Present (Prev (Decl))
2098 and then Nkind (Decl) = N_Implicit_Label_Declaration
996ae0b0 2099 loop
ea3c0651 2100 Prev (Decl);
996ae0b0 2101 end loop;
ea3c0651
AC
2102 end Adjust_Decl;
2103
fba9ebfc
AC
2104 --------------------------------------
2105 -- Handle_Late_Controlled_Primitive --
2106 --------------------------------------
2107
2108 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2109 Body_Spec : constant Node_Id := Specification (Body_Decl);
2110 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2111 Loc : constant Source_Ptr := Sloc (Body_Id);
2112 Params : constant List_Id :=
2113 Parameter_Specifications (Body_Spec);
79b49b87 2114 Spec : Node_Id;
fba9ebfc
AC
2115 Spec_Id : Entity_Id;
2116
2117 Dummy : Entity_Id;
2118 pragma Unreferenced (Dummy);
2119 -- A dummy variable used to capture the unused result of subprogram
2120 -- spec analysis.
2121
2122 begin
4446a13f
AC
2123 -- Consider only procedure bodies whose name matches one of the three
2124 -- controlled primitives.
fba9ebfc
AC
2125
2126 if Nkind (Body_Spec) /= N_Procedure_Specification
2127 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2128 Name_Finalize,
2129 Name_Initialize)
2130 then
2131 return;
2132
4446a13f 2133 -- A controlled primitive must have exactly one formal
fba9ebfc
AC
2134
2135 elsif List_Length (Params) /= 1 then
2136 return;
2137 end if;
2138
2139 Dummy := Analyze_Subprogram_Specification (Body_Spec);
2140
4446a13f
AC
2141 -- The type of the formal must be derived from [Limited_]Controlled
2142
fba9ebfc
AC
2143 if not Is_Controlled (Etype (Defining_Entity (First (Params)))) then
2144 return;
2145 end if;
2146
2147 Spec_Id := Find_Corresponding_Spec (Body_Decl, Post_Error => False);
2148
2149 -- The body has a matching spec, therefore it cannot be a late
2150 -- primitive.
2151
2152 if Present (Spec_Id) then
2153 return;
2154 end if;
2155
2156 -- At this point the body is known to be a late controlled primitive.
4446a13f
AC
2157 -- Generate a matching spec and insert it before the body. Note the
2158 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2159 -- tree in this case.
fba9ebfc 2160
79b49b87
HK
2161 Spec := Copy_Separate_Tree (Body_Spec);
2162
2163 -- Ensure that the subprogram declaration does not inherit the null
8c35b40a 2164 -- indicator from the body as we now have a proper spec/body pair.
79b49b87
HK
2165
2166 Set_Null_Present (Spec, False);
2167
fba9ebfc
AC
2168 Insert_Before_And_Analyze (Body_Decl,
2169 Make_Subprogram_Declaration (Loc,
79b49b87 2170 Specification => Spec));
fba9ebfc
AC
2171 end Handle_Late_Controlled_Primitive;
2172
c5c0ce68
HK
2173 --------------------------------
2174 -- Remove_Visible_Refinements --
2175 --------------------------------
2176
2177 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2178 State_Elmt : Elmt_Id;
c5c0ce68
HK
2179 begin
2180 if Present (Abstract_States (Spec_Id)) then
2181 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2182 while Present (State_Elmt) loop
2183 Set_Has_Visible_Refinement (Node (State_Elmt), False);
c5c0ce68
HK
2184 Next_Elmt (State_Elmt);
2185 end loop;
2186 end if;
2187 end Remove_Visible_Refinements;
2188
ea3c0651
AC
2189 -- Local variables
2190
39af2bac 2191 Context : Node_Id;
ea3c0651
AC
2192 Freeze_From : Entity_Id := Empty;
2193 Next_Decl : Node_Id;
39af2bac 2194 Spec_Id : Entity_Id;
996ae0b0 2195
fba9ebfc
AC
2196 Body_Seen : Boolean := False;
2197 -- Flag set when the first body [stub] is encountered
2198
c5c0ce68
HK
2199 In_Package_Body : Boolean := False;
2200 -- Flag set when the current declaration list belongs to a package body
2201
996ae0b0
RK
2202 -- Start of processing for Analyze_Declarations
2203
2204 begin
6480338a 2205 if Restriction_Check_Required (SPARK_05) then
23685ae6
AC
2206 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2207 end if;
2208
ea3c0651
AC
2209 Decl := First (L);
2210 while Present (Decl) loop
996ae0b0 2211
a54d0eb4 2212 -- Package spec cannot contain a package declaration in SPARK
8ed68165 2213
ea3c0651 2214 if Nkind (Decl) = N_Package_Declaration
8ed68165
AC
2215 and then Nkind (Parent (L)) = N_Package_Specification
2216 then
a54d0eb4
AC
2217 Check_SPARK_Restriction
2218 ("package specification cannot contain a package declaration",
ea3c0651 2219 Decl);
8ed68165
AC
2220 end if;
2221
996ae0b0
RK
2222 -- Complete analysis of declaration
2223
ea3c0651
AC
2224 Analyze (Decl);
2225 Next_Decl := Next (Decl);
996ae0b0
RK
2226
2227 if No (Freeze_From) then
2228 Freeze_From := First_Entity (Current_Scope);
2229 end if;
2230
2231 -- At the end of a declarative part, freeze remaining entities
a5b62485
AC
2232 -- declared in it. The end of the visible declarations of package
2233 -- specification is not the end of a declarative part if private
2234 -- declarations are present. The end of a package declaration is a
2235 -- freezing point only if it a library package. A task definition or
2236 -- protected type definition is not a freeze point either. Finally,
2237 -- we do not freeze entities in generic scopes, because there is no
2238 -- code generated for them and freeze nodes will be generated for
2239 -- the instance.
996ae0b0
RK
2240
2241 -- The end of a package instantiation is not a freeze point, but
2242 -- for now we make it one, because the generic body is inserted
2243 -- (currently) immediately after. Generic instantiations will not
2244 -- be a freeze point once delayed freezing of bodies is implemented.
2245 -- (This is needed in any case for early instantiations ???).
2246
ea3c0651 2247 if No (Next_Decl) then
7d7af38a
JM
2248 if Nkind_In (Parent (L), N_Component_List,
2249 N_Task_Definition,
2250 N_Protected_Definition)
996ae0b0
RK
2251 then
2252 null;
2253
2254 elsif Nkind (Parent (L)) /= N_Package_Specification then
996ae0b0
RK
2255 if Nkind (Parent (L)) = N_Package_Body then
2256 Freeze_From := First_Entity (Current_Scope);
2257 end if;
2258
ad4e3362
ES
2259 -- There may have been several freezing points previously,
2260 -- for example object declarations or subprogram bodies, but
2261 -- at the end of a declarative part we check freezing from
2262 -- the beginning, even though entities may already be frozen,
2263 -- in order to perform visibility checks on delayed aspects.
2264
ea3c0651 2265 Adjust_Decl;
ad4e3362 2266 Freeze_All (First_Entity (Current_Scope), Decl);
996ae0b0
RK
2267 Freeze_From := Last_Entity (Current_Scope);
2268
2269 elsif Scope (Current_Scope) /= Standard_Standard
2270 and then not Is_Child_Unit (Current_Scope)
2271 and then No (Generic_Parent (Parent (L)))
2272 then
2273 null;
2274
2275 elsif L /= Visible_Declarations (Parent (L))
2276 or else No (Private_Declarations (Parent (L)))
2277 or else Is_Empty_List (Private_Declarations (Parent (L)))
2278 then
ea3c0651 2279 Adjust_Decl;
ad4e3362 2280 Freeze_All (First_Entity (Current_Scope), Decl);
996ae0b0
RK
2281 Freeze_From := Last_Entity (Current_Scope);
2282 end if;
2283
2284 -- If next node is a body then freeze all types before the body.
fea9e956
ES
2285 -- An exception occurs for some expander-generated bodies. If these
2286 -- are generated at places where in general language rules would not
2287 -- allow a freeze point, then we assume that the expander has
2288 -- explicitly checked that all required types are properly frozen,
2289 -- and we do not cause general freezing here. This special circuit
2290 -- is used when the encountered body is marked as having already
d976bf74 2291 -- been analyzed.
fea9e956
ES
2292
2293 -- In all other cases (bodies that come from source, and expander
2294 -- generated bodies that have not been analyzed yet), freeze all
2295 -- types now. Note that in the latter case, the expander must take
2296 -- care to attach the bodies at a proper place in the tree so as to
2297 -- not cause unwanted freezing at that point.
996ae0b0 2298
fba9ebfc
AC
2299 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2300
2301 -- When a controlled type is frozen, the expander generates stream
2302 -- and controlled type support routines. If the freeze is caused
2303 -- by the stand alone body of Initialize, Adjust and Finalize, the
2304 -- expander will end up using the wrong version of these routines
2305 -- as the body has not been processed yet. To remedy this, detect
2306 -- a late controlled primitive and create a proper spec for it.
2307 -- This ensures that the primitive will override its inherited
2308 -- counterpart before the freeze takes place.
2309
53c53f6d
AC
2310 -- If the declaration we just processed is a body, do not attempt
2311 -- to examine Next_Decl as the late primitive idiom can only apply
2312 -- to the first encountered body.
2313
2314 -- The spec of the late primitive is not generated in ASIS mode to
2315 -- ensure a consistent list of primitives that indicates the true
2316 -- semantic structure of the program (which is not relevant when
2317 -- generating executable code.
2318
fba9ebfc
AC
2319 -- ??? a cleaner approach may be possible and/or this solution
2320 -- could be extended to general-purpose late primitives, TBD.
2321
53c53f6d
AC
2322 if not ASIS_Mode
2323 and then not Body_Seen
2324 and then not Is_Body (Decl)
2325 then
fba9ebfc
AC
2326 Body_Seen := True;
2327
2328 if Nkind (Next_Decl) = N_Subprogram_Body then
2329 Handle_Late_Controlled_Primitive (Next_Decl);
2330 end if;
2331 end if;
2332
ea3c0651
AC
2333 Adjust_Decl;
2334 Freeze_All (Freeze_From, Decl);
996ae0b0
RK
2335 Freeze_From := Last_Entity (Current_Scope);
2336 end if;
2337
ea3c0651 2338 Decl := Next_Decl;
996ae0b0 2339 end loop;
1fb00064 2340
d7af5ea5
HK
2341 -- Analyze the contracts of packages and their bodies
2342
39af2bac
AC
2343 if Present (L) then
2344 Context := Parent (L);
2345
476b301a
AC
2346 if Nkind (Context) = N_Package_Specification then
2347
2348 -- When a package has private declarations, its contract must be
2349 -- analyzed at the end of the said declarations. This way both the
2350 -- analysis and freeze actions are properly synchronized in case
2351 -- of private type use within the contract.
2352
2353 if L = Private_Declarations (Context) then
2354 Analyze_Package_Contract (Defining_Entity (Context));
2355
2356 -- Otherwise the contract is analyzed at the end of the visible
2357 -- declarations.
2358
2359 elsif L = Visible_Declarations (Context)
2360 and then No (Private_Declarations (Context))
2361 then
2362 Analyze_Package_Contract (Defining_Entity (Context));
2363 end if;
54e28df2
HK
2364
2365 elsif Nkind (Context) = N_Package_Body then
c5c0ce68 2366 In_Package_Body := True;
39af2bac 2367 Spec_Id := Corresponding_Spec (Context);
39af2bac 2368
d7af5ea5 2369 Analyze_Package_Body_Contract (Defining_Entity (Context));
39af2bac
AC
2370 end if;
2371 end if;
2372
6c3c671e
AC
2373 -- Analyze the contracts of subprogram declarations, subprogram bodies
2374 -- and variables now due to the delayed visibility requirements of their
2375 -- aspects.
1fb00064 2376
ea3c0651
AC
2377 Decl := First (L);
2378 while Present (Decl) loop
d7af5ea5
HK
2379 if Nkind (Decl) = N_Object_Declaration then
2380 Analyze_Object_Contract (Defining_Entity (Decl));
2381
2382 elsif Nkind (Decl) = N_Subprogram_Body then
ab8843fa 2383 Analyze_Subprogram_Body_Contract (Defining_Entity (Decl));
1fb00064 2384
1037b0f4
AC
2385 elsif Nkind_In (Decl, N_Subprogram_Declaration,
2386 N_Abstract_Subprogram_Declaration)
ebb6b0bd 2387 then
ab8843fa 2388 Analyze_Subprogram_Contract (Defining_Entity (Decl));
ea3c0651 2389 end if;
1fb00064 2390
ea3c0651
AC
2391 Next (Decl);
2392 end loop;
c5c0ce68
HK
2393
2394 -- State refinements are visible upto the end the of the package body
2395 -- declarations. Hide the refinements from visibility to restore the
2396 -- original state conditions.
2397
2398 if In_Package_Body then
2399 Remove_Visible_Refinements (Spec_Id);
2400 end if;
996ae0b0
RK
2401 end Analyze_Declarations;
2402
0f1a6a0b
AC
2403 -----------------------------------
2404 -- Analyze_Full_Type_Declaration --
2405 -----------------------------------
996ae0b0 2406
0f1a6a0b
AC
2407 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2408 Def : constant Node_Id := Type_Definition (N);
2409 Def_Id : constant Entity_Id := Defining_Identifier (N);
0f1a6a0b
AC
2410 T : Entity_Id;
2411 Prev : Entity_Id;
996ae0b0 2412
0f1a6a0b
AC
2413 Is_Remote : constant Boolean :=
2414 (Is_Remote_Types (Current_Scope)
2415 or else Is_Remote_Call_Interface (Current_Scope))
5ee96c9d
TQ
2416 and then not (In_Private_Part (Current_Scope)
2417 or else In_Package_Body (Current_Scope));
996ae0b0 2418
0f1a6a0b 2419 procedure Check_Ops_From_Incomplete_Type;
4637729f
AC
2420 -- If there is a tagged incomplete partial view of the type, traverse
2421 -- the primitives of the incomplete view and change the type of any
2422 -- controlling formals and result to indicate the full view. The
2423 -- primitives will be added to the full type's primitive operations
2424 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2425 -- is called from Process_Incomplete_Dependents).
996ae0b0 2426
0f1a6a0b
AC
2427 ------------------------------------
2428 -- Check_Ops_From_Incomplete_Type --
2429 ------------------------------------
996ae0b0 2430
0f1a6a0b
AC
2431 procedure Check_Ops_From_Incomplete_Type is
2432 Elmt : Elmt_Id;
2433 Formal : Entity_Id;
2434 Op : Entity_Id;
996ae0b0 2435
0f1a6a0b
AC
2436 begin
2437 if Prev /= T
2438 and then Ekind (Prev) = E_Incomplete_Type
2439 and then Is_Tagged_Type (Prev)
2440 and then Is_Tagged_Type (T)
2441 then
2442 Elmt := First_Elmt (Primitive_Operations (Prev));
2443 while Present (Elmt) loop
2444 Op := Node (Elmt);
d8db0bca 2445
0f1a6a0b
AC
2446 Formal := First_Formal (Op);
2447 while Present (Formal) loop
2448 if Etype (Formal) = Prev then
2449 Set_Etype (Formal, T);
2450 end if;
d8db0bca 2451
0f1a6a0b
AC
2452 Next_Formal (Formal);
2453 end loop;
d8db0bca 2454
0f1a6a0b
AC
2455 if Etype (Op) = Prev then
2456 Set_Etype (Op, T);
2457 end if;
996ae0b0 2458
0f1a6a0b
AC
2459 Next_Elmt (Elmt);
2460 end loop;
2461 end if;
2462 end Check_Ops_From_Incomplete_Type;
996ae0b0 2463
0f1a6a0b 2464 -- Start of processing for Analyze_Full_Type_Declaration
996ae0b0 2465
0f1a6a0b
AC
2466 begin
2467 Prev := Find_Type_Name (N);
996ae0b0 2468
0f1a6a0b 2469 -- The full view, if present, now points to the current type
996ae0b0 2470
0f1a6a0b
AC
2471 -- Ada 2005 (AI-50217): If the type was previously decorated when
2472 -- imported through a LIMITED WITH clause, it appears as incomplete
2473 -- but has no full view.
996ae0b0 2474
0f1a6a0b
AC
2475 if Ekind (Prev) = E_Incomplete_Type
2476 and then Present (Full_View (Prev))
2477 then
2478 T := Full_View (Prev);
0f1a6a0b
AC
2479 else
2480 T := Prev;
2481 end if;
950d3e7d 2482
0f1a6a0b 2483 Set_Is_Pure (T, Is_Pure (Current_Scope));
950d3e7d 2484
0f1a6a0b
AC
2485 -- We set the flag Is_First_Subtype here. It is needed to set the
2486 -- corresponding flag for the Implicit class-wide-type created
2487 -- during tagged types processing.
950d3e7d 2488
0f1a6a0b 2489 Set_Is_First_Subtype (T, True);
2b73cf68 2490
0f1a6a0b
AC
2491 -- Only composite types other than array types are allowed to have
2492 -- discriminants.
2b73cf68 2493
0f1a6a0b 2494 case Nkind (Def) is
2b73cf68 2495
0f1a6a0b
AC
2496 -- For derived types, the rule will be checked once we've figured
2497 -- out the parent type.
2b73cf68 2498
0f1a6a0b
AC
2499 when N_Derived_Type_Definition =>
2500 null;
ce4a6e84 2501
fe5d3068 2502 -- For record types, discriminants are allowed, unless we are in
2ba431e5 2503 -- SPARK.
ce4a6e84 2504
0f1a6a0b 2505 when N_Record_Definition =>
fe5d3068 2506 if Present (Discriminant_Specifications (N)) then
2ba431e5 2507 Check_SPARK_Restriction
fe5d3068
YM
2508 ("discriminant type is not allowed",
2509 Defining_Identifier
277c9abe 2510 (First (Discriminant_Specifications (N))));
fe5d3068 2511 end if;
950d3e7d 2512
0f1a6a0b
AC
2513 when others =>
2514 if Present (Discriminant_Specifications (N)) then
2515 Error_Msg_N
2516 ("elementary or array type cannot have discriminants",
2517 Defining_Identifier
277c9abe 2518 (First (Discriminant_Specifications (N))));
0f1a6a0b
AC
2519 end if;
2520 end case;
996ae0b0 2521
0f1a6a0b
AC
2522 -- Elaborate the type definition according to kind, and generate
2523 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2524 -- already done (this happens during the reanalysis that follows a call
2525 -- to the high level optimizer).
996ae0b0 2526
0f1a6a0b
AC
2527 if not Analyzed (T) then
2528 Set_Analyzed (T);
996ae0b0 2529
0f1a6a0b 2530 case Nkind (Def) is
996ae0b0 2531
0f1a6a0b
AC
2532 when N_Access_To_Subprogram_Definition =>
2533 Access_Subprogram_Declaration (T, Def);
996ae0b0 2534
0f1a6a0b
AC
2535 -- If this is a remote access to subprogram, we must create the
2536 -- equivalent fat pointer type, and related subprograms.
996ae0b0 2537
0f1a6a0b
AC
2538 if Is_Remote then
2539 Process_Remote_AST_Declaration (N);
2540 end if;
996ae0b0 2541
0f1a6a0b
AC
2542 -- Validate categorization rule against access type declaration
2543 -- usually a violation in Pure unit, Shared_Passive unit.
996ae0b0 2544
0f1a6a0b 2545 Validate_Access_Type_Declaration (T, N);
996ae0b0 2546
0f1a6a0b
AC
2547 when N_Access_To_Object_Definition =>
2548 Access_Type_Declaration (T, Def);
996ae0b0 2549
0f1a6a0b
AC
2550 -- Validate categorization rule against access type declaration
2551 -- usually a violation in Pure unit, Shared_Passive unit.
ce9e9122 2552
0f1a6a0b 2553 Validate_Access_Type_Declaration (T, N);
ce9e9122 2554
0f1a6a0b
AC
2555 -- If we are in a Remote_Call_Interface package and define a
2556 -- RACW, then calling stubs and specific stream attributes
2557 -- must be added.
ce9e9122 2558
0f1a6a0b
AC
2559 if Is_Remote
2560 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2561 then
2562 Add_RACW_Features (Def_Id);
2563 end if;
996ae0b0 2564
0f1a6a0b 2565 -- Set no strict aliasing flag if config pragma seen
996ae0b0 2566
0f1a6a0b
AC
2567 if Opt.No_Strict_Aliasing then
2568 Set_No_Strict_Aliasing (Base_Type (Def_Id));
2569 end if;
996ae0b0 2570
0f1a6a0b
AC
2571 when N_Array_Type_Definition =>
2572 Array_Type_Declaration (T, Def);
996ae0b0 2573
0f1a6a0b
AC
2574 when N_Derived_Type_Definition =>
2575 Derived_Type_Declaration (T, N, T /= Def_Id);
996ae0b0 2576
0f1a6a0b
AC
2577 when N_Enumeration_Type_Definition =>
2578 Enumeration_Type_Declaration (T, Def);
996ae0b0 2579
0f1a6a0b
AC
2580 when N_Floating_Point_Definition =>
2581 Floating_Point_Type_Declaration (T, Def);
996ae0b0 2582
0f1a6a0b
AC
2583 when N_Decimal_Fixed_Point_Definition =>
2584 Decimal_Fixed_Point_Type_Declaration (T, Def);
996ae0b0 2585
0f1a6a0b
AC
2586 when N_Ordinary_Fixed_Point_Definition =>
2587 Ordinary_Fixed_Point_Type_Declaration (T, Def);
996ae0b0 2588
0f1a6a0b
AC
2589 when N_Signed_Integer_Type_Definition =>
2590 Signed_Integer_Type_Declaration (T, Def);
996ae0b0 2591
0f1a6a0b
AC
2592 when N_Modular_Type_Definition =>
2593 Modular_Type_Declaration (T, Def);
996ae0b0 2594
0f1a6a0b
AC
2595 when N_Record_Definition =>
2596 Record_Type_Declaration (T, N, Prev);
996ae0b0 2597
0f1a6a0b 2598 -- If declaration has a parse error, nothing to elaborate.
996ae0b0 2599
0f1a6a0b
AC
2600 when N_Error =>
2601 null;
996ae0b0 2602
0f1a6a0b
AC
2603 when others =>
2604 raise Program_Error;
fbf5a39b 2605
0f1a6a0b 2606 end case;
996ae0b0
RK
2607 end if;
2608
0f1a6a0b 2609 if Etype (T) = Any_Type then
4818e7b9 2610 return;
996ae0b0
RK
2611 end if;
2612
2ba431e5 2613 -- Controlled type is not allowed in SPARK
8ed68165 2614
fe5d3068 2615 if Is_Visibly_Controlled (T) then
2ba431e5 2616 Check_SPARK_Restriction ("controlled type is not allowed", N);
8ed68165
AC
2617 end if;
2618
0f1a6a0b 2619 -- Some common processing for all types
996ae0b0 2620
0f1a6a0b
AC
2621 Set_Depends_On_Private (T, Has_Private_Component (T));
2622 Check_Ops_From_Incomplete_Type;
996ae0b0 2623
0f1a6a0b
AC
2624 -- Both the declared entity, and its anonymous base type if one
2625 -- was created, need freeze nodes allocated.
996ae0b0 2626
0f1a6a0b
AC
2627 declare
2628 B : constant Entity_Id := Base_Type (T);
996ae0b0 2629
0f1a6a0b
AC
2630 begin
2631 -- In the case where the base type differs from the first subtype, we
2632 -- pre-allocate a freeze node, and set the proper link to the first
2633 -- subtype. Freeze_Entity will use this preallocated freeze node when
2634 -- it freezes the entity.
996ae0b0 2635
0f1a6a0b
AC
2636 -- This does not apply if the base type is a generic type, whose
2637 -- declaration is independent of the current derived definition.
6e937c1c 2638
0f1a6a0b
AC
2639 if B /= T and then not Is_Generic_Type (B) then
2640 Ensure_Freeze_Node (B);
2641 Set_First_Subtype_Link (Freeze_Node (B), T);
2642 end if;
6e937c1c 2643
0f1a6a0b
AC
2644 -- A type that is imported through a limited_with clause cannot
2645 -- generate any code, and thus need not be frozen. However, an access
2646 -- type with an imported designated type needs a finalization list,
2647 -- which may be referenced in some other package that has non-limited
2648 -- visibility on the designated type. Thus we must create the
2649 -- finalization list at the point the access type is frozen, to
2650 -- prevent unsatisfied references at link time.
6e937c1c 2651
7b56a91b 2652 if not From_Limited_With (T) or else Is_Access_Type (T) then
0f1a6a0b
AC
2653 Set_Has_Delayed_Freeze (T);
2654 end if;
2655 end;
6e937c1c 2656
0f1a6a0b
AC
2657 -- Case where T is the full declaration of some private type which has
2658 -- been swapped in Defining_Identifier (N).
6e937c1c 2659
0f1a6a0b
AC
2660 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2661 Process_Full_View (N, T, Def_Id);
6e937c1c 2662
0f1a6a0b
AC
2663 -- Record the reference. The form of this is a little strange, since
2664 -- the full declaration has been swapped in. So the first parameter
2665 -- here represents the entity to which a reference is made which is
2666 -- the "real" entity, i.e. the one swapped in, and the second
2667 -- parameter provides the reference location.
6e937c1c 2668
0f1a6a0b
AC
2669 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2670 -- since we don't want a complaint about the full type being an
2671 -- unwanted reference to the private type
6e937c1c 2672
0f1a6a0b
AC
2673 declare
2674 B : constant Boolean := Has_Pragma_Unreferenced (T);
2675 begin
2676 Set_Has_Pragma_Unreferenced (T, False);
2677 Generate_Reference (T, T, 'c');
2678 Set_Has_Pragma_Unreferenced (T, B);
2679 end;
6e937c1c 2680
0f1a6a0b 2681 Set_Completion_Referenced (Def_Id);
6e937c1c 2682
0f1a6a0b
AC
2683 -- For completion of incomplete type, process incomplete dependents
2684 -- and always mark the full type as referenced (it is the incomplete
2685 -- type that we get for any real reference).
6e937c1c 2686
0f1a6a0b
AC
2687 elsif Ekind (Prev) = E_Incomplete_Type then
2688 Process_Incomplete_Dependents (N, T, Prev);
2689 Generate_Reference (Prev, Def_Id, 'c');
2690 Set_Completion_Referenced (Def_Id);
6e937c1c 2691
0f1a6a0b
AC
2692 -- If not private type or incomplete type completion, this is a real
2693 -- definition of a new entity, so record it.
996ae0b0 2694
0f1a6a0b
AC
2695 else
2696 Generate_Definition (Def_Id);
2697 end if;
996ae0b0 2698
0f1a6a0b
AC
2699 if Chars (Scope (Def_Id)) = Name_System
2700 and then Chars (Def_Id) = Name_Address
2701 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2702 then
2703 Set_Is_Descendent_Of_Address (Def_Id);
2704 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2705 Set_Is_Descendent_Of_Address (Prev);
2706 end if;
996ae0b0 2707
0f1a6a0b
AC
2708 Set_Optimize_Alignment_Flags (Def_Id);
2709 Check_Eliminated (Def_Id);
996ae0b0 2710
deb4f5ba
ES
2711 -- If the declaration is a completion and aspects are present, apply
2712 -- them to the entity for the type which is currently the partial
2713 -- view, but which is the one that will be frozen.
2714
eaba57fb 2715 if Has_Aspects (N) then
deb4f5ba
ES
2716 if Prev /= Def_Id then
2717 Analyze_Aspect_Specifications (N, Prev);
2718 else
2719 Analyze_Aspect_Specifications (N, Def_Id);
2720 end if;
eaba57fb 2721 end if;
0f1a6a0b 2722 end Analyze_Full_Type_Declaration;
996ae0b0 2723
0f1a6a0b
AC
2724 ----------------------------------
2725 -- Analyze_Incomplete_Type_Decl --
2726 ----------------------------------
996ae0b0 2727
0f1a6a0b
AC
2728 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2729 F : constant Boolean := Is_Pure (Current_Scope);
2730 T : Entity_Id;
996ae0b0 2731
0f1a6a0b 2732 begin
2ba431e5 2733 Check_SPARK_Restriction ("incomplete type is not allowed", N);
7ff2d234 2734
0f1a6a0b 2735 Generate_Definition (Defining_Identifier (N));
5a989c6b 2736
0f1a6a0b
AC
2737 -- Process an incomplete declaration. The identifier must not have been
2738 -- declared already in the scope. However, an incomplete declaration may
2739 -- appear in the private part of a package, for a private type that has
2740 -- already been declared.
ce4a6e84 2741
0f1a6a0b 2742 -- In this case, the discriminants (if any) must match
ce4a6e84 2743
0f1a6a0b 2744 T := Find_Type_Name (N);
5a989c6b 2745
0f1a6a0b
AC
2746 Set_Ekind (T, E_Incomplete_Type);
2747 Init_Size_Align (T);
2748 Set_Is_First_Subtype (T, True);
2749 Set_Etype (T, T);
5a989c6b 2750
0f1a6a0b
AC
2751 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2752 -- incomplete types.
2753
2754 if Tagged_Present (N) then
2755 Set_Is_Tagged_Type (T);
2756 Make_Class_Wide_Type (T);
ef2a63ba 2757 Set_Direct_Primitive_Operations (T, New_Elmt_List);
996ae0b0
RK
2758 end if;
2759
0f1a6a0b 2760 Push_Scope (T);
996ae0b0 2761
0f1a6a0b 2762 Set_Stored_Constraint (T, No_Elist);
996ae0b0 2763
0f1a6a0b
AC
2764 if Present (Discriminant_Specifications (N)) then
2765 Process_Discriminants (N);
2766 end if;
71d9e9f2 2767
0f1a6a0b 2768 End_Scope;
996ae0b0 2769
0f1a6a0b
AC
2770 -- If the type has discriminants, non-trivial subtypes may be
2771 -- declared before the full view of the type. The full views of those
2772 -- subtypes will be built after the full view of the type.
996ae0b0 2773
0f1a6a0b 2774 Set_Private_Dependents (T, New_Elmt_List);
df3e68b1 2775 Set_Is_Pure (T, F);
0f1a6a0b 2776 end Analyze_Incomplete_Type_Decl;
996ae0b0 2777
0f1a6a0b
AC
2778 -----------------------------------
2779 -- Analyze_Interface_Declaration --
2780 -----------------------------------
996ae0b0 2781
0f1a6a0b
AC
2782 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2783 CW : constant Entity_Id := Class_Wide_Type (T);
dc06abec 2784
0f1a6a0b
AC
2785 begin
2786 Set_Is_Tagged_Type (T);
996ae0b0 2787
0f1a6a0b
AC
2788 Set_Is_Limited_Record (T, Limited_Present (Def)
2789 or else Task_Present (Def)
2790 or else Protected_Present (Def)
2791 or else Synchronized_Present (Def));
fea9e956 2792
0f1a6a0b
AC
2793 -- Type is abstract if full declaration carries keyword, or if previous
2794 -- partial view did.
996ae0b0 2795
0f1a6a0b
AC
2796 Set_Is_Abstract_Type (T);
2797 Set_Is_Interface (T);
2820d220 2798
0f1a6a0b
AC
2799 -- Type is a limited interface if it includes the keyword limited, task,
2800 -- protected, or synchronized.
9dfd2ff8 2801
0f1a6a0b
AC
2802 Set_Is_Limited_Interface
2803 (T, Limited_Present (Def)
2804 or else Protected_Present (Def)
2805 or else Synchronized_Present (Def)
2806 or else Task_Present (Def));
9dfd2ff8 2807
0f1a6a0b 2808 Set_Interfaces (T, New_Elmt_List);
ef2a63ba 2809 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2820d220 2810
0f1a6a0b
AC
2811 -- Complete the decoration of the class-wide entity if it was already
2812 -- built (i.e. during the creation of the limited view)
996ae0b0 2813
0f1a6a0b
AC
2814 if Present (CW) then
2815 Set_Is_Interface (CW);
2816 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2817 end if;
996ae0b0 2818
0f1a6a0b
AC
2819 -- Check runtime support for synchronized interfaces
2820
2821 if VM_Target = No_VM
2822 and then (Is_Task_Interface (T)
5ee96c9d
TQ
2823 or else Is_Protected_Interface (T)
2824 or else Is_Synchronized_Interface (T))
0f1a6a0b 2825 and then not RTE_Available (RE_Select_Specific_Data)
996ae0b0 2826 then
0f1a6a0b
AC
2827 Error_Msg_CRT ("synchronized interfaces", T);
2828 end if;
2829 end Analyze_Interface_Declaration;
33931112 2830
0f1a6a0b
AC
2831 -----------------------------
2832 -- Analyze_Itype_Reference --
2833 -----------------------------
33931112 2834
0f1a6a0b
AC
2835 -- Nothing to do. This node is placed in the tree only for the benefit of
2836 -- back end processing, and has no effect on the semantic processing.
33931112 2837
0f1a6a0b
AC
2838 procedure Analyze_Itype_Reference (N : Node_Id) is
2839 begin
2840 pragma Assert (Is_Itype (Itype (N)));
2841 null;
2842 end Analyze_Itype_Reference;
996ae0b0 2843
0f1a6a0b
AC
2844 --------------------------------
2845 -- Analyze_Number_Declaration --
2846 --------------------------------
996ae0b0 2847
0f1a6a0b
AC
2848 procedure Analyze_Number_Declaration (N : Node_Id) is
2849 Id : constant Entity_Id := Defining_Identifier (N);
2850 E : constant Node_Id := Expression (N);
2851 T : Entity_Id;
2852 Index : Interp_Index;
2853 It : Interp;
996ae0b0 2854
0f1a6a0b
AC
2855 begin
2856 Generate_Definition (Id);
2857 Enter_Name (Id);
996ae0b0 2858
0f1a6a0b 2859 -- This is an optimization of a common case of an integer literal
996ae0b0 2860
0f1a6a0b
AC
2861 if Nkind (E) = N_Integer_Literal then
2862 Set_Is_Static_Expression (E, True);
2863 Set_Etype (E, Universal_Integer);
996ae0b0 2864
0f1a6a0b
AC
2865 Set_Etype (Id, Universal_Integer);
2866 Set_Ekind (Id, E_Named_Integer);
2867 Set_Is_Frozen (Id, True);
2868 return;
996ae0b0
RK
2869 end if;
2870
0f1a6a0b 2871 Set_Is_Pure (Id, Is_Pure (Current_Scope));
996ae0b0 2872
0f1a6a0b
AC
2873 -- Process expression, replacing error by integer zero, to avoid
2874 -- cascaded errors or aborts further along in the processing
996ae0b0 2875
8e0aa19b
RD
2876 -- Replace Error by integer zero, which seems least likely to cause
2877 -- cascaded errors.
758c442c 2878
0f1a6a0b
AC
2879 if E = Error then
2880 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2881 Set_Error_Posted (E);
996ae0b0
RK
2882 end if;
2883
0f1a6a0b 2884 Analyze (E);
996ae0b0 2885
0f1a6a0b
AC
2886 -- Verify that the expression is static and numeric. If
2887 -- the expression is overloaded, we apply the preference
2888 -- rule that favors root numeric types.
996ae0b0 2889
0f1a6a0b
AC
2890 if not Is_Overloaded (E) then
2891 T := Etype (E);
ce4a6e84 2892
0f1a6a0b
AC
2893 else
2894 T := Any_Type;
ce4a6e84 2895
0f1a6a0b
AC
2896 Get_First_Interp (E, Index, It);
2897 while Present (It.Typ) loop
5ee96c9d 2898 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
0f1a6a0b
AC
2899 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2900 then
2901 if T = Any_Type then
2902 T := It.Typ;
ce4a6e84 2903
0f1a6a0b
AC
2904 elsif It.Typ = Universal_Real
2905 or else It.Typ = Universal_Integer
2906 then
2907 -- Choose universal interpretation over any other
996ae0b0 2908
0f1a6a0b
AC
2909 T := It.Typ;
2910 exit;
2911 end if;
2912 end if;
9bc856dd 2913
0f1a6a0b
AC
2914 Get_Next_Interp (Index, It);
2915 end loop;
2916 end if;
9bc856dd 2917
0f1a6a0b
AC
2918 if Is_Integer_Type (T) then
2919 Resolve (E, T);
2920 Set_Etype (Id, Universal_Integer);
2921 Set_Ekind (Id, E_Named_Integer);
fbf5a39b 2922
0f1a6a0b 2923 elsif Is_Real_Type (T) then
fbf5a39b 2924
0f1a6a0b
AC
2925 -- Because the real value is converted to universal_real, this is a
2926 -- legal context for a universal fixed expression.
ce4a6e84 2927
0f1a6a0b
AC
2928 if T = Universal_Fixed then
2929 declare
2930 Loc : constant Source_Ptr := Sloc (N);
2931 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2932 Subtype_Mark =>
2933 New_Occurrence_Of (Universal_Real, Loc),
2934 Expression => Relocate_Node (E));
ce4a6e84 2935
0f1a6a0b
AC
2936 begin
2937 Rewrite (E, Conv);
2938 Analyze (E);
2939 end;
ce4a6e84 2940
0f1a6a0b
AC
2941 elsif T = Any_Fixed then
2942 Error_Msg_N ("illegal context for mixed mode operation", E);
ce4a6e84 2943
0f1a6a0b
AC
2944 -- Expression is of the form : universal_fixed * integer. Try to
2945 -- resolve as universal_real.
c775e4a1 2946
0f1a6a0b 2947 T := Universal_Real;
c775e4a1
AC
2948 Set_Etype (E, T);
2949 end if;
2950
0f1a6a0b
AC
2951 Resolve (E, T);
2952 Set_Etype (Id, Universal_Real);
2953 Set_Ekind (Id, E_Named_Real);
17cf3985 2954
0f1a6a0b
AC
2955 else
2956 Wrong_Type (E, Any_Numeric);
2957 Resolve (E, T);
17cf3985 2958
0f1a6a0b
AC
2959 Set_Etype (Id, T);
2960 Set_Ekind (Id, E_Constant);
2961 Set_Never_Set_In_Source (Id, True);
2962 Set_Is_True_Constant (Id, True);
2963 return;
2964 end if;
2b73cf68 2965
0f1a6a0b
AC
2966 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2967 Set_Etype (E, Etype (Id));
2968 end if;
996ae0b0 2969
0f1a6a0b
AC
2970 if not Is_OK_Static_Expression (E) then
2971 Flag_Non_Static_Expr
2972 ("non-static expression used in number declaration!", E);
2973 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2974 Set_Etype (E, Any_Type);
2975 end if;
2976 end Analyze_Number_Declaration;
88b32fc3 2977
f1bd0415
AC
2978 -----------------------------
2979 -- Analyze_Object_Contract --
2980 -----------------------------
2981
2982 procedure Analyze_Object_Contract (Obj_Id : Entity_Id) is
2983 AR_Val : Boolean := False;
2984 AW_Val : Boolean := False;
2985 ER_Val : Boolean := False;
2986 EW_Val : Boolean := False;
f1bd0415
AC
2987 Prag : Node_Id;
2988 Seen : Boolean := False;
2989
2990 begin
2991 if Ekind (Obj_Id) = E_Constant then
2992
2993 -- A constant cannot be volatile. This check is only relevant when
2994 -- SPARK_Mode is on as it is not standard Ada legality rule. Do not
2995 -- flag internally-generated constants that map generic formals to
f9966234 2996 -- actuals in instantiations (SPARK RM 7.1.3(6)).
f1bd0415
AC
2997
2998 if SPARK_Mode = On
2999 and then Is_SPARK_Volatile_Object (Obj_Id)
3000 and then No (Corresponding_Generic_Association (Parent (Obj_Id)))
3001 then
f9966234 3002 Error_Msg_N ("constant cannot be volatile", Obj_Id);
f1bd0415
AC
3003 end if;
3004
3005 else pragma Assert (Ekind (Obj_Id) = E_Variable);
3006
3007 -- The following checks are only relevant when SPARK_Mode is on as
3008 -- they are not standard Ada legality rules.
3009
3010 if SPARK_Mode = On then
3011
3012 -- A non-volatile object cannot have volatile components
f9966234 3013 -- (SPARK RM 7.1.3(7)).
f1bd0415
AC
3014
3015 if not Is_SPARK_Volatile_Object (Obj_Id)
3016 and then Has_Volatile_Component (Etype (Obj_Id))
3017 then
3018 Error_Msg_N
f9966234
AC
3019 ("non-volatile variable & cannot have volatile components",
3020 Obj_Id);
f1bd0415
AC
3021
3022 -- The declaration of a volatile object must appear at the library
3023 -- level.
3024
3025 elsif Is_SPARK_Volatile_Object (Obj_Id)
3026 and then not Is_Library_Level_Entity (Obj_Id)
3027 then
3028 Error_Msg_N
3029 ("volatile variable & must be declared at library level "
3030 & "(SPARK RM 7.1.3(5))", Obj_Id);
3031 end if;
3032 end if;
3033
d7af5ea5 3034 -- Analyze all external properties
f1bd0415 3035
d7af5ea5 3036 Prag := Get_Pragma (Obj_Id, Pragma_Async_Readers);
f1bd0415 3037
d7af5ea5
HK
3038 if Present (Prag) then
3039 Analyze_External_Property_In_Decl_Part (Prag, AR_Val);
3040 Seen := True;
3041 end if;
f1bd0415 3042
d7af5ea5 3043 Prag := Get_Pragma (Obj_Id, Pragma_Async_Writers);
f1bd0415 3044
d7af5ea5
HK
3045 if Present (Prag) then
3046 Analyze_External_Property_In_Decl_Part (Prag, AW_Val);
3047 Seen := True;
3048 end if;
f1bd0415 3049
d7af5ea5 3050 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Reads);
f1bd0415 3051
d7af5ea5
HK
3052 if Present (Prag) then
3053 Analyze_External_Property_In_Decl_Part (Prag, ER_Val);
3054 Seen := True;
3055 end if;
f1bd0415 3056
d7af5ea5 3057 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Writes);
f1bd0415 3058
d7af5ea5
HK
3059 if Present (Prag) then
3060 Analyze_External_Property_In_Decl_Part (Prag, EW_Val);
3061 Seen := True;
f1bd0415
AC
3062 end if;
3063
d7af5ea5 3064 -- Verify the mutual interaction of the various external properties
f1bd0415
AC
3065
3066 if Seen then
3067 Check_External_Properties (Obj_Id, AR_Val, AW_Val, ER_Val, EW_Val);
3068 end if;
d7af5ea5
HK
3069
3070 -- Check whether the lack of indicator Part_Of agrees with the
3071 -- placement of the variable with respect to the state space.
3072
3073 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
3074
3075 if No (Prag) then
3076 Check_Missing_Part_Of (Obj_Id);
3077 end if;
f1bd0415
AC
3078 end if;
3079 end Analyze_Object_Contract;
3080
0f1a6a0b
AC
3081 --------------------------------
3082 -- Analyze_Object_Declaration --
3083 --------------------------------
e27b834b 3084
0f1a6a0b
AC
3085 procedure Analyze_Object_Declaration (N : Node_Id) is
3086 Loc : constant Source_Ptr := Sloc (N);
3087 Id : constant Entity_Id := Defining_Identifier (N);
0f1a6a0b
AC
3088 T : Entity_Id;
3089 Act_T : Entity_Id;
996ae0b0 3090
0f1a6a0b
AC
3091 E : Node_Id := Expression (N);
3092 -- E is set to Expression (N) throughout this routine. When
3093 -- Expression (N) is modified, E is changed accordingly.
dc06abec 3094
0f1a6a0b 3095 Prev_Entity : Entity_Id := Empty;
dc06abec 3096
0f1a6a0b
AC
3097 function Count_Tasks (T : Entity_Id) return Uint;
3098 -- This function is called when a non-generic library level object of a
3099 -- task type is declared. Its function is to count the static number of
3100 -- tasks declared within the type (it is only called if Has_Tasks is set
3101 -- for T). As a side effect, if an array of tasks with non-static bounds
3102 -- or a variant record type is encountered, Check_Restrictions is called
3103 -- indicating the count is unknown.
4755cce9 3104
0f1a6a0b
AC
3105 -----------------
3106 -- Count_Tasks --
3107 -----------------
996ae0b0 3108
0f1a6a0b
AC
3109 function Count_Tasks (T : Entity_Id) return Uint is
3110 C : Entity_Id;
3111 X : Node_Id;
3112 V : Uint;
996ae0b0 3113
0f1a6a0b
AC
3114 begin
3115 if Is_Task_Type (T) then
3116 return Uint_1;
ffe9aba8 3117
0f1a6a0b
AC
3118 elsif Is_Record_Type (T) then
3119 if Has_Discriminants (T) then
3120 Check_Restriction (Max_Tasks, N);
3121 return Uint_0;
ffe9aba8 3122
0f1a6a0b
AC
3123 else
3124 V := Uint_0;
3125 C := First_Component (T);
3126 while Present (C) loop
3127 V := V + Count_Tasks (Etype (C));
3128 Next_Component (C);
3129 end loop;
996ae0b0 3130
0f1a6a0b
AC
3131 return V;
3132 end if;
996ae0b0 3133
0f1a6a0b
AC
3134 elsif Is_Array_Type (T) then
3135 X := First_Index (T);
3136 V := Count_Tasks (Component_Type (T));
3137 while Present (X) loop
3138 C := Etype (X);
996ae0b0 3139
0f1a6a0b
AC
3140 if not Is_Static_Subtype (C) then
3141 Check_Restriction (Max_Tasks, N);
3142 return Uint_0;
3143 else
3144 V := V * (UI_Max (Uint_0,
3145 Expr_Value (Type_High_Bound (C)) -
3146 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3147 end if;
996ae0b0 3148
0f1a6a0b
AC
3149 Next_Index (X);
3150 end loop;
996ae0b0 3151
0f1a6a0b 3152 return V;
996ae0b0 3153
0f1a6a0b
AC
3154 else
3155 return Uint_0;
3156 end if;
3157 end Count_Tasks;
996ae0b0 3158
0f1a6a0b 3159 -- Start of processing for Analyze_Object_Declaration
ce4a6e84 3160
0f1a6a0b
AC
3161 begin
3162 -- There are three kinds of implicit types generated by an
3163 -- object declaration:
ce4a6e84 3164
7ff2d234 3165 -- 1. Those generated by the original Object Definition
ce4a6e84 3166
0f1a6a0b 3167 -- 2. Those generated by the Expression
996ae0b0 3168
702d2020 3169 -- 3. Those used to constrain the Object Definition with the
579fda56 3170 -- expression constraints when the definition is unconstrained.
996ae0b0 3171
0f1a6a0b
AC
3172 -- They must be generated in this order to avoid order of elaboration
3173 -- issues. Thus the first step (after entering the name) is to analyze
3174 -- the object definition.
996ae0b0 3175
0f1a6a0b
AC
3176 if Constant_Present (N) then
3177 Prev_Entity := Current_Entity_In_Scope (Id);
996ae0b0 3178
0f1a6a0b
AC
3179 if Present (Prev_Entity)
3180 and then
579fda56 3181
0f1a6a0b
AC
3182 -- If the homograph is an implicit subprogram, it is overridden
3183 -- by the current declaration.
996ae0b0 3184
0f1a6a0b
AC
3185 ((Is_Overloadable (Prev_Entity)
3186 and then Is_Inherited_Operation (Prev_Entity))
996ae0b0 3187
0f1a6a0b
AC
3188 -- The current object is a discriminal generated for an entry
3189 -- family index. Even though the index is a constant, in this
3190 -- particular context there is no true constant redeclaration.
3191 -- Enter_Name will handle the visibility.
996ae0b0 3192
0f1a6a0b
AC
3193 or else
3194 (Is_Discriminal (Id)
3195 and then Ekind (Discriminal_Link (Id)) =
3196 E_Entry_Index_Parameter)
996ae0b0 3197
0f1a6a0b
AC
3198 -- The current object is the renaming for a generic declared
3199 -- within the instance.
996ae0b0 3200
0f1a6a0b
AC
3201 or else
3202 (Ekind (Prev_Entity) = E_Package
3203 and then Nkind (Parent (Prev_Entity)) =
3204 N_Package_Renaming_Declaration
3205 and then not Comes_From_Source (Prev_Entity)
3206 and then Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
3207 then
3208 Prev_Entity := Empty;
3209 end if;
3210 end if;
0e41a941 3211
0f1a6a0b
AC
3212 if Present (Prev_Entity) then
3213 Constant_Redeclaration (Id, N, T);
0e41a941 3214
0f1a6a0b
AC
3215 Generate_Reference (Prev_Entity, Id, 'c');
3216 Set_Completion_Referenced (Id);
996ae0b0 3217
0f1a6a0b 3218 if Error_Posted (N) then
996ae0b0 3219
0f1a6a0b
AC
3220 -- Type mismatch or illegal redeclaration, Do not analyze
3221 -- expression to avoid cascaded errors.
996ae0b0 3222
0f1a6a0b
AC
3223 T := Find_Type_Of_Object (Object_Definition (N), N);
3224 Set_Etype (Id, T);
3225 Set_Ekind (Id, E_Variable);
3226 goto Leave;
996ae0b0
RK
3227 end if;
3228
0f1a6a0b
AC
3229 -- In the normal case, enter identifier at the start to catch premature
3230 -- usage in the initialization expression.
996ae0b0 3231
0f1a6a0b
AC
3232 else
3233 Generate_Definition (Id);
3234 Enter_Name (Id);
996ae0b0 3235
0f1a6a0b 3236 Mark_Coextensions (N, Object_Definition (N));
996ae0b0 3237
0f1a6a0b 3238 T := Find_Type_Of_Object (Object_Definition (N), N);
996ae0b0 3239
0f1a6a0b
AC
3240 if Nkind (Object_Definition (N)) = N_Access_Definition
3241 and then Present
5ee96c9d 3242 (Access_To_Subprogram_Definition (Object_Definition (N)))
0f1a6a0b 3243 and then Protected_Present
5ee96c9d 3244 (Access_To_Subprogram_Definition (Object_Definition (N)))
0f1a6a0b
AC
3245 then
3246 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3247 end if;
449d2be3 3248
0f1a6a0b
AC
3249 if Error_Posted (Id) then
3250 Set_Etype (Id, T);
3251 Set_Ekind (Id, E_Variable);
3252 goto Leave;
3253 end if;
3254 end if;
449d2be3 3255
0f1a6a0b
AC
3256 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3257 -- out some static checks
2514b839 3258
0f1a6a0b
AC
3259 if Ada_Version >= Ada_2005
3260 and then Can_Never_Be_Null (T)
3261 then
3262 -- In case of aggregates we must also take care of the correct
3263 -- initialization of nested aggregates bug this is done at the
3264 -- point of the analysis of the aggregate (see sem_aggr.adb)
996ae0b0 3265
0f1a6a0b
AC
3266 if Present (Expression (N))
3267 and then Nkind (Expression (N)) = N_Aggregate
996ae0b0
RK
3268 then
3269 null;
3270
3271 else
0f1a6a0b
AC
3272 declare
3273 Save_Typ : constant Entity_Id := Etype (Id);
3274 begin
3275 Set_Etype (Id, T); -- Temp. decoration for static checks
3276 Null_Exclusion_Static_Checks (N);
3277 Set_Etype (Id, Save_Typ);
3278 end;
996ae0b0 3279 end if;
0f1a6a0b 3280 end if;
996ae0b0 3281
926a0900
AC
3282 -- Object is marked pure if it is in a pure scope
3283
0f1a6a0b 3284 Set_Is_Pure (Id, Is_Pure (Current_Scope));
88b32fc3 3285
0f1a6a0b
AC
3286 -- If deferred constant, make sure context is appropriate. We detect
3287 -- a deferred constant as a constant declaration with no expression.
3288 -- A deferred constant can appear in a package body if its completion
3289 -- is by means of an interface pragma.
3290
5ee96c9d
TQ
3291 if Constant_Present (N) and then No (E) then
3292
0f1a6a0b
AC
3293 -- A deferred constant may appear in the declarative part of the
3294 -- following constructs:
030d25f4 3295
0f1a6a0b
AC
3296 -- blocks
3297 -- entry bodies
3298 -- extended return statements
3299 -- package specs
3300 -- package bodies
3301 -- subprogram bodies
3302 -- task bodies
030d25f4 3303
0f1a6a0b
AC
3304 -- When declared inside a package spec, a deferred constant must be
3305 -- completed by a full constant declaration or pragma Import. In all
3306 -- other cases, the only proper completion is pragma Import. Extended
3307 -- return statements are flagged as invalid contexts because they do
3308 -- not have a declarative part and so cannot accommodate the pragma.
996ae0b0 3309
0f1a6a0b
AC
3310 if Ekind (Current_Scope) = E_Return_Statement then
3311 Error_Msg_N
3312 ("invalid context for deferred constant declaration (RM 7.4)",
3313 N);
3314 Error_Msg_N
3315 ("\declaration requires an initialization expression",
3316 N);
3317 Set_Constant_Present (N, False);
996ae0b0 3318
0f1a6a0b 3319 -- In Ada 83, deferred constant must be of private type
996ae0b0 3320
0f1a6a0b
AC
3321 elsif not Is_Private_Type (T) then
3322 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3323 Error_Msg_N
3324 ("(Ada 83) deferred constant must be private type", N);
3325 end if;
3326 end if;
ce4a6e84 3327
0f1a6a0b 3328 -- If not a deferred constant, then object declaration freezes its type
996ae0b0 3329
0f1a6a0b
AC
3330 else
3331 Check_Fully_Declared (T, N);
3332 Freeze_Before (N, T);
3333 end if;
dc06abec 3334
0f1a6a0b
AC
3335 -- If the object was created by a constrained array definition, then
3336 -- set the link in both the anonymous base type and anonymous subtype
3337 -- that are built to represent the array type to point to the object.
dc06abec 3338
0f1a6a0b
AC
3339 if Nkind (Object_Definition (Declaration_Node (Id))) =
3340 N_Constrained_Array_Definition
3341 then
3342 Set_Related_Array_Object (T, Id);
3343 Set_Related_Array_Object (Base_Type (T), Id);
3344 end if;
996ae0b0 3345
0f1a6a0b 3346 -- Special checks for protected objects not at library level
996ae0b0 3347
0f1a6a0b
AC
3348 if Is_Protected_Type (T)
3349 and then not Is_Library_Level_Entity (Id)
3350 then
3351 Check_Restriction (No_Local_Protected_Objects, Id);
996ae0b0 3352
0f1a6a0b 3353 -- Protected objects with interrupt handlers must be at library level
996ae0b0 3354
113a62d9 3355 -- Ada 2005: This test is not needed (and the corresponding clause
0f1a6a0b
AC
3356 -- in the RM is removed) because accessibility checks are sufficient
3357 -- to make handlers not at the library level illegal.
996ae0b0 3358
113a62d9 3359 -- AI05-0303: The AI is in fact a binding interpretation, and thus
a39a553e
AC
3360 -- applies to the '95 version of the language as well.
3361
5ee96c9d 3362 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
0f1a6a0b
AC
3363 Error_Msg_N
3364 ("interrupt object can only be declared at library level", Id);
996ae0b0
RK
3365 end if;
3366 end if;
3367
0f1a6a0b
AC
3368 -- The actual subtype of the object is the nominal subtype, unless
3369 -- the nominal one is unconstrained and obtained from the expression.
996ae0b0 3370
0f1a6a0b 3371 Act_T := T;
dc06abec 3372
7ff2d234
AC
3373 -- These checks should be performed before the initialization expression
3374 -- is considered, so that the Object_Definition node is still the same
3375 -- as in source code.
3376
2ba431e5
YM
3377 -- In SPARK, the nominal subtype shall be given by a subtype mark and
3378 -- shall not be unconstrained. (The only exception to this is the
3379 -- admission of declarations of constants of type String.)
7ff2d234 3380
23685ae6
AC
3381 if not
3382 Nkind_In (Object_Definition (N), N_Identifier, N_Expanded_Name)
fe5d3068 3383 then
2ba431e5 3384 Check_SPARK_Restriction
23685ae6 3385 ("subtype mark required", Object_Definition (N));
277c9abe 3386
fe5d3068
YM
3387 elsif Is_Array_Type (T)
3388 and then not Is_Constrained (T)
3389 and then T /= Standard_String
3390 then
2ba431e5 3391 Check_SPARK_Restriction
277c9abe
AC
3392 ("subtype mark of constrained type expected",
3393 Object_Definition (N));
fe5d3068 3394 end if;
7ff2d234 3395
2ba431e5 3396 -- There are no aliased objects in SPARK
7ff2d234 3397
fe5d3068 3398 if Aliased_Present (N) then
2ba431e5 3399 Check_SPARK_Restriction ("aliased object is not allowed", N);
7ff2d234
AC
3400 end if;
3401
0f1a6a0b 3402 -- Process initialization expression if present and not in error
996ae0b0 3403
0f1a6a0b 3404 if Present (E) and then E /= Error then
88b32fc3 3405
0f1a6a0b
AC
3406 -- Generate an error in case of CPP class-wide object initialization.
3407 -- Required because otherwise the expansion of the class-wide
3408 -- assignment would try to use 'size to initialize the object
3409 -- (primitive that is not available in CPP tagged types).
88b32fc3 3410
0f1a6a0b
AC
3411 if Is_Class_Wide_Type (Act_T)
3412 and then
3413 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3414 or else
3415 (Present (Full_View (Root_Type (Etype (Act_T))))
277c9abe
AC
3416 and then
3417 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
996ae0b0 3418 then
0f1a6a0b
AC
3419 Error_Msg_N
3420 ("predefined assignment not available for 'C'P'P tagged types",
3421 E);
996ae0b0 3422 end if;
996ae0b0 3423
0f1a6a0b
AC
3424 Mark_Coextensions (N, E);
3425 Analyze (E);
dc06abec 3426
0f1a6a0b
AC
3427 -- In case of errors detected in the analysis of the expression,
3428 -- decorate it with the expected type to avoid cascaded errors
996ae0b0 3429
0f1a6a0b
AC
3430 if No (Etype (E)) then
3431 Set_Etype (E, T);
3432 end if;
dc06abec 3433
0f1a6a0b
AC
3434 -- If an initialization expression is present, then we set the
3435 -- Is_True_Constant flag. It will be reset if this is a variable
3436 -- and it is indeed modified.
3437
3438 Set_Is_True_Constant (Id, True);
3439
3440 -- If we are analyzing a constant declaration, set its completion
3441 -- flag after analyzing and resolving the expression.
3442
3443 if Constant_Present (N) then
3444 Set_Has_Completion (Id);
996ae0b0
RK
3445 end if;
3446
3a3af4c3
AC
3447 -- Set type and resolve (type may be overridden later on). Note:
3448 -- Ekind (Id) must still be E_Void at this point so that incorrect
3449 -- early usage within E is properly diagnosed.
9dfd2ff8 3450
0f1a6a0b
AC
3451 Set_Etype (Id, T);
3452 Resolve (E, T);
996ae0b0 3453
84f4072a
JM
3454 -- No further action needed if E is a call to an inlined function
3455 -- which returns an unconstrained type and it has been expanded into
3456 -- a procedure call. In that case N has been replaced by an object
3457 -- declaration without initializing expression and it has been
3458 -- analyzed (see Expand_Inlined_Call).
3459
3460 if Debug_Flag_Dot_K
3461 and then Expander_Active
3462 and then Nkind (E) = N_Function_Call
3463 and then Nkind (Name (E)) in N_Has_Entity
3464 and then Is_Inlined (Entity (Name (E)))
3465 and then not Is_Constrained (Etype (E))
3466 and then Analyzed (N)
3467 and then No (Expression (N))
3468 then
3469 return;
3470 end if;
3471
0f1a6a0b
AC
3472 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3473 -- node (which was marked already-analyzed), we need to set the type
3474 -- to something other than Any_Access in order to keep gigi happy.
fbf5a39b 3475
0f1a6a0b
AC
3476 if Etype (E) = Any_Access then
3477 Set_Etype (E, T);
3478 end if;
996ae0b0 3479
0f1a6a0b
AC
3480 -- If the object is an access to variable, the initialization
3481 -- expression cannot be an access to constant.
996ae0b0 3482
0f1a6a0b
AC
3483 if Is_Access_Type (T)
3484 and then not Is_Access_Constant (T)
3485 and then Is_Access_Type (Etype (E))
3486 and then Is_Access_Constant (Etype (E))
3487 then
3488 Error_Msg_N
3489 ("access to variable cannot be initialized "
3490 & "with an access-to-constant expression", E);
3491 end if;
fbf5a39b 3492
0f1a6a0b
AC
3493 if not Assignment_OK (N) then
3494 Check_Initialization (T, E);
3495 end if;
996ae0b0 3496
0f1a6a0b 3497 Check_Unset_Reference (E);
996ae0b0 3498
0f1a6a0b
AC
3499 -- If this is a variable, then set current value. If this is a
3500 -- declared constant of a scalar type with a static expression,
3501 -- indicate that it is always valid.
996ae0b0 3502
0f1a6a0b
AC
3503 if not Constant_Present (N) then
3504 if Compile_Time_Known_Value (E) then
3505 Set_Current_Value (Id, E);
3506 end if;
996ae0b0 3507
0f1a6a0b
AC
3508 elsif Is_Scalar_Type (T)
3509 and then Is_OK_Static_Expression (E)
3510 then
3511 Set_Is_Known_Valid (Id);
3512 end if;
996ae0b0 3513
0f1a6a0b 3514 -- Deal with setting of null flags
996ae0b0 3515
0f1a6a0b
AC
3516 if Is_Access_Type (T) then
3517 if Known_Non_Null (E) then
3518 Set_Is_Known_Non_Null (Id, True);
3519 elsif Known_Null (E)
3520 and then not Can_Never_Be_Null (Id)
3521 then
3522 Set_Is_Known_Null (Id, True);
3523 end if;
3524 end if;
996ae0b0 3525
08988ed9 3526 -- Check incorrect use of dynamically tagged expressions
996ae0b0 3527
0f1a6a0b
AC
3528 if Is_Tagged_Type (T) then
3529 Check_Dynamically_Tagged_Expression
3530 (Expr => E,
3531 Typ => T,
3532 Related_Nod => N);
3533 end if;
996ae0b0 3534
0f1a6a0b
AC
3535 Apply_Scalar_Range_Check (E, T);
3536 Apply_Static_Length_Check (E, T);
aa1e353a
AC
3537
3538 if Nkind (Original_Node (N)) = N_Object_Declaration
3539 and then Comes_From_Source (Original_Node (N))
f5afb270
AC
3540
3541 -- Only call test if needed
3542
6480338a 3543 and then Restriction_Check_Required (SPARK_05)
08988ed9 3544 and then not Is_SPARK_Initialization_Expr (Original_Node (E))
aa1e353a 3545 then
2ba431e5 3546 Check_SPARK_Restriction
aa1e353a
AC
3547 ("initialization expression is not appropriate", E);
3548 end if;
0f1a6a0b 3549 end if;
996ae0b0 3550
0f1a6a0b
AC
3551 -- If the No_Streams restriction is set, check that the type of the
3552 -- object is not, and does not contain, any subtype derived from
3553 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3554 -- Has_Stream just for efficiency reasons. There is no point in
3555 -- spending time on a Has_Stream check if the restriction is not set.
996ae0b0 3556
0f1a6a0b
AC
3557 if Restriction_Check_Required (No_Streams) then
3558 if Has_Stream (T) then
3559 Check_Restriction (No_Streams, N);
996ae0b0
RK
3560 end if;
3561 end if;
3562
f197d2f2
AC
3563 -- Deal with predicate check before we start to do major rewriting. It
3564 -- is OK to initialize and then check the initialized value, since the
3565 -- object goes out of scope if we get a predicate failure. Note that we
3566 -- do this in the analyzer and not the expander because the analyzer
3567 -- does some substantial rewriting in some cases.
f2acf80c
AC
3568
3569 -- We need a predicate check if the type has predicates, and if either
3570 -- there is an initializing expression, or for default initialization
aa9b151a
AC
3571 -- when we have at least one case of an explicit default initial value
3572 -- and then this is not an internal declaration whose initialization
3573 -- comes later (as for an aggregate expansion).
f2acf80c
AC
3574
3575 if not Suppress_Assignment_Checks (N)
3576 and then Present (Predicate_Function (T))
aa9b151a 3577 and then not No_Initialization (N)
f2acf80c
AC
3578 and then
3579 (Present (E)
3580 or else
3581 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3582 then
fd8b8c01
AC
3583 -- If the type has a static predicate and the expression is known at
3584 -- compile time, see if the expression satisfies the predicate.
f197d2f2
AC
3585
3586 if Present (E) then
3587 Check_Expression_Against_Static_Predicate (E, T);
3588 end if;
3589
f2acf80c
AC
3590 Insert_After (N,
3591 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3592 end if;
3593
0f1a6a0b 3594 -- Case of unconstrained type
fbf5a39b 3595
0f1a6a0b 3596 if Is_Indefinite_Subtype (T) then
ce4a6e84 3597
9ec080cb
AC
3598 -- In SPARK, a declaration of unconstrained type is allowed
3599 -- only for constants of type string.
3600
20428725 3601 if Is_String_Type (T) and then not Constant_Present (N) then
9ec080cb 3602 Check_SPARK_Restriction
fd8b8c01 3603 ("declaration of object of unconstrained type not allowed", N);
9ec080cb
AC
3604 end if;
3605
0f1a6a0b 3606 -- Nothing to do in deferred constant case
ce4a6e84 3607
0f1a6a0b 3608 if Constant_Present (N) and then No (E) then
ce4a6e84
RD
3609 null;
3610
0f1a6a0b 3611 -- Case of no initialization present
ce4a6e84 3612
0f1a6a0b
AC
3613 elsif No (E) then
3614 if No_Initialization (N) then
3615 null;
ce4a6e84 3616
0f1a6a0b
AC
3617 elsif Is_Class_Wide_Type (T) then
3618 Error_Msg_N
3619 ("initialization required in class-wide declaration ", N);
ce4a6e84 3620
0f1a6a0b
AC
3621 else
3622 Error_Msg_N
3623 ("unconstrained subtype not allowed (need initialization)",
3624 Object_Definition (N));
07fc65c4 3625
0f1a6a0b
AC
3626 if Is_Record_Type (T) and then Has_Discriminants (T) then
3627 Error_Msg_N
3628 ("\provide initial value or explicit discriminant values",
3629 Object_Definition (N));
07fc65c4 3630
0f1a6a0b
AC
3631 Error_Msg_NE
3632 ("\or give default discriminant values for type&",
3633 Object_Definition (N), T);
07fc65c4 3634
0f1a6a0b
AC
3635 elsif Is_Array_Type (T) then
3636 Error_Msg_N
3637 ("\provide initial value or explicit array bounds",
3638 Object_Definition (N));
3639 end if;
3640 end if;
07fc65c4 3641
0f1a6a0b
AC
3642 -- Case of initialization present but in error. Set initial
3643 -- expression as absent (but do not make above complaints)
996ae0b0 3644
0f1a6a0b
AC
3645 elsif E = Error then
3646 Set_Expression (N, Empty);
3647 E := Empty;
996ae0b0 3648
0f1a6a0b 3649 -- Case of initialization present
996ae0b0 3650
0f1a6a0b 3651 else
9ec080cb 3652 -- Check restrictions in Ada 83
996ae0b0 3653
0f1a6a0b 3654 if not Constant_Present (N) then
5b5588dd 3655
99d520ad 3656 -- Unconstrained variables not allowed in Ada 83 mode
5b5588dd 3657
0f1a6a0b
AC
3658 if Ada_Version = Ada_83
3659 and then Comes_From_Source (Object_Definition (N))
3660 then
3661 Error_Msg_N
3662 ("(Ada 83) unconstrained variable not allowed",
3663 Object_Definition (N));
3664 end if;
3665 end if;
996ae0b0 3666
0f1a6a0b 3667 -- Now we constrain the variable from the initializing expression
fbf5a39b 3668
0f1a6a0b
AC
3669 -- If the expression is an aggregate, it has been expanded into
3670 -- individual assignments. Retrieve the actual type from the
3671 -- expanded construct.
fbf5a39b 3672
0f1a6a0b
AC
3673 if Is_Array_Type (T)
3674 and then No_Initialization (N)
3675 and then Nkind (Original_Node (E)) = N_Aggregate
3676 then
3677 Act_T := Etype (E);
996ae0b0 3678
0f1a6a0b
AC
3679 -- In case of class-wide interface object declarations we delay
3680 -- the generation of the equivalent record type declarations until
3681 -- its expansion because there are cases in they are not required.
996ae0b0 3682
0f1a6a0b
AC
3683 elsif Is_Interface (T) then
3684 null;
88b32fc3 3685
0f1a6a0b
AC
3686 else
3687 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3688 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3689 end if;
88b32fc3 3690
0f1a6a0b 3691 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
5b2217f8 3692
9a7049fd 3693 if Aliased_Present (N) then
0f1a6a0b
AC
3694 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3695 end if;
5b2217f8 3696
0f1a6a0b
AC
3697 Freeze_Before (N, Act_T);
3698 Freeze_Before (N, T);
3699 end if;
996ae0b0 3700
0f1a6a0b
AC
3701 elsif Is_Array_Type (T)
3702 and then No_Initialization (N)
3703 and then Nkind (Original_Node (E)) = N_Aggregate
3704 then
3705 if not Is_Entity_Name (Object_Definition (N)) then
3706 Act_T := Etype (E);
3707 Check_Compile_Time_Size (Act_T);
996ae0b0 3708
0f1a6a0b
AC
3709 if Aliased_Present (N) then
3710 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3711 end if;
3712 end if;
996ae0b0 3713
0f1a6a0b
AC
3714 -- When the given object definition and the aggregate are specified
3715 -- independently, and their lengths might differ do a length check.
3716 -- This cannot happen if the aggregate is of the form (others =>...)
996ae0b0 3717
0f1a6a0b
AC
3718 if not Is_Constrained (T) then
3719 null;
996ae0b0 3720
0f1a6a0b 3721 elsif Nkind (E) = N_Raise_Constraint_Error then
996ae0b0 3722
0f1a6a0b 3723 -- Aggregate is statically illegal. Place back in declaration
758c442c 3724
0f1a6a0b
AC
3725 Set_Expression (N, E);
3726 Set_No_Initialization (N, False);
9dfd2ff8 3727
0f1a6a0b
AC
3728 elsif T = Etype (E) then
3729 null;
758c442c 3730
0f1a6a0b
AC
3731 elsif Nkind (E) = N_Aggregate
3732 and then Present (Component_Associations (E))
3733 and then Present (Choices (First (Component_Associations (E))))
3734 and then Nkind (First
3735 (Choices (First (Component_Associations (E))))) = N_Others_Choice
3736 then
3737 null;
758c442c 3738
0f1a6a0b
AC
3739 else
3740 Apply_Length_Check (E, T);
3741 end if;
996ae0b0 3742
0f1a6a0b
AC
3743 -- If the type is limited unconstrained with defaulted discriminants and
3744 -- there is no expression, then the object is constrained by the
3745 -- defaults, so it is worthwhile building the corresponding subtype.
996ae0b0 3746
0f1a6a0b
AC
3747 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
3748 and then not Is_Constrained (T)
3749 and then Has_Discriminants (T)
996ae0b0 3750 then
0f1a6a0b
AC
3751 if No (E) then
3752 Act_T := Build_Default_Subtype (T, N);
3753 else
113a62d9 3754 -- Ada 2005: A limited object may be initialized by means of an
0f1a6a0b
AC
3755 -- aggregate. If the type has default discriminants it has an
3756 -- unconstrained nominal type, Its actual subtype will be obtained
3757 -- from the aggregate, and not from the default discriminants.
996ae0b0 3758
0f1a6a0b
AC
3759 Act_T := Etype (E);
3760 end if;
996ae0b0 3761
0f1a6a0b 3762 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
dc06abec 3763
d4129bfa 3764 elsif Nkind (E) = N_Function_Call
0f1a6a0b 3765 and then Constant_Present (N)
d4129bfa 3766 and then Has_Unconstrained_Elements (Etype (E))
0f1a6a0b
AC
3767 then
3768 -- The back-end has problems with constants of a discriminated type
3769 -- with defaults, if the initial value is a function call. We
d4129bfa
AC
3770 -- generate an intermediate temporary that will receive a reference
3771 -- to the result of the call. The initialization expression then
3772 -- becomes a dereference of that temporary.
dc06abec 3773
0f1a6a0b 3774 Remove_Side_Effects (E);
5c34e9cd 3775
53f29d4f
AC
3776 -- If this is a constant declaration of an unconstrained type and
3777 -- the initialization is an aggregate, we can use the subtype of the
3778 -- aggregate for the declared entity because it is immutable.
3779
5c34e9cd
AC
3780 elsif not Is_Constrained (T)
3781 and then Has_Discriminants (T)
3782 and then Constant_Present (N)
3783 and then not Has_Unchecked_Union (T)
3784 and then Nkind (E) = N_Aggregate
3785 then
5c34e9cd 3786 Act_T := Etype (E);
996ae0b0
RK
3787 end if;
3788
0f1a6a0b 3789 -- Check No_Wide_Characters restriction
996ae0b0 3790
0f1a6a0b 3791 Check_Wide_Character_Restriction (T, Object_Definition (N));
996ae0b0 3792
53f29d4f
AC
3793 -- Indicate this is not set in source. Certainly true for constants, and
3794 -- true for variables so far (will be reset for a variable if and when
3795 -- we encounter a modification in the source).
996ae0b0 3796
0f1a6a0b 3797 Set_Never_Set_In_Source (Id, True);
996ae0b0 3798
3a3af4c3 3799 -- Now establish the proper kind and type of the object
996ae0b0 3800
0f1a6a0b 3801 if Constant_Present (N) then
3a3af4c3 3802 Set_Ekind (Id, E_Constant);
ab8843fa 3803 Set_Is_True_Constant (Id);
996ae0b0 3804
0f1a6a0b
AC
3805 else
3806 Set_Ekind (Id, E_Variable);
996ae0b0 3807
0f1a6a0b 3808 -- A variable is set as shared passive if it appears in a shared
53f29d4f
AC
3809 -- passive package, and is at the outer level. This is not done for
3810 -- entities generated during expansion, because those are always
3811 -- manipulated locally.
e6f69614 3812
0f1a6a0b
AC
3813 if Is_Shared_Passive (Current_Scope)
3814 and then Is_Library_Level_Entity (Id)
3815 and then Comes_From_Source (Id)
3816 then
3817 Set_Is_Shared_Passive (Id);
3818 Check_Shared_Var (Id, T, N);
3819 end if;
653da906 3820
0f1a6a0b
AC
3821 -- Set Has_Initial_Value if initializing expression present. Note
3822 -- that if there is no initializing expression, we leave the state
3823 -- of this flag unchanged (usually it will be False, but notably in
3824 -- the case of exception choice variables, it will already be true).
88b32fc3 3825
0f1a6a0b
AC
3826 if Present (E) then
3827 Set_Has_Initial_Value (Id, True);
3828 end if;
6c3c671e
AC
3829
3830 Set_Contract (Id, Make_Contract (Sloc (Id)));
0f1a6a0b 3831 end if;
88b32fc3 3832
0f1a6a0b 3833 -- Initialize alignment and size and capture alignment setting
88b32fc3 3834
0f1a6a0b
AC
3835 Init_Alignment (Id);
3836 Init_Esize (Id);
3837 Set_Optimize_Alignment_Flags (Id);
88b32fc3 3838
0f1a6a0b 3839 -- Deal with aliased case
88b32fc3 3840
0f1a6a0b
AC
3841 if Aliased_Present (N) then
3842 Set_Is_Aliased (Id);
88b32fc3 3843
0f1a6a0b
AC
3844 -- If the object is aliased and the type is unconstrained with
3845 -- defaulted discriminants and there is no expression, then the
3846 -- object is constrained by the defaults, so it is worthwhile
3847 -- building the corresponding subtype.
88b32fc3 3848
0f1a6a0b
AC
3849 -- Ada 2005 (AI-363): If the aliased object is discriminated and
3850 -- unconstrained, then only establish an actual subtype if the
3851 -- nominal subtype is indefinite. In definite cases the object is
3852 -- unconstrained in Ada 2005.
88b32fc3 3853
0f1a6a0b
AC
3854 if No (E)
3855 and then Is_Record_Type (T)
3856 and then not Is_Constrained (T)
3857 and then Has_Discriminants (T)
3858 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
3859 then
3860 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
3861 end if;
3862 end if;
88b32fc3 3863
0f1a6a0b 3864 -- Now we can set the type of the object
88b32fc3 3865
0f1a6a0b 3866 Set_Etype (Id, Act_T);
88b32fc3 3867
926a0900
AC
3868 -- Object is marked to be treated as volatile if type is volatile and
3869 -- we clear the Current_Value setting that may have been set above.
3870
3871 if Treat_As_Volatile (Etype (Id)) then
3872 Set_Treat_As_Volatile (Id);
3873 Set_Current_Value (Id, Empty);
3874 end if;
3875
0f1a6a0b 3876 -- Deal with controlled types
88b32fc3 3877
0f1a6a0b
AC
3878 if Has_Controlled_Component (Etype (Id))
3879 or else Is_Controlled (Etype (Id))
3880 then
3881 if not Is_Library_Level_Entity (Id) then
3882 Check_Restriction (No_Nested_Finalization, N);
88b32fc3 3883 else
0f1a6a0b 3884 Validate_Controlled_Object (Id);
88b32fc3 3885 end if;
0f1a6a0b 3886 end if;
996ae0b0 3887
0f1a6a0b
AC
3888 if Has_Task (Etype (Id)) then
3889 Check_Restriction (No_Tasking, N);
996ae0b0 3890
0f1a6a0b 3891 -- Deal with counting max tasks
996ae0b0 3892
0f1a6a0b 3893 -- Nothing to do if inside a generic
996ae0b0 3894
0f1a6a0b
AC
3895 if Inside_A_Generic then
3896 null;
996ae0b0 3897
0f1a6a0b 3898 -- If library level entity, then count tasks
996ae0b0 3899
0f1a6a0b
AC
3900 elsif Is_Library_Level_Entity (Id) then
3901 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
996ae0b0 3902
0f1a6a0b
AC
3903 -- If not library level entity, then indicate we don't know max
3904 -- tasks and also check task hierarchy restriction and blocking
a90bd866 3905 -- operation (since starting a task is definitely blocking).
996ae0b0 3906
0f1a6a0b
AC
3907 else
3908 Check_Restriction (Max_Tasks, N);
3909 Check_Restriction (No_Task_Hierarchy, N);
3910 Check_Potentially_Blocking_Operation (N);
3911 end if;
996ae0b0 3912
0f1a6a0b
AC
3913 -- A rather specialized test. If we see two tasks being declared
3914 -- of the same type in the same object declaration, and the task
3915 -- has an entry with an address clause, we know that program error
3916 -- will be raised at run time since we can't have two tasks with
3917 -- entries at the same address.
996ae0b0 3918
0f1a6a0b
AC
3919 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3920 declare
3921 E : Entity_Id;
996ae0b0 3922
0f1a6a0b
AC
3923 begin
3924 E := First_Entity (Etype (Id));
3925 while Present (E) loop
3926 if Ekind (E) = E_Entry
3927 and then Present (Get_Attribute_Definition_Clause
3928 (E, Attribute_Address))
3929 then
43417b90 3930 Error_Msg_Warn := SPARK_Mode /= On;
0f1a6a0b 3931 Error_Msg_N
4a28b181
AC
3932 ("more than one task with same entry address<<", N);
3933 Error_Msg_N ("\Program_Error [<<", N);
0f1a6a0b
AC
3934 Insert_Action (N,
3935 Make_Raise_Program_Error (Loc,
3936 Reason => PE_Duplicated_Entry_Address));
3937 exit;
3938 end if;
996ae0b0 3939
0f1a6a0b
AC
3940 Next_Entity (E);
3941 end loop;
3942 end;
3943 end if;
3944 end if;
996ae0b0 3945
0f1a6a0b
AC
3946 -- Some simple constant-propagation: if the expression is a constant
3947 -- string initialized with a literal, share the literal. This avoids
3948 -- a run-time copy.
2b73cf68 3949
0f1a6a0b
AC
3950 if Present (E)
3951 and then Is_Entity_Name (E)
3952 and then Ekind (Entity (E)) = E_Constant
3953 and then Base_Type (Etype (E)) = Standard_String
3954 then
3955 declare
3956 Val : constant Node_Id := Constant_Value (Entity (E));
3957 begin
3958 if Present (Val)
3959 and then Nkind (Val) = N_String_Literal
3960 then
3961 Rewrite (E, New_Copy (Val));
3962 end if;
3963 end;
3964 end if;
996ae0b0 3965
0f1a6a0b
AC
3966 -- Another optimization: if the nominal subtype is unconstrained and
3967 -- the expression is a function call that returns an unconstrained
3968 -- type, rewrite the declaration as a renaming of the result of the
3969 -- call. The exceptions below are cases where the copy is expected,
3970 -- either by the back end (Aliased case) or by the semantics, as for
3971 -- initializing controlled types or copying tags for classwide types.
996ae0b0 3972
0f1a6a0b
AC
3973 if Present (E)
3974 and then Nkind (E) = N_Explicit_Dereference
3975 and then Nkind (Original_Node (E)) = N_Function_Call
3976 and then not Is_Library_Level_Entity (Id)
3977 and then not Is_Constrained (Underlying_Type (T))
3978 and then not Is_Aliased (Id)
3979 and then not Is_Class_Wide_Type (T)
3980 and then not Is_Controlled (T)
3981 and then not Has_Controlled_Component (Base_Type (T))
3982 and then Expander_Active
3983 then
3984 Rewrite (N,
3985 Make_Object_Renaming_Declaration (Loc,
3986 Defining_Identifier => Id,
3987 Access_Definition => Empty,
3988 Subtype_Mark => New_Occurrence_Of
3989 (Base_Type (Etype (Id)), Loc),
3990 Name => E));
996ae0b0 3991
0f1a6a0b 3992 Set_Renamed_Object (Id, E);
996ae0b0 3993
0f1a6a0b
AC
3994 -- Force generation of debugging information for the constant and for
3995 -- the renamed function call.
996ae0b0 3996
0f1a6a0b
AC
3997 Set_Debug_Info_Needed (Id);
3998 Set_Debug_Info_Needed (Entity (Prefix (E)));
3999 end if;
996ae0b0 4000
0f1a6a0b
AC
4001 if Present (Prev_Entity)
4002 and then Is_Frozen (Prev_Entity)
4003 and then not Error_Posted (Id)
4004 then
4005 Error_Msg_N ("full constant declaration appears too late", N);
4006 end if;
996ae0b0 4007
0f1a6a0b 4008 Check_Eliminated (Id);
996ae0b0 4009
0f1a6a0b 4010 -- Deal with setting In_Private_Part flag if in private part
996ae0b0 4011
0f1a6a0b
AC
4012 if Ekind (Scope (Id)) = E_Package
4013 and then In_Private_Part (Scope (Id))
4014 then
4015 Set_In_Private_Part (Id);
4016 end if;
2820d220 4017
0f1a6a0b 4018 -- Check for violation of No_Local_Timing_Events
996ae0b0 4019
273adcdf 4020 if Restriction_Check_Required (No_Local_Timing_Events)
0f1a6a0b 4021 and then not Is_Library_Level_Entity (Id)
273adcdf 4022 and then Is_RTE (Etype (Id), RE_Timing_Event)
0f1a6a0b
AC
4023 then
4024 Check_Restriction (No_Local_Timing_Events, N);
4025 end if;
996ae0b0 4026
c7f0d2c0 4027 <<Leave>>
ab8843fa
HK
4028 -- Initialize the refined state of a variable here because this is a
4029 -- common destination for legal and illegal object declarations.
4030
4031 if Ekind (Id) = E_Variable then
d7af5ea5 4032 Set_Encapsulating_State (Id, Empty);
ab8843fa
HK
4033 end if;
4034
eaba57fb
RD
4035 if Has_Aspects (N) then
4036 Analyze_Aspect_Specifications (N, Id);
4037 end if;
54c04d6c 4038
dec6faf1 4039 Analyze_Dimension (N);
0812b84e
AC
4040
4041 -- Verify whether the object declaration introduces an illegal hidden
4042 -- state within a package subject to a null abstract state.
4043
e19fd0bd 4044 if Ekind (Id) = E_Variable then
0812b84e
AC
4045 Check_No_Hidden_State (Id);
4046 end if;
0f1a6a0b 4047 end Analyze_Object_Declaration;
996ae0b0 4048
0f1a6a0b
AC
4049 ---------------------------
4050 -- Analyze_Others_Choice --
4051 ---------------------------
996ae0b0 4052
0f1a6a0b
AC
4053 -- Nothing to do for the others choice node itself, the semantic analysis
4054 -- of the others choice will occur as part of the processing of the parent
88b32fc3 4055
0f1a6a0b
AC
4056 procedure Analyze_Others_Choice (N : Node_Id) is
4057 pragma Warnings (Off, N);
4058 begin
4059 null;
4060 end Analyze_Others_Choice;
88b32fc3 4061
0f1a6a0b
AC
4062 -------------------------------------------
4063 -- Analyze_Private_Extension_Declaration --
4064 -------------------------------------------
88b32fc3 4065
0f1a6a0b
AC
4066 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4067 T : constant Entity_Id := Defining_Identifier (N);
4068 Indic : constant Node_Id := Subtype_Indication (N);
0f1a6a0b
AC
4069 Parent_Type : Entity_Id;
4070 Parent_Base : Entity_Id;
88b32fc3 4071
0f1a6a0b
AC
4072 begin
4073 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
88b32fc3 4074
0f1a6a0b
AC
4075 if Is_Non_Empty_List (Interface_List (N)) then
4076 declare
4077 Intf : Node_Id;
4078 T : Entity_Id;
88b32fc3 4079
0f1a6a0b
AC
4080 begin
4081 Intf := First (Interface_List (N));
4082 while Present (Intf) loop
4083 T := Find_Type_Of_Subtype_Indic (Intf);
996ae0b0 4084
0f1a6a0b
AC
4085 Diagnose_Interface (Intf, T);
4086 Next (Intf);
4087 end loop;
4088 end;
996ae0b0
RK
4089 end if;
4090
0f1a6a0b 4091 Generate_Definition (T);
6191e212 4092
e606088a
AC
4093 -- For other than Ada 2012, just enter the name in the current scope
4094
6191e212
AC
4095 if Ada_Version < Ada_2012 then
4096 Enter_Name (T);
4097
4098 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4099 -- case of private type that completes an incomplete type.
4100
4101 else
4102 declare
4103 Prev : Entity_Id;
4104
4105 begin
4106 Prev := Find_Type_Name (N);
4107
4108 pragma Assert (Prev = T
4109 or else (Ekind (Prev) = E_Incomplete_Type
4110 and then Present (Full_View (Prev))
4111 and then Full_View (Prev) = T));
4112 end;
4113 end if;
996ae0b0 4114
0f1a6a0b
AC
4115 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4116 Parent_Base := Base_Type (Parent_Type);
996ae0b0 4117
0f1a6a0b
AC
4118 if Parent_Type = Any_Type
4119 or else Etype (Parent_Type) = Any_Type
4120 then
4121 Set_Ekind (T, Ekind (Parent_Type));
4122 Set_Etype (T, Any_Type);
4123 goto Leave;
996ae0b0 4124
0f1a6a0b
AC
4125 elsif not Is_Tagged_Type (Parent_Type) then
4126 Error_Msg_N
4127 ("parent of type extension must be a tagged type ", Indic);
4128 goto Leave;
996ae0b0 4129
0f1a6a0b
AC
4130 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4131 Error_Msg_N ("premature derivation of incomplete type", Indic);
4132 goto Leave;
4133
4134 elsif Is_Concurrent_Type (Parent_Type) then
4135 Error_Msg_N
4136 ("parent type of a private extension cannot be "
4137 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
4138
4139 Set_Etype (T, Any_Type);
4140 Set_Ekind (T, E_Limited_Private_Type);
4141 Set_Private_Dependents (T, New_Elmt_List);
4142 Set_Error_Posted (T);
4143 goto Leave;
4144 end if;
4145
4146 -- Perhaps the parent type should be changed to the class-wide type's
4147 -- specific type in this case to prevent cascading errors ???
996ae0b0 4148
0f1a6a0b
AC
4149 if Is_Class_Wide_Type (Parent_Type) then
4150 Error_Msg_N
4151 ("parent of type extension must not be a class-wide type", Indic);
4152 goto Leave;
2b73cf68
JM
4153 end if;
4154
0f1a6a0b
AC
4155 if (not Is_Package_Or_Generic_Package (Current_Scope)
4156 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4157 or else In_Private_Part (Current_Scope)
4158
996ae0b0 4159 then
0f1a6a0b
AC
4160 Error_Msg_N ("invalid context for private extension", N);
4161 end if;
2b73cf68 4162
0f1a6a0b 4163 -- Set common attributes
2b73cf68 4164
0f1a6a0b
AC
4165 Set_Is_Pure (T, Is_Pure (Current_Scope));
4166 Set_Scope (T, Current_Scope);
4167 Set_Ekind (T, E_Record_Type_With_Private);
4168 Init_Size_Align (T);
996ae0b0 4169
0f1a6a0b
AC
4170 Set_Etype (T, Parent_Base);
4171 Set_Has_Task (T, Has_Task (Parent_Base));
4172
4173 Set_Convention (T, Convention (Parent_Type));
4174 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4175 Set_Is_First_Subtype (T);
4176 Make_Class_Wide_Type (T);
4177
4178 if Unknown_Discriminants_Present (N) then
4179 Set_Discriminant_Constraint (T, No_Elist);
996ae0b0
RK
4180 end if;
4181
0f1a6a0b 4182 Build_Derived_Record_Type (N, Parent_Type, T);
996ae0b0 4183
f2264ac2
RD
4184 -- Propagate inherited invariant information. The new type has
4185 -- invariants, if the parent type has inheritable invariants,
4186 -- and these invariants can in turn be inherited.
4187
4188 if Has_Inheritable_Invariants (Parent_Type) then
4189 Set_Has_Inheritable_Invariants (T);
4190 Set_Has_Invariants (T);
4191 end if;
4192
0f1a6a0b
AC
4193 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4194 -- synchronized formal derived type.
996ae0b0 4195
0f1a6a0b
AC
4196 if Ada_Version >= Ada_2005
4197 and then Synchronized_Present (N)
996ae0b0 4198 then
0f1a6a0b 4199 Set_Is_Limited_Record (T);
996ae0b0 4200
0f1a6a0b 4201 -- Formal derived type case
996ae0b0 4202
0f1a6a0b 4203 if Is_Generic_Type (T) then
996ae0b0 4204
0f1a6a0b
AC
4205 -- The parent must be a tagged limited type or a synchronized
4206 -- interface.
996ae0b0 4207
0f1a6a0b
AC
4208 if (not Is_Tagged_Type (Parent_Type)
4209 or else not Is_Limited_Type (Parent_Type))
996ae0b0 4210 and then
0f1a6a0b
AC
4211 (not Is_Interface (Parent_Type)
4212 or else not Is_Synchronized_Interface (Parent_Type))
996ae0b0 4213 then
0f1a6a0b
AC
4214 Error_Msg_NE ("parent type of & must be tagged limited " &
4215 "or synchronized", N, T);
4216 end if;
4217
4218 -- The progenitors (if any) must be limited or synchronized
4219 -- interfaces.
4220
4221 if Present (Interfaces (T)) then
996ae0b0 4222 declare
0f1a6a0b
AC
4223 Iface : Entity_Id;
4224 Iface_Elmt : Elmt_Id;
4225
996ae0b0 4226 begin
0f1a6a0b
AC
4227 Iface_Elmt := First_Elmt (Interfaces (T));
4228 while Present (Iface_Elmt) loop
4229 Iface := Node (Iface_Elmt);
996ae0b0 4230
0f1a6a0b
AC
4231 if not Is_Limited_Interface (Iface)
4232 and then not Is_Synchronized_Interface (Iface)
4233 then
4234 Error_Msg_NE ("progenitor & must be limited " &
4235 "or synchronized", N, Iface);
4236 end if;
4237
4238 Next_Elmt (Iface_Elmt);
4239 end loop;
996ae0b0
RK
4240 end;
4241 end if;
996ae0b0 4242
0f1a6a0b
AC
4243 -- Regular derived extension, the parent must be a limited or
4244 -- synchronized interface.
996ae0b0 4245
0f1a6a0b
AC
4246 else
4247 if not Is_Interface (Parent_Type)
4248 or else (not Is_Limited_Interface (Parent_Type)
4249 and then
4250 not Is_Synchronized_Interface (Parent_Type))
4251 then
4252 Error_Msg_NE
4253 ("parent type of & must be limited interface", N, T);
4254 end if;
4255 end if;
ce9e9122 4256
0f1a6a0b
AC
4257 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4258 -- extension with a synchronized parent must be explicitly declared
4259 -- synchronized, because the full view will be a synchronized type.
4260 -- This must be checked before the check for limited types below,
4261 -- to ensure that types declared limited are not allowed to extend
4262 -- synchronized interfaces.
996ae0b0 4263
0f1a6a0b
AC
4264 elsif Is_Interface (Parent_Type)
4265 and then Is_Synchronized_Interface (Parent_Type)
4266 and then not Synchronized_Present (N)
4267 then
4268 Error_Msg_NE
4269 ("private extension of& must be explicitly synchronized",
4270 N, Parent_Type);
996ae0b0 4271
0f1a6a0b
AC
4272 elsif Limited_Present (N) then
4273 Set_Is_Limited_Record (T);
996ae0b0 4274
0f1a6a0b
AC
4275 if not Is_Limited_Type (Parent_Type)
4276 and then
4277 (not Is_Interface (Parent_Type)
4278 or else not Is_Limited_Interface (Parent_Type))
4279 then
4280 Error_Msg_NE ("parent type& of limited extension must be limited",
4281 N, Parent_Type);
4282 end if;
4283 end if;
fbf5a39b 4284
eaba57fb
RD
4285 <<Leave>>
4286 if Has_Aspects (N) then
4287 Analyze_Aspect_Specifications (N, T);
4288 end if;
0f1a6a0b 4289 end Analyze_Private_Extension_Declaration;
950d3e7d 4290
0f1a6a0b
AC
4291 ---------------------------------
4292 -- Analyze_Subtype_Declaration --
4293 ---------------------------------
950d3e7d 4294
0f1a6a0b
AC
4295 procedure Analyze_Subtype_Declaration
4296 (N : Node_Id;
4297 Skip : Boolean := False)
4298 is
4299 Id : constant Entity_Id := Defining_Identifier (N);
0f1a6a0b
AC
4300 T : Entity_Id;
4301 R_Checks : Check_Result;
950d3e7d 4302
0f1a6a0b
AC
4303 begin
4304 Generate_Definition (Id);
4305 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4306 Init_Size_Align (Id);
950d3e7d 4307
0f1a6a0b
AC
4308 -- The following guard condition on Enter_Name is to handle cases where
4309 -- the defining identifier has already been entered into the scope but
4310 -- the declaration as a whole needs to be analyzed.
950d3e7d 4311
0f1a6a0b
AC
4312 -- This case in particular happens for derived enumeration types. The
4313 -- derived enumeration type is processed as an inserted enumeration type
4314 -- declaration followed by a rewritten subtype declaration. The defining
4315 -- identifier, however, is entered into the name scope very early in the
4316 -- processing of the original type declaration and therefore needs to be
4317 -- avoided here, when the created subtype declaration is analyzed. (See
4318 -- Build_Derived_Types)
950d3e7d 4319
0f1a6a0b
AC
4320 -- This also happens when the full view of a private type is derived
4321 -- type with constraints. In this case the entity has been introduced
4322 -- in the private declaration.
950d3e7d 4323
ad0d71b5 4324 -- Finally this happens in some complex cases when validity checks are
cf895a01
AC
4325 -- enabled, where the same subtype declaration may be analyzed twice.
4326 -- This can happen if the subtype is created by the pre-analysis of
4327 -- an attribute tht gives the range of a loop statement, and the loop
4328 -- itself appears within an if_statement that will be rewritten during
4329 -- expansion.
4330
0f1a6a0b
AC
4331 if Skip
4332 or else (Present (Etype (Id))
0f853035
YM
4333 and then (Is_Private_Type (Etype (Id))
4334 or else Is_Task_Type (Etype (Id))
4335 or else Is_Rewrite_Substitution (N)))
0f1a6a0b
AC
4336 then
4337 null;
950d3e7d 4338
cf895a01
AC
4339 elsif Current_Entity (Id) = Id then
4340 null;
4341
0f1a6a0b
AC
4342 else
4343 Enter_Name (Id);
4344 end if;
950d3e7d 4345
0f1a6a0b 4346 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
996ae0b0 4347
b38c20a6
AC
4348 -- Class-wide equivalent types of records with unknown discriminants
4349 -- involve the generation of an itype which serves as the private view
4350 -- of a constrained record subtype. In such cases the base type of the
4351 -- current subtype we are processing is the private itype. Use the full
4352 -- of the private itype when decorating various attributes.
4353
4354 if Is_Itype (T)
4355 and then Is_Private_Type (T)
4356 and then Present (Full_View (T))
4357 then
4358 T := Full_View (T);
4359 end if;
4360
0f1a6a0b 4361 -- Inherit common attributes
19f0526a 4362
0f1a6a0b
AC
4363 Set_Is_Volatile (Id, Is_Volatile (T));
4364 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
dc3af7e2 4365 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
0f1a6a0b 4366 Set_Convention (Id, Convention (T));
86200f66
RD
4367
4368 -- If ancestor has predicates then so does the subtype, and in addition
4369 -- we must delay the freeze to properly arrange predicate inheritance.
4370
f2acf80c
AC
4371 -- The Ancestor_Type test is a big kludge, there seem to be cases in
4372 -- which T = ID, so the above tests and assignments do nothing???
4373
4374 if Has_Predicates (T)
4375 or else (Present (Ancestor_Subtype (T))
0f853035 4376 and then Has_Predicates (Ancestor_Subtype (T)))
f2acf80c 4377 then
86200f66
RD
4378 Set_Has_Predicates (Id);
4379 Set_Has_Delayed_Freeze (Id);
4380 end if;
fbf5a39b 4381
2ba431e5 4382 -- Subtype of Boolean cannot have a constraint in SPARK
7ff2d234 4383
fe5d3068 4384 if Is_Boolean_Type (T)
7ff2d234
AC
4385 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4386 then
2ba431e5 4387 Check_SPARK_Restriction
fe5d3068 4388 ("subtype of Boolean cannot have constraint", N);
7ff2d234
AC
4389 end if;
4390
23685ae6 4391 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
d8b962d8 4392 declare
23685ae6
AC
4393 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4394 One_Cstr : Node_Id;
4395 Low : Node_Id;
4396 High : Node_Id;
176dadf6 4397
d8b962d8 4398 begin
23685ae6
AC
4399 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4400 One_Cstr := First (Constraints (Cstr));
4401 while Present (One_Cstr) loop
d8b962d8 4402
2ba431e5
YM
4403 -- Index or discriminant constraint in SPARK must be a
4404 -- subtype mark.
d8b962d8 4405
23685ae6
AC
4406 if not
4407 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
d8b962d8 4408 then
2ba431e5 4409 Check_SPARK_Restriction
23685ae6
AC
4410 ("subtype mark required", One_Cstr);
4411
4412 -- String subtype must have a lower bound of 1 in SPARK.
4413 -- Note that we do not need to test for the non-static case
4414 -- here, since that was already taken care of in
4415 -- Process_Range_Expr_In_Decl.
4416
4417 elsif Base_Type (T) = Standard_String then
4418 Get_Index_Bounds (One_Cstr, Low, High);
4419
4420 if Is_OK_Static_Expression (Low)
4421 and then Expr_Value (Low) /= 1
4422 then
2ba431e5 4423 Check_SPARK_Restriction
23685ae6
AC
4424 ("String subtype must have lower bound of 1", N);
4425 end if;
d8b962d8 4426 end if;
23685ae6
AC
4427
4428 Next (One_Cstr);
4429 end loop;
d8b962d8
AC
4430 end if;
4431 end;
4432 end if;
4433
0f1a6a0b
AC
4434 -- In the case where there is no constraint given in the subtype
4435 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4436 -- semantic attributes must be established here.
fea9e956 4437
0f1a6a0b
AC
4438 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4439 Set_Etype (Id, Base_Type (T));
fea9e956 4440
7ff2d234 4441 -- Subtype of unconstrained array without constraint is not allowed
2ba431e5 4442 -- in SPARK.
7ff2d234 4443
fe5d3068 4444 if Is_Array_Type (T)
7ff2d234
AC
4445 and then not Is_Constrained (T)
4446 then
2ba431e5 4447 Check_SPARK_Restriction
fe5d3068 4448 ("subtype of unconstrained array must have constraint", N);
7ff2d234
AC
4449 end if;
4450
0f1a6a0b
AC
4451 case Ekind (T) is
4452 when Array_Kind =>
4453 Set_Ekind (Id, E_Array_Subtype);
4454 Copy_Array_Subtype_Attributes (Id, T);
996ae0b0 4455
0f1a6a0b
AC
4456 when Decimal_Fixed_Point_Kind =>
4457 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4458 Set_Digits_Value (Id, Digits_Value (T));
4459 Set_Delta_Value (Id, Delta_Value (T));
4460 Set_Scale_Value (Id, Scale_Value (T));
4461 Set_Small_Value (Id, Small_Value (T));
4462 Set_Scalar_Range (Id, Scalar_Range (T));
4463 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4464 Set_Is_Constrained (Id, Is_Constrained (T));
4465 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4466 Set_RM_Size (Id, RM_Size (T));
996ae0b0 4467
0f1a6a0b
AC
4468 when Enumeration_Kind =>
4469 Set_Ekind (Id, E_Enumeration_Subtype);
4470 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4471 Set_Scalar_Range (Id, Scalar_Range (T));
4472 Set_Is_Character_Type (Id, Is_Character_Type (T));
4473 Set_Is_Constrained (Id, Is_Constrained (T));
4474 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4475 Set_RM_Size (Id, RM_Size (T));
996ae0b0 4476
0f1a6a0b
AC
4477 when Ordinary_Fixed_Point_Kind =>
4478 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4479 Set_Scalar_Range (Id, Scalar_Range (T));
4480 Set_Small_Value (Id, Small_Value (T));
4481 Set_Delta_Value (Id, Delta_Value (T));
4482 Set_Is_Constrained (Id, Is_Constrained (T));
4483 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4484 Set_RM_Size (Id, RM_Size (T));
996ae0b0 4485
0f1a6a0b
AC
4486 when Float_Kind =>
4487 Set_Ekind (Id, E_Floating_Point_Subtype);
4488 Set_Scalar_Range (Id, Scalar_Range (T));
4489 Set_Digits_Value (Id, Digits_Value (T));
4490 Set_Is_Constrained (Id, Is_Constrained (T));
996ae0b0 4491
0f1a6a0b
AC
4492 when Signed_Integer_Kind =>
4493 Set_Ekind (Id, E_Signed_Integer_Subtype);
4494 Set_Scalar_Range (Id, Scalar_Range (T));
4495 Set_Is_Constrained (Id, Is_Constrained (T));
4496 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4497 Set_RM_Size (Id, RM_Size (T));
996ae0b0 4498
0f1a6a0b
AC
4499 when Modular_Integer_Kind =>
4500 Set_Ekind (Id, E_Modular_Integer_Subtype);
4501 Set_Scalar_Range (Id, Scalar_Range (T));
4502 Set_Is_Constrained (Id, Is_Constrained (T));
4503 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4504 Set_RM_Size (Id, RM_Size (T));
996ae0b0 4505
0f1a6a0b
AC
4506 when Class_Wide_Kind =>
4507 Set_Ekind (Id, E_Class_Wide_Subtype);
4508 Set_First_Entity (Id, First_Entity (T));
4509 Set_Last_Entity (Id, Last_Entity (T));
4510 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4511 Set_Cloned_Subtype (Id, T);
4512 Set_Is_Tagged_Type (Id, True);
4513 Set_Has_Unknown_Discriminants
4514 (Id, True);
996ae0b0 4515
0f1a6a0b
AC
4516 if Ekind (T) = E_Class_Wide_Subtype then
4517 Set_Equivalent_Type (Id, Equivalent_Type (T));
4518 end if;
996ae0b0 4519
0f1a6a0b
AC
4520 when E_Record_Type | E_Record_Subtype =>
4521 Set_Ekind (Id, E_Record_Subtype);
996ae0b0 4522
0f1a6a0b
AC
4523 if Ekind (T) = E_Record_Subtype
4524 and then Present (Cloned_Subtype (T))
4525 then
4526 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4527 else
4528 Set_Cloned_Subtype (Id, T);
4529 end if;
996ae0b0 4530
0f1a6a0b
AC
4531 Set_First_Entity (Id, First_Entity (T));
4532 Set_Last_Entity (Id, Last_Entity (T));
4533 Set_Has_Discriminants (Id, Has_Discriminants (T));
4534 Set_Is_Constrained (Id, Is_Constrained (T));
4535 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
44a10091
AC
4536 Set_Has_Implicit_Dereference
4537 (Id, Has_Implicit_Dereference (T));
0f1a6a0b
AC
4538 Set_Has_Unknown_Discriminants
4539 (Id, Has_Unknown_Discriminants (T));
996ae0b0 4540
0f1a6a0b
AC
4541 if Has_Discriminants (T) then
4542 Set_Discriminant_Constraint
4543 (Id, Discriminant_Constraint (T));
4544 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
996ae0b0 4545
0f1a6a0b
AC
4546 elsif Has_Unknown_Discriminants (Id) then
4547 Set_Discriminant_Constraint (Id, No_Elist);
4548 end if;
996ae0b0 4549
0f1a6a0b
AC
4550 if Is_Tagged_Type (T) then
4551 Set_Is_Tagged_Type (Id);
4552 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
ef2a63ba
JM
4553 Set_Direct_Primitive_Operations
4554 (Id, Direct_Primitive_Operations (T));
0f1a6a0b 4555 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
996ae0b0 4556
0f1a6a0b
AC
4557 if Is_Interface (T) then
4558 Set_Is_Interface (Id);
4559 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4560 end if;
4561 end if;
996ae0b0 4562
0f1a6a0b 4563 when Private_Kind =>
dedac3eb
RD
4564 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4565 Set_Has_Discriminants (Id, Has_Discriminants (T));
4566 Set_Is_Constrained (Id, Is_Constrained (T));
4567 Set_First_Entity (Id, First_Entity (T));
4568 Set_Last_Entity (Id, Last_Entity (T));
0f1a6a0b 4569 Set_Private_Dependents (Id, New_Elmt_List);
dedac3eb 4570 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
44a10091 4571 Set_Has_Implicit_Dereference
dedac3eb 4572 (Id, Has_Implicit_Dereference (T));
0f1a6a0b 4573 Set_Has_Unknown_Discriminants
dedac3eb 4574 (Id, Has_Unknown_Discriminants (T));
0f1a6a0b
AC
4575 Set_Known_To_Have_Preelab_Init
4576 (Id, Known_To_Have_Preelab_Init (T));
4577
4578 if Is_Tagged_Type (T) then
ef2a63ba
JM
4579 Set_Is_Tagged_Type (Id);
4580 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
dedac3eb 4581 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
ef2a63ba
JM
4582 Set_Direct_Primitive_Operations (Id,
4583 Direct_Primitive_Operations (T));
996ae0b0
RK
4584 end if;
4585
0f1a6a0b
AC
4586 -- In general the attributes of the subtype of a private type
4587 -- are the attributes of the partial view of parent. However,
4588 -- the full view may be a discriminated type, and the subtype
4589 -- must share the discriminant constraint to generate correct
4590 -- calls to initialization procedures.
996ae0b0 4591
0f1a6a0b
AC
4592 if Has_Discriminants (T) then
4593 Set_Discriminant_Constraint
dedac3eb 4594 (Id, Discriminant_Constraint (T));
0f1a6a0b 4595 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
996ae0b0 4596
0f1a6a0b
AC
4597 elsif Present (Full_View (T))
4598 and then Has_Discriminants (Full_View (T))
4599 then
4600 Set_Discriminant_Constraint
dedac3eb 4601 (Id, Discriminant_Constraint (Full_View (T)));
0f1a6a0b 4602 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
996ae0b0 4603
0f1a6a0b 4604 -- This would seem semantically correct, but apparently
a52e6d7e 4605 -- generates spurious errors about missing components ???
996ae0b0 4606
0f1a6a0b
AC
4607 -- Set_Has_Discriminants (Id);
4608 end if;
996ae0b0 4609
0f1a6a0b 4610 Prepare_Private_Subtype_Completion (Id, N);
996ae0b0 4611
a52e6d7e
AC
4612 -- If this is the subtype of a constrained private type with
4613 -- discriminants that has got a full view and we also have
4614 -- built a completion just above, show that the completion
4615 -- is a clone of the full view to the back-end.
4616
4617 if Has_Discriminants (T)
4618 and then not Has_Unknown_Discriminants (T)
4619 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
4620 and then Present (Full_View (T))
4621 and then Present (Full_View (Id))
4622 then
4623 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
4624 end if;
4625
0f1a6a0b
AC
4626 when Access_Kind =>
4627 Set_Ekind (Id, E_Access_Subtype);
4628 Set_Is_Constrained (Id, Is_Constrained (T));
4629 Set_Is_Access_Constant
4630 (Id, Is_Access_Constant (T));
4631 Set_Directly_Designated_Type
4632 (Id, Designated_Type (T));
4633 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4634
4635 -- A Pure library_item must not contain the declaration of a
4636 -- named access type, except within a subprogram, generic
4637 -- subprogram, task unit, or protected unit, or if it has
4638 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4639
4640 if Comes_From_Source (Id)
4641 and then In_Pure_Unit
4642 and then not In_Subprogram_Task_Protected_Unit
4643 and then not No_Pool_Assigned (Id)
996ae0b0 4644 then
0f1a6a0b
AC
4645 Error_Msg_N
4646 ("named access types not allowed in pure unit", N);
996ae0b0
RK
4647 end if;
4648
0f1a6a0b
AC
4649 when Concurrent_Kind =>
4650 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4651 Set_Corresponding_Record_Type (Id,
4652 Corresponding_Record_Type (T));
4653 Set_First_Entity (Id, First_Entity (T));
4654 Set_First_Private_Entity (Id, First_Private_Entity (T));
4655 Set_Has_Discriminants (Id, Has_Discriminants (T));
4656 Set_Is_Constrained (Id, Is_Constrained (T));
4657 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4658 Set_Last_Entity (Id, Last_Entity (T));
8a6a52dc 4659
0f1a6a0b
AC
4660 if Has_Discriminants (T) then
4661 Set_Discriminant_Constraint (Id,
4662 Discriminant_Constraint (T));
4663 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
8a6a52dc
AC
4664 end if;
4665
0f1a6a0b
AC
4666 when E_Incomplete_Type =>
4667 if Ada_Version >= Ada_2005 then
996ae0b0 4668
d3b00ce3
AC
4669 -- In Ada 2005 an incomplete type can be explicitly tagged:
4670 -- propagate indication.
967e927f
ES
4671
4672 Set_Ekind (Id, E_Incomplete_Subtype);
4673 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4674 Set_Private_Dependents (Id, New_Elmt_List);
4675
4676 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
4677 -- incomplete type visible through a limited with clause.
996ae0b0 4678
7b56a91b 4679 if From_Limited_With (T)
0f1a6a0b
AC
4680 and then Present (Non_Limited_View (T))
4681 then
7b56a91b
AC
4682 Set_From_Limited_With (Id);
4683 Set_Non_Limited_View (Id, Non_Limited_View (T));
996ae0b0 4684
0f1a6a0b
AC
4685 -- Ada 2005 (AI-412): Add the regular incomplete subtype
4686 -- to the private dependents of the original incomplete
4687 -- type for future transformation.
996ae0b0 4688
0f1a6a0b
AC
4689 else
4690 Append_Elmt (Id, Private_Dependents (T));
4691 end if;
996ae0b0 4692
0f1a6a0b
AC
4693 -- If the subtype name denotes an incomplete type an error
4694 -- was already reported by Process_Subtype.
1c218ac3 4695
0f1a6a0b
AC
4696 else
4697 Set_Etype (Id, Any_Type);
4698 end if;
1c218ac3 4699
996ae0b0
RK
4700 when others =>
4701 raise Program_Error;
996ae0b0
RK
4702 end case;
4703 end if;
4704
0f1a6a0b
AC
4705 if Etype (Id) = Any_Type then
4706 goto Leave;
996ae0b0
RK
4707 end if;
4708
0f1a6a0b 4709 -- Some common processing on all types
996ae0b0 4710
2808600b 4711 Set_Size_Info (Id, T);
0f1a6a0b 4712 Set_First_Rep_Item (Id, First_Rep_Item (T));
996ae0b0 4713
2808600b
ES
4714 -- If the parent type is a generic actual, so is the subtype. This may
4715 -- happen in a nested instance. Why Comes_From_Source test???
4716
4717 if not Comes_From_Source (N) then
4718 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
4719 end if;
4720
0f1a6a0b 4721 T := Etype (Id);
996ae0b0 4722
0f1a6a0b
AC
4723 Set_Is_Immediately_Visible (Id, True);
4724 Set_Depends_On_Private (Id, Has_Private_Component (T));
4725 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
996ae0b0 4726
0f1a6a0b
AC
4727 if Is_Interface (T) then
4728 Set_Is_Interface (Id);
4729 end if;
03b64787 4730
0f1a6a0b
AC
4731 if Present (Generic_Parent_Type (N))
4732 and then
4733 (Nkind
4734 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
4735 or else Nkind
4736 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
4737 /= N_Formal_Private_Type_Definition)
4738 then
4739 if Is_Tagged_Type (Id) then
996ae0b0 4740
0f1a6a0b
AC
4741 -- If this is a generic actual subtype for a synchronized type,
4742 -- the primitive operations are those of the corresponding record
4743 -- for which there is a separate subtype declaration.
996ae0b0 4744
0f1a6a0b
AC
4745 if Is_Concurrent_Type (Id) then
4746 null;
4747 elsif Is_Class_Wide_Type (Id) then
4748 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
4749 else
4750 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
4751 end if;
8737a29a 4752
0f1a6a0b
AC
4753 elsif Scope (Etype (Id)) /= Standard_Standard then
4754 Derive_Subprograms (Generic_Parent_Type (N), Id);
996ae0b0 4755 end if;
0f1a6a0b 4756 end if;
996ae0b0 4757
0f1a6a0b
AC
4758 if Is_Private_Type (T)
4759 and then Present (Full_View (T))
4760 then
4761 Conditional_Delay (Id, Full_View (T));
996ae0b0 4762
0f1a6a0b
AC
4763 -- The subtypes of components or subcomponents of protected types
4764 -- do not need freeze nodes, which would otherwise appear in the
4765 -- wrong scope (before the freeze node for the protected type). The
4766 -- proper subtypes are those of the subcomponents of the corresponding
4767 -- record.
996ae0b0 4768
0f1a6a0b 4769 elsif Ekind (Scope (Id)) /= E_Protected_Type
a90bd866 4770 and then Present (Scope (Scope (Id))) -- error defense
0f1a6a0b
AC
4771 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
4772 then
4773 Conditional_Delay (Id, T);
4774 end if;
996ae0b0 4775
f9adb9d4
AC
4776 -- Check that Constraint_Error is raised for a scalar subtype indication
4777 -- when the lower or upper bound of a non-null range lies outside the
4778 -- range of the type mark.
fea9e956 4779
0f1a6a0b
AC
4780 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4781 if Is_Scalar_Type (Etype (Id))
4782 and then Scalar_Range (Id) /=
4783 Scalar_Range (Etype (Subtype_Mark
4784 (Subtype_Indication (N))))
4785 then
4786 Apply_Range_Check
4787 (Scalar_Range (Id),
4788 Etype (Subtype_Mark (Subtype_Indication (N))));
fea9e956 4789
f9adb9d4
AC
4790 -- In the array case, check compatibility for each index
4791
0f1a6a0b
AC
4792 elsif Is_Array_Type (Etype (Id))
4793 and then Present (First_Index (Id))
4794 then
4795 -- This really should be a subprogram that finds the indications
4796 -- to check???
996ae0b0 4797
f9adb9d4
AC
4798 declare
4799 Subt_Index : Node_Id := First_Index (Id);
4800 Target_Index : Node_Id :=
4801 First_Index (Etype
4802 (Subtype_Mark (Subtype_Indication (N))));
4803 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
4804
4805 begin
4806 while Present (Subt_Index) loop
4807 if ((Nkind (Subt_Index) = N_Identifier
4808 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
4809 or else Nkind (Subt_Index) = N_Subtype_Indication)
4810 and then
4811 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
4812 then
4813 declare
4814 Target_Typ : constant Entity_Id :=
4815 Etype (Target_Index);
4816 begin
4817 R_Checks :=
4818 Get_Range_Checks
4819 (Scalar_Range (Etype (Subt_Index)),
4820 Target_Typ,
4821 Etype (Subt_Index),
4822 Defining_Identifier (N));
4823
4824 -- Reset Has_Dynamic_Range_Check on the subtype to
4825 -- prevent elision of the index check due to a dynamic
4826 -- check generated for a preceding index (needed since
4827 -- Insert_Range_Checks tries to avoid generating
4828 -- redundant checks on a given declaration).
4829
4830 Set_Has_Dynamic_Range_Check (N, False);
4831
4832 Insert_Range_Checks
4833 (R_Checks,
4834 N,
4835 Target_Typ,
4836 Sloc (Defining_Identifier (N)));
4837
4838 -- Record whether this index involved a dynamic check
4839
4840 Has_Dyn_Chk :=
4841 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
4842 end;
4843 end if;
4844
4845 Next_Index (Subt_Index);
4846 Next_Index (Target_Index);
4847 end loop;
4848
4849 -- Finally, mark whether the subtype involves dynamic checks
4850
4851 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
4852 end;
0f1a6a0b
AC
4853 end if;
4854 end if;
996ae0b0 4855
6b958cec 4856 -- Make sure that generic actual types are properly frozen. The subtype
718deaf1
AC
4857 -- is marked as a generic actual type when the enclosing instance is
4858 -- analyzed, so here we identify the subtype from the tree structure.
c159409f
AC
4859
4860 if Expander_Active
4861 and then Is_Generic_Actual_Type (Id)
718deaf1
AC
4862 and then In_Instance
4863 and then not Comes_From_Source (N)
4864 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
4865 and then Is_Frozen (T)
c159409f 4866 then
6b958cec 4867 Freeze_Before (N, Id);
c159409f
AC
4868 end if;
4869
0f1a6a0b
AC
4870 Set_Optimize_Alignment_Flags (Id);
4871 Check_Eliminated (Id);
996ae0b0 4872
2d4e0553 4873 <<Leave>>
eaba57fb
RD
4874 if Has_Aspects (N) then
4875 Analyze_Aspect_Specifications (N, Id);
4876 end if;
54c04d6c 4877
dec6faf1 4878 Analyze_Dimension (N);
0f1a6a0b 4879 end Analyze_Subtype_Declaration;
996ae0b0 4880
0f1a6a0b
AC
4881 --------------------------------
4882 -- Analyze_Subtype_Indication --
4883 --------------------------------
9c510803 4884
0f1a6a0b
AC
4885 procedure Analyze_Subtype_Indication (N : Node_Id) is
4886 T : constant Entity_Id := Subtype_Mark (N);
4887 R : constant Node_Id := Range_Expression (Constraint (N));
4888
4889 begin
4890 Analyze (T);
4891
4892 if R /= Error then
4893 Analyze (R);
4894 Set_Etype (N, Etype (R));
4895 Resolve (R, Entity (T));
4896 else
4897 Set_Error_Posted (R);
4898 Set_Error_Posted (T);
4899 end if;
4900 end Analyze_Subtype_Indication;
996ae0b0
RK
4901
4902 --------------------------
4903 -- Analyze_Variant_Part --
4904 --------------------------
4905
4906 procedure Analyze_Variant_Part (N : Node_Id) is
15918371
AC
4907 Discr_Name : Node_Id;
4908 Discr_Type : Entity_Id;
996ae0b0 4909
15918371
AC
4910 procedure Process_Variant (A : Node_Id);
4911 -- Analyze declarations for a single variant
996ae0b0 4912
15918371
AC
4913 package Analyze_Variant_Choices is
4914 new Generic_Analyze_Choices (Process_Variant);
4915 use Analyze_Variant_Choices;
996ae0b0 4916
15918371
AC
4917 ---------------------
4918 -- Process_Variant --
4919 ---------------------
996ae0b0 4920
15918371
AC
4921 procedure Process_Variant (A : Node_Id) is
4922 CL : constant Node_Id := Component_List (A);
996ae0b0 4923 begin
15918371
AC
4924 if not Null_Present (CL) then
4925 Analyze_Declarations (Component_Items (CL));
996ae0b0 4926
15918371
AC
4927 if Present (Variant_Part (CL)) then
4928 Analyze (Variant_Part (CL));
996ae0b0
RK
4929 end if;
4930 end if;
15918371 4931 end Process_Variant;
0501956d 4932
996ae0b0
RK
4933 -- Start of processing for Analyze_Variant_Part
4934
4935 begin
4936 Discr_Name := Name (N);
4937 Analyze (Discr_Name);
4938
ce4a6e84 4939 -- If Discr_Name bad, get out (prevent cascaded errors)
2b73cf68 4940
ce4a6e84 4941 if Etype (Discr_Name) = Any_Type then
2b73cf68 4942 return;
ce4a6e84 4943 end if;
2b73cf68 4944
ce4a6e84
RD
4945 -- Check invalid discriminant in variant part
4946
4947 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
996ae0b0
RK
4948 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4949 end if;
4950
4951 Discr_Type := Etype (Entity (Discr_Name));
4952
855ff2e1
GB
4953 if not Is_Discrete_Type (Discr_Type) then
4954 Error_Msg_N
4955 ("discriminant in a variant part must be of a discrete type",
4956 Name (N));
4957 return;
4958 end if;
4959
15918371
AC
4960 -- Now analyze the choices, which also analyzes the declarations that
4961 -- are associated with each choice.
4962
4963 Analyze_Choices (Variants (N), Discr_Type);
4964
4965 -- Note: we used to instantiate and call Check_Choices here to check
4966 -- that the choices covered the discriminant, but it's too early to do
4967 -- that because of statically predicated subtypes, whose analysis may
4968 -- be deferred to their freeze point which may be as late as the freeze
4969 -- point of the containing record. So this call is now to be found in
4970 -- Freeze_Record_Declaration.
996ae0b0 4971
996ae0b0
RK
4972 end Analyze_Variant_Part;
4973
4974 ----------------------------
4975 -- Array_Type_Declaration --
4976 ----------------------------
4977
4978 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
a397db96 4979 Component_Def : constant Node_Id := Component_Definition (Def);
d8b962d8 4980 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
996ae0b0
RK
4981 Element_Type : Entity_Id;
4982 Implicit_Base : Entity_Id;
4983 Index : Node_Id;
4984 Related_Id : Entity_Id := Empty;
4985 Nb_Index : Nat;
4986 P : constant Node_Id := Parent (Def);
4987 Priv : Entity_Id;
4988
4989 begin
4990 if Nkind (Def) = N_Constrained_Array_Definition then
996ae0b0 4991 Index := First (Discrete_Subtype_Definitions (Def));
6e937c1c
AC
4992 else
4993 Index := First (Subtype_Marks (Def));
4994 end if;
996ae0b0 4995
33931112
JM
4996 -- Find proper names for the implicit types which may be public. In case
4997 -- of anonymous arrays we use the name of the first object of that type
4998 -- as prefix.
996ae0b0 4999
6e937c1c 5000 if No (T) then
23685ae6 5001 Related_Id := Defining_Identifier (P);
996ae0b0 5002 else
6e937c1c 5003 Related_Id := T;
996ae0b0
RK
5004 end if;
5005
5006 Nb_Index := 1;
996ae0b0 5007 while Present (Index) loop
db72f10a
AC
5008 Analyze (Index);
5009
23685ae6 5010 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
2ba431e5 5011 Check_SPARK_Restriction ("subtype mark required", Index);
d8b962d8
AC
5012 end if;
5013
88b32fc3
BD
5014 -- Add a subtype declaration for each index of private array type
5015 -- declaration whose etype is also private. For example:
5016
5017 -- package Pkg is
5018 -- type Index is private;
5019 -- private
5020 -- type Table is array (Index) of ...
5021 -- end;
5022
33931112
JM
5023 -- This is currently required by the expander for the internally
5024 -- generated equality subprogram of records with variant parts in
5025 -- which the etype of some component is such private type.
88b32fc3
BD
5026
5027 if Ekind (Current_Scope) = E_Package
5028 and then In_Private_Part (Current_Scope)
5029 and then Has_Private_Declaration (Etype (Index))
5030 then
5031 declare
5032 Loc : constant Source_Ptr := Sloc (Def);
5033 New_E : Entity_Id;
5034 Decl : Entity_Id;
5035
5036 begin
092ef350 5037 New_E := Make_Temporary (Loc, 'T');
88b32fc3
BD
5038 Set_Is_Internal (New_E);
5039
5040 Decl :=
5041 Make_Subtype_Declaration (Loc,
5042 Defining_Identifier => New_E,
5043 Subtype_Indication =>
5044 New_Occurrence_Of (Etype (Index), Loc));
5045
5046 Insert_Before (Parent (Def), Decl);
5047 Analyze (Decl);
5048 Set_Etype (Index, New_E);
5049
5050 -- If the index is a range the Entity attribute is not
5051 -- available. Example:
5052
5053 -- package Pkg is
5054 -- type T is private;
5055 -- private
5056 -- type T is new Natural;
5057 -- Table : array (T(1) .. T(10)) of Boolean;
5058 -- end Pkg;
5059
5060 if Nkind (Index) /= N_Range then
5061 Set_Entity (Index, New_E);
5062 end if;
5063 end;
5064 end if;
5065
996ae0b0 5066 Make_Index (Index, P, Related_Id, Nb_Index);
ea034236
AC
5067
5068 -- Check error of subtype with predicate for index type
5069
ed00f472
RD
5070 Bad_Predicated_Subtype_Use
5071 ("subtype& has predicate, not allowed as index subtype",
5072 Index, Etype (Index));
ea034236
AC
5073
5074 -- Move to next index
5075
996ae0b0
RK
5076 Next_Index (Index);
5077 Nb_Index := Nb_Index + 1;
5078 end loop;
5079
88b32fc3
BD
5080 -- Process subtype indication if one is present
5081
d8b962d8 5082 if Present (Component_Typ) then
9fd9d2be
AC
5083 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5084
cf161d66 5085 Set_Etype (Component_Typ, Element_Type);
db72f10a 5086
23685ae6 5087 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
2ba431e5 5088 Check_SPARK_Restriction ("subtype mark required", Component_Typ);
d8b962d8
AC
5089 end if;
5090
0ab80019 5091 -- Ada 2005 (AI-230): Access Definition case
6e937c1c 5092
9bc856dd 5093 else pragma Assert (Present (Access_Definition (Component_Def)));
fea9e956
ES
5094
5095 -- Indicate that the anonymous access type is created by the
5096 -- array type declaration.
5097
6e937c1c 5098 Element_Type := Access_Definition
fea9e956 5099 (Related_Nod => P,
6e937c1c 5100 N => Access_Definition (Component_Def));
758c442c 5101 Set_Is_Local_Anonymous_Access (Element_Type);
6e937c1c 5102
fea9e956
ES
5103 -- Propagate the parent. This field is needed if we have to generate
5104 -- the master_id associated with an anonymous access to task type
5105 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5106
5107 Set_Parent (Element_Type, Parent (T));
5108
33931112
JM
5109 -- Ada 2005 (AI-230): In case of components that are anonymous access
5110 -- types the level of accessibility depends on the enclosing type
5111 -- declaration
35b7fa6a 5112
0ab80019 5113 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
35b7fa6a 5114
0ab80019 5115 -- Ada 2005 (AI-254)
7324bf49 5116
af4b9434
AC
5117 declare
5118 CD : constant Node_Id :=
5119 Access_To_Subprogram_Definition
5120 (Access_Definition (Component_Def));
5121 begin
5122 if Present (CD) and then Protected_Present (CD) then
5123 Element_Type :=
fea9e956 5124 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
af4b9434
AC
5125 end if;
5126 end;
6e937c1c 5127 end if;
996ae0b0
RK
5128
5129 -- Constrained array case
5130
5131 if No (T) then
5132 T := Create_Itype (E_Void, P, Related_Id, 'T');
5133 end if;
5134
5135 if Nkind (Def) = N_Constrained_Array_Definition then
5136
5137 -- Establish Implicit_Base as unconstrained base type
5138
5139 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5140
996ae0b0
RK
5141 Set_Etype (Implicit_Base, Implicit_Base);
5142 Set_Scope (Implicit_Base, Current_Scope);
5143 Set_Has_Delayed_Freeze (Implicit_Base);
5144
5145 -- The constrained array type is a subtype of the unconstrained one
5146
5147 Set_Ekind (T, E_Array_Subtype);
5148 Init_Size_Align (T);
5149 Set_Etype (T, Implicit_Base);
5150 Set_Scope (T, Current_Scope);
5151 Set_Is_Constrained (T, True);
5152 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
5153 Set_Has_Delayed_Freeze (T);
5154
5155 -- Complete setup of implicit base type
5156
fea9e956
ES
5157 Set_First_Index (Implicit_Base, First_Index (T));
5158 Set_Component_Type (Implicit_Base, Element_Type);
5159 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
5160 Set_Component_Size (Implicit_Base, Uint_0);
5161 Set_Packed_Array_Type (Implicit_Base, Empty);
07fc65c4 5162 Set_Has_Controlled_Component
fea9e956
ES
5163 (Implicit_Base, Has_Controlled_Component
5164 (Element_Type)
5165 or else Is_Controlled
5166 (Element_Type));
07fc65c4 5167 Set_Finalize_Storage_Only
fea9e956
ES
5168 (Implicit_Base, Finalize_Storage_Only
5169 (Element_Type));
996ae0b0
RK
5170
5171 -- Unconstrained array case
5172
5173 else
5174 Set_Ekind (T, E_Array_Type);
5175 Init_Size_Align (T);
5176 Set_Etype (T, T);
5177 Set_Scope (T, Current_Scope);
5178 Set_Component_Size (T, Uint_0);
5179 Set_Is_Constrained (T, False);
5180 Set_First_Index (T, First (Subtype_Marks (Def)));
5181 Set_Has_Delayed_Freeze (T, True);
07fc65c4
GB
5182 Set_Has_Task (T, Has_Task (Element_Type));
5183 Set_Has_Controlled_Component (T, Has_Controlled_Component
5184 (Element_Type)
5185 or else
5186 Is_Controlled (Element_Type));
5187 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5188 (Element_Type));
996ae0b0
RK
5189 end if;
5190
fea9e956
ES
5191 -- Common attributes for both cases
5192
07fc65c4 5193 Set_Component_Type (Base_Type (T), Element_Type);
fea9e956 5194 Set_Packed_Array_Type (T, Empty);
996ae0b0 5195
a397db96 5196 if Aliased_Present (Component_Definition (Def)) then
2ba431e5 5197 Check_SPARK_Restriction
d8b962d8 5198 ("aliased is not allowed", Component_Definition (Def));
996ae0b0
RK
5199 Set_Has_Aliased_Components (Etype (T));
5200 end if;
5201
0ab80019 5202 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
9dfd2ff8 5203 -- array type to ensure that objects of this type are initialized.
2820d220 5204
0791fbe9 5205 if Ada_Version >= Ada_2005
9dfd2ff8 5206 and then Can_Never_Be_Null (Element_Type)
2820d220
AC
5207 then
5208 Set_Can_Never_Be_Null (T);
5209
5210 if Null_Exclusion_Present (Component_Definition (Def))
9dfd2ff8 5211
33931112
JM
5212 -- No need to check itypes because in their case this check was
5213 -- done at their point of creation
9dfd2ff8
CC
5214
5215 and then not Is_Itype (Element_Type)
2820d220
AC
5216 then
5217 Error_Msg_N
2b73cf68 5218 ("`NOT NULL` not allowed (null already excluded)",
2820d220
AC
5219 Subtype_Indication (Component_Definition (Def)));
5220 end if;
5221 end if;
5222
996ae0b0
RK
5223 Priv := Private_Component (Element_Type);
5224
5225 if Present (Priv) then
07fc65c4
GB
5226
5227 -- Check for circular definitions
996ae0b0
RK
5228
5229 if Priv = Any_Type then
996ae0b0
RK
5230 Set_Component_Type (Etype (T), Any_Type);
5231
fbf5a39b 5232 -- There is a gap in the visibility of operations on the composite
996ae0b0
RK
5233 -- type only if the component type is defined in a different scope.
5234
5235 elsif Scope (Priv) = Current_Scope then
5236 null;
5237
5238 elsif Is_Limited_Type (Priv) then
5239 Set_Is_Limited_Composite (Etype (T));
5240 Set_Is_Limited_Composite (T);
5241 else
5242 Set_Is_Private_Composite (Etype (T));
5243 Set_Is_Private_Composite (T);
5244 end if;
5245 end if;
5246
33931112
JM
5247 -- A syntax error in the declaration itself may lead to an empty index
5248 -- list, in which case do a minimal patch.
2b73cf68
JM
5249
5250 if No (First_Index (T)) then
5251 Error_Msg_N ("missing index definition in array type declaration", T);
5252
5253 declare
3b42c566 5254 Indexes : constant List_Id :=
dc06abec 5255 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
2b73cf68 5256 begin
3b42c566
RD
5257 Set_Discrete_Subtype_Definitions (Def, Indexes);
5258 Set_First_Index (T, First (Indexes));
2b73cf68
JM
5259 return;
5260 end;
5261 end if;
5262
c6fe3827
GD
5263 -- Create a concatenation operator for the new type. Internal array
5264 -- types created for packed entities do not need such, they are
5265 -- compatible with the user-defined type.
996ae0b0
RK
5266
5267 if Number_Dimensions (T) = 1
5268 and then not Is_Packed_Array_Type (T)
5269 then
6c1e24d3 5270 New_Concatenation_Op (T);
996ae0b0
RK
5271 end if;
5272
c6fe3827 5273 -- In the case of an unconstrained array the parser has already verified
3b42c566 5274 -- that all the indexes are unconstrained but we still need to make sure
c6fe3827 5275 -- that the element type is constrained.
996ae0b0
RK
5276
5277 if Is_Indefinite_Subtype (Element_Type) then
5278 Error_Msg_N
a397db96
AC
5279 ("unconstrained element type in array declaration",
5280 Subtype_Indication (Component_Def));
996ae0b0 5281
fea9e956 5282 elsif Is_Abstract_Type (Element_Type) then
a397db96 5283 Error_Msg_N
758c442c 5284 ("the type of a component cannot be abstract",
a397db96 5285 Subtype_Indication (Component_Def));
996ae0b0 5286 end if;
67336960 5287
d85be3ba
AC
5288 -- There may be an invariant declared for the component type, but
5289 -- the construction of the component invariant checking procedure
5290 -- takes place during expansion.
996ae0b0
RK
5291 end Array_Type_Declaration;
5292
7324bf49
AC
5293 ------------------------------------------------------
5294 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5295 ------------------------------------------------------
5296
5297 function Replace_Anonymous_Access_To_Protected_Subprogram
c6fe3827 5298 (N : Node_Id) return Entity_Id
7324bf49
AC
5299 is
5300 Loc : constant Source_Ptr := Sloc (N);
5301
5302 Curr_Scope : constant Scope_Stack_Entry :=
5303 Scope_Stack.Table (Scope_Stack.Last);
5304
092ef350 5305 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
246ff1ae
AC
5306
5307 Acc : Node_Id;
5308 -- Access definition in declaration
5309
7324bf49 5310 Comp : Node_Id;
246ff1ae
AC
5311 -- Object definition or formal definition with an access definition
5312
7324bf49 5313 Decl : Node_Id;
246ff1ae
AC
5314 -- Declaration of anonymous access to subprogram type
5315
5316 Spec : Node_Id;
5317 -- Original specification in access to subprogram
5318
5319 P : Node_Id;
7324bf49
AC
5320
5321 begin
5322 Set_Is_Internal (Anon);
5323
5324 case Nkind (N) is
5325 when N_Component_Declaration |
5326 N_Unconstrained_Array_Definition |
5327 N_Constrained_Array_Definition =>
5328 Comp := Component_Definition (N);
fea9e956 5329 Acc := Access_Definition (Comp);
7324bf49
AC
5330
5331 when N_Discriminant_Specification =>
5332 Comp := Discriminant_Type (N);
fea9e956 5333 Acc := Comp;
7324bf49
AC
5334
5335 when N_Parameter_Specification =>
5336 Comp := Parameter_Type (N);
fea9e956
ES
5337 Acc := Comp;
5338
2b73cf68
JM
5339 when N_Access_Function_Definition =>
5340 Comp := Result_Definition (N);
5341 Acc := Comp;
5342
fea9e956
ES
5343 when N_Object_Declaration =>
5344 Comp := Object_Definition (N);
5345 Acc := Comp;
7324bf49 5346
b1c11e0e
JM
5347 when N_Function_Specification =>
5348 Comp := Result_Definition (N);
5349 Acc := Comp;
5350
7324bf49 5351 when others =>
9bc856dd 5352 raise Program_Error;
7324bf49
AC
5353 end case;
5354
246ff1ae
AC
5355 Spec := Access_To_Subprogram_Definition (Acc);
5356
5357 Decl :=
5358 Make_Full_Type_Declaration (Loc,
5359 Defining_Identifier => Anon,
2c28c7a7 5360 Type_Definition => Copy_Separate_Tree (Spec));
7324bf49
AC
5361
5362 Mark_Rewrite_Insertion (Decl);
5363
2c28c7a7
AC
5364 -- In ASIS mode, analyze the profile on the original node, because
5365 -- the separate copy does not provide enough links to recover the
5366 -- original tree. Analysis is limited to type annotations, within
e9f80612
AC
5367 -- a temporary scope that serves as an anonymous subprogram to collect
5368 -- otherwise useless temporaries and itypes.
2c28c7a7
AC
5369
5370 if ASIS_Mode then
5371 declare
5372 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5373
5374 begin
5375 if Nkind (Spec) = N_Access_Function_Definition then
5376 Set_Ekind (Typ, E_Function);
5377 else
5378 Set_Ekind (Typ, E_Procedure);
5379 end if;
5380
5381 Set_Parent (Typ, N);
5382 Set_Scope (Typ, Current_Scope);
5383 Push_Scope (Typ);
5384
5385 Process_Formals (Parameter_Specifications (Spec), Spec);
5386
5387 if Nkind (Spec) = N_Access_Function_Definition then
14c34330
AC
5388 declare
5389 Def : constant Node_Id := Result_Definition (Spec);
5390
5391 begin
5392 -- The result might itself be an anonymous access type, so
5393 -- have to recurse.
5394
5395 if Nkind (Def) = N_Access_Definition then
5396 if Present (Access_To_Subprogram_Definition (Def)) then
cd38efa5
AC
5397 Set_Etype
5398 (Def,
14c34330
AC
5399 Replace_Anonymous_Access_To_Protected_Subprogram
5400 (Spec));
5401 else
5402 Find_Type (Subtype_Mark (Def));
5403 end if;
cd38efa5 5404
14c34330
AC
5405 else
5406 Find_Type (Def);
5407 end if;
5408 end;
2c28c7a7
AC
5409 end if;
5410
5411 End_Scope;
5412 end;
5413 end if;
5414
cd1c668b
ES
5415 -- Insert the new declaration in the nearest enclosing scope. If the
5416 -- node is a body and N is its return type, the declaration belongs in
5417 -- the enclosing scope.
7324bf49 5418
9dfd2ff8 5419 P := Parent (N);
92298782 5420
cd1c668b
ES
5421 if Nkind (P) = N_Subprogram_Body
5422 and then Nkind (N) = N_Function_Specification
5423 then
5424 P := Parent (P);
5425 end if;
5426
af4b9434 5427 while Present (P) and then not Has_Declarations (P) loop
7324bf49
AC
5428 P := Parent (P);
5429 end loop;
5430
af4b9434
AC
5431 pragma Assert (Present (P));
5432
5433 if Nkind (P) = N_Package_Specification then
5434 Prepend (Decl, Visible_Declarations (P));
5435 else
5436 Prepend (Decl, Declarations (P));
5437 end if;
7324bf49
AC
5438
5439 -- Replace the anonymous type with an occurrence of the new declaration.
9dfd2ff8 5440 -- In all cases the rewritten node does not have the null-exclusion
7324bf49
AC
5441 -- attribute because (if present) it was already inherited by the
5442 -- anonymous entity (Anon). Thus, in case of components we do not
5443 -- inherit this attribute.
5444
5445 if Nkind (N) = N_Parameter_Specification then
5446 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5447 Set_Etype (Defining_Identifier (N), Anon);
5448 Set_Null_Exclusion_Present (N, False);
fea9e956
ES
5449
5450 elsif Nkind (N) = N_Object_Declaration then
5451 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5452 Set_Etype (Defining_Identifier (N), Anon);
5453
2b73cf68
JM
5454 elsif Nkind (N) = N_Access_Function_Definition then
5455 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5456
b1c11e0e
JM
5457 elsif Nkind (N) = N_Function_Specification then
5458 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5459 Set_Etype (Defining_Unit_Name (N), Anon);
5460
7324bf49
AC
5461 else
5462 Rewrite (Comp,
5463 Make_Component_Definition (Loc,
5464 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5465 end if;
5466
5467 Mark_Rewrite_Insertion (Comp);
5468
7d7af38a 5469 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
fea9e956 5470 Analyze (Decl);
2b73cf68 5471
fea9e956 5472 else
cd1c668b
ES
5473 -- Temporarily remove the current scope (record or subprogram) from
5474 -- the stack to add the new declarations to the enclosing scope.
5475
2b73cf68 5476 Scope_Stack.Decrement_Last;
fea9e956 5477 Analyze (Decl);
2b73cf68
JM
5478 Set_Is_Itype (Anon);
5479 Scope_Stack.Append (Curr_Scope);
fea9e956 5480 end if;
7324bf49 5481
fea9e956 5482 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
7d7af38a 5483 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
7324bf49
AC
5484 return Anon;
5485 end Replace_Anonymous_Access_To_Protected_Subprogram;
5486
996ae0b0
RK
5487 -------------------------------
5488 -- Build_Derived_Access_Type --
5489 -------------------------------
5490
5491 procedure Build_Derived_Access_Type
5492 (N : Node_Id;
5493 Parent_Type : Entity_Id;
5494 Derived_Type : Entity_Id)
5495 is
5496 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5497
5498 Desig_Type : Entity_Id;
5499 Discr : Entity_Id;
5500 Discr_Con_Elist : Elist_Id;
5501 Discr_Con_El : Elmt_Id;
6e937c1c 5502 Subt : Entity_Id;
996ae0b0
RK
5503
5504 begin
c6fe3827
GD
5505 -- Set the designated type so it is available in case this is an access
5506 -- to a self-referential type, e.g. a standard list type with a next
5507 -- pointer. Will be reset after subtype is built.
996ae0b0 5508
a397db96
AC
5509 Set_Directly_Designated_Type
5510 (Derived_Type, Designated_Type (Parent_Type));
996ae0b0
RK
5511
5512 Subt := Process_Subtype (S, N);
5513
5514 if Nkind (S) /= N_Subtype_Indication
5515 and then Subt /= Base_Type (Subt)
5516 then
5517 Set_Ekind (Derived_Type, E_Access_Subtype);
5518 end if;
5519
5520 if Ekind (Derived_Type) = E_Access_Subtype then
5521 declare
5522 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5523 Ibase : constant Entity_Id :=
5524 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5525 Svg_Chars : constant Name_Id := Chars (Ibase);
5526 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5527
5528 begin
5529 Copy_Node (Pbase, Ibase);
5530
07fc65c4
GB
5531 Set_Chars (Ibase, Svg_Chars);
5532 Set_Next_Entity (Ibase, Svg_Next_E);
5533 Set_Sloc (Ibase, Sloc (Derived_Type));
5534 Set_Scope (Ibase, Scope (Derived_Type));
5535 Set_Freeze_Node (Ibase, Empty);
5536 Set_Is_Frozen (Ibase, False);
5537 Set_Comes_From_Source (Ibase, False);
5538 Set_Is_First_Subtype (Ibase, False);
996ae0b0
RK
5539
5540 Set_Etype (Ibase, Pbase);
5541 Set_Etype (Derived_Type, Ibase);
5542 end;
5543 end if;
5544
5545 Set_Directly_Designated_Type
5546 (Derived_Type, Designated_Type (Subt));
5547
5548 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5549 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5550 Set_Size_Info (Derived_Type, Parent_Type);
5551 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5552 Set_Depends_On_Private (Derived_Type,
5553 Has_Private_Component (Derived_Type));
5554 Conditional_Delay (Derived_Type, Subt);
5555
7bd98753 5556 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
fa961f76 5557 -- that it is not redundant.
2820d220 5558
fa961f76
ES
5559 if Null_Exclusion_Present (Type_Definition (N)) then
5560 Set_Can_Never_Be_Null (Derived_Type);
5561
5562 if Can_Never_Be_Null (Parent_Type)
5563 and then False
5564 then
5565 Error_Msg_NE
5566 ("`NOT NULL` not allowed (& already excludes null)",
5567 N, Parent_Type);
5568 end if;
5569
5570 elsif Can_Never_Be_Null (Parent_Type) then
2820d220
AC
5571 Set_Can_Never_Be_Null (Derived_Type);
5572 end if;
5573
c6fe3827
GD
5574 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5575 -- the root type for this information.
996ae0b0
RK
5576
5577 -- Apply range checks to discriminants for derived record case
5578 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
5579
5580 Desig_Type := Designated_Type (Derived_Type);
5581 if Is_Composite_Type (Desig_Type)
5582 and then (not Is_Array_Type (Desig_Type))
5583 and then Has_Discriminants (Desig_Type)
5584 and then Base_Type (Desig_Type) /= Desig_Type
5585 then
5586 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
5587 Discr_Con_El := First_Elmt (Discr_Con_Elist);
5588
5589 Discr := First_Discriminant (Base_Type (Desig_Type));
5590 while Present (Discr_Con_El) loop
5591 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
5592 Next_Elmt (Discr_Con_El);
5593 Next_Discriminant (Discr);
5594 end loop;
5595 end if;
5596 end Build_Derived_Access_Type;
5597
5598 ------------------------------
5599 -- Build_Derived_Array_Type --
5600 ------------------------------
5601
5602 procedure Build_Derived_Array_Type
5603 (N : Node_Id;
5604 Parent_Type : Entity_Id;
5605 Derived_Type : Entity_Id)
5606 is
5607 Loc : constant Source_Ptr := Sloc (N);
5608 Tdef : constant Node_Id := Type_Definition (N);
5609 Indic : constant Node_Id := Subtype_Indication (Tdef);
5610 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5611 Implicit_Base : Entity_Id;
5612 New_Indic : Node_Id;
5613
5614 procedure Make_Implicit_Base;
c6fe3827
GD
5615 -- If the parent subtype is constrained, the derived type is a subtype
5616 -- of an implicit base type derived from the parent base.
996ae0b0
RK
5617
5618 ------------------------
5619 -- Make_Implicit_Base --
5620 ------------------------
5621
5622 procedure Make_Implicit_Base is
5623 begin
5624 Implicit_Base :=
5625 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5626
5627 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5628 Set_Etype (Implicit_Base, Parent_Base);
5629
5630 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
5631 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
5632
5633 Set_Has_Delayed_Freeze (Implicit_Base, True);
5634 end Make_Implicit_Base;
5635
5636 -- Start of processing for Build_Derived_Array_Type
5637
5638 begin
5639 if not Is_Constrained (Parent_Type) then
5640 if Nkind (Indic) /= N_Subtype_Indication then
5641 Set_Ekind (Derived_Type, E_Array_Type);
5642
5643 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5644 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
5645
5646 Set_Has_Delayed_Freeze (Derived_Type, True);
5647
5648 else
5649 Make_Implicit_Base;
5650 Set_Etype (Derived_Type, Implicit_Base);
5651
5652 New_Indic :=
5653 Make_Subtype_Declaration (Loc,
5654 Defining_Identifier => Derived_Type,
5655 Subtype_Indication =>
5656 Make_Subtype_Indication (Loc,
e4494292 5657 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
996ae0b0
RK
5658 Constraint => Constraint (Indic)));
5659
5660 Rewrite (N, New_Indic);
5661 Analyze (N);
5662 end if;
5663
5664 else
5665 if Nkind (Indic) /= N_Subtype_Indication then
5666 Make_Implicit_Base;
5667
5668 Set_Ekind (Derived_Type, Ekind (Parent_Type));
5669 Set_Etype (Derived_Type, Implicit_Base);
5670 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5671
5672 else
5673 Error_Msg_N ("illegal constraint on constrained type", Indic);
5674 end if;
5675 end if;
5676
9dfd2ff8
CC
5677 -- If parent type is not a derived type itself, and is declared in
5678 -- closed scope (e.g. a subprogram), then we must explicitly introduce
5679 -- the new type's concatenation operator since Derive_Subprograms
5680 -- will not inherit the parent's operator. If the parent type is
5681 -- unconstrained, the operator is of the unconstrained base type.
996ae0b0
RK
5682
5683 if Number_Dimensions (Parent_Type) = 1
5684 and then not Is_Limited_Type (Parent_Type)
5685 and then not Is_Derived_Type (Parent_Type)
950d3e7d
ES
5686 and then not Is_Package_Or_Generic_Package
5687 (Scope (Base_Type (Parent_Type)))
996ae0b0 5688 then
81a5b587
AC
5689 if not Is_Constrained (Parent_Type)
5690 and then Is_Constrained (Derived_Type)
5691 then
5692 New_Concatenation_Op (Implicit_Base);
5693 else
5694 New_Concatenation_Op (Derived_Type);
5695 end if;
996ae0b0
RK
5696 end if;
5697 end Build_Derived_Array_Type;
5698
5699 -----------------------------------
5700 -- Build_Derived_Concurrent_Type --
5701 -----------------------------------
5702
5703 procedure Build_Derived_Concurrent_Type
5704 (N : Node_Id;
5705 Parent_Type : Entity_Id;
5706 Derived_Type : Entity_Id)
5707 is
8d12c865
RD
5708 Loc : constant Source_Ptr := Sloc (N);
5709
092ef350 5710 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
f7e71125
AC
5711 Corr_Decl : Node_Id;
5712 Corr_Decl_Needed : Boolean;
8d12c865
RD
5713 -- If the derived type has fewer discriminants than its parent, the
5714 -- corresponding record is also a derived type, in order to account for
5715 -- the bound discriminants. We create a full type declaration for it in
5716 -- this case.
f7e71125 5717
8d12c865
RD
5718 Constraint_Present : constant Boolean :=
5719 Nkind (Subtype_Indication (Type_Definition (N))) =
5720 N_Subtype_Indication;
f7e71125
AC
5721
5722 D_Constraint : Node_Id;
5723 New_Constraint : Elist_Id;
5724 Old_Disc : Entity_Id;
5725 New_Disc : Entity_Id;
5726 New_N : Node_Id;
996ae0b0
RK
5727
5728 begin
fbf5a39b 5729 Set_Stored_Constraint (Derived_Type, No_Elist);
f7e71125
AC
5730 Corr_Decl_Needed := False;
5731 Old_Disc := Empty;
5732
5733 if Present (Discriminant_Specifications (N))
5734 and then Constraint_Present
5735 then
5736 Old_Disc := First_Discriminant (Parent_Type);
5737 New_Disc := First (Discriminant_Specifications (N));
5738 while Present (New_Disc) and then Present (Old_Disc) loop
5739 Next_Discriminant (Old_Disc);
5740 Next (New_Disc);
5741 end loop;
5742 end if;
5743
f915704f 5744 if Present (Old_Disc) and then Expander_Active then
f7e71125
AC
5745
5746 -- The new type has fewer discriminants, so we need to create a new
5747 -- corresponding record, which is derived from the corresponding
8d12c865 5748 -- record of the parent, and has a stored constraint that captures
9fc2854d
AC
5749 -- the values of the discriminant constraints. The corresponding
5750 -- record is needed only if expander is active and code generation is
5751 -- enabled.
8d12c865 5752
f915704f
AC
5753 -- The type declaration for the derived corresponding record has the
5754 -- same discriminant part and constraints as the current declaration.
5755 -- Copy the unanalyzed tree to build declaration.
f7e71125
AC
5756
5757 Corr_Decl_Needed := True;
5758 New_N := Copy_Separate_Tree (N);
5759
5760 Corr_Decl :=
5761 Make_Full_Type_Declaration (Loc,
f915704f 5762 Defining_Identifier => Corr_Record,
f7e71125
AC
5763 Discriminant_Specifications =>
5764 Discriminant_Specifications (New_N),
f915704f 5765 Type_Definition =>
f7e71125
AC
5766 Make_Derived_Type_Definition (Loc,
5767 Subtype_Indication =>
5768 Make_Subtype_Indication (Loc,
5769 Subtype_Mark =>
5770 New_Occurrence_Of
5771 (Corresponding_Record_Type (Parent_Type), Loc),
f915704f 5772 Constraint =>
f7e71125
AC
5773 Constraint
5774 (Subtype_Indication (Type_Definition (New_N))))));
5775 end if;
996ae0b0 5776
ce4a6e84
RD
5777 -- Copy Storage_Size and Relative_Deadline variables if task case
5778
996ae0b0
RK
5779 if Is_Task_Type (Parent_Type) then
5780 Set_Storage_Size_Variable (Derived_Type,
5781 Storage_Size_Variable (Parent_Type));
ce4a6e84
RD
5782 Set_Relative_Deadline_Variable (Derived_Type,
5783 Relative_Deadline_Variable (Parent_Type));
996ae0b0
RK
5784 end if;
5785
5786 if Present (Discriminant_Specifications (N)) then
2b73cf68 5787 Push_Scope (Derived_Type);
996ae0b0 5788 Check_Or_Process_Discriminants (N, Derived_Type);
f7e71125
AC
5789
5790 if Constraint_Present then
5791 New_Constraint :=
5792 Expand_To_Stored_Constraint
5793 (Parent_Type,
5794 Build_Discriminant_Constraints
5795 (Parent_Type,
5796 Subtype_Indication (Type_Definition (N)), True));
5797 end if;
5798
996ae0b0 5799 End_Scope;
7ae0dcd8
ES
5800
5801 elsif Constraint_Present then
5802
2eef7403
AC
5803 -- Build constrained subtype, copying the constraint, and derive
5804 -- from it to create a derived constrained type.
7ae0dcd8
ES
5805
5806 declare
5807 Loc : constant Source_Ptr := Sloc (N);
fbf5a39b 5808 Anon : constant Entity_Id :=
7ae0dcd8 5809 Make_Defining_Identifier (Loc,
7675ad4f 5810 Chars => New_External_Name (Chars (Derived_Type), 'T'));
7ae0dcd8
ES
5811 Decl : Node_Id;
5812
5813 begin
5814 Decl :=
5815 Make_Subtype_Declaration (Loc,
5816 Defining_Identifier => Anon,
5817 Subtype_Indication =>
2eef7403 5818 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
7ae0dcd8 5819 Insert_Before (N, Decl);
88b32fc3
BD
5820 Analyze (Decl);
5821
7ae0dcd8
ES
5822 Rewrite (Subtype_Indication (Type_Definition (N)),
5823 New_Occurrence_Of (Anon, Loc));
7ae0dcd8
ES
5824 Set_Analyzed (Derived_Type, False);
5825 Analyze (N);
5826 return;
5827 end;
996ae0b0
RK
5828 end if;
5829
f7e71125
AC
5830 -- By default, operations and private data are inherited from parent.
5831 -- However, in the presence of bound discriminants, a new corresponding
5832 -- record will be created, see below.
996ae0b0
RK
5833
5834 Set_Has_Discriminants
7ae0dcd8 5835 (Derived_Type, Has_Discriminants (Parent_Type));
996ae0b0 5836 Set_Corresponding_Record_Type
7ae0dcd8 5837 (Derived_Type, Corresponding_Record_Type (Parent_Type));
996ae0b0 5838
0501956d
GD
5839 -- Is_Constrained is set according the parent subtype, but is set to
5840 -- False if the derived type is declared with new discriminants.
5841
5842 Set_Is_Constrained
5843 (Derived_Type,
5844 (Is_Constrained (Parent_Type) or else Constraint_Present)
5845 and then not Present (Discriminant_Specifications (N)));
5846
996ae0b0 5847 if Constraint_Present then
996ae0b0
RK
5848 if not Has_Discriminants (Parent_Type) then
5849 Error_Msg_N ("untagged parent must have discriminants", N);
5850
5851 elsif Present (Discriminant_Specifications (N)) then
5852
9dfd2ff8 5853 -- Verify that new discriminants are used to constrain old ones
996ae0b0 5854
996ae0b0 5855 D_Constraint :=
7ae0dcd8
ES
5856 First
5857 (Constraints
5858 (Constraint (Subtype_Indication (Type_Definition (N)))));
996ae0b0 5859
f7e71125 5860 Old_Disc := First_Discriminant (Parent_Type);
7ae0dcd8 5861
f7e71125
AC
5862 while Present (D_Constraint) loop
5863 if Nkind (D_Constraint) /= N_Discriminant_Association then
5864
8d12c865
RD
5865 -- Positional constraint. If it is a reference to a new
5866 -- discriminant, it constrains the corresponding old one.
f7e71125
AC
5867
5868 if Nkind (D_Constraint) = N_Identifier then
5869 New_Disc := First_Discriminant (Derived_Type);
5870 while Present (New_Disc) loop
8d12c865 5871 exit when Chars (New_Disc) = Chars (D_Constraint);
f7e71125
AC
5872 Next_Discriminant (New_Disc);
5873 end loop;
5874
5875 if Present (New_Disc) then
5876 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
5877 end if;
5878 end if;
5879
5880 Next_Discriminant (Old_Disc);
5881
8d12c865
RD
5882 -- if this is a named constraint, search by name for the old
5883 -- discriminants constrained by the new one.
f7e71125
AC
5884
5885 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
5886
8d12c865 5887 -- Find new discriminant with that name
f7e71125
AC
5888
5889 New_Disc := First_Discriminant (Derived_Type);
5890 while Present (New_Disc) loop
5891 exit when
5892 Chars (New_Disc) = Chars (Expression (D_Constraint));
5893 Next_Discriminant (New_Disc);
5894 end loop;
5895
5896 if Present (New_Disc) then
5897
8d12c865
RD
5898 -- Verify that new discriminant renames some discriminant
5899 -- of the parent type, and associate the new discriminant
5900 -- with one or more old ones that it renames.
f7e71125
AC
5901
5902 declare
5903 Selector : Node_Id;
5904
5905 begin
5906 Selector := First (Selector_Names (D_Constraint));
f7e71125
AC
5907 while Present (Selector) loop
5908 Old_Disc := First_Discriminant (Parent_Type);
f7e71125
AC
5909 while Present (Old_Disc) loop
5910 exit when Chars (Old_Disc) = Chars (Selector);
5911 Next_Discriminant (Old_Disc);
5912 end loop;
5913
5914 if Present (Old_Disc) then
5915 Set_Corresponding_Discriminant
5916 (New_Disc, Old_Disc);
f7e71125
AC
5917 end if;
5918
5919 Next (Selector);
5920 end loop;
5921 end;
996ae0b0
RK
5922 end if;
5923 end if;
5924
f7e71125
AC
5925 Next (D_Constraint);
5926 end loop;
5927
8d12c865 5928 New_Disc := First_Discriminant (Derived_Type);
f7e71125
AC
5929 while Present (New_Disc) loop
5930 if No (Corresponding_Discriminant (New_Disc)) then
5931 Error_Msg_NE
8d12c865
RD
5932 ("new discriminant& must constrain old one", N, New_Disc);
5933
f7e71125 5934 elsif not
8d12c865
RD
5935 Subtypes_Statically_Compatible
5936 (Etype (New_Disc),
5937 Etype (Corresponding_Discriminant (New_Disc)))
996ae0b0 5938 then
f7e71125
AC
5939 Error_Msg_NE
5940 ("& not statically compatible with parent discriminant",
5941 N, New_Disc);
996ae0b0
RK
5942 end if;
5943
996ae0b0 5944 Next_Discriminant (New_Disc);
996ae0b0 5945 end loop;
996ae0b0
RK
5946 end if;
5947
5948 elsif Present (Discriminant_Specifications (N)) then
5949 Error_Msg_N
8d12c865 5950 ("missing discriminant constraint in untagged derivation", N);
996ae0b0
RK
5951 end if;
5952
8d12c865
RD
5953 -- The entity chain of the derived type includes the new discriminants
5954 -- but shares operations with the parent.
f7e71125 5955
996ae0b0 5956 if Present (Discriminant_Specifications (N)) then
996ae0b0 5957 Old_Disc := First_Discriminant (Parent_Type);
996ae0b0 5958 while Present (Old_Disc) loop
996ae0b0
RK
5959 if No (Next_Entity (Old_Disc))
5960 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
5961 then
8d12c865
RD
5962 Set_Next_Entity
5963 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
996ae0b0
RK
5964 exit;
5965 end if;
5966
5967 Next_Discriminant (Old_Disc);
5968 end loop;
5969
5970 else
5971 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7ae0dcd8 5972 if Has_Discriminants (Parent_Type) then
7324bf49 5973 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7ae0dcd8
ES
5974 Set_Discriminant_Constraint (
5975 Derived_Type, Discriminant_Constraint (Parent_Type));
5976 end if;
996ae0b0
RK
5977 end if;
5978
5979 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
5980
5981 Set_Has_Completion (Derived_Type);
f7e71125
AC
5982
5983 if Corr_Decl_Needed then
5984 Set_Stored_Constraint (Derived_Type, New_Constraint);
5985 Insert_After (N, Corr_Decl);
5986 Analyze (Corr_Decl);
5987 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
5988 end if;
996ae0b0
RK
5989 end Build_Derived_Concurrent_Type;
5990
5991 ------------------------------------
5992 -- Build_Derived_Enumeration_Type --
5993 ------------------------------------
5994
5995 procedure Build_Derived_Enumeration_Type
5996 (N : Node_Id;
5997 Parent_Type : Entity_Id;
5998 Derived_Type : Entity_Id)
5999 is
6000 Loc : constant Source_Ptr := Sloc (N);
6001 Def : constant Node_Id := Type_Definition (N);
6002 Indic : constant Node_Id := Subtype_Indication (Def);
6003 Implicit_Base : Entity_Id;
6004 Literal : Entity_Id;
6005 New_Lit : Entity_Id;
6006 Literals_List : List_Id;
6007 Type_Decl : Node_Id;
6008 Hi, Lo : Node_Id;
6009 Rang_Expr : Node_Id;
6010
6011 begin
94fd3dc6 6012 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
996ae0b0
RK
6013 -- not have explicit literals lists we need to process types derived
6014 -- from them specially. This is handled by Derived_Standard_Character.
6015 -- If the parent type is a generic type, there are no literals either,
6016 -- and we construct the same skeletal representation as for the generic
6017 -- parent type.
6018
ce4a6e84 6019 if Is_Standard_Character_Type (Parent_Type) then
996ae0b0
RK
6020 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6021
6022 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6023 declare
6024 Lo : Node_Id;
6025 Hi : Node_Id;
6026
6027 begin
054275e4
ES
6028 if Nkind (Indic) /= N_Subtype_Indication then
6029 Lo :=
6030 Make_Attribute_Reference (Loc,
6031 Attribute_Name => Name_First,
e4494292 6032 Prefix => New_Occurrence_Of (Derived_Type, Loc));
054275e4
ES
6033 Set_Etype (Lo, Derived_Type);
6034
6035 Hi :=
6036 Make_Attribute_Reference (Loc,
6037 Attribute_Name => Name_Last,
e4494292 6038 Prefix => New_Occurrence_Of (Derived_Type, Loc));
054275e4
ES
6039 Set_Etype (Hi, Derived_Type);
6040
6041 Set_Scalar_Range (Derived_Type,
6042 Make_Range (Loc,
03b64787 6043 Low_Bound => Lo,
054275e4
ES
6044 High_Bound => Hi));
6045 else
6046
6047 -- Analyze subtype indication and verify compatibility
6048 -- with parent type.
6049
03b64787
AC
6050 if Base_Type (Process_Subtype (Indic, N)) /=
6051 Base_Type (Parent_Type)
054275e4
ES
6052 then
6053 Error_Msg_N
6054 ("illegal constraint for formal discrete type", N);
6055 end if;
6056 end if;
996ae0b0
RK
6057 end;
6058
6059 else
6060 -- If a constraint is present, analyze the bounds to catch
6061 -- premature usage of the derived literals.
6062
6063 if Nkind (Indic) = N_Subtype_Indication
6064 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6065 then
6066 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6067 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6068 end if;
6069
c6fe3827
GD
6070 -- Introduce an implicit base type for the derived type even if there
6071 -- is no constraint attached to it, since this seems closer to the
6072 -- Ada semantics. Build a full type declaration tree for the derived
6073 -- type using the implicit base type as the defining identifier. The
6074 -- build a subtype declaration tree which applies the constraint (if
6075 -- any) have it replace the derived type declaration.
996ae0b0
RK
6076
6077 Literal := First_Literal (Parent_Type);
6078 Literals_List := New_List;
996ae0b0
RK
6079 while Present (Literal)
6080 and then Ekind (Literal) = E_Enumeration_Literal
6081 loop
6082 -- Literals of the derived type have the same representation as
6083 -- those of the parent type, but this representation can be
6084 -- overridden by an explicit representation clause. Indicate
6085 -- that there is no explicit representation given yet. These
6086 -- derived literals are implicit operations of the new type,
9dfd2ff8 6087 -- and can be overridden by explicit ones.
996ae0b0
RK
6088
6089 if Nkind (Literal) = N_Defining_Character_Literal then
6090 New_Lit :=
6091 Make_Defining_Character_Literal (Loc, Chars (Literal));
6092 else
6093 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6094 end if;
6095
6096 Set_Ekind (New_Lit, E_Enumeration_Literal);
6097 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6098 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6099 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6100 Set_Alias (New_Lit, Literal);
6101 Set_Is_Known_Valid (New_Lit, True);
6102
6103 Append (New_Lit, Literals_List);
6104 Next_Literal (Literal);
6105 end loop;
6106
6107 Implicit_Base :=
6108 Make_Defining_Identifier (Sloc (Derived_Type),
7675ad4f 6109 Chars => New_External_Name (Chars (Derived_Type), 'B'));
996ae0b0 6110
c6fe3827
GD
6111 -- Indicate the proper nature of the derived type. This must be done
6112 -- before analysis of the literals, to recognize cases when a literal
6113 -- may be hidden by a previous explicit function definition (cf.
6114 -- c83031a).
996ae0b0
RK
6115
6116 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6117 Set_Etype (Derived_Type, Implicit_Base);
6118
6119 Type_Decl :=
6120 Make_Full_Type_Declaration (Loc,
6121 Defining_Identifier => Implicit_Base,
6122 Discriminant_Specifications => No_List,
6123 Type_Definition =>
6124 Make_Enumeration_Type_Definition (Loc, Literals_List));
6125
6126 Mark_Rewrite_Insertion (Type_Decl);
6127 Insert_Before (N, Type_Decl);
6128 Analyze (Type_Decl);
6129
a5b62485
AC
6130 -- After the implicit base is analyzed its Etype needs to be changed
6131 -- to reflect the fact that it is derived from the parent type which
6132 -- was ignored during analysis. We also set the size at this point.
996ae0b0
RK
6133
6134 Set_Etype (Implicit_Base, Parent_Type);
6135
6136 Set_Size_Info (Implicit_Base, Parent_Type);
6137 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6138 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6139
bd29d519
AC
6140 -- Copy other flags from parent type
6141
996ae0b0
RK
6142 Set_Has_Non_Standard_Rep
6143 (Implicit_Base, Has_Non_Standard_Rep
6144 (Parent_Type));
bd29d519
AC
6145 Set_Has_Pragma_Ordered
6146 (Implicit_Base, Has_Pragma_Ordered
6147 (Parent_Type));
996ae0b0
RK
6148 Set_Has_Delayed_Freeze (Implicit_Base);
6149
c6fe3827
GD
6150 -- Process the subtype indication including a validation check on the
6151 -- constraint, if any. If a constraint is given, its bounds must be
6152 -- implicitly converted to the new type.
996ae0b0
RK
6153
6154 if Nkind (Indic) = N_Subtype_Indication then
996ae0b0 6155 declare
71d9e9f2
ES
6156 R : constant Node_Id :=
6157 Range_Expression (Constraint (Indic));
996ae0b0
RK
6158
6159 begin
6160 if Nkind (R) = N_Range then
6161 Hi := Build_Scalar_Bound
07fc65c4 6162 (High_Bound (R), Parent_Type, Implicit_Base);
996ae0b0 6163 Lo := Build_Scalar_Bound
07fc65c4 6164 (Low_Bound (R), Parent_Type, Implicit_Base);
996ae0b0
RK
6165
6166 else
c6fe3827
GD
6167 -- Constraint is a Range attribute. Replace with explicit
6168 -- mention of the bounds of the prefix, which must be a
6169 -- subtype.
996ae0b0
RK
6170
6171 Analyze (Prefix (R));
6172 Hi :=
6173 Convert_To (Implicit_Base,
6174 Make_Attribute_Reference (Loc,
6175 Attribute_Name => Name_Last,
6176 Prefix =>
6177 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6178
6179 Lo :=
6180 Convert_To (Implicit_Base,
6181 Make_Attribute_Reference (Loc,
6182 Attribute_Name => Name_First,
6183 Prefix =>
6184 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6185 end if;
996ae0b0
RK
6186 end;
6187
6188 else
6189 Hi :=
6190 Build_Scalar_Bound
6191 (Type_High_Bound (Parent_Type),
07fc65c4 6192 Parent_Type, Implicit_Base);
996ae0b0
RK
6193 Lo :=
6194 Build_Scalar_Bound
6195 (Type_Low_Bound (Parent_Type),
07fc65c4 6196 Parent_Type, Implicit_Base);
996ae0b0
RK
6197 end if;
6198
6199 Rang_Expr :=
6200 Make_Range (Loc,
6201 Low_Bound => Lo,
6202 High_Bound => Hi);
6203
6204 -- If we constructed a default range for the case where no range
6205 -- was given, then the expressions in the range must not freeze
6206 -- since they do not correspond to expressions in the source.
6207
6208 if Nkind (Indic) /= N_Subtype_Indication then
6209 Set_Must_Not_Freeze (Lo);
6210 Set_Must_Not_Freeze (Hi);
6211 Set_Must_Not_Freeze (Rang_Expr);
6212 end if;
6213
6214 Rewrite (N,
6215 Make_Subtype_Declaration (Loc,
6216 Defining_Identifier => Derived_Type,
6217 Subtype_Indication =>
6218 Make_Subtype_Indication (Loc,
6219 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6220 Constraint =>
6221 Make_Range_Constraint (Loc,
6222 Range_Expression => Rang_Expr))));
6223
6224 Analyze (N);
6225
a5b62485
AC
6226 -- Apply a range check. Since this range expression doesn't have an
6227 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6228 -- this right???
996ae0b0
RK
6229
6230 if Nkind (Indic) = N_Subtype_Indication then
6231 Apply_Range_Check (Range_Expression (Constraint (Indic)),
6232 Parent_Type,
6233 Source_Typ => Entity (Subtype_Mark (Indic)));
6234 end if;
6235 end if;
996ae0b0
RK
6236 end Build_Derived_Enumeration_Type;
6237
6238 --------------------------------
6239 -- Build_Derived_Numeric_Type --
6240 --------------------------------
6241
6242 procedure Build_Derived_Numeric_Type
6243 (N : Node_Id;
6244 Parent_Type : Entity_Id;
6245 Derived_Type : Entity_Id)
6246 is
6247 Loc : constant Source_Ptr := Sloc (N);
6248 Tdef : constant Node_Id := Type_Definition (N);
6249 Indic : constant Node_Id := Subtype_Indication (Tdef);
6250 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6251 No_Constraint : constant Boolean := Nkind (Indic) /=
6252 N_Subtype_Indication;
71d9e9f2 6253 Implicit_Base : Entity_Id;
996ae0b0
RK
6254
6255 Lo : Node_Id;
6256 Hi : Node_Id;
996ae0b0
RK
6257
6258 begin
6259 -- Process the subtype indication including a validation check on
6260 -- the constraint if any.
6261
fbf5a39b 6262 Discard_Node (Process_Subtype (Indic, N));
996ae0b0 6263
a5b62485
AC
6264 -- Introduce an implicit base type for the derived type even if there
6265 -- is no constraint attached to it, since this seems closer to the Ada
6266 -- semantics.
996ae0b0
RK
6267
6268 Implicit_Base :=
6269 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6270
6271 Set_Etype (Implicit_Base, Parent_Base);
6272 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6273 Set_Size_Info (Implicit_Base, Parent_Base);
996ae0b0
RK
6274 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6275 Set_Parent (Implicit_Base, Parent (Derived_Type));
8dc2ddaf 6276 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
996ae0b0 6277
7d7af38a
JM
6278 -- Set RM Size for discrete type or decimal fixed-point type
6279 -- Ordinary fixed-point is excluded, why???
6280
6281 if Is_Discrete_Type (Parent_Base)
6282 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7bde4677 6283 then
996ae0b0
RK
6284 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6285 end if;
6286
6287 Set_Has_Delayed_Freeze (Implicit_Base);
6288
6289 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6290 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6291
6292 Set_Scalar_Range (Implicit_Base,
6293 Make_Range (Loc,
6294 Low_Bound => Lo,
6295 High_Bound => Hi));
6296
6297 if Has_Infinities (Parent_Base) then
6298 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6299 end if;
6300
a5b62485
AC
6301 -- The Derived_Type, which is the entity of the declaration, is a
6302 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6303 -- absence of an explicit constraint.
996ae0b0
RK
6304
6305 Set_Etype (Derived_Type, Implicit_Base);
6306
6307 -- If we did not have a constraint, then the Ekind is set from the
6308 -- parent type (otherwise Process_Subtype has set the bounds)
6309
6310 if No_Constraint then
6311 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6312 end if;
6313
a5b62485 6314 -- If we did not have a range constraint, then set the range from the
498d1b80 6315 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
996ae0b0
RK
6316
6317 if No_Constraint
6318 or else not Has_Range_Constraint (Indic)
6319 then
6320 Set_Scalar_Range (Derived_Type,
6321 Make_Range (Loc,
6322 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6323 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6324 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6325
6326 if Has_Infinities (Parent_Type) then
6327 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6328 end if;
8dc2ddaf
RD
6329
6330 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
996ae0b0
RK
6331 end if;
6332
9c510803
ES
6333 Set_Is_Descendent_Of_Address (Derived_Type,
6334 Is_Descendent_Of_Address (Parent_Type));
6335 Set_Is_Descendent_Of_Address (Implicit_Base,
6336 Is_Descendent_Of_Address (Parent_Type));
6337
996ae0b0
RK
6338 -- Set remaining type-specific fields, depending on numeric type
6339
6340 if Is_Modular_Integer_Type (Parent_Type) then
6341 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6342
6343 Set_Non_Binary_Modulus
6344 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6345
8dc2ddaf
RD
6346 Set_Is_Known_Valid
6347 (Implicit_Base, Is_Known_Valid (Parent_Base));
6348
996ae0b0
RK
6349 elsif Is_Floating_Point_Type (Parent_Type) then
6350
6351 -- Digits of base type is always copied from the digits value of
6352 -- the parent base type, but the digits of the derived type will
6353 -- already have been set if there was a constraint present.
6354
6355 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
23c799b1 6356 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
996ae0b0
RK
6357
6358 if No_Constraint then
6359 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6360 end if;
6361
6362 elsif Is_Fixed_Point_Type (Parent_Type) then
6363
a5b62485
AC
6364 -- Small of base type and derived type are always copied from the
6365 -- parent base type, since smalls never change. The delta of the
6366 -- base type is also copied from the parent base type. However the
6367 -- delta of the derived type will have been set already if a
6368 -- constraint was present.
996ae0b0
RK
6369
6370 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6371 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6372 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6373
6374 if No_Constraint then
6375 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6376 end if;
6377
6378 -- The scale and machine radix in the decimal case are always
6379 -- copied from the parent base type.
6380
6381 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6382 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6383 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6384
6385 Set_Machine_Radix_10
6386 (Derived_Type, Machine_Radix_10 (Parent_Base));
6387 Set_Machine_Radix_10
6388 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6389
6390 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6391
6392 if No_Constraint then
6393 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6394
6395 else
6396 -- the analysis of the subtype_indication sets the
6397 -- digits value of the derived type.
6398
6399 null;
6400 end if;
6401 end if;
6402 end if;
6403
4c51ff88
AC
6404 if Is_Integer_Type (Parent_Type) then
6405 Set_Has_Shift_Operator
6406 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6407 end if;
6408
996ae0b0
RK
6409 -- The type of the bounds is that of the parent type, and they
6410 -- must be converted to the derived type.
6411
6412 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6413
6414 -- The implicit_base should be frozen when the derived type is frozen,
a5b62485
AC
6415 -- but note that it is used in the conversions of the bounds. For fixed
6416 -- types we delay the determination of the bounds until the proper
996ae0b0
RK
6417 -- freezing point. For other numeric types this is rejected by GCC, for
6418 -- reasons that are currently unclear (???), so we choose to freeze the
6419 -- implicit base now. In the case of integers and floating point types
6420 -- this is harmless because subsequent representation clauses cannot
6421 -- affect anything, but it is still baffling that we cannot use the
6422 -- same mechanism for all derived numeric types.
6423
573e5dd6
RD
6424 -- There is a further complication: actually some representation
6425 -- clauses can affect the implicit base type. For example, attribute
88b32fc3 6426 -- definition clauses for stream-oriented attributes need to set the
573e5dd6
RD
6427 -- corresponding TSS entries on the base type, and this normally
6428 -- cannot be done after the base type is frozen, so the circuitry in
6429 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6430 -- and not use Set_TSS in this case.
6431
6432 -- There are also consequences for the case of delayed representation
6433 -- aspects for some cases. For example, a Size aspect is delayed and
6434 -- should not be evaluated to the freeze point. This early freezing
6435 -- means that the size attribute evaluation happens too early???
88b32fc3 6436
996ae0b0
RK
6437 if Is_Fixed_Point_Type (Parent_Type) then
6438 Conditional_Delay (Implicit_Base, Parent_Type);
6439 else
6440 Freeze_Before (N, Implicit_Base);
6441 end if;
996ae0b0
RK
6442 end Build_Derived_Numeric_Type;
6443
6444 --------------------------------
6445 -- Build_Derived_Private_Type --
6446 --------------------------------
6447
6448 procedure Build_Derived_Private_Type
07fc65c4
GB
6449 (N : Node_Id;
6450 Parent_Type : Entity_Id;
6451 Derived_Type : Entity_Id;
996ae0b0
RK
6452 Is_Completion : Boolean;
6453 Derive_Subps : Boolean := True)
6454 is
39f346aa 6455 Loc : constant Source_Ptr := Sloc (N);
996ae0b0
RK
6456 Der_Base : Entity_Id;
6457 Discr : Entity_Id;
6458 Full_Decl : Node_Id := Empty;
6459 Full_Der : Entity_Id;
6460 Full_P : Entity_Id;
6461 Last_Discr : Entity_Id;
6462 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
6463 Swapped : Boolean := False;
6464
6465 procedure Copy_And_Build;
6466 -- Copy derived type declaration, replace parent with its full view,
6467 -- and analyze new declaration.
6468
07fc65c4
GB
6469 --------------------
6470 -- Copy_And_Build --
6471 --------------------
6472
996ae0b0 6473 procedure Copy_And_Build is
71d9e9f2 6474 Full_N : Node_Id;
996ae0b0
RK
6475
6476 begin
6477 if Ekind (Parent_Type) in Record_Kind
82c80734
RD
6478 or else
6479 (Ekind (Parent_Type) in Enumeration_Kind
ce4a6e84 6480 and then not Is_Standard_Character_Type (Parent_Type)
82c80734 6481 and then not Is_Generic_Type (Root_Type (Parent_Type)))
996ae0b0
RK
6482 then
6483 Full_N := New_Copy_Tree (N);
6484 Insert_After (N, Full_N);
6485 Build_Derived_Type (
6486 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
6487
6488 else
6489 Build_Derived_Type (
6490 N, Parent_Type, Full_Der, True, Derive_Subps => False);
6491 end if;
6492 end Copy_And_Build;
6493
6494 -- Start of processing for Build_Derived_Private_Type
6495
6496 begin
6497 if Is_Tagged_Type (Parent_Type) then
9013065b 6498 Full_P := Full_View (Parent_Type);
39f346aa
ES
6499
6500 -- A type extension of a type with unknown discriminants is an
6501 -- indefinite type that the back-end cannot handle directly.
6502 -- We treat it as a private type, and build a completion that is
6503 -- derived from the full view of the parent, and hopefully has
9013065b
AC
6504 -- known discriminants.
6505
c206e8fd
AC
6506 -- If the full view of the parent type has an underlying record view,
6507 -- use it to generate the underlying record view of this derived type
6508 -- (required for chains of derivations with unknown discriminants).
9013065b 6509
c206e8fd 6510 -- Minor optimization: we avoid the generation of useless underlying
9013065b 6511 -- record view entities if the private type declaration has unknown
c206e8fd
AC
6512 -- discriminants but its corresponding full view has no
6513 -- discriminants.
39f346aa
ES
6514
6515 if Has_Unknown_Discriminants (Parent_Type)
9013065b
AC
6516 and then Present (Full_P)
6517 and then (Has_Discriminants (Full_P)
6518 or else Present (Underlying_Record_View (Full_P)))
39f346aa 6519 and then not In_Open_Scopes (Par_Scope)
39f346aa
ES
6520 and then Expander_Active
6521 then
6522 declare
092ef350 6523 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
c5d91669
AC
6524 New_Ext : constant Node_Id :=
6525 Copy_Separate_Tree
6526 (Record_Extension_Part (Type_Definition (N)));
9013065b 6527 Decl : Node_Id;
39f346aa
ES
6528
6529 begin
6530 Build_Derived_Record_Type
6531 (N, Parent_Type, Derived_Type, Derive_Subps);
6532
6533 -- Build anonymous completion, as a derivation from the full
bf06d37f
AC
6534 -- view of the parent. This is not a completion in the usual
6535 -- sense, because the current type is not private.
39f346aa
ES
6536
6537 Decl :=
6538 Make_Full_Type_Declaration (Loc,
6539 Defining_Identifier => Full_Der,
6540 Type_Definition =>
6541 Make_Derived_Type_Definition (Loc,
6542 Subtype_Indication =>
6543 New_Copy_Tree
6544 (Subtype_Indication (Type_Definition (N))),
6545 Record_Extension_Part => New_Ext));
9013065b 6546
c206e8fd
AC
6547 -- If the parent type has an underlying record view, use it
6548 -- here to build the new underlying record view.
9013065b
AC
6549
6550 if Present (Underlying_Record_View (Full_P)) then
6551 pragma Assert
6552 (Nkind (Subtype_Indication (Type_Definition (Decl)))
6553 = N_Identifier);
6554 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
6555 Underlying_Record_View (Full_P));
6556 end if;
6557
39f346aa
ES
6558 Install_Private_Declarations (Par_Scope);
6559 Install_Visible_Declarations (Par_Scope);
bddd6058 6560 Insert_Before (N, Decl);
9013065b 6561
c206e8fd
AC
6562 -- Mark entity as an underlying record view before analysis,
6563 -- to avoid generating the list of its primitive operations
6564 -- (which is not really required for this entity) and thus
6565 -- prevent spurious errors associated with missing overriding
6566 -- of abstract primitives (overridden only for Derived_Type).
9013065b
AC
6567
6568 Set_Ekind (Full_Der, E_Record_Type);
6569 Set_Is_Underlying_Record_View (Full_Der);
6570
39f346aa 6571 Analyze (Decl);
9013065b
AC
6572
6573 pragma Assert (Has_Discriminants (Full_Der)
6574 and then not Has_Unknown_Discriminants (Full_Der));
6575
39f346aa
ES
6576 Uninstall_Declarations (Par_Scope);
6577
c206e8fd
AC
6578 -- Freeze the underlying record view, to prevent generation of
6579 -- useless dispatching information, which is simply shared with
6580 -- the real derived type.
39f346aa
ES
6581
6582 Set_Is_Frozen (Full_Der);
9013065b 6583
c206e8fd 6584 -- Set up links between real entity and underlying record view
9013065b
AC
6585
6586 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
6587 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
39f346aa
ES
6588 end;
6589
c206e8fd 6590 -- If discriminants are known, build derived record
39f346aa
ES
6591
6592 else
6593 Build_Derived_Record_Type
6594 (N, Parent_Type, Derived_Type, Derive_Subps);
6595 end if;
6596
996ae0b0
RK
6597 return;
6598
6599 elsif Has_Discriminants (Parent_Type) then
996ae0b0
RK
6600 if Present (Full_View (Parent_Type)) then
6601 if not Is_Completion then
6602
a5b62485
AC
6603 -- Copy declaration for subsequent analysis, to provide a
6604 -- completion for what is a private declaration. Indicate that
6605 -- the full type is internally generated.
996ae0b0
RK
6606
6607 Full_Decl := New_Copy_Tree (N);
6608 Full_Der := New_Copy (Derived_Type);
7324bf49 6609 Set_Comes_From_Source (Full_Decl, False);
950d3e7d 6610 Set_Comes_From_Source (Full_Der, False);
f4b049db 6611 Set_Parent (Full_Der, Full_Decl);
fbf5a39b 6612
996ae0b0
RK
6613 Insert_After (N, Full_Decl);
6614
6615 else
c206e8fd
AC
6616 -- If this is a completion, the full view being built is itself
6617 -- private. We build a subtype of the parent with the same
6618 -- constraints as this full view, to convey to the back end the
6619 -- constrained components and the size of this subtype. If the
6620 -- parent is constrained, its full view can serve as the
6621 -- underlying full view of the derived type.
996ae0b0
RK
6622
6623 if No (Discriminant_Specifications (N)) then
71d9e9f2
ES
6624 if Nkind (Subtype_Indication (Type_Definition (N))) =
6625 N_Subtype_Indication
996ae0b0
RK
6626 then
6627 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
6628
6629 elsif Is_Constrained (Full_View (Parent_Type)) then
c5d91669
AC
6630 Set_Underlying_Full_View
6631 (Derived_Type, Full_View (Parent_Type));
996ae0b0
RK
6632 end if;
6633
6634 else
6635 -- If there are new discriminants, the parent subtype is
6636 -- constrained by them, but it is not clear how to build
c206e8fd 6637 -- the Underlying_Full_View in this case???
996ae0b0
RK
6638
6639 null;
6640 end if;
6641 end if;
6642 end if;
6643
ffe9aba8 6644 -- Build partial view of derived type from partial view of parent
fbf5a39b 6645
996ae0b0
RK
6646 Build_Derived_Record_Type
6647 (N, Parent_Type, Derived_Type, Derive_Subps);
6648
c206e8fd 6649 if Present (Full_View (Parent_Type)) and then not Is_Completion then
996ae0b0
RK
6650 if not In_Open_Scopes (Par_Scope)
6651 or else not In_Same_Source_Unit (N, Parent_Type)
6652 then
6653 -- Swap partial and full views temporarily
6654
6655 Install_Private_Declarations (Par_Scope);
6656 Install_Visible_Declarations (Par_Scope);
6657 Swapped := True;
6658 end if;
6659
a5b62485
AC
6660 -- Build full view of derived type from full view of parent which
6661 -- is now installed. Subprograms have been derived on the partial
6662 -- view, the completion does not derive them anew.
996ae0b0 6663
fbf5a39b 6664 if not Is_Tagged_Type (Parent_Type) then
950d3e7d
ES
6665
6666 -- If the parent is itself derived from another private type,
6667 -- installing the private declarations has not affected its
6668 -- privacy status, so use its own full view explicitly.
6669
6670 if Is_Private_Type (Parent_Type) then
6671 Build_Derived_Record_Type
6672 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
6673 else
6674 Build_Derived_Record_Type
6675 (Full_Decl, Parent_Type, Full_Der, False);
6676 end if;
fbf5a39b 6677
71d9e9f2 6678 else
c206e8fd
AC
6679 -- If full view of parent is tagged, the completion inherits
6680 -- the proper primitive operations.
fbf5a39b
AC
6681
6682 Set_Defining_Identifier (Full_Decl, Full_Der);
6683 Build_Derived_Record_Type
6684 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
fbf5a39b 6685 end if;
996ae0b0 6686
f4b049db
AC
6687 -- The full declaration has been introduced into the tree and
6688 -- processed in the step above. It should not be analyzed again
6689 -- (when encountered later in the current list of declarations)
6690 -- to prevent spurious name conflicts. The full entity remains
6691 -- invisible.
6692
6693 Set_Analyzed (Full_Decl);
6694
996ae0b0
RK
6695 if Swapped then
6696 Uninstall_Declarations (Par_Scope);
6697
6698 if In_Open_Scopes (Par_Scope) then
6699 Install_Visible_Declarations (Par_Scope);
6700 end if;
6701 end if;
6702
6703 Der_Base := Base_Type (Derived_Type);
6704 Set_Full_View (Derived_Type, Full_Der);
6705 Set_Full_View (Der_Base, Base_Type (Full_Der));
6706
a5b62485 6707 -- Copy the discriminant list from full view to the partial views
c206e8fd
AC
6708 -- (base type and its subtype). Gigi requires that the partial and
6709 -- full views have the same discriminants.
a5b62485
AC
6710
6711 -- Note that since the partial view is pointing to discriminants
6712 -- in the full view, their scope will be that of the full view.
c206e8fd 6713 -- This might cause some front end problems and need adjustment???
996ae0b0
RK
6714
6715 Discr := First_Discriminant (Base_Type (Full_Der));
6716 Set_First_Entity (Der_Base, Discr);
6717
6718 loop
6719 Last_Discr := Discr;
6720 Next_Discriminant (Discr);
6721 exit when No (Discr);
6722 end loop;
6723
6724 Set_Last_Entity (Der_Base, Last_Discr);
6725
6726 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
6727 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
30c20106 6728 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
996ae0b0
RK
6729
6730 else
c206e8fd
AC
6731 -- If this is a completion, the derived type stays private and
6732 -- there is no need to create a further full view, except in the
6733 -- unusual case when the derivation is nested within a child unit,
6734 -- see below.
996ae0b0
RK
6735
6736 null;
6737 end if;
6738
6739 elsif Present (Full_View (Parent_Type))
6740 and then Has_Discriminants (Full_View (Parent_Type))
6741 then
6742 if Has_Unknown_Discriminants (Parent_Type)
7d7af38a
JM
6743 and then Nkind (Subtype_Indication (Type_Definition (N))) =
6744 N_Subtype_Indication
996ae0b0
RK
6745 then
6746 Error_Msg_N
6747 ("cannot constrain type with unknown discriminants",
6748 Subtype_Indication (Type_Definition (N)));
6749 return;
6750 end if;
6751
c206e8fd
AC
6752 -- If full view of parent is a record type, build full view as a
6753 -- derivation from the parent's full view. Partial view remains
6754 -- private. For code generation and linking, the full view must have
6755 -- the same public status as the partial one. This full view is only
6756 -- needed if the parent type is in an enclosing scope, so that the
6757 -- full view may actually become visible, e.g. in a child unit. This
6758 -- is both more efficient, and avoids order of freezing problems with
6759 -- the added entities.
fbf5a39b
AC
6760
6761 if not Is_Private_Type (Full_View (Parent_Type))
6762 and then (In_Open_Scopes (Scope (Parent_Type)))
6763 then
7675ad4f 6764 Full_Der :=
33bd17e7
ES
6765 Make_Defining_Identifier (Sloc (Derived_Type),
6766 Chars => Chars (Derived_Type));
6767
07fc65c4
GB
6768 Set_Is_Itype (Full_Der);
6769 Set_Has_Private_Declaration (Full_Der);
6770 Set_Has_Private_Declaration (Derived_Type);
6771 Set_Associated_Node_For_Itype (Full_Der, N);
6772 Set_Parent (Full_Der, Parent (Derived_Type));
6773 Set_Full_View (Derived_Type, Full_Der);
fbf5a39b 6774 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
07fc65c4
GB
6775 Full_P := Full_View (Parent_Type);
6776 Exchange_Declarations (Parent_Type);
6777 Copy_And_Build;
6778 Exchange_Declarations (Full_P);
996ae0b0 6779
07fc65c4
GB
6780 else
6781 Build_Derived_Record_Type
6782 (N, Full_View (Parent_Type), Derived_Type,
33bd17e7
ES
6783 Derive_Subps => False);
6784
6785 -- Except in the context of the full view of the parent, there
6786 -- are no non-extension aggregates for the derived type.
6787
6788 Set_Has_Private_Ancestor (Derived_Type);
07fc65c4 6789 end if;
996ae0b0 6790
c206e8fd
AC
6791 -- In any case, the primitive operations are inherited from the
6792 -- parent type, not from the internal full view.
996ae0b0 6793
996ae0b0
RK
6794 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
6795
6796 if Derive_Subps then
6797 Derive_Subprograms (Parent_Type, Derived_Type);
6798 end if;
6799
6800 else
07fc65c4 6801 -- Untagged type, No discriminants on either view
996ae0b0 6802
71d9e9f2
ES
6803 if Nkind (Subtype_Indication (Type_Definition (N))) =
6804 N_Subtype_Indication
996ae0b0
RK
6805 then
6806 Error_Msg_N
6807 ("illegal constraint on type without discriminants", N);
6808 end if;
6809
6810 if Present (Discriminant_Specifications (N))
6811 and then Present (Full_View (Parent_Type))
6812 and then not Is_Tagged_Type (Full_View (Parent_Type))
6813 then
c206e8fd 6814 Error_Msg_N ("cannot add discriminants to untagged type", N);
996ae0b0
RK
6815 end if;
6816
fbf5a39b 6817 Set_Stored_Constraint (Derived_Type, No_Elist);
07fc65c4
GB
6818 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6819 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
6820 Set_Has_Controlled_Component
6821 (Derived_Type, Has_Controlled_Component
6822 (Parent_Type));
996ae0b0 6823
07fc65c4 6824 -- Direct controlled types do not inherit Finalize_Storage_Only flag
996ae0b0
RK
6825
6826 if not Is_Controlled (Parent_Type) then
07fc65c4
GB
6827 Set_Finalize_Storage_Only
6828 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
996ae0b0
RK
6829 end if;
6830
c206e8fd
AC
6831 -- Construct the implicit full view by deriving from full view of the
6832 -- parent type. In order to get proper visibility, we install the
6833 -- parent scope and its declarations.
996ae0b0 6834
c206e8fd
AC
6835 -- ??? If the parent is untagged private and its completion is
6836 -- tagged, this mechanism will not work because we cannot derive from
6837 -- the tagged full view unless we have an extension.
996ae0b0
RK
6838
6839 if Present (Full_View (Parent_Type))
6840 and then not Is_Tagged_Type (Full_View (Parent_Type))
6841 and then not Is_Completion
6842 then
71d9e9f2 6843 Full_Der :=
7675ad4f
AC
6844 Make_Defining_Identifier
6845 (Sloc (Derived_Type), Chars (Derived_Type));
996ae0b0
RK
6846 Set_Is_Itype (Full_Der);
6847 Set_Has_Private_Declaration (Full_Der);
6848 Set_Has_Private_Declaration (Derived_Type);
6849 Set_Associated_Node_For_Itype (Full_Der, N);
6850 Set_Parent (Full_Der, Parent (Derived_Type));
6851 Set_Full_View (Derived_Type, Full_Der);
6852
6853 if not In_Open_Scopes (Par_Scope) then
6854 Install_Private_Declarations (Par_Scope);
6855 Install_Visible_Declarations (Par_Scope);
6856 Copy_And_Build;
6857 Uninstall_Declarations (Par_Scope);
6858
a5b62485
AC
6859 -- If parent scope is open and in another unit, and parent has a
6860 -- completion, then the derivation is taking place in the visible
6861 -- part of a child unit. In that case retrieve the full view of
6862 -- the parent momentarily.
996ae0b0
RK
6863
6864 elsif not In_Same_Source_Unit (N, Parent_Type) then
6865 Full_P := Full_View (Parent_Type);
6866 Exchange_Declarations (Parent_Type);
6867 Copy_And_Build;
6868 Exchange_Declarations (Full_P);
6869
ffe9aba8 6870 -- Otherwise it is a local derivation
996ae0b0
RK
6871
6872 else
6873 Copy_And_Build;
6874 end if;
6875
6876 Set_Scope (Full_Der, Current_Scope);
6877 Set_Is_First_Subtype (Full_Der,
6878 Is_First_Subtype (Derived_Type));
6879 Set_Has_Size_Clause (Full_Der, False);
6880 Set_Has_Alignment_Clause (Full_Der, False);
6881 Set_Next_Entity (Full_Der, Empty);
6882 Set_Has_Delayed_Freeze (Full_Der);
6883 Set_Is_Frozen (Full_Der, False);
6884 Set_Freeze_Node (Full_Der, Empty);
6885 Set_Depends_On_Private (Full_Der,
c206e8fd 6886 Has_Private_Component (Full_Der));
f91b40db 6887 Set_Public_Status (Full_Der);
996ae0b0
RK
6888 end if;
6889 end if;
6890
6891 Set_Has_Unknown_Discriminants (Derived_Type,
6892 Has_Unknown_Discriminants (Parent_Type));
6893
6894 if Is_Private_Type (Derived_Type) then
6895 Set_Private_Dependents (Derived_Type, New_Elmt_List);
6896 end if;
6897
6898 if Is_Private_Type (Parent_Type)
6899 and then Base_Type (Parent_Type) = Parent_Type
6900 and then In_Open_Scopes (Scope (Parent_Type))
6901 then
6902 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
6903
ceee0bde
AC
6904 -- Check for unusual case where a type completed by a private
6905 -- derivation occurs within a package nested in a child unit, and
6906 -- the parent is declared in an ancestor.
6907
996ae0b0
RK
6908 if Is_Child_Unit (Scope (Current_Scope))
6909 and then Is_Completion
6910 and then In_Private_Part (Current_Scope)
3a77b68d 6911 and then Scope (Parent_Type) /= Current_Scope
ceee0bde
AC
6912
6913 -- Note that if the parent has a completion in the private part,
6914 -- (which is itself a derivation from some other private type)
6915 -- it is that completion that is visible, there is no full view
6916 -- available, and no special processing is needed.
6917
97948f41 6918 and then Present (Full_View (Parent_Type))
996ae0b0 6919 then
ceee0bde
AC
6920 -- In this case, the full view of the parent type will become
6921 -- visible in the body of the enclosing child, and only then will
6922 -- the current type be possibly non-private. We build an
6923 -- underlying full view that will be installed when the enclosing
6924 -- child body is compiled.
996ae0b0 6925
fea9e956 6926 Full_Der :=
7675ad4f
AC
6927 Make_Defining_Identifier
6928 (Sloc (Derived_Type), Chars (Derived_Type));
fea9e956
ES
6929 Set_Is_Itype (Full_Der);
6930 Build_Itype_Reference (Full_Der, N);
996ae0b0 6931
fea9e956
ES
6932 -- The full view will be used to swap entities on entry/exit to
6933 -- the body, and must appear in the entity list for the package.
6934
6935 Append_Entity (Full_Der, Scope (Derived_Type));
6936 Set_Has_Private_Declaration (Full_Der);
6937 Set_Has_Private_Declaration (Derived_Type);
6938 Set_Associated_Node_For_Itype (Full_Der, N);
6939 Set_Parent (Full_Der, Parent (Derived_Type));
6940 Full_P := Full_View (Parent_Type);
6941 Exchange_Declarations (Parent_Type);
6942 Copy_And_Build;
6943 Exchange_Declarations (Full_P);
6944 Set_Underlying_Full_View (Derived_Type, Full_Der);
996ae0b0
RK
6945 end if;
6946 end if;
6947 end Build_Derived_Private_Type;
6948
6949 -------------------------------
6950 -- Build_Derived_Record_Type --
6951 -------------------------------
6952
71d9e9f2 6953 -- 1. INTRODUCTION
996ae0b0
RK
6954
6955 -- Ideally we would like to use the same model of type derivation for
6956 -- tagged and untagged record types. Unfortunately this is not quite
6957 -- possible because the semantics of representation clauses is different
6958 -- for tagged and untagged records under inheritance. Consider the
6959 -- following:
6960
6961 -- type R (...) is [tagged] record ... end record;
6962 -- type T (...) is new R (...) [with ...];
6963
fea9e956
ES
6964 -- The representation clauses for T can specify a completely different
6965 -- record layout from R's. Hence the same component can be placed in two
fdac1f80
AC
6966 -- very different positions in objects of type T and R. If R and T are
6967 -- tagged types, representation clauses for T can only specify the layout
6968 -- of non inherited components, thus components that are common in R and T
6969 -- have the same position in objects of type R and T.
996ae0b0
RK
6970
6971 -- This has two implications. The first is that the entire tree for R's
a5b62485
AC
6972 -- declaration needs to be copied for T in the untagged case, so that T
6973 -- can be viewed as a record type of its own with its own representation
996ae0b0
RK
6974 -- clauses. The second implication is the way we handle discriminants.
6975 -- Specifically, in the untagged case we need a way to communicate to Gigi
6976 -- what are the real discriminants in the record, while for the semantics
6977 -- we need to consider those introduced by the user to rename the
6978 -- discriminants in the parent type. This is handled by introducing the
fbf5a39b 6979 -- notion of stored discriminants. See below for more.
996ae0b0
RK
6980
6981 -- Fortunately the way regular components are inherited can be handled in
6982 -- the same way in tagged and untagged types.
6983
6984 -- To complicate things a bit more the private view of a private extension
6985 -- cannot be handled in the same way as the full view (for one thing the
6986 -- semantic rules are somewhat different). We will explain what differs
6987 -- below.
6988
71d9e9f2 6989 -- 2. DISCRIMINANTS UNDER INHERITANCE
996ae0b0
RK
6990
6991 -- The semantic rules governing the discriminants of derived types are
6992 -- quite subtle.
6993
6994 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
88b32fc3 6995 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
996ae0b0
RK
6996
6997 -- If parent type has discriminants, then the discriminants that are
6998 -- declared in the derived type are [3.4 (11)]:
6999
7000 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7001 -- there is one;
7002
a5b62485
AC
7003 -- o Otherwise, each discriminant of the parent type (implicitly declared
7004 -- in the same order with the same specifications). In this case, the
7005 -- discriminants are said to be "inherited", or if unknown in the parent
7006 -- are also unknown in the derived type.
996ae0b0
RK
7007
7008 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7009
7010 -- o The parent subtype shall be constrained;
7011
7012 -- o If the parent type is not a tagged type, then each discriminant of
7013 -- the derived type shall be used in the constraint defining a parent
88b32fc3
BD
7014 -- subtype. [Implementation note: This ensures that the new discriminant
7015 -- can share storage with an existing discriminant.]
996ae0b0
RK
7016
7017 -- For the derived type each discriminant of the parent type is either
7018 -- inherited, constrained to equal some new discriminant of the derived
7019 -- type, or constrained to the value of an expression.
7020
7021 -- When inherited or constrained to equal some new discriminant, the
7022 -- parent discriminant and the discriminant of the derived type are said
7023 -- to "correspond".
7024
7025 -- If a discriminant of the parent type is constrained to a specific value
7026 -- in the derived type definition, then the discriminant is said to be
7027 -- "specified" by that derived type definition.
7028
ffe9aba8 7029 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
996ae0b0 7030
fbf5a39b
AC
7031 -- We have spoken about stored discriminants in point 1 (introduction)
7032 -- above. There are two sort of stored discriminants: implicit and
996ae0b0 7033 -- explicit. As long as the derived type inherits the same discriminants as
fbf5a39b 7034 -- the root record type, stored discriminants are the same as regular
996ae0b0
RK
7035 -- discriminants, and are said to be implicit. However, if any discriminant
7036 -- in the root type was renamed in the derived type, then the derived
fbf5a39b 7037 -- type will contain explicit stored discriminants. Explicit stored
996ae0b0 7038 -- discriminants are discriminants in addition to the semantically visible
fbf5a39b 7039 -- discriminants defined for the derived type. Stored discriminants are
996ae0b0
RK
7040 -- used by Gigi to figure out what are the physical discriminants in
7041 -- objects of the derived type (see precise definition in einfo.ads).
7042 -- As an example, consider the following:
7043
7044 -- type R (D1, D2, D3 : Int) is record ... end record;
7045 -- type T1 is new R;
7046 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7047 -- type T3 is new T2;
7048 -- type T4 (Y : Int) is new T3 (Y, 99);
7049
fbf5a39b 7050 -- The following table summarizes the discriminants and stored
996ae0b0
RK
7051 -- discriminants in R and T1 through T4.
7052
fbf5a39b 7053 -- Type Discrim Stored Discrim Comment
30c20106
AC
7054 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7055 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7056 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7057 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7058 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7059
7060 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7061 -- find the corresponding discriminant in the parent type, while
996ae0b0
RK
7062 -- Original_Record_Component (abbreviated ORC below), the actual physical
7063 -- component that is renamed. Finally the field Is_Completely_Hidden
fbf5a39b 7064 -- (abbreviated ICH below) is set for all explicit stored discriminants
996ae0b0
RK
7065 -- (see einfo.ads for more info). For the above example this gives:
7066
7067 -- Discrim CD ORC ICH
7068 -- ^^^^^^^ ^^ ^^^ ^^^
7069 -- D1 in R empty itself no
7070 -- D2 in R empty itself no
7071 -- D3 in R empty itself no
7072
7073 -- D1 in T1 D1 in R itself no
7074 -- D2 in T1 D2 in R itself no
7075 -- D3 in T1 D3 in R itself no
7076
7077 -- X1 in T2 D3 in T1 D3 in T2 no
7078 -- X2 in T2 D1 in T1 D1 in T2 no
7079 -- D1 in T2 empty itself yes
7080 -- D2 in T2 empty itself yes
7081 -- D3 in T2 empty itself yes
7082
7083 -- X1 in T3 X1 in T2 D3 in T3 no
7084 -- X2 in T3 X2 in T2 D1 in T3 no
7085 -- D1 in T3 empty itself yes
7086 -- D2 in T3 empty itself yes
7087 -- D3 in T3 empty itself yes
7088
7089 -- Y in T4 X1 in T3 D3 in T3 no
7090 -- D1 in T3 empty itself yes
7091 -- D2 in T3 empty itself yes
7092 -- D3 in T3 empty itself yes
7093
71d9e9f2 7094 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
996ae0b0 7095
88b32fc3 7096 -- Type derivation for tagged types is fairly straightforward. If no
996ae0b0 7097 -- discriminants are specified by the derived type, these are inherited
fbf5a39b 7098 -- from the parent. No explicit stored discriminants are ever necessary.
996ae0b0
RK
7099 -- The only manipulation that is done to the tree is that of adding a
7100 -- _parent field with parent type and constrained to the same constraint
7101 -- specified for the parent in the derived type definition. For instance:
7102
7103 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7104 -- type T1 is new R with null record;
7105 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7106
71d9e9f2 7107 -- are changed into:
996ae0b0
RK
7108
7109 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7110 -- _parent : R (D1, D2, D3);
7111 -- end record;
7112
7113 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7114 -- _parent : T1 (X2, 88, X1);
7115 -- end record;
7116
7117 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7118 -- ORC and ICH fields are:
7119
7120 -- Discrim CD ORC ICH
7121 -- ^^^^^^^ ^^ ^^^ ^^^
7122 -- D1 in R empty itself no
7123 -- D2 in R empty itself no
7124 -- D3 in R empty itself no
7125
7126 -- D1 in T1 D1 in R D1 in R no
7127 -- D2 in T1 D2 in R D2 in R no
7128 -- D3 in T1 D3 in R D3 in R no
7129
7130 -- X1 in T2 D3 in T1 D3 in R no
7131 -- X2 in T2 D1 in T1 D1 in R no
7132
71d9e9f2 7133 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
996ae0b0
RK
7134 --
7135 -- Regardless of whether we dealing with a tagged or untagged type
7136 -- we will transform all derived type declarations of the form
7137 --
7138 -- type T is new R (...) [with ...];
7139 -- or
7140 -- subtype S is R (...);
7141 -- type T is new S [with ...];
7142 -- into
7143 -- type BT is new R [with ...];
7144 -- subtype T is BT (...);
7145 --
7146 -- That is, the base derived type is constrained only if it has no
7147 -- discriminants. The reason for doing this is that GNAT's semantic model
7148 -- assumes that a base type with discriminants is unconstrained.
7149 --
7150 -- Note that, strictly speaking, the above transformation is not always
fbf5a39b 7151 -- correct. Consider for instance the following excerpt from ACVC b34011a:
996ae0b0
RK
7152 --
7153 -- procedure B34011A is
7154 -- type REC (D : integer := 0) is record
7155 -- I : Integer;
7156 -- end record;
7157
7158 -- package P is
7159 -- type T6 is new Rec;
7160 -- function F return T6;
7161 -- end P;
7162
7163 -- use P;
7164 -- package Q6 is
7165 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7166 -- end Q6;
7167 --
7168 -- The definition of Q6.U is illegal. However transforming Q6.U into
7169
7170 -- type BaseU is new T6;
7171 -- subtype U is BaseU (Q6.F.I)
7172
7173 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7174 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7175 -- the transformation described above.
7176
7177 -- There is another instance where the above transformation is incorrect.
7178 -- Consider:
7179
7180 -- package Pack is
7181 -- type Base (D : Integer) is tagged null record;
7182 -- procedure P (X : Base);
7183
7184 -- type Der is new Base (2) with null record;
7185 -- procedure P (X : Der);
7186 -- end Pack;
7187
7188 -- Then the above transformation turns this into
7189
7190 -- type Der_Base is new Base with null record;
44d6a706 7191 -- -- procedure P (X : Base) is implicitly inherited here
996ae0b0
RK
7192 -- -- as procedure P (X : Der_Base).
7193
7194 -- subtype Der is Der_Base (2);
7195 -- procedure P (X : Der);
7196 -- -- The overriding of P (X : Der_Base) is illegal since we
7197 -- -- have a parameter conformance problem.
7198
7199 -- To get around this problem, after having semantically processed Der_Base
7200 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7201 -- Discriminant_Constraint from Der so that when parameter conformance is
fbf5a39b 7202 -- checked when P is overridden, no semantic errors are flagged.
996ae0b0 7203
ffe9aba8 7204 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
996ae0b0 7205
fbf5a39b 7206 -- Regardless of whether we are dealing with a tagged or untagged type
996ae0b0
RK
7207 -- we will transform all derived type declarations of the form
7208
7209 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7210 -- type T is new R [with ...];
7211 -- into
7212 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7213
7214 -- The reason for such transformation is that it allows us to implement a
7215 -- very clean form of component inheritance as explained below.
7216
7217 -- Note that this transformation is not achieved by direct tree rewriting
7218 -- and manipulation, but rather by redoing the semantic actions that the
7219 -- above transformation will entail. This is done directly in routine
7220 -- Inherit_Components.
7221
71d9e9f2 7222 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
996ae0b0
RK
7223
7224 -- In both tagged and untagged derived types, regular non discriminant
7225 -- components are inherited in the derived type from the parent type. In
7226 -- the absence of discriminants component, inheritance is straightforward
7227 -- as components can simply be copied from the parent.
a5b62485 7228
996ae0b0
RK
7229 -- If the parent has discriminants, inheriting components constrained with
7230 -- these discriminants requires caution. Consider the following example:
7231
7232 -- type R (D1, D2 : Positive) is [tagged] record
7233 -- S : String (D1 .. D2);
7234 -- end record;
7235
7236 -- type T1 is new R [with null record];
7237 -- type T2 (X : positive) is new R (1, X) [with null record];
7238
7239 -- As explained in 6. above, T1 is rewritten as
996ae0b0 7240 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
996ae0b0
RK
7241 -- which makes the treatment for T1 and T2 identical.
7242
7243 -- What we want when inheriting S, is that references to D1 and D2 in R are
f3d57416 7244 -- replaced with references to their correct constraints, i.e. D1 and D2 in
996ae0b0
RK
7245 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7246 -- with either discriminant references in the derived type or expressions.
fbf5a39b 7247 -- This replacement is achieved as follows: before inheriting R's
996ae0b0
RK
7248 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7249 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7250 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7251 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7252 -- by String (1 .. X).
7253
71d9e9f2 7254 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
996ae0b0
RK
7255
7256 -- We explain here the rules governing private type extensions relevant to
7257 -- type derivation. These rules are explained on the following example:
7258
7259 -- type D [(...)] is new A [(...)] with private; <-- partial view
7260 -- type D [(...)] is new P [(...)] with null record; <-- full view
7261
7262 -- Type A is called the ancestor subtype of the private extension.
7263 -- Type P is the parent type of the full view of the private extension. It
7264 -- must be A or a type derived from A.
7265
7266 -- The rules concerning the discriminants of private type extensions are
7267 -- [7.3(10-13)]:
7268
7269 -- o If a private extension inherits known discriminants from the ancestor
7270 -- subtype, then the full view shall also inherit its discriminants from
7271 -- the ancestor subtype and the parent subtype of the full view shall be
7272 -- constrained if and only if the ancestor subtype is constrained.
7273
7274 -- o If a partial view has unknown discriminants, then the full view may
7275 -- define a definite or an indefinite subtype, with or without
7276 -- discriminants.
7277
7278 -- o If a partial view has neither known nor unknown discriminants, then
7279 -- the full view shall define a definite subtype.
7280
7281 -- o If the ancestor subtype of a private extension has constrained
fbf5a39b 7282 -- discriminants, then the parent subtype of the full view shall impose a
996ae0b0
RK
7283 -- statically matching constraint on those discriminants.
7284
7285 -- This means that only the following forms of private extensions are
7286 -- allowed:
7287
7288 -- type D is new A with private; <-- partial view
7289 -- type D is new P with null record; <-- full view
7290
7291 -- If A has no discriminants than P has no discriminants, otherwise P must
7292 -- inherit A's discriminants.
7293
7294 -- type D is new A (...) with private; <-- partial view
7295 -- type D is new P (:::) with null record; <-- full view
7296
7297 -- P must inherit A's discriminants and (...) and (:::) must statically
7298 -- match.
7299
7300 -- subtype A is R (...);
7301 -- type D is new A with private; <-- partial view
7302 -- type D is new P with null record; <-- full view
7303
7304 -- P must have inherited R's discriminants and must be derived from A or
7305 -- any of its subtypes.
7306
7307 -- type D (..) is new A with private; <-- partial view
7308 -- type D (..) is new P [(:::)] with null record; <-- full view
7309
7310 -- No specific constraints on P's discriminants or constraint (:::).
7311 -- Note that A can be unconstrained, but the parent subtype P must either
7312 -- be constrained or (:::) must be present.
7313
7314 -- type D (..) is new A [(...)] with private; <-- partial view
7315 -- type D (..) is new P [(:::)] with null record; <-- full view
7316
7317 -- P's constraints on A's discriminants must statically match those
7318 -- imposed by (...).
7319
71d9e9f2 7320 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
996ae0b0
RK
7321
7322 -- The full view of a private extension is handled exactly as described
a5b62485 7323 -- above. The model chose for the private view of a private extension is
f3d57416 7324 -- the same for what concerns discriminants (i.e. they receive the same
996ae0b0
RK
7325 -- treatment as in the tagged case). However, the private view of the
7326 -- private extension always inherits the components of the parent base,
a5b62485
AC
7327 -- without replacing any discriminant reference. Strictly speaking this is
7328 -- incorrect. However, Gigi never uses this view to generate code so this
7329 -- is a purely semantic issue. In theory, a set of transformations similar
7330 -- to those given in 5. and 6. above could be applied to private views of
7331 -- private extensions to have the same model of component inheritance as
7332 -- for non private extensions. However, this is not done because it would
7333 -- further complicate private type processing. Semantically speaking, this
7334 -- leaves us in an uncomfortable situation. As an example consider:
996ae0b0
RK
7335
7336 -- package Pack is
7337 -- type R (D : integer) is tagged record
7338 -- S : String (1 .. D);
7339 -- end record;
7340 -- procedure P (X : R);
7341 -- type T is new R (1) with private;
7342 -- private
7343 -- type T is new R (1) with null record;
7344 -- end;
7345
7346 -- This is transformed into:
7347
7348 -- package Pack is
7349 -- type R (D : integer) is tagged record
7350 -- S : String (1 .. D);
7351 -- end record;
7352 -- procedure P (X : R);
7353 -- type T is new R (1) with private;
7354 -- private
7355 -- type BaseT is new R with null record;
7356 -- subtype T is BaseT (1);
7357 -- end;
7358
ffe9aba8 7359 -- (strictly speaking the above is incorrect Ada)
996ae0b0
RK
7360
7361 -- From the semantic standpoint the private view of private extension T
7362 -- should be flagged as constrained since one can clearly have
7363 --
7364 -- Obj : T;
7365 --
7366 -- in a unit withing Pack. However, when deriving subprograms for the
7367 -- private view of private extension T, T must be seen as unconstrained
7368 -- since T has discriminants (this is a constraint of the current
7369 -- subprogram derivation model). Thus, when processing the private view of
7370 -- a private extension such as T, we first mark T as unconstrained, we
7371 -- process it, we perform program derivation and just before returning from
7372 -- Build_Derived_Record_Type we mark T as constrained.
a5b62485 7373
fbf5a39b 7374 -- ??? Are there are other uncomfortable cases that we will have to
996ae0b0
RK
7375 -- deal with.
7376
71d9e9f2 7377 -- 10. RECORD_TYPE_WITH_PRIVATE complications
996ae0b0
RK
7378
7379 -- Types that are derived from a visible record type and have a private
7380 -- extension present other peculiarities. They behave mostly like private
7381 -- types, but if they have primitive operations defined, these will not
7382 -- have the proper signatures for further inheritance, because other
7383 -- primitive operations will use the implicit base that we define for
7384 -- private derivations below. This affect subprogram inheritance (see
7385 -- Derive_Subprograms for details). We also derive the implicit base from
7386 -- the base type of the full view, so that the implicit base is a record
7387 -- type and not another private type, This avoids infinite loops.
7388
7389 procedure Build_Derived_Record_Type
7390 (N : Node_Id;
7391 Parent_Type : Entity_Id;
7392 Derived_Type : Entity_Id;
7393 Derive_Subps : Boolean := True)
7394 is
07fc65c4
GB
7395 Discriminant_Specs : constant Boolean :=
7396 Present (Discriminant_Specifications (N));
df3e68b1
HK
7397 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7398 Loc : constant Source_Ptr := Sloc (N);
07fc65c4 7399 Private_Extension : constant Boolean :=
7d7af38a 7400 Nkind (N) = N_Private_Extension_Declaration;
df3e68b1 7401 Assoc_List : Elist_Id;
c6fe3827 7402 Constraint_Present : Boolean;
df3e68b1
HK
7403 Constrs : Elist_Id;
7404 Discrim : Entity_Id;
7405 Indic : Node_Id;
c6fe3827 7406 Inherit_Discrims : Boolean := False;
df3e68b1
HK
7407 Last_Discrim : Entity_Id;
7408 New_Base : Entity_Id;
7409 New_Decl : Node_Id;
7410 New_Discrs : Elist_Id;
7411 New_Indic : Node_Id;
7412 Parent_Base : Entity_Id;
c6fe3827
GD
7413 Save_Etype : Entity_Id;
7414 Save_Discr_Constr : Elist_Id;
7415 Save_Next_Entity : Entity_Id;
df3e68b1
HK
7416 Type_Def : Node_Id;
7417
7418 Discs : Elist_Id := New_Elmt_List;
7419 -- An empty Discs list means that there were no constraints in the
7420 -- subtype indication or that there was an error processing it.
996ae0b0
RK
7421
7422 begin
7423 if Ekind (Parent_Type) = E_Record_Type_With_Private
7424 and then Present (Full_View (Parent_Type))
7425 and then Has_Discriminants (Parent_Type)
7426 then
7427 Parent_Base := Base_Type (Full_View (Parent_Type));
7428 else
7429 Parent_Base := Base_Type (Parent_Type);
7430 end if;
7431
87729e5a
AC
7432 -- AI05-0115 : if this is a derivation from a private type in some
7433 -- other scope that may lead to invisible components for the derived
7434 -- type, mark it accordingly.
7435
7436 if Is_Private_Type (Parent_Type) then
7437 if Scope (Parent_Type) = Scope (Derived_Type) then
7438 null;
7439
7440 elsif In_Open_Scopes (Scope (Parent_Type))
7441 and then In_Private_Part (Scope (Parent_Type))
7442 then
7443 null;
7444
7445 else
7446 Set_Has_Private_Ancestor (Derived_Type);
7447 end if;
7448
7449 else
7450 Set_Has_Private_Ancestor
7451 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7452 end if;
7453
996ae0b0 7454 -- Before we start the previously documented transformations, here is
fea9e956
ES
7455 -- little fix for size and alignment of tagged types. Normally when we
7456 -- derive type D from type P, we copy the size and alignment of P as the
7457 -- default for D, and in the absence of explicit representation clauses
7458 -- for D, the size and alignment are indeed the same as the parent.
7459
7460 -- But this is wrong for tagged types, since fields may be added, and
7461 -- the default size may need to be larger, and the default alignment may
7462 -- need to be larger.
996ae0b0 7463
fea9e956
ES
7464 -- We therefore reset the size and alignment fields in the tagged case.
7465 -- Note that the size and alignment will in any case be at least as
7466 -- large as the parent type (since the derived type has a copy of the
7467 -- parent type in the _parent field)
996ae0b0 7468
fea9e956
ES
7469 -- The type is also marked as being tagged here, which is needed when
7470 -- processing components with a self-referential anonymous access type
7471 -- in the call to Check_Anonymous_Access_Components below. Note that
7472 -- this flag is also set later on for completeness.
996ae0b0
RK
7473
7474 if Is_Tagged then
fea9e956
ES
7475 Set_Is_Tagged_Type (Derived_Type);
7476 Init_Size_Align (Derived_Type);
996ae0b0
RK
7477 end if;
7478
71d9e9f2 7479 -- STEP 0a: figure out what kind of derived type declaration we have
996ae0b0
RK
7480
7481 if Private_Extension then
7482 Type_Def := N;
7483 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7484
7485 else
7486 Type_Def := Type_Definition (N);
7487
c6fe3827 7488 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
996ae0b0
RK
7489 -- Parent_Base can be a private type or private extension. However,
7490 -- for tagged types with an extension the newly added fields are
7491 -- visible and hence the Derived_Type is always an E_Record_Type.
7492 -- (except that the parent may have its own private fields).
7493 -- For untagged types we preserve the Ekind of the Parent_Base.
7494
7495 if Present (Record_Extension_Part (Type_Def)) then
7496 Set_Ekind (Derived_Type, E_Record_Type);
fea9e956
ES
7497
7498 -- Create internal access types for components with anonymous
7499 -- access types.
7500
0791fbe9 7501 if Ada_Version >= Ada_2005 then
fea9e956
ES
7502 Check_Anonymous_Access_Components
7503 (N, Derived_Type, Derived_Type,
7504 Component_List (Record_Extension_Part (Type_Def)));
7505 end if;
7506
996ae0b0
RK
7507 else
7508 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7509 end if;
7510 end if;
7511
7512 -- Indic can either be an N_Identifier if the subtype indication
7513 -- contains no constraint or an N_Subtype_Indication if the subtype
7514 -- indication has a constraint.
7515
7516 Indic := Subtype_Indication (Type_Def);
7517 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
7518
8a6a52dc
AC
7519 -- Check that the type has visible discriminants. The type may be
7520 -- a private type with unknown discriminants whose full view has
7521 -- discriminants which are invisible.
7522
996ae0b0 7523 if Constraint_Present then
8a6a52dc
AC
7524 if not Has_Discriminants (Parent_Base)
7525 or else
7526 (Has_Unknown_Discriminants (Parent_Base)
7527 and then Is_Private_Type (Parent_Base))
7528 then
996ae0b0
RK
7529 Error_Msg_N
7530 ("invalid constraint: type has no discriminant",
7531 Constraint (Indic));
7532
7533 Constraint_Present := False;
7534 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7535
7536 elsif Is_Constrained (Parent_Type) then
7537 Error_Msg_N
7538 ("invalid constraint: parent type is already constrained",
7539 Constraint (Indic));
7540
7541 Constraint_Present := False;
7542 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7543 end if;
7544 end if;
7545
71d9e9f2 7546 -- STEP 0b: If needed, apply transformation given in point 5. above
996ae0b0
RK
7547
7548 if not Private_Extension
7549 and then Has_Discriminants (Parent_Type)
7550 and then not Discriminant_Specs
7551 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7552 then
ffe9aba8 7553 -- First, we must analyze the constraint (see comment in point 5.)
63bb4268
AC
7554 -- The constraint may come from the subtype indication of the full
7555 -- declaration.
996ae0b0
RK
7556
7557 if Constraint_Present then
808876a9 7558 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
996ae0b0 7559
63bb4268
AC
7560 -- If there is no explicit constraint, there might be one that is
7561 -- inherited from a constrained parent type. In that case verify that
7562 -- it conforms to the constraint in the partial view. In perverse
7563 -- cases the parent subtypes of the partial and full view can have
7564 -- different constraints.
996ae0b0 7565
63bb4268
AC
7566 elsif Present (Stored_Constraint (Parent_Type)) then
7567 New_Discrs := Stored_Constraint (Parent_Type);
996ae0b0 7568
63bb4268
AC
7569 else
7570 New_Discrs := No_Elist;
7571 end if;
ea0a7f39 7572
63bb4268
AC
7573 if Has_Discriminants (Derived_Type)
7574 and then Has_Private_Declaration (Derived_Type)
7575 and then Present (Discriminant_Constraint (Derived_Type))
7576 and then Present (New_Discrs)
7577 then
7578 -- Verify that constraints of the full view statically match
7579 -- those given in the partial view.
7580
7581 declare
808876a9 7582 C1, C2 : Elmt_Id;
63bb4268
AC
7583
7584 begin
7585 C1 := First_Elmt (New_Discrs);
7586 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
7587 while Present (C1) and then Present (C2) loop
7588 if Fully_Conformant_Expressions (Node (C1), Node (C2))
7589 or else
7590 (Is_OK_Static_Expression (Node (C1))
808876a9
RD
7591 and then Is_OK_Static_Expression (Node (C2))
7592 and then
7593 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
63bb4268
AC
7594 then
7595 null;
7596
7597 else
7598 if Constraint_Present then
808876a9
RD
7599 Error_Msg_N
7600 ("constraint not conformant to previous declaration",
7601 Node (C1));
63bb4268 7602 else
808876a9
RD
7603 Error_Msg_N
7604 ("constraint of full view is incompatible "
7605 & "with partial view", N);
996ae0b0 7606 end if;
63bb4268 7607 end if;
9dfd2ff8 7608
63bb4268
AC
7609 Next_Elmt (C1);
7610 Next_Elmt (C2);
7611 end loop;
7612 end;
996ae0b0
RK
7613 end if;
7614
7615 -- Insert and analyze the declaration for the unconstrained base type
7616
7617 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7618
7619 New_Decl :=
7620 Make_Full_Type_Declaration (Loc,
7621 Defining_Identifier => New_Base,
7622 Type_Definition =>
7623 Make_Derived_Type_Definition (Loc,
7624 Abstract_Present => Abstract_Present (Type_Def),
fdac1f80 7625 Limited_Present => Limited_Present (Type_Def),
996ae0b0
RK
7626 Subtype_Indication =>
7627 New_Occurrence_Of (Parent_Base, Loc),
7628 Record_Extension_Part =>
fdac1f80
AC
7629 Relocate_Node (Record_Extension_Part (Type_Def)),
7630 Interface_List => Interface_List (Type_Def)));
996ae0b0
RK
7631
7632 Set_Parent (New_Decl, Parent (N));
7633 Mark_Rewrite_Insertion (New_Decl);
7634 Insert_Before (N, New_Decl);
7635
61441c18 7636 -- In the extension case, make sure ancestor is frozen appropriately
47d3b920
AC
7637 -- (see also non-discriminated case below).
7638
61441c18 7639 if Present (Record_Extension_Part (Type_Def))
c42bfef2 7640 or else Is_Interface (Parent_Base)
61441c18 7641 then
47d3b920
AC
7642 Freeze_Before (New_Decl, Parent_Type);
7643 end if;
7644
a5b62485
AC
7645 -- Note that this call passes False for the Derive_Subps parameter
7646 -- because subprogram derivation is deferred until after creating
7647 -- the subtype (see below).
996ae0b0
RK
7648
7649 Build_Derived_Type
7650 (New_Decl, Parent_Base, New_Base,
7651 Is_Completion => True, Derive_Subps => False);
7652
7653 -- ??? This needs re-examination to determine whether the
7654 -- above call can simply be replaced by a call to Analyze.
7655
7656 Set_Analyzed (New_Decl);
7657
7658 -- Insert and analyze the declaration for the constrained subtype
7659
7660 if Constraint_Present then
7661 New_Indic :=
7662 Make_Subtype_Indication (Loc,
7663 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7664 Constraint => Relocate_Node (Constraint (Indic)));
7665
7666 else
7667 declare
fbf5a39b 7668 Constr_List : constant List_Id := New_List;
996ae0b0 7669 C : Elmt_Id;
fbf5a39b 7670 Expr : Node_Id;
996ae0b0
RK
7671
7672 begin
7673 C := First_Elmt (Discriminant_Constraint (Parent_Type));
7674 while Present (C) loop
7675 Expr := Node (C);
7676
7677 -- It is safe here to call New_Copy_Tree since
7678 -- Force_Evaluation was called on each constraint in
7679 -- Build_Discriminant_Constraints.
7680
7681 Append (New_Copy_Tree (Expr), To => Constr_List);
7682
7683 Next_Elmt (C);
7684 end loop;
7685
7686 New_Indic :=
7687 Make_Subtype_Indication (Loc,
7688 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7689 Constraint =>
7690 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
7691 end;
7692 end if;
7693
7694 Rewrite (N,
7695 Make_Subtype_Declaration (Loc,
7696 Defining_Identifier => Derived_Type,
7697 Subtype_Indication => New_Indic));
7698
7699 Analyze (N);
7700
71d9e9f2 7701 -- Derivation of subprograms must be delayed until the full subtype
3e582869 7702 -- has been established, to ensure proper overriding of subprograms
71d9e9f2
ES
7703 -- inherited by full types. If the derivations occurred as part of
7704 -- the call to Build_Derived_Type above, then the check for type
7705 -- conformance would fail because earlier primitive subprograms
7706 -- could still refer to the full type prior the change to the new
7707 -- subtype and hence would not match the new base type created here.
3e582869
AC
7708 -- Subprograms are not derived, however, when Derive_Subps is False
7709 -- (since otherwise there could be redundant derivations).
996ae0b0 7710
3e582869
AC
7711 if Derive_Subps then
7712 Derive_Subprograms (Parent_Type, Derived_Type);
7713 end if;
996ae0b0
RK
7714
7715 -- For tagged types the Discriminant_Constraint of the new base itype
7716 -- is inherited from the first subtype so that no subtype conformance
7717 -- problem arise when the first subtype overrides primitive
7718 -- operations inherited by the implicit base type.
7719
7720 if Is_Tagged then
7721 Set_Discriminant_Constraint
7722 (New_Base, Discriminant_Constraint (Derived_Type));
7723 end if;
7724
7725 return;
7726 end if;
7727
7728 -- If we get here Derived_Type will have no discriminants or it will be
7729 -- a discriminated unconstrained base type.
7730
7731 -- STEP 1a: perform preliminary actions/checks for derived tagged types
7732
7733 if Is_Tagged then
71d9e9f2 7734
996ae0b0 7735 -- The parent type is frozen for non-private extensions (RM 13.14(7))
88b32fc3
BD
7736 -- The declaration of a specific descendant of an interface type
7737 -- freezes the interface type (RM 13.14).
996ae0b0 7738
47d3b920 7739 if not Private_Extension or else Is_Interface (Parent_Base) then
996ae0b0
RK
7740 Freeze_Before (N, Parent_Type);
7741 end if;
7742
758c442c
GD
7743 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
7744 -- cannot be declared at a deeper level than its parent type is
7745 -- removed. The check on derivation within a generic body is also
7746 -- relaxed, but there's a restriction that a derived tagged type
7747 -- cannot be declared in a generic body if it's derived directly
7748 -- or indirectly from a formal type of that generic.
7749
0791fbe9 7750 if Ada_Version >= Ada_2005 then
758c442c
GD
7751 if Present (Enclosing_Generic_Body (Derived_Type)) then
7752 declare
9dfd2ff8 7753 Ancestor_Type : Entity_Id;
758c442c
GD
7754
7755 begin
7756 -- Check to see if any ancestor of the derived type is a
7757 -- formal type.
7758
9dfd2ff8 7759 Ancestor_Type := Parent_Type;
758c442c
GD
7760 while not Is_Generic_Type (Ancestor_Type)
7761 and then Etype (Ancestor_Type) /= Ancestor_Type
7762 loop
7763 Ancestor_Type := Etype (Ancestor_Type);
7764 end loop;
7765
7766 -- If the derived type does have a formal type as an
7767 -- ancestor, then it's an error if the derived type is
7768 -- declared within the body of the generic unit that
7769 -- declares the formal type in its generic formal part. It's
7770 -- sufficient to check whether the ancestor type is declared
7771 -- inside the same generic body as the derived type (such as
7772 -- within a nested generic spec), in which case the
7773 -- derivation is legal. If the formal type is declared
7774 -- outside of that generic body, then it's guaranteed that
7775 -- the derived type is declared within the generic body of
7776 -- the generic unit declaring the formal type.
7777
7778 if Is_Generic_Type (Ancestor_Type)
7779 and then Enclosing_Generic_Body (Ancestor_Type) /=
7780 Enclosing_Generic_Body (Derived_Type)
7781 then
7782 Error_Msg_NE
7783 ("parent type of& must not be descendant of formal type"
7784 & " of an enclosing generic body",
7785 Indic, Derived_Type);
7786 end if;
7787 end;
7788 end if;
7789
7790 elsif Type_Access_Level (Derived_Type) /=
7791 Type_Access_Level (Parent_Type)
996ae0b0
RK
7792 and then not Is_Generic_Type (Derived_Type)
7793 then
7794 if Is_Controlled (Parent_Type) then
7795 Error_Msg_N
7796 ("controlled type must be declared at the library level",
7797 Indic);
7798 else
7799 Error_Msg_N
7800 ("type extension at deeper accessibility level than parent",
7801 Indic);
7802 end if;
7803
7804 else
7805 declare
7806 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
7807
7808 begin
7809 if Present (GB)
7810 and then GB /= Enclosing_Generic_Body (Parent_Base)
7811 then
fbf5a39b
AC
7812 Error_Msg_NE
7813 ("parent type of& must not be outside generic body"
dc06abec 7814 & " (RM 3.9.1(4))",
fbf5a39b 7815 Indic, Derived_Type);
996ae0b0
RK
7816 end if;
7817 end;
7818 end if;
7819 end if;
7820
758c442c
GD
7821 -- Ada 2005 (AI-251)
7822
0791fbe9 7823 if Ada_Version >= Ada_2005 and then Is_Tagged then
946db1e2 7824
758c442c
GD
7825 -- "The declaration of a specific descendant of an interface type
7826 -- freezes the interface type" (RM 13.14).
7827
7828 declare
7829 Iface : Node_Id;
7830 begin
7831 if Is_Non_Empty_List (Interface_List (Type_Def)) then
7832 Iface := First (Interface_List (Type_Def));
758c442c
GD
7833 while Present (Iface) loop
7834 Freeze_Before (N, Etype (Iface));
7835 Next (Iface);
7836 end loop;
7837 end if;
7838 end;
7839 end if;
7840
996ae0b0
RK
7841 -- STEP 1b : preliminary cleanup of the full view of private types
7842
7843 -- If the type is already marked as having discriminants, then it's the
7844 -- completion of a private type or private extension and we need to
7845 -- retain the discriminants from the partial view if the current
7846 -- declaration has Discriminant_Specifications so that we can verify
7847 -- conformance. However, we must remove any existing components that
fbf5a39b 7848 -- were inherited from the parent (and attached in Copy_And_Swap)
996ae0b0 7849 -- because the full type inherits all appropriate components anyway, and
71d9e9f2 7850 -- we do not want the partial view's components interfering.
996ae0b0
RK
7851
7852 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
7853 Discrim := First_Discriminant (Derived_Type);
7854 loop
7855 Last_Discrim := Discrim;
7856 Next_Discriminant (Discrim);
7857 exit when No (Discrim);
7858 end loop;
7859
7860 Set_Last_Entity (Derived_Type, Last_Discrim);
7861
7862 -- In all other cases wipe out the list of inherited components (even
7863 -- inherited discriminants), it will be properly rebuilt here.
7864
7865 else
7866 Set_First_Entity (Derived_Type, Empty);
7867 Set_Last_Entity (Derived_Type, Empty);
7868 end if;
7869
7870 -- STEP 1c: Initialize some flags for the Derived_Type
7871
7872 -- The following flags must be initialized here so that
88b32fc3
BD
7873 -- Process_Discriminants can check that discriminants of tagged types do
7874 -- not have a default initial value and that access discriminants are
7875 -- only specified for limited records. For completeness, these flags are
7876 -- also initialized along with all the other flags below.
996ae0b0 7877
88b32fc3
BD
7878 -- AI-419: Limitedness is not inherited from an interface parent, so to
7879 -- be limited in that case the type must be explicitly declared as
dc06abec 7880 -- limited. However, task and protected interfaces are always limited.
653da906 7881
dc06abec
RD
7882 if Limited_Present (Type_Def) then
7883 Set_Is_Limited_Record (Derived_Type);
7884
ce4a6e84
RD
7885 elsif Is_Limited_Record (Parent_Type)
7886 or else (Present (Full_View (Parent_Type))
7887 and then Is_Limited_Record (Full_View (Parent_Type)))
7888 then
dc06abec
RD
7889 if not Is_Interface (Parent_Type)
7890 or else Is_Synchronized_Interface (Parent_Type)
7891 or else Is_Protected_Interface (Parent_Type)
7892 or else Is_Task_Interface (Parent_Type)
7893 then
7894 Set_Is_Limited_Record (Derived_Type);
7895 end if;
7896 end if;
996ae0b0 7897
71d9e9f2 7898 -- STEP 2a: process discriminants of derived type if any
996ae0b0 7899
2b73cf68 7900 Push_Scope (Derived_Type);
996ae0b0
RK
7901
7902 if Discriminant_Specs then
7903 Set_Has_Unknown_Discriminants (Derived_Type, False);
7904
7905 -- The following call initializes fields Has_Discriminants and
7906 -- Discriminant_Constraint, unless we are processing the completion
7907 -- of a private type declaration.
7908
7909 Check_Or_Process_Discriminants (N, Derived_Type);
7910
dd386db0 7911 -- For untagged types, the constraint on the Parent_Type must be
996ae0b0
RK
7912 -- present and is used to rename the discriminants.
7913
7914 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
7915 Error_Msg_N ("untagged parent must have discriminants", Indic);
7916
7917 elsif not Is_Tagged and then not Constraint_Present then
7918 Error_Msg_N
7919 ("discriminant constraint needed for derived untagged records",
7920 Indic);
7921
7922 -- Otherwise the parent subtype must be constrained unless we have a
7923 -- private extension.
7924
7925 elsif not Constraint_Present
7926 and then not Private_Extension
7927 and then not Is_Constrained (Parent_Type)
7928 then
7929 Error_Msg_N
7930 ("unconstrained type not allowed in this context", Indic);
7931
7932 elsif Constraint_Present then
7933 -- The following call sets the field Corresponding_Discriminant
7934 -- for the discriminants in the Derived_Type.
7935
7936 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
7937
7938 -- For untagged types all new discriminants must rename
7939 -- discriminants in the parent. For private extensions new
7940 -- discriminants cannot rename old ones (implied by [7.3(13)]).
7941
7942 Discrim := First_Discriminant (Derived_Type);
996ae0b0
RK
7943 while Present (Discrim) loop
7944 if not Is_Tagged
57193e09 7945 and then No (Corresponding_Discriminant (Discrim))
996ae0b0
RK
7946 then
7947 Error_Msg_N
7948 ("new discriminants must constrain old ones", Discrim);
7949
7950 elsif Private_Extension
7951 and then Present (Corresponding_Discriminant (Discrim))
7952 then
7953 Error_Msg_N
fbf5a39b 7954 ("only static constraints allowed for parent"
996ae0b0 7955 & " discriminants in the partial view", Indic);
996ae0b0
RK
7956 exit;
7957 end if;
7958
a5b62485
AC
7959 -- If a new discriminant is used in the constraint, then its
7960 -- subtype must be statically compatible with the parent
7961 -- discriminant's subtype (3.7(15)).
996ae0b0 7962
6cb3037c
AC
7963 -- However, if the record contains an array constrained by
7964 -- the discriminant but with some different bound, the compiler
7965 -- attemps to create a smaller range for the discriminant type.
7966 -- (See exp_ch3.Adjust_Discriminants). In this case, where
7967 -- the discriminant type is a scalar type, the check must use
7968 -- the original discriminant type in the parent declaration.
7969
7970 declare
7971 Corr_Disc : constant Entity_Id :=
7972 Corresponding_Discriminant (Discrim);
7973 Disc_Type : constant Entity_Id := Etype (Discrim);
7974 Corr_Type : Entity_Id;
7975
7976 begin
7977 if Present (Corr_Disc) then
7978 if Is_Scalar_Type (Disc_Type) then
7979 Corr_Type :=
7980 Entity (Discriminant_Type (Parent (Corr_Disc)));
7981 else
7982 Corr_Type := Etype (Corr_Disc);
7983 end if;
7984
7985 if not
7986 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
7987 then
7988 Error_Msg_N
7989 ("subtype must be compatible "
7990 & "with parent discriminant",
7991 Discrim);
7992 end if;
7993 end if;
7994 end;
996ae0b0
RK
7995
7996 Next_Discriminant (Discrim);
7997 end loop;
0da2c8ac
AC
7998
7999 -- Check whether the constraints of the full view statically
8000 -- match those imposed by the parent subtype [7.3(13)].
8001
8002 if Present (Stored_Constraint (Derived_Type)) then
8003 declare
8004 C1, C2 : Elmt_Id;
8005
8006 begin
8007 C1 := First_Elmt (Discs);
8008 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8009 while Present (C1) and then Present (C2) loop
8010 if not
8011 Fully_Conformant_Expressions (Node (C1), Node (C2))
8012 then
88b32fc3
BD
8013 Error_Msg_N
8014 ("not conformant with previous declaration",
8015 Node (C1));
0da2c8ac
AC
8016 end if;
8017
8018 Next_Elmt (C1);
8019 Next_Elmt (C2);
8020 end loop;
8021 end;
8022 end if;
996ae0b0
RK
8023 end if;
8024
8025 -- STEP 2b: No new discriminants, inherit discriminants if any
8026
8027 else
8028 if Private_Extension then
8029 Set_Has_Unknown_Discriminants
0da2c8ac
AC
8030 (Derived_Type,
8031 Has_Unknown_Discriminants (Parent_Type)
8032 or else Unknown_Discriminants_Present (N));
8a6a52dc
AC
8033
8034 -- The partial view of the parent may have unknown discriminants,
8035 -- but if the full view has discriminants and the parent type is
8036 -- in scope they must be inherited.
8037
8038 elsif Has_Unknown_Discriminants (Parent_Type)
8039 and then
8040 (not Has_Discriminants (Parent_Type)
8041 or else not In_Open_Scopes (Scope (Parent_Type)))
8042 then
8043 Set_Has_Unknown_Discriminants (Derived_Type);
996ae0b0
RK
8044 end if;
8045
8046 if not Has_Unknown_Discriminants (Derived_Type)
ffe9aba8 8047 and then not Has_Unknown_Discriminants (Parent_Base)
996ae0b0
RK
8048 and then Has_Discriminants (Parent_Type)
8049 then
8050 Inherit_Discrims := True;
8051 Set_Has_Discriminants
8052 (Derived_Type, True);
8053 Set_Discriminant_Constraint
8054 (Derived_Type, Discriminant_Constraint (Parent_Base));
8055 end if;
8056
8057 -- The following test is true for private types (remember
8058 -- transformation 5. is not applied to those) and in an error
8059 -- situation.
8060
8061 if Constraint_Present then
8062 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8063 end if;
8064
fbf5a39b 8065 -- For now mark a new derived type as constrained only if it has no
996ae0b0
RK
8066 -- discriminants. At the end of Build_Derived_Record_Type we properly
8067 -- set this flag in the case of private extensions. See comments in
8068 -- point 9. just before body of Build_Derived_Record_Type.
8069
8070 Set_Is_Constrained
8071 (Derived_Type,
8072 not (Inherit_Discrims
71d9e9f2 8073 or else Has_Unknown_Discriminants (Derived_Type)));
996ae0b0
RK
8074 end if;
8075
ffe9aba8 8076 -- STEP 3: initialize fields of derived type
996ae0b0
RK
8077
8078 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
fbf5a39b 8079 Set_Stored_Constraint (Derived_Type, No_Elist);
996ae0b0 8080
758c442c
GD
8081 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8082 -- but cannot be interfaces
8083
8084 if not Private_Extension
8085 and then Ekind (Derived_Type) /= E_Private_Type
8086 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8087 then
fea9e956
ES
8088 if Interface_Present (Type_Def) then
8089 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8090 end if;
8091
ce2b6ba5 8092 Set_Interfaces (Derived_Type, No_Elist);
758c442c
GD
8093 end if;
8094
996ae0b0
RK
8095 -- Fields inherited from the Parent_Type
8096
996ae0b0 8097 Set_Has_Specified_Layout
b603e37b 8098 (Derived_Type, Has_Specified_Layout (Parent_Type));
996ae0b0 8099 Set_Is_Limited_Composite
b603e37b 8100 (Derived_Type, Is_Limited_Composite (Parent_Type));
996ae0b0 8101 Set_Is_Private_Composite
b603e37b 8102 (Derived_Type, Is_Private_Composite (Parent_Type));
996ae0b0
RK
8103
8104 -- Fields inherited from the Parent_Base
8105
8106 Set_Has_Controlled_Component
8107 (Derived_Type, Has_Controlled_Component (Parent_Base));
8108 Set_Has_Non_Standard_Rep
8109 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8110 Set_Has_Primitive_Operations
8111 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8112
df89ab66 8113 -- Fields inherited from the Parent_Base in the non-private case
c6fe3827
GD
8114
8115 if Ekind (Derived_Type) = E_Record_Type then
8116 Set_Has_Complex_Representation
8117 (Derived_Type, Has_Complex_Representation (Parent_Base));
8118 end if;
8119
df89ab66
ES
8120 -- Fields inherited from the Parent_Base for record types
8121
8122 if Is_Record_Type (Derived_Type) then
b603e37b 8123
d024b126
AC
8124 declare
8125 Parent_Full : Entity_Id;
39ad1665 8126
d024b126
AC
8127 begin
8128 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8129 -- Parent_Base can be a private type or private extension. Go
8130 -- to the full view here to get the E_Record_Type specific flags.
8131
8132 if Present (Full_View (Parent_Base)) then
8133 Parent_Full := Full_View (Parent_Base);
8134 else
8135 Parent_Full := Parent_Base;
8136 end if;
b603e37b 8137
b603e37b 8138 Set_OK_To_Reorder_Components
d024b126 8139 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
d024b126 8140 end;
df89ab66
ES
8141 end if;
8142
ffe9aba8 8143 -- Set fields for private derived types
996ae0b0
RK
8144
8145 if Is_Private_Type (Derived_Type) then
8146 Set_Depends_On_Private (Derived_Type, True);
8147 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8148
8149 -- Inherit fields from non private record types. If this is the
8150 -- completion of a derivation from a private type, the parent itself
8151 -- is private, and the attributes come from its full view, which must
8152 -- be present.
8153
8154 else
8155 if Is_Private_Type (Parent_Base)
8156 and then not Is_Record_Type (Parent_Base)
8157 then
8158 Set_Component_Alignment
8159 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8160 Set_C_Pass_By_Copy
8161 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8162 else
8163 Set_Component_Alignment
8164 (Derived_Type, Component_Alignment (Parent_Base));
996ae0b0
RK
8165 Set_C_Pass_By_Copy
8166 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8167 end if;
8168 end if;
8169
fbf5a39b 8170 -- Set fields for tagged types
996ae0b0
RK
8171
8172 if Is_Tagged then
ef2a63ba 8173 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
996ae0b0
RK
8174
8175 -- All tagged types defined in Ada.Finalization are controlled
8176
8177 if Chars (Scope (Derived_Type)) = Name_Finalization
8178 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8179 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8180 then
8181 Set_Is_Controlled (Derived_Type);
8182 else
8183 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8184 end if;
8185
c206e8fd
AC
8186 -- Minor optimization: there is no need to generate the class-wide
8187 -- entity associated with an underlying record view.
9013065b
AC
8188
8189 if not Is_Underlying_Record_View (Derived_Type) then
8190 Make_Class_Wide_Type (Derived_Type);
8191 end if;
8192
fea9e956 8193 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
996ae0b0
RK
8194
8195 if Has_Discriminants (Derived_Type)
8196 and then Constraint_Present
8197 then
fbf5a39b
AC
8198 Set_Stored_Constraint
8199 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
996ae0b0
RK
8200 end if;
8201
0791fbe9 8202 if Ada_Version >= Ada_2005 then
88b32fc3
BD
8203 declare
8204 Ifaces_List : Elist_Id;
c6fe3827 8205
88b32fc3 8206 begin
c6fe3827
GD
8207 -- Checks rules 3.9.4 (13/2 and 14/2)
8208
8209 if Comes_From_Source (Derived_Type)
8210 and then not Is_Private_Type (Derived_Type)
8211 and then Is_Interface (Parent_Type)
8212 and then not Is_Interface (Derived_Type)
8213 then
8214 if Is_Task_Interface (Parent_Type) then
8215 Error_Msg_N
8216 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8217 Derived_Type);
8218
8219 elsif Is_Protected_Interface (Parent_Type) then
8220 Error_Msg_N
8221 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8222 Derived_Type);
8223 end if;
8224 end if;
8225
fea9e956
ES
8226 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8227
ce2b6ba5 8228 Check_Interfaces (N, Type_Def);
fea9e956
ES
8229
8230 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8231 -- not already in the parents.
8232
ce2b6ba5
JM
8233 Collect_Interfaces
8234 (T => Derived_Type,
8235 Ifaces_List => Ifaces_List,
8236 Exclude_Parents => True);
8237
8238 Set_Interfaces (Derived_Type, Ifaces_List);
7cec010e
ES
8239
8240 -- If the derived type is the anonymous type created for
8241 -- a declaration whose parent has a constraint, propagate
8242 -- the interface list to the source type. This must be done
8243 -- prior to the completion of the analysis of the source type
8244 -- because the components in the extension may contain current
8245 -- instances whose legality depends on some ancestor.
8246
8247 if Is_Itype (Derived_Type) then
8248 declare
8249 Def : constant Node_Id :=
8250 Associated_Node_For_Itype (Derived_Type);
8251 begin
8252 if Present (Def)
8253 and then Nkind (Def) = N_Full_Type_Declaration
8254 then
8255 Set_Interfaces
8256 (Defining_Identifier (Def), Ifaces_List);
8257 end if;
8258 end;
8259 end if;
88b32fc3 8260 end;
758c442c
GD
8261 end if;
8262
996ae0b0
RK
8263 else
8264 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8265 Set_Has_Non_Standard_Rep
8266 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8267 end if;
8268
8269 -- STEP 4: Inherit components from the parent base and constrain them.
8270 -- Apply the second transformation described in point 6. above.
8271
8272 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8273 or else not Has_Discriminants (Parent_Type)
8274 or else not Is_Constrained (Parent_Type)
8275 then
8276 Constrs := Discs;
8277 else
8278 Constrs := Discriminant_Constraint (Parent_Type);
8279 end if;
8280
57193e09
TQ
8281 Assoc_List :=
8282 Inherit_Components
8283 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
996ae0b0
RK
8284
8285 -- STEP 5a: Copy the parent record declaration for untagged types
8286
8287 if not Is_Tagged then
8288
8289 -- Discriminant_Constraint (Derived_Type) has been properly
71d9e9f2
ES
8290 -- constructed. Save it and temporarily set it to Empty because we
8291 -- do not want the call to New_Copy_Tree below to mess this list.
996ae0b0
RK
8292
8293 if Has_Discriminants (Derived_Type) then
8294 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8295 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8296 else
8297 Save_Discr_Constr := No_Elist;
8298 end if;
8299
71d9e9f2
ES
8300 -- Save the Etype field of Derived_Type. It is correctly set now,
8301 -- but the call to New_Copy tree may remap it to point to itself,
8302 -- which is not what we want. Ditto for the Next_Entity field.
996ae0b0
RK
8303
8304 Save_Etype := Etype (Derived_Type);
8305 Save_Next_Entity := Next_Entity (Derived_Type);
8306
fbf5a39b
AC
8307 -- Assoc_List maps all stored discriminants in the Parent_Base to
8308 -- stored discriminants in the Derived_Type. It is fundamental that
8309 -- no types or itypes with discriminants other than the stored
996ae0b0 8310 -- discriminants appear in the entities declared inside
71d9e9f2 8311 -- Derived_Type, since the back end cannot deal with it.
996ae0b0
RK
8312
8313 New_Decl :=
8314 New_Copy_Tree
8315 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8316
8317 -- Restore the fields saved prior to the New_Copy_Tree call
fbf5a39b 8318 -- and compute the stored constraint.
996ae0b0
RK
8319
8320 Set_Etype (Derived_Type, Save_Etype);
8321 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8322
8323 if Has_Discriminants (Derived_Type) then
8324 Set_Discriminant_Constraint
8325 (Derived_Type, Save_Discr_Constr);
fbf5a39b 8326 Set_Stored_Constraint
30c20106 8327 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
07fc65c4 8328 Replace_Components (Derived_Type, New_Decl);
44a10091
AC
8329 Set_Has_Implicit_Dereference
8330 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
996ae0b0
RK
8331 end if;
8332
8333 -- Insert the new derived type declaration
8334
8335 Rewrite (N, New_Decl);
8336
8337 -- STEP 5b: Complete the processing for record extensions in generics
8338
8339 -- There is no completion for record extensions declared in the
8340 -- parameter part of a generic, so we need to complete processing for
fbf5a39b
AC
8341 -- these generic record extensions here. The Record_Type_Definition call
8342 -- will change the Ekind of the components from E_Void to E_Component.
996ae0b0
RK
8343
8344 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8345 Record_Type_Definition (Empty, Derived_Type);
8346
c885d7a1 8347 -- STEP 5c: Process the record extension for non private tagged types
996ae0b0
RK
8348
8349 elsif not Private_Extension then
996ae0b0 8350
c885d7a1
AC
8351 -- Add the _parent field in the derived type
8352
8353 Expand_Record_Extension (Derived_Type, Type_Def);
996ae0b0 8354
758c442c
GD
8355 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8356 -- implemented interfaces if we are in expansion mode
8357
fea9e956 8358 if Expander_Active
ce2b6ba5 8359 and then Has_Interfaces (Derived_Type)
fea9e956 8360 then
758c442c
GD
8361 Add_Interface_Tag_Components (N, Derived_Type);
8362 end if;
8363
996ae0b0
RK
8364 -- Analyze the record extension
8365
8366 Record_Type_Definition
8367 (Record_Extension_Part (Type_Def), Derived_Type);
8368 end if;
8369
8370 End_Scope;
8371
88b32fc3
BD
8372 -- Nothing else to do if there is an error in the derivation.
8373 -- An unusual case: the full view may be derived from a type in an
8374 -- instance, when the partial view was used illegally as an actual
8375 -- in that instance, leading to a circular definition.
8376
8377 if Etype (Derived_Type) = Any_Type
8378 or else Etype (Parent_Type) = Derived_Type
8379 then
996ae0b0
RK
8380 return;
8381 end if;
8382
8383 -- Set delayed freeze and then derive subprograms, we need to do
8384 -- this in this order so that derived subprograms inherit the
8385 -- derived freeze if necessary.
8386
8387 Set_Has_Delayed_Freeze (Derived_Type);
758c442c 8388
996ae0b0 8389 if Derive_Subps then
88b32fc3
BD
8390 Derive_Subprograms (Parent_Type, Derived_Type);
8391 end if;
758c442c 8392
88b32fc3
BD
8393 -- If we have a private extension which defines a constrained derived
8394 -- type mark as constrained here after we have derived subprograms. See
8395 -- comment on point 9. just above the body of Build_Derived_Record_Type.
758c442c 8396
88b32fc3
BD
8397 if Private_Extension and then Inherit_Discrims then
8398 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8399 Set_Is_Constrained (Derived_Type, True);
8400 Set_Discriminant_Constraint (Derived_Type, Discs);
758c442c 8401
88b32fc3
BD
8402 elsif Is_Constrained (Parent_Type) then
8403 Set_Is_Constrained
8404 (Derived_Type, True);
8405 Set_Discriminant_Constraint
8406 (Derived_Type, Discriminant_Constraint (Parent_Type));
8407 end if;
8408 end if;
950d3e7d 8409
c206e8fd
AC
8410 -- Update the class-wide type, which shares the now-completed entity
8411 -- list with its specific type. In case of underlying record views,
9013065b 8412 -- we do not generate the corresponding class wide entity.
950d3e7d 8413
9013065b
AC
8414 if Is_Tagged
8415 and then not Is_Underlying_Record_View (Derived_Type)
8416 then
88b32fc3
BD
8417 Set_First_Entity
8418 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8419 Set_Last_Entity
8420 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8421 end if;
d3820795
JM
8422
8423 Check_Function_Writable_Actuals (N);
88b32fc3 8424 end Build_Derived_Record_Type;
996ae0b0
RK
8425
8426 ------------------------
8427 -- Build_Derived_Type --
8428 ------------------------
8429
8430 procedure Build_Derived_Type
8431 (N : Node_Id;
8432 Parent_Type : Entity_Id;
8433 Derived_Type : Entity_Id;
8434 Is_Completion : Boolean;
8435 Derive_Subps : Boolean := True)
8436 is
8437 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8438
8439 begin
8440 -- Set common attributes
8441
0f853035 8442 Set_Scope (Derived_Type, Current_Scope);
996ae0b0 8443
0f853035
YM
8444 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8445 Set_Etype (Derived_Type, Parent_Base);
8446 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
996ae0b0 8447
93bcda23
AC
8448 Set_Size_Info (Derived_Type, Parent_Type);
8449 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
93bcda23
AC
8450 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8451 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
fbf5a39b 8452
8b3c6430
AC
8453 -- If the parent type is a private subtype, the convention on the base
8454 -- type may be set in the private part, and not propagated to the
8455 -- subtype until later, so we obtain the convention from the base type.
8456
8457 Set_Convention (Derived_Type, Convention (Parent_Base));
8458
e606088a
AC
8459 -- Propagate invariant information. The new type has invariants if
8460 -- they are inherited from the parent type, and these invariants can
8461 -- be further inherited, so both flags are set.
8462
4818e7b9
RD
8463 -- We similarly inherit predicates
8464
8465 if Has_Predicates (Parent_Type) then
8466 Set_Has_Predicates (Derived_Type);
8467 end if;
8468
fbf5a39b
AC
8469 -- The derived type inherits the representation clauses of the parent.
8470 -- However, for a private type that is completed by a derivation, there
8471 -- may be operation attributes that have been specified already (stream
8472 -- attributes and External_Tag) and those must be provided. Finally,
8473 -- if the partial view is a private extension, the representation items
8474 -- of the parent have been inherited already, and should not be chained
8475 -- twice to the derived type.
8476
8477 if Is_Tagged_Type (Parent_Type)
8478 and then Present (First_Rep_Item (Derived_Type))
8479 then
8480 -- The existing items are either operational items or items inherited
8481 -- from a private extension declaration.
8482
8483 declare
dc06abec
RD
8484 Rep : Node_Id;
8485 -- Used to iterate over representation items of the derived type
8486
8487 Last_Rep : Node_Id;
8488 -- Last representation item of the (non-empty) representation
8489 -- item list of the derived type.
8490
fbf5a39b
AC
8491 Found : Boolean := False;
8492
8493 begin
dc06abec
RD
8494 Rep := First_Rep_Item (Derived_Type);
8495 Last_Rep := Rep;
fbf5a39b
AC
8496 while Present (Rep) loop
8497 if Rep = First_Rep_Item (Parent_Type) then
8498 Found := True;
8499 exit;
dc06abec 8500
fbf5a39b
AC
8501 else
8502 Rep := Next_Rep_Item (Rep);
dc06abec
RD
8503
8504 if Present (Rep) then
8505 Last_Rep := Rep;
8506 end if;
fbf5a39b
AC
8507 end if;
8508 end loop;
8509
dc06abec
RD
8510 -- Here if we either encountered the parent type's first rep
8511 -- item on the derived type's rep item list (in which case
8512 -- Found is True, and we have nothing else to do), or if we
8513 -- reached the last rep item of the derived type, which is
8514 -- Last_Rep, in which case we further chain the parent type's
8515 -- rep items to those of the derived type.
8516
fbf5a39b 8517 if not Found then
dc06abec 8518 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
fbf5a39b
AC
8519 end if;
8520 end;
8521
8522 else
8523 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
8524 end if;
996ae0b0 8525
15e934bf
AC
8526 -- If the parent type has delayed rep aspects, then mark the derived
8527 -- type as possibly inheriting a delayed rep aspect.
8528
8529 if Has_Delayed_Rep_Aspects (Parent_Type) then
8530 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
8531 end if;
8532
8533 -- Type dependent processing
8534
996ae0b0
RK
8535 case Ekind (Parent_Type) is
8536 when Numeric_Kind =>
8537 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
8538
8539 when Array_Kind =>
8540 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
8541
8542 when E_Record_Type
8543 | E_Record_Subtype
8544 | Class_Wide_Kind =>
8545 Build_Derived_Record_Type
8546 (N, Parent_Type, Derived_Type, Derive_Subps);
8547 return;
8548
8549 when Enumeration_Kind =>
8550 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
8551
8552 when Access_Kind =>
8553 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
8554
8555 when Incomplete_Or_Private_Kind =>
8556 Build_Derived_Private_Type
8557 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
8558
8559 -- For discriminated types, the derivation includes deriving
8560 -- primitive operations. For others it is done below.
8561
8562 if Is_Tagged_Type (Parent_Type)
8563 or else Has_Discriminants (Parent_Type)
8564 or else (Present (Full_View (Parent_Type))
8565 and then Has_Discriminants (Full_View (Parent_Type)))
8566 then
8567 return;
8568 end if;
8569
8570 when Concurrent_Kind =>
8571 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
8572
8573 when others =>
8574 raise Program_Error;
8575 end case;
8576
15e934bf
AC
8577 -- Nothing more to do if some error occurred
8578
996ae0b0
RK
8579 if Etype (Derived_Type) = Any_Type then
8580 return;
8581 end if;
8582
a5b62485
AC
8583 -- Set delayed freeze and then derive subprograms, we need to do this
8584 -- in this order so that derived subprograms inherit the derived freeze
8585 -- if necessary.
996ae0b0
RK
8586
8587 Set_Has_Delayed_Freeze (Derived_Type);
15e934bf 8588
996ae0b0
RK
8589 if Derive_Subps then
8590 Derive_Subprograms (Parent_Type, Derived_Type);
8591 end if;
8592
8593 Set_Has_Primitive_Operations
8594 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
8595 end Build_Derived_Type;
8596
8597 -----------------------
8598 -- Build_Discriminal --
8599 -----------------------
8600
8601 procedure Build_Discriminal (Discrim : Entity_Id) is
8602 D_Minal : Entity_Id;
8603 CR_Disc : Entity_Id;
8604
8605 begin
71d9e9f2 8606 -- A discriminal has the same name as the discriminant
996ae0b0 8607
7675ad4f 8608 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
996ae0b0
RK
8609
8610 Set_Ekind (D_Minal, E_In_Parameter);
8611 Set_Mechanism (D_Minal, Default_Mechanism);
8612 Set_Etype (D_Minal, Etype (Discrim));
f0d10385 8613 Set_Scope (D_Minal, Current_Scope);
996ae0b0
RK
8614
8615 Set_Discriminal (Discrim, D_Minal);
8616 Set_Discriminal_Link (D_Minal, Discrim);
8617
8618 -- For task types, build at once the discriminants of the corresponding
8619 -- record, which are needed if discriminants are used in entry defaults
8620 -- and in family bounds.
8621
8622 if Is_Concurrent_Type (Current_Scope)
8623 or else Is_Limited_Type (Current_Scope)
8624 then
8625 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8626
950d3e7d
ES
8627 Set_Ekind (CR_Disc, E_In_Parameter);
8628 Set_Mechanism (CR_Disc, Default_Mechanism);
8629 Set_Etype (CR_Disc, Etype (Discrim));
f0d10385 8630 Set_Scope (CR_Disc, Current_Scope);
950d3e7d
ES
8631 Set_Discriminal_Link (CR_Disc, Discrim);
8632 Set_CR_Discriminant (Discrim, CR_Disc);
996ae0b0
RK
8633 end if;
8634 end Build_Discriminal;
8635
8636 ------------------------------------
8637 -- Build_Discriminant_Constraints --
8638 ------------------------------------
8639
8640 function Build_Discriminant_Constraints
8641 (T : Entity_Id;
8642 Def : Node_Id;
b0f26df5 8643 Derived_Def : Boolean := False) return Elist_Id
996ae0b0 8644 is
71d9e9f2
ES
8645 C : constant Node_Id := Constraint (Def);
8646 Nb_Discr : constant Nat := Number_Discriminants (T);
8647
996ae0b0 8648 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
71d9e9f2 8649 -- Saves the expression corresponding to a given discriminant in T
996ae0b0
RK
8650
8651 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
8652 -- Return the Position number within array Discr_Expr of a discriminant
8653 -- D within the discriminant list of the discriminated type T.
8654
9b7424a7
AC
8655 procedure Process_Discriminant_Expression
8656 (Expr : Node_Id;
8657 D : Entity_Id);
8658 -- If this is a discriminant constraint on a partial view, do not
8659 -- generate an overflow check on the discriminant expression. The check
8660 -- will be generated when constraining the full view. Otherwise the
8661 -- backend creates duplicate symbols for the temporaries corresponding
8662 -- to the expressions to be checked, causing spurious assembler errors.
8663
996ae0b0
RK
8664 ------------------
8665 -- Pos_Of_Discr --
8666 ------------------
8667
8668 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
8669 Disc : Entity_Id;
8670
8671 begin
8672 Disc := First_Discriminant (T);
8673 for J in Discr_Expr'Range loop
8674 if Disc = D then
8675 return J;
8676 end if;
8677
8678 Next_Discriminant (Disc);
8679 end loop;
8680
8681 -- Note: Since this function is called on discriminants that are
8682 -- known to belong to the discriminated type, falling through the
8683 -- loop with no match signals an internal compiler error.
8684
8685 raise Program_Error;
8686 end Pos_Of_Discr;
8687
9b7424a7
AC
8688 -------------------------------------
8689 -- Process_Discriminant_Expression --
8690 -------------------------------------
8691
8692 procedure Process_Discriminant_Expression
8693 (Expr : Node_Id;
8694 D : Entity_Id)
8695 is
8696 BDT : constant Entity_Id := Base_Type (Etype (D));
8697
8698 begin
8699 -- If this is a discriminant constraint on a partial view, do
8700 -- not generate an overflow on the discriminant expression. The
8701 -- check will be generated when constraining the full view.
8702
8703 if Is_Private_Type (T)
8704 and then Present (Full_View (T))
8705 then
8706 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9b7424a7
AC
8707 else
8708 Analyze_And_Resolve (Expr, BDT);
8709 end if;
8710 end Process_Discriminant_Expression;
8711
fbf5a39b 8712 -- Declarations local to Build_Discriminant_Constraints
996ae0b0
RK
8713
8714 Discr : Entity_Id;
8715 E : Entity_Id;
fbf5a39b 8716 Elist : constant Elist_Id := New_Elmt_List;
996ae0b0 8717
71d9e9f2
ES
8718 Constr : Node_Id;
8719 Expr : Node_Id;
8720 Id : Node_Id;
8721 Position : Nat;
8722 Found : Boolean;
996ae0b0
RK
8723
8724 Discrim_Present : Boolean := False;
8725
8726 -- Start of processing for Build_Discriminant_Constraints
8727
8728 begin
8729 -- The following loop will process positional associations only.
8730 -- For a positional association, the (single) discriminant is
8731 -- implicitly specified by position, in textual order (RM 3.7.2).
8732
8733 Discr := First_Discriminant (T);
8734 Constr := First (Constraints (C));
996ae0b0
RK
8735 for D in Discr_Expr'Range loop
8736 exit when Nkind (Constr) = N_Discriminant_Association;
8737
8738 if No (Constr) then
8739 Error_Msg_N ("too few discriminants given in constraint", C);
8740 return New_Elmt_List;
8741
8742 elsif Nkind (Constr) = N_Range
8743 or else (Nkind (Constr) = N_Attribute_Reference
8744 and then
8745 Attribute_Name (Constr) = Name_Range)
8746 then
8747 Error_Msg_N
8748 ("a range is not a valid discriminant constraint", Constr);
8749 Discr_Expr (D) := Error;
8750
8751 else
9b7424a7 8752 Process_Discriminant_Expression (Constr, Discr);
996ae0b0
RK
8753 Discr_Expr (D) := Constr;
8754 end if;
8755
8756 Next_Discriminant (Discr);
8757 Next (Constr);
8758 end loop;
8759
8760 if No (Discr) and then Present (Constr) then
8761 Error_Msg_N ("too many discriminants given in constraint", Constr);
8762 return New_Elmt_List;
8763 end if;
8764
8765 -- Named associations can be given in any order, but if both positional
8766 -- and named associations are used in the same discriminant constraint,
8767 -- then positional associations must occur first, at their normal
8768 -- position. Hence once a named association is used, the rest of the
8769 -- discriminant constraint must use only named associations.
8770
8771 while Present (Constr) loop
8772
ffe9aba8 8773 -- Positional association forbidden after a named association
996ae0b0
RK
8774
8775 if Nkind (Constr) /= N_Discriminant_Association then
8776 Error_Msg_N ("positional association follows named one", Constr);
8777 return New_Elmt_List;
8778
8779 -- Otherwise it is a named association
8780
8781 else
8782 -- E records the type of the discriminants in the named
8783 -- association. All the discriminants specified in the same name
8784 -- association must have the same type.
8785
8786 E := Empty;
8787
8788 -- Search the list of discriminants in T to see if the simple name
8789 -- given in the constraint matches any of them.
8790
8791 Id := First (Selector_Names (Constr));
8792 while Present (Id) loop
8793 Found := False;
8794
8795 -- If Original_Discriminant is present, we are processing a
8796 -- generic instantiation and this is an instance node. We need
8797 -- to find the name of the corresponding discriminant in the
8798 -- actual record type T and not the name of the discriminant in
8799 -- the generic formal. Example:
88b32fc3 8800
996ae0b0
RK
8801 -- generic
8802 -- type G (D : int) is private;
8803 -- package P is
8804 -- subtype W is G (D => 1);
8805 -- end package;
8806 -- type Rec (X : int) is record ... end record;
8807 -- package Q is new P (G => Rec);
88b32fc3 8808
996ae0b0
RK
8809 -- At the point of the instantiation, formal type G is Rec
8810 -- and therefore when reanalyzing "subtype W is G (D => 1);"
8811 -- which really looks like "subtype W is Rec (D => 1);" at
8812 -- the point of instantiation, we want to find the discriminant
f3d57416 8813 -- that corresponds to D in Rec, i.e. X.
996ae0b0 8814
c0b11850
AC
8815 if Present (Original_Discriminant (Id))
8816 and then In_Instance
8817 then
996ae0b0
RK
8818 Discr := Find_Corresponding_Discriminant (Id, T);
8819 Found := True;
8820
8821 else
8822 Discr := First_Discriminant (T);
8823 while Present (Discr) loop
8824 if Chars (Discr) = Chars (Id) then
8825 Found := True;
8826 exit;
8827 end if;
8828
8829 Next_Discriminant (Discr);
8830 end loop;
8831
8832 if not Found then
8833 Error_Msg_N ("& does not match any discriminant", Id);
8834 return New_Elmt_List;
8835
e4982b64
AC
8836 -- If the parent type is a generic formal, preserve the
8837 -- name of the discriminant for subsequent instances.
8838 -- see comment at the beginning of this if statement.
996ae0b0 8839
e4982b64 8840 elsif Is_Generic_Type (Root_Type (T)) then
996ae0b0
RK
8841 Set_Original_Discriminant (Id, Discr);
8842 end if;
8843 end if;
8844
8845 Position := Pos_Of_Discr (T, Discr);
8846
8847 if Present (Discr_Expr (Position)) then
8848 Error_Msg_N ("duplicate constraint for discriminant&", Id);
8849
8850 else
8851 -- Each discriminant specified in the same named association
8852 -- must be associated with a separate copy of the
8853 -- corresponding expression.
8854
8855 if Present (Next (Id)) then
8856 Expr := New_Copy_Tree (Expression (Constr));
8857 Set_Parent (Expr, Parent (Expression (Constr)));
8858 else
8859 Expr := Expression (Constr);
8860 end if;
8861
8862 Discr_Expr (Position) := Expr;
9b7424a7 8863 Process_Discriminant_Expression (Expr, Discr);
996ae0b0
RK
8864 end if;
8865
8866 -- A discriminant association with more than one discriminant
8867 -- name is only allowed if the named discriminants are all of
8868 -- the same type (RM 3.7.1(8)).
8869
8870 if E = Empty then
8871 E := Base_Type (Etype (Discr));
8872
8873 elsif Base_Type (Etype (Discr)) /= E then
8874 Error_Msg_N
8875 ("all discriminants in an association " &
8876 "must have the same type", Id);
8877 end if;
8878
8879 Next (Id);
8880 end loop;
8881 end if;
8882
8883 Next (Constr);
8884 end loop;
8885
8886 -- A discriminant constraint must provide exactly one value for each
8887 -- discriminant of the type (RM 3.7.1(8)).
8888
8889 for J in Discr_Expr'Range loop
8890 if No (Discr_Expr (J)) then
8891 Error_Msg_N ("too few discriminants given in constraint", C);
8892 return New_Elmt_List;
8893 end if;
8894 end loop;
8895
ffe9aba8 8896 -- Determine if there are discriminant expressions in the constraint
996ae0b0
RK
8897
8898 for J in Discr_Expr'Range loop
88b32fc3
BD
8899 if Denotes_Discriminant
8900 (Discr_Expr (J), Check_Concurrent => True)
8901 then
996ae0b0
RK
8902 Discrim_Present := True;
8903 end if;
8904 end loop;
8905
8906 -- Build an element list consisting of the expressions given in the
2820d220
AC
8907 -- discriminant constraint and apply the appropriate checks. The list
8908 -- is constructed after resolving any named discriminant associations
8909 -- and therefore the expressions appear in the textual order of the
8910 -- discriminants.
996ae0b0
RK
8911
8912 Discr := First_Discriminant (T);
8913 for J in Discr_Expr'Range loop
8914 if Discr_Expr (J) /= Error then
996ae0b0
RK
8915 Append_Elmt (Discr_Expr (J), Elist);
8916
8917 -- If any of the discriminant constraints is given by a
8918 -- discriminant and we are in a derived type declaration we
8919 -- have a discriminant renaming. Establish link between new
8920 -- and old discriminant.
8921
8922 if Denotes_Discriminant (Discr_Expr (J)) then
8923 if Derived_Def then
8924 Set_Corresponding_Discriminant
8925 (Entity (Discr_Expr (J)), Discr);
8926 end if;
8927
8928 -- Force the evaluation of non-discriminant expressions.
8929 -- If we have found a discriminant in the constraint 3.4(26)
8930 -- and 3.8(18) demand that no range checks are performed are
fbf5a39b
AC
8931 -- after evaluation. If the constraint is for a component
8932 -- definition that has a per-object constraint, expressions are
8933 -- evaluated but not checked either. In all other cases perform
8934 -- a range check.
996ae0b0
RK
8935
8936 else
fbf5a39b
AC
8937 if Discrim_Present then
8938 null;
8939
a397db96 8940 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
fbf5a39b
AC
8941 and then
8942 Has_Per_Object_Constraint
a397db96 8943 (Defining_Identifier (Parent (Parent (Def))))
fbf5a39b
AC
8944 then
8945 null;
8946
2820d220
AC
8947 elsif Is_Access_Type (Etype (Discr)) then
8948 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
8949
fbf5a39b 8950 else
996ae0b0
RK
8951 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
8952 end if;
8953
8954 Force_Evaluation (Discr_Expr (J));
8955 end if;
8956
88b32fc3
BD
8957 -- Check that the designated type of an access discriminant's
8958 -- expression is not a class-wide type unless the discriminant's
8959 -- designated type is also class-wide.
996ae0b0
RK
8960
8961 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
8962 and then not Is_Class_Wide_Type
8963 (Designated_Type (Etype (Discr)))
8964 and then Etype (Discr_Expr (J)) /= Any_Type
8965 and then Is_Class_Wide_Type
8966 (Designated_Type (Etype (Discr_Expr (J))))
8967 then
8968 Wrong_Type (Discr_Expr (J), Etype (Discr));
49d8b802
ES
8969
8970 elsif Is_Access_Type (Etype (Discr))
8971 and then not Is_Access_Constant (Etype (Discr))
8972 and then Is_Access_Type (Etype (Discr_Expr (J)))
8973 and then Is_Access_Constant (Etype (Discr_Expr (J)))
8974 then
8975 Error_Msg_NE
8976 ("constraint for discriminant& must be access to variable",
8977 Def, Discr);
996ae0b0
RK
8978 end if;
8979 end if;
8980
8981 Next_Discriminant (Discr);
8982 end loop;
8983
8984 return Elist;
8985 end Build_Discriminant_Constraints;
8986
8987 ---------------------------------
8988 -- Build_Discriminated_Subtype --
8989 ---------------------------------
8990
8991 procedure Build_Discriminated_Subtype
8992 (T : Entity_Id;
8993 Def_Id : Entity_Id;
8994 Elist : Elist_Id;
8995 Related_Nod : Node_Id;
8996 For_Access : Boolean := False)
8997 is
8998 Has_Discrs : constant Boolean := Has_Discriminants (T);
88b32fc3
BD
8999 Constrained : constant Boolean :=
9000 (Has_Discrs
9001 and then not Is_Empty_Elmt_List (Elist)
9002 and then not Is_Class_Wide_Type (T))
9003 or else Is_Constrained (T);
996ae0b0
RK
9004
9005 begin
9006 if Ekind (T) = E_Record_Type then
9007 if For_Access then
9008 Set_Ekind (Def_Id, E_Private_Subtype);
9009 Set_Is_For_Access_Subtype (Def_Id, True);
9010 else
9011 Set_Ekind (Def_Id, E_Record_Subtype);
9012 end if;
9013
7d7af38a
JM
9014 -- Inherit preelaboration flag from base, for types for which it
9015 -- may have been set: records, private types, protected types.
9016
9017 Set_Known_To_Have_Preelab_Init
9018 (Def_Id, Known_To_Have_Preelab_Init (T));
9019
996ae0b0
RK
9020 elsif Ekind (T) = E_Task_Type then
9021 Set_Ekind (Def_Id, E_Task_Subtype);
9022
9023 elsif Ekind (T) = E_Protected_Type then
9024 Set_Ekind (Def_Id, E_Protected_Subtype);
7d7af38a
JM
9025 Set_Known_To_Have_Preelab_Init
9026 (Def_Id, Known_To_Have_Preelab_Init (T));
996ae0b0
RK
9027
9028 elsif Is_Private_Type (T) then
9029 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
7d7af38a
JM
9030 Set_Known_To_Have_Preelab_Init
9031 (Def_Id, Known_To_Have_Preelab_Init (T));
996ae0b0 9032
70861157 9033 -- Private subtypes may have private dependents
24778dbb
AC
9034
9035 Set_Private_Dependents (Def_Id, New_Elmt_List);
9036
996ae0b0
RK
9037 elsif Is_Class_Wide_Type (T) then
9038 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9039
9040 else
88b32fc3 9041 -- Incomplete type. Attach subtype to list of dependents, to be
35ae2ed8
AC
9042 -- completed with full view of parent type, unless is it the
9043 -- designated subtype of a record component within an init_proc.
9044 -- This last case arises for a component of an access type whose
9045 -- designated type is incomplete (e.g. a Taft Amendment type).
9046 -- The designated subtype is within an inner scope, and needs no
9047 -- elaboration, because only the access type is needed in the
9048 -- initialization procedure.
996ae0b0
RK
9049
9050 Set_Ekind (Def_Id, Ekind (T));
35ae2ed8
AC
9051
9052 if For_Access and then Within_Init_Proc then
9053 null;
9054 else
9055 Append_Elmt (Def_Id, Private_Dependents (T));
9056 end if;
996ae0b0
RK
9057 end if;
9058
9059 Set_Etype (Def_Id, T);
9060 Init_Size_Align (Def_Id);
9061 Set_Has_Discriminants (Def_Id, Has_Discrs);
9062 Set_Is_Constrained (Def_Id, Constrained);
9063
9064 Set_First_Entity (Def_Id, First_Entity (T));
9065 Set_Last_Entity (Def_Id, Last_Entity (T));
44a10091
AC
9066 Set_Has_Implicit_Dereference
9067 (Def_Id, Has_Implicit_Dereference (T));
33931112
JM
9068
9069 -- If the subtype is the completion of a private declaration, there may
9070 -- have been representation clauses for the partial view, and they must
9071 -- be preserved. Build_Derived_Type chains the inherited clauses with
9072 -- the ones appearing on the extension. If this comes from a subtype
9073 -- declaration, all clauses are inherited.
9074
9075 if No (First_Rep_Item (Def_Id)) then
23c4ff9b 9076 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
33931112 9077 end if;
996ae0b0
RK
9078
9079 if Is_Tagged_Type (T) then
df3e68b1 9080 Set_Is_Tagged_Type (Def_Id);
996ae0b0
RK
9081 Make_Class_Wide_Type (Def_Id);
9082 end if;
9083
fbf5a39b 9084 Set_Stored_Constraint (Def_Id, No_Elist);
996ae0b0
RK
9085
9086 if Has_Discrs then
9087 Set_Discriminant_Constraint (Def_Id, Elist);
fbf5a39b 9088 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
996ae0b0
RK
9089 end if;
9090
9091 if Is_Tagged_Type (T) then
030d25f4
JM
9092
9093 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9094 -- concurrent record type (which has the list of primitive
9095 -- operations).
9096
0791fbe9 9097 if Ada_Version >= Ada_2005
030d25f4
JM
9098 and then Is_Concurrent_Type (T)
9099 then
9100 Set_Corresponding_Record_Type (Def_Id,
9101 Corresponding_Record_Type (T));
9102 else
ef2a63ba
JM
9103 Set_Direct_Primitive_Operations (Def_Id,
9104 Direct_Primitive_Operations (T));
030d25f4
JM
9105 end if;
9106
fea9e956 9107 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
996ae0b0
RK
9108 end if;
9109
9110 -- Subtypes introduced by component declarations do not need to be
9111 -- marked as delayed, and do not get freeze nodes, because the semantics
9112 -- verifies that the parents of the subtypes are frozen before the
9113 -- enclosing record is frozen.
9114
9115 if not Is_Type (Scope (Def_Id)) then
9116 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9117
9118 if Is_Private_Type (T)
9119 and then Present (Full_View (T))
9120 then
9121 Conditional_Delay (Def_Id, Full_View (T));
9122 else
9123 Conditional_Delay (Def_Id, T);
9124 end if;
9125 end if;
9126
9127 if Is_Record_Type (T) then
9128 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9129
9130 if Has_Discrs
9131 and then not Is_Empty_Elmt_List (Elist)
9132 and then not For_Access
9133 then
9134 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9135 elsif not For_Access then
9136 Set_Cloned_Subtype (Def_Id, T);
9137 end if;
9138 end if;
996ae0b0
RK
9139 end Build_Discriminated_Subtype;
9140
fea9e956
ES
9141 ---------------------------
9142 -- Build_Itype_Reference --
9143 ---------------------------
9144
9145 procedure Build_Itype_Reference
9146 (Ityp : Entity_Id;
9147 Nod : Node_Id)
9148 is
9149 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9150 begin
4317e442 9151
e917aec2 9152 -- Itype references are only created for use by the back-end
4317e442
AC
9153
9154 if Inside_A_Generic then
9155 return;
9156 else
9157 Set_Itype (IR, Ityp);
9158 Insert_After (Nod, IR);
9159 end if;
fea9e956
ES
9160 end Build_Itype_Reference;
9161
996ae0b0
RK
9162 ------------------------
9163 -- Build_Scalar_Bound --
9164 ------------------------
9165
9166 function Build_Scalar_Bound
9167 (Bound : Node_Id;
9168 Par_T : Entity_Id;
b0f26df5 9169 Der_T : Entity_Id) return Node_Id
996ae0b0
RK
9170 is
9171 New_Bound : Entity_Id;
9172
9173 begin
9174 -- Note: not clear why this is needed, how can the original bound
9175 -- be unanalyzed at this point? and if it is, what business do we
9176 -- have messing around with it? and why is the base type of the
9177 -- parent type the right type for the resolution. It probably is
a90bd866
RD
9178 -- not. It is OK for the new bound we are creating, but not for
9179 -- the old one??? Still if it never happens, no problem.
996ae0b0
RK
9180
9181 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9182
7d7af38a 9183 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
996ae0b0
RK
9184 New_Bound := New_Copy (Bound);
9185 Set_Etype (New_Bound, Der_T);
9186 Set_Analyzed (New_Bound);
9187
9188 elsif Is_Entity_Name (Bound) then
9189 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9190
9191 -- The following is almost certainly wrong. What business do we have
9192 -- relocating a node (Bound) that is presumably still attached to
9193 -- the tree elsewhere???
9194
9195 else
9196 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9197 end if;
9198
9199 Set_Etype (New_Bound, Der_T);
9200 return New_Bound;
9201 end Build_Scalar_Bound;
9202
9203 --------------------------------
9204 -- Build_Underlying_Full_View --
9205 --------------------------------
9206
9207 procedure Build_Underlying_Full_View
9208 (N : Node_Id;
9209 Typ : Entity_Id;
9210 Par : Entity_Id)
9211 is
9212 Loc : constant Source_Ptr := Sloc (N);
9213 Subt : constant Entity_Id :=
9214 Make_Defining_Identifier
9215 (Loc, New_External_Name (Chars (Typ), 'S'));
9216
9217 Constr : Node_Id;
9218 Indic : Node_Id;
9219 C : Node_Id;
9220 Id : Node_Id;
9221
244e5a2c
AC
9222 procedure Set_Discriminant_Name (Id : Node_Id);
9223 -- If the derived type has discriminants, they may rename discriminants
9224 -- of the parent. When building the full view of the parent, we need to
9225 -- recover the names of the original discriminants if the constraint is
9226 -- given by named associations.
9227
9228 ---------------------------
9229 -- Set_Discriminant_Name --
9230 ---------------------------
9231
9232 procedure Set_Discriminant_Name (Id : Node_Id) is
9233 Disc : Entity_Id;
9234
9235 begin
9236 Set_Original_Discriminant (Id, Empty);
9237
9238 if Has_Discriminants (Typ) then
9239 Disc := First_Discriminant (Typ);
244e5a2c
AC
9240 while Present (Disc) loop
9241 if Chars (Disc) = Chars (Id)
9242 and then Present (Corresponding_Discriminant (Disc))
9243 then
9244 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9245 end if;
9246 Next_Discriminant (Disc);
9247 end loop;
9248 end if;
9249 end Set_Discriminant_Name;
9250
9251 -- Start of processing for Build_Underlying_Full_View
9252
996ae0b0
RK
9253 begin
9254 if Nkind (N) = N_Full_Type_Declaration then
9255 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9256
244e5a2c 9257 elsif Nkind (N) = N_Subtype_Declaration then
996ae0b0 9258 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
996ae0b0 9259
244e5a2c
AC
9260 elsif Nkind (N) = N_Component_Declaration then
9261 Constr :=
9262 New_Copy_Tree
9263 (Constraint (Subtype_Indication (Component_Definition (N))));
996ae0b0 9264
244e5a2c
AC
9265 else
9266 raise Program_Error;
9267 end if;
996ae0b0 9268
244e5a2c 9269 C := First (Constraints (Constr));
996ae0b0 9270 while Present (C) loop
996ae0b0
RK
9271 if Nkind (C) = N_Discriminant_Association then
9272 Id := First (Selector_Names (C));
996ae0b0 9273 while Present (Id) loop
244e5a2c 9274 Set_Discriminant_Name (Id);
996ae0b0
RK
9275 Next (Id);
9276 end loop;
9277 end if;
9278
9279 Next (C);
9280 end loop;
9281
244e5a2c
AC
9282 Indic :=
9283 Make_Subtype_Declaration (Loc,
9284 Defining_Identifier => Subt,
9285 Subtype_Indication =>
9286 Make_Subtype_Indication (Loc,
e4494292 9287 Subtype_Mark => New_Occurrence_Of (Par, Loc),
244e5a2c 9288 Constraint => New_Copy_Tree (Constr)));
996ae0b0 9289
615cbd95
AC
9290 -- If this is a component subtype for an outer itype, it is not
9291 -- a list member, so simply set the parent link for analysis: if
9292 -- the enclosing type does not need to be in a declarative list,
9293 -- neither do the components.
9294
244e5a2c
AC
9295 if Is_List_Member (N)
9296 and then Nkind (N) /= N_Component_Declaration
9297 then
615cbd95
AC
9298 Insert_Before (N, Indic);
9299 else
9300 Set_Parent (Indic, Parent (N));
9301 end if;
9302
996ae0b0
RK
9303 Analyze (Indic);
9304 Set_Underlying_Full_View (Typ, Full_View (Subt));
9305 end Build_Underlying_Full_View;
9306
9307 -------------------------------
9308 -- Check_Abstract_Overriding --
9309 -------------------------------
9310
9311 procedure Check_Abstract_Overriding (T : Entity_Id) is
88b32fc3 9312 Alias_Subp : Entity_Id;
57193e09 9313 Elmt : Elmt_Id;
88b32fc3 9314 Op_List : Elist_Id;
57193e09 9315 Subp : Entity_Id;
57193e09 9316 Type_Def : Node_Id;
996ae0b0 9317
bfae1846
AC
9318 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9319 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9320 -- which has pragma Implemented already set. Check whether Subp's entity
9321 -- kind conforms to the implementation kind of the overridden routine.
9322
9323 procedure Check_Pragma_Implemented
9324 (Subp : Entity_Id;
9325 Iface_Subp : Entity_Id);
9326 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9327 -- Iface_Subp and both entities have pragma Implemented already set on
9328 -- them. Check whether the two implementation kinds are conforming.
9329
9330 procedure Inherit_Pragma_Implemented
9331 (Subp : Entity_Id;
9332 Iface_Subp : Entity_Id);
9333 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9334 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9335 -- Propagate the implementation kind of Iface_Subp to Subp.
9336
9337 ------------------------------
9338 -- Check_Pragma_Implemented --
9339 ------------------------------
9340
9341 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9342 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9343 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
b3aa0ca8 9344 Subp_Alias : constant Entity_Id := Alias (Subp);
bfae1846 9345 Contr_Typ : Entity_Id;
b3aa0ca8 9346 Impl_Subp : Entity_Id;
bfae1846
AC
9347
9348 begin
9349 -- Subp must have an alias since it is a hidden entity used to link
9350 -- an interface subprogram to its overriding counterpart.
9351
b3aa0ca8
AC
9352 pragma Assert (Present (Subp_Alias));
9353
9354 -- Handle aliases to synchronized wrappers
9355
9356 Impl_Subp := Subp_Alias;
9357
9358 if Is_Primitive_Wrapper (Impl_Subp) then
9359 Impl_Subp := Wrapped_Entity (Impl_Subp);
9360 end if;
bfae1846
AC
9361
9362 -- Extract the type of the controlling formal
9363
b3aa0ca8 9364 Contr_Typ := Etype (First_Formal (Subp_Alias));
bfae1846
AC
9365
9366 if Is_Concurrent_Record_Type (Contr_Typ) then
9367 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9368 end if;
9369
9370 -- An interface subprogram whose implementation kind is By_Entry must
9371 -- be implemented by an entry.
9372
9373 if Impl_Kind = Name_By_Entry
b3aa0ca8 9374 and then Ekind (Impl_Subp) /= E_Entry
bfae1846
AC
9375 then
9376 Error_Msg_Node_2 := Iface_Alias;
9377 Error_Msg_NE
9378 ("type & must implement abstract subprogram & with an entry",
b3aa0ca8 9379 Subp_Alias, Contr_Typ);
bfae1846
AC
9380
9381 elsif Impl_Kind = Name_By_Protected_Procedure then
9382
9383 -- An interface subprogram whose implementation kind is By_
9384 -- Protected_Procedure cannot be implemented by a primitive
9385 -- procedure of a task type.
9386
9387 if Ekind (Contr_Typ) /= E_Protected_Type then
9388 Error_Msg_Node_2 := Contr_Typ;
9389 Error_Msg_NE
9390 ("interface subprogram & cannot be implemented by a " &
b3aa0ca8 9391 "primitive procedure of task type &", Subp_Alias,
bfae1846
AC
9392 Iface_Alias);
9393
9394 -- An interface subprogram whose implementation kind is By_
9395 -- Protected_Procedure must be implemented by a procedure.
9396
b3aa0ca8 9397 elsif Ekind (Impl_Subp) /= E_Procedure then
bfae1846
AC
9398 Error_Msg_Node_2 := Iface_Alias;
9399 Error_Msg_NE
9400 ("type & must implement abstract subprogram & with a " &
b3aa0ca8 9401 "procedure", Subp_Alias, Contr_Typ);
a6ce7e76
AC
9402
9403 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9404 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9405 then
9406 Error_Msg_Name_1 := Impl_Kind;
9407 Error_Msg_N
9408 ("overriding operation& must have synchronization%",
edbd98c4 9409 Subp_Alias);
bfae1846 9410 end if;
a6ce7e76
AC
9411
9412 -- If primitive has Optional synchronization, overriding operation
9413 -- must match if it has an explicit synchronization..
9414
9415 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9416 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9417 then
9418 Error_Msg_Name_1 := Impl_Kind;
9419 Error_Msg_N
9420 ("overriding operation& must have syncrhonization%",
edbd98c4 9421 Subp_Alias);
bfae1846
AC
9422 end if;
9423 end Check_Pragma_Implemented;
9424
9425 ------------------------------
9426 -- Check_Pragma_Implemented --
9427 ------------------------------
9428
9429 procedure Check_Pragma_Implemented
9430 (Subp : Entity_Id;
9431 Iface_Subp : Entity_Id)
9432 is
9433 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9434 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9435
9436 begin
9437 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9438 -- and overriding subprogram are different. In general this is an
9439 -- error except when the implementation kind of the overridden
b3aa0ca8 9440 -- subprograms is By_Any or Optional.
bfae1846
AC
9441
9442 if Iface_Kind /= Subp_Kind
9443 and then Iface_Kind /= Name_By_Any
b3aa0ca8 9444 and then Iface_Kind /= Name_Optional
bfae1846
AC
9445 then
9446 if Iface_Kind = Name_By_Entry then
9447 Error_Msg_N
9448 ("incompatible implementation kind, overridden subprogram " &
9449 "is marked By_Entry", Subp);
9450 else
9451 Error_Msg_N
9452 ("incompatible implementation kind, overridden subprogram " &
9453 "is marked By_Protected_Procedure", Subp);
9454 end if;
9455 end if;
9456 end Check_Pragma_Implemented;
9457
9458 --------------------------------
9459 -- Inherit_Pragma_Implemented --
9460 --------------------------------
9461
9462 procedure Inherit_Pragma_Implemented
9463 (Subp : Entity_Id;
9464 Iface_Subp : Entity_Id)
9465 is
9466 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9467 Loc : constant Source_Ptr := Sloc (Subp);
9468 Impl_Prag : Node_Id;
9469
9470 begin
9471 -- Since the implementation kind is stored as a representation item
9472 -- rather than a flag, create a pragma node.
9473
9474 Impl_Prag :=
9475 Make_Pragma (Loc,
3860d469 9476 Chars => Name_Implemented,
bfae1846
AC
9477 Pragma_Argument_Associations => New_List (
9478 Make_Pragma_Argument_Association (Loc,
e4494292 9479 Expression => New_Occurrence_Of (Subp, Loc)),
bfae1846
AC
9480
9481 Make_Pragma_Argument_Association (Loc,
7675ad4f 9482 Expression => Make_Identifier (Loc, Iface_Kind))));
bfae1846 9483
308e6f3a 9484 -- The pragma doesn't need to be analyzed because it is internally
3860d469 9485 -- built. It is safe to directly register it as a rep item since we
bfae1846
AC
9486 -- are only interested in the characters of the implementation kind.
9487
9488 Record_Rep_Item (Subp, Impl_Prag);
9489 end Inherit_Pragma_Implemented;
9490
9491 -- Start of processing for Check_Abstract_Overriding
9492
996ae0b0
RK
9493 begin
9494 Op_List := Primitive_Operations (T);
9495
9496 -- Loop to check primitive operations
9497
9498 Elmt := First_Elmt (Op_List);
9499 while Present (Elmt) loop
9500 Subp := Node (Elmt);
57193e09
TQ
9501 Alias_Subp := Alias (Subp);
9502
9503 -- Inherited subprograms are identified by the fact that they do not
9504 -- come from source, and the associated source location is the
9505 -- location of the first subtype of the derived type.
996ae0b0 9506
fea9e956
ES
9507 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
9508 -- subprograms that "require overriding".
9509
a5b62485 9510 -- Special exception, do not complain about failure to override the
9dfd2ff8
CC
9511 -- stream routines _Input and _Output, as well as the primitive
9512 -- operations used in dispatching selects since we always provide
996ae0b0
RK
9513 -- automatic overridings for these subprograms.
9514
2b73cf68
JM
9515 -- Also ignore this rule for convention CIL since .NET libraries
9516 -- do bizarre things with interfaces???
9517
9518 -- The partial view of T may have been a private extension, for
9519 -- which inherited functions dispatching on result are abstract.
9520 -- If the full view is a null extension, there is no need for
885c4871 9521 -- overriding in Ada 2005, but wrappers need to be built for them
2b73cf68
JM
9522 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
9523
9524 if Is_Null_Extension (T)
9525 and then Has_Controlling_Result (Subp)
0791fbe9 9526 and then Ada_Version >= Ada_2005
ce2b6ba5 9527 and then Present (Alias_Subp)
2b73cf68 9528 and then not Comes_From_Source (Subp)
ce2b6ba5 9529 and then not Is_Abstract_Subprogram (Alias_Subp)
ce4a6e84 9530 and then not Is_Access_Type (Etype (Subp))
2b73cf68 9531 then
7d7af38a 9532 null;
2b73cf68 9533
ce2b6ba5
JM
9534 -- Ada 2005 (AI-251): Internal entities of interfaces need no
9535 -- processing because this check is done with the aliased
9536 -- entity
9537
9538 elsif Present (Interface_Alias (Subp)) then
9539 null;
9540
7d7af38a 9541 elsif (Is_Abstract_Subprogram (Subp)
ce4a6e84
RD
9542 or else Requires_Overriding (Subp)
9543 or else
9544 (Has_Controlling_Result (Subp)
9545 and then Present (Alias_Subp)
9546 and then not Comes_From_Source (Subp)
9547 and then Sloc (Subp) = Sloc (First_Subtype (T))))
fbf5a39b
AC
9548 and then not Is_TSS (Subp, TSS_Stream_Input)
9549 and then not Is_TSS (Subp, TSS_Stream_Output)
fea9e956 9550 and then not Is_Abstract_Type (T)
2b73cf68 9551 and then Convention (T) /= Convention_CIL
ce2b6ba5 9552 and then not Is_Predefined_Interface_Primitive (Subp)
88b32fc3
BD
9553
9554 -- Ada 2005 (AI-251): Do not consider hidden entities associated
9555 -- with abstract interface types because the check will be done
9556 -- with the aliased entity (otherwise we generate a duplicated
9557 -- error message).
9558
ce2b6ba5 9559 and then not Present (Interface_Alias (Subp))
996ae0b0 9560 then
57193e09
TQ
9561 if Present (Alias_Subp) then
9562
9563 -- Only perform the check for a derived subprogram when the
f3d0f304 9564 -- type has an explicit record extension. This avoids incorrect
ce4a6e84
RD
9565 -- flagging of abstract subprograms for the case of a type
9566 -- without an extension that is derived from a formal type
9567 -- with a tagged actual (can occur within a private part).
57193e09
TQ
9568
9569 -- Ada 2005 (AI-391): In the case of an inherited function with
9570 -- a controlling result of the type, the rule does not apply if
9571 -- the type is a null extension (unless the parent function
9572 -- itself is abstract, in which case the function must still be
9573 -- be overridden). The expander will generate an overriding
9574 -- wrapper function calling the parent subprogram (see
9575 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
996ae0b0
RK
9576
9577 Type_Def := Type_Definition (Parent (T));
7d7af38a 9578
996ae0b0
RK
9579 if Nkind (Type_Def) = N_Derived_Type_Definition
9580 and then Present (Record_Extension_Part (Type_Def))
57193e09 9581 and then
0791fbe9 9582 (Ada_Version < Ada_2005
57193e09
TQ
9583 or else not Is_Null_Extension (T)
9584 or else Ekind (Subp) = E_Procedure
9585 or else not Has_Controlling_Result (Subp)
fea9e956
ES
9586 or else Is_Abstract_Subprogram (Alias_Subp)
9587 or else Requires_Overriding (Subp)
57193e09 9588 or else Is_Access_Type (Etype (Subp)))
996ae0b0 9589 then
ce2b6ba5
JM
9590 -- Avoid reporting error in case of abstract predefined
9591 -- primitive inherited from interface type because the
9592 -- body of internally generated predefined primitives
9593 -- of tagged types are generated later by Freeze_Type
9594
9595 if Is_Interface (Root_Type (T))
9596 and then Is_Abstract_Subprogram (Subp)
9597 and then Is_Predefined_Dispatching_Operation (Subp)
9598 and then not Comes_From_Source (Ultimate_Alias (Subp))
7d7af38a
JM
9599 then
9600 null;
9dfd2ff8 9601
7d7af38a
JM
9602 else
9603 Error_Msg_NE
9604 ("type must be declared abstract or & overridden",
9605 T, Subp);
9dfd2ff8 9606
7d7af38a
JM
9607 -- Traverse the whole chain of aliased subprograms to
9608 -- complete the error notification. This is especially
9609 -- useful for traceability of the chain of entities when
9610 -- the subprogram corresponds with an interface
9611 -- subprogram (which may be defined in another package).
9612
9613 if Present (Alias_Subp) then
9614 declare
9615 E : Entity_Id;
9616
9617 begin
9618 E := Subp;
9619 while Present (Alias (E)) loop
83de674b
AC
9620
9621 -- Avoid reporting redundant errors on entities
9622 -- inherited from interfaces
9623
9624 if Sloc (E) /= Sloc (T) then
9625 Error_Msg_Sloc := Sloc (E);
9626 Error_Msg_NE
9627 ("\& has been inherited #", T, Subp);
9628 end if;
9629
7d7af38a
JM
9630 E := Alias (E);
9631 end loop;
9dfd2ff8 9632
7d7af38a 9633 Error_Msg_Sloc := Sloc (E);
97ed5872
AC
9634
9635 -- AI05-0068: report if there is an overriding
9636 -- non-abstract subprogram that is invisible.
bb3c784c 9637
97ed5872
AC
9638 if Is_Hidden (E)
9639 and then not Is_Abstract_Subprogram (E)
9640 then
9641 Error_Msg_NE
bb3c784c
AC
9642 ("\& subprogram# is not visible",
9643 T, Subp);
97ed5872
AC
9644
9645 else
9646 Error_Msg_NE
9647 ("\& has been inherited from subprogram #",
9648 T, Subp);
9649 end if;
7d7af38a
JM
9650 end;
9651 end if;
9dfd2ff8
CC
9652 end if;
9653
758c442c 9654 -- Ada 2005 (AI-345): Protected or task type implementing
9dfd2ff8 9655 -- abstract interfaces.
758c442c
GD
9656
9657 elsif Is_Concurrent_Record_Type (T)
ce2b6ba5 9658 and then Present (Interfaces (T))
758c442c 9659 then
162c21d9
AC
9660 -- If an inherited subprogram is implemented by a protected
9661 -- procedure or an entry, then the first parameter of the
d0ef7921 9662 -- inherited subprogram shall be of mode OUT or IN OUT, or
162c21d9 9663 -- an access-to-variable parameter (RM 9.4(11.9/3))
88b32fc3 9664
162c21d9
AC
9665 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
9666 and then Ekind (First_Formal (Subp)) = E_In_Parameter
8f983e64 9667 and then Ekind (Subp) /= E_Function
162c21d9 9668 and then not Is_Predefined_Dispatching_Operation (Subp)
8f983e64 9669 then
162c21d9 9670 Error_Msg_PT (T, Subp);
88b32fc3
BD
9671
9672 -- Some other kind of overriding failure
9673
9674 else
9675 Error_Msg_NE
9676 ("interface subprogram & must be overridden",
9677 T, Subp);
8f983e64
ES
9678
9679 -- Examine primitive operations of synchronized type,
9680 -- to find homonyms that have the wrong profile.
9681
9682 declare
9683 Prim : Entity_Id;
9684
9685 begin
9686 Prim :=
9687 First_Entity (Corresponding_Concurrent_Type (T));
9688 while Present (Prim) loop
9689 if Chars (Prim) = Chars (Subp) then
9690 Error_Msg_NE
9691 ("profile is not type conformant with "
9692 & "prefixed view profile of "
9693 & "inherited operation&", Prim, Subp);
9694 end if;
9695
9696 Next_Entity (Prim);
9697 end loop;
9698 end;
88b32fc3 9699 end if;
996ae0b0 9700 end if;
88b32fc3 9701
996ae0b0 9702 else
fea9e956
ES
9703 Error_Msg_Node_2 := T;
9704 Error_Msg_N
9705 ("abstract subprogram& not allowed for type&", Subp);
9706
9707 -- Also post unconditional warning on the type (unconditional
9708 -- so that if there are more than one of these cases, we get
9709 -- them all, and not just the first one).
9710
9711 Error_Msg_Node_2 := Subp;
ed2233dc 9712 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
996ae0b0
RK
9713 end if;
9714 end if;
9715
e917e3b8 9716 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
7d7af38a 9717
bfae1846
AC
9718 -- Subp is an expander-generated procedure which maps an interface
9719 -- alias to a protected wrapper. The interface alias is flagged by
9720 -- pragma Implemented. Ensure that Subp is a procedure when the
9721 -- implementation kind is By_Protected_Procedure or an entry when
9722 -- By_Entry.
9723
9724 if Ada_Version >= Ada_2012
7d7af38a 9725 and then Is_Hidden (Subp)
ce2b6ba5 9726 and then Present (Interface_Alias (Subp))
bfae1846 9727 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
7d7af38a 9728 then
bfae1846
AC
9729 Check_Pragma_Implemented (Subp);
9730 end if;
7d7af38a 9731
bfae1846
AC
9732 -- Subp is an interface primitive which overrides another interface
9733 -- primitive marked with pragma Implemented.
7d7af38a 9734
bfae1846 9735 if Ada_Version >= Ada_2012
bfae1846
AC
9736 and then Present (Overridden_Operation (Subp))
9737 and then Has_Rep_Pragma
9738 (Overridden_Operation (Subp), Name_Implemented)
9739 then
9740 -- If the overriding routine is also marked by Implemented, check
9741 -- that the two implementation kinds are conforming.
9742
9743 if Has_Rep_Pragma (Subp, Name_Implemented) then
9744 Check_Pragma_Implemented
9745 (Subp => Subp,
9746 Iface_Subp => Overridden_Operation (Subp));
9747
9748 -- Otherwise the overriding routine inherits the implementation
9749 -- kind from the overridden subprogram.
9750
9751 else
9752 Inherit_Pragma_Implemented
9753 (Subp => Subp,
9754 Iface_Subp => Overridden_Operation (Subp));
9755 end if;
7d7af38a
JM
9756 end if;
9757
409274f1
AC
9758 -- If the operation is a wrapper for a synchronized primitive, it
9759 -- may be called indirectly through a dispatching select. We assume
9760 -- that it will be referenced elsewhere indirectly, and suppress
9761 -- warnings about an unused entity.
9762
9763 if Is_Primitive_Wrapper (Subp)
9764 and then Present (Wrapped_Entity (Subp))
9765 then
9766 Set_Referenced (Wrapped_Entity (Subp));
9767 end if;
9768
7d7af38a 9769 Next_Elmt (Elmt);
996ae0b0
RK
9770 end loop;
9771 end Check_Abstract_Overriding;
9772
9773 ------------------------------------------------
9774 -- Check_Access_Discriminant_Requires_Limited --
9775 ------------------------------------------------
9776
9777 procedure Check_Access_Discriminant_Requires_Limited
9778 (D : Node_Id;
9779 Loc : Node_Id)
9780 is
9781 begin
9dfd2ff8
CC
9782 -- A discriminant_specification for an access discriminant shall appear
9783 -- only in the declaration for a task or protected type, or for a type
9784 -- with the reserved word 'limited' in its definition or in one of its
0144fd18
RD
9785 -- ancestors (RM 3.7(10)).
9786
9787 -- AI-0063: The proper condition is that type must be immutably limited,
9788 -- or else be a partial view.
996ae0b0 9789
e0ae93e2 9790 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
51245e2d 9791 if Is_Limited_View (Current_Scope)
e0ae93e2 9792 or else
0144fd18 9793 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
e0ae93e2
RD
9794 and then Limited_Present (Parent (Current_Scope)))
9795 then
9796 null;
9797
9798 else
9799 Error_Msg_N
9800 ("access discriminants allowed only for limited types", Loc);
9801 end if;
996ae0b0
RK
9802 end if;
9803 end Check_Access_Discriminant_Requires_Limited;
9804
9805 -----------------------------------
9806 -- Check_Aliased_Component_Types --
9807 -----------------------------------
9808
9809 procedure Check_Aliased_Component_Types (T : Entity_Id) is
9810 C : Entity_Id;
9811
9812 begin
a5b62485
AC
9813 -- ??? Also need to check components of record extensions, but not
9814 -- components of protected types (which are always limited).
996ae0b0 9815
9dfd2ff8
CC
9816 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
9817 -- types to be unconstrained. This is safe because it is illegal to
9818 -- create access subtypes to such types with explicit discriminant
9819 -- constraints.
758c442c 9820
996ae0b0
RK
9821 if not Is_Limited_Type (T) then
9822 if Ekind (T) = E_Record_Type then
9823 C := First_Component (T);
9824 while Present (C) loop
9825 if Is_Aliased (C)
9826 and then Has_Discriminants (Etype (C))
9827 and then not Is_Constrained (Etype (C))
950d3e7d 9828 and then not In_Instance_Body
0791fbe9 9829 and then Ada_Version < Ada_2005
996ae0b0
RK
9830 then
9831 Error_Msg_N
dc06abec 9832 ("aliased component must be constrained (RM 3.6(11))",
996ae0b0
RK
9833 C);
9834 end if;
9835
9836 Next_Component (C);
9837 end loop;
9838
9839 elsif Ekind (T) = E_Array_Type then
9840 if Has_Aliased_Components (T)
9841 and then Has_Discriminants (Component_Type (T))
9842 and then not Is_Constrained (Component_Type (T))
950d3e7d 9843 and then not In_Instance_Body
0791fbe9 9844 and then Ada_Version < Ada_2005
996ae0b0
RK
9845 then
9846 Error_Msg_N
dc06abec 9847 ("aliased component type must be constrained (RM 3.6(11))",
996ae0b0
RK
9848 T);
9849 end if;
9850 end if;
9851 end if;
9852 end Check_Aliased_Component_Types;
9853
9854 ----------------------
9855 -- Check_Completion --
9856 ----------------------
9857
9858 procedure Check_Completion (Body_Id : Node_Id := Empty) is
9859 E : Entity_Id;
9860
9861 procedure Post_Error;
9862 -- Post error message for lack of completion for entity E
9863
fbf5a39b
AC
9864 ----------------
9865 -- Post_Error --
9866 ----------------
9867
996ae0b0 9868 procedure Post_Error is
b568955d
AC
9869
9870 procedure Missing_Body;
9871 -- Output missing body message
9872
9873 ------------------
9874 -- Missing_Body --
9875 ------------------
9876
9877 procedure Missing_Body is
9878 begin
9879 -- Spec is in same unit, so we can post on spec
9880
9881 if In_Same_Source_Unit (Body_Id, E) then
9882 Error_Msg_N ("missing body for &", E);
9883
9884 -- Spec is in a separate unit, so we have to post on the body
9885
9886 else
9887 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
9888 end if;
9889 end Missing_Body;
9890
9891 -- Start of processing for Post_Error
9892
996ae0b0
RK
9893 begin
9894 if not Comes_From_Source (E) then
9895
bce79204 9896 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
996ae0b0
RK
9897 -- It may be an anonymous protected type created for a
9898 -- single variable. Post error on variable, if present.
9899
9900 declare
9901 Var : Entity_Id;
9902
9903 begin
9904 Var := First_Entity (Current_Scope);
996ae0b0
RK
9905 while Present (Var) loop
9906 exit when Etype (Var) = E
9907 and then Comes_From_Source (Var);
9908
9909 Next_Entity (Var);
9910 end loop;
9911
9912 if Present (Var) then
9913 E := Var;
9914 end if;
9915 end;
9916 end if;
9917 end if;
9918
9919 -- If a generated entity has no completion, then either previous
a5b62485 9920 -- semantic errors have disabled the expansion phase, or else we had
fea9e956 9921 -- missing subunits, or else we are compiling without expansion,
a5b62485 9922 -- or else something is very wrong.
996ae0b0
RK
9923
9924 if not Comes_From_Source (E) then
9925 pragma Assert
07fc65c4 9926 (Serious_Errors_Detected > 0
fbf5a39b 9927 or else Configurable_Run_Time_Violations > 0
996ae0b0
RK
9928 or else Subunits_Missing
9929 or else not Expander_Active);
9930 return;
9931
9932 -- Here for source entity
9933
9934 else
9935 -- Here if no body to post the error message, so we post the error
9936 -- on the declaration that has no completion. This is not really
9937 -- the right place to post it, think about this later ???
9938
9939 if No (Body_Id) then
9940 if Is_Type (E) then
9941 Error_Msg_NE
9942 ("missing full declaration for }", Parent (E), E);
9943 else
ed2233dc 9944 Error_Msg_NE ("missing body for &", Parent (E), E);
996ae0b0
RK
9945 end if;
9946
9947 -- Package body has no completion for a declaration that appears
9948 -- in the corresponding spec. Post error on the body, with a
9949 -- reference to the non-completed declaration.
9950
9951 else
9952 Error_Msg_Sloc := Sloc (E);
9953
9954 if Is_Type (E) then
ed2233dc 9955 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
996ae0b0
RK
9956
9957 elsif Is_Overloadable (E)
9958 and then Current_Entity_In_Scope (E) /= E
9959 then
7d7af38a
JM
9960 -- It may be that the completion is mistyped and appears as
9961 -- a distinct overloading of the entity.
996ae0b0
RK
9962
9963 declare
fbf5a39b
AC
9964 Candidate : constant Entity_Id :=
9965 Current_Entity_In_Scope (E);
9966 Decl : constant Node_Id :=
9967 Unit_Declaration_Node (Candidate);
996ae0b0
RK
9968
9969 begin
9970 if Is_Overloadable (Candidate)
9971 and then Ekind (Candidate) = Ekind (E)
9972 and then Nkind (Decl) = N_Subprogram_Body
9973 and then Acts_As_Spec (Decl)
9974 then
9975 Check_Type_Conformant (Candidate, E);
9976
9977 else
b568955d 9978 Missing_Body;
996ae0b0
RK
9979 end if;
9980 end;
b568955d 9981
996ae0b0 9982 else
b568955d 9983 Missing_Body;
996ae0b0
RK
9984 end if;
9985 end if;
9986 end if;
9987 end Post_Error;
9988
d8221f45 9989 -- Start of processing for Check_Completion
996ae0b0
RK
9990
9991 begin
9992 E := First_Entity (Current_Scope);
9993 while Present (E) loop
9994 if Is_Intrinsic_Subprogram (E) then
9995 null;
9996
7d7af38a
JM
9997 -- The following situation requires special handling: a child unit
9998 -- that appears in the context clause of the body of its parent:
996ae0b0
RK
9999
10000 -- procedure Parent.Child (...);
a5b62485 10001
996ae0b0
RK
10002 -- with Parent.Child;
10003 -- package body Parent is
10004
7d7af38a
JM
10005 -- Here Parent.Child appears as a local entity, but should not be
10006 -- flagged as requiring completion, because it is a compilation
10007 -- unit.
996ae0b0 10008
fea9e956
ES
10009 -- Ignore missing completion for a subprogram that does not come from
10010 -- source (including the _Call primitive operation of RAS types,
10011 -- which has to have the flag Comes_From_Source for other purposes):
10012 -- we assume that the expander will provide the missing completion.
e1f3cb58 10013 -- In case of previous errors, other expansion actions that provide
d6533e74 10014 -- bodies for null procedures with not be invoked, so inhibit message
e1f3cb58 10015 -- in those cases.
d600ef16 10016
d6533e74
RD
10017 -- Note that E_Operator is not in the list that follows, because
10018 -- this kind is reserved for predefined operators, that are
10019 -- intrinsic and do not need completion.
fea9e956 10020
996ae0b0
RK
10021 elsif Ekind (E) = E_Function
10022 or else Ekind (E) = E_Procedure
10023 or else Ekind (E) = E_Generic_Function
10024 or else Ekind (E) = E_Generic_Procedure
10025 then
e1f3cb58
AC
10026 if Has_Completion (E) then
10027 null;
10028
10029 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
10030 null;
10031
10032 elsif Is_Subprogram (E)
10033 and then (not Comes_From_Source (E)
b69cd36a 10034 or else Chars (E) = Name_uCall)
e1f3cb58
AC
10035 then
10036 null;
10037
10038 elsif
10039 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
10040 then
10041 null;
10042
10043 elsif Nkind (Parent (E)) = N_Procedure_Specification
10044 and then Null_Present (Parent (E))
10045 and then Serious_Errors_Detected > 0
996ae0b0 10046 then
e1f3cb58
AC
10047 null;
10048
10049 else
996ae0b0
RK
10050 Post_Error;
10051 end if;
10052
10053 elsif Is_Entry (E) then
10054 if not Has_Completion (E) and then
10055 (Ekind (Scope (E)) = E_Protected_Object
10056 or else Ekind (Scope (E)) = E_Protected_Type)
10057 then
10058 Post_Error;
10059 end if;
10060
950d3e7d 10061 elsif Is_Package_Or_Generic_Package (E) then
996ae0b0
RK
10062 if Unit_Requires_Body (E) then
10063 if not Has_Completion (E)
10064 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
10065 N_Compilation_Unit
10066 then
10067 Post_Error;
10068 end if;
10069
10070 elsif not Is_Child_Unit (E) then
10071 May_Need_Implicit_Body (E);
10072 end if;
10073
9c25bb25
AC
10074 -- A formal incomplete type (Ada 2012) does not require a completion;
10075 -- other incomplete type declarations do.
d600ef16 10076
996ae0b0
RK
10077 elsif Ekind (E) = E_Incomplete_Type
10078 and then No (Underlying_Type (E))
0add5a95 10079 and then not Is_Generic_Type (E)
996ae0b0
RK
10080 then
10081 Post_Error;
10082
10083 elsif (Ekind (E) = E_Task_Type or else
10084 Ekind (E) = E_Protected_Type)
10085 and then not Has_Completion (E)
10086 then
10087 Post_Error;
10088
a5b62485
AC
10089 -- A single task declared in the current scope is a constant, verify
10090 -- that the body of its anonymous type is in the same scope. If the
10091 -- task is defined elsewhere, this may be a renaming declaration for
fbf5a39b
AC
10092 -- which no completion is needed.
10093
996ae0b0
RK
10094 elsif Ekind (E) = E_Constant
10095 and then Ekind (Etype (E)) = E_Task_Type
10096 and then not Has_Completion (Etype (E))
fbf5a39b 10097 and then Scope (Etype (E)) = Current_Scope
996ae0b0
RK
10098 then
10099 Post_Error;
10100
10101 elsif Ekind (E) = E_Protected_Object
10102 and then not Has_Completion (Etype (E))
10103 then
10104 Post_Error;
10105
10106 elsif Ekind (E) = E_Record_Type then
10107 if Is_Tagged_Type (E) then
10108 Check_Abstract_Overriding (E);
88b32fc3 10109 Check_Conventions (E);
996ae0b0
RK
10110 end if;
10111
10112 Check_Aliased_Component_Types (E);
10113
10114 elsif Ekind (E) = E_Array_Type then
10115 Check_Aliased_Component_Types (E);
10116
10117 end if;
10118
10119 Next_Entity (E);
10120 end loop;
10121 end Check_Completion;
10122
9a7e930f
AC
10123 ------------------------------------
10124 -- Check_CPP_Type_Has_No_Defaults --
10125 ------------------------------------
539fcb45 10126
9a7e930f 10127 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
539fcb45
JM
10128 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
10129 Clist : Node_Id;
10130 Comp : Node_Id;
10131
10132 begin
9a7e930f
AC
10133 -- Obtain the component list
10134
539fcb45
JM
10135 if Nkind (Tdef) = N_Record_Definition then
10136 Clist := Component_List (Tdef);
9a7e930f 10137 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
539fcb45
JM
10138 Clist := Component_List (Record_Extension_Part (Tdef));
10139 end if;
10140
9a7e930f
AC
10141 -- Check all components to ensure no default expressions
10142
539fcb45
JM
10143 if Present (Clist) then
10144 Comp := First (Component_Items (Clist));
10145 while Present (Comp) loop
10146 if Present (Expression (Comp)) then
10147 Error_Msg_N
9a7e930f
AC
10148 ("component of imported 'C'P'P type cannot have "
10149 & "default expression", Expression (Comp));
539fcb45
JM
10150 end if;
10151
10152 Next (Comp);
10153 end loop;
10154 end if;
9a7e930f 10155 end Check_CPP_Type_Has_No_Defaults;
539fcb45 10156
996ae0b0
RK
10157 ----------------------------
10158 -- Check_Delta_Expression --
10159 ----------------------------
10160
10161 procedure Check_Delta_Expression (E : Node_Id) is
10162 begin
10163 if not (Is_Real_Type (Etype (E))) then
10164 Wrong_Type (E, Any_Real);
10165
10166 elsif not Is_OK_Static_Expression (E) then
fbf5a39b
AC
10167 Flag_Non_Static_Expr
10168 ("non-static expression used for delta value!", E);
996ae0b0
RK
10169
10170 elsif not UR_Is_Positive (Expr_Value_R (E)) then
10171 Error_Msg_N ("delta expression must be positive", E);
10172
10173 else
10174 return;
10175 end if;
10176
10177 -- If any of above errors occurred, then replace the incorrect
10178 -- expression by the real 0.1, which should prevent further errors.
10179
10180 Rewrite (E,
10181 Make_Real_Literal (Sloc (E), Ureal_Tenth));
10182 Analyze_And_Resolve (E, Standard_Float);
996ae0b0
RK
10183 end Check_Delta_Expression;
10184
10185 -----------------------------
10186 -- Check_Digits_Expression --
10187 -----------------------------
10188
10189 procedure Check_Digits_Expression (E : Node_Id) is
10190 begin
10191 if not (Is_Integer_Type (Etype (E))) then
10192 Wrong_Type (E, Any_Integer);
10193
10194 elsif not Is_OK_Static_Expression (E) then
fbf5a39b
AC
10195 Flag_Non_Static_Expr
10196 ("non-static expression used for digits value!", E);
996ae0b0
RK
10197
10198 elsif Expr_Value (E) <= 0 then
10199 Error_Msg_N ("digits value must be greater than zero", E);
10200
10201 else
10202 return;
10203 end if;
10204
10205 -- If any of above errors occurred, then replace the incorrect
10206 -- expression by the integer 1, which should prevent further errors.
10207
10208 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
10209 Analyze_And_Resolve (E, Standard_Integer);
10210
10211 end Check_Digits_Expression;
10212
996ae0b0
RK
10213 --------------------------
10214 -- Check_Initialization --
10215 --------------------------
10216
10217 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
10218 begin
88b32fc3 10219 if Is_Limited_Type (T)
996ae0b0 10220 and then not In_Instance
c45b6ae0 10221 and then not In_Inlined_Body
996ae0b0 10222 then
2a31c32b 10223 if not OK_For_Limited_Init (T, Exp) then
71f62180
ES
10224
10225 -- In GNAT mode, this is just a warning, to allow it to be evilly
10226 -- turned off. Otherwise it is a real error.
65356e64 10227
88b32fc3
BD
10228 if GNAT_Mode then
10229 Error_Msg_N
71f62180
ES
10230 ("?cannot initialize entities of limited type!", Exp);
10231
0791fbe9 10232 elsif Ada_Version < Ada_2005 then
151c42b0
YM
10233
10234 -- The side effect removal machinery may generate illegal Ada
10235 -- code to avoid the usage of access types and 'reference in
06b599fd 10236 -- SPARK mode. Since this is legal code with respect to theorem
151c42b0
YM
10237 -- proving, do not emit the error.
10238
f5da7a97 10239 if GNATprove_Mode
151c42b0
YM
10240 and then Nkind (Exp) = N_Function_Call
10241 and then Nkind (Parent (Exp)) = N_Object_Declaration
10242 and then not Comes_From_Source
10243 (Defining_Identifier (Parent (Exp)))
10244 then
10245 null;
10246
10247 else
10248 Error_Msg_N
10249 ("cannot initialize entities of limited type", Exp);
10250 Explain_Limited_Type (T, Exp);
10251 end if;
71f62180
ES
10252
10253 else
10254 -- Specialize error message according to kind of illegal
10255 -- initial expression.
10256
10257 if Nkind (Exp) = N_Type_Conversion
10258 and then Nkind (Expression (Exp)) = N_Function_Call
10259 then
10260 Error_Msg_N
10261 ("illegal context for call"
10262 & " to function with limited result", Exp);
10263
10264 else
10265 Error_Msg_N
7d7af38a 10266 ("initialization of limited object requires aggregate "
71f62180
ES
10267 & "or function call", Exp);
10268 end if;
88b32fc3 10269 end if;
65356e64 10270 end if;
996ae0b0
RK
10271 end if;
10272 end Check_Initialization;
10273
ce2b6ba5
JM
10274 ----------------------
10275 -- Check_Interfaces --
10276 ----------------------
10277
10278 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
10279 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
10280
10281 Iface : Node_Id;
10282 Iface_Def : Node_Id;
10283 Iface_Typ : Entity_Id;
10284 Parent_Node : Node_Id;
10285
10286 Is_Task : Boolean := False;
10287 -- Set True if parent type or any progenitor is a task interface
10288
10289 Is_Protected : Boolean := False;
10290 -- Set True if parent type or any progenitor is a protected interface
10291
10292 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
10293 -- Check that a progenitor is compatible with declaration.
10294 -- Error is posted on Error_Node.
10295
10296 ------------------
10297 -- Check_Ifaces --
10298 ------------------
10299
10300 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
10301 Iface_Id : constant Entity_Id :=
10302 Defining_Identifier (Parent (Iface_Def));
10303 Type_Def : Node_Id;
10304
10305 begin
10306 if Nkind (N) = N_Private_Extension_Declaration then
10307 Type_Def := N;
10308 else
10309 Type_Def := Type_Definition (N);
10310 end if;
10311
10312 if Is_Task_Interface (Iface_Id) then
10313 Is_Task := True;
10314
10315 elsif Is_Protected_Interface (Iface_Id) then
10316 Is_Protected := True;
10317 end if;
10318
443614e3
AC
10319 if Is_Synchronized_Interface (Iface_Id) then
10320
10321 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
10322 -- extension derived from a synchronized interface must explicitly
10323 -- be declared synchronized, because the full view will be a
10324 -- synchronized type.
10325
10326 if Nkind (N) = N_Private_Extension_Declaration then
10327 if not Synchronized_Present (N) then
10328 Error_Msg_NE
10329 ("private extension of& must be explicitly synchronized",
10330 N, Iface_Id);
10331 end if;
10332
10333 -- However, by 3.9.4(16/2), a full type that is a record extension
10334 -- is never allowed to derive from a synchronized interface (note
10335 -- that interfaces must be excluded from this check, because those
10336 -- are represented by derived type definitions in some cases).
10337
10338 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
10339 and then not Interface_Present (Type_Definition (N))
10340 then
10341 Error_Msg_N ("record extension cannot derive from synchronized"
10342 & " interface", Error_Node);
10343 end if;
10344 end if;
10345
ce2b6ba5
JM
10346 -- Check that the characteristics of the progenitor are compatible
10347 -- with the explicit qualifier in the declaration.
10348 -- The check only applies to qualifiers that come from source.
10349 -- Limited_Present also appears in the declaration of corresponding
10350 -- records, and the check does not apply to them.
10351
10352 if Limited_Present (Type_Def)
10353 and then not
10354 Is_Concurrent_Record_Type (Defining_Identifier (N))
10355 then
10356 if Is_Limited_Interface (Parent_Type)
10357 and then not Is_Limited_Interface (Iface_Id)
10358 then
10359 Error_Msg_NE
10360 ("progenitor& must be limited interface",
10361 Error_Node, Iface_Id);
10362
10363 elsif
10364 (Task_Present (Iface_Def)
10365 or else Protected_Present (Iface_Def)
10366 or else Synchronized_Present (Iface_Def))
10367 and then Nkind (N) /= N_Private_Extension_Declaration
e358346d 10368 and then not Error_Posted (N)
ce2b6ba5
JM
10369 then
10370 Error_Msg_NE
10371 ("progenitor& must be limited interface",
10372 Error_Node, Iface_Id);
10373 end if;
10374
10375 -- Protected interfaces can only inherit from limited, synchronized
10376 -- or protected interfaces.
10377
10378 elsif Nkind (N) = N_Full_Type_Declaration
10379 and then Protected_Present (Type_Def)
10380 then
10381 if Limited_Present (Iface_Def)
10382 or else Synchronized_Present (Iface_Def)
10383 or else Protected_Present (Iface_Def)
10384 then
10385 null;
10386
10387 elsif Task_Present (Iface_Def) then
10388 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
10389 & " from task interface", Error_Node);
10390
10391 else
10392 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
10393 & " from non-limited interface", Error_Node);
10394 end if;
10395
10396 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
10397 -- limited and synchronized.
10398
10399 elsif Synchronized_Present (Type_Def) then
10400 if Limited_Present (Iface_Def)
10401 or else Synchronized_Present (Iface_Def)
10402 then
10403 null;
10404
10405 elsif Protected_Present (Iface_Def)
10406 and then Nkind (N) /= N_Private_Extension_Declaration
10407 then
10408 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
10409 & " from protected interface", Error_Node);
10410
10411 elsif Task_Present (Iface_Def)
10412 and then Nkind (N) /= N_Private_Extension_Declaration
10413 then
10414 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
10415 & " from task interface", Error_Node);
10416
10417 elsif not Is_Limited_Interface (Iface_Id) then
10418 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
10419 & " from non-limited interface", Error_Node);
10420 end if;
10421
10422 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
10423 -- synchronized or task interfaces.
10424
10425 elsif Nkind (N) = N_Full_Type_Declaration
10426 and then Task_Present (Type_Def)
10427 then
10428 if Limited_Present (Iface_Def)
10429 or else Synchronized_Present (Iface_Def)
10430 or else Task_Present (Iface_Def)
10431 then
10432 null;
10433
10434 elsif Protected_Present (Iface_Def) then
10435 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
10436 & " protected interface", Error_Node);
10437
10438 else
10439 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
10440 & " non-limited interface", Error_Node);
10441 end if;
10442 end if;
10443 end Check_Ifaces;
10444
10445 -- Start of processing for Check_Interfaces
10446
10447 begin
10448 if Is_Interface (Parent_Type) then
10449 if Is_Task_Interface (Parent_Type) then
10450 Is_Task := True;
10451
10452 elsif Is_Protected_Interface (Parent_Type) then
10453 Is_Protected := True;
10454 end if;
10455 end if;
10456
10457 if Nkind (N) = N_Private_Extension_Declaration then
10458
10459 -- Check that progenitors are compatible with declaration
10460
10461 Iface := First (Interface_List (Def));
10462 while Present (Iface) loop
10463 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
10464
10465 Parent_Node := Parent (Base_Type (Iface_Typ));
10466 Iface_Def := Type_Definition (Parent_Node);
10467
10468 if not Is_Interface (Iface_Typ) then
6765b310 10469 Diagnose_Interface (Iface, Iface_Typ);
ce2b6ba5
JM
10470
10471 else
10472 Check_Ifaces (Iface_Def, Iface);
10473 end if;
10474
10475 Next (Iface);
10476 end loop;
10477
10478 if Is_Task and Is_Protected then
10479 Error_Msg_N
10480 ("type cannot derive from task and protected interface", N);
10481 end if;
10482
10483 return;
10484 end if;
10485
10486 -- Full type declaration of derived type.
10487 -- Check compatibility with parent if it is interface type
10488
10489 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
10490 and then Is_Interface (Parent_Type)
10491 then
10492 Parent_Node := Parent (Parent_Type);
10493
10494 -- More detailed checks for interface varieties
10495
10496 Check_Ifaces
10497 (Iface_Def => Type_Definition (Parent_Node),
10498 Error_Node => Subtype_Indication (Type_Definition (N)));
10499 end if;
10500
10501 Iface := First (Interface_List (Def));
10502 while Present (Iface) loop
10503 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
10504
10505 Parent_Node := Parent (Base_Type (Iface_Typ));
10506 Iface_Def := Type_Definition (Parent_Node);
10507
10508 if not Is_Interface (Iface_Typ) then
6765b310 10509 Diagnose_Interface (Iface, Iface_Typ);
ce2b6ba5
JM
10510
10511 else
10512 -- "The declaration of a specific descendant of an interface
10513 -- type freezes the interface type" RM 13.14
10514
10515 Freeze_Before (N, Iface_Typ);
10516 Check_Ifaces (Iface_Def, Error_Node => Iface);
10517 end if;
10518
10519 Next (Iface);
10520 end loop;
10521
10522 if Is_Task and Is_Protected then
10523 Error_Msg_N
10524 ("type cannot derive from task and protected interface", N);
10525 end if;
10526 end Check_Interfaces;
10527
996ae0b0
RK
10528 ------------------------------------
10529 -- Check_Or_Process_Discriminants --
10530 ------------------------------------
10531
9dfd2ff8
CC
10532 -- If an incomplete or private type declaration was already given for the
10533 -- type, the discriminants may have already been processed if they were
10534 -- present on the incomplete declaration. In this case a full conformance
8e4dac80
TQ
10535 -- check has been performed in Find_Type_Name, and we then recheck here
10536 -- some properties that can't be checked on the partial view alone.
10537 -- Otherwise we call Process_Discriminants.
996ae0b0 10538
fbf5a39b
AC
10539 procedure Check_Or_Process_Discriminants
10540 (N : Node_Id;
10541 T : Entity_Id;
10542 Prev : Entity_Id := Empty)
10543 is
996ae0b0
RK
10544 begin
10545 if Has_Discriminants (T) then
10546
8e4dac80
TQ
10547 -- Discriminants are already set on T if they were already present
10548 -- on the partial view. Make them visible to component declarations.
996ae0b0
RK
10549
10550 declare
027dbed8
AC
10551 D : Entity_Id;
10552 -- Discriminant on T (full view) referencing expr on partial view
8e4dac80
TQ
10553
10554 Prev_D : Entity_Id;
10555 -- Entity of corresponding discriminant on partial view
996ae0b0 10556
8e4dac80
TQ
10557 New_D : Node_Id;
10558 -- Discriminant specification for full view, expression is the
10559 -- syntactic copy on full view (which has been checked for
10560 -- conformance with partial view), only used here to post error
10561 -- message.
027dbed8 10562
996ae0b0 10563 begin
027dbed8 10564 D := First_Discriminant (T);
8e4dac80 10565 New_D := First (Discriminant_Specifications (N));
996ae0b0 10566 while Present (D) loop
8e4dac80 10567 Prev_D := Current_Entity (D);
996ae0b0
RK
10568 Set_Current_Entity (D);
10569 Set_Is_Immediately_Visible (D);
8e4dac80
TQ
10570 Set_Homonym (D, Prev_D);
10571
10572 -- Handle the case where there is an untagged partial view and
10573 -- the full view is tagged: must disallow discriminants with
5e5db3b4
GD
10574 -- defaults, unless compiling for Ada 2012, which allows a
10575 -- limited tagged type to have defaulted discriminants (see
e917e3b8
AC
10576 -- AI05-0214). However, suppress error here if it was already
10577 -- reported on the default expression of the partial view.
8e4dac80
TQ
10578
10579 if Is_Tagged_Type (T)
e917e3b8
AC
10580 and then Present (Expression (Parent (D)))
10581 and then (not Is_Limited_Type (Current_Scope)
10582 or else Ada_Version < Ada_2012)
10583 and then not Error_Posted (Expression (Parent (D)))
8e4dac80 10584 then
5e5db3b4
GD
10585 if Ada_Version >= Ada_2012 then
10586 Error_Msg_N
10587 ("discriminants of nonlimited tagged type cannot have"
10588 & " defaults",
10589 Expression (New_D));
10590 else
10591 Error_Msg_N
10592 ("discriminants of tagged type cannot have defaults",
10593 Expression (New_D));
10594 end if;
8e4dac80 10595 end if;
996ae0b0 10596
0ab80019
AC
10597 -- Ada 2005 (AI-230): Access discriminant allowed in
10598 -- non-limited record types.
996ae0b0 10599
0791fbe9 10600 if Ada_Version < Ada_2005 then
6e937c1c 10601
9dfd2ff8
CC
10602 -- This restriction gets applied to the full type here. It
10603 -- has already been applied earlier to the partial view.
6e937c1c
AC
10604
10605 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
10606 end if;
996ae0b0
RK
10607
10608 Next_Discriminant (D);
8e4dac80 10609 Next (New_D);
996ae0b0
RK
10610 end loop;
10611 end;
10612
10613 elsif Present (Discriminant_Specifications (N)) then
fbf5a39b 10614 Process_Discriminants (N, Prev);
996ae0b0
RK
10615 end if;
10616 end Check_Or_Process_Discriminants;
10617
10618 ----------------------
10619 -- Check_Real_Bound --
10620 ----------------------
10621
10622 procedure Check_Real_Bound (Bound : Node_Id) is
10623 begin
10624 if not Is_Real_Type (Etype (Bound)) then
10625 Error_Msg_N
10626 ("bound in real type definition must be of real type", Bound);
10627
10628 elsif not Is_OK_Static_Expression (Bound) then
fbf5a39b
AC
10629 Flag_Non_Static_Expr
10630 ("non-static expression used for real type bound!", Bound);
996ae0b0
RK
10631
10632 else
10633 return;
10634 end if;
10635
10636 Rewrite
10637 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
10638 Analyze (Bound);
10639 Resolve (Bound, Standard_Float);
10640 end Check_Real_Bound;
10641
758c442c
GD
10642 ------------------------------
10643 -- Complete_Private_Subtype --
10644 ------------------------------
10645
10646 procedure Complete_Private_Subtype
10647 (Priv : Entity_Id;
10648 Full : Entity_Id;
10649 Full_Base : Entity_Id;
10650 Related_Nod : Node_Id)
10651 is
10652 Save_Next_Entity : Entity_Id;
10653 Save_Homonym : Entity_Id;
10654
10655 begin
10656 -- Set semantic attributes for (implicit) private subtype completion.
10657 -- If the full type has no discriminants, then it is a copy of the full
10658 -- view of the base. Otherwise, it is a subtype of the base with a
10659 -- possible discriminant constraint. Save and restore the original
10660 -- Next_Entity field of full to ensure that the calls to Copy_Node
10661 -- do not corrupt the entity chain.
10662
9dfd2ff8
CC
10663 -- Note that the type of the full view is the same entity as the type of
10664 -- the partial view. In this fashion, the subtype has access to the
10665 -- correct view of the parent.
996ae0b0
RK
10666
10667 Save_Next_Entity := Next_Entity (Full);
10668 Save_Homonym := Homonym (Priv);
10669
10670 case Ekind (Full_Base) is
996ae0b0
RK
10671 when E_Record_Type |
10672 E_Record_Subtype |
10673 Class_Wide_Kind |
10674 Private_Kind |
10675 Task_Kind |
10676 Protected_Kind =>
10677 Copy_Node (Priv, Full);
10678
d7761b2d
AC
10679 Set_Has_Discriminants
10680 (Full, Has_Discriminants (Full_Base));
ca4a4fe9 10681 Set_Has_Unknown_Discriminants
d7761b2d
AC
10682 (Full, Has_Unknown_Discriminants (Full_Base));
10683 Set_First_Entity (Full, First_Entity (Full_Base));
10684 Set_Last_Entity (Full, Last_Entity (Full_Base));
996ae0b0 10685
39d3009f
AC
10686 -- If the underlying base type is constrained, we know that the
10687 -- full view of the subtype is constrained as well (the converse
10688 -- is not necessarily true).
10689
10690 if Is_Constrained (Full_Base) then
10691 Set_Is_Constrained (Full);
10692 end if;
10693
996ae0b0
RK
10694 when others =>
10695 Copy_Node (Full_Base, Full);
d7761b2d 10696
ca4a4fe9
HK
10697 Set_Chars (Full, Chars (Priv));
10698 Conditional_Delay (Full, Priv);
10699 Set_Sloc (Full, Sloc (Priv));
996ae0b0
RK
10700 end case;
10701
d7761b2d
AC
10702 Set_Next_Entity (Full, Save_Next_Entity);
10703 Set_Homonym (Full, Save_Homonym);
996ae0b0
RK
10704 Set_Associated_Node_For_Itype (Full, Related_Nod);
10705
8b3c6430 10706 -- Set common attributes for all subtypes: kind, convention, etc.
996ae0b0
RK
10707
10708 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
8b3c6430 10709 Set_Convention (Full, Convention (Full_Base));
996ae0b0
RK
10710
10711 -- The Etype of the full view is inconsistent. Gigi needs to see the
10712 -- structural full view, which is what the current scheme gives:
10713 -- the Etype of the full view is the etype of the full base. However,
10714 -- if the full base is a derived type, the full view then looks like
10715 -- a subtype of the parent, not a subtype of the full base. If instead
10716 -- we write:
10717
10718 -- Set_Etype (Full, Full_Base);
10719
10720 -- then we get inconsistencies in the front-end (confusion between
71d9e9f2 10721 -- views). Several outstanding bugs are related to this ???
996ae0b0
RK
10722
10723 Set_Is_First_Subtype (Full, False);
10724 Set_Scope (Full, Scope (Priv));
10725 Set_Size_Info (Full, Full_Base);
10726 Set_RM_Size (Full, RM_Size (Full_Base));
10727 Set_Is_Itype (Full);
10728
10729 -- A subtype of a private-type-without-discriminants, whose full-view
a90bd866 10730 -- has discriminants with default expressions, is not constrained.
996ae0b0
RK
10731
10732 if not Has_Discriminants (Priv) then
10733 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
fbf5a39b
AC
10734
10735 if Has_Discriminants (Full_Base) then
10736 Set_Discriminant_Constraint
10737 (Full, Discriminant_Constraint (Full_Base));
35ae2ed8
AC
10738
10739 -- The partial view may have been indefinite, the full view
10740 -- might not be.
10741
10742 Set_Has_Unknown_Discriminants
10743 (Full, Has_Unknown_Discriminants (Full_Base));
fbf5a39b 10744 end if;
996ae0b0
RK
10745 end if;
10746
10747 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
10748 Set_Depends_On_Private (Full, Has_Private_Component (Full));
10749
a5b62485
AC
10750 -- Freeze the private subtype entity if its parent is delayed, and not
10751 -- already frozen. We skip this processing if the type is an anonymous
10752 -- subtype of a record component, or is the corresponding record of a
10753 -- protected type, since ???
996ae0b0
RK
10754
10755 if not Is_Type (Scope (Full)) then
10756 Set_Has_Delayed_Freeze (Full,
10757 Has_Delayed_Freeze (Full_Base)
71d9e9f2 10758 and then (not Is_Frozen (Full_Base)));
996ae0b0
RK
10759 end if;
10760
10761 Set_Freeze_Node (Full, Empty);
10762 Set_Is_Frozen (Full, False);
10763 Set_Full_View (Priv, Full);
10764
10765 if Has_Discriminants (Full) then
fbf5a39b
AC
10766 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
10767 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
71d9e9f2 10768
996ae0b0
RK
10769 if Has_Unknown_Discriminants (Full) then
10770 Set_Discriminant_Constraint (Full, No_Elist);
10771 end if;
10772 end if;
10773
10774 if Ekind (Full_Base) = E_Record_Type
10775 and then Has_Discriminants (Full_Base)
10776 and then Has_Discriminants (Priv) -- might not, if errors
e6f69614 10777 and then not Has_Unknown_Discriminants (Priv)
996ae0b0
RK
10778 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
10779 then
10780 Create_Constrained_Components
10781 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
10782
10783 -- If the full base is itself derived from private, build a congruent
244e5a2c
AC
10784 -- subtype of its underlying type, for use by the back end. For a
10785 -- constrained record component, the declaration cannot be placed on
9dfd2ff8
CC
10786 -- the component list, but it must nevertheless be built an analyzed, to
10787 -- supply enough information for Gigi to compute the size of component.
996ae0b0
RK
10788
10789 elsif Ekind (Full_Base) in Private_Kind
10790 and then Is_Derived_Type (Full_Base)
10791 and then Has_Discriminants (Full_Base)
24105bab 10792 and then (Ekind (Current_Scope) /= E_Record_Subtype)
996ae0b0 10793 then
244e5a2c
AC
10794 if not Is_Itype (Priv)
10795 and then
10796 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
10797 then
10798 Build_Underlying_Full_View
10799 (Parent (Priv), Full, Etype (Full_Base));
10800
10801 elsif Nkind (Related_Nod) = N_Component_Declaration then
10802 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
10803 end if;
996ae0b0
RK
10804
10805 elsif Is_Record_Type (Full_Base) then
10806
71d9e9f2 10807 -- Show Full is simply a renaming of Full_Base
996ae0b0
RK
10808
10809 Set_Cloned_Subtype (Full, Full_Base);
10810 end if;
10811
7a1f1775 10812 -- It is unsafe to share the bounds of a scalar type, because the Itype
a5b62485
AC
10813 -- is elaborated on demand, and if a bound is non-static then different
10814 -- orders of elaboration in different units will lead to different
10815 -- external symbols.
996ae0b0
RK
10816
10817 if Is_Scalar_Type (Full_Base) then
10818 Set_Scalar_Range (Full,
10819 Make_Range (Sloc (Related_Nod),
fbf5a39b
AC
10820 Low_Bound =>
10821 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
10822 High_Bound =>
10823 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
10824
10825 -- This completion inherits the bounds of the full parent, but if
10826 -- the parent is an unconstrained floating point type, so is the
10827 -- completion.
10828
10829 if Is_Floating_Point_Type (Full_Base) then
10830 Set_Includes_Infinities
10831 (Scalar_Range (Full), Has_Infinities (Full_Base));
10832 end if;
996ae0b0
RK
10833 end if;
10834
a5b62485
AC
10835 -- ??? It seems that a lot of fields are missing that should be copied
10836 -- from Full_Base to Full. Here are some that are introduced in a
10837 -- non-disruptive way but a cleanup is necessary.
996ae0b0
RK
10838
10839 if Is_Tagged_Type (Full_Base) then
10840 Set_Is_Tagged_Type (Full);
ef2a63ba
JM
10841 Set_Direct_Primitive_Operations (Full,
10842 Direct_Primitive_Operations (Full_Base));
0fb31b5f
AC
10843
10844 -- Inherit class_wide type of full_base in case the partial view was
10845 -- not tagged. Otherwise it has already been created when the private
10846 -- subtype was analyzed.
10847
10848 if No (Class_Wide_Type (Full)) then
10849 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
10850 end if;
996ae0b0 10851
fc4039b9
ES
10852 -- If this is a subtype of a protected or task type, constrain its
10853 -- corresponding record, unless this is a subtype without constraints,
10854 -- i.e. a simple renaming as with an actual subtype in an instance.
10855
996ae0b0 10856 elsif Is_Concurrent_Type (Full_Base) then
996ae0b0
RK
10857 if Has_Discriminants (Full)
10858 and then Present (Corresponding_Record_Type (Full_Base))
fc4039b9
ES
10859 and then
10860 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
996ae0b0
RK
10861 then
10862 Set_Corresponding_Record_Type (Full,
10863 Constrain_Corresponding_Record
10864 (Full, Corresponding_Record_Type (Full_Base),
10865 Related_Nod, Full_Base));
10866
10867 else
10868 Set_Corresponding_Record_Type (Full,
10869 Corresponding_Record_Type (Full_Base));
10870 end if;
10871 end if;
b4ca2d2c 10872
a043e735
AC
10873 -- Link rep item chain, and also setting of Has_Predicates from private
10874 -- subtype to full subtype, since we will need these on the full subtype
10875 -- to create the predicate function. Note that the full subtype may
10876 -- already have rep items, inherited from the full view of the base
10877 -- type, so we must be sure not to overwrite these entries.
b4ca2d2c 10878
a043e735 10879 declare
d3ba478e 10880 Append : Boolean;
a043e735
AC
10881 Item : Node_Id;
10882 Next_Item : Node_Id;
10883
10884 begin
10885 Item := First_Rep_Item (Full);
10886
10887 -- If no existing rep items on full type, we can just link directly
10888 -- to the list of items on the private type.
10889
10890 if No (Item) then
10891 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
10892
b715bc59
AC
10893 -- Otherwise, search to the end of items currently linked to the full
10894 -- subtype and append the private items to the end. However, if Priv
10895 -- and Full already have the same list of rep items, then the append
10896 -- is not done, as that would create a circularity.
a043e735 10897
b715bc59 10898 elsif Item /= First_Rep_Item (Priv) then
d3ba478e
AC
10899 Append := True;
10900
a043e735
AC
10901 loop
10902 Next_Item := Next_Rep_Item (Item);
10903 exit when No (Next_Item);
10904 Item := Next_Item;
d3ba478e
AC
10905
10906 -- If the private view has aspect specifications, the full view
10907 -- inherits them. Since these aspects may already have been
10908 -- attached to the full view during derivation, do not append
10909 -- them if already present.
10910
10911 if Item = First_Rep_Item (Priv) then
10912 Append := False;
10913 exit;
10914 end if;
a043e735
AC
10915 end loop;
10916
10917 -- And link the private type items at the end of the chain
10918
d3ba478e
AC
10919 if Append then
10920 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
10921 end if;
a043e735
AC
10922 end if;
10923 end;
10924
10925 -- Make sure Has_Predicates is set on full type if it is set on the
10926 -- private type. Note that it may already be set on the full type and
10927 -- if so, we don't want to unset it.
10928
10929 if Has_Predicates (Priv) then
10930 Set_Has_Predicates (Full);
10931 end if;
996ae0b0
RK
10932 end Complete_Private_Subtype;
10933
10934 ----------------------------
10935 -- Constant_Redeclaration --
10936 ----------------------------
10937
10938 procedure Constant_Redeclaration
10939 (Id : Entity_Id;
10940 N : Node_Id;
10941 T : out Entity_Id)
10942 is
10943 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
10944 Obj_Def : constant Node_Id := Object_Definition (N);
10945 New_T : Entity_Id;
10946
57193e09
TQ
10947 procedure Check_Possible_Deferred_Completion
10948 (Prev_Id : Entity_Id;
10949 Prev_Obj_Def : Node_Id;
10950 Curr_Obj_Def : Node_Id);
10951 -- Determine whether the two object definitions describe the partial
10952 -- and the full view of a constrained deferred constant. Generate
10953 -- a subtype for the full view and verify that it statically matches
10954 -- the subtype of the partial view.
10955
07fc65c4 10956 procedure Check_Recursive_Declaration (Typ : Entity_Id);
9dfd2ff8
CC
10957 -- If deferred constant is an access type initialized with an allocator,
10958 -- check whether there is an illegal recursion in the definition,
10959 -- through a default value of some record subcomponent. This is normally
10960 -- detected when generating init procs, but requires this additional
10961 -- mechanism when expansion is disabled.
07fc65c4 10962
57193e09
TQ
10963 ----------------------------------------
10964 -- Check_Possible_Deferred_Completion --
10965 ----------------------------------------
10966
10967 procedure Check_Possible_Deferred_Completion
10968 (Prev_Id : Entity_Id;
10969 Prev_Obj_Def : Node_Id;
10970 Curr_Obj_Def : Node_Id)
10971 is
10972 begin
10973 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
10974 and then Present (Constraint (Prev_Obj_Def))
10975 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
10976 and then Present (Constraint (Curr_Obj_Def))
10977 then
10978 declare
10979 Loc : constant Source_Ptr := Sloc (N);
092ef350
RD
10980 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
10981 Decl : constant Node_Id :=
57193e09 10982 Make_Subtype_Declaration (Loc,
092ef350
RD
10983 Defining_Identifier => Def_Id,
10984 Subtype_Indication =>
57193e09
TQ
10985 Relocate_Node (Curr_Obj_Def));
10986
10987 begin
10988 Insert_Before_And_Analyze (N, Decl);
10989 Set_Etype (Id, Def_Id);
10990
10991 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
10992 Error_Msg_Sloc := Sloc (Prev_Id);
10993 Error_Msg_N ("subtype does not statically match deferred " &
10994 "declaration#", N);
10995 end if;
10996 end;
10997 end if;
10998 end Check_Possible_Deferred_Completion;
10999
fbf5a39b
AC
11000 ---------------------------------
11001 -- Check_Recursive_Declaration --
11002 ---------------------------------
11003
07fc65c4
GB
11004 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
11005 Comp : Entity_Id;
11006
11007 begin
11008 if Is_Record_Type (Typ) then
11009 Comp := First_Component (Typ);
07fc65c4
GB
11010 while Present (Comp) loop
11011 if Comes_From_Source (Comp) then
11012 if Present (Expression (Parent (Comp)))
11013 and then Is_Entity_Name (Expression (Parent (Comp)))
11014 and then Entity (Expression (Parent (Comp))) = Prev
11015 then
11016 Error_Msg_Sloc := Sloc (Parent (Comp));
11017 Error_Msg_NE
11018 ("illegal circularity with declaration for&#",
11019 N, Comp);
11020 return;
11021
11022 elsif Is_Record_Type (Etype (Comp)) then
11023 Check_Recursive_Declaration (Etype (Comp));
11024 end if;
11025 end if;
11026
11027 Next_Component (Comp);
11028 end loop;
11029 end if;
11030 end Check_Recursive_Declaration;
11031
11032 -- Start of processing for Constant_Redeclaration
11033
996ae0b0
RK
11034 begin
11035 if Nkind (Parent (Prev)) = N_Object_Declaration then
11036 if Nkind (Object_Definition
11037 (Parent (Prev))) = N_Subtype_Indication
11038 then
11039 -- Find type of new declaration. The constraints of the two
11040 -- views must match statically, but there is no point in
11041 -- creating an itype for the full view.
11042
11043 if Nkind (Obj_Def) = N_Subtype_Indication then
11044 Find_Type (Subtype_Mark (Obj_Def));
11045 New_T := Entity (Subtype_Mark (Obj_Def));
11046
11047 else
11048 Find_Type (Obj_Def);
11049 New_T := Entity (Obj_Def);
11050 end if;
11051
11052 T := Etype (Prev);
11053
11054 else
11055 -- The full view may impose a constraint, even if the partial
11056 -- view does not, so construct the subtype.
11057
11058 New_T := Find_Type_Of_Object (Obj_Def, N);
11059 T := New_T;
11060 end if;
11061
11062 else
71d9e9f2 11063 -- Current declaration is illegal, diagnosed below in Enter_Name
996ae0b0
RK
11064
11065 T := Empty;
11066 New_T := Any_Type;
11067 end if;
11068
4f08579c
AC
11069 -- If previous full declaration or a renaming declaration exists, or if
11070 -- a homograph is present, let Enter_Name handle it, either with an
11071 -- error or with the removal of an overridden implicit subprogram.
979b94ea
AC
11072 -- The previous one is a full declaration if it has an expression
11073 -- (which in the case of an aggregate is indicated by the Init flag).
996ae0b0
RK
11074
11075 if Ekind (Prev) /= E_Constant
4f08579c 11076 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
996ae0b0 11077 or else Present (Expression (Parent (Prev)))
979b94ea 11078 or else Has_Init_Expression (Parent (Prev))
07fc65c4 11079 or else Present (Full_View (Prev))
996ae0b0
RK
11080 then
11081 Enter_Name (Id);
11082
758c442c
GD
11083 -- Verify that types of both declarations match, or else that both types
11084 -- are anonymous access types whose designated subtypes statically match
11085 -- (as allowed in Ada 2005 by AI-385).
996ae0b0 11086
758c442c
GD
11087 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
11088 and then
11089 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
11090 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
ce4a6e84
RD
11091 or else Is_Access_Constant (Etype (New_T)) /=
11092 Is_Access_Constant (Etype (Prev))
11093 or else Can_Never_Be_Null (Etype (New_T)) /=
11094 Can_Never_Be_Null (Etype (Prev))
11095 or else Null_Exclusion_Present (Parent (Prev)) /=
11096 Null_Exclusion_Present (Parent (Id))
758c442c
GD
11097 or else not Subtypes_Statically_Match
11098 (Designated_Type (Etype (Prev)),
11099 Designated_Type (Etype (New_T))))
11100 then
996ae0b0
RK
11101 Error_Msg_Sloc := Sloc (Prev);
11102 Error_Msg_N ("type does not match declaration#", N);
11103 Set_Full_View (Prev, Id);
11104 Set_Etype (Id, Any_Type);
11105
ce4a6e84
RD
11106 elsif
11107 Null_Exclusion_Present (Parent (Prev))
11108 and then not Null_Exclusion_Present (N)
11109 then
11110 Error_Msg_Sloc := Sloc (Prev);
11111 Error_Msg_N ("null-exclusion does not match declaration#", N);
11112 Set_Full_View (Prev, Id);
11113 Set_Etype (Id, Any_Type);
11114
996ae0b0
RK
11115 -- If so, process the full constant declaration
11116
11117 else
57193e09
TQ
11118 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
11119 -- the deferred declaration is constrained, then the subtype defined
11120 -- by the subtype_indication in the full declaration shall match it
11121 -- statically.
11122
11123 Check_Possible_Deferred_Completion
11124 (Prev_Id => Prev,
11125 Prev_Obj_Def => Object_Definition (Parent (Prev)),
11126 Curr_Obj_Def => Obj_Def);
11127
996ae0b0
RK
11128 Set_Full_View (Prev, Id);
11129 Set_Is_Public (Id, Is_Public (Prev));
11130 Set_Is_Internal (Id);
11131 Append_Entity (Id, Current_Scope);
11132
11133 -- Check ALIASED present if present before (RM 7.4(7))
11134
11135 if Is_Aliased (Prev)
11136 and then not Aliased_Present (N)
11137 then
11138 Error_Msg_Sloc := Sloc (Prev);
11139 Error_Msg_N ("ALIASED required (see declaration#)", N);
11140 end if;
11141
07fc65c4
GB
11142 -- Check that placement is in private part and that the incomplete
11143 -- declaration appeared in the visible part.
996ae0b0 11144
b16d9747 11145 if Ekind (Current_Scope) = E_Package
996ae0b0
RK
11146 and then not In_Private_Part (Current_Scope)
11147 then
11148 Error_Msg_Sloc := Sloc (Prev);
ed2233dc
AC
11149 Error_Msg_N
11150 ("full constant for declaration#"
11151 & " must be in private part", N);
07fc65c4
GB
11152
11153 elsif Ekind (Current_Scope) = E_Package
bce79204
AC
11154 and then
11155 List_Containing (Parent (Prev)) /=
d12b19fa 11156 Visible_Declarations (Package_Specification (Current_Scope))
07fc65c4
GB
11157 then
11158 Error_Msg_N
11159 ("deferred constant must be declared in visible part",
11160 Parent (Prev));
11161 end if;
11162
11163 if Is_Access_Type (T)
11164 and then Nkind (Expression (N)) = N_Allocator
11165 then
11166 Check_Recursive_Declaration (Designated_Type (T));
996ae0b0 11167 end if;
2a8fcd43
AC
11168
11169 -- A deferred constant is a visible entity. If type has invariants,
11170 -- verify that the initial value satisfies them.
11171
51597c23
AC
11172 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
11173 Insert_After (N,
11174 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
2a8fcd43 11175 end if;
996ae0b0
RK
11176 end if;
11177 end Constant_Redeclaration;
11178
11179 ----------------------
11180 -- Constrain_Access --
11181 ----------------------
11182
11183 procedure Constrain_Access
11184 (Def_Id : in out Entity_Id;
11185 S : Node_Id;
11186 Related_Nod : Node_Id)
11187 is
11188 T : constant Entity_Id := Entity (Subtype_Mark (S));
11189 Desig_Type : constant Entity_Id := Designated_Type (T);
11190 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
11191 Constraint_OK : Boolean := True;
11192
758c442c
GD
11193 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
11194 -- Simple predicate to test for defaulted discriminants
11195 -- Shouldn't this be in sem_util???
11196
11197 ---------------------------------
11198 -- Has_Defaulted_Discriminants --
11199 ---------------------------------
11200
11201 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
11202 begin
11203 return Has_Discriminants (Typ)
11204 and then Present (First_Discriminant (Typ))
11205 and then Present
11206 (Discriminant_Default_Value (First_Discriminant (Typ)));
11207 end Has_Defaulted_Discriminants;
11208
11209 -- Start of processing for Constrain_Access
11210
996ae0b0
RK
11211 begin
11212 if Is_Array_Type (Desig_Type) then
11213 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
11214
11215 elsif (Is_Record_Type (Desig_Type)
11216 or else Is_Incomplete_Or_Private_Type (Desig_Type))
11217 and then not Is_Constrained (Desig_Type)
11218 then
71d9e9f2
ES
11219 -- ??? The following code is a temporary kludge to ignore a
11220 -- discriminant constraint on access type if it is constraining
11221 -- the current record. Avoid creating the implicit subtype of the
11222 -- record we are currently compiling since right now, we cannot
11223 -- handle these. For now, just return the access type itself.
996ae0b0
RK
11224
11225 if Desig_Type = Current_Scope
11226 and then No (Def_Id)
11227 then
11228 Set_Ekind (Desig_Subtype, E_Record_Subtype);
11229 Def_Id := Entity (Subtype_Mark (S));
11230
71d9e9f2
ES
11231 -- This call added to ensure that the constraint is analyzed
11232 -- (needed for a B test). Note that we still return early from
11233 -- this procedure to avoid recursive processing. ???
996ae0b0
RK
11234
11235 Constrain_Discriminated_Type
11236 (Desig_Subtype, S, Related_Nod, For_Access => True);
996ae0b0
RK
11237 return;
11238 end if;
11239
a46cde68
AC
11240 -- Enforce rule that the constraint is illegal if there is an
11241 -- unconstrained view of the designated type. This means that the
11242 -- partial view (either a private type declaration or a derivation
11243 -- from a private type) has no discriminants. (Defect Report
11244 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
11245
113a62d9 11246 -- Rule updated for Ada 2005: The private type is said to have
a46cde68
AC
11247 -- a constrained partial view, given that objects of the type
11248 -- can be declared. Furthermore, the rule applies to all access
11249 -- types, unlike the rule concerning default discriminants (see
11250 -- RM 3.7.1(7/3))
11251
11252 if (Ekind (T) = E_General_Access_Type
11253 or else Ada_Version >= Ada_2005)
07fc65c4
GB
11254 and then Has_Private_Declaration (Desig_Type)
11255 and then In_Open_Scopes (Scope (Desig_Type))
f29b857f 11256 and then Has_Discriminants (Desig_Type)
07fc65c4 11257 then
07fc65c4 11258 declare
fbf5a39b
AC
11259 Pack : constant Node_Id :=
11260 Unit_Declaration_Node (Scope (Desig_Type));
07fc65c4
GB
11261 Decls : List_Id;
11262 Decl : Node_Id;
11263
11264 begin
11265 if Nkind (Pack) = N_Package_Declaration then
11266 Decls := Visible_Declarations (Specification (Pack));
11267 Decl := First (Decls);
07fc65c4
GB
11268 while Present (Decl) loop
11269 if (Nkind (Decl) = N_Private_Type_Declaration
11270 and then
11271 Chars (Defining_Identifier (Decl)) =
11272 Chars (Desig_Type))
11273
11274 or else
11275 (Nkind (Decl) = N_Full_Type_Declaration
11276 and then
11277 Chars (Defining_Identifier (Decl)) =
11278 Chars (Desig_Type)
11279 and then Is_Derived_Type (Desig_Type)
11280 and then
11281 Has_Private_Declaration (Etype (Desig_Type)))
11282 then
11283 if No (Discriminant_Specifications (Decl)) then
11284 Error_Msg_N
a46cde68
AC
11285 ("cannot constrain access type if designated " &
11286 "type has constrained partial view", S);
07fc65c4
GB
11287 end if;
11288
11289 exit;
11290 end if;
11291
11292 Next (Decl);
11293 end loop;
11294 end if;
11295 end;
11296 end if;
11297
996ae0b0
RK
11298 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
11299 For_Access => True);
11300
11301 elsif (Is_Task_Type (Desig_Type)
11302 or else Is_Protected_Type (Desig_Type))
11303 and then not Is_Constrained (Desig_Type)
11304 then
11305 Constrain_Concurrent
11306 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
11307
11308 else
11309 Error_Msg_N ("invalid constraint on access type", S);
11310 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
11311 Constraint_OK := False;
11312 end if;
11313
11314 if No (Def_Id) then
11315 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
11316 else
11317 Set_Ekind (Def_Id, E_Access_Subtype);
11318 end if;
11319
11320 if Constraint_OK then
11321 Set_Etype (Def_Id, Base_Type (T));
11322
11323 if Is_Private_Type (Desig_Type) then
11324 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
11325 end if;
11326 else
11327 Set_Etype (Def_Id, Any_Type);
11328 end if;
11329
11330 Set_Size_Info (Def_Id, T);
11331 Set_Is_Constrained (Def_Id, Constraint_OK);
11332 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
11333 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11334 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
11335
c6823a20 11336 Conditional_Delay (Def_Id, T);
758c442c 11337
9dfd2ff8
CC
11338 -- AI-363 : Subtypes of general access types whose designated types have
11339 -- default discriminants are disallowed. In instances, the rule has to
11340 -- be checked against the actual, of which T is the subtype. In a
11341 -- generic body, the rule is checked assuming that the actual type has
11342 -- defaulted discriminants.
758c442c 11343
0791fbe9 11344 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
758c442c
GD
11345 if Ekind (Base_Type (T)) = E_General_Access_Type
11346 and then Has_Defaulted_Discriminants (Desig_Type)
11347 then
0791fbe9 11348 if Ada_Version < Ada_2005 then
fea9e956
ES
11349 Error_Msg_N
11350 ("access subtype of general access type would not " &
324ac540 11351 "be allowed in Ada 2005?y?", S);
fea9e956
ES
11352 else
11353 Error_Msg_N
308e6f3a 11354 ("access subtype of general access type not allowed", S);
fea9e956
ES
11355 end if;
11356
88b32fc3 11357 Error_Msg_N ("\discriminants have defaults", S);
758c442c
GD
11358
11359 elsif Is_Access_Type (T)
11360 and then Is_Generic_Type (Desig_Type)
11361 and then Has_Discriminants (Desig_Type)
11362 and then In_Package_Body (Current_Scope)
11363 then
0791fbe9 11364 if Ada_Version < Ada_2005 then
fea9e956
ES
11365 Error_Msg_N
11366 ("access subtype would not be allowed in generic body " &
324ac540 11367 "in Ada 2005?y?", S);
fea9e956
ES
11368 else
11369 Error_Msg_N
11370 ("access subtype not allowed in generic body", S);
11371 end if;
11372
758c442c 11373 Error_Msg_N
88b32fc3 11374 ("\designated type is a discriminated formal", S);
758c442c
GD
11375 end if;
11376 end if;
996ae0b0
RK
11377 end Constrain_Access;
11378
11379 ---------------------
11380 -- Constrain_Array --
11381 ---------------------
11382
11383 procedure Constrain_Array
11384 (Def_Id : in out Entity_Id;
11385 SI : Node_Id;
11386 Related_Nod : Node_Id;
11387 Related_Id : Entity_Id;
11388 Suffix : Character)
11389 is
11390 C : constant Node_Id := Constraint (SI);
11391 Number_Of_Constraints : Nat := 0;
11392 Index : Node_Id;
11393 S, T : Entity_Id;
11394 Constraint_OK : Boolean := True;
11395
11396 begin
11397 T := Entity (Subtype_Mark (SI));
11398
11399 if Ekind (T) in Access_Kind then
11400 T := Designated_Type (T);
11401 end if;
11402
11403 -- If an index constraint follows a subtype mark in a subtype indication
11404 -- then the type or subtype denoted by the subtype mark must not already
11405 -- impose an index constraint. The subtype mark must denote either an
11406 -- unconstrained array type or an access type whose designated type
11407 -- is such an array type... (RM 3.6.1)
11408
11409 if Is_Constrained (T) then
ed2233dc 11410 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
996ae0b0
RK
11411 Constraint_OK := False;
11412
11413 else
11414 S := First (Constraints (C));
996ae0b0
RK
11415 while Present (S) loop
11416 Number_Of_Constraints := Number_Of_Constraints + 1;
11417 Next (S);
11418 end loop;
11419
11420 -- In either case, the index constraint must provide a discrete
11421 -- range for each index of the array type and the type of each
11422 -- discrete range must be the same as that of the corresponding
11423 -- index. (RM 3.6.1)
11424
11425 if Number_Of_Constraints /= Number_Dimensions (T) then
11426 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
11427 Constraint_OK := False;
11428
11429 else
11430 S := First (Constraints (C));
11431 Index := First_Index (T);
11432 Analyze (Index);
11433
11434 -- Apply constraints to each index type
11435
11436 for J in 1 .. Number_Of_Constraints loop
11437 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
11438 Next (Index);
11439 Next (S);
11440 end loop;
11441
11442 end if;
11443 end if;
11444
11445 if No (Def_Id) then
11446 Def_Id :=
11447 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
fbf5a39b
AC
11448 Set_Parent (Def_Id, Related_Nod);
11449
996ae0b0
RK
11450 else
11451 Set_Ekind (Def_Id, E_Array_Subtype);
11452 end if;
11453
11454 Set_Size_Info (Def_Id, (T));
11455 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11456 Set_Etype (Def_Id, Base_Type (T));
11457
11458 if Constraint_OK then
11459 Set_First_Index (Def_Id, First (Constraints (C)));
758c442c
GD
11460 else
11461 Set_First_Index (Def_Id, First_Index (T));
996ae0b0
RK
11462 end if;
11463
996ae0b0
RK
11464 Set_Is_Constrained (Def_Id, True);
11465 Set_Is_Aliased (Def_Id, Is_Aliased (T));
11466 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11467
11468 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
11469 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
11470
2b73cf68
JM
11471 -- A subtype does not inherit the packed_array_type of is parent. We
11472 -- need to initialize the attribute because if Def_Id is previously
11473 -- analyzed through a limited_with clause, it will have the attributes
11474 -- of an incomplete type, one of which is an Elist that overlaps the
11475 -- Packed_Array_Type field.
11476
11477 Set_Packed_Array_Type (Def_Id, Empty);
11478
11479 -- Build a freeze node if parent still needs one. Also make sure that
11480 -- the Depends_On_Private status is set because the subtype will need
11481 -- reprocessing at the time the base type does, and also we must set a
11482 -- conditional delay.
996ae0b0 11483
c6823a20
EB
11484 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
11485 Conditional_Delay (Def_Id, T);
996ae0b0
RK
11486 end Constrain_Array;
11487
11488 ------------------------------
11489 -- Constrain_Component_Type --
11490 ------------------------------
11491
11492 function Constrain_Component_Type
c6823a20 11493 (Comp : Entity_Id;
996ae0b0
RK
11494 Constrained_Typ : Entity_Id;
11495 Related_Node : Node_Id;
11496 Typ : Entity_Id;
b0f26df5 11497 Constraints : Elist_Id) return Entity_Id
996ae0b0 11498 is
c6823a20
EB
11499 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
11500 Compon_Type : constant Entity_Id := Etype (Comp);
3b1d4d82 11501 Array_Comp : Node_Id;
996ae0b0
RK
11502
11503 function Build_Constrained_Array_Type
b0f26df5 11504 (Old_Type : Entity_Id) return Entity_Id;
3b42c566 11505 -- If Old_Type is an array type, one of whose indexes is constrained
a5b62485
AC
11506 -- by a discriminant, build an Itype whose constraint replaces the
11507 -- discriminant with its value in the constraint.
996ae0b0
RK
11508
11509 function Build_Constrained_Discriminated_Type
b0f26df5 11510 (Old_Type : Entity_Id) return Entity_Id;
71d9e9f2 11511 -- Ditto for record components
996ae0b0
RK
11512
11513 function Build_Constrained_Access_Type
b0f26df5 11514 (Old_Type : Entity_Id) return Entity_Id;
996ae0b0
RK
11515 -- Ditto for access types. Makes use of previous two functions, to
11516 -- constrain designated type.
11517
11518 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
11519 -- T is an array or discriminated type, C is a list of constraints
11520 -- that apply to T. This routine builds the constrained subtype.
11521
11522 function Is_Discriminant (Expr : Node_Id) return Boolean;
71d9e9f2 11523 -- Returns True if Expr is a discriminant
996ae0b0 11524
07fc65c4 11525 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
71d9e9f2 11526 -- Find the value of discriminant Discrim in Constraint
996ae0b0
RK
11527
11528 -----------------------------------
11529 -- Build_Constrained_Access_Type --
11530 -----------------------------------
11531
11532 function Build_Constrained_Access_Type
b0f26df5 11533 (Old_Type : Entity_Id) return Entity_Id
996ae0b0
RK
11534 is
11535 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
11536 Itype : Entity_Id;
11537 Desig_Subtype : Entity_Id;
11538 Scop : Entity_Id;
11539
11540 begin
11541 -- if the original access type was not embedded in the enclosing
11542 -- type definition, there is no need to produce a new access
11543 -- subtype. In fact every access type with an explicit constraint
11544 -- generates an itype whose scope is the enclosing record.
11545
11546 if not Is_Type (Scope (Old_Type)) then
11547 return Old_Type;
11548
11549 elsif Is_Array_Type (Desig_Type) then
11550 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
11551
11552 elsif Has_Discriminants (Desig_Type) then
11553
11554 -- This may be an access type to an enclosing record type for
11555 -- which we are constructing the constrained components. Return
11556 -- the enclosing record subtype. This is not always correct,
11557 -- but avoids infinite recursion. ???
11558
11559 Desig_Subtype := Any_Type;
11560
11561 for J in reverse 0 .. Scope_Stack.Last loop
11562 Scop := Scope_Stack.Table (J).Entity;
11563
11564 if Is_Type (Scop)
11565 and then Base_Type (Scop) = Base_Type (Desig_Type)
11566 then
11567 Desig_Subtype := Scop;
11568 end if;
11569
11570 exit when not Is_Type (Scop);
11571 end loop;
11572
11573 if Desig_Subtype = Any_Type then
11574 Desig_Subtype :=
11575 Build_Constrained_Discriminated_Type (Desig_Type);
11576 end if;
11577
11578 else
11579 return Old_Type;
11580 end if;
11581
11582 if Desig_Subtype /= Desig_Type then
71d9e9f2 11583
996ae0b0
RK
11584 -- The Related_Node better be here or else we won't be able
11585 -- to attach new itypes to a node in the tree.
11586
11587 pragma Assert (Present (Related_Node));
11588
11589 Itype := Create_Itype (E_Access_Subtype, Related_Node);
11590
11591 Set_Etype (Itype, Base_Type (Old_Type));
11592 Set_Size_Info (Itype, (Old_Type));
11593 Set_Directly_Designated_Type (Itype, Desig_Subtype);
11594 Set_Depends_On_Private (Itype, Has_Private_Component
11595 (Old_Type));
11596 Set_Is_Access_Constant (Itype, Is_Access_Constant
11597 (Old_Type));
11598
11599 -- The new itype needs freezing when it depends on a not frozen
11600 -- type and the enclosing subtype needs freezing.
11601
11602 if Has_Delayed_Freeze (Constrained_Typ)
11603 and then not Is_Frozen (Constrained_Typ)
11604 then
11605 Conditional_Delay (Itype, Base_Type (Old_Type));
11606 end if;
11607
11608 return Itype;
11609
11610 else
11611 return Old_Type;
11612 end if;
11613 end Build_Constrained_Access_Type;
11614
11615 ----------------------------------
11616 -- Build_Constrained_Array_Type --
11617 ----------------------------------
11618
11619 function Build_Constrained_Array_Type
b0f26df5 11620 (Old_Type : Entity_Id) return Entity_Id
996ae0b0
RK
11621 is
11622 Lo_Expr : Node_Id;
11623 Hi_Expr : Node_Id;
11624 Old_Index : Node_Id;
11625 Range_Node : Node_Id;
11626 Constr_List : List_Id;
11627
11628 Need_To_Create_Itype : Boolean := False;
11629
11630 begin
11631 Old_Index := First_Index (Old_Type);
11632 while Present (Old_Index) loop
11633 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
11634
11635 if Is_Discriminant (Lo_Expr)
11636 or else Is_Discriminant (Hi_Expr)
11637 then
11638 Need_To_Create_Itype := True;
11639 end if;
11640
11641 Next_Index (Old_Index);
11642 end loop;
11643
11644 if Need_To_Create_Itype then
11645 Constr_List := New_List;
11646
11647 Old_Index := First_Index (Old_Type);
11648 while Present (Old_Index) loop
11649 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
11650
11651 if Is_Discriminant (Lo_Expr) then
07fc65c4 11652 Lo_Expr := Get_Discr_Value (Lo_Expr);
996ae0b0
RK
11653 end if;
11654
11655 if Is_Discriminant (Hi_Expr) then
07fc65c4 11656 Hi_Expr := Get_Discr_Value (Hi_Expr);
996ae0b0
RK
11657 end if;
11658
11659 Range_Node :=
11660 Make_Range
11661 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
11662
11663 Append (Range_Node, To => Constr_List);
11664
11665 Next_Index (Old_Index);
11666 end loop;
11667
11668 return Build_Subtype (Old_Type, Constr_List);
11669
11670 else
11671 return Old_Type;
11672 end if;
11673 end Build_Constrained_Array_Type;
11674
11675 ------------------------------------------
11676 -- Build_Constrained_Discriminated_Type --
11677 ------------------------------------------
11678
11679 function Build_Constrained_Discriminated_Type
b0f26df5 11680 (Old_Type : Entity_Id) return Entity_Id
996ae0b0
RK
11681 is
11682 Expr : Node_Id;
11683 Constr_List : List_Id;
11684 Old_Constraint : Elmt_Id;
11685
11686 Need_To_Create_Itype : Boolean := False;
11687
11688 begin
11689 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
11690 while Present (Old_Constraint) loop
11691 Expr := Node (Old_Constraint);
11692
11693 if Is_Discriminant (Expr) then
11694 Need_To_Create_Itype := True;
11695 end if;
11696
11697 Next_Elmt (Old_Constraint);
11698 end loop;
11699
11700 if Need_To_Create_Itype then
11701 Constr_List := New_List;
11702
11703 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
11704 while Present (Old_Constraint) loop
11705 Expr := Node (Old_Constraint);
11706
11707 if Is_Discriminant (Expr) then
07fc65c4 11708 Expr := Get_Discr_Value (Expr);
996ae0b0
RK
11709 end if;
11710
11711 Append (New_Copy_Tree (Expr), To => Constr_List);
11712
11713 Next_Elmt (Old_Constraint);
11714 end loop;
11715
11716 return Build_Subtype (Old_Type, Constr_List);
11717
11718 else
11719 return Old_Type;
11720 end if;
11721 end Build_Constrained_Discriminated_Type;
11722
11723 -------------------
11724 -- Build_Subtype --
11725 -------------------
11726
11727 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
11728 Indic : Node_Id;
11729 Subtyp_Decl : Node_Id;
11730 Def_Id : Entity_Id;
11731 Btyp : Entity_Id := Base_Type (T);
11732
11733 begin
a5b62485
AC
11734 -- The Related_Node better be here or else we won't be able to
11735 -- attach new itypes to a node in the tree.
996ae0b0
RK
11736
11737 pragma Assert (Present (Related_Node));
11738
11739 -- If the view of the component's type is incomplete or private
11740 -- with unknown discriminants, then the constraint must be applied
11741 -- to the full type.
11742
11743 if Has_Unknown_Discriminants (Btyp)
11744 and then Present (Underlying_Type (Btyp))
11745 then
11746 Btyp := Underlying_Type (Btyp);
11747 end if;
11748
11749 Indic :=
11750 Make_Subtype_Indication (Loc,
11751 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
11752 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
11753
11754 Def_Id := Create_Itype (Ekind (T), Related_Node);
11755
11756 Subtyp_Decl :=
11757 Make_Subtype_Declaration (Loc,
11758 Defining_Identifier => Def_Id,
11759 Subtype_Indication => Indic);
24105bab 11760
996ae0b0
RK
11761 Set_Parent (Subtyp_Decl, Parent (Related_Node));
11762
ffe9aba8 11763 -- Itypes must be analyzed with checks off (see package Itypes)
996ae0b0
RK
11764
11765 Analyze (Subtyp_Decl, Suppress => All_Checks);
11766
11767 return Def_Id;
11768 end Build_Subtype;
11769
07fc65c4
GB
11770 ---------------------
11771 -- Get_Discr_Value --
11772 ---------------------
996ae0b0 11773
07fc65c4 11774 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
9dfd2ff8
CC
11775 D : Entity_Id;
11776 E : Elmt_Id;
996ae0b0
RK
11777
11778 begin
07fc65c4
GB
11779 -- The discriminant may be declared for the type, in which case we
11780 -- find it by iterating over the list of discriminants. If the
11781 -- discriminant is inherited from a parent type, it appears as the
11782 -- corresponding discriminant of the current type. This will be the
11783 -- case when constraining an inherited component whose constraint is
11784 -- given by a discriminant of the parent.
996ae0b0 11785
9dfd2ff8
CC
11786 D := First_Discriminant (Typ);
11787 E := First_Elmt (Constraints);
88b32fc3 11788
07fc65c4 11789 while Present (D) loop
996ae0b0 11790 if D = Entity (Discrim)
88b32fc3 11791 or else D = CR_Discriminant (Entity (Discrim))
996ae0b0
RK
11792 or else Corresponding_Discriminant (D) = Entity (Discrim)
11793 then
11794 return Node (E);
11795 end if;
11796
11797 Next_Discriminant (D);
11798 Next_Elmt (E);
11799 end loop;
11800
027dbed8 11801 -- The Corresponding_Discriminant mechanism is incomplete, because
07fc65c4 11802 -- the correspondence between new and old discriminants is not one
a5b62485
AC
11803 -- to one: one new discriminant can constrain several old ones. In
11804 -- that case, scan sequentially the stored_constraint, the list of
11805 -- discriminants of the parents, and the constraints.
324ac540 11806
ab8bfb64
ES
11807 -- Previous code checked for the present of the Stored_Constraint
11808 -- list for the derived type, but did not use it at all. Should it
11809 -- be present when the component is a discriminated task type?
07fc65c4
GB
11810
11811 if Is_Derived_Type (Typ)
07fc65c4
GB
11812 and then Scope (Entity (Discrim)) = Etype (Typ)
11813 then
11814 D := First_Discriminant (Etype (Typ));
11815 E := First_Elmt (Constraints);
07fc65c4
GB
11816 while Present (D) loop
11817 if D = Entity (Discrim) then
11818 return Node (E);
11819 end if;
11820
11821 Next_Discriminant (D);
11822 Next_Elmt (E);
07fc65c4
GB
11823 end loop;
11824 end if;
11825
996ae0b0
RK
11826 -- Something is wrong if we did not find the value
11827
11828 raise Program_Error;
07fc65c4 11829 end Get_Discr_Value;
996ae0b0
RK
11830
11831 ---------------------
11832 -- Is_Discriminant --
11833 ---------------------
11834
11835 function Is_Discriminant (Expr : Node_Id) return Boolean is
11836 Discrim_Scope : Entity_Id;
11837
11838 begin
11839 if Denotes_Discriminant (Expr) then
11840 Discrim_Scope := Scope (Entity (Expr));
11841
11842 -- Either we have a reference to one of Typ's discriminants,
11843
11844 pragma Assert (Discrim_Scope = Typ
11845
11846 -- or to the discriminants of the parent type, in the case
11847 -- of a derivation of a tagged type with variants.
11848
11849 or else Discrim_Scope = Etype (Typ)
11850 or else Full_View (Discrim_Scope) = Etype (Typ)
11851
11852 -- or same as above for the case where the discriminants
11853 -- were declared in Typ's private view.
11854
11855 or else (Is_Private_Type (Discrim_Scope)
11856 and then Chars (Discrim_Scope) = Chars (Typ))
11857
11858 -- or else we are deriving from the full view and the
11859 -- discriminant is declared in the private entity.
11860
11861 or else (Is_Private_Type (Typ)
33931112 11862 and then Chars (Discrim_Scope) = Chars (Typ))
996ae0b0 11863
88b32fc3
BD
11864 -- Or we are constrained the corresponding record of a
11865 -- synchronized type that completes a private declaration.
11866
11867 or else (Is_Concurrent_Record_Type (Typ)
11868 and then
11869 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
11870
996ae0b0
RK
11871 -- or we have a class-wide type, in which case make sure the
11872 -- discriminant found belongs to the root type.
11873
11874 or else (Is_Class_Wide_Type (Typ)
33931112 11875 and then Etype (Typ) = Discrim_Scope));
996ae0b0
RK
11876
11877 return True;
11878 end if;
11879
ffe9aba8 11880 -- In all other cases we have something wrong
996ae0b0
RK
11881
11882 return False;
11883 end Is_Discriminant;
11884
11885 -- Start of processing for Constrain_Component_Type
11886
11887 begin
c6823a20
EB
11888 if Nkind (Parent (Comp)) = N_Component_Declaration
11889 and then Comes_From_Source (Parent (Comp))
11890 and then Comes_From_Source
11891 (Subtype_Indication (Component_Definition (Parent (Comp))))
11892 and then
11893 Is_Entity_Name
11894 (Subtype_Indication (Component_Definition (Parent (Comp))))
11895 then
11896 return Compon_Type;
11897
11898 elsif Is_Array_Type (Compon_Type) then
3b1d4d82
AC
11899 Array_Comp := Build_Constrained_Array_Type (Compon_Type);
11900
11901 -- If the component of the parent is packed, and the record type is
11902 -- already frozen, as is the case for an itype, the component type
11903 -- itself will not be frozen, and the packed array type for it must
dba44dbe
AC
11904 -- be constructed explicitly. Since the creation of packed types is
11905 -- an expansion activity, we only do this if expansion is active.
3b1d4d82 11906
ca3e17b0
AC
11907 if Expander_Active
11908 and then Is_Packed (Compon_Type)
11909 and then Is_Frozen (Current_Scope)
11910 then
3b1d4d82
AC
11911 Create_Packed_Array_Type (Array_Comp);
11912 end if;
683e5dc2 11913
3b1d4d82 11914 return Array_Comp;
996ae0b0
RK
11915
11916 elsif Has_Discriminants (Compon_Type) then
11917 return Build_Constrained_Discriminated_Type (Compon_Type);
11918
11919 elsif Is_Access_Type (Compon_Type) then
11920 return Build_Constrained_Access_Type (Compon_Type);
996ae0b0 11921
c6823a20
EB
11922 else
11923 return Compon_Type;
11924 end if;
996ae0b0
RK
11925 end Constrain_Component_Type;
11926
11927 --------------------------
11928 -- Constrain_Concurrent --
11929 --------------------------
11930
11931 -- For concurrent types, the associated record value type carries the same
11932 -- discriminants, so when we constrain a concurrent type, we must constrain
950d3e7d 11933 -- the corresponding record type as well.
996ae0b0
RK
11934
11935 procedure Constrain_Concurrent
11936 (Def_Id : in out Entity_Id;
11937 SI : Node_Id;
11938 Related_Nod : Node_Id;
11939 Related_Id : Entity_Id;
11940 Suffix : Character)
11941 is
36b8f95f
AC
11942 -- Retrieve Base_Type to ensure getting to the concurrent type in the
11943 -- case of a private subtype (needed when only doing semantic analysis).
11944
11945 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
996ae0b0
RK
11946 T_Val : Entity_Id;
11947
11948 begin
11949 if Ekind (T_Ent) in Access_Kind then
11950 T_Ent := Designated_Type (T_Ent);
11951 end if;
11952
11953 T_Val := Corresponding_Record_Type (T_Ent);
11954
11955 if Present (T_Val) then
11956
11957 if No (Def_Id) then
11958 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11959 end if;
11960
11961 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11962
11963 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11964 Set_Corresponding_Record_Type (Def_Id,
11965 Constrain_Corresponding_Record
11966 (Def_Id, T_Val, Related_Nod, Related_Id));
11967
11968 else
11969 -- If there is no associated record, expansion is disabled and this
11970 -- is a generic context. Create a subtype in any case, so that
11971 -- semantic analysis can proceed.
11972
11973 if No (Def_Id) then
11974 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11975 end if;
11976
11977 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11978 end if;
11979 end Constrain_Concurrent;
11980
11981 ------------------------------------
11982 -- Constrain_Corresponding_Record --
11983 ------------------------------------
11984
11985 function Constrain_Corresponding_Record
11986 (Prot_Subt : Entity_Id;
11987 Corr_Rec : Entity_Id;
11988 Related_Nod : Node_Id;
b0f26df5 11989 Related_Id : Entity_Id) return Entity_Id
996ae0b0 11990 is
71d9e9f2
ES
11991 T_Sub : constant Entity_Id :=
11992 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
996ae0b0
RK
11993
11994 begin
71d9e9f2 11995 Set_Etype (T_Sub, Corr_Rec);
71d9e9f2
ES
11996 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
11997 Set_Is_Constrained (T_Sub, True);
11998 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
11999 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
996ae0b0 12000
88b32fc3
BD
12001 -- As elsewhere, we do not want to create a freeze node for this itype
12002 -- if it is created for a constrained component of an enclosing record
12003 -- because references to outer discriminants will appear out of scope.
12004
12005 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
12006 Conditional_Delay (T_Sub, Corr_Rec);
12007 else
12008 Set_Is_Frozen (T_Sub);
12009 end if;
996ae0b0
RK
12010
12011 if Has_Discriminants (Prot_Subt) then -- False only if errors.
71d9e9f2
ES
12012 Set_Discriminant_Constraint
12013 (T_Sub, Discriminant_Constraint (Prot_Subt));
fbf5a39b 12014 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
71d9e9f2
ES
12015 Create_Constrained_Components
12016 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
996ae0b0
RK
12017 end if;
12018
12019 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
12020
12021 return T_Sub;
12022 end Constrain_Corresponding_Record;
12023
12024 -----------------------
12025 -- Constrain_Decimal --
12026 -----------------------
12027
07fc65c4 12028 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
12029 T : constant Entity_Id := Entity (Subtype_Mark (S));
12030 C : constant Node_Id := Constraint (S);
12031 Loc : constant Source_Ptr := Sloc (C);
12032 Range_Expr : Node_Id;
12033 Digits_Expr : Node_Id;
12034 Digits_Val : Uint;
12035 Bound_Val : Ureal;
12036
12037 begin
12038 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
12039
12040 if Nkind (C) = N_Range_Constraint then
12041 Range_Expr := Range_Expression (C);
12042 Digits_Val := Digits_Value (T);
12043
12044 else
12045 pragma Assert (Nkind (C) = N_Digits_Constraint);
7ff2d234 12046
2ba431e5 12047 Check_SPARK_Restriction ("digits constraint is not allowed", S);
7ff2d234 12048
996ae0b0
RK
12049 Digits_Expr := Digits_Expression (C);
12050 Analyze_And_Resolve (Digits_Expr, Any_Integer);
12051
12052 Check_Digits_Expression (Digits_Expr);
12053 Digits_Val := Expr_Value (Digits_Expr);
12054
12055 if Digits_Val > Digits_Value (T) then
12056 Error_Msg_N
12057 ("digits expression is incompatible with subtype", C);
12058 Digits_Val := Digits_Value (T);
12059 end if;
12060
12061 if Present (Range_Constraint (C)) then
12062 Range_Expr := Range_Expression (Range_Constraint (C));
12063 else
12064 Range_Expr := Empty;
12065 end if;
12066 end if;
12067
12068 Set_Etype (Def_Id, Base_Type (T));
12069 Set_Size_Info (Def_Id, (T));
12070 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12071 Set_Delta_Value (Def_Id, Delta_Value (T));
12072 Set_Scale_Value (Def_Id, Scale_Value (T));
12073 Set_Small_Value (Def_Id, Small_Value (T));
12074 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
12075 Set_Digits_Value (Def_Id, Digits_Val);
12076
12077 -- Manufacture range from given digits value if no range present
12078
12079 if No (Range_Expr) then
12080 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
12081 Range_Expr :=
71d9e9f2
ES
12082 Make_Range (Loc,
12083 Low_Bound =>
12084 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
12085 High_Bound =>
12086 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
996ae0b0
RK
12087 end if;
12088
07fc65c4 12089 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
996ae0b0
RK
12090 Set_Discrete_RM_Size (Def_Id);
12091
12092 -- Unconditionally delay the freeze, since we cannot set size
12093 -- information in all cases correctly until the freeze point.
12094
12095 Set_Has_Delayed_Freeze (Def_Id);
12096 end Constrain_Decimal;
12097
12098 ----------------------------------
12099 -- Constrain_Discriminated_Type --
12100 ----------------------------------
12101
12102 procedure Constrain_Discriminated_Type
12103 (Def_Id : Entity_Id;
12104 S : Node_Id;
12105 Related_Nod : Node_Id;
12106 For_Access : Boolean := False)
12107 is
07fc65c4 12108 E : constant Entity_Id := Entity (Subtype_Mark (S));
996ae0b0
RK
12109 T : Entity_Id;
12110 C : Node_Id;
12111 Elist : Elist_Id := New_Elmt_List;
12112
12113 procedure Fixup_Bad_Constraint;
12114 -- This is called after finding a bad constraint, and after having
12115 -- posted an appropriate error message. The mission is to leave the
a90bd866 12116 -- entity T in as reasonable state as possible.
996ae0b0 12117
fbf5a39b
AC
12118 --------------------------
12119 -- Fixup_Bad_Constraint --
12120 --------------------------
12121
996ae0b0
RK
12122 procedure Fixup_Bad_Constraint is
12123 begin
12124 -- Set a reasonable Ekind for the entity. For an incomplete type,
12125 -- we can't do much, but for other types, we can set the proper
12126 -- corresponding subtype kind.
12127
12128 if Ekind (T) = E_Incomplete_Type then
12129 Set_Ekind (Def_Id, Ekind (T));
12130 else
12131 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
12132 end if;
12133
491016e5
GD
12134 -- Set Etype to the known type, to reduce chances of cascaded errors
12135
12136 Set_Etype (Def_Id, E);
996ae0b0
RK
12137 Set_Error_Posted (Def_Id);
12138 end Fixup_Bad_Constraint;
12139
12140 -- Start of processing for Constrain_Discriminated_Type
12141
12142 begin
12143 C := Constraint (S);
12144
12145 -- A discriminant constraint is only allowed in a subtype indication,
12146 -- after a subtype mark. This subtype mark must denote either a type
12147 -- with discriminants, or an access type whose designated type is a
12148 -- type with discriminants. A discriminant constraint specifies the
12149 -- values of these discriminants (RM 3.7.2(5)).
12150
12151 T := Base_Type (Entity (Subtype_Mark (S)));
12152
12153 if Ekind (T) in Access_Kind then
12154 T := Designated_Type (T);
12155 end if;
12156
88b32fc3
BD
12157 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
12158 -- Avoid generating an error for access-to-incomplete subtypes.
12159
0791fbe9 12160 if Ada_Version >= Ada_2005
88b32fc3
BD
12161 and then Ekind (T) = E_Incomplete_Type
12162 and then Nkind (Parent (S)) = N_Subtype_Declaration
12163 and then not Is_Itype (Def_Id)
12164 then
12165 -- A little sanity check, emit an error message if the type
12166 -- has discriminants to begin with. Type T may be a regular
12167 -- incomplete type or imported via a limited with clause.
12168
12169 if Has_Discriminants (T)
7b56a91b
AC
12170 or else (From_Limited_With (T)
12171 and then Present (Non_Limited_View (T))
12172 and then Nkind (Parent (Non_Limited_View (T))) =
12173 N_Full_Type_Declaration
12174 and then Present (Discriminant_Specifications
12175 (Parent (Non_Limited_View (T)))))
88b32fc3
BD
12176 then
12177 Error_Msg_N
12178 ("(Ada 2005) incomplete subtype may not be constrained", C);
12179 else
ed2233dc 12180 Error_Msg_N ("invalid constraint: type has no discriminant", C);
88b32fc3
BD
12181 end if;
12182
12183 Fixup_Bad_Constraint;
12184 return;
12185
8a6a52dc
AC
12186 -- Check that the type has visible discriminants. The type may be
12187 -- a private type with unknown discriminants whose full view has
12188 -- discriminants which are invisible.
12189
88b32fc3 12190 elsif not Has_Discriminants (T)
8a6a52dc
AC
12191 or else
12192 (Has_Unknown_Discriminants (T)
12193 and then Is_Private_Type (T))
12194 then
996ae0b0
RK
12195 Error_Msg_N ("invalid constraint: type has no discriminant", C);
12196 Fixup_Bad_Constraint;
12197 return;
12198
07fc65c4
GB
12199 elsif Is_Constrained (E)
12200 or else (Ekind (E) = E_Class_Wide_Subtype
12201 and then Present (Discriminant_Constraint (E)))
12202 then
996ae0b0
RK
12203 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
12204 Fixup_Bad_Constraint;
12205 return;
12206 end if;
12207
12208 -- T may be an unconstrained subtype (e.g. a generic actual).
12209 -- Constraint applies to the base type.
12210
12211 T := Base_Type (T);
12212
12213 Elist := Build_Discriminant_Constraints (T, S);
12214
12215 -- If the list returned was empty we had an error in building the
12216 -- discriminant constraint. We have also already signalled an error
12217 -- in the incomplete type case
12218
12219 if Is_Empty_Elmt_List (Elist) then
12220 Fixup_Bad_Constraint;
12221 return;
12222 end if;
12223
12224 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
12225 end Constrain_Discriminated_Type;
12226
12227 ---------------------------
12228 -- Constrain_Enumeration --
12229 ---------------------------
12230
07fc65c4 12231 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
12232 T : constant Entity_Id := Entity (Subtype_Mark (S));
12233 C : constant Node_Id := Constraint (S);
12234
12235 begin
12236 Set_Ekind (Def_Id, E_Enumeration_Subtype);
12237
12238 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
12239
12240 Set_Etype (Def_Id, Base_Type (T));
12241 Set_Size_Info (Def_Id, (T));
12242 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12243 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
12244
07fc65c4 12245 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
996ae0b0
RK
12246
12247 Set_Discrete_RM_Size (Def_Id);
996ae0b0
RK
12248 end Constrain_Enumeration;
12249
12250 ----------------------
12251 -- Constrain_Float --
12252 ----------------------
12253
07fc65c4 12254 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
12255 T : constant Entity_Id := Entity (Subtype_Mark (S));
12256 C : Node_Id;
12257 D : Node_Id;
12258 Rais : Node_Id;
12259
12260 begin
12261 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
12262
12263 Set_Etype (Def_Id, Base_Type (T));
12264 Set_Size_Info (Def_Id, (T));
12265 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12266
12267 -- Process the constraint
12268
12269 C := Constraint (S);
12270
12271 -- Digits constraint present
12272
12273 if Nkind (C) = N_Digits_Constraint then
7ff2d234 12274
2ba431e5 12275 Check_SPARK_Restriction ("digits constraint is not allowed", S);
5f3ab6fb
AC
12276 Check_Restriction (No_Obsolescent_Features, C);
12277
fbf5a39b
AC
12278 if Warn_On_Obsolescent_Feature then
12279 Error_Msg_N
12280 ("subtype digits constraint is an " &
324ac540 12281 "obsolescent feature (RM J.3(8))?j?", C);
fbf5a39b
AC
12282 end if;
12283
996ae0b0
RK
12284 D := Digits_Expression (C);
12285 Analyze_And_Resolve (D, Any_Integer);
12286 Check_Digits_Expression (D);
12287 Set_Digits_Value (Def_Id, Expr_Value (D));
12288
12289 -- Check that digits value is in range. Obviously we can do this
12290 -- at compile time, but it is strictly a runtime check, and of
a90bd866 12291 -- course there is an ACVC test that checks this.
996ae0b0
RK
12292
12293 if Digits_Value (Def_Id) > Digits_Value (T) then
12294 Error_Msg_Uint_1 := Digits_Value (T);
324ac540 12295 Error_Msg_N ("??digits value is too large, maximum is ^", D);
07fc65c4
GB
12296 Rais :=
12297 Make_Raise_Constraint_Error (Sloc (D),
12298 Reason => CE_Range_Check_Failed);
996ae0b0
RK
12299 Insert_Action (Declaration_Node (Def_Id), Rais);
12300 end if;
12301
12302 C := Range_Constraint (C);
12303
12304 -- No digits constraint present
12305
12306 else
12307 Set_Digits_Value (Def_Id, Digits_Value (T));
12308 end if;
12309
12310 -- Range constraint present
12311
12312 if Nkind (C) = N_Range_Constraint then
07fc65c4 12313 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
996ae0b0
RK
12314
12315 -- No range constraint present
12316
12317 else
12318 pragma Assert (No (C));
12319 Set_Scalar_Range (Def_Id, Scalar_Range (T));
12320 end if;
12321
12322 Set_Is_Constrained (Def_Id);
12323 end Constrain_Float;
12324
12325 ---------------------
12326 -- Constrain_Index --
12327 ---------------------
12328
12329 procedure Constrain_Index
12330 (Index : Node_Id;
12331 S : Node_Id;
12332 Related_Nod : Node_Id;
12333 Related_Id : Entity_Id;
12334 Suffix : Character;
12335 Suffix_Index : Nat)
12336 is
7324bf49
AC
12337 Def_Id : Entity_Id;
12338 R : Node_Id := Empty;
12339 T : constant Entity_Id := Etype (Index);
996ae0b0
RK
12340
12341 begin
12342 if Nkind (S) = N_Range
fbf5a39b
AC
12343 or else
12344 (Nkind (S) = N_Attribute_Reference
12345 and then Attribute_Name (S) = Name_Range)
996ae0b0 12346 then
bd434b3f 12347 -- A Range attribute will be transformed into N_Range by Resolve
996ae0b0
RK
12348
12349 Analyze (S);
12350 Set_Etype (S, T);
12351 R := S;
12352
7324bf49 12353 Process_Range_Expr_In_Decl (R, T, Empty_List);
996ae0b0
RK
12354
12355 if not Error_Posted (S)
12356 and then
12357 (Nkind (S) /= N_Range
891a6e79
AC
12358 or else not Covers (T, (Etype (Low_Bound (S))))
12359 or else not Covers (T, (Etype (High_Bound (S)))))
996ae0b0
RK
12360 then
12361 if Base_Type (T) /= Any_Type
12362 and then Etype (Low_Bound (S)) /= Any_Type
12363 and then Etype (High_Bound (S)) /= Any_Type
12364 then
12365 Error_Msg_N ("range expected", S);
12366 end if;
12367 end if;
12368
12369 elsif Nkind (S) = N_Subtype_Indication then
71d9e9f2
ES
12370
12371 -- The parser has verified that this is a discrete indication
996ae0b0
RK
12372
12373 Resolve_Discrete_Subtype_Indication (S, T);
12374 R := Range_Expression (Constraint (S));
12375
4230bdb7
AC
12376 -- Capture values of bounds and generate temporaries for them if
12377 -- needed, since checks may cause duplication of the expressions
12378 -- which must not be reevaluated.
12379
f5da7a97
YM
12380 -- The forced evaluation removes side effects from expressions, which
12381 -- should occur also in GNATprove mode. Otherwise, we end up with
ef992452
AC
12382 -- unexpected insertions of actions at places where this is not
12383 -- supposed to occur, e.g. on default parameters of a call.
12384
f5da7a97 12385 if Expander_Active or GNATprove_Mode then
4230bdb7
AC
12386 Force_Evaluation (Low_Bound (R));
12387 Force_Evaluation (High_Bound (R));
12388 end if;
12389
996ae0b0
RK
12390 elsif Nkind (S) = N_Discriminant_Association then
12391
71d9e9f2 12392 -- Syntactically valid in subtype indication
996ae0b0
RK
12393
12394 Error_Msg_N ("invalid index constraint", S);
12395 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
12396 return;
12397
12398 -- Subtype_Mark case, no anonymous subtypes to construct
12399
12400 else
12401 Analyze (S);
12402
12403 if Is_Entity_Name (S) then
996ae0b0
RK
12404 if not Is_Type (Entity (S)) then
12405 Error_Msg_N ("expect subtype mark for index constraint", S);
12406
12407 elsif Base_Type (Entity (S)) /= Base_Type (T) then
12408 Wrong_Type (S, Base_Type (T));
ea034236
AC
12409
12410 -- Check error of subtype with predicate in index constraint
12411
ed00f472
RD
12412 else
12413 Bad_Predicated_Subtype_Use
12414 ("subtype& has predicate, not allowed in index constraint",
ea034236 12415 S, Entity (S));
996ae0b0
RK
12416 end if;
12417
12418 return;
12419
12420 else
12421 Error_Msg_N ("invalid index constraint", S);
12422 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
12423 return;
12424 end if;
12425 end if;
12426
12427 Def_Id :=
12428 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
12429
12430 Set_Etype (Def_Id, Base_Type (T));
12431
12432 if Is_Modular_Integer_Type (T) then
12433 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
12434
12435 elsif Is_Integer_Type (T) then
12436 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
12437
12438 else
12439 Set_Ekind (Def_Id, E_Enumeration_Subtype);
12440 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
3428cb9f 12441 Set_First_Literal (Def_Id, First_Literal (T));
996ae0b0
RK
12442 end if;
12443
12444 Set_Size_Info (Def_Id, (T));
12445 Set_RM_Size (Def_Id, RM_Size (T));
12446 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12447
996ae0b0
RK
12448 Set_Scalar_Range (Def_Id, R);
12449
12450 Set_Etype (S, Def_Id);
12451 Set_Discrete_RM_Size (Def_Id);
12452 end Constrain_Index;
12453
12454 -----------------------
12455 -- Constrain_Integer --
12456 -----------------------
12457
07fc65c4 12458 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
12459 T : constant Entity_Id := Entity (Subtype_Mark (S));
12460 C : constant Node_Id := Constraint (S);
12461
12462 begin
07fc65c4 12463 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
996ae0b0
RK
12464
12465 if Is_Modular_Integer_Type (T) then
12466 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
12467 else
12468 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
12469 end if;
12470
b69cd36a
AC
12471 Set_Etype (Def_Id, Base_Type (T));
12472 Set_Size_Info (Def_Id, (T));
12473 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
996ae0b0 12474 Set_Discrete_RM_Size (Def_Id);
996ae0b0
RK
12475 end Constrain_Integer;
12476
12477 ------------------------------
12478 -- Constrain_Ordinary_Fixed --
12479 ------------------------------
12480
07fc65c4 12481 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
996ae0b0
RK
12482 T : constant Entity_Id := Entity (Subtype_Mark (S));
12483 C : Node_Id;
12484 D : Node_Id;
12485 Rais : Node_Id;
12486
12487 begin
12488 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
b69cd36a
AC
12489 Set_Etype (Def_Id, Base_Type (T));
12490 Set_Size_Info (Def_Id, (T));
12491 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12492 Set_Small_Value (Def_Id, Small_Value (T));
996ae0b0
RK
12493
12494 -- Process the constraint
12495
12496 C := Constraint (S);
12497
12498 -- Delta constraint present
12499
12500 if Nkind (C) = N_Delta_Constraint then
7ff2d234 12501
2ba431e5 12502 Check_SPARK_Restriction ("delta constraint is not allowed", S);
5f3ab6fb
AC
12503 Check_Restriction (No_Obsolescent_Features, C);
12504
fbf5a39b
AC
12505 if Warn_On_Obsolescent_Feature then
12506 Error_Msg_S
12507 ("subtype delta constraint is an " &
324ac540 12508 "obsolescent feature (RM J.3(7))?j?");
fbf5a39b
AC
12509 end if;
12510
996ae0b0
RK
12511 D := Delta_Expression (C);
12512 Analyze_And_Resolve (D, Any_Real);
12513 Check_Delta_Expression (D);
12514 Set_Delta_Value (Def_Id, Expr_Value_R (D));
12515
12516 -- Check that delta value is in range. Obviously we can do this
12517 -- at compile time, but it is strictly a runtime check, and of
a90bd866 12518 -- course there is an ACVC test that checks this.
996ae0b0
RK
12519
12520 if Delta_Value (Def_Id) < Delta_Value (T) then
324ac540 12521 Error_Msg_N ("??delta value is too small", D);
07fc65c4
GB
12522 Rais :=
12523 Make_Raise_Constraint_Error (Sloc (D),
12524 Reason => CE_Range_Check_Failed);
996ae0b0
RK
12525 Insert_Action (Declaration_Node (Def_Id), Rais);
12526 end if;
12527
12528 C := Range_Constraint (C);
12529
12530 -- No delta constraint present
12531
12532 else
12533 Set_Delta_Value (Def_Id, Delta_Value (T));
12534 end if;
12535
12536 -- Range constraint present
12537
12538 if Nkind (C) = N_Range_Constraint then
07fc65c4 12539 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
996ae0b0
RK
12540
12541 -- No range constraint present
12542
12543 else
12544 pragma Assert (No (C));
12545 Set_Scalar_Range (Def_Id, Scalar_Range (T));
12546
12547 end if;
12548
12549 Set_Discrete_RM_Size (Def_Id);
12550
12551 -- Unconditionally delay the freeze, since we cannot set size
12552 -- information in all cases correctly until the freeze point.
12553
12554 Set_Has_Delayed_Freeze (Def_Id);
12555 end Constrain_Ordinary_Fixed;
12556
dc06abec
RD
12557 -----------------------
12558 -- Contain_Interface --
12559 -----------------------
12560
12561 function Contain_Interface
12562 (Iface : Entity_Id;
12563 Ifaces : Elist_Id) return Boolean
12564 is
12565 Iface_Elmt : Elmt_Id;
12566
12567 begin
12568 if Present (Ifaces) then
12569 Iface_Elmt := First_Elmt (Ifaces);
12570 while Present (Iface_Elmt) loop
12571 if Node (Iface_Elmt) = Iface then
12572 return True;
12573 end if;
12574
12575 Next_Elmt (Iface_Elmt);
12576 end loop;
12577 end if;
12578
12579 return False;
12580 end Contain_Interface;
12581
996ae0b0
RK
12582 ---------------------------
12583 -- Convert_Scalar_Bounds --
12584 ---------------------------
12585
12586 procedure Convert_Scalar_Bounds
12587 (N : Node_Id;
12588 Parent_Type : Entity_Id;
12589 Derived_Type : Entity_Id;
12590 Loc : Source_Ptr)
12591 is
12592 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
12593
12594 Lo : Node_Id;
12595 Hi : Node_Id;
12596 Rng : Node_Id;
12597
12598 begin
199c6a10
AC
12599 -- Defend against previous errors
12600
12601 if No (Scalar_Range (Derived_Type)) then
ee2ba856 12602 Check_Error_Detected;
199c6a10
AC
12603 return;
12604 end if;
12605
996ae0b0
RK
12606 Lo := Build_Scalar_Bound
12607 (Type_Low_Bound (Derived_Type),
07fc65c4 12608 Parent_Type, Implicit_Base);
996ae0b0
RK
12609
12610 Hi := Build_Scalar_Bound
12611 (Type_High_Bound (Derived_Type),
07fc65c4 12612 Parent_Type, Implicit_Base);
996ae0b0
RK
12613
12614 Rng :=
12615 Make_Range (Loc,
12616 Low_Bound => Lo,
12617 High_Bound => Hi);
12618
12619 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
12620
12621 Set_Parent (Rng, N);
12622 Set_Scalar_Range (Derived_Type, Rng);
12623
12624 -- Analyze the bounds
12625
12626 Analyze_And_Resolve (Lo, Implicit_Base);
12627 Analyze_And_Resolve (Hi, Implicit_Base);
12628
12629 -- Analyze the range itself, except that we do not analyze it if
12630 -- the bounds are real literals, and we have a fixed-point type.
12631 -- The reason for this is that we delay setting the bounds in this
12632 -- case till we know the final Small and Size values (see circuit
12633 -- in Freeze.Freeze_Fixed_Point_Type for further details).
12634
12635 if Is_Fixed_Point_Type (Parent_Type)
12636 and then Nkind (Lo) = N_Real_Literal
12637 and then Nkind (Hi) = N_Real_Literal
12638 then
12639 return;
12640
ffe9aba8 12641 -- Here we do the analysis of the range
996ae0b0
RK
12642
12643 -- Note: we do this manually, since if we do a normal Analyze and
12644 -- Resolve call, there are problems with the conversions used for
12645 -- the derived type range.
12646
12647 else
12648 Set_Etype (Rng, Implicit_Base);
12649 Set_Analyzed (Rng, True);
12650 end if;
12651 end Convert_Scalar_Bounds;
12652
12653 -------------------
12654 -- Copy_And_Swap --
12655 -------------------
12656
fbf5a39b 12657 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
996ae0b0
RK
12658 begin
12659 -- Initialize new full declaration entity by copying the pertinent
12660 -- fields of the corresponding private declaration entity.
12661
996ae0b0
RK
12662 -- We temporarily set Ekind to a value appropriate for a type to
12663 -- avoid assert failures in Einfo from checking for setting type
12664 -- attributes on something that is not a type. Ekind (Priv) is an
12665 -- appropriate choice, since it allowed the attributes to be set
12666 -- in the first place. This Ekind value will be modified later.
12667
12668 Set_Ekind (Full, Ekind (Priv));
12669
12670 -- Also set Etype temporarily to Any_Type, again, in the absence
12671 -- of errors, it will be properly reset, and if there are errors,
12672 -- then we want a value of Any_Type to remain.
12673
12674 Set_Etype (Full, Any_Type);
12675
12676 -- Now start copying attributes
12677
12678 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
12679
12680 if Has_Discriminants (Full) then
12681 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
fbf5a39b 12682 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
996ae0b0
RK
12683 end if;
12684
fbf5a39b 12685 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
996ae0b0
RK
12686 Set_Homonym (Full, Homonym (Priv));
12687 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
12688 Set_Is_Public (Full, Is_Public (Priv));
12689 Set_Is_Pure (Full, Is_Pure (Priv));
12690 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
4a214958 12691 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
fea9e956
ES
12692 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
12693 Set_Has_Pragma_Unreferenced_Objects
12694 (Full, Has_Pragma_Unreferenced_Objects
12695 (Priv));
996ae0b0
RK
12696
12697 Conditional_Delay (Full, Priv);
12698
12699 if Is_Tagged_Type (Full) then
ef2a63ba
JM
12700 Set_Direct_Primitive_Operations (Full,
12701 Direct_Primitive_Operations (Priv));
996ae0b0 12702
d347f572 12703 if Is_Base_Type (Priv) then
996ae0b0
RK
12704 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
12705 end if;
12706 end if;
12707
12708 Set_Is_Volatile (Full, Is_Volatile (Priv));
fbf5a39b 12709 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
996ae0b0
RK
12710 Set_Scope (Full, Scope (Priv));
12711 Set_Next_Entity (Full, Next_Entity (Priv));
12712 Set_First_Entity (Full, First_Entity (Priv));
12713 Set_Last_Entity (Full, Last_Entity (Priv));
12714
a5b62485
AC
12715 -- If access types have been recorded for later handling, keep them in
12716 -- the full view so that they get handled when the full view freeze
12717 -- node is expanded.
996ae0b0
RK
12718
12719 if Present (Freeze_Node (Priv))
12720 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
12721 then
12722 Ensure_Freeze_Node (Full);
fbf5a39b
AC
12723 Set_Access_Types_To_Process
12724 (Freeze_Node (Full),
12725 Access_Types_To_Process (Freeze_Node (Priv)));
996ae0b0 12726 end if;
996ae0b0 12727
308e6f3a
RW
12728 -- Swap the two entities. Now Private is the full type entity and Full
12729 -- is the private one. They will be swapped back at the end of the
12730 -- private part. This swapping ensures that the entity that is visible
12731 -- in the private part is the full declaration.
996ae0b0 12732
fbf5a39b
AC
12733 Exchange_Entities (Priv, Full);
12734 Append_Entity (Full, Scope (Full));
12735 end Copy_And_Swap;
996ae0b0 12736
fbf5a39b
AC
12737 -------------------------------------
12738 -- Copy_Array_Base_Type_Attributes --
12739 -------------------------------------
996ae0b0 12740
fbf5a39b
AC
12741 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
12742 begin
12743 Set_Component_Alignment (T1, Component_Alignment (T2));
12744 Set_Component_Type (T1, Component_Type (T2));
12745 Set_Component_Size (T1, Component_Size (T2));
12746 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
fbf5a39b
AC
12747 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
12748 Set_Has_Task (T1, Has_Task (T2));
12749 Set_Is_Packed (T1, Is_Packed (T2));
12750 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
12751 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
12752 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
12753 end Copy_Array_Base_Type_Attributes;
12754
12755 -----------------------------------
12756 -- Copy_Array_Subtype_Attributes --
12757 -----------------------------------
12758
12759 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
12760 begin
12761 Set_Size_Info (T1, T2);
12762
12763 Set_First_Index (T1, First_Index (T2));
12764 Set_Is_Aliased (T1, Is_Aliased (T2));
fbf5a39b
AC
12765 Set_Is_Volatile (T1, Is_Volatile (T2));
12766 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
12767 Set_Is_Constrained (T1, Is_Constrained (T2));
12768 Set_Depends_On_Private (T1, Has_Private_Component (T2));
12769 Set_First_Rep_Item (T1, First_Rep_Item (T2));
12770 Set_Convention (T1, Convention (T2));
12771 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
12772 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
ff7139c3 12773 Set_Packed_Array_Type (T1, Packed_Array_Type (T2));
fbf5a39b
AC
12774 end Copy_Array_Subtype_Attributes;
12775
12776 -----------------------------------
12777 -- Create_Constrained_Components --
12778 -----------------------------------
12779
12780 procedure Create_Constrained_Components
12781 (Subt : Entity_Id;
12782 Decl_Node : Node_Id;
12783 Typ : Entity_Id;
12784 Constraints : Elist_Id)
12785 is
12786 Loc : constant Source_Ptr := Sloc (Subt);
12787 Comp_List : constant Elist_Id := New_Elmt_List;
12788 Parent_Type : constant Entity_Id := Etype (Typ);
12789 Assoc_List : constant List_Id := New_List;
12790 Discr_Val : Elmt_Id;
12791 Errors : Boolean;
12792 New_C : Entity_Id;
12793 Old_C : Entity_Id;
12794 Is_Static : Boolean := True;
12795
12796 procedure Collect_Fixed_Components (Typ : Entity_Id);
0da2c8ac 12797 -- Collect parent type components that do not appear in a variant part
fbf5a39b
AC
12798
12799 procedure Create_All_Components;
ffe9aba8 12800 -- Iterate over Comp_List to create the components of the subtype
fbf5a39b
AC
12801
12802 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
12803 -- Creates a new component from Old_Compon, copying all the fields from
12804 -- it, including its Etype, inserts the new component in the Subt entity
12805 -- chain and returns the new component.
12806
12807 function Is_Variant_Record (T : Entity_Id) return Boolean;
12808 -- If true, and discriminants are static, collect only components from
12809 -- variants selected by discriminant values.
12810
12811 ------------------------------
996ae0b0
RK
12812 -- Collect_Fixed_Components --
12813 ------------------------------
12814
12815 procedure Collect_Fixed_Components (Typ : Entity_Id) is
12816 begin
a5b62485
AC
12817 -- Build association list for discriminants, and find components of the
12818 -- variant part selected by the values of the discriminants.
996ae0b0
RK
12819
12820 Old_C := First_Discriminant (Typ);
12821 Discr_Val := First_Elmt (Constraints);
996ae0b0
RK
12822 while Present (Old_C) loop
12823 Append_To (Assoc_List,
12824 Make_Component_Association (Loc,
12825 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
12826 Expression => New_Copy (Node (Discr_Val))));
12827
12828 Next_Elmt (Discr_Val);
12829 Next_Discriminant (Old_C);
12830 end loop;
12831
df3e68b1
HK
12832 -- The tag and the possible parent component are unconditionally in
12833 -- the subtype.
996ae0b0
RK
12834
12835 if Is_Tagged_Type (Typ)
12836 or else Has_Controlled_Component (Typ)
12837 then
12838 Old_C := First_Component (Typ);
996ae0b0 12839 while Present (Old_C) loop
b69cd36a 12840 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
996ae0b0
RK
12841 Append_Elmt (Old_C, Comp_List);
12842 end if;
12843
12844 Next_Component (Old_C);
12845 end loop;
12846 end if;
12847 end Collect_Fixed_Components;
12848
12849 ---------------------------
12850 -- Create_All_Components --
12851 ---------------------------
12852
12853 procedure Create_All_Components is
12854 Comp : Elmt_Id;
12855
12856 begin
12857 Comp := First_Elmt (Comp_List);
996ae0b0
RK
12858 while Present (Comp) loop
12859 Old_C := Node (Comp);
12860 New_C := Create_Component (Old_C);
12861
12862 Set_Etype
12863 (New_C,
12864 Constrain_Component_Type
c6823a20 12865 (Old_C, Subt, Decl_Node, Typ, Constraints));
996ae0b0
RK
12866 Set_Is_Public (New_C, Is_Public (Subt));
12867
12868 Next_Elmt (Comp);
12869 end loop;
12870 end Create_All_Components;
12871
12872 ----------------------
12873 -- Create_Component --
12874 ----------------------
12875
12876 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
fbf5a39b 12877 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
996ae0b0
RK
12878
12879 begin
c0bca7e1
EB
12880 if Ekind (Old_Compon) = E_Discriminant
12881 and then Is_Completely_Hidden (Old_Compon)
12882 then
c0bca7e1 12883 -- This is a shadow discriminant created for a discriminant of
c9e7bd8e
AC
12884 -- the parent type, which needs to be present in the subtype.
12885 -- Give the shadow discriminant an internal name that cannot
12886 -- conflict with that of visible components.
c0bca7e1
EB
12887
12888 Set_Chars (New_Compon, New_Internal_Name ('C'));
12889 end if;
12890
653da906
RD
12891 -- Set the parent so we have a proper link for freezing etc. This is
12892 -- not a real parent pointer, since of course our parent does not own
12893 -- up to us and reference us, we are an illegitimate child of the
a90bd866 12894 -- original parent.
996ae0b0
RK
12895
12896 Set_Parent (New_Compon, Parent (Old_Compon));
12897
653da906
RD
12898 -- If the old component's Esize was already determined and is a
12899 -- static value, then the new component simply inherits it. Otherwise
12900 -- the old component's size may require run-time determination, but
12901 -- the new component's size still might be statically determinable
12902 -- (if, for example it has a static constraint). In that case we want
12903 -- Layout_Type to recompute the component's size, so we reset its
12904 -- size and positional fields.
12905
12906 if Frontend_Layout_On_Target
12907 and then not Known_Static_Esize (Old_Compon)
12908 then
12909 Set_Esize (New_Compon, Uint_0);
12910 Init_Normalized_First_Bit (New_Compon);
12911 Init_Normalized_Position (New_Compon);
12912 Init_Normalized_Position_Max (New_Compon);
12913 end if;
12914
996ae0b0 12915 -- We do not want this node marked as Comes_From_Source, since
653da906
RD
12916 -- otherwise it would get first class status and a separate cross-
12917 -- reference line would be generated. Illegitimate children do not
12918 -- rate such recognition.
996ae0b0
RK
12919
12920 Set_Comes_From_Source (New_Compon, False);
12921
653da906
RD
12922 -- But it is a real entity, and a birth certificate must be properly
12923 -- registered by entering it into the entity list.
996ae0b0
RK
12924
12925 Enter_Name (New_Compon);
653da906 12926
996ae0b0
RK
12927 return New_Compon;
12928 end Create_Component;
12929
12930 -----------------------
12931 -- Is_Variant_Record --
12932 -----------------------
12933
12934 function Is_Variant_Record (T : Entity_Id) return Boolean is
12935 begin
12936 return Nkind (Parent (T)) = N_Full_Type_Declaration
12937 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
12938 and then Present (Component_List (Type_Definition (Parent (T))))
2b73cf68
JM
12939 and then
12940 Present
12941 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
996ae0b0
RK
12942 end Is_Variant_Record;
12943
12944 -- Start of processing for Create_Constrained_Components
12945
12946 begin
12947 pragma Assert (Subt /= Base_Type (Subt));
12948 pragma Assert (Typ = Base_Type (Typ));
12949
12950 Set_First_Entity (Subt, Empty);
12951 Set_Last_Entity (Subt, Empty);
12952
12953 -- Check whether constraint is fully static, in which case we can
12954 -- optimize the list of components.
12955
12956 Discr_Val := First_Elmt (Constraints);
996ae0b0 12957 while Present (Discr_Val) loop
996ae0b0
RK
12958 if not Is_OK_Static_Expression (Node (Discr_Val)) then
12959 Is_Static := False;
12960 exit;
12961 end if;
12962
12963 Next_Elmt (Discr_Val);
12964 end loop;
12965
88b32fc3
BD
12966 Set_Has_Static_Discriminants (Subt, Is_Static);
12967
2b73cf68 12968 Push_Scope (Subt);
996ae0b0 12969
71d9e9f2 12970 -- Inherit the discriminants of the parent type
996ae0b0 12971
c0bca7e1
EB
12972 Add_Discriminants : declare
12973 Num_Disc : Int;
12974 Num_Gird : Int;
12975
12976 begin
12977 Num_Disc := 0;
12978 Old_C := First_Discriminant (Typ);
12979
12980 while Present (Old_C) loop
12981 Num_Disc := Num_Disc + 1;
12982 New_C := Create_Component (Old_C);
12983 Set_Is_Public (New_C, Is_Public (Subt));
12984 Next_Discriminant (Old_C);
12985 end loop;
12986
12987 -- For an untagged derived subtype, the number of discriminants may
12988 -- be smaller than the number of inherited discriminants, because
c9e7bd8e
AC
12989 -- several of them may be renamed by a single new discriminant or
12990 -- constrained. In this case, add the hidden discriminants back into
12991 -- the subtype, because they need to be present if the optimizer of
12992 -- the GCC 4.x back-end decides to break apart assignments between
12993 -- objects using the parent view into member-wise assignments.
c0bca7e1
EB
12994
12995 Num_Gird := 0;
12996
12997 if Is_Derived_Type (Typ)
12998 and then not Is_Tagged_Type (Typ)
12999 then
13000 Old_C := First_Stored_Discriminant (Typ);
13001
13002 while Present (Old_C) loop
13003 Num_Gird := Num_Gird + 1;
13004 Next_Stored_Discriminant (Old_C);
13005 end loop;
13006 end if;
13007
13008 if Num_Gird > Num_Disc then
13009
13010 -- Find out multiple uses of new discriminants, and add hidden
13011 -- components for the extra renamed discriminants. We recognize
13012 -- multiple uses through the Corresponding_Discriminant of a
13013 -- new discriminant: if it constrains several old discriminants,
13014 -- this field points to the last one in the parent type. The
13015 -- stored discriminants of the derived type have the same name
13016 -- as those of the parent.
13017
13018 declare
13019 Constr : Elmt_Id;
13020 New_Discr : Entity_Id;
13021 Old_Discr : Entity_Id;
13022
13023 begin
13024 Constr := First_Elmt (Stored_Constraint (Typ));
13025 Old_Discr := First_Stored_Discriminant (Typ);
c0bca7e1
EB
13026 while Present (Constr) loop
13027 if Is_Entity_Name (Node (Constr))
13028 and then Ekind (Entity (Node (Constr))) = E_Discriminant
13029 then
13030 New_Discr := Entity (Node (Constr));
13031
dc06abec
RD
13032 if Chars (Corresponding_Discriminant (New_Discr)) /=
13033 Chars (Old_Discr)
c0bca7e1 13034 then
dc06abec
RD
13035 -- The new discriminant has been used to rename a
13036 -- subsequent old discriminant. Introduce a shadow
c0bca7e1
EB
13037 -- component for the current old discriminant.
13038
13039 New_C := Create_Component (Old_Discr);
c9e7bd8e 13040 Set_Original_Record_Component (New_C, Old_Discr);
c0bca7e1 13041 end if;
c9e7bd8e
AC
13042
13043 else
13044 -- The constraint has eliminated the old discriminant.
13045 -- Introduce a shadow component.
13046
13047 New_C := Create_Component (Old_Discr);
13048 Set_Original_Record_Component (New_C, Old_Discr);
c0bca7e1
EB
13049 end if;
13050
13051 Next_Elmt (Constr);
13052 Next_Stored_Discriminant (Old_Discr);
13053 end loop;
13054 end;
13055 end if;
13056 end Add_Discriminants;
996ae0b0
RK
13057
13058 if Is_Static
13059 and then Is_Variant_Record (Typ)
13060 then
13061 Collect_Fixed_Components (Typ);
13062
13063 Gather_Components (
13064 Typ,
13065 Component_List (Type_Definition (Parent (Typ))),
13066 Governed_By => Assoc_List,
13067 Into => Comp_List,
13068 Report_Errors => Errors);
13069 pragma Assert (not Errors);
13070
13071 Create_All_Components;
13072
13073 -- If the subtype declaration is created for a tagged type derivation
13074 -- with constraints, we retrieve the record definition of the parent
13075 -- type to select the components of the proper variant.
13076
13077 elsif Is_Static
13078 and then Is_Tagged_Type (Typ)
13079 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
13080 and then
13081 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
13082 and then Is_Variant_Record (Parent_Type)
13083 then
13084 Collect_Fixed_Components (Typ);
13085
13086 Gather_Components (
13087 Typ,
13088 Component_List (Type_Definition (Parent (Parent_Type))),
13089 Governed_By => Assoc_List,
13090 Into => Comp_List,
13091 Report_Errors => Errors);
13092 pragma Assert (not Errors);
13093
13094 -- If the tagged derivation has a type extension, collect all the
13095 -- new components therein.
13096
0da2c8ac
AC
13097 if Present
13098 (Record_Extension_Part (Type_Definition (Parent (Typ))))
996ae0b0
RK
13099 then
13100 Old_C := First_Component (Typ);
996ae0b0
RK
13101 while Present (Old_C) loop
13102 if Original_Record_Component (Old_C) = Old_C
13103 and then Chars (Old_C) /= Name_uTag
13104 and then Chars (Old_C) /= Name_uParent
996ae0b0
RK
13105 then
13106 Append_Elmt (Old_C, Comp_List);
13107 end if;
13108
13109 Next_Component (Old_C);
13110 end loop;
13111 end if;
13112
13113 Create_All_Components;
13114
13115 else
9dfd2ff8
CC
13116 -- If discriminants are not static, or if this is a multi-level type
13117 -- extension, we have to include all components of the parent type.
996ae0b0
RK
13118
13119 Old_C := First_Component (Typ);
996ae0b0
RK
13120 while Present (Old_C) loop
13121 New_C := Create_Component (Old_C);
13122
13123 Set_Etype
13124 (New_C,
13125 Constrain_Component_Type
c6823a20 13126 (Old_C, Subt, Decl_Node, Typ, Constraints));
996ae0b0
RK
13127 Set_Is_Public (New_C, Is_Public (Subt));
13128
13129 Next_Component (Old_C);
13130 end loop;
13131 end if;
13132
13133 End_Scope;
13134 end Create_Constrained_Components;
13135
13136 ------------------------------------------
13137 -- Decimal_Fixed_Point_Type_Declaration --
13138 ------------------------------------------
13139
13140 procedure Decimal_Fixed_Point_Type_Declaration
13141 (T : Entity_Id;
13142 Def : Node_Id)
13143 is
13144 Loc : constant Source_Ptr := Sloc (Def);
13145 Digs_Expr : constant Node_Id := Digits_Expression (Def);
13146 Delta_Expr : constant Node_Id := Delta_Expression (Def);
13147 Implicit_Base : Entity_Id;
13148 Digs_Val : Uint;
13149 Delta_Val : Ureal;
13150 Scale_Val : Uint;
13151 Bound_Val : Ureal;
13152
996ae0b0 13153 begin
2ba431e5 13154 Check_SPARK_Restriction
fe5d3068 13155 ("decimal fixed point type is not allowed", Def);
996ae0b0
RK
13156 Check_Restriction (No_Fixed_Point, Def);
13157
13158 -- Create implicit base type
13159
13160 Implicit_Base :=
13161 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
13162 Set_Etype (Implicit_Base, Implicit_Base);
13163
13164 -- Analyze and process delta expression
13165
13166 Analyze_And_Resolve (Delta_Expr, Universal_Real);
13167
13168 Check_Delta_Expression (Delta_Expr);
13169 Delta_Val := Expr_Value_R (Delta_Expr);
13170
13171 -- Check delta is power of 10, and determine scale value from it
13172
13173 declare
9dfd2ff8 13174 Val : Ureal;
996ae0b0
RK
13175
13176 begin
13177 Scale_Val := Uint_0;
9dfd2ff8 13178 Val := Delta_Val;
996ae0b0
RK
13179
13180 if Val < Ureal_1 then
13181 while Val < Ureal_1 loop
13182 Val := Val * Ureal_10;
13183 Scale_Val := Scale_Val + 1;
13184 end loop;
13185
13186 if Scale_Val > 18 then
13187 Error_Msg_N ("scale exceeds maximum value of 18", Def);
13188 Scale_Val := UI_From_Int (+18);
13189 end if;
13190
13191 else
13192 while Val > Ureal_1 loop
13193 Val := Val / Ureal_10;
13194 Scale_Val := Scale_Val - 1;
13195 end loop;
13196
13197 if Scale_Val < -18 then
13198 Error_Msg_N ("scale is less than minimum value of -18", Def);
13199 Scale_Val := UI_From_Int (-18);
13200 end if;
13201 end if;
13202
13203 if Val /= Ureal_1 then
13204 Error_Msg_N ("delta expression must be a power of 10", Def);
13205 Delta_Val := Ureal_10 ** (-Scale_Val);
13206 end if;
13207 end;
13208
13209 -- Set delta, scale and small (small = delta for decimal type)
13210
13211 Set_Delta_Value (Implicit_Base, Delta_Val);
13212 Set_Scale_Value (Implicit_Base, Scale_Val);
13213 Set_Small_Value (Implicit_Base, Delta_Val);
13214
13215 -- Analyze and process digits expression
13216
13217 Analyze_And_Resolve (Digs_Expr, Any_Integer);
13218 Check_Digits_Expression (Digs_Expr);
13219 Digs_Val := Expr_Value (Digs_Expr);
13220
13221 if Digs_Val > 18 then
13222 Digs_Val := UI_From_Int (+18);
13223 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
13224 end if;
13225
13226 Set_Digits_Value (Implicit_Base, Digs_Val);
13227 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
13228
13229 -- Set range of base type from digits value for now. This will be
13230 -- expanded to represent the true underlying base range by Freeze.
13231
13232 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
13233
ce4a6e84
RD
13234 -- Note: We leave size as zero for now, size will be set at freeze
13235 -- time. We have to do this for ordinary fixed-point, because the size
13236 -- depends on the specified small, and we might as well do the same for
13237 -- decimal fixed-point.
996ae0b0 13238
ce4a6e84 13239 pragma Assert (Esize (Implicit_Base) = Uint_0);
996ae0b0 13240
996ae0b0
RK
13241 -- If there are bounds given in the declaration use them as the
13242 -- bounds of the first named subtype.
13243
13244 if Present (Real_Range_Specification (Def)) then
13245 declare
13246 RRS : constant Node_Id := Real_Range_Specification (Def);
13247 Low : constant Node_Id := Low_Bound (RRS);
13248 High : constant Node_Id := High_Bound (RRS);
13249 Low_Val : Ureal;
13250 High_Val : Ureal;
13251
13252 begin
13253 Analyze_And_Resolve (Low, Any_Real);
13254 Analyze_And_Resolve (High, Any_Real);
13255 Check_Real_Bound (Low);
13256 Check_Real_Bound (High);
13257 Low_Val := Expr_Value_R (Low);
13258 High_Val := Expr_Value_R (High);
13259
13260 if Low_Val < (-Bound_Val) then
13261 Error_Msg_N
13262 ("range low bound too small for digits value", Low);
13263 Low_Val := -Bound_Val;
13264 end if;
13265
13266 if High_Val > Bound_Val then
13267 Error_Msg_N
13268 ("range high bound too large for digits value", High);
13269 High_Val := Bound_Val;
13270 end if;
13271
13272 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
13273 end;
13274
13275 -- If no explicit range, use range that corresponds to given
13276 -- digits value. This will end up as the final range for the
13277 -- first subtype.
13278
13279 else
13280 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
13281 end if;
13282
c45b6ae0
AC
13283 -- Complete entity for first subtype
13284
13285 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
13286 Set_Etype (T, Implicit_Base);
13287 Set_Size_Info (T, Implicit_Base);
13288 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13289 Set_Digits_Value (T, Digs_Val);
13290 Set_Delta_Value (T, Delta_Val);
13291 Set_Small_Value (T, Delta_Val);
13292 Set_Scale_Value (T, Scale_Val);
13293 Set_Is_Constrained (T);
996ae0b0
RK
13294 end Decimal_Fixed_Point_Type_Declaration;
13295
ce2b6ba5
JM
13296 -----------------------------------
13297 -- Derive_Progenitor_Subprograms --
13298 -----------------------------------
758c442c 13299
ce2b6ba5 13300 procedure Derive_Progenitor_Subprograms
88b32fc3 13301 (Parent_Type : Entity_Id;
ce2b6ba5 13302 Tagged_Type : Entity_Id)
88b32fc3 13303 is
ce2b6ba5
JM
13304 E : Entity_Id;
13305 Elmt : Elmt_Id;
13306 Iface : Entity_Id;
13307 Iface_Elmt : Elmt_Id;
13308 Iface_Subp : Entity_Id;
13309 New_Subp : Entity_Id := Empty;
13310 Prim_Elmt : Elmt_Id;
13311 Subp : Entity_Id;
13312 Typ : Entity_Id;
758c442c 13313
ce2b6ba5 13314 begin
0791fbe9 13315 pragma Assert (Ada_Version >= Ada_2005
ce2b6ba5
JM
13316 and then Is_Record_Type (Tagged_Type)
13317 and then Is_Tagged_Type (Tagged_Type)
13318 and then Has_Interfaces (Tagged_Type));
13319
30783513 13320 -- Step 1: Transfer to the full-view primitives associated with the
ce2b6ba5
JM
13321 -- partial-view that cover interface primitives. Conceptually this
13322 -- work should be done later by Process_Full_View; done here to
13323 -- simplify its implementation at later stages. It can be safely
13324 -- done here because interfaces must be visible in the partial and
13325 -- private view (RM 7.3(7.3/2)).
13326
0cc71b48
AC
13327 -- Small optimization: This work is only required if the parent may
13328 -- have entities whose Alias attribute reference an interface primitive.
13329 -- Such a situation may occur if the parent is an abstract type and the
13330 -- primitive has not been yet overridden or if the parent is a generic
13331 -- formal type covering interfaces.
13332
13333 -- If the tagged type is not abstract, it cannot have abstract
13334 -- primitives (the only entities in the list of primitives of
13335 -- non-abstract tagged types that can reference abstract primitives
13336 -- through its Alias attribute are the internal entities that have
13337 -- attribute Interface_Alias, and these entities are generated later
13338 -- by Add_Internal_Interface_Entities).
88b32fc3 13339
ce2b6ba5 13340 if In_Private_Part (Current_Scope)
f0b741b6 13341 and then (Is_Abstract_Type (Parent_Type)
0cc71b48
AC
13342 or else
13343 Is_Generic_Type (Parent_Type))
ce2b6ba5
JM
13344 then
13345 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
13346 while Present (Elmt) loop
13347 Subp := Node (Elmt);
88b32fc3 13348
ce2b6ba5 13349 -- At this stage it is not possible to have entities in the list
0cc71b48 13350 -- of primitives that have attribute Interface_Alias.
758c442c 13351
ce2b6ba5 13352 pragma Assert (No (Interface_Alias (Subp)));
758c442c 13353
ce2b6ba5 13354 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
88b32fc3 13355
ce2b6ba5
JM
13356 if Is_Interface (Typ) then
13357 E := Find_Primitive_Covering_Interface
13358 (Tagged_Type => Tagged_Type,
13359 Iface_Prim => Subp);
88b32fc3 13360
ce2b6ba5
JM
13361 if Present (E)
13362 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
13363 then
13364 Replace_Elmt (Elmt, E);
13365 Remove_Homonym (Subp);
950d3e7d 13366 end if;
88b32fc3
BD
13367 end if;
13368
13369 Next_Elmt (Elmt);
13370 end loop;
88b32fc3
BD
13371 end if;
13372
ce2b6ba5 13373 -- Step 2: Add primitives of progenitors that are not implemented by
0cc71b48 13374 -- parents of Tagged_Type.
88b32fc3 13375
59262ebb
AC
13376 if Present (Interfaces (Base_Type (Tagged_Type))) then
13377 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
ce2b6ba5
JM
13378 while Present (Iface_Elmt) loop
13379 Iface := Node (Iface_Elmt);
88b32fc3 13380
ce2b6ba5
JM
13381 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
13382 while Present (Prim_Elmt) loop
13383 Iface_Subp := Node (Prim_Elmt);
fea9e956 13384
9800ef59 13385 -- Exclude derivation of predefined primitives except those
3128f955
AC
13386 -- that come from source, or are inherited from one that comes
13387 -- from source. Required to catch declarations of equality
13388 -- operators of interfaces. For example:
9800ef59
JM
13389
13390 -- type Iface is interface;
13391 -- function "=" (Left, Right : Iface) return Boolean;
13392
8c3dd7a8 13393 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
3128f955 13394 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
8c3dd7a8 13395 then
ce2b6ba5
JM
13396 E := Find_Primitive_Covering_Interface
13397 (Tagged_Type => Tagged_Type,
13398 Iface_Prim => Iface_Subp);
88b32fc3 13399
ce2b6ba5 13400 -- If not found we derive a new primitive leaving its alias
0cc71b48 13401 -- attribute referencing the interface primitive.
88b32fc3 13402
ce2b6ba5
JM
13403 if No (E) then
13404 Derive_Subprogram
13405 (New_Subp, Iface_Subp, Tagged_Type, Iface);
88b32fc3 13406
ce09f8b3
AC
13407 -- Ada 2012 (AI05-0197): If the covering primitive's name
13408 -- differs from the name of the interface primitive then it
13409 -- is a private primitive inherited from a parent type. In
13410 -- such case, given that Tagged_Type covers the interface,
13411 -- the inherited private primitive becomes visible. For such
13412 -- purpose we add a new entity that renames the inherited
13413 -- private primitive.
13414
13415 elsif Chars (E) /= Chars (Iface_Subp) then
13416 pragma Assert (Has_Suffix (E, 'P'));
13417 Derive_Subprogram
13418 (New_Subp, Iface_Subp, Tagged_Type, Iface);
13419 Set_Alias (New_Subp, E);
13420 Set_Is_Abstract_Subprogram (New_Subp,
13421 Is_Abstract_Subprogram (E));
13422
ce2b6ba5 13423 -- Propagate to the full view interface entities associated
0cc71b48 13424 -- with the partial view.
88b32fc3 13425
ce2b6ba5
JM
13426 elsif In_Private_Part (Current_Scope)
13427 and then Present (Alias (E))
13428 and then Alias (E) = Iface_Subp
13429 and then
13430 List_Containing (Parent (E)) /=
13431 Private_Declarations
13432 (Specification
13433 (Unit_Declaration_Node (Current_Scope)))
13434 then
13435 Append_Elmt (E, Primitive_Operations (Tagged_Type));
13436 end if;
88b32fc3
BD
13437 end if;
13438
ce2b6ba5 13439 Next_Elmt (Prim_Elmt);
88b32fc3
BD
13440 end loop;
13441
ce2b6ba5 13442 Next_Elmt (Iface_Elmt);
88b32fc3
BD
13443 end loop;
13444 end if;
ce2b6ba5 13445 end Derive_Progenitor_Subprograms;
758c442c 13446
996ae0b0
RK
13447 -----------------------
13448 -- Derive_Subprogram --
13449 -----------------------
13450
13451 procedure Derive_Subprogram
13452 (New_Subp : in out Entity_Id;
13453 Parent_Subp : Entity_Id;
13454 Derived_Type : Entity_Id;
13455 Parent_Type : Entity_Id;
13456 Actual_Subp : Entity_Id := Empty)
13457 is
ce4a6e84
RD
13458 Formal : Entity_Id;
13459 -- Formal parameter of parent primitive operation
13460
13461 Formal_Of_Actual : Entity_Id;
13462 -- Formal parameter of actual operation, when the derivation is to
13463 -- create a renaming for a primitive operation of an actual in an
13464 -- instantiation.
13465
13466 New_Formal : Entity_Id;
13467 -- Formal of inherited operation
13468
fbf5a39b 13469 Visible_Subp : Entity_Id := Parent_Subp;
996ae0b0
RK
13470
13471 function Is_Private_Overriding return Boolean;
ce4a6e84
RD
13472 -- If Subp is a private overriding of a visible operation, the inherited
13473 -- operation derives from the overridden op (even though its body is the
13474 -- overriding one) and the inherited operation is visible now. See
13475 -- sem_disp to see the full details of the handling of the overridden
13476 -- subprogram, which is removed from the list of primitive operations of
13477 -- the type. The overridden subprogram is saved locally in Visible_Subp,
13478 -- and used to diagnose abstract operations that need overriding in the
13479 -- derived type.
996ae0b0
RK
13480
13481 procedure Replace_Type (Id, New_Id : Entity_Id);
13482 -- When the type is an anonymous access type, create a new access type
13483 -- designating the derived type.
13484
fbf5a39b
AC
13485 procedure Set_Derived_Name;
13486 -- This procedure sets the appropriate Chars name for New_Subp. This
13487 -- is normally just a copy of the parent name. An exception arises for
13488 -- type support subprograms, where the name is changed to reflect the
13489 -- name of the derived type, e.g. if type foo is derived from type bar,
13490 -- then a procedure barDA is derived with a name fooDA.
13491
996ae0b0
RK
13492 ---------------------------
13493 -- Is_Private_Overriding --
13494 ---------------------------
13495
13496 function Is_Private_Overriding return Boolean is
13497 Prev : Entity_Id;
13498
13499 begin
88b32fc3
BD
13500 -- If the parent is not a dispatching operation there is no
13501 -- need to investigate overridings
13502
13503 if not Is_Dispatching_Operation (Parent_Subp) then
13504 return False;
13505 end if;
13506
9dfd2ff8 13507 -- The visible operation that is overridden is a homonym of the
a5b62485
AC
13508 -- parent subprogram. We scan the homonym chain to find the one
13509 -- whose alias is the subprogram we are deriving.
996ae0b0 13510
9dfd2ff8 13511 Prev := Current_Entity (Parent_Subp);
996ae0b0 13512 while Present (Prev) loop
88b32fc3 13513 if Ekind (Prev) = Ekind (Parent_Subp)
996ae0b0
RK
13514 and then Alias (Prev) = Parent_Subp
13515 and then Scope (Parent_Subp) = Scope (Prev)
88b32fc3 13516 and then not Is_Hidden (Prev)
996ae0b0 13517 then
fbf5a39b 13518 Visible_Subp := Prev;
996ae0b0
RK
13519 return True;
13520 end if;
13521
13522 Prev := Homonym (Prev);
13523 end loop;
13524
13525 return False;
13526 end Is_Private_Overriding;
13527
13528 ------------------
13529 -- Replace_Type --
13530 ------------------
13531
13532 procedure Replace_Type (Id, New_Id : Entity_Id) is
13533 Acc_Type : Entity_Id;
0da2c8ac 13534 Par : constant Node_Id := Parent (Derived_Type);
996ae0b0
RK
13535
13536 begin
13537 -- When the type is an anonymous access type, create a new access
13538 -- type designating the derived type. This itype must be elaborated
13539 -- at the point of the derivation, not on subsequent calls that may
13540 -- be out of the proper scope for Gigi, so we insert a reference to
13541 -- it after the derivation.
13542
13543 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
13544 declare
13545 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
13546
13547 begin
13548 if Ekind (Desig_Typ) = E_Record_Type_With_Private
13549 and then Present (Full_View (Desig_Typ))
13550 and then not Is_Private_Type (Parent_Type)
13551 then
13552 Desig_Typ := Full_View (Desig_Typ);
13553 end if;
13554
88b32fc3
BD
13555 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
13556
13557 -- Ada 2005 (AI-251): Handle also derivations of abstract
13558 -- interface primitives.
13559
13560 or else (Is_Interface (Desig_Typ)
13561 and then not Is_Class_Wide_Type (Desig_Typ))
13562 then
996ae0b0
RK
13563 Acc_Type := New_Copy (Etype (Id));
13564 Set_Etype (Acc_Type, Acc_Type);
13565 Set_Scope (Acc_Type, New_Subp);
13566
71d9e9f2 13567 -- Compute size of anonymous access type
996ae0b0
RK
13568
13569 if Is_Array_Type (Desig_Typ)
13570 and then not Is_Constrained (Desig_Typ)
13571 then
13572 Init_Size (Acc_Type, 2 * System_Address_Size);
13573 else
13574 Init_Size (Acc_Type, System_Address_Size);
13575 end if;
13576
13577 Init_Alignment (Acc_Type);
996ae0b0
RK
13578 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
13579
13580 Set_Etype (New_Id, Acc_Type);
13581 Set_Scope (New_Id, New_Subp);
13582
0da2c8ac 13583 -- Create a reference to it
fea9e956 13584 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
996ae0b0
RK
13585
13586 else
13587 Set_Etype (New_Id, Etype (Id));
13588 end if;
13589 end;
0da2c8ac 13590
996ae0b0
RK
13591 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
13592 or else
13593 (Ekind (Etype (Id)) = E_Record_Type_With_Private
13594 and then Present (Full_View (Etype (Id)))
0da2c8ac
AC
13595 and then
13596 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
996ae0b0 13597 then
996ae0b0
RK
13598 -- Constraint checks on formals are generated during expansion,
13599 -- based on the signature of the original subprogram. The bounds
13600 -- of the derived type are not relevant, and thus we can use
13601 -- the base type for the formals. However, the return type may be
13602 -- used in a context that requires that the proper static bounds
13603 -- be used (a case statement, for example) and for those cases
13604 -- we must use the derived type (first subtype), not its base.
13605
0da2c8ac
AC
13606 -- If the derived_type_definition has no constraints, we know that
13607 -- the derived type has the same constraints as the first subtype
13608 -- of the parent, and we can also use it rather than its base,
13609 -- which can lead to more efficient code.
13610
13611 if Etype (Id) = Parent_Type then
13612 if Is_Scalar_Type (Parent_Type)
13613 and then
13614 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
13615 then
13616 Set_Etype (New_Id, Derived_Type);
13617
13618 elsif Nkind (Par) = N_Full_Type_Declaration
13619 and then
13620 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
13621 and then
13622 Is_Entity_Name
13623 (Subtype_Indication (Type_Definition (Par)))
13624 then
13625 Set_Etype (New_Id, Derived_Type);
13626
13627 else
13628 Set_Etype (New_Id, Base_Type (Derived_Type));
13629 end if;
13630
996ae0b0
RK
13631 else
13632 Set_Etype (New_Id, Base_Type (Derived_Type));
13633 end if;
13634
13635 else
13636 Set_Etype (New_Id, Etype (Id));
13637 end if;
13638 end Replace_Type;
13639
fbf5a39b
AC
13640 ----------------------
13641 -- Set_Derived_Name --
13642 ----------------------
13643
13644 procedure Set_Derived_Name is
13645 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
13646 begin
13647 if Nm = TSS_Null then
13648 Set_Chars (New_Subp, Chars (Parent_Subp));
13649 else
13650 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
13651 end if;
13652 end Set_Derived_Name;
13653
996ae0b0
RK
13654 -- Start of processing for Derive_Subprogram
13655
13656 begin
13657 New_Subp :=
13658 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
13659 Set_Ekind (New_Subp, Ekind (Parent_Subp));
dac3bede 13660 Set_Contract (New_Subp, Make_Contract (Sloc (New_Subp)));
996ae0b0
RK
13661
13662 -- Check whether the inherited subprogram is a private operation that
13663 -- should be inherited but not yet made visible. Such subprograms can
13664 -- become visible at a later point (e.g., the private part of a public
13665 -- child unit) via Declare_Inherited_Private_Subprograms. If the
13666 -- following predicate is true, then this is not such a private
13667 -- operation and the subprogram simply inherits the name of the parent
13668 -- subprogram. Note the special check for the names of controlled
13669 -- operations, which are currently exempted from being inherited with
13670 -- a hidden name because they must be findable for generation of
13671 -- implicit run-time calls.
13672
13673 if not Is_Hidden (Parent_Subp)
13674 or else Is_Internal (Parent_Subp)
13675 or else Is_Private_Overriding
13676 or else Is_Internal_Name (Chars (Parent_Subp))
b69cd36a
AC
13677 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
13678 Name_Adjust,
13679 Name_Finalize)
996ae0b0 13680 then
fbf5a39b 13681 Set_Derived_Name;
996ae0b0 13682
af268547
ES
13683 -- An inherited dispatching equality will be overridden by an internally
13684 -- generated one, or by an explicit one, so preserve its name and thus
13685 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
13686 -- private operation it may become invisible if the full view has
13687 -- progenitors, and the dispatch table will be malformed.
13688 -- We check that the type is limited to handle the anomalous declaration
13689 -- of Limited_Controlled, which is derived from a non-limited type, and
13690 -- which is handled specially elsewhere as well.
13691
13692 elsif Chars (Parent_Subp) = Name_Op_Eq
13693 and then Is_Dispatching_Operation (Parent_Subp)
13694 and then Etype (Parent_Subp) = Standard_Boolean
c0985d4e 13695 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
af268547 13696 and then
c0985d4e
HK
13697 Etype (First_Formal (Parent_Subp)) =
13698 Etype (Next_Formal (First_Formal (Parent_Subp)))
af268547
ES
13699 then
13700 Set_Derived_Name;
13701
996ae0b0
RK
13702 -- If parent is hidden, this can be a regular derivation if the
13703 -- parent is immediately visible in a non-instantiating context,
13704 -- or if we are in the private part of an instance. This test
13705 -- should still be refined ???
13706
a5b62485
AC
13707 -- The test for In_Instance_Not_Visible avoids inheriting the derived
13708 -- operation as a non-visible operation in cases where the parent
13709 -- subprogram might not be visible now, but was visible within the
13710 -- original generic, so it would be wrong to make the inherited
13711 -- subprogram non-visible now. (Not clear if this test is fully
13712 -- correct; are there any cases where we should declare the inherited
13713 -- operation as not visible to avoid it being overridden, e.g., when
13714 -- the parent type is a generic actual with private primitives ???)
996ae0b0
RK
13715
13716 -- (they should be treated the same as other private inherited
13717 -- subprograms, but it's not clear how to do this cleanly). ???
13718
13719 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
13720 and then Is_Immediately_Visible (Parent_Subp)
13721 and then not In_Instance)
13722 or else In_Instance_Not_Visible
13723 then
fbf5a39b 13724 Set_Derived_Name;
996ae0b0 13725
ce2b6ba5
JM
13726 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
13727 -- overrides an interface primitive because interface primitives
13728 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
88b32fc3 13729
0791fbe9 13730 elsif Ada_Version >= Ada_2005
0052da20
JM
13731 and then Is_Dispatching_Operation (Parent_Subp)
13732 and then Covers_Some_Interface (Parent_Subp)
13733 then
88b32fc3
BD
13734 Set_Derived_Name;
13735
af268547 13736 -- Otherwise, the type is inheriting a private operation, so enter
996ae0b0
RK
13737 -- it with a special name so it can't be overridden.
13738
13739 else
13740 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
13741 end if;
13742
13743 Set_Parent (New_Subp, Parent (Derived_Type));
ce4a6e84
RD
13744
13745 if Present (Actual_Subp) then
13746 Replace_Type (Actual_Subp, New_Subp);
13747 else
13748 Replace_Type (Parent_Subp, New_Subp);
13749 end if;
13750
996ae0b0
RK
13751 Conditional_Delay (New_Subp, Parent_Subp);
13752
ce4a6e84
RD
13753 -- If we are creating a renaming for a primitive operation of an
13754 -- actual of a generic derived type, we must examine the signature
f3d0f304 13755 -- of the actual primitive, not that of the generic formal, which for
ce4a6e84
RD
13756 -- example may be an interface. However the name and initial value
13757 -- of the inherited operation are those of the formal primitive.
13758
996ae0b0 13759 Formal := First_Formal (Parent_Subp);
ce4a6e84
RD
13760
13761 if Present (Actual_Subp) then
13762 Formal_Of_Actual := First_Formal (Actual_Subp);
13763 else
13764 Formal_Of_Actual := Empty;
13765 end if;
13766
996ae0b0
RK
13767 while Present (Formal) loop
13768 New_Formal := New_Copy (Formal);
13769
13770 -- Normally we do not go copying parents, but in the case of
a5b62485
AC
13771 -- formals, we need to link up to the declaration (which is the
13772 -- parameter specification), and it is fine to link up to the
13773 -- original formal's parameter specification in this case.
996ae0b0
RK
13774
13775 Set_Parent (New_Formal, Parent (Formal));
996ae0b0
RK
13776 Append_Entity (New_Formal, New_Subp);
13777
ce4a6e84
RD
13778 if Present (Formal_Of_Actual) then
13779 Replace_Type (Formal_Of_Actual, New_Formal);
13780 Next_Formal (Formal_Of_Actual);
13781 else
13782 Replace_Type (Formal, New_Formal);
13783 end if;
13784
996ae0b0
RK
13785 Next_Formal (Formal);
13786 end loop;
13787
13788 -- If this derivation corresponds to a tagged generic actual, then
13789 -- primitive operations rename those of the actual. Otherwise the
ce4a6e84
RD
13790 -- primitive operations rename those of the parent type, If the parent
13791 -- renames an intrinsic operator, so does the new subprogram. We except
13792 -- concatenation, which is always properly typed, and does not get
13793 -- expanded as other intrinsic operations.
996ae0b0
RK
13794
13795 if No (Actual_Subp) then
fbf5a39b
AC
13796 if Is_Intrinsic_Subprogram (Parent_Subp) then
13797 Set_Is_Intrinsic_Subprogram (New_Subp);
13798
13799 if Present (Alias (Parent_Subp))
13800 and then Chars (Parent_Subp) /= Name_Op_Concat
13801 then
13802 Set_Alias (New_Subp, Alias (Parent_Subp));
13803 else
13804 Set_Alias (New_Subp, Parent_Subp);
13805 end if;
13806
13807 else
13808 Set_Alias (New_Subp, Parent_Subp);
13809 end if;
996ae0b0
RK
13810
13811 else
13812 Set_Alias (New_Subp, Actual_Subp);
13813 end if;
13814
13815 -- Derived subprograms of a tagged type must inherit the convention
13816 -- of the parent subprogram (a requirement of AI-117). Derived
13817 -- subprograms of untagged types simply get convention Ada by default.
13818
1824c168
AC
13819 -- If the derived type is a tagged generic formal type with unknown
13820 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
13821
13822 -- However, if the type is derived from a generic formal, the further
13823 -- inherited subprogram has the convention of the non-generic ancestor.
13824 -- Otherwise there would be no way to override the operation.
13825 -- (This is subject to forthcoming ARG discussions).
13826
996ae0b0 13827 if Is_Tagged_Type (Derived_Type) then
1824c168
AC
13828 if Is_Generic_Type (Derived_Type)
13829 and then Has_Unknown_Discriminants (Derived_Type)
13830 then
13831 Set_Convention (New_Subp, Convention_Intrinsic);
13832
13833 else
13834 if Is_Generic_Type (Parent_Type)
13835 and then Has_Unknown_Discriminants (Parent_Type)
13836 then
13837 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
13838 else
13839 Set_Convention (New_Subp, Convention (Parent_Subp));
13840 end if;
13841 end if;
996ae0b0
RK
13842 end if;
13843
fd0d899b
AC
13844 -- Predefined controlled operations retain their name even if the parent
13845 -- is hidden (see above), but they are not primitive operations if the
13846 -- ancestor is not visible, for example if the parent is a private
13847 -- extension completed with a controlled extension. Note that a full
13848 -- type that is controlled can break privacy: the flag Is_Controlled is
13849 -- set on both views of the type.
13850
13851 if Is_Controlled (Parent_Type)
b69cd36a
AC
13852 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
13853 Name_Adjust,
13854 Name_Finalize)
fd0d899b
AC
13855 and then Is_Hidden (Parent_Subp)
13856 and then not Is_Visibly_Controlled (Parent_Type)
13857 then
13858 Set_Is_Hidden (New_Subp);
13859 end if;
13860
996ae0b0
RK
13861 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
13862 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
13863
13864 if Ekind (Parent_Subp) = E_Procedure then
13865 Set_Is_Valued_Procedure
13866 (New_Subp, Is_Valued_Procedure (Parent_Subp));
c86ee18a
AC
13867 else
13868 Set_Has_Controlling_Result
13869 (New_Subp, Has_Controlling_Result (Parent_Subp));
996ae0b0
RK
13870 end if;
13871
57193e09
TQ
13872 -- No_Return must be inherited properly. If this is overridden in the
13873 -- case of a dispatching operation, then a check is made in Sem_Disp
13874 -- that the overriding operation is also No_Return (no such check is
13875 -- required for the case of non-dispatching operation.
13876
13877 Set_No_Return (New_Subp, No_Return (Parent_Subp));
13878
a5b62485
AC
13879 -- A derived function with a controlling result is abstract. If the
13880 -- Derived_Type is a nonabstract formal generic derived type, then
13881 -- inherited operations are not abstract: the required check is done at
13882 -- instantiation time. If the derivation is for a generic actual, the
13883 -- function is not abstract unless the actual is.
fbf5a39b
AC
13884
13885 if Is_Generic_Type (Derived_Type)
fea9e956 13886 and then not Is_Abstract_Type (Derived_Type)
fbf5a39b
AC
13887 then
13888 null;
13889
fea9e956
ES
13890 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
13891 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
13892
0791fbe9 13893 elsif Ada_Version >= Ada_2005
fea9e956
ES
13894 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13895 or else (Is_Tagged_Type (Derived_Type)
1824c168
AC
13896 and then Etype (New_Subp) = Derived_Type
13897 and then not Is_Null_Extension (Derived_Type))
fea9e956 13898 or else (Is_Tagged_Type (Derived_Type)
1824c168 13899 and then Ekind (Etype (New_Subp)) =
fea9e956 13900 E_Anonymous_Access_Type
1824c168
AC
13901 and then Designated_Type (Etype (New_Subp)) =
13902 Derived_Type
13903 and then not Is_Null_Extension (Derived_Type)))
fea9e956
ES
13904 and then No (Actual_Subp)
13905 then
13906 if not Is_Tagged_Type (Derived_Type)
13907 or else Is_Abstract_Type (Derived_Type)
13908 or else Is_Abstract_Subprogram (Alias (New_Subp))
13909 then
13910 Set_Is_Abstract_Subprogram (New_Subp);
13911 else
13912 Set_Requires_Overriding (New_Subp);
13913 end if;
13914
0791fbe9 13915 elsif Ada_Version < Ada_2005
fea9e956
ES
13916 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13917 or else (Is_Tagged_Type (Derived_Type)
13918 and then Etype (New_Subp) = Derived_Type
13919 and then No (Actual_Subp)))
fbf5a39b 13920 then
fea9e956 13921 Set_Is_Abstract_Subprogram (New_Subp);
fbf5a39b 13922
c86ee18a
AC
13923 -- AI05-0097 : an inherited operation that dispatches on result is
13924 -- abstract if the derived type is abstract, even if the parent type
13925 -- is concrete and the derived type is a null extension.
13926
13927 elsif Has_Controlling_Result (Alias (New_Subp))
13928 and then Is_Abstract_Type (Etype (New_Subp))
13929 then
13930 Set_Is_Abstract_Subprogram (New_Subp);
13931
2b73cf68 13932 -- Finally, if the parent type is abstract we must verify that all
ce4a6e84
RD
13933 -- inherited operations are either non-abstract or overridden, or that
13934 -- the derived type itself is abstract (this check is performed at the
13935 -- end of a package declaration, in Check_Abstract_Overriding). A
13936 -- private overriding in the parent type will not be visible in the
fbf5a39b
AC
13937 -- derivation if we are not in an inner package or in a child unit of
13938 -- the parent type, in which case the abstractness of the inherited
13939 -- operation is carried to the new subprogram.
13940
fea9e956 13941 elsif Is_Abstract_Type (Parent_Type)
fbf5a39b
AC
13942 and then not In_Open_Scopes (Scope (Parent_Type))
13943 and then Is_Private_Overriding
fea9e956 13944 and then Is_Abstract_Subprogram (Visible_Subp)
fbf5a39b 13945 then
2b73cf68
JM
13946 if No (Actual_Subp) then
13947 Set_Alias (New_Subp, Visible_Subp);
b8dfbe1e
AC
13948 Set_Is_Abstract_Subprogram (New_Subp, True);
13949
2b73cf68
JM
13950 else
13951 -- If this is a derivation for an instance of a formal derived
13952 -- type, abstractness comes from the primitive operation of the
13953 -- actual, not from the operation inherited from the ancestor.
13954
13955 Set_Is_Abstract_Subprogram
13956 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
13957 end if;
fbf5a39b
AC
13958 end if;
13959
996ae0b0
RK
13960 New_Overloaded_Entity (New_Subp, Derived_Type);
13961
a5b62485
AC
13962 -- Check for case of a derived subprogram for the instantiation of a
13963 -- formal derived tagged type, if so mark the subprogram as dispatching
292689c2 13964 -- and inherit the dispatching attributes of the actual subprogram. The
a5b62485
AC
13965 -- derived subprogram is effectively renaming of the actual subprogram,
13966 -- so it needs to have the same attributes as the actual.
996ae0b0
RK
13967
13968 if Present (Actual_Subp)
292689c2 13969 and then Is_Dispatching_Operation (Actual_Subp)
996ae0b0
RK
13970 then
13971 Set_Is_Dispatching_Operation (New_Subp);
88b32fc3 13972
292689c2
AC
13973 if Present (DTC_Entity (Actual_Subp)) then
13974 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
13975 Set_DT_Position (New_Subp, DT_Position (Actual_Subp));
996ae0b0
RK
13976 end if;
13977 end if;
13978
a5b62485
AC
13979 -- Indicate that a derived subprogram does not require a body and that
13980 -- it does not require processing of default expressions.
996ae0b0
RK
13981
13982 Set_Has_Completion (New_Subp);
13983 Set_Default_Expressions_Processed (New_Subp);
13984
996ae0b0
RK
13985 if Ekind (New_Subp) = E_Function then
13986 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
13987 end if;
13988 end Derive_Subprogram;
13989
13990 ------------------------
13991 -- Derive_Subprograms --
13992 ------------------------
13993
13994 procedure Derive_Subprograms
7d7af38a
JM
13995 (Parent_Type : Entity_Id;
13996 Derived_Type : Entity_Id;
13997 Generic_Actual : Entity_Id := Empty)
996ae0b0 13998 is
ce2b6ba5
JM
13999 Op_List : constant Elist_Id :=
14000 Collect_Primitive_Operations (Parent_Type);
14001
14002 function Check_Derived_Type return Boolean;
ff2efe85 14003 -- Check that all the entities derived from Parent_Type are found in
ce2b6ba5
JM
14004 -- the list of primitives of Derived_Type exactly in the same order.
14005
ff2efe85
AC
14006 procedure Derive_Interface_Subprogram
14007 (New_Subp : in out Entity_Id;
14008 Subp : Entity_Id;
14009 Actual_Subp : Entity_Id);
14010 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
14011 -- (which is an interface primitive). If Generic_Actual is present then
14012 -- Actual_Subp is the actual subprogram corresponding with the generic
14013 -- subprogram Subp.
14014
ce2b6ba5
JM
14015 function Check_Derived_Type return Boolean is
14016 E : Entity_Id;
14017 Elmt : Elmt_Id;
14018 List : Elist_Id;
14019 New_Subp : Entity_Id;
14020 Op_Elmt : Elmt_Id;
14021 Subp : Entity_Id;
14022
14023 begin
14024 -- Traverse list of entities in the current scope searching for
14025 -- an incomplete type whose full-view is derived type
14026
14027 E := First_Entity (Scope (Derived_Type));
1824c168 14028 while Present (E) and then E /= Derived_Type loop
ce2b6ba5
JM
14029 if Ekind (E) = E_Incomplete_Type
14030 and then Present (Full_View (E))
14031 and then Full_View (E) = Derived_Type
14032 then
14033 -- Disable this test if Derived_Type completes an incomplete
14034 -- type because in such case more primitives can be added
14035 -- later to the list of primitives of Derived_Type by routine
14036 -- Process_Incomplete_Dependents
14037
14038 return True;
14039 end if;
14040
14041 E := Next_Entity (E);
14042 end loop;
14043
14044 List := Collect_Primitive_Operations (Derived_Type);
14045 Elmt := First_Elmt (List);
14046
14047 Op_Elmt := First_Elmt (Op_List);
14048 while Present (Op_Elmt) loop
14049 Subp := Node (Op_Elmt);
14050 New_Subp := Node (Elmt);
14051
14052 -- At this early stage Derived_Type has no entities with attribute
14053 -- Interface_Alias. In addition, such primitives are always
14054 -- located at the end of the list of primitives of Parent_Type.
14055 -- Therefore, if found we can safely stop processing pending
14056 -- entities.
14057
14058 exit when Present (Interface_Alias (Subp));
14059
14060 -- Handle hidden entities
14061
14062 if not Is_Predefined_Dispatching_Operation (Subp)
14063 and then Is_Hidden (Subp)
14064 then
14065 if Present (New_Subp)
14066 and then Primitive_Names_Match (Subp, New_Subp)
14067 then
14068 Next_Elmt (Elmt);
14069 end if;
14070
14071 else
14072 if not Present (New_Subp)
14073 or else Ekind (Subp) /= Ekind (New_Subp)
14074 or else not Primitive_Names_Match (Subp, New_Subp)
14075 then
14076 return False;
14077 end if;
14078
14079 Next_Elmt (Elmt);
14080 end if;
14081
14082 Next_Elmt (Op_Elmt);
14083 end loop;
14084
14085 return True;
14086 end Check_Derived_Type;
14087
ff2efe85
AC
14088 ---------------------------------
14089 -- Derive_Interface_Subprogram --
14090 ---------------------------------
14091
14092 procedure Derive_Interface_Subprogram
14093 (New_Subp : in out Entity_Id;
14094 Subp : Entity_Id;
14095 Actual_Subp : Entity_Id)
14096 is
14097 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
14098 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
14099
14100 begin
14101 pragma Assert (Is_Interface (Iface_Type));
14102
14103 Derive_Subprogram
14104 (New_Subp => New_Subp,
14105 Parent_Subp => Iface_Subp,
14106 Derived_Type => Derived_Type,
14107 Parent_Type => Iface_Type,
14108 Actual_Subp => Actual_Subp);
14109
14110 -- Given that this new interface entity corresponds with a primitive
14111 -- of the parent that was not overridden we must leave it associated
14112 -- with its parent primitive to ensure that it will share the same
14113 -- dispatch table slot when overridden.
14114
14115 if No (Actual_Subp) then
14116 Set_Alias (New_Subp, Subp);
14117
14118 -- For instantiations this is not needed since the previous call to
14119 -- Derive_Subprogram leaves the entity well decorated.
14120
14121 else
14122 pragma Assert (Alias (New_Subp) = Actual_Subp);
14123 null;
14124 end if;
14125 end Derive_Interface_Subprogram;
14126
ce2b6ba5
JM
14127 -- Local variables
14128
14129 Alias_Subp : Entity_Id;
88b32fc3 14130 Act_List : Elist_Id;
39ce7604 14131 Act_Elmt : Elmt_Id;
ce2b6ba5 14132 Act_Subp : Entity_Id := Empty;
88b32fc3 14133 Elmt : Elmt_Id;
ce2b6ba5 14134 Need_Search : Boolean := False;
88b32fc3
BD
14135 New_Subp : Entity_Id := Empty;
14136 Parent_Base : Entity_Id;
14137 Subp : Entity_Id;
996ae0b0 14138
ce2b6ba5
JM
14139 -- Start of processing for Derive_Subprograms
14140
996ae0b0
RK
14141 begin
14142 if Ekind (Parent_Type) = E_Record_Type_With_Private
14143 and then Has_Discriminants (Parent_Type)
14144 and then Present (Full_View (Parent_Type))
14145 then
14146 Parent_Base := Full_View (Parent_Type);
14147 else
14148 Parent_Base := Parent_Type;
14149 end if;
14150
996ae0b0
RK
14151 if Present (Generic_Actual) then
14152 Act_List := Collect_Primitive_Operations (Generic_Actual);
14153 Act_Elmt := First_Elmt (Act_List);
39ce7604
AC
14154 else
14155 Act_List := No_Elist;
14156 Act_Elmt := No_Elmt;
996ae0b0
RK
14157 end if;
14158
ce2b6ba5
JM
14159 -- Derive primitives inherited from the parent. Note that if the generic
14160 -- actual is present, this is not really a type derivation, it is a
14161 -- completion within an instance.
996ae0b0 14162
ce2b6ba5
JM
14163 -- Case 1: Derived_Type does not implement interfaces
14164
14165 if not Is_Tagged_Type (Derived_Type)
14166 or else (not Has_Interfaces (Derived_Type)
14167 and then not (Present (Generic_Actual)
1824c168 14168 and then Has_Interfaces (Generic_Actual)))
ce2b6ba5
JM
14169 then
14170 Elmt := First_Elmt (Op_List);
14171 while Present (Elmt) loop
14172 Subp := Node (Elmt);
996ae0b0 14173
ce2b6ba5
JM
14174 -- Literals are derived earlier in the process of building the
14175 -- derived type, and are skipped here.
950d3e7d 14176
ce2b6ba5 14177 if Ekind (Subp) = E_Enumeration_Literal then
9dfd2ff8 14178 null;
758c442c 14179
ce2b6ba5
JM
14180 -- The actual is a direct descendant and the common primitive
14181 -- operations appear in the same order.
7d7af38a 14182
ce2b6ba5
JM
14183 -- If the generic parent type is present, the derived type is an
14184 -- instance of a formal derived type, and within the instance its
14185 -- operations are those of the actual. We derive from the formal
14186 -- type but make the inherited operations aliases of the
14187 -- corresponding operations of the actual.
7d7af38a 14188
ce2b6ba5 14189 else
b4d7b435
AC
14190 pragma Assert (No (Node (Act_Elmt))
14191 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
1824c168
AC
14192 and then
14193 Type_Conformant
14194 (Subp, Node (Act_Elmt),
14195 Skip_Controlling_Formals => True)));
b4d7b435 14196
ce2b6ba5
JM
14197 Derive_Subprogram
14198 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
88b32fc3 14199
ce2b6ba5
JM
14200 if Present (Act_Elmt) then
14201 Next_Elmt (Act_Elmt);
14202 end if;
14203 end if;
88b32fc3 14204
ce2b6ba5
JM
14205 Next_Elmt (Elmt);
14206 end loop;
14207
14208 -- Case 2: Derived_Type implements interfaces
14209
14210 else
14211 -- If the parent type has no predefined primitives we remove
14212 -- predefined primitives from the list of primitives of generic
14213 -- actual to simplify the complexity of this algorithm.
14214
14215 if Present (Generic_Actual) then
14216 declare
14217 Has_Predefined_Primitives : Boolean := False;
14218
14219 begin
14220 -- Check if the parent type has predefined primitives
14221
14222 Elmt := First_Elmt (Op_List);
14223 while Present (Elmt) loop
14224 Subp := Node (Elmt);
14225
14226 if Is_Predefined_Dispatching_Operation (Subp)
14227 and then not Comes_From_Source (Ultimate_Alias (Subp))
14228 then
14229 Has_Predefined_Primitives := True;
14230 exit;
14231 end if;
14232
14233 Next_Elmt (Elmt);
14234 end loop;
14235
14236 -- Remove predefined primitives of Generic_Actual. We must use
14237 -- an auxiliary list because in case of tagged types the value
14238 -- returned by Collect_Primitive_Operations is the value stored
14239 -- in its Primitive_Operations attribute (and we don't want to
14240 -- modify its current contents).
14241
14242 if not Has_Predefined_Primitives then
14243 declare
14244 Aux_List : constant Elist_Id := New_Elmt_List;
14245
14246 begin
14247 Elmt := First_Elmt (Act_List);
14248 while Present (Elmt) loop
14249 Subp := Node (Elmt);
14250
14251 if not Is_Predefined_Dispatching_Operation (Subp)
14252 or else Comes_From_Source (Subp)
14253 then
14254 Append_Elmt (Subp, Aux_List);
14255 end if;
14256
14257 Next_Elmt (Elmt);
14258 end loop;
14259
14260 Act_List := Aux_List;
14261 end;
88b32fc3 14262 end if;
996ae0b0 14263
ce2b6ba5
JM
14264 Act_Elmt := First_Elmt (Act_List);
14265 Act_Subp := Node (Act_Elmt);
14266 end;
14267 end if;
14268
14269 -- Stage 1: If the generic actual is not present we derive the
14270 -- primitives inherited from the parent type. If the generic parent
14271 -- type is present, the derived type is an instance of a formal
14272 -- derived type, and within the instance its operations are those of
14273 -- the actual. We derive from the formal type but make the inherited
14274 -- operations aliases of the corresponding operations of the actual.
14275
14276 Elmt := First_Elmt (Op_List);
14277 while Present (Elmt) loop
14278 Subp := Node (Elmt);
14279 Alias_Subp := Ultimate_Alias (Subp);
14280
74853971 14281 -- Do not derive internal entities of the parent that link
ff2efe85 14282 -- interface primitives with their covering primitive. These
74853971 14283 -- entities will be added to this type when frozen.
ce2b6ba5 14284
74853971
AC
14285 if Present (Interface_Alias (Subp)) then
14286 goto Continue;
14287 end if;
ce2b6ba5
JM
14288
14289 -- If the generic actual is present find the corresponding
14290 -- operation in the generic actual. If the parent type is a
14291 -- direct ancestor of the derived type then, even if it is an
14292 -- interface, the operations are inherited from the primary
14293 -- dispatch table and are in the proper order. If we detect here
14294 -- that primitives are not in the same order we traverse the list
14295 -- of primitive operations of the actual to find the one that
14296 -- implements the interface primitive.
14297
14298 if Need_Search
14299 or else
14300 (Present (Generic_Actual)
4a214958 14301 and then Present (Act_Subp)
b4d7b435
AC
14302 and then not
14303 (Primitive_Names_Match (Subp, Act_Subp)
14304 and then
14305 Type_Conformant (Subp, Act_Subp,
14306 Skip_Controlling_Formals => True)))
ce2b6ba5 14307 then
b37d5bc6
AC
14308 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
14309 Use_Full_View => True));
ce2b6ba5 14310
4a214958 14311 -- Remember that we need searching for all pending primitives
ce2b6ba5
JM
14312
14313 Need_Search := True;
14314
14315 -- Handle entities associated with interface primitives
14316
b4d7b435
AC
14317 if Present (Alias_Subp)
14318 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
ce2b6ba5 14319 and then not Is_Predefined_Dispatching_Operation (Subp)
71f62180 14320 then
b4d7b435
AC
14321 -- Search for the primitive in the homonym chain
14322
ce2b6ba5
JM
14323 Act_Subp :=
14324 Find_Primitive_Covering_Interface
14325 (Tagged_Type => Generic_Actual,
b4d7b435
AC
14326 Iface_Prim => Alias_Subp);
14327
14328 -- Previous search may not locate primitives covering
14329 -- interfaces defined in generics units or instantiations.
14330 -- (it fails if the covering primitive has formals whose
14331 -- type is also defined in generics or instantiations).
14332 -- In such case we search in the list of primitives of the
14333 -- generic actual for the internal entity that links the
14334 -- interface primitive and the covering primitive.
14335
14336 if No (Act_Subp)
14337 and then Is_Generic_Type (Parent_Type)
14338 then
14339 -- This code has been designed to handle only generic
14340 -- formals that implement interfaces that are defined
14341 -- in a generic unit or instantiation. If this code is
14342 -- needed for other cases we must review it because
14343 -- (given that it relies on Original_Location to locate
14344 -- the primitive of Generic_Actual that covers the
14345 -- interface) it could leave linked through attribute
14346 -- Alias entities of unrelated instantiations).
14347
14348 pragma Assert
14349 (Is_Generic_Unit
14350 (Scope (Find_Dispatching_Type (Alias_Subp)))
39ce7604
AC
14351 or else
14352 Instantiation_Depth
14353 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
b4d7b435
AC
14354
14355 declare
14356 Iface_Prim_Loc : constant Source_Ptr :=
14357 Original_Location (Sloc (Alias_Subp));
39ce7604
AC
14358
14359 Elmt : Elmt_Id;
14360 Prim : Entity_Id;
14361
b4d7b435
AC
14362 begin
14363 Elmt :=
14364 First_Elmt (Primitive_Operations (Generic_Actual));
14365
14366 Search : while Present (Elmt) loop
14367 Prim := Node (Elmt);
14368
14369 if Present (Interface_Alias (Prim))
14370 and then Original_Location
39ce7604
AC
14371 (Sloc (Interface_Alias (Prim))) =
14372 Iface_Prim_Loc
b4d7b435
AC
14373 then
14374 Act_Subp := Alias (Prim);
14375 exit Search;
14376 end if;
14377
14378 Next_Elmt (Elmt);
14379 end loop Search;
14380 end;
14381 end if;
14382
14383 pragma Assert (Present (Act_Subp)
14384 or else Is_Abstract_Type (Generic_Actual)
14385 or else Serious_Errors_Detected > 0);
2b73cf68 14386
ce2b6ba5
JM
14387 -- Handle predefined primitives plus the rest of user-defined
14388 -- primitives
14389
14390 else
71f62180 14391 Act_Elmt := First_Elmt (Act_List);
2b73cf68 14392 while Present (Act_Elmt) loop
ce2b6ba5
JM
14393 Act_Subp := Node (Act_Elmt);
14394
14395 exit when Primitive_Names_Match (Subp, Act_Subp)
4a214958
AC
14396 and then Type_Conformant
14397 (Subp, Act_Subp,
14398 Skip_Controlling_Formals => True)
ce2b6ba5
JM
14399 and then No (Interface_Alias (Act_Subp));
14400
2b73cf68
JM
14401 Next_Elmt (Act_Elmt);
14402 end loop;
b4d7b435
AC
14403
14404 if No (Act_Elmt) then
14405 Act_Subp := Empty;
14406 end if;
2b73cf68 14407 end if;
ce2b6ba5 14408 end if;
2b73cf68 14409
ce2b6ba5
JM
14410 -- Case 1: If the parent is a limited interface then it has the
14411 -- predefined primitives of synchronized interfaces. However, the
14412 -- actual type may be a non-limited type and hence it does not
14413 -- have such primitives.
2b73cf68 14414
ce2b6ba5
JM
14415 if Present (Generic_Actual)
14416 and then not Present (Act_Subp)
14417 and then Is_Limited_Interface (Parent_Base)
14418 and then Is_Predefined_Interface_Primitive (Subp)
14419 then
14420 null;
2b73cf68 14421
4120ada7
RD
14422 -- Case 2: Inherit entities associated with interfaces that were
14423 -- not covered by the parent type. We exclude here null interface
14424 -- primitives because they do not need special management.
14425
14426 -- We also exclude interface operations that are renamings. If the
14427 -- subprogram is an explicit renaming of an interface primitive,
14428 -- it is a regular primitive operation, and the presence of its
14429 -- alias is not relevant: it has to be derived like any other
14430 -- primitive.
ce2b6ba5
JM
14431
14432 elsif Present (Alias (Subp))
4120ada7
RD
14433 and then Nkind (Unit_Declaration_Node (Subp)) /=
14434 N_Subprogram_Renaming_Declaration
ce2b6ba5
JM
14435 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
14436 and then not
14437 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
4a214958 14438 and then Null_Present (Parent (Alias_Subp)))
ce2b6ba5 14439 then
ff2efe85
AC
14440 -- If this is an abstract private type then we transfer the
14441 -- derivation of the interface primitive from the partial view
14442 -- to the full view. This is safe because all the interfaces
14443 -- must be visible in the partial view. Done to avoid adding
14444 -- a new interface derivation to the private part of the
14445 -- enclosing package; otherwise this new derivation would be
14446 -- decorated as hidden when the analysis of the enclosing
14447 -- package completes.
14448
14449 if Is_Abstract_Type (Derived_Type)
14450 and then In_Private_Part (Current_Scope)
14451 and then Has_Private_Declaration (Derived_Type)
14452 then
14453 declare
14454 Partial_View : Entity_Id;
14455 Elmt : Elmt_Id;
14456 Ent : Entity_Id;
14457
14458 begin
14459 Partial_View := First_Entity (Current_Scope);
14460 loop
14461 exit when No (Partial_View)
14462 or else (Has_Private_Declaration (Partial_View)
14463 and then
14464 Full_View (Partial_View) = Derived_Type);
14465
14466 Next_Entity (Partial_View);
14467 end loop;
14468
14469 -- If the partial view was not found then the source code
14470 -- has errors and the derivation is not needed.
ce2b6ba5 14471
ff2efe85
AC
14472 if Present (Partial_View) then
14473 Elmt :=
14474 First_Elmt (Primitive_Operations (Partial_View));
14475 while Present (Elmt) loop
14476 Ent := Node (Elmt);
14477
14478 if Present (Alias (Ent))
14479 and then Ultimate_Alias (Ent) = Alias (Subp)
14480 then
14481 Append_Elmt
14482 (Ent, Primitive_Operations (Derived_Type));
14483 exit;
14484 end if;
14485
14486 Next_Elmt (Elmt);
14487 end loop;
14488
14489 -- If the interface primitive was not found in the
14490 -- partial view then this interface primitive was
14491 -- overridden. We add a derivation to activate in
14492 -- Derive_Progenitor_Subprograms the machinery to
14493 -- search for it.
14494
14495 if No (Elmt) then
14496 Derive_Interface_Subprogram
14497 (New_Subp => New_Subp,
14498 Subp => Subp,
14499 Actual_Subp => Act_Subp);
14500 end if;
14501 end if;
14502 end;
14503 else
14504 Derive_Interface_Subprogram
14505 (New_Subp => New_Subp,
14506 Subp => Subp,
14507 Actual_Subp => Act_Subp);
2b73cf68 14508 end if;
996ae0b0 14509
ce2b6ba5 14510 -- Case 3: Common derivation
88b32fc3 14511
ce2b6ba5
JM
14512 else
14513 Derive_Subprogram
14514 (New_Subp => New_Subp,
14515 Parent_Subp => Subp,
14516 Derived_Type => Derived_Type,
14517 Parent_Type => Parent_Base,
14518 Actual_Subp => Act_Subp);
14519 end if;
2b73cf68 14520
ce2b6ba5
JM
14521 -- No need to update Act_Elm if we must search for the
14522 -- corresponding operation in the generic actual
7d7af38a 14523
ce2b6ba5
JM
14524 if not Need_Search
14525 and then Present (Act_Elmt)
14526 then
14527 Next_Elmt (Act_Elmt);
14528 Act_Subp := Node (Act_Elmt);
14529 end if;
7d7af38a 14530
74853971 14531 <<Continue>>
7d7af38a
JM
14532 Next_Elmt (Elmt);
14533 end loop;
ce2b6ba5
JM
14534
14535 -- Inherit additional operations from progenitors. If the derived
14536 -- type is a generic actual, there are not new primitive operations
14537 -- for the type because it has those of the actual, and therefore
14538 -- nothing needs to be done. The renamings generated above are not
14539 -- primitive operations, and their purpose is simply to make the
14540 -- proper operations visible within an instantiation.
14541
14542 if No (Generic_Actual) then
14543 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
14544 end if;
7d7af38a 14545 end if;
ce2b6ba5
JM
14546
14547 -- Final check: Direct descendants must have their primitives in the
dd386db0 14548 -- same order. We exclude from this test untagged types and instances
ce2b6ba5
JM
14549 -- of formal derived types. We skip this test if we have already
14550 -- reported serious errors in the sources.
14551
14552 pragma Assert (not Is_Tagged_Type (Derived_Type)
14553 or else Present (Generic_Actual)
14554 or else Serious_Errors_Detected > 0
14555 or else Check_Derived_Type);
996ae0b0
RK
14556 end Derive_Subprograms;
14557
14558 --------------------------------
14559 -- Derived_Standard_Character --
14560 --------------------------------
14561
14562 procedure Derived_Standard_Character
71f62180
ES
14563 (N : Node_Id;
14564 Parent_Type : Entity_Id;
14565 Derived_Type : Entity_Id)
996ae0b0
RK
14566 is
14567 Loc : constant Source_Ptr := Sloc (N);
14568 Def : constant Node_Id := Type_Definition (N);
14569 Indic : constant Node_Id := Subtype_Indication (Def);
14570 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
14571 Implicit_Base : constant Entity_Id :=
14572 Create_Itype
14573 (E_Enumeration_Type, N, Derived_Type, 'B');
14574
14575 Lo : Node_Id;
14576 Hi : Node_Id;
996ae0b0
RK
14577
14578 begin
fbf5a39b 14579 Discard_Node (Process_Subtype (Indic, N));
996ae0b0
RK
14580
14581 Set_Etype (Implicit_Base, Parent_Base);
14582 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
14583 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
14584
14585 Set_Is_Character_Type (Implicit_Base, True);
14586 Set_Has_Delayed_Freeze (Implicit_Base);
14587
fbf5a39b
AC
14588 -- The bounds of the implicit base are the bounds of the parent base.
14589 -- Note that their type is the parent base.
14590
14591 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
14592 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
996ae0b0
RK
14593
14594 Set_Scalar_Range (Implicit_Base,
14595 Make_Range (Loc,
14596 Low_Bound => Lo,
14597 High_Bound => Hi));
14598
14599 Conditional_Delay (Derived_Type, Parent_Type);
14600
14601 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
14602 Set_Etype (Derived_Type, Implicit_Base);
14603 Set_Size_Info (Derived_Type, Parent_Type);
14604
14605 if Unknown_RM_Size (Derived_Type) then
14606 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
14607 end if;
14608
14609 Set_Is_Character_Type (Derived_Type, True);
14610
14611 if Nkind (Indic) /= N_Subtype_Indication then
fbf5a39b
AC
14612
14613 -- If no explicit constraint, the bounds are those
14614 -- of the parent type.
14615
14616 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
14617 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
14618 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
996ae0b0
RK
14619 end if;
14620
14621 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
14622
7d7af38a
JM
14623 -- Because the implicit base is used in the conversion of the bounds, we
14624 -- have to freeze it now. This is similar to what is done for numeric
14625 -- types, and it equally suspicious, but otherwise a non-static bound
14626 -- will have a reference to an unfrozen type, which is rejected by Gigi
14627 -- (???). This requires specific care for definition of stream
14628 -- attributes. For details, see comments at the end of
88b32fc3 14629 -- Build_Derived_Numeric_Type.
996ae0b0
RK
14630
14631 Freeze_Before (N, Implicit_Base);
996ae0b0
RK
14632 end Derived_Standard_Character;
14633
14634 ------------------------------
14635 -- Derived_Type_Declaration --
14636 ------------------------------
14637
14638 procedure Derived_Type_Declaration
14639 (T : Entity_Id;
14640 N : Node_Id;
14641 Is_Completion : Boolean)
14642 is
996ae0b0 14643 Parent_Type : Entity_Id;
996ae0b0 14644
c6823a20
EB
14645 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
14646 -- Check whether the parent type is a generic formal, or derives
14647 -- directly or indirectly from one.
14648
14649 ------------------------
14650 -- Comes_From_Generic --
14651 ------------------------
14652
14653 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
14654 begin
14655 if Is_Generic_Type (Typ) then
14656 return True;
14657
14658 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
14659 return True;
14660
14661 elsif Is_Private_Type (Typ)
14662 and then Present (Full_View (Typ))
14663 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
14664 then
14665 return True;
14666
14667 elsif Is_Generic_Actual_Type (Typ) then
14668 return True;
14669
14670 else
14671 return False;
14672 end if;
14673 end Comes_From_Generic;
14674
2b73cf68
JM
14675 -- Local variables
14676
14677 Def : constant Node_Id := Type_Definition (N);
14678 Iface_Def : Node_Id;
14679 Indic : constant Node_Id := Subtype_Indication (Def);
14680 Extension : constant Node_Id := Record_Extension_Part (Def);
14681 Parent_Node : Node_Id;
2b73cf68
JM
14682 Taggd : Boolean;
14683
fa7c4d23
AC
14684 -- Start of processing for Derived_Type_Declaration
14685
996ae0b0
RK
14686 begin
14687 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
14688
758c442c
GD
14689 -- Ada 2005 (AI-251): In case of interface derivation check that the
14690 -- parent is also an interface.
14691
14692 if Interface_Present (Def) then
2ba431e5 14693 Check_SPARK_Restriction ("interface is not allowed", Def);
d8b962d8 14694
758c442c 14695 if not Is_Interface (Parent_Type) then
6765b310 14696 Diagnose_Interface (Indic, Parent_Type);
758c442c
GD
14697
14698 else
2b73cf68
JM
14699 Parent_Node := Parent (Base_Type (Parent_Type));
14700 Iface_Def := Type_Definition (Parent_Node);
758c442c
GD
14701
14702 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
14703 -- other limited interfaces.
14704
14705 if Limited_Present (Def) then
14706 if Limited_Present (Iface_Def) then
14707 null;
14708
14709 elsif Protected_Present (Iface_Def) then
e358346d
AC
14710 Error_Msg_NE
14711 ("descendant of& must be declared"
14712 & " as a protected interface",
14713 N, Parent_Type);
758c442c
GD
14714
14715 elsif Synchronized_Present (Iface_Def) then
e358346d
AC
14716 Error_Msg_NE
14717 ("descendant of& must be declared"
14718 & " as a synchronized interface",
14719 N, Parent_Type);
758c442c
GD
14720
14721 elsif Task_Present (Iface_Def) then
e358346d
AC
14722 Error_Msg_NE
14723 ("descendant of& must be declared as a task interface",
14724 N, Parent_Type);
758c442c
GD
14725
14726 else
dc06abec
RD
14727 Error_Msg_N
14728 ("(Ada 2005) limited interface cannot "
14729 & "inherit from non-limited interface", Indic);
758c442c
GD
14730 end if;
14731
14732 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
14733 -- from non-limited or limited interfaces.
14734
14735 elsif not Protected_Present (Def)
14736 and then not Synchronized_Present (Def)
14737 and then not Task_Present (Def)
14738 then
14739 if Limited_Present (Iface_Def) then
14740 null;
14741
14742 elsif Protected_Present (Iface_Def) then
e358346d
AC
14743 Error_Msg_NE
14744 ("descendant of& must be declared"
14745 & " as a protected interface",
14746 N, Parent_Type);
758c442c
GD
14747
14748 elsif Synchronized_Present (Iface_Def) then
e358346d
AC
14749 Error_Msg_NE
14750 ("descendant of& must be declared"
14751 & " as a synchronized interface",
14752 N, Parent_Type);
758c442c
GD
14753
14754 elsif Task_Present (Iface_Def) then
e358346d
AC
14755 Error_Msg_NE
14756 ("descendant of& must be declared as a task interface",
14757 N, Parent_Type);
758c442c
GD
14758 else
14759 null;
14760 end if;
14761 end if;
14762 end if;
14763 end if;
14764
fea9e956
ES
14765 if Is_Tagged_Type (Parent_Type)
14766 and then Is_Concurrent_Type (Parent_Type)
14767 and then not Is_Interface (Parent_Type)
fea9e956 14768 then
dc06abec
RD
14769 Error_Msg_N
14770 ("parent type of a record extension cannot be "
14771 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
14772 Set_Etype (T, Any_Type);
fea9e956
ES
14773 return;
14774 end if;
14775
758c442c
GD
14776 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
14777 -- interfaces
14778
14779 if Is_Tagged_Type (Parent_Type)
14780 and then Is_Non_Empty_List (Interface_List (Def))
14781 then
14782 declare
9dfd2ff8
CC
14783 Intf : Node_Id;
14784 T : Entity_Id;
14785
758c442c 14786 begin
9dfd2ff8
CC
14787 Intf := First (Interface_List (Def));
14788 while Present (Intf) loop
14789 T := Find_Type_Of_Subtype_Indic (Intf);
758c442c
GD
14790
14791 if not Is_Interface (T) then
6765b310 14792 Diagnose_Interface (Intf, T);
653da906 14793
2b73cf68
JM
14794 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
14795 -- a limited type from having a nonlimited progenitor.
14796
14797 elsif (Limited_Present (Def)
14798 or else (not Is_Interface (Parent_Type)
14799 and then Is_Limited_Type (Parent_Type)))
653da906
RD
14800 and then not Is_Limited_Interface (T)
14801 then
14802 Error_Msg_NE
14803 ("progenitor interface& of limited type must be limited",
14804 N, T);
758c442c
GD
14805 end if;
14806
9dfd2ff8 14807 Next (Intf);
758c442c
GD
14808 end loop;
14809 end;
14810 end if;
14811
996ae0b0
RK
14812 if Parent_Type = Any_Type
14813 or else Etype (Parent_Type) = Any_Type
14814 or else (Is_Class_Wide_Type (Parent_Type)
4c51ff88 14815 and then Etype (Parent_Type) = T)
996ae0b0 14816 then
a5b62485
AC
14817 -- If Parent_Type is undefined or illegal, make new type into a
14818 -- subtype of Any_Type, and set a few attributes to prevent cascaded
14819 -- errors. If this is a self-definition, emit error now.
996ae0b0
RK
14820
14821 if T = Parent_Type
14822 or else T = Etype (Parent_Type)
14823 then
14824 Error_Msg_N ("type cannot be used in its own definition", Indic);
14825 end if;
14826
14827 Set_Ekind (T, Ekind (Parent_Type));
14828 Set_Etype (T, Any_Type);
14829 Set_Scalar_Range (T, Scalar_Range (Any_Type));
14830
ef2a63ba
JM
14831 if Is_Tagged_Type (T)
14832 and then Is_Record_Type (T)
14833 then
14834 Set_Direct_Primitive_Operations (T, New_Elmt_List);
996ae0b0 14835 end if;
07fc65c4 14836
996ae0b0 14837 return;
996ae0b0
RK
14838 end if;
14839
653da906
RD
14840 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
14841 -- an interface is special because the list of interfaces in the full
14842 -- view can be given in any order. For example:
14843
14844 -- type A is interface;
14845 -- type B is interface and A;
14846 -- type D is new B with private;
14847 -- private
14848 -- type D is new A and B with null record; -- 1 --
14849
14850 -- In this case we perform the following transformation of -1-:
14851
14852 -- type D is new B and A with null record;
14853
14854 -- If the parent of the full-view covers the parent of the partial-view
14855 -- we have two possible cases:
14856
14857 -- 1) They have the same parent
14858 -- 2) The parent of the full-view implements some further interfaces
14859
14860 -- In both cases we do not need to perform the transformation. In the
14861 -- first case the source program is correct and the transformation is
14862 -- not needed; in the second case the source program does not fulfill
14863 -- the no-hidden interfaces rule (AI-396) and the error will be reported
14864 -- later.
14865
14866 -- This transformation not only simplifies the rest of the analysis of
14867 -- this type declaration but also simplifies the correct generation of
14868 -- the object layout to the expander.
14869
14870 if In_Private_Part (Current_Scope)
14871 and then Is_Interface (Parent_Type)
14872 then
14873 declare
14874 Iface : Node_Id;
14875 Partial_View : Entity_Id;
14876 Partial_View_Parent : Entity_Id;
14877 New_Iface : Node_Id;
14878
14879 begin
14880 -- Look for the associated private type declaration
14881
14882 Partial_View := First_Entity (Current_Scope);
14883 loop
57193e09 14884 exit when No (Partial_View)
653da906
RD
14885 or else (Has_Private_Declaration (Partial_View)
14886 and then Full_View (Partial_View) = T);
14887
14888 Next_Entity (Partial_View);
14889 end loop;
14890
14891 -- If the partial view was not found then the source code has
14892 -- errors and the transformation is not needed.
14893
14894 if Present (Partial_View) then
14895 Partial_View_Parent := Etype (Partial_View);
14896
14897 -- If the parent of the full-view covers the parent of the
14898 -- partial-view we have nothing else to do.
14899
14900 if Interface_Present_In_Ancestor
14901 (Parent_Type, Partial_View_Parent)
14902 then
14903 null;
14904
14905 -- Traverse the list of interfaces of the full-view to look
14906 -- for the parent of the partial-view and perform the tree
14907 -- transformation.
14908
14909 else
14910 Iface := First (Interface_List (Def));
14911 while Present (Iface) loop
14912 if Etype (Iface) = Etype (Partial_View) then
14913 Rewrite (Subtype_Indication (Def),
14914 New_Copy (Subtype_Indication
14915 (Parent (Partial_View))));
14916
7675ad4f
AC
14917 New_Iface :=
14918 Make_Identifier (Sloc (N), Chars (Parent_Type));
653da906
RD
14919 Append (New_Iface, Interface_List (Def));
14920
14921 -- Analyze the transformed code
14922
14923 Derived_Type_Declaration (T, N, Is_Completion);
14924 return;
14925 end if;
14926
14927 Next (Iface);
14928 end loop;
14929 end if;
14930 end if;
14931 end;
14932 end if;
14933
996ae0b0 14934 -- Only composite types other than array types are allowed to have
2ba431e5 14935 -- discriminants. In SPARK, no types are allowed to have discriminants.
996ae0b0 14936
fe5d3068
YM
14937 if Present (Discriminant_Specifications (N)) then
14938 if (Is_Elementary_Type (Parent_Type)
14939 or else Is_Array_Type (Parent_Type))
14940 and then not Error_Posted (N)
14941 then
14942 Error_Msg_N
14943 ("elementary or array type cannot have discriminants",
14944 Defining_Identifier (First (Discriminant_Specifications (N))));
14945 Set_Has_Discriminants (T, False);
14946 else
2ba431e5 14947 Check_SPARK_Restriction ("discriminant type is not allowed", N);
fe5d3068 14948 end if;
996ae0b0
RK
14949 end if;
14950
14951 -- In Ada 83, a derived type defined in a package specification cannot
14952 -- be used for further derivation until the end of its visible part.
14953 -- Note that derivation in the private part of the package is allowed.
14954
0ab80019 14955 if Ada_Version = Ada_83
996ae0b0
RK
14956 and then Is_Derived_Type (Parent_Type)
14957 and then In_Visible_Part (Scope (Parent_Type))
14958 then
0ab80019 14959 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
996ae0b0
RK
14960 Error_Msg_N
14961 ("(Ada 83): premature use of type for derivation", Indic);
14962 end if;
14963 end if;
14964
14965 -- Check for early use of incomplete or private type
14966
bce79204 14967 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
996ae0b0
RK
14968 Error_Msg_N ("premature derivation of incomplete type", Indic);
14969 return;
14970
14971 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
c6823a20 14972 and then not Comes_From_Generic (Parent_Type))
996ae0b0
RK
14973 or else Has_Private_Component (Parent_Type)
14974 then
14975 -- The ancestor type of a formal type can be incomplete, in which
277c9abe
AC
14976 -- case only the operations of the partial view are available in the
14977 -- generic. Subsequent checks may be required when the full view is
14978 -- analyzed to verify that a derivation from a tagged type has an
14979 -- extension.
996ae0b0
RK
14980
14981 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
14982 null;
14983
14984 elsif No (Underlying_Type (Parent_Type))
14985 or else Has_Private_Component (Parent_Type)
14986 then
14987 Error_Msg_N
14988 ("premature derivation of derived or private type", Indic);
14989
14990 -- Flag the type itself as being in error, this prevents some
c6823a20 14991 -- nasty problems with subsequent uses of the malformed type.
996ae0b0
RK
14992
14993 Set_Error_Posted (T);
14994
14995 -- Check that within the immediate scope of an untagged partial
14996 -- view it's illegal to derive from the partial view if the
14997 -- full view is tagged. (7.3(7))
14998
14999 -- We verify that the Parent_Type is a partial view by checking
15000 -- that it is not a Full_Type_Declaration (i.e. a private type or
15001 -- private extension declaration), to distinguish a partial view
15002 -- from a derivation from a private type which also appears as
0b3d16c0
AC
15003 -- E_Private_Type. If the parent base type is not declared in an
15004 -- enclosing scope there is no need to check.
996ae0b0
RK
15005
15006 elsif Present (Full_View (Parent_Type))
15007 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
15008 and then not Is_Tagged_Type (Parent_Type)
15009 and then Is_Tagged_Type (Full_View (Parent_Type))
0b3d16c0 15010 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
996ae0b0 15011 then
0b3d16c0
AC
15012 Error_Msg_N
15013 ("premature derivation from type with tagged full view",
15014 Indic);
996ae0b0
RK
15015 end if;
15016 end if;
15017
15018 -- Check that form of derivation is appropriate
15019
15020 Taggd := Is_Tagged_Type (Parent_Type);
15021
15022 -- Perhaps the parent type should be changed to the class-wide type's
15023 -- specific type in this case to prevent cascading errors ???
15024
15025 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
15026 Error_Msg_N ("parent type must not be a class-wide type", Indic);
15027 return;
15028 end if;
15029
15030 if Present (Extension) and then not Taggd then
15031 Error_Msg_N
15032 ("type derived from untagged type cannot have extension", Indic);
15033
15034 elsif No (Extension) and then Taggd then
71d9e9f2 15035
a5b62485
AC
15036 -- If this declaration is within a private part (or body) of a
15037 -- generic instantiation then the derivation is allowed (the parent
15038 -- type can only appear tagged in this case if it's a generic actual
15039 -- type, since it would otherwise have been rejected in the analysis
15040 -- of the generic template).
996ae0b0
RK
15041
15042 if not Is_Generic_Actual_Type (Parent_Type)
15043 or else In_Visible_Part (Scope (Parent_Type))
15044 then
46256d9d
AC
15045 if Is_Class_Wide_Type (Parent_Type) then
15046 Error_Msg_N
15047 ("parent type must not be a class-wide type", Indic);
15048
15049 -- Use specific type to prevent cascaded errors.
15050
15051 Parent_Type := Etype (Parent_Type);
15052
15053 else
15054 Error_Msg_N
15055 ("type derived from tagged type must have extension", Indic);
15056 end if;
996ae0b0
RK
15057 end if;
15058 end if;
15059
88b32fc3
BD
15060 -- AI-443: Synchronized formal derived types require a private
15061 -- extension. There is no point in checking the ancestor type or
15062 -- the progenitors since the construct is wrong to begin with.
15063
0791fbe9 15064 if Ada_Version >= Ada_2005
88b32fc3
BD
15065 and then Is_Generic_Type (T)
15066 and then Present (Original_Node (N))
15067 then
15068 declare
15069 Decl : constant Node_Id := Original_Node (N);
15070
15071 begin
15072 if Nkind (Decl) = N_Formal_Type_Declaration
15073 and then Nkind (Formal_Type_Definition (Decl)) =
15074 N_Formal_Derived_Type_Definition
15075 and then Synchronized_Present (Formal_Type_Definition (Decl))
15076 and then No (Extension)
15077
15078 -- Avoid emitting a duplicate error message
15079
15080 and then not Error_Posted (Indic)
15081 then
15082 Error_Msg_N
15083 ("synchronized derived type must have extension", N);
15084 end if;
15085 end;
15086 end if;
15087
fa961f76
ES
15088 if Null_Exclusion_Present (Def)
15089 and then not Is_Access_Type (Parent_Type)
15090 then
15091 Error_Msg_N ("null exclusion can only apply to an access type", N);
15092 end if;
15093
c206e8fd 15094 -- Avoid deriving parent primitives of underlying record views
9013065b
AC
15095
15096 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
15097 Derive_Subps => not Is_Underlying_Record_View (T));
653da906 15098
88b32fc3 15099 -- AI-419: The parent type of an explicitly limited derived type must
57193e09 15100 -- be a limited type or a limited interface.
653da906
RD
15101
15102 if Limited_Present (Def) then
15103 Set_Is_Limited_Record (T);
15104
030d25f4
JM
15105 if Is_Interface (T) then
15106 Set_Is_Limited_Interface (T);
15107 end if;
15108
653da906 15109 if not Is_Limited_Type (Parent_Type)
57193e09
TQ
15110 and then
15111 (not Is_Interface (Parent_Type)
15112 or else not Is_Limited_Interface (Parent_Type))
653da906 15113 then
ef237104 15114 -- AI05-0096: a derivation in the private part of an instance is
2604ec03
AC
15115 -- legal if the generic formal is untagged limited, and the actual
15116 -- is non-limited.
15117
15118 if Is_Generic_Actual_Type (Parent_Type)
15119 and then In_Private_Part (Current_Scope)
15120 and then
15121 not Is_Tagged_Type
ef237104 15122 (Generic_Parent_Type (Parent (Parent_Type)))
2604ec03
AC
15123 then
15124 null;
15125
15126 else
15127 Error_Msg_NE
15128 ("parent type& of limited type must be limited",
15129 N, Parent_Type);
15130 end if;
653da906
RD
15131 end if;
15132 end if;
7ff2d234 15133
2ba431e5
YM
15134 -- In SPARK, there are no derived type definitions other than type
15135 -- extensions of tagged record types.
7ff2d234 15136
fe5d3068 15137 if No (Extension) then
cf895a01
AC
15138 Check_SPARK_Restriction
15139 ("derived type is not allowed", Original_Node (N));
7ff2d234 15140 end if;
996ae0b0
RK
15141 end Derived_Type_Declaration;
15142
6765b310
ES
15143 ------------------------
15144 -- Diagnose_Interface --
15145 ------------------------
15146
15147 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
15148 begin
15149 if not Is_Interface (E)
15150 and then E /= Any_Type
15151 then
15152 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
15153 end if;
15154 end Diagnose_Interface;
15155
996ae0b0
RK
15156 ----------------------------------
15157 -- Enumeration_Type_Declaration --
15158 ----------------------------------
15159
15160 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15161 Ev : Uint;
15162 L : Node_Id;
15163 R_Node : Node_Id;
15164 B_Node : Node_Id;
15165
15166 begin
15167 -- Create identifier node representing lower bound
15168
15169 B_Node := New_Node (N_Identifier, Sloc (Def));
15170 L := First (Literals (Def));
15171 Set_Chars (B_Node, Chars (L));
15172 Set_Entity (B_Node, L);
15173 Set_Etype (B_Node, T);
15174 Set_Is_Static_Expression (B_Node, True);
15175
15176 R_Node := New_Node (N_Range, Sloc (Def));
15177 Set_Low_Bound (R_Node, B_Node);
15178
15179 Set_Ekind (T, E_Enumeration_Type);
15180 Set_First_Literal (T, L);
15181 Set_Etype (T, T);
15182 Set_Is_Constrained (T);
15183
15184 Ev := Uint_0;
15185
15186 -- Loop through literals of enumeration type setting pos and rep values
00838d9a
AC
15187 -- except that if the Ekind is already set, then it means the literal
15188 -- was already constructed (case of a derived type declaration and we
15189 -- should not disturb the Pos and Rep values.
996ae0b0
RK
15190
15191 while Present (L) loop
15192 if Ekind (L) /= E_Enumeration_Literal then
15193 Set_Ekind (L, E_Enumeration_Literal);
15194 Set_Enumeration_Pos (L, Ev);
15195 Set_Enumeration_Rep (L, Ev);
15196 Set_Is_Known_Valid (L, True);
15197 end if;
15198
15199 Set_Etype (L, T);
15200 New_Overloaded_Entity (L);
15201 Generate_Definition (L);
15202 Set_Convention (L, Convention_Intrinsic);
15203
30196a76
RD
15204 -- Case of character literal
15205
996ae0b0
RK
15206 if Nkind (L) = N_Defining_Character_Literal then
15207 Set_Is_Character_Type (T, True);
30196a76
RD
15208
15209 -- Check violation of No_Wide_Characters
15210
7a963087 15211 if Restriction_Check_Required (No_Wide_Characters) then
30196a76
RD
15212 Get_Name_String (Chars (L));
15213
15214 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
15215 Check_Restriction (No_Wide_Characters, L);
15216 end if;
15217 end if;
996ae0b0
RK
15218 end if;
15219
15220 Ev := Ev + 1;
15221 Next (L);
15222 end loop;
15223
15224 -- Now create a node representing upper bound
15225
15226 B_Node := New_Node (N_Identifier, Sloc (Def));
15227 Set_Chars (B_Node, Chars (Last (Literals (Def))));
15228 Set_Entity (B_Node, Last (Literals (Def)));
15229 Set_Etype (B_Node, T);
15230 Set_Is_Static_Expression (B_Node, True);
15231
15232 Set_High_Bound (R_Node, B_Node);
2b73cf68
JM
15233
15234 -- Initialize various fields of the type. Some of this information
15235 -- may be overwritten later through rep.clauses.
15236
15237 Set_Scalar_Range (T, R_Node);
15238 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
15239 Set_Enum_Esize (T);
15240 Set_Enum_Pos_To_Rep (T, Empty);
996ae0b0 15241
fbf5a39b 15242 -- Set Discard_Names if configuration pragma set, or if there is
996ae0b0
RK
15243 -- a parameterless pragma in the current declarative region
15244
1824c168 15245 if Global_Discard_Names or else Discard_Names (Scope (T)) then
996ae0b0
RK
15246 Set_Discard_Names (T);
15247 end if;
07fc65c4
GB
15248
15249 -- Process end label if there is one
15250
15251 if Present (Def) then
15252 Process_End_Label (Def, 'e', T);
15253 end if;
996ae0b0
RK
15254 end Enumeration_Type_Declaration;
15255
996ae0b0 15256 ---------------------------------
fbf5a39b 15257 -- Expand_To_Stored_Constraint --
996ae0b0
RK
15258 ---------------------------------
15259
fbf5a39b 15260 function Expand_To_Stored_Constraint
996ae0b0 15261 (Typ : Entity_Id;
b0f26df5 15262 Constraint : Elist_Id) return Elist_Id
996ae0b0
RK
15263 is
15264 Explicitly_Discriminated_Type : Entity_Id;
15265 Expansion : Elist_Id;
15266 Discriminant : Entity_Id;
15267
15268 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
ffe9aba8 15269 -- Find the nearest type that actually specifies discriminants
996ae0b0
RK
15270
15271 ---------------------------------
15272 -- Type_With_Explicit_Discrims --
15273 ---------------------------------
15274
15275 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
15276 Typ : constant E := Base_Type (Id);
15277
15278 begin
15279 if Ekind (Typ) in Incomplete_Or_Private_Kind then
15280 if Present (Full_View (Typ)) then
15281 return Type_With_Explicit_Discrims (Full_View (Typ));
15282 end if;
15283
15284 else
15285 if Has_Discriminants (Typ) then
15286 return Typ;
15287 end if;
15288 end if;
15289
15290 if Etype (Typ) = Typ then
15291 return Empty;
15292 elsif Has_Discriminants (Typ) then
15293 return Typ;
15294 else
15295 return Type_With_Explicit_Discrims (Etype (Typ));
15296 end if;
15297
15298 end Type_With_Explicit_Discrims;
15299
fbf5a39b 15300 -- Start of processing for Expand_To_Stored_Constraint
996ae0b0
RK
15301
15302 begin
15303 if No (Constraint)
15304 or else Is_Empty_Elmt_List (Constraint)
15305 then
15306 return No_Elist;
15307 end if;
15308
15309 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
15310
15311 if No (Explicitly_Discriminated_Type) then
15312 return No_Elist;
15313 end if;
15314
15315 Expansion := New_Elmt_List;
15316
15317 Discriminant :=
fbf5a39b 15318 First_Stored_Discriminant (Explicitly_Discriminated_Type);
996ae0b0 15319 while Present (Discriminant) loop
996ae0b0
RK
15320 Append_Elmt (
15321 Get_Discriminant_Value (
15322 Discriminant, Explicitly_Discriminated_Type, Constraint),
15323 Expansion);
fbf5a39b 15324 Next_Stored_Discriminant (Discriminant);
996ae0b0
RK
15325 end loop;
15326
15327 return Expansion;
fbf5a39b 15328 end Expand_To_Stored_Constraint;
996ae0b0 15329
dc06abec
RD
15330 ---------------------------
15331 -- Find_Hidden_Interface --
15332 ---------------------------
15333
15334 function Find_Hidden_Interface
15335 (Src : Elist_Id;
15336 Dest : Elist_Id) return Entity_Id
15337 is
15338 Iface : Entity_Id;
15339 Iface_Elmt : Elmt_Id;
15340
15341 begin
15342 if Present (Src) and then Present (Dest) then
15343 Iface_Elmt := First_Elmt (Src);
15344 while Present (Iface_Elmt) loop
15345 Iface := Node (Iface_Elmt);
15346
15347 if Is_Interface (Iface)
15348 and then not Contain_Interface (Iface, Dest)
15349 then
15350 return Iface;
15351 end if;
15352
15353 Next_Elmt (Iface_Elmt);
15354 end loop;
15355 end if;
15356
15357 return Empty;
15358 end Find_Hidden_Interface;
15359
996ae0b0
RK
15360 --------------------
15361 -- Find_Type_Name --
15362 --------------------
15363
15364 function Find_Type_Name (N : Node_Id) return Entity_Id is
15365 Id : constant Entity_Id := Defining_Identifier (N);
15366 Prev : Entity_Id;
15367 New_Id : Entity_Id;
15368 Prev_Par : Node_Id;
15369
9479ded4
AC
15370 procedure Check_Duplicate_Aspects;
15371 -- Check that aspects specified in a completion have not been specified
15372 -- already in the partial view. Type_Invariant and others can be
15373 -- specified on either view but never on both.
15374
33931112 15375 procedure Tag_Mismatch;
abed5dc6 15376 -- Diagnose a tagged partial view whose full view is untagged.
33931112
JM
15377 -- We post the message on the full view, with a reference to
15378 -- the previous partial view. The partial view can be private
15379 -- or incomplete, and these are handled in a different manner,
15380 -- so we determine the position of the error message from the
15381 -- respective slocs of both.
15382
9479ded4
AC
15383 -----------------------------
15384 -- Check_Duplicate_Aspects --
15385 -----------------------------
15386 procedure Check_Duplicate_Aspects is
15387 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
15388 Full_Aspects : constant List_Id := Aspect_Specifications (N);
15389 F_Spec, P_Spec : Node_Id;
15390
15391 begin
15392 if Present (Prev_Aspects) and then Present (Full_Aspects) then
15393 F_Spec := First (Full_Aspects);
15394 while Present (F_Spec) loop
15395 P_Spec := First (Prev_Aspects);
15396 while Present (P_Spec) loop
15397 if
15398 Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
15399 then
15400 Error_Msg_N
15401 ("aspect already specified in private declaration",
15402 F_Spec);
15403 Remove (F_Spec);
15404 return;
15405 end if;
15406
15407 Next (P_Spec);
15408 end loop;
15409
15410 Next (F_Spec);
15411 end loop;
15412 end if;
15413 end Check_Duplicate_Aspects;
15414
33931112
JM
15415 ------------------
15416 -- Tag_Mismatch --
15417 ------------------
15418
15419 procedure Tag_Mismatch is
15420 begin
15421 if Sloc (Prev) < Sloc (Id) then
6191e212
AC
15422 if Ada_Version >= Ada_2012
15423 and then Nkind (N) = N_Private_Type_Declaration
15424 then
15425 Error_Msg_NE
15426 ("declaration of private } must be a tagged type ", Id, Prev);
15427 else
15428 Error_Msg_NE
15429 ("full declaration of } must be a tagged type ", Id, Prev);
15430 end if;
e917e3b8 15431
33931112 15432 else
6191e212
AC
15433 if Ada_Version >= Ada_2012
15434 and then Nkind (N) = N_Private_Type_Declaration
15435 then
15436 Error_Msg_NE
15437 ("declaration of private } must be a tagged type ", Prev, Id);
15438 else
15439 Error_Msg_NE
15440 ("full declaration of } must be a tagged type ", Prev, Id);
15441 end if;
33931112
JM
15442 end if;
15443 end Tag_Mismatch;
15444
d8221f45 15445 -- Start of processing for Find_Type_Name
33931112 15446
996ae0b0 15447 begin
71d9e9f2 15448 -- Find incomplete declaration, if one was given
996ae0b0
RK
15449
15450 Prev := Current_Entity_In_Scope (Id);
15451
6191e212
AC
15452 -- New type declaration
15453
15454 if No (Prev) then
15455 Enter_Name (Id);
15456 return Id;
996ae0b0 15457
6191e212 15458 -- Previous declaration exists
996ae0b0 15459
6191e212 15460 else
996ae0b0
RK
15461 Prev_Par := Parent (Prev);
15462
6191e212
AC
15463 -- Error if not incomplete/private case except if previous
15464 -- declaration is implicit, etc. Enter_Name will emit error if
15465 -- appropriate.
15466
996ae0b0
RK
15467 if not Is_Incomplete_Or_Private_Type (Prev) then
15468 Enter_Name (Id);
15469 New_Id := Id;
15470
6191e212
AC
15471 -- Check invalid completion of private or incomplete type
15472
7d7af38a
JM
15473 elsif not Nkind_In (N, N_Full_Type_Declaration,
15474 N_Task_Type_Declaration,
15475 N_Protected_Type_Declaration)
6191e212
AC
15476 and then
15477 (Ada_Version < Ada_2012
e917e3b8
AC
15478 or else not Is_Incomplete_Type (Prev)
15479 or else not Nkind_In (N, N_Private_Type_Declaration,
15480 N_Private_Extension_Declaration))
996ae0b0
RK
15481 then
15482 -- Completion must be a full type declarations (RM 7.3(4))
15483
15484 Error_Msg_Sloc := Sloc (Prev);
15485 Error_Msg_NE ("invalid completion of }", Id, Prev);
15486
15487 -- Set scope of Id to avoid cascaded errors. Entity is never
15488 -- examined again, except when saving globals in generics.
15489
15490 Set_Scope (Id, Current_Scope);
15491 New_Id := Id;
15492
d4429d51
ES
15493 -- If this is a repeated incomplete declaration, no further
15494 -- checks are possible.
15495
15496 if Nkind (N) = N_Incomplete_Type_Declaration then
15497 return Prev;
15498 end if;
15499
996ae0b0
RK
15500 -- Case of full declaration of incomplete type
15501
6191e212
AC
15502 elsif Ekind (Prev) = E_Incomplete_Type
15503 and then (Ada_Version < Ada_2012
e606088a
AC
15504 or else No (Full_View (Prev))
15505 or else not Is_Private_Type (Full_View (Prev)))
6191e212 15506 then
996ae0b0 15507
a5b62485
AC
15508 -- Indicate that the incomplete declaration has a matching full
15509 -- declaration. The defining occurrence of the incomplete
996ae0b0
RK
15510 -- declaration remains the visible one, and the procedure
15511 -- Get_Full_View dereferences it whenever the type is used.
15512
15513 if Present (Full_View (Prev)) then
15514 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
15515 end if;
15516
56e94186 15517 Set_Full_View (Prev, Id);
996ae0b0
RK
15518 Append_Entity (Id, Current_Scope);
15519 Set_Is_Public (Id, Is_Public (Prev));
15520 Set_Is_Internal (Id);
15521 New_Id := Prev;
15522
6191e212
AC
15523 -- If the incomplete view is tagged, a class_wide type has been
15524 -- created already. Use it for the private type as well, in order
15525 -- to prevent multiple incompatible class-wide types that may be
15526 -- created for self-referential anonymous access components.
15527
15528 if Is_Tagged_Type (Prev)
15529 and then Present (Class_Wide_Type (Prev))
15530 then
15531 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
15532 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
98ee6f8d
AC
15533
15534 -- If the incomplete type is completed by a private declaration
15535 -- the class-wide type remains associated with the incomplete
15536 -- type, to prevent order-of-elaboration issues in gigi, else
15537 -- we associate the class-wide type with the known full view.
15538
15539 if Nkind (N) /= N_Private_Type_Declaration then
15540 Set_Etype (Class_Wide_Type (Id), Id);
15541 end if;
6191e212
AC
15542 end if;
15543
996ae0b0
RK
15544 -- Case of full declaration of private type
15545
15546 else
6191e212
AC
15547 -- If the private type was a completion of an incomplete type then
15548 -- update Prev to reference the private type
15549
15550 if Ada_Version >= Ada_2012
15551 and then Ekind (Prev) = E_Incomplete_Type
15552 and then Present (Full_View (Prev))
15553 and then Is_Private_Type (Full_View (Prev))
15554 then
15555 Prev := Full_View (Prev);
15556 Prev_Par := Parent (Prev);
15557 end if;
15558
996ae0b0
RK
15559 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
15560 if Etype (Prev) /= Prev then
15561
15562 -- Prev is a private subtype or a derived type, and needs
15563 -- no completion.
15564
15565 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
15566 New_Id := Id;
15567
15568 elsif Ekind (Prev) = E_Private_Type
7d7af38a
JM
15569 and then Nkind_In (N, N_Task_Type_Declaration,
15570 N_Protected_Type_Declaration)
996ae0b0
RK
15571 then
15572 Error_Msg_N
15573 ("completion of nonlimited type cannot be limited", N);
57193e09
TQ
15574
15575 elsif Ekind (Prev) = E_Record_Type_With_Private
7d7af38a
JM
15576 and then Nkind_In (N, N_Task_Type_Declaration,
15577 N_Protected_Type_Declaration)
57193e09
TQ
15578 then
15579 if not Is_Limited_Record (Prev) then
15580 Error_Msg_N
15581 ("completion of nonlimited type cannot be limited", N);
15582
15583 elsif No (Interface_List (N)) then
15584 Error_Msg_N
15585 ("completion of tagged private type must be tagged",
ff2e7c1e 15586 N);
57193e09 15587 end if;
26a43556
AC
15588
15589 elsif Nkind (N) = N_Full_Type_Declaration
15590 and then
15591 Nkind (Type_Definition (N)) = N_Record_Definition
15592 and then Interface_Present (Type_Definition (N))
15593 then
15594 Error_Msg_N
ff2e7c1e 15595 ("completion of private type cannot be an interface", N);
996ae0b0
RK
15596 end if;
15597
dc06abec
RD
15598 -- Ada 2005 (AI-251): Private extension declaration of a task
15599 -- type or a protected type. This case arises when covering
15600 -- interface types.
758c442c 15601
7d7af38a
JM
15602 elsif Nkind_In (N, N_Task_Type_Declaration,
15603 N_Protected_Type_Declaration)
758c442c
GD
15604 then
15605 null;
15606
996ae0b0
RK
15607 elsif Nkind (N) /= N_Full_Type_Declaration
15608 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
15609 then
71d9e9f2
ES
15610 Error_Msg_N
15611 ("full view of private extension must be an extension", N);
996ae0b0
RK
15612
15613 elsif not (Abstract_Present (Parent (Prev)))
15614 and then Abstract_Present (Type_Definition (N))
15615 then
71d9e9f2
ES
15616 Error_Msg_N
15617 ("full view of non-abstract extension cannot be abstract", N);
996ae0b0
RK
15618 end if;
15619
15620 if not In_Private_Part (Current_Scope) then
15621 Error_Msg_N
71d9e9f2 15622 ("declaration of full view must appear in private part", N);
996ae0b0
RK
15623 end if;
15624
9479ded4
AC
15625 if Ada_Version >= Ada_2012 then
15626 Check_Duplicate_Aspects;
15627 end if;
15628
996ae0b0 15629 Copy_And_Swap (Prev, Id);
996ae0b0
RK
15630 Set_Has_Private_Declaration (Prev);
15631 Set_Has_Private_Declaration (Id);
07fc65c4 15632
833eaa8a
AC
15633 -- Preserve aspect and iterator flags that may have been set on
15634 -- the partial view.
57a8057a
AC
15635
15636 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
15637 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
15638
07fc65c4
GB
15639 -- If no error, propagate freeze_node from private to full view.
15640 -- It may have been generated for an early operational item.
15641
15642 if Present (Freeze_Node (Id))
15643 and then Serious_Errors_Detected = 0
15644 and then No (Full_View (Id))
15645 then
15646 Set_Freeze_Node (Prev, Freeze_Node (Id));
15647 Set_Freeze_Node (Id, Empty);
15648 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
15649 end if;
15650
15651 Set_Full_View (Id, Prev);
996ae0b0
RK
15652 New_Id := Prev;
15653 end if;
15654
33931112 15655 -- Verify that full declaration conforms to partial one
996ae0b0
RK
15656
15657 if Is_Incomplete_Or_Private_Type (Prev)
15658 and then Present (Discriminant_Specifications (Prev_Par))
15659 then
15660 if Present (Discriminant_Specifications (N)) then
15661 if Ekind (Prev) = E_Incomplete_Type then
15662 Check_Discriminant_Conformance (N, Prev, Prev);
15663 else
15664 Check_Discriminant_Conformance (N, Prev, Id);
15665 end if;
15666
15667 else
15668 Error_Msg_N
15669 ("missing discriminants in full type declaration", N);
15670
15671 -- To avoid cascaded errors on subsequent use, share the
15672 -- discriminants of the partial view.
15673
15674 Set_Discriminant_Specifications (N,
15675 Discriminant_Specifications (Prev_Par));
15676 end if;
15677 end if;
15678
33931112 15679 -- A prior untagged partial view can have an associated class-wide
abed5dc6
AC
15680 -- type due to use of the class attribute, and in this case the full
15681 -- type must also be tagged. This Ada 95 usage is deprecated in favor
15682 -- of incomplete tagged declarations, but we check for it.
996ae0b0
RK
15683
15684 if Is_Type (Prev)
15685 and then (Is_Tagged_Type (Prev)
6191e212 15686 or else Present (Class_Wide_Type (Prev)))
996ae0b0 15687 then
6191e212 15688 -- Ada 2012 (AI05-0162): A private type may be the completion of
e917e3b8 15689 -- an incomplete type.
6191e212
AC
15690
15691 if Ada_Version >= Ada_2012
15692 and then Is_Incomplete_Type (Prev)
15693 and then Nkind_In (N, N_Private_Type_Declaration,
15694 N_Private_Extension_Declaration)
15695 then
15696 -- No need to check private extensions since they are tagged
15697
15698 if Nkind (N) = N_Private_Type_Declaration
15699 and then not Tagged_Present (N)
15700 then
15701 Tag_Mismatch;
15702 end if;
15703
af4133b2
ST
15704 -- The full declaration is either a tagged type (including
15705 -- a synchronized type that implements interfaces) or a
15706 -- type extension, otherwise this is an error.
15707
6191e212
AC
15708 elsif Nkind_In (N, N_Task_Type_Declaration,
15709 N_Protected_Type_Declaration)
af4133b2
ST
15710 then
15711 if No (Interface_List (N))
15712 and then not Error_Posted (N)
15713 then
33931112 15714 Tag_Mismatch;
af4133b2
ST
15715 end if;
15716
15717 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
15718
15719 -- Indicate that the previous declaration (tagged incomplete
15720 -- or private declaration) requires the same on the full one.
996ae0b0 15721
996ae0b0 15722 if not Tagged_Present (Type_Definition (N)) then
33931112 15723 Tag_Mismatch;
996ae0b0 15724 Set_Is_Tagged_Type (Id);
996ae0b0
RK
15725 end if;
15726
15727 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
15728 if No (Record_Extension_Part (Type_Definition (N))) then
ed2233dc
AC
15729 Error_Msg_NE
15730 ("full declaration of } must be a record extension",
15731 Prev, Id);
93bcda23 15732
03b64787 15733 -- Set some attributes to produce a usable full view
93bcda23 15734
996ae0b0 15735 Set_Is_Tagged_Type (Id);
996ae0b0
RK
15736 end if;
15737
15738 else
33931112 15739 Tag_Mismatch;
996ae0b0
RK
15740 end if;
15741 end if;
94bbf008 15742
99d520ad
ES
15743 if Present (Prev)
15744 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
15745 and then Present (Premature_Use (Parent (Prev)))
15746 then
15747 Error_Msg_Sloc := Sloc (N);
15748 Error_Msg_N
15749 ("\full declaration #", Premature_Use (Parent (Prev)));
15750 end if;
996ae0b0
RK
15751
15752 return New_Id;
996ae0b0
RK
15753 end if;
15754 end Find_Type_Name;
15755
15756 -------------------------
15757 -- Find_Type_Of_Object --
15758 -------------------------
15759
15760 function Find_Type_Of_Object
15761 (Obj_Def : Node_Id;
b0f26df5 15762 Related_Nod : Node_Id) return Entity_Id
996ae0b0
RK
15763 is
15764 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
a397db96 15765 P : Node_Id := Parent (Obj_Def);
996ae0b0
RK
15766 T : Entity_Id;
15767 Nam : Name_Id;
15768
15769 begin
a397db96
AC
15770 -- If the parent is a component_definition node we climb to the
15771 -- component_declaration node
15772
15773 if Nkind (P) = N_Component_Definition then
15774 P := Parent (P);
15775 end if;
15776
996ae0b0
RK
15777 -- Case of an anonymous array subtype
15778
7d7af38a
JM
15779 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
15780 N_Unconstrained_Array_Definition)
996ae0b0
RK
15781 then
15782 T := Empty;
15783 Array_Type_Declaration (T, Obj_Def);
15784
ffe9aba8 15785 -- Create an explicit subtype whenever possible
996ae0b0
RK
15786
15787 elsif Nkind (P) /= N_Component_Declaration
15788 and then Def_Kind = N_Subtype_Indication
15789 then
15790 -- Base name of subtype on object name, which will be unique in
15791 -- the current scope.
15792
15793 -- If this is a duplicate declaration, return base type, to avoid
15794 -- generating duplicate anonymous types.
15795
15796 if Error_Posted (P) then
15797 Analyze (Subtype_Mark (Obj_Def));
15798 return Entity (Subtype_Mark (Obj_Def));
15799 end if;
15800
15801 Nam :=
15802 New_External_Name
15803 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
15804
15805 T := Make_Defining_Identifier (Sloc (P), Nam);
15806
15807 Insert_Action (Obj_Def,
15808 Make_Subtype_Declaration (Sloc (P),
15809 Defining_Identifier => T,
15810 Subtype_Indication => Relocate_Node (Obj_Def)));
15811
aa720a54 15812 -- This subtype may need freezing, and this will not be done
a5b62485
AC
15813 -- automatically if the object declaration is not in declarative
15814 -- part. Since this is an object declaration, the type cannot always
15815 -- be frozen here. Deferred constants do not freeze their type
15816 -- (which often enough will be private).
996ae0b0
RK
15817
15818 if Nkind (P) = N_Object_Declaration
15819 and then Constant_Present (P)
15820 and then No (Expression (P))
15821 then
15822 null;
e2ef0ff6
AC
15823
15824 -- Here we freeze the base type of object type to catch premature use
15825 -- of discriminated private type without a full view.
15826
996ae0b0 15827 else
e2ef0ff6 15828 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
996ae0b0
RK
15829 end if;
15830
758c442c
GD
15831 -- Ada 2005 AI-406: the object definition in an object declaration
15832 -- can be an access definition.
15833
15834 elsif Def_Kind = N_Access_Definition then
15835 T := Access_Definition (Related_Nod, Obj_Def);
d15f9422 15836
996c8821
RD
15837 Set_Is_Local_Anonymous_Access
15838 (T,
15839 V => (Ada_Version < Ada_2012)
15840 or else (Nkind (P) /= N_Object_Declaration)
15841 or else Is_Library_Level_Entity (Defining_Identifier (P)));
88b32fc3
BD
15842
15843 -- Otherwise, the object definition is just a subtype_mark
758c442c 15844
996ae0b0
RK
15845 else
15846 T := Process_Subtype (Obj_Def, Related_Nod);
b60a3f26
AC
15847
15848 -- If expansion is disabled an object definition that is an aggregate
15849 -- will not get expanded and may lead to scoping problems in the back
15850 -- end, if the object is referenced in an inner scope. In that case
15851 -- create an itype reference for the object definition now. This
15852 -- may be redundant in some cases, but harmless.
15853
15854 if Is_Itype (T)
15855 and then Nkind (Related_Nod) = N_Object_Declaration
15856 and then ASIS_Mode
15857 then
15858 Build_Itype_Reference (T, Related_Nod);
15859 end if;
996ae0b0
RK
15860 end if;
15861
15862 return T;
15863 end Find_Type_Of_Object;
15864
15865 --------------------------------
15866 -- Find_Type_Of_Subtype_Indic --
15867 --------------------------------
15868
15869 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
15870 Typ : Entity_Id;
15871
15872 begin
15873 -- Case of subtype mark with a constraint
15874
15875 if Nkind (S) = N_Subtype_Indication then
15876 Find_Type (Subtype_Mark (S));
15877 Typ := Entity (Subtype_Mark (S));
15878
15879 if not
15880 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
15881 then
15882 Error_Msg_N
15883 ("incorrect constraint for this kind of type", Constraint (S));
15884 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
15885 end if;
15886
15887 -- Otherwise we have a subtype mark without a constraint
15888
dd5875a6
ES
15889 elsif Error_Posted (S) then
15890 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
15891 return Any_Type;
15892
996ae0b0
RK
15893 else
15894 Find_Type (S);
15895 Typ := Entity (S);
15896 end if;
15897
ce4a6e84
RD
15898 -- Check No_Wide_Characters restriction
15899
30196a76 15900 Check_Wide_Character_Restriction (Typ, S);
996ae0b0
RK
15901
15902 return Typ;
15903 end Find_Type_Of_Subtype_Indic;
15904
15905 -------------------------------------
15906 -- Floating_Point_Type_Declaration --
15907 -------------------------------------
15908
15909 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15910 Digs : constant Node_Id := Digits_Expression (Def);
15b682ca 15911 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
996ae0b0
RK
15912 Digs_Val : Uint;
15913 Base_Typ : Entity_Id;
15914 Implicit_Base : Entity_Id;
15915 Bound : Node_Id;
15916
15917 function Can_Derive_From (E : Entity_Id) return Boolean;
15b682ca
GB
15918 -- Find if given digits value, and possibly a specified range, allows
15919 -- derivation from specified type
996ae0b0 15920
70c34e1c
AC
15921 function Find_Base_Type return Entity_Id;
15922 -- Find a predefined base type that Def can derive from, or generate
15923 -- an error and substitute Long_Long_Float if none exists.
15924
fbf5a39b
AC
15925 ---------------------
15926 -- Can_Derive_From --
15927 ---------------------
15928
996ae0b0
RK
15929 function Can_Derive_From (E : Entity_Id) return Boolean is
15930 Spec : constant Entity_Id := Real_Range_Specification (Def);
15931
15932 begin
a17e8c05
AC
15933 -- Check specified "digits" constraint
15934
996ae0b0
RK
15935 if Digs_Val > Digits_Value (E) then
15936 return False;
15937 end if;
15938
a17e8c05
AC
15939 -- Avoid types not matching pragma Float_Representation, if present
15940
15941 if (Opt.Float_Format = 'I' and then Float_Rep (E) /= IEEE_Binary)
15942 or else
15943 (Opt.Float_Format = 'V' and then Float_Rep (E) /= VAX_Native)
15944 then
15945 return False;
15946 end if;
15947
15948 -- Check for matching range, if specified
15949
996ae0b0
RK
15950 if Present (Spec) then
15951 if Expr_Value_R (Type_Low_Bound (E)) >
15952 Expr_Value_R (Low_Bound (Spec))
15953 then
15954 return False;
15955 end if;
15956
15957 if Expr_Value_R (Type_High_Bound (E)) <
15958 Expr_Value_R (High_Bound (Spec))
15959 then
15960 return False;
15961 end if;
15962 end if;
15963
15964 return True;
15965 end Can_Derive_From;
15966
70c34e1c
AC
15967 --------------------
15968 -- Find_Base_Type --
15969 --------------------
15970
15971 function Find_Base_Type return Entity_Id is
15972 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
15973
15974 begin
15975 -- Iterate over the predefined types in order, returning the first
15976 -- one that Def can derive from.
15977
15978 while Present (Choice) loop
15979 if Can_Derive_From (Node (Choice)) then
15980 return Node (Choice);
15981 end if;
15982
15983 Next_Elmt (Choice);
15984 end loop;
15985
15986 -- If we can't derive from any existing type, use Long_Long_Float
15987 -- and give appropriate message explaining the problem.
15988
15989 if Digs_Val > Max_Digs_Val then
15990 -- It might be the case that there is a type with the requested
15991 -- range, just not the combination of digits and range.
15992
15993 Error_Msg_N
15994 ("no predefined type has requested range and precision",
15995 Real_Range_Specification (Def));
15996
15997 else
15998 Error_Msg_N
15999 ("range too large for any predefined type",
16000 Real_Range_Specification (Def));
16001 end if;
16002
16003 return Standard_Long_Long_Float;
16004 end Find_Base_Type;
16005
996ae0b0
RK
16006 -- Start of processing for Floating_Point_Type_Declaration
16007
16008 begin
16009 Check_Restriction (No_Floating_Point, Def);
16010
16011 -- Create an implicit base type
16012
16013 Implicit_Base :=
16014 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
16015
16016 -- Analyze and verify digits value
16017
16018 Analyze_And_Resolve (Digs, Any_Integer);
16019 Check_Digits_Expression (Digs);
16020 Digs_Val := Expr_Value (Digs);
16021
16022 -- Process possible range spec and find correct type to derive from
16023
16024 Process_Real_Range_Specification (Def);
16025
15b682ca
GB
16026 -- Check that requested number of digits is not too high.
16027
16028 if Digs_Val > Max_Digs_Val then
16029 -- The check for Max_Base_Digits may be somewhat expensive, as it
16030 -- requires reading System, so only do it when necessary.
16031
16032 declare
16033 Max_Base_Digits : constant Uint :=
ded8909b
AC
16034 Expr_Value
16035 (Expression
16036 (Parent (RTE (RE_Max_Base_Digits))));
16037
15b682ca
GB
16038 begin
16039 if Digs_Val > Max_Base_Digits then
16040 Error_Msg_Uint_1 := Max_Base_Digits;
16041 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
16042
16043 elsif No (Real_Range_Specification (Def)) then
16044 Error_Msg_Uint_1 := Max_Digs_Val;
16045 Error_Msg_N ("types with more than ^ digits need range spec "
a4640a39 16046 & "(RM 3.5.7(6))", Digs);
15b682ca
GB
16047 end if;
16048 end;
16049 end if;
996ae0b0 16050
70c34e1c 16051 -- Find a suitable type to derive from or complain and use a substitute
996ae0b0 16052
70c34e1c 16053 Base_Typ := Find_Base_Type;
996ae0b0
RK
16054
16055 -- If there are bounds given in the declaration use them as the bounds
16056 -- of the type, otherwise use the bounds of the predefined base type
16057 -- that was chosen based on the Digits value.
16058
16059 if Present (Real_Range_Specification (Def)) then
16060 Set_Scalar_Range (T, Real_Range_Specification (Def));
16061 Set_Is_Constrained (T);
16062
16063 -- The bounds of this range must be converted to machine numbers
16064 -- in accordance with RM 4.9(38).
16065
16066 Bound := Type_Low_Bound (T);
16067
16068 if Nkind (Bound) = N_Real_Literal then
fbf5a39b
AC
16069 Set_Realval
16070 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
996ae0b0
RK
16071 Set_Is_Machine_Number (Bound);
16072 end if;
16073
16074 Bound := Type_High_Bound (T);
16075
16076 if Nkind (Bound) = N_Real_Literal then
fbf5a39b
AC
16077 Set_Realval
16078 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
996ae0b0
RK
16079 Set_Is_Machine_Number (Bound);
16080 end if;
16081
16082 else
16083 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
16084 end if;
16085
16086 -- Complete definition of implicit base and declared first subtype
16087
16088 Set_Etype (Implicit_Base, Base_Typ);
16089
16090 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
16091 Set_Size_Info (Implicit_Base, (Base_Typ));
16092 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
16093 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
16094 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
23c799b1 16095 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
996ae0b0
RK
16096
16097 Set_Ekind (T, E_Floating_Point_Subtype);
16098 Set_Etype (T, Implicit_Base);
16099
16100 Set_Size_Info (T, (Implicit_Base));
16101 Set_RM_Size (T, RM_Size (Implicit_Base));
16102 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
16103 Set_Digits_Value (T, Digs_Val);
996ae0b0
RK
16104 end Floating_Point_Type_Declaration;
16105
16106 ----------------------------
16107 -- Get_Discriminant_Value --
16108 ----------------------------
16109
ffe9aba8 16110 -- This is the situation:
996ae0b0
RK
16111
16112 -- There is a non-derived type
16113
16114 -- type T0 (Dx, Dy, Dz...)
16115
a5b62485
AC
16116 -- There are zero or more levels of derivation, with each derivation
16117 -- either purely inheriting the discriminants, or defining its own.
996ae0b0
RK
16118
16119 -- type Ti is new Ti-1
16120 -- or
16121 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
16122 -- or
16123 -- subtype Ti is ...
16124
a5b62485
AC
16125 -- The subtype issue is avoided by the use of Original_Record_Component,
16126 -- and the fact that derived subtypes also derive the constraints.
996ae0b0
RK
16127
16128 -- This chain leads back from
16129
16130 -- Typ_For_Constraint
16131
16132 -- Typ_For_Constraint has discriminants, and the value for each
16133 -- discriminant is given by its corresponding Elmt of Constraints.
16134
71d9e9f2 16135 -- Discriminant is some discriminant in this hierarchy
996ae0b0 16136
71d9e9f2 16137 -- We need to return its value
996ae0b0
RK
16138
16139 -- We do this by recursively searching each level, and looking for
16140 -- Discriminant. Once we get to the bottom, we start backing up
16141 -- returning the value for it which may in turn be a discriminant
16142 -- further up, so on the backup we continue the substitution.
16143
16144 function Get_Discriminant_Value
16145 (Discriminant : Entity_Id;
16146 Typ_For_Constraint : Entity_Id;
b0f26df5 16147 Constraint : Elist_Id) return Node_Id
996ae0b0 16148 is
78c0f016
AC
16149 function Root_Corresponding_Discriminant
16150 (Discr : Entity_Id) return Entity_Id;
16151 -- Given a discriminant, traverse the chain of inherited discriminants
16152 -- and return the topmost discriminant.
16153
fbf5a39b 16154 function Search_Derivation_Levels
996ae0b0
RK
16155 (Ti : Entity_Id;
16156 Discrim_Values : Elist_Id;
b0f26df5 16157 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
996ae0b0
RK
16158 -- This is the routine that performs the recursive search of levels
16159 -- as described above.
16160
78c0f016
AC
16161 -------------------------------------
16162 -- Root_Corresponding_Discriminant --
16163 -------------------------------------
16164
16165 function Root_Corresponding_Discriminant
16166 (Discr : Entity_Id) return Entity_Id
16167 is
3419a445 16168 D : Entity_Id;
78c0f016
AC
16169
16170 begin
3419a445 16171 D := Discr;
78c0f016
AC
16172 while Present (Corresponding_Discriminant (D)) loop
16173 D := Corresponding_Discriminant (D);
16174 end loop;
16175
16176 return D;
16177 end Root_Corresponding_Discriminant;
16178
fbf5a39b
AC
16179 ------------------------------
16180 -- Search_Derivation_Levels --
16181 ------------------------------
16182
16183 function Search_Derivation_Levels
996ae0b0
RK
16184 (Ti : Entity_Id;
16185 Discrim_Values : Elist_Id;
b0f26df5 16186 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
996ae0b0
RK
16187 is
16188 Assoc : Elmt_Id;
16189 Disc : Entity_Id;
16190 Result : Node_Or_Entity_Id;
16191 Result_Entity : Node_Id;
16192
16193 begin
16194 -- If inappropriate type, return Error, this happens only in
16195 -- cascaded error situations, and we want to avoid a blow up.
16196
16197 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
16198 return Error;
16199 end if;
16200
fbf5a39b 16201 -- Look deeper if possible. Use Stored_Constraints only for
996ae0b0
RK
16202 -- untagged types. For tagged types use the given constraint.
16203 -- This asymmetry needs explanation???
16204
fbf5a39b
AC
16205 if not Stored_Discrim_Values
16206 and then Present (Stored_Constraint (Ti))
996ae0b0
RK
16207 and then not Is_Tagged_Type (Ti)
16208 then
fbf5a39b
AC
16209 Result :=
16210 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
996ae0b0
RK
16211 else
16212 declare
fbf5a39b 16213 Td : constant Entity_Id := Etype (Ti);
996ae0b0 16214
fbf5a39b 16215 begin
996ae0b0
RK
16216 if Td = Ti then
16217 Result := Discriminant;
16218
16219 else
fbf5a39b 16220 if Present (Stored_Constraint (Ti)) then
996ae0b0 16221 Result :=
fbf5a39b
AC
16222 Search_Derivation_Levels
16223 (Td, Stored_Constraint (Ti), True);
996ae0b0
RK
16224 else
16225 Result :=
fbf5a39b
AC
16226 Search_Derivation_Levels
16227 (Td, Discrim_Values, Stored_Discrim_Values);
996ae0b0
RK
16228 end if;
16229 end if;
16230 end;
16231 end if;
16232
16233 -- Extra underlying places to search, if not found above. For
16234 -- concurrent types, the relevant discriminant appears in the
16235 -- corresponding record. For a type derived from a private type
16236 -- without discriminant, the full view inherits the discriminants
16237 -- of the full view of the parent.
16238
16239 if Result = Discriminant then
16240 if Is_Concurrent_Type (Ti)
16241 and then Present (Corresponding_Record_Type (Ti))
16242 then
16243 Result :=
fbf5a39b 16244 Search_Derivation_Levels (
996ae0b0
RK
16245 Corresponding_Record_Type (Ti),
16246 Discrim_Values,
fbf5a39b 16247 Stored_Discrim_Values);
996ae0b0
RK
16248
16249 elsif Is_Private_Type (Ti)
16250 and then not Has_Discriminants (Ti)
16251 and then Present (Full_View (Ti))
16252 and then Etype (Full_View (Ti)) /= Ti
16253 then
16254 Result :=
fbf5a39b 16255 Search_Derivation_Levels (
996ae0b0
RK
16256 Full_View (Ti),
16257 Discrim_Values,
fbf5a39b 16258 Stored_Discrim_Values);
996ae0b0
RK
16259 end if;
16260 end if;
16261
71d9e9f2
ES
16262 -- If Result is not a (reference to a) discriminant, return it,
16263 -- otherwise set Result_Entity to the discriminant.
996ae0b0
RK
16264
16265 if Nkind (Result) = N_Defining_Identifier then
996ae0b0 16266 pragma Assert (Result = Discriminant);
996ae0b0
RK
16267 Result_Entity := Result;
16268
16269 else
16270 if not Denotes_Discriminant (Result) then
16271 return Result;
16272 end if;
16273
16274 Result_Entity := Entity (Result);
16275 end if;
16276
16277 -- See if this level of derivation actually has discriminants
16278 -- because tagged derivations can add them, hence the lower
16279 -- levels need not have any.
16280
16281 if not Has_Discriminants (Ti) then
16282 return Result;
16283 end if;
16284
16285 -- Scan Ti's discriminants for Result_Entity,
16286 -- and return its corresponding value, if any.
16287
16288 Result_Entity := Original_Record_Component (Result_Entity);
16289
16290 Assoc := First_Elmt (Discrim_Values);
16291
fbf5a39b
AC
16292 if Stored_Discrim_Values then
16293 Disc := First_Stored_Discriminant (Ti);
996ae0b0
RK
16294 else
16295 Disc := First_Discriminant (Ti);
16296 end if;
16297
16298 while Present (Disc) loop
996ae0b0
RK
16299 pragma Assert (Present (Assoc));
16300
16301 if Original_Record_Component (Disc) = Result_Entity then
16302 return Node (Assoc);
16303 end if;
16304
16305 Next_Elmt (Assoc);
16306
fbf5a39b
AC
16307 if Stored_Discrim_Values then
16308 Next_Stored_Discriminant (Disc);
996ae0b0
RK
16309 else
16310 Next_Discriminant (Disc);
16311 end if;
16312 end loop;
16313
16314 -- Could not find it
16315 --
16316 return Result;
fbf5a39b 16317 end Search_Derivation_Levels;
996ae0b0 16318
ce4a6e84
RD
16319 -- Local Variables
16320
996ae0b0
RK
16321 Result : Node_Or_Entity_Id;
16322
16323 -- Start of processing for Get_Discriminant_Value
16324
16325 begin
71d9e9f2
ES
16326 -- ??? This routine is a gigantic mess and will be deleted. For the
16327 -- time being just test for the trivial case before calling recurse.
996ae0b0
RK
16328
16329 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
16330 declare
9dfd2ff8
CC
16331 D : Entity_Id;
16332 E : Elmt_Id;
71d9e9f2 16333
996ae0b0 16334 begin
9dfd2ff8
CC
16335 D := First_Discriminant (Typ_For_Constraint);
16336 E := First_Elmt (Constraint);
996ae0b0
RK
16337 while Present (D) loop
16338 if Chars (D) = Chars (Discriminant) then
16339 return Node (E);
16340 end if;
16341
16342 Next_Discriminant (D);
16343 Next_Elmt (E);
16344 end loop;
16345 end;
16346 end if;
16347
fbf5a39b
AC
16348 Result := Search_Derivation_Levels
16349 (Typ_For_Constraint, Constraint, False);
996ae0b0
RK
16350
16351 -- ??? hack to disappear when this routine is gone
16352
78c0f016 16353 if Nkind (Result) = N_Defining_Identifier then
996ae0b0 16354 declare
9dfd2ff8
CC
16355 D : Entity_Id;
16356 E : Elmt_Id;
fbf5a39b 16357
996ae0b0 16358 begin
9dfd2ff8
CC
16359 D := First_Discriminant (Typ_For_Constraint);
16360 E := First_Elmt (Constraint);
996ae0b0 16361 while Present (D) loop
78c0f016 16362 if Root_Corresponding_Discriminant (D) = Discriminant then
996ae0b0
RK
16363 return Node (E);
16364 end if;
16365
16366 Next_Discriminant (D);
16367 Next_Elmt (E);
16368 end loop;
16369 end;
16370 end if;
16371
16372 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
16373 return Result;
16374 end Get_Discriminant_Value;
16375
16376 --------------------------
16377 -- Has_Range_Constraint --
16378 --------------------------
16379
16380 function Has_Range_Constraint (N : Node_Id) return Boolean is
16381 C : constant Node_Id := Constraint (N);
16382
16383 begin
16384 if Nkind (C) = N_Range_Constraint then
16385 return True;
16386
16387 elsif Nkind (C) = N_Digits_Constraint then
16388 return
16389 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
16390 or else
16391 Present (Range_Constraint (C));
16392
16393 elsif Nkind (C) = N_Delta_Constraint then
16394 return Present (Range_Constraint (C));
16395
16396 else
16397 return False;
16398 end if;
16399 end Has_Range_Constraint;
16400
16401 ------------------------
16402 -- Inherit_Components --
16403 ------------------------
16404
16405 function Inherit_Components
16406 (N : Node_Id;
16407 Parent_Base : Entity_Id;
16408 Derived_Base : Entity_Id;
16409 Is_Tagged : Boolean;
16410 Inherit_Discr : Boolean;
b0f26df5 16411 Discs : Elist_Id) return Elist_Id
996ae0b0 16412 is
fbf5a39b 16413 Assoc_List : constant Elist_Id := New_Elmt_List;
996ae0b0
RK
16414
16415 procedure Inherit_Component
16416 (Old_C : Entity_Id;
16417 Plain_Discrim : Boolean := False;
fbf5a39b 16418 Stored_Discrim : Boolean := False);
a5b62485
AC
16419 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
16420 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
16421 -- True, Old_C is a stored discriminant. If they are both false then
16422 -- Old_C is a regular component.
996ae0b0
RK
16423
16424 -----------------------
16425 -- Inherit_Component --
16426 -----------------------
16427
16428 procedure Inherit_Component
16429 (Old_C : Entity_Id;
16430 Plain_Discrim : Boolean := False;
fbf5a39b 16431 Stored_Discrim : Boolean := False)
996ae0b0 16432 is
d7386a7a
AC
16433 procedure Set_Anonymous_Type (Id : Entity_Id);
16434 -- Id denotes the entity of an access discriminant or anonymous
16435 -- access component. Set the type of Id to either the same type of
16436 -- Old_C or create a new one depending on whether the parent and
16437 -- the child types are in the same scope.
16438
16439 ------------------------
16440 -- Set_Anonymous_Type --
16441 ------------------------
16442
16443 procedure Set_Anonymous_Type (Id : Entity_Id) is
2c17ca0a 16444 Old_Typ : constant Entity_Id := Etype (Old_C);
d7386a7a
AC
16445
16446 begin
16447 if Scope (Parent_Base) = Scope (Derived_Base) then
2c17ca0a 16448 Set_Etype (Id, Old_Typ);
d7386a7a
AC
16449
16450 -- The parent and the derived type are in two different scopes.
16451 -- Reuse the type of the original discriminant / component by
2c17ca0a 16452 -- copying it in order to preserve all attributes.
d7386a7a
AC
16453
16454 else
2c17ca0a
AC
16455 declare
16456 Typ : constant Entity_Id := New_Copy (Old_Typ);
16457
16458 begin
16459 Set_Etype (Id, Typ);
16460
16461 -- Since we do not generate component declarations for
16462 -- inherited components, associate the itype with the
16463 -- derived type.
16464
16465 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
16466 Set_Scope (Typ, Derived_Base);
16467 end;
d7386a7a
AC
16468 end if;
16469 end Set_Anonymous_Type;
16470
16471 -- Local variables and constants
16472
fbf5a39b 16473 New_C : constant Entity_Id := New_Copy (Old_C);
996ae0b0 16474
996ae0b0 16475 Corr_Discrim : Entity_Id;
d7386a7a
AC
16476 Discrim : Entity_Id;
16477
16478 -- Start of processing for Inherit_Component
996ae0b0
RK
16479
16480 begin
fbf5a39b 16481 pragma Assert (not Is_Tagged or else not Stored_Discrim);
996ae0b0
RK
16482
16483 Set_Parent (New_C, Parent (Old_C));
16484
88b32fc3
BD
16485 -- Regular discriminants and components must be inserted in the scope
16486 -- of the Derived_Base. Do it here.
996ae0b0 16487
fbf5a39b 16488 if not Stored_Discrim then
996ae0b0
RK
16489 Enter_Name (New_C);
16490 end if;
16491
16492 -- For tagged types the Original_Record_Component must point to
16493 -- whatever this field was pointing to in the parent type. This has
16494 -- already been achieved by the call to New_Copy above.
16495
16496 if not Is_Tagged then
16497 Set_Original_Record_Component (New_C, New_C);
16498 end if;
16499
d7386a7a
AC
16500 -- Set the proper type of an access discriminant
16501
16502 if Ekind (New_C) = E_Discriminant
16503 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
16504 then
16505 Set_Anonymous_Type (New_C);
16506 end if;
16507
996ae0b0
RK
16508 -- If we have inherited a component then see if its Etype contains
16509 -- references to Parent_Base discriminants. In this case, replace
16510 -- these references with the constraints given in Discs. We do not
16511 -- do this for the partial view of private types because this is
16512 -- not needed (only the components of the full view will be used
16513 -- for code generation) and cause problem. We also avoid this
16514 -- transformation in some error situations.
16515
16516 if Ekind (New_C) = E_Component then
d7386a7a
AC
16517
16518 -- Set the proper type of an anonymous access component
16519
16520 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
16521 Set_Anonymous_Type (New_C);
16522
16523 elsif (Is_Private_Type (Derived_Base)
9f55bc62 16524 and then not Is_Generic_Type (Derived_Base))
996ae0b0 16525 or else (Is_Empty_Elmt_List (Discs)
d7386a7a 16526 and then not Expander_Active)
996ae0b0
RK
16527 then
16528 Set_Etype (New_C, Etype (Old_C));
88b32fc3 16529
996ae0b0 16530 else
88b32fc3
BD
16531 -- The current component introduces a circularity of the
16532 -- following kind:
16533
16534 -- limited with Pack_2;
16535 -- package Pack_1 is
16536 -- type T_1 is tagged record
16537 -- Comp : access Pack_2.T_2;
16538 -- ...
16539 -- end record;
16540 -- end Pack_1;
16541
16542 -- with Pack_1;
16543 -- package Pack_2 is
16544 -- type T_2 is new Pack_1.T_1 with ...;
16545 -- end Pack_2;
16546
9f55bc62
AC
16547 Set_Etype
16548 (New_C,
16549 Constrain_Component_Type
16550 (Old_C, Derived_Base, N, Parent_Base, Discs));
996ae0b0
RK
16551 end if;
16552 end if;
16553
16554 -- In derived tagged types it is illegal to reference a non
16555 -- discriminant component in the parent type. To catch this, mark
16556 -- these components with an Ekind of E_Void. This will be reset in
16557 -- Record_Type_Definition after processing the record extension of
16558 -- the derived type.
16559
2b73cf68
JM
16560 -- If the declaration is a private extension, there is no further
16561 -- record extension to process, and the components retain their
16562 -- current kind, because they are visible at this point.
16563
16564 if Is_Tagged and then Ekind (New_C) = E_Component
16565 and then Nkind (N) /= N_Private_Extension_Declaration
16566 then
996ae0b0
RK
16567 Set_Ekind (New_C, E_Void);
16568 end if;
16569
16570 if Plain_Discrim then
16571 Set_Corresponding_Discriminant (New_C, Old_C);
16572 Build_Discriminal (New_C);
16573
fbf5a39b 16574 -- If we are explicitly inheriting a stored discriminant it will be
996ae0b0
RK
16575 -- completely hidden.
16576
fbf5a39b 16577 elsif Stored_Discrim then
996ae0b0
RK
16578 Set_Corresponding_Discriminant (New_C, Empty);
16579 Set_Discriminal (New_C, Empty);
16580 Set_Is_Completely_Hidden (New_C);
16581
16582 -- Set the Original_Record_Component of each discriminant in the
fbf5a39b 16583 -- derived base to point to the corresponding stored that we just
996ae0b0
RK
16584 -- created.
16585
16586 Discrim := First_Discriminant (Derived_Base);
16587 while Present (Discrim) loop
16588 Corr_Discrim := Corresponding_Discriminant (Discrim);
16589
9dfd2ff8 16590 -- Corr_Discrim could be missing in an error situation
996ae0b0
RK
16591
16592 if Present (Corr_Discrim)
16593 and then Original_Record_Component (Corr_Discrim) = Old_C
16594 then
16595 Set_Original_Record_Component (Discrim, New_C);
16596 end if;
16597
16598 Next_Discriminant (Discrim);
16599 end loop;
16600
16601 Append_Entity (New_C, Derived_Base);
16602 end if;
16603
16604 if not Is_Tagged then
16605 Append_Elmt (Old_C, Assoc_List);
16606 Append_Elmt (New_C, Assoc_List);
16607 end if;
16608 end Inherit_Component;
16609
71d9e9f2 16610 -- Variables local to Inherit_Component
996ae0b0
RK
16611
16612 Loc : constant Source_Ptr := Sloc (N);
16613
16614 Parent_Discrim : Entity_Id;
fbf5a39b 16615 Stored_Discrim : Entity_Id;
996ae0b0 16616 D : Entity_Id;
71d9e9f2 16617 Component : Entity_Id;
996ae0b0
RK
16618
16619 -- Start of processing for Inherit_Components
16620
16621 begin
16622 if not Is_Tagged then
16623 Append_Elmt (Parent_Base, Assoc_List);
16624 Append_Elmt (Derived_Base, Assoc_List);
16625 end if;
16626
ffe9aba8 16627 -- Inherit parent discriminants if needed
996ae0b0
RK
16628
16629 if Inherit_Discr then
16630 Parent_Discrim := First_Discriminant (Parent_Base);
16631 while Present (Parent_Discrim) loop
16632 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
16633 Next_Discriminant (Parent_Discrim);
16634 end loop;
16635 end if;
16636
ffe9aba8 16637 -- Create explicit stored discrims for untagged types when necessary
996ae0b0
RK
16638
16639 if not Has_Unknown_Discriminants (Derived_Base)
16640 and then Has_Discriminants (Parent_Base)
16641 and then not Is_Tagged
16642 and then
16643 (not Inherit_Discr
71d9e9f2
ES
16644 or else First_Discriminant (Parent_Base) /=
16645 First_Stored_Discriminant (Parent_Base))
996ae0b0 16646 then
fbf5a39b
AC
16647 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
16648 while Present (Stored_Discrim) loop
16649 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
16650 Next_Stored_Discriminant (Stored_Discrim);
996ae0b0
RK
16651 end loop;
16652 end if;
16653
16654 -- See if we can apply the second transformation for derived types, as
16655 -- explained in point 6. in the comments above Build_Derived_Record_Type
a5b62485
AC
16656 -- This is achieved by appending Derived_Base discriminants into Discs,
16657 -- which has the side effect of returning a non empty Discs list to the
16658 -- caller of Inherit_Components, which is what we want. This must be
16659 -- done for private derived types if there are explicit stored
16660 -- discriminants, to ensure that we can retrieve the values of the
16661 -- constraints provided in the ancestors.
996ae0b0
RK
16662
16663 if Inherit_Discr
16664 and then Is_Empty_Elmt_List (Discs)
30c20106
AC
16665 and then Present (First_Discriminant (Derived_Base))
16666 and then
16667 (not Is_Private_Type (Derived_Base)
71d9e9f2
ES
16668 or else Is_Completely_Hidden
16669 (First_Stored_Discriminant (Derived_Base))
16670 or else Is_Generic_Type (Derived_Base))
996ae0b0
RK
16671 then
16672 D := First_Discriminant (Derived_Base);
16673 while Present (D) loop
e4494292 16674 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
996ae0b0
RK
16675 Next_Discriminant (D);
16676 end loop;
16677 end if;
16678
16679 -- Finally, inherit non-discriminant components unless they are not
16680 -- visible because defined or inherited from the full view of the
16681 -- parent. Don't inherit the _parent field of the parent type.
16682
16683 Component := First_Entity (Parent_Base);
16684 while Present (Component) loop
758c442c 16685
2b73cf68
JM
16686 -- Ada 2005 (AI-251): Do not inherit components associated with
16687 -- secondary tags of the parent.
758c442c
GD
16688
16689 if Ekind (Component) = E_Component
7d7af38a 16690 and then Present (Related_Type (Component))
758c442c
GD
16691 then
16692 null;
16693
16694 elsif Ekind (Component) /= E_Component
996ae0b0
RK
16695 or else Chars (Component) = Name_uParent
16696 then
16697 null;
16698
16699 -- If the derived type is within the parent type's declarative
16700 -- region, then the components can still be inherited even though
16701 -- they aren't visible at this point. This can occur for cases
16702 -- such as within public child units where the components must
16703 -- become visible upon entering the child unit's private part.
16704
16705 elsif not Is_Visible_Component (Component)
16706 and then not In_Open_Scopes (Scope (Parent_Base))
16707 then
16708 null;
16709
bce79204
AC
16710 elsif Ekind_In (Derived_Base, E_Private_Type,
16711 E_Limited_Private_Type)
996ae0b0
RK
16712 then
16713 null;
16714
16715 else
16716 Inherit_Component (Component);
16717 end if;
16718
16719 Next_Entity (Component);
16720 end loop;
16721
16722 -- For tagged derived types, inherited discriminants cannot be used in
16723 -- component declarations of the record extension part. To achieve this
16724 -- we mark the inherited discriminants as not visible.
16725
16726 if Is_Tagged and then Inherit_Discr then
16727 D := First_Discriminant (Derived_Base);
16728 while Present (D) loop
16729 Set_Is_Immediately_Visible (D, False);
16730 Next_Discriminant (D);
16731 end loop;
16732 end if;
16733
16734 return Assoc_List;
16735 end Inherit_Components;
16736
57193e09
TQ
16737 -----------------------
16738 -- Is_Null_Extension --
16739 -----------------------
16740
16741 function Is_Null_Extension (T : Entity_Id) return Boolean is
1646c947 16742 Type_Decl : constant Node_Id := Parent (Base_Type (T));
2b73cf68
JM
16743 Comp_List : Node_Id;
16744 Comp : Node_Id;
57193e09
TQ
16745
16746 begin
fea9e956
ES
16747 if Nkind (Type_Decl) /= N_Full_Type_Declaration
16748 or else not Is_Tagged_Type (T)
16749 or else Nkind (Type_Definition (Type_Decl)) /=
16750 N_Derived_Type_Definition
16751 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
57193e09
TQ
16752 then
16753 return False;
16754 end if;
16755
fea9e956
ES
16756 Comp_List :=
16757 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
57193e09 16758
fea9e956 16759 if Present (Discriminant_Specifications (Type_Decl)) then
57193e09
TQ
16760 return False;
16761
16762 elsif Present (Comp_List)
16763 and then Is_Non_Empty_List (Component_Items (Comp_List))
16764 then
2b73cf68
JM
16765 Comp := First (Component_Items (Comp_List));
16766
16767 -- Only user-defined components are relevant. The component list
16768 -- may also contain a parent component and internal components
16769 -- corresponding to secondary tags, but these do not determine
16770 -- whether this is a null extension.
16771
16772 while Present (Comp) loop
16773 if Comes_From_Source (Comp) then
16774 return False;
16775 end if;
57193e09 16776
2b73cf68
JM
16777 Next (Comp);
16778 end loop;
57193e09 16779
2b73cf68 16780 return True;
57193e09
TQ
16781 else
16782 return True;
16783 end if;
16784 end Is_Null_Extension;
16785
996ae0b0
RK
16786 ------------------------------
16787 -- Is_Valid_Constraint_Kind --
16788 ------------------------------
16789
16790 function Is_Valid_Constraint_Kind
16791 (T_Kind : Type_Kind;
b0f26df5 16792 Constraint_Kind : Node_Kind) return Boolean
996ae0b0
RK
16793 is
16794 begin
16795 case T_Kind is
996ae0b0
RK
16796 when Enumeration_Kind |
16797 Integer_Kind =>
16798 return Constraint_Kind = N_Range_Constraint;
16799
16800 when Decimal_Fixed_Point_Kind =>
7d7af38a
JM
16801 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
16802 N_Range_Constraint);
996ae0b0
RK
16803
16804 when Ordinary_Fixed_Point_Kind =>
7d7af38a
JM
16805 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
16806 N_Range_Constraint);
996ae0b0
RK
16807
16808 when Float_Kind =>
7d7af38a
JM
16809 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
16810 N_Range_Constraint);
996ae0b0
RK
16811
16812 when Access_Kind |
16813 Array_Kind |
16814 E_Record_Type |
16815 E_Record_Subtype |
16816 Class_Wide_Kind |
16817 E_Incomplete_Type |
16818 Private_Kind |
16819 Concurrent_Kind =>
16820 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
16821
16822 when others =>
71d9e9f2 16823 return True; -- Error will be detected later
996ae0b0 16824 end case;
996ae0b0
RK
16825 end Is_Valid_Constraint_Kind;
16826
16827 --------------------------
16828 -- Is_Visible_Component --
16829 --------------------------
16830
a53c5613
AC
16831 function Is_Visible_Component
16832 (C : Entity_Id;
16833 N : Node_Id := Empty) return Boolean
16834 is
fbf5a39b 16835 Original_Comp : Entity_Id := Empty;
996ae0b0 16836 Original_Scope : Entity_Id;
fbf5a39b
AC
16837 Type_Scope : Entity_Id;
16838
16839 function Is_Local_Type (Typ : Entity_Id) return Boolean;
a5b62485
AC
16840 -- Check whether parent type of inherited component is declared locally,
16841 -- possibly within a nested package or instance. The current scope is
16842 -- the derived record itself.
fbf5a39b
AC
16843
16844 -------------------
16845 -- Is_Local_Type --
16846 -------------------
16847
16848 function Is_Local_Type (Typ : Entity_Id) return Boolean is
9dfd2ff8 16849 Scop : Entity_Id;
fbf5a39b
AC
16850
16851 begin
9dfd2ff8 16852 Scop := Scope (Typ);
fbf5a39b
AC
16853 while Present (Scop)
16854 and then Scop /= Standard_Standard
16855 loop
16856 if Scop = Scope (Current_Scope) then
16857 return True;
16858 end if;
16859
16860 Scop := Scope (Scop);
16861 end loop;
71d9e9f2 16862
fbf5a39b
AC
16863 return False;
16864 end Is_Local_Type;
16865
16866 -- Start of processing for Is_Visible_Component
996ae0b0
RK
16867
16868 begin
bce79204 16869 if Ekind_In (C, E_Component, E_Discriminant) then
fbf5a39b
AC
16870 Original_Comp := Original_Record_Component (C);
16871 end if;
16872
996ae0b0
RK
16873 if No (Original_Comp) then
16874
16875 -- Premature usage, or previous error
16876
16877 return False;
16878
16879 else
16880 Original_Scope := Scope (Original_Comp);
fbf5a39b 16881 Type_Scope := Scope (Base_Type (Scope (C)));
996ae0b0
RK
16882 end if;
16883
1355d373
AC
16884 -- For an untagged type derived from a private type, the only visible
16885 -- components are new discriminants. In an instance all components are
16886 -- visible (see Analyze_Selected_Component).
996ae0b0
RK
16887
16888 if not Is_Tagged_Type (Original_Scope) then
7271429c 16889 return not Has_Private_Ancestor (Original_Scope)
1355d373
AC
16890 or else In_Open_Scopes (Scope (Original_Scope))
16891 or else In_Instance
16892 or else (Ekind (Original_Comp) = E_Discriminant
16893 and then Original_Scope = Type_Scope);
996ae0b0 16894
fbf5a39b 16895 -- If it is _Parent or _Tag, there is no visibility issue
996ae0b0
RK
16896
16897 elsif not Comes_From_Source (Original_Comp) then
16898 return True;
16899
a53c5613
AC
16900 -- Discriminants are visible unless the (private) type has unknown
16901 -- discriminants. If the discriminant reference is inserted for a
16902 -- discriminant check on a full view it is also visible.
996ae0b0
RK
16903
16904 elsif Ekind (Original_Comp) = E_Discriminant
a53c5613
AC
16905 and then
16906 (not Has_Unknown_Discriminants (Original_Scope)
16907 or else (Present (N)
16908 and then Nkind (N) = N_Selected_Component
16909 and then Nkind (Prefix (N)) = N_Type_Conversion
16910 and then not Comes_From_Source (Prefix (N))))
996ae0b0
RK
16911 then
16912 return True;
16913
4913e24c
AC
16914 -- In the body of an instantiation, no need to check for the visibility
16915 -- of a component.
db4b3c49
AC
16916
16917 elsif In_Instance_Body then
4913e24c 16918 return True;
db4b3c49 16919
71d9e9f2
ES
16920 -- If the component has been declared in an ancestor which is currently
16921 -- a private type, then it is not visible. The same applies if the
16922 -- component's containing type is not in an open scope and the original
dc06abec 16923 -- component's enclosing type is a visible full view of a private type
71d9e9f2
ES
16924 -- (which can occur in cases where an attempt is being made to reference
16925 -- a component in a sibling package that is inherited from a visible
16926 -- component of a type in an ancestor package; the component in the
16927 -- sibling package should not be visible even though the component it
16928 -- inherited from is visible). This does not apply however in the case
16929 -- where the scope of the type is a private child unit, or when the
16930 -- parent comes from a local package in which the ancestor is currently
16931 -- visible. The latter suppression of visibility is needed for cases
16932 -- that are tested in B730006.
fbf5a39b
AC
16933
16934 elsif Is_Private_Type (Original_Scope)
16935 or else
16936 (not Is_Private_Descendant (Type_Scope)
16937 and then not In_Open_Scopes (Type_Scope)
16938 and then Has_Private_Declaration (Original_Scope))
996ae0b0 16939 then
fbf5a39b
AC
16940 -- If the type derives from an entity in a formal package, there
16941 -- are no additional visible components.
16942
16943 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
16944 N_Formal_Package_Declaration
16945 then
16946 return False;
16947
16948 -- if we are not in the private part of the current package, there
16949 -- are no additional visible components.
16950
16951 elsif Ekind (Scope (Current_Scope)) = E_Package
16952 and then not In_Private_Part (Scope (Current_Scope))
16953 then
16954 return False;
16955 else
16956 return
16957 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
dc06abec 16958 and then In_Open_Scopes (Scope (Original_Scope))
fbf5a39b
AC
16959 and then Is_Local_Type (Type_Scope);
16960 end if;
996ae0b0 16961
1355d373
AC
16962 -- There is another weird way in which a component may be invisible when
16963 -- the private and the full view are not derived from the same ancestor.
16964 -- Here is an example :
996ae0b0
RK
16965
16966 -- type A1 is tagged record F1 : integer; end record;
16967 -- type A2 is new A1 with record F2 : integer; end record;
16968 -- type T is new A1 with private;
16969 -- private
fbf5a39b 16970 -- type T is new A2 with null record;
996ae0b0 16971
a5b62485
AC
16972 -- In this case, the full view of T inherits F1 and F2 but the private
16973 -- view inherits only F1
996ae0b0
RK
16974
16975 else
16976 declare
16977 Ancestor : Entity_Id := Scope (C);
16978
16979 begin
16980 loop
16981 if Ancestor = Original_Scope then
16982 return True;
16983 elsif Ancestor = Etype (Ancestor) then
16984 return False;
16985 end if;
16986
16987 Ancestor := Etype (Ancestor);
16988 end loop;
996ae0b0
RK
16989 end;
16990 end if;
16991 end Is_Visible_Component;
16992
16993 --------------------------
16994 -- Make_Class_Wide_Type --
16995 --------------------------
16996
16997 procedure Make_Class_Wide_Type (T : Entity_Id) is
16998 CW_Type : Entity_Id;
16999 CW_Name : Name_Id;
17000 Next_E : Entity_Id;
17001
17002 begin
996ae0b0 17003 if Present (Class_Wide_Type (T)) then
996ae0b0 17004
df3e68b1
HK
17005 -- The class-wide type is a partially decorated entity created for a
17006 -- unanalyzed tagged type referenced through a limited with clause.
17007 -- When the tagged type is analyzed, its class-wide type needs to be
17008 -- redecorated. Note that we reuse the entity created by Decorate_
17009 -- Tagged_Type in order to preserve all links.
17010
17011 if Materialize_Entity (Class_Wide_Type (T)) then
17012 CW_Type := Class_Wide_Type (T);
17013 Set_Materialize_Entity (CW_Type, False);
17014
17015 -- The class wide type can have been defined by the partial view, in
17016 -- which case everything is already done.
17017
17018 else
17019 return;
17020 end if;
17021
17022 -- Default case, we need to create a new class-wide type
17023
17024 else
17025 CW_Type :=
17026 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
17027 end if;
996ae0b0
RK
17028
17029 -- Inherit root type characteristics
17030
17031 CW_Name := Chars (CW_Type);
17032 Next_E := Next_Entity (CW_Type);
17033 Copy_Node (T, CW_Type);
17034 Set_Comes_From_Source (CW_Type, False);
17035 Set_Chars (CW_Type, CW_Name);
17036 Set_Parent (CW_Type, Parent (T));
17037 Set_Next_Entity (CW_Type, Next_E);
88b32fc3
BD
17038
17039 -- Ensure we have a new freeze node for the class-wide type. The partial
17040 -- view may have freeze action of its own, requiring a proper freeze
17041 -- node, and the same freeze node cannot be shared between the two
17042 -- types.
17043
996ae0b0 17044 Set_Has_Delayed_Freeze (CW_Type);
88b32fc3 17045 Set_Freeze_Node (CW_Type, Empty);
996ae0b0
RK
17046
17047 -- Customize the class-wide type: It has no prim. op., it cannot be
07fc65c4 17048 -- abstract and its Etype points back to the specific root type.
996ae0b0 17049
ef2a63ba
JM
17050 Set_Ekind (CW_Type, E_Class_Wide_Type);
17051 Set_Is_Tagged_Type (CW_Type, True);
17052 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
17053 Set_Is_Abstract_Type (CW_Type, False);
17054 Set_Is_Constrained (CW_Type, False);
17055 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
996ae0b0 17056
07fc65c4
GB
17057 if Ekind (T) = E_Class_Wide_Subtype then
17058 Set_Etype (CW_Type, Etype (Base_Type (T)));
17059 else
17060 Set_Etype (CW_Type, T);
17061 end if;
17062
996ae0b0
RK
17063 -- If this is the class_wide type of a constrained subtype, it does
17064 -- not have discriminants.
17065
17066 Set_Has_Discriminants (CW_Type,
17067 Has_Discriminants (T) and then not Is_Constrained (T));
17068
17069 Set_Has_Unknown_Discriminants (CW_Type, True);
17070 Set_Class_Wide_Type (T, CW_Type);
17071 Set_Equivalent_Type (CW_Type, Empty);
17072
17073 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
17074
17075 Set_Class_Wide_Type (CW_Type, CW_Type);
996ae0b0
RK
17076 end Make_Class_Wide_Type;
17077
17078 ----------------
17079 -- Make_Index --
17080 ----------------
17081
17082 procedure Make_Index
17083 (I : Node_Id;
17084 Related_Nod : Node_Id;
17085 Related_Id : Entity_Id := Empty;
db72f10a
AC
17086 Suffix_Index : Nat := 1;
17087 In_Iter_Schm : Boolean := False)
996ae0b0
RK
17088 is
17089 R : Node_Id;
17090 T : Entity_Id;
17091 Def_Id : Entity_Id := Empty;
17092 Found : Boolean := False;
17093
17094 begin
17095 -- For a discrete range used in a constrained array definition and
17096 -- defined by a range, an implicit conversion to the predefined type
17097 -- INTEGER is assumed if each bound is either a numeric literal, a named
17098 -- number, or an attribute, and the type of both bounds (prior to the
17099 -- implicit conversion) is the type universal_integer. Otherwise, both
17100 -- bounds must be of the same discrete type, other than universal
17101 -- integer; this type must be determinable independently of the
17102 -- context, but using the fact that the type must be discrete and that
17103 -- both bounds must have the same type.
17104
17105 -- Character literals also have a universal type in the absence of
17106 -- of additional context, and are resolved to Standard_Character.
17107
17108 if Nkind (I) = N_Range then
17109
17110 -- The index is given by a range constraint. The bounds are known
17111 -- to be of a consistent type.
17112
17113 if not Is_Overloaded (I) then
17114 T := Etype (I);
17115
2b73cf68 17116 -- For universal bounds, choose the specific predefined type
996ae0b0
RK
17117
17118 if T = Universal_Integer then
17119 T := Standard_Integer;
17120
17121 elsif T = Any_Character then
2b73cf68 17122 Ambiguous_Character (Low_Bound (I));
996ae0b0
RK
17123
17124 T := Standard_Character;
17125 end if;
17126
df89ab66
ES
17127 -- The node may be overloaded because some user-defined operators
17128 -- are available, but if a universal interpretation exists it is
17129 -- also the selected one.
17130
17131 elsif Universal_Interpretation (I) = Universal_Integer then
17132 T := Standard_Integer;
17133
996ae0b0
RK
17134 else
17135 T := Any_Type;
17136
17137 declare
17138 Ind : Interp_Index;
17139 It : Interp;
17140
17141 begin
17142 Get_First_Interp (I, Ind, It);
996ae0b0
RK
17143 while Present (It.Typ) loop
17144 if Is_Discrete_Type (It.Typ) then
17145
17146 if Found
17147 and then not Covers (It.Typ, T)
17148 and then not Covers (T, It.Typ)
17149 then
17150 Error_Msg_N ("ambiguous bounds in discrete range", I);
17151 exit;
17152 else
17153 T := It.Typ;
17154 Found := True;
17155 end if;
17156 end if;
17157
17158 Get_Next_Interp (Ind, It);
17159 end loop;
17160
17161 if T = Any_Type then
17162 Error_Msg_N ("discrete type required for range", I);
17163 Set_Etype (I, Any_Type);
17164 return;
17165
17166 elsif T = Universal_Integer then
17167 T := Standard_Integer;
17168 end if;
17169 end;
17170 end if;
17171
17172 if not Is_Discrete_Type (T) then
17173 Error_Msg_N ("discrete type required for range", I);
17174 Set_Etype (I, Any_Type);
17175 return;
17176 end if;
17177
fbf5a39b
AC
17178 if Nkind (Low_Bound (I)) = N_Attribute_Reference
17179 and then Attribute_Name (Low_Bound (I)) = Name_First
17180 and then Is_Entity_Name (Prefix (Low_Bound (I)))
17181 and then Is_Type (Entity (Prefix (Low_Bound (I))))
17182 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
17183 then
a5b62485
AC
17184 -- The type of the index will be the type of the prefix, as long
17185 -- as the upper bound is 'Last of the same type.
fbf5a39b
AC
17186
17187 Def_Id := Entity (Prefix (Low_Bound (I)));
17188
17189 if Nkind (High_Bound (I)) /= N_Attribute_Reference
17190 or else Attribute_Name (High_Bound (I)) /= Name_Last
17191 or else not Is_Entity_Name (Prefix (High_Bound (I)))
17192 or else Entity (Prefix (High_Bound (I))) /= Def_Id
17193 then
17194 Def_Id := Empty;
17195 end if;
17196 end if;
17197
996ae0b0 17198 R := I;
db72f10a 17199 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
996ae0b0
RK
17200
17201 elsif Nkind (I) = N_Subtype_Indication then
17202
71d9e9f2 17203 -- The index is given by a subtype with a range constraint
996ae0b0
RK
17204
17205 T := Base_Type (Entity (Subtype_Mark (I)));
17206
17207 if not Is_Discrete_Type (T) then
17208 Error_Msg_N ("discrete type required for range", I);
17209 Set_Etype (I, Any_Type);
17210 return;
17211 end if;
17212
17213 R := Range_Expression (Constraint (I));
17214
17215 Resolve (R, T);
db72f10a
AC
17216 Process_Range_Expr_In_Decl
17217 (R, Entity (Subtype_Mark (I)), In_Iter_Schm => In_Iter_Schm);
996ae0b0
RK
17218
17219 elsif Nkind (I) = N_Attribute_Reference then
17220
17221 -- The parser guarantees that the attribute is a RANGE attribute
17222
fbf5a39b
AC
17223 -- If the node denotes the range of a type mark, that is also the
17224 -- resulting type, and we do no need to create an Itype for it.
17225
17226 if Is_Entity_Name (Prefix (I))
17227 and then Comes_From_Source (I)
17228 and then Is_Type (Entity (Prefix (I)))
17229 and then Is_Discrete_Type (Entity (Prefix (I)))
17230 then
17231 Def_Id := Entity (Prefix (I));
17232 end if;
17233
d087cd96 17234 Analyze_And_Resolve (I);
996ae0b0 17235 T := Etype (I);
996ae0b0
RK
17236 R := I;
17237
17238 -- If none of the above, must be a subtype. We convert this to a
17239 -- range attribute reference because in the case of declared first
17240 -- named subtypes, the types in the range reference can be different
17241 -- from the type of the entity. A range attribute normalizes the
17242 -- reference and obtains the correct types for the bounds.
17243
17244 -- This transformation is in the nature of an expansion, is only
17245 -- done if expansion is active. In particular, it is not done on
17246 -- formal generic types, because we need to retain the name of the
17247 -- original index for instantiation purposes.
17248
17249 else
17250 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
17251 Error_Msg_N ("invalid subtype mark in discrete range ", I);
17252 Set_Etype (I, Any_Integer);
17253 return;
71d9e9f2 17254
996ae0b0
RK
17255 else
17256 -- The type mark may be that of an incomplete type. It is only
17257 -- now that we can get the full view, previous analysis does
17258 -- not look specifically for a type mark.
17259
17260 Set_Entity (I, Get_Full_View (Entity (I)));
17261 Set_Etype (I, Entity (I));
17262 Def_Id := Entity (I);
17263
17264 if not Is_Discrete_Type (Def_Id) then
17265 Error_Msg_N ("discrete type required for index", I);
17266 Set_Etype (I, Any_Type);
17267 return;
17268 end if;
17269 end if;
17270
17271 if Expander_Active then
17272 Rewrite (I,
17273 Make_Attribute_Reference (Sloc (I),
17274 Attribute_Name => Name_Range,
17275 Prefix => Relocate_Node (I)));
17276
17277 -- The original was a subtype mark that does not freeze. This
17278 -- means that the rewritten version must not freeze either.
17279
17280 Set_Must_Not_Freeze (I);
17281 Set_Must_Not_Freeze (Prefix (I));
88b32fc3 17282 Analyze_And_Resolve (I);
996ae0b0 17283 T := Etype (I);
996ae0b0
RK
17284 R := I;
17285
fbf5a39b
AC
17286 -- If expander is inactive, type is legal, nothing else to construct
17287
996ae0b0 17288 else
996ae0b0
RK
17289 return;
17290 end if;
17291 end if;
17292
17293 if not Is_Discrete_Type (T) then
17294 Error_Msg_N ("discrete type required for range", I);
17295 Set_Etype (I, Any_Type);
17296 return;
17297
17298 elsif T = Any_Type then
17299 Set_Etype (I, Any_Type);
17300 return;
17301 end if;
17302
a5b62485
AC
17303 -- We will now create the appropriate Itype to describe the range, but
17304 -- first a check. If we originally had a subtype, then we just label
17305 -- the range with this subtype. Not only is there no need to construct
17306 -- a new subtype, but it is wrong to do so for two reasons:
996ae0b0 17307
a5b62485
AC
17308 -- 1. A legality concern, if we have a subtype, it must not freeze,
17309 -- and the Itype would cause freezing incorrectly
996ae0b0 17310
a5b62485
AC
17311 -- 2. An efficiency concern, if we created an Itype, it would not be
17312 -- recognized as the same type for the purposes of eliminating
17313 -- checks in some circumstances.
996ae0b0 17314
71d9e9f2 17315 -- We signal this case by setting the subtype entity in Def_Id
996ae0b0 17316
996ae0b0 17317 if No (Def_Id) then
996ae0b0
RK
17318 Def_Id :=
17319 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
17320 Set_Etype (Def_Id, Base_Type (T));
17321
17322 if Is_Signed_Integer_Type (T) then
17323 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
17324
17325 elsif Is_Modular_Integer_Type (T) then
17326 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
17327
17328 else
17329 Set_Ekind (Def_Id, E_Enumeration_Subtype);
17330 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
fbf5a39b 17331 Set_First_Literal (Def_Id, First_Literal (T));
996ae0b0
RK
17332 end if;
17333
17334 Set_Size_Info (Def_Id, (T));
17335 Set_RM_Size (Def_Id, RM_Size (T));
17336 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
17337
17338 Set_Scalar_Range (Def_Id, R);
17339 Conditional_Delay (Def_Id, T);
17340
17341 -- In the subtype indication case, if the immediate parent of the
17342 -- new subtype is non-static, then the subtype we create is non-
17343 -- static, even if its bounds are static.
17344
17345 if Nkind (I) = N_Subtype_Indication
17346 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
17347 then
17348 Set_Is_Non_Static_Subtype (Def_Id);
17349 end if;
17350 end if;
17351
17352 -- Final step is to label the index with this constructed type
17353
17354 Set_Etype (I, Def_Id);
17355 end Make_Index;
17356
17357 ------------------------------
17358 -- Modular_Type_Declaration --
17359 ------------------------------
17360
17361 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17362 Mod_Expr : constant Node_Id := Expression (Def);
17363 M_Val : Uint;
17364
17365 procedure Set_Modular_Size (Bits : Int);
17366 -- Sets RM_Size to Bits, and Esize to normal word size above this
17367
fbf5a39b
AC
17368 ----------------------
17369 -- Set_Modular_Size --
17370 ----------------------
17371
996ae0b0
RK
17372 procedure Set_Modular_Size (Bits : Int) is
17373 begin
17374 Set_RM_Size (T, UI_From_Int (Bits));
17375
17376 if Bits <= 8 then
17377 Init_Esize (T, 8);
17378
17379 elsif Bits <= 16 then
17380 Init_Esize (T, 16);
17381
17382 elsif Bits <= 32 then
17383 Init_Esize (T, 32);
17384
17385 else
17386 Init_Esize (T, System_Max_Binary_Modulus_Power);
17387 end if;
8dc2ddaf
RD
17388
17389 if not Non_Binary_Modulus (T)
17390 and then Esize (T) = RM_Size (T)
17391 then
17392 Set_Is_Known_Valid (T);
17393 end if;
996ae0b0
RK
17394 end Set_Modular_Size;
17395
17396 -- Start of processing for Modular_Type_Declaration
17397
17398 begin
b727a82b 17399 -- If the mod expression is (exactly) 2 * literal, where literal is
a90bd866 17400 -- 64 or less,then almost certainly the * was meant to be **. Warn.
b727a82b
AC
17401
17402 if Warn_On_Suspicious_Modulus_Value
17403 and then Nkind (Mod_Expr) = N_Op_Multiply
17404 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
17405 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
17406 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
17407 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
17408 then
324ac540
AC
17409 Error_Msg_N
17410 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
b727a82b
AC
17411 end if;
17412
17413 -- Proceed with analysis of mod expression
17414
996ae0b0
RK
17415 Analyze_And_Resolve (Mod_Expr, Any_Integer);
17416 Set_Etype (T, T);
17417 Set_Ekind (T, E_Modular_Integer_Type);
17418 Init_Alignment (T);
17419 Set_Is_Constrained (T);
17420
17421 if not Is_OK_Static_Expression (Mod_Expr) then
fbf5a39b
AC
17422 Flag_Non_Static_Expr
17423 ("non-static expression used for modular type bound!", Mod_Expr);
996ae0b0
RK
17424 M_Val := 2 ** System_Max_Binary_Modulus_Power;
17425 else
17426 M_Val := Expr_Value (Mod_Expr);
17427 end if;
17428
17429 if M_Val < 1 then
17430 Error_Msg_N ("modulus value must be positive", Mod_Expr);
17431 M_Val := 2 ** System_Max_Binary_Modulus_Power;
17432 end if;
17433
17434 Set_Modulus (T, M_Val);
17435
17436 -- Create bounds for the modular type based on the modulus given in
17437 -- the type declaration and then analyze and resolve those bounds.
17438
17439 Set_Scalar_Range (T,
17440 Make_Range (Sloc (Mod_Expr),
7675ad4f
AC
17441 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
17442 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
996ae0b0
RK
17443
17444 -- Properly analyze the literals for the range. We do this manually
17445 -- because we can't go calling Resolve, since we are resolving these
a90bd866 17446 -- bounds with the type, and this type is certainly not complete yet.
996ae0b0
RK
17447
17448 Set_Etype (Low_Bound (Scalar_Range (T)), T);
17449 Set_Etype (High_Bound (Scalar_Range (T)), T);
17450 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
17451 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
17452
17453 -- Loop through powers of two to find number of bits required
17454
17455 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
17456
17457 -- Binary case
17458
17459 if M_Val = 2 ** Bits then
17460 Set_Modular_Size (Bits);
17461 return;
17462
17463 -- Non-binary case
17464
17465 elsif M_Val < 2 ** Bits then
2ba431e5 17466 Check_SPARK_Restriction ("modulus should be a power of 2", T);
996ae0b0
RK
17467 Set_Non_Binary_Modulus (T);
17468
17469 if Bits > System_Max_Nonbinary_Modulus_Power then
17470 Error_Msg_Uint_1 :=
17471 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
2b73cf68 17472 Error_Msg_F
996ae0b0
RK
17473 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
17474 Set_Modular_Size (System_Max_Binary_Modulus_Power);
17475 return;
17476
17477 else
71d9e9f2 17478 -- In the non-binary case, set size as per RM 13.3(55)
996ae0b0
RK
17479
17480 Set_Modular_Size (Bits);
17481 return;
17482 end if;
17483 end if;
17484
17485 end loop;
17486
17487 -- If we fall through, then the size exceed System.Max_Binary_Modulus
17488 -- so we just signal an error and set the maximum size.
17489
17490 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
2b73cf68 17491 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
996ae0b0
RK
17492
17493 Set_Modular_Size (System_Max_Binary_Modulus_Power);
17494 Init_Alignment (T);
dc06abec 17495
996ae0b0
RK
17496 end Modular_Type_Declaration;
17497
6c1e24d3
AC
17498 --------------------------
17499 -- New_Concatenation_Op --
17500 --------------------------
996ae0b0 17501
6c1e24d3 17502 procedure New_Concatenation_Op (Typ : Entity_Id) is
996ae0b0
RK
17503 Loc : constant Source_Ptr := Sloc (Typ);
17504 Op : Entity_Id;
17505
17506 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
17507 -- Create abbreviated declaration for the formal of a predefined
17508 -- Operator 'Op' of type 'Typ'
17509
17510 --------------------
17511 -- Make_Op_Formal --
17512 --------------------
17513
17514 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
17515 Formal : Entity_Id;
996ae0b0
RK
17516 begin
17517 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
17518 Set_Etype (Formal, Typ);
17519 Set_Mechanism (Formal, Default_Mechanism);
17520 return Formal;
17521 end Make_Op_Formal;
17522
6c1e24d3 17523 -- Start of processing for New_Concatenation_Op
996ae0b0
RK
17524
17525 begin
6c1e24d3 17526 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
996ae0b0
RK
17527
17528 Set_Ekind (Op, E_Operator);
17529 Set_Scope (Op, Current_Scope);
17530 Set_Etype (Op, Typ);
6c1e24d3 17531 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
996ae0b0
RK
17532 Set_Is_Immediately_Visible (Op);
17533 Set_Is_Intrinsic_Subprogram (Op);
17534 Set_Has_Completion (Op);
17535 Append_Entity (Op, Current_Scope);
17536
6c1e24d3 17537 Set_Name_Entity_Id (Name_Op_Concat, Op);
996ae0b0
RK
17538
17539 Append_Entity (Make_Op_Formal (Typ, Op), Op);
17540 Append_Entity (Make_Op_Formal (Typ, Op), Op);
6c1e24d3 17541 end New_Concatenation_Op;
996ae0b0 17542
88b32fc3
BD
17543 -------------------------
17544 -- OK_For_Limited_Init --
17545 -------------------------
17546
17547 -- ???Check all calls of this, and compare the conditions under which it's
17548 -- called.
17549
2a31c32b
AC
17550 function OK_For_Limited_Init
17551 (Typ : Entity_Id;
17552 Exp : Node_Id) return Boolean
17553 is
88b32fc3 17554 begin
236fecbf 17555 return Is_CPP_Constructor_Call (Exp)
0791fbe9 17556 or else (Ada_Version >= Ada_2005
236fecbf 17557 and then not Debug_Flag_Dot_L
2a31c32b 17558 and then OK_For_Limited_Init_In_05 (Typ, Exp));
88b32fc3
BD
17559 end OK_For_Limited_Init;
17560
17561 -------------------------------
17562 -- OK_For_Limited_Init_In_05 --
17563 -------------------------------
17564
2a31c32b
AC
17565 function OK_For_Limited_Init_In_05
17566 (Typ : Entity_Id;
17567 Exp : Node_Id) return Boolean
17568 is
88b32fc3 17569 begin
2a31c32b
AC
17570 -- An object of a limited interface type can be initialized with any
17571 -- expression of a nonlimited descendant type.
17572
17573 if Is_Class_Wide_Type (Typ)
17574 and then Is_Limited_Interface (Typ)
17575 and then not Is_Limited_Type (Etype (Exp))
17576 then
17577 return True;
17578 end if;
17579
c6fe3827
GD
17580 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
17581 -- case of limited aggregates (including extension aggregates), and
4adf3c50 17582 -- function calls. The function call may have been given in prefixed
2b73cf68 17583 -- notation, in which case the original node is an indexed component.
4adf3c50 17584 -- If the function is parameterless, the original node was an explicit
8da1a312
AC
17585 -- dereference. The function may also be parameterless, in which case
17586 -- the source node is just an identifier.
88b32fc3
BD
17587
17588 case Nkind (Original_Node (Exp)) is
2b73cf68 17589 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
88b32fc3
BD
17590 return True;
17591
8da1a312
AC
17592 when N_Identifier =>
17593 return Present (Entity (Original_Node (Exp)))
17594 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
17595
71f62180
ES
17596 when N_Qualified_Expression =>
17597 return
2a31c32b
AC
17598 OK_For_Limited_Init_In_05
17599 (Typ, Expression (Original_Node (Exp)));
71f62180 17600
2b73cf68 17601 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
c6fe3827 17602 -- with a function call, the expander has rewritten the call into an
2b73cf68
JM
17603 -- N_Type_Conversion node to force displacement of the pointer to
17604 -- reference the component containing the secondary dispatch table.
71f62180 17605 -- Otherwise a type conversion is not a legal context.
e80d72ea
ES
17606 -- A return statement for a build-in-place function returning a
17607 -- synchronized type also introduces an unchecked conversion.
2b73cf68 17608
e606088a
AC
17609 when N_Type_Conversion |
17610 N_Unchecked_Type_Conversion =>
71f62180
ES
17611 return not Comes_From_Source (Exp)
17612 and then
2a31c32b
AC
17613 OK_For_Limited_Init_In_05
17614 (Typ, Expression (Original_Node (Exp)));
88b32fc3 17615
e606088a
AC
17616 when N_Indexed_Component |
17617 N_Selected_Component |
17618 N_Explicit_Dereference =>
2b73cf68
JM
17619 return Nkind (Exp) = N_Function_Call;
17620
c6fe3827
GD
17621 -- A use of 'Input is a function call, hence allowed. Normally the
17622 -- attribute will be changed to a call, but the attribute by itself
17623 -- can occur with -gnatc.
17624
17625 when N_Attribute_Reference =>
17626 return Attribute_Name (Original_Node (Exp)) = Name_Input;
17627
9b16cb57 17628 -- For a case expression, all dependent expressions must be legal
9f8d1e5c
AC
17629
17630 when N_Case_Expression =>
17631 declare
17632 Alt : Node_Id;
17633
17634 begin
17635 Alt := First (Alternatives (Original_Node (Exp)));
17636 while Present (Alt) loop
17637 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
17638 return False;
17639 end if;
17640
17641 Next (Alt);
17642 end loop;
17643
17644 return True;
17645 end;
17646
9b16cb57
RD
17647 -- For an if expression, all dependent expressions must be legal
17648
17649 when N_If_Expression =>
17650 declare
17651 Then_Expr : constant Node_Id :=
17652 Next (First (Expressions (Original_Node (Exp))));
17653 Else_Expr : constant Node_Id := Next (Then_Expr);
17654 begin
17655 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
17656 and then
17657 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
17658 end;
17659
88b32fc3
BD
17660 when others =>
17661 return False;
17662 end case;
17663 end OK_For_Limited_Init_In_05;
17664
996ae0b0
RK
17665 -------------------------------------------
17666 -- Ordinary_Fixed_Point_Type_Declaration --
17667 -------------------------------------------
17668
17669 procedure Ordinary_Fixed_Point_Type_Declaration
17670 (T : Entity_Id;
17671 Def : Node_Id)
17672 is
17673 Loc : constant Source_Ptr := Sloc (Def);
17674 Delta_Expr : constant Node_Id := Delta_Expression (Def);
17675 RRS : constant Node_Id := Real_Range_Specification (Def);
17676 Implicit_Base : Entity_Id;
17677 Delta_Val : Ureal;
17678 Small_Val : Ureal;
17679 Low_Val : Ureal;
17680 High_Val : Ureal;
17681
17682 begin
17683 Check_Restriction (No_Fixed_Point, Def);
17684
17685 -- Create implicit base type
17686
17687 Implicit_Base :=
17688 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
17689 Set_Etype (Implicit_Base, Implicit_Base);
17690
17691 -- Analyze and process delta expression
17692
17693 Analyze_And_Resolve (Delta_Expr, Any_Real);
17694
17695 Check_Delta_Expression (Delta_Expr);
17696 Delta_Val := Expr_Value_R (Delta_Expr);
17697
17698 Set_Delta_Value (Implicit_Base, Delta_Val);
17699
a5b62485
AC
17700 -- Compute default small from given delta, which is the largest power
17701 -- of two that does not exceed the given delta value.
996ae0b0
RK
17702
17703 declare
9dfd2ff8
CC
17704 Tmp : Ureal;
17705 Scale : Int;
996ae0b0
RK
17706
17707 begin
9dfd2ff8
CC
17708 Tmp := Ureal_1;
17709 Scale := 0;
17710
996ae0b0
RK
17711 if Delta_Val < Ureal_1 then
17712 while Delta_Val < Tmp loop
17713 Tmp := Tmp / Ureal_2;
17714 Scale := Scale + 1;
17715 end loop;
17716
17717 else
17718 loop
17719 Tmp := Tmp * Ureal_2;
17720 exit when Tmp > Delta_Val;
17721 Scale := Scale - 1;
17722 end loop;
17723 end if;
17724
17725 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
17726 end;
17727
17728 Set_Small_Value (Implicit_Base, Small_Val);
17729
17730 -- If no range was given, set a dummy range
17731
17732 if RRS <= Empty_Or_Error then
17733 Low_Val := -Small_Val;
17734 High_Val := Small_Val;
17735
17736 -- Otherwise analyze and process given range
17737
17738 else
17739 declare
17740 Low : constant Node_Id := Low_Bound (RRS);
17741 High : constant Node_Id := High_Bound (RRS);
17742
17743 begin
17744 Analyze_And_Resolve (Low, Any_Real);
17745 Analyze_And_Resolve (High, Any_Real);
17746 Check_Real_Bound (Low);
17747 Check_Real_Bound (High);
17748
17749 -- Obtain and set the range
17750
17751 Low_Val := Expr_Value_R (Low);
17752 High_Val := Expr_Value_R (High);
17753
17754 if Low_Val > High_Val then
324ac540 17755 Error_Msg_NE ("??fixed point type& has null range", Def, T);
996ae0b0
RK
17756 end if;
17757 end;
17758 end if;
17759
a5b62485
AC
17760 -- The range for both the implicit base and the declared first subtype
17761 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
17762 -- set a temporary range in place. Note that the bounds of the base
17763 -- type will be widened to be symmetrical and to fill the available
17764 -- bits when the type is frozen.
996ae0b0
RK
17765
17766 -- We could do this with all discrete types, and probably should, but
17767 -- we absolutely have to do it for fixed-point, since the end-points
17768 -- of the range and the size are determined by the small value, which
17769 -- could be reset before the freeze point.
17770
17771 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
17772 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
17773
996ae0b0
RK
17774 -- Complete definition of first subtype
17775
17776 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
17777 Set_Etype (T, Implicit_Base);
17778 Init_Size_Align (T);
17779 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
17780 Set_Small_Value (T, Small_Val);
17781 Set_Delta_Value (T, Delta_Val);
17782 Set_Is_Constrained (T);
17783
17784 end Ordinary_Fixed_Point_Type_Declaration;
17785
17786 ----------------------------------------
17787 -- Prepare_Private_Subtype_Completion --
17788 ----------------------------------------
17789
17790 procedure Prepare_Private_Subtype_Completion
17791 (Id : Entity_Id;
17792 Related_Nod : Node_Id)
17793 is
17794 Id_B : constant Entity_Id := Base_Type (Id);
17795 Full_B : constant Entity_Id := Full_View (Id_B);
17796 Full : Entity_Id;
17797
17798 begin
17799 if Present (Full_B) then
17800
a5b62485
AC
17801 -- The Base_Type is already completed, we can complete the subtype
17802 -- now. We have to create a new entity with the same name, Thus we
544e7c17 17803 -- can't use Create_Itype.
a5b62485 17804
996ae0b0
RK
17805 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
17806 Set_Is_Itype (Full);
17807 Set_Associated_Node_For_Itype (Full, Related_Nod);
17808 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
17809 end if;
17810
17811 -- The parent subtype may be private, but the base might not, in some
17812 -- nested instances. In that case, the subtype does not need to be
17813 -- exchanged. It would still be nice to make private subtypes and their
17814 -- bases consistent at all times ???
17815
17816 if Is_Private_Type (Id_B) then
17817 Append_Elmt (Id, Private_Dependents (Id_B));
17818 end if;
996ae0b0
RK
17819 end Prepare_Private_Subtype_Completion;
17820
17821 ---------------------------
17822 -- Process_Discriminants --
17823 ---------------------------
17824
fbf5a39b
AC
17825 procedure Process_Discriminants
17826 (N : Node_Id;
17827 Prev : Entity_Id := Empty)
17828 is
17829 Elist : constant Elist_Id := New_Elmt_List;
996ae0b0
RK
17830 Id : Node_Id;
17831 Discr : Node_Id;
17832 Discr_Number : Uint;
17833 Discr_Type : Entity_Id;
17834 Default_Present : Boolean := False;
17835 Default_Not_Present : Boolean := False;
996ae0b0
RK
17836
17837 begin
17838 -- A composite type other than an array type can have discriminants.
996ae0b0
RK
17839 -- On entry, the current scope is the composite type.
17840
17841 -- The discriminants are initially entered into the scope of the type
17842 -- via Enter_Name with the default Ekind of E_Void to prevent premature
17843 -- use, as explained at the end of this procedure.
17844
17845 Discr := First (Discriminant_Specifications (N));
17846 while Present (Discr) loop
17847 Enter_Name (Defining_Identifier (Discr));
17848
fbf5a39b
AC
17849 -- For navigation purposes we add a reference to the discriminant
17850 -- in the entity for the type. If the current declaration is a
17851 -- completion, place references on the partial view. Otherwise the
17852 -- type is the current scope.
17853
17854 if Present (Prev) then
17855
17856 -- The references go on the partial view, if present. If the
17857 -- partial view has discriminants, the references have been
17858 -- generated already.
17859
17860 if not Has_Discriminants (Prev) then
17861 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
17862 end if;
17863 else
17864 Generate_Reference
17865 (Current_Scope, Defining_Identifier (Discr), 'd');
17866 end if;
17867
996ae0b0 17868 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
57193e09 17869 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
996ae0b0 17870
0ab80019 17871 -- Ada 2005 (AI-254)
7324bf49
AC
17872
17873 if Present (Access_To_Subprogram_Definition
17874 (Discriminant_Type (Discr)))
17875 and then Protected_Present (Access_To_Subprogram_Definition
17876 (Discriminant_Type (Discr)))
17877 then
17878 Discr_Type :=
fea9e956 17879 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
7324bf49
AC
17880 end if;
17881
996ae0b0
RK
17882 else
17883 Find_Type (Discriminant_Type (Discr));
17884 Discr_Type := Etype (Discriminant_Type (Discr));
17885
17886 if Error_Posted (Discriminant_Type (Discr)) then
17887 Discr_Type := Any_Type;
17888 end if;
17889 end if;
17890
17891 if Is_Access_Type (Discr_Type) then
6e937c1c 17892
0ab80019 17893 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
6e937c1c
AC
17894 -- record types
17895
0791fbe9 17896 if Ada_Version < Ada_2005 then
6e937c1c
AC
17897 Check_Access_Discriminant_Requires_Limited
17898 (Discr, Discriminant_Type (Discr));
17899 end if;
996ae0b0 17900
0ab80019 17901 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
996ae0b0
RK
17902 Error_Msg_N
17903 ("(Ada 83) access discriminant not allowed", Discr);
17904 end if;
17905
17906 elsif not Is_Discrete_Type (Discr_Type) then
17907 Error_Msg_N ("discriminants must have a discrete or access type",
17908 Discriminant_Type (Discr));
17909 end if;
17910
17911 Set_Etype (Defining_Identifier (Discr), Discr_Type);
17912
17913 -- If a discriminant specification includes the assignment compound
17914 -- delimiter followed by an expression, the expression is the default
17915 -- expression of the discriminant; the default expression must be of
17916 -- the type of the discriminant. (RM 3.7.1) Since this expression is
17917 -- a default expression, we do the special preanalysis, since this
fbf5a39b
AC
17918 -- expression does not freeze (see "Handling of Default and Per-
17919 -- Object Expressions" in spec of package Sem).
996ae0b0
RK
17920
17921 if Present (Expression (Discr)) then
ce4a6e84 17922 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
996ae0b0
RK
17923
17924 if Nkind (N) = N_Formal_Type_Declaration then
17925 Error_Msg_N
17926 ("discriminant defaults not allowed for formal type",
17927 Expression (Discr));
17928
5e5db3b4
GD
17929 -- Flag an error for a tagged type with defaulted discriminants,
17930 -- excluding limited tagged types when compiling for Ada 2012
17931 -- (see AI05-0214).
17932
7324bf49 17933 elsif Is_Tagged_Type (Current_Scope)
5e5db3b4
GD
17934 and then (not Is_Limited_Type (Current_Scope)
17935 or else Ada_Version < Ada_2012)
027dbed8 17936 and then Comes_From_Source (N)
7324bf49 17937 then
027dbed8
AC
17938 -- Note: see similar test in Check_Or_Process_Discriminants, to
17939 -- handle the (illegal) case of the completion of an untagged
17940 -- view with discriminants with defaults by a tagged full view.
5e5db3b4 17941 -- We skip the check if Discr does not come from source, to
027dbed8 17942 -- account for the case of an untagged derived type providing
5e5db3b4 17943 -- defaults for a renamed discriminant from a private untagged
027dbed8 17944 -- ancestor with a tagged full view (ACATS B460006).
8e4dac80 17945
5e5db3b4
GD
17946 if Ada_Version >= Ada_2012 then
17947 Error_Msg_N
17948 ("discriminants of nonlimited tagged type cannot have"
17949 & " defaults",
17950 Expression (Discr));
17951 else
17952 Error_Msg_N
17953 ("discriminants of tagged type cannot have defaults",
17954 Expression (Discr));
17955 end if;
996ae0b0
RK
17956
17957 else
17958 Default_Present := True;
17959 Append_Elmt (Expression (Discr), Elist);
17960
17961 -- Tag the defining identifiers for the discriminants with
17962 -- their corresponding default expressions from the tree.
17963
17964 Set_Discriminant_Default_Value
17965 (Defining_Identifier (Discr), Expression (Discr));
17966 end if;
17967
17968 else
17969 Default_Not_Present := True;
17970 end if;
17971
9dfd2ff8
CC
17972 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
17973 -- Discr_Type but with the null-exclusion attribute
17974
0791fbe9 17975 if Ada_Version >= Ada_2005 then
9dfd2ff8
CC
17976
17977 -- Ada 2005 (AI-231): Static checks
17978
17979 if Can_Never_Be_Null (Discr_Type) then
17980 Null_Exclusion_Static_Checks (Discr);
17981
17982 elsif Is_Access_Type (Discr_Type)
17983 and then Null_Exclusion_Present (Discr)
17984
17985 -- No need to check itypes because in their case this check
17986 -- was done at their point of creation
17987
17988 and then not Is_Itype (Discr_Type)
17989 then
17990 if Can_Never_Be_Null (Discr_Type) then
2b73cf68
JM
17991 Error_Msg_NE
17992 ("`NOT NULL` not allowed (& already excludes null)",
17993 Discr,
17994 Discr_Type);
9dfd2ff8
CC
17995 end if;
17996
17997 Set_Etype (Defining_Identifier (Discr),
17998 Create_Null_Excluding_Itype
17999 (T => Discr_Type,
18000 Related_Nod => Discr));
fa961f76
ES
18001
18002 -- Check for improper null exclusion if the type is otherwise
18003 -- legal for a discriminant.
18004
18005 elsif Null_Exclusion_Present (Discr)
18006 and then Is_Discrete_Type (Discr_Type)
18007 then
18008 Error_Msg_N
18009 ("null exclusion can only apply to an access type", Discr);
9dfd2ff8 18010 end if;
2820d220 18011
88b32fc3 18012 -- Ada 2005 (AI-402): access discriminants of nonlimited types
ce4a6e84
RD
18013 -- can't have defaults. Synchronized types, or types that are
18014 -- explicitly limited are fine, but special tests apply to derived
18015 -- types in generics: in a generic body we have to assume the
18016 -- worst, and therefore defaults are not allowed if the parent is
18017 -- a generic formal private type (see ACATS B370001).
88b32fc3 18018
59e6b23c 18019 if Is_Access_Type (Discr_Type) and then Default_Present then
88b32fc3 18020 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
88b32fc3
BD
18021 or else Is_Limited_Record (Current_Scope)
18022 or else Is_Concurrent_Type (Current_Scope)
18023 or else Is_Concurrent_Record_Type (Current_Scope)
18024 or else Ekind (Current_Scope) = E_Limited_Private_Type
18025 then
ce4a6e84
RD
18026 if not Is_Derived_Type (Current_Scope)
18027 or else not Is_Generic_Type (Etype (Current_Scope))
18028 or else not In_Package_Body (Scope (Etype (Current_Scope)))
18029 or else Limited_Present
18030 (Type_Definition (Parent (Current_Scope)))
18031 then
18032 null;
18033
18034 else
18035 Error_Msg_N ("access discriminants of nonlimited types",
18036 Expression (Discr));
18037 Error_Msg_N ("\cannot have defaults", Expression (Discr));
18038 end if;
dc06abec
RD
18039
18040 elsif Present (Expression (Discr)) then
88b32fc3
BD
18041 Error_Msg_N
18042 ("(Ada 2005) access discriminants of nonlimited types",
18043 Expression (Discr));
18044 Error_Msg_N ("\cannot have defaults", Expression (Discr));
18045 end if;
18046 end if;
2820d220
AC
18047 end if;
18048
f1bd0415 18049 -- A discriminant cannot be volatile. This check is only relevant
f9966234
AC
18050 -- when SPARK_Mode is on as it is not standard Ada legality rule
18051 -- (SPARK RM 7.1.3(6)).
f1bd0415
AC
18052
18053 if SPARK_Mode = On
18054 and then Is_SPARK_Volatile_Object (Defining_Identifier (Discr))
18055 then
f9966234 18056 Error_Msg_N ("discriminant cannot be volatile", Discr);
f1bd0415
AC
18057 end if;
18058
996ae0b0
RK
18059 Next (Discr);
18060 end loop;
18061
18062 -- An element list consisting of the default expressions of the
18063 -- discriminants is constructed in the above loop and used to set
18064 -- the Discriminant_Constraint attribute for the type. If an object
18065 -- is declared of this (record or task) type without any explicit
18066 -- discriminant constraint given, this element list will form the
18067 -- actual parameters for the corresponding initialization procedure
18068 -- for the type.
18069
18070 Set_Discriminant_Constraint (Current_Scope, Elist);
fbf5a39b 18071 Set_Stored_Constraint (Current_Scope, No_Elist);
996ae0b0
RK
18072
18073 -- Default expressions must be provided either for all or for none
18074 -- of the discriminants of a discriminant part. (RM 3.7.1)
18075
18076 if Default_Present and then Default_Not_Present then
18077 Error_Msg_N
18078 ("incomplete specification of defaults for discriminants", N);
18079 end if;
18080
18081 -- The use of the name of a discriminant is not allowed in default
18082 -- expressions of a discriminant part if the specification of the
18083 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
18084
18085 -- To detect this, the discriminant names are entered initially with an
18086 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
18087 -- attempt to use a void entity (for example in an expression that is
18088 -- type-checked) produces the error message: premature usage. Now after
18089 -- completing the semantic analysis of the discriminant part, we can set
18090 -- the Ekind of all the discriminants appropriately.
18091
18092 Discr := First (Discriminant_Specifications (N));
18093 Discr_Number := Uint_1;
996ae0b0
RK
18094 while Present (Discr) loop
18095 Id := Defining_Identifier (Discr);
18096 Set_Ekind (Id, E_Discriminant);
18097 Init_Component_Location (Id);
18098 Init_Esize (Id);
18099 Set_Discriminant_Number (Id, Discr_Number);
18100
18101 -- Make sure this is always set, even in illegal programs
18102
18103 Set_Corresponding_Discriminant (Id, Empty);
18104
18105 -- Initialize the Original_Record_Component to the entity itself.
18106 -- Inherit_Components will propagate the right value to
18107 -- discriminants in derived record types.
18108
18109 Set_Original_Record_Component (Id, Id);
18110
ffe9aba8 18111 -- Create the discriminal for the discriminant
996ae0b0
RK
18112
18113 Build_Discriminal (Id);
18114
18115 Next (Discr);
18116 Discr_Number := Discr_Number + 1;
18117 end loop;
18118
18119 Set_Has_Discriminants (Current_Scope);
18120 end Process_Discriminants;
18121
18122 -----------------------
18123 -- Process_Full_View --
18124 -----------------------
18125
18126 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
18127 Priv_Parent : Entity_Id;
18128 Full_Parent : Entity_Id;
18129 Full_Indic : Node_Id;
18130
653da906
RD
18131 procedure Collect_Implemented_Interfaces
18132 (Typ : Entity_Id;
18133 Ifaces : Elist_Id);
18134 -- Ada 2005: Gather all the interfaces that Typ directly or
18135 -- inherently implements. Duplicate entries are not added to
18136 -- the list Ifaces.
18137
653da906
RD
18138 ------------------------------------
18139 -- Collect_Implemented_Interfaces --
18140 ------------------------------------
758c442c 18141
653da906
RD
18142 procedure Collect_Implemented_Interfaces
18143 (Typ : Entity_Id;
18144 Ifaces : Elist_Id)
758c442c 18145 is
653da906
RD
18146 Iface : Entity_Id;
18147 Iface_Elmt : Elmt_Id;
758c442c
GD
18148
18149 begin
57193e09
TQ
18150 -- Abstract interfaces are only associated with tagged record types
18151
18152 if not Is_Tagged_Type (Typ)
18153 or else not Is_Record_Type (Typ)
18154 then
18155 return;
18156 end if;
18157
88b32fc3
BD
18158 -- Recursively climb to the ancestors
18159
18160 if Etype (Typ) /= Typ
18161
18162 -- Protect the frontend against wrong cyclic declarations like:
758c442c 18163
88b32fc3
BD
18164 -- type B is new A with private;
18165 -- type C is new A with private;
18166 -- private
18167 -- type B is new C with null record;
18168 -- type C is new B with null record;
18169
18170 and then Etype (Typ) /= Priv_T
18171 and then Etype (Typ) /= Full_T
653da906 18172 then
88b32fc3
BD
18173 -- Keep separate the management of private type declarations
18174
18175 if Ekind (Typ) = E_Record_Type_With_Private then
18176
308e6f3a 18177 -- Handle the following erroneous case:
88b32fc3
BD
18178 -- type Private_Type is tagged private;
18179 -- private
18180 -- type Private_Type is new Type_Implementing_Iface;
18181
18182 if Present (Full_View (Typ))
18183 and then Etype (Typ) /= Full_View (Typ)
18184 then
dc06abec
RD
18185 if Is_Interface (Etype (Typ)) then
18186 Append_Unique_Elmt (Etype (Typ), Ifaces);
88b32fc3
BD
18187 end if;
18188
18189 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
18190 end if;
18191
18192 -- Non-private types
18193
18194 else
dc06abec
RD
18195 if Is_Interface (Etype (Typ)) then
18196 Append_Unique_Elmt (Etype (Typ), Ifaces);
88b32fc3
BD
18197 end if;
18198
18199 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
18200 end if;
653da906 18201 end if;
9dfd2ff8 18202
88b32fc3 18203 -- Handle entities in the list of abstract interfaces
9dfd2ff8 18204
ce2b6ba5
JM
18205 if Present (Interfaces (Typ)) then
18206 Iface_Elmt := First_Elmt (Interfaces (Typ));
653da906
RD
18207 while Present (Iface_Elmt) loop
18208 Iface := Node (Iface_Elmt);
18209
57193e09
TQ
18210 pragma Assert (Is_Interface (Iface));
18211
18212 if not Contain_Interface (Iface, Ifaces) then
653da906 18213 Append_Elmt (Iface, Ifaces);
57193e09 18214 Collect_Implemented_Interfaces (Iface, Ifaces);
653da906
RD
18215 end if;
18216
18217 Next_Elmt (Iface_Elmt);
18218 end loop;
18219 end if;
653da906
RD
18220 end Collect_Implemented_Interfaces;
18221
758c442c
GD
18222 -- Start of processing for Process_Full_View
18223
996ae0b0
RK
18224 begin
18225 -- First some sanity checks that must be done after semantic
18226 -- decoration of the full view and thus cannot be placed with other
18227 -- similar checks in Find_Type_Name
18228
18229 if not Is_Limited_Type (Priv_T)
18230 and then (Is_Limited_Type (Full_T)
18231 or else Is_Limited_Composite (Full_T))
18232 then
702d2020
AC
18233 if In_Instance then
18234 null;
18235 else
18236 Error_Msg_N
18237 ("completion of nonlimited type cannot be limited", Full_T);
18238 Explain_Limited_Type (Full_T, Full_T);
18239 end if;
996ae0b0 18240
fea9e956
ES
18241 elsif Is_Abstract_Type (Full_T)
18242 and then not Is_Abstract_Type (Priv_T)
18243 then
996ae0b0
RK
18244 Error_Msg_N
18245 ("completion of nonabstract type cannot be abstract", Full_T);
18246
18247 elsif Is_Tagged_Type (Priv_T)
18248 and then Is_Limited_Type (Priv_T)
18249 and then not Is_Limited_Type (Full_T)
18250 then
dc06abec
RD
18251 -- If pragma CPP_Class was applied to the private declaration
18252 -- propagate the limitedness to the full-view
18253
18254 if Is_CPP_Class (Priv_T) then
18255 Set_Is_Limited_Record (Full_T);
18256
996ae0b0 18257 -- GNAT allow its own definition of Limited_Controlled to disobey
df3e68b1 18258 -- this rule in order in ease the implementation. This test is safe
d34cd274
AC
18259 -- because Root_Controlled is defined in a child of System that
18260 -- normal programs are not supposed to use.
996ae0b0 18261
df3e68b1 18262 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
996ae0b0
RK
18263 Set_Is_Limited_Composite (Full_T);
18264 else
18265 Error_Msg_N
18266 ("completion of limited tagged type must be limited", Full_T);
18267 end if;
18268
18269 elsif Is_Generic_Type (Priv_T) then
18270 Error_Msg_N ("generic type cannot have a completion", Full_T);
18271 end if;
18272
88b32fc3
BD
18273 -- Check that ancestor interfaces of private and full views are
18274 -- consistent. We omit this check for synchronized types because
fea9e956 18275 -- they are performed on the corresponding record type when frozen.
88b32fc3 18276
0791fbe9 18277 if Ada_Version >= Ada_2005
653da906 18278 and then Is_Tagged_Type (Priv_T)
758c442c 18279 and then Is_Tagged_Type (Full_T)
fea9e956 18280 and then not Is_Concurrent_Type (Full_T)
758c442c
GD
18281 then
18282 declare
653da906
RD
18283 Iface : Entity_Id;
18284 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
18285 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
758c442c
GD
18286
18287 begin
653da906
RD
18288 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
18289 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
758c442c 18290
57193e09
TQ
18291 -- Ada 2005 (AI-251): The partial view shall be a descendant of
18292 -- an interface type if and only if the full type is descendant
c01817d2 18293 -- of the interface type (AARM 7.3 (7.3/2)).
57193e09
TQ
18294
18295 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
18296
18297 if Present (Iface) then
ed2233dc
AC
18298 Error_Msg_NE
18299 ("interface & not implemented by full type " &
18300 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
57193e09 18301 end if;
758c442c 18302
653da906 18303 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
758c442c 18304
653da906 18305 if Present (Iface) then
ed2233dc
AC
18306 Error_Msg_NE
18307 ("interface & not implemented by partial view " &
18308 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
758c442c
GD
18309 end if;
18310 end;
18311 end if;
18312
996ae0b0
RK
18313 if Is_Tagged_Type (Priv_T)
18314 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
18315 and then Is_Derived_Type (Full_T)
18316 then
18317 Priv_Parent := Etype (Priv_T);
18318
18319 -- The full view of a private extension may have been transformed
18320 -- into an unconstrained derived type declaration and a subtype
18321 -- declaration (see build_derived_record_type for details).
18322
18323 if Nkind (N) = N_Subtype_Declaration then
18324 Full_Indic := Subtype_Indication (N);
18325 Full_Parent := Etype (Base_Type (Full_T));
18326 else
18327 Full_Indic := Subtype_Indication (Type_Definition (N));
18328 Full_Parent := Etype (Full_T);
18329 end if;
18330
18331 -- Check that the parent type of the full type is a descendant of
18332 -- the ancestor subtype given in the private extension. If either
18333 -- entity has an Etype equal to Any_Type then we had some previous
18334 -- error situation [7.3(8)].
18335
18336 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
18337 return;
18338
653da906
RD
18339 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
18340 -- any order. Therefore we don't have to check that its parent must
18341 -- be a descendant of the parent of the private type declaration.
18342
18343 elsif Is_Interface (Priv_Parent)
18344 and then Is_Interface (Full_Parent)
18345 then
18346 null;
18347
57193e09
TQ
18348 -- Ada 2005 (AI-251): If the parent of the private type declaration
18349 -- is an interface there is no need to check that it is an ancestor
18350 -- of the associated full type declaration. The required tests for
16b05213 18351 -- this case are performed by Build_Derived_Record_Type.
57193e09
TQ
18352
18353 elsif not Is_Interface (Base_Type (Priv_Parent))
18354 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
18355 then
950d3e7d
ES
18356 Error_Msg_N
18357 ("parent of full type must descend from parent"
18358 & " of private extension", Full_Indic);
996ae0b0 18359
12f0c50c
AC
18360 -- First check a formal restriction, and then proceed with checking
18361 -- Ada rules. Since the formal restriction is not a serious error, we
18362 -- don't prevent further error detection for this check, hence the
18363 -- ELSE.
996ae0b0 18364
12f0c50c 18365 else
996ae0b0 18366
12f0c50c
AC
18367 -- In formal mode, when completing a private extension the type
18368 -- named in the private part must be exactly the same as that
18369 -- named in the visible part.
996ae0b0 18370
12f0c50c
AC
18371 if Priv_Parent /= Full_Parent then
18372 Error_Msg_Name_1 := Chars (Priv_Parent);
2ba431e5 18373 Check_SPARK_Restriction ("% expected", Full_Indic);
12f0c50c 18374 end if;
996ae0b0 18375
12f0c50c
AC
18376 -- Check the rules of 7.3(10): if the private extension inherits
18377 -- known discriminants, then the full type must also inherit those
18378 -- discriminants from the same (ancestor) type, and the parent
18379 -- subtype of the full type must be constrained if and only if
18380 -- the ancestor subtype of the private extension is constrained.
996ae0b0 18381
12f0c50c
AC
18382 if No (Discriminant_Specifications (Parent (Priv_T)))
18383 and then not Has_Unknown_Discriminants (Priv_T)
18384 and then Has_Discriminants (Base_Type (Priv_Parent))
18385 then
18386 declare
18387 Priv_Indic : constant Node_Id :=
18388 Subtype_Indication (Parent (Priv_T));
18389
18390 Priv_Constr : constant Boolean :=
18391 Is_Constrained (Priv_Parent)
18392 or else
18393 Nkind (Priv_Indic) = N_Subtype_Indication
ded8909b
AC
18394 or else
18395 Is_Constrained (Entity (Priv_Indic));
12f0c50c
AC
18396
18397 Full_Constr : constant Boolean :=
18398 Is_Constrained (Full_Parent)
18399 or else
18400 Nkind (Full_Indic) = N_Subtype_Indication
ded8909b
AC
18401 or else
18402 Is_Constrained (Entity (Full_Indic));
12f0c50c
AC
18403
18404 Priv_Discr : Entity_Id;
18405 Full_Discr : Entity_Id;
996ae0b0 18406
12f0c50c
AC
18407 begin
18408 Priv_Discr := First_Discriminant (Priv_Parent);
18409 Full_Discr := First_Discriminant (Full_Parent);
18410 while Present (Priv_Discr) and then Present (Full_Discr) loop
18411 if Original_Record_Component (Priv_Discr) =
ded8909b 18412 Original_Record_Component (Full_Discr)
12f0c50c
AC
18413 or else
18414 Corresponding_Discriminant (Priv_Discr) =
18415 Corresponding_Discriminant (Full_Discr)
18416 then
18417 null;
18418 else
18419 exit;
18420 end if;
996ae0b0 18421
12f0c50c
AC
18422 Next_Discriminant (Priv_Discr);
18423 Next_Discriminant (Full_Discr);
18424 end loop;
996ae0b0 18425
12f0c50c
AC
18426 if Present (Priv_Discr) or else Present (Full_Discr) then
18427 Error_Msg_N
18428 ("full view must inherit discriminants of the parent"
18429 & " type used in the private extension", Full_Indic);
996ae0b0 18430
12f0c50c
AC
18431 elsif Priv_Constr and then not Full_Constr then
18432 Error_Msg_N
18433 ("parent subtype of full type must be constrained",
18434 Full_Indic);
996ae0b0 18435
12f0c50c
AC
18436 elsif Full_Constr and then not Priv_Constr then
18437 Error_Msg_N
18438 ("parent subtype of full type must be unconstrained",
18439 Full_Indic);
18440 end if;
18441 end;
18442
18443 -- Check the rules of 7.3(12): if a partial view has neither
18444 -- known or unknown discriminants, then the full type
18445 -- declaration shall define a definite subtype.
996ae0b0 18446
12f0c50c
AC
18447 elsif not Has_Unknown_Discriminants (Priv_T)
18448 and then not Has_Discriminants (Priv_T)
18449 and then not Is_Constrained (Full_T)
18450 then
18451 Error_Msg_N
18452 ("full view must define a constrained type if partial view"
18453 & " has no discriminants", Full_T);
18454 end if;
18455
18456 -- ??????? Do we implement the following properly ?????
18457 -- If the ancestor subtype of a private extension has constrained
18458 -- discriminants, then the parent subtype of the full view shall
18459 -- impose a statically matching constraint on those discriminants
18460 -- [7.3(13)].
18461 end if;
996ae0b0
RK
18462
18463 else
18464 -- For untagged types, verify that a type without discriminants
18465 -- is not completed with an unconstrained type.
18466
18467 if not Is_Indefinite_Subtype (Priv_T)
18468 and then Is_Indefinite_Subtype (Full_T)
18469 then
18470 Error_Msg_N ("full view of type must be definite subtype", Full_T);
18471 end if;
18472 end if;
18473
653da906
RD
18474 -- AI-419: verify that the use of "limited" is consistent
18475
18476 declare
18477 Orig_Decl : constant Node_Id := Original_Node (N);
88b32fc3 18478
653da906
RD
18479 begin
18480 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
18481 and then not Limited_Present (Parent (Priv_T))
88b32fc3 18482 and then not Synchronized_Present (Parent (Priv_T))
653da906
RD
18483 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
18484 and then Nkind
18485 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
18486 and then Limited_Present (Type_Definition (Orig_Decl))
18487 then
18488 Error_Msg_N
18489 ("full view of non-limited extension cannot be limited", N);
18490 end if;
18491 end;
18492
88b32fc3
BD
18493 -- Ada 2005 (AI-443): A synchronized private extension must be
18494 -- completed by a task or protected type.
18495
0791fbe9 18496 if Ada_Version >= Ada_2005
88b32fc3
BD
18497 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
18498 and then Synchronized_Present (Parent (Priv_T))
fea9e956 18499 and then not Is_Concurrent_Type (Full_T)
88b32fc3
BD
18500 then
18501 Error_Msg_N ("full view of synchronized extension must " &
18502 "be synchronized type", N);
18503 end if;
18504
758c442c
GD
18505 -- Ada 2005 AI-363: if the full view has discriminants with
18506 -- defaults, it is illegal to declare constrained access subtypes
18507 -- whose designated type is the current type. This allows objects
18508 -- of the type that are declared in the heap to be unconstrained.
18509
18510 if not Has_Unknown_Discriminants (Priv_T)
18511 and then not Has_Discriminants (Priv_T)
18512 and then Has_Discriminants (Full_T)
18513 and then
88b32fc3 18514 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
758c442c
GD
18515 then
18516 Set_Has_Constrained_Partial_View (Full_T);
18517 Set_Has_Constrained_Partial_View (Priv_T);
18518 end if;
18519
996ae0b0 18520 -- Create a full declaration for all its subtypes recorded in
a5b62485
AC
18521 -- Private_Dependents and swap them similarly to the base type. These
18522 -- are subtypes that have been define before the full declaration of
18523 -- the private type. We also swap the entry in Private_Dependents list
18524 -- so we can properly restore the private view on exit from the scope.
996ae0b0
RK
18525
18526 declare
18527 Priv_Elmt : Elmt_Id;
18528 Priv : Entity_Id;
18529 Full : Entity_Id;
18530
18531 begin
18532 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
18533 while Present (Priv_Elmt) loop
18534 Priv := Node (Priv_Elmt);
18535
bce79204
AC
18536 if Ekind_In (Priv, E_Private_Subtype,
18537 E_Limited_Private_Subtype,
18538 E_Record_Subtype_With_Private)
996ae0b0
RK
18539 then
18540 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
18541 Set_Is_Itype (Full);
18542 Set_Parent (Full, Parent (Priv));
18543 Set_Associated_Node_For_Itype (Full, N);
18544
18545 -- Now we need to complete the private subtype, but since the
18546 -- base type has already been swapped, we must also swap the
18547 -- subtypes (and thus, reverse the arguments in the call to
18548 -- Complete_Private_Subtype).
18549
18550 Copy_And_Swap (Priv, Full);
18551 Complete_Private_Subtype (Full, Priv, Full_T, N);
18552 Replace_Elmt (Priv_Elmt, Full);
18553 end if;
18554
18555 Next_Elmt (Priv_Elmt);
18556 end loop;
18557 end;
18558
2b73cf68
JM
18559 -- If the private view was tagged, copy the new primitive operations
18560 -- from the private view to the full view.
996ae0b0 18561
d44202ba 18562 if Is_Tagged_Type (Full_T) then
996ae0b0 18563 declare
d44202ba
HK
18564 Disp_Typ : Entity_Id;
18565 Full_List : Elist_Id;
996ae0b0 18566 Prim : Entity_Id;
d44202ba
HK
18567 Prim_Elmt : Elmt_Id;
18568 Priv_List : Elist_Id;
18569
18570 function Contains
18571 (E : Entity_Id;
18572 L : Elist_Id) return Boolean;
18573 -- Determine whether list L contains element E
18574
18575 --------------
18576 -- Contains --
18577 --------------
18578
18579 function Contains
18580 (E : Entity_Id;
18581 L : Elist_Id) return Boolean
18582 is
18583 List_Elmt : Elmt_Id;
18584
18585 begin
18586 List_Elmt := First_Elmt (L);
18587 while Present (List_Elmt) loop
18588 if Node (List_Elmt) = E then
18589 return True;
18590 end if;
18591
18592 Next_Elmt (List_Elmt);
18593 end loop;
18594
18595 return False;
18596 end Contains;
18597
18598 -- Start of processing
996ae0b0
RK
18599
18600 begin
18601 if Is_Tagged_Type (Priv_T) then
18602 Priv_List := Primitive_Operations (Priv_T);
d44202ba
HK
18603 Prim_Elmt := First_Elmt (Priv_List);
18604
18605 -- In the case of a concurrent type completing a private tagged
16b05213 18606 -- type, primitives may have been declared in between the two
d44202ba
HK
18607 -- views. These subprograms need to be wrapped the same way
18608 -- entries and protected procedures are handled because they
18609 -- cannot be directly shared by the two views.
18610
18611 if Is_Concurrent_Type (Full_T) then
18612 declare
18613 Conc_Typ : constant Entity_Id :=
18614 Corresponding_Record_Type (Full_T);
d44202ba
HK
18615 Curr_Nod : Node_Id := Parent (Conc_Typ);
18616 Wrap_Spec : Node_Id;
996ae0b0 18617
d44202ba
HK
18618 begin
18619 while Present (Prim_Elmt) loop
18620 Prim := Node (Prim_Elmt);
996ae0b0 18621
d44202ba
HK
18622 if Comes_From_Source (Prim)
18623 and then not Is_Abstract_Subprogram (Prim)
18624 then
18625 Wrap_Spec :=
eb9cb0fc 18626 Make_Subprogram_Declaration (Sloc (Prim),
d44202ba 18627 Specification =>
eb9cb0fc
ES
18628 Build_Wrapper_Spec
18629 (Subp_Id => Prim,
18630 Obj_Typ => Conc_Typ,
18631 Formals =>
18632 Parameter_Specifications (
18633 Parent (Prim))));
d44202ba
HK
18634
18635 Insert_After (Curr_Nod, Wrap_Spec);
18636 Curr_Nod := Wrap_Spec;
18637
18638 Analyze (Wrap_Spec);
18639 end if;
996ae0b0 18640
d44202ba 18641 Next_Elmt (Prim_Elmt);
996ae0b0
RK
18642 end loop;
18643
d44202ba
HK
18644 return;
18645 end;
18646
18647 -- For non-concurrent types, transfer explicit primitives, but
18648 -- omit those inherited from the parent of the private view
18649 -- since they will be re-inherited later on.
18650
18651 else
18652 Full_List := Primitive_Operations (Full_T);
18653
18654 while Present (Prim_Elmt) loop
18655 Prim := Node (Prim_Elmt);
996ae0b0 18656
d44202ba
HK
18657 if Comes_From_Source (Prim)
18658 and then not Contains (Prim, Full_List)
18659 then
996ae0b0
RK
18660 Append_Elmt (Prim, Full_List);
18661 end if;
996ae0b0 18662
d44202ba
HK
18663 Next_Elmt (Prim_Elmt);
18664 end loop;
18665 end if;
18666
18667 -- Untagged private view
996ae0b0
RK
18668
18669 else
d44202ba
HK
18670 Full_List := Primitive_Operations (Full_T);
18671
88b32fc3
BD
18672 -- In this case the partial view is untagged, so here we locate
18673 -- all of the earlier primitives that need to be treated as
18674 -- dispatching (those that appear between the two views). Note
18675 -- that these additional operations must all be new operations
18676 -- (any earlier operations that override inherited operations
18677 -- of the full view will already have been inserted in the
18678 -- primitives list, marked by Check_Operation_From_Private_View
18679 -- as dispatching. Note that implicit "/=" operators are
18680 -- excluded from being added to the primitives list since they
18681 -- shouldn't be treated as dispatching (tagged "/=" is handled
18682 -- specially).
996ae0b0
RK
18683
18684 Prim := Next_Entity (Full_T);
18685 while Present (Prim) and then Prim /= Priv_T loop
bce79204 18686 if Ekind_In (Prim, E_Procedure, E_Function) then
d44202ba 18687 Disp_Typ := Find_Dispatching_Type (Prim);
996ae0b0 18688
d44202ba 18689 if Disp_Typ = Full_T
996ae0b0
RK
18690 and then (Chars (Prim) /= Name_Op_Ne
18691 or else Comes_From_Source (Prim))
18692 then
18693 Check_Controlling_Formals (Full_T, Prim);
18694
18695 if not Is_Dispatching_Operation (Prim) then
18696 Append_Elmt (Prim, Full_List);
18697 Set_Is_Dispatching_Operation (Prim, True);
18698 Set_DT_Position (Prim, No_Uint);
18699 end if;
18700
18701 elsif Is_Dispatching_Operation (Prim)
d44202ba 18702 and then Disp_Typ /= Full_T
996ae0b0
RK
18703 then
18704
88b32fc3
BD
18705 -- Verify that it is not otherwise controlled by a
18706 -- formal or a return value of type T.
996ae0b0 18707
d44202ba 18708 Check_Controlling_Formals (Disp_Typ, Prim);
996ae0b0
RK
18709 end if;
18710 end if;
18711
18712 Next_Entity (Prim);
18713 end loop;
18714 end if;
18715
61441c18
TQ
18716 -- For the tagged case, the two views can share the same primitive
18717 -- operations list and the same class-wide type. Update attributes
18718 -- of the class-wide type which depend on the full declaration.
996ae0b0
RK
18719
18720 if Is_Tagged_Type (Priv_T) then
ef2a63ba 18721 Set_Direct_Primitive_Operations (Priv_T, Full_List);
996ae0b0
RK
18722 Set_Class_Wide_Type
18723 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
18724
996ae0b0 18725 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
996ae0b0
RK
18726 end if;
18727 end;
18728 end if;
88b32fc3 18729
e2ef0ff6 18730 -- Ada 2005 AI 161: Check preelaborable initialization consistency
88b32fc3
BD
18731
18732 if Known_To_Have_Preelab_Init (Priv_T) then
18733
18734 -- Case where there is a pragma Preelaborable_Initialization. We
18735 -- always allow this in predefined units, which is a bit of a kludge,
18736 -- but it means we don't have to struggle to meet the requirements in
18737 -- the RM for having Preelaborable Initialization. Otherwise we
18738 -- require that the type meets the RM rules. But we can't check that
308e6f3a
RW
18739 -- yet, because of the rule about overriding Initialize, so we simply
18740 -- set a flag that will be checked at freeze time.
88b32fc3
BD
18741
18742 if not In_Predefined_Unit (Full_T) then
18743 Set_Must_Have_Preelab_Init (Full_T);
18744 end if;
18745 end if;
2b73cf68
JM
18746
18747 -- If pragma CPP_Class was applied to the private type declaration,
18748 -- propagate it now to the full type declaration.
18749
18750 if Is_CPP_Class (Priv_T) then
18751 Set_Is_CPP_Class (Full_T);
18752 Set_Convention (Full_T, Convention_CPP);
539fcb45
JM
18753
18754 -- Check that components of imported CPP types do not have default
18755 -- expressions.
18756
9a7e930f 18757 Check_CPP_Type_Has_No_Defaults (Full_T);
2b73cf68 18758 end if;
23c4ff9b
AC
18759
18760 -- If the private view has user specified stream attributes, then so has
18761 -- the full view.
18762
e606088a
AC
18763 -- Why the test, how could these flags be already set in Full_T ???
18764
23c4ff9b
AC
18765 if Has_Specified_Stream_Read (Priv_T) then
18766 Set_Has_Specified_Stream_Read (Full_T);
18767 end if;
e606088a 18768
23c4ff9b
AC
18769 if Has_Specified_Stream_Write (Priv_T) then
18770 Set_Has_Specified_Stream_Write (Full_T);
18771 end if;
e606088a 18772
23c4ff9b
AC
18773 if Has_Specified_Stream_Input (Priv_T) then
18774 Set_Has_Specified_Stream_Input (Full_T);
18775 end if;
e606088a 18776
23c4ff9b
AC
18777 if Has_Specified_Stream_Output (Priv_T) then
18778 Set_Has_Specified_Stream_Output (Full_T);
18779 end if;
e606088a 18780
f2264ac2 18781 -- Propagate invariants to full type
e606088a 18782
f2264ac2 18783 if Has_Invariants (Priv_T) then
e606088a 18784 Set_Has_Invariants (Full_T);
f2264ac2 18785 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
e606088a
AC
18786 end if;
18787
f2264ac2 18788 if Has_Inheritable_Invariants (Priv_T) then
e606088a 18789 Set_Has_Inheritable_Invariants (Full_T);
4818e7b9
RD
18790 end if;
18791
e2ef0ff6
AC
18792 -- Propagate predicates to full type, and predicate function if already
18793 -- defined. It is not clear that this can actually happen? the partial
18794 -- view cannot be frozen yet, and the predicate function has not been
18795 -- built. Still it is a cheap check and seems safer to make it.
4818e7b9
RD
18796
18797 if Has_Predicates (Priv_T) then
e2ef0ff6
AC
18798 if Present (Predicate_Function (Priv_T)) then
18799 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
18800 end if;
18801
de6e4fc4 18802 Set_Has_Predicates (Full_T);
e606088a 18803 end if;
996ae0b0
RK
18804 end Process_Full_View;
18805
18806 -----------------------------------
18807 -- Process_Incomplete_Dependents --
18808 -----------------------------------
18809
18810 procedure Process_Incomplete_Dependents
18811 (N : Node_Id;
18812 Full_T : Entity_Id;
18813 Inc_T : Entity_Id)
18814 is
18815 Inc_Elmt : Elmt_Id;
18816 Priv_Dep : Entity_Id;
18817 New_Subt : Entity_Id;
18818
18819 Disc_Constraint : Elist_Id;
18820
18821 begin
18822 if No (Private_Dependents (Inc_T)) then
18823 return;
996ae0b0
RK
18824 end if;
18825
9dfd2ff8
CC
18826 -- Itypes that may be generated by the completion of an incomplete
18827 -- subtype are not used by the back-end and not attached to the tree.
18828 -- They are created only for constraint-checking purposes.
18829
18830 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
996ae0b0
RK
18831 while Present (Inc_Elmt) loop
18832 Priv_Dep := Node (Inc_Elmt);
18833
18834 if Ekind (Priv_Dep) = E_Subprogram_Type then
18835
18836 -- An Access_To_Subprogram type may have a return type or a
18837 -- parameter type that is incomplete. Replace with the full view.
18838
18839 if Etype (Priv_Dep) = Inc_T then
18840 Set_Etype (Priv_Dep, Full_T);
18841 end if;
18842
18843 declare
18844 Formal : Entity_Id;
18845
18846 begin
18847 Formal := First_Formal (Priv_Dep);
996ae0b0 18848 while Present (Formal) loop
996ae0b0
RK
18849 if Etype (Formal) = Inc_T then
18850 Set_Etype (Formal, Full_T);
18851 end if;
18852
18853 Next_Formal (Formal);
18854 end loop;
18855 end;
18856
9dfd2ff8 18857 elsif Is_Overloadable (Priv_Dep) then
996ae0b0 18858
4637729f
AC
18859 -- If a subprogram in the incomplete dependents list is primitive
18860 -- for a tagged full type then mark it as a dispatching operation,
18861 -- check whether it overrides an inherited subprogram, and check
18862 -- restrictions on its controlling formals. Note that a protected
18863 -- operation is never dispatching: only its wrapper operation
18864 -- (which has convention Ada) is.
9dfd2ff8
CC
18865
18866 if Is_Tagged_Type (Full_T)
4637729f 18867 and then Is_Primitive (Priv_Dep)
9dfd2ff8
CC
18868 and then Convention (Priv_Dep) /= Convention_Protected
18869 then
996ae0b0
RK
18870 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
18871 Set_Is_Dispatching_Operation (Priv_Dep);
18872 Check_Controlling_Formals (Full_T, Priv_Dep);
18873 end if;
18874
18875 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
18876
18877 -- Can happen during processing of a body before the completion
18878 -- of a TA type. Ignore, because spec is also on dependent list.
18879
18880 return;
18881
88b32fc3
BD
18882 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
18883 -- corresponding subtype of the full view.
18884
18885 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
18886 Set_Subtype_Indication
e4494292 18887 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
88b32fc3
BD
18888 Set_Etype (Priv_Dep, Full_T);
18889 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
18890 Set_Analyzed (Parent (Priv_Dep), False);
18891
18892 -- Reanalyze the declaration, suppressing the call to
18893 -- Enter_Name to avoid duplicate names.
18894
18895 Analyze_Subtype_Declaration
18896 (N => Parent (Priv_Dep),
18897 Skip => True);
18898
996ae0b0
RK
18899 -- Dependent is a subtype
18900
18901 else
18902 -- We build a new subtype indication using the full view of the
18903 -- incomplete parent. The discriminant constraints have been
18904 -- elaborated already at the point of the subtype declaration.
18905
18906 New_Subt := Create_Itype (E_Void, N);
18907
18908 if Has_Discriminants (Full_T) then
18909 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
18910 else
18911 Disc_Constraint := No_Elist;
18912 end if;
18913
18914 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
18915 Set_Full_View (Priv_Dep, New_Subt);
18916 end if;
18917
18918 Next_Elmt (Inc_Elmt);
18919 end loop;
996ae0b0
RK
18920 end Process_Incomplete_Dependents;
18921
18922 --------------------------------
18923 -- Process_Range_Expr_In_Decl --
18924 --------------------------------
18925
18926 procedure Process_Range_Expr_In_Decl
db72f10a
AC
18927 (R : Node_Id;
18928 T : Entity_Id;
18929 Check_List : List_Id := Empty_List;
18930 R_Check_Off : Boolean := False;
18931 In_Iter_Schm : Boolean := False)
996ae0b0 18932 is
0592046e
AC
18933 Lo, Hi : Node_Id;
18934 R_Checks : Check_Result;
18935 Insert_Node : Node_Id;
18936 Def_Id : Entity_Id;
996ae0b0
RK
18937
18938 begin
18939 Analyze_And_Resolve (R, Base_Type (T));
18940
18941 if Nkind (R) = N_Range then
db72f10a 18942
0f853035
YM
18943 -- In SPARK, all ranges should be static, with the exception of the
18944 -- discrete type definition of a loop parameter specification.
db72f10a
AC
18945
18946 if not In_Iter_Schm
18947 and then not Is_Static_Range (R)
18948 then
2ba431e5 18949 Check_SPARK_Restriction ("range should be static", R);
218e6dee
RD
18950 end if;
18951
996ae0b0
RK
18952 Lo := Low_Bound (R);
18953 Hi := High_Bound (R);
18954
88b32fc3
BD
18955 -- We need to ensure validity of the bounds here, because if we
18956 -- go ahead and do the expansion, then the expanded code will get
18957 -- analyzed with range checks suppressed and we miss the check.
2838fa93
AC
18958 -- Validity checks on the range of a quantified expression are
18959 -- delayed until the construct is transformed into a loop.
88b32fc3 18960
2838fa93
AC
18961 if Nkind (Parent (R)) /= N_Loop_Parameter_Specification
18962 or else Nkind (Parent (Parent (R))) /= N_Quantified_Expression
18963 then
18964 Validity_Check_Range (R);
18965 end if;
88b32fc3 18966
996ae0b0
RK
18967 -- If there were errors in the declaration, try and patch up some
18968 -- common mistakes in the bounds. The cases handled are literals
18969 -- which are Integer where the expected type is Real and vice versa.
18970 -- These corrections allow the compilation process to proceed further
18971 -- along since some basic assumptions of the format of the bounds
18972 -- are guaranteed.
18973
18974 if Etype (R) = Any_Type then
996ae0b0
RK
18975 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
18976 Rewrite (Lo,
18977 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
18978
18979 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
18980 Rewrite (Hi,
18981 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
18982
18983 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
18984 Rewrite (Lo,
18985 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
18986
18987 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
18988 Rewrite (Hi,
18989 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
18990 end if;
18991
18992 Set_Etype (Lo, T);
18993 Set_Etype (Hi, T);
18994 end if;
18995
a5b62485
AC
18996 -- If the bounds of the range have been mistakenly given as string
18997 -- literals (perhaps in place of character literals), then an error
18998 -- has already been reported, but we rewrite the string literal as a
18999 -- bound of the range's type to avoid blowups in later processing
19000 -- that looks at static values.
996ae0b0
RK
19001
19002 if Nkind (Lo) = N_String_Literal then
19003 Rewrite (Lo,
19004 Make_Attribute_Reference (Sloc (Lo),
19005 Attribute_Name => Name_First,
e4494292 19006 Prefix => New_Occurrence_Of (T, Sloc (Lo))));
996ae0b0
RK
19007 Analyze_And_Resolve (Lo);
19008 end if;
19009
19010 if Nkind (Hi) = N_String_Literal then
19011 Rewrite (Hi,
19012 Make_Attribute_Reference (Sloc (Hi),
19013 Attribute_Name => Name_First,
e4494292 19014 Prefix => New_Occurrence_Of (T, Sloc (Hi))));
996ae0b0
RK
19015 Analyze_And_Resolve (Hi);
19016 end if;
19017
19018 -- If bounds aren't scalar at this point then exit, avoiding
19019 -- problems with further processing of the range in this procedure.
19020
19021 if not Is_Scalar_Type (Etype (Lo)) then
19022 return;
19023 end if;
19024
19025 -- Resolve (actually Sem_Eval) has checked that the bounds are in
19026 -- then range of the base type. Here we check whether the bounds
19027 -- are in the range of the subtype itself. Note that if the bounds
19028 -- represent the null range the Constraint_Error exception should
19029 -- not be raised.
19030
19031 -- ??? The following code should be cleaned up as follows
a5b62485 19032
fbf5a39b 19033 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
996ae0b0 19034 -- is done in the call to Range_Check (R, T); below
a5b62485 19035
996ae0b0
RK
19036 -- 2. The use of R_Check_Off should be investigated and possibly
19037 -- removed, this would clean up things a bit.
19038
19039 if Is_Null_Range (Lo, Hi) then
19040 null;
19041
19042 else
fbf5a39b
AC
19043 -- Capture values of bounds and generate temporaries for them
19044 -- if needed, before applying checks, since checks may cause
19045 -- duplication of the expression without forcing evaluation.
19046
ef992452 19047 -- The forced evaluation removes side effects from expressions,
f5da7a97 19048 -- which should occur also in GNATprove mode. Otherwise, we end up
06b599fd
YM
19049 -- with unexpected insertions of actions at places where this is
19050 -- not supposed to occur, e.g. on default parameters of a call.
ef992452 19051
f5da7a97 19052 if Expander_Active or GNATprove_Mode then
fbf5a39b
AC
19053 Force_Evaluation (Lo);
19054 Force_Evaluation (Hi);
19055 end if;
19056
996ae0b0 19057 -- We use a flag here instead of suppressing checks on the
fbf5a39b
AC
19058 -- type because the type we check against isn't necessarily
19059 -- the place where we put the check.
996ae0b0
RK
19060
19061 if not R_Check_Off then
dc06abec 19062 R_Checks := Get_Range_Checks (R, T);
996ae0b0 19063
0592046e
AC
19064 -- Look up tree to find an appropriate insertion point. We
19065 -- can't just use insert_actions because later processing
885c4871 19066 -- depends on the insertion node. Prior to Ada 2012 the
0592046e
AC
19067 -- insertion point could only be a declaration or a loop, but
19068 -- quantified expressions can appear within any context in an
19069 -- expression, and the insertion point can be any statement,
19070 -- pragma, or declaration.
19071
19072 Insert_Node := Parent (R);
19073 while Present (Insert_Node) loop
19074 exit when
19075 Nkind (Insert_Node) in N_Declaration
19076 and then
19077 not Nkind_In
19078 (Insert_Node, N_Component_Declaration,
19079 N_Loop_Parameter_Specification,
19080 N_Function_Specification,
19081 N_Procedure_Specification);
19082
19083 exit when Nkind (Insert_Node) in N_Later_Decl_Item
19084 or else Nkind (Insert_Node) in
19085 N_Statement_Other_Than_Procedure_Call
19086 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
19087 N_Pragma);
19088
19089 Insert_Node := Parent (Insert_Node);
996ae0b0
RK
19090 end loop;
19091
19092 -- Why would Type_Decl not be present??? Without this test,
19093 -- short regression tests fail.
19094
0592046e 19095 if Present (Insert_Node) then
fbf5a39b 19096
0592046e
AC
19097 -- Case of loop statement. Verify that the range is part
19098 -- of the subtype indication of the iteration scheme.
fbf5a39b 19099
0592046e 19100 if Nkind (Insert_Node) = N_Loop_Statement then
996ae0b0 19101 declare
9dfd2ff8 19102 Indic : Node_Id;
fbf5a39b 19103
996ae0b0 19104 begin
9dfd2ff8 19105 Indic := Parent (R);
7d7af38a
JM
19106 while Present (Indic)
19107 and then Nkind (Indic) /= N_Subtype_Indication
996ae0b0
RK
19108 loop
19109 Indic := Parent (Indic);
19110 end loop;
19111
19112 if Present (Indic) then
19113 Def_Id := Etype (Subtype_Mark (Indic));
19114
19115 Insert_Range_Checks
19116 (R_Checks,
0592046e 19117 Insert_Node,
996ae0b0 19118 Def_Id,
0592046e 19119 Sloc (Insert_Node),
996ae0b0
RK
19120 R,
19121 Do_Before => True);
19122 end if;
19123 end;
fbf5a39b 19124
0592046e
AC
19125 -- Insertion before a declaration. If the declaration
19126 -- includes discriminants, the list of applicable checks
19127 -- is given by the caller.
fbf5a39b 19128
0592046e
AC
19129 elsif Nkind (Insert_Node) in N_Declaration then
19130 Def_Id := Defining_Identifier (Insert_Node);
996ae0b0
RK
19131
19132 if (Ekind (Def_Id) = E_Record_Type
19133 and then Depends_On_Discriminant (R))
19134 or else
19135 (Ekind (Def_Id) = E_Protected_Type
19136 and then Has_Discriminants (Def_Id))
19137 then
19138 Append_Range_Checks
0592046e
AC
19139 (R_Checks,
19140 Check_List, Def_Id, Sloc (Insert_Node), R);
996ae0b0
RK
19141
19142 else
19143 Insert_Range_Checks
0592046e
AC
19144 (R_Checks,
19145 Insert_Node, Def_Id, Sloc (Insert_Node), R);
996ae0b0
RK
19146
19147 end if;
0592046e
AC
19148
19149 -- Insertion before a statement. Range appears in the
19150 -- context of a quantified expression. Insertion will
19151 -- take place when expression is expanded.
19152
19153 else
19154 null;
996ae0b0
RK
19155 end if;
19156 end if;
19157 end if;
19158 end if;
996ae0b0 19159
0592046e
AC
19160 -- Case of other than an explicit N_Range node
19161
ef992452 19162 -- The forced evaluation removes side effects from expressions, which
f5da7a97
YM
19163 -- should occur also in GNATprove mode. Otherwise, we end up with
19164 -- unexpected insertions of actions at places where this is not
19165 -- supposed to occur, e.g. on default parameters of a call.
ef992452 19166
f5da7a97 19167 elsif Expander_Active or GNATprove_Mode then
fbf5a39b 19168 Get_Index_Bounds (R, Lo, Hi);
996ae0b0
RK
19169 Force_Evaluation (Lo);
19170 Force_Evaluation (Hi);
19171 end if;
996ae0b0
RK
19172 end Process_Range_Expr_In_Decl;
19173
19174 --------------------------------------
19175 -- Process_Real_Range_Specification --
19176 --------------------------------------
19177
19178 procedure Process_Real_Range_Specification (Def : Node_Id) is
19179 Spec : constant Node_Id := Real_Range_Specification (Def);
19180 Lo : Node_Id;
19181 Hi : Node_Id;
19182 Err : Boolean := False;
19183
19184 procedure Analyze_Bound (N : Node_Id);
19185 -- Analyze and check one bound
19186
fbf5a39b
AC
19187 -------------------
19188 -- Analyze_Bound --
19189 -------------------
19190
996ae0b0
RK
19191 procedure Analyze_Bound (N : Node_Id) is
19192 begin
19193 Analyze_And_Resolve (N, Any_Real);
19194
19195 if not Is_OK_Static_Expression (N) then
fbf5a39b
AC
19196 Flag_Non_Static_Expr
19197 ("bound in real type definition is not static!", N);
996ae0b0
RK
19198 Err := True;
19199 end if;
19200 end Analyze_Bound;
19201
fbf5a39b
AC
19202 -- Start of processing for Process_Real_Range_Specification
19203
996ae0b0
RK
19204 begin
19205 if Present (Spec) then
19206 Lo := Low_Bound (Spec);
19207 Hi := High_Bound (Spec);
19208 Analyze_Bound (Lo);
19209 Analyze_Bound (Hi);
19210
19211 -- If error, clear away junk range specification
19212
19213 if Err then
19214 Set_Real_Range_Specification (Def, Empty);
19215 end if;
19216 end if;
19217 end Process_Real_Range_Specification;
19218
19219 ---------------------
19220 -- Process_Subtype --
19221 ---------------------
19222
19223 function Process_Subtype
19224 (S : Node_Id;
19225 Related_Nod : Node_Id;
19226 Related_Id : Entity_Id := Empty;
b0f26df5 19227 Suffix : Character := ' ') return Entity_Id
996ae0b0
RK
19228 is
19229 P : Node_Id;
19230 Def_Id : Entity_Id;
9dfd2ff8 19231 Error_Node : Node_Id;
996ae0b0
RK
19232 Full_View_Id : Entity_Id;
19233 Subtype_Mark_Id : Entity_Id;
fbf5a39b 19234
9dfd2ff8
CC
19235 May_Have_Null_Exclusion : Boolean;
19236
fbf5a39b
AC
19237 procedure Check_Incomplete (T : Entity_Id);
19238 -- Called to verify that an incomplete type is not used prematurely
19239
19240 ----------------------
19241 -- Check_Incomplete --
19242 ----------------------
19243
19244 procedure Check_Incomplete (T : Entity_Id) is
19245 begin
88b32fc3
BD
19246 -- Ada 2005 (AI-412): Incomplete subtypes are legal
19247
19248 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
19249 and then
0791fbe9 19250 not (Ada_Version >= Ada_2005
88b32fc3
BD
19251 and then
19252 (Nkind (Parent (T)) = N_Subtype_Declaration
19253 or else
19254 (Nkind (Parent (T)) = N_Subtype_Indication
19255 and then Nkind (Parent (Parent (T))) =
19256 N_Subtype_Declaration)))
19257 then
fbf5a39b
AC
19258 Error_Msg_N ("invalid use of type before its full declaration", T);
19259 end if;
19260 end Check_Incomplete;
19261
19262 -- Start of processing for Process_Subtype
996ae0b0
RK
19263
19264 begin
fbf5a39b
AC
19265 -- Case of no constraints present
19266
19267 if Nkind (S) /= N_Subtype_Indication then
fbf5a39b
AC
19268 Find_Type (S);
19269 Check_Incomplete (S);
9dfd2ff8 19270 P := Parent (S);
2820d220 19271
0ab80019 19272 -- Ada 2005 (AI-231): Static check
2820d220 19273
0791fbe9 19274 if Ada_Version >= Ada_2005
9dfd2ff8
CC
19275 and then Present (P)
19276 and then Null_Exclusion_Present (P)
19277 and then Nkind (P) /= N_Access_To_Object_Definition
2820d220
AC
19278 and then not Is_Access_Type (Entity (S))
19279 then
2b73cf68 19280 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
9dfd2ff8
CC
19281 end if;
19282
7d7af38a
JM
19283 -- The following is ugly, can't we have a range or even a flag???
19284
9dfd2ff8 19285 May_Have_Null_Exclusion :=
7d7af38a
JM
19286 Nkind_In (P, N_Access_Definition,
19287 N_Access_Function_Definition,
19288 N_Access_Procedure_Definition,
19289 N_Access_To_Object_Definition,
19290 N_Allocator,
19291 N_Component_Definition)
19292 or else
19293 Nkind_In (P, N_Derived_Type_Definition,
19294 N_Discriminant_Specification,
aecf0203 19295 N_Formal_Object_Declaration,
7d7af38a 19296 N_Object_Declaration,
aecf0203 19297 N_Object_Renaming_Declaration,
7d7af38a
JM
19298 N_Parameter_Specification,
19299 N_Subtype_Declaration);
9dfd2ff8
CC
19300
19301 -- Create an Itype that is a duplicate of Entity (S) but with the
498d1b80 19302 -- null-exclusion attribute.
9dfd2ff8
CC
19303
19304 if May_Have_Null_Exclusion
19305 and then Is_Access_Type (Entity (S))
19306 and then Null_Exclusion_Present (P)
19307
19308 -- No need to check the case of an access to object definition.
19309 -- It is correct to define double not-null pointers.
88b32fc3 19310
9dfd2ff8
CC
19311 -- Example:
19312 -- type Not_Null_Int_Ptr is not null access Integer;
19313 -- type Acc is not null access Not_Null_Int_Ptr;
19314
19315 and then Nkind (P) /= N_Access_To_Object_Definition
19316 then
19317 if Can_Never_Be_Null (Entity (S)) then
19318 case Nkind (Related_Nod) is
19319 when N_Full_Type_Declaration =>
19320 if Nkind (Type_Definition (Related_Nod))
19321 in N_Array_Type_Definition
19322 then
19323 Error_Node :=
19324 Subtype_Indication
19325 (Component_Definition
19326 (Type_Definition (Related_Nod)));
19327 else
19328 Error_Node :=
19329 Subtype_Indication (Type_Definition (Related_Nod));
19330 end if;
19331
19332 when N_Subtype_Declaration =>
19333 Error_Node := Subtype_Indication (Related_Nod);
19334
19335 when N_Object_Declaration =>
19336 Error_Node := Object_Definition (Related_Nod);
19337
19338 when N_Component_Declaration =>
19339 Error_Node :=
19340 Subtype_Indication (Component_Definition (Related_Nod));
19341
aecf0203
AC
19342 when N_Allocator =>
19343 Error_Node := Expression (Related_Nod);
19344
9dfd2ff8
CC
19345 when others =>
19346 pragma Assert (False);
19347 Error_Node := Related_Nod;
19348 end case;
19349
2b73cf68
JM
19350 Error_Msg_NE
19351 ("`NOT NULL` not allowed (& already excludes null)",
19352 Error_Node,
19353 Entity (S));
9dfd2ff8
CC
19354 end if;
19355
19356 Set_Etype (S,
19357 Create_Null_Excluding_Itype
19358 (T => Entity (S),
19359 Related_Nod => P));
19360 Set_Entity (S, Etype (S));
2820d220 19361 end if;
9dfd2ff8 19362
fbf5a39b
AC
19363 return Entity (S);
19364
996ae0b0
RK
19365 -- Case of constraint present, so that we have an N_Subtype_Indication
19366 -- node (this node is created only if constraints are present).
19367
fbf5a39b 19368 else
996ae0b0
RK
19369 Find_Type (Subtype_Mark (S));
19370
19371 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
19372 and then not
19373 (Nkind (Parent (S)) = N_Subtype_Declaration
653da906 19374 and then Is_Itype (Defining_Identifier (Parent (S))))
996ae0b0
RK
19375 then
19376 Check_Incomplete (Subtype_Mark (S));
19377 end if;
19378
19379 P := Parent (S);
19380 Subtype_Mark_Id := Entity (Subtype_Mark (S));
19381
996ae0b0
RK
19382 -- Explicit subtype declaration case
19383
19384 if Nkind (P) = N_Subtype_Declaration then
19385 Def_Id := Defining_Identifier (P);
19386
19387 -- Explicit derived type definition case
19388
19389 elsif Nkind (P) = N_Derived_Type_Definition then
19390 Def_Id := Defining_Identifier (Parent (P));
19391
19392 -- Implicit case, the Def_Id must be created as an implicit type.
a5b62485
AC
19393 -- The one exception arises in the case of concurrent types, array
19394 -- and access types, where other subsidiary implicit types may be
19395 -- created and must appear before the main implicit type. In these
19396 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
19397 -- has not yet been called to create Def_Id.
996ae0b0
RK
19398
19399 else
19400 if Is_Array_Type (Subtype_Mark_Id)
19401 or else Is_Concurrent_Type (Subtype_Mark_Id)
19402 or else Is_Access_Type (Subtype_Mark_Id)
19403 then
19404 Def_Id := Empty;
19405
19406 -- For the other cases, we create a new unattached Itype,
19407 -- and set the indication to ensure it gets attached later.
19408
19409 else
19410 Def_Id :=
19411 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
19412 end if;
996ae0b0
RK
19413 end if;
19414
19415 -- If the kind of constraint is invalid for this kind of type,
19416 -- then give an error, and then pretend no constraint was given.
19417
19418 if not Is_Valid_Constraint_Kind
19419 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
19420 then
19421 Error_Msg_N
19422 ("incorrect constraint for this kind of type", Constraint (S));
19423
19424 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
19425
758c442c 19426 -- Set Ekind of orphan itype, to prevent cascaded errors
82c80734
RD
19427
19428 if Present (Def_Id) then
19429 Set_Ekind (Def_Id, Ekind (Any_Type));
19430 end if;
19431
996ae0b0
RK
19432 -- Make recursive call, having got rid of the bogus constraint
19433
19434 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
19435 end if;
19436
36b8f95f
AC
19437 -- Remaining processing depends on type. Select on Base_Type kind to
19438 -- ensure getting to the concrete type kind in the case of a private
19439 -- subtype (needed when only doing semantic analysis).
996ae0b0 19440
36b8f95f 19441 case Ekind (Base_Type (Subtype_Mark_Id)) is
996ae0b0 19442 when Access_Kind =>
fb620b37
AC
19443
19444 -- If this is a constraint on a class-wide type, discard it.
19445 -- There is currently no way to express a partial discriminant
19446 -- constraint on a type with unknown discriminants. This is
19447 -- a pathology that the ACATS wisely decides not to test.
19448
19449 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
19450 if Comes_From_Source (S) then
19451 Error_Msg_N
19452 ("constraint on class-wide type ignored?",
19453 Constraint (S));
19454 end if;
19455
19456 if Nkind (P) = N_Subtype_Declaration then
19457 Set_Subtype_Indication (P,
19458 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
19459 end if;
19460
19461 return Subtype_Mark_Id;
19462 end if;
19463
996ae0b0
RK
19464 Constrain_Access (Def_Id, S, Related_Nod);
19465
fea9e956
ES
19466 if Expander_Active
19467 and then Is_Itype (Designated_Type (Def_Id))
19468 and then Nkind (Related_Nod) = N_Subtype_Declaration
19469 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
19470 then
19471 Build_Itype_Reference
19472 (Designated_Type (Def_Id), Related_Nod);
19473 end if;
19474
996ae0b0
RK
19475 when Array_Kind =>
19476 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
19477
19478 when Decimal_Fixed_Point_Kind =>
07fc65c4 19479 Constrain_Decimal (Def_Id, S);
996ae0b0
RK
19480
19481 when Enumeration_Kind =>
07fc65c4 19482 Constrain_Enumeration (Def_Id, S);
996ae0b0
RK
19483
19484 when Ordinary_Fixed_Point_Kind =>
07fc65c4 19485 Constrain_Ordinary_Fixed (Def_Id, S);
996ae0b0
RK
19486
19487 when Float_Kind =>
07fc65c4 19488 Constrain_Float (Def_Id, S);
996ae0b0
RK
19489
19490 when Integer_Kind =>
07fc65c4 19491 Constrain_Integer (Def_Id, S);
996ae0b0
RK
19492
19493 when E_Record_Type |
19494 E_Record_Subtype |
19495 Class_Wide_Kind |
19496 E_Incomplete_Type =>
19497 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
19498
93bcda23
AC
19499 if Ekind (Def_Id) = E_Incomplete_Type then
19500 Set_Private_Dependents (Def_Id, New_Elmt_List);
19501 end if;
19502
996ae0b0
RK
19503 when Private_Kind =>
19504 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
19505 Set_Private_Dependents (Def_Id, New_Elmt_List);
19506
19507 -- In case of an invalid constraint prevent further processing
19508 -- since the type constructed is missing expected fields.
19509
19510 if Etype (Def_Id) = Any_Type then
19511 return Def_Id;
19512 end if;
19513
19514 -- If the full view is that of a task with discriminants,
19515 -- we must constrain both the concurrent type and its
19516 -- corresponding record type. Otherwise we will just propagate
19517 -- the constraint to the full view, if available.
19518
19519 if Present (Full_View (Subtype_Mark_Id))
19520 and then Has_Discriminants (Subtype_Mark_Id)
19521 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
19522 then
19523 Full_View_Id :=
19524 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
19525
19526 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
19527 Constrain_Concurrent (Full_View_Id, S,
19528 Related_Nod, Related_Id, Suffix);
19529 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
19530 Set_Full_View (Def_Id, Full_View_Id);
19531
88b32fc3
BD
19532 -- Introduce an explicit reference to the private subtype,
19533 -- to prevent scope anomalies in gigi if first use appears
19534 -- in a nested context, e.g. a later function body.
19535 -- Should this be generated in other contexts than a full
19536 -- type declaration?
19537
19538 if Is_Itype (Def_Id)
19539 and then
19540 Nkind (Parent (P)) = N_Full_Type_Declaration
19541 then
fea9e956 19542 Build_Itype_Reference (Def_Id, Parent (P));
88b32fc3
BD
19543 end if;
19544
996ae0b0
RK
19545 else
19546 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
19547 end if;
19548
19549 when Concurrent_Kind =>
19550 Constrain_Concurrent (Def_Id, S,
19551 Related_Nod, Related_Id, Suffix);
19552
19553 when others =>
19554 Error_Msg_N ("invalid subtype mark in subtype indication", S);
19555 end case;
19556
19557 -- Size and Convention are always inherited from the base type
19558
19559 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
19560 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
19561
19562 return Def_Id;
996ae0b0
RK
19563 end if;
19564 end Process_Subtype;
19565
fea9e956
ES
19566 ---------------------------------------
19567 -- Check_Anonymous_Access_Components --
19568 ---------------------------------------
996ae0b0 19569
fea9e956
ES
19570 procedure Check_Anonymous_Access_Components
19571 (Typ_Decl : Node_Id;
19572 Typ : Entity_Id;
19573 Prev : Entity_Id;
19574 Comp_List : Node_Id)
fbf5a39b 19575 is
fea9e956
ES
19576 Loc : constant Source_Ptr := Sloc (Typ_Decl);
19577 Anon_Access : Entity_Id;
19578 Acc_Def : Node_Id;
19579 Comp : Node_Id;
19580 Comp_Def : Node_Id;
19581 Decl : Node_Id;
19582 Type_Def : Node_Id;
19583
19584 procedure Build_Incomplete_Type_Declaration;
758c442c 19585 -- If the record type contains components that include an access to the
fea9e956
ES
19586 -- current record, then create an incomplete type declaration for the
19587 -- record, to be used as the designated type of the anonymous access.
19588 -- This is done only once, and only if there is no previous partial
19589 -- view of the type.
19590
5320014a 19591 function Designates_T (Subt : Node_Id) return Boolean;
d33fb1e6
BD
19592 -- Check whether a node designates the enclosing record type, or 'Class
19593 -- of that type
5320014a 19594
fea9e956
ES
19595 function Mentions_T (Acc_Def : Node_Id) return Boolean;
19596 -- Check whether an access definition includes a reference to
5320014a
ST
19597 -- the enclosing record type. The reference can be a subtype mark
19598 -- in the access definition itself, a 'Class attribute reference, or
19599 -- recursively a reference appearing in a parameter specification
19600 -- or result definition of an access_to_subprogram definition.
996ae0b0 19601
fea9e956
ES
19602 --------------------------------------
19603 -- Build_Incomplete_Type_Declaration --
19604 --------------------------------------
996ae0b0 19605
fea9e956
ES
19606 procedure Build_Incomplete_Type_Declaration is
19607 Decl : Node_Id;
19608 Inc_T : Entity_Id;
19609 H : Entity_Id;
996ae0b0 19610
d33fb1e6
BD
19611 -- Is_Tagged indicates whether the type is tagged. It is tagged if
19612 -- it's "is new ... with record" or else "is tagged record ...".
19613
19614 Is_Tagged : constant Boolean :=
19615 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
19616 and then
19617 Present
19618 (Record_Extension_Part (Type_Definition (Typ_Decl))))
19619 or else
19620 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
19621 and then Tagged_Present (Type_Definition (Typ_Decl)));
19622
fea9e956
ES
19623 begin
19624 -- If there is a previous partial view, no need to create a new one
19625 -- If the partial view, given by Prev, is incomplete, If Prev is
19626 -- a private declaration, full declaration is flagged accordingly.
758c442c 19627
fea9e956 19628 if Prev /= Typ then
d33fb1e6 19629 if Is_Tagged then
fea9e956
ES
19630 Make_Class_Wide_Type (Prev);
19631 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
19632 Set_Etype (Class_Wide_Type (Typ), Typ);
19633 end if;
758c442c 19634
fea9e956 19635 return;
758c442c 19636
fea9e956 19637 elsif Has_Private_Declaration (Typ) then
d33fb1e6
BD
19638
19639 -- If we refer to T'Class inside T, and T is the completion of a
19640 -- private type, then we need to make sure the class-wide type
19641 -- exists.
19642
19643 if Is_Tagged then
19644 Make_Class_Wide_Type (Typ);
19645 end if;
19646
fea9e956 19647 return;
57193e09 19648
fea9e956
ES
19649 -- If there was a previous anonymous access type, the incomplete
19650 -- type declaration will have been created already.
57193e09 19651
fea9e956
ES
19652 elsif Present (Current_Entity (Typ))
19653 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
19654 and then Full_View (Current_Entity (Typ)) = Typ
19655 then
b0760739
AC
19656 if Is_Tagged
19657 and then Comes_From_Source (Current_Entity (Typ))
19658 and then not Is_Tagged_Type (Current_Entity (Typ))
19659 then
19660 Make_Class_Wide_Type (Typ);
19661 Error_Msg_N
324ac540 19662 ("incomplete view of tagged type should be declared tagged??",
2383acbd 19663 Parent (Current_Entity (Typ)));
b0760739 19664 end if;
fea9e956 19665 return;
758c442c 19666
fea9e956 19667 else
ce4a6e84
RD
19668 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
19669 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
758c442c 19670
2383acbd
AC
19671 -- Type has already been inserted into the current scope. Remove
19672 -- it, and add incomplete declaration for type, so that subsequent
19673 -- anonymous access types can use it. The entity is unchained from
19674 -- the homonym list and from immediate visibility. After analysis,
19675 -- the entity in the incomplete declaration becomes immediately
19676 -- visible in the record declaration that follows.
758c442c 19677
fea9e956 19678 H := Current_Entity (Typ);
758c442c 19679
fea9e956
ES
19680 if H = Typ then
19681 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
19682 else
19683 while Present (H)
19684 and then Homonym (H) /= Typ
19685 loop
19686 H := Homonym (Typ);
19687 end loop;
758c442c 19688
fea9e956 19689 Set_Homonym (H, Homonym (Typ));
758c442c 19690 end if;
758c442c 19691
fea9e956
ES
19692 Insert_Before (Typ_Decl, Decl);
19693 Analyze (Decl);
19694 Set_Full_View (Inc_T, Typ);
758c442c 19695
d33fb1e6 19696 if Is_Tagged then
2383acbd
AC
19697
19698 -- Create a common class-wide type for both views, and set the
19699 -- Etype of the class-wide type to the full view.
758c442c 19700
fea9e956
ES
19701 Make_Class_Wide_Type (Inc_T);
19702 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
19703 Set_Etype (Class_Wide_Type (Typ), Typ);
19704 end if;
19705 end if;
19706 end Build_Incomplete_Type_Declaration;
758c442c 19707
5320014a
ST
19708 ------------------
19709 -- Designates_T --
19710 ------------------
19711
19712 function Designates_T (Subt : Node_Id) return Boolean is
fea9e956 19713 Type_Id : constant Name_Id := Chars (Typ);
758c442c 19714
2b73cf68 19715 function Names_T (Nam : Node_Id) return Boolean;
2b73cf68
JM
19716 -- The record type has not been introduced in the current scope
19717 -- yet, so we must examine the name of the type itself, either
19718 -- an identifier T, or an expanded name of the form P.T, where
19719 -- P denotes the current scope.
19720
dc06abec
RD
19721 -------------
19722 -- Names_T --
19723 -------------
19724
2b73cf68
JM
19725 function Names_T (Nam : Node_Id) return Boolean is
19726 begin
19727 if Nkind (Nam) = N_Identifier then
19728 return Chars (Nam) = Type_Id;
19729
19730 elsif Nkind (Nam) = N_Selected_Component then
19731 if Chars (Selector_Name (Nam)) = Type_Id then
19732 if Nkind (Prefix (Nam)) = N_Identifier then
19733 return Chars (Prefix (Nam)) = Chars (Current_Scope);
19734
19735 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
dc06abec
RD
19736 return Chars (Selector_Name (Prefix (Nam))) =
19737 Chars (Current_Scope);
2b73cf68
JM
19738 else
19739 return False;
19740 end if;
7d7af38a 19741
2b73cf68
JM
19742 else
19743 return False;
19744 end if;
7d7af38a 19745
2b73cf68
JM
19746 else
19747 return False;
19748 end if;
19749 end Names_T;
19750
5320014a 19751 -- Start of processing for Designates_T
dc06abec 19752
fea9e956 19753 begin
5320014a
ST
19754 if Nkind (Subt) = N_Identifier then
19755 return Chars (Subt) = Type_Id;
88b32fc3 19756
fea9e956 19757 -- Reference can be through an expanded name which has not been
2b73cf68 19758 -- analyzed yet, and which designates enclosing scopes.
88b32fc3 19759
5320014a
ST
19760 elsif Nkind (Subt) = N_Selected_Component then
19761 if Names_T (Subt) then
19762 return True;
88b32fc3 19763
5320014a
ST
19764 -- Otherwise it must denote an entity that is already visible.
19765 -- The access definition may name a subtype of the enclosing
19766 -- type, if there is a previous incomplete declaration for it.
fea9e956 19767
fea9e956 19768 else
5320014a
ST
19769 Find_Selected_Component (Subt);
19770 return
19771 Is_Entity_Name (Subt)
19772 and then Scope (Entity (Subt)) = Current_Scope
19773 and then
19774 (Chars (Base_Type (Entity (Subt))) = Type_Id
19775 or else
19776 (Is_Class_Wide_Type (Entity (Subt))
19777 and then
b69cd36a 19778 Chars (Etype (Base_Type (Entity (Subt)))) =
7d7af38a 19779 Type_Id));
758c442c
GD
19780 end if;
19781
5320014a
ST
19782 -- A reference to the current type may appear as the prefix of
19783 -- a 'Class attribute.
19784
19785 elsif Nkind (Subt) = N_Attribute_Reference
19786 and then Attribute_Name (Subt) = Name_Class
19787 then
19788 return Names_T (Prefix (Subt));
19789
fea9e956 19790 else
5320014a
ST
19791 return False;
19792 end if;
19793 end Designates_T;
758c442c 19794
5320014a
ST
19795 ----------------
19796 -- Mentions_T --
19797 ----------------
fea9e956 19798
5320014a
ST
19799 function Mentions_T (Acc_Def : Node_Id) return Boolean is
19800 Param_Spec : Node_Id;
fea9e956 19801
5320014a 19802 Acc_Subprg : constant Node_Id :=
7d7af38a 19803 Access_To_Subprogram_Definition (Acc_Def);
fea9e956 19804
5320014a
ST
19805 begin
19806 if No (Acc_Subprg) then
19807 return Designates_T (Subtype_Mark (Acc_Def));
758c442c 19808 end if;
5320014a
ST
19809
19810 -- Component is an access_to_subprogram: examine its formals,
19811 -- and result definition in the case of an access_to_function.
19812
19813 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
19814 while Present (Param_Spec) loop
19815 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
19816 and then Mentions_T (Parameter_Type (Param_Spec))
19817 then
19818 return True;
19819
19820 elsif Designates_T (Parameter_Type (Param_Spec)) then
19821 return True;
19822 end if;
19823
19824 Next (Param_Spec);
19825 end loop;
19826
19827 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
19828 if Nkind (Result_Definition (Acc_Subprg)) =
19829 N_Access_Definition
19830 then
19831 return Mentions_T (Result_Definition (Acc_Subprg));
19832 else
19833 return Designates_T (Result_Definition (Acc_Subprg));
19834 end if;
19835 end if;
19836
19837 return False;
fea9e956 19838 end Mentions_T;
996ae0b0 19839
fea9e956 19840 -- Start of processing for Check_Anonymous_Access_Components
758c442c 19841
fea9e956
ES
19842 begin
19843 if No (Comp_List) then
19844 return;
19845 end if;
758c442c 19846
fea9e956
ES
19847 Comp := First (Component_Items (Comp_List));
19848 while Present (Comp) loop
19849 if Nkind (Comp) = N_Component_Declaration
19850 and then Present
19851 (Access_Definition (Component_Definition (Comp)))
19852 and then
19853 Mentions_T (Access_Definition (Component_Definition (Comp)))
57193e09 19854 then
fea9e956
ES
19855 Comp_Def := Component_Definition (Comp);
19856 Acc_Def :=
19857 Access_To_Subprogram_Definition
19858 (Access_Definition (Comp_Def));
758c442c 19859
fea9e956 19860 Build_Incomplete_Type_Declaration;
092ef350 19861 Anon_Access := Make_Temporary (Loc, 'S');
758c442c 19862
fea9e956
ES
19863 -- Create a declaration for the anonymous access type: either
19864 -- an access_to_object or an access_to_subprogram.
758c442c 19865
fea9e956 19866 if Present (Acc_Def) then
499769ec 19867 if Nkind (Acc_Def) = N_Access_Function_Definition then
fea9e956
ES
19868 Type_Def :=
19869 Make_Access_Function_Definition (Loc,
19870 Parameter_Specifications =>
19871 Parameter_Specifications (Acc_Def),
19872 Result_Definition => Result_Definition (Acc_Def));
19873 else
19874 Type_Def :=
19875 Make_Access_Procedure_Definition (Loc,
19876 Parameter_Specifications =>
19877 Parameter_Specifications (Acc_Def));
19878 end if;
758c442c 19879
758c442c 19880 else
fea9e956
ES
19881 Type_Def :=
19882 Make_Access_To_Object_Definition (Loc,
19883 Subtype_Indication =>
19884 Relocate_Node
19885 (Subtype_Mark
19886 (Access_Definition (Comp_Def))));
2b73cf68
JM
19887
19888 Set_Constant_Present
19889 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
19890 Set_All_Present
19891 (Type_Def, All_Present (Access_Definition (Comp_Def)));
758c442c
GD
19892 end if;
19893
2b73cf68
JM
19894 Set_Null_Exclusion_Present
19895 (Type_Def,
19896 Null_Exclusion_Present (Access_Definition (Comp_Def)));
19897
19898 Decl :=
19899 Make_Full_Type_Declaration (Loc,
19900 Defining_Identifier => Anon_Access,
19901 Type_Definition => Type_Def);
fea9e956
ES
19902
19903 Insert_Before (Typ_Decl, Decl);
758c442c 19904 Analyze (Decl);
758c442c 19905
499769ec
AC
19906 -- If an access to subprogram, create the extra formals
19907
19908 if Present (Acc_Def) then
19909 Create_Extra_Formals (Designated_Type (Anon_Access));
19910
19911 -- If an access to object, preserve entity of designated type,
fea9e956
ES
19912 -- for ASIS use, before rewriting the component definition.
19913
499769ec 19914 else
fea9e956
ES
19915 declare
19916 Desig : Entity_Id;
19917
19918 begin
19919 Desig := Entity (Subtype_Indication (Type_Def));
19920
19921 -- If the access definition is to the current record,
19922 -- the visible entity at this point is an incomplete
19923 -- type. Retrieve the full view to simplify ASIS queries
19924
19925 if Ekind (Desig) = E_Incomplete_Type then
19926 Desig := Full_View (Desig);
19927 end if;
19928
19929 Set_Entity
19930 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
19931 end;
758c442c 19932 end if;
fea9e956
ES
19933
19934 Rewrite (Comp_Def,
19935 Make_Component_Definition (Loc,
19936 Subtype_Indication =>
19937 New_Occurrence_Of (Anon_Access, Loc)));
5320014a
ST
19938
19939 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
19940 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
19941 else
19942 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
19943 end if;
19944
fea9e956 19945 Set_Is_Local_Anonymous_Access (Anon_Access);
758c442c 19946 end if;
758c442c 19947
fea9e956
ES
19948 Next (Comp);
19949 end loop;
19950
19951 if Present (Variant_Part (Comp_List)) then
19952 declare
19953 V : Node_Id;
19954 begin
19955 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
19956 while Present (V) loop
19957 Check_Anonymous_Access_Components
19958 (Typ_Decl, Typ, Prev, Component_List (V));
19959 Next_Non_Pragma (V);
19960 end loop;
19961 end;
19962 end if;
19963 end Check_Anonymous_Access_Components;
19964
3db39676
YM
19965 ----------------------------------
19966 -- Preanalyze_Assert_Expression --
19967 ----------------------------------
19968
19969 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19970 begin
19971 In_Assertion_Expr := In_Assertion_Expr + 1;
19972 Preanalyze_Spec_Expression (N, T);
19973 In_Assertion_Expr := In_Assertion_Expr - 1;
19974 end Preanalyze_Assert_Expression;
19975
ce4a6e84
RD
19976 --------------------------------
19977 -- Preanalyze_Spec_Expression --
19978 --------------------------------
19979
19980 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19981 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19982 begin
19983 In_Spec_Expression := True;
19984 Preanalyze_And_Resolve (N, T);
19985 In_Spec_Expression := Save_In_Spec_Expression;
19986 end Preanalyze_Spec_Expression;
19987
fea9e956
ES
19988 -----------------------------
19989 -- Record_Type_Declaration --
19990 -----------------------------
19991
19992 procedure Record_Type_Declaration
19993 (T : Entity_Id;
19994 N : Node_Id;
19995 Prev : Entity_Id)
19996 is
19997 Def : constant Node_Id := Type_Definition (N);
19998 Is_Tagged : Boolean;
19999 Tag_Comp : Entity_Id;
758c442c
GD
20000
20001 begin
996ae0b0
RK
20002 -- These flags must be initialized before calling Process_Discriminants
20003 -- because this routine makes use of them.
20004
ce2b6ba5
JM
20005 Set_Ekind (T, E_Record_Type);
20006 Set_Etype (T, T);
20007 Init_Size_Align (T);
20008 Set_Interfaces (T, No_Elist);
20009 Set_Stored_Constraint (T, No_Elist);
996ae0b0 20010
758c442c 20011 -- Normal case
996ae0b0 20012
0791fbe9 20013 if Ada_Version < Ada_2005
758c442c
GD
20014 or else not Interface_Present (Def)
20015 then
d8b962d8 20016 if Limited_Present (Def) then
2ba431e5 20017 Check_SPARK_Restriction ("limited is not allowed", N);
d8b962d8
AC
20018 end if;
20019
20020 if Abstract_Present (Def) then
2ba431e5 20021 Check_SPARK_Restriction ("abstract is not allowed", N);
d8b962d8
AC
20022 end if;
20023
758c442c
GD
20024 -- The flag Is_Tagged_Type might have already been set by
20025 -- Find_Type_Name if it detected an error for declaration T. This
20026 -- arises in the case of private tagged types where the full view
20027 -- omits the word tagged.
996ae0b0 20028
758c442c
GD
20029 Is_Tagged :=
20030 Tagged_Present (Def)
20031 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
996ae0b0 20032
758c442c
GD
20033 Set_Is_Tagged_Type (T, Is_Tagged);
20034 Set_Is_Limited_Record (T, Limited_Present (Def));
20035
20036 -- Type is abstract if full declaration carries keyword, or if
20037 -- previous partial view did.
20038
fea9e956 20039 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
758c442c
GD
20040 or else Abstract_Present (Def));
20041
20042 else
2ba431e5 20043 Check_SPARK_Restriction ("interface is not allowed", N);
d8b962d8 20044
758c442c 20045 Is_Tagged := True;
950d3e7d 20046 Analyze_Interface_Declaration (T, Def);
88b32fc3
BD
20047
20048 if Present (Discriminant_Specifications (N)) then
20049 Error_Msg_N
20050 ("interface types cannot have discriminants",
20051 Defining_Identifier
20052 (First (Discriminant_Specifications (N))));
20053 end if;
758c442c
GD
20054 end if;
20055
20056 -- First pass: if there are self-referential access components,
20057 -- create the required anonymous access type declarations, and if
20058 -- need be an incomplete type declaration for T itself.
20059
fea9e956 20060 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
758c442c 20061
0791fbe9 20062 if Ada_Version >= Ada_2005
758c442c
GD
20063 and then Present (Interface_List (Def))
20064 then
ce2b6ba5 20065 Check_Interfaces (N, Def);
fea9e956 20066
758c442c 20067 declare
88b32fc3 20068 Ifaces_List : Elist_Id;
950d3e7d 20069
758c442c 20070 begin
88b32fc3
BD
20071 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
20072 -- already in the parents.
20073
ce2b6ba5
JM
20074 Collect_Interfaces
20075 (T => T,
20076 Ifaces_List => Ifaces_List,
20077 Exclude_Parents => True);
88b32fc3 20078
ce2b6ba5 20079 Set_Interfaces (T, Ifaces_List);
758c442c
GD
20080 end;
20081 end if;
20082
20083 -- Records constitute a scope for the component declarations within.
20084 -- The scope is created prior to the processing of these declarations.
20085 -- Discriminants are processed first, so that they are visible when
20086 -- processing the other components. The Ekind of the record type itself
20087 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
20088
20089 -- Enter record scope
20090
2b73cf68 20091 Push_Scope (T);
996ae0b0
RK
20092
20093 -- If an incomplete or private type declaration was already given for
20094 -- the type, then this scope already exists, and the discriminants have
20095 -- been declared within. We must verify that the full declaration
20096 -- matches the incomplete one.
20097
fbf5a39b 20098 Check_Or_Process_Discriminants (N, T, Prev);
996ae0b0
RK
20099
20100 Set_Is_Constrained (T, not Has_Discriminants (T));
20101 Set_Has_Delayed_Freeze (T, True);
20102
20103 -- For tagged types add a manually analyzed component corresponding
20104 -- to the component _tag, the corresponding piece of tree will be
20105 -- expanded as part of the freezing actions if it is not a CPP_Class.
20106
20107 if Is_Tagged then
ffe9aba8
AC
20108
20109 -- Do not add the tag unless we are in expansion mode
996ae0b0
RK
20110
20111 if Expander_Active then
20112 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
20113 Enter_Name (Tag_Comp);
20114
7d7af38a 20115 Set_Ekind (Tag_Comp, E_Component);
996ae0b0 20116 Set_Is_Tag (Tag_Comp);
758c442c 20117 Set_Is_Aliased (Tag_Comp);
996ae0b0
RK
20118 Set_Etype (Tag_Comp, RTE (RE_Tag));
20119 Set_DT_Entry_Count (Tag_Comp, No_Uint);
20120 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
20121 Init_Component_Location (Tag_Comp);
758c442c
GD
20122
20123 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
fea9e956 20124 -- implemented interfaces.
758c442c 20125
ce2b6ba5 20126 if Has_Interfaces (T) then
fea9e956
ES
20127 Add_Interface_Tag_Components (N, T);
20128 end if;
996ae0b0
RK
20129 end if;
20130
20131 Make_Class_Wide_Type (T);
ef2a63ba 20132 Set_Direct_Primitive_Operations (T, New_Elmt_List);
996ae0b0
RK
20133 end if;
20134
ef2a63ba
JM
20135 -- We must suppress range checks when processing record components in
20136 -- the presence of discriminants, since we don't want spurious checks to
20137 -- be generated during their analysis, but Suppress_Range_Checks flags
20138 -- must be reset the after processing the record definition.
996ae0b0 20139
88b32fc3
BD
20140 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
20141 -- couldn't we just use the normal range check suppression method here.
20142 -- That would seem cleaner ???
20143
fbf5a39b
AC
20144 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
20145 Set_Kill_Range_Checks (T, True);
20146 Record_Type_Definition (Def, Prev);
20147 Set_Kill_Range_Checks (T, False);
20148 else
20149 Record_Type_Definition (Def, Prev);
996ae0b0
RK
20150 end if;
20151
20152 -- Exit from record scope
20153
20154 End_Scope;
758c442c 20155
88b32fc3
BD
20156 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
20157 -- the implemented interfaces and associate them an aliased entity.
20158
20159 if Is_Tagged
758c442c
GD
20160 and then not Is_Empty_List (Interface_List (Def))
20161 then
ce2b6ba5 20162 Derive_Progenitor_Subprograms (T, T);
758c442c 20163 end if;
d3820795
JM
20164
20165 Check_Function_Writable_Actuals (N);
996ae0b0
RK
20166 end Record_Type_Declaration;
20167
20168 ----------------------------
20169 -- Record_Type_Definition --
20170 ----------------------------
20171
fbf5a39b 20172 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
996ae0b0
RK
20173 Component : Entity_Id;
20174 Ctrl_Components : Boolean := False;
fbf5a39b
AC
20175 Final_Storage_Only : Boolean;
20176 T : Entity_Id;
996ae0b0
RK
20177
20178 begin
fbf5a39b
AC
20179 if Ekind (Prev_T) = E_Incomplete_Type then
20180 T := Full_View (Prev_T);
20181 else
20182 T := Prev_T;
20183 end if;
20184
2ba431e5
YM
20185 -- In SPARK, tagged types and type extensions may only be declared in
20186 -- the specification of library unit packages.
d8b962d8
AC
20187
20188 if Present (Def) and then Is_Tagged_Type (T) then
20189 declare
20190 Typ : Node_Id;
20191 Ctxt : Node_Id;
176dadf6 20192
d8b962d8
AC
20193 begin
20194 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
20195 Typ := Parent (Def);
20196 else
20197 pragma Assert
20198 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
20199 Typ := Parent (Parent (Def));
20200 end if;
20201
20202 Ctxt := Parent (Typ);
20203
20204 if Nkind (Ctxt) = N_Package_Body
20205 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
20206 then
2ba431e5 20207 Check_SPARK_Restriction
d8b962d8 20208 ("type should be defined in package specification", Typ);
176dadf6 20209
d8b962d8 20210 elsif Nkind (Ctxt) /= N_Package_Specification
176dadf6 20211 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
d8b962d8 20212 then
2ba431e5 20213 Check_SPARK_Restriction
d8b962d8 20214 ("type should be defined in library unit package", Typ);
d8b962d8
AC
20215 end if;
20216 end;
20217 end if;
20218
fbf5a39b
AC
20219 Final_Storage_Only := not Is_Controlled (T);
20220
113a62d9 20221 -- Ada 2005: Check whether an explicit Limited is present in a derived
57193e09
TQ
20222 -- type declaration.
20223
20224 if Nkind (Parent (Def)) = N_Derived_Type_Definition
20225 and then Limited_Present (Parent (Def))
20226 then
20227 Set_Is_Limited_Record (T);
20228 end if;
20229
996ae0b0
RK
20230 -- If the component list of a record type is defined by the reserved
20231 -- word null and there is no discriminant part, then the record type has
20232 -- no components and all records of the type are null records (RM 3.7)
20233 -- This procedure is also called to process the extension part of a
20234 -- record extension, in which case the current scope may have inherited
20235 -- components.
20236
20237 if No (Def)
20238 or else No (Component_List (Def))
20239 or else Null_Present (Component_List (Def))
20240 then
d8b962d8 20241 if not Is_Tagged_Type (T) then
2ba431e5 20242 Check_SPARK_Restriction ("non-tagged record cannot be null", Def);
d8b962d8 20243 end if;
996ae0b0
RK
20244
20245 else
20246 Analyze_Declarations (Component_Items (Component_List (Def)));
20247
20248 if Present (Variant_Part (Component_List (Def))) then
2ba431e5 20249 Check_SPARK_Restriction ("variant part is not allowed", Def);
996ae0b0
RK
20250 Analyze (Variant_Part (Component_List (Def)));
20251 end if;
20252 end if;
20253
20254 -- After completing the semantic analysis of the record definition,
fea9e956 20255 -- record components, both new and inherited, are accessible. Set their
2b73cf68
JM
20256 -- kind accordingly. Exclude malformed itypes from illegal declarations,
20257 -- whose Ekind may be void.
996ae0b0
RK
20258
20259 Component := First_Entity (Current_Scope);
20260 while Present (Component) loop
2b73cf68
JM
20261 if Ekind (Component) = E_Void
20262 and then not Is_Itype (Component)
20263 then
996ae0b0
RK
20264 Set_Ekind (Component, E_Component);
20265 Init_Component_Location (Component);
20266 end if;
20267
20268 if Has_Task (Etype (Component)) then
20269 Set_Has_Task (T);
20270 end if;
20271
20272 if Ekind (Component) /= E_Component then
20273 null;
20274
80fa4617
EB
20275 -- Do not set Has_Controlled_Component on a class-wide equivalent
20276 -- type. See Make_CW_Equivalent_Type.
20277
20278 elsif not Is_Class_Wide_Equivalent_Type (T)
20279 and then (Has_Controlled_Component (Etype (Component))
20280 or else (Chars (Component) /= Name_uParent
20281 and then Is_Controlled (Etype (Component))))
996ae0b0
RK
20282 then
20283 Set_Has_Controlled_Component (T, True);
33931112
JM
20284 Final_Storage_Only :=
20285 Final_Storage_Only
20286 and then Finalize_Storage_Only (Etype (Component));
996ae0b0
RK
20287 Ctrl_Components := True;
20288 end if;
20289
20290 Next_Entity (Component);
20291 end loop;
20292
fea9e956
ES
20293 -- A Type is Finalize_Storage_Only only if all its controlled components
20294 -- are also.
996ae0b0
RK
20295
20296 if Ctrl_Components then
20297 Set_Finalize_Storage_Only (T, Final_Storage_Only);
20298 end if;
20299
fbf5a39b
AC
20300 -- Place reference to end record on the proper entity, which may
20301 -- be a partial view.
20302
996ae0b0 20303 if Present (Def) then
fbf5a39b 20304 Process_End_Label (Def, 'e', Prev_T);
996ae0b0
RK
20305 end if;
20306 end Record_Type_Definition;
20307
07fc65c4
GB
20308 ------------------------
20309 -- Replace_Components --
20310 ------------------------
20311
20312 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
20313 function Process (N : Node_Id) return Traverse_Result;
20314
20315 -------------
20316 -- Process --
20317 -------------
20318
20319 function Process (N : Node_Id) return Traverse_Result is
20320 Comp : Entity_Id;
20321
20322 begin
20323 if Nkind (N) = N_Discriminant_Specification then
20324 Comp := First_Discriminant (Typ);
07fc65c4
GB
20325 while Present (Comp) loop
20326 if Chars (Comp) = Chars (Defining_Identifier (N)) then
20327 Set_Defining_Identifier (N, Comp);
20328 exit;
20329 end if;
20330
20331 Next_Discriminant (Comp);
20332 end loop;
20333
20334 elsif Nkind (N) = N_Component_Declaration then
20335 Comp := First_Component (Typ);
07fc65c4
GB
20336 while Present (Comp) loop
20337 if Chars (Comp) = Chars (Defining_Identifier (N)) then
20338 Set_Defining_Identifier (N, Comp);
20339 exit;
20340 end if;
20341
20342 Next_Component (Comp);
20343 end loop;
20344 end if;
20345
20346 return OK;
20347 end Process;
20348
20349 procedure Replace is new Traverse_Proc (Process);
20350
20351 -- Start of processing for Replace_Components
20352
20353 begin
20354 Replace (Decl);
20355 end Replace_Components;
20356
20357 -------------------------------
20358 -- Set_Completion_Referenced --
20359 -------------------------------
20360
20361 procedure Set_Completion_Referenced (E : Entity_Id) is
20362 begin
20363 -- If in main unit, mark entity that is a completion as referenced,
20364 -- warnings go on the partial view when needed.
20365
20366 if In_Extended_Main_Source_Unit (E) then
20367 Set_Referenced (E);
20368 end if;
20369 end Set_Completion_Referenced;
20370
996ae0b0
RK
20371 ---------------------
20372 -- Set_Fixed_Range --
20373 ---------------------
20374
20375 -- The range for fixed-point types is complicated by the fact that we
20376 -- do not know the exact end points at the time of the declaration. This
20377 -- is true for three reasons:
20378
86f0e17a
AC
20379 -- A size clause may affect the fudging of the end-points.
20380 -- A small clause may affect the values of the end-points.
20381 -- We try to include the end-points if it does not affect the size.
996ae0b0 20382
86f0e17a
AC
20383 -- This means that the actual end-points must be established at the
20384 -- point when the type is frozen. Meanwhile, we first narrow the range
20385 -- as permitted (so that it will fit if necessary in a small specified
20386 -- size), and then build a range subtree with these narrowed bounds.
20387 -- Set_Fixed_Range constructs the range from real literal values, and
20388 -- sets the range as the Scalar_Range of the given fixed-point type entity.
996ae0b0 20389
a5b62485
AC
20390 -- The parent of this range is set to point to the entity so that it is
20391 -- properly hooked into the tree (unlike normal Scalar_Range entries for
20392 -- other scalar types, which are just pointers to the range in the
996ae0b0
RK
20393 -- original tree, this would otherwise be an orphan).
20394
20395 -- The tree is left unanalyzed. When the type is frozen, the processing
20396 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
20397 -- analyzed, and uses this as an indication that it should complete
20398 -- work on the range (it will know the final small and size values).
20399
20400 procedure Set_Fixed_Range
20401 (E : Entity_Id;
20402 Loc : Source_Ptr;
20403 Lo : Ureal;
20404 Hi : Ureal)
20405 is
20406 S : constant Node_Id :=
20407 Make_Range (Loc,
20408 Low_Bound => Make_Real_Literal (Loc, Lo),
20409 High_Bound => Make_Real_Literal (Loc, Hi));
996ae0b0
RK
20410 begin
20411 Set_Scalar_Range (E, S);
20412 Set_Parent (S, E);
86f0e17a
AC
20413
20414 -- Before the freeze point, the bounds of a fixed point are universal
20415 -- and carry the corresponding type.
20416
20417 Set_Etype (Low_Bound (S), Universal_Real);
20418 Set_Etype (High_Bound (S), Universal_Real);
996ae0b0
RK
20419 end Set_Fixed_Range;
20420
996ae0b0
RK
20421 ----------------------------------
20422 -- Set_Scalar_Range_For_Subtype --
20423 ----------------------------------
20424
20425 procedure Set_Scalar_Range_For_Subtype
07fc65c4
GB
20426 (Def_Id : Entity_Id;
20427 R : Node_Id;
20428 Subt : Entity_Id)
996ae0b0
RK
20429 is
20430 Kind : constant Entity_Kind := Ekind (Def_Id);
71d9e9f2 20431
996ae0b0 20432 begin
199c6a10
AC
20433 -- Defend against previous error
20434
20435 if Nkind (R) = N_Error then
20436 return;
20437 end if;
20438
996ae0b0
RK
20439 Set_Scalar_Range (Def_Id, R);
20440
20441 -- We need to link the range into the tree before resolving it so
20442 -- that types that are referenced, including importantly the subtype
20443 -- itself, are properly frozen (Freeze_Expression requires that the
20444 -- expression be properly linked into the tree). Of course if it is
20445 -- already linked in, then we do not disturb the current link.
20446
20447 if No (Parent (R)) then
20448 Set_Parent (R, Def_Id);
20449 end if;
20450
20451 -- Reset the kind of the subtype during analysis of the range, to
20452 -- catch possible premature use in the bounds themselves.
20453
20454 Set_Ekind (Def_Id, E_Void);
07fc65c4 20455 Process_Range_Expr_In_Decl (R, Subt);
996ae0b0 20456 Set_Ekind (Def_Id, Kind);
996ae0b0
RK
20457 end Set_Scalar_Range_For_Subtype;
20458
fbf5a39b
AC
20459 --------------------------------------------------------
20460 -- Set_Stored_Constraint_From_Discriminant_Constraint --
20461 --------------------------------------------------------
20462
20463 procedure Set_Stored_Constraint_From_Discriminant_Constraint
20464 (E : Entity_Id)
20465 is
20466 begin
71d9e9f2 20467 -- Make sure set if encountered during Expand_To_Stored_Constraint
fbf5a39b
AC
20468
20469 Set_Stored_Constraint (E, No_Elist);
20470
20471 -- Give it the right value
20472
20473 if Is_Constrained (E) and then Has_Discriminants (E) then
20474 Set_Stored_Constraint (E,
20475 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
20476 end if;
fbf5a39b
AC
20477 end Set_Stored_Constraint_From_Discriminant_Constraint;
20478
996ae0b0
RK
20479 -------------------------------------
20480 -- Signed_Integer_Type_Declaration --
20481 -------------------------------------
20482
20483 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
20484 Implicit_Base : Entity_Id;
20485 Base_Typ : Entity_Id;
20486 Lo_Val : Uint;
20487 Hi_Val : Uint;
20488 Errs : Boolean := False;
20489 Lo : Node_Id;
20490 Hi : Node_Id;
20491
20492 function Can_Derive_From (E : Entity_Id) return Boolean;
20493 -- Determine whether given bounds allow derivation from specified type
20494
20495 procedure Check_Bound (Expr : Node_Id);
20496 -- Check bound to make sure it is integral and static. If not, post
20497 -- appropriate error message and set Errs flag
20498
fbf5a39b
AC
20499 ---------------------
20500 -- Can_Derive_From --
20501 ---------------------
20502
71d9e9f2
ES
20503 -- Note we check both bounds against both end values, to deal with
20504 -- strange types like ones with a range of 0 .. -12341234.
20505
996ae0b0
RK
20506 function Can_Derive_From (E : Entity_Id) return Boolean is
20507 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
20508 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
996ae0b0 20509 begin
996ae0b0
RK
20510 return Lo <= Lo_Val and then Lo_Val <= Hi
20511 and then
20512 Lo <= Hi_Val and then Hi_Val <= Hi;
20513 end Can_Derive_From;
20514
fbf5a39b
AC
20515 -----------------
20516 -- Check_Bound --
20517 -----------------
20518
996ae0b0
RK
20519 procedure Check_Bound (Expr : Node_Id) is
20520 begin
20521 -- If a range constraint is used as an integer type definition, each
20522 -- bound of the range must be defined by a static expression of some
20523 -- integer type, but the two bounds need not have the same integer
20524 -- type (Negative bounds are allowed.) (RM 3.5.4)
20525
20526 if not Is_Integer_Type (Etype (Expr)) then
20527 Error_Msg_N
20528 ("integer type definition bounds must be of integer type", Expr);
20529 Errs := True;
20530
20531 elsif not Is_OK_Static_Expression (Expr) then
fbf5a39b
AC
20532 Flag_Non_Static_Expr
20533 ("non-static expression used for integer type bound!", Expr);
996ae0b0
RK
20534 Errs := True;
20535
20536 -- The bounds are folded into literals, and we set their type to be
20537 -- universal, to avoid typing difficulties: we cannot set the type
20538 -- of the literal to the new type, because this would be a forward
20539 -- reference for the back end, and if the original type is user-
20540 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
20541
20542 else
20543 if Is_Entity_Name (Expr) then
fbf5a39b 20544 Fold_Uint (Expr, Expr_Value (Expr), True);
996ae0b0
RK
20545 end if;
20546
20547 Set_Etype (Expr, Universal_Integer);
20548 end if;
20549 end Check_Bound;
20550
20551 -- Start of processing for Signed_Integer_Type_Declaration
20552
20553 begin
20554 -- Create an anonymous base type
20555
20556 Implicit_Base :=
20557 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
20558
20559 -- Analyze and check the bounds, they can be of any integer type
20560
20561 Lo := Low_Bound (Def);
20562 Hi := High_Bound (Def);
996ae0b0 20563
ce9e9122 20564 -- Arbitrarily use Integer as the type if either bound had an error
996ae0b0 20565
ce9e9122
RD
20566 if Hi = Error or else Lo = Error then
20567 Base_Typ := Any_Integer;
20568 Set_Error_Posted (T, True);
996ae0b0 20569
ce9e9122 20570 -- Here both bounds are OK expressions
996ae0b0 20571
ce9e9122
RD
20572 else
20573 Analyze_And_Resolve (Lo, Any_Integer);
20574 Analyze_And_Resolve (Hi, Any_Integer);
996ae0b0 20575
ce9e9122
RD
20576 Check_Bound (Lo);
20577 Check_Bound (Hi);
996ae0b0 20578
ce9e9122
RD
20579 if Errs then
20580 Hi := Type_High_Bound (Standard_Long_Long_Integer);
20581 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
20582 end if;
996ae0b0 20583
ce9e9122 20584 -- Find type to derive from
996ae0b0 20585
ce9e9122
RD
20586 Lo_Val := Expr_Value (Lo);
20587 Hi_Val := Expr_Value (Hi);
996ae0b0 20588
ce9e9122
RD
20589 if Can_Derive_From (Standard_Short_Short_Integer) then
20590 Base_Typ := Base_Type (Standard_Short_Short_Integer);
996ae0b0 20591
ce9e9122
RD
20592 elsif Can_Derive_From (Standard_Short_Integer) then
20593 Base_Typ := Base_Type (Standard_Short_Integer);
20594
20595 elsif Can_Derive_From (Standard_Integer) then
20596 Base_Typ := Base_Type (Standard_Integer);
20597
20598 elsif Can_Derive_From (Standard_Long_Integer) then
20599 Base_Typ := Base_Type (Standard_Long_Integer);
20600
20601 elsif Can_Derive_From (Standard_Long_Long_Integer) then
20602 Base_Typ := Base_Type (Standard_Long_Long_Integer);
20603
20604 else
20605 Base_Typ := Base_Type (Standard_Long_Long_Integer);
20606 Error_Msg_N ("integer type definition bounds out of range", Def);
20607 Hi := Type_High_Bound (Standard_Long_Long_Integer);
20608 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
20609 end if;
996ae0b0
RK
20610 end if;
20611
20612 -- Complete both implicit base and declared first subtype entities
20613
b69cd36a 20614 Set_Etype (Implicit_Base, Base_Typ);
996ae0b0
RK
20615 Set_Size_Info (Implicit_Base, (Base_Typ));
20616 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
20617 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
20618
20619 Set_Ekind (T, E_Signed_Integer_Subtype);
20620 Set_Etype (T, Implicit_Base);
20621
e19fd0bd 20622 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
00332244 20623
996ae0b0
RK
20624 Set_Size_Info (T, (Implicit_Base));
20625 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
20626 Set_Scalar_Range (T, Def);
20627 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
20628 Set_Is_Constrained (T);
996ae0b0
RK
20629 end Signed_Integer_Type_Declaration;
20630
20631end Sem_Ch3;